3ddb182 - Fix insecure boss frames
[wowui.git] / OmaUF / CastBar.lua
1 -- CastBar.lua
2 local _;
3 local unpack = unpack;
4 local ssub = string.sub;
5 local min = math.min;
6 local ceil = math.ceil;
7 local UnitCastingInfo, UnitChannelInfo = UnitCastingInfo, UnitChannelInfo;
8 local GetTime = GetTime;
9
10 local barTexture = "Interface\\AddOns\\OmaRF\\images\\minimalist";
11 local castingColor = {1, 0.49, 0}; -- from Quartz defaults
12 local nointerruptColor = {0.6, 0.6, 0.6};
13 local channelingColor = {0.32, 0.3, 1};
14
15 local M = {};
16 OmaUFCastBar = M;
17
18 -- TODO trade skill bar updates as well, check Quartz modules/Tradeskill.lua
19 local function onUpdate(bar)
20     if not bar:IsShown() then return end
21     local width = bar.icon:IsShown() and bar.cast.width or bar.cast.width; -- TODO fullwidth
22     local startTime, endTime = bar.startTime, bar.endTime;
23     local currentClamped = min(GetTime(), endTime);
24     local remaining = endTime - currentClamped;
25     local percent;
26     if bar.channeling then
27         percent = remaining / (endTime - startTime);
28     else
29         percent = (currentClamped - startTime) / (endTime - startTime);
30     end
31
32     width = percent*width;
33     if width <= 0 then
34         bar.cast:SetWidth(0.1);
35     else
36         bar.cast:SetWidth(width);
37     end
38     bar.time:SetFormattedText("%.1f", remaining);
39 end
40
41 local function toggleInterruptible(bar, nointr)
42     if bar.unit == "player" then return end
43     if nointr then
44         bar.cast:SetVertexColor(unpack(nointerruptColor));
45         bar.shield:Show();
46     else
47         bar.cast:SetVertexColor(unpack(bar.cast.color));
48         bar.shield:Hide();
49     end
50 end
51
52 local function startCast(bar, unit, channeling)
53     local name, icon, startTime, endTime, noInterrupt, id;
54     if channeling then
55         name, _, _, icon, startTime, endTime, _, noInterrupt = UnitChannelInfo(unit);
56         if not startTime or not endTime then return nil end
57         bar.channeling = true;
58         bar.cast.color = channelingColor;
59     else
60         _, _, name, icon, startTime, endTime, _, _, noInterrupt, id = UnitCastingInfo(unit);
61         if not startTime or not endTime then return nil end
62         bar.channeling = nil;
63         bar.cast.color = castingColor;
64     end
65     bar.startTime = startTime / 1000;
66     bar.endTime = endTime / 1000;
67     -- don't show samwise for non-existent icons
68     if icon ~= "Interface\\Icons\\Temp" then
69         bar.icon:SetTexture(icon);
70         bar.icon:Show();
71         bar.cast:SetWidth(channeling and bar.cast.width or 0.1);
72     else
73         bar.icon:Hide();
74         bar.cast:SetWidth(channeling and bar.cast.width or 0.1); -- TODO use fullwidth
75     end
76     bar.spell:SetText(ssub(name, 1, bar.spell.count));
77     bar.time:SetFormattedText("%.1f", (endTime - startTime)/1000);
78     bar.cast:SetVertexColor(unpack(bar.cast.color));
79     bar:Show();
80     toggleInterruptible(bar, noInterrupt);
81     return true;
82 end
83
84 local function applyDelay(bar, unit, channeling)
85     local startTime, endTime;
86     if channeling then
87         _, _, _, _, startTime, endTime = UnitChannelInfo(unit);
88     else
89         _, _, _, _, startTime, endTime = UnitCastingInfo(unit);
90     end
91     if not startTime or not endTime then return bar:Hide() end
92     bar.startTime = startTime / 1000;
93     bar.endTime = endTime / 1000;
94     -- TODO show delay text
95 end
96
97 local events = {
98     ["UNIT_SPELLCAST_START"] = function(bar, unit)
99         startCast(bar, unit);
100     end,
101     ["PLAYER_TARGET_CHANGED"] = function(bar)
102         bar:Hide();
103         if not startCast(bar, bar.unit) then
104             startCast(bar, bar.unit, true);
105         end
106     end,
107     ["UNIT_SPELLCAST_CHANNEL_START"] = function(bar, unit)
108         startCast(bar, unit, true);
109     end,
110     ["UNIT_SPELLCAST_STOP"] = function(bar, unit)
111         bar:Hide();
112     end,
113     ["UNIT_SPELLCAST_DELAYED"] = function(bar, unit)
114         applyDelay(bar, unit);
115     end,
116     ["UNIT_SPELLCAST_CHANNEL_UPDATE"] = function(bar, unit)
117         applyDelay(bar, unit, true);
118     end,
119     ["UNIT_SPELLCAST_INTERRUPTIBLE"] = function(bar)
120         toggleInterruptible(bar, false);
121     end,
122     ["UNIT_SPELLCAST_NOT_INTERRUPTIBLE"] = function(bar)
123         toggleInterruptible(bar, true);
124     end,
125 };
126 events["UNIT_SPELLCAST_CHANNEL_STOP"] = events["UNIT_SPELLCAST_STOP"];
127
128 local function onEvent(bar, event, unit)
129     if unit == bar.unit or (bar.unit == "player" and unit == "vehicle") then
130         --print(unit, event)
131         events[event](bar, unit);
132     elseif event == "PLAYER_TARGET_CHANGED" then
133         events[event](bar);
134     end
135 end
136
137 function M.RegisterCastEvents(bar)
138     --bar:RegisterEvent("UNIT_SPELLCAST_SENT");
139     bar:RegisterEvent("UNIT_SPELLCAST_START");
140     bar:RegisterEvent("UNIT_SPELLCAST_STOP");
141     --bar:RegisterEvent("UNIT_SPELLCAST_FAILED");
142     --bar:RegisterEvent("UNIT_SPELLCAST_INTERRUPTED");
143     bar:RegisterEvent("UNIT_SPELLCAST_DELAYED");
144     bar:RegisterEvent("UNIT_SPELLCAST_CHANNEL_START");
145     bar:RegisterEvent("UNIT_SPELLCAST_CHANNEL_STOP");
146     bar:RegisterEvent("UNIT_SPELLCAST_CHANNEL_UPDATE");
147     bar:RegisterEvent("UNIT_SPELLCAST_INTERRUPTIBLE");
148     bar:RegisterEvent("UNIT_SPELLCAST_NOT_INTERRUPTIBLE");
149     if bar.unit == "target" then bar:RegisterEvent("PLAYER_TARGET_CHANGED") end
150     bar:SetScript("OnUpdate", onUpdate);
151     -- trigger initial check
152     if not startCast(bar, bar.unit) then
153         startCast(bar, bar.unit, true);
154     end
155 end
156
157 function M.UnregisterCastEvents(bar)
158     bar:UnregisterAllEvents();
159     bar:SetScript("OnUpdate", nil);
160 end
161
162 function M.CreateCastBar(parent, unit, yoffset)
163     local bar = CreateFrame("Frame", parent:GetName().."CastBar", parent);
164     bar.unit = unit;
165     bar:SetPoint("BOTTOMLEFT", parent, "TOPLEFT", 0, yoffset);
166     bar:SetPoint("BOTTOMRIGHT", parent, "TOPRIGHT", 0, yoffset);
167     bar:SetHeight(22);
168     bar:Hide();
169     bar.back = bar:CreateTexture(nil, "BACKGROUND");
170     bar.back:SetAllPoints();
171     bar.back:SetColorTexture(0, 0, 0, 0.7);
172     bar.icon = bar:CreateTexture(nil, "ARTWORK", 1);
173     bar.icon:SetPoint("TOPLEFT", bar, "TOPLEFT", 1, -1);
174     bar.icon:SetPoint("BOTTOMRIGHT", bar, "BOTTOMLEFT", 21, 1);
175     bar.icon:SetTexCoord(0.07, 0.93, 0.07, 0.93); -- remove borders (from Quartz)
176     bar.shield = bar:CreateTexture(nil, "ARTWORK");
177     bar.shield:SetPoint("CENTER", bar.icon, "CENTER", -2, -1);
178     bar.shield:SetWidth(28);
179     bar.shield:SetHeight(50);
180     bar.shield:SetTexture("Interface\\CastingBar\\UI-CastingBar-Small-Shield");
181     bar.shield:SetTexCoord(0, 36/256, 0, 1);
182     bar.shield:Hide();
183     bar.cast = bar:CreateTexture(nil, "ARTWORK");
184     bar.cast:SetPoint("TOPLEFT", bar.icon, "TOPRIGHT", 1, 0);
185     bar.cast:SetPoint("BOTTOMLEFT", bar.icon, "BOTTOMRIGHT", 1, 0);
186     bar.cast.width = bar:GetWidth() - bar.icon:GetWidth() - 3;
187     bar.cast.fullwidth = bar:GetWidth() - 2; -- for casts without icon
188     bar.cast:SetWidth(bar.cast.width);
189     bar.cast:SetTexture(barTexture);
190     bar.cast.color = castingColor;
191     bar.spell = bar:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
192     bar.spell:SetPoint("LEFT", bar.icon, "RIGHT", 2, 0);
193     bar.spell.count = ceil(bar.cast:GetWidth()/10);
194     bar.time = bar:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
195     bar.time:SetPoint("RIGHT", bar, "RIGHT", -2, 0);
196     bar:SetScript("OnEvent", onEvent);
197     return bar;
198 end