akihex

enderGUI

Aug 3rd, 2014
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.85 KB | None | 0 0
  1. local termX, termY = term.getSize()
  2. local wind = window.create(term.current(), 1, 1, termX, termY, false)
  3. local bgColor = colors.white
  4. local altBgColor = colors.blue
  5. local fgColor = colors.blue
  6. local altFgColor = colors.white
  7. local frameColor = colors.green
  8. local altFrameColor = colors.red
  9. local overlay = {}
  10. local reverse = {}
  11.  
  12. local direction = {"right","front","bottom","left","back","top"}
  13.  
  14. local cols = {colors.black, colors.gray, colors.lightGray, colors.white, colors.pink, colors.magenta, colors.purple, colors.red, colors.lightBlue, colors.blue, colors.cyan, colors.green, colors.lime, colors.yellow, colors.orange, colors.brown}
  15.  
  16. local ender = nil
  17. local coord = {}
  18.  
  19. local highlight = {nil,nil,nil}
  20.  
  21. local function drawColor(color,row)
  22.   local x,y = wind.getCursorPos()
  23.  
  24.   local frame = window.create(wind,x-1,y-1,4,4,false)
  25.   if color == frameColor then frame.setBackgroundColor(altFrameColor)
  26.   else frame.setBackgroundColor(frameColor)
  27.   end
  28.   frame.clear()
  29.  
  30.   local clearFrame = window.create(wind,x-1,y-1,4,4,false)
  31.   if x < 15 then clearFrame.setBackgroundColor(altBgColor)
  32.   else clearFrame.setBackgroundColor(bgColor)
  33.   end
  34.   clearFrame.clear()
  35.  
  36.   if x == 15 then
  37.     clearFrame.setBackgroundColor(altBgColor)
  38.     for i=1,4 do
  39.       clearFrame.setCursorPos(1,i)
  40.       clearFrame.write(" ")
  41.     end
  42.   end
  43.  
  44.   reverse[textutils.serialize({color,row})] = {x,y}
  45.  
  46.   local function draw()  
  47.     wind.setBackgroundColor(color)
  48.     frame.setBackgroundColor(color)
  49.     clearFrame.setBackgroundColor(color)
  50.     for i=1,2 do
  51.       coord[textutils.serialize({wind.getCursorPos()})] = {color,row}
  52.       overlay[textutils.serialize({wind.getCursorPos()})] = {frame,row,clearFrame}
  53.       wind.write(" ")
  54.       frame.write(" ")
  55.       clearFrame.write(" ")
  56.     end
  57.   end
  58.  
  59.   wind.setCursorPos(x,y)
  60.   frame.setCursorPos(2,2)
  61.   clearFrame.setCursorPos(2,2)
  62.   draw()
  63.   wind.setCursorPos(x,y+1)
  64.   frame.setCursorPos(2,3)
  65.   clearFrame.setCursorPos(2,3)
  66.   draw()
  67.   wind.setCursorPos(x+3,y)
  68. end
  69.  
  70. local function prepareWindow()
  71.   wind.setVisible(false)
  72.   wind.setBackgroundColor(bgColor)
  73.   wind.setTextColor(fgColor)
  74.   wind.clear()
  75.   wind.setBackgroundColor(altBgColor)
  76.   for i=1,19 do
  77.     wind.setCursorPos(1,i)
  78.     wind.write("              ")
  79.   end
  80.   wind.setCursorPos(1,1)
  81.   wind.setBackgroundColor(altBgColor)
  82.   wind.setTextColor(altFgColor)
  83.   wind.write("Ender Chest/Ta")
  84.   wind.setBackgroundColor(bgColor)
  85.   wind.setTextColor(fgColor)
  86.   wind.write("nk Colour Coder")
  87.   wind.setCursorPos(1,termY)
  88.   wind.setBackgroundColor(altBgColor)
  89.   wind.setTextColor(altFgColor)
  90.   wind.write("Press Escape t")
  91.   wind.setBackgroundColor(bgColor)
  92.   wind.setTextColor(fgColor)
  93.   wind.write("o exit")
  94.  
  95.   coord = {}
  96.  
  97.   local startX, startY = 3, 5
  98.  
  99.   local function draw(row)
  100.     local _,Y = wind.getCursorPos()
  101.     for n,c in pairs(cols) do
  102.       --if n == 9 then wind.setCursorPos(startX,Y+offset) end
  103.       --coord[textutils.serialize({wind.getCursorPos()})] = c
  104.       drawColor(c,row)
  105.     end
  106.   end
  107.  
  108.   wind.setCursorPos(startX,startY)
  109.   draw(1)
  110.   wind.setCursorPos(startX,startY + 4)
  111.   draw(2)
  112.   wind.setCursorPos(startX,startY + 8)
  113.   draw(3)
  114.  
  115.   wind.setVisible(true)
  116. end
  117.  
  118. local function drawUserInterface()
  119.   wind.redraw()
  120.  
  121. end
  122.  
  123. local function switchVisible(frame, row, clearFrame, col)
  124.   local oldClear = nil
  125.   if highlight[row] then
  126.     if highlight[row][1] then
  127.       highlight[row][1].setVisible(false)
  128.     end
  129.     if highlight[row][2] then
  130.       highlight[row][2].setVisible(true)
  131.     end
  132.     if highlight[row][3] then
  133.       highlight[row][3].setVisible(false)
  134.     end
  135.     oldClear = highlight[row][2]
  136.   end
  137.   highlight[row] = {frame,clearFrame,oldClear}
  138.   frame.setVisible(true)
  139.   --if not ender then refreshChest() end
  140.   if ender and col and col[1] and col[2] then
  141.     local currentColors = {}
  142.     currentColors[1], currentColors[2], currentColors[3] = ender.getColors()
  143.     if currentColors[1] and currentColors[2] and currentColors[3] then
  144.       currentColors[col[2]] = col[1]
  145.       ender.setColors(currentColors[1],currentColors[2],currentColors[3])
  146.     end
  147.   end
  148. end
  149.  
  150. local function removeOverlay()
  151.   for i=1,3 do
  152.     if highlight[i] then
  153.       if highlight[i][3] then
  154.         highlight[i][3].setVisible(false)
  155.       end
  156.       if highlight[i][2] then
  157.         highlight[i][2].setVisible(true)
  158.         highlight[i][3] = highlight[i][2]
  159.         highlight[i][2] = nil
  160.       end
  161.       if highlight[i][1] then
  162.         highlight[i][1].setVisible(false)
  163.         highlight[i][1] = nil
  164.       end
  165.     end
  166.   end
  167. end
  168.  
  169. local function refreshChest()
  170.   for i,dir in pairs(direction) do
  171.     --rs.getSides()
  172.     if peripheral.getType(dir) == "ender_chest" or peripheral.getType(dir) == "ender_tank" then
  173.       ender = peripheral.wrap(dir)
  174.     end
  175.     if ender then break end
  176.   end
  177.   if ender then
  178.     removeOverlay()
  179.     local currentColors = {}
  180.     currentColors[1], currentColors[2], currentColors[3] = ender.getColors()
  181.     for i=1,3 do
  182.       local frame = overlay[textutils.serialize(reverse[textutils.serialize({currentColors[i],i})])]
  183.       --local col = coord[textutils.serialize(reverse[textutils.serialize({currentColors[i],i})])]
  184.       if frame and frame[1] and frame[2] and frame[3] then switchVisible(frame[1],frame[2],frame[3],nil) end
  185.     end
  186.   end
  187. end
  188.  
  189.  
  190. prepareWindow()
  191.  
  192. while true do
  193.   drawUserInterface()
  194.   local event, button, xPos, yPos = os.pullEvent()
  195.   if event == "peripheral" then refreshChest() end
  196.   if event == "mouse_click" and button == 1 then
  197.     refreshChest()
  198.     local frame = overlay[textutils.serialize({xPos,yPos})]
  199.     local col = coord[textutils.serialize({xPos,yPos})]
  200.     if frame and frame[1] and frame[2] and frame[3] then switchVisible(frame[1],frame[2],frame[3],col)
  201.     --else refreshChest()
  202.     end
  203.     --drawUserInterface()
  204.   end
  205.   sleep(0.2)
  206. end
Advertisement
Add Comment
Please, Sign In to add comment