Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- PASTE MT6gfL6e
- local mov = term.setCursorPos
- local write = term.write
- local color = function(color)
- if term.isColor then
- term.setTextColor(color)
- end
- end
- local fill = function(color)
- if term.isColor then
- term.setBackgroundColor(color)
- end
- end
- local WIDTH, HEIGHT = term.getSize()
- -- Getters
- local function getWidth()
- return WIDTH
- end
- local function getHeight()
- return HEIGHT
- end
- -- Draw a panel
- local function panel(x, y, width, height)
- x = x or 1
- y = y or 1
- width = width or 1
- height = height or 1
- for r = y, y + height - 1 do
- mov(x, r)
- local str = ""
- for c = x, x + width - 1 do
- if (r == y or r == y + height - 1) and (c == x or c == x + width - 1) then
- str = str .. string.char(143)
- elseif r == y or r == y + height - 1 then
- str = str .. "-"
- elseif c == x or c == x + width - 1 then
- str = str .. "|"
- else
- str = str .. " "
- end
- end
- write(str)
- end
- end
- -- Make text
- local function text(x, y, txt)
- term.setCursorPos(x, y)
- term.write(txt)
- end
- -- Make text centered along the x axis
- local function xCenteredText(y, txt)
- local x = WIDTH / 2 - string.len(txt) / 2
- text(x, y, txt)
- end
- -- Make text centered along the y axis
- local function yCenteredText(x, txt)
- local y = HEIGHT / 2
- text(x, y, txt)
- end
- -- Make text centered in the middle of the screen
- local function xyCenteredText(txt)
- local x = WIDTH / 2 - string.len(txt) / 2
- local y = HEIGHT / 2
- text(x, y, txt)
- end
- -- Make a menu option
- local function menuOption(name, func)
- return {name = name, func = func}
- end
- -- Make a menu object
- local function menu(x, y, defaultOption, options)
- x = x or 1
- y = y or 1
- local curOpt = defaultOption or 1
- local menuOptions = options or {menuOption("default option", nil)}
- -- Show the menu until an option gets selected
- local event, param
- while param ~= keys.enter do
- -- Draw the menu
- local tempFill = term.getBackgroundColor()
- local tempColor = term.getTextColor()
- for i, opt in ipairs(menuOptions) do
- mov(x, y + i - 1)
- if i == curOpt then
- term.setBackgroundColor(tempColor)
- term.setTextColor(tempFill)
- else
- term.setBackgroundColor(tempFill)
- term.setTextColor(tempColor)
- end
- write(opt.name)
- end
- term.setBackgroundColor(tempFill);
- term.setTextColor(tempColor)
- -- Detect key presses
- event, param = os.pullEvent("key")
- if param == keys.up then
- curOpt = curOpt - 1
- if curOpt < 1 then
- curOpt = #menuOptions
- end
- elseif param == keys.down then
- curOpt = curOpt + 1
- if curOpt > #menuOptions then
- curOpt = 1
- end
- end
- end
- menuOptions[curOpt].func()
- end
- -- Make a menu centered on the x axis
- local function xCenteredMenu(y, defaultOption, options)
- local longest = string.len(options[1].name)
- for i,v in ipairs(options) do
- local len = string.len(v.name)
- if len > longest then
- longest = len
- end
- end
- local x = WIDTH / 2 - longest / 2
- menu(x, y, defaultOption, options)
- end
- -- Make a menu centered on the y axis
- local function yCenteredMenu(x, defaultOption, options)
- local y = HEIGHT / 2 - #options / 2
- menu(x, y, defaultOptions, options)
- end
- -- Make a menu centered in the middle of the screen
- local function xyCenteredMenu(defaultOption, options)
- local longest = string.len(options[1].name)
- for i,v in ipairs(options) do
- local len = string.len(v.name)
- if len > longest then
- longest = len
- end
- end
- local x = WIDTH / 2 - longest / 2
- local y = HEIGHT / 2 - #options / 2
- menu(x, y, defaultOptions, options)
- end
- -- Make a string input field
- local function textField(x, y, width, maxLen)
- local text = ""
- local caret = true
- -- Get keyboard input in loop until enter is pressed
- while true do
- -- The timer is for a blinking caret
- os.startTimer(.5)
- local event, key = os.pullEvent()
- if event == "key" then
- -- Enter ends input
- if key == keys.enter or key == keys.numPadEnter then
- break
- -- Backspace to delete
- elseif key == keys.backspace then
- if string.len(text) > 0 then
- text = string.sub(text, 1, string.len(text) - 1)
- end
- end
- elseif event == "char" and string.len(text) < maxLen then
- text = text .. key
- end
- caret = not caret
- -- Display the text
- mov(x, y)
- write(string.rep(" ", width))
- mov(x, y)
- local dispText = text
- if string.len(dispText) > width - 1 then
- dispText = string.sub(dispText, -width + 1)
- end
- write(dispText)
- if caret == true then
- write("_")
- end
- end
- return text
- end
- -- Make a string input field centered on the x axis
- local function xCenteredTextField(y, width, maxLen)
- local x = WIDTH / 2 - width / 2
- textField(x, y, width, maxLen)
- end
- -- Make a string input field centered on the y axis
- local function yCenteredTextField(x, width, maxLen)
- local y = HEIGHT / 2
- textField(x, y, width, maxLen)
- end
- -- Make a string input field centered in the middle of the screen
- local function xyCenteredTextField(width, maxLen)
- local x = WIDTH / 2 - width / 2
- local y = HEIGHT / 2
- textField(x, y, width, maxLen)
- end
- -- Make a number input field
- local function numField(x, y, width)
- local text = ""
- local caret = true
- local hasDecimal = false
- -- Get keyboard input in loop until enter is pressed
- while true do
- -- The timer is for a blinking caret
- os.startTimer(.5)
- local event, key = os.pullEvent()
- if event == "key" then
- -- Enter ends input
- if key == keys.enter or key == keys.numPadEnter then
- break
- -- Backspace to delete
- elseif key == keys.backspace then
- if string.len(text) > 0 then
- text = string.sub(text, 1, string.len(text) - 1)
- end
- end
- elseif event == "char" and
- ((hasDecimal == false and key == ".") or
- (string.len(text) == 0 and key == "-") or
- (tonumber(key))) then
- text = text .. key
- end
- caret = not caret
- -- Display the text
- mov(x, y)
- write(string.rep(" ", width))
- mov(x, y)
- local dispText = text
- if string.len(dispText) > width - 1 then
- dispText = string.sub(dispText, -width + 1)
- end
- write(dispText)
- if caret == true then
- write("_")
- end
- end
- return tonumber(text)
- end
- -- Make a number input field centered on the x axis
- local function xCenteredNumField(y, width)
- local x = WIDTH / 2 - width / 2
- numField(x, y, width)
- end
- -- Make a number input field centered on the y axis
- local function yCenteredNumField(x, width)
- local y = HEIGHT / 2
- numField(x, y, width)
- end
- -- Make a number input field centered in the middle of the screen
- local function xyCenteredNumField(width)
- local x = WIDTH / 2 - width / 2
- local y = HEIGHT / 2
- numField(x, y, width)
- end
- -- Return library
- return {
- color = color,
- fill = fill,
- getWidth = getWidth,
- getHeight = getHeight,
- panel = panel,
- text = text,
- xCenteredText = xCenteredText,
- yCenteredText = yCenteredText,
- xyCenteredText = xyCenteredText,
- menuOption = menuOption,
- menu = menu,
- xCenteredMenu = xCenteredMenu,
- yCenteredMenu = yCenteredMenu,
- xyCenteredMenu = xyCenteredMenu,
- textField = textField,
- xCenteredTextField = xCenteredTextField,
- yCenteredTextField = yCenteredTextField,
- xyCenteredTextField = xyCenteredTextField,
- numField = numField,
- xCenteredNumField = xCenteredNumField,
- yCenteredNumField = yCenteredNumField,
- xyCenteredNumField = xyCenteredNumField
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement