-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStateManager.lua
More file actions
230 lines (191 loc) · 7.15 KB
/
Copy pathStateManager.lua
File metadata and controls
230 lines (191 loc) · 7.15 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
--------------------------------------------------------------------------------
-- State Manager - Determines when UI should be visible
--------------------------------------------------------------------------------
local ZenHUD = _G.ZenHUD
local Config = ZenHUD.Config
local Utils = ZenHUD.Utils
local StateManager = {
inCombat = false,
hasLivingTarget = false,
isResting = false,
isMounted = false,
isDead = false,
onTaxi = false,
inVehicle = false,
-- Transition tracking
lastVisibilityDecision = nil, -- Track last show/hide decision to avoid redundant calls
isAFK = false,
mouseoverUI = false,
-- Grace period tracking
graceUntil = {
combat = 0,
target = 0,
mouseover = 0,
},
-- Zone debouncing
lastZoneTime = 0,
pendingZoneCheck = false,
zoneDebounceTimer = nil,
}
function StateManager:OnZoneChanged()
local ZONE_DEBOUNCE = 0.6
local now = Utils.GetTime()
-- If we're within debounce window, schedule delayed check
if now - self.lastZoneTime < ZONE_DEBOUNCE then
self.pendingZoneCheck = true
if not self.zoneDebounceTimer then
self.zoneDebounceTimer = CreateFrame("Frame")
end
local timeLeft = ZONE_DEBOUNCE - (now - self.lastZoneTime)
if timeLeft < 0.05 then timeLeft = 0.05 end
Utils.Print(string.format("Zone debounce: %.2fs", timeLeft), true)
Utils.After(timeLeft, function()
if self.pendingZoneCheck then
self.pendingZoneCheck = false
self:SetResting(IsResting())
end
end)
return
end
-- Outside debounce window - update immediately
self.lastZoneTime = now
self:SetResting(IsResting())
end
function StateManager:Update()
-- Don't run until addon is fully loaded
if not ZenHUD.loaded then return end
if not Config:Get("enabled") then return end
local time = Utils.GetTime()
local inGrace = false
local graceReason = nil
-- Check grace periods
for reason, deadline in pairs(self.graceUntil) do
if deadline > time then
inGrace = true
graceReason = reason
break
end
end
-- Determine visibility with clear priority
local shouldShow = self.inCombat
or (Config:Get("showOnTarget") and self.hasLivingTarget)
or self.mouseoverUI
or inGrace
or self.isResting
or self.inVehicle
or Config:ShouldShowInZone() -- Zone-based override (dungeons, raids, etc.)
-- Only call Show/Hide if decision has changed (avoid redundant calls)
if shouldShow ~= self.lastVisibilityDecision then
self.lastVisibilityDecision = shouldShow
if shouldShow then
local priority = self.inCombat or self.hasLivingTarget or self.mouseoverUI
Utils.Print(string.format("Showing UI (combat=%s, target=%s, mouseover=%s, grace=%s, resting=%s, vehicle=%s)",
tostring(self.inCombat), tostring(self.hasLivingTarget), tostring(self.mouseoverUI),
graceReason or "none", tostring(self.isResting), tostring(self.inVehicle)), true)
if ZenHUD.FrameManager then
ZenHUD.FrameManager:ShowAll(priority)
end
else
Utils.Print("Hiding UI", true)
if ZenHUD.FrameManager then
ZenHUD.FrameManager:HideAll()
end
end
end
end
function StateManager:SetCombat(inCombat)
self.inCombat = inCombat
if inCombat then
-- Entering combat - clear all grace periods for immediate UI response
for k in pairs(self.graceUntil) do
self.graceUntil[k] = 0
end
Utils.Print("Combat: ENTERING combat", true)
else
-- Leaving combat - start grace period with timer callback
local grace = Config:Get("gracePeriods").combat
self.graceUntil.combat = Utils.GetTime() + grace
Utils.Print(string.format("Combat: LEAVING combat, %.1fs grace period", grace), true)
-- Timer callback to hide UI after grace expires
Utils.After(grace, function()
-- Clear this grace period and force update
self.graceUntil.combat = 0
Utils.Print("Combat grace expired, updating visibility", true)
self:Update()
end)
end
self:Update()
end
function StateManager:SetTarget(hasTarget, isAlive)
local hadLivingTarget = self.hasLivingTarget
self.hasLivingTarget = hasTarget and isAlive
if hasTarget and isAlive then
-- Acquired living target - clear grace
self.graceUntil.target = 0
Utils.Print("Target: acquired living target", true)
elseif not hasTarget and hadLivingTarget then
-- Lost living target - start grace period with timer callback
local grace = Config:Get("gracePeriods").target
self.graceUntil.target = Utils.GetTime() + grace
Utils.Print(string.format("Target: lost target, %.1fs grace period", grace), true)
-- Timer callback to hide UI after grace expires
Utils.After(grace, function()
-- Clear this grace period and force update
self.graceUntil.target = 0
Utils.Print("Target grace expired, updating visibility", true)
self:Update()
end)
end
self:Update()
end
function StateManager:SetResting(isResting)
self.isResting = isResting
self:Update()
end
function StateManager:SetMounted(isMounted)
self.isMounted = isMounted
self:Update()
end
function StateManager:SetDead(isDead)
self.isDead = isDead
self:Update()
end
function StateManager:SetTaxi(onTaxi)
self.onTaxi = onTaxi
self:Update()
end
function StateManager:SetVehicle(inVehicle)
self.inVehicle = inVehicle
self:Update()
end
function StateManager:SetAFK(isAFK)
self.isAFK = isAFK
self:Update()
end
function StateManager:SetMouseover(mouseoverUI)
local wasMouseover = self.mouseoverUI
self.mouseoverUI = mouseoverUI
if mouseoverUI then
-- Entered UI - cancel grace period immediately for instant response
self.graceUntil.mouseover = 0
Utils.Print("Mouseover: entering UI area", true)
else
-- Left UI - start grace period with timer callback
if wasMouseover then
local grace = Config:Get("gracePeriods").mouseover
local now = Utils.GetTime()
self.graceUntil.mouseover = now + grace
Utils.Print(string.format("Mouseover: left UI, %.1fs grace period", grace), true)
-- Timer callback to hide UI after grace expires
Utils.After(grace, function()
-- Clear this grace period and force update
self.graceUntil.mouseover = 0
Utils.Print("Mouseover grace expired, updating visibility", true)
self:Update()
end)
end
end
self:Update()
end
-- Export to ZenHUD namespace
ZenHUD.StateManager = StateManager