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