Te-ki

CCEnderPainter

Mar 28th, 2016
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.92 KB | None | 0 0
  1. --ComputerCraft EnderPainter by Teki 0.1
  2.  
  3. local monitor
  4. local eventType = "mouse_click"
  5. local enderStorage
  6. local color1 = 1
  7. local color2 = 1
  8. local color3 = 1
  9. local colorSaved1 = 1
  10. local colorSaved2 = 1
  11. local colorSaved3 = 1
  12.  
  13. local function roundTo(num, n)
  14.   local mult = 10^(n or 0)
  15.   return math.floor(num * mult + 0.5) / mult
  16. end
  17.  
  18. function getIndex(tab, target)
  19.     for i=1,#tab do
  20.         if tab[i] == target then
  21.             return i
  22.         end
  23.     end
  24.     return false
  25. end
  26.  
  27. if peripheral.find("monitor") == nil then
  28.     monitor = term.current()
  29. else
  30.     monitor = peripheral.find("monitor")
  31.     term.redirect(monitor)
  32.     eventType = "monitor_touch"
  33. end
  34.  
  35. local sizeX,sizeY = monitor.getSize()
  36.  
  37. local unitX = roundTo(sizeX/7)
  38. local unitY = roundTo(sizeY/3)
  39.  
  40. local enderColors = {
  41.     colors.white,
  42.     colors.black,
  43.     colors.orange,
  44.     colors.magenta,
  45.     colors.lightBlue,
  46.     colors.yellow,
  47.     colors.lime,
  48.     colors.pink,
  49.     colors.gray,
  50.     colors.lightGray,
  51.     colors.cyan,
  52.     colors.purple,
  53.     colors.blue,
  54.     colors.brown,
  55.     colors.green,
  56.     colors.red
  57. }
  58.  
  59. function catchStorage()
  60.     if peripheral.find("ender_chest") ~= nil then
  61.         enderStorage = peripheral.find("ender_chest")
  62.     elseif peripheral.find("ender_tank") ~= nil then
  63.         enderStorage = peripheral.find("ender_tank")
  64.     else
  65.         enderStorage = nil
  66.     end
  67.    
  68.     if enderStorage ~= nil then
  69.         color1, color2, color3 = enderStorage.getColors()
  70.     end
  71. end
  72.  
  73. function updateColor(colorUpdated)
  74.     colorUpdated = getIndex(enderColors, colorUpdated)
  75.    
  76.     if colorUpdated < 16 then
  77.         return enderColors[colorUpdated+1]
  78.     else
  79.         return enderColors[1]
  80.     end
  81. end
  82.  
  83. function draw()
  84.     monitor.setBackgroundColour(colors.black)
  85.     monitor.clear()
  86.    
  87.     --ENDERSTORAGE COLORS
  88.     paintutils.drawFilledBox(unitX, unitY, 2*unitX, 2*unitY-1, color1)
  89.     paintutils.drawFilledBox(3*unitX, unitY, 4*unitX, 2*unitY-1, color2)
  90.     paintutils.drawFilledBox(5*unitX, unitY, 6*unitX, 2*unitY-1, color3)
  91.    
  92.     -- COLOR INDEX
  93.     paintutils.drawFilledBox(1, sizeY-1, 16, sizeY-1, colors.white)
  94.     for i=1, #enderColors do
  95.         paintutils.drawFilledBox(i, sizeY, i, sizeY, enderColors[i])
  96.     end
  97.    
  98.     -- SAVED COLORS
  99.     paintutils.drawFilledBox(sizeX-8, sizeY-2, sizeX-7, sizeY-1, colorSaved1)
  100.     paintutils.drawFilledBox(sizeX-5, sizeY-2, sizeX-4, sizeY-1, colorSaved2)
  101.     paintutils.drawFilledBox(sizeX-2, sizeY-2, sizeX-1, sizeY-1, colorSaved3)
  102.    
  103.     monitor.setBackgroundColour(colors.black)
  104.     monitor.setCursorPos(2,2)
  105.     if enderStorage == nil then
  106.         monitor.setTextColor(colors.red)
  107.         write("No EnderStorage Found !")
  108.     else
  109.         monitor.setTextColor(colors.green)
  110.         write("EnderStorage Found !   ")
  111.     end
  112.    
  113.         -- ENDERSTORAGE COLOR INDEX
  114.     monitor.setCursorPos(getIndex(enderColors, color3),sizeY-1)
  115.     write("3")
  116.     monitor.setCursorPos(getIndex(enderColors, color2),sizeY-1)
  117.     write("2")
  118.     monitor.setCursorPos(getIndex(enderColors, color1),sizeY-1)
  119.     write("1")
  120.    
  121.     -- COPY/PASTE
  122.     monitor.setCursorPos(sizeX-6,sizeY-3)
  123.     write("COPY")
  124.     monitor.setCursorPos(sizeX-7,sizeY)
  125.     write("PASTE")
  126.    
  127. end
  128.  
  129. while true do
  130.  
  131.     -- Fetching Data
  132.     catchStorage()
  133.    
  134.     -- Drawing
  135.     draw()
  136.    
  137.     -- Catching Events
  138.     event, side, xPos, yPos = os.pullEvent(eventType)
  139.    
  140.     if enderStorage ~= nil then
  141.         enderStorage.setColors(color1, color2, color3)
  142.     end
  143.    
  144.     if (xPos >= unitX and xPos <= unitX*2) and (yPos >= unitY and yPos <= 2*unitY-1) then
  145.         color1 = updateColor(color1)
  146.     elseif (xPos >= unitX*3 and xPos <= unitX*4) and (yPos >= unitY and yPos <= 2*unitY-1) then
  147.         color2 = updateColor(color2)
  148.     elseif (xPos >= unitX*5 and xPos <= unitX*6) and (yPos >= unitY and yPos <= 2*unitY-1) then
  149.         color3 = updateColor(color3)
  150.     elseif (xPos >= 23 and xPos <= 28) and (yPos == 9) then
  151.         colorSaved1 = color1
  152.         colorSaved2 = color2
  153.         colorSaved3 = color3
  154.     elseif (xPos >= 23 and xPos <= 28) and (yPos == 12) then
  155.         color1 = colorSaved1
  156.         color2 = colorSaved2
  157.         color3 = colorSaved3
  158.     end
  159.    
  160.     if enderStorage ~= nil then
  161.         enderStorage.setColors(color1, color2, color3)
  162.     end
  163.    
  164.     sleep(0)
  165. end
Add Comment
Please, Sign In to add comment