Repository navigation
Expand file tree
/
Copy pathCinematicCamera.lua
More file actions
313 lines (280 loc) · 11.6 KB
/
Copy pathCinematicCamera.lua
File metadata and controls
313 lines (280 loc) · 11.6 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
309
310
311
312
313
local ADDON, ns = ...
-- These are the same zoom curves Narcissus uses for its working photo-mode
-- composition. Retail also ignores the shoulder CVar while its two character
-- centering locks are enabled, so those CVars are leased only while the live
-- Frame Gambit offset is on.
local CURVE_DEFAULT = { 0.361, -0.1654 }
local CURVE_VISAGE_MALE = { 0.361, -0.1654 }
local CURVE_VISAGE_FEMALE = { 0.38, 0.0311 }
local CURVE_DRACTHYR = { 0.1704, 0.0749 }
local CURVE_MOUNTED = { 1.2495, -4 }
local MODEL_CURVE = {
[4207724] = CURVE_DRACTHYR,
[4220448] = CURVE_VISAGE_FEMALE,
[4395382] = CURVE_VISAGE_MALE,
}
local SHOULDER_MAX, ZOOM_EPSILON, VALUE_EPSILON = 5.5, 0.01, 0.0001
local CVAR_NAME = {
fov = "cameraFov",
shoulder = "test_cameraOverShoulder",
centered = "CameraKeepCharacterCentered",
reduced = "CameraReduceUnexpectedMovement",
}
local state = { active = false, offsetEnabled = false, refreshGeneration = 0 }
local modelProbe
local function IsSecret(value)
if type(issecretvalue) ~= "function" then return false end
local ok, secret = pcall(issecretvalue, value)
return ok and secret == true
end
local function Number(value)
if IsSecret(value) then return nil end
value = tonumber(value)
if not value or value ~= value or value == math.huge or value == -math.huge then return nil end
return value
end
local function GetCVarNumber(name)
if not C_CVar or type(C_CVar.GetCVar) ~= "function" then return nil end
local ok, value = pcall(C_CVar.GetCVar, name)
return ok and Number(value) or nil
end
local function SetCVarNumber(name, value)
value = Number(value)
if not value or not C_CVar or type(C_CVar.SetCVar) ~= "function" then return false end
return pcall(C_CVar.SetCVar, name, value)
end
local function NearlyEqual(left, right)
return left ~= nil and right ~= nil and math.abs(left - right) <= VALUE_EPSILON
end
local function LeaseCVar(name, value)
local lease = state[name]
if not lease then
local original = GetCVarNumber(CVAR_NAME[name])
if original == nil then return false, false end
lease = { original = original }
state[name] = lease
end
local live = GetCVarNumber(CVAR_NAME[name])
if NearlyEqual(lease.applied, value) and NearlyEqual(live, value) then return true, false end
if not SetCVarNumber(CVAR_NAME[name], value) then return false, false end
lease.applied = value
return true, true
end
local function RestoreCVar(name)
local lease = state[name]
if not lease then return true end
local live = GetCVarNumber(CVAR_NAME[name])
if live == nil then return false end
if not NearlyEqual(live, lease.applied) then state[name] = nil; return true end
if not SetCVarNumber(CVAR_NAME[name], lease.original) then return false end
state[name] = nil
return true
end
local function CameraZoom()
if type(GetCameraZoom) ~= "function" then return nil end
local ok, zoom = pcall(GetCameraZoom)
return ok and Number(zoom) or nil
end
local function PlayerRaceID()
if type(UnitRace) ~= "function" then return nil end
local ok, _, raceFile, raceID = pcall(UnitRace, "player")
if not ok or IsSecret(raceFile) or IsSecret(raceID) then return nil end
raceID = Number(raceID)
if raceFile == "Dracthyr" or raceID == 52 or raceID == 70 then return raceID or 52 end
return raceID
end
local function IsDracthyr()
local raceID = PlayerRaceID()
return raceID == 52 or raceID == 70
end
local function IsPlayerMounted()
if type(IsMounted) ~= "function" then return false end
local ok, mounted = pcall(IsMounted)
return ok and not IsSecret(mounted) and mounted == true
end
local function DracthyrVisageState()
if not IsDracthyr() or not C_UnitAuras or type(C_UnitAuras.WantsAlteredForm) ~= "function" then return nil end
local ok, wantsAltered = pcall(C_UnitAuras.WantsAlteredForm, "player")
if not ok or IsSecret(wantsAltered) or type(wantsAltered) ~= "boolean" then return nil end
-- Matches Narcissus' Retail adapter: false is Visage, true is Dracthyr.
return wantsAltered == false
end
local function EnsureModelProbe()
if modelProbe ~= nil then return modelProbe or nil end
if type(CreateFrame) ~= "function" then modelProbe = false; return nil end
local ok, probe = pcall(CreateFrame, "PlayerModel")
if not ok or not probe or type(probe.SetUnit) ~= "function"
or type(probe.GetModelFileID) ~= "function" then modelProbe = false; return nil end
if type(probe.Hide) == "function" then probe:Hide() end
modelProbe = probe
return probe
end
local function ModelCurve()
local probe = EnsureModelProbe()
if not probe then return nil end
if type(probe.ClearModel) == "function" then pcall(probe.ClearModel, probe) end
if not pcall(probe.SetUnit, probe, "player") then return nil end
local ok, modelID = pcall(probe.GetModelFileID, probe)
modelID = ok and Number(modelID) or nil
return modelID and MODEL_CURVE[modelID] or nil
end
local function CurrentCurve()
if IsPlayerMounted() then return CURVE_MOUNTED end
if IsDracthyr() then
local visage = DracthyrVisageState()
if visage == true then
if type(UnitSex) == "function" then
local ok, sex = pcall(UnitSex, "player")
if ok and not IsSecret(sex) and sex == 3 then return CURVE_VISAGE_FEMALE end
end
return CURVE_VISAGE_MALE
elseif visage == false then
return CURVE_DRACTHYR
end
return ModelCurve() or CURVE_VISAGE_MALE
end
return CURVE_DEFAULT
end
local function ShoulderForZoom(zoom)
zoom = Number(zoom)
if not zoom then return nil end
local curve = CurrentCurve()
return math.max(0, math.min(SHOULDER_MAX, zoom * curve[1] + curve[2]))
end
local function RequestCameraRefresh()
if type(CameraZoomIn) ~= "function" then return end
state.refreshGeneration = state.refreshGeneration + 1
local generation = state.refreshGeneration
local function Refresh()
if generation ~= state.refreshGeneration or not state.active or not state.offsetEnabled then return end
pcall(CameraZoomIn, 0)
end
if C_Timer and type(C_Timer.After) == "function" then C_Timer.After(0, Refresh) else Refresh() end
end
local function EnableOffset(refreshCamera)
local zoom = CameraZoom()
local shoulder = ShoulderForZoom(zoom)
if not shoulder then return false end
local okCentered, centeredChanged = LeaseCVar("centered", 0)
local okReduced, reducedChanged = LeaseCVar("reduced", 0)
local okShoulder, shoulderChanged = LeaseCVar("shoulder", shoulder)
if not (okCentered and okReduced and okShoulder) then return false end
state.lastZoom = zoom
if refreshCamera and (centeredChanged or reducedChanged or shoulderChanged) then RequestCameraRefresh() end
return true
end
local function DisableOffset()
state.refreshGeneration = state.refreshGeneration + 1
LeaseCVar("shoulder", 0)
RestoreCVar("centered")
RestoreCVar("reduced")
state.lastZoom = nil
return true
end
local function StopZoomWatcher()
if state.zoomTicker and type(state.zoomTicker.Cancel) == "function" then state.zoomTicker:Cancel() end
state.zoomTicker = nil
end
local function StartZoomWatcher()
StopZoomWatcher()
if not state.offsetEnabled or not C_Timer or type(C_Timer.NewTicker) ~= "function" then return end
state.zoomTicker = C_Timer.NewTicker(0.10, function()
if not state.active or not state.offsetEnabled then return end
local zoom = CameraZoom()
if zoom and (not state.lastZoom or math.abs(zoom - state.lastZoom) > ZOOM_EPSILON) then
-- The player's own zoom command already refreshed the camera.
EnableOffset(false)
end
end)
end
local function ApplyLiveOffset(enabled, refreshCamera)
if not state.active then return false end
state.offsetEnabled = enabled == true
if state.offsetEnabled then
local ok = EnableOffset(refreshCamera)
StartZoomWatcher()
return ok
end
StopZoomWatcher()
return DisableOffset()
end
function ns:GetCinematicCameraSettings()
local cinematic = FrameGambitDB and FrameGambitDB.cinematic or {}
return math.max(40, math.min(100, tonumber(cinematic.cameraFOV) or 90)), cinematic.cameraOffsetDefault == true
end
function ns:SetCinematicFOV(value)
if not FrameGambitDB then return false end
FrameGambitDB.cinematic = type(FrameGambitDB.cinematic) == "table" and FrameGambitDB.cinematic or {}
value = math.max(40, math.min(100, math.floor((tonumber(value) or 90) + 0.5)))
FrameGambitDB.cinematic.cameraFOV = value
if state.active then LeaseCVar("fov", value) end
return true
end
function ns:SetCinematicCameraOffsetDefault(enabled)
if not FrameGambitDB then return false end
FrameGambitDB.cinematic = type(FrameGambitDB.cinematic) == "table" and FrameGambitDB.cinematic or {}
enabled = enabled == true
FrameGambitDB.cinematic.cameraOffsetDefault = enabled
if state.active then ApplyLiveOffset(enabled, true) end
return true
end
function ns:IsCinematicCameraOffsetEnabled()
return state.active and state.offsetEnabled == true
end
function ns:AcquireCinematicCamera()
if not self:IsCinematicActive() then return false end
local fov, entryOffset = self:GetCinematicCameraSettings()
local entering = not state.active
if entering then state.active, state.offsetEnabled, state.lastZoom = true, entryOffset, nil end
LeaseCVar("fov", fov)
if state.offsetEnabled then EnableOffset(entering); StartZoomWatcher() else DisableOffset() end
return true
end
function ns:ReleaseCinematicCamera()
if not state.active and not state.fov and not state.shoulder
and not state.centered and not state.reduced then return false end
state.active, state.offsetEnabled = false, false
state.refreshGeneration = state.refreshGeneration + 1
StopZoomWatcher()
RestoreCVar("fov")
RestoreCVar("shoulder")
RestoreCVar("centered")
RestoreCVar("reduced")
state.lastZoom = nil
return true
end
function ns:RefreshCinematicCamera()
if self:IsCinematicActive() then return self:AcquireCinematicCamera() end
self:ReleaseCinematicCamera()
return true
end
function ns:RefreshCinematicCameraOffset()
if not state.active or not state.offsetEnabled then return false end
state.refreshGeneration = state.refreshGeneration + 1
local generation = state.refreshGeneration
local function Refresh()
if generation ~= state.refreshGeneration or not state.active or not state.offsetEnabled then return end
EnableOffset(true)
end
if C_Timer and type(C_Timer.After) == "function" then C_Timer.After(0, Refresh) else Refresh() end
return true
end
function ns:ToggleCinematicCameraOffset()
if not self:IsCinematicActive() then return false, "Turn on Cinematic Mode first." end
if not state.active then self:AcquireCinematicCamera() end
local enabled = not state.offsetEnabled
ApplyLiveOffset(enabled, true)
return true, enabled
end
local events = CreateFrame("Frame")
local modelEventRegistered = false
if type(events.RegisterUnitEvent) == "function" then
modelEventRegistered = pcall(events.RegisterUnitEvent, events, "UNIT_MODEL_CHANGED", "player")
end
if not modelEventRegistered and type(events.RegisterEvent) == "function" then
pcall(events.RegisterEvent, events, "UNIT_MODEL_CHANGED")
end
if type(events.RegisterEvent) == "function" then pcall(events.RegisterEvent, events, "PLAYER_MOUNT_DISPLAY_CHANGED") end
events:SetScript("OnEvent", function(_, _, unit)
if unit == nil or (not IsSecret(unit) and unit == "player") then ns:RefreshCinematicCameraOffset() end
end)