Advertisement
Encreedem

Screen v1.0

Jul 6th, 2013
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.73 KB | None | 0 0
  1. -- Object types (objType):
  2.  
  3. -- Button: X, Y, width, height, text, funcType,
  4. --         param
  5. -- A normal button. You press it and something
  6. -- happens!
  7. --
  8. -- function types:
  9. -- * switch (changes the shown screen)
  10. -- * function (calls a function; The param-value
  11. --             has to be the function, not a string!)
  12.  
  13. -- Text:  X, Y, text
  14. -- Boring old text. It's just on your monitor and
  15. -- looks nice.
  16.  
  17. -- Variable: X, Y, variableID
  18. -- Just like text but you set the value using the
  19. -- "getVariableValue" method.
  20.  
  21. -- Slider: X, Y, length, direction, sliderID
  22. -- It's like the variable but you set the value in
  23. -- percent (0-100) and it gets shown as a bar.
  24.  
  25. -- Input: X, Y, inputID, message, isPassword
  26. -- It's like the button but if the user touches
  27. -- it, he will get redirected to the computer to
  28. -- enter something.
  29.  
  30. --monitor
  31. local monitorSide = "right"
  32. local monitor = nil
  33.  
  34. -- texts
  35. local refreshText = "Refresh"
  36. local backText = "Back"
  37. local alphabet = {}
  38. alphabet[30]="a"
  39. alphabet[48]="b"
  40. alphabet[46]="c"
  41. alphabet[32]="d"
  42. alphabet[18]="e"
  43. alphabet[33]="f"
  44. alphabet[34]="g"
  45. alphabet[35]="h"
  46. alphabet[23]="i"
  47. alphabet[36]="j"
  48. alphabet[37]="k"
  49. alphabet[38]="l"
  50. alphabet[50]="m"
  51. alphabet[49]="n"
  52. alphabet[24]="o"
  53. alphabet[25]="p"
  54. alphabet[16]="q"
  55. alphabet[19]="r"
  56. alphabet[31]="s"
  57. alphabet[20]="t"
  58. alphabet[22]="u"
  59. alphabet[47]="v"
  60. alphabet[17]="w"
  61. alphabet[45]="x"
  62. alphabet[21]="y"
  63. alphabet[44]="z"
  64. alphabet[57]=" "
  65. alphabet[2]="1"
  66. alphabet[3]="2"
  67. alphabet[4]="3"
  68. alphabet[5]="4"
  69. alphabet[6]="5"
  70. alphabet[7]="6"
  71. alphabet[8]="7"
  72. alphabet[9]="8"
  73. alphabet[10]="9"
  74. alphabet[11]="0"
  75.  
  76. -- colors
  77. local buttonDefaultColor = colors.red
  78. local buttonPressedColor = colors.lime
  79. local sliderLowValueColor = colors.red
  80. local sliderMediumValueColor = colors.yellow
  81. local sliderHighValueColor = colors.lime
  82.  
  83. -- other
  84. local quit = false
  85. local maxX, maxY = 51, 19
  86. local screens = {}
  87. local currentScreen = "mainScreen"
  88. local lastScreen = ""
  89. local userInputs = {}
  90.  
  91. screens = {
  92.   mainScreen = {
  93.     { objType="Text", x=27, y=2, text="Time:" };
  94.     { objType="Variable", x=33, y=2, varID="Time" };
  95.     { objType="Button", x=2, y=4, width=25, height=3, text="call function!", funcType="function", param=setRandomValue };
  96.     { objType="Button", x=2, y=9, width=25, height=3, text="switch to subScreen", funcType="switch", param="subScreen" };
  97.   };
  98.  
  99.   subScreen = {
  100.     parentScreen="mainScreen";
  101.     { objType="Slider", x=2, y=2, length=15, sliderID="randomSlider" };
  102.     { objType="Text", x=2, y=4, text="Input:" };
  103.     { objType="Input", x=9, y=4, inputID="randomInput", message="Thanks for trying out the input object!", isPassword=false }
  104.   };
  105. }
  106.  
  107. -- Displays a star in the upper left corner for a
  108. -- short amount of time. Used when you want to see
  109. -- when something certain happens.
  110. -- Should only be used when you are desperately
  111. -- looking for a bug.
  112. function extremeDebug()
  113.   term.setCursorPos(1, 1)
  114.   term.write("*")
  115.   os.sleep(0.5)
  116.   term.setCursorPos(1, 1)
  117.   term.write(" ")
  118.   os.sleep(0.5)
  119. end
  120.  
  121. function reloadScreen()
  122.   showScreen(currentScreen)
  123. end
  124.  
  125. -- user variables
  126. local randomValue= 50
  127.  
  128. -- user functions region start
  129.  
  130. function setRandomValue()
  131.   randomValue = math.random(100)
  132. end
  133.  
  134. -- user functions region end
  135.  
  136. -- Let's the user define the value of a certain
  137. -- variable.
  138. function getVariableValue(variable)
  139.   if (variable == nil or variable.objType ~= "Variable") then
  140.     return
  141.   end
  142.  
  143.   variableID = variable.varID
  144.   if (variableID == "testVariable") then
  145.     return 42;
  146.   elseif (variableID == "Time") then
  147.     return textutils.formatTime(os.time(), true)
  148.   end
  149.  
  150.   return ""
  151. end
  152.  
  153. -- Let's the user set the filled part of the
  154. -- slider in percent.
  155. function getSliderValue(slider)
  156.   if (slider == nil or slider.objType ~= "Slider") then
  157.     return
  158.   end
  159.  
  160.   sliderID = slider.sliderID
  161.  
  162.   if (sliderID == "testSlider") then
  163.     return 87;
  164.   elseif (sliderID == "randomSlider") then
  165.     return randomValue
  166.   end
  167. end
  168.  
  169. -- WARNING! Everything below this comment
  170. -- shouldn't be edited! If you do and the program
  171. -- doesn't work anymore, it's your fault!
  172.  
  173.  
  174. -- Redirects the input to the computer and lets
  175. -- the user enter something. The result will be
  176. -- in the userInputs array with the inputID as the
  177. -- key.
  178. function getUserInput(inputObject)
  179.   if (inputObject == nil or inputObject.objType ~= "Input") then
  180.     return
  181.   end
  182.  
  183.   x = inputObject.x
  184.   y = inputObject.y
  185.   inputID = inputObject.inputID
  186.   message = inputObject.message
  187.   isPassword = (inputObject.isPassword == nil) and false or inputObject.isPassword
  188.   maxLength = inputObject.maxLength
  189.  
  190.   existingInput = userInputs[inputID]
  191.   if (existingInput ~= nil) then -- Clear the text on the input object.
  192.     term.setCursorPos(x, y)
  193.     for i = -1, string.len(existingInput) do
  194.       term.write(" ")
  195.     end
  196.    
  197.     userInputs[inputID] = nil
  198.   end
  199.  
  200.   -- make the input-object yellow
  201.   term.setCursorPos(x, y)
  202.   term.setBackgroundColor(colors.yellow)
  203.   term.write("  ")
  204.   term.setBackgroundColor(colors.black)
  205.  
  206.   term.restore()
  207.   print(message)
  208.   print("ENTER to confirm or CTRL to cancel")
  209.   cursorX, cursorY = term.getCursorPos()
  210.   term.write("_")
  211.   term.setCursorPos(cursorX, cursorY)
  212.  
  213.   finished = false
  214.   ret = false
  215.   userInput = ""
  216.   while (finished == false) do
  217.     event, key = os.pullEvent("key")
  218.     if (key == 28) then -- ENTER
  219.       term.setCursorPos(cursorX, cursorY)
  220.       print(" ")
  221.       print()
  222.       userInputs[inputID] = userInput
  223.       finished = true
  224.       ret = true
  225.     elseif (key == 29) then -- CTRL
  226.       print(" ")
  227.       finished = true
  228.       ret = false
  229.     elseif (key == 14) then -- Backspace
  230.       if (string.len(userInput) > 0) then
  231.         userInput = string.sub(userInput, 1, string.len(userInput) - 1)
  232.         cursorX = cursorX - 1
  233.         term.setCursorPos(cursorX, cursorY)
  234.         term.write("_ ")
  235.         term.setCursorPos(cursorX, cursorY)
  236.       end
  237.     else
  238.       pressedChar = alphabet[key]
  239.       if (pressedChar ~= nil) then
  240.         if (maxLength == nil or string.len(userInput) < maxLenght) then
  241.           if (isPassword) then
  242.             term.write("*_")
  243.           else
  244.             term.write(pressedChar .. "_")
  245.           end
  246.          
  247.           term.setCursorPos(cursorX + 1, cursorY)
  248.           userInput = userInput .. pressedChar
  249.           cursorX = cursorX + 1
  250.         end
  251.       end
  252.     end
  253.   end
  254.  
  255.   term.redirect(monitor)
  256.  
  257.   term.setCursorPos(x, y)
  258.   term.setBackgroundColor(colors.white)
  259.   term.setTextColor(colors.black)
  260.  
  261.   term.write(" ")
  262.   if (userInput ~= nil and userInput ~= "") then
  263.     if isPassword then
  264.       for i = 1, string.len(userInput) do
  265.         term.write("*")
  266.       end
  267.     else
  268.       term.write(userInput)
  269.     end
  270.   end
  271.  
  272.   term.write(" ")
  273.   term.setBackgroundColor(colors.black)
  274.   term.setTextColor(colors.white)
  275.  
  276.   return ret
  277. end
  278.  
  279. -- Checks if dir is a valid direction-string
  280. function isValidDirection(dir)
  281.   if (dir == "left" or
  282.     dir == "up" or
  283.     dir == "right" or
  284.     dir == "down") then
  285.     return true
  286.   end
  287.  
  288.   return false
  289. end
  290.  
  291. -- Displays the text on the screen.
  292. function showText(textObject)
  293.   if (textObject.objType ~= "Text") then
  294.     return
  295.   end
  296.  
  297.   x = textObject.x
  298.   y = textObject.y
  299.   text = textObject.text
  300.   assert(x, "Text: X-coordinate has to be set!")
  301.   assert(y, "Text: Y-coordinate has to be set!")
  302.   assert(text, "Text: text has to be set!")
  303.  
  304.   term.setCursorPos(x, y)
  305.   term.write(text)
  306. end
  307.  
  308. -- Displays the slider on the screen.
  309. function showSlider(slider, fillPercentage)
  310.   if (slider == nil or slider.objType ~= "Slider") then
  311.     return
  312.   end
  313.  
  314.   x = slider.x
  315.   y = slider.y
  316.   length = slider.length
  317.   direction = (isValidDirection(slider.direction)) and slider.direction or "right"
  318.  
  319.   startSymbol, endSymbol = "<", ">"
  320.   addX, addY = 1, 0 -- Sets the direction of the slider, therefore it could even be diagonal.
  321.  
  322.   if (direction == "left") then
  323.     addX, addY = -1, 0
  324.     startSymbol, endSymbol = ">", "<"
  325.     term.setCursorPos(x - length, y)
  326.     term.write(endSymbol)
  327.   elseif (direction == "up") then
  328.     addX, addY = 0, -1
  329.     startSymbol, endSymbol = "V", "^"
  330.     term.setCursorPos(x, y - length)
  331.     term.write(endSymbol)
  332.   elseif (direction == "right") then
  333.     addX, addY = 1, 0
  334.     startSymbol, endSymbol = "<", ">"
  335.     term.setCursorPos(x + length, y)
  336.     term.write(endSymbol)
  337.   elseif (direction == "down") then
  338.     addX, addY = 0, 1
  339.     startSymbol, endSymbol = "^", "V"
  340.     term.setCursorPos(x, y + length)
  341.     term.write(endSymbol)
  342.   else -- return if it's not a valid direction, even if I checked it before
  343.     return
  344.   end
  345.  
  346.   term.setCursorPos(x, y)
  347.   term.write(startSymbol)
  348.  
  349.   if (fillPercentage ~= nil) then
  350.     if (fillPercentage < 33) then
  351.       sliderColor = sliderLowValueColor
  352.     elseif (fillPercentage > 66) then
  353.       sliderColor = sliderHighValueColor
  354.     else
  355.       sliderColor = sliderMediumValueColor
  356.     end
  357.    
  358.     filled = math.floor((length / 100) * fillPercentage)
  359.     currentX = x + addX
  360.     currentY = y + addY
  361.    
  362.     for i = 1, filled do
  363.       paintutils.drawPixel(currentX, currentY, sliderColor)
  364.       currentX = currentX + addX
  365.       currentY = currentY + addY
  366.     end
  367.   end
  368.  
  369.   term.setBackgroundColor(colors.black)
  370. end
  371.  
  372. -- Displays the given button on the screen.
  373. function showButton(button, color)
  374.   if (button == nil or button.objType ~= "Button") then
  375.     return
  376.   end
  377.  
  378.   x = button.x
  379.   y = button.y
  380.   width = button.width
  381.   height = button.height
  382.   text = button.text
  383.  
  384.   for row = x, x + width - 1 do
  385.     for col = y, y + height - 1 do
  386.       paintutils.drawPixel(row, col, color)
  387.     end
  388.   end
  389.  
  390.   --tries to center the text in the button
  391.   textCol = x + math.floor((width - string.len(text)) / 2)
  392.   textRow = y + math.ceil(height / 2) - 1
  393.   term.setCursorPos(textCol, textRow)
  394.   term.write(text)
  395.  
  396.   term.setBackgroundColor(colors.black)
  397. end
  398.  
  399. function showInput(inputObject)
  400.   if (inputObject == nil or inputObject.objType ~= "Input") then
  401.     return
  402.   end
  403.  
  404.   x = inputObject.x
  405.   y = inputObject.y
  406.  
  407.   term.setCursorPos(x, y)
  408.   term.setBackgroundColor(colors.white)
  409.   term.write("  ")
  410.   term.setBackgroundColor(colors.black)
  411. end
  412.  
  413. -- Displays the text with red background color.
  414. function showSimpleButton(x, y, text)
  415.   term.setCursorPos(x, y)
  416.   term.setBackgroundColor(colors.red)
  417.   term.write(text)
  418.   term.setBackgroundColor(colors.black)
  419. end
  420.  
  421. -- Displays the "Back"- and "Refresh"-Buttons
  422. function showDefaultButtons()
  423.   x = maxX - string.len(refreshText) + 1
  424.   showSimpleButton(x, maxY, refreshText)
  425.  
  426.   showSimpleButton(1, maxY, backText)
  427. end
  428.  
  429. -- Loads the values of all variables and sliders
  430. -- of the current screen.
  431. function loadObjects()
  432.   for objectID, object in pairs(screens[currentScreen]) do
  433.     objectType = object.objType
  434.     x = object.x
  435.     y = object.y
  436.    
  437.     if (objectType == "Variable") then
  438.       value = getVariableValue(object)
  439.       term.setCursorPos(x, y)
  440.       term.write(value)
  441.     elseif (objectType == "Slider") then
  442.       length = object.length
  443.       value = getSliderValue(object)
  444.       showSlider(object, value)
  445.     end
  446.   end
  447. end
  448.  
  449. -- Displays all objects of the selected screen.
  450. function showScreen(screenID)
  451.   term.clear()
  452.   currentScreen = screenID
  453.  
  454.   if (currentScreen == "mainScreen") then
  455.     backText = "Quit"
  456.   else
  457.     backText = "Back"
  458.   end
  459.  
  460.   local objectType
  461.  
  462.   for sObjectID, sObject in pairs(screens[screenID]) do
  463.     objectType = sObject.objType
  464.    
  465.     if (objectType == "Button") then
  466.       showButton(sObject, buttonDefaultColor)
  467.     elseif (objectType == "Text") then
  468.       showText(sObject)
  469.     elseif (objectType == "Slider") then
  470.       showSlider(sObject, 0)
  471.     elseif (objectType == "Input") then
  472.       showInput(sObject)
  473.     end
  474.   end
  475.  
  476.   loadObjects()
  477.   showDefaultButtons()
  478.  
  479.   term.setCursorPos(1, maxY)
  480. end
  481.  
  482. -- Waits until the user touches the monitor and
  483. -- if he touched a button, the function stored in
  484. -- it will be called.
  485. function getInput()
  486.   finished = false
  487.   key, side, x, y = os.pullEvent("monitor_touch")
  488.  
  489.   if (y == maxY) then -- Checking the default buttons
  490.     if (x <= string.len(backText)) then -- "Back"-Button pressed
  491.       if (currentScreen == "mainScreen") then
  492.         quit = true
  493.       else
  494.         if (screens[currentScreen].parentScreen ~= nil) then
  495.           showScreen(screens[currentScreen].parentScreen)
  496.           finished = true
  497.         else
  498.           showScreen("mainScreen")
  499.           finished = true
  500.         end
  501.       end
  502.     elseif (x >= maxX - string.len(refreshText)) then -- "Refresh"-Button pressed
  503.       showScreen(currentScreen)
  504.       finished = true
  505.     end
  506.   end
  507.  
  508.   if (finished == true or quit==true) then
  509.     return nil
  510.   end
  511.  
  512.   for sObjectID, sObject in pairs(screens[currentScreen]) do
  513.     objectType = sObject.objType
  514.    
  515.     if (objectType == "Button") then
  516.       left = sObject.x
  517.       top = sObject.y
  518.       width = sObject.width
  519.       height = sObject.height
  520.       right = left + width
  521.       bottom = top + height
  522.      
  523.       if (x >= left and x < right and y >= top and y < bottom) then
  524.         if (sObject.funcType ~= nil and sObject.param ~= nil) then
  525.           callAction(sObject.funcType, sObject.param, sObject)
  526.           finished = true
  527.         end
  528.       end
  529.     elseif (objectType == "Input") then
  530.       left = sObject.x
  531.       top = sObject.y
  532.       inputID = sObject.inputID
  533.       message = sObject.message
  534.       isPassword = sObject.isPassword
  535.      
  536.       if ((x == left or x == left + 1) and y == top) then
  537.         getUserInput(sObject)
  538.       end
  539.     end
  540.    
  541.     if (finished == true) then
  542.       break
  543.     end
  544.   end
  545.  
  546.   --print(key)
  547.   --print("X: " .. x .. ", Y: " .. y)
  548.   --print("max X: " .. maxX .. ", max Y: " .. maxY)
  549. end
  550.  
  551. function callAction(actionType, param, button)
  552.   if (actionType == "switch") then
  553.     showScreen(param)
  554.   elseif (actionType == "function") then
  555.     showButton(button, buttonPressedColor)
  556.     param()
  557.     showButton(button, buttonDefaultColor)
  558.   end
  559. end
  560.  
  561. function getMonitor()
  562.   if (peripheral.getType(monitorSide) == "monitor") then
  563.     monitor = peripheral.wrap(monitorSide)
  564.     return true
  565.   else
  566.     return false
  567.   end
  568. end
  569.  
  570. function debugMessage(message)
  571.   term.restore()
  572.   print(message)
  573.   term.redirect(monitor)
  574. end
  575.  
  576. function main()
  577.   if not getMonitor() then
  578.     print("No monitor at " .. monitorSide .. " side!");
  579.     return
  580.   end
  581.  
  582.   maxX, maxY = monitor.getSize()
  583.   term.redirect(monitor)
  584.   showScreen("mainScreen")
  585.  
  586.   while not quit do
  587.     getInput()
  588.   end
  589.  
  590.   term.clear()
  591.   term.setCursorPos(1, 1)
  592.   term.restore()
  593. end
  594.  
  595. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement