ebed9b1f1ef778fc18271dcc0357add4b3f8f98f
[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 eventFuncs = {
205     ["UNIT_HEALTH"] = function(frame, unit)
206         updateHealth(frame, unit);
207         updateShield(frame, unit);
208         updateHealAbsorb(frame, unit);
209         -- no heal prediction update, that doesn't overflow too much
210     end,
211     ["UNIT_POWER"] = function(frame, unit)
212         updatePower(frame, unit);
213     end,
214     ["UNIT_AURA"] = function(frame, unit)
215         updateAuras(frame, unit);
216     end,
217     ["UNIT_HEAL_PREDICTION"] = function(frame, unit)
218         updateHealPred(frame, unit);
219     end,
220     ["UNIT_ABSORB_AMOUNT_CHANGED"] = function(frame, unit)
221         updateShield(frame, unit);
222     end,
223     ["UNIT_HEAL_ABSORB_AMOUNT_CHANGED"] = function(frame, unit)
224         updateHealAbsorb(frame, unit);
225     end,
226     ["UNIT_THREAT_SITUATION_UPDATE"] = function(frame, unit)
227         updateAggro(frame, unit);
228     end,
229     ["UNIT_MAXHEALTH"] = function(frame, unit)
230         updateMaxHealth(frame, unit);
231     end,
232     ["UNIT_MAXPOWER"] = function(frame, unit)
233         updateMaxPower(frame, unit);
234     end,
235     ["UNIT_DISPLAYPOWER"] = function(frame, unit)
236         updatePowerColor(frame, unit);
237     end,
238     ["UNIT_NAME_UPDATE"] = function(frame, unit)
239         updateName(frame, unit);
240     end,
241     ["UNIT_CONNECTION"] = function(frame, unit)
242         updateHealth(frame, unit);
243     end,
244     ["INCOMING_RESURRECT_CHANGED"] = function(frame, unit)
245         -- TODO have an icon
246         updateHealth(frame, unit);
247     end,
248     ["PARTY_MEMBER_ENABLE"] = function(frame)
249         -- new power info possibly (FrameXML/CompactUnitFrame.lua)
250         updateMaxPower(frame, frame.unit);
251         updatePowerColor(frame, frame.unit);
252     end,
253     ["UPDATE_ALL_BARS"] = function(frame, unit)
254         updateMaxHealth(frame, unit);
255         updateMaxPower(frame, unit);
256         --updateHealth(frame, unit); -- MaxHealth covers this
257         --updatePower(frame, unit); -- MaxPower covers this
258         updateAuras(frame, unit);
259         updateShield(frame, unit);
260         updateHealPred(frame, unit);
261         updateHealAbsorb(frame, unit);
262         updatePowerColor(frame, unit);
263         updateName(frame, unit);
264     end,
265 };
266 eventFuncs["UNIT_HEALTH_FREQUENT"] = eventFuncs["UNIT_HEALTH"];
267 eventFuncs["PARTY_MEMBER_DISABLE"] = eventFuncs["PARTY_MEMBER_ENABLE"];
268 local function unitEvent(self, event, ...)
269     local arg1, arg2, arg3, arg4 = ...;
270     eventFuncs[event](self, arg1);
271 end
272
273 local function unitUpdate(self, elapsed)
274     -- there's no in/out of range event, have to check each frame
275     -- from FrameXML/CompactUnitFrame.lua
276     local inRange, checked = UnitInRange(self.unit);
277     if checked and not inRange then
278         self:SetAlpha(0.55);
279     else
280         self:SetAlpha(1);
281     end
282 end
283
284 local function registerEvents(frame, unit)
285     -- events are taken from FrameXML/CompactUnitFrame.lua
286     -- TODO vehicle support, ready check support, raid marker support,
287     -- player flags support (/afk, /dnd)
288     frame:RegisterEvent("PARTY_MEMBER_ENABLE");
289     frame:RegisterEvent("PARTY_MEMBER_DISABLE");
290     frame:RegisterUnitEvent("UNIT_HEALTH", unit);
291     frame:RegisterUnitEvent("UNIT_HEALTH_FREQUENT", unit);
292     frame:RegisterUnitEvent("UNIT_MAXHEALTH", unit);
293     frame:RegisterUnitEvent("UNIT_POWER", unit);
294     frame:RegisterUnitEvent("UNIT_MAXPOWER", unit);
295     frame:RegisterUnitEvent("UNIT_DISPLAYPOWER", unit);
296     frame:RegisterUnitEvent("UNIT_NAME_UPDATE", unit);
297     frame:RegisterUnitEvent("UNIT_AURA", unit);
298     frame:RegisterUnitEvent("UNIT_HEAL_PREDICTION", unit);
299     frame:RegisterUnitEvent("UNIT_ABSORB_AMOUNT_CHANGED", unit);
300     frame:RegisterUnitEvent("UNIT_HEAL_ABSORB_AMOUNT_CHANGED", unit);
301     frame:RegisterUnitEvent("UNIT_THREAT_SITUATION_UPDATE", unit);
302     frame:RegisterUnitEvent("UNIT_CONNECTION", unit);
303     frame:RegisterUnitEvent("INCOMING_RESURRECT_CHANGED", unit);
304 end
305
306 local function vis(frame, vis)
307     if vis then
308         frame:Show();
309         frame:SetScript("OnUpdate", unitUpdate);
310         frame:UnregisterAllEvents();
311         registerEvents(frame, frame.unit);
312         -- wait one frame to update data
313         -- create function if needed to pass arguments to unitEvent
314         local func = updaters[frame];
315         if not func then
316             func = function() unitEvent(frame, "UPDATE_ALL_BARS", frame.unit) end;
317             updaters[frame] = func;
318         end
319         CTimerAfter(0.01, func);
320     else
321         frame:Hide();
322         frame:SetScript("OnUpdate", nil);
323         frame:UnregisterAllEvents();
324     end;
325 end
326
327 local function updateGroup()
328     if IsInGroup() then
329         if IsInRaid() then
330             -- show raid
331             for _, val in ipairs(raid) do
332                 if UnitInRaid(val.frame.unit) then
333                     vis(val.frame, true);
334                 elseif val.frame:IsShown() then
335                     vis(val.frame, false);
336                 end
337             end
338             -- hide player + party (use pairs, not ipairs)
339             for _, val in pairs(party) do
340                 if val.frame:IsShown() then vis(val.frame, false) end
341             end
342         else
343             -- show player + party
344             for _, val in pairs(party) do
345                 if UnitInParty(val.frame.unit) then
346                     vis(val.frame, true);
347                 elseif val.frame:IsShown() then
348                     vis(val.frame, false);
349                 end
350             end
351             -- hide raid
352             for _, val in ipairs(raid) do
353                 if val.frame:IsShown() then vis(val.frame, false) end
354             end
355         end
356     else
357         -- show player
358         vis(party[0].frame, true);
359         -- hide all other frames
360         for _, val in ipairs(party) do
361             if val.frame:IsShown() then vis(val.frame, false) end
362         end
363         for _, val in ipairs(raid) do
364             if val.frame:IsShown() then vis(val.frame, false) end
365         end
366     end
367 end
368
369 local function setupIndicators(frame)
370     frame.inds = CreateFrame("Frame", nil, frame);
371     frame.inds:SetAllPoints();
372     frame.inds:Hide();
373     for _, pos in pairs(positions) do
374         frame.inds[pos] = frame.inds:CreateTexture(nil, "OVERLAY");
375         frame.inds[pos]:SetPoint(pos, frame.inds, pos);
376         frame.inds[pos]:SetWidth(indSize);
377         frame.inds[pos]:SetHeight(indSize);
378         frame.inds[pos]:SetTexture("Interface\\AddOns\\OmaRF\\images\\rhomb");
379         frame.inds[pos]:SetVertexColor(1, 0, 0);
380         frame.inds[pos]:Hide();
381         frame.inds[pos].text = frame.inds:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
382         frame.inds[pos].text:SetPoint("BOTTOMRIGHT", frame.inds[pos], "BOTTOMRIGHT");
383         frame.inds[pos].text:Hide();
384     end
385     frame.major = CreateFrame("Frame", nil, frame);
386     frame.major:SetPoint("TOPLEFT", frame, "TOPLEFT", 4, -indSize + 4);
387     frame.major:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT");
388     for i = 1,3 do
389         frame.major[i] = frame.major:CreateTexture(nil, "OVERLAY");
390         if i == 1 then
391             frame.major[i]:SetPoint("TOPLEFT", frame.major, "TOPLEFT");
392         else
393             frame.major[i]:SetPoint("TOPLEFT", frame.major[i-1], "TOPLEFT");
394         end
395         frame.major[i]:SetWidth(indSize*2);
396         frame.major[i]:SetHeight(indSize*2);
397         frame.major[i]:Hide();
398         frame.major[i].text = frame.major:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
399         frame.major[i].text:SetPoint("BOTTOMRIGHT", frame.major[i], "BOTTOMRIGHT");
400         frame.major[i].text:Hide();
401         frame.major[i].stack = frame.major:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
402         frame.major[i].stack:SetPoint("TOPLEFT", frame.major[i], "TOPLEFT");
403         frame.major[i].stack:Hide();
404     end
405 end
406
407 local function setupFrame(frame, secure, unit)
408     -- create visuals
409     secure:SetWidth(width+2);
410     frame:SetWidth(width+2);
411     frame.base = frame:CreateTexture(nil, "BACKGROUND");
412     frame.base:SetAllPoints();
413     frame.base:SetColorTexture(unpack(baseColor));
414     frame.background = frame:CreateTexture(nil, "BACKGROUND", nil, 1);
415     frame.background:SetPoint("TOPLEFT", frame, "TOPLEFT", 1, -1);
416     frame.background:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -1, 1);
417     frame.background:SetColorTexture(unpack(bgColor));
418     frame.health = frame:CreateTexture(nil, "BORDER");
419     frame.health:SetTexture("Interface\\RaidFrame\\Raid-Bar-Hp-Fill");
420     frame.health:SetPoint("TOPLEFT", frame.background, "TOPLEFT");
421     frame.health:SetPoint("BOTTOMLEFT", frame.background, "BOTTOMLEFT", 0, 2);
422     frame.health:SetVertexColor(unpack(healthColor));
423     frame.health:SetWidth(width);
424     frame.health.max = UnitHealthMax(unit);
425     frame.mana = frame:CreateTexture(nil, "BORDER");
426     frame.mana:SetPoint("TOPLEFT", frame.background, "BOTTOMLEFT", 0, 2);
427     frame.mana:SetPoint("BOTTOMLEFT", frame.background, "BOTTOMLEFT");
428     frame.mana:SetColorTexture(1, 1, 1);
429     frame.mana:SetWidth(width);
430     frame.mana.max = UnitPowerMax(unit);
431     frame.shield = frame:CreateTexture(nil, "BORDER");
432     frame.shield:SetPoint("TOPLEFT", frame.health, "TOPRIGHT");
433     frame.shield:SetPoint("BOTTOMLEFT", frame.health, "BOTTOMRIGHT");
434     frame.shield:SetColorTexture(unpack(shieldColor));
435     frame.shield:Hide();
436     frame.shieldhl = frame:CreateTexture(nil, "ARTWORK");
437     frame.shieldhl:SetPoint("TOPLEFT", frame.background, "TOPRIGHT", -1, 0);
438     frame.shieldhl:SetPoint("BOTTOMRIGHT", frame.background, "BOTTOMRIGHT", 1, 0);
439     frame.shieldhl:SetColorTexture(unpack(shieldhlColor));
440     frame.shieldhl:Hide();
441     frame.healpred = frame:CreateTexture(nil, "ARTWORK");
442     frame.healpred:SetPoint("TOPLEFT", frame.health, "TOPRIGHT");
443     frame.healpred:SetPoint("BOTTOMLEFT", frame.health, "BOTTOMRIGHT");
444     frame.healpred:SetColorTexture(unpack(healpredColor));
445     frame.healpred:Hide();
446     frame.healabsorb = frame:CreateTexture(nil, "ARTWORK");
447     frame.healabsorb:SetPoint("TOPRIGHT", frame.health, "TOPRIGHT");
448     frame.healabsorb:SetPoint("BOTTOMRIGHT", frame.health, "BOTTOMRIGHT");
449     frame.healabsorb:SetColorTexture(unpack(healabsorbColor));
450     frame.healabsorb:Hide();
451     frame.overlay = frame:CreateTexture(nil, "ARTWORK", nil, 1);
452     frame.overlay:SetPoint("TOPLEFT", frame.background, "TOPLEFT");
453     frame.overlay:SetPoint("BOTTOMRIGHT", frame.background, "BOTTOMRIGHT", 0, 2);
454     frame.overlay:Hide();
455     frame.name = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
456     frame.name:SetPoint("CENTER", frame.background, "CENTER", 0, 11);
457     frame.text = frame:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
458     frame.text:SetFont(STANDARD_TEXT_FONT, 13);
459     frame.text:SetPoint("CENTER", frame.background, "CENTER", 0, -1);
460     frame.text:Hide();
461     setupIndicators(frame);
462     -- set attributes
463     secure:RegisterForClicks("AnyDown");
464     secure:SetAttribute("type1", "spell"); -- left click
465     secure:SetAttribute("type2", "spell"); -- right click
466     secure:SetAttribute("shift-type1", "spell"); -- shift left click
467     secure:SetAttribute("shift-type2", "spell"); -- shift right click
468     secure:SetAttribute("ctrl-type1", "spell"); -- ctrl left click
469     secure:SetAttribute("alt-type2", "spell"); -- alt right click
470     secure:SetAttribute("alt-shift-type1", "spell"); -- alt+shift left click
471     secure:SetAttribute("alt-shift-type2", "spell"); -- alt+shift right click
472     secure:SetAttribute("spell1", "Holy Light");
473     secure:SetAttribute("spell2", "Bestow Faith");
474     secure:SetAttribute("shift-spell1", "Flash of Light");
475     secure:SetAttribute("shift-spell2", "Light of the Martyr");
476     secure:SetAttribute("ctrl-spell1", "Cleanse");
477     secure:SetAttribute("alt-spell2", "Lay on Hands");
478     secure:SetAttribute("alt-shift-spell1", "Beacon of Light");
479     secure:SetAttribute("alt-shift-spell2", "Beacon of Faith");
480     -- rest give target and menu
481     secure:SetAttribute("*type1", "target");
482     secure:SetAttribute("*type2", "togglemenu");
483 end
484
485 local function initializeParty()
486     local secure = CreateFrame("Button", "OmaPartySecure0", CFrame, "SecureUnitButtonTemplate");
487     local frame = CreateFrame("Frame", "OmaParty0", CFrame);
488     local unit = "player";
489     secure:SetAttribute("unit", unit);
490     secure:SetPoint("TOPLEFT", CFrame, "TOPLEFT");
491     secure:SetHeight(height+2);
492     frame.unit = unit;
493     frame:SetPoint("TOPLEFT", CFrame, "TOPLEFT");
494     frame:SetHeight(height+2);
495     frame:SetScript("OnEvent", unitEvent);
496     frame:Hide();
497     setupFrame(frame, secure, unit);
498     RegisterStateDriver(secure, "visibility", "[@player,group:raid] hide; show");
499     party[0] = {secure=secure, frame=frame};
500     for i = 1,4 do
501         local secure = CreateFrame("Button", "OmaPartySecure"..i, CFrame, "SecureUnitButtonTemplate");
502         local frame = CreateFrame("Frame", "OmaParty"..i, CFrame);
503         local unit = "party"..i;
504         secure:SetAttribute("unit", unit);
505         secure:SetPoint("TOPLEFT", party[i-1].secure, "TOPRIGHT");
506         secure:SetPoint("BOTTOMLEFT", party[i-1].secure, "BOTTOMRIGHT");
507         frame.unit = unit;
508         frame:SetPoint("TOPLEFT", party[i-1].frame, "TOPRIGHT");
509         frame:SetPoint("BOTTOMLEFT", party[i-1].frame, "BOTTOMRIGHT");
510         frame:SetScript("OnEvent", unitEvent);
511         frame:Hide();
512         setupFrame(frame, secure, unit);
513         RegisterUnitWatch(secure);
514         party[i] = {secure=secure, frame=frame};
515     end
516 end
517
518 local function initializeRaid()
519     local secure = CreateFrame("Button", "OmaRaidSecure1", CFrame, "SecureUnitButtonTemplate");
520     local frame = CreateFrame("Frame", "OmaRaid1", CFrame);
521     local unit = "raid1";
522     secure:SetAttribute("unit", unit);
523     secure:SetPoint("TOPLEFT", CFrame, "TOPLEFT");
524     secure:SetHeight(height+2);
525     frame.unit = unit;
526     frame:SetPoint("TOPLEFT", CFrame, "TOPLEFT");
527     frame:SetHeight(height+2);
528     frame:SetScript("OnEvent", unitEvent);
529     frame:Hide();
530     setupFrame(frame, secure, unit);
531     RegisterUnitWatch(secure);
532     raid[1] = {secure=secure, frame=frame};
533     for y = 1,7 do
534         local i = y*5+1;
535         local secure = CreateFrame("Button", "OmaRaidSecure"..i, CFrame, "SecureUnitButtonTemplate");
536         local frame = CreateFrame("Frame", "OmaRaid"..i, CFrame);
537         local unit = "raid"..i;
538         secure:SetAttribute("unit", unit);
539         secure:SetPoint("TOPLEFT", raid[i-5].secure, "BOTTOMLEFT");
540         secure:SetHeight(height+2);
541         frame.unit = unit;
542         frame:SetPoint("TOPLEFT", raid[i-5].frame, "BOTTOMLEFT");
543         frame:SetHeight(height+2);
544         frame:SetScript("OnEvent", unitEvent);
545         frame:Hide();
546         setupFrame(frame, secure, unit);
547         RegisterUnitWatch(secure);
548         raid[i] = {secure=secure, frame=frame};
549     end
550     for y = 0,7 do
551         for x = 1,4 do
552             local i = y*5+x+1;
553             local secure = CreateFrame("Button", "OmaRaidSecure"..i, CFrame, "SecureUnitButtonTemplate");
554             local frame = CreateFrame("Frame", "OmaRaid"..i, CFrame);
555             local unit = "raid"..i;
556             secure:SetAttribute("unit", unit);
557             secure:SetPoint("TOPLEFT", raid[i-1].secure, "TOPRIGHT");
558             secure:SetPoint("BOTTOMLEFT", raid[i-1].secure, "BOTTOMRIGHT");
559             frame.unit = unit;
560             frame:SetPoint("TOPLEFT", raid[i-1].frame, "TOPRIGHT");
561             frame:SetPoint("BOTTOMLEFT", raid[i-1].frame, "BOTTOMRIGHT");
562             frame:SetScript("OnEvent", unitEvent);
563             frame:Hide();
564             setupFrame(frame, secure, unit);
565             RegisterUnitWatch(secure);
566             raid[i] = {secure=secure, frame=frame};
567         end
568     end
569 end
570
571 local function initialize()
572     CFrame:SetPoint("CENTER", nil, "CENTER", anchorX, anchorY);
573     CFrame:SetHeight((height+2)*8);
574     CFrame:SetWidth((width+2)*5+1); -- extra pixel for shield overflow
575     initializeParty();
576     initializeRaid();
577 end
578
579 CFrame:RegisterEvent("PLAYER_LOGIN");
580 CFrame:RegisterEvent("PLAYER_ENTERING_WORLD");
581 CFrame:RegisterEvent("GROUP_ROSTER_UPDATE");
582 CFrame:RegisterEvent("PLAYER_REGEN_ENABLED");
583 CFrame:SetScript("OnEvent", function(self, event, ...)
584     if event == "GROUP_ROSTER_UPDATE" or event == "PLAYER_REGEN_ENABLED" or
585        event == "PLAYER_ENTERING_WORLD" then
586         updateGroup();
587     elseif event == "PLAYER_LOGIN" then
588         initialize();
589     end
590 end);