Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --vvvvvvvvvv Delete the lines below if you do not use docs vvvvvvvvvv
- if not docs then os.loadAPI("apis/docs.lua") end
- --Type registry
- local types = {
- ["Button"]={colors.brown,"A button that can be clicked and stuff idk"},
- ["FormField"]={colors.brown,"A button that can be used to enter strings"},
- ["TextBox"]={colors.brown,"A generic text box that cannot be interacted with"},
- ["Toggle"]={colors.brown,"A switch that can be turned off and on"},
- ["Dropdown"]={colors.brown,"A button that can be switched between several items"},
- ["Screen"]={colors.pink,"A generic screen that can contain buttons"},
- ["SelectionScreen"]={colors.pink,"A screen that allows you to select an item from a list"},
- ["ScrollScreen"]={colors.pink,"A screen that can be scrolled up and down, NYI"},
- ["SettingsScreen"]={colors.pink,"A screen where you can modify settings"}
- }
- for k,v in pairs(types) do
- docs.registerType(k,v[1],v[2])
- end
- --^^^^^^^^^^ Delete the lines above if you do not use docs ^^^^^^^^^^
- section_char = string.sub(utf8.char(167),2,2)
- --Creates a button object
- --@Arg number x: The x position of the top left part of the button
- --@Arg number y: The y position of the top left part of the button
- --@Arg string text: The text of the button
- --@OptionalArg number w: The width the button should be, defaulting to the text length
- --@OptionalArg number h: The height the button should be, defaulting to 1
- --@OptionalArg Color color: The text color of the button, defaulting to green
- --@OptionalArg Color background: The background color of the button
- --@OptionalArg function clickFunction: What the button should do when it gets clicked
- --@OptionalArg string buttonType: The type of button it is, defaulting to "default"
- --@Returns Button button: The created button
- function Button(x,y,text,w,h,color,background,clickFunction,t)
- local btn = {
- text = text,
- x = x,
- y = y,
- w = w or #text,
- h = h or 1,
- color = color or colors.green,
- background = background or colors.black,
- rendered = false,
- click = clickFunction or function() return (t or "default"), text end,
- buttonType = t or "default"
- }
- function btn:render()
- local x,y = term.getCursorPos()
- local textColor = term.getTextColor()
- local backgroundColor = term.getBackgroundColor()
- term.setCursorPos(self.x,self.y)
- term.setTextColor(self.color)
- term.setBackgroundColor(self.background)
- local index = 1
- for j=1,self.h do
- for i=1,self.w do
- local char = string.sub(self.text,(j-1)*self.w+i,(j-1)*self.w+i)
- if type(char) ~= "string" or #char ~= 1 then char = " " end
- term.write(char)
- end
- term.setCursorPos(self.x,self.y + j)
- end
- term.setCursorPos(x,y)
- term.setTextColor(textColor)
- term.setBackgroundColor(backgroundColor)
- self.rendered = true
- end
- function btn:unrender()
- local x,y = term.getCursorPos()
- term.setCursorPos(self.x,self.y)
- for j=1,self.h do
- for i=1,self.w do
- term.write(" ")
- end
- term.setCursorPos(self.x,self.y + j)
- end
- term.setCursorPos(x,y)
- self.rendered = false
- end
- function btn:isClicked(x,y)
- return self.rendered and x >= self.x and x < self.x + self.w and y >= self.y and y < self.y + self.h
- end
- btn:render()
- return btn
- end
- --Creates a non interactive text box
- --@Arg number x: The x coordinate of top left corner
- --@Arg number y: The y coordinate of top left corner
- --@Arg string text: The text to display
- --@OptionalArg number w: The width of the box
- --@OptionalArg number h: The height of the box
- --@OptionalArg Color color: The text color of the text
- --@OptionalArg Coloc background: The background color of the box
- --@Returns TextBox box: The text box
- function TextBox(x,y,text,w,h,color,background)
- local button = Button(x,y,text,w,h,color,background,function() return end,"text")
- function button:isClicked(x,y)
- return false
- end
- return button
- end
- --Creates a button that you can type in
- --@Arg number x: The x position of the top left part of the form field
- --@Arg number y: The y position of the top left part of the button
- --@Arg string text: The text of the form field
- --@OptionalArg number w: The width the form field should be, defaulting to the text length + max Length
- --@OptionalArg number h: The height the form field should be, defaulting to 2
- --@OptionalArg Color color: The text color of the button, defaulting to green
- --@OptionalArg Color background: The background color of the button
- --@OptionalArg boolean secure: Whether the text that gets entered should be hidden or not
- --@OptionalArg number maxLength: The maximum length of the password
- --@OptionalArg function charFilter: The filter function for the char
- --@Returns Button button: The created button
- function FormField(x,y,text,w,h,color,background,secure,maxLength,charFilter)
- h = tonumber(h) or 2
- if h > 1 then w = math.max(w or #text,maxLength or 16)
- else w = #text+(maxLength or 16) end
- entryColor = entryColor or colors.yellow
- if type(charFilter) ~= "function" then charFilter = function(char) return char end end
- local function clickFunction(this)
- local _,key
- local oldBlink = term.getCursorBlink()
- term.setCursorBlink(true)
- repeat
- if this.h == 1 then
- term.setCursorPos(this.x + #this.text,this.y)
- else
- term.setCursorPos(this.x+1+#this.pass,this.y+1)
- end
- _,key = os.pullEvent("key")
- if key <= 256 and #this.pass <= this.maxLength then
- local _,char = os.pullEvent("char")
- this.pass = this.pass .. charFilter(char)
- this:updateText()
- elseif key == keys.delete or key == keys.backspace then
- this.pass = string.sub(this.pass,1,-2)
- this:updateText()
- end
- until key == keys.enter
- term.setCursorBlink(oldBlink)
- return this.type,this.header,this.pass,this.name
- end
- local button = Button(x,y,text,w,h,color,background,clickFunction,"form")
- button.pass = ""
- button.header = text
- button.secure = secure
- button.maxLength = maxLength
- button.charFilter = charFilter
- button.type = "form"
- function button:updateText()
- local displayPass = self.pass
- if self.secure then displayPass = string.rep("*",#self.pass) end
- if self.h == 1 then
- self.text = self.header .. displayPass
- elseif self.h >= 2 then
- if #self.header > 0 then
- self.text = self.header .. string.rep(" ",self.w-#self.header) .. ">" .. displayPass
- else
- self.text = displayPass
- end
- end
- self:render()
- end
- button:updateText()
- return button
- end
- --Creates a toggle button
- --@Arg number x: The x position of the button
- --@Arg number y: The y position of the button
- --@OptionalArg boolean startActive: Whether the button should start toggled or not
- function Toggle(x,y,startActive)
- startActive = startActive == true
- local button = Button(x,y," ",2,1,colors.white,colors.gray,nil,"toggle")
- button.state = startActive
- function button:render()
- local x,y = term.getCursorPos()
- local textColor = term.getTextColor()
- local backgroundColor = term.getBackgroundColor()
- term.setCursorPos(self.x,self.y)
- term.setTextColor(self.color)
- term.setBackgroundColor(self.background)
- if self.state then
- term.blit(" o","00","85")
- else
- term.blit("x ","00","e8")
- end
- term.setCursorPos(x,y)
- term.setTextColor(textColor)
- term.setBackgroundColor(backgroundColor)
- self.rendered = true
- end
- function button:click(x,y)
- if x == self.x and y == self.y then
- self.state = false
- elseif x == self.x + 1 and y == self.y then
- self.state = true
- end
- self:render()
- return self.type, self.state, self.name
- end
- return button
- end
- --Gets the button clicked inside a list, waiting until one is clicked
- --@Arg table buttons: The buttons to check
- --@OptionalArg Screen screen: The screen the button is a part of
- --@Returns Button button: The selected button
- function getButtonClicked(buttons,screen,allowNil)
- --local e,b,x,y = os.pullEvent("mouse_click")
- local xOffset,yOffset = 0,0
- if screen then xOffset,yOffset = screen.x - 1, screen.y - 1 end
- local selectedButton
- -- for k,v in pairs(buttons) do
- -- if v:isClicked(x,y) then selectedButton = v end
- -- end
- while not selectedButton do
- local e,b,x,y = os.pullEvent("mouse_click")
- for k,v in pairs(buttons) do
- if v:isClicked(x-xOffset,y-yOffset) then return v,x,y,xOffset,yOffset end --selectedButton = v end
- end
- if allowNil then return nil,x,y end
- end
- return selectedButton,x,y,xOffset,yOffset
- end
- --Creates a screen with interactive buttons
- --@OptionalArg number x: The top left x coordinate
- --@OptinalArg number y: The top left y coordinate
- --@OptinalArg number w: The width of the screen
- --@OptionalArg number h: The height of the screen
- --@OptionalArg table b: The buttons to add on startup
- --@OptionalArg table t: The text boxes to add on startup
- --@Returns Screen screen: The newly created screen
- function Screen(x,y,w,h,b,t)
- local maxX,maxY = term.getSize()
- x = tonumber(x) or 1
- y = tonumber(y) or 1
- w = tonumber(w) or maxX - x + 1
- h = tonumber(h) or maxY - x + 1
- local screen = {
- x = x,
- y = y,
- w = w,
- h = h,
- buttons = {},
- rendered = true,
- txtClr = colors.white,
- bgClr = colors.black,
- cursorX = 1,
- cursorY = 1,
- newTerm = false,
- textBoxes = {}
- }
- screen.oldX,screen.oldY = term.getCursorPos()
- screen.oldBG, screen.oldTxt = term.getBackgroundColor(), term.getTextColor()
- screen.oldTerm = term.current()
- screen.wndw = window.create(screen.oldTerm,x,y,w,h)
- function screen:useOldTerm()
- if self.newTerm then
- self.txtClr = term.getTextColor()
- self.bgClr = term.getBackgroundColor()
- self.cursorX,self.cursorY = term.getCursorPos()
- term.redirect(self.oldTerm)
- term.setCursorPos(self.oldX,self.oldY)
- term.setBackgroundColor(self.oldBG)
- term.setTextColor(self.oldTxt)
- end
- self.newTerm = false
- end
- function screen:useNewTerm(txtClr,bgClr,x,y)
- if not self.newTerm then
- self.oldX,self.oldY = term.getCursorPos()
- self.oldBG, self.oldTxt = term.getBackgroundColor(), term.getTextColor()
- term.redirect(self.wndw)
- self.txtClr = txtClr or self.txtClr
- self.bgClr = bgClr or self.bgClr
- self.cursorX = x or self.cursorX
- self.cursorY = y or self.cursorY
- term.setTextColor(self.txtClr)
- term.setBackgroundColor(self.bgClr)
- term.setCursorPos(self.cursorX,self.cursorY)
- end
- self.newTerm = true
- end
- function screen:addButton(button)
- term.redirect(self.wndw)
- table.insert(self.buttons,button)
- self:useOldTerm()
- end
- function screen:setButtons(newButtons)
- term.redirect(self.wndw)
- if type(newButtons) ~= "table" then
- self:useOldTerm()
- return end
- self.buttons = newButtons
- self:useOldTerm()
- end
- function screen:render(shouldRender,background)
- if type(shouldRender) ~= "boolean" then shouldRender = true end
- --if shouldRender == self.rendered then return end
- --self.wndw.setVisible(shouldRender)
- term.redirect(self.wndw)
- if background then term.setBackgroundColor(background) end
- term.clear()
- for k,v in pairs(self.textBoxes) do
- v:render()
- end
- for k,v in pairs(self.buttons) do
- v:render()
- end
- self.rendered = shouldRender
- end
- function screen:waitForClick(f)
- if type(f) ~= "function" then f = function(a,b) return b end end
- if self.rendered then return f(self,getButtonClicked(self.buttons,self)) end
- end
- if type(b) == "table" then
- screen:setButtons(b)
- end
- if type(t) == "table" then
- screen.textBoxes = t
- end
- function screen:getClick(x,y)
- if tonumber(x) and tonumber(y) and self.rendered then
- x = tonumber(x) - self.x + 1
- y = tonumber(y) - self.y + 1
- for k,v in pairs(self.buttons) do
- if v:isClicked(tonumber(x),tonumber(y)) then return v,tonumber(x),tonumber(y) end
- end
- elseif self.rendered then return getButtonClicked(self.buttons,self,true) end
- return nil,tonumber(x),tonumber(y)
- end
- return screen
- end
- --Creates a dropdown menu
- --@Arg number x: the x coordinate
- --@Arg number y: The y coordinate
- --@Arg table options: The options to choose from
- --@OptionalArg number w: The width of the dropdown
- function Dropdown(x,y,options,w)
- local width = 0
- local key
- for k,v in pairs(options) do
- if not key then key = k end
- if not tonumber(w) then width = math.max(width,#v) end
- end
- local button = Button(x,y,options[key],tonumber(w) or width,1,colors.white,colors.cyan,nil,"dropdown")
- button.key = key
- button.options = options
- function button:click()
- local current = term.current()
- local scroll = 0
- local dHeight = math.min(#self.options,5)
- local maxX,maxY = term.getSize()
- local x,y = 1,1
- if self.y + dHeight > maxY then
- y = self.y + 1
- else
- y = self.y - dHeight
- end
- x = self.x + 1
- local screen = Screen(x,y,w,dHeight)
- term.redirect(screen.wndw)
- local index = 1
- for k,v in ipairs(self.options) do
- if k == key then
- table.insert(screen.buttons,Button(screen.x,index,v,w,1,colors.white,colors.green,function() return "option",k,v end,"option"))
- elseif index%2 == 1 then
- table.insert(screen.buttons,Button(screen.x,index,v,w,1,colors.white,colors.gray,function() return "option",k,v end,"option"))
- else
- table.insert(screen.buttons,Button(screen.x,index,v,w,1,colors.white,colors.lightGray,function() return "option",k,v end,"option"))
- end
- index = index + 1
- end
- local buttonType,key,value
- repeat
- local e,b,x,y = os.pullEvent()
- if e == "mouse_click" then
- local button,x,y = screen:getClick(x,y)
- if button then
- local t,k,v = button:click()
- if t and t.type == "option" then return self.type, v, self.options, self.name end
- end
- elseif e == "key" then
- --TODO: add clamping (too lazy to do right now)
- if b == keys.down then
- term.clear()
- for k,v in pairs(screen.buttons) do
- v.y = v.y - 1
- v:render()
- end
- elseif b == keys.up then
- term.clear()
- for k,v in pairs(screen.buttons) do
- v.y = v.y + 1
- v:render()
- end
- end
- end
- until false
- end
- end
- --Creates a screen where you can select an item from a list of items
- --@Arg number x: The x coordinate of the screen
- --@Arg number y: The y coordinate of the screen
- --@OptionalArg number w: The width of the screen
- --@OptionalArg number h: The height of the screen
- --@OptionalArg string header: What to write in the screen (unclickable)
- --@Returns SelectionScreen screen: The selection screen
- function SelectionScreen(x,y,w,h,items,header)
- local screen = Screen(x,y,w,h)
- screen.scrollAmt = 0
- screen.search = ""
- screen.controls = {}
- table.insert(screen.controls,Button(1,1,"back",4,1,colors.red,colors.black,function() return "back","back" end,"back"))
- table.insert(screen.controls,Button(6,1,"v",1,1,colors.blue,colors.black,function() return "down","v" end,"down"))
- table.insert(screen.controls,Button(8,1,"^",1,1,colors.blue,colors.black,function() return "up","^" end,"up"))
- table.insert(screen.controls,Button(10,1,"quit",4,1,colors.red,colors.black,function() return "quit","quit" end,"quit"))
- table.insert(screen.controls,FormField(1,2,"Search: ",screen.w,1,colors.black, colors.yellow, false, screen.w -8))
- screen:useNewTerm(nil,nil,1,3)
- term.write(header)
- screen:useOldTerm()
- screen.items = items
- function screen:positionItems(filter)
- screen.positionedItems = {}
- if type(filter) ~= "string" then filter = "" end
- local x,y = 1,1
- for i=1,#self.items do
- local args = string.match(self.items[i],"%b" .. section_char .. section_char)
- if type(args) == "string" then args = textutils.unserialiseJSON(string.sub(args,2,-2)) end
- if type(args) ~= "table" then args = {} end
- local item = string.gsub(self.items[i],"%b"..section_char .. section_char,"")
- if #filter <= 0 or string.match(item,filter) then
- args.color = args.color or colors.black
- args.bgColor = args.bgColor or colors.white
- local width = math.min(#item,self.w)
- if x+width-1 > self.w then
- x = 1
- y = y+1
- end
- table.insert(self.positionedItems,{text = item, args = args, x = x, y = y,w = width})
- x = x + width + 1
- end
- end
- self.dataHeight = y
- end
- screen:positionItems()
- function screen:refresh()
- for k,v in pairs(self.buttons) do
- v:unrender()
- self.buttons[k] = nil
- end
- for k,v in pairs(self.controls) do
- v:render()
- end
- for k,v in ipairs(self.positionedItems) do
- if v.y - self.scrollAmt >= 1 then
- 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"))
- end
- end
- for k,v in ipairs(self.controls) do
- table.insert(self.buttons,v)
- end
- end
- screen:refresh()
- function screen:scroll(amt)
- screen.scrollAmt = screen.scrollAmt + math.floor(tonumber(amt) or 1)
- screen.scrollAmt = math.max(math.min(screen.scrollAmt,self.dataHeight+3-self.h),0)
- self:refresh()
- end
- function screen:updateSearch(search)
- self.search = search
- self:positionItems(search)
- self:refresh()
- end
- function screen:selectItem()
- local buttonType,txt,result
- self:useNewTerm()
- repeat
- buttonType,txt,result = self:waitForClick(function(a,b) return b:click() end)
- if buttonType == "up" then
- self:scroll(-1)
- elseif buttonType == "down" then
- self:scroll()
- elseif buttonType == "form" then
- self:updateSearch(result)
- end
- until buttonType == "item" or buttonType == "back" or buttonType == "quit"
- self:useOldTerm()
- return buttonType,txt,result
- end
- return screen
- end
- --Creates a settings screen
- --Instantiated by a settings table;
- --Example: { lightMode = false, lang = {{"fr","en"},"fr"}, eventTimeout = 0, displayName = "Bob" }
- --Example would create a settings screen of
- -- lightMode: [toggle button]
- -- lang: [dropdown menu]
- -- eventTimeout: [number formField]
- -- displayName: [string formField]
- --@Arg table settings: The settings to start
- --@Returns SettingsScreen screen: The finished settings screen
- function SettingsScreen(settings)
- local maxX,maxY = term.getSize()
- local screen = Screen(1,1,maxX,maxY)
- screen.settings = settings
- screen.header = header or ""
- screen.page = 1
- function screen:createButtons(settings)
- if type(settings) == "table" then self.settings = settings end
- local buttonPages = {}
- local i,yPos,keyWidth = 1,1, math.floor(self.w/2)
- for k,v in pairs(self.settings) do
- if not buttonPages[i] then
- term.redirect(self.wndw)
- buttonPages[i] = Screen(1,2,self.w,self.h)
- end
- term.redirect(buttonPages[i].wndw)
- if type(v) ~= "nil" then
- table.insert(buttonPages[i].textBoxes,TextBox(1,yPos,k,math.min(#k,keyWidth),2,colors.white,colors.black))
- end
- if type(v) == "boolean" then
- local t = Toggle(keyWidth+1,yPos,v)
- t.name = k
- table.insert(buttonPages[i].buttons,t)
- yPos = yPos + 2
- elseif type(v) == "table" then
- local t = Dropdown(keyWidth+1,yPos,v[1])
- t.name = k
- table.insert(buttonPages[i].buttons,t)
- yPos = yPos + 2
- elseif type(v) == "number" then
- 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)
- f.type = "numForm"
- f.name = k
- f.pass = "" .. v
- f:updateText()
- table.insert(buttonPages[i].buttons,f)
- yPos = yPos + 2
- elseif type(v) == "string" then
- local t = FormField(keyWidth+1,yPos,"",math.max(self.w-keyWidth,#v),1,colors.lightBlue,colors.gray,false,math.max(self.w-keyWidth,#v))
- t.name = k
- table.insert(buttonPages[i].buttons,t)
- yPos = yPos + 2
- end
- if yPos > buttonPages[i].h then
- yPos = 1
- i = i + 1
- end
- end
- term.redirect(self.wndw)
- local buttons = {
- Button(1,1,"Quit",4,1,colors.white,colors.red,nil,"Quit"),
- Button(6,1,"^",1,1,colors.white,colors.blue,nil,"Up"),
- Button(8,1,"v",1,1,colors.white, colors.blue,nil,"Down")
- }
- term.clear()
- self.buttons = buttons
- self.pages = buttonPages
- self.page = 1
- term.redirect(self.oldTerm)
- end
- screen:createButtons()
- function screen:updateSettings()
- for k,v in pairs(self.buttons) do
- v:render()
- end
- while true do
- local screen = self.pages[self.page]
- if not screen then screen = Screen(1,2,self.w,self.h) end
- term.redirect(screen.wndw)
- term.clear()
- screen:render()
- local e,b,x,y = os.pullEvent("mouse_click")
- local button = self:getClick(x,y)
- local buttonType,a,b,c
- if button then
- buttonType,a,b,c = button:click(x,y)
- end
- if buttonType == "Quit" then return self.settings
- elseif buttonType == "Up" then
- self.page = math.max(1,math.min(self.page - 1,#self.pages))
- elseif buttonType == "Down" then
- self.page = math.max(1,math.min(self.page + 1,#self.pages))
- else
- local button,x,y = screen:getClick(x,y)
- if button then
- local buttonType,a,b,c = button:click(x,y)
- if buttonType == "form" then
- self.settings[c] = b
- elseif buttonType == "numForm" then
- self.settings[c] = tonumber(b)
- elseif buttonType == "dropdown" then
- self.settings[c] = {a,b}
- else
- self.settings[b] = a
- end
- end
- end
- end
- end
- return screen
- end
Advertisement
Add Comment
Please, Sign In to add comment