Advertisement
billysback

Vindu

Oct 24th, 2013
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.75 KB | None | 0 0
  1. local curCols = {}
  2. buffer_details = {1, 1, colors.black, colors.white, nil}
  3. local sw,sh = term.getSize()
  4.  
  5. local function split(str, pat)
  6.     local t = {}  -- NOTE: use {n = 0} in Lua-5.0
  7.     if str ~= nil then
  8.        local fpat = "(.-)" .. pat
  9.        local last_end = 1
  10.        local s, e, cap = str:find(fpat, 1)
  11.        while s do
  12.           if s ~= 1 or cap ~= "" then
  13.          table.insert(t,cap)
  14.           end
  15.           last_end = e+1
  16.           s, e, cap = str:find(fpat, last_end)
  17.        end
  18.        if last_end <= #str then
  19.           cap = str:sub(last_end)
  20.           table.insert(t, cap)
  21.        end
  22.     else
  23.         print("##ERROR failed to split ["..str.."] by:"..pat)
  24.     end
  25.     return t
  26. end
  27.  
  28. function getColorTab(char, background, text)
  29.     if curCols[char] ~= nil then
  30.         if curCols[char][background] ~= nil then
  31.             if curCols[char][background][text] ~= nil then
  32.                 return curCols[char][background][text]
  33.             end
  34.         end
  35.     end
  36.     return nil
  37. end
  38.  
  39. function addColorTab(col)
  40.     local char = col.char
  41.     local background = col.background
  42.     local text = col.text
  43.     if getColorTab(char, background, text) == nil then
  44.         if curCols[char] == nil then curCols[char] = {} end
  45.         if curCols[char][background] == nil then curCols[char][background] = {} end
  46.         curCols[char][background][text] = col
  47.         return true
  48.     end
  49.     return false
  50. end
  51.  
  52. function createColor(char, background, text)
  53.     --sleep(0)
  54.     local col = getColorTab(char, background, text)
  55.     if col == nil then
  56.         col = {}
  57.         col.char = char
  58.         col.background = background
  59.         col.text = text
  60.         addColorTab(col)
  61.     end
  62.     return col
  63. end
  64.  
  65. function createPixel(x, y, col)
  66.     local pixel = {}
  67.     pixel.x = x
  68.     pixel.y = y
  69.     pixel.col = col
  70.     pixel.compare = function(self, col)
  71.         return self.col == col
  72.     end
  73.     return pixel
  74. end
  75.  
  76.  
  77. function createBuffer()
  78.     local buffer = {}
  79.     buffer.width = sw
  80.     buffer.height = height
  81.     buffer.changes = {}
  82.     buffer.changed = {}
  83.     buffer.term = term
  84.     buffer.tick = 0
  85.     buffer.amt = 0
  86.     buffer.res = 1
  87.    
  88.     buffer_details[5] = buffer
  89.     for x=1,sw do
  90.         buffer[x] = {}
  91.         buffer.changes[x] = {}
  92.         for y=1,sh do
  93.             --print("x="..x..", y="..y)
  94.             buffer[x][y] = createPixel(x, y, createColor(" ", colors.black, colors.white))
  95.             buffer.changes[x][y] = false
  96.         end
  97.     end
  98.    
  99.     buffer.get = function(self, x, y)
  100.         if self[x] ~= nil then
  101.             if self[x][y] ~= nil then
  102.                 return self[x][y]
  103.             end
  104.         end
  105.         return nil
  106.     end
  107.    
  108.     buffer.check = function(self, x, y)
  109.         if self.changes[x] ~= nil then
  110.             if self.changes[x][y] ~= nil then
  111.                 return self.changes[x][y]
  112.             end
  113.         end
  114.         return false
  115.     end
  116.    
  117.     buffer.set = function(self, x, y, col)
  118.         local pixel = self:get(x,y)
  119.         if pixel ~= nil then
  120.             if not pixel:compare(col) then
  121.                 pixel.col = col
  122.                 if not self:check(x,y) then
  123.                     self.changed[#self.changed + 1] = {x,y}
  124.                     if self.changes[x] == nil then self.changes[x] = {} end
  125.                     self.changes[x][y] = true
  126.                 end
  127.             end
  128.         end
  129.     end
  130.    
  131.     buffer.renderPixel = function(self, x, y)
  132.         self.changes[x][y] = false
  133.         local pixel = self[x][y]
  134.         for px = (pixel.x*self.res)-(self.res-1), pixel.x*self.res do
  135.             for py = (pixel.y*self.res)-(self.res-1), pixel.y*self.res do
  136.                 self.term.setCursorPos(px, py)
  137.                 self.term.setBackgroundColor(pixel.col.background)
  138.                 self.term.setTextColor(pixel.col.text)
  139.                 self.term.write(pixel.col.char)
  140.             end
  141.         end
  142.     end
  143.    
  144.     buffer.render = function(self, fps)
  145.         for i=1,#self.changed do
  146.             local pos = self.changed[i]
  147.             self:renderPixel(pos[1], pos[2])
  148.         end
  149.        
  150.         self.amt = self.amt + #self.changed
  151.         self.tick = self.tick + 1
  152.         if self.tick >= 10 then
  153.             term.setCursorPos(1, 13)
  154.             term.setBackgroundColor(colors.black)
  155.             term.setTextColor(colors.white)
  156.             --print(self.amt.." pixels/sec")
  157.             self.tick = 0
  158.             self.amt = 0
  159.         end
  160.         self.changed = {}
  161.     end
  162.    
  163.     buffer.setBuffer = function(self)
  164.         buffer_detials[5] = self
  165.     end
  166.    
  167.     buffer.setCursorPos = function(x, y)
  168.         buffer_details[1] = x
  169.         buffer_details[2] = y
  170.     end
  171.    
  172.     buffer.setBackgroundColor = function(col)
  173.         buffer_details[3] = col
  174.     end
  175.    
  176.     buffer.setTextColor = function(col)
  177.         buffer_details[4] = col
  178.     end
  179.    
  180.     buffer.getSize = function()
  181.         local buffer = buffer_details[5]
  182.         if buffer == nil then print("NIL_BUFFER") end
  183.         return buffer_details[5].width, buffer_details[5].height
  184.     end
  185.    
  186.     buffer.write = function(char)
  187.         char = char:sub(1,1)
  188.         local col = createColor(char, buffer_details[3], buffer_details[4])
  189.         local buffer = buffer_details[5]
  190.         if buffer ~= nil then
  191.             buffer:set(buffer_details[1], buffer_details[2], col)
  192.         end
  193.     end
  194.    
  195.     buffer.writeLine = function(str)
  196.         local buffer = buffer_details[5]
  197.         local x, y = buffer_details[1], buffer_details[2]
  198.         for i=1,str:len() do
  199.             buffer.setCursorPos(x+i-1, y)
  200.             buffer.write(str:sub(i, i))
  201.         end
  202.     end
  203.    
  204.     buffer.clear = function()
  205.         buffer_details[5]:reset()
  206.     end
  207.    
  208.     buffer.reset = function(self)
  209.         local buffer = self
  210.         for x=1,self.width do
  211.             buffer[x] = {}
  212.             buffer.changes[x] = {}
  213.             for y=1,sh do
  214.                 --print("x="..x..", y="..y)
  215.                 local pixel = createPixel(x, y, createColor(" ", buffer_details[3], colors.white))
  216.                 term.setCursorPos(x, y)
  217.                 term.setBackgroundColor(pixel.col.background)
  218.                 term.setTextColor(pixel.col.text)
  219.                 term.write(pixel.col.char)
  220.                 buffer[x][y] = pixel
  221.                
  222.                 buffer.changes[x][y] = false
  223.             end
  224.         end
  225.         buffer.changes = {}
  226.         buffer.changed = {}
  227.     end
  228.    
  229.     return buffer
  230. end
  231.  
  232. local buffer = createBuffer()
  233.  
  234. local on = true
  235. local w, h = term.getSize()
  236. local function createAnimation(frames)
  237.     local ani =  {
  238.         image = {}, -- {background, text, colour}
  239.         curImage = {},
  240.         frames = frames, -- { {x, y}, {background, text, colour} }
  241.         frame = 1,
  242.         paint = {colors.white, colors.black, " "},
  243.         cloneImage = function(self)
  244.             local curImage = {}
  245.             for x=1,#self.image do
  246.                 local tab = self.image[x]
  247.                 curImage[x] = {}
  248.                 for y=1,#tab do
  249.                     local col = self.image[x][y]
  250.                     curImage[x][y] = {unpack(col)}
  251.                 end
  252.             end
  253.             return curImage
  254.         end,
  255.         resetImage = function(self)
  256.             for x=1,w-1 do
  257.                 self.image[x] = {}
  258.                 for y=1,h-5 do
  259.                     self.image[x][y] = {unpack(self.paint)}
  260.                 end
  261.             end
  262.             self.curImage = self:cloneImage()
  263.             self.frame = 1
  264.         end,
  265.         renderEverything = function(self, playing)
  266.             self:render()
  267.             self:renderFrameSelecter(playing)
  268.             self:renderPaintSelecter()
  269.             buffer:render()
  270.         end,
  271.         render = function(self)
  272.             for x=1,#self.curImage do
  273.                 local tab = self.curImage[x]
  274.                 for y=1,#tab do
  275.                     local col = tab[y]
  276.                     buffer.setCursorPos(x, y+2)
  277.                     buffer.setBackgroundColor(col[1])
  278.                     buffer.setTextColor(col[2])
  279.                     buffer.write(col[3])
  280.                 end
  281.             end
  282.         end,
  283.         setBackground = function(self, colour)
  284.             self.paint[1] = colour
  285.         end,
  286.         setText = function(self, colour)
  287.             self.paint[2] = colour
  288.         end,
  289.         setChar = function(self, char)
  290.             self.paint[3] = char
  291.         end,
  292.         paintPoint = function(self, x, y)
  293.             y = y - 2
  294.             --[[if self.frame == 1 then
  295.                 if self.image[x] ~= nil then
  296.                     if self.image[x][y] ~= nil then
  297.                         self.image[x][y] = {unpack(self.paint)}
  298.                     end
  299.                 end
  300.                 self.curImage = self:cloneImage()
  301.             else]]
  302.                 if self.frames[self.frame] == nil then self.frames[self.frame] = {} end
  303.                 local tab = self.frames[self.frame]
  304.                 tab[#tab + 1] = { {x, y}, {unpack(self.paint)} }
  305.                 local old = self.frame
  306.                 if self.frame > 1 then
  307.                     self:setFrame(1)
  308.                     self.frame = 1
  309.                 end
  310.                 self:setFrame(old)
  311.                 self.frame = old
  312.             --end
  313.         end,
  314.         renderPaintSelecter = function(self)
  315.             for y=1,16 do
  316.                 buffer.setCursorPos(w, y)
  317.                 buffer.setBackgroundColor(2^(y-1))
  318.                 buffer.write(" ")
  319.             end
  320.             buffer.setCursorPos(w-1, h-3)
  321.             buffer.setBackgroundColor(self.paint[1])
  322.             buffer.setTextColor(self.paint[2])
  323.             buffer.write(self.paint[3])
  324.         end,
  325.         drawFrame = function(self, f)
  326.             local frame = self.frames[f]
  327.             if frame ~= nil then
  328.                 for i=1,#frame do
  329.                     local info = frame[i]
  330.                     local pos = info[1]
  331.                     local col = info[2]
  332.                     self.curImage[pos[1]][pos[2]] = {unpack(col)}
  333.                 end
  334.                
  335.             end
  336.             --self.frame = f
  337.         end,
  338.         setFrame = function(self, f)
  339.             if f <= self.frame then
  340.                 self.curImage = self:cloneImage()
  341.                 for i=1,f do
  342.                     self:drawFrame(i)
  343.                 end
  344.             elseif f > self.frame then
  345.                 for i=self.frame+1,f do
  346.                     self:drawFrame(i)
  347.                 end
  348.             end
  349.             if self.frame ~= 1 and self.frames[self.frame] == nil then self.frames[self.frame] = {} end
  350.         end,
  351.         getFrame = function(self, i1, i2, i3) --i1 is bar 1, i2 is bar 2, i3 = bar 3
  352.             local frame = ( (i3-1) * w * w ) + ( (i2-1) * w ) + i1
  353.             return frame
  354.         end,
  355.         getFrameBreakdown = function(self, frame)
  356.             local f1 = frame % (w * w)
  357.             local i3 = ((frame - f1) / (w * w))+1
  358.            
  359.             local f2 = f1 % (w)
  360.             local i2 = ((f1 - f2) / (w))+1
  361.            
  362.             local i1 = f2
  363.             return i1, i2, i3
  364.         end,
  365.         renderFrameSelecter = function(self, playing)
  366.             local i1, i2, i3 = self:getFrameBreakdown(self.frame)
  367.            
  368.             local cols = {
  369.                 { colors.lightGray, colors.lightBlue, "-", "+" },
  370.                 { colors.lightGray, colors.blue, "-", "+" },
  371.                 { colors.lightGray, colors.black, "-", "+" }
  372.             }
  373.             if playing then
  374.                 for i=1,#cols do cols[i][1] = colors.red end
  375.             end
  376.             local is = {i1, i2, i3}
  377.             for x=1,w do
  378.                 for off=-2,0 do
  379.                     buffer.setBackgroundColor(cols[off+3][1])
  380.                     buffer.setTextColor(cols[off+3][2])
  381.                     local char = cols[off+3][3] if x == is[off+3] then char = cols[off+3][4] end
  382.                     buffer.setCursorPos(x, h+off)
  383.                     buffer.write(char)
  384.                 end
  385.             end
  386.         end,
  387.         click = function(self, x, y, button)
  388.             if y >= h-2 then --selected frame
  389.                 local i = 3 - (h - y)
  390.                 local ifs = {self:getFrameBreakdown(self.frame)}
  391.                 ifs[i] = x
  392.                 local nframe = self:getFrame(unpack(ifs))
  393.                 self:setFrame(nframe)
  394.                 self.frame = nframe
  395.             elseif x == w then
  396.                 if button == 1 then
  397.                     self:setBackground(2^(y-1))
  398.                 elseif button == 2 then
  399.                     self:setText(2^(y-1))
  400.                 end
  401.             else
  402.                 self:paintPoint(x, y)
  403.             end
  404.         end,
  405.         playAnimation = function(self, pause)
  406.             --self:setFrame(1)
  407.             --self.frame = 1
  408.             local ison = true
  409.            
  410.             local timer = os.startTimer(pause)
  411.             self:renderEverything(true)
  412.             while ison do
  413.                 local event, p1, p2, p3 = os.pullEvent()
  414.                 if event == "timer" and p1 == timer then
  415.                     self:setFrame(1)
  416.                     local old = self.frame
  417.                     self.frame = 1
  418.                     self:setFrame(old)
  419.                     self.frame = old + 1
  420.                     if self.frame >= w^3 then ison = false end
  421.                    
  422.                     self:renderEverything(true)
  423.                     timer = os.startTimer(pause)
  424.                 elseif event == "key" then
  425.                     if p1 == 29 then ison = false end
  426.                 end
  427.             end
  428.             on = true
  429.             runAnimationProgram()
  430.         end,
  431.     }
  432.    
  433.     if ani.frames == nil then ani.frames = {} end
  434.     return ani
  435. end
  436.  
  437. local function getSize(tab)
  438.     local size = 0
  439.     for k,v in pairs(tab) do if v ~= nil then size = size + 1 end end
  440.     return size
  441. end
  442.  
  443. local function form(tab, space)
  444.     --[[if space == nil then space = "" end
  445.     local str = space.." {"
  446.     for k,v in pairs(tab) do
  447.         if type(v) == "table" then
  448.             str = str..", "..form(v, " ")
  449.         else
  450.             str = str..", "..tostring(v)
  451.         end
  452.     end
  453.     return str.." } \n"]]
  454.     return textutils.serialize(tab)
  455. end
  456.  
  457. local function loadFrames(dir)
  458.     local frames = {} -- contains frames[index] = {}
  459.                       --              contains {frame_info1, frame_info2, .. frame_infon}
  460.                       --                    frame_info = { {x, y}, {background, text, char} }
  461.     local file = fs.open(dir, "r")
  462.     local line = file.readLine()
  463.     while line ~= nil do
  464.         local sp1 = split(line, "==")
  465.         local index = tonumber(sp1[1])
  466.         if frames[index] == nil then frames[index] = {} end
  467.         local frame = frames[index]
  468.         if sp1[2] ~= nil and sp1[2] ~= "" then
  469.             local sp2 = split(sp1[2], "::")
  470.             local pos = split(sp2[1], ",")
  471.             local background = tonumber(sp2[2])
  472.             local text = tonumber(sp2[3])
  473.             local char = tostring(sp2[4])
  474.             --print(pos[1]..","..pos[2]..":"..background..":"..text..":"..char)
  475.             frames[index][#frames[index] + 1] = { {tonumber(pos[1]), tonumber(pos[2])}, {background, text, char} }
  476.         end
  477.         frames[index] = frame
  478.         --print("f:"..#frame..", "..index)
  479.         line = file.readLine()
  480.     end
  481.     file.close()
  482.     --print("frames:"..getSize(frames))
  483.     --sleep(1)
  484.     return frames
  485. end
  486.  
  487. local animation = createAnimation()
  488.  
  489. local function saveFrames(dir, frames)
  490.     local file = fs.open(dir, "w")
  491.     for i=1,w^3 do
  492.         local frame = frames[i]
  493.         if frame ~= nil then
  494.             for j=1,#frame do
  495.                 local info = frame[j]
  496.                 local pos = info[1]
  497.                 local col = info[2]
  498.                 --local str = i.."=="..pos[1]..","..pos[2].."::"..col[1].."::"..col[2].."::"..col[3]
  499.                 local str = i.."=="
  500.                 str = str..pos[1]..","
  501.                 str = str..pos[2].."::"
  502.                 str = str..col[1].."::"
  503.                 str = str..col[2].."::"
  504.                 str = str..col[3]
  505.                 file.writeLine(str)
  506.             end
  507.         end
  508.     end
  509.     file.close()
  510. end
  511.  
  512. animation:resetImage()
  513. animation:drawFrame(1)
  514.  
  515.  
  516.  
  517.  
  518. local function fillString(str, w)
  519.     local nstr = str
  520.     while nstr:len() < w do nstr = nstr.." " end
  521.     return nstr
  522. end
  523.  
  524. local input = ""
  525. local msg = ""
  526. local function drawMenu()
  527.     buffer.setCursorPos(1,1)
  528.     buffer.setBackgroundColor(colors.lightGray)
  529.     buffer.setTextColor(colors.blue)
  530.     buffer.writeLine(fillString("| SAVE | OPEN | CLEAR | CLEAR ALL | "..msg, w-1))
  531.     buffer.setCursorPos(1,2)
  532.     buffer.writeLine(fillString("| INPUT: "..input, w-1))
  533. end
  534.  
  535.  
  536. local function checkMenu(x, y)
  537.     if y == 2 then
  538.         term.setBackgroundColor(colors.lightGray)
  539.         term.setTextColor(colors.blue)
  540.         term.setCursorPos(1, 2)
  541.         term.write(fillString("| INPUT: ", w-1))
  542.         term.setCursorPos(("| INPUT:  "):len(), 2)
  543.         input = read()
  544.     elseif y == 1 then
  545.         if x >= 1 and x < 8 then --save
  546.             saveFrames(input, animation.frames)
  547.             msg = "FILE SAVED"
  548.         elseif x >= 8 and x < 15 then --open
  549.             animation.frames = loadFrames(input)
  550.             animation:resetImage()
  551.             animation:setFrame(1)
  552.             animation.frame = 1
  553.             drawMenu()
  554.             animation:renderEverything(false)
  555.             msg = "FILE OPENED"
  556.         elseif x >= 15 and x < 23 then --clear
  557.             animation.frames[animation.frame] = {}
  558.             msg = "FRAME CLEARED"
  559.         elseif x >= 23 and x < 35 then --clear all
  560.             animation:resetImage()
  561.             animation.frames = {}
  562.             msg = "ALL CLEARED"
  563.         end
  564.     end
  565. end
  566.  
  567.  
  568. drawMenu()
  569. animation:renderEverything(false)
  570.  
  571. function runAnimationProgram()
  572.     local doAni = false
  573.     animation:renderEverything(false)
  574.     while on do
  575.         local event, p1, p2, p3 = os.pullEvent()
  576.         if event == "mouse_click" or event == "mouse_drag" then
  577.             animation:click(p2, p3, p1)
  578.             checkMenu(p2, p3)
  579.             drawMenu()
  580.             animation:renderEverything(false)
  581.            
  582.         elseif event == "key" then
  583.             if p1 == 29 then
  584.                 on = false
  585.             elseif p1 == 56 then
  586.                 doAni = true
  587.                 on = false
  588.                 animation:renderEverything(false)
  589.             end
  590.         elseif event == "char" then
  591.             animation:setChar(p1)
  592.         end
  593.     end
  594.     if doAni then animation:playAnimation(0.1) end
  595. end
  596. runAnimationProgram()
  597.  
  598. term.setBackgroundColor(colors.black)
  599. term.clear()
  600. term.setCursorPos(1, 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement