diff ObjectiveTracker/Block.lua @ 43:9480bd904f4c

- file name organizing
author Nenue
date Mon, 25 Apr 2016 13:51:58 -0400
parents ObjectiveTracker/TrackerBlock.lua@7a65ed86e4dd
children 756e8aeb040b
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ObjectiveTracker/Block.lua	Mon Apr 25 13:51:58 2016 -0400
@@ -0,0 +1,191 @@
+--- ${PACKAGE_NAME}
+-- @file-author@
+-- @project-revision@ @project-hash@
+-- @file-revision@ @file-hash@
+-- Created: 4/24/2016 11:30 AM
+--- These functions deal with propagating and managing block/line templates
+local B = select(2,...).frame
+local T = B:RegisterModule("ObjectiveTracker", _G.VeneerObjectiveWrapper, 'BuffFrame')
+local _G, ipairs, max, tostring = _G, ipairs, max, tostring
+local tinsert, tremove, tContains = table.insert, table.remove, tContains
+local Default = T.DefaultHandler
+local CreateFrame = CreateFrame
+local print = B.print('Layout')
+local bprint = B.print('Block')
+local lprint = B.print('Layout')
+local fprint = B.print('Frame')
+local used, free
+
+local blockFadeOut_OnPlay = function(self)
+  fprint(self:GetName(), '|cFF00FF00PLAY|r', debugstack(1,3,1))
+end
+local blockFadeOut_OnFinished = function(self)
+  fprint(self:GetName(), '|cFF00FF00FINISHED|r', debugstack(1,3, 1))
+end
+
+local tMove = function(source, dest, frame)
+  -- if it's already in the stack, sanity check source stack
+  local removed
+  if tContains(dest, frame) then
+    for i, entry in ipairs(source) do
+      if entry == frame then
+        removed = i
+        tremove(source, i)
+        break
+      end
+    end
+    -- still need to resolve position
+    for i, entry in ipairs(dest) do
+      if entry == frame then
+          bprint('tMove result:', (removed and ('|cFFFF4400a|r['..removed .. '] is now ') or '') .. '|cFF00FF00b|r[' .. i..']')
+          return i
+      end
+    end
+  else
+  -- if it's not, then pull from source stack
+    for i, entry in ipairs(source) do
+      if entry == frame then
+        removed = i
+        tremove(source, i)
+        break
+      end
+    end
+    tinsert(dest, frame)
+    bprint('tMove result:', (removed and ('|cFFFF4400a|r['..removed .. '] is now ') or '') .. '|cFF00FF00b|r[' .. #dest..']')
+    return #dest
+  end
+end
+
+
+--- Creates or retrieves a complete line data object
+function Default:GetLine (block, lineIndex)
+  local print = lprint
+  local blockIndex = block.index
+  local lines = block.lines
+  if not lineIndex then
+    lineIndex = block.currentLine + 1
+    print('        |cFFFFFF00generating a frame')
+  end
+
+  block.numLines = max(block.numLines, lineIndex)
+
+  if not lines[lineIndex] then
+    print('     |cFF00FF88created line #'..lineIndex..' from for '..self.name..' block #'..blockIndex)
+    lines[lineIndex] = CreateFrame('Frame', 'Vn'..self.name .. blockIndex..'ObjectiveLine'..lineIndex, block, 'VeneerTrackerObjective')
+    local line = lines[lineIndex]
+    line.index = lineIndex
+    line.height = 0
+    line.schema = ''
+    B.SetConfigLayers(line)
+
+    if debug then
+      for _, region in ipairs(lines[lineIndex].debug) do
+        region:Show()
+      end
+    end
+
+  end
+  return lines[lineIndex]
+end
+
+
+
+--- Creates or retrieves a complete block frame object
+--- todo: make it use data index to avoid re-coloring every block
+function Default:GetBlock (index)
+  local print = bprint
+  print('|cFF0088FF'..self.name..':GetBlock', index)
+  local block = self.InfoBlock[index]
+  local used = self.usedBlocks
+  local free = self.freeBlocks
+
+  if block then
+    print(block.info.id, index)
+  end
+
+  -- if the frame entry is still good, sort heaps
+  if block and block.info.id == index then
+    block.posIndex = tMove(free, used, block)
+    print('  |cFFFFFF00using '..self.name..'|r.|cFF00FFBBusedBlocks['..tostring(block.posIndex)..'] ('.. block:GetName()..', "'..tostring(block.info.title)..'")')
+  else
+    local source = 'cache'
+    if #self.freeBlocks >= 1 then
+      block = tremove(self.freeBlocks)
+      print(' |cFF00FF00 assigning from free heap', block:GetName())
+    else
+
+      local blockIndex = (#self.usedBlocks + #self.freeBlocks) + 1
+      block = CreateFrame('Frame', 'Veneer'..tostring(self)..'Block'..blockIndex, self.frame, 'VeneerTrackerBlock')
+      --block:SetParent()
+      block.schema = ''
+      block.lines = {}
+      block.numLines = 0
+      block.currentLine = 0
+      block.attachmentHeight = 0
+      block.offset = 0
+      B.SetConfigLayers(block)
+      --- methods for event handlers
+      block.Select = self.Select
+      block.Open = self.Open
+      block.Remove = self.Remove
+      block.Link = self.Link
+      block.clickZone:SetScript('OnMouseUp', function(frame, ...) self.OnMouseUp(block, ...) end)
+      block.clickZone:SetScript('OnMouseDown', function(frame, ...) self.OnMouseDown(block, ...) end)
+      block:ClearAllPoints()
+      block.index = blockIndex
+      block.blockFadeOut:SetScript('OnPlay', blockFadeOut_OnPlay)
+      source = 'new'
+    end
+    self.InfoBlock[index] = block
+    block.posIndex = tMove(free, used, block)
+    print('  |cFF00FF00('..source..')|r |cFF0088FF'..self.name..'|r.|cFF00FFBBusedBlocks['..block.posIndex..'] =|r', block:GetName())
+  end
+  block.blockFadeOut:SetScript('OnFinished', blockFadeOut_OnFinished)
+  block:SetScript('OnHide', function(self)
+    fprint(self:GetName(), '|cFF00FF00HIDE|r', debugstack(1,3,1))
+    if(self.DebugTab:IsShown()) then
+      self.DebugTab:Hide()
+    end
+    self.blockFadeOut:SetScript('OnFinished', blockFadeOut_OnFinished)
+    self.isAnimating = nil
+  end)
+  print('  used/free: |cFFFFFF00' .. #self.usedBlocks .. '|r/|cFF00FFFF'..#self.freeBlocks ..'|r')
+  return block
+end
+
+--- begins a blockFadeOut animation and fires FreeBlock when that's done
+function Default:ClearBlock (block)
+  -- double ensure multiple finish scripts from firing
+  if block.isAnimating then
+    return
+  end
+  block.isAnimating = true
+  block.blockFadeOut:SetScript('OnFinished', nil)
+  block.blockFadeOut:SetScript('OnFinished', function(anim)
+    --@debug@
+    fprint(anim:GetName(), '|cFFFFFF00FINISHED|r', debugstack())--@end-debug@
+    anim:SetScript('OnFinished', blockFadeOut_OnFinished)
+    self:FreeBlock(block)
+    block.isAnimating = nil
+  end)
+  block.blockFadeOut:Play()
+end
+
+--- fired by OnFinished scripts; does the actual table swapping
+function Default:FreeBlock(block)
+  bprint('|cFFFF4400FreeBlock|r', block:GetName())
+  local used = self.usedBlocks
+  local free = self.freeBlocks
+  tMove(used, free, block)
+
+  bprint('  |cFFFF4444used/free:|r |cFFFFFF00' .. #used .. '|r/|cFF00FFFF'..#free ..'|r')
+
+  block:Hide()
+  local animations = {block:GetAnimationGroups() }
+  for i, animGroup in ipairs(animations) do
+    bprint('  animGroup', i,  animGroup:GetName())
+    animGroup:Stop()
+  end
+
+  T:Update(self.updateReasonsModule)
+end