2f55379 - Add proper indicator support and fix bugs found while playing
[wowui.git] / OmaRF / CFrame.lua
1 -- CFrame.lua
2 local _;
3 local unpack, pairs, ipairs = unpack, pairs, ipairs;
4 local ssub = string.sub;
5 local min = math.min;
6 local UnitName, UnitInRange, UnitDebuff = UnitName, UnitInRange, UnitDebuff;
7 local UnitPower, UnitPowerMax, UnitPowerType = UnitPower, UnitPowerMax, UnitPowerType;
8 local UnitHealth, UnitHealthMax = UnitHealth, UnitHealthMax;
9 local UnitInParty, UnitInRaid = UnitInParty, UnitInRaid;
10 local UnitGetIncomingHeals, UnitGetTotalAbsorbs = UnitGetIncomingHeals, UnitGetTotalAbsorbs;
11 local UnitGetTotalHealAbsorbs = UnitGetTotalHealAbsorbs;
12 local UnitIsDeadOrGhost, UnitIsConnected = UnitIsDeadOrGhost, UnitIsConnected;
13 local IsInGroup, IsInRaid = IsInGroup, IsInRaid;
14 local UnitIsCharmed = UnitIsCharmed;
15 local CTimerAfter = C_Timer.After;
16 local UnitClass = UnitClass;
17 local UnitHasIncomingResurrection = UnitHasIncomingResurrection;
18 local UnitThreatSituation, GetThreatStatusColor = UnitThreatSituation, GetThreatStatusColor;
19 local RAID_CLASS_COLORS = RAID_CLASS_COLORS;
20 local CreateFrame, RegisterStateDriver, RegisterUnitWatch = CreateFrame, RegisterStateDriver, RegisterUnitWatch;
21 local SPELL_POWER_MANA = SPELL_POWER_MANA;
22 local checkIndicators = OmaCheckIndicators;
23
24 local CFrame = CreateFrame("Frame", "OmaCFrame", UIParent);
25 local party = {};
26 local raid = {};
27 local positions = {"TOPRIGHT", "BOTTOMLEFT"};
28 local updaters = {};
29
30 -- configurable settings
31 local width, height = 80, 40;
32 local anchorX, anchorY = 0, -330;
33 local indSize = 14;
34 local baseColor = {0, 0, 0};
35 local bgColor = {0.7, 0.7, 0.7};
36 local healthColor = {0.3, 0.3, 0.3};
37 local shieldColor = {0, 0.7, 1};
38 local shieldhlColor = {0.5, 0.8, 1};
39 local healpredColor = {0.5, 0.6, 0.5};
40 local healabsorbColor = {0.1, 0.1, 0.1};
41 local overlayColorDispel = {1, 0.5, 0, 0.5};
42 local overlayColorCharm = {0.8, 0, 1, 0.5};
43 local overlayColorAlert = {1, 0, 0, 0.5};
44 local powerColors = {
45     [SPELL_POWER_MANA] = {0, 0.5, 1},
46     [SPELL_POWER_RAGE] = {1, 0, 0},
47     [SPELL_POWER_FOCUS] = {1, 0.5, 0},
48     [SPELL_POWER_ENERGY] = {1, 0.8, 0},
49     [SPELL_POWER_RUNIC_POWER] = {0.9, 0, 0.1},
50 };
51 -- watch to not remove mana entry
52 setmetatable(powerColors, {__index = function(t) return t[SPELL_POWER_MANA] end});
53
54 local function updateHealth(frame, unit)
55     local current, max = UnitHealth(unit), frame.health.max;
56     local healthLost = max - current;
57     -- sanity check, occasionally UnitHealthMax gives zero
58     if max > 0 then
59         frame.health:SetWidth(current/frame.health.max*width);
60     else
61         frame.health:SetWidth(width);
62         frame.text:Hide();
63         return;
64     end
65
66     if UnitIsDeadOrGhost(unit) then
67         frame.health:SetWidth(1);
68         if UnitHasIncomingResurrection(unit) then
69             frame.text:SetText("Rez");
70         else
71             frame.text:SetText("Dead");
72         end
73     elseif not UnitIsConnected(unit) then
74         frame.health:SetWidth(1);
75         frame.text:SetText("DC");
76     elseif healthLost > 0 then
77         if healthLost > 1200000000 then -- 1.2B
78             frame.text:SetFormattedText("-%.1fB", healthLost / 1000000000);
79         elseif healthLost > 1200000 then -- 1.2M
80             frame.text:SetFormattedText("-%.1fM", healthLost / 1000000);
81         elseif healthLost > 1000 then -- 1K
82             frame.text:SetFormattedText("-%dK", healthLost / 1000);
83         else
84             frame.text:SetFormattedText("-%d", healthLost)
85         end
86         frame.text:Show();
87     else
88         frame.text:Hide();
89     end
90 end
91
92 local function updateMaxHealth(frame, unit)
93     frame.health.max = UnitHealthMax(unit);
94     updateHealth(frame, unit);
95 end
96
97 local function updatePower(frame, unit)
98     local max = frame.mana.max;
99     -- sanity check, occasionally UnitPowerMax gives zero
100     if max > 0 then
101         frame.mana:SetWidth(UnitPower(unit)/max*width);
102     else
103         frame.mana:SetWidth(width);
104     end
105 end
106
107 local function updateMaxPower(frame, unit)
108     frame.mana.max = UnitPowerMax(unit);
109     updatePower(frame, unit);
110 end
111
112 local function updatePowerColor(frame, unit)
113     frame.mana:SetColorTexture(unpack(powerColors[UnitPowerType(unit)]));
114 end
115
116 local function updateName(frame, unit)
117     local name = UnitName(unit);
118     if not name then return end
119     frame.name:SetText(ssub(name, 1, 6));
120
121     local _, class = UnitClass(unit);
122     local color = RAID_CLASS_COLORS[class];
123     if color then frame.name:SetVertexColor(color.r, color.g, color.b) end
124 end
125
126 local function updateHealPred(frame, unit)
127     local incoming = UnitGetIncomingHeals(unit) or 0;
128     if incoming > 0 then
129         local max = frame.health.max;
130         local space = width - frame.health:GetWidth() + 1;
131         local pred = (incoming / max) * width;
132         frame.healpred:SetWidth(min(space, pred));
133         frame.healpred:Show();
134     else
135         frame.healpred:Hide();
136     end
137 end
138
139 local function updateShield(frame, unit)
140     local shield = UnitGetTotalAbsorbs(unit) or 0;
141     if shield > 0 then
142         local max = frame.health.max;
143         local space = width - frame.health:GetWidth();
144         shield = (shield / max) * width;
145         if space < shield then
146             frame.shield:SetWidth(space);
147             frame.shieldhl:Show();
148         else
149             frame.shield:SetWidth(shield);
150             frame.shieldhl:Hide();
151         end
152         frame.shield:Show();
153     else
154         frame.shield:Hide();
155         frame.shieldhl:Hide();
156     end
157 end
158
159 local function updateHealAbsorb(frame, unit)
160     local absorb = UnitGetTotalHealAbsorbs(unit) or 0;
161     if absorb > 0 then
162         local max = frame.health.max;
163         local space = frame.health:GetWidth();
164         absorb = (absorb / max) * width;
165         frame.healabsorb:SetWidth(min(space, absorb));
166         frame.healabsorb:Show();
167     else
168         frame.healabsorb:Hide();
169     end
170 end
171
172 local function updateAuras(frame, unit)
173     checkIndicators(frame, unit);
174     if UnitDebuff(unit, 1, "RAID") ~= nil then
175         -- something dispellable
176         if frame.overlay.color ~= overlayColorDispel then
177             frame.overlay:SetColorTexture(unpack(overlayColorDispel));
178             frame.overlay:Show();
179             frame.overlay.color = overlayColorDispel;
180         end
181     elseif UnitIsCharmed(unit) then
182         if frame.overlay.color ~= overlayColorCharm then
183             frame.overlay:SetColorTexture(unpack(overlayColorCharm));
184             frame.overlay:Show();
185             frame.overlay.color = overlayColorCharm;
186         end
187     else
188         if frame.overlay.color ~= nil then
189             frame.overlay:Hide();
190             frame.overlay.color = nil;
191         end
192     end
193 end
194
195 local function updateAggro(frame, unit)
196     local status = UnitThreatSituation(unit);
197     if status and status > 0 then
198         frame.base:SetColorTexture(GetThreatStatusColor(status));
199     else
200         frame.base:SetColorTexture(unpack(baseColor));
201     end
202 end
203
204 local function unitEvent(self, event, ...)
205     local arg1, arg2, arg3, arg4 = ...;
206     if event == "UNIT_HEALTH" or event == "UNIT_HEALTH_FREQUENT" then
207         updateHealth(self, arg1);
208         updateShield(self, arg1);
209         updateHealAbsorb(self, arg1);
210         -- no heal prediction update, that doesn't overflow too much
211     elseif event == "UNIT_POWER" then
212         updatePower(self, arg1);
213     elseif event == "UNIT_AURA" then
214         updateAuras(self, arg1);
215     elseif event == "UNIT_ABSORB_AMOUNT_CHANGED" then
216         updateShield(self, arg1);
217     elseif event == "UNIT_HEAL_PREDICTION" then
218         updateHealPred(self, arg1);
219     elseif event == "UNIT_HEAL_ABSORB_AMOUNT_CHANGED" then
220         updateHealAbsorb(self, arg1);
221     elseif event == "UNIT_THREAT_SITUATION_UPDATE" then
222         updateAggro(self, arg1);
223     elseif event == "UNIT_MAXHEALTH" then
224         updateMaxHealth(self, arg1);
225     elseif event == "UNIT_MAXPOWER" then
226         updateMaxPower(self, arg1);
227     elseif event == "UNIT_DISPLAYPOWER" then
228         updatePowerColor(self, arg1);
229     elseif event == "INCOMING_RESURRECT_CHANGED" then
230         -- TODO have an icon
231         updateHealth(self, arg1);
232     elseif event == "UNIT_NAME_UPDATE" then
233         updateName(self, arg1);
234     elseif event == "PARTY_MEMBER_ENABLE" or event == "PARTY_MEMBER_DISABLE" then
235         -- new power info possibly (FrameXML/CompactUnitFrame.lua)
236         updateMaxPower(self, self.unit);
237         updatePowerColor(self, self.unit);
238     elseif event == "UNIT_CONNECTION" then
239         updateHealth(self, arg1);
240     elseif event == "UPDATE_ALL_BARS" then
241         updateMaxHealth(self, arg1);
242         updateMaxPower(self, arg1);
243         --updateHealth(self, arg1); -- MaxHealth covers this
244         --updatePower(self, arg1); -- MaxPower covers this
245         updateAuras(self, arg1);
246         updateShield(self, arg1);
247         updateHealPred(self, arg1);
248         updateHealAbsorb(self, arg1);
249         updatePowerColor(self, arg1);
250         updateName(self, arg1);
251     end
252 end
253
254 local function unitUpdate(self, elapsed)
255     -- there's no in/out of range event, have to check each frame
256     -- from FrameXML/CompactUnitFrame.lua
257     local inRange, checked = UnitInRange(self.unit);
258     if checked and not inRange then
259         self:SetAlpha(0.55);
260     else
261         self:SetAlpha(1);
262     end
263 end
264
265 local function registerEvents(frame, unit)
266     -- events are taken from FrameXML/CompactUnitFrame.lua
267     -- TODO vehicle support, ready check support, raid marker support,
268     -- player flags support (/afk, /dnd)
269     frame:RegisterEvent("PARTY_MEMBER_ENABLE");
270     frame:RegisterEvent("PARTY_MEMBER_DISABLE");
271     frame:RegisterUnitEvent("UNIT_HEALTH", unit);
272     frame:RegisterUnitEvent("UNIT_HEALTH_FREQUENT", unit);
273     frame:RegisterUnitEvent("UNIT_MAXHEALTH", unit);
274     frame:RegisterUnitEvent("UNIT_POWER", unit);
275     frame:RegisterUnitEvent("UNIT_MAXPOWER", unit);
276     frame:RegisterUnitEvent("UNIT_DISPLAYPOWER", unit);
277     frame:RegisterUnitEvent("UNIT_NAME_UPDATE", unit);
278     frame:RegisterUnitEvent("UNIT_AURA", unit);
279     frame:RegisterUnitEvent("UNIT_HEAL_PREDICTION", unit);
280     frame:RegisterUnitEvent("UNIT_ABSORB_AMOUNT_CHANGED", unit);
281     frame:RegisterUnitEvent("UNIT_HEAL_ABSORB_AMOUNT_CHANGED", unit);
282     frame:RegisterUnitEvent("UNIT_THREAT_SITUATION_UPDATE", unit);
283     frame:RegisterUnitEvent("UNIT_CONNECTION", unit);
284     frame:RegisterUnitEvent("INCOMING_RESURRECT_CHANGED", unit);
285 end
286
287 local function vis(frame, vis)
288     if vis then
289         frame:Show();
290         frame:SetScript("OnUpdate", unitUpdate);
291         frame:UnregisterAllEvents();
292         registerEvents(frame, frame.unit);
293         -- wait one frame to update data
294         -- create function if needed to pass arguments to unitEvent
295         local func = updaters[frame];
296         if not func then
297             func = function() unitEvent(frame, "UPDATE_ALL_BARS", frame.unit) end;
298             updaters[frame] = func;
299         end
300         CTimerAfter(0.01, func);
301     else
302         frame:Hide();
303         frame:SetScript("OnUpdate", nil);
304         frame:UnregisterAllEvents();
305     end;
306 end
307
308 local function updateGroup()
309     if IsInGroup() then
310         if IsInRaid() then
311             -- show raid
312             for _, val in ipairs(raid) do
313                 if UnitInRaid(val.frame.unit) then
314                     vis(val.frame, true);
315                 elseif val.frame:IsShown() then
316                     vis(val.frame, false);
317                 end
318             end
319             -- hide player + party (use pairs, not ipairs)
320             for _, val in pairs(party) do
321                 if val.frame:IsShown() then vis(val.frame, false) end
322             end
323         else
324             -- show player + party
325             for _, val in pairs(party) do
326                 if UnitInParty(val.frame.unit) then
327                     vis(val.frame, true);
328                 elseif val.frame:IsShown() then
329                     vis(val.frame, false);
330                 end
331             end
332             -- hide raid
333             for _, val in ipairs(raid) do
334                 if val.frame:IsShown() then vis(val.frame, false) end
335             end
336         end
337     else
338         -- show player
339         vis(party[0].frame, true);
340         -- hide all other frames
341         for _, val in ipairs(party) do
342             if val.frame:IsShown() then vis(val.frame, false) end
343         end
344         for _, val in ipairs(raid) do
345             if val.frame:IsShown() then vis(val.frame, false) end
346         end
347     end
348 end
349
350 local function setupIndicators(frame)
351     frame.inds = CreateFrame("Frame", nil, frame);
352     frame.inds:SetAllPoints();
353     frame.inds:Hide();
354     for _, pos in pairs(positions) do
355         frame.inds[pos] = frame.inds:CreateTexture(nil, "OVERLAY");
356         frame.inds[pos]:SetPoint(pos, frame.inds, pos);
357         frame.inds[pos]:SetWidth(indSize);
358         frame.inds[pos]:SetHeight(indSize);
359         frame.inds[pos]:SetTexture("Interface\\AddOns\\OmaRF\\images\\rhomb");
360         frame.inds[pos]:SetVertexColor(1, 0, 0);
361         frame.inds[pos]:Hide();
362         frame.inds[pos].text = frame.inds:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
363         frame.inds[pos].text:SetPoint("BOTTOMRIGHT", frame.inds[pos], "BOTTOMRIGHT");
364         frame.inds[pos].text:Hide();
365     end
366     frame.major = CreateFrame("Frame", nil, frame);
367     frame.major:SetPoint("TOPLEFT", frame, "TOPLEFT", 4, -indSize + 4);
368     frame.major:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT");
369     for i = 1,3 do
370         frame.major[i] = frame.major:CreateTexture(nil, "OVERLAY");
371         if i == 1 then
372             frame.major[i]:SetPoint("TOPLEFT", frame.major, "TOPLEFT");
373         else
374             frame.major[i]:SetPoint("TOPLEFT", frame.major[i-1], "TOPLEFT");
375         end
376         frame.major[i]:SetWidth(indSize*2);
377         frame.major[i]:SetHeight(indSize*2);
378         frame.major[i]:Hide();
379         frame.major[i].text = frame.major:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
380         frame.major[i].text:SetPoint("BOTTOMRIGHT", frame.major[i], "BOTTOMRIGHT");
381         frame.major[i].text:Hide();
382         frame.major[i].stack = frame.major:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
383         frame.major[i].stack:SetPoint("TOPLEFT", frame.major[i], "TOPLEFT");
384         frame.major[i].stack:Hide();
385     end
386 end
387
388 local function setupFrame(frame, secure, unit)
389     -- create visuals
390     secure:SetWidth(width+2);
391     frame:SetWidth(width+2);
392     frame.base = frame:CreateTexture(nil, "BACKGROUND");
393     frame.base:SetAllPoints();
394     frame.base:SetColorTexture(unpack(baseColor));
395     frame.background = frame:CreateTexture(nil, "BACKGROUND", nil, 1);
396     frame.background:SetPoint("TOPLEFT", frame, "TOPLEFT", 1, -1);
397     frame.background:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -1, 1);
398     frame.background:SetColorTexture(unpack(bgColor));
399     frame.health = frame:CreateTexture(nil, "BORDER");
400     frame.health:SetTexture("Interface\\RaidFrame\\Raid-Bar-Hp-Fill");
401     frame.health:SetPoint("TOPLEFT", frame.background, "TOPLEFT");
402     frame.health:SetPoint("BOTTOMLEFT", frame.background, "BOTTOMLEFT", 0, 2);
403     frame.health:SetVertexColor(unpack(healthColor));
404     frame.health:SetWidth(width);
405     frame.health.max = UnitHealthMax(unit);
406     frame.mana = frame:CreateTexture(nil, "BORDER");
407     frame.mana:SetPoint("TOPLEFT", frame.background, "BOTTOMLEFT", 0, 2);
408     frame.mana:SetPoint("BOTTOMLEFT", frame.background, "BOTTOMLEFT");
409     frame.mana:SetColorTexture(1, 1, 1);
410     frame.mana:SetWidth(width);
411     frame.mana.max = UnitPowerMax(unit);
412     frame.shield = frame:CreateTexture(nil, "BORDER");
413     frame.shield:SetPoint("TOPLEFT", frame.health, "TOPRIGHT");
414     frame.shield:SetPoint("BOTTOMLEFT", frame.health, "BOTTOMRIGHT");
415     frame.shield:SetColorTexture(unpack(shieldColor));
416     frame.shield:Hide();
417     frame.shieldhl = frame:CreateTexture(nil, "ARTWORK");
418     frame.shieldhl:SetPoint("TOPLEFT", frame.background, "TOPRIGHT", -1, 0);
419     frame.shieldhl:SetPoint("BOTTOMRIGHT", frame.background, "BOTTOMRIGHT", 1, 0);
420     frame.shieldhl:SetColorTexture(unpack(shieldhlColor));
421     frame.shieldhl:Hide();
422     frame.healpred = frame:CreateTexture(nil, "ARTWORK");
423     frame.healpred:SetPoint("TOPLEFT", frame.health, "TOPRIGHT");
424     frame.healpred:SetPoint("BOTTOMLEFT", frame.health, "BOTTOMRIGHT");
425     frame.healpred:SetColorTexture(unpack(healpredColor));
426     frame.healpred:Hide();
427     frame.healabsorb = frame:CreateTexture(nil, "ARTWORK");
428     frame.healabsorb:SetPoint("TOPRIGHT", frame.health, "TOPRIGHT");
429     frame.healabsorb:SetPoint("BOTTOMRIGHT", frame.health, "BOTTOMRIGHT");
430     frame.healabsorb:SetColorTexture(unpack(healabsorbColor));
431     frame.healabsorb:Hide();
432     frame.overlay = frame:CreateTexture(nil, "ARTWORK", nil, 1);
433     frame.overlay:SetPoint("TOPLEFT", frame.background, "TOPLEFT");
434     frame.overlay:SetPoint("BOTTOMRIGHT", frame.background, "BOTTOMRIGHT", 0, 2);
435     frame.overlay:Hide();
436     frame.name = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
437     frame.name:SetPoint("CENTER", frame.background, "CENTER", 0, 11);
438     frame.text = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
439     frame.text:SetFont(STANDARD_TEXT_FONT, 13);
440     frame.text:SetPoint("CENTER", frame.background, "CENTER", 0, -1);
441     frame.text:Hide();
442     setupIndicators(frame);
443     -- set attributes
444     secure:RegisterForClicks("AnyDown");
445     secure:SetAttribute("type1", "spell"); -- left click
446     secure:SetAttribute("type2", "spell"); -- right click
447     secure:SetAttribute("shift-type1", "spell"); -- shift left click
448     secure:SetAttribute("shift-type2", "spell"); -- shift right click
449     secure:SetAttribute("ctrl-type1", "spell"); -- ctrl left click
450     secure:SetAttribute("alt-type2", "spell"); -- alt right click
451     secure:SetAttribute("alt-shift-type1", "spell"); -- alt+shift left click
452     secure:SetAttribute("alt-shift-type2", "spell"); -- alt+shift right click
453     secure:SetAttribute("spell1", "Holy Light");
454     secure:SetAttribute("spell2", "Bestow Faith");
455     secure:SetAttribute("shift-spell1", "Flash of Light");
456     secure:SetAttribute("shift-spell2", "Light of the Martyr");
457     secure:SetAttribute("ctrl-spell1", "Cleanse");
458     secure:SetAttribute("alt-spell2", "Lay on Hands");
459     secure:SetAttribute("alt-shift-spell1", "Beacon of Light");
460     secure:SetAttribute("alt-shift-spell2", "Beacon of Faith");
461     -- rest give target and menu
462     secure:SetAttribute("*type1", "target");
463     secure:SetAttribute("*type2", "togglemenu");
464 end
465
466 local function initializeParty()
467     local secure = CreateFrame("Button", "OmaPartySecure0", CFrame, "SecureUnitButtonTemplate");
468     local frame = CreateFrame("Frame", "OmaParty0", CFrame);
469     local unit = "player";
470     secure:SetAttribute("unit", unit);
471     secure:SetPoint("TOPLEFT", CFrame, "TOPLEFT");
472     secure:SetHeight(height+2);
473     frame.unit = unit;
474     frame:SetPoint("TOPLEFT", CFrame, "TOPLEFT");
475     frame:SetHeight(height+2);
476     frame:SetScript("OnEvent", unitEvent);
477     frame:Hide();
478     setupFrame(frame, secure, unit);
479     RegisterStateDriver(secure, "visibility", "[@player,group:raid] hide; show");
480     party[0] = {secure=secure, frame=frame};
481     for i = 1,4 do
482         local secure = CreateFrame("Button", "OmaPartySecure"..i, CFrame, "SecureUnitButtonTemplate");
483         local frame = CreateFrame("Frame", "OmaParty"..i, CFrame);
484         local unit = "party"..i;
485         secure:SetAttribute("unit", unit);
486         secure:SetPoint("TOPLEFT", party[i-1].secure, "TOPRIGHT");
487         secure:SetPoint("BOTTOMLEFT", party[i-1].secure, "BOTTOMRIGHT");
488         frame.unit = unit;
489         frame:SetPoint("TOPLEFT", party[i-1].frame, "TOPRIGHT");
490         frame:SetPoint("BOTTOMLEFT", party[i-1].frame, "BOTTOMRIGHT");
491         frame:SetScript("OnEvent", unitEvent);
492         frame:Hide();
493         setupFrame(frame, secure, unit);
494         RegisterUnitWatch(secure);
495         party[i] = {secure=secure, frame=frame};
496     end
497 end
498
499 local function initializeRaid()
500     local secure = CreateFrame("Button", "OmaRaidSecure1", CFrame, "SecureUnitButtonTemplate");
501     local frame = CreateFrame("Frame", "OmaRaid1", CFrame);
502     local unit = "raid1";
503     secure:SetAttribute("unit", unit);
504     secure:SetPoint("TOPLEFT", CFrame, "TOPLEFT");
505     secure:SetHeight(height+2);
506     frame.unit = unit;
507     frame:SetPoint("TOPLEFT", CFrame, "TOPLEFT");
508     frame:SetHeight(height+2);
509     frame:SetScript("OnEvent", unitEvent);
510     frame:Hide();
511     setupFrame(frame, secure, unit);
512     RegisterUnitWatch(secure);
513     raid[1] = {secure=secure, frame=frame};
514     for y = 1,7 do
515         local i = y*5+1;
516         local secure = CreateFrame("Button", "OmaRaidSecure"..i, CFrame, "SecureUnitButtonTemplate");
517         local frame = CreateFrame("Frame", "OmaRaid"..i, CFrame);
518         local unit = "raid"..i;
519         secure:SetAttribute("unit", unit);
520         secure:SetPoint("TOPLEFT", raid[i-5].secure, "BOTTOMLEFT");
521         secure:SetHeight(height+2);
522         frame.unit = unit;
523         frame:SetPoint("TOPLEFT", raid[i-5].frame, "BOTTOMLEFT");
524         frame:SetHeight(height+2);
525         frame:SetScript("OnEvent", unitEvent);
526         frame:Hide();
527         setupFrame(frame, secure, unit);
528         RegisterUnitWatch(secure);
529         raid[i] = {secure=secure, frame=frame};
530     end
531     for y = 0,7 do
532         for x = 1,4 do
533             local i = y*5+x+1;
534             local secure = CreateFrame("Button", "OmaRaidSecure"..i, CFrame, "SecureUnitButtonTemplate");
535             local frame = CreateFrame("Frame", "OmaRaid"..i, CFrame);
536             local unit = "raid"..i;
537             secure:SetAttribute("unit", unit);
538             secure:SetPoint("TOPLEFT", raid[i-1].secure, "TOPRIGHT");
539             secure:SetPoint("BOTTOMLEFT", raid[i-1].secure, "BOTTOMRIGHT");
540             frame.unit = unit;
541             frame:SetPoint("TOPLEFT", raid[i-1].frame, "TOPRIGHT");
542             frame:SetPoint("BOTTOMLEFT", raid[i-1].frame, "BOTTOMRIGHT");
543             frame:SetScript("OnEvent", unitEvent);
544             frame:Hide();
545             setupFrame(frame, secure, unit);
546             RegisterUnitWatch(secure);
547             raid[i] = {secure=secure, frame=frame};
548         end
549     end
550 end
551
552 local function initialize()
553     CFrame:SetPoint("CENTER", nil, "CENTER", anchorX, anchorY);
554     CFrame:SetHeight((height+2)*8);
555     CFrame:SetWidth((width+2)*5+1); -- extra pixel for shield overflow
556     initializeParty();
557     initializeRaid();
558 end
559
560 CFrame:RegisterEvent("PLAYER_LOGIN");
561 CFrame:RegisterEvent("PLAYER_ENTERING_WORLD");
562 CFrame:RegisterEvent("GROUP_ROSTER_UPDATE");
563 CFrame:RegisterEvent("PLAYER_REGEN_ENABLED");
564 CFrame:SetScript("OnEvent", function(self, event, ...)
565     if event == "GROUP_ROSTER_UPDATE" or event == "PLAYER_REGEN_ENABLED" or
566        event == "PLAYER_ENTERING_WORLD" then
567         updateGroup();
568     elseif event == "PLAYER_LOGIN" then
569         initialize();
570     end
571 end);