Advertisement
Redxone

[Computercraft] - Console Demo

May 29th, 2016
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.47 KB | None | 0 0
  1. local sbc = term.setBackgroundColor
  2. local stc = term.setTextColor
  3. local scp = term.setCursorPos
  4. local pa = paintutils
  5. local running = true
  6. local w,h = term.getSize()
  7. local states = {
  8.     inHelp = false,
  9.     inMain = true,
  10.     inMenu = false,
  11. }
  12. function expandBox(fx,fy,tx,ty,col)
  13.     for i =1, tx-fx do
  14.       pa.drawFilledBox(fx,fy,i,ty,col)
  15.       sleep(0.1)
  16.     end
  17. end
  18. function despandBox(fx,fy,tx,ty,col)
  19.     for i =fx-tx, 1 do
  20.       pa.drawFilledBox(fx-i,fy,tx,ty,col)
  21.       sleep(0.1)
  22.     end
  23. end
  24. function cwrite(t,y)
  25.     scp((w/2) - #t/2, y)
  26.     write(t)
  27. end
  28. function drawhelp()
  29.     if(not states.inHelp)then
  30.         despandBox(1,2,12,19,colors.white)
  31.         states.inMain = true
  32.         return
  33.     end
  34.     scp(1,1)
  35.     sbc(colors.gray)
  36.     term.clearLine()
  37.     cwrite("Help Menu",1)
  38.     expandBox(1,2,12,19,colors.gray)
  39.     drawbuttons()
  40. end
  41.  
  42. function drawmenu()
  43.     if(not states.inMenu)then
  44.         despandBox(1,2,12,19,colors.white)
  45.     else
  46.         expandBox(1,2,12,19,colors.gray)
  47.         drawbuttons()
  48.     end
  49. end
  50.  
  51. local buttons = {
  52.     {n="Start",x=1,y=1,depends='inMain' or 'inMenu',tc=colors.white,bc=colors.gray,fbc=colors.black,ftc=colors.lightGray,click=function()
  53.          states.inMenu = not states.inMenu
  54.          drawmenu()
  55.     end},
  56.     {n="Help      ",x=2,y=3,depends='inMenu',tc=colors.white,bc=colors.gray,fbc=colors.black,ftc=colors.lightGray,click=function()
  57.         states.inHelp = true
  58.         states.inMain = false
  59.         states.inMenu = false
  60.         drawmenu()
  61.         drawhelp()
  62.     end},
  63.     {n="Reboot    ",x=2,y=4,depends='inMenu',tc=colors.white,bc=colors.gray,fbc=colors.black,ftc=colors.lightBlue,click=function()
  64.         os.reboot()
  65.     end},
  66.     {n="Quit      ",x=2,y=5,depends='inMenu',tc=colors.white,bc=colors.gray,fbc=colors.black,ftc=colors.red,click=function()
  67.     end},
  68.     {n="Topic 1   ",x=2,y=3,depends='inHelp',tc=colors.white,bc=colors.gray,fbc=colors.black,ftc=colors.lightGray,click=function()
  69.     end},
  70.     {n="Topic 2   ",x=2,y=4,depends='inHelp',tc=colors.white,bc=colors.gray,fbc=colors.black,ftc=colors.lightBlue,click=function()
  71.     end},
  72.     {n="Quit      ",x=2,y=5,depends='inHelp',tc=colors.white,bc=colors.gray,fbc=colors.black,ftc=colors.red,click=function()
  73.          states.inHelp = false
  74.          drawhelp()
  75.          redraw()
  76.          drawbuttons()
  77.          states.inMenu = not states.inMenu
  78.          drawmenu()
  79.     end},
  80. }
  81.  
  82. function drawbuttons()
  83.     -- Draw buttons based on state dependances
  84.     for i = 1, #buttons do
  85.         if(buttons[i].depends == nil or states[buttons[i].depends])then
  86.             -- Draw button
  87.             sbc(buttons[i].bc)
  88.             stc(buttons[i].tc)
  89.             scp(buttons[i].x,buttons[i].y)
  90.             write(buttons[i].n)
  91.         end
  92.     end
  93. end
  94.  
  95. function flashbutton(n)
  96.     sbc(buttons[n].fbc)
  97.     stc(buttons[n].ftc)
  98.     scp(buttons[n].x,buttons[n].y)
  99.     write(buttons[n].n)
  100.     sleep(0.2)
  101.     sbc(buttons[n].bc)
  102.     stc(buttons[n].tc)
  103.     scp(buttons[n].x,buttons[n].y)
  104.     write(buttons[n].n)
  105. end
  106. function updatebuttons(e)
  107.     if(e[1] == "mouse_click")then
  108.     -- Update buttons based on state dependances
  109.         for i = 1, #buttons do
  110.             if(buttons[i].depends == nil or states[buttons[i].depends])then
  111.                 -- Update button
  112.                 local x,y = e[3], e[4]
  113.                 if(x >= buttons[i].x and x <= buttons[i].x+#buttons[i].n and y == buttons[i].y)then
  114.                      flashbutton(i)
  115.                      buttons[i].click()
  116.                      return
  117.                 end
  118.             end
  119.         end
  120.     end
  121. end
  122. function redraw()
  123.     sbc(colors.white)
  124.     term.clear()
  125.     scp(1,1)
  126.     sbc(colors.gray)
  127.     term.clearLine()
  128.     scp(1,1)
  129.     drawbuttons()
  130. end
  131.  
  132. function loop()
  133.     redraw()
  134.     while running do
  135.         -- Manage screen states (States that change the entire screen layout)
  136.         local e = {os.pullEvent()}
  137.         updatebuttons(e)
  138.     end
  139. end
  140. loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement