Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- orderFunctions = {}
- function interfaceStartup()
- while not mainIsDoneWithStartup do
- sleep(0.1)
- if multishell.getTitle(1) ~= "startup" then
- error()
- end
- end
- for k,v in pairs(peripheral.getNames()) do
- if peripheral.getType(peripheral.getNames()[k]) == "modem" then
- rednet.open(peripheral.getNames()[k])
- end
- end
- defaultOrders = {
- ["restockSeeds"] = {["param1Options"] = "seedNames", ["param1Name"] = "Seed name:", ["priority"] = 4},
- ["produceEssence"] = {["param1Options"] = "essenceNames", ["param1Name"] = "Essence name:", ["param2Options"] = "#n", ["param2Name"] = "Amount:", ["priority"] = 5}
- }
- defaultOrderNames = {}
- for k, v in pairs(defaultOrders) do
- table.insert(defaultOrderNames, k)
- end
- orderTypesDropdown = DropdownMenu:new(1,4,40,defaultOrderNames)
- tickTimer = os.startTimer(1)
- end
- function interfaceMain()
- while true do
- info1,info2,info3,info4 = os.pullEvent()
- if multishell.getTitle(1) ~= "startup" then
- error()
- end
- if info1 == "rednet_message" then
- if type(info3) == "table" then
- else
- end
- end
- if info1 == "timer" then
- if info2 == tickTimer then
- curEnv()
- tickTimer = os.startTimer(1)
- end
- end
- if curEnv == homeMenu then
- if buttonCheck(17,4,24,6) then
- ordersMenu()
- end
- if buttonCheck(27,4,33,6) then
- seedsMenu()
- end
- end
- if curEnv == ordersMenu then
- local scrollOut = checkScrollmenu("orderScrollMenu")
- if scrollOut then
- orderInfoMenu(scrollmenuOrderKeys[scrollOut])
- end
- if buttonCheck(46,16,51,18) then
- homeMenu()
- end
- if buttonCheck(38,12,51,14) then
- openCreateOrderMenu()
- end
- end
- if curEnv == createOrderMenu then
- if orderTypesDropdown:check() then
- openCreateOrderMenu()
- end
- if param1Dropdown then
- param1Dropdown:check()
- elseif param1Reader then
- param1Reader:read()
- end
- if param2Dropdown then
- param2Dropdown:check()
- elseif param2Reader then
- param2Reader:read()
- end
- if buttonCheck(46,16,51,18) then
- ordersMenu()
- end
- if buttonCheck(1,16,9,18) then
- if param1Dropdown then
- param1 = param1Dropdown:getOutput()
- elseif param1Reader then
- param1 = param1Reader:getOutput()
- end
- if param2Dropdown then
- param2 = param2Dropdown:getOutput()
- elseif param2Reader then
- param2 = param2Reader:getOutput()
- end
- param2 = tonumber(param2)
- table.insert(requestsFromInterface,{["action"]="createOrder",["functionName"]=orderTypesDropdown:getOutput(),["param1"]=param1,["param2"]=param2,["priority"]=defaultOrders[orderTypesDropdown:getOutput()]["priority"]})
- ordersMenu()
- end
- end
- if curEnv == orderInfoMenu then
- if buttonCheck(46,16,51,18) then
- ordersMenu()
- end
- if buttonCheck(1,16,14,18) then
- table.insert(requestsFromInterface,{["action"]="cancelOrder",["orderKey"]=curOrderKey})
- end
- end
- if curEnv == seedsMenu then
- scrollOut = checkScrollmenu("seedsScrollmenu")
- if buttonCheck(46,16,51,18) then
- homeMenu()
- end
- if buttonCheck(42,12,51,14) then
- checkUnregisteredSeeds()
- end
- end
- if curEnv == registerSeedMenu then
- restockSeedDropdown:check()
- term.setTextColor(colors.white)
- seedDropReader:read()
- if buttonCheck(46,16,51,18) then
- seedsMenu()
- end
- if buttonCheck(1,16,9,18) then
- table.insert(requestsFromInterface, {["action"]="registerSeed",["seedName"]=curSeed,["drop"]=seedDropReader:getOutput(), ["restockSeed"]=restockSeedDropdown:getOutput() == "true"})
- seedsMenu()
- end
- end
- end
- end
- function seedsMenu()
- curEnv = seedsMenu
- term.clear()
- headline("Seeds")
- if scrollmenuInfos["seedsScrollmenu"] then
- position = scrollmenuInfos["seedsScrollmenu"]["position"]
- else
- position = 0
- end
- createScrollmenu("seedsScrollmenu",1,3,40,18,seedNames,position)
- printScrollmenu("seedsScrollmenu")
- button("Back",46,16,51,18)
- button("Register",42,12,51,14)
- end
- function checkUnregisteredSeeds()
- for k,v in pairs(seedStoragePeripheral.list()) do
- if not table.contains(seedNames,v["name"]) then
- openRegisterSeedMenu(v["name"])
- break
- end
- end
- end
- function openRegisterSeedMenu(seedName)
- seedDropReader = BetterRead:new(7,6)
- seedDropReader["output"] = string.sub(seedName,1,string.find(seedName,":"))
- seedDropReader:startReading()
- restockSeedDropdown = DropdownMenu:new(1,9,5,{"true", "false"})
- registerSeedMenu(seedName)
- end
- function registerSeedMenu(seedName)
- curSeed = seedName or curSeed
- seedName = curSeed
- curEnv = registerSeedMenu
- term.clear()
- headline("Register seed")
- button("Back",46,16,51,18)
- button("Confirm",1,16,9,18)
- term.setCursorPos(1,4)
- term.write("Seed name: ")
- term.setTextColor(colors.white)
- print(seedName)
- term.setCursorPos(1,6)
- term.setTextColor(colors.green)
- term.write("Drop: ")
- term.setTextColor(colors.white)
- term.write(seedDropReader:getOutput())
- if seedDropReader["isReading"] and seedDropReader["blinkIsThere"] then
- print("_")
- end
- term.setTextColor(colors.green)
- term.setCursorPos(1,8)
- print("Restock seed:")
- term.setTextColor(colors.white)
- restockSeedDropdown:print()
- end
- function openCreateOrderMenu()
- param1Name = nil
- param2Name = nil
- param1Options = nil
- param2Options = nil
- param1Dropdown = nil
- param2Dropdown = nil
- param1Reader = nil
- param2Reader = nil
- if defaultOrders[orderTypesDropdown:getOutput()]["param1Options"] then
- param1Name = defaultOrders[orderTypesDropdown:getOutput()]["param1Name"]
- if not string.find(defaultOrders[orderTypesDropdown:getOutput()]["param1Options"],"#") then
- param1Reader = nil
- param1Options = _ENV[defaultOrders[orderTypesDropdown:getOutput()]["param1Options"]]
- param1Dropdown = DropdownMenu:new(1,7,40,param1Options)
- else
- param1Dropdown = nil
- param1Options = defaultOrders[orderTypesDropdown:getOutput()]["param1Options"]
- if param1Options == "#n" then
- param1Reader = BetterRead:new(1,7,false,{"1","2","3","4","5","6","7","8","9","0"})
- param1Reader:startReading()
- end
- end
- end
- if defaultOrders[orderTypesDropdown:getOutput()]["param2Options"] then
- param2Name = defaultOrders[orderTypesDropdown:getOutput()]["param2Name"]
- if not string.find(defaultOrders[orderTypesDropdown:getOutput()]["param2Options"],"#") then
- param2Reader = nil
- param2Options = _ENV[defaultOrders[orderTypesDropdown:getOutput()]["param2Options"]]
- param2Dropdown = DropdownMenu:new(1,10,40,param2Options)
- else
- param2Dropdown = nil
- param2Options = defaultOrders[orderTypesDropdown:getOutput()]["param2Options"]
- if param2Options == "#n" then
- param2Reader = BetterRead:new(1,10,false,{"1","2","3","4","5","6","7","8","9","0"})
- param2Reader:startReading()
- end
- end
- end
- createOrderMenu()
- end
- function createOrderMenu()
- curEnv = createOrderMenu
- term.clear()
- headline("Create order")
- button("Back",46,16,51,18)
- button("Confirm",1,16,9,18)
- term.setCursorPos(1,3)
- term.setTextColor(colors.green)
- print("Function type:")
- orderTypesDropdown:print()
- if param1Name then
- term.setCursorPos(1,6)
- print(param1Name)
- end
- if param2Name then
- term.setCursorPos(1,9)
- print(param2Name)
- end
- term.setTextColor(colors.white)
- if param1Dropdown then
- param1Dropdown:print()
- elseif param1Reader then
- print(param1Reader:getOutput())
- if param1Reader["blinkIsThere"] and param1Reader["isReading"] then
- print("_")
- end
- end
- if param2Dropdown then
- param2Dropdown:print()
- elseif param2Reader then
- term.setCursorPos(1,10)
- term.write(param2Reader:getOutput())
- if param2Reader["blinkIsThere"] and param2Reader["isReading"] then
- print("_")
- end
- end
- end
- function homeMenu()
- curEnv = homeMenu
- term.clear()
- headline("Home")
- button("Orders",17,4,24,6)
- button("Seeds",27,4,33,6)
- end
- function orderInfoMenu(orderKey)
- if orders[orderKey or curOrderKey] then
- curEnv = orderInfoMenu
- curOrderKey = orderKey or curOrderKey
- orderKey = curOrderKey
- term.clear()
- headline("Order " .. orderKey)
- term.setCursorPos(1,3)
- term.setTextColor(colors.green)
- term.write("Order key (ID): ")
- term.setTextColor(colors.white)
- print(orderKey)
- term.setTextColor(colors.green)
- term.write("Function name: ")
- term.setTextColor(colors.white)
- print(orders[orderKey]["functionName"])
- term.setTextColor(colors.green)
- term.write("Parameter 1: ")
- term.setTextColor(colors.white)
- print(orders[orderKey]["param1"])
- term.setTextColor(colors.green)
- term.write("Parameter 2: ")
- term.setTextColor(colors.white)
- print(orders[orderKey]["param2"])
- term.setTextColor(colors.green)
- term.write("Pot amount: ")
- term.setTextColor(colors.white)
- print(orders[orderKey]["potAmount"])
- term.setTextColor(colors.green)
- term.write("Priority: ")
- term.setTextColor(colors.white)
- print(orders[orderKey]["priority"])
- button("Back",46,16,51,18)
- button("Cancel order",1,16,14,18,colors.red)
- else
- ordersMenu()
- end
- end
- function ordersMenu()
- curEnv = ordersMenu
- term.clear()
- headline("Order menu")
- button("Back",46,16,51,18)
- button("Create order",38,12,51,14)
- createOrderScrollmenu()
- printScrollmenu("orderScrollMenu")
- end
- function createOrderScrollmenu()
- local content = {}
- scrollmenuOrderKeys = {}
- for k, order in pairs(orders) do
- table.insert(content, k .. ": " .. order["functionName"])
- table.insert(scrollmenuOrderKeys, k)
- end
- if scrollmenuInfos["orderScrollMenu"] then
- createScrollmenu("orderScrollMenu",1,2,20,16,content,scrollmenuInfos["orderScrollMenu"]["position"])
- else
- createScrollmenu("orderScrollMenu",1,2,20,16,content,0)
- end
- end
- -- buttons and all that stuff
- BetterRead = {
- new = function(self,xPos,yPos,isBlacklist,filter)
- if type(xPos) ~= "number" then
- error("bad argument #1 (xPos is supposed to be a number)")
- elseif type(yPos) ~= "number" then
- error("bad argument #2 (yPos is supposed to be a number)")
- elseif type(isBlacklist) ~= "boolean" then
- isBlacklist = true
- end
- returnTable = {["xPos"]=xPos,["yPos"]=yPos,["isBlacklist"]=isBlacklist,["filter"]=filter or {},["isReading"]=false,["output"]="",["blinkIsThere"]=false}
- setmetatable(returnTable,self)
- self.__index = self
- return returnTable
- end
- ,
- startReading = function(self)
- self["isReading"] = true
- self["blinkTimer"] = os.startTimer(0.4)
- self["blinkIsThere"] = true
- term.setCursorPos(self["xPos"],self["yPos"])
- term.write(self["output"] .. "_")
- end
- ,
- stopReading = function(self)
- self["isReading"] = false
- if self["blinkIsThere"] then
- term.setCursorPos(self["xPos"]+string.len(self["output"]),self["yPos"])
- term.write(" ")
- end
- end
- ,
- read = function(self)
- if self["isReading"] then
- if info1 == "key" then
- if info2 == keys.backspace and string.len(self["output"]) > 0 then
- term.setCursorPos(self["xPos"]+string.len(self["output"])-1,self["yPos"])
- if self["blinkIsThere"] then
- term.write("_ ")
- else
- term.write(" ")
- end
- self["output"] = string.sub(self["output"],1,string.len(self["output"])-1)
- end
- if info2 == keys.enter then
- self:stopReading()
- info1 = nil
- return true
- end
- end
- if info1 == "timer" and info2 == self["blinkTimer"] then
- self["blinkIsThere"] = not self["blinkIsThere"]
- if self["blinkIsThere"] then
- term.setCursorPos(self["xPos"]+string.len(self["output"]),self["yPos"])
- term.write("_")
- else
- term.setCursorPos(self["xPos"]+string.len(self["output"]),self["yPos"])
- term.write(" ")
- end
- self["blinkTimer"] = os.startTimer(0.5)
- end
- if info1 == "char" then
- if not self["isBlacklist"] then
- for k,v in pairs(self["filter"]) do
- if info2 == v then
- self["output"] = self["output"] .. info2
- term.setCursorPos(self["xPos"],self["yPos"])
- if self["blinkIsThere"] then
- term.write(self["output"] .. "_")
- else
- term.write(self["output"])
- end
- end
- end
- else
- for k,v in pairs(self["filter"]) do
- if info2 == v then
- return
- end
- end
- self["output"] = self["output"] .. info2
- term.setCursorPos(self["xPos"],self["yPos"])
- if self["blinkIsThere"] then
- term.write(self["output"] .. "_")
- else
- term.write(self["output"])
- end
- end
- end
- else
- if info1 == "mouse_click" and info4 == self["yPos"] then
- self:startReading()
- end
- end
- end
- ,
- getOutput = function(self)
- return self["output"]
- end
- }
- function resetEvent()
- info1 = nil
- end
- function createScrollmenu(name,x1,y1,x2,y2,content,position)
- scrollmenuLen[name]=0
- scrollmenuInfos[name]={["x1"]=x1,["y1"]=y1,["x2"]=x2,["y2"]=y2,["content"]=content,["position"]=position,["previewText"]={}}
- for k,v in pairs(content) do
- scrollmenuLen[name]=scrollmenuLen[name]+1
- end
- for k,v in pairs(content) do
- if string.len(v) > x2 - x1 + 1 then
- scrollmenuInfos[name]["previewText"][k] = string.sub(v,1,x2-x1-2) .. "..."
- else
- scrollmenuInfos[name]["previewText"][k] = v
- end
- end
- end
- function printScrollmenu(name)
- local lastBackColor = term.getBackgroundColor()
- local lastTextColor = term.getTextColor()
- term.setTextColor(colors.white)
- for i=1,math.min(scrollmenuLen[name],scrollmenuInfos[name]["y2"]-scrollmenuInfos[name]["y1"]+1) do
- if (i+scrollmenuInfos[name]["position"])%2==0 then
- term.setBackgroundColor(colors.gray)
- else
- term.setBackgroundColor(colors.black)
- end
- term.setCursorPos(scrollmenuInfos[name]["x1"],scrollmenuInfos[name]["y1"]+i-1)
- term.write(scrollmenuInfos[name]["previewText"][i+scrollmenuInfos[name]["position"]])
- for i2=1,scrollmenuInfos[name]["x2"]-scrollmenuInfos[name]["x1"]+1-string.len(scrollmenuInfos[name]["content"][i+scrollmenuInfos[name]["position"]]) do
- term.write(" ")
- end
- end
- term.setBackgroundColor(lastBackColor)
- term.setTextColor(lastTextColor)
- end
- function checkScrollmenu(name)
- 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
- resetEvent()
- return info4-scrollmenuInfos[name]["y1"]+1+scrollmenuInfos[name]["position"]
- end
- 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
- scrollmenuInfos[name]["position"] = scrollmenuInfos[name]["position"] + info2
- printScrollmenu(name)
- end
- end
- function headline(headline)
- width = term.getSize()
- headline_lenght=string.len(" "..headline.." ")
- headline_pos=(width/2)-(headline_lenght/2)+1
- term.setCursorPos(headline_pos,1)
- term.setBackgroundColor(colors.yellow)
- term.setTextColor(colors.blue)
- term.write(" "..headline.." ")
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- end
- function button(button_text,button_pos_x1,button_pos_y1,button_pos_x2,button_pos_y2,button_color)
- term.setTextColor(colors.green)
- if button_color~=nil then
- term.setTextColor(button_color)
- end
- square(button_pos_x1,button_pos_y1,button_pos_x2,button_pos_y2,colors.black,"-","|")
- 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)
- term.write(button_text)
- end
- function buttonCheck(b_check_x1,b_check_y1,b_check_x2,b_check_y2)
- 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
- resetEvent()
- return true
- else
- return false
- end
- end
- function square(x1,y1,x2,y2,back_color_square,symbol_square_x,symbol_square_y,text_color)
- x_1=x1
- y_1=y1
- x_2=x2
- y_2=y2
- while x_1~=x2+1 do
- term.setBackgroundColor(back_color_square)
- term.setCursorPos(x_1,y_1)
- if text_color~=nil then
- term.setTextColor(text_color)
- end
- term.write(symbol_square_x)
- x_1=x_1+1
- end
- while y_1~=y2-1 do
- term.setCursorPos(x_1-1,y_1+1)
- term.write(symbol_square_y)
- y_1=y_1+1
- end
- x2=x2-1
- while x_2~=x1-1 do
- term.setCursorPos(x_2,y_2)
- term.write(symbol_square_x)
- x_2=x_2-1
- end
- while y_2~=y1+1 do
- term.setCursorPos(x_2+1,y_2-1)
- term.write(symbol_square_y)
- y_2=y_2-1
- end
- term.setBackgroundColor(colors.black)
- end
- DropdownMenu = {
- new = function(self,x1,y,x2,content)
- if type(x1) ~= "number" then
- error("bad argument #1 (x1 is supposed to be a number)")
- elseif type(y) ~= "number" then
- error("bad argument #2 (y is supposed to be a number)")
- elseif type(x2) ~= "number" then
- error("bad argument #3 (x2 is supposed to be a number)")
- end
- local width,hight = term.getSize()
- returnTable = {["x1"]=x1,["y"]=y,["x2"]=x2,["content"]=content or {},["position"]=0,["isOpen"]=false,["maxLen"] = hight - y}
- setmetatable(returnTable,self)
- self.__index = self
- return returnTable
- end
- ,
- print = function(self)
- local lastTextColor = term.getTextColor()
- local lastBackColor = term.getBackgroundColor()
- if self["isOpen"] then
- for i = 1, math.min(self["maxLen"],table.maxn(self["content"])) do
- if i%2 == 0 then
- term.setBackgroundColor(colors.lightGray)
- term.setTextColor(colors.black)
- else
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- end
- term.setCursorPos(self["x1"],self["y"]+i-1)
- if string.len(self["content"][i+self["position"]]) > self["x2"] - self["x1"] + 1 then
- term.write(string.sub(self["content"][i+self["position"]],1,self["x2"] - self["x1"] - 2) .. "...")
- else
- term.write(self["content"][i+self["position"]])
- for i = 1, self["x2"] - self["x1"] + 1 - string.len(self["content"][i+self["position"]]) do
- term.write(" ")
- end
- end
- end
- if math.min(self["maxLen"],table.maxn(self["content"])) < table.maxn(self["content"]) - self["position"] then
- term.setCursorPos(self["x1"],self["y"]+math.min(self["maxLen"],table.maxn(self["content"]))-1)
- term.write("+")
- for i = 1, self["x2"] - self["x1"] do
- term.write(" ")
- end
- end
- else
- term.setBackgroundColor(colors.gray)
- term.setTextColor(colors.white)
- term.setCursorPos(self["x1"],self["y"])
- if string.len(self["content"][1]) > self["x2"] - self["x1"] + 1 then
- term.write(string.sub(self["content"][1],1,self["x2"] - self["x1"] - 2) .. "...")
- else
- term.write(self["content"][1])
- for i = 1, self["x2"] - self["x1"] + 1 - string.len(self["content"][1]) do
- term.write(" ")
- end
- end
- end
- term.setTextColor(lastTextColor)
- term.setBackgroundColor(lastBackColor)
- end
- ,
- check = function(self)
- local returnMessage = nil
- if info1 == "mouse_click" then
- if info3 >= self["x1"] and info3 <= self["x2"] and info4 == self["y"] and self["position"] == 0 then -- if first element is clicked
- if self["isOpen"] then
- self:close()
- else
- self:open()
- end
- 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
- local clickedPos = info4 - self["y"] + 1
- if clickedPos == self["maxLen"] and self["maxLen"] + self["position"] < table.maxn(self["content"]) then -- checks if "+" is clicked
- self["position"] = self["position"] + 1
- self:print()
- else -- changes the first element to the clicked one
- local contentSave = self["content"][1]
- self["content"][1] = self["content"][clickedPos + self["position"]]
- self["content"][clickedPos + self["position"]] = contentSave
- self:close()
- returnMessage = "changed"
- end
- info1 = nil
- end
- end
- if info1 == "mouse_scroll" and self["isOpen"] then -- checks for scrolling
- if self["position"] + info2 + self["maxLen"] <= table.maxn(self["content"]) and self["position"] + info2 > -1 then
- self["position"] = self["position"] + info2
- curEnv()
- end
- end
- return returnMessage
- end
- ,
- close = function(self)
- self["isOpen"] = false
- self["position"] = 0
- curEnv()
- end
- ,
- open = function(self)
- self["isOpen"] = true
- self["position"] = 0
- curEnv()
- end
- ,
- getOutput = function(self,index)
- return self["content"][index or 1]
- end
- ,
- changePos = function(self,x1,y,x2)
- self["x1"],self["y"],self["x2"] = x1,y,x2
- end
- }
- function filledSquare(fill_x1,fill_y1,fill_x2,fill_y2,fill_color)
- term.setBackgroundColor(fill_color)
- curr_fill_pos_x=fill_x1
- curr_fill_pos_y=fill_y1
- while curr_fill_pos_y~=fill_y2+1 do
- while curr_fill_pos_x~=fill_x2+1 do
- term.setCursorPos(curr_fill_pos_x,curr_fill_pos_y)
- term.write(" ")
- curr_fill_pos_x=curr_fill_pos_x+1
- end
- curr_fill_pos_x=fill_x1
- curr_fill_pos_y=curr_fill_pos_y+1
- end
- term.setBackgroundColor(colors.black)
- end
- function lineCheck(y)
- if info1 == "mouse_click" and info4 == y then
- return true
- else
- return false
- end
- end
- -------------------------------------------------
- interfaceStartup()
- homeMenu()
- interfaceMain()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement