Advertisement
LegoStax

GSet

Mar 7th, 2016
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.94 KB | None | 0 0
  1. --[[
  2.     GSet - LegoStax
  3.     GUI program for global CC settings
  4. ]]--
  5. local tArgs = {...}
  6. local running = true
  7. local w,h = term.getSize()
  8. local SETTINGS_PATH = ".settings"
  9. local SETTINGS = settings.getNames()
  10. local scrollpos = 1
  11. local pospossible = #SETTINGS-(h-4)
  12. table.sort(SETTINGS)
  13. local choice = 1
  14. local advMode
  15.  
  16. if term.isColor() or term.isColour() then
  17.     advMode = true
  18. else
  19.     advMode = false
  20. end
  21.  
  22. if fs.exists(SETTINGS_PATH) then settings.load(SETTINGS_PATH) end
  23.  
  24. if tArgs[1] ~= nil then
  25.     if tArgs[1] == "-?" or tArgs[1] == "-help" then
  26.         print(fs.getName(shell.getRunningProgram()).." [settings_path]")
  27.         running = false
  28.         return
  29.     elseif fs.exists(tArgs[1]) then
  30.         SETTINGS_PATH = tArgs[1]
  31.         settings.load(SETTINGS_PATH)
  32.     else
  33.         print("Create new settings file? (y/n)")
  34.         while true do
  35.             local e = {os.pullEvent("key")}
  36.             if e[2] == keys.y then
  37.                 SETTINGS_PATH = tArgs[1]
  38.                 break
  39.             elseif e[2] == keys.n then
  40.                 print("Aborted")
  41.                 sleep(1)
  42.                 running = false
  43.                 break
  44.             end
  45.         end
  46.     end
  47. end
  48.  
  49. local function drawTitleBar()
  50.     term.setCursorPos(1,1)
  51.     term.setTextColor(colors.white)
  52.     if advMode then
  53.         term.setBackgroundColor(colors.blue)
  54.         term.clearLine()
  55.         term.write("GSet")
  56.         term.setBackgroundColor(colors.red)
  57.         term.setCursorPos(w,1)
  58.         term.write("X")
  59.     else
  60.         term.setBackgroundColor(colors.lightGray)
  61.         term.clearLine()
  62.         term.write("GSet - Press Q to quit")
  63.     end
  64. end
  65.  
  66. local function drawSettings()
  67.     term.setCursorPos(1,2)
  68.     if advMode then
  69.         term.setBackgroundColor(colors.white)
  70.         term.setTextColor(colors.lightGray)
  71.         term.write("Name")
  72.         term.setTextColor(colors.black)
  73.         term.setCursorPos(1,3)
  74.     else
  75.         term.setBackgroundColor(colors.black)
  76.         term.setTextColor(colors.lightGray)
  77.         term.write("Name")
  78.         term.setTextColor(colors.white)
  79.         term.setCursorPos(3,3)
  80.     end
  81.     for i = scrollpos,#SETTINGS do
  82.         if i > #SETTINGS then break end
  83.         local x,y = term.getCursorPos()
  84.        
  85.         -- determine color
  86.         if advMode then
  87.             if settings.get(SETTINGS[i]) == true then
  88.                 term.setTextColor(colors.lime)
  89.             elseif settings.get(SETTINGS[i]) == false then
  90.                 term.setTextColor(colors.red)
  91.             else
  92.                 term.setTextColor(colors.black)
  93.             end
  94.             term.write(SETTINGS[i])
  95.         else
  96.             if choice == i then
  97.                 term.setCursorPos(1,y)
  98.                 term.write("> "..SETTINGS[i])
  99.             else
  100.                 term.write(SETTINGS[i])
  101.             end
  102.         end
  103.        
  104.         y = y+1
  105.         if y > h-1 then break end
  106.         if advMode then term.setCursorPos(1,y)
  107.         else term.setCursorPos(3,y) end
  108.     end
  109. end
  110.  
  111. local function drawButtons()
  112.     if advMode then
  113.         term.setBackgroundColor(colors.lime)
  114.         term.setTextColor(colors.white)
  115.         term.setCursorPos(w-4,3)
  116.         term.write("     ")
  117.         term.setCursorPos(w-4,4)
  118.         term.write("  +  ")
  119.         term.setCursorPos(w-4,5)
  120.         term.write("     ")
  121.         term.setBackgroundColor(colors.red)
  122.         term.setCursorPos(w-4,6)
  123.         term.write("     ")
  124.         term.setCursorPos(w-4,7)
  125.         term.write("  -  ")
  126.         term.setCursorPos(w-4,8)
  127.         term.write("     ")
  128.     else
  129.         term.setBackgroundColor(colors.black)
  130.         term.setCursorPos(w-2,2)
  131.         term.write("+ -")
  132.     end
  133. end
  134.  
  135. local function newDialog()
  136.     term.setBackgroundColor(colors.gray)
  137.     term.setTextColor(colors.white)
  138.     term.setCursorPos(1,h)
  139.     term.clearLine()
  140.     write("New: ")
  141.     local new = read()
  142.     table.insert(SETTINGS, new)
  143.     return new
  144. end
  145.  
  146. local function setDialog(i)
  147.     term.setBackgroundColor(colors.gray)
  148.     term.setTextColor(colors.white)
  149.     term.setCursorPos(1,h)
  150.     term.clearLine()
  151.     local s = settings.get(SETTINGS[i])
  152.     if s ~= nil then
  153.         s = tostring(s)
  154.         for i = 1,#s do
  155.             os.queueEvent("char", s:sub(i,i))
  156.         end
  157.     end
  158.     local d = read()
  159.     if tonumber(d) ~= nil then
  160.         d = tonumber(d)
  161.     elseif d == "true" then
  162.         d = true
  163.     elseif d == "false" then
  164.         d = false
  165.     end
  166.     settings.set(SETTINGS[i], d)
  167. end
  168.  
  169. local function alertDialog(s)
  170.     term.setBackgroundColor(colors.gray)
  171.     term.setTextColor(colors.white)
  172.     term.setCursorPos(1,h)
  173.     term.clearLine()
  174.     write(s)
  175. end
  176.  
  177. local function resetScroll()
  178.     SETTINGS = settings.getNames()
  179.     scrollpos = 1
  180.     pospossible = #SETTINGS-(h-4)
  181.     table.sort(SETTINGS)
  182. end
  183.  
  184. local function drawScreen()
  185.     if advMode then
  186.         term.setBackgroundColor(colors.white)
  187.     else
  188.         term.setBackgroundColor(colors.black)
  189.     end
  190.     term.clear()
  191.     drawTitleBar()
  192.     drawSettings()
  193.     drawButtons()
  194. end
  195. drawScreen()
  196.  
  197. local function evtHandler()
  198.     while running do
  199.         local e = {os.pullEvent()}
  200.         if e[1] == "set_change" then
  201.             settings.save(SETTINGS_PATH)
  202.         end
  203.         if advMode then
  204.             if e[1] == "mouse_scroll" then
  205.                 if e[2] == 1 and scrollpos < pospossible then
  206.                     scrollpos = scrollpos+1
  207.                 elseif e[2] == -1 and scrollpos > 1 then
  208.                     scrollpos = scrollpos-1
  209.                 end
  210.                 drawScreen()
  211.             elseif e[1] == "mouse_click" and e[3] == w and e[4] == 1 then
  212.                 running = false
  213.             elseif e[1] == "mouse_click" and e[3] >= w-5 and e[4] >= 3 and e[4] <= 5 then
  214.                 newDialog()
  215.                 setDialog(#SETTINGS)
  216.                 resetScroll()
  217.                 os.queueEvent("set_change")
  218.                 drawScreen()
  219.             elseif e[1] == "mouse_click" and e[3] >= w-5 and e[4] >= 6 and e[4] <= 8 then
  220.                 alertDialog("Click a setting to delete it")
  221.                 while true do
  222.                     local e = {os.pullEvent("mouse_click")}
  223.                     if e[3] <= w-5 and e[4] >= 3 and e[4] <= h-1 then
  224.                         local ref = (e[4]-3)+scrollpos
  225.                         if SETTINGS[ref] ~= nil then
  226.                             settings.unset(SETTINGS[ref])
  227.                             table.remove(SETTINGS, ref)
  228.                             break
  229.                         end
  230.                     end
  231.                 end
  232.                 resetScroll()
  233.                 os.queueEvent("set_change")
  234.                 drawScreen()
  235.             elseif e[1] == "mouse_click" and e[3] <= w-5 and e[4] >= 3 and e[4] <= h-1 then
  236.                 local ref = (e[4]-3)+scrollpos
  237.                 if SETTINGS[ref] ~= nil then
  238.                     if e[2] == 2 then
  239.                         setDialog(ref)
  240.                     else
  241.                         if type(settings.get(SETTINGS[ref])) == "boolean" then
  242.                             if settings.get(SETTINGS[ref]) then
  243.                                 settings.set(SETTINGS[ref], false)
  244.                             else
  245.                                 settings.set(SETTINGS[ref], true)
  246.                             end
  247.                         else
  248.                             setDialog(ref)
  249.                         end
  250.                     end
  251.                     os.queueEvent("set_change")
  252.                     drawScreen()
  253.                 end
  254.             elseif e[1] == "char" and e[2] == "q" then
  255.                 running = false
  256.             elseif e[1] == "char" and e[2] == "a" or e[2] == "+" then
  257.                 newDialog()
  258.                 setDialog(#SETTINGS)
  259.                 resetScroll()
  260.                 os.queueEvent("set_change")
  261.                 drawScreen()
  262.             elseif e[1] == "char" and e[2] == "d" or e[2] == "-" then
  263.                 alertDialog("Click a setting to delete it")
  264.                 while true do
  265.                     local e = {os.pullEvent("mouse_click")}
  266.                     if e[3] <= w-5 and e[4] >= 3 and e[4] <= h-1 then
  267.                         local ref = (e[4]-3)+scrollpos
  268.                         if SETTINGS[ref] ~= nil then
  269.                             settings.unset(SETTINGS[ref])
  270.                             table.remove(SETTINGS, ref)
  271.                             break
  272.                         end
  273.                     elseif e[3] == w and e[4] == 1 then
  274.                         running = false
  275.                         break
  276.                     end
  277.                 end
  278.                 resetScroll()
  279.                 os.queueEvent("set_change")
  280.                 drawScreen()
  281.             end
  282.         else
  283.             if e[1] == "char" and e[2] == "q" then
  284.                 running = false
  285.             elseif e[1] == "char" and e[2] == "+" or e[2] == "a" then
  286.                 newDialog()
  287.                 setDialog(#SETTINGS)
  288.                 resetScroll()
  289.                 os.queueEvent("set_change")
  290.                 drawScreen()
  291.             elseif e[1] == "char" and e[2] == "-" or e[2] == "d" then
  292.                 settings.unset(SETTINGS[choice])
  293.                 table.remove(SETTINGS,choice)
  294.                 resetScroll()
  295.                 os.queueEvent("set_change")
  296.                 drawScreen()
  297.             elseif e[1] == "key" then
  298.                 if e[2] == keys.w or e[2] == keys.up then
  299.                     if choice > 1 then
  300.                         choice = choice-1
  301.                         if scrollpos > 1 then
  302.                             scrollpos = scrollpos-1
  303.                         end
  304.                         drawScreen()
  305.                     end
  306.                 elseif e[2] == keys.s or e[2] == keys.down then
  307.                     if choice < #SETTINGS then
  308.                         choice = choice+1
  309.                         if scrollpos < pospossible then
  310.                             scrollpos = scrollpos+1
  311.                         end
  312.                         drawScreen()
  313.                     end
  314.                 elseif e[2] == keys.enter then
  315.                     setDialog(choice)
  316.                     os.queueEvent("set_change")
  317.                     drawScreen()
  318.                 end
  319.             end
  320.         end
  321.     end
  322. end
  323. evtHandler()
  324. term.setBackgroundColor(colors.black)
  325. term.clear()
  326. term.setCursorPos(1,1)
  327. print("Thank you for using GSet\nby LegoStax")
  328. coroutine.yield()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement