Imgoodisher

Gui Editor

May 18th, 2013
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 17.92 KB | None | 0 0
  1. local args = {...}
  2. local file_name = args[1] or nil
  3.  
  4. local tw, th = term.getSize()
  5.  
  6. local gui_objs
  7. local gui_menu = "default"
  8.  
  9. local preview = false
  10.  
  11. local function w(obj)
  12.     return obj.a - obj.x + 1
  13. end
  14. local function h(obj)
  15.     return obj.b - obj.y + 1
  16. end
  17.  
  18. function twrite(str) -- From the Googol API
  19.     local tbl = {}
  20.     for char in string.gmatch(str, ".") do
  21.         table.insert(tbl, char)
  22.     end
  23.    
  24.     local skip = false
  25.     for i=1, #tbl do
  26.         if tbl[i] == "&" then
  27.             if string.match(tbl[i+1], "%x") then
  28.                 skip = true
  29.                 term.setTextColor(2^tonumber(tbl[i+1], 16))
  30.             else
  31.                 write("&")
  32.             end
  33.            
  34.         elseif tbl[i] == "#" then
  35.             if string.match(tbl[i+1], "%x") then
  36.                 skip = true
  37.                 term.setBackgroundColor(2^tonumber(tbl[i+1], 16))
  38.             else
  39.                 write("#")
  40.             end
  41.            
  42.         elseif not skip then
  43.             write(tbl[i])
  44.         else
  45.             skip = false
  46.         end
  47.     end
  48.     --term.setTextColor(colors.white)
  49. end
  50.  
  51. function tlen(str)
  52.     local len = 0
  53.     local tbl = {}
  54.     for char in string.gmatch(str, ".") do
  55.         table.insert(tbl, char)
  56.     end
  57.    
  58.     local skip = false
  59.     for i=1, #tbl do
  60.         if tbl[i] == "&" then
  61.             if string.match(tbl[i+1], "%x") then
  62.                 skip = true
  63.             else
  64.                 len = len + 1
  65.             end
  66.            
  67.         elseif tbl[i] == "#" then
  68.             if string.match(tbl[i+1], "%x") then
  69.                 skip = true
  70.             else
  71.                 len = len + 1
  72.             end
  73.            
  74.         elseif not skip then
  75.             len = len + 1
  76.         else
  77.             skip = false
  78.         end
  79.     end
  80.     return len
  81. end
  82.  
  83. local function newObject(x, y, a, b, bc, tc, menu)
  84.     if not menu then menu = gui_menu end
  85.     local obj = {x=x, y=y, a=a, b=b, bc=bc, tc=tc}
  86.     local i = #gui_objs[menu] + 1
  87.     gui_objs[menu][i] = obj
  88.     return i
  89. end
  90.  
  91. local function newImage(x, y, data)
  92.     local w = 0
  93.  
  94.     for ind,val in pairs(data) do
  95.         for i = #val, 1, -1 do
  96.             if val[i] == 0 then
  97.                 data[ind][i] = nil
  98.             else
  99.                 break
  100.             end
  101.         end
  102.         w = (#val > w and #val) or w
  103.     end
  104.    
  105.     for i = #data, 1, -1 do
  106.         if #data[i] == 0 then
  107.             data[i] = nil
  108.         else
  109.             break
  110.         end
  111.     end
  112.  
  113.     local h = #data
  114.  
  115.     local i = #gui_objs[gui_menu] + 1
  116.     gui_objs[gui_menu][i] = {x=x, y=y, a=x+w, b=y+h, d=data, nfp=1}
  117.     return i
  118. end
  119.  
  120. local function wordwrap(text, w)
  121.     local lines = {} -- stores all the lines
  122.     for match in string.gmatch(text, "[^\n]+") do -- seperate the block of text into seperate lines
  123.         local len = 0
  124.         local line = ""
  125.         for word in string.gmatch(match, "[^%s]+") do -- word wrap if the line is too long
  126.             word = word .. " "
  127.             if tlen(line..word) > w then
  128.                 table.insert(lines, line)
  129.                 line = ""
  130.             end
  131.             line = line .. word
  132.             while tlen(line) > w do
  133.                 table.insert(lines, line:sub(1, w))
  134.                 line = line:sub(w+1)
  135.             end
  136.         end
  137.         table.insert(lines, line)
  138.     end
  139.     local delete = {}
  140.     for i,v in pairs(lines) do
  141.         lines[i] = v:match("^%s*(.-)%s*$")
  142.         if lines[i] == "" then
  143.             table.insert(delete, i)
  144.         end
  145.     end
  146.     for i,v in pairs(delete) do lines[v] = nil end
  147.     return lines
  148. end
  149.  
  150. local function drawGui()
  151.     for i,v in pairs(gui_objs[gui_menu]) do
  152.         if not v.nfp then
  153.             term.setBackgroundColor(v.bc)
  154.             term.setTextColor(v.tc)
  155.             for y = v.y, v.b do
  156.                 term.setCursorPos(v.x, y)
  157.                 term.write(string.rep(" ", w(v)))
  158.             end
  159.             if v.t then
  160.                 v.t = v.t:match("^%s*(.+)%s*$") or v.t
  161.                 local lines = wordwrap(v.t, w(v) - 2)
  162.                 for i2,line in pairs(lines) do
  163.                     term.setCursorPos(v.x + math.floor(w(v)/2-tlen(line)/2), v.y + h(v)/2 - #lines/2 + i2 - 1) -- ((h(v)%2==1 and h(v)/2) or ((h(v)/2)-1))
  164.                     twrite(line)
  165.                 end
  166.             end
  167.  
  168.             if not preview then
  169.                 if v.bc == colors.lightGray then
  170.                     term.setTextColor(colors.gray)
  171.                 else
  172.                     term.setTextColor(colors.lightGray)
  173.                 end
  174.                 term.setCursorPos(v.x, v.y)
  175.                 term.write("+")
  176.                 term.setCursorPos(v.a, v.b)
  177.                 term.write("%")
  178.  
  179.                 if v.n then
  180.                     term.setCursorPos(v.x, v.b)
  181.                     term.write(v.n)
  182.                 end
  183.             end
  184.         else
  185.             paintutils.drawImage(v.d, v.x, v.y)
  186.             if not preview then
  187.                 term.setCursorPos(v.x, v.y)
  188.                 term.write("+")
  189.  
  190.                 if v.n then
  191.                     term.setCursorPos(v.x, v.b)
  192.                     term.write(v.n)
  193.                 end
  194.             end
  195.         end
  196.     end
  197.  
  198.     if not preview then
  199.         term.setCursorPos(1, th)
  200.         term.setTextColor(colors.gray)
  201.         term.setBackgroundColor(colors.black)
  202.         write("Current Menu: "..gui_menu)
  203.  
  204.         local s = file_name or "Untitled"
  205.         term.setCursorPos(tw - string.len(s), th)
  206.         term.write(s)
  207.     end
  208. end
  209.  
  210. local function mod_read( _sReplaceChar, _tHistory, wid, starttext)
  211.     term.setCursorBlink( true )
  212.  
  213.     local sLine = starttext or ""
  214.     local nHistoryPos = nil
  215.     local nPos = (starttext and starttext:len()) or 0
  216.     if _sReplaceChar then
  217.         _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
  218.     end
  219.    
  220.     local w, h = term.getSize()
  221.     local sx, sy = term.getCursorPos()
  222.  
  223.     w = sx + wid - 1
  224.    
  225.     local function redraw( _sCustomReplaceChar )
  226.         local nScroll = 0
  227.         if sx + nPos >= w then
  228.             nScroll = (sx + nPos) - w
  229.         end
  230.            
  231.         term.setCursorPos( sx, sy )
  232.         local sReplace = _sCustomReplaceChar or _sReplaceChar
  233.         if sReplace then
  234.             term.write( string.rep(sReplace, string.len(sLine) - nScroll) )
  235.         else
  236.             term.write( string.sub( sLine, nScroll + 1 ) )
  237.         end
  238.         term.setCursorPos( sx + nPos - nScroll, sy )
  239.     end
  240.    
  241.     redraw()
  242.     while true do
  243.         local sEvent, param = os.pullEvent()
  244.         if sEvent == "char" then
  245.             sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
  246.             nPos = nPos + 1
  247.             redraw()
  248.            
  249.         elseif sEvent == "key" then
  250.             if param == keys.enter then
  251.                 -- Enter
  252.                 break
  253.                
  254.             elseif param == keys.left then
  255.                 -- Left
  256.                 if nPos > 0 then
  257.                     nPos = nPos - 1
  258.                     redraw()
  259.                 end
  260.                
  261.             elseif param == keys.right then
  262.                 -- Right               
  263.                 if nPos < string.len(sLine) then
  264.                     nPos = nPos + 1
  265.                     redraw()
  266.                 end
  267.            
  268.             elseif param == keys.up or param == keys.down then
  269.                 -- Up or down
  270.                 if _tHistory then
  271.                     redraw(" ");
  272.                     if param == keys.up then
  273.                         -- Up
  274.                         if nHistoryPos == nil then
  275.                             if #_tHistory > 0 then
  276.                                 nHistoryPos = #_tHistory
  277.                             end
  278.                         elseif nHistoryPos > 1 then
  279.                             nHistoryPos = nHistoryPos - 1
  280.                         end
  281.                     else
  282.                         -- Down
  283.                         if nHistoryPos == #_tHistory then
  284.                             nHistoryPos = nil
  285.                         elseif nHistoryPos ~= nil then
  286.                             nHistoryPos = nHistoryPos + 1
  287.                         end                    
  288.                     end
  289.                    
  290.                     if nHistoryPos then
  291.                         sLine = _tHistory[nHistoryPos]
  292.                         nPos = string.len( sLine )
  293.                     else
  294.                         sLine = ""
  295.                         nPos = 0
  296.                     end
  297.                     redraw()
  298.                 end
  299.             elseif param == keys.backspace then
  300.                 -- Backspace
  301.                 if nPos > 0 then
  302.                     redraw(" ");
  303.                     sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
  304.                     nPos = nPos - 1                
  305.                     redraw()
  306.                 end
  307.             elseif param == keys.home then
  308.                 -- Home
  309.                 nPos = 0
  310.                 redraw()       
  311.             elseif param == keys.delete then
  312.                 if nPos < string.len(sLine) then
  313.                     redraw(" ");
  314.                     sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )              
  315.                     redraw()
  316.                 end
  317.             elseif param == keys["end"] then
  318.                 -- End
  319.                 nPos = string.len(sLine)
  320.                 redraw()
  321.             end
  322.         end
  323.     end
  324.    
  325.     term.setCursorBlink( false )
  326.     term.setCursorPos( w + 1, sy )
  327.     print()
  328.    
  329.     return sLine
  330. end
  331.  
  332. local function textbox(prompt, starttext)
  333.     sleep(0)
  334.     term.setBackgroundColor(colors.black)
  335.     term.clear()
  336.     drawGui()
  337.  
  338.     local wid = ((prompt:len() <= 15 and 20) or prompt:len() + 5)
  339.     local startx = tw/2 - wid/2
  340.     local starty = ((th%2==1 and th/2) or ((th/2)-1)) - 2 -- 4/2
  341.  
  342.     term.setBackgroundColor(colors.lightGray)
  343.     for i = 0, 3 do
  344.         term.setCursorPos(startx, starty + i)
  345.         print(string.rep(" ", wid))
  346.     end
  347.  
  348.     term.setCursorPos(startx + 1, starty + 1)
  349.     term.setTextColor(colors.black)
  350.     print(prompt)
  351.  
  352.     term.setBackgroundColor(colors.gray)
  353.     term.setCursorPos(startx + 1, starty + 2)
  354.     print(string.rep(" ", wid-2))
  355.  
  356.     term.setCursorPos(startx + 1, starty + 2)
  357.     term.setTextColor(colors.white)
  358.     return mod_read(nil, nil, wid-2, starttext)
  359. end
  360.  
  361. local function text(...)
  362.     term.setBackgroundColor(colors.black)
  363.     term.clear()
  364.     drawGui()
  365.  
  366.     local wid = 0
  367.     local strs = {...}
  368.     for i,v in pairs(strs) do
  369.         local _wid = ((v:len() <= 15 and 20) or v:len() + 5)
  370.         wid = (_wid > wid and _wid) or wid
  371.     end
  372.     local startx = tw/2 - wid/2
  373.     local starty = ((th%2==1 and th/2) or ((th/2)-1)) - 1 -- ~ 3/2
  374.  
  375.     term.setBackgroundColor(colors.lightGray)
  376.     for i = 0, #strs + 1 do
  377.         term.setCursorPos(startx, starty + i)
  378.         print(string.rep(" ", wid))
  379.     end
  380.  
  381.     for i,v in pairs(strs) do
  382.         term.setCursorPos(startx + 1, starty + i)
  383.         term.setTextColor(colors.gray)
  384.         print(v)
  385.     end
  386.  
  387.     os.pullEvent()
  388.     return true
  389. end
  390.  
  391. local function rightClick(x, y)
  392.     local menu
  393.     local index
  394.     local obj
  395.  
  396.     for i = #gui_objs[gui_menu], 1, -1 do
  397.         if x >= gui_objs[gui_menu][i].x and x <= gui_objs[gui_menu][i].a and y >= gui_objs[gui_menu][i].y and y <= gui_objs[gui_menu][i].b then
  398.             if not gui_objs[gui_menu][i].nfp then
  399.                 menu = {
  400.                     {"     Set Text     ", 10},
  401.                     {"      Delete      ", 11},
  402.                     {" Background Color ", 12},
  403.                     {"    Text Color    ", 13},
  404.                     {"      Move Up     ", 14},
  405.                     {"     Move Down    ", 15},
  406.                     {"     Set Name     ", 16},
  407.                     {" ---------------- ", -1},
  408.                     {"    New Object    ", 1},
  409.                     {"   New NFP Image  ", 2},
  410.                     {"     New Menu     ", 3},
  411.                     {"    Delete Menu   ", 4},
  412.                     {"     Goto Menu    ", 5},
  413.                     --{" ---------------- ", -1},
  414.                     --{"       Save       ", 20},
  415.                     --{"       Load       ", 21},
  416.                 }
  417.                 index = i
  418.                 obj = gui_objs[gui_menu][index]
  419.                 break
  420.  
  421.             else
  422.                 menu = {
  423.                     {"      Delete      ", 11},
  424.                     {"      Move Up     ", 14},
  425.                     {"     Move Down    ", 15},
  426.                     {" ---------------- ", -1},
  427.                     {"    New Object    ", 1},
  428.                     {"   New NFP Image  ", 2},
  429.                     {"     New Menu     ", 3},
  430.                     {"    Delete Menu   ", 4},
  431.                     {"     Goto Menu    ", 5},
  432.                     --{" ---------------- ", -1},
  433.                     --{"       Save       ", 20},
  434.                     --{"       Load       ", 21},
  435.                 }
  436.                 index = i
  437.                 obj = gui_objs[gui_menu][index]
  438.                 break
  439.             end
  440.         end
  441.     end
  442.  
  443.     if not menu then
  444.         menu = {
  445.             {"   New Object   ", 1},
  446.             {"  New NFP Image ", 2},
  447.             {"    New Menu    ", 3},
  448.             {"   Delete Menu  ", 4},
  449.             {"    Goto Menu   ", 5},
  450.             --{" ------------ ", -1},
  451.             --{"     Save     ", 20},
  452.             --{"     Load     ", 21},
  453.         }
  454.     end
  455.  
  456.     local startx = (x < tw - menu[1][1]:len() and x) or (tw - menu[1][1]:len())
  457.     local starty = (y < th - #menu            and y) or (th - #menu)
  458.     term.setTextColor(colors.white)
  459.     term.setBackgroundColor(colors.gray)
  460.     for i,v in pairs(menu) do
  461.         term.setCursorPos(startx, starty + i)
  462.         term.write(v[1])
  463.     end
  464.  
  465.     while true do
  466.         local event = {os.pullEvent("mouse_click")}
  467.         if event[3] >= startx and event[3] <= startx + menu[1][1]:len() then
  468.             if menu[event[4] - starty] then
  469.                 local option = menu[event[4] - starty][2]
  470.                 if option ~= -1 then
  471.  
  472.                     -- Misc. Options
  473.                     if option == 1 then -- new object
  474.                         newObject(x, y, x + 6, y + 4, colors.white, colors.black)
  475.                     elseif option == 2 then -- new image
  476.                         local path = textbox("Image Path")
  477.                         newImage(x, y, paintutils.loadImage(path))
  478.                     elseif option == 3 then -- new menu
  479.                         local name = textbox("Create Menu")
  480.                         gui_objs[name] = {}
  481.                         gui_menu = name
  482.                     elseif option == 4 then -- delete menu
  483.                         local name = textbox("Delete Menu")
  484.                         gui_objs[name] = nil
  485.                         if gui_menu == name then if not gui_objs.default then gui_objs.default = {} end gui_menu = "default" end
  486.                     elseif option == 5 then -- goto menu
  487.                         gui_menu = textbox("Go to Menu")
  488.                         if not gui_objs[gui_menu] then gui_objs[gui_menu] = {} end
  489.  
  490.  
  491.                     -- Object Options
  492.                     elseif option == 10 then -- add text
  493.                         obj.t = textbox("Object Text", obj.t)
  494.                     elseif option == 11 then -- delete
  495.                         table.remove(gui_objs[gui_menu], index)
  496.                     elseif option == 12 then -- background color
  497.                         obj.bc = colors[textbox("Background Color")] or obj.bc
  498.                     elseif option == 13 then -- text color
  499.                         obj.tc = colors[textbox("Text Color")] or obj.tc
  500.                     elseif option == 14 then -- move up
  501.                         if index ~= #gui_objs[gui_menu] then
  502.                             local obj = table.remove(gui_objs[gui_menu], index)
  503.                             table.insert(gui_objs[gui_menu], index + 1, obj)
  504.                         end
  505.                     elseif option == 15 then -- move down
  506.                         if index ~= 1 then
  507.                             local obj = table.remove(gui_objs[gui_menu], index)
  508.                             table.insert(gui_objs[gui_menu], index - 1, obj)
  509.                         end
  510.                     elseif option == 16 then
  511.                         obj.n = textbox("Name")
  512.                         if obj.n == "" then obj.n = nil end
  513.                    
  514.                     -- File Options
  515.                     elseif option == 20 then -- Save
  516.                         local name = textbox("Save As")
  517.                         local file = io.open(name, "w")
  518.                         file:write(textutils.serialize(gui_objs))
  519.                         file:close()
  520.                     elseif option == 21 then -- Load
  521.                         local name = textbox("Open")
  522.                         local file = io.open(name, "r")
  523.                         local ok, obj = pcall(textutils.unserialize, file:read("*a"))
  524.                         if type(obj) == "table" then
  525.                             gui_objs = obj
  526.                         elseif not ok then
  527.                             text("Error Loading:", obj)
  528.                         else
  529.                             text("Invalid Type:", type(obj) .. " (" .. tostring(obj) .. ")")
  530.                         end
  531.                         file:close()
  532.                     end
  533.                     break
  534.                 end
  535.             else
  536.                 break
  537.             end
  538.         else
  539.             break
  540.         end
  541.     end
  542. end
  543.  
  544. local function filemenu()
  545.     local x, y = 2, 1
  546.  
  547.     local menu = {
  548.         {"  [O]pen   ", 1},
  549.         {"  [S]ave   ", 2},
  550.         {" Save [A]s ", 3},
  551.         {" --------- ", -1},
  552.         {" [P]review ", 5},
  553.         {" --------- ", -1},
  554.         {"  [E]xit   ", 10},
  555.     }
  556.     local menukeys = {
  557.         [keys.o] = 1,
  558.         [keys.s] = 2,
  559.         [keys.a] = 3,
  560.         [keys.p] = 5,
  561.         [keys.e] = 10,
  562.         [keys.t] = 10,
  563.         [keys.leftCtrl] = 13,
  564.     }
  565.  
  566.     local startx = (x < tw - menu[1][1]:len() and x) or (tw - menu[1][1]:len())
  567.     local starty = (y < th - #menu            and y) or (th - #menu)
  568.     term.setTextColor(colors.white)
  569.     term.setBackgroundColor(colors.gray)
  570.     for i,v in pairs(menu) do
  571.         term.setCursorPos(startx, starty + i)
  572.         term.write(v[1])
  573.     end
  574.  
  575.     while true do
  576.         local event = {os.pullEvent()}
  577.         local option
  578.         if event[1] == "mouse_click" then
  579.             if event[3] >= startx and event[3] <= startx + menu[1][1]:len() then
  580.                 if menu[event[4] - starty] then
  581.                     option = menu[event[4] - starty][2]
  582.                 end
  583.             end
  584.         elseif event[1] == "key" then
  585.             if menukeys[event[2]] then option = menukeys[event[2]] end
  586.         end
  587.  
  588.         if option and option ~= -1 then
  589.             if option == 1 then -- open
  590.                 local name = textbox("Open")
  591.                 local file = io.open(name, "r")
  592.                 local ok, obj = pcall(textutils.unserialize, file:read("*a"))
  593.                 if type(obj) == "table" then
  594.                     gui_objs = obj
  595.                 elseif not ok then
  596.                     text("Error Loading:", obj)
  597.                 else
  598.                     text("Invalid Type:", type(obj) .. " (" .. tostring(obj) .. ")")
  599.                 end
  600.                 file:close()
  601.                 break
  602.  
  603.             elseif option == 2 then -- save
  604.                 if not file_name then
  605.                     file_name = textbox("Save As")
  606.                 end
  607.                 local file = io.open(file_name, "w")
  608.                 file:write(textutils.serialize(gui_objs))
  609.                 file:close()
  610.                 break
  611.  
  612.             elseif option == 3 then
  613.                 file_name = textbox("Save As")
  614.                 local file = io.open(file_name, "w")
  615.                 file:write(textutils.serialize(gui_objs))
  616.                 file:close()
  617.                 break
  618.  
  619.             elseif option == 5 then -- preview
  620.                 preview = not preview
  621.                 break
  622.  
  623.             elseif option == 10 then -- exit
  624.                 return true
  625.  
  626.             elseif option == 13 then
  627.                 break
  628.             end
  629.         elseif not option then
  630.             break
  631.         end
  632.     end
  633. end
  634.  
  635. if args[1] then
  636.     local file = io.open(args[1], "r")
  637.     local ok, obj = pcall(textutils.unserialize, file:read("*a"))
  638.     if type(obj) == "table" then
  639.         gui_objs = obj
  640.     elseif not ok then
  641.         text("Error Loading:", obj)
  642.     else
  643.         text("Invalid Type:", type(obj) .. " (" .. tostring(obj) .. ")")
  644.     end
  645.     file:close()
  646. else
  647.     gui_objs = {default={}}
  648. end
  649.  
  650. local dragging
  651. local drag_mode
  652.  
  653.  
  654.  
  655. local menulist
  656.  
  657. while true do
  658.     term.setBackgroundColor(colors.black)
  659.     term.clear()
  660.     drawGui()
  661.     local event = {os.pullEvent()}
  662.  
  663.     if event[1] == "mouse_click" and event[2] == 1 then
  664.         -- start dragging if clicked on the top left or bottom right
  665.         dragging = nil
  666.  
  667.         for i = #gui_objs[gui_menu], 1, -1 do
  668.             local obj = gui_objs[gui_menu][i]
  669.             if event[3] == obj.a and event[4] == obj.b then
  670.                 dragging = i
  671.                 drag_mode = 2
  672.                 break
  673.             elseif event[3] == obj.x and event[4] == obj.y then
  674.                 dragging = i
  675.                 drag_mode = 1
  676.                 break
  677.             end
  678.         end
  679.  
  680.     elseif event[1] == "mouse_drag" and dragging then
  681.         -- drag
  682.         if drag_mode == 1 then
  683.             if not gui_objs[gui_menu][dragging].nfp then
  684.                 gui_objs[gui_menu][dragging].a = event[3] + w(gui_objs[gui_menu][dragging]) - 1
  685.                 gui_objs[gui_menu][dragging].b = event[4] + h(gui_objs[gui_menu][dragging]) - 1
  686.             end
  687.             gui_objs[gui_menu][dragging].x = event[3]
  688.             gui_objs[gui_menu][dragging].y = event[4]
  689.         elseif drag_mode == 2 and not gui_objs[gui_menu][dragging].nfp then
  690.             gui_objs[gui_menu][dragging].a = (event[3] >= gui_objs[gui_menu][dragging].x and event[3]) or gui_objs[gui_menu][dragging].x
  691.             gui_objs[gui_menu][dragging].b = (event[4] >= gui_objs[gui_menu][dragging].y and event[4]) or gui_objs[gui_menu][dragging].y
  692.         end
  693.  
  694.     elseif event[1] == "mouse_click" and event[2] == 2 then
  695.         -- right-click menu
  696.         dragging = nil
  697.         rightClick(event[3], event[4])
  698.  
  699.     elseif event[1] == "key" then
  700.         if event[2] == keys.left then
  701.             local this_menu
  702.             menulist = {}
  703.             for i,v in pairs(gui_objs) do
  704.                 local index = #menulist + 1
  705.                 menulist[index] = i
  706.                 if i == gui_menu then this_menu = index end
  707.             end
  708.             gui_menu = menulist[(this_menu % #menulist) + 1]
  709.  
  710.         elseif event[2] == keys.right then
  711.             local this_menu
  712.             menulist = {}
  713.             for i,v in pairs(gui_objs) do
  714.                 local index = #menulist + 1
  715.                 menulist[index] = i
  716.                 if i == gui_menu then this_menu = index end
  717.             end
  718.             gui_menu = menulist[((this_menu-2) % #menulist) + 1]
  719.  
  720.         elseif event[2] == keys.leftCtrl then
  721.             local exit = filemenu()
  722.             if exit then
  723.                 term.setTextColor(colors.white)
  724.                 term.setBackgroundColor(colors.black)
  725.                 term.clear()
  726.                 term.setCursorPos(1, 1)
  727.                 break
  728.             end
  729.         end
  730.     end
  731. end
Advertisement
Add Comment
Please, Sign In to add comment