Advertisement
Dojnaz

Wireless CC Main

Jan 16th, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.92 KB | None | 0 0
  1. -- by Hannez04
  2.  
  3. local default = {text="???", color=14, colorActive=6, colorText=15, w = 4, h = 3}
  4. local addMode = false
  5. local selected = 0
  6. local square = 4
  7. local w,h = term.getSize()
  8. local colorName = {colors.white, colors.orange, colors.magenta, colors.lightBlue, colors.yellow, colors.lime, colors.pink, colors.lightGray, colors.cyan, colors.purple, colors.blue, colors.brown, colors.green, colors.red, colors.black}
  9. local box = {}
  10.  
  11.  
  12. function save()
  13.         local file = fs.open("redstone","w")
  14.                 file.writeLine(textutils.serialize(default))
  15.                 file.writeLine(textutils.serialize(colorName))
  16.                 file.writeLine(textutils.serialize(box))
  17.         file.close()
  18. end
  19.  
  20.  
  21. function load()
  22.         if fs.exists("redstone") then
  23.                 local file = fs.open("redstone", "r")
  24.                         default = textutils.unserialize(file.readLine())
  25.                         colorName = textutils.unserialize(file.readLine())
  26.                         box = textutils.unserialize(file.readLine())
  27.                 file.close()
  28.         end
  29. end
  30.  
  31.  
  32. function drawBox(b,id)
  33.         id = id or -1
  34.         for i=b.x, b.x+b.w do
  35.                 for j=b.y, (b.y+b.h-1) do
  36.                         term.setCursorPos(i, j)
  37.                         if addMode and selected == id then
  38.                                 if i==b.x and j==b.y then
  39.                                         term.setBackgroundColor(colorName[b.color])
  40.                                 else
  41.                                         if b.color+1 > 14 then
  42.                                                 term.setBackgroundColor(colorName[1])
  43.                                         else
  44.                                                 term.setBackgroundColor(colorName[b.color+1])
  45.                                         end
  46.                                 end
  47.                         elseif b.active then
  48.                                 term.setBackgroundColor(colorName[b.colorActive])
  49.                         else
  50.                                 term.setBackgroundColor(colorName[b.color])
  51.                         end
  52.                         term.write(" ")
  53.                 end
  54.         end
  55.         term.setCursorPos(math.ceil(b.x+b.w/2-#b.text/2), b.y+math.floor(b.h/2))
  56.         term.setTextColor(colorName[b.colorText])
  57.         term.write(b.text)
  58. end
  59.  
  60.  
  61.  
  62.  
  63. function draw()
  64.         term.setCursorPos(1,1)
  65.         term.setBackgroundColor(colors.black)
  66.         term.clear()
  67.        
  68.  
  69.         if addMode then
  70.                 term.setTextColor(colors.black)
  71.                 term.setBackgroundColor(colors.red)
  72.                 term.setCursorPos(1, 1)
  73.                 term.write( string.rep(' ', w) )
  74.                 term.setCursorPos(1, h)
  75.                 term.write( string.rep(' ', w) )
  76.                 for i=2,h-1 do
  77.                         term.setCursorPos(1, i)
  78.                         term.write(" ")
  79.                         term.setCursorPos(w, i)
  80.                         term.write(" ")
  81.                 end
  82.                 term.setCursorPos(w-14, h)
  83.                 term.write("by Hannez04")
  84.         else
  85.                 term.setTextColor(colors.red)
  86.         end
  87.         term.setCursorPos(1, 1)
  88.         term.write("+")
  89.  
  90.         for id,b in pairs(box) do
  91.                 drawBox(b,id)
  92.         end
  93. end
  94.  
  95.  
  96. function changeBox()
  97.         term.setBackgroundColor(colors.black)
  98.         term.setTextColor(colors.white)
  99.         term.clear()
  100.         term.setCursorPos(7,13)
  101.         term.write("Look at the computer screen!")
  102.  
  103.         repeat
  104.                 term.restore()
  105.                 local w, h = term.getSize()
  106.         until w == 51 and h == 19
  107.  
  108.         local b = box[selected]
  109.         while true do
  110.                 term.setBackgroundColor(colors.black)
  111.                 term.clear()
  112.                 for i=19, 19+square do
  113.                         for j=2, (2+math.ceil(square/2)) do
  114.                                 term.setCursorPos(i, j)
  115.                                 term.setBackgroundColor(colorName[b.color])
  116.                                 term.write(" ")
  117.                         end
  118.                 end
  119.                 term.setTextColor(colorName[b.colorText])
  120.                 write(b.text, math.ceil(19+square/2-#b.text/2), 2+math.ceil(square/2)-1)
  121.                 for i=27, 27+square do
  122.                         for j=2, (2+math.ceil(square/2)) do
  123.                                 term.setCursorPos(i, j)
  124.                                 term.setBackgroundColor(colorName[b.colorActive])
  125.                                 term.write(" ")
  126.                         end
  127.                 end
  128.                 term.setTextColor(colorName[b.colorText])
  129.                 write(b.text, math.ceil(27+square/2-#b.text/2), 2+math.ceil(square/2)-1)
  130.  
  131.                 term.setTextColor(colors.white)
  132.                 term.setBackgroundColor(colors.black)
  133.                 write("DELETE",6, 3)
  134.                 write("BACK",40,3)
  135.                 write("+",20,15)
  136.                 write("+",30,15)
  137.                 write("-",20,17)
  138.                 write("-",30,17)
  139.                 write(b.w+1, 20, 16)
  140.                 write(b.h, 30, 16)
  141.                 write("Text (click to edit): " ..  b.text, 1, 7)
  142.                 write("Text Color:", 1,9)
  143.                 for i=1,15 do
  144.                         term.setCursorPos(15+i*2, 9)
  145.                         term.setBackgroundColor(colorName[i])
  146.                         term.write("  ")
  147.                 end
  148.                 term.setBackgroundColor(colors.black)
  149.                 term.setCursorPos(1,11)
  150.                 term.write("Backgroundcolor:")
  151.                 for i=1,15 do
  152.                         term.setCursorPos(15+i*2, 11)
  153.                         term.setBackgroundColor(colorName[i])
  154.                         term.write("  ")
  155.                 end
  156.                 term.setBackgroundColor(colors.black)
  157.                 term.setCursorPos(1,13)
  158.                 term.write("Active Color:")
  159.                 for i=1,15 do
  160.                         term.setCursorPos(15+i*2, 13)
  161.                         term.setBackgroundColor(colorName[i])
  162.                         term.write("  ")
  163.                 end
  164.                 term.setBackgroundColor(colors.black)
  165.                 term.setTextColor(colors.white)
  166.  
  167.  
  168.  
  169.                 local event = {os.pullEvent()}
  170.                 if event[1] == "mouse_click" then
  171.                         local x,y = event[3], event[4]
  172.                         if y == 3 and x <= 17 then
  173.                                 table.remove(box, selected)
  174.                                 selected = 0
  175.                                 break
  176.                         elseif y == 3 and x >= 40 then
  177.                                 break
  178.                         elseif y == 7 then
  179.                                 b.text = betterRead(23,7)
  180.                         elseif y == 9 and x >= 17 and x < 47 then
  181.                                 b.colorText = math.ceil((x-16)/2)
  182.                         elseif y == 11 and x >= 17 and x < 47 then
  183.                                 b.color = math.ceil((x-16)/2)
  184.                         elseif y == 13 and x >= 17 and x < 47 then
  185.                                 b.colorActive = math.ceil((x-16)/2)
  186.                         elseif y == 15 then
  187.                                 if x < 25 then
  188.                                         b.w = b.w + 1
  189.                                 elseif x > 25 then
  190.                                         b.h = b.h + 1
  191.                                 end
  192.                         elseif y == 17 then
  193.                                 if x < 25 and b.w > 1 then
  194.                                         b.w = b.w - 1
  195.                                 elseif x > 25 and b.h > 2 then
  196.                                         b.h = b.h - 1
  197.                                 end
  198.                         end
  199.                 end
  200.         end
  201.         term.clear()
  202.         term.setCursorPos(1, 1)
  203.         term.write("Look at the monitor!")
  204.         local monitor = peripheral.wrap("top")
  205.         term.redirect(monitor)
  206.         w,h = term.getSize()
  207. end
  208.  
  209. function write(text,x,y)
  210.         term.setCursorPos(x, y)
  211.         term.write(tostring(text))
  212. end
  213.  
  214. function betterRead(x,y) -- DONE 1
  215.         term.setTextColor(colors.lightGray)
  216.         term.setCursorBlink(true)
  217.         local s = tostring(box[selected].text)
  218.  
  219.         while true do
  220.             term.setCursorPos(x,y)
  221.             term.write( string.rep(' ', w - x + 1) )
  222.             term.setCursorPos(x,y)
  223.             if s:len()+x < w then
  224.                 term.write(s)
  225.             else
  226.                 term.write(s:sub( s:len() - (w-x-2)))
  227.             end
  228.             local e = { os.pullEvent() }
  229.                 if e[1] == "mouse_click" then
  230.                         break
  231.                 elseif e[1] == "char" then
  232.                         s = s .. e[2]
  233.                 elseif e[1] == "key" then
  234.                         if e[2] == keys.enter then
  235.                                 break
  236.                         elseif e[2] == keys.backspace then
  237.                                 s = s:sub( 1, s:len() - 1 )
  238.                         end
  239.                 end
  240.         end
  241.  
  242.         term.setTextColor(colors.white)
  243.         term.setCursorBlink(false)
  244.         return s
  245. end
  246.  
  247. function countArray(a)
  248.         local elements = 0
  249.         for k,v in pairs(a) do
  250.                 elements = elements + 1
  251.         end
  252.         return elements
  253. end
  254.  
  255. function main()
  256.         while true do
  257.                 save()
  258.                 draw()
  259.                 local event = {os.pullEvent()}
  260.                 if event[1] == "monitor_touch" then
  261.                         local Px,Py = event[3], event[4]
  262.                         if Px == 1 and Py == 1 then -- toggle between mode
  263.                                 addMode = not addMode
  264.                                 selected = 0
  265.                         elseif addMode then -- edit
  266.                                 local createNew = true
  267.                                 for id,b in pairs(box) do
  268.                                         if (Px >= b.x and Px <= b.x+b.w) and (Py >= b.y and Py <= b.y+math.ceil(b.h/2)) then
  269.                                                 if selected == id and b.x==Px and b.y ==Py then
  270.                                                         changeBox()
  271.                                                         createNew = false
  272.                                                         break
  273.                                                 elseif selected == id then
  274.                                                         createNew = false
  275.                                                         selected = 0
  276.                                                         break
  277.                                                 else
  278.                                                         selected = id
  279.                                                         createNew = false
  280.                                                         break
  281.                                                 end
  282.                                         elseif selected == id then
  283.                                                 box[selected].x = Px
  284.                                                 box[selected].y = Py
  285.                                                 createNew = false
  286.                                                 break
  287.                                         end
  288.                                 end
  289.                                 if createNew and selected == 0 then
  290.                                         table.insert(box, countArray(box)+1, {x=Px,y=Py,w=default.w,h=default.h,text=default.text,color=default.color,colorActive=default.colorActive,colorText=default.colorText,active=false})
  291.                                 end
  292.                         else -- activate
  293.                                 for id,b in pairs(box) do
  294.                                         if (Px >= b.x and Px <= b.x+b.w) and (Py >= b.y and Py <= b.y+math.ceil(b.h/2)) then
  295.                                                 b.active = not b.active
  296.                                                 rednet.broadcast("" .. b.text .. tostring(b.active))
  297.                                         end
  298.                                 end
  299.                         end
  300.                 end
  301.                 sleep(0)
  302.         end
  303. end
  304.  
  305. function openRednet()
  306.         for _, side in pairs(redstone.getSides()) do
  307.       if peripheral.getType(side) == "modem" then
  308.         rednet.open(side)
  309.       end
  310.     end
  311. end
  312.  
  313. openRednet()
  314.  
  315. term.clear()
  316. term.setCursorPos(1, 1)
  317. term.write("Look at the monitor!")
  318. local monitor = peripheral.wrap("top")
  319. term.redirect(monitor)
  320. w,h = term.getSize()
  321.  
  322. if not (w == 39 or h == 26) then
  323.         repeat
  324.                 term.restore()
  325.                 local w, h = term.getSize()
  326.         until w == 51 and h == 19
  327.         term.clear()
  328.         term.setCursorPos(1, 1)
  329.         print("You are not using a 4x4 monitor!")
  330. else
  331.         load()
  332.         main()
  333. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement