Advertisement
PaymentOption

Animate

Sep 14th, 2014
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.20 KB | None | 0 0
  1. local bEnableAnimations = true
  2.  
  3. function enable()
  4.     bEnableAnimations = true
  5. end
  6.  
  7. function disable()
  8.     bEnableAnimations = false
  9. end
  10.  
  11. local function getTableSize (tTable)
  12.     local nSize = 0
  13.  
  14.     for _, __ in pairs (tTable) do
  15.         nSize = nSize + 1
  16.     end
  17.  
  18.     return nSize
  19. end
  20.  
  21. local function getShrunkBuffer (tBuffer, nWidthScale, nHeightScale)
  22.     nHeightScale = math.floor (nHeightScale)
  23.     nWidthScale  = math.floor (nWidthScale)
  24.  
  25.     -- Gotta set up new width and height dimensions so getChunks doesn't freak out!
  26.     local tShrunkBuffer = {
  27.         tText       = {},
  28.         tTextColors = {},
  29.         tBackColors = {},
  30.  
  31.         tTerm        = tBuffer.tTerm,
  32.         bCursorBlink = false
  33.     }
  34.  
  35.     local nShrunkLineNumber = 1
  36.     local nLeftConstraint   = tBuffer.nWidth / 2 - nWidthScale
  37.     local nRightConstraint  = tBuffer.nWidth / 2 + nWidthScale
  38.  
  39.     for nLineNumber = math.floor (tBuffer.nHeight / 2 - nHeightScale), math.floor (tBuffer.nHeight / 2 + nHeightScale) do
  40.         if nLineNumber >= 1 and nLineNumber <= tBuffer.nHeight then
  41.             tShrunkBuffer.tText[nShrunkLineNumber]       = tBuffer.tText[nLineNumber]:sub (nLeftConstraint, nRightConstraint)
  42.             tShrunkBuffer.tTextColors[nShrunkLineNumber] = tBuffer.tTextColors[nLineNumber]:sub (nLeftConstraint, nRightConstraint)
  43.             tShrunkBuffer.tBackColors[nShrunkLineNumber] = tBuffer.tBackColors[nLineNumber]:sub (nLeftConstraint, nRightConstraint)
  44.         end
  45.  
  46.         nShrunkLineNumber = nShrunkLineNumber + 1
  47.     end
  48.  
  49.     tShrunkBuffer.nHeight = getTableSize (tShrunkBuffer.tText)
  50.     return tShrunkBuffer
  51. end
  52.  
  53. function openCenter (tBuffer, nRunTime, nModifier)
  54.     if bEnableAnimations then
  55.         local nModifier = nModifier or 1
  56.  
  57.         local nRequiredIterations = math.min (tBuffer.nWidth, tBuffer.nHeight)
  58.         local nSleepTime          = nRunTime / nRequiredIterations
  59.  
  60.         -- Step needs to be the amount of growth per sleep time to make it under the run time!
  61.         local nWidthStep  = tBuffer.nWidth / nRequiredIterations
  62.         local nHeightStep = tBuffer.nHeight / nRequiredIterations
  63.        
  64.         local nWidthScale  = nWidthStep
  65.         local nHeightScale = nHeightStep
  66.  
  67.         for nTime = 0, nRunTime, nSleepTime * nModifier do
  68.             local tShrunkBuffer = setmetatable (getShrunkBuffer (tBuffer, nWidthScale, nHeightScale), { __index = tBuffer })
  69.  
  70.             if #tShrunkBuffer.tText > 0 then
  71.                 tShrunkBuffer.x = tBuffer.nWidth / 2 - tShrunkBuffer.tText[1]:len() / 2
  72.                 tShrunkBuffer.y = math.max (tBuffer.nHeight / 2 - tShrunkBuffer.nHeight / 2, tBuffer.y)
  73.  
  74.                 tBuffer.render (tShrunkBuffer)
  75.             else
  76.                 break
  77.             end
  78.  
  79.             sleep (nSleepTime)
  80.  
  81.             nWidthScale  = nWidthScale + nWidthStep * nModifier
  82.             nHeightScale = nHeightScale + nHeightStep * nModifier
  83.         end
  84.     end
  85.  
  86.     return tBuffer:render()
  87. end
  88.  
  89. function openCenterColoredRectangle (nColor, nWidth, nHeight, nRunTime, nModifier)
  90.     openCenter (
  91.         Buffer.new (nWidth, nHeight, 1, 1, term.current()):setBackgroundColor (nColor):clear(),
  92.         nRunTime,
  93.         nModifier
  94.     )
  95. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement