-
Notifications
You must be signed in to change notification settings - Fork 7
/
line_indicator.lua
468 lines (386 loc) · 16.5 KB
/
line_indicator.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
local detailsFramework = _G ["DetailsFramework"]
if (not detailsFramework or not DetailsFrameworkCanLoad) then
return
end
local _
---@class df_lineindicator_data : table
---@field value number
---@field valueType "PERCENT"|"TIME"|"PIXELS"
---@field width number
---@field color number[]
---@field alpha number
---@field onClick fun(self:df_lineindicator_line)
---@field onEnter fun(self:df_lineindicator_line)
---@field onLeave fun(self:df_lineindicator_line)
---@class df_lineindicator_line : button
---@field xOffset number
---@field left number
---@field index number
---@field data df_lineindicator_data
---@field Texture texture
---@param self df_lineindicator
---@param indicator df_lineindicator_line
local lineIndicator_GetValueForMoving = function(self, indicator, leftDiff)
local targetFrame = self:LineIndicatorGetTarget()
local data = indicator.data
if (data.valueType == "PERCENT") then
local effectiveWidth = targetFrame:GetWidth() - self.lineIndicatorXOffset
local x = indicator:GetLeft() - self.lineIndicatorXOffset
local percent = x / effectiveWidth
data.value = percent
return data.value
elseif (data.valueType == "TIME") then
local pixelPerSecond = self.lineIndicatorPixelPerSecond
if (leftDiff) then --leftDiff is the amount of pixels the indicator has moved
--scale the pixels per second with the scale set
pixelPerSecond = pixelPerSecond * self.lineIndicatorScale
--get the time difference in seconds by dividing the pixels moved by the pixels per second
local timeDiff = leftDiff / pixelPerSecond
--add the time difference to the current value
data.value = data.value + timeDiff
return data.value
else
local effectiveWidth = targetFrame:GetWidth() - self.lineIndicatorXOffset
local indicatorXOffset = indicator:GetLeft() - self.lineIndicatorXOffset
local timePercent = indicatorXOffset / effectiveWidth
data.value = timePercent * self.lineIndicatorTotalTime
return data.value
end
elseif (data.valueType == "PIXELS") then
local x = indicator:GetLeft() - self.lineIndicatorXOffset
data.value = x
return data.value
end
end
---@class df_lineindicator : table, frame
---@field lineIndicatorTotalTime number
---@field lineIndicatorXOffset number
---@field lineIndicators df_pool
---@field lineIndicatorData table
---@field lineIndicatorValueType string
---@field lineIndicatorFrameTarget frame
---@field lineIndicatorScale number
---@field lineIndicatorLineHeight number
---@field lineIndicatorLineWidth number
---@field lineIndicatorPixelPerSecond number
---@field lineIndicatorMouseEnabled boolean
---@field lineIndicatorColor any
---@field lineIndicatorValueFontString fontstring
---@field LineIndicatorConstructor fun(self:df_lineindicator)
---@field LineIndicatorSetTarget fun(self:df_lineindicator, frameTarget:frame)
---@field LineIndicatorsReset fun(self:df_lineindicator)
---@field LineIndicatorCreateLine fun(self:df_lineindicator, index:number):df_lineindicator_line
---@field LineIndicatorGetLine fun(self:df_lineindicator):df_lineindicator_line
---@field LineIndicatorSetElapsedTime fun(self:df_lineindicator, totalTime:number)
---@field LineIndicatorSetLinePosition fun(self:df_lineindicator, line:df_lineindicator_line, value:number, valueType:string)
---@field LineIndicatorSetValueType fun(self:df_lineindicator, valueType:"PERCENT"|"TIME"|"PIXELS")
---@field LineIndicatorAddData fun(self:df_lineindicator, data:df_lineindicator_data)
---@field LineIndicatorSetData fun(self:df_lineindicator, data:df_lineindicator_data[])
---@field LineIndicatorRemoveData fun(self:df_lineindicator, dataId:number|df_lineindicator_data)
---@field LineIndicatorAddLine fun(self:df_lineindicator, value:number, valueType:string) : df_lineindicator_line
---@field LineIndicatorSetXOffset fun(self:df_lineindicator, xOffset:number)
---@field LineIndicatorSetScale fun(self:df_lineindicator, scale:number)
---@field LineIndicatorRefresh fun(self:df_lineindicator)
---@field LineIndicatorSetAllLinesWidth fun(self:df_lineindicator, width:number)
---@field LineIndicatorSetAllLinesHeight fun(self:df_lineindicator, height:number) set the height of all lines
---@field LineIndicatorSetAllLinesColor fun(self:df_lineindicator, color:any, g:number?, b:number?)
---@field LineIndicatorSetLineWidth fun(self:df_lineindicator, dataId:number|df_lineindicator_data, newWidth:number)
---@field LineIndicatorSetLineColor fun(self:df_lineindicator, dataId:number|df_lineindicator_data, color:any, g:number?, b:number?)
---@field LineIndicatorSetLineAlpha fun(self:df_lineindicator, dataId:number|df_lineindicator_data, alpha:number)
---@field LineIndicatorGetTarget fun(self:df_lineindicator):frame
---@field LineIndicatorSetPixelsPerSecond fun(self:df_lineindicator, pixelsPerSecond:number)
detailsFramework.LineIndicatorMixin = {
LineIndicatorConstructor = function(self)
self.lineIndicatorTotalTime = 0
self.lineIndicators = detailsFramework:CreatePool(detailsFramework.LineIndicatorMixin.LineIndicatorCreateLine, self)
self.lineIndicators:SetOnReset(function(lineIndicator) lineIndicator:Hide() lineIndicator:ClearAllPoints() end)
self.lineIndicatorFrameTarget = nil
self.lineIndicatorData = {}
self.lineIndicatorValueType = "PIXELS"
self.lineIndicatorXOffset = 0
self.lineIndicatorScale = 1
self.lineIndicatorLineHeight = 50
self.lineIndicatorLineWidth = 3
self.lineIndicatorColor = {1, 1, 1}
self.lineIndicatorPixelPerSecond = 20
self.lineIndicatorMouseEnabled = true
end,
LineIndicatorSetTarget = function(self, frameTarget)
self.lineIndicatorFrameTarget = frameTarget
end,
LineIndicatorGetTarget = function(self)
return self.lineIndicatorFrameTarget or self
end,
LineIndicatorSetPixelsPerSecond = function(self, pixelsPerSecond)
self.lineIndicatorPixelPerSecond = pixelsPerSecond
end,
--hide all indicators and clear their points
---@param self df_lineindicator
LineIndicatorsReset = function(self)
self.lineIndicatorTotalTime = 0
self.lineIndicators:Reset()
end,
---@param pool df_pool
---@param self df_lineindicator
---@return df_lineindicator_line
LineIndicatorCreateLine = function(pool, self)
local index = pool:GetAmount() + 1
local parentName = self:GetName()
local indicatorName = parentName and parentName .. "LineIndicator" .. index
local targetFrame = self:LineIndicatorGetTarget()
---@type df_lineindicator_line
local indicator = CreateFrame("button", indicatorName, targetFrame, "BackdropTemplate")
indicator:SetSize(3, targetFrame:GetParent():GetHeight())
indicator:SetFrameLevel(targetFrame:GetFrameLevel() + 10)
local texture = indicator:CreateTexture(nil, "background")
texture:SetColorTexture(1, 1, 1, 1)
texture:SetAllPoints()
indicator:SetMovable(true)
indicator:RegisterForDrag("LeftButton")
indicator:SetScript("OnMouseDown", function()
indicator.left = indicator:GetLeft()
end)
indicator:SetScript("OnDragStart", function()
indicator:StartMoving()
indicator:SetUserPlaced(false)
if (not self.lineIndicatorValueFrame) then
self.lineIndicatorValueFrame = CreateFrame("frame", nil, self)
self.lineIndicatorValueFrame:SetSize(100, 20)
self.lineIndicatorValueFrame:SetFrameLevel(self:GetFrameLevel() + 11)
local valueString = self.lineIndicatorValueFrame:CreateFontString(nil, "overlay", "GameFontNormal")
valueString:SetPoint("left", self.lineIndicatorValueFrame, "left", 2, 0)
self.lineIndicatorValueFrame.lineIndicatorValueFontString = valueString
valueString.Background = self.lineIndicatorValueFrame:CreateTexture(nil, "artwork")
valueString.Background:SetColorTexture(0, 0, 0, 0.7)
valueString.Background:SetPoint("topleft", valueString, "topleft", -2, 4)
valueString.Background:SetPoint("bottomright", valueString, "bottomright", 2, -4)
end
self.lineIndicatorValueFrame:Show()
self.lineIndicatorValueFrame:ClearAllPoints()
--self.lineIndicatorValueFrame:SetPoint("bottomleft", indicator, "bottomright", 2, 0)
local leftValue = indicator.left
indicator:SetScript("OnUpdate", function()
local newLeftValue = indicator:GetLeft()
local leftDiff = newLeftValue - leftValue --how much the indicator was moved
local value = lineIndicator_GetValueForMoving(self, indicator, leftDiff)
leftValue = newLeftValue
indicator.left = newLeftValue
--detailsFramework:DebugVisibility(self.lineIndicatorValueFrame)
self.lineIndicatorValueFrame:SetPoint("topleft", targetFrame, "topleft", newLeftValue + 2, -2)
if (indicator.data.valueType == "TIME") then
self.lineIndicatorValueFrame.lineIndicatorValueFontString:SetText(detailsFramework:IntegerToTimer(value))
elseif (indicator.data.valueType == "PERCENT") then
self.lineIndicatorValueFrame.lineIndicatorValueFontString:SetText(format("%.2f%%", value * 100))
elseif (indicator.data.valueType == "PIXELS") then
self.lineIndicatorValueFrame.lineIndicatorValueFontString:SetText(value)
end
end)
end)
indicator:SetScript("OnDragStop", function()
if (self.lineIndicatorValueFrame) then
self.lineIndicatorValueFrame:Hide()
end
indicator:SetScript("OnUpdate", nil)
indicator:StopMovingOrSizing()
--[=[ not over engineering this for now
--need to auto scroll the horizontal scroll frame if the indicator is moved
--get the amount of width that the horizontal scroll frame has scrolled
local horizontalScrolled = 0
if (self.GetHorizontalScrolledWidth) then
horizontalScrolled = self:GetHorizontalScrolledWidth() or 0
end
--add the amount of width that the horizontal scroll frame has scrolled to the indicator left position
if (horizontalScrolled ~= 0) then
local diff = indicator.left + horizontalScrolled
local value = lineIndicator_GetValueForMoving(self, indicator, diff)
end
--]=]
self:LineIndicatorRefresh()
end)
indicator.Texture = texture
return indicator
end,
LineIndicatorGetLine = function(self)
assert(self.lineIndicators, "LineIndicatorGetLine(): LineIndicatorConstructor() not called.")
local thisIndicator = self.lineIndicators:Acquire()
return thisIndicator
end,
LineIndicatorSetElapsedTime = function(self, totalTime)
self.lineIndicatorTotalTime = totalTime
end,
LineIndicatorSetLinePosition = function(self, line, value, valueType)
local targetFrame = self:LineIndicatorGetTarget()
if (valueType) then
if (valueType == "PERCENT") then
local effectiveWidth = targetFrame:GetWidth() - self.lineIndicatorXOffset
--effectiveWidth = effectiveWidth * self.lineIndicatorScale
local x = effectiveWidth * value
line:ClearAllPoints()
line:SetPoint("topleft", targetFrame, "topleft", self.lineIndicatorXOffset + x, 0)
line.xOffset = x
elseif (valueType == "TIME") then
assert(self.lineIndicatorTotalTime > 0, "LineIndicatorSetElapsedTime(self, totalTime) must be called before SetLineIndicatorPosition() with valueType TIME.")
local timePercent = value / self.lineIndicatorTotalTime
local effectiveWidth = targetFrame:GetWidth() - self.lineIndicatorXOffset
--effectiveWidth = effectiveWidth * self.lineIndicatorScale
local x = effectiveWidth * timePercent
line:ClearAllPoints()
line:SetPoint("left", targetFrame, "left", self.lineIndicatorXOffset + x, 0)
line:SetHeight(GetScreenHeight())
line.xOffset = x
line.left = line:GetLeft()
elseif (valueType == "PIXELS") then
--value = value * self.lineIndicatorScale
line:ClearAllPoints()
line:SetPoint("topleft", targetFrame, "topleft", self.lineIndicatorXOffset + value, 0)
line.xOffset = x
end
end
end,
LineIndicatorRefresh = function(self)
--release all objects
self.lineIndicators:Reset()
--redraw all objects
for i = 1, #self.lineIndicatorData do
local data = self.lineIndicatorData[i]
if (not data.valueType) then
data.valueType = self.lineIndicatorValueType
end
local line = self:LineIndicatorAddLine(data.value, data.valueType)
line.index = i
line.data = data
if (self.lineIndicatorLineHeight == -1) then
line:SetHeight(GetScreenHeight() * 2)
else
line:SetHeight(self.lineIndicatorLineHeight)
end
line:SetWidth(data.width or self.lineIndicatorLineWidth)
line:SetAlpha(data.alpha or 1)
line.Texture:SetVertexColor(unpack(self.lineIndicatorColor))
line:SetScript("OnClick", data.onClick)
line:SetScript("OnEnter", data.onEnter)
line:SetScript("OnLeave", data.onLeave)
if (self.lineIndicatorMouseEnabled) then
end
end
end,
LineIndicatorSetValueType = function(self, valueType)
assert(valueType == "PERCENT" or valueType == "TIME" or valueType == "PIXELS", "SetLineIndicatorValueType(valueType): valueType must be PERCENT, TIME or PIXELS.")
self.lineIndicatorValueType = valueType
end,
LineIndicatorAddLine = function(self, value, valueType)
local line = self:LineIndicatorGetLine()
self:LineIndicatorSetLinePosition(line, value, valueType or self.lineIndicatorValueType)
line:Show()
return line
end,
LineIndicatorRemoveData = function(self, dataId)
assert(type(dataId) == "number" or type(dataId) == "table", "LineIndicatorRemoveData(dataId): dataId must be the data index or a data table.")
if (type(dataId) == "number") then
local index = dataId
table.remove(self.lineIndicatorData, index)
elseif (type(dataId) == "table") then
local dataTable = dataId
for i = 1, #self.lineIndicatorData do
if (self.lineIndicatorData[i] == dataTable) then
table.remove(self.lineIndicatorData, i)
break
end
end
end
self:LineIndicatorRefresh()
end,
LineIndicatorAddData = function(self, data)
self.lineIndicatorData[#self.lineIndicatorData+1] = data
self:LineIndicatorRefresh()
end,
LineIndicatorSetData = function(self, data)
self.lineIndicatorData = data
self:LineIndicatorRefresh()
end,
LineIndicatorSetXOffset = function(self, xOffset)
self.lineIndicatorXOffset = xOffset
end,
LineIndicatorSetScale = function(self, scale)
self.lineIndicatorScale = scale
end,
LineIndicatorSetAllLinesHeight = function(self, height)
assert(type(height) == "number", "LineIndicatorSetAllLinesHeight(height): height must be a number.")
self.lineIndicatorLineHeight = height
self:LineIndicatorRefresh()
end,
LineIndicatorSetAllLinesWidth = function(self, width)
assert(type(width) == "number", "LineIndicatorSetAllLinesWidth(width): width must be a number.")
self.lineIndicatorLineWidth = width
self:LineIndicatorRefresh()
end,
LineIndicatorSetLineWidth = function(self, dataId, newWidth)
assert(type(dataId) == "number" or type(dataId) == "table", "LineIndicatorSetLineWidth(dataId): dataId must be the data index or a data table.")
if (type(dataId) == "number") then
local index = dataId
local data = self.lineIndicatorData[index]
if (data) then
data.width = newWidth
end
elseif (type(dataId) == "table") then
local dataTable = dataId
for i = 1, #self.lineIndicatorData do
if (self.lineIndicatorData[i] == dataTable) then
self.lineIndicatorData[i].width = newWidth
break
end
end
end
self:LineIndicatorRefresh()
end,
LineIndicatorSetAllLinesColor = function(self, color, g, b)
local r, g, b = detailsFramework:ParseColors(color, g, b)
self.lineIndicatorColor[1] = r
self.lineIndicatorColor[2] = g
self.lineIndicatorColor[3] = b
self:LineIndicatorRefresh()
end,
LineIndicatorSetLineColor = function(self, dataId, color, g, b)
assert(type(dataId) == "number" or type(dataId) == "table", "LineIndicatorSetLineColor(dataId): dataId must be the data index or a data table.")
local r, g, b = detailsFramework:ParseColors(color, g, b)
if (type(dataId) == "number") then
local index = dataId
local data = self.lineIndicatorData[index]
if (data) then
data.color[1] = r
data.color[2] = g
data.color[3] = b
end
elseif (type(dataId) == "table") then
local dataTable = dataId
for i = 1, #self.lineIndicatorData do
if (self.lineIndicatorData[i] == dataTable) then
self.lineIndicatorData[i].color[1] = r
self.lineIndicatorData[i].color[2] = g
self.lineIndicatorData[i].color[3] = b
break
end
end
end
self:LineIndicatorRefresh()
end,
LineIndicatorSetLineAlpha = function(self, dataId, alpha)
assert(type(dataId) == "number" or type(dataId) == "table", "LineIndicatorSetLineAlpha(dataId): dataId must be the data index or a data table.")
if (type(dataId) == "number") then
local index = dataId
local data = self.lineIndicatorData[index]
if (data) then
data.alpha = alpha
end
elseif (type(dataId) == "table") then
local dataTable = dataId
for i = 1, #self.lineIndicatorData do
if (self.lineIndicatorData[i] == dataTable) then
self.lineIndicatorData[i].alpha = alpha
break
end
end
end
self:LineIndicatorRefresh()
end,
}