Mercurial > wow > skeletonkey
comparison 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 |
comparison
equal
deleted
inserted
replaced
3:07293831dd7b | 4:a30285f8191e |
---|---|
17 local buttons = {} | 17 local buttons = {} |
18 | 18 |
19 local params = setmetatable({}, { | 19 local params = setmetatable({}, { |
20 __index = function(t, k) | 20 __index = function(t, k) |
21 print('get', k) | 21 print('get', k) |
22 return rawget(t.player, k) | 22 return t.player |
23 end, | 23 end, |
24 __newindex = function(t, k, v) | 24 __newindex = function(t, k, v) |
25 if type(v) == 'table' then | 25 if type(v) == 'table' then |
26 print('branch', k) | |
26 v = setmetatable(v, { | 27 v = setmetatable(v, { |
27 __index = function(tt, kk) | 28 __index = function(tt, kk) |
28 print('get', k, kk) | 29 print('get', k, kk) |
29 return rawget(t.player, kk) | 30 return rawget(t.player, kk) |
30 end, | 31 end, |
31 __newindex = function(tt, kk, vv) | 32 __newindex = function(tt, kk, vv) |
32 if type(vv) == 'table' then | 33 if type(vv) == 'table' then |
34 print('_branch', k, kk) | |
33 vv = setmetatable(vv, { | 35 vv = setmetatable(vv, { |
34 __index = function(_tt, _kk) | 36 __index = function(_tt, _kk) |
35 print('_get', k, kk, _kk) | 37 print('_get', k, kk, _kk) |
36 return rawget(t.player[kk], _kk) | 38 return rawget(t.player[kk], _kk) |
37 end | 39 end |
48 params.player = { | 50 params.player = { |
49 width = 220, | 51 width = 220, |
50 height = 30, | 52 height = 30, |
51 health = { height = 24 }, | 53 health = { height = 24 }, |
52 power = { height = 6}, | 54 power = { height = 6}, |
53 } | 55 position = {'CENTER', nil, 'CENTER',0,-120 } |
56 } | |
54 params.pet = { | 57 params.pet = { |
55 width = 180, | 58 width = 180, |
56 height = 25 | 59 height = 25, |
60 | |
61 position = {'TOPLEFT', 'player', 'BOTTOMLEFT',0,-4} | |
57 } | 62 } |
58 params.focus = params.pet | 63 params.target = { |
64 | |
65 position = {'TOPLEFT', 'player', 'TOPRIGHT',4,0} | |
66 } | |
67 params.focus = { | |
68 width = 180, | |
69 height = 25, | |
70 position = {'BOTTOMRIGHT', 'target', 'TOPRIGHT',0,4} | |
71 } | |
59 | 72 |
60 uf.handler = uf -- so uf.event can work | 73 uf.handler = uf -- so uf.event can work |
61 | 74 |
62 | 75 |
63 local UpdateUnitAnchor = function(self) | 76 local UpdateUnitAnchor = function(self) |
75 local SetupButton = function(self, unit, index) | 88 local SetupButton = function(self, unit, index) |
76 if not buttons[unit][index] then | 89 if not buttons[unit][index] then |
77 buttons[unit][index] = CreateFrame('Frame', 'KT'..unit..'Buff'..index, self, 'KTAuraButton') | 90 buttons[unit][index] = CreateFrame('Frame', 'KT'..unit..'Buff'..index, self, 'KTAuraButton') |
78 buttons[unit][index]:SetSize(BUFF_SIZE, BUFF_SIZE) | 91 buttons[unit][index]:SetSize(BUFF_SIZE, BUFF_SIZE) |
79 buttons[unit][index].cooldown:SetHideCountdownNumbers(true) | 92 buttons[unit][index].cooldown:SetHideCountdownNumbers(true) |
80 | |
81 end | 93 end |
82 return buttons[unit][index] | 94 return buttons[unit][index] |
83 end | 95 end |
84 | 96 |
85 uf.unit = function(unit) | 97 uf.unit = function(unit) |
86 if not prototypes[unit] then | 98 if not prototypes[unit] then |
87 return | 99 return |
88 end | 100 end |
89 print('|cFFFFFF00unit|r:', unit) | 101 print('|cFFFFFF00unit|r:', unit) |
90 | 102 |
103 local c = params[unit] | |
104 | |
91 if not _G['KT'..unit..'Frame'] then | 105 if not _G['KT'..unit..'Frame'] then |
92 CreateFrame('Frame', 'KT'.. unit .. 'Frame', uf) | 106 local new = CreateFrame('Button', 'KT'.. unit .. 'Frame', uf, 'KTUnitFrameTemplate') |
93 end | 107 new.unit = unit |
94 | 108 new.anchorPoint = {new:GetPoint(1)} |
95 local frame = _G['KT'..unit..'Frame'] | 109 new.handler = prototypes[unit] |
96 frame.unit = unit | 110 new.params = c |
97 frame.anchorPoint = {frame:GetPoint(1)} | 111 |
98 frame.handler = prototypes[unit] | 112 --if not db.position[unit] then |
99 | 113 db.position[unit] = c.position |
100 buttons[unit] = {} | 114 --end |
101 units[unit] = frame | 115 new.position = db.position[unit] |
102 | 116 |
103 local width = params.player.width | 117 |
104 local height = params.player.height | 118 buttons[unit] = {} |
105 | 119 new.buttons = buttons[unit] |
106 if params[unit] then | 120 new.refresh = prototypes[unit].refresh |
107 if params[unit].width then | 121 new.init = prototypes[unit].init |
108 width = params[unit].width | 122 |
109 end | 123 new:EnableMouse(true) |
110 if params[unit].height then | 124 new:SetScript('OnMouseUp', prototypes[unit].OnMouseUp) |
111 height = params[unit].height | 125 |
112 end | 126 new:SetAttribute("type", "target") |
113 end | 127 new:SetAttribute("unit", unit) |
114 | 128 RegisterUnitWatch(new) |
115 frame:SetWidth(width) | 129 units[unit] = new |
116 frame:SetHeight(height) | 130 end |
117 | 131 |
118 return frame | 132 return _G['KT'..unit..'Frame'] |
119 end | 133 end |
120 | 134 |
121 uf.ui = function () | 135 uf.ui = function () |
122 for unit, frame in pairs(units) do | 136 for unit, frame in pairs(units) do |
123 frame.handler.refresh(frame) | 137 frame.handler.refresh(frame) |
124 end | 138 end |
125 end | 139 end |
126 | 140 |
127 uf.init = function() | 141 uf.init = function() |
128 uf:RegisterEvent('PLAYER_TARGET_CHANGED') | 142 uf:RegisterEvent('PLAYER_TARGET_CHANGED') |
143 uf:RegisterEvent('PLAYER_FOCUS_CHANGED') | |
129 uf:RegisterUnitEvent("UNIT_AURA") | 144 uf:RegisterUnitEvent("UNIT_AURA") |
130 end | 145 end |
131 | 146 |
132 uf.variables = function() | 147 uf.variables = function() |
133 if not FossilDB then | 148 if not FossilDB then |
154 end | 169 end |
155 return true | 170 return true |
156 end | 171 end |
157 | 172 |
158 uf.PLAYER_TARGET_CHANGED = function() | 173 uf.PLAYER_TARGET_CHANGED = function() |
159 prototypes.player.refresh(units.target) | 174 print('caught target change') |
175 units.target:refresh() | |
176 end | |
177 | |
178 uf.PLAYER_FOCUS_CHANGED = function() | |
179 units.focus:refresh() | |
160 end | 180 end |
161 | 181 |
162 prototypes.player = {} | 182 prototypes.player = {} |
163 local player = prototypes.player | 183 local player = prototypes.player |
164 player.init = function(self) | 184 player.init = function(self) |
166 self:RegisterUnitEvent("UNIT_POWER_FREQUENT", self.unit) | 186 self:RegisterUnitEvent("UNIT_POWER_FREQUENT", self.unit) |
167 self:RegisterEvent("NAME_PLATE_UNIT_ADDED") | 187 self:RegisterEvent("NAME_PLATE_UNIT_ADDED") |
168 self:RegisterEvent("NAME_PLATE_UNIT_REMOVED") | 188 self:RegisterEvent("NAME_PLATE_UNIT_REMOVED") |
169 self:RegisterEvent("UNIT_TARGET") | 189 self:RegisterEvent("UNIT_TARGET") |
170 self:SetScript('OnEvent', uf.event) | 190 self:SetScript('OnEvent', uf.event) |
191 | |
192 local anchor, parent, relative, x, y = unpack(self.position) | |
193 if parent and units[parent] then | |
194 parent = units[parent] | |
195 else | |
196 parent = UIParent | |
197 end | |
198 | |
199 self:SetPoint(anchor, parent, relative, x, y) | |
200 self:SetSize(self.params.width, self.params.height) | |
201 | |
171 end | 202 end |
172 | 203 |
173 | 204 |
174 --- Runs when event handler decides so | 205 --- Runs when event handler decides so |
175 player.refresh = function(self) | 206 player.refresh = function(self) |
207 print(self.unit, UnitExists(self.unit)) | |
176 if not UnitExists(self.unit) then | 208 if not UnitExists(self.unit) then |
209 print('hiding unit') | |
177 self:Hide() | 210 self:Hide() |
178 return | 211 return |
179 end | 212 end |
180 | 213 |
214 self:Show() | |
181 if UnitIsPlayer(self.unit) then | 215 if UnitIsPlayer(self.unit) then |
182 local class, classFile = UnitClass(self.unit) | 216 local class, classFile = UnitClass(self.unit) |
183 if classFile then | 217 if classFile then |
184 self.healthbar:SetColorTexture(RAID_CLASS_COLORS[classFile].r, RAID_CLASS_COLORS[classFile].g, RAID_CLASS_COLORS[classFile].b) | 218 self.healthbar:SetColorTexture(RAID_CLASS_COLORS[classFile].r, RAID_CLASS_COLORS[classFile].g, RAID_CLASS_COLORS[classFile].b) |
185 end | 219 end |
196 | 230 |
197 self.handler.UNIT_POWER_FREQUENT(self) | 231 self.handler.UNIT_POWER_FREQUENT(self) |
198 else | 232 else |
199 self.healthbar:SetHeight(params[self.unit].health.height + params[self.unit].power.height) | 233 self.healthbar:SetHeight(params[self.unit].health.height + params[self.unit].power.height) |
200 end | 234 end |
201 | 235 uf.UNIT_AURA(self, 'UNIT_AURA', self.unit) |
202 | |
203 | 236 |
204 self.handler.UNIT_HEALTH_FREQUENT(self) | 237 self.handler.UNIT_HEALTH_FREQUENT(self) |
205 end | 238 end |
239 | |
206 | 240 |
207 uf.UNIT_AURA = function(self, event, unit) | 241 uf.UNIT_AURA = function(self, event, unit) |
208 if not units[unit] then return true end | 242 if not units[unit] then return true end |
209 | 243 |
210 local buffOffset = 0 | 244 local buffOffset = 0 |
238 --UpdateUnitAnchor(self) | 272 --UpdateUnitAnchor(self) |
239 end | 273 end |
240 | 274 |
241 player.UNIT_HEALTH_FREQUENT = function(self, ...) | 275 player.UNIT_HEALTH_FREQUENT = function(self, ...) |
242 if UnitHealthMax(self.unit) > 0 then | 276 if UnitHealthMax(self.unit) > 0 then |
243 self.healthbar:SetWidth(PLAYER_WIDTH * UnitHealth(self.unit) / UnitHealthMax(self.unit)) | 277 self.healthbar:SetWidth(self.params.width * UnitHealth(self.unit) / UnitHealthMax(self.unit)) |
244 self.healthtext:SetText(UnitHealth(self.unit)) | 278 self.healthtext:SetText(UnitHealth(self.unit)) |
245 else | 279 else |
246 self.healthbar:SetWidth(PLAYER_WIDTH) | 280 self.healthbar:SetWidth(PLAYER_WIDTH) |
247 self.healthtext:SetText(nil) | 281 self.healthtext:SetText(nil) |
248 end | 282 end |
249 return true | 283 return true |
250 end | 284 end |
251 | 285 |
252 player.UNIT_POWER_FREQUENT = function(self) | 286 player.UNIT_POWER_FREQUENT = function(self) |
253 if UnitPowerMax(self.unit) > 0 then | 287 if UnitPowerMax(self.unit) > 0 then |
254 self.powerbar:SetWidth(PLAYER_WIDTH * UnitPower(self.unit) / UnitPowerMax(self.unit)) | 288 self.powerbar:SetWidth(self.params.width * UnitPower(self.unit) / UnitPowerMax(self.unit)) |
255 self.powertext:SetText(UnitPower(self.unit)) | 289 self.powertext:SetText(UnitPower(self.unit)) |
256 else | 290 else |
257 self.powerbar:Hide() | 291 self.powerbar:Hide() |
258 self.powertext:SetText(nil) | 292 self.powertext:SetText(nil) |
259 end | 293 end |
270 end | 304 end |
271 end | 305 end |
272 | 306 |
273 prototypes.pet = player | 307 prototypes.pet = player |
274 prototypes.target = player | 308 prototypes.target = player |
309 prototypes.focus = player |