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