Advertisement
natie3

Monitor api

Nov 22nd, 2013
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.53 KB | None | 0 0
  1. m = peripheral.wrap("right")
  2. m.setBackgroundColor(32768)
  3. m.setTextColor(colors.white)
  4. m.setTextScale(0.5)
  5. m.clear()
  6.  
  7. state = "Grid"
  8. x, y = m.getSize()
  9. tempButtons = {}
  10. buttons = {}
  11. current = {}
  12. last = {}
  13. info = {}
  14. info["buttons"] = {}
  15. info["config"] = {}
  16. wide = {["36"] = 5,["57"] = 8,["79"] = 6,["100"] = 9,["121"] = 6,["143"] = 7,["164"] = 9}  
  17. high = {["24"] = 7,["38"] = 7,["52"] = 7}
  18. colors = {["white"] = 1,["orange"] = 2,["magenta"] = 4,["lightblue"] = 8,["yellow"] = 16,["lime"] = 32,["pink"] = 64,["gray"] = 128,["lightgray"] = 256,["cyan"] = 512,["purple"] = 1024,["blue"] = 2048,["brown"] = 4096,["green"] = 8192,["red"] = 16384,["black"] = 32768}
  19. if(x == 15 or y == 15) then
  20.   state = "Wrong_monitor_size"
  21. else
  22.   w = wide[tostring(x)]
  23.   h = high[tostring(y)]
  24. end
  25.  
  26. function clear()
  27.   m.setBackgroundColor(colors.black)
  28.   m.clear()
  29. end
  30.  
  31. function copy(data1,data2)
  32.   data2["x1"] = data1["x1"]
  33.   data2["x2"] = data1["x2"]
  34.   data2["y1"] = data1["y1"]
  35.   data2["y2"] = data1["y2"]
  36. end
  37.  
  38. function tempButton(name,x1,x2,y1,y2)
  39.   tempButtons[name] = {}
  40.   tempButtons[name]["x1"] = x1
  41.   tempButtons[name]["x2"] = x2
  42.   tempButtons[name]["y1"] = y1
  43.   tempButtons[name]["y2"] = y2
  44. end
  45.  
  46. function checkxy(x, y, button)
  47.   for name, data in pairs(button) do
  48.     if y >= data["y1"] and  y <= data["y2"] then
  49.       if x >= data["x1"] and x <= data["x2"] then
  50.         return name
  51.       end
  52.     end
  53.   end
  54.   return "none"
  55. end
  56.  
  57. function checkx(x,y1,y2)
  58.   for i = y1, y2 do
  59.     if checkxy(x,i,buttons) ~= "none" then
  60.       return false
  61.     end
  62.   end
  63.   return true
  64. end
  65.  
  66. function checky(y,x1,x2)
  67.   for i = x1, x2 do
  68.     if checkxy(i,y,buttons) ~= "none" then
  69.       return false
  70.     end
  71.   end
  72.   return true
  73. end
  74.  
  75. function checkLine(a,b)
  76.   if a%w == 1 or b%h == 3 or a == 1 or a == x or b == 1 or b == 2 or b == y then
  77.     return true
  78.   elseif(x == 143) and (a == 142) then
  79.     return true
  80.   else
  81.     return false
  82.   end
  83. end
  84.  
  85. function find(a,b)
  86.   while not checkLine(a,0) do
  87.     a = a - 1
  88.   end
  89.   a = a + 1
  90.   while not checkLine(0,b) do
  91.     b = b - 1
  92.   end
  93.   b = b + 1
  94.   return a, b
  95. end
  96.  
  97. function finish()
  98.   m.setBackgroundColor(colors.black)
  99.   m.clear()
  100.   drawButtons()
  101.   good = false
  102.   while not good do
  103.     term.clear()
  104.     term.setCursorPos(1,1)
  105.     print("[white/orange/magenta/lightblue/yellow/lime/pink/")
  106.     print("gray/lightgray/cyan/purple/blue/brown/green/red/")
  107.     print("black]")
  108.     print("Which color do you want the background to have? ")
  109.     color = io.read()
  110.     if(colors[color] ~= nil) then
  111.       info["config"]["background"] = colors[color]
  112.       good = true
  113.     end
  114.   end
  115.   term.clear()
  116.   term.setCursorPos(1,1)
  117.   print("All the buttons the same colors?[Y/n]")
  118.   result = io.read()
  119.   if(result ~= "n") then
  120.     all = true
  121.     good = false
  122.     while not good do
  123.       term.clear()
  124.       term.setCursorPos(1,1)
  125.       print("[white/orange/magenta/lightblue/yellow/lime/pink/")
  126.       print("gray/lightgray/cyan/purple/blue/brown/green/red/")
  127.       print("black]")
  128.       print("What color should the buttons be? ")
  129.       buttonC = io.read()
  130.       if(colors[buttonC] ~= nil) then
  131.         buttonInC = colors[buttonC]
  132.         good = true
  133.       end
  134.     end
  135.     good = false
  136.     while not good do
  137.       term.clear()
  138.       term.setCursorPos(1,1)
  139.       print("[white/orange/magenta/lightblue/yellow/lime/pink/")
  140.       print("gray/lightgray/cyan/purple/blue/brown/green/red/")
  141.       print("black]")
  142.       print("What color should the border be?")
  143.       print("Leave empty to use the same as the button")
  144.       buttonC = io.read()
  145.       if(buttonC == "") then
  146.         buttonOutC = buttonInC
  147.         good = true
  148.       elseif(colors[buttonC] ~= nil) then
  149.         buttonOutC = colors[buttonC]
  150.         good = true
  151.       end
  152.     end
  153.     good = false
  154.     while not good do
  155.       term.clear()
  156.       term.setCursorPos(1,1)
  157.       print("[white/orange/magenta/lightblue/yellow/lime/pink/")
  158.       print("gray/lightgray/cyan/purple/blue/brown/green/red/")
  159.       print("black]")
  160.       print("What color should text be?")
  161.       buttonC = io.read()
  162.       if(colors[buttonC] ~= nil) then
  163.         textC = colors[buttonC]
  164.         good = true
  165.       end
  166.     end
  167.   else
  168.     all = false
  169.   end
  170.   for name, data in pairs(buttons) do
  171.     if(all) then
  172.       buttons[name]["cIn"] = buttonInC
  173.       buttons[name]["cOut"] = buttonOutC
  174.       buttons[name]["text"] = textC
  175.     else
  176.       good = false
  177.       while not good do
  178.         term.clear()
  179.         term.setCursorPos(1,1)
  180.         print("[white/orange/magenta/lightblue/yellow/lime/pink/")
  181.         print("gray/lightgray/cyan/purple/blue/brown/green/red/")
  182.         print("black]")
  183.         print("What color should '"..name.."' be? ")
  184.         buttonC = io.read()
  185.         if(colors[buttonC] ~= nil) then
  186.           buttons[name]["cIn"] = colors[buttonC]
  187.           good = true
  188.         end
  189.       end
  190.       good = false
  191.       while not good do
  192.         term.clear()
  193.         term.setCursorPos(1,1)
  194.         print("[white/orange/magenta/lightblue/yellow/lime/pink/")
  195.         print("gray/lightgray/cyan/purple/blue/brown/green/red/")
  196.         print("black]")
  197.         print("What color should the border be?")
  198.         print("Leave empty to use the same as the button")
  199.         buttonC = io.read()
  200.         if(buttonC == "") then
  201.           buttons[name]["cOut"] = buttons[name]["cIn"]
  202.           good = true
  203.         elseif(colors[buttonC] ~= nil) then
  204.           buttons[name]["cOut"] = colors[buttonC]
  205.           good = true
  206.         end
  207.       end
  208.       good = false
  209.       while not good do
  210.         term.clear()
  211.         term.setCursorPos(1,1)
  212.         print("[white/orange/magenta/lightblue/yellow/lime/pink/")
  213.         print("gray/lightgray/cyan/purple/blue/brown/green/red/")
  214.         print("black]")
  215.         print("What color should the text be? ")
  216.         textC = io.read()
  217.         if(colors[textC] ~= nil) then
  218.           buttons[name]["text"] = colors[textC]
  219.           good = true
  220.         end
  221.       end  
  222.     end
  223.   end
  224.   term.clear()
  225.   term.setCursorPos(1,1)
  226.   m.setBackgroundColor(info["config"]["background"])
  227.   m.clear()
  228.   drawButtons()
  229.   print("Is this the way you wanted it?[Y/n]")
  230.   result = io.read()
  231.   if(result ~= "n") then  
  232.     return true
  233.   else
  234.     return false
  235.   end
  236. end
  237.  
  238. function drawTempButton(a,a2,b,b2,color,name)
  239.   for i = a, a2 do
  240.     for j = b, b2 do
  241.       m.setBackgroundColor(color)
  242.       m.setCursorPos(i,j)
  243.       m.write(" ")
  244.     end
  245.   end
  246.   yspot = math.floor((b + b2) /2)
  247.   xspot= math.floor((a + a2 - string.len(name))/2)
  248.   m.setCursorPos(xspot,yspot)
  249.   m.write(name)
  250. end
  251.  
  252. function drawGrid()
  253.   for i = 1, x do
  254.     for j = 1, y do
  255.       m.setCursorPos(i,j)
  256.       if checkLine(i,j) then
  257.         m.setBackgroundColor(colors.black)
  258.       else
  259.         m.setBackgroundColor(colors.red)
  260.       end
  261.       m.write(" ")
  262.     end
  263.   end
  264. end
  265.  
  266. function drawButton(name,data)
  267.   for i = data["x1"], data["x2"] do
  268.     for j = data["y1"], data["y2"] do
  269.       m.setCursorPos(i,j)
  270.       if(i == data["x1"] or i == data["x2"] or j == data["y1"] or j == data["y2"]) then
  271.         m.setBackgroundColor(data["cOut"])
  272.       else
  273.         m.setBackgroundColor(data["cIn"])
  274.       end
  275.       m.write(" ")
  276.     end
  277.   end
  278.   yspot = math.floor((data["y1"] + data["y2"]) /2)
  279.   xspot= math.floor((data["x1"] + data["x2"] - string.len(name))/2) + 1
  280.   m.setCursorPos(xspot,yspot)
  281.   m.setBackgroundColor(data["cIn"])
  282.   m.setTextColor(data["text"])
  283.   m.write(name)
  284. end
  285.  
  286. function drawButtons()
  287.   for name, data in pairs(buttons) do
  288.     drawButton(name,data)
  289.   end
  290. end
  291.  
  292. function drawCurrentButton()
  293.   drawTempButton(current["x1"],current["x2"],current["y1"],current["y2"],colors.lime,"")
  294.   if (current["x1"] > 2 and checkx(current["x1"]-w,current["y1"],current["y2"])) then
  295.     tempButton("1",current["x1"] - w,current["x1"] - 1,current["y1"],current["y2"])
  296.     drawTempButton(tempButtons["1"]["x1"],tempButtons["1"]["x2"],tempButtons["1"]["y1"],tempButtons["1"]["y2"],colors.red,"")
  297.   end
  298.   if (current["y1"] > 4 and checky(current["y1"]-h,current["x1"],current["x2"])) then
  299.     tempButton("2",current["x1"],current["x2"],current["y1"] - h,current["y1"]-1)
  300.     drawTempButton(tempButtons["2"]["x1"],tempButtons["2"]["x2"],tempButtons["2"]["y1"],tempButtons["2"]        ["y2"],colors.red,"")
  301.   end
  302.   if (current["x2"] < (x - 4) and checkx(current["x2"]+w,current["y1"],current["y2"])) then
  303.     tempButton("3",current["x2"]+1,current["x2"] + w,current["y1"],current["y2"])
  304.     drawTempButton(tempButtons["3"]["x1"],tempButtons["3"]["x2"],tempButtons["3"]["y1"],tempButtons["3"]["y2"],colors.red,"")
  305.   end
  306.   if (current["y2"] < (y - 4) and checky(current["y2"]+h,current["x1"],current["x2"])) then
  307.     tempButton("4",current["x1"],current["x2"],current["y2"]+1,current["y2"]+h)
  308.     drawTempButton(tempButtons["4"]["x1"],tempButtons["4"]["x2"],tempButtons["4"]["y1"],tempButtons["4"]["y2"],colors.red,"")
  309.   end
  310. end
  311.  
  312. function makeCurrentButton()
  313.   startx, starty = find(event[3],event[4])
  314.   current["x1"] = startx
  315.   current["x2"] = startx + w - 2
  316.   current["y1"] = starty
  317.   current["y2"] = starty + h - 2
  318.   copy(current,last)  
  319. end
  320.  
  321. function makeButton(name)
  322.   buttons[name] = {}
  323.   buttons[name]["x1"] = current["x1"]
  324.   buttons[name]["x2"] = current["x2"]
  325.   buttons[name]["y1"] = current["y1"]
  326.   buttons[name]["y2"] = current["y2"]
  327.   buttons[name]["cIn"] = 2048
  328.   buttons[name]["cOut"] = 2048
  329.   buttons[name]["text"] = 1
  330. end
  331.  
  332. function editButton(r)
  333.   if r == "1" then
  334.     copy(current,last)
  335.     current["x1"] = tempButtons["1"]["x1"]
  336.   elseif r == "2" then
  337.     copy(current,last)
  338.     current["y1"] = tempButtons["2"]["y1"]
  339.   elseif r == "3" then
  340.     copy(current,last)
  341.     current["x2"] = tempButtons["3"]["x2"]
  342.   elseif r == "4" then
  343.     copy(current,last)
  344.     current["y2"] = tempButtons["4"]["y2"]
  345.   end
  346. end
  347.  
  348. tempButton("Clear",1,12,1,3)
  349. tempButton("Undo",14,22,1,3)
  350. tempButton("Confirm",24,36,1,3)
  351. while state ~= "Ready" do
  352.   clear()
  353.   if(state == "Editing") then
  354.     drawTempButton(tempButtons["Clear"]["x1"],tempButtons["Clear"]["x2"],tempButtons["Clear"]["y1"],tempButtons["Clear"]["y2"],colors.gray,"Clear")
  355.     drawTempButton(tempButtons["Undo"]["x1"],tempButtons["Undo"]["x2"],tempButtons["Undo"]["y1"],tempButtons["Undo"]["y2"],colors.gray,"Undo")
  356.     drawTempButton(tempButtons["Confirm"]["x1"],tempButtons["Confirm"]["x2"],tempButtons["Confirm"]["y1"],tempButtons["Confirm"]["y2"],colors.gray,"Confirm")
  357.     drawCurrentButton()
  358.     drawButtons()
  359.   elseif(state == "Grid") then
  360.     drawGrid()
  361.     drawButtons()
  362.   elseif(state == "Wrong_monitor_size") then
  363.     m.setCursorPos(1,1)
  364.     m.write("This size")
  365.     m.setCursorPos(1,2)
  366.     m.write("isnt supported")
  367.   elseif(state == "Monitor_resize") then
  368.     a,b = m.getSize()
  369.     yspot = math.floor(b/2)
  370.     xspot = math.floor((1 + a - 25)/2) + 1
  371.     m.setCursorPos(xspot,yspot-1)
  372.     m.write("You resized the monitor,")
  373.     m.setCursorPos(xspot,yspot)
  374.     m.write("return to the old size")
  375.     m.setCursorPos(xspot,yspot+1)
  376.     m.write("or restart this program")
  377.   end
  378.   event = { os.pullEvent() }
  379.   if(event[1] == "monitor_resize") then
  380.     a,b = m.getSize()
  381.     if(a == 15 or b == 15) then
  382.       state = "Wrong_monitor_size"
  383.     elseif(a ~= x or b ~= y) then
  384.       state = "Monitor_resize"
  385.     elseif(a == x and b == y) then
  386.       state = "Grid"
  387.     end
  388.   elseif(event[1] == "monitor_touch") then
  389.     if(state == "Grid") then
  390.       if not checkLine(event[3],event[4]) then
  391.         makeCurrentButton()
  392.         state = "Editing"
  393.       end
  394.     elseif(state == "Editing") then
  395.       result = checkxy(event[3], event[4], tempButtons)
  396.       if(result == "Clear") then
  397.         state = "Grid"
  398.       elseif(result == "Undo") then
  399.         copy(last,current)
  400.         drawCurrentButton()
  401.       elseif(result == "Confirm") then
  402.         term.clear()
  403.         clear()
  404.         term.setCursorPos(1,1)
  405.         yspot = math.floor(y/2)
  406.         xspot = math.floor((1 + x - 25)/2) + 1
  407.         m.setCursorPos(15,15)
  408.         m.write("Plz fill the name into the terminal")
  409.         write("Whats the label of the button? ")
  410.         name = io.read()
  411.         makeButton(name)
  412.         term.setCursorPos(1,2)
  413.         write("Do you want to add another button?[Y/n] ")
  414.         another = io.read()
  415.         if(another ~= "n") then
  416.           state = "Grid"
  417.         else
  418.           state = "Ready"
  419.           result = false
  420.           while not result do
  421.             result = finish()
  422.           end
  423.         end
  424.       elseif(result ~= "none") then
  425.         editButton(result)
  426.       end
  427.     end
  428.   end
  429. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement