diff Veneer.lua @ 90:6e2cb847c3c6

Implement a mixin template for the basic visual config widgets.
author Nenue
date Mon, 24 Oct 2016 18:28:40 -0400
parents 74e714637d6a
children caded2668701
line wrap: on
line diff
--- a/Veneer.lua	Fri Oct 21 18:03:35 2016 -0400
+++ b/Veneer.lua	Mon Oct 24 18:28:40 2016 -0400
@@ -7,8 +7,16 @@
 
 SLASH_VENEER1 = "/veneer"
 SLASH_VENEER2 = "/vn"
+local VENEER_VERSION = 703
 
 SlashCmdList.VENEER = function(cmd)
+
+  if Veneer.ConfigMode then
+    Veneer.ConfigMode = false
+  else
+    Veneer.ConfigMode = true
+  end
+  Veneer:UpdateConfigLayers()
 end
 
 VeneerCore = {
@@ -17,20 +25,8 @@
   FrameClusters = {},
   parserDepth = 0,
   pendingCalls = {},
+  AddOnCheck = {}
 }
-VeneerHandlerMixin = {
-
-  anchorPoint = 'CENTER', -- indicates the initial cluster group point
-  --anchorPath = 'BOTTOM', -- indicates the point from which the frame is anchored in a cluster arrangement
-  OnHide = function()
-    Veneer:DynamicReanchor()
-  end,
-  OnShow = function(self)
-    self:Reanchor()
-    Veneer:StaticReanchor(self)
-  end
-}
-VeneerAnimationMixin = {}
 local print = DEVIAN_WORKSPACE and function(...) print('Veneer', ...) end or nop
 local wipe = table.wipe
 
@@ -42,7 +38,8 @@
   BuffFrame = {
     width = 48,
     height = 48,
-  }
+  },
+  ConfigMode = true
 }
 
 local configMode
@@ -109,14 +106,36 @@
 
 end
 
+local select, IsAddOnLoaded, IsLoggedIn = select, IsAddOnLoaded, IsLoggedIn
+
 function VeneerCore:OnEvent(event, ...)
+  print(event, ...)
   if event == 'ADDON_LOADED' or event == 'PLAYER_LOGIN' then
+    print(IsLoggedIn(), self.initialized)
     if IsLoggedIn() and not self.intialized then
       self:Setup()
-      self:UnregisterEvent('ADDON_LOADED')
-      self:UnregisterEvent('PLAYER_LOGIN')
-      self:Reanchor()
-      self:Update()
+      self.intialized = true
+      print('popping init sequence', self.intialized)
+    end
+
+
+    if self.intialized then
+      local addon  = ...
+      if self.AddOnCheck[addon] then
+        print('  - setting up '..addon..' dependent modules:')
+        local keepChecking = false
+        for index, handler in ipairs(self.AddOnCheck[addon]) do
+          print('  -', handler:GetName(), (not handler.initialized) and (handler.addonFrame and not _G[handler.addonFrame]))
+          if not handler.initialized then
+            print('  '..handler:GetName()..':Setup()')
+            handler:Setup()
+            handler.initialized = true
+          end
+        end
+        if not keepChecking then
+          self.AddOnCheck[addon] = nil
+        end
+      end
     end
   end
 end
@@ -130,18 +149,71 @@
 end
 
 function VeneerCore:Setup ()
-  self.initialized = true
-  if (not VeneerData) or (not VeneerData.version) then
+  local resetConfig = (not VeneerData)
+  if (not VeneerData) then
     VeneerData = defaults
+    VeneerData.version = VENEER_VERSION
+    self:print('Fresh install.')
+  elseif (VeneerData.version and VeneerData.version < VENEER_VERSION) then
+    local oldVars = VeneerData
+    VeneerData = defaults
+    VeneerData.version = VENEER_VERSION
+    VeneerData.oldSettings = oldVars
+    self:print('Resetting to temp defaults. Use /vn retro to use old settings.')
   end
   self.data = VeneerData
-  self:ExecuteOnClusters(nil, 'Setup')
+  self:ExecuteOnClusters(nil, function(frame)
+    if (not frame.addonTrigger) or select(2,IsAddOnLoaded(frame.addonTrigger)) then
+      if not frame.initialized then
+        frame:Setup()
+        frame.initialized = true
+      end
+    end
+  end)
+
+  self.ConfigMode = VeneerData.ConfigMode
+  self:UpdateConfigLayers()
+  self:Reanchor()
+  self:Update()
 end
 
+function VeneerCore:UpdateConfigLayers()
+  if VeneerData then
+
+    VeneerData.ConfigMode = self.ConfigMode
+  end
+
+  self:print('Config mode '..(self.ConfigMode and '|cFF00FF00ON|r' or '|cFFFF0000OFF|r')..'.')
+  self:ExecuteOnClusters(nil, function(frame)
+    if frame.UpdateConfigLayers then
+      frame:UpdateConfigLayers(self.ConfigMode)
+    end
+
+
+    if type(frame.ConfigLayer) == 'table' then
+      for index, region in ipairs(frame.ConfigLayer) do
+        print('setting', frame:GetName() .. '['.. index..']', 'to', self.ConfigMode)
+
+        region:SetShown(self.ConfigMode)
+      end
+    end
+
+    self.ConfigLayers[frame] = frame:IsShown()
+    if self.ConfigMode then
+      print(frame:GetName(), self.ConfigLayers[frame])
+      frame:SetShown(self.ConfigMode)
+    else
+      frame:SetShown(self.ConfigLayers[frame])
+    end
+  end)
+end
 
 function VeneerCore:GetClusterFromArgs (...)
   local primaryAnchor
   local insertPosition
+
+
+
   local clusterTable = self.FrameClusters
   for i = 1, select('#', ...) do
     local arg = select(i, ...)
@@ -169,13 +241,15 @@
 function VeneerCore:AddHandler(handler, ...)
   print('*** Adding handler:', handler.moduleName or handler:GetName())
 
-  local anchorGroup, clusterTable, clusterIndex = self:GetClusterFromArgs(...)
-  if clusterIndex == 1 then
-    for i, frame in ipairs(clusterTable) do
-      frame.clusterIndex = i + 1
+
+    local anchorGroup, clusterTable, clusterIndex = self:GetClusterFromArgs(...)
+    if clusterIndex == 1 then
+      for i, frame in ipairs(clusterTable) do
+        frame.clusterIndex = i + 1
+      end
     end
-  end
-  tinsert(clusterTable, clusterIndex, handler)
+    tinsert(clusterTable, clusterIndex, handler)
+
   print('cluster', anchorGroup, 'table', clusterTable, 'position', clusterIndex)
 
   handler.anchorCluster = clusterTable
@@ -186,13 +260,28 @@
       handler[k] = v
     end
   end
+
+  if handler.addonTrigger and not IsAddOnLoaded(handler.addonTrigger) then
+    print('|cFFFF4400  -- dependency:', handler.addonTrigger)
+    self.AddOnCheck[handler.addonTrigger] = self.AddOnCheck[handler.addonTrigger] or {}
+    tinsert(self.AddOnCheck[handler.addonTrigger], handler)
+  end
+
   if self.initialized then
-    print('  -- doing initialization')
-    if handler.Setup and not handler.initialized then
-      handler:Setup()
-      handler.initialized = true
+    print('  -- initialization check')
+    if handler.Setup then
+      local doInit = (not handler.initialized)
+      if handler.addonTrigger and not IsAddOnLoaded(handler.addonTrigger) then
+        doInit = false
+      end
+      -- room to add other checks
+
+      if doInit then
+        handler:Setup()
+        handler.initialized = true
+        self:InternalReanchor(handler)
+      end
     end
-    self:InternalReanchor(handler)
   end
 end
 
@@ -202,7 +291,11 @@
 end
 
 function VeneerCore:Update()
-  self:ExecuteOnClusters(nil, 'Update')
+  self:ExecuteOnClusters(nil, function(frame)
+    if frame.initialized and frame.Update then
+      frame:Update()
+    end
+  end)
   self:Reanchor()
 end
 
@@ -219,16 +312,24 @@
   for anchorPoint, cluster in pairs(parent.FrameClusters) do
     local lastFrame
     for index, frame in ipairs(cluster) do
-      print('  |cFF00FF00'..index, frame:GetName(), frame:IsVisible())
+      print('  |cFF00FF00'..index, frame:GetName(), frame:IsVisible(), (lastFrame and ('|cFFFFFF00'..lastFrame:GetName()..'|r') or '|cFF00FFFFUIParent'))
       if frame:IsVisible() then
-        anchorPoint = frame.anchorPoint
-        frame:ClearAllPoints()
-        if lastFrame then
-          frame:SetPoint(anchorPoint, lastFrame, ANCHOR_OFFSET_POINT[anchorPoint], 0, 0)
+
+        if frame.anchorFrame then
+          frame:SetPoint(frame.anchorPoint, frame.anchorFrame, frame.anchorFrom, frame.anchorX, frame.anchorY)
+          print(frame:GetTop(), frame:GetRight())
         else
-          frame:SetPoint(anchorPoint, UIParent, anchorPoint, 0, 0)
+          anchorPoint = frame.anchorPoint
+          frame:ClearAllPoints()
+          if lastFrame then
+            frame:SetPoint(anchorPoint, lastFrame, ANCHOR_OFFSET_POINT[anchorPoint], 0, 0)
+          else
+            frame:SetPoint(anchorPoint, UIParent, anchorPoint, frame.anchorX, frame.anchorY)
+          end
+          print(frame:GetTop(), frame:GetRight())
+          lastFrame = frame
         end
-        lastFrame = frame
+
       end
 
     end
@@ -238,6 +339,12 @@
 -- Evaluates the current visibility state and re-anchors adjacent blocks accordingly
 function VeneerCore:InternalReanchor(handler, printFunc)
   print('|cFF00FFFFVeneer:InternalReanchor('..handler:GetName()..')')
+  if handler.anchorFrame then
+    handler:SetPoint(handler.anchorPoint, handler.anchorFrame, handler.anchorFrom, handler.anchorX, handler.anchorY)
+    return
+  end
+
+
   local anchorPoint = handler.anchorPath or handler.anchorPoint
   local anchorParent, anchorTo = UIParent, anchorPoint
   local subPoint, subTo
@@ -304,7 +411,7 @@
       print('delaying walk for', method)
       return
     end
-    print('|cFF00FF00Veneer:ExecuteOnClusters|r('..tostring(layer)..', '..method..')')
+    print('|cFF00FF00Veneer:ExecuteOnClusters|r('..tostring(layer)..', '..tostring(method)..')')
   else
     print(' Level '..self.parserDepth)
   end
@@ -313,7 +420,9 @@
   for anchor, cluster in pairs(layer) do
     for index, frame in ipairs(cluster) do
       print(' '..anchor..'.'..index..' = '..frame:GetName())
-      if frame[method] then
+      if type(method) == 'function' then
+        method(frame, true)
+      elseif frame[method] then
         print('  |cFF00FF00'..frame:GetName())
         frame[method](frame, true)
       end
@@ -331,102 +440,7 @@
   end
 end
 
-local VeneerButton_OnDragStart = function(self)
-  self.startingLeft = self:GetLeft()
-  self.startingBottom = self:GetBottom()
-  self.anchors = self.anchors or {}
-  table.wipe(self.anchors)
 
