diff SkeletonUnit/UnitFrame.lua @ 4:a30285f8191e

Units: make sure unit frames are SecureUnitButton derivatives remove of unnecessary target/focus events Stats: resolve GUID after event handlers have fired keep frame manipulation in uf.ui, get needed values from wrapper functions
author Nenue
date Tue, 21 Jun 2016 11:56:14 -0400
parents 07293831dd7b
children
line wrap: on
line diff
--- a/SkeletonUnit/UnitFrame.lua	Tue Jun 21 08:14:22 2016 -0400
+++ b/SkeletonUnit/UnitFrame.lua	Tue Jun 21 11:56:14 2016 -0400
@@ -19,10 +19,11 @@
 local params = setmetatable({}, {
   __index = function(t, k)
     print('get', k)
-    return rawget(t.player, k)
+    return t.player
   end,
   __newindex = function(t, k, v)
     if type(v) == 'table' then
+      print('branch', k)
       v = setmetatable(v, {
         __index  = function(tt, kk)
           print('get', k, kk)
@@ -30,6 +31,7 @@
         end,
         __newindex = function(tt, kk, vv)
           if type(vv) == 'table' then
+            print('_branch', k, kk)
             vv = setmetatable(vv, {
               __index = function(_tt, _kk)
                 print('_get', k, kk, _kk)
@@ -50,12 +52,23 @@
   height = 30,
     health = { height = 24 },
     power = { height = 6},
-  }
+  position = {'CENTER', nil, 'CENTER',0,-120 }
+}
 params.pet = {
   width = 180,
-  height = 25
+  height = 25,
+
+  position = {'TOPLEFT', 'player', 'BOTTOMLEFT',0,-4}
 }
-params.focus = params.pet
+params.target = {
+
+  position = {'TOPLEFT', 'player', 'TOPRIGHT',4,0}
+}
+params.focus = {
+  width = 180,
+  height = 25,
+  position = {'BOTTOMRIGHT', 'target', 'TOPRIGHT',0,4}
+}
 
 uf.handler = uf -- so uf.event can work
 
@@ -77,7 +90,6 @@
     buttons[unit][index] = CreateFrame('Frame', 'KT'..unit..'Buff'..index, self, 'KTAuraButton')
     buttons[unit][index]:SetSize(BUFF_SIZE, BUFF_SIZE)
     buttons[unit][index].cooldown:SetHideCountdownNumbers(true)
-
   end
   return buttons[unit][index]
 end
@@ -88,34 +100,36 @@
   end
   print('|cFFFFFF00unit|r:', unit)
 
+  local c = params[unit]
+
   if not _G['KT'..unit..'Frame'] then
-    CreateFrame('Frame', 'KT'.. unit .. 'Frame', uf)
+    local new = CreateFrame('Button', 'KT'.. unit .. 'Frame', uf, 'KTUnitFrameTemplate')
+    new.unit = unit
+    new.anchorPoint = {new:GetPoint(1)}
+    new.handler = prototypes[unit]
+    new.params = c
+
+    --if not db.position[unit] then
+      db.position[unit] = c.position
+    --end
+    new.position = db.position[unit]
+
+
+    buttons[unit] = {}
+    new.buttons = buttons[unit]
+    new.refresh = prototypes[unit].refresh
+    new.init = prototypes[unit].init
+
+    new:EnableMouse(true)
+    new:SetScript('OnMouseUp', prototypes[unit].OnMouseUp)
+
+    new:SetAttribute("type", "target")
+    new:SetAttribute("unit", unit)
+    RegisterUnitWatch(new)
+    units[unit] = new
   end
 
-  local frame = _G['KT'..unit..'Frame']
-  frame.unit = unit
-  frame.anchorPoint = {frame:GetPoint(1)}
-  frame.handler = prototypes[unit]
-
-  buttons[unit] = {}
-  units[unit] = frame
-
-  local width = params.player.width
-  local height = params.player.height
-
-  if params[unit] then
-    if params[unit].width then
-      width = params[unit].width
-    end
-    if params[unit].height then
-      height = params[unit].height
-    end
-  end
-
-  frame:SetWidth(width)
-  frame:SetHeight(height)
-
-  return frame
+  return _G['KT'..unit..'Frame']
 end
 
 uf.ui = function ()
@@ -126,6 +140,7 @@
 
 uf.init = function()
   uf:RegisterEvent('PLAYER_TARGET_CHANGED')
+  uf:RegisterEvent('PLAYER_FOCUS_CHANGED')
   uf:RegisterUnitEvent("UNIT_AURA")
 end
 
@@ -156,7 +171,12 @@
 end
 
 uf.PLAYER_TARGET_CHANGED = function()
-  prototypes.player.refresh(units.target)
+  print('caught target change')
+  units.target:refresh()
+end
+
+uf.PLAYER_FOCUS_CHANGED = function()
+  units.focus:refresh()
 end
 
 prototypes.player = {}
@@ -168,16 +188,30 @@
   self:RegisterEvent("NAME_PLATE_UNIT_REMOVED")
   self:RegisterEvent("UNIT_TARGET")
   self:SetScript('OnEvent', uf.event)
+
+  local anchor, parent, relative, x, y = unpack(self.position)
+  if parent and units[parent] then
+    parent = units[parent]
+  else
+    parent = UIParent
+  end
+
+  self:SetPoint(anchor, parent, relative, x, y)
+  self:SetSize(self.params.width, self.params.height)
+
 end
 
 
 --- Runs when event handler decides so
 player.refresh = function(self)
+  print(self.unit, UnitExists(self.unit))
   if not UnitExists(self.unit) then
+    print('hiding unit')
     self:Hide()
     return
   end
 
+  self:Show()
   if UnitIsPlayer(self.unit) then
     local class, classFile = UnitClass(self.unit)
     if classFile then
@@ -198,12 +232,12 @@
   else
     self.healthbar:SetHeight(params[self.unit].health.height + params[self.unit].power.height)
   end
-
-
+  uf.UNIT_AURA(self, 'UNIT_AURA', self.unit)
 
   self.handler.UNIT_HEALTH_FREQUENT(self)
 end
 
+
 uf.UNIT_AURA = function(self, event, unit)
   if not units[unit] then return true end
 
@@ -240,7 +274,7 @@
 
 player.UNIT_HEALTH_FREQUENT = function(self, ...)
   if UnitHealthMax(self.unit) > 0 then
-    self.healthbar:SetWidth(PLAYER_WIDTH * UnitHealth(self.unit) / UnitHealthMax(self.unit))
+    self.healthbar:SetWidth(self.params.width * UnitHealth(self.unit) / UnitHealthMax(self.unit))
     self.healthtext:SetText(UnitHealth(self.unit))
   else
     self.healthbar:SetWidth(PLAYER_WIDTH)
@@ -251,7 +285,7 @@
 
 player.UNIT_POWER_FREQUENT = function(self)
   if UnitPowerMax(self.unit) > 0 then
-    self.powerbar:SetWidth(PLAYER_WIDTH * UnitPower(self.unit) / UnitPowerMax(self.unit))
+    self.powerbar:SetWidth(self.params.width * UnitPower(self.unit) / UnitPowerMax(self.unit))
     self.powertext:SetText(UnitPower(self.unit))
   else
     self.powerbar:Hide()
@@ -271,4 +305,5 @@
 end
 
 prototypes.pet = player
-prototypes.target = player
\ No newline at end of file
+prototypes.target = player
+prototypes.focus = player
\ No newline at end of file