ndfjay

Computercraft Basic | Advanced OS/UI Tutorial | Part 1

Dec 21st, 2014
1,875
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.32 KB | None | 0 0
  1. --Variable
  2. local osVer = "Alpha 1.0"
  3. local w,h = term.getSize()
  4. local brunning = true
  5. local slc = 0
  6. local _mtext = 1
  7. local _mback = 128
  8.  
  9. --Tables
  10. local menu = {
  11.     [""] = {tcol = _mtext; bcol = _mback; x = 1; xx = 7; y = 1; cmd = function() slc = 1 end};
  12.     ["            "] = {tcol = _mtext; bcol = _mback; x = 1; xx = 12; y = 2; cmd = function() end};
  13.     [" Programs > "] = {tcol = _mtext; bcol = _mback; x = 1; xx = 12; y = 3; cmd = function() end};
  14.     [" Settings > "] = {tcol = _mtext; bcol = _mback; x = 1; xx = 12; y = 4; cmd = function() end};
  15.     [" Lua Prompt "] = {tcol = _mtext; bcol = _mback; x = 1; xx = 12; y = 5; cmd = function() end};
  16.     [" Quit       "] = {tcol = _mtext; bcol = _mback; x = 1; xx = 12; y = 6; cmd = function() term.setBackgroundColor(colors.black) term.setCursorPos(1,1) term.clear() brunning = false end};
  17.     [" Shutdown   "] = {tcol = _mtext; bcol = _mback; x = 1; xx = 12; y = 7; cmd = function() os.shutdown() end};
  18.     [" Logoff     "] = {tcol = _mtext; bcol = _mback; x = 1; xx = 12; y = 8; cmd = function() end};
  19.     [" Reboot     "] = {tcol = _mtext; bcol = _mback; x = 1; xx = 12; y = 9; cmd = function() os.reboot() end};
  20.     ["            "] = {tcol = _mtext; bcol = _mback; x = 1; xx = 12; y = 10; cmd = function() end};
  21. }
  22. --Functions
  23. local function tb_Draw()
  24.     term.setCursorPos(1,1)
  25.     term.setBackgroundColor(8)
  26.     term.setTextColor(1)
  27.     term.clearLine()
  28.     print("[Begin]")
  29.     term.setCursorPos(math.floor(w)-#osVer, 1)
  30.     print(osVer)
  31. end
  32.  
  33. local function clear(bcol)
  34.     term.setBackgroundColor(bcol)
  35.     term.clear()
  36. end
  37.  
  38. local function drawMenu()
  39.     for k,v in pairs(menu) do
  40.         term.setBackgroundColor(v.bcol)
  41.         term.setTextColor(v.tcol)
  42.         term.setCursorPos(v.x, v.y)
  43.         print(k)
  44.     end
  45. end
  46.  
  47. local function menuClick()
  48.     for k,v in pairs(menu) do
  49.         if  e[1] == "mouse_click" then
  50.             if e[2] == 1 then
  51.                 if e[3] >= v.x and e[3] <= v.xx and e[4] == v.y then
  52.                     v.cmd()
  53.                     else slc = 0 clear(colors.black) tb_Draw()
  54.                 end
  55.             end
  56.         end
  57.     end
  58. end
  59.  
  60. --Main code
  61. clear(colors.black)
  62. tb_Draw()
  63. while brunning do
  64. e = {os.pullEvent()}
  65.     if slc == 0 then
  66.         clear(colors.black)
  67.         tb_Draw()
  68.         if  e[1] == "mouse_click" then
  69.             if e[2] == 1 then
  70.                 if e[3] >= 1 and e[3] <= 7 and e[4] == 1 then
  71.                     slc = 1
  72.                     drawMenu()
  73.                 end
  74.             end
  75.         end
  76.  
  77.     elseif slc == 1 then
  78.         drawMenu()
  79.         menuClick()
  80.     end
  81. end
Advertisement
Add Comment
Please, Sign In to add comment