comparison 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
comparison
equal deleted inserted replaced
2:f7a0898d293c 3:07293831dd7b
5 -- @file - r e v i s i o n @ @file-hash@ 5 -- @file - r e v i s i o n @ @file-hash@
6 -- Created: 6/16/2016 3:46 AM 6 -- Created: 6/16/2016 3:46 AM
7 -------------------------------------------- 7 --------------------------------------------
8 8
9 local KT = LibKT.register(SkeletonUnits) 9 local KT = LibKT.register(SkeletonUnits)
10 local db
10 local PLAYER_NAMEPLATE 11 local PLAYER_NAMEPLATE
11 local PLAYER_WIDTH = 220 12 local PLAYER_WIDTH = 220
12 local BUFF_SIZE = 24 13 local BUFF_SIZE = 24
13 local sk = SkeletonUnits 14 local uf = SkeletonUnits
14 local player = KTPlayerFrame 15 local prototypes = {}
15 local prototypes = {
16 player = {},
17 target = {},
18 pet = {},
19 focus = {}
20 }
21 local units = {} 16 local units = {}
22 local buttons = {} 17 local buttons = {}
23 local params = { 18
24 player = {width = 220, height = 30}, 19 local params = setmetatable({}, {
25 pet = {height = 25} 20 __index = function(t, k)
21 print('get', k)
22 return rawget(t.player, k)
23 end,
24 __newindex = function(t, k, v)
25 if type(v) == 'table' then
26 v = setmetatable(v, {
27 __index = function(tt, kk)
28 print('get', k, kk)
29 return rawget(t.player, kk)
30 end,
31 __newindex = function(tt, kk, vv)
32 if type(vv) == 'table' then
33 vv = setmetatable(vv, {
34 __index = function(_tt, _kk)
35 print('_get', k, kk, _kk)
36 return rawget(t.player[kk], _kk)
37 end
38 })
39 end
40 rawset(tt, kk ,vv)
41 end
42 })
43 end
44 rawset(t,k,v)
45 end
46 })
47
48 params.player = {
49 width = 220,
50 height = 30,
51 health = { height = 24 },
52 power = { height = 6},
53 }
54 params.pet = {
55 width = 180,
56 height = 25
26 } 57 }
27 sk.handler = sk -- so sk.event can work 58 params.focus = params.pet
59
60 uf.handler = uf -- so uf.event can work
28 61
29 62
30 local UpdateUnitAnchor = function(self) 63 local UpdateUnitAnchor = function(self)
31 self.nameplate = C_NamePlate.GetNamePlateForUnit(self.unit) 64 self.nameplate = C_NamePlate.GetNamePlateForUnit(self.unit)
32 if self.nameplate and self.nameplate.namePlateUnitToken then 65 if self.nameplate and self.nameplate.namePlateUnitToken then
47 80
48 end 81 end
49 return buttons[unit][index] 82 return buttons[unit][index]
50 end 83 end
51 84
52 sk.init = function() 85 uf.unit = function(unit)
53 for unit, handler in pairs(prototypes) do 86 if not prototypes[unit] then
54 if not _G['KT'..unit..'Frame'] then 87 return
55 CreateFrame('Frame', 'KT'.. unit .. 'Frame', sk) 88 end
56 end 89 print('|cFFFFFF00unit|r:', unit)
57 local frame = _G['KT'..unit..'Frame'] 90
58 frame.unit = unit 91 if not _G['KT'..unit..'Frame'] then
59 frame.anchorPoint = {frame:GetPoint(1)} 92 CreateFrame('Frame', 'KT'.. unit .. 'Frame', uf)
60 frame.handler = handler 93 end
61 if handler.init then 94
62 handler.init(frame, unit) 95 local frame = _G['KT'..unit..'Frame']
63 end 96 frame.unit = unit
64 buttons[unit] = {} 97 frame.anchorPoint = {frame:GetPoint(1)}
65 units[unit] = frame 98 frame.handler = prototypes[unit]
66 end 99
67 100 buttons[unit] = {}
68 sk:RegisterEvent('PLAYER_TARGET_CHANGED') 101 units[unit] = frame
69 sk:RegisterUnitEvent("UNIT_AURA") 102
70 end 103 local width = params.player.width
71 sk.variables = function() 104 local height = params.player.height
105
106 if params[unit] then
107 if params[unit].width then
108 width = params[unit].width
109 end
110 if params[unit].height then
111 height = params[unit].height
112 end
113 end
114
115 frame:SetWidth(width)
116 frame:SetHeight(height)
117
118 return frame
119 end
120
121 uf.ui = function ()
72 for unit, frame in pairs(units) do 122 for unit, frame in pairs(units) do
73 if frame.handler.variables then 123 frame.handler.refresh(frame)
74 frame.handler.variables(frame, unit) 124 end
75 end 125 end
76 local width = params.player.width 126
77 local height = params.player.height 127 uf.init = function()
78 if params[unit] then 128 uf:RegisterEvent('PLAYER_TARGET_CHANGED')
79 if params[unit].width then 129 uf:RegisterUnitEvent("UNIT_AURA")
80 width = params[unit].width 130 end
81 end 131
82 if params[unit].height then 132 uf.variables = function()
83 height = params[unit].height 133 if not FossilDB then
84 end 134 FossilDB = {
85 end 135 units = {'player', 'target', 'focus', 'pet', 'targettarget' },
86 136 position = {}
87 frame:SetWidth(width) 137 }
88 frame:SetHeight(height) 138 end
89 end 139 db = FossilDB
90 end 140
91 141 for i, unit in pairs(db.units) do
92 sk.event = function(self, event, ...) 142 print(unit)
143 local frame = uf.unit(unit)
144 if frame then
145 frame.handler.init(frame)
146 end
147 end
148 uf.ui()
149 end
150
151 uf.event = function(self, event, ...)
93 if self.handler[event] then 152 if self.handler[event] then
94 self.handler[event](self, event, ...) 153 self.handler[event](self, event, ...)
95 end 154 end
96 end 155 return true
97 156 end
98 sk.PLAYER_TARGET_CHANGED = function() 157
158 uf.PLAYER_TARGET_CHANGED = function()
99 prototypes.player.refresh(units.target) 159 prototypes.player.refresh(units.target)
100 end 160 end
101 161
102 prototypes.player.init = function(self) 162 prototypes.player = {}
163 local player = prototypes.player
164 player.init = function(self)
103 self:RegisterUnitEvent("UNIT_HEALTH_FREQUENT", self.unit) 165 self:RegisterUnitEvent("UNIT_HEALTH_FREQUENT", self.unit)
104 self:RegisterUnitEvent("UNIT_POWER_FREQUENT", self.unit) 166 self:RegisterUnitEvent("UNIT_POWER_FREQUENT", self.unit)
105 self:RegisterEvent("NAME_PLATE_UNIT_ADDED") 167 self:RegisterEvent("NAME_PLATE_UNIT_ADDED")
106 self:RegisterEvent("NAME_PLATE_UNIT_REMOVED") 168 self:RegisterEvent("NAME_PLATE_UNIT_REMOVED")
107 self:RegisterEvent("UNIT_TARGET") 169 self:RegisterEvent("UNIT_TARGET")
108 self:SetScript('OnEvent', sk.event) 170 self:SetScript('OnEvent', uf.event)
109 end 171 end
110 172
111 --- Runs once
112 prototypes.player.variables = function(self)
113 self.handler.refresh(self)
114 end
115 173
116 --- Runs when event handler decides so 174 --- Runs when event handler decides so
117 prototypes.player.refresh = function(self) 175 player.refresh = function(self)
118 if not UnitExists(self.unit) then 176 if not UnitExists(self.unit) then
119 self:Hide() 177 self:Hide()
120 return 178 return
121 end 179 end
122 180
123 local class, classFile = UnitClass(self.unit) 181 if UnitIsPlayer(self.unit) then
124 if classFile then 182 local class, classFile = UnitClass(self.unit)
125 self.healthbar:SetColorTexture(RAID_CLASS_COLORS[classFile].r, RAID_CLASS_COLORS[classFile].g, RAID_CLASS_COLORS[classFile].b) 183 if classFile then
126 end 184 self.healthbar:SetColorTexture(RAID_CLASS_COLORS[classFile].r, RAID_CLASS_COLORS[classFile].g, RAID_CLASS_COLORS[classFile].b)
185 end
186 elseif UnitIsFriend('player', self.unit) then
187 self.healthbar:SetColorTexture(0,.75,0,1)
188 elseif UnitIsEnemy('player', self.unit) then
189 self.healthbar:SetColorTexture(1,0,0,1)
190 else
191 self.healthbar:SetColorTexture(1,1,0,1)
192 end
193 self.powertype = UnitPowerType(self.unit)
194 if self.powertype then
195 self.powerbar:SetColorTexture(0,.3,1, 1)
196
197 self.handler.UNIT_POWER_FREQUENT(self)
198 else
199 self.healthbar:SetHeight(params[self.unit].health.height + params[self.unit].power.height)
200 end
201
202
127 203
128 self.handler.UNIT_HEALTH_FREQUENT(self) 204 self.handler.UNIT_HEALTH_FREQUENT(self)
129 self.handler.UNIT_POWER_FREQUENT(self) 205 end
130 end 206
131 207 uf.UNIT_AURA = function(self, event, unit)
132 sk.UNIT_AURA = function(self, event, unit) 208 if not units[unit] then return true end
133 if not units[unit] then return end
134 209
135 local buffOffset = 0 210 local buffOffset = 0
136 local buttons = buttons[unit] 211 local buttons = buttons[unit]
137 for i = 1, 16 do 212 for i = 1, 16 do
138 213
151 if buttons[i] then 226 if buttons[i] then
152 buttons[i]:Hide() 227 buttons[i]:Hide()
153 end 228 end
154 end 229 end
155 end 230 end
156 end 231 return true
157 232 end
158 prototypes.player.NAME_PLATE_UNIT_ADDED = function(self, event, nameplate) 233
159 UpdateUnitAnchor(self) 234 player.NAME_PLATE_UNIT_ADDED = function(self, event, nameplate)
160 end 235 --UpdateUnitAnchor(self)
161 prototypes.player.NAME_PLATE_UNIT_REMOVED = function(self, event, nameplate) 236 end
162 UpdateUnitAnchor(self) 237 player.NAME_PLATE_UNIT_REMOVED = function(self, event, nameplate)
163 end 238 --UpdateUnitAnchor(self)
164 239 end
165 prototypes.player.UNIT_HEALTH_FREQUENT = function(self, ...) 240
241 player.UNIT_HEALTH_FREQUENT = function(self, ...)
166 if UnitHealthMax(self.unit) > 0 then 242 if UnitHealthMax(self.unit) > 0 then
167 self.healthbar:SetWidth(PLAYER_WIDTH * UnitHealth(self.unit) / UnitHealthMax(self.unit)) 243 self.healthbar:SetWidth(PLAYER_WIDTH * UnitHealth(self.unit) / UnitHealthMax(self.unit))
168 self.healthtext:SetText(UnitHealth(self.unit)) 244 self.healthtext:SetText(UnitHealth(self.unit))
169 else 245 else
170 self.healthbar:SetWidth(PLAYER_WIDTH) 246 self.healthbar:SetWidth(PLAYER_WIDTH)
171 self.healthtext:SetText(nil) 247 self.healthtext:SetText(nil)
172 end 248 end
173 return true 249 return true
174 end 250 end
175 251
176 prototypes.player.UNIT_POWER_FREQUENT = function(self) 252 player.UNIT_POWER_FREQUENT = function(self)
177 if UnitPowerMax(self.unit) > 0 then 253 if UnitPowerMax(self.unit) > 0 then
178 self.powerbar:SetWidth(PLAYER_WIDTH * UnitPower(self.unit) / UnitPowerMax(self.unit)) 254 self.powerbar:SetWidth(PLAYER_WIDTH * UnitPower(self.unit) / UnitPowerMax(self.unit))
179 self.powertext:SetText(UnitPower(self.unit)) 255 self.powertext:SetText(UnitPower(self.unit))
180 else 256 else
181 self.powerbar:Hide() 257 self.powerbar:Hide()
183 end 259 end
184 return true 260 return true
185 end 261 end
186 262
187 263
188 prototypes.player.UNIT_TARGET = function(self, ...) 264 player.UNIT_TARGET = function(self, ...)
189 if not UnitExists(self.unit) then 265 if not UnitExists(self.unit) then
190 self:Hide() 266 self:Hide()
191 else 267 else
192 self:Show() 268 self:Show()
193 self.handler.refresh(self) 269 self.handler.refresh(self)
194 UpdateUnitAnchor(self) 270 end
195 end 271 end
196 end 272
197 273 prototypes.pet = player
198 prototypes.pet = prototypes.player 274 prototypes.target = player
199 prototypes.target = prototypes.player