-  local frame = self:GetParent()
-  local n = frame:GetNumPoints()
-  for i = 1, n do
-    local anchor, parent, relative, x, y = frame:GetPoint(i)
-    self.anchors[i] = {
-      anchor = anchor,
-      parent = parent,
-      relative = relative,
-      x = x,
-      y = y
-    }
-  end
-
-  print(self:GetName(), 'start moving', self.startingLeft, self.startingBottom)
-  self:StartMoving()
-end
-
-local VeneerButton_OnDragStop =  function(self)
-  self:StopMovingOrSizing()
-  if self.OnDragStop then
-    self.OnDragStop(self)
-  else
-    local frame = self:GetParent()
-    local dx = self:GetLeft() - self.startingLeft
-    local dy = self:GetBottom() - self.startingBottom
-
-    frame:ClearAllPoints()
-    for i, point in ipairs(self.anchors) do
-      frame:SetPoint(point.anchor, point.parent, point.relative, point.x + dx, point.y + dy)
-      print('adjusting anchor', point.anchor, point.parent, point.relative, point.x + dx, point.y + dy)
-    end
-  end
-end
-
-local Veneer_FixMovers = function()
-  for frame, veneer in pairs(veneers) do
-    if veneer:IsMoving() then
-      VeneerButton_OnDragStop(veneer)
-    end
-  end
-end
-
-local VeneerButton_Update = function(self)
-  if configMode then
-    self:SetScript('OnDragStart', VeneerButton_OnDragStart)
-    self:SetScript('OnDragStop', VeneerButton_OnDragStop)
-    self:SetMovable(true)
-    self:EnableMouse(true)
-    self:RegisterForDrag('LeftButton')
-
-    self.bg:SetColorTexture(0,1,0,0.5)
-    for i, region in ipairs(self.configLayers) do
-      region:Show()
-    end
-    self:Show()
-  else
-
-    self:SetScript('OnDragStart', self.StartMoving)
-    self:SetScript('OnDragStop', self.StopMovingOrSizing)
-    self:SetMovable(false)
-    self:EnableMouse(false)
-
-    self.bg:SetColorTexture(0,1,0,0)
-    for i, region in ipairs(self.configLayers) do
-      region:Hide()
-    end
-    if self.isHidden then
-      self:Hide()
-    end
-
-  end
-end
-
-local ToggleVeneerConfig = function()
-  if configMode then
-    configMode = false
-    Veneer:print('Config mode off.')
-  else
-    configMode = true
-    Veneer:print('Config mode on.')
-  end
-
-  for frame, veneer in pairs(veneers) do
-    VeneerButton_Update(veneer)
-  end
-end
-
-local VeneerButton_OnShow = function(self)
-  VeneerButton_Update(self)
-end
 
 -- Takes frame handle and assigns a block to it
 function VeneerCore:Acquire (frame, template)
@@ -437,8 +451,8 @@
   local veneer = self.Frames[frame]
   if not veneer then
     local name = type(frame) == 'table' and GetTableName(frame) or GetAnonymousName()
-    veneer = CreateFrame('Frame', name, frame, template or 'VeneerTemplate')
-    print('+veneer', name)
+    veneer = CreateFrame('Frame', name, frame, template or 'VeneerStatusButtonTemplate')
+    print(self:GetName()..':Acquire()', frame:GetName(), template)
 
     veneer:SetAllPoints(frame)
     veneer:SetParent(frame)
@@ -446,37 +460,10 @@
     veneer.bg:SetColorTexture(0,0,0,0)
     veneer:Hide()
     veneer:EnableMouse(false)
-
-    veneer:SetScript('OnShow', VeneerButton_OnShow)
-
     -- find current X/Y
     veneer.currentLeft = frame:GetLeft()
     veneer.currentTop = frame:GetTop()
     self.Frames[frame] = veneer
   end
-
   return veneer
-end
-
-function VeneerHandlerMixin:Reanchor (anchorAll)
-  if not anchorAll then
-    Veneer:InternalReanchor(self)
-  end
-
-end
-
-function VeneerAnimationMixin:OnPlay()
-  PlaySoundKitID(229)
-  self.animating = true
-  print('|cFF00FF00Anim:OnPlay|r @', unpack(self.sourcePoint))
-end
-function VeneerAnimationMixin:OnStop()
-  PlaySoundKitID(229)
-  self.animating = nil
-  print('|cFF00FF00Anim:OnFinish|r @', unpack(self.destPoint))
-end
-function VeneerAnimationMixin:OnFinished()
-  PlaySoundKitID(229)
-  self.animating = nil
-  print('|cFF00FF00Anim:OnFinish|r @', unpack(self.destPoint))
 end
\ No newline at end of file