-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathMouseZoom.lua
More file actions
515 lines (385 loc) · 18.1 KB
/
Copy pathMouseZoom.lua
File metadata and controls
515 lines (385 loc) · 18.1 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
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
local LibCamera = LibStub("LibCamera-1.0")
local LibEasing = LibStub("LibEasing-1.0")
local L = LibStub("AceLocale-3.0"):GetLocale("DynamicCam")
------------
-- LOCALS --
------------
local function Round(num, numDecimalPlaces)
local mult = 10^(numDecimalPlaces or 0)
return math.floor(num * mult + 0.5) / mult
end
-----------------------
-- NON-REACTIVE ZOOM --
-----------------------
-- To indicate if a non-reactive zoom is in progress.
-- This is needed to correct reactiveZoomTarget in case the target is missed.
local nonReactiveZoomStarted = false
local nonReactiveZoomInProgress = false
local nonReactiveZoomStartValue = GetCameraZoom()
local OldCameraZoomIn = CameraZoomIn
local OldCameraZoomOut = CameraZoomOut
local function NonReactiveZoom(zoomIn, increments)
-- print("NonReactiveZoom", zoomIn, increments, GetCameraZoom())
-- Stop zooming that might currently be in progress from a situation change.
LibCamera:StopZooming(true)
-- If we are not using this from within ReactiveZoom, we can also use the increment multiplier here.
if not DynamicCam:GetSettingsValue(DynamicCam.currentSituationID, "reactiveZoomEnabled") then
increments = increments + DynamicCam:GetSettingsValue(DynamicCam.currentSituationID, "reactiveZoomAddIncrementsAlways")
else
-- print("NonReactiveZoom starting", GetCameraZoom(), GetTime())
nonReactiveZoomStarted = true
nonReactiveZoomInProgress = false
nonReactiveZoomStartValue = GetCameraZoom()
end
if zoomIn then
OldCameraZoomIn(increments)
else
OldCameraZoomOut(increments)
end
end
local function NonReactiveZoomIn(increments)
-- No idea, why WoW does in-out-in-out with increments 0 after each mouse wheel turn.
if increments == 0 then return end
NonReactiveZoom(true, increments)
end
local function NonReactiveZoomOut(increments)
-- No idea, why WoW does in-out-in-out with increments 0 after each mouse wheel turn.
if increments == 0 then return end
NonReactiveZoom(false, increments)
end
-------------------
-- REACTIVE ZOOM --
-------------------
local reactiveZoomTarget = nil
-- We have to be able to set this to nil whenever
-- SetZoom() or SetView() happens. Otherwise, the
-- next scroll wheel turn will scroll back
-- to the last zoom position.
function DynamicCam:ResetReactiveZoomTarget()
reactiveZoomTarget = nil
end
-- This is needed to correct reactiveZoomTarget in case the target is missed.
local lastZoomForCorrection = GetCameraZoom()
local function ReactiveZoomTargetCorrectionFunction(_, elapsed)
if not DynamicCam:GetSettingsValue(DynamicCam.currentSituationID, "reactiveZoomEnabled") then return end
local currentZoom = GetCameraZoom()
if nonReactiveZoomStarted and nonReactiveZoomStartValue ~= currentZoom then
-- print("NonReactiveZoom just Started", nonReactiveZoomStartValue, GetTime())
nonReactiveZoomInProgress = true
nonReactiveZoomStarted = false
elseif nonReactiveZoomInProgress and lastZoomForCorrection == currentZoom then
-- print("NonReactiveZoom finished", GetTime())
nonReactiveZoomInProgress = false
end
if not LibCamera:IsZooming() and not nonReactiveZoomStarted and not nonReactiveZoomInProgress and reactiveZoomTarget ~= currentZoom then
-- print("Correcting reactiveZoomTarget", reactiveZoomTarget, "to", currentZoom, GetTime())
reactiveZoomTarget = currentZoom
end
lastZoomForCorrection = currentZoom
end
local reactiveZoomTargetCorrectionFrame = CreateFrame("Frame")
local function ReactiveZoom(zoomIn, increments)
-- print("ReactiveZoom", zoomIn, increments, reactiveZoomTarget)
increments = increments or 1
-- If this is a "mouse wheel" CameraZoomIn/CameraZoomOut, increments is 1.
-- Unlike a CameraZoomIn/CameraZoomOut from within LibCamera.SetZoomUsingCVar().
if increments == 1 then
local currentZoom = GetCameraZoom()
local addIncrementsAlways = DynamicCam:GetSettingsValue(DynamicCam.currentSituationID, "reactiveZoomAddIncrementsAlways")
local addIncrements = DynamicCam:GetSettingsValue(DynamicCam.currentSituationID, "reactiveZoomAddIncrements")
local maxZoomTime = DynamicCam:GetSettingsValue(DynamicCam.currentSituationID, "reactiveZoomMaxZoomTime")
local incAddDifference = DynamicCam:GetSettingsValue(DynamicCam.currentSituationID, "reactiveZoomIncAddDifference")
local easingFunc = DynamicCam:GetSettingsValue(DynamicCam.currentSituationID, "reactiveZoomEasingFunc")
-- scale increments up
increments = increments + addIncrementsAlways
if reactiveZoomTarget and math.abs(reactiveZoomTarget - currentZoom) > incAddDifference then
increments = increments + addIncrements
end
-- if we've changed directions, make sure to reset
if zoomIn then
if reactiveZoomTarget and reactiveZoomTarget > currentZoom then
reactiveZoomTarget = nil
end
else
if reactiveZoomTarget and reactiveZoomTarget < currentZoom then
reactiveZoomTarget = nil
end
end
-- if there is already a target zoom, base off that one, or just use the current zoom
reactiveZoomTarget = reactiveZoomTarget or currentZoom
if zoomIn then
if reactiveZoomTarget - increments < 0 then
-- Also update the increments if we need to make a NonReactiveZoom below,
-- in case of "zoomTime < secondsPerFrame": the raw increments may be
-- fewer than the distance still to go, which would stop short of first
-- person. (Not needed when the target is already 0, as any zoom-in from
-- there lands on 0 anyway.)
if reactiveZoomTarget > 0 then
increments = currentZoom
end
-- print("go to 0")
reactiveZoomTarget = 0
else
-- No math.max() needed: the branch above covers going below 0.
reactiveZoomTarget = reactiveZoomTarget - increments
end
-- zoom out
else
-- From first person go directly into closest third person.
if currentZoom == 0 then
-- print("Giving this to non-reactive zoom")
NonReactiveZoomOut(0.05)
return
else
reactiveZoomTarget = math.min(GetCVar("cameraDistanceMaxZoomFactor")*15, reactiveZoomTarget + increments)
end
end
-- if we don't need to zoom because we're at the max limits, then don't
if (reactiveZoomTarget == DynamicCam.cameraDistanceMaxZoomFactor_max and currentZoom == DynamicCam.cameraDistanceMaxZoomFactor_max) or (reactiveZoomTarget == 0 and currentZoom == 0) then
return
end
-- get the current time to zoom if we were going linearly or use maxZoomTime, if that's too high
local zoomTime = math.min(maxZoomTime, math.abs(reactiveZoomTarget - currentZoom) / tonumber(GetCVar("cameraZoomSpeed")) )
-- print ("Want to get from", currentZoom, "to", reactiveZoomTarget, "in", zoomTime, "with one frame being", DynamicCam.secondsPerFrame)
if zoomTime < DynamicCam.secondsPerFrame then
-- print("No easing for you", zoomTime, DynamicCam.secondsPerFrame, increments)
if zoomIn then
NonReactiveZoomIn(increments)
else
NonReactiveZoomOut(increments)
end
else
-- print("REACTIVE ZOOM start", GetTime())
-- LibCamera:SetZoom(reactiveZoomTarget, zoomTime, LibEasing[easingFunc], function() print("REACTIVE ZOOM end", GetTime()) end)
LibCamera:SetZoom(reactiveZoomTarget, zoomTime, LibEasing[easingFunc])
end
else
-- Called from within LibCamera.SetZoomUsingCVar(), through SetZoom() when the target zoom was missed.
-- print("...this is no mouse wheel call!", increments)
if zoomIn then
NonReactiveZoomIn(increments)
else
NonReactiveZoomOut(increments)
end
end
end
local function ReactiveZoomIn(increments)
-- No idea, why WoW does in-out-in-out with increments 0 after each mouse wheel turn.
if increments == 0 then return end
ReactiveZoom(true, increments)
end
local function ReactiveZoomOut(increments)
-- No idea, why WoW does in-out-in-out with increments 0 after each mouse wheel turn.
if increments == 0 then return end
ReactiveZoom(false, increments)
end
function DynamicCam:ReactiveZoomOn()
-- print("ReactiveZoomOn()")
if CameraZoomIn == ReactiveZoomIn then
-- print("already on")
return
end
CameraZoomIn = ReactiveZoomIn
CameraZoomOut = ReactiveZoomOut
reactiveZoomTarget = GetCameraZoom()
reactiveZoomTargetCorrectionFrame:SetScript("OnUpdate", ReactiveZoomTargetCorrectionFunction)
end
function DynamicCam:ReactiveZoomOff()
-- print("ReactiveZoomOff()")
if CameraZoomIn == NonReactiveZoomIn then
-- print("already off")
return
end
CameraZoomIn = NonReactiveZoomIn
CameraZoomOut = NonReactiveZoomOut
reactiveZoomTarget = nil
reactiveZoomTargetCorrectionFrame:SetScript("OnUpdate", nil)
end
function DynamicCam:ReactiveZoomIsOn()
return CameraZoomIn == ReactiveZoomIn
end
------------------------------------
-- ReactiveZoom Visual Aid (RZVA) --
------------------------------------
-- RZVA Colors
local RZVA_COLORS = {
currentZoom = {1, 0.2, 0.2, 1}, -- Red for current zoom
targetZoom = {0.2, 0.2, 1, 1}, -- Blue for target
increment = {1, 1, 0, 1}, -- Yellow for increment
gridLabel = {0.7, 0.7, 0.7},
gridMajor = {0.4, 0.4, 0.5, 0.6},
disabled = {0.3, 0.3, 0.3, 1},
}
-- RZVA Dimensions. The width has to leave room left of the graph for the y-axis
-- labels, which hang off it inside the content area.
local RZVA_WIDTH = 180
local RZVA_HEIGHT = 410
local RZVA_GRAPH_WIDTH = 120
local RZVA_GRAPH_HEIGHT = 250
local RZVA_GRAPH_HALF_WIDTH = RZVA_GRAPH_WIDTH / 2
-- Gaps between the window edge and the chrome's content area (Ui/Window.lua).
-- The bottom is tighter than the chrome's default: with no inner nine-slice
-- there is no border down there for the instructions text to clear.
local RZVA_GAPS = {top = 28, bottom = 14, left = 22, right = 18}
local rzvaFrame = nil
local lastReactiveZoomTarget = reactiveZoomTarget
local reactiveZoomGraphUpdateFrame = CreateFrame("Frame")
local function RZVADrawLine(parent, startX, startY, endX, endY, thickness, r, g, b, a)
local line = parent:CreateLine()
line:SetThickness(thickness)
line:SetColorTexture(r, g, b, a)
line:SetStartPoint("BOTTOMLEFT", parent, startX, startY)
line:SetEndPoint("BOTTOMLEFT", parent, endX, endY)
return line
end
local function ReactiveZoomGraphUpdateFunction()
local graphFrame = rzvaFrame.graphFrame
local graphHeight = RZVA_GRAPH_HEIGHT
local maxZoom = DynamicCam.cameraDistanceMaxZoomFactor_max
local currentZoom = GetCameraZoom()
-- Update current zoom marker.
local currentY = graphHeight - (graphHeight * currentZoom / maxZoom)
graphFrame.zm:ClearAllPoints()
graphFrame.zm:SetPoint("BOTTOMRIGHT", graphFrame, "BOTTOMRIGHT", 0, currentY)
rzvaFrame.cameraZoomValue:SetText(string.format("%.1f", currentZoom))
if DynamicCam:GetSettingsValue(DynamicCam.currentSituationID, "reactiveZoomEnabled") then
if not graphFrame.rzt:IsShown() then
graphFrame.rzt:Show()
graphFrame.rzi:Show()
rzvaFrame.reactiveZoomTargetLabelText:SetTextColor(unpack(RZVA_COLORS.targetZoom))
rzvaFrame.reactiveZoomTargetValue:SetTextColor(unpack(RZVA_COLORS.targetZoom))
end
graphFrame.rzt:ClearAllPoints()
if reactiveZoomTarget then
local targetY = graphHeight - (graphHeight * reactiveZoomTarget / maxZoom)
graphFrame.rzt:SetPoint("BOTTOMLEFT", graphFrame, "BOTTOMLEFT", 0, targetY)
rzvaFrame.reactiveZoomTargetValue:SetText(string.format("%.1f", reactiveZoomTarget))
if lastReactiveZoomTarget then
local step = lastReactiveZoomTarget - reactiveZoomTarget
if step ~= 0 then
graphFrame.rzi:SetHeight(graphHeight * math.abs(step) / maxZoom)
graphFrame.rzi:Show()
else
graphFrame.rzi:Hide()
end
end
lastReactiveZoomTarget = reactiveZoomTarget
else
graphFrame.rzi:Hide()
graphFrame.rzt:Hide()
rzvaFrame.reactiveZoomTargetValue:SetText("---")
end
else
if graphFrame.rzt:IsShown() then
graphFrame.rzt:Hide()
graphFrame.rzi:Hide()
rzvaFrame.reactiveZoomTargetLabelText:SetTextColor(unpack(RZVA_COLORS.disabled))
rzvaFrame.reactiveZoomTargetValue:SetTextColor(unpack(RZVA_COLORS.disabled))
rzvaFrame.reactiveZoomTargetValue:SetText("---")
end
end
end
function DynamicCam:ToggleRZVA()
if not rzvaFrame then
-- Shared window chrome (Ui/Window.lua), so this looks like the main window.
-- No inner nine-slice: the graph is the only content and reads better
-- without a second box around it.
rzvaFrame = DynamicCam.Ui.CreateWindow({
name = "DynamicCamRZVA",
title = L["Reactive Zoom"],
width = RZVA_WIDTH,
height = RZVA_HEIGHT,
strata = "DIALOG",
gaps = RZVA_GAPS,
innerFrame = false,
})
rzvaFrame:SetPoint("BOTTOMRIGHT", SettingsPanel, "BOTTOMLEFT", -20, -20)
-- Everything below lives in the chrome's content region - the rectangle left
-- once the window's borders are accounted for (RZVA_GAPS) - rather than on
-- the window itself, so it keeps its margins without repeating them.
local content = rzvaFrame.contentArea
-- Graph frame (the visual area). The x offset leaves room for the y-axis
-- labels, which hang off its left.
rzvaFrame.graphFrame = CreateFrame("Frame", nil, content)
rzvaFrame.graphFrame:SetSize(RZVA_GRAPH_WIDTH, RZVA_GRAPH_HEIGHT)
rzvaFrame.graphFrame:SetPoint("TOP", content, "TOP", 12, -10)
-- Graph background
rzvaFrame.graphFrame.bg = rzvaFrame.graphFrame:CreateTexture(nil, "BACKGROUND")
rzvaFrame.graphFrame.bg:SetAllPoints()
rzvaFrame.graphFrame.bg:SetColorTexture(0.05, 0.05, 0.1, 1)
-- Graph border lines
local graphFrame = rzvaFrame.graphFrame
RZVADrawLine(graphFrame, 0, 0, RZVA_GRAPH_WIDTH, 0, 1, unpack(RZVA_COLORS.gridMajor)) -- Bottom
RZVADrawLine(graphFrame, 0, RZVA_GRAPH_HEIGHT, RZVA_GRAPH_WIDTH, RZVA_GRAPH_HEIGHT, 1, unpack(RZVA_COLORS.gridMajor)) -- Top
RZVADrawLine(graphFrame, 0, 0, 0, RZVA_GRAPH_HEIGHT, 1, unpack(RZVA_COLORS.gridMajor)) -- Left
RZVADrawLine(graphFrame, RZVA_GRAPH_WIDTH, 0, RZVA_GRAPH_WIDTH, RZVA_GRAPH_HEIGHT, 1, unpack(RZVA_COLORS.gridMajor)) -- Right
-- Y-axis labels (Zoom values)
rzvaFrame.yAxisMin = content:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
rzvaFrame.yAxisMin:SetPoint("RIGHT", graphFrame, "TOPLEFT", -5, 0)
rzvaFrame.yAxisMin:SetText("0")
rzvaFrame.yAxisMin:SetTextColor(unpack(RZVA_COLORS.gridLabel))
rzvaFrame.yAxisMax = content:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
rzvaFrame.yAxisMax:SetPoint("RIGHT", graphFrame, "BOTTOMLEFT", -5, 0)
rzvaFrame.yAxisMax:SetText(tostring(DynamicCam.cameraDistanceMaxZoomFactor_max))
rzvaFrame.yAxisMax:SetTextColor(unpack(RZVA_COLORS.gridLabel))
-- Current zoom marker (red, right side)
graphFrame.zm = CreateFrame("Frame", nil, graphFrame)
graphFrame.zm:SetSize(RZVA_GRAPH_HALF_WIDTH, 1)
graphFrame.zm:Show()
RZVADrawLine(graphFrame.zm, 0, 0, RZVA_GRAPH_HALF_WIDTH, 0, 5, unpack(RZVA_COLORS.currentZoom))
-- Reactive zoom target marker (blue, left side)
graphFrame.rzt = CreateFrame("Frame", nil, graphFrame)
graphFrame.rzt:SetSize(RZVA_GRAPH_HALF_WIDTH, 1)
graphFrame.rzt:Show()
RZVADrawLine(graphFrame.rzt, 0, 0, RZVA_GRAPH_HALF_WIDTH, 0, 5, unpack(RZVA_COLORS.targetZoom))
-- Reactive zoom increment marker (yellow area)
graphFrame.rzi = CreateFrame("Frame", nil, graphFrame)
graphFrame.rzi:SetWidth(RZVA_GRAPH_HALF_WIDTH)
graphFrame.rzi:SetPoint("TOP", graphFrame.rzt, "BOTTOM", 0, 0)
graphFrame.rzi.t = graphFrame.rzi:CreateTexture()
graphFrame.rzi.t:SetAllPoints()
graphFrame.rzi.t:SetColorTexture(unpack(RZVA_COLORS.increment))
-- Current zoom display (static label + dynamic value)
rzvaFrame.cameraZoomLabelText = content:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
rzvaFrame.cameraZoomLabelText:SetPoint("TOP", graphFrame, "BOTTOM", RZVA_GRAPH_WIDTH/4, -8)
rzvaFrame.cameraZoomLabelText:SetText(L["Current\nZoom\nValue"])
rzvaFrame.cameraZoomLabelText:SetTextColor(unpack(RZVA_COLORS.currentZoom))
rzvaFrame.cameraZoomValue = content:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
rzvaFrame.cameraZoomValue:SetPoint("TOP", rzvaFrame.cameraZoomLabelText, "BOTTOM", 0, -3)
rzvaFrame.cameraZoomValue:SetTextColor(unpack(RZVA_COLORS.currentZoom))
rzvaFrame.cameraZoomValue:SetText("0.0")
-- Reactive zoom target display (static label + dynamic value)
rzvaFrame.reactiveZoomTargetLabelText = content:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
rzvaFrame.reactiveZoomTargetLabelText:SetPoint("TOP", graphFrame, "BOTTOM", -RZVA_GRAPH_WIDTH/4, -8)
rzvaFrame.reactiveZoomTargetLabelText:SetText(L["Reactive\nZoom\nTarget"])
rzvaFrame.reactiveZoomTargetLabelText:SetTextColor(unpack(RZVA_COLORS.targetZoom))
rzvaFrame.reactiveZoomTargetValue = content:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
rzvaFrame.reactiveZoomTargetValue:SetPoint("TOP", rzvaFrame.reactiveZoomTargetLabelText, "BOTTOM", 0, -3)
rzvaFrame.reactiveZoomTargetValue:SetTextColor(unpack(RZVA_COLORS.targetZoom))
rzvaFrame.reactiveZoomTargetValue:SetText("---")
-- Instructions
rzvaFrame.instructions = content:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
rzvaFrame.instructions:SetPoint("BOTTOM", content, "BOTTOM", 0, 0)
rzvaFrame.instructions:SetText(L["This graph helps you to\nunderstand how\nReactive Zoom works."])
rzvaFrame.instructions:SetTextColor(unpack(RZVA_COLORS.gridLabel))
-- Initial hide
rzvaFrame:Hide()
rzvaFrame:HookScript("OnShow", function()
reactiveZoomGraphUpdateFrame:SetScript("OnUpdate", ReactiveZoomGraphUpdateFunction)
end)
rzvaFrame:HookScript("OnHide", function()
reactiveZoomGraphUpdateFrame:SetScript("OnUpdate", nil)
end)
end
if not rzvaFrame:IsShown() then
rzvaFrame:Show()
else
rzvaFrame:Hide()
end
end
-- For UI that mirrors the visual aid's state (the new settings UI's toggle in
-- the "Reactive Zoom" header). The frame is created lazily above, so this is
-- false until it has been opened at least once.
function DynamicCam:IsRZVAShown()
return rzvaFrame ~= nil and rzvaFrame:IsShown()
end