-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathMouseZoom.lua
573 lines (410 loc) · 18.1 KB
/
MouseZoom.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
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
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
-------------------------
-- Minimal Zoom-Level --
-------------------------
-- When zooming in, the mimimal third person zoom value depends on the current model.
-- We store this in a SaveVariable.
-- Initialize the SaveVariable if it does not yet exist.
if not minZoomValues then
minZoomValues = {}
end
local function SetMinZoom(zoom)
if not DynamicCam.modelFrame then return end
DynamicCam.modelFrame:SetUnit("player")
-- print("Storing", zoom, "for", DynamicCam.modelFrame:GetModelFileID())
minZoomValues[DynamicCam.modelFrame:GetModelFileID()] = zoom
end
local function GetMinZoom()
if IsMounted() then
-- For mounted we just take a default instead of storing the value for each mount and player-model.
return 1.5
else
if not DynamicCam.modelFrame then return 0.5 end
-- If we have already stored the minimum value, return it. Else use a default.
DynamicCam.modelFrame:SetUnit("player")
return minZoomValues[DynamicCam.modelFrame:GetModelFileID()] or 1.5
end
end
-- Set to true when zooming out from first person, such that the next zoom is stored as min value.
local storeMinZoom = false
-----------------------
-- 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
-- Notice: The feature of zooming to the smallest third person zoom level does only work good for ReactiveZoom.
-- In NonReactiveZoom it only works if you set the zoom speed to max.
-- That's why we are not doing it for NonReactiveZoom.
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
if storeMinZoom then
-- print("User zoomed out again before reaching min value. Interrupting store process.")
storeMinZoom = false
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
if storeMinZoom then
SetMinZoom(currentZoom)
storeMinZoom = false
end
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
-- Always stop at closest third person zoom level.
local minZoom = GetMinZoom()
if zoomIn then
if reactiveZoomTarget - increments < minZoom then
if reactiveZoomTarget > minZoom then
-- print("go to minZoom", minZoom)
reactiveZoomTarget = minZoom
-- Also update the increments if we need to make a NonReactiveZoom below,
-- in case of "zoomTime < secondsPerFrame".
increments = currentZoom - minZoom
else
-- print("go to 0")
reactiveZoomTarget = 0
-- No need to update increments because any zoom target below minZoom
-- will result in 0 automatically.
end
else
reactiveZoomTarget = math.max(0, 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)
-- When this zoom is finished, store the minimal zoom distance,
-- such that we can also use it while zooming in.
if not IsMounted() then
storeMinZoom = true
end
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
if storeMinZoom then
-- print("User zoomed out again before reaching min value. Interrupting store process.")
storeMinZoom = false
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) --
------------------------------------
local function DrawLine(f, startRelativeAnchor, startOffsetX, startOffsetY,
endRelativeAnchor, endOffsetX, endOffsetY,
thickness, r, g, b, a)
local line = f:CreateLine()
line:SetThickness(thickness)
line:SetColorTexture(r, g, b, a)
line:SetStartPoint(startRelativeAnchor, f, startOffsetX, startOffsetY)
line:SetEndPoint(endRelativeAnchor, f, endOffsetX, endOffsetY)
end
local function SetFrameBorder(f, thickness, r, g, b, a)
-- Bottom line.
DrawLine(f, "BOTTOMLEFT", 0, 0, "BOTTOMRIGHT", 0, 0, thickness, r, g, b, a)
-- Top line.
DrawLine(f, "TOPLEFT", 0, 0, "TOPRIGHT", 0, 0, thickness, r, g, b, a)
-- Left line.
DrawLine(f, "BOTTOMLEFT", 0, 0, "TOPLEFT", 0, 0, thickness, r, g, b, a)
-- Right line.
DrawLine(f, "BOTTOMRIGHT", 0, 0, "TOPRIGHT", 0, 0, thickness, r, g, b, a)
end
local rzvaWidth = 120
local rzvaHeight = 200
local rzvaHalfWidth = rzvaWidth/2
local rzvaFrame = nil
local lastReactiveZoomTarget = reactiveZoomTarget
local reactiveZoomGraphUpdateFrame = CreateFrame("Frame")
local function ReactiveZoomGraphUpdateFunction()
rzvaFrame.zm:ClearAllPoints()
rzvaFrame.zm:SetPoint("BOTTOMRIGHT", 0, rzvaFrame:GetHeight() - (rzvaFrame:GetHeight() * GetCameraZoom() / DynamicCam.cameraDistanceMaxZoomFactor_max) )
rzvaFrame.cameraZoomValue:SetText(Round(GetCameraZoom(), 3))
if DynamicCam:GetSettingsValue(DynamicCam.currentSituationID, "reactiveZoomEnabled") then
if not rzvaFrame.rzt:IsShown() then
rzvaFrame.rzt:Show()
rzvaFrame.rzi:Show()
rzvaFrame.reactiveZoomTargetLabel:SetTextColor(.3, .3, 1, 1)
rzvaFrame.reactiveZoomTargetValue:SetTextColor(.3, .3, 1, 1)
end
rzvaFrame.rzt:ClearAllPoints()
if reactiveZoomTarget then
rzvaFrame.rzt:SetPoint("BOTTOMLEFT", 0, rzvaFrame:GetHeight() - (rzvaFrame:GetHeight()* reactiveZoomTarget / DynamicCam.cameraDistanceMaxZoomFactor_max) )
rzvaFrame.reactiveZoomTargetValue:SetText(Round(reactiveZoomTarget, 3))
if lastReactiveZoomTarget then
local step = lastReactiveZoomTarget - reactiveZoomTarget
if step > 0 then
rzvaFrame.rzi:SetHeight(rzvaFrame:GetHeight() * step / DynamicCam.cameraDistanceMaxZoomFactor_max)
rzvaFrame.rzi:Show()
elseif step < 0 then
rzvaFrame.rzi:SetHeight(rzvaFrame:GetHeight() * step / DynamicCam.cameraDistanceMaxZoomFactor_max)
rzvaFrame.rzi:Show()
else
rzvaFrame.rzi:Hide()
end
end
lastReactiveZoomTarget = reactiveZoomTarget
else
rzvaFrame.rzi:Hide()
rzvaFrame.rzt:Hide()
rzvaFrame.reactiveZoomTargetValue:SetText("---")
end
else
if rzvaFrame.rzt:IsShown() then
rzvaFrame.rzt:Hide()
rzvaFrame.rzi:Hide()
rzvaFrame.reactiveZoomTargetLabel:SetTextColor(.3, .3, .3, 1)
rzvaFrame.reactiveZoomTargetValue:SetTextColor(.3, .3, .3, 1)
rzvaFrame.reactiveZoomTargetValue:SetText("---")
end
end
end
function DynamicCam:ToggleRZVA()
if not rzvaFrame then
rzvaFrame = CreateFrame("Frame", "reactiveZoomVisualAid", UIParent)
rzvaFrame:SetFrameStrata("TOOLTIP")
rzvaFrame:SetMovable(true)
rzvaFrame:EnableMouse(true)
rzvaFrame:RegisterForDrag("LeftButton")
rzvaFrame:SetScript("OnDragStart", rzvaFrame.StartMoving)
rzvaFrame:SetScript("OnDragStop", rzvaFrame.StopMovingOrSizing)
rzvaFrame:SetClampedToScreen(true)
-- Closes with right button.
rzvaFrame:SetScript("OnMouseDown", function(self, button)
if button == "RightButton" then
self:Hide()
end
end)
rzvaFrame:SetWidth(rzvaWidth)
rzvaFrame:SetHeight(rzvaHeight)
rzvaFrame:ClearAllPoints()
rzvaFrame:SetPoint("BOTTOMLEFT", SettingsPanel.Container, "BOTTOMLEFT", 45, 35)
rzvaFrame.t = rzvaFrame:CreateTexture()
rzvaFrame.t:SetAllPoints()
rzvaFrame.t:SetTexture("Interface/BUTTONS/WHITE8X8")
rzvaFrame.t:SetColorTexture(1, 1, 1, .1)
SetFrameBorder(rzvaFrame, 2, 1, 1, 1, 1)
rzvaFrame.cameraZoomLabel = rzvaFrame:CreateFontString()
rzvaFrame.cameraZoomLabel:SetWidth(rzvaHalfWidth)
rzvaFrame.cameraZoomLabel:SetJustifyH("CENTER")
rzvaFrame.cameraZoomLabel:SetJustifyV("MIDDLE")
rzvaFrame.cameraZoomLabel:SetPoint("BOTTOMRIGHT", rzvaFrame, "TOPRIGHT", 0, 19)
rzvaFrame.cameraZoomLabel:SetFont("Fonts/FRIZQT__.TTF", 12)
rzvaFrame.cameraZoomLabel:SetTextColor(1, .3, .3, 1)
rzvaFrame.cameraZoomLabel:SetText(L["Actual\nZoom\nValue"])
rzvaFrame.cameraZoomValue = rzvaFrame:CreateFontString()
rzvaFrame.cameraZoomValue:SetWidth(rzvaHalfWidth)
rzvaFrame.cameraZoomValue:SetJustifyH("CENTER")
rzvaFrame.cameraZoomValue:SetJustifyV("MIDDLE")
rzvaFrame.cameraZoomValue:SetPoint("BOTTOMRIGHT", rzvaFrame, "TOPRIGHT", 0, 4)
rzvaFrame.cameraZoomValue:SetFont("Fonts/FRIZQT__.TTF", 14)
rzvaFrame.cameraZoomValue:SetTextColor(1, .3, .3, 1)
rzvaFrame.cameraZoomValue:SetText(GetCameraZoom())
rzvaFrame.reactiveZoomTargetLabel = rzvaFrame:CreateFontString()
rzvaFrame.reactiveZoomTargetLabel:SetWidth(rzvaHalfWidth)
rzvaFrame.reactiveZoomTargetLabel:SetJustifyH("CENTER")
rzvaFrame.reactiveZoomTargetLabel:SetJustifyV("MIDDLE")
rzvaFrame.reactiveZoomTargetLabel:SetPoint("BOTTOMLEFT", rzvaFrame, "TOPLEFT", 0, 19)
rzvaFrame.reactiveZoomTargetLabel:SetFont("Fonts/FRIZQT__.TTF", 12)
rzvaFrame.reactiveZoomTargetLabel:SetTextColor(.3, .3, 1, 1)
rzvaFrame.reactiveZoomTargetLabel:SetText(L["Reactive\nZoom\nTarget"])
rzvaFrame.reactiveZoomTargetValue = rzvaFrame:CreateFontString()
rzvaFrame.reactiveZoomTargetValue:SetWidth(rzvaHalfWidth)
rzvaFrame.reactiveZoomTargetValue:SetJustifyH("CENTER")
rzvaFrame.reactiveZoomTargetValue:SetJustifyV("MIDDLE")
rzvaFrame.reactiveZoomTargetValue:SetPoint("BOTTOMLEFT", rzvaFrame, "TOPLEFT", 0, 4)
rzvaFrame.reactiveZoomTargetValue:SetFont("Fonts/FRIZQT__.TTF", 14)
rzvaFrame.reactiveZoomTargetValue:SetTextColor(.3, .3, 1, 1)
rzvaFrame.zm = CreateFrame("Frame", "cameraZoomMarker", rzvaFrame)
rzvaFrame.zm:SetWidth(rzvaHalfWidth)
rzvaFrame.zm:SetHeight(1)
rzvaFrame.zm:Show()
DrawLine(rzvaFrame.zm, "BOTTOMLEFT", 0, 0, "BOTTOMRIGHT", 0, 0, 5, 1, .3, .3, 1)
rzvaFrame.rzt = CreateFrame("Frame", "reactiveZoomTargetMarker", rzvaFrame)
rzvaFrame.rzt:SetWidth(rzvaHalfWidth)
rzvaFrame.rzt:SetHeight(1)
rzvaFrame.rzt:Show()
DrawLine(rzvaFrame.rzt, "BOTTOMRIGHT", 0, 0, "BOTTOMLEFT", 0, 0, 5, .3, .3, 1, 1)
rzvaFrame.rzi = CreateFrame("Frame", "reactiveZoomIncrementMarker", rzvaFrame)
rzvaFrame.rzi:SetWidth(rzvaHalfWidth)
-- Must set points here, otherwise the texture is not created...
rzvaFrame.rzi:SetPoint("TOP", rzvaFrame.rzt, "BOTTOM", 0, 0)
rzvaFrame.rzi.t = rzvaFrame.rzi:CreateTexture()
rzvaFrame.rzi.t:SetAllPoints()
rzvaFrame.rzi.t:SetTexture("Interface/BUTTONS/WHITE8X8")
rzvaFrame.rzi.t:SetColorTexture(1, 1, 0, 1)
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