-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathSituationManager.lua
1416 lines (1034 loc) · 44.2 KB
/
SituationManager.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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local LibCamera = LibStub("LibCamera-1.0")
local LibEasing = LibStub("LibEasing-1.0")
-- The transition time of SetView() is hard to predict.
-- Use this for now.
local SET_VIEW_TRANSITION_TIME = 0.5
------------
-- LOCALS --
------------
local function Round(num, numDecimalPlaces)
local mult = 10^(numDecimalPlaces or 0)
return math.floor(num * mult + 0.5) / mult
end
------------
-- EVENTS --
------------
DynamicCam.events = {}
-- The user may try to register invalid events. In order to prevent AceEvent-3.0
-- from remembering invalid events, we have to check if an event exists ourselves!
-- https://www.wowinterface.com/forums/showthread.php?t=58681
local eventExistsFrame = CreateFrame("Frame");
local function EventExists(event)
local exists = pcall(eventExistsFrame.RegisterEvent, eventExistsFrame, event)
if exists then eventExistsFrame:UnregisterEvent(event) end
return exists
end
function DynamicCam:EventHandler(event, ...)
-- To be less spammy, we ignore all UNIT_AURA events not belonging to player.
-- ...until anyone comes up with a situation for which they need other player's UNIT_AURA events.
if event == "UNIT_AURA" and ... ~= "player" then
-- print("Ignoring non-player UNIT_AURA")
return
end
-- print("Eventhandler got", event, ...)
-- When entering combat, we have to act now.
-- Otherwise, we might not be able to call protected functions like UIParent:Show().
if event == "PLAYER_REGEN_DISABLED" then
self:EvaluateSituations()
else
self.evaluateSituationsNextFrame = true
end
end
function DynamicCam:RegisterEvents()
self:RegisterEvent("PLAYER_CONTROL_GAINED", "EventHandler")
for situationID, situation in pairs(self.db.profile.situations) do
if situation.enabled and not situation.errorEncountered then
self:RegisterSituationEvents(situationID)
end
end
end
function DynamicCam:RegisterSituationEvents(situationID)
-- print("RegisterSituationEvents", situationID)
local situation = self.db.profile.situations[situationID]
if situation and situation.events then
for i, event in pairs(situation.events) do
if not self.events[event] then
-- We have to check if the event exists ourselves.
-- https://www.wowinterface.com/forums/showthread.php?t=58681
if not EventExists(event) then
DynamicCam:ScriptError(situationID, "events", "events", "Trying to register an unknown event: " .. event)
-- print("Trying to register an unknown event: ", event)
else
-- If the event does exist, we should never get any problems here,
-- but just to be on the safe side:
local result = {pcall(DynamicCam.RegisterEvent, DynamicCam, event, "EventHandler")}
if result[1] == false then
-- print("Not registered for event:", event)
DynamicCam:ScriptError(situationID, "events", "events", result[2])
else
-- print("Registered for event:", event)
self.events[event] = true
end
end
end
end
end
end
-------------
-- SCRIPTS --
-------------
local functionCache = {}
local situationEnvironments = {}
function DynamicCam:RunScript(situationID, scriptID)
local script = DynamicCam.db.profile.situations[situationID][scriptID]
if not script or script == "" then return end
-- make sure that we're not creating tables willy nilly
if not functionCache[situationID] then
functionCache[situationID] = {}
end
if not functionCache[situationID][scriptID] or functionCache[situationID][scriptID] ~= script then
local f, msg = loadstring(script)
if not f then
DynamicCam:ScriptError(situationID, scriptID, "syntax", msg)
return nil
else
functionCache[situationID][scriptID] = f
end
-- if env, set the environment to that
if situationID then
if not situationEnvironments[situationID] then
situationEnvironments[situationID] = setmetatable({}, { __index =
function(t, k)
if k == "_G" then
return t
elseif k == "this" then
return situationEnvironments[situationID].this
else
return _G[k]
end
end
})
situationEnvironments[situationID].this = {}
end
setfenv(functionCache[situationID][scriptID], situationEnvironments[situationID])
end
end
local result = {pcall(functionCache[situationID][scriptID])}
if result[1] == false then
DynamicCam:ScriptError(situationID, scriptID, "runtime", result[2])
return nil
else
tremove(result, 1)
-- print(DynamicCam.db.profile.situations[situationID].name, unpack(result))
return unpack(result)
end
end
-- For errors in scripts.
-- TODO: Later with localisation:
local scriptNames = {
executeOnInit = "Initialisation Script",
condition = "Condition Script",
executeOnEnter = "On-Enter Script",
executeOnExit = "On-Exit Script",
events = "Events",
}
-- Export box for the script error dialog:
local scrollBoxWidth = 400
local scrollBoxHeight = 90
local outerFrame = CreateFrame("Frame")
outerFrame:SetSize(scrollBoxWidth + 80, scrollBoxHeight + 20)
local borderFrame = CreateFrame("Frame", nil, outerFrame, "TooltipBackdropTemplate")
borderFrame:SetSize(scrollBoxWidth + 34, scrollBoxHeight + 10)
borderFrame:SetPoint("CENTER")
local scrollFrame = CreateFrame("ScrollFrame", nil, outerFrame, "UIPanelScrollFrameTemplate")
scrollFrame:SetPoint("CENTER", -10, 0)
scrollFrame:SetSize(scrollBoxWidth, scrollBoxHeight)
local editbox = CreateFrame("EditBox", nil, scrollFrame, "InputBoxScriptTemplate")
editbox:SetMultiLine(true)
editbox:SetAutoFocus(false)
editbox:SetFontObject(ChatFontNormal)
editbox:SetWidth(scrollBoxWidth)
editbox:SetCursorPosition(0)
scrollFrame:SetScrollChild(editbox)
local hideDefaultButton = false
-- The script error dialog.
StaticPopupDialogs["DYNAMICCAM_SCRIPT_ERROR"] = {
timeout = 0,
whileDead = true,
hideOnEscape = true,
preferredIndex = 3, -- avoid some UI taint, see https://authors.curseforge.com/forums/world-of-warcraft/general-chat/lua-code-discussion/226040-how-to-reduce-chance-of-ui-taint-from
text = "%s",
OnShow = function (self)
self.ludius_originalTextWidth = self.text:GetWidth()
self.text:SetWidth(borderFrame:GetWidth())
end,
OnHide = function(self)
self.text:SetWidth(self.ludius_originalTextWidth)
self.ludius_originalTextWidth = nil
end,
-- So that we can have different functions for each button.
selectCallbackByIndex = true,
button1 = "Dismiss",
OnButton1 = function() end,
button2 = "Disable situation",
OnButton2 = function(_, data)
DynamicCam.db.profile.situations[data.situationID].enabled = false
DynamicCam:EvaluateSituations()
LibStub("AceConfigRegistry-3.0"):NotifyChange("DynamicCam")
end,
button3 = "Fix manually",
OnButton3 = function(_, data)
-- if data then print(data.situationID, data.scriptID) end
DynamicCam:OpenMenu()
LibStub("AceConfigDialog-3.0"):SelectGroup("DynamicCam", "situationSettingsTab", "situationControls", data.scriptID)
DynamicCam.Options:SelectSituation(data.situationID)
end,
button4 = "Reset to default",
OnButton4 = function(_, data)
-- if data then print(data.situationID, data.scriptID) end
DynamicCam.db.profile.situations[data.situationID][data.scriptID] = DynamicCam.defaults.profile.situations[data.situationID][data.scriptID]
DynamicCam:UpdateSituation(data.situationID)
LibStub("AceConfigRegistry-3.0"):NotifyChange("DynamicCam")
end,
DisplayButton4 = function() return not hideDefaultButton end,
}
function DynamicCam:ScriptError(situationID, scriptID, errorType, errorMessage)
-- print(situationID, scriptID, errorType, errorMessage)
local situation = DynamicCam.db.profile.situations[situationID]
situation.errorEncountered = scriptID
situation.errorMessage = errorMessage
local situationName = situation.name
local scriptName = scriptNames[scriptID]
-- If the cause is in the init script instead of the one triggering the error.
local scriptIdForButton = scriptID
local isCustomSituation = true
local alreadyUsingDefault = true
if DynamicCam.defaults.profile.situations[situationID] then
isCustomSituation = false
if scriptID == "events" then
alreadyUsingDefault = DynamicCam:EventsEqual(situation.events, DynamicCam.defaults.profile.situations[situationID].events)
else
alreadyUsingDefault = DynamicCam:ScriptEqual(situation[scriptID], DynamicCam.defaults.profile.situations[situationID][scriptID])
end
end
local text = "There is a problem with your DynamicCam Situation Controls!\n(Situation: " .. situationName .. "," .. scriptName .. ")\n\n"
if isCustomSituation then
text = text .. "This error is caused by one of your custom situations, so you have to resolve it yourself or seek assistance online.\n\n"
elseif alreadyUsingDefault then
if scriptID ~= "executeOnInit" and not DynamicCam:ScriptEqual(situation["executeOnInit"], DynamicCam.defaults.profile.situations[situationID]["executeOnInit"]) then
text = text .. "You have modified the Initialisation Script of this DynamicCam stock situation which is probably leading to this error. A reset to the default should resolve this. Otherwise you can try to fix it manually or disable the situation.\n\n"
scriptIdForButton = "executeOnInit"
alreadyUsingDefault = false
else
text = text .. "This is really embarrassing, because this error is caused by the default settings of a DynamicCam stock situation. Please copy the error message below and send it to us on GitHub, Curseforge or WoWInterface. In the meantime you can try to fix it yourself or just disable this situation. Sorry!\n\n"
end
else
text = text .. "You have modified the default settings of this DynamicCam stock situation. A reset to the default should resolve this. Otherwise you can try to fix it manually or disable the situation.\n\n"
end
local errorText = "Error: " .. situationName .. " (" .. situationID .. "), " .. scriptID .. ", " .. errorType .. "\n\n" .. errorMessage .. "\n\n\n"
editbox:SetText(errorText)
-- Data for the button press functions.
local data = {
["situationID"] = situationID,
["scriptID"] = scriptIdForButton,
}
-- Only show the default button, if there is a default to return to.
hideDefaultButton = isCustomSituation or alreadyUsingDefault
StaticPopup_Show("DYNAMICCAM_SCRIPT_ERROR", text, nil, data, outerFrame)
end
---------------------
-- NPC INTERACTION --
---------------------
-- For EvaluateSituations() below.
-- So the transition has to wait a little.
local moreQuestDialog = false
local unsetMoreQuestDialogTimer = nil
local waitForQuestFrameToReopen = 0.3
-- GetNumActiveQuests() does not exist in classic.
if WOW_PROJECT_ID ~= WOW_PROJECT_CLASSIC then
-- When an NPC has more than one quest, after accepting/returning the first quest,
-- the QuestFrame disappears for a short time, which momentarily exits
-- the NPC interaction situation (not looking nice).
-- Unfortunately, when exiting the situation, there is no direct way to determine if
-- the NPC still has quests or if a quest was just accepted.
-- The QUEST_ACCEPTED event comes a few frames after the PLAYER_INTERACTION_MANAGER_FRAME_HIDE
-- and QUEST_FINISHED events which are also the only indicators of the quest frame closing
-- without accepting a quest.
-- That's why we have to do some tricks to know that the NPC still has quests when exiting
-- the situation, so we can enforce an extra delay of 0.3 seconds (should be enough).
local questFrameClosed = true
local lastQuestFrameCloseTime = GetTime()
local moreQuestDialogFrame = CreateFrame("Frame")
moreQuestDialogFrame:RegisterEvent("QUEST_GREETING")
moreQuestDialogFrame:RegisterEvent("GOSSIP_SHOW")
moreQuestDialogFrame:RegisterEvent("QUEST_DETAIL")
moreQuestDialogFrame:RegisterEvent("QUEST_ACCEPTED")
moreQuestDialogFrame:RegisterEvent("QUEST_REMOVED") -- For finishing quests.
moreQuestDialogFrame:RegisterEvent("QUEST_COMPLETE") -- To check if a quest about to be finished in is part of a quest line.
moreQuestDialogFrame:SetScript("OnEvent", function(_, event)
if event == "QUEST_GREETING" or event == "GOSSIP_SHOW" then
-- print(event, "available quests", C_GossipInfo.GetNumAvailableQuests(), GetNumAvailableQuests())
-- print(event, "active quests", C_GossipInfo.GetNumActiveQuests(), GetNumActiveQuests())
-- Find out if the active quests can actually be turned in. Because only then do we want to count them.
-- We need C_GossipInfo.GetNumAvailableQuests and GetNumAvailableQuests, because
-- apparently one is for normal quests and the other for campaign quests.
local completeQuests = 0
if C_GossipInfo.GetNumActiveQuests() > 0 then
for _, v in pairs(C_GossipInfo.GetActiveQuests()) do
-- print(v.title, v.isComplete)
if v.isComplete then
completeQuests = completeQuests + 1
end
end
elseif GetNumActiveQuests() > 0 then
for i=1, GetNumActiveQuests() do
local title, isComplete = GetActiveTitle(i)
-- print(title, isComplete)
if isComplete then
completeQuests = completeQuests + 1
end
end
end
-- print(event, "(probably) complete quests", completeQuests)
if (C_GossipInfo.GetNumAvailableQuests() + completeQuests > 1) or (GetNumAvailableQuests() + completeQuests > 1) then
-- print(GetTime(), event, "setting moreQuestDialog to TRUE.")
DynamicCam:CancelTimer(unsetMoreQuestDialogTimer)
moreQuestDialog = true
questFrameClosed = false
end
-- When the quest detail view is shown, we reset moreQuestDialog,
-- unless we have set moreQuestDialog before without the quest
-- frame being closed.
elseif event == "QUEST_DETAIL" then
if questFrameClosed then
-- print(GetTime(), event, "setting moreQuestDialog to FALSE.")
moreQuestDialog = false
end
-- Sometimes QUEST_ACCEPTED comes after the quest frame has been reopened. Therefore we check GetNumAvailableQuests too.
elseif (event == "QUEST_ACCEPTED" or event == "QUEST_REMOVED") and C_GossipInfo.GetNumAvailableQuests() == 0 and GetNumAvailableQuests() == 0 then
-- print(GetTime(), event, "setting moreQuestDialog to FALSE.")
moreQuestDialog = false
questFrameClosed = true
-- -- TODO: Use grail to determine if NPC has a follow-up quest.
-- elseif event == "QUEST_COMPLETE" then
-- print(event, "About to turn in a quest.")
-- questID = GetQuestID()
-- print(GetTitleText(), questID)
-- local questLineInfo = C_QuestLine.GetQuestLineInfo(questID, C_Map.GetBestMapForUnit("player"))
-- DynamicCam:PrintTable(questLineInfo, 0)
-- local questLineID = questLineInfo.questLineID
-- print(questLineID)
-- local questIDs = C_QuestLine.GetQuestLineQuests(questLineID)
-- for k, v in pairs(questIDs) do
-- print("----------------------", v)
-- local questLineInfo = C_QuestLine.GetQuestLineInfo(v, C_Map.GetBestMapForUnit("player"))
-- if questLineInfo then
-- DynamicCam:PrintTable(questLineInfo, 0)
-- end
-- print("----------------------")
-- end
-- DynamicCam:PrintTable(questIDs, 0)
-- print("ha", C_QuestLog.GetNextWaypoint(questID))
end
end)
-- Cannot use PLAYER_INTERACTION_MANAGER_FRAME_HIDE or QUEST_FINISHED to update
-- questFrameClosed, because this gets called at every QuestFrame transition.
-- We therefore check if the QuestFrame was really closed.
QuestFrame:HookScript("OnHide", function()
-- print("QuestFrame closed", GetTime())
questFrameClosed = true
lastQuestFrameCloseTime = GetTime()
end)
-- When switching to the quest detail view the QuestFrame gets closed and reopened within one frame.
QuestFrame:HookScript("OnShow", function()
-- print("QuestFrame shown", GetTime())
if lastQuestFrameCloseTime == GetTime() then
questFrameClosed = false
end
end)
if ImmersionFrame then
ImmersionFrame:HookScript("OnHide", function()
-- print("ImmersionFrame closed", GetTime())
questFrameClosed = true
end)
end
end
-- Start rotating when entering a situation.
local function StartRotation(newSituation, transitionTime)
local r = newSituation.rotation
local profile = DynamicCam.db.profile
if r.enabled then
if r.rotationType == "continuous" then
LibCamera:BeginContinuousYaw(r.rotationSpeed, transitionTime)
elseif r.rotationType == "degrees" then
if r.yawDegrees ~= 0 then
LibCamera:Yaw(r.yawDegrees, transitionTime, LibEasing[profile.easingYaw])
end
if r.pitchDegrees ~= 0 then
LibCamera:Pitch(r.pitchDegrees, transitionTime, LibEasing[profile.easingPitch])
end
end
end
end
-- Stop rotating when leaving a situation.
local function StopRotation(oldSituation)
local r = oldSituation.rotation
local profile = DynamicCam.db.profile
if r.enabled then
if r.rotationType == "continuous" then
local yaw = LibCamera:StopYawing()
-- rotate back if we want to
if r.rotateBack then
-- print("Ended rotate, degrees rotated, yaw:", yaw)
if yaw then
local yawBack = yaw % 360
-- we're beyond 180 degrees, go the other way
if yawBack > 180 then
yawBack = yawBack - 360
end
LibCamera:Yaw(-yawBack, r.rotateBackTime, LibEasing[profile.easingYaw])
end
end
elseif r.rotationType == "degrees" then
if LibCamera:IsRotating() then
-- interrupted rotation
local yaw, pitch = LibCamera:StopRotating()
-- rotate back if we want to
if r.rotateBack then
-- print("Ended rotate early, degrees rotated, yaw:", yaw, "pitch:", pitch)
if yaw then
LibCamera:Yaw(-yaw, r.rotateBackTime, LibEasing[profile.easingYaw])
end
if pitch then
LibCamera:Pitch(-pitch, r.rotateBackTime, LibEasing[profile.easingPitch])
end
end
else
if r.rotateBack then
if r.yawDegrees ~= 0 then
LibCamera:Yaw(-r.yawDegrees, r.rotateBackTime, LibEasing[profile.easingYaw])
end
if r.pitchDegrees ~= 0 then
LibCamera:Pitch(-r.pitchDegrees, r.rotateBackTime, LibEasing[profile.easingPitch])
end
end
end
end
end
end
-- To store the last zoom when leaving a situation.
local lastZoom = {}
-- To store the previous situation when entering another situation.
local lastSituation = {}
-- Depending on the "Restore Zoom" setting, a user may want to always restore
-- the last zoom when returning to a previous situation. Only for the "adaptive"
-- setting (which is actually the original way DynamicCam did it) we also have
-- to remember the last situation, because we only restore the zoom when returning
-- to the same situation we came from.
-- Used by ChangeSituation() to determine if a stored zoom should
-- be restored when returning to a situation.
local function ShouldRestoreZoom(oldSituationID, newSituationID)
-- print("Should Restore Zoom")
if DynamicCam.db.profile.zoomRestoreSetting == "never" then
-- print("Setting is never.")
return false
end
-- Restore if we're just exiting a situation, and have a stored value for default.
-- (This is the case for both "always" and "adaptive".)
if not newSituationID then
if lastZoom["no-situation"] then
-- print("Restoring saved zoom for no-situation.", lastZoom["no-situation"])
return true, lastZoom["no-situation"]
else
-- print("Not restoring zoom because returning to no-situation with no saved value.")
return false
end
end
-- Don't restore if we don't have a saved zoom value.
-- (Also the case for both "always" and "adaptive".)
if not lastZoom[newSituationID] then
-- print("Not restoring zoom because we have no saved value for this situation.")
return false
end
-- From now on we know that we are entering a new situation and have a stored zoom.
local newSituation = DynamicCam.db.profile.situations[newSituationID]
-- Don't restore zoom if we're about to go into a view.
if newSituation.viewZoom.enabled and newSituation.viewZoom.viewZoomType == "view" then
-- print("Not restoring zoom because entering a view.")
return false
end
local restoreZoom = lastZoom[newSituationID]
if DynamicCam.db.profile.zoomRestoreSetting == "always" then
-- print("Setting is always.")
return true, restoreZoom
end
-- The following are for the zoomRestoreSetting == "adaptive" setting.
-- print("Setting is adaptive.")
-- Only restore zoom if returning to the same situation
if oldSituationID and lastSituation[oldSituationID] ~= newSituationID then
-- print("Not restoring zoom because this is not the situation we came from.")
return false
end
local c = newSituation.viewZoom
-- Restore zoom based on newSituation viewZoomType.
if not c.enabled or c.viewZoomType ~= "zoom" then
-- print("Not restoring zoom because new situation has no zoom setting.")
return false
end
if c.zoomType == "set" then
-- print("Not restoring zoom because new situation has a fixed zoom setting.")
return false
end
if c.zoomType == "range" then
-- only restore zoom if zoom will be in the range
if c.zoomMin <= restoreZoom + .5 and
c.zoomMax >= restoreZoom - .5 then
return true, restoreZoom
else
return false
end
end
if c.zoomType == "in" then
-- Only restore if the stored zoom level is smaller or equal to the situation value
-- and do not zoom out.
if c.zoomValue >= restoreZoom - .5 and GetCameraZoom() > restoreZoom then
return true, restoreZoom
else
-- print("Not restoring because saved value", restoreZoom, "is not smaller than zoom IN of situation.")
return false
end
elseif c.zoomType == "out" then
-- restore zoom if newSituation is zooming out and we would already be zooming out farther
if c.zoomValue <= restoreZoom + .5 and GetCameraZoom() < restoreZoom then
return true, restoreZoom
else
-- print("Not restoring because saved value", restoreZoom, "is not greater than zoom OUT of situation.")
return false
end
end
-- if nothing else, don't restore
return false
end
local function gotoView(view, instant)
-- print("gotoView", view, instant)
if not view then return end
-- View change overrides all zooming and rotating.
LibCamera:StopZooming()
LibCamera:StopRotating()
-- Whenever the zoom changes we need to reset the reactiveZoomTarget.
DynamicCam:ResetReactiveZoomTarget()
local cameraZoomBefore = GetCameraZoom()
-- if you call SetView twice, then it's instant
if instant then
SetView(view)
end
SetView(view)
local cameraZoomAfter = GetCameraZoom()
-- print("Going from", cameraZoomBefore, "to", cameraZoomAfter)
-- If "Adjust Shoulder offset according to zoom level" is activated,
-- the shoulder offset will be instantaneously set according to the new
-- camera zoom level. However, we should instead ease it for SET_VIEW_TRANSITION_TIME.
if DynamicCam:GetSettingsValue(DynamicCam.currentSituationID, "shoulderOffsetZoomEnabled") and not DynamicCam.shoulderOffsetZoomTmpDisable then
DynamicCam.easeShoulderOffsetInProgress = true
DynamicCam.virtualCameraZoom = cameraZoomBefore
LibEasing:Ease(
function(newValue)
DynamicCam.virtualCameraZoom = newValue
end,
cameraZoomBefore,
cameraZoomAfter,
SET_VIEW_TRANSITION_TIME,
LibEasing.Linear,
function()
DynamicCam.easeShoulderOffsetInProgress = false
DynamicCam.virtualCameraZoom = nil
end
)
end
end
function DynamicCam:ChangeSituation(oldSituationID, newSituationID)
-- print("ChangeSituation", oldSituationID, newSituationID, GetTime())
LibCamera:StopZooming()
-- When we are restoring or setting a view, we shall not apply any zoom.
-- The variable "viewInstant" will define the transition speed below.
local settingView = false
local viewInstant
-- Needed so often that we are setting these shortcuts for the whole function scope.
local oldSituation
local newSituation
if oldSituationID then
-- Store last zoom level of this situation.
lastZoom[oldSituationID] = GetCameraZoom()
-- print("---> Storing zoom", lastZoom[oldSituationID], oldSituationID)
-- Shortcut variable.
oldSituation = self.db.profile.situations[oldSituationID]
end
if newSituationID then
-- Store the old situation as the new situation's last situation.
-- May also be nil in case of coming from the no-situation state.
-- (Needed for "adaptive restore", where we only restore when
-- returning to the same situation we came from.)
lastSituation[newSituationID] = oldSituationID
-- Shortcut variable.
newSituation = self.db.profile.situations[newSituationID]
end
-- If we are exiting another situation.
if oldSituation then
-- Stop rotating if applicable.
StopRotation(oldSituation)
-- Restore view if the new situation does not have a view itself.
-- (Setting a new view has a higher priority than reseting an old one.)
local old = oldSituation.viewZoom
if old.enabled and old.viewZoomType == "view" and (not newSituation or not (newSituation.viewZoom.enabled and newSituation.viewZoom.viewZoomType == "view")) then
if GetCVar("cameraSmoothStyle") == "0" then
if old.viewRestore then
gotoView(1, old.viewInstant)
settingView = true
viewInstant = old.viewInstant
end
-- Special treatment if camera follow is activated.
else
if old.restoreDefaultViewNumber then
ResetView(old.restoreDefaultViewNumber)
gotoView(old.restoreDefaultViewNumber, old.viewInstant)
settingView = true
viewInstant = old.viewInstant
end
end
end
-- Load and run advanced script onExit.
self:RunScript(oldSituationID, "executeOnExit")
-- Unhide UI if applicable.
if oldSituation.hideUI.enabled then
self:FadeInUI(oldSituation.hideUI.fadeInTime)
end
self:SendMessage("DC_SITUATION_EXITED")
-- If we are coming from the no-situation state.
elseif self.enteredSituationAtLogin then
lastZoom["no-situation"] = GetCameraZoom()
-- print("---> Storing default zoom", lastZoom[oldSituationID], oldSituationID)
end
-- If we are entering a new situation.
if newSituation then
-- Set view settings
local new = newSituation.viewZoom
if new.enabled and new.viewZoomType == "view" then
if new.viewRestore then SaveView(1) end
gotoView(new.viewNumber, new.viewInstant)
settingView = true
viewInstant = new.viewInstant
end
-- Load and run advanced script onEnter.
self:RunScript(newSituationID, "executeOnEnter")
-- Hide UI if applicable.
if newSituation.hideUI.enabled then
self:FadeOutUI(newSituation.hideUI.fadeOutTime, newSituation.hideUI)
-- If we are currently exiting a situation, we have already called
-- FadeInUI() above. Only if we are neither entering nor exiting a situation
-- with UI fade, we show the UI, to be on the safe side.
elseif not oldSituation or not oldSituation.hideUI.enabled then
self:FadeInUI(0)
end
-- If we are entering the no-situation state.
-- else
-- print("Not entering a new situation")
end
-- These values are needed for the actual transition.
local newZoomLevel
local newShoulderOffset
local transitionTime
-- ##### Determine newZoomLevel. #####
newZoomLevel = GetCameraZoom()
-- We only need to determine newZoomLevel if we are zooming.
if not settingView then
-- Check if we should restore a stored zoom level.
local shouldRestore, zoomLevel = ShouldRestoreZoom(oldSituationID, newSituationID)
if shouldRestore then
newZoomLevel = zoomLevel
-- Otherwise take the zoom level of the situation we are entering.
-- (There is no default zoom level for the no-situation case!)
elseif newSituationID then
local c = newSituation.viewZoom
if c.enabled and c.viewZoomType == "zoom" then
if (c.zoomType == "set") or
(c.zoomType == "in" and newZoomLevel > c.zoomValue) or
(c.zoomType == "out" and newZoomLevel < c.zoomValue) then
newZoomLevel = c.zoomValue
elseif c.zoomType == "range" then
if newZoomLevel < c.zoomMin then
newZoomLevel = c.zoomMin
elseif newZoomLevel > c.zoomMax then
newZoomLevel = c.zoomMax
end
end
end
end
end
-- ##### Determine newShoulderOffset. #####
if newSituation and newSituation.situationSettings.cvars.test_cameraOverShoulder then
newShoulderOffset = newSituation.situationSettings.cvars.test_cameraOverShoulder
else
newShoulderOffset = self.db.profile.standardSettings.cvars.test_cameraOverShoulder
end
-- ##### Determine transitionTime. #####
-- After reloading the UI we want to enter the current situation immediately!
if not self.enteredSituationAtLogin then
transitionTime = 0
-- If there is a transitionTime in the environment, it has maximum priority.
elseif newSituationID and situationEnvironments[newSituationID].this.transitionTime then
transitionTime = situationEnvironments[newSituationID].this.transitionTime
-- When restoring or setting a view, there is no additional zoom.
-- The shoulder offset transition should be as fast at the view change.
-- SET_VIEW_TRANSITION_TIME = 0.5 seems to be good for non-instant gotoView.
elseif settingView then
-- If settingView is true, we know there must be a newSituationID.
if viewInstant then
transitionTime = 0
else
transitionTime = SET_VIEW_TRANSITION_TIME
end
-- Otherwise the new situation's transition time is taken.
elseif newSituation and newSituation.viewZoom.enabled and newSituation.viewZoom.viewZoomType == "zoom" then
transitionTime = newSituation.viewZoom.zoomTransitionTime
-- If the "Don't slow" option is selected, we have to check
-- if actually a faster transition time is possible.
if transitionTime > 0 and newSituation.viewZoom.zoomTimeIsMax then
local difference = math.abs(newZoomLevel - GetCameraZoom())
local linearSpeed = difference / transitionTime
local currentSpeed = self:GetSettingsValue(newSituationID, "cvars", "cameraZoomSpeed")
if linearSpeed < currentSpeed then
-- min time 10 frames
transitionTime = math.max(DynamicCam.secondsPerFrame*10, difference / currentSpeed)
end
end
-- Default is this "magic number"...
else
transitionTime = 0.75
end
-- print("transitionTime", transitionTime)
-- Start the actual easing.
local easeFunction = LibEasing[self.db.profile.easingZoom]
if settingView then
easeFunction = LibEasing.Linear
else
-- We only need to zoom when not going into a view.
-- Whenever the zoom changes we need to reset the reactiveZoomTarget.
DynamicCam:ResetReactiveZoomTarget()
LibCamera:SetZoom(newZoomLevel, transitionTime, easeFunction)
end
self:EaseShoulderOffset(newShoulderOffset, transitionTime, easeFunction)
-- Set default values (possibly for new situation, may be nil).
self.currentSituationID = newSituationID
self:ApplySettings(true)
-- Set situation specific values.
-- (Except shoulder offset, which we are easing above.)
if newSituation then
-- If there is a rotationTime in the environment, it has priority.
local rotationTime = situationEnvironments[newSituationID].this.rotationTime or newSituation.rotation.rotationTime
-- Start rotating if applicable.