Advertisement
Chaos_Cash

seedManagerInterface.lua

Feb 3rd, 2025 (edited)
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 21.17 KB | None | 0 0
  1. orderFunctions = {}
  2.  
  3.  
  4. function interfaceStartup()
  5.  
  6.     while not mainIsDoneWithStartup do
  7.     sleep(0.1)
  8.         if multishell.getTitle(1) ~= "startup" then
  9.         error()
  10.         end
  11.     end
  12.  
  13.     for k,v in pairs(peripheral.getNames()) do
  14.         if peripheral.getType(peripheral.getNames()[k]) == "modem" then
  15.         rednet.open(peripheral.getNames()[k])
  16.         end
  17.     end
  18.  
  19.  
  20. defaultOrders = {
  21. ["restockSeeds"] = {["param1Options"] = "seedNames", ["param1Name"] = "Seed name:", ["priority"] = 4},
  22. ["produceEssence"] = {["param1Options"] = "essenceNames", ["param1Name"] = "Essence name:", ["param2Options"] = "#n", ["param2Name"] = "Amount:", ["priority"] = 5}
  23. }
  24.  
  25. defaultOrderNames = {}
  26.     for k, v in pairs(defaultOrders) do
  27.     table.insert(defaultOrderNames, k)
  28.     end
  29.  
  30. orderTypesDropdown = DropdownMenu:new(1,4,40,defaultOrderNames)
  31.  
  32.  
  33. tickTimer = os.startTimer(1)
  34. end
  35.  
  36.  
  37.  
  38. function interfaceMain()
  39.  
  40.     while true do
  41.  
  42.     info1,info2,info3,info4 = os.pullEvent()
  43.    
  44.         if multishell.getTitle(1) ~= "startup" then
  45.         error()
  46.         end
  47.    
  48.    
  49.         if info1 == "rednet_message" then  
  50.            
  51.                 if type(info3) == "table" then
  52.                
  53.                
  54.                
  55.                 else
  56.                
  57.                
  58.                
  59.                 end
  60.        
  61.         end
  62.        
  63.        
  64.         if info1 == "timer" then
  65.        
  66.             if info2 == tickTimer then
  67.             curEnv()
  68.             tickTimer = os.startTimer(1)
  69.             end
  70.        
  71.         end
  72.        
  73.        
  74.         if curEnv == homeMenu then
  75.        
  76.             if buttonCheck(17,4,24,6) then
  77.             ordersMenu()
  78.             end
  79.            
  80.             if buttonCheck(27,4,33,6) then
  81.             seedsMenu()
  82.             end
  83.        
  84.         end
  85.        
  86.        
  87.         if curEnv == ordersMenu then
  88.        
  89.         local scrollOut = checkScrollmenu("orderScrollMenu")
  90.             if scrollOut then
  91.             orderInfoMenu(scrollmenuOrderKeys[scrollOut])
  92.             end
  93.        
  94.        
  95.             if buttonCheck(46,16,51,18) then
  96.             homeMenu()
  97.             end
  98.            
  99.             if buttonCheck(38,12,51,14) then
  100.             openCreateOrderMenu()
  101.             end
  102.        
  103.         end
  104.        
  105.        
  106.         if curEnv == createOrderMenu then
  107.        
  108.             if orderTypesDropdown:check() then
  109.             openCreateOrderMenu()
  110.             end
  111.        
  112.             if param1Dropdown then
  113.             param1Dropdown:check()
  114.             elseif param1Reader then
  115.             param1Reader:read()
  116.             end
  117.            
  118.             if param2Dropdown then
  119.             param2Dropdown:check()
  120.             elseif param2Reader then
  121.             param2Reader:read()
  122.             end
  123.        
  124.             if buttonCheck(46,16,51,18) then
  125.             ordersMenu()
  126.             end
  127.            
  128.             if buttonCheck(1,16,9,18) then
  129.                 if param1Dropdown then
  130.                 param1 = param1Dropdown:getOutput()
  131.                 elseif param1Reader then
  132.                 param1 = param1Reader:getOutput()
  133.                 end
  134.                
  135.                 if param2Dropdown then
  136.                 param2 = param2Dropdown:getOutput()
  137.                 elseif param2Reader then
  138.                 param2 = param2Reader:getOutput()
  139.                 end
  140.                
  141.             param2 = tonumber(param2)
  142.                
  143.             table.insert(requestsFromInterface,{["action"]="createOrder",["functionName"]=orderTypesDropdown:getOutput(),["param1"]=param1,["param2"]=param2,["priority"]=defaultOrders[orderTypesDropdown:getOutput()]["priority"]})
  144.            
  145.             ordersMenu()
  146.             end
  147.        
  148.         end
  149.        
  150.        
  151.         if curEnv == orderInfoMenu then
  152.        
  153.             if buttonCheck(46,16,51,18) then
  154.             ordersMenu()
  155.             end
  156.            
  157.             if buttonCheck(1,16,14,18) then
  158.             table.insert(requestsFromInterface,{["action"]="cancelOrder",["orderKey"]=curOrderKey})
  159.             end
  160.        
  161.         end
  162.    
  163.    
  164.         if curEnv == seedsMenu then
  165.         scrollOut = checkScrollmenu("seedsScrollmenu")
  166.        
  167.        
  168.             if buttonCheck(46,16,51,18) then
  169.             homeMenu()
  170.             end
  171.            
  172.             if buttonCheck(42,12,51,14) then
  173.             checkUnregisteredSeeds()
  174.             end
  175.            
  176.         end
  177.        
  178.        
  179.         if curEnv == registerSeedMenu then
  180.         restockSeedDropdown:check()
  181.        
  182.         term.setTextColor(colors.white)
  183.         seedDropReader:read()
  184.        
  185.             if buttonCheck(46,16,51,18) then
  186.             seedsMenu()
  187.             end
  188.            
  189.             if buttonCheck(1,16,9,18) then
  190.             table.insert(requestsFromInterface, {["action"]="registerSeed",["seedName"]=curSeed,["drop"]=seedDropReader:getOutput(), ["restockSeed"]=restockSeedDropdown:getOutput() == "true"})
  191.             seedsMenu()
  192.             end
  193.        
  194.         end
  195.    
  196.     end
  197.    
  198. end
  199.  
  200.  
  201.  
  202. function seedsMenu()
  203. curEnv = seedsMenu
  204. term.clear()
  205. headline("Seeds")
  206.  
  207.  
  208.     if scrollmenuInfos["seedsScrollmenu"] then
  209.     position = scrollmenuInfos["seedsScrollmenu"]["position"]
  210.     else
  211.     position = 0
  212.     end
  213. createScrollmenu("seedsScrollmenu",1,3,40,18,seedNames,position)
  214. printScrollmenu("seedsScrollmenu")
  215.  
  216.  
  217. button("Back",46,16,51,18)
  218. button("Register",42,12,51,14)
  219. end
  220.  
  221.  
  222.  
  223. function checkUnregisteredSeeds()
  224.     for k,v in pairs(seedStoragePeripheral.list()) do
  225.         if not table.contains(seedNames,v["name"]) then
  226.         openRegisterSeedMenu(v["name"])
  227.         break
  228.         end
  229.     end
  230. end
  231.  
  232.  
  233.  
  234. function openRegisterSeedMenu(seedName)
  235. seedDropReader = BetterRead:new(7,6)
  236. seedDropReader["output"] = string.sub(seedName,1,string.find(seedName,":"))
  237. seedDropReader:startReading()
  238. restockSeedDropdown = DropdownMenu:new(1,9,5,{"true", "false"})
  239. registerSeedMenu(seedName)
  240. end
  241.  
  242.  
  243.  
  244. function registerSeedMenu(seedName)
  245. curSeed = seedName or curSeed
  246. seedName = curSeed
  247. curEnv = registerSeedMenu
  248. term.clear()
  249. headline("Register seed")
  250.  
  251. button("Back",46,16,51,18)
  252. button("Confirm",1,16,9,18)
  253.  
  254.  
  255. term.setCursorPos(1,4)
  256. term.write("Seed name: ")
  257. term.setTextColor(colors.white)
  258. print(seedName)
  259.  
  260. term.setCursorPos(1,6)
  261. term.setTextColor(colors.green)
  262. term.write("Drop: ")
  263. term.setTextColor(colors.white)
  264. term.write(seedDropReader:getOutput())
  265.     if seedDropReader["isReading"] and seedDropReader["blinkIsThere"] then
  266.     print("_")
  267.     end
  268.  
  269. term.setTextColor(colors.green)
  270. term.setCursorPos(1,8)
  271. print("Restock seed:")
  272. term.setTextColor(colors.white)
  273. restockSeedDropdown:print()
  274. end
  275.  
  276.  
  277.  
  278. function openCreateOrderMenu()
  279. param1Name = nil
  280. param2Name = nil
  281. param1Options = nil
  282. param2Options = nil
  283. param1Dropdown = nil
  284. param2Dropdown = nil
  285. param1Reader = nil
  286. param2Reader = nil
  287.  
  288.     if defaultOrders[orderTypesDropdown:getOutput()]["param1Options"] then
  289.     param1Name = defaultOrders[orderTypesDropdown:getOutput()]["param1Name"]
  290.         if not string.find(defaultOrders[orderTypesDropdown:getOutput()]["param1Options"],"#") then
  291.         param1Reader = nil
  292.         param1Options = _ENV[defaultOrders[orderTypesDropdown:getOutput()]["param1Options"]]
  293.         param1Dropdown = DropdownMenu:new(1,7,40,param1Options)
  294.         else
  295.         param1Dropdown = nil
  296.         param1Options = defaultOrders[orderTypesDropdown:getOutput()]["param1Options"]
  297.             if param1Options == "#n" then
  298.             param1Reader = BetterRead:new(1,7,false,{"1","2","3","4","5","6","7","8","9","0"})
  299.             param1Reader:startReading()
  300.             end
  301.         end
  302.     end
  303.  
  304.  
  305.     if defaultOrders[orderTypesDropdown:getOutput()]["param2Options"] then
  306.     param2Name = defaultOrders[orderTypesDropdown:getOutput()]["param2Name"]
  307.         if not string.find(defaultOrders[orderTypesDropdown:getOutput()]["param2Options"],"#") then
  308.         param2Reader = nil
  309.         param2Options = _ENV[defaultOrders[orderTypesDropdown:getOutput()]["param2Options"]]
  310.         param2Dropdown = DropdownMenu:new(1,10,40,param2Options)
  311.         else
  312.         param2Dropdown = nil
  313.         param2Options = defaultOrders[orderTypesDropdown:getOutput()]["param2Options"]
  314.             if param2Options == "#n" then
  315.             param2Reader = BetterRead:new(1,10,false,{"1","2","3","4","5","6","7","8","9","0"})
  316.             param2Reader:startReading()
  317.             end
  318.         end
  319.     end
  320.  
  321. createOrderMenu()
  322.  
  323. end
  324.  
  325.  
  326.  
  327. function createOrderMenu()
  328. curEnv = createOrderMenu
  329. term.clear()
  330. headline("Create order")
  331.  
  332. button("Back",46,16,51,18)
  333. button("Confirm",1,16,9,18)
  334.  
  335. term.setCursorPos(1,3)
  336. term.setTextColor(colors.green)
  337. print("Function type:")
  338. orderTypesDropdown:print()
  339.  
  340.     if param1Name then
  341.     term.setCursorPos(1,6)
  342.     print(param1Name)
  343.     end
  344.    
  345.     if param2Name then
  346.     term.setCursorPos(1,9)
  347.     print(param2Name)
  348.     end
  349.    
  350.    
  351. term.setTextColor(colors.white)
  352.     if param1Dropdown then 
  353.     param1Dropdown:print()
  354.     elseif param1Reader then
  355.     print(param1Reader:getOutput())
  356.         if param1Reader["blinkIsThere"] and param1Reader["isReading"] then
  357.         print("_")
  358.         end
  359.     end
  360.    
  361.     if param2Dropdown then 
  362.     param2Dropdown:print()
  363.     elseif param2Reader then
  364.     term.setCursorPos(1,10)
  365.     term.write(param2Reader:getOutput())
  366.         if param2Reader["blinkIsThere"] and param2Reader["isReading"] then
  367.         print("_")
  368.         end
  369.     end
  370.  
  371.  
  372. end
  373.  
  374.  
  375.  
  376. function homeMenu()
  377. curEnv = homeMenu
  378. term.clear()
  379. headline("Home")
  380.  
  381. button("Orders",17,4,24,6)
  382. button("Seeds",27,4,33,6)
  383.  
  384. end
  385.  
  386.  
  387.  
  388. function orderInfoMenu(orderKey)
  389.     if orders[orderKey or curOrderKey] then
  390.    
  391.     curEnv = orderInfoMenu
  392.     curOrderKey = orderKey or curOrderKey
  393.     orderKey = curOrderKey
  394.     term.clear()
  395.     headline("Order " .. orderKey)
  396.  
  397.     term.setCursorPos(1,3)
  398.     term.setTextColor(colors.green)
  399.     term.write("Order key (ID): ")
  400.     term.setTextColor(colors.white)
  401.     print(orderKey)
  402.  
  403.     term.setTextColor(colors.green)
  404.     term.write("Function name: ")
  405.     term.setTextColor(colors.white)
  406.     print(orders[orderKey]["functionName"])
  407.  
  408.     term.setTextColor(colors.green)
  409.     term.write("Parameter 1: ")
  410.     term.setTextColor(colors.white)
  411.     print(orders[orderKey]["param1"])
  412.  
  413.     term.setTextColor(colors.green)
  414.     term.write("Parameter 2: ")
  415.     term.setTextColor(colors.white)
  416.     print(orders[orderKey]["param2"])
  417.  
  418.     term.setTextColor(colors.green)
  419.     term.write("Pot amount: ")
  420.     term.setTextColor(colors.white)
  421.     print(orders[orderKey]["potAmount"])
  422.  
  423.     term.setTextColor(colors.green)
  424.     term.write("Priority: ")
  425.     term.setTextColor(colors.white)
  426.     print(orders[orderKey]["priority"])
  427.  
  428.  
  429.     button("Back",46,16,51,18)
  430.     button("Cancel order",1,16,14,18,colors.red)
  431.        
  432.     else
  433.     ordersMenu()
  434.     end
  435.    
  436. end
  437.  
  438.  
  439.  
  440. function ordersMenu()
  441. curEnv = ordersMenu
  442. term.clear()
  443. headline("Order menu")
  444.  
  445. button("Back",46,16,51,18)
  446.  
  447. button("Create order",38,12,51,14)
  448.  
  449.  
  450. createOrderScrollmenu()
  451. printScrollmenu("orderScrollMenu")
  452. end
  453.  
  454.  
  455.  
  456. function createOrderScrollmenu()
  457. local content = {}
  458. scrollmenuOrderKeys = {}
  459.     for k, order in pairs(orders) do
  460.     table.insert(content, k .. ": " .. order["functionName"])
  461.     table.insert(scrollmenuOrderKeys, k)
  462.     end
  463.    
  464.  
  465.     if scrollmenuInfos["orderScrollMenu"] then
  466.     createScrollmenu("orderScrollMenu",1,2,20,16,content,scrollmenuInfos["orderScrollMenu"]["position"])
  467.     else
  468.     createScrollmenu("orderScrollMenu",1,2,20,16,content,0)
  469.     end
  470. end
  471.  
  472.  
  473.  
  474.  
  475. -- buttons and all that stuff
  476.  
  477.  
  478. BetterRead = {
  479. new = function(self,xPos,yPos,isBlacklist,filter)
  480.     if type(xPos) ~= "number" then
  481.     error("bad argument #1 (xPos is supposed to be a number)")
  482.     elseif type(yPos) ~= "number" then
  483.     error("bad argument #2 (yPos is supposed to be a number)")
  484.     elseif type(isBlacklist) ~= "boolean" then
  485.     isBlacklist = true
  486.     end
  487.    
  488. returnTable = {["xPos"]=xPos,["yPos"]=yPos,["isBlacklist"]=isBlacklist,["filter"]=filter or {},["isReading"]=false,["output"]="",["blinkIsThere"]=false}
  489. setmetatable(returnTable,self)
  490. self.__index = self
  491. return returnTable
  492. end
  493. ,
  494. startReading = function(self)
  495. self["isReading"] = true
  496. self["blinkTimer"] = os.startTimer(0.4)
  497. self["blinkIsThere"] = true
  498. term.setCursorPos(self["xPos"],self["yPos"])
  499. term.write(self["output"] .. "_")
  500. end
  501. ,
  502. stopReading = function(self)
  503. self["isReading"] = false
  504.     if self["blinkIsThere"] then
  505.     term.setCursorPos(self["xPos"]+string.len(self["output"]),self["yPos"])
  506.     term.write(" ")
  507.     end
  508. end
  509. ,
  510. read = function(self)
  511.     if self["isReading"] then
  512.         if info1 == "key"  then
  513.             if info2 == keys.backspace and string.len(self["output"]) > 0 then
  514.             term.setCursorPos(self["xPos"]+string.len(self["output"])-1,self["yPos"])
  515.                 if self["blinkIsThere"] then
  516.                 term.write("_ ")
  517.                 else
  518.                 term.write(" ")
  519.                 end
  520.             self["output"] = string.sub(self["output"],1,string.len(self["output"])-1)
  521.             end
  522.            
  523.             if info2 == keys.enter then
  524.             self:stopReading()
  525.             info1 = nil
  526.             return true
  527.             end
  528.         end
  529.        
  530.        
  531.         if info1 == "timer" and info2 == self["blinkTimer"] then
  532.         self["blinkIsThere"] = not self["blinkIsThere"]
  533.             if self["blinkIsThere"] then
  534.             term.setCursorPos(self["xPos"]+string.len(self["output"]),self["yPos"])
  535.             term.write("_")
  536.             else
  537.             term.setCursorPos(self["xPos"]+string.len(self["output"]),self["yPos"])
  538.             term.write(" ")
  539.             end
  540.         self["blinkTimer"] = os.startTimer(0.5)
  541.         end
  542.        
  543.        
  544.         if info1 == "char" then
  545.             if not self["isBlacklist"] then
  546.                 for k,v in pairs(self["filter"]) do
  547.                     if info2 == v then
  548.                     self["output"] = self["output"] .. info2
  549.                     term.setCursorPos(self["xPos"],self["yPos"])
  550.                         if self["blinkIsThere"] then
  551.                         term.write(self["output"] .. "_")
  552.                         else
  553.                         term.write(self["output"])
  554.                         end
  555.                     end
  556.                 end
  557.                
  558.             else
  559.            
  560.                 for k,v in pairs(self["filter"]) do
  561.                     if info2 == v then
  562.                     return
  563.                     end
  564.                 end
  565.            
  566.             self["output"] = self["output"] .. info2
  567.             term.setCursorPos(self["xPos"],self["yPos"])
  568.                 if self["blinkIsThere"] then
  569.                 term.write(self["output"] .. "_")
  570.                 else
  571.                 term.write(self["output"])
  572.                 end
  573.             end
  574.         end
  575.    
  576.     else
  577.         if info1 == "mouse_click" and info4 == self["yPos"] then
  578.         self:startReading()
  579.         end
  580.     end
  581. end
  582. ,
  583. getOutput = function(self)
  584. return self["output"]
  585. end
  586. }
  587.  
  588.  
  589.  
  590. function resetEvent()
  591. info1 = nil
  592. end
  593.  
  594.  
  595.  
  596. function createScrollmenu(name,x1,y1,x2,y2,content,position)
  597. scrollmenuLen[name]=0
  598. scrollmenuInfos[name]={["x1"]=x1,["y1"]=y1,["x2"]=x2,["y2"]=y2,["content"]=content,["position"]=position,["previewText"]={}}
  599.  
  600.     for k,v in pairs(content) do
  601.     scrollmenuLen[name]=scrollmenuLen[name]+1
  602.     end
  603.    
  604.     for k,v in pairs(content) do
  605.         if string.len(v) > x2 - x1 + 1 then
  606.         scrollmenuInfos[name]["previewText"][k] = string.sub(v,1,x2-x1-2) .. "..."
  607.         else
  608.         scrollmenuInfos[name]["previewText"][k] = v
  609.         end
  610.     end
  611.  
  612. end
  613.  
  614.  
  615.  
  616. function printScrollmenu(name)
  617. local lastBackColor = term.getBackgroundColor()
  618. local lastTextColor = term.getTextColor()
  619.  
  620. term.setTextColor(colors.white)
  621.  
  622.     for i=1,math.min(scrollmenuLen[name],scrollmenuInfos[name]["y2"]-scrollmenuInfos[name]["y1"]+1) do
  623.    
  624.         if (i+scrollmenuInfos[name]["position"])%2==0 then
  625.         term.setBackgroundColor(colors.gray)
  626.         else
  627.         term.setBackgroundColor(colors.black)
  628.         end
  629.        
  630.     term.setCursorPos(scrollmenuInfos[name]["x1"],scrollmenuInfos[name]["y1"]+i-1)
  631.     term.write(scrollmenuInfos[name]["previewText"][i+scrollmenuInfos[name]["position"]])
  632.    
  633.         for i2=1,scrollmenuInfos[name]["x2"]-scrollmenuInfos[name]["x1"]+1-string.len(scrollmenuInfos[name]["content"][i+scrollmenuInfos[name]["position"]]) do
  634.         term.write(" ")
  635.         end
  636.  
  637.     end
  638.  
  639. term.setBackgroundColor(lastBackColor)
  640. term.setTextColor(lastTextColor)
  641. end
  642.  
  643.  
  644.  
  645. function checkScrollmenu(name)
  646.  
  647.     if info1 == "mouse_click" and info3 > scrollmenuInfos[name]["x1"]-1 and info3 < scrollmenuInfos[name]["x2"]+1 and info4 > scrollmenuInfos[name]["y1"]-1 and info4 < scrollmenuInfos[name]["y2"]+1 then
  648.     resetEvent()
  649.     return info4-scrollmenuInfos[name]["y1"]+1+scrollmenuInfos[name]["position"]
  650.     end
  651.    
  652.     if info1 == "mouse_scroll" and not (scrollmenuInfos[name]["position"]+info2 > scrollmenuLen[name]-(scrollmenuInfos[name]["y2"]-scrollmenuInfos[name]["y1"]+1)) and not (scrollmenuInfos[name]["position"]+info2 < 0) then
  653.     scrollmenuInfos[name]["position"] = scrollmenuInfos[name]["position"] + info2
  654.     printScrollmenu(name)
  655.     end
  656.  
  657.  
  658. end
  659.  
  660.  
  661.  
  662. function headline(headline)
  663. width = term.getSize()
  664. headline_lenght=string.len("   "..headline.."   ")
  665. headline_pos=(width/2)-(headline_lenght/2)+1
  666. term.setCursorPos(headline_pos,1)
  667. term.setBackgroundColor(colors.yellow)
  668. term.setTextColor(colors.blue)
  669. term.write("   "..headline.."   ")
  670. term.setBackgroundColor(colors.black)
  671. term.setTextColor(colors.white)
  672. end
  673.  
  674.  
  675.  
  676. function button(button_text,button_pos_x1,button_pos_y1,button_pos_x2,button_pos_y2,button_color)
  677. term.setTextColor(colors.green)
  678.  
  679.   if button_color~=nil then
  680.   term.setTextColor(button_color)
  681.   end
  682.  
  683. square(button_pos_x1,button_pos_y1,button_pos_x2,button_pos_y2,colors.black,"-","|")
  684. term.setCursorPos(math.ceil(button_pos_x1-string.len(button_text)/2+(button_pos_x2-button_pos_x1)/2),button_pos_y1+(button_pos_y2-button_pos_y1)/2)
  685. term.write(button_text)
  686. end
  687.  
  688.  
  689.  
  690. function buttonCheck(b_check_x1,b_check_y1,b_check_x2,b_check_y2)
  691.   if info1=="mouse_click" and info3 > b_check_x1-1 and info3 < b_check_x2+1 and info4 > b_check_y1-1 and info4 < b_check_y2+1 then
  692.   resetEvent()
  693.   return true
  694.   else
  695.   return false
  696.   end
  697. end
  698.  
  699.  
  700.  
  701. function square(x1,y1,x2,y2,back_color_square,symbol_square_x,symbol_square_y,text_color)
  702. x_1=x1
  703. y_1=y1
  704. x_2=x2
  705. y_2=y2
  706.   while x_1~=x2+1 do
  707.   term.setBackgroundColor(back_color_square)
  708.   term.setCursorPos(x_1,y_1)
  709.     if text_color~=nil then
  710.     term.setTextColor(text_color)
  711.     end
  712.   term.write(symbol_square_x)
  713.   x_1=x_1+1
  714.   end
  715.  
  716.   while y_1~=y2-1 do
  717.   term.setCursorPos(x_1-1,y_1+1)
  718.   term.write(symbol_square_y)
  719.   y_1=y_1+1
  720.   end
  721. x2=x2-1
  722.   while x_2~=x1-1 do
  723.   term.setCursorPos(x_2,y_2)
  724.   term.write(symbol_square_x)
  725.   x_2=x_2-1
  726.   end
  727.  
  728.   while y_2~=y1+1 do
  729.   term.setCursorPos(x_2+1,y_2-1)
  730.   term.write(symbol_square_y)
  731.   y_2=y_2-1
  732.   end
  733. term.setBackgroundColor(colors.black)
  734. end
  735.  
  736.  
  737.  
  738. DropdownMenu = {
  739. new = function(self,x1,y,x2,content)
  740.     if type(x1) ~= "number" then
  741.     error("bad argument #1 (x1 is supposed to be a number)")
  742.     elseif type(y) ~= "number" then
  743.     error("bad argument #2 (y is supposed to be a number)")
  744.     elseif type(x2) ~= "number" then
  745.     error("bad argument #3 (x2 is supposed to be a number)")
  746.     end
  747.    
  748. local width,hight = term.getSize()
  749. returnTable = {["x1"]=x1,["y"]=y,["x2"]=x2,["content"]=content or {},["position"]=0,["isOpen"]=false,["maxLen"] = hight - y}
  750. setmetatable(returnTable,self)
  751. self.__index = self
  752. return returnTable
  753. end
  754. ,
  755.  
  756.  
  757. print = function(self)
  758.  
  759. local lastTextColor = term.getTextColor()
  760. local lastBackColor = term.getBackgroundColor()
  761.  
  762.     if self["isOpen"] then
  763.    
  764.         for i = 1, math.min(self["maxLen"],table.maxn(self["content"])) do
  765.            
  766.             if i%2 == 0 then
  767.             term.setBackgroundColor(colors.lightGray)
  768.             term.setTextColor(colors.black)
  769.             else
  770.             term.setBackgroundColor(colors.gray)
  771.             term.setTextColor(colors.white)
  772.             end
  773.            
  774.         term.setCursorPos(self["x1"],self["y"]+i-1)
  775.             if string.len(self["content"][i+self["position"]]) > self["x2"] - self["x1"] + 1 then
  776.             term.write(string.sub(self["content"][i+self["position"]],1,self["x2"] - self["x1"] - 2) .. "...")
  777.             else
  778.             term.write(self["content"][i+self["position"]])
  779.                 for i = 1, self["x2"] - self["x1"] + 1 - string.len(self["content"][i+self["position"]]) do
  780.                 term.write(" ")
  781.                 end
  782.             end
  783.        
  784.         end
  785.        
  786.         if math.min(self["maxLen"],table.maxn(self["content"])) < table.maxn(self["content"]) - self["position"] then
  787.         term.setCursorPos(self["x1"],self["y"]+math.min(self["maxLen"],table.maxn(self["content"]))-1)
  788.         term.write("+")
  789.             for i = 1, self["x2"] - self["x1"]  do
  790.             term.write(" ")
  791.             end
  792.         end
  793.        
  794.     else
  795.    
  796.     term.setBackgroundColor(colors.gray)
  797.     term.setTextColor(colors.white)
  798.     term.setCursorPos(self["x1"],self["y"])
  799.         if string.len(self["content"][1]) > self["x2"] - self["x1"] + 1 then
  800.        
  801.         term.write(string.sub(self["content"][1],1,self["x2"] - self["x1"] - 2) .. "...")
  802.         else
  803.        
  804.         term.write(self["content"][1])
  805.             for i = 1, self["x2"] - self["x1"] + 1 - string.len(self["content"][1]) do
  806.             term.write(" ")
  807.             end
  808.         end
  809.    
  810.     end
  811.    
  812. term.setTextColor(lastTextColor)
  813. term.setBackgroundColor(lastBackColor)
  814. end
  815. ,
  816.  
  817.  
  818. check = function(self)
  819. local returnMessage = nil
  820.  
  821.     if info1 == "mouse_click" then
  822.    
  823.         if info3 >= self["x1"] and info3 <= self["x2"] and info4 == self["y"] and self["position"] == 0 then -- if first element is clicked
  824.             if self["isOpen"] then
  825.             self:close()
  826.             else
  827.             self:open()
  828.             end
  829.        
  830.         elseif info3 >= self["x1"] and info3 <= self["x2"] and info4 > self["y"]-1 and info4 < self["y"] + self["maxLen"] and self["isOpen"] then -- if is open and an element thats not the first one is clicked
  831.        
  832.         local clickedPos =  info4 - self["y"] + 1
  833.             if clickedPos == self["maxLen"] and self["maxLen"] + self["position"] < table.maxn(self["content"]) then -- checks if "+" is clicked
  834.             self["position"] = self["position"] + 1
  835.             self:print()
  836.             else -- changes the first element to the clicked one
  837.             local contentSave = self["content"][1]
  838.             self["content"][1] = self["content"][clickedPos + self["position"]]
  839.             self["content"][clickedPos + self["position"]] = contentSave
  840.             self:close()
  841.             returnMessage = "changed"
  842.             end
  843.        
  844.         info1 = nil
  845.         end
  846.    
  847.     end
  848.    
  849.    
  850.     if info1 == "mouse_scroll" and self["isOpen"] then -- checks for scrolling
  851.         if self["position"] + info2 + self["maxLen"] <= table.maxn(self["content"]) and self["position"] + info2 > -1 then
  852.         self["position"] = self["position"] + info2
  853.         curEnv()
  854.         end
  855.     end
  856.    
  857. return returnMessage
  858. end
  859. ,
  860.  
  861.  
  862. close = function(self)
  863. self["isOpen"] = false
  864. self["position"] = 0
  865. curEnv()
  866. end
  867. ,
  868.  
  869.  
  870. open = function(self)
  871. self["isOpen"] = true
  872. self["position"] = 0
  873. curEnv()
  874. end
  875. ,
  876.  
  877.  
  878. getOutput = function(self,index)
  879. return self["content"][index or 1]
  880. end
  881. ,
  882.  
  883.  
  884. changePos = function(self,x1,y,x2)
  885. self["x1"],self["y"],self["x2"] = x1,y,x2
  886. end
  887. }
  888.  
  889.  
  890.  
  891. function filledSquare(fill_x1,fill_y1,fill_x2,fill_y2,fill_color)
  892. term.setBackgroundColor(fill_color)
  893. curr_fill_pos_x=fill_x1
  894. curr_fill_pos_y=fill_y1
  895.   while curr_fill_pos_y~=fill_y2+1 do
  896.     while curr_fill_pos_x~=fill_x2+1 do
  897.     term.setCursorPos(curr_fill_pos_x,curr_fill_pos_y)
  898.     term.write(" ")
  899.     curr_fill_pos_x=curr_fill_pos_x+1
  900.     end
  901.   curr_fill_pos_x=fill_x1
  902.   curr_fill_pos_y=curr_fill_pos_y+1
  903.   end
  904. term.setBackgroundColor(colors.black)
  905. end
  906.  
  907.  
  908.  
  909. function lineCheck(y)
  910.  
  911.     if info1 == "mouse_click" and info4 == y then
  912.     return true
  913.     else
  914.     return false
  915.     end
  916.  
  917. end
  918.  
  919. -------------------------------------------------
  920.  
  921.  
  922. interfaceStartup()
  923. homeMenu()
  924. interfaceMain()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement