-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimers.lua
More file actions
308 lines (247 loc) · 8.75 KB
/
timers.lua
File metadata and controls
308 lines (247 loc) · 8.75 KB
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
local UIC_TIMERS = 12345677
local timersWindow
local timers = table.load(filePath)
local DAY = 86400
local HOUR = 3600
local MINUTE = 60
local SECOND = 1
---Creates a finished timer window.
---@return Window
local function CreateFinishedTimerWindow(timer)
local window = SetViewOfFinishedTimer(timer)
local titleBar = window.titleBar ---@type Window
local closeButton = titleBar.closeButton ---@type Button
window:SetSounds("bag")
window:SetCloseOnEscape(true)
window:EnableHidingIsRemove(true)
window:SetAlphaAnimation(0, 1, .1, .1)
window:SetStartAnimation(true, true)
window:SetUILayer("normal")
window:SetHandler("OnScale", function (self)
CorrectWidgetScreenPos(window)
end)
titleBar:SetHandler("OnDragStart", function ()
window:StartMoving()
end)
titleBar:SetHandler("OnDragStop", function ()
window:StopMovingOrSizing()
CorrectWidgetScreenPos(window)
end)
closeButton:SetHandler("OnClick", function ()
window:Show(false)
end)
local timeCheck = 0
window:SetHandler("OnUpdate", function (self, frameTime)
timeCheck = timeCheck + frameTime
if timeCheck < 1000 then
return
end
timeCheck = timeCheck % 1000
X2Sound:PlayUISound("event_commercial_mail_alarm")
end)
return window
end
function UpdateTimers()
for row, timer in ipairs(timers) do
if timer.startTime ~= nil and timer.pauseTime == nil then
local remainingTime = timer.startTime + timer.duration - os.time()
if remainingTime <= 0 then
timer.startTime = nil
timer.pauseTime = nil
local error = table.save(filePath, timers)
if error then
ADDON:ChatLog(error)
else
local finishedTimerWindow = CreateFinishedTimerWindow(timer)
finishedTimerWindow:Show(true)
end
if timersWindow ~= nil and timersWindow:IsVisible(true) then
timersWindow.contentFrame.scrollFrame.timersListCtrl:UpdateList()
end
elseif timersWindow ~= nil and timersWindow:IsVisible(true) then
local rowWindow = timersWindow.contentFrame.scrollFrame.timersListCtrl.items[row]
local checkButton, timerNameTextbox, timerDurationTextbox, refreshButton, deleteButton = unpack(
rowWindow.subItems
)
timerDurationTextbox:SetText(formatDHMS(remainingTime))
end
end
end
end
function EnableTimer(timer)
if timer.startTime == nil and timer.pauseTime == nil then
timer.startTime = os.time()
elseif timer.startTime ~= nil and timer.pauseTime == nil then
timer.pauseTime = os.time()
elseif timer.startTime ~= nil and timer.pauseTime ~= nil then
timer.startTime = timer.startTime + (os.time() - timer.pauseTime)
timer.pauseTime = nil
end
local error = table.save(filePath, timers)
if error then
ADDON:ChatLog(error)
else
timersWindow.contentFrame.scrollFrame.timersListCtrl:UpdateList()
end
end
---Formats seconds into day hour minute seconds.
---@param seconds number
---@return string
function formatDHMS(seconds)
if not seconds or seconds <= 0 then
return "0 s"
end
seconds = math.floor(seconds)
local totalDays = math.floor(seconds / DAY)
local totalHours = math.floor((seconds % DAY) / HOUR)
local totalMinutes = math.floor((seconds % HOUR) / MINUTE)
local totalSeconds = seconds % 60
local parts = {}
if totalDays > 0 then
table.insert(parts, totalDays .. " d")
end
if totalHours > 0 or totalDays > 0 then
table.insert(parts, totalHours .. " h")
end
if totalMinutes > 0 or totalHours > 0 or totalDays > 0 then
table.insert(parts, totalMinutes .. " m")
end
table.insert(parts, totalSeconds .. " s")
return table.concat(parts, " ")
end
---Creates a example addon window.
---@return Window
local function CreateTimersWindow()
local window = SetViewOfTimers(timers)
local titleBar = window.titleBar ---@type Window
local closeButton = titleBar.closeButton ---@type Button
local createTimerFrame = window.createTimerFrame ---@type EmptyWidget
local nameEditbox = createTimerFrame.nameEditbox ---@type X2Editbox
local dayEditbox = createTimerFrame.dayEditbox ---@type X2Editbox
local hourEditbox = createTimerFrame.hourEditbox ---@type X2Editbox
local minuteEditbox = createTimerFrame.minuteEditbox ---@type X2Editbox
local secondEditbox = createTimerFrame.secondEditbox ---@type X2Editbox
local createButton = createTimerFrame.createButton ---@type Button
local contentFrame = window.contentFrame ---@type EmptyWidget
local scrollFrame = contentFrame.scrollFrame ---@type EmptyWidget
local sliderFrame = contentFrame.sliderFrame ---@type EmptyWidget
local upButton = sliderFrame.upButton ---@type Button
local downButton = sliderFrame.downButton ---@type Button
local slider = sliderFrame.slider ---@type Slider
local timersListCtrl = scrollFrame.timersListCtrl ---@type ListCtrl
window:SetSounds("bag")
window:SetCloseOnEscape(true)
-- window:EnableHidingIsRemove(true)
window:SetAlphaAnimation(0, 1, .1, .1)
window:SetStartAnimation(true, true)
window:SetUILayer("normal")
window:SetHandler("OnScale", function (self)
CorrectWidgetScreenPos(window)
end)
titleBar:SetHandler("OnDragStart", function ()
window:StartMoving()
end)
titleBar:SetHandler("OnDragStop", function ()
window:StopMovingOrSizing()
CorrectWidgetScreenPos(window)
end)
closeButton:SetHandler("OnClick", function ()
window:Show(false)
end)
local function CreateTimer()
local timerName = nameEditbox:GetText()
if #timerName > 0 then
local timerDay = tonumber(dayEditbox:GetText()) or 0
local timerHour = tonumber(hourEditbox:GetText()) or 0
local timerMinute = tonumber(minuteEditbox:GetText()) or 0
local timerSecond = tonumber(secondEditbox:GetText()) or 0
local duration = timerDay * DAY + timerHour * HOUR + timerMinute * MINUTE + timerSecond * SECOND
if duration > 0 then
---@type Timer
local timer = {
name = timerName,
duration = duration
}
table.insert(timers, timer)
nameEditbox:SetText("")
dayEditbox:SetText("")
hourEditbox:SetText("")
minuteEditbox:SetText("")
secondEditbox:SetText("")
nameEditbox:ClearFocus()
dayEditbox:ClearFocus()
hourEditbox:ClearFocus()
minuteEditbox:ClearFocus()
secondEditbox:ClearFocus()
local error = table.save(filePath, timers)
if error then
ADDON:ChatLog(error)
else
timersListCtrl:UpdateList()
end
end
end
end
dayEditbox:SetHandler("OnEnterPressed", CreateTimer)
hourEditbox:SetHandler("OnEnterPressed", CreateTimer)
minuteEditbox:SetHandler("OnEnterPressed", CreateTimer)
secondEditbox:SetHandler("OnEnterPressed", CreateTimer)
createButton:SetHandler("OnClick", CreateTimer)
upButton:SetHandler("OnClick", function ()
slider:Up(WINDOW.ITEM_DIMENSION)
end)
slider:SetHandler("OnSliderChanged", function (self, value)
scrollFrame:ChangeChildAnchorByScrollValue("vert", value)
end)
downButton:SetHandler("OnClick", function ()
slider:Down(WINDOW.ITEM_DIMENSION)
end)
slider:SetPageStep(WINDOW.ITEM_DIMENSION)
slider:SetValueStep(WINDOW.ITEM_DIMENSION)
slider:SetFixedThumb(true)
contentFrame:SetHandler("OnWheelUp", function ()
slider:Up(WINDOW.ITEM_DIMENSION)
end)
contentFrame:SetHandler("OnWheelDown", function ()
slider:Down(WINDOW.ITEM_DIMENSION)
end)
window:SetHandler("OnShow", function (self)
UpdateTimers()
end)
return window
end
---Toggles the example addon window.
---@param show boolean|nil
local function ToggleTimersWindow(show)
-- If the window should be shown.
if show == nil then
show = timersWindow == nil or not timersWindow:IsVisible()
end
-- If the window should be shown and doesn't exist, create it.
if show == true and timersWindow == nil then
timersWindow = CreateTimersWindow()
timersWindow:SetDeletedHandler(function ()
timersWindow = nil
end)
end
-- If the window exists, Show or Hide it.
if timersWindow then
timersWindow:Show(show)
end
end
function CreateTimerNotificationWindow()
local timerNotifier = UIParent:CreateWidget("emptywidget", "timerNotifier", "UIParent")
timerNotifier:Show(true)
local timeCheck = 0
timerNotifier:SetHandler("OnUpdate", function (self, frameTime)
timeCheck = timeCheck + frameTime
if timeCheck < 1000 then
return
end
timeCheck = timeCheck % 1000
UpdateTimers()
end)
end
CreateTimerNotificationWindow()
ADDON:RegisterContentTriggerFunc(UIC_TIMERS, ToggleTimersWindow)
ADDON:AddEscMenuButton(4, UIC_TIMERS, "optimizer", locale.addon.name)