Advertisement
AquaJD

Budj demo

Sep 14th, 2017
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.08 KB | None | 0 0
  1. --[[
  2.    
  3.     Budj Demo
  4.    
  5.     By Dave-ee Jones
  6.  
  7. --]]
  8.  
  9. term.setBackgroundColor(colors.black)
  10. term.clear()
  11.  
  12. -- Prepare Budj
  13. os.loadAPI("/budj")
  14.  
  15. -- Budj stuff
  16. local board1 = budj.BOARD:new("Main")
  17. local button1 = budj.BUTTON:new(board1,"Button",2,2,14,6,colours.white,colors.blue,colors.lightBlue,true)
  18. local cb1 = budj.CHECKBOX:new(board1,"Checkbox",2,9,colours.white,colours.black,colours.white,colours.blue,colours.lightBlue)
  19.  
  20. -- The function to be assigned to button1/checkbox
  21. -- Swaps the label of the given object
  22. local function swapLabel(o)
  23.   if o.LABEL == "Button" then
  24.     o:assignLabel("Button 2")
  25.   elseif o.LABEL == "Button 2" then
  26.     o:assignLabel("Button")
  27.   elseif o.LABEL == "Checkbox" then
  28.     o:assignLabel("Checkbox 2")
  29.   elseif o.LABEL == "Checkbox 2" then
  30.     o:assignLabel("Checkbox")
  31.   end
  32. end
  33.  
  34. -- Another function to be assigned to button1
  35. -- Toggles checkbox being drawn or not
  36. local function undrawObject(o)
  37.     if o.DRAWN then
  38.         o:undraw()
  39.     else
  40.         o:draw()
  41.     end
  42. end
  43.  
  44. -- More Budj stuff - but adjusting this time
  45. button1:alignLabel("centered")
  46. button1:assignFunction(function() undrawObject(cb1) end) -- Now, when you click the button it undraws the checkbox
  47. cb1:assignFunction(function() swapLabel(button1) end) -- When you toggle the checkbox it changes button1's label
  48. board1:draw()
  49.  
  50. while true do
  51.   -- Event handling, proudly supported by Budj
  52.   local event = { os.pullEvent() }
  53.   if event[1] == "key" and event[2] == keys.enter then
  54.     break
  55.   end
  56.   local status, object, returned = budj.handleEvent(unpack(event))
  57.   -- Ignore this clearing stuff, not important
  58.   term.setCursorPos(1,12)
  59.   term.clearLine()
  60.   term.setCursorPos(1,13)
  61.   term.clearLine()
  62.   term.setCursorPos(1,14)
  63.   term.clearLine()
  64.   term.setCursorPos(1,15)
  65.   term.clearLine()
  66.   term.setCursorPos(1,12)
  67.   print(status)
  68.   -- It begins!
  69.   if status then
  70.     print("Object label: "..object.LABEL)
  71.     if status ~= "wait" then
  72.       print("Object's function returned: "..tostring(returned[1]))
  73.       print("Object returned: "..tostring(returned[2]))
  74.     end
  75.   end
  76. end
  77.  
  78. board1:undraw()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement