Advertisement
Chaos_Cash

pocketStorage.lua

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