Advertisement
goppi

Untitled

Aug 22nd, 2019
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.65 KB | None | 0 0
  1. --примерные данные кнопки
  2. --код запускать на corona sdk старге 2 поколения
  3. someJSON = [[{
  4.         "type" : "Button",
  5.         "parent" : "PerentControlName",
  6.         "text" : "Кнопка 1",
  7.         "name" : "Button1",
  8.         "position" : {
  9.             "x" : 100,
  10.             "y" : 100
  11.         },
  12.         "on_click" : "onClickMethodName",
  13.         "children" : ["path/to/first/child/element.json","path/to/last/child/element.json"]
  14.         }]]
  15.  
  16. function CreateUIElement( data )
  17.  
  18.     local json = require( "json" ) -- подгружаем модуль жижсона
  19.     local widget = require( "widget" ) -- либа для кнопок от coronaSDK (годный контент огромные возможности кастомизации )
  20.  
  21.     local decoded, pos, msg = json.decode( data ) -- создаем таблицу из жижсона
  22.  
  23.     if not decoded then -- отрабочик ошибок
  24.         print( "Decode failed at " .. tostring(pos) .. ": " .. tostring(msg) )
  25.     else
  26.  
  27.         newObj = {}
  28.  
  29.         newObj.type = decoded.type
  30.         newObj.parent = decoded.parent
  31.  
  32.         newObj.children = {}
  33.  
  34.         for i,v in ipairs(decoded.children) do
  35.             newObj.children[i] = v
  36.         end
  37.  
  38.         local newObj = widget.newButton(
  39.             {
  40.                 label = decoded.name,
  41.                 onEvent = decoded.on_click,
  42.                 emboss = false,
  43.                 shape = "roundedRect",
  44.                 width = 200,
  45.                 height = 40,
  46.                 cornerRadius = 2,
  47.                 fillColor = { default={1,0,0,1}, over={1,0.1,0.2,1} }
  48.             }
  49.         )
  50.          
  51.         newObj.x = decoded.position.x
  52.         newObj.y = decoded.position.y
  53.          
  54.         newObj:setLabel( decoded.text )
  55.     end
  56.     return newObj
  57. end
  58.  
  59. myBut = CreateUIElement( someJSON )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement