diff SkeletonUnit/UnitFrame.lua @ 3:07293831dd7b

- implement unit parameters table inheritance from a stock table - UnitFrames.unit() and UnitFrames.ui() methods
author Nenue
date Tue, 21 Jun 2016 08:14:22 -0400
parents cd7d06bcd98d
children a30285f8191e
line wrap: on
line diff
--- a/SkeletonUnit/UnitFrame.lua	Tue Jun 21 08:10:36 2016 -0400
+++ b/SkeletonUnit/UnitFrame.lua	Tue Jun 21 08:14:22 2016 -0400
@@ -7,24 +7,57 @@
 --------------------------------------------
 
 local KT = LibKT.register(SkeletonUnits)
+local db
 local PLAYER_NAMEPLATE
 local PLAYER_WIDTH = 220
 local BUFF_SIZE = 24
-local sk = SkeletonUnits
-local player = KTPlayerFrame
-local prototypes = {
-  player = {},
-  target = {},
-  pet = {},
-  focus = {}
-}
+local uf = SkeletonUnits
+local prototypes = {}
 local units = {}
 local buttons = {}
-local params = {
-  player = {width = 220, height = 30},
-  pet = {height = 25}
+
+local params = setmetatable({}, {
+  __index = function(t, k)
+    print('get', k)
+    return rawget(t.player, k)
+  end,
+  __newindex = function(t, k, v)
+    if type(v) == 'table' then
+      v = setmetatable(v, {
+        __index  = function(tt, kk)
+          print('get', k, kk)
+          return rawget(t.player, kk)
+        end,
+        __newindex = function(tt, kk, vv)
+          if type(vv) == 'table' then
+            vv = setmetatable(vv, {
+              __index = function(_tt, _kk)
+                print('_get', k, kk, _kk)
+                return rawget(t.player[kk], _kk)
+              end
+            })
+          end
+          rawset(tt, kk ,vv)
+        end
+      })
+    end
+      rawset(t,k,v)
+  end
+})
+
+params.player = {
+  width = 220,
+  height = 30,
+    health = { height = 24 },
+    power = { height = 6},
+  }
+params.pet = {
+  width = 180,
+  height = 25
 }
-sk.handler = sk -- so sk.event can work
+params.focus = params.pet
+
+uf.handler = uf -- so uf.event can work
 
 
 local UpdateUnitAnchor = function(self)
@@ -49,88 +82,130 @@
   return buttons[unit][index]
 end
 
-sk.init = function()
-  for unit, handler in pairs(prototypes) do
-    if not _G['KT'..unit..'Frame'] then
-      CreateFrame('Frame', 'KT'.. unit .. 'Frame', sk)
-    end
-    local frame = _G['KT'..unit..'Frame']
-    frame.unit = unit
-    frame.anchorPoint = {frame:GetPoint(1)}
-    frame.handler = handler
-    if handler.init then
-      handler.init(frame, unit)
-    end
-    buttons[unit] = {}
-    units[unit] = frame
+uf.unit = function(unit)
+  if not prototypes[unit] then
+    return
+  end
+  print('|cFFFFFF00unit|r:', unit)
+
+  if not _G['KT'..unit..'Frame'] then
+    CreateFrame('Frame', 'KT'.. unit .. 'Frame', uf)
   end
 
-  sk:RegisterEvent('PLAYER_TARGET_CHANGED')
-  sk:RegisterUnitEvent("UNIT_AURA")
+  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
 end
-sk.variables = function()
+
+uf.ui = function ()
   for unit, frame in pairs(units) do
-    if frame.handler.variables then
-      frame.handler.variables(frame, unit)
-    end
-    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)
+    frame.handler.refresh(frame)
   end
 end
 
-sk.event = function(self, event, ...)
+uf.init = function()
+  uf:RegisterEvent('PLAYER_TARGET_CHANGED')
+  uf:RegisterUnitEvent("UNIT_AURA")
+end
+
+uf.variables = function()
+  if not FossilDB then
+    FossilDB = {
+      units = {'player', 'target', 'focus', 'pet', 'targettarget' },
+      position = {}
+    }
+  end
+  db = FossilDB
+
+  for i, unit in pairs(db.units) do
+    print(unit)
+    local frame = uf.unit(unit)
+    if frame then
+      frame.handler.init(frame)
+    end
+  end
+  uf.ui()
+end
+
+uf.event = function(self, event, ...)
   if self.handler[event] then
     self.handler[event](self, event, ...)
   end
+  return true
 end
 
-sk.PLAYER_TARGET_CHANGED = function()
+uf.PLAYER_TARGET_CHANGED = function()
   prototypes.player.refresh(units.target)
 end
 
-prototypes.player.init = function(self)
+prototypes.player = {}
+local player = prototypes.player
+player.init = function(self)
   self:RegisterUnitEvent("UNIT_HEALTH_FREQUENT", self.unit)
   self:RegisterUnitEvent("UNIT_POWER_FREQUENT", self.unit)
   self:RegisterEvent("NAME_PLATE_UNIT_ADDED")
   self:RegisterEvent("NAME_PLATE_UNIT_REMOVED")
   self:RegisterEvent("UNIT_TARGET")
-  self:SetScript('OnEvent', sk.event)
+  self:SetScript('OnEvent', uf.event)
 end
 
---- Runs once
-prototypes.player.variables = function(self)
-  self.handler.refresh(self)
-end
 
 --- Runs when event handler decides so
-prototypes.player.refresh = function(self)
+player.refresh = function(self)
   if not UnitExists(self.unit) then
     self:Hide()
     return
   end
 
-  local class, classFile = UnitClass(self.unit)
-  if classFile then
-    self.healthbar:SetColorTexture(RAID_CLASS_COLORS[classFile].r, RAID_CLASS_COLORS[classFile].g, RAID_CLASS_COLORS[classFile].b)
+  if UnitIsPlayer(self.unit) then
+    local class, classFile = UnitClass(self.unit)
+    if classFile then
+      self.healthbar:SetColorTexture(RAID_CLASS_COLORS[classFile].r, RAID_CLASS_COLORS[classFile].g, RAID_CLASS_COLORS[classFile].b)
+    end
+  elseif UnitIsFriend('player', self.unit) then
+    self.healthbar:SetColorTexture(0,.75,0,1)
+  elseif UnitIsEnemy('player', self.unit) then
+    self.healthbar:SetColorTexture(1,0,0,1)
+  else
+    self.healthbar:SetColorTexture(1,1,0,1)
+  end
+  self.powertype = UnitPowerType(self.unit)
+  if self.powertype then
+    self.powerbar:SetColorTexture(0,.3,1, 1)
+
+    self.handler.UNIT_POWER_FREQUENT(self)
+  else
+    self.healthbar:SetHeight(params[self.unit].health.height + params[self.unit].power.height)
   end
 
+
+
   self.handler.UNIT_HEALTH_FREQUENT(self)
-  self.handler.UNIT_POWER_FREQUENT(self)
 end
 
-sk.UNIT_AURA = function(self, event, unit)
-  if not units[unit] then return end
+uf.UNIT_AURA = function(self, event, unit)
+  if not units[unit] then return true end
 
   local buffOffset = 0
   local buttons = buttons[unit]
@@ -153,16 +228,17 @@
       end
     end
   end
+  return true
 end
 
-prototypes.player.NAME_PLATE_UNIT_ADDED = function(self, event, nameplate)
-    UpdateUnitAnchor(self)
+player.NAME_PLATE_UNIT_ADDED = function(self, event, nameplate)
+    --UpdateUnitAnchor(self)
 end
-prototypes.player.NAME_PLATE_UNIT_REMOVED = function(self, event, nameplate)
-    UpdateUnitAnchor(self)
+player.NAME_PLATE_UNIT_REMOVED = function(self, event, nameplate)
+    --UpdateUnitAnchor(self)
 end
 
-prototypes.player.UNIT_HEALTH_FREQUENT = function(self, ...)
+player.UNIT_HEALTH_FREQUENT = function(self, ...)
   if UnitHealthMax(self.unit) > 0 then
     self.healthbar:SetWidth(PLAYER_WIDTH * UnitHealth(self.unit) / UnitHealthMax(self.unit))
     self.healthtext:SetText(UnitHealth(self.unit))
@@ -173,7 +249,7 @@
   return true
 end
 
-prototypes.player.UNIT_POWER_FREQUENT = function(self)
+player.UNIT_POWER_FREQUENT = function(self)
   if UnitPowerMax(self.unit) > 0 then
     self.powerbar:SetWidth(PLAYER_WIDTH * UnitPower(self.unit) / UnitPowerMax(self.unit))
     self.powertext:SetText(UnitPower(self.unit))
@@ -185,15 +261,14 @@
 end
 
 
-prototypes.player.UNIT_TARGET = function(self, ...)
+player.UNIT_TARGET = function(self, ...)
   if not UnitExists(self.unit) then
     self:Hide()
   else
     self:Show()
     self.handler.refresh(self)
-    UpdateUnitAnchor(self)
   end
 end
 
-prototypes.pet = prototypes.player
-prototypes.target = prototypes.player
\ No newline at end of file
+prototypes.pet = player
+prototypes.target = player
\ No newline at end of file