-
Notifications
You must be signed in to change notification settings - Fork 8
/
UI.lua
325 lines (268 loc) · 8.54 KB
/
UI.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
local RaidNotifier = RaidNotifier
local UI = RaidNotifier.UI
do
local UI_FRAGMENT
local function GetKey(c, s)
return c.."_"..s
end
function UI.AddFragment()
if not UI_FRAGMENT then
UI_FRAGMENT = ZO_HUDFadeSceneFragment:New(RaidNotifierUI)
end
HUD_SCENE:AddFragment(UI_FRAGMENT)
HUD_UI_SCENE:AddFragment(UI_FRAGMENT)
end
function UI.RemoveFragment()
if not UI_FRAGMENT then
UI_FRAGMENT = ZO_HUDFadeSceneFragment:New(RaidNotifierUI)
end
HUD_SCENE:RemoveFragment(UI_FRAGMENT)
HUD_UI_SCENE:RemoveFragment(UI_FRAGMENT)
end
local elements = {}
function UI.RegisterElement(control, hidden)
elements[GetKey(control.category, control.setting)] = control
control:SetHandler("OnMoveStop", UI.SaveElement)
-- update handler, name of handler as string in the "RaidNotifier.UI" namespace
if control.onUpdate ~= nil then
control:SetHandler("OnUpdate", function() UI[control.onUpdate](control) end)
end
if control.onInitialize ~= nil then
UI[control.onInitialize](control)
end
if hidden ~= nil then
control:SetHidden(hidden)
end
end
function UI.LoadElements()
local parent = RaidNotifierUI
for k, elem in pairs(elements) do
local settings = RaidNotifier.Vars[elem.category]
elem:ClearAnchors()
elem:SetAnchor(TOPLEFT, parent, TOPLEFT, unpack(settings[elem.setting]))
end
end
function UI.SaveElement(control)
local settings = RaidNotifier.Vars[control.category]
settings[control.setting] = {control:GetLeft(), control:GetTop()}
end
function UI.SetElementHidden(category, setting, hidden)
local control = elements[GetKey(category, setting)]
if control then -- and control:IsHidden() ~= hidden then
control:SetHidden(hidden)
end
end
function UI.HideAllElements()
for k, elem in pairs(elements) do
elem:SetHidden(true)
end
end
function UI.GetElement(category, setting)
return elements[GetKey(category, setting)]
end
end
-- -------------------
-- -- RNTIMER OBJECT
do -------------------
local Util = RaidNotifier.Util
local OnUpdate = nil --forwarding
local refCounter = Util.RefCounter:New("RNTimerCounter",
-- on getting atleast one reference
function()
--d("Registering for update")
EVENT_MANAGER:RegisterForUpdate(RaidNotifier.Name, 100, OnUpdate)
end,
-- on reaching zero references
function()
--d("Unregistering for update")
EVENT_MANAGER:UnregisterForUpdate(RaidNotifier.Name)
end)
-- The master list for all timers, active and inactive
-- TODO: switch to a proper pool?
local RNTimerList = {}
OnUpdate = function()
local currentTimeMs = GetFrameTimeMilliseconds()
for _, timer in ipairs(RNTimerList) do
timer:Update(currentTimeMs)
end
end
-- Now for the actual object, still a WIP so structure may change drastically
RNTimer = ZO_Object:Subclass()
function RNTimer:New(...)
local object = ZO_Object.New(self)
object:Initialize(...)
return object
end
function RNTimer:Initialize(control, formatter, onCompleteFn)
self.control = control
local function DefaultFormatter(remaining)
return ZO_FormatTimeMilliseconds(remaining, TIME_FORMAT_STYLE_DESCRIPTIVE_MINIMAL_SHOW_TENTHS_SECS)
end
self.formatter = formatter or DefaultFormatter
self.onCompleteFn = onCompleteFn
self.startMs = 0
self.duration = 0
self.endMs = 0
table.insert(RNTimerList, self)
end
function RNTimer:GetTimerInfo()
return self.startMs, self.duration, self.endMs
end
function RNTimer:SetText(text)
self.control:SetText(text)
end
function RNTimer:Reset()
self.startMs = 0
self.duration = 0
self.endMs = 0
-- placed here since calling OnComplete _might_ become optional in the future
refCounter:Decr()
end
function RNTimer:OnComplete(forceStopped)
--TODO: hide control by default here?
if self.onCompleteFn then
self.onCompleteFn(self, forceStopped)
end
self:Reset()
end
function RNTimer:Start(startMs, duration, endMs)
-- increment counter only if we weren't already active
if self.startMs <= 0 then
refCounter:Incr()
end
-- endMs can be omitted
self.startMs = startMs
self.duration = duration
if endMs == nil then
endMs = startMs + duration
end
self.endMs = endMs
self:Update(startMs) -- use Update to apply (initial) layout
end
-- Use this to force a timer to stop
function RNTimer:Stop()
--only stop if we are active
if (self.startMs > 0) then
self:OnComplete(true) -- handles resetting
end
end
-- return: hasEnded
function RNTimer:Update(currentFrameTimeMs)
if (self.startMs > 0) then
local remaining = self.endMs - currentFrameTimeMs
if remaining <= 0 then
self:OnComplete() -- handles resetting
return true
else
self:SetText(self.formatter(remaining))
end
end
return false
end
end
-- --------------------
-- -- SCALDED DISPLAY
do --------------------
local display = nil -- the parent display
local timerObj = nil -- the timer object (NOT THE ACTUAL CONTROL)
local Util = RaidNotifier.Util
function UI.UpdateScaldedStacks(stacks, startTime, endTime)
display = display or UI.GetElement("hallsFab", "pinnacleBoss_scalded_display")
if not display then return end
if (stacks > 0) then
local r,g,b = Util.HSL2RGB((10 - stacks) / 30, 1, 0.5)
display.debuff:SetColor(r,g,b,1)
display.debuff:SetText(("%d%%"):format(stacks * 10))
display:SetHidden(false)
timerObj:Start(startTime*1000, nil, endTime*1000)
else
timerObj:Stop()
display:SetHidden(true)
end
end
function UI.InitializeScaldedDisplay(control)
display = control
display.debuff = display:GetNamedChild("Debuff")
display.timer = display:GetNamedChild("Timer")
local function OnComplete(control, forceStopped)
-- hide display (maybe not needed since we also listen to when the effect fades)
display:SetHidden(true)
end
timerObj = RNTimer:New(display.timer, nil, OnComplete)
end
end
-- -----------------
-- -- GLYPH WINDOW
do -----------------
local window = nil -- the window itself
local glyphTimers = {} -- the glyph objects (NOT THE ACTUAL CONTROLS)
-- GlyphTimer: handles showing and hiding of the overlay and timer
-- in addition to updating the timer itself.
local RNGlyphTimer = RNTimer:Subclass()
function RNGlyphTimer:New(...)
return RNTimer.New(self, ...)
end
function RNGlyphTimer:Initialize(control, formatter, onCompleteFn)
RNTimer.Initialize(self, control, formatter, onCompleteFn)
self.bg = control:GetNamedChild("BG")
self.overlay = control:GetNamedChild("Overlay")
self.timer = control:GetNamedChild("Timer")
-- show overlay when inactive
self.timer:SetHidden(true)
self.overlay:SetHidden(false)
end
function RNGlyphTimer:Reset()
RNTimer.Reset(self)
-- show overlay when inactive
self.timer:SetHidden(true)
self.overlay:SetHidden(false)
end
function RNGlyphTimer:Start(startMs, duration, endMs)
RNTimer.Start(self, startMs, duration, endMs) -- will call Update()
self.timer:SetHidden(false)
self.overlay:SetHidden(true)
end
function RNGlyphTimer:SetText(text)
self.timer:SetText(text)
end
-- Wrapper functions
function UI.StartGlyphTimer(index, cooldown)
window = window or UI.GetElement("mawLorkhaj", "zhaj_glyph_window")
if not window then return end
local glyph = glyphTimers[index]
if not glyph then return end
glyph:Start(GetFrameTimeMilliseconds(), cooldown)
end
function UI.StopGlyphTimer(index)
window = window or UI.GetElement("mawLorkhaj", "zhaj_glyph_window")
if not window then return end
local glyph = glyphTimers[index]
if not glyph then return end
glyph:Stop() -- just calls OnComplete, nothing special for now
end
-- Called upon loading and when the setting is changed
function UI.InvertGlyphs()
window = window or UI.GetElement("mawLorkhaj", "zhaj_glyph_window")
if not window then return end
for _, glyph in ipairs(glyphTimers) do
local _, _, _, _, offsetX, offsetY = glyph.control:GetAnchor(0)
glyph.control:SetAnchor(CENTER, nil, CENTER, -1*offsetX, -1*offsetY)
end
local label = window:GetNamedChild("Exit")
local _, point = label:GetAnchor(0)
label:ClearAnchors()
label:SetAnchor(point == TOP and BOTTOM or TOP, nil, point == TOP and BOTTOM or TOP)
end
-- Called upon registering; handles GlyphTimer object creation
function UI.InitializeGlyphs(control)
window = control
glyphTimers = {}
for i=1,7 do
glyphTimers[i] = RNGlyphTimer:New(window:GetNamedChild("Glyph"..i))
end
-- change the textures for the magical 'player glyph'
local playerGlyph = glyphTimers[7]
playerGlyph.bg:SetTexture("RaidNotifier/assets/white_circle.dds")
playerGlyph.overlay:SetTexture("RaidNotifier/assets/dummy.dds")
end
end