t8rt0t

screenAPI.lua

Apr 17th, 2024 (edited)
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 24.41 KB | None | 0 0
  1. --vvvvvvvvvv Delete the lines below if you do not use docs vvvvvvvvvv
  2.  
  3. if not docs then os.loadAPI("apis/docs.lua") end
  4. --Type registry
  5. local types = {
  6.     ["Button"]={colors.brown,"A button that can be clicked and stuff idk"},
  7.     ["FormField"]={colors.brown,"A button that can be used to enter strings"},
  8.     ["TextBox"]={colors.brown,"A generic text box that cannot be interacted with"},
  9.     ["Toggle"]={colors.brown,"A switch that can be turned off and on"},
  10.     ["Dropdown"]={colors.brown,"A button that can be switched between several items"},
  11.     ["Screen"]={colors.pink,"A generic screen that can contain buttons"},
  12.     ["SelectionScreen"]={colors.pink,"A screen that allows you to select an item from a list"},
  13.     ["ScrollScreen"]={colors.pink,"A screen that can be scrolled up and down, NYI"},
  14.     ["SettingsScreen"]={colors.pink,"A screen where you can modify settings"}
  15. }
  16. for k,v in pairs(types) do
  17.     docs.registerType(k,v[1],v[2])
  18. end
  19.  
  20. --^^^^^^^^^^ Delete the lines above if you do not use docs ^^^^^^^^^^
  21.  
  22. section_char = string.sub(utf8.char(167),2,2)
  23.  
  24. --Creates a button object
  25. --@Arg number x: The x position of the top left part of the button
  26. --@Arg number y: The y position of the top left part of the button
  27. --@Arg string text: The text of the button
  28. --@OptionalArg number w: The width the button should be, defaulting to the text length
  29. --@OptionalArg number h: The height the button should be, defaulting to 1
  30. --@OptionalArg Color color: The text color of the button, defaulting to green
  31. --@OptionalArg Color background: The background color of the button
  32. --@OptionalArg function clickFunction: What the button should do when it gets clicked
  33. --@OptionalArg string buttonType: The type of button it is, defaulting to "default"
  34. --@Returns Button button: The created button
  35. function Button(x,y,text,w,h,color,background,clickFunction,t)
  36.     local btn = {
  37.         text = text,
  38.         x = x,
  39.         y = y,
  40.         w = w or #text,
  41.         h = h or 1,
  42.         color = color or colors.green,
  43.         background = background or colors.black,
  44.         rendered = false,
  45.         click = clickFunction or function() return (t or "default"), text end,
  46.         buttonType = t or "default"
  47.     }
  48.     function btn:render()
  49.         local x,y = term.getCursorPos()
  50.         local textColor = term.getTextColor()
  51.         local backgroundColor = term.getBackgroundColor()
  52.         term.setCursorPos(self.x,self.y)
  53.         term.setTextColor(self.color)
  54.         term.setBackgroundColor(self.background)
  55.         local index = 1
  56.         for j=1,self.h do
  57.             for i=1,self.w do
  58.                 local char = string.sub(self.text,(j-1)*self.w+i,(j-1)*self.w+i)
  59.                 if type(char) ~= "string" or #char ~= 1 then char = " " end
  60.                 term.write(char)
  61.             end
  62.             term.setCursorPos(self.x,self.y + j)
  63.         end
  64.         term.setCursorPos(x,y)
  65.         term.setTextColor(textColor)
  66.         term.setBackgroundColor(backgroundColor)
  67.         self.rendered = true
  68.     end
  69.     function btn:unrender()
  70.         local x,y = term.getCursorPos()
  71.         term.setCursorPos(self.x,self.y)
  72.         for j=1,self.h do
  73.             for i=1,self.w do
  74.                 term.write(" ")
  75.             end
  76.             term.setCursorPos(self.x,self.y + j)
  77.         end
  78.         term.setCursorPos(x,y)
  79.         self.rendered = false
  80.     end
  81.     function btn:isClicked(x,y)
  82.         return self.rendered and x >= self.x and x < self.x + self.w and y >= self.y and y < self.y + self.h
  83.     end
  84.     btn:render()
  85.     return btn
  86. end
  87. --Creates a non interactive text box
  88. --@Arg number x: The x coordinate of top left corner
  89. --@Arg number y: The y coordinate of top left corner
  90. --@Arg string text: The text to display
  91. --@OptionalArg number w: The width of the box
  92. --@OptionalArg number h: The height of the box
  93. --@OptionalArg Color color: The text color of the text
  94. --@OptionalArg Coloc background: The background color of the box
  95. --@Returns TextBox box: The text box
  96. function TextBox(x,y,text,w,h,color,background)
  97.     local button = Button(x,y,text,w,h,color,background,function() return end,"text")
  98.     function button:isClicked(x,y)
  99.         return false
  100.     end
  101.     return button
  102. end
  103. --Creates a button that you can type in
  104. --@Arg number x: The x position of the top left part of the form field
  105. --@Arg number y: The y position of the top left part of the button
  106. --@Arg string text: The text of the form field
  107. --@OptionalArg number w: The width the form field should be, defaulting to the text length + max Length
  108. --@OptionalArg number h: The height the form field should be, defaulting to 2
  109. --@OptionalArg Color color: The text color of the button, defaulting to green
  110. --@OptionalArg Color background: The background color of the button
  111. --@OptionalArg boolean secure: Whether the text that gets entered should be hidden or not
  112. --@OptionalArg number maxLength: The maximum length of the password
  113. --@OptionalArg function charFilter: The filter function for the char
  114. --@Returns Button button: The created button
  115. function FormField(x,y,text,w,h,color,background,secure,maxLength,charFilter)
  116.     h = tonumber(h) or 2
  117.     if h > 1 then w = math.max(w or #text,maxLength or 16)
  118.     else w = #text+(maxLength or 16) end
  119.     entryColor = entryColor or colors.yellow
  120.     if type(charFilter) ~= "function" then charFilter = function(char) return char end end
  121.     local function clickFunction(this)
  122.         local _,key
  123.         local oldBlink = term.getCursorBlink()
  124.         term.setCursorBlink(true)
  125.         repeat
  126.             if this.h == 1 then
  127.                 term.setCursorPos(this.x + #this.text,this.y)
  128.             else
  129.                 term.setCursorPos(this.x+1+#this.pass,this.y+1)
  130.             end
  131.             _,key = os.pullEvent("key")
  132.             if key <= 256 and #this.pass <= this.maxLength then
  133.                 local _,char = os.pullEvent("char")
  134.                 this.pass = this.pass .. charFilter(char)
  135.                 this:updateText()
  136.             elseif key == keys.delete or key == keys.backspace then
  137.                 this.pass = string.sub(this.pass,1,-2)
  138.                 this:updateText()
  139.             end
  140.         until key == keys.enter
  141.         term.setCursorBlink(oldBlink)
  142.         return this.type,this.header,this.pass,this.name
  143.     end
  144.     local button = Button(x,y,text,w,h,color,background,clickFunction,"form")
  145.     button.pass = ""
  146.     button.header = text
  147.     button.secure = secure
  148.     button.maxLength = maxLength
  149.     button.charFilter = charFilter
  150.     button.type = "form"
  151.     function button:updateText()
  152.         local displayPass = self.pass
  153.         if self.secure then displayPass = string.rep("*",#self.pass) end
  154.         if self.h == 1 then
  155.             self.text = self.header .. displayPass
  156.         elseif self.h >= 2 then
  157.             if #self.header > 0 then
  158.                 self.text = self.header .. string.rep(" ",self.w-#self.header) .. ">" .. displayPass
  159.             else
  160.                 self.text = displayPass
  161.             end
  162.         end
  163.         self:render()
  164.     end
  165.     button:updateText()
  166.     return button
  167. end
  168. --Creates a toggle button
  169. --@Arg number x: The x position of the button
  170. --@Arg number y: The y position of the button
  171. --@OptionalArg boolean startActive: Whether the button should start toggled or not
  172. function Toggle(x,y,startActive)
  173.     startActive = startActive == true
  174.     local button = Button(x,y,"  ",2,1,colors.white,colors.gray,nil,"toggle")
  175.     button.state = startActive
  176.     function button:render()
  177.         local x,y = term.getCursorPos()
  178.         local textColor = term.getTextColor()
  179.         local backgroundColor = term.getBackgroundColor()
  180.         term.setCursorPos(self.x,self.y)
  181.         term.setTextColor(self.color)
  182.         term.setBackgroundColor(self.background)
  183.         if self.state then
  184.             term.blit(" o","00","85")
  185.         else
  186.             term.blit("x ","00","e8")
  187.         end
  188.         term.setCursorPos(x,y)
  189.         term.setTextColor(textColor)
  190.         term.setBackgroundColor(backgroundColor)
  191.         self.rendered = true
  192.     end
  193.     function button:click(x,y)
  194.         if x == self.x and y == self.y then
  195.             self.state = false
  196.         elseif x == self.x + 1 and y == self.y then
  197.             self.state = true
  198.         end
  199.         self:render()
  200.         return self.type, self.state, self.name
  201.     end
  202.     return button
  203. end
  204.    
  205. --Gets the button clicked inside a list, waiting until one is clicked
  206. --@Arg table buttons: The buttons to check
  207. --@OptionalArg Screen screen: The screen the button is a part of
  208. --@Returns Button button: The selected button
  209. function getButtonClicked(buttons,screen,allowNil)
  210.     --local e,b,x,y = os.pullEvent("mouse_click")
  211.     local xOffset,yOffset = 0,0
  212.     if screen then xOffset,yOffset = screen.x - 1, screen.y - 1 end
  213.     local selectedButton
  214.     -- for k,v in pairs(buttons) do
  215.     --     if v:isClicked(x,y) then selectedButton = v end
  216.     -- end
  217.     while not selectedButton do
  218.         local e,b,x,y = os.pullEvent("mouse_click")
  219.         for k,v in pairs(buttons) do
  220.             if v:isClicked(x-xOffset,y-yOffset) then return v,x,y,xOffset,yOffset end --selectedButton = v end
  221.         end
  222.         if allowNil then return nil,x,y end
  223.     end
  224.     return selectedButton,x,y,xOffset,yOffset
  225. end
  226. --Creates a screen with interactive buttons
  227. --@OptionalArg number x: The top left x coordinate
  228. --@OptinalArg number y: The top left y coordinate
  229. --@OptinalArg number w: The width of the screen
  230. --@OptionalArg number h: The height of the screen
  231. --@OptionalArg table b: The buttons to add on startup
  232. --@OptionalArg table t: The text boxes to add on startup
  233. --@Returns Screen screen: The newly created screen
  234. function Screen(x,y,w,h,b,t)
  235.     local maxX,maxY = term.getSize()
  236.     x = tonumber(x) or 1
  237.     y = tonumber(y) or 1
  238.     w = tonumber(w) or maxX - x + 1
  239.     h = tonumber(h) or maxY - x + 1
  240.     local screen = {
  241.         x = x,
  242.         y = y,
  243.         w = w,
  244.         h = h,
  245.         buttons = {},
  246.         rendered = true,
  247.         txtClr = colors.white,
  248.         bgClr = colors.black,
  249.         cursorX = 1,
  250.         cursorY = 1,
  251.         newTerm = false,
  252.         textBoxes = {}
  253.     }
  254.     screen.oldX,screen.oldY = term.getCursorPos()
  255.     screen.oldBG, screen.oldTxt = term.getBackgroundColor(), term.getTextColor()
  256.     screen.oldTerm = term.current()
  257.     screen.wndw = window.create(screen.oldTerm,x,y,w,h)
  258.     function screen:useOldTerm()
  259.         if self.newTerm then
  260.             self.txtClr = term.getTextColor()
  261.             self.bgClr = term.getBackgroundColor()
  262.             self.cursorX,self.cursorY = term.getCursorPos()
  263.             term.redirect(self.oldTerm)
  264.             term.setCursorPos(self.oldX,self.oldY)
  265.             term.setBackgroundColor(self.oldBG)
  266.             term.setTextColor(self.oldTxt)
  267.         end
  268.         self.newTerm = false
  269.     end
  270.     function screen:useNewTerm(txtClr,bgClr,x,y)
  271.         if not self.newTerm then
  272.             self.oldX,self.oldY = term.getCursorPos()
  273.             self.oldBG, self.oldTxt = term.getBackgroundColor(), term.getTextColor()
  274.             term.redirect(self.wndw)
  275.             self.txtClr = txtClr or self.txtClr
  276.             self.bgClr = bgClr or self.bgClr
  277.             self.cursorX = x or self.cursorX
  278.             self.cursorY = y or self.cursorY
  279.             term.setTextColor(self.txtClr)
  280.             term.setBackgroundColor(self.bgClr)
  281.             term.setCursorPos(self.cursorX,self.cursorY)
  282.         end
  283.         self.newTerm = true
  284.     end
  285.     function screen:addButton(button)
  286.         term.redirect(self.wndw)
  287.         table.insert(self.buttons,button)
  288.         self:useOldTerm()
  289.     end
  290.     function screen:setButtons(newButtons)
  291.         term.redirect(self.wndw)
  292.         if type(newButtons) ~= "table" then
  293.             self:useOldTerm()
  294.         return end
  295.         self.buttons = newButtons
  296.         self:useOldTerm()
  297.     end
  298.     function screen:render(shouldRender,background)
  299.         if type(shouldRender) ~= "boolean" then shouldRender = true end
  300.         --if shouldRender == self.rendered then return end
  301.         --self.wndw.setVisible(shouldRender)
  302.         term.redirect(self.wndw)
  303.         if background then term.setBackgroundColor(background) end
  304.         term.clear()
  305.         for k,v in pairs(self.textBoxes) do
  306.             v:render()
  307.         end
  308.         for k,v in pairs(self.buttons) do
  309.             v:render()
  310.         end
  311.         self.rendered = shouldRender
  312.     end
  313.     function screen:waitForClick(f)
  314.         if type(f) ~= "function" then f = function(a,b) return b end end
  315.         if self.rendered then return f(self,getButtonClicked(self.buttons,self)) end
  316.     end
  317.     if type(b) == "table" then
  318.         screen:setButtons(b)
  319.     end
  320.     if type(t) == "table" then
  321.         screen.textBoxes = t
  322.     end
  323.     function screen:getClick(x,y)
  324.         if tonumber(x) and tonumber(y) and self.rendered then
  325.             x = tonumber(x) - self.x + 1
  326.             y = tonumber(y) - self.y + 1
  327.             for k,v in pairs(self.buttons) do
  328.                 if v:isClicked(tonumber(x),tonumber(y)) then return v,tonumber(x),tonumber(y) end
  329.             end
  330.         elseif self.rendered then return getButtonClicked(self.buttons,self,true) end
  331.         return nil,tonumber(x),tonumber(y)
  332.     end
  333.     return screen
  334. end
  335. --Creates a dropdown menu
  336. --@Arg number x: the x coordinate
  337. --@Arg number y: The y coordinate
  338. --@Arg table options: The options to choose from
  339. --@OptionalArg number w: The width of the dropdown
  340. function Dropdown(x,y,options,w)
  341.     local width = 0
  342.     local key
  343.     for k,v in pairs(options) do
  344.         if not key then key = k end
  345.         if not tonumber(w) then width = math.max(width,#v) end
  346.     end
  347.     local button = Button(x,y,options[key],tonumber(w) or width,1,colors.white,colors.cyan,nil,"dropdown")
  348.     button.key = key
  349.     button.options = options
  350.     function button:click()
  351.         local current = term.current()
  352.         local scroll = 0
  353.         local dHeight = math.min(#self.options,5)
  354.         local maxX,maxY = term.getSize()
  355.         local x,y = 1,1
  356.         if self.y + dHeight > maxY then
  357.             y = self.y + 1
  358.         else
  359.             y = self.y - dHeight
  360.         end
  361.         x = self.x + 1
  362.         local screen = Screen(x,y,w,dHeight)
  363.         term.redirect(screen.wndw)
  364.         local index = 1
  365.         for k,v in ipairs(self.options) do
  366.             if k == key then
  367.                 table.insert(screen.buttons,Button(screen.x,index,v,w,1,colors.white,colors.green,function() return "option",k,v end,"option"))
  368.             elseif index%2 == 1 then
  369.                 table.insert(screen.buttons,Button(screen.x,index,v,w,1,colors.white,colors.gray,function() return "option",k,v end,"option"))
  370.             else
  371.                 table.insert(screen.buttons,Button(screen.x,index,v,w,1,colors.white,colors.lightGray,function() return "option",k,v end,"option"))
  372.             end
  373.             index = index + 1
  374.         end
  375.         local buttonType,key,value
  376.         repeat
  377.             local e,b,x,y = os.pullEvent()
  378.             if e == "mouse_click" then
  379.                 local button,x,y = screen:getClick(x,y)
  380.                 if button then
  381.                     local t,k,v = button:click()
  382.                     if t and t.type == "option" then return self.type, v, self.options, self.name end
  383.                 end
  384.             elseif e == "key" then
  385.                 --TODO: add clamping (too lazy to do right now)
  386.                 if b == keys.down then
  387.                     term.clear()
  388.                     for k,v in pairs(screen.buttons) do
  389.                         v.y = v.y - 1
  390.                         v:render()
  391.                     end
  392.                 elseif b == keys.up then
  393.                     term.clear()
  394.                     for k,v in pairs(screen.buttons) do
  395.                         v.y = v.y + 1
  396.                         v:render()
  397.                     end
  398.                 end
  399.             end
  400.         until false
  401.     end
  402. end
  403. --Creates a screen where you can select an item from a list of items
  404. --@Arg number x: The x coordinate of the screen
  405. --@Arg number y: The y coordinate of the screen
  406. --@OptionalArg number w: The width of the screen
  407. --@OptionalArg number h: The height of the screen
  408. --@OptionalArg string header: What to write in the screen (unclickable)
  409. --@Returns SelectionScreen screen: The selection screen
  410. function SelectionScreen(x,y,w,h,items,header)
  411.     local screen = Screen(x,y,w,h)
  412.     screen.scrollAmt = 0
  413.     screen.search = ""
  414.     screen.controls = {}
  415.     table.insert(screen.controls,Button(1,1,"back",4,1,colors.red,colors.black,function() return "back","back" end,"back"))
  416.     table.insert(screen.controls,Button(6,1,"v",1,1,colors.blue,colors.black,function() return "down","v" end,"down"))
  417.     table.insert(screen.controls,Button(8,1,"^",1,1,colors.blue,colors.black,function() return "up","^" end,"up"))
  418.     table.insert(screen.controls,Button(10,1,"quit",4,1,colors.red,colors.black,function() return "quit","quit" end,"quit"))
  419.     table.insert(screen.controls,FormField(1,2,"Search: ",screen.w,1,colors.black, colors.yellow, false, screen.w -8))
  420.     screen:useNewTerm(nil,nil,1,3)
  421.     term.write(header)
  422.     screen:useOldTerm()
  423.     screen.items = items
  424.     function screen:positionItems(filter)
  425.         screen.positionedItems = {}
  426.         if type(filter) ~= "string" then filter = "" end
  427.         local x,y = 1,1
  428.         for i=1,#self.items do
  429.             local args = string.match(self.items[i],"%b" .. section_char .. section_char)
  430.             if type(args) == "string" then args = textutils.unserialiseJSON(string.sub(args,2,-2)) end
  431.             if type(args) ~= "table" then args = {} end
  432.             local item = string.gsub(self.items[i],"%b"..section_char .. section_char,"")
  433.             if #filter <= 0 or string.match(item,filter) then
  434.                 args.color = args.color or colors.black
  435.                 args.bgColor = args.bgColor or colors.white
  436.                 local width = math.min(#item,self.w)
  437.                 if x+width-1 > self.w then
  438.                     x = 1
  439.                     y = y+1
  440.                 end
  441.                 table.insert(self.positionedItems,{text = item, args = args, x = x, y = y,w = width})
  442.                 x = x + width + 1
  443.             end
  444.         end
  445.         self.dataHeight = y
  446.     end
  447.     screen:positionItems()
  448.     function screen:refresh()
  449.         for k,v in pairs(self.buttons) do
  450.             v:unrender()
  451.             self.buttons[k] = nil
  452.         end
  453.         for k,v in pairs(self.controls) do
  454.             v:render()
  455.         end
  456.         for k,v in ipairs(self.positionedItems) do
  457.             if v.y - self.scrollAmt >= 1 then
  458.                 table.insert(self.buttons,Button(v.x,v.y-self.scrollAmt + 3,v.text,v.w,1,v.args.color,v.args.bgColor,function() return "item",v.text,v.args end,"item"))
  459.             end
  460.         end
  461.         for k,v in ipairs(self.controls) do
  462.             table.insert(self.buttons,v)
  463.         end
  464.     end
  465.     screen:refresh()
  466.     function screen:scroll(amt)
  467.         screen.scrollAmt = screen.scrollAmt + math.floor(tonumber(amt) or 1)
  468.         screen.scrollAmt = math.max(math.min(screen.scrollAmt,self.dataHeight+3-self.h),0)
  469.         self:refresh()
  470.     end
  471.     function screen:updateSearch(search)
  472.         self.search = search
  473.         self:positionItems(search)
  474.         self:refresh()
  475.     end  
  476.     function screen:selectItem()
  477.         local buttonType,txt,result
  478.         self:useNewTerm()
  479.         repeat
  480.             buttonType,txt,result = self:waitForClick(function(a,b) return b:click() end)
  481.             if buttonType == "up" then
  482.                 self:scroll(-1)
  483.             elseif buttonType == "down" then
  484.                 self:scroll()
  485.             elseif buttonType == "form" then
  486.                 self:updateSearch(result)
  487.             end
  488.         until buttonType == "item" or buttonType == "back" or buttonType == "quit"
  489.         self:useOldTerm()
  490.         return buttonType,txt,result
  491.     end
  492.     return screen
  493. end
  494.  
  495. --Creates a settings screen
  496. --Instantiated by a settings table;
  497. --Example: { lightMode = false, lang = {{"fr","en"},"fr"}, eventTimeout = 0, displayName = "Bob" }
  498. --Example would create a settings screen of
  499. -- lightMode: [toggle button]
  500. -- lang: [dropdown menu]
  501. -- eventTimeout: [number formField]
  502. -- displayName: [string formField]
  503. --@Arg table settings: The settings to start
  504. --@Returns SettingsScreen screen: The finished settings screen
  505. function SettingsScreen(settings)
  506.     local maxX,maxY = term.getSize()
  507.     local screen = Screen(1,1,maxX,maxY)
  508.     screen.settings = settings
  509.     screen.header = header or ""
  510.     screen.page = 1
  511.     function screen:createButtons(settings)
  512.         if type(settings) == "table" then self.settings = settings end
  513.         local buttonPages = {}
  514.         local i,yPos,keyWidth = 1,1, math.floor(self.w/2)
  515.         for k,v in pairs(self.settings) do
  516.             if not buttonPages[i] then
  517.                 term.redirect(self.wndw)
  518.                 buttonPages[i] = Screen(1,2,self.w,self.h)
  519.             end
  520.             term.redirect(buttonPages[i].wndw)
  521.             if type(v) ~= "nil" then
  522.                 table.insert(buttonPages[i].textBoxes,TextBox(1,yPos,k,math.min(#k,keyWidth),2,colors.white,colors.black))
  523.             end
  524.             if type(v) == "boolean" then
  525.                 local t = Toggle(keyWidth+1,yPos,v)
  526.                 t.name = k
  527.                 table.insert(buttonPages[i].buttons,t)
  528.                 yPos = yPos + 2
  529.             elseif type(v) == "table" then
  530.                 local t = Dropdown(keyWidth+1,yPos,v[1])
  531.                 t.name = k
  532.                 table.insert(buttonPages[i].buttons,t)
  533.                 yPos = yPos + 2
  534.             elseif type(v) == "number" then
  535.                 local f = FormField(keyWidth+1,yPos,"",self.w-keyWidth,1,colors.pink,colors.gray,false,self.w-keyWidth,function(char) return string.match(char,"%d") or "" end)
  536.                 f.type = "numForm"
  537.                 f.name = k
  538.                 f.pass = "" .. v
  539.                 f:updateText()
  540.                 table.insert(buttonPages[i].buttons,f)
  541.                 yPos = yPos + 2
  542.             elseif type(v) == "string" then
  543.                 local t = FormField(keyWidth+1,yPos,"",math.max(self.w-keyWidth,#v),1,colors.lightBlue,colors.gray,false,math.max(self.w-keyWidth,#v))
  544.                 t.name = k
  545.                 table.insert(buttonPages[i].buttons,t)
  546.                 yPos = yPos + 2
  547.             end
  548.             if yPos > buttonPages[i].h then
  549.                 yPos = 1
  550.                 i = i + 1
  551.             end
  552.         end
  553.         term.redirect(self.wndw)
  554.         local buttons = {
  555.             Button(1,1,"Quit",4,1,colors.white,colors.red,nil,"Quit"),
  556.             Button(6,1,"^",1,1,colors.white,colors.blue,nil,"Up"),
  557.             Button(8,1,"v",1,1,colors.white, colors.blue,nil,"Down")
  558.         }
  559.         term.clear()
  560.         self.buttons = buttons
  561.         self.pages = buttonPages
  562.         self.page = 1
  563.         term.redirect(self.oldTerm)
  564.     end
  565.     screen:createButtons()
  566.     function screen:updateSettings()
  567.         for k,v in pairs(self.buttons) do
  568.             v:render()
  569.         end
  570.         while true do
  571.             local screen = self.pages[self.page]
  572.             if not screen then screen = Screen(1,2,self.w,self.h) end
  573.             term.redirect(screen.wndw)
  574.             term.clear()
  575.             screen:render()
  576.             local e,b,x,y = os.pullEvent("mouse_click")
  577.             local button = self:getClick(x,y)
  578.             local buttonType,a,b,c
  579.             if button then
  580.                 buttonType,a,b,c = button:click(x,y)
  581.             end
  582.             if buttonType == "Quit" then return self.settings
  583.             elseif buttonType == "Up" then
  584.                 self.page = math.max(1,math.min(self.page - 1,#self.pages))
  585.             elseif buttonType == "Down" then
  586.                 self.page = math.max(1,math.min(self.page + 1,#self.pages))
  587.             else
  588.                 local button,x,y = screen:getClick(x,y)
  589.                 if button then
  590.                     local buttonType,a,b,c = button:click(x,y)
  591.                     if buttonType == "form" then
  592.                         self.settings[c] = b
  593.                     elseif buttonType == "numForm" then
  594.                         self.settings[c] = tonumber(b)
  595.                     elseif buttonType == "dropdown" then
  596.                         self.settings[c] = {a,b}
  597.                     else
  598.                         self.settings[b] = a
  599.                     end
  600.                 end
  601.             end
  602.         end
  603.     end
  604.     return screen
  605. end                
Advertisement
Add Comment
Please, Sign In to add comment