Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Object types (objType):
- -- Button: X, Y, width, height, text, funcType,
- -- param
- -- A normal button. You press it and something
- -- happens!
- --
- -- function types:
- -- * switch (changes the shown screen)
- -- * function (calls a function; The param-value
- -- has to be the function, not a string!)
- -- Text: X, Y, text
- -- Boring old text. It's just on your monitor and
- -- looks nice.
- -- Variable: X, Y, variableID
- -- Just like text but you set the value using the
- -- "getVariableValue" method.
- -- Slider: X, Y, length, direction, sliderID
- -- It's like the variable but you set the value in
- -- percent (0-100) and it gets shown as a bar.
- -- Input: X, Y, inputID, message, isPassword
- -- It's like the button but if the user touches
- -- it, he will get redirected to the computer to
- -- enter something.
- --monitor
- local monitorSide = "right"
- local monitor = nil
- -- texts
- local refreshText = "Refresh"
- local backText = "Back"
- local alphabet = {}
- alphabet[30]="a"
- alphabet[48]="b"
- alphabet[46]="c"
- alphabet[32]="d"
- alphabet[18]="e"
- alphabet[33]="f"
- alphabet[34]="g"
- alphabet[35]="h"
- alphabet[23]="i"
- alphabet[36]="j"
- alphabet[37]="k"
- alphabet[38]="l"
- alphabet[50]="m"
- alphabet[49]="n"
- alphabet[24]="o"
- alphabet[25]="p"
- alphabet[16]="q"
- alphabet[19]="r"
- alphabet[31]="s"
- alphabet[20]="t"
- alphabet[22]="u"
- alphabet[47]="v"
- alphabet[17]="w"
- alphabet[45]="x"
- alphabet[21]="y"
- alphabet[44]="z"
- alphabet[57]=" "
- alphabet[2]="1"
- alphabet[3]="2"
- alphabet[4]="3"
- alphabet[5]="4"
- alphabet[6]="5"
- alphabet[7]="6"
- alphabet[8]="7"
- alphabet[9]="8"
- alphabet[10]="9"
- alphabet[11]="0"
- -- colors
- local buttonDefaultColor = colors.red
- local buttonPressedColor = colors.lime
- local sliderLowValueColor = colors.red
- local sliderMediumValueColor = colors.yellow
- local sliderHighValueColor = colors.lime
- -- other
- local quit = false
- local maxX, maxY = 51, 19
- local screens = {}
- local currentScreen = "mainScreen"
- local lastScreen = ""
- local userInputs = {}
- screens = {
- mainScreen = {
- { objType="Text", x=27, y=2, text="Time:" };
- { objType="Variable", x=33, y=2, varID="Time" };
- { objType="Button", x=2, y=4, width=25, height=3, text="call function!", funcType="function", param=setRandomValue };
- { objType="Button", x=2, y=9, width=25, height=3, text="switch to subScreen", funcType="switch", param="subScreen" };
- };
- subScreen = {
- parentScreen="mainScreen";
- { objType="Slider", x=2, y=2, length=15, sliderID="randomSlider" };
- { objType="Text", x=2, y=4, text="Input:" };
- { objType="Input", x=9, y=4, inputID="randomInput", message="Thanks for trying out the input object!", isPassword=false }
- };
- }
- -- Displays a star in the upper left corner for a
- -- short amount of time. Used when you want to see
- -- when something certain happens.
- -- Should only be used when you are desperately
- -- looking for a bug.
- function extremeDebug()
- term.setCursorPos(1, 1)
- term.write("*")
- os.sleep(0.5)
- term.setCursorPos(1, 1)
- term.write(" ")
- os.sleep(0.5)
- end
- function reloadScreen()
- showScreen(currentScreen)
- end
- -- user variables
- local randomValue= 50
- -- user functions region start
- function setRandomValue()
- randomValue = math.random(100)
- end
- -- user functions region end
- -- Let's the user define the value of a certain
- -- variable.
- function getVariableValue(variable)
- if (variable == nil or variable.objType ~= "Variable") then
- return
- end
- variableID = variable.varID
- if (variableID == "testVariable") then
- return 42;
- elseif (variableID == "Time") then
- return textutils.formatTime(os.time(), true)
- end
- return ""
- end
- -- Let's the user set the filled part of the
- -- slider in percent.
- function getSliderValue(slider)
- if (slider == nil or slider.objType ~= "Slider") then
- return
- end
- sliderID = slider.sliderID
- if (sliderID == "testSlider") then
- return 87;
- elseif (sliderID == "randomSlider") then
- return randomValue
- end
- end
- -- WARNING! Everything below this comment
- -- shouldn't be edited! If you do and the program
- -- doesn't work anymore, it's your fault!
- -- Redirects the input to the computer and lets
- -- the user enter something. The result will be
- -- in the userInputs array with the inputID as the
- -- key.
- function getUserInput(inputObject)
- if (inputObject == nil or inputObject.objType ~= "Input") then
- return
- end
- x = inputObject.x
- y = inputObject.y
- inputID = inputObject.inputID
- message = inputObject.message
- isPassword = (inputObject.isPassword == nil) and false or inputObject.isPassword
- maxLength = inputObject.maxLength
- existingInput = userInputs[inputID]
- if (existingInput ~= nil) then -- Clear the text on the input object.
- term.setCursorPos(x, y)
- for i = -1, string.len(existingInput) do
- term.write(" ")
- end
- userInputs[inputID] = nil
- end
- -- make the input-object yellow
- term.setCursorPos(x, y)
- term.setBackgroundColor(colors.yellow)
- term.write(" ")
- term.setBackgroundColor(colors.black)
- term.restore()
- print(message)
- print("ENTER to confirm or CTRL to cancel")
- cursorX, cursorY = term.getCursorPos()
- term.write("_")
- term.setCursorPos(cursorX, cursorY)
- finished = false
- ret = false
- userInput = ""
- while (finished == false) do
- event, key = os.pullEvent("key")
- if (key == 28) then -- ENTER
- term.setCursorPos(cursorX, cursorY)
- print(" ")
- print()
- userInputs[inputID] = userInput
- finished = true
- ret = true
- elseif (key == 29) then -- CTRL
- print(" ")
- finished = true
- ret = false
- elseif (key == 14) then -- Backspace
- if (string.len(userInput) > 0) then
- userInput = string.sub(userInput, 1, string.len(userInput) - 1)
- cursorX = cursorX - 1
- term.setCursorPos(cursorX, cursorY)
- term.write("_ ")
- term.setCursorPos(cursorX, cursorY)
- end
- else
- pressedChar = alphabet[key]
- if (pressedChar ~= nil) then
- if (maxLength == nil or string.len(userInput) < maxLenght) then
- if (isPassword) then
- term.write("*_")
- else
- term.write(pressedChar .. "_")
- end
- term.setCursorPos(cursorX + 1, cursorY)
- userInput = userInput .. pressedChar
- cursorX = cursorX + 1
- end
- end
- end
- end
- term.redirect(monitor)
- term.setCursorPos(x, y)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.write(" ")
- if (userInput ~= nil and userInput ~= "") then
- if isPassword then
- for i = 1, string.len(userInput) do
- term.write("*")
- end
- else
- term.write(userInput)
- end
- end
- term.write(" ")
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- return ret
- end
- -- Checks if dir is a valid direction-string
- function isValidDirection(dir)
- if (dir == "left" or
- dir == "up" or
- dir == "right" or
- dir == "down") then
- return true
- end
- return false
- end
- -- Displays the text on the screen.
- function showText(textObject)
- if (textObject.objType ~= "Text") then
- return
- end
- x = textObject.x
- y = textObject.y
- text = textObject.text
- assert(x, "Text: X-coordinate has to be set!")
- assert(y, "Text: Y-coordinate has to be set!")
- assert(text, "Text: text has to be set!")
- term.setCursorPos(x, y)
- term.write(text)
- end
- -- Displays the slider on the screen.
- function showSlider(slider, fillPercentage)
- if (slider == nil or slider.objType ~= "Slider") then
- return
- end
- x = slider.x
- y = slider.y
- length = slider.length
- direction = (isValidDirection(slider.direction)) and slider.direction or "right"
- startSymbol, endSymbol = "<", ">"
- addX, addY = 1, 0 -- Sets the direction of the slider, therefore it could even be diagonal.
- if (direction == "left") then
- addX, addY = -1, 0
- startSymbol, endSymbol = ">", "<"
- term.setCursorPos(x - length, y)
- term.write(endSymbol)
- elseif (direction == "up") then
- addX, addY = 0, -1
- startSymbol, endSymbol = "V", "^"
- term.setCursorPos(x, y - length)
- term.write(endSymbol)
- elseif (direction == "right") then
- addX, addY = 1, 0
- startSymbol, endSymbol = "<", ">"
- term.setCursorPos(x + length, y)
- term.write(endSymbol)
- elseif (direction == "down") then
- addX, addY = 0, 1
- startSymbol, endSymbol = "^", "V"
- term.setCursorPos(x, y + length)
- term.write(endSymbol)
- else -- return if it's not a valid direction, even if I checked it before
- return
- end
- term.setCursorPos(x, y)
- term.write(startSymbol)
- if (fillPercentage ~= nil) then
- if (fillPercentage < 33) then
- sliderColor = sliderLowValueColor
- elseif (fillPercentage > 66) then
- sliderColor = sliderHighValueColor
- else
- sliderColor = sliderMediumValueColor
- end
- filled = math.floor((length / 100) * fillPercentage)
- currentX = x + addX
- currentY = y + addY
- for i = 1, filled do
- paintutils.drawPixel(currentX, currentY, sliderColor)
- currentX = currentX + addX
- currentY = currentY + addY
- end
- end
- term.setBackgroundColor(colors.black)
- end
- -- Displays the given button on the screen.
- function showButton(button, color)
- if (button == nil or button.objType ~= "Button") then
- return
- end
- x = button.x
- y = button.y
- width = button.width
- height = button.height
- text = button.text
- for row = x, x + width - 1 do
- for col = y, y + height - 1 do
- paintutils.drawPixel(row, col, color)
- end
- end
- --tries to center the text in the button
- textCol = x + math.floor((width - string.len(text)) / 2)
- textRow = y + math.ceil(height / 2) - 1
- term.setCursorPos(textCol, textRow)
- term.write(text)
- term.setBackgroundColor(colors.black)
- end
- function showInput(inputObject)
- if (inputObject == nil or inputObject.objType ~= "Input") then
- return
- end
- x = inputObject.x
- y = inputObject.y
- term.setCursorPos(x, y)
- term.setBackgroundColor(colors.white)
- term.write(" ")
- term.setBackgroundColor(colors.black)
- end
- -- Displays the text with red background color.
- function showSimpleButton(x, y, text)
- term.setCursorPos(x, y)
- term.setBackgroundColor(colors.red)
- term.write(text)
- term.setBackgroundColor(colors.black)
- end
- -- Displays the "Back"- and "Refresh"-Buttons
- function showDefaultButtons()
- x = maxX - string.len(refreshText) + 1
- showSimpleButton(x, maxY, refreshText)
- showSimpleButton(1, maxY, backText)
- end
- -- Loads the values of all variables and sliders
- -- of the current screen.
- function loadObjects()
- for objectID, object in pairs(screens[currentScreen]) do
- objectType = object.objType
- x = object.x
- y = object.y
- if (objectType == "Variable") then
- value = getVariableValue(object)
- term.setCursorPos(x, y)
- term.write(value)
- elseif (objectType == "Slider") then
- length = object.length
- value = getSliderValue(object)
- showSlider(object, value)
- end
- end
- end
- -- Displays all objects of the selected screen.
- function showScreen(screenID)
- term.clear()
- currentScreen = screenID
- if (currentScreen == "mainScreen") then
- backText = "Quit"
- else
- backText = "Back"
- end
- local objectType
- for sObjectID, sObject in pairs(screens[screenID]) do
- objectType = sObject.objType
- if (objectType == "Button") then
- showButton(sObject, buttonDefaultColor)
- elseif (objectType == "Text") then
- showText(sObject)
- elseif (objectType == "Slider") then
- showSlider(sObject, 0)
- elseif (objectType == "Input") then
- showInput(sObject)
- end
- end
- loadObjects()
- showDefaultButtons()
- term.setCursorPos(1, maxY)
- end
- -- Waits until the user touches the monitor and
- -- if he touched a button, the function stored in
- -- it will be called.
- function getInput()
- finished = false
- key, side, x, y = os.pullEvent("monitor_touch")
- if (y == maxY) then -- Checking the default buttons
- if (x <= string.len(backText)) then -- "Back"-Button pressed
- if (currentScreen == "mainScreen") then
- quit = true
- else
- if (screens[currentScreen].parentScreen ~= nil) then
- showScreen(screens[currentScreen].parentScreen)
- finished = true
- else
- showScreen("mainScreen")
- finished = true
- end
- end
- elseif (x >= maxX - string.len(refreshText)) then -- "Refresh"-Button pressed
- showScreen(currentScreen)
- finished = true
- end
- end
- if (finished == true or quit==true) then
- return nil
- end
- for sObjectID, sObject in pairs(screens[currentScreen]) do
- objectType = sObject.objType
- if (objectType == "Button") then
- left = sObject.x
- top = sObject.y
- width = sObject.width
- height = sObject.height
- right = left + width
- bottom = top + height
- if (x >= left and x < right and y >= top and y < bottom) then
- if (sObject.funcType ~= nil and sObject.param ~= nil) then
- callAction(sObject.funcType, sObject.param, sObject)
- finished = true
- end
- end
- elseif (objectType == "Input") then
- left = sObject.x
- top = sObject.y
- inputID = sObject.inputID
- message = sObject.message
- isPassword = sObject.isPassword
- if ((x == left or x == left + 1) and y == top) then
- getUserInput(sObject)
- end
- end
- if (finished == true) then
- break
- end
- end
- --print(key)
- --print("X: " .. x .. ", Y: " .. y)
- --print("max X: " .. maxX .. ", max Y: " .. maxY)
- end
- function callAction(actionType, param, button)
- if (actionType == "switch") then
- showScreen(param)
- elseif (actionType == "function") then
- showButton(button, buttonPressedColor)
- param()
- showButton(button, buttonDefaultColor)
- end
- end
- function getMonitor()
- if (peripheral.getType(monitorSide) == "monitor") then
- monitor = peripheral.wrap(monitorSide)
- return true
- else
- return false
- end
- end
- function debugMessage(message)
- term.restore()
- print(message)
- term.redirect(monitor)
- end
- function main()
- if not getMonitor() then
- print("No monitor at " .. monitorSide .. " side!");
- return
- end
- maxX, maxY = monitor.getSize()
- term.redirect(monitor)
- showScreen("mainScreen")
- while not quit do
- getInput()
- end
- term.clear()
- term.setCursorPos(1, 1)
- term.restore()
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement