-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSFT_UI.lua
More file actions
235 lines (193 loc) · 7.38 KB
/
Copy pathSFT_UI.lua
File metadata and controls
235 lines (193 loc) · 7.38 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
local SFT = SamisFishTrackerAddon
local constants = SFT.constants
local function formatIconLabel(iconPath, amount)
return string.format("|t16:16:%s|t : %d", iconPath, amount)
end
local function formatRoeLabel(amount)
local roeRate = SFT.savedVariables and SFT.savedVariables.roeRate or constants.roeRate
return string.format("Est. |t16:16:%s|t : %d", constants.icons.roe, amount * roeRate)
end
local function getCatchHistory()
if not SFT.averageRateCatchHistory then
SFT.averageRateCatchHistory = {}
end
return SFT.averageRateCatchHistory
end
local function pruneCatchHistory(now, windowSeconds)
local catchHistory = getCatchHistory()
local cutoff = now - windowSeconds
while #catchHistory > 0 and catchHistory[1].timestamp <= cutoff do
table.remove(catchHistory, 1)
end
return catchHistory
end
function SFT.RecordFishCatch(amount)
local catchHistory = getCatchHistory()
catchHistory[#catchHistory + 1] = {
timestamp = os.time(),
amount = amount or 0,
}
end
local function formatAverageLabel()
local useRollingWindow = not SFT.savedVariables or SFT.savedVariables.averageRateUseRollingWindow ~= false
if useRollingWindow then
local windowSeconds = (SFT.savedVariables and SFT.savedVariables.averageRateRollingWindowSeconds) or 300
windowSeconds = math.max(1, math.min(3600, windowSeconds))
local now = os.time()
local catchHistory = pruneCatchHistory(now, windowSeconds)
local fishInWindow = 0
for i = 1, #catchHistory do
fishInWindow = fishInWindow + catchHistory[i].amount
end
local fishPerHour = (fishInWindow / windowSeconds) * 3600
return string.format("Avg/hr: %.1f", fishPerHour)
else
local sessionStartTime = SFT.sessionStartTime or os.time()
local elapsedSeconds = math.max(os.time() - sessionStartTime, 1)
local fishPerHour = (SFT.fishamount / elapsedSeconds) * 3600
return string.format("Avg/hr: %.1f", fishPerHour)
end
end
local function isAverageRateEnabled()
return not SFT.savedVariables or SFT.savedVariables.showAverageRate ~= false
end
function SFT.IsRoeTrackingEnabled()
return not SFT.savedVariables or SFT.savedVariables.enableRoeTracking ~= false
end
local function resizeFilletWindowToFitStats()
local control = SamisFilletTrackerControl
local label = SamisFilletTrackerControlLabelFilletStats
if not control or not label then
return
end
local horizontalPadding = 20
local minWidth = constants.filletWindowWidth or 300
label:SetWrapMode(TEXT_WRAP_MODE_TRUNCATE)
label:SetWidth(1000)
local textWidth = select(1, label:GetTextDimensions()) or 0
local desiredWidth = math.max(minWidth, math.ceil(textWidth) + horizontalPadding)
control:SetWidth(desiredWidth)
label:SetWidth(desiredWidth - horizontalPadding)
if SFT.filletWindowBackground then
SFT.filletWindowBackground:SetDimensions(desiredWidth, constants.filletWindowHeight)
end
end
function SFT.ApplyRoeTrackingVisibility()
local enabled = SFT.IsRoeTrackingEnabled()
SamisFishTrackerControlLabelRoe:SetHidden(not enabled)
SamisFishTrackerControlLabelBagRoe:SetHidden(not enabled)
SamisFishTrackerControlLabelBankRoe:SetHidden(not enabled)
SamisFilletTrackerControl:SetHidden(not enabled)
if enabled then
resizeFilletWindowToFitStats()
end
end
function SFT.InitializeBackground()
if not SFT.windowBackground then
local background = WINDOW_MANAGER:CreateControl(nil, SamisFishTrackerControl, CT_TEXTURE)
background:SetDimensions(200, constants.windowHeightFull)
background:SetAnchor(TOPLEFT, SamisFishTrackerControl, TOPLEFT, 0, 0)
background:SetDrawLevel(-1)
background:SetColor(0, 0, 0, 0.8)
SFT.windowBackground = background
end
if not SFT.windowSeparatorLine then
local separatorLine = WINDOW_MANAGER:CreateControl(nil, SamisFishTrackerControl, CT_TEXTURE)
separatorLine:SetColor(0.80, 0.80, 0.80, 1)
separatorLine:SetDimensions(170, 1)
separatorLine:SetAnchor(BOTTOM, SamisFishTrackerControl, BOTTOM, 0, -26)
SFT.windowSeparatorLine = separatorLine
end
if not SFT.filletWindowBackground then
local filletBg = WINDOW_MANAGER:CreateControl(nil, SamisFilletTrackerControl, CT_TEXTURE)
filletBg:SetDimensions(constants.filletWindowWidth, constants.filletWindowHeight)
filletBg:SetAnchor(TOPLEFT, SamisFilletTrackerControl, TOPLEFT, 0, 0)
filletBg:SetDrawLevel(-1)
filletBg:SetColor(0, 0, 0, 0.8)
SFT.filletWindowBackground = filletBg
end
end
function SFT.UpdateAverageRateLabel(forceUpdate)
local isEnabled = isAverageRateEnabled()
SamisFishTrackerControlLabelAverage:SetHidden(not isEnabled)
if not isEnabled then
return
end
local autoUpdateEnabled = not SFT.savedVariables or SFT.savedVariables.averageRateAutoUpdateEnabled ~= false
if not autoUpdateEnabled and not forceUpdate then
return
end
SamisFishTrackerControlLabelAverage:SetText(formatAverageLabel())
end
function SFT.UpdateFilletStatsLabel()
if not SFT.IsRoeTrackingEnabled() then
return
end
local sinceRoe = SFT.filletsSinceRoe or 0
local lastFillets = SFT.lastRoeFillets or 0
local observedPercent = SFT.lastRoeRatePercent or 0
SamisFilletTrackerControlLabelFilletStats:SetText(string.format("Fillets since Roe: %d (Last: %d, Obs: %.2f%%)",
sinceRoe,
lastFillets, observedPercent))
resizeFilletWindowToFitStats()
end
function SFT.ResizeWindow()
local bankHidden = SamisFishTrackerControlLabelBankFish:IsHidden()
local newHeight = bankHidden and constants.windowHeightCollapsed or constants.windowHeightFull
if not isAverageRateEnabled() then
newHeight = newHeight - 15
end
SamisFishTrackerControl:SetHeight(newHeight)
if SFT.windowBackground then
SFT.windowBackground:SetDimensions(200, newHeight)
end
end
function SFT.UpdateBankDisplay()
local roeEnabled = SFT.IsRoeTrackingEnabled()
if SFT.total_bank <= 0 then
SamisFishTrackerControlLabelBankFish:SetHidden(true)
SamisFishTrackerControlLabelBankRoe:SetHidden(true)
else
SamisFishTrackerControlLabelBankFish:SetHidden(false)
SamisFishTrackerControlLabelBankRoe:SetHidden(not roeEnabled)
SamisFishTrackerControlLabelBankFish:SetText(formatIconLabel(constants.icons.bank, SFT.total_bank))
if roeEnabled then
SamisFishTrackerControlLabelBankRoe:SetText(formatRoeLabel(SFT.total_bank))
end
end
SFT.ResizeWindow()
end
function SFT.RefreshStorageLabels()
local roeEnabled = SFT.IsRoeTrackingEnabled()
SFT.ApplyRoeTrackingVisibility()
SamisFishTrackerControlLabelBagFish:SetText(formatIconLabel(constants.icons.bag, SFT.total_bag))
if roeEnabled then
SamisFishTrackerControlLabelBagRoe:SetText(formatRoeLabel(SFT.total_bag))
end
if roeEnabled then
SFT.UpdateFilletStatsLabel()
end
SFT.UpdateAverageRateLabel()
SFT.UpdateBankDisplay()
end
function SFT.UpdateFishCount(count)
SFT.fishamount = count or 0
SamisFishTrackerControlLabelFish:SetText(formatIconLabel(constants.icons.fish, SFT.fishamount))
if SFT.IsRoeTrackingEnabled() then
SamisFishTrackerControlLabelRoe:SetText(formatRoeLabel(SFT.fishamount))
end
SFT.UpdateAverageRateLabel()
end
function SFT.UpdateFishAmount(amount)
local increment = amount or 0
SFT.UpdateFishCount(SFT.fishamount + increment)
SFT.total_bag = SFT.total_bag + increment
SFT.RefreshStorageLabels()
end
function SFT.ResetFishAmount()
SFT.sessionStartTime = os.time()
SFT.averageRateCatchHistory = {}
SFT.UpdateFishCount(0)
SFT.savedVariables.amount = 0
SFT.UpdateAverageRateLabel()
end