Advertisement
UNOBTANIUM

WirelessControlPDA

Apr 17th, 2014
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.52 KB | None | 0 0
  1. -- by UNOBTANIUM 4.4.2014
  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 UNOBTANIUM")
  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.  
  101.     local b = box[selected]
  102.     while true do
  103.         term.setBackgroundColor(colors.black)
  104.         term.clear()
  105.         for i=4, 4+square do
  106.             for j=2, (2+math.ceil(square/2)) do
  107.                 term.setCursorPos(i, j)
  108.                 term.setBackgroundColor(colorName[b.color])
  109.                 term.write(" ")
  110.             end
  111.         end
  112.         term.setTextColor(colorName[b.colorText])
  113.         write(b.text, math.ceil(4+square/2-#b.text/2), 2+math.ceil(square/2)-1)
  114.         for i=10, 10+square do
  115.             for j=2, (2+math.ceil(square/2)) do
  116.                 term.setCursorPos(i, j)
  117.                 term.setBackgroundColor(colorName[b.colorActive])
  118.                 term.write(" ")
  119.             end
  120.         end
  121.         term.setTextColor(colorName[b.colorText])
  122.         write(b.text, math.ceil(10+square/2-#b.text/2), 2+math.ceil(square/2)-1)
  123.  
  124.         term.setTextColor(colors.white)
  125.         term.setBackgroundColor(colors.black)
  126.         write("DELETE",6, 3)
  127.         write("BACK",40,3)
  128.         write("+",20,15)
  129.         write("+",30,15)
  130.         write("-",20,17)
  131.         write("-",30,17)
  132.         write(b.w+1, 20, 16)
  133.         write(b.h, 30, 16)
  134.         write("Text (click to edit): " ..  b.text, 1, 7)
  135.         write("Text Color:", 1,9)
  136.         for i=1,15 do
  137.             term.setCursorPos(15+i*2, 9)
  138.             term.setBackgroundColor(colorName[i])
  139.             term.write("  ")
  140.         end
  141.         term.setBackgroundColor(colors.black)
  142.         term.setCursorPos(1,11)
  143.         term.write("Backgroundcolor:")
  144.         for i=1,15 do
  145.             term.setCursorPos(15+i*2, 11)
  146.             term.setBackgroundColor(colorName[i])
  147.             term.write("  ")
  148.         end
  149.         term.setBackgroundColor(colors.black)
  150.         term.setCursorPos(1,13)
  151.         term.write("Active Color:")
  152.         for i=1,15 do
  153.             term.setCursorPos(15+i*2, 13)
  154.             term.setBackgroundColor(colorName[i])
  155.             term.write("  ")
  156.         end
  157.         term.setBackgroundColor(colors.black)
  158.         term.setTextColor(colors.white)
  159.  
  160.  
  161.  
  162.         local event = {os.pullEvent()}
  163.         if event[1] == "mouse_click" then
  164.             local x,y = event[3], event[4]
  165.             if y == 3 and x <= 17 then
  166.                 table.remove(box, selected)
  167.                 selected = 0
  168.                 break
  169.             elseif y == 3 and x >= 40 then
  170.                 break
  171.             elseif y == 7 then
  172.                 b.text = betterRead(23,7)
  173.             elseif y == 9 and x >= 17 and x < 47 then
  174.                 b.colorText = math.ceil((x-16)/2)
  175.             elseif y == 11 and x >= 17 and x < 47 then
  176.                 b.color = math.ceil((x-16)/2)
  177.             elseif y == 13 and x >= 17 and x < 47 then
  178.                 b.colorActive = math.ceil((x-16)/2)
  179.             elseif y == 15 then
  180.                 if x < 25 then
  181.                     b.w = b.w + 1
  182.                 elseif x > 25 then
  183.                     b.h = b.h + 1
  184.                 end
  185.             elseif y == 17 then
  186.                 if x < 25 and b.w > 1 then
  187.                     b.w = b.w - 1
  188.                 elseif x > 25 and b.h > 2 then
  189.                     b.h = b.h - 1
  190.                 end
  191.             end
  192.         end
  193.     end
  194.     term.clear()
  195.     term.setCursorPos(1, 1)
  196. end
  197.  
  198. function write(text,x,y)
  199.     term.setCursorPos(x, y)
  200.     term.write(tostring(text))
  201. end
  202.  
  203. function betterRead(x,y) -- DONE 1
  204.     term.setTextColor(colors.lightGray)
  205.     term.setCursorBlink(true)
  206.     local s = tostring(box[selected].text)
  207.  
  208.     while true do
  209.         term.setCursorPos(x,y)
  210.         term.write( string.rep(' ', w - x + 1) )
  211.         term.setCursorPos(x,y)
  212.         if s:len()+x < w then
  213.             term.write(s)
  214.         else
  215.             term.write(s:sub( s:len() - (w-x-2)))
  216.         end
  217.         local e = { os.pullEvent() }
  218.         if e[1] == "mouse_click" then
  219.             break
  220.         elseif e[1] == "char" then
  221.             s = s .. e[2]
  222.         elseif e[1] == "key" then
  223.             if e[2] == keys.enter then
  224.                 break
  225.             elseif e[2] == keys.backspace then
  226.                 s = s:sub( 1, s:len() - 1 )
  227.             end
  228.         end
  229.     end
  230.  
  231.     term.setTextColor(colors.white)
  232.     term.setCursorBlink(false)
  233.     return s
  234. end
  235.  
  236. function countArray(a)
  237.     local elements = 0
  238.     for k,v in pairs(a) do
  239.         elements = elements + 1
  240.     end
  241.     return elements
  242. end
  243.  
  244. function main()
  245.     while true do
  246.         save()
  247.         draw()
  248.         local event = {os.pullEvent()}
  249.         if event[1] == "mouse_click" then
  250.             local Px,Py = event[3], event[4]
  251.             if Px == 1 and Py == 1 then -- toggle between mode
  252.                 addMode = not addMode
  253.                 selected = 0
  254.             elseif addMode then -- edit
  255.                 local createNew = true
  256.                 for id,b in pairs(box) do
  257.                     if (Px >= b.x and Px <= b.x+b.w) and (Py >= b.y and Py <= b.y+math.ceil(b.h/2)) then
  258.                         if selected == id and b.x==Px and b.y ==Py then
  259.                             changeBox()
  260.                             createNew = false
  261.                             break
  262.                         elseif selected == id then
  263.                             createNew = false
  264.                             selected = 0
  265.                             break
  266.                         else
  267.                             selected = id
  268.                             createNew = false
  269.                             break
  270.                         end
  271.                     elseif selected == id then
  272.                         box[selected].x = Px
  273.                         box[selected].y = Py
  274.                         createNew = false
  275.                         break
  276.                     end
  277.                 end
  278.                 if createNew and selected == 0 then
  279.                     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})
  280.                 end
  281.             else -- activate
  282.                 for id,b in pairs(box) do
  283.                     if (Px >= b.x and Px <= b.x+b.w) and (Py >= b.y and Py <= b.y+math.ceil(b.h/2)) then
  284.                         b.active = not b.active
  285.                         rednet.broadcast("" .. b.text .. tostring(b.active))
  286.                     end
  287.                 end
  288.             end
  289.         end
  290.         sleep(0)
  291.     end
  292. end
  293.  
  294. function openRednet()
  295.     rednet.open("back")
  296. end
  297.  
  298. openRednet()
  299.  
  300. term.clear()
  301. term.setCursorPos(1, 1)
  302. if not (w == 26 or h == 20) then
  303.         print("You are not using a pda!")
  304. else
  305.         load()
  306.         main()
  307. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement