newcat

GUI API Tutorial Example Program

Aug 24th, 2014
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.74 KB | None | 0 0
  1. os.unloadAPI("gui")
  2. os.loadAPI("gui")
  3.  
  4. function waitMouse()
  5.     while true do
  6.         local ev,mb,x,y = os.pullEvent("mouse_click")
  7.         if (mb == 1 and gui.detect(x,y, false) ~= nil) then
  8.             return gui.detect(x,y, false)
  9.         end
  10.     end
  11. end
  12.  
  13. function firstFunction()
  14.     term.setBackgroundColor(colors.black)
  15.     term.clear()
  16.    
  17.     local progBar = gui.createBar("bar")
  18.     progBar:draw(15, 8, 20, colors.white, colors.blue, true, colors.black, colors.white)
  19.     for i = 1, 100 do
  20.         progBar:update(i)
  21.         sleep(0.05)
  22.     end
  23.    
  24.     local dBox = gui.createDialogueBox("Finished!", "The progressbar is full!", "ok")
  25.     dBox:draw(12,6,5,colors.lightGray, colors.gray, colors.white)
  26. end
  27.  
  28. function secondFunction()
  29.     term.setBackgroundColor(colors.black)
  30.     term.clear()
  31.    
  32.     local userinput = gui.newTextBox(20,10,20, colors.white, colors.black)
  33.    
  34.     local dBox = gui.createDialogueBox("Really?", {"Did you really write this:", string.char(34) .. userinput .. string.char(34) .. "?"}, "yn")
  35.     local result = dBox:draw(12,6,6,colors.lightGray, colors.gray, colors.white)
  36.     if result == true then
  37.         print("Great. Awesome!")
  38.     else
  39.         print("Not cool! Really not cool!")
  40.     end
  41.     sleep(3)
  42. end
  43.  
  44. function main()
  45.     term.setBackgroundColor(colors.black)
  46.     term.setTextColor(colors.white)
  47.     term.clear()
  48.     term.setCursorPos(1,1)
  49.    
  50.     local firstButton = gui.createButton("First ", firstFunction)
  51.     local secondButton = gui.createButton("Second", secondFunction)
  52.    
  53.     firstButton:draw(20, 3, 3, colors.red, colors.white)
  54.     secondButton:draw(20, 9, 3, colors.green, colors.white)
  55.    
  56.     local clicked = waitMouse()
  57.    
  58.     firstButton:remove()
  59.     secondButton:remove()
  60.    
  61.     if clicked == "First " then
  62.         firstFunction()
  63.     elseif clicked == "Second" then
  64.         secondFunction()
  65.     end
  66. end
  67.  
  68. while true do
  69.     main()
  70. end
Advertisement
Add Comment
Please, Sign In to add comment