Advertisement
Shiranuit

SurroundAPI

Aug 12th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.70 KB | None | 0 0
  1. --   API de Shiranuit
  2. -- Ne pas vous l'approprié
  3. function newSurround(dt)
  4.     local w, h = 0, 0
  5.     local _w, _h = 0, 0
  6.     local posX, posY = 1, 1
  7.     local _posX, _posY = 1, 1
  8.     local sel = dt[1][1]
  9.     local bg = dt[1][1].getBackgroundColor()
  10.     local fg = dt[1][1].getTextColor()
  11.     local buffer = {}
  12.     local all = {}
  13.     local emptyLine = ""
  14.     local emptyBg = ""
  15.     local emptyFg = ""
  16.     local blinkstate = false
  17.     for x=1, #dt do
  18.         for y=1, #dt[x] do
  19.             if dt[x][y] then
  20.                 all[#all+1]=dt[x][y]
  21.             end
  22.         end
  23.     end
  24.  
  25.     local function getColor(char)
  26.         local chars = "0123456789abcdef"
  27.         local _start, _end = string.find(chars,char)
  28.         if _start then
  29.             return 2^(_start-1)
  30.         end
  31.     end
  32.    
  33.     local function getChar(num)
  34.         local chars = "0123456789abcdef"
  35.         local val = math.log(num)/math.log(2)
  36.         return chars:sub(val+1,val+1)
  37.     end
  38.    
  39.     local function calc()
  40.         w, h = 0, 0
  41.         _w, _h = 0, 0
  42.         for x=1, #dt do
  43.             local _w, _h = dt[x][1].getSize()
  44.             w=w+_w
  45.         end
  46.         for y=1,#dt[1] do
  47.             local _w, _h = dt[1][y].getSize()
  48.             h=h+_h
  49.         end
  50.         _w, _h = dt[1][1].getSize()
  51.         emptyLine = string.rep(" ",w)
  52.         emptyFg = string.rep("0",w)
  53.         emptyBg = string.rep("f",w)
  54.     end
  55.     calc()
  56.    
  57.     local function get(x,y)
  58.         x=x-1
  59.         y=y-1
  60.         local _x = (x-(x%_w))/_w
  61.         local _y = (y-(y%_h))/_h
  62.         _x = _x+1
  63.         _y = _y+1
  64.         return (dt[_x] and dt[_x][_y]), _x, _y
  65.     end
  66.  
  67.     local function getSize()
  68.         return w, h
  69.     end
  70.    
  71.     local function setCursorBlink(state)
  72.         for i=1, #all do
  73.             local sc = all[i]
  74.             sc.setCursorBlink(false)
  75.         end
  76.         if sel then
  77.             sel.setCursorBlink(state)
  78.         end
  79.         blinkstate = state
  80.     end
  81.    
  82.     local function setCursorPos(x,y)
  83.         local sc, w, h = get(x,y)
  84.         posX, posY = x, y
  85.         _posX, _posY = x-((w-1)*_w), y-((h-1)*_h)
  86.         if sc then
  87.             sc.setCursorPos(_posX, _posY)
  88.         end
  89.         if sel ~= sc then
  90.             sel=sc
  91.             setCursorBlink(blinkstate)
  92.         end
  93.     end
  94.    
  95.     local function replacePos(ntxt,x,txt)
  96.         local ntxt = ntxt:sub(1,x-1)..txt..ntxt:sub(x+#txt+1,#ntxt)
  97.         return ntxt
  98.     end
  99.    
  100.     local function writeBuffer(x,y,txt,fg,bg)
  101.         buffer[y] = buffer[y] or {txt=emptyLine,fg=emptyFg,bg=emptyBg}
  102.         buffer[y].txt = replacePos(buffer[y].txt,x,txt)
  103.         buffer[y].fg = replacePos(buffer[y].fg,x,fg)
  104.         buffer[y].bg = replacePos(buffer[y].bg,x,bg)
  105.     end
  106.    
  107.     local function write(txt)
  108.         setCursorPos(posX,posY)
  109.         local tposX, tposY = posX, posY
  110.         local npos = 1
  111.         while npos <= #txt do
  112.             local dst = _w-_posX
  113.             if #txt-npos < dst then
  114.                 dst = #txt-npos
  115.             end
  116.             if sel then
  117.                 sel.write(txt:sub(npos,npos+dst))
  118.             end
  119.             npos = npos + dst + 1
  120.             setCursorPos(posX+1+dst,posY)
  121.         end
  122.         writeBuffer(tposX,tposY,txt,string.rep(getChar(fg),#txt),string.rep(getChar(bg),#txt))
  123.     end
  124.    
  125.     local function getCursorPos()
  126.         return posX, posY
  127.     end
  128.    
  129.     local function setTextColor(color)
  130.         for i=1, #all do
  131.             local sc = all[i]
  132.             sc.setTextColor(color)
  133.         end
  134.         fg = color
  135.     end
  136.        
  137.     local function setBackgroundColor(color)
  138.         for i=1, #all do
  139.             local sc = all[i]
  140.             sc.setBackgroundColor(color)
  141.         end
  142.         bg = color
  143.     end
  144.    
  145.     local function clear()
  146.         setBackgroundColor(bg)
  147.         setTextColor(fg)
  148.         for i=1, #all do
  149.             local sc = all[i]
  150.             sc.clear()
  151.         end
  152.         for i=1, h do
  153.             buffer[i] = {txt=emptyLine,fg=emptyFg,bg=emptyBg}
  154.         end
  155.         setCursorBlink(blinkstate)
  156.     end
  157.    
  158.     local function clearLine()
  159.         setBackgroundColor(bg)
  160.         setTextColor(fg)
  161.         for x=1, #dt do
  162.             local x, y = (x-1)*_w+1, posY
  163.             local sc, w, h = get(x,y)
  164.             local _posX, _posY = x-((w-1)*_w), y-((h-1)*_h)
  165.             if sc then
  166.                 sc.setCursorPos(_posX, _posY)
  167.                 sc.clearLine()
  168.             end
  169.         end
  170.         buffer[posY]={txt=emptyLine,fg=emptyFg,bg=emptyBg}
  171.         setCursorBlink(blinkstate)
  172.     end
  173.    
  174.     local function blit(txt, fgcolor, bgcolor)
  175.         setCursorPos(posX,posY)
  176.         local tposX, tposY = posX, posY
  177.         local npos = 1
  178.         while npos <= #txt do
  179.             local dst = _w-_posX
  180.             if #txt-npos < dst then
  181.                 dst = #txt-npos
  182.             end
  183.             if sel then
  184.                 sel.blit(txt:sub(npos,npos+dst),fgcolor:sub(npos,npos+dst),bgcolor:sub(npos,npos+dst))
  185.             end
  186.             npos = npos + dst + 1
  187.             setCursorPos(posX+1+dst,posY)
  188.         end
  189.         writeBuffer(tposX, tposY, txt, fgcolor, bgcolor)
  190.     end
  191.    
  192.     local function redraw()
  193.         local tposX, tposY = posX, posY
  194.         for i=1, #all do
  195.             local sc = all[i]
  196.             sc.clear()
  197.         end
  198.         for y, v in pairs(buffer) do
  199.             setCursorPos(1,y)
  200.             blit(v.txt,v.fg,v.bg)
  201.         end
  202.         setCursorPos(tposX,tposY)
  203.         setCursorBlink(blinkstate)
  204.     end
  205.    
  206.     local function setTextScale(scale)
  207.         for i=1, #all do
  208.             local sc = all[i]
  209.             sc.setTextScale(scale)
  210.         end
  211.         calc()
  212.         redraw()
  213.     end
  214.    
  215.     local function setPaletteColor(cindex, r, g, b)
  216.         for i=1, #all do
  217.             local sc = all[i]
  218.             sc.setPaletteColor(cindex, r, g, b)
  219.         end
  220.     end
  221.    
  222.     local function getPaletteColor(cindex)
  223.         return all[1].getPaletteColor(cindex)
  224.     end
  225.    
  226.     local function scroll(y)
  227.         y=y or 1
  228.         local nbuffer = {}
  229.         for ny, v in pairs(buffer) do
  230.             nbuffer[ny-y]=v
  231.         end
  232.         buffer = nbuffer
  233.         redraw()
  234.     end
  235.    
  236.     local function getTextColor()
  237.         return fg
  238.     end
  239.    
  240.     local function getBackgroundColor()
  241.         return bg
  242.     end
  243.    
  244.    
  245.     local function isColor()
  246.         return dt[1][1].isColor()
  247.     end
  248.    
  249.     return {getSize=getSize ,setCursorPos=setCursorPos, clear=clear, clearLine=clearLine, write=write, setTextColor=setTextColor, setTextColour=setTextColor, setBackgroundColor=setBackgroundColor, setBackgroundColour=setBackgroundColor, setTextScale=setTextScale, getCursorPos=getCursorPos, scroll=scroll, getBackgroundColor=getBackgroundColor, getBackgroundColour=getBackgroundColor, getTextColor=getTextColor, getTextColour=getTextColor, blit=blit, isColor=isColor, isColour=isColor, setCursorBlink=setCursorBlink, setPaletteColor=setPaletteColor, getPaletteColor=getPaletteColor, setPaletteColour=setPaletteColor, getPaletteColour=getPaletteColor}
  250. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement