-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera_cofc_2.lua
2790 lines (2465 loc) · 91.1 KB
/
camera_cofc_2.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
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
version = "1.0"
function widget:GetInfo()
return {
name = "Combo Overhead / Free Camera",
desc = "[v"..version.."] Camera featuring 6 actions",
author = "CarRepairer, msafwan, dahn",
date = "2020-05-01",
license = "GNU GPL, v2 or later",
layer = 0,
handler = true,
enabled = false,
}
end
include("keysym.lua")
include("Widgets/COFCtools/Interpolate.lua")
include("Widgets/COFCtools/TraceScreenRay.lua")
-- WG Exports:
-- WG.COFC_SetCameraTarget: {number gx, number gy, number gz(, number smoothness(,boolean useSmoothMeshSetting(, number dist)))} -> {}, Set Camera target, ensures COFC options are respected
-- WG.COFC_SetCameraTargetBox: {number minX, number minZ, number maxX, number maxZ, number minDist(, number maxY(, number smoothness(,boolean useSmoothMeshSetting)))} -> {}, Set Camera to contain input box. maxY should be the highest point in the box, defaults to ground height of box center
-- WG.COFC_SkyBufferProportion: {} -> number [0..1], proportion of maximum zoom height the camera is currently at. 0 is the ground, 1 is maximum zoom height.
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local init = true
local trackmode = false --before options
local thirdperson_trackunit = false
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
options_path = 'Settings/Camera/Camera Controls'
local zoomPath = 'Settings/Camera/Camera Controls/Zoom Behaviour'
local rotatePath = 'Settings/Camera/Camera Controls/Rotation Behaviour'
local scrollPath = 'Settings/Camera/Camera Controls/Scroll Behaviour'
local miscPath = 'Settings/Camera/Camera Controls/Misc'
local cameraFollowPath = 'Settings/Camera/Camera Following'
local minimap_path = 'Settings/HUD Panels/Minimap'
options_order = {
'helpwindow',
'topBottomEdge',
'leftRightEdge',
'middleMouseButton',
'smoothness',
'lblZoom',
-- 'zoomintocursor',
-- 'zoomoutfromcursor',
'zoominfactor',
'zoomin',
'zoomoutfactor',
'zoomout',
'drifttocenter',
'invertzoom',
'invertalt',
'tiltedzoom',
'tiltzoomfactor',
'zoomouttocenter',
'lblRotate',
'rotatefactor',
'rotsmoothness',
'targetmouse',
-- 'rotateonedge',
'inverttilt',
'tiltfactor',
'groundrot',
'lblScroll',
'speedFactor',
'speedFactor_k',
-- 'edgemove',
'invertscroll',
'smoothscroll',
'smoothmeshscroll',
'lblMisc',
'fov',
'overviewmode',
'overviewset',
'rotatebackfromov',
--'restrictangle',
--'mingrounddist',
'resetcam',
'freemode',
--following:
'lblFollowCursor',
'follow',
'lblFollowUnit',
'trackmode',
'persistenttrackmode',
'thirdpersontrack',
'lblFollowCursorZoom',
'followautozoom',
'followinscrollspeed',
'followoutscrollspeed',
'followzoominspeed',
'followzoomoutspeed',
'followzoommindist',
'followzoommaxdist',
'label_controlgroups',
'enableCycleView',
'groupSelectionTapTimeout',
}
local OverviewAction = function() end
local OverviewSetAction = function() end
local GetDistForBounds = function(width, height, maxGroundHeight, edgeBufferProportion) end
local SetFOV = function(fov) end
local SelectNextPlayer = function() end
local ApplyCenterBounds = function(cs) end
options = {
lblblank1 = {name='', type='label'},
lblRotate = {name='Rotation Behaviour', type='label', path=rotatePath},
lblScroll = {name='Scroll Behaviour', type='label', path=scrollPath},
lblZoom = {name='Zoom Behaviour', type='label', path=zoomPath},
lblMisc = {name='Misc.', type='label', path=miscPath},
lblFollowCursor = {name='Cursor Following', type='label', path=cameraFollowPath},
lblFollowCursorZoom = {name='Auto-Zooming', type='label', path=cameraFollowPath},
lblFollowUnit = {name='Unit Following', type='label', path=cameraFollowPath},
topBottomEdge = {
name = 'Top/Bottom Edge Behaviour',
type = 'radioButton',
value = 'pan',
items = {
{key = 'pan', name='Pan'},
{key = 'orbit', name='Rotate World'},
{key = 'rotate', name='Rotate Camera'},
{key = 'off', name='Off'},
},
noHotkey = true,
},
leftRightEdge = {
name = 'Left/Right Edge Behaviour',
type = 'radioButton',
value = 'pan',
items = {
{key = 'pan', name='Pan'},
{key = 'orbit', name='Rotate World'},
{key = 'rotate', name='Rotate Camera'},
{key = 'off', name='Off'},
},
noHotkey = true,
},
middleMouseButton = {
name = 'Middle Mouse Button Behaviour',
type = 'radioButton',
value = 'pan',
items = {
{key = 'pan', name='Pan'},
{key = 'orbit', name='Rotate World'},
{key = 'rotate', name='Rotate Camera'},
{key = 'off', name='Off'},
},
noHotkey = true,
advanced = true,
},
smoothness = {
name = 'Smoothness',
desc = "Controls how smoothly the camera moves.",
type = 'number',
min = 0.0, max = 0.8, step = 0.1,
value = 0.3,
-- Applies to the following:
-- Zoom()
-- Altitude()
-- SetFOV()
-- edge screen scroll
-- toggling zoomouttocenter
-- and calls to SetCameraTarget() without passing in an explicit smoothness parameter
--
-- Smoothness for rotation and tilt are handled by the rotsmoothness option instead
},
helpwindow = {
name = 'COFCam Help',
type = 'text',
value = [[
Complete Overhead/Free Camera has six main actions...
Zoom..... <Mousewheel>
Tilt World..... <Ctrl> + <Mousewheel>
Altitude..... <Alt> + <Mousewheel>
Mouse Scroll..... <Middlebutton-drag>
Rotate World..... <Ctrl> + <Middlebutton-drag>
Rotate Camera..... <Alt> + <Middlebutton-drag>
Additional actions:
Keyboard: <arrow keys> replicate middlebutton drag while <pgup/pgdn> replicate mousewheel. You can use these with ctrl, alt & shift to replicate mouse camera actions.
Use <Shift> to speed up camera movements.
Reset Camera..... <Ctrl> + <Alt> + <Middleclick>
]],
},
zoominfactor = { --should be lower than zoom-out-speed to help user aim tiny units
name = 'Zoom-in speed',
type = 'number',
min = 0.1, max = 1, step = 0.05,
value = 0.5,
path = zoomPath,
},
zoomoutfactor = { --should be higher than zoom-in-speed to help user escape to bigger picture
name = 'Zoom-out speed',
type = 'number',
min = 0.1, max = 1, step = 0.05,
value = 0.8,
path = zoomPath,
},
invertzoom = {
name = 'Invert zoom',
desc = 'Invert the scroll wheel direction for zooming.',
type = 'bool',
value = true,
noHotkey = true,
path = zoomPath,
},
invertalt = {
name = 'Invert altitude',
desc = 'Invert the scroll wheel direction for altitude.',
type = 'bool',
value = false,
noHotkey = true,
path = zoomPath,
},
zoomin = {
name = 'Zoom In',
type = 'radioButton',
value = 'toCursor',
items = {
{key = 'toCursor', name='To Cursor'},
{key = 'toCenter', name='To Screen Center'},
},
noHotkey = true,
path = zoomPath,
},
zoomout = {
name = 'Zoom Out',
type = 'radioButton',
value = 'fromCenter',
items = {
{key = 'fromCursor', name='From Cursor'},
{key = 'fromCenter', name='From Screen Center'},
},
noHotkey = true,
path = zoomPath,
},
zoomouttocenter = {
name = 'Zoom out to center',
desc = 'Center the map as you zoom out.',
type = 'bool',
value = true,
OnChange = function(self)
local cs = Spring.GetCameraState()
if cs.rx then
cs = ApplyCenterBounds(cs)
-- Spring.SetCameraState(cs, options.smoothness.value)
OverrideSetCameraStateInterpolate(cs,options.smoothness.value)
end
end,
noHotkey = true,
path = zoomPath,
},
drifttocenter = {
name = 'Drift zoom target to center',
desc = 'Moves object under cursor to screen center. Only works when zooming to cursor.',
type = 'bool',
value = true,
noHotkey = true,
path = zoomPath,
},
tiltedzoom = {
name = 'Tilt camera while zooming',
desc = 'Have the camera tilt while zooming. Camera faces ground when zoomed out, and looks out nearly parallel to ground when fully zoomed in',
type = 'bool',
value = true,
noHotkey = true,
path = zoomPath,
},
tiltzoomfactor = {
name = 'Tilt amount',
desc = 'How tilted the camera is when fully zoomed in. 0.1 is fully tilted, 2.0 is not tilted.',
type = 'number',
min = 0.1, max = 2, step = 0.1,
value = 1.0,
OnChange = function(self) SetFOV(options.fov.value) end,
path = zoomPath,
},
rotatefactor = {
name = 'Rotation speed',
type = 'number',
min = 0.5, max = 10, step = 0.5,
value = 2,
path = rotatePath,
},
rotsmoothness = {
name = 'Rotation Smoothness',
desc = "Controls how smoothly the camera rotates.",
type = 'number',
min = 0.0, max = 0.8, step = 0.1,
value = 0.1,
path = rotatePath,
},
-- rotateonedge = {
-- name = "Rotate camera at edge",
-- desc = "Rotate camera when the cursor is at the edge of the screen (edge scroll must be off).",
-- type = 'bool',
-- value = false,
-- },
-- restrictangle = {
-- name = "Restrict Camera Angle",
-- desc = "If disabled you can point the camera upward, but end up with strange camera positioning.",
-- type = 'bool',
-- advanced = true,
-- value = true,
-- OnChange = function(self) init = true; end,
-- noHotkey = true,
-- },
targetmouse = {
name = 'Rotate world origin at cursor',
desc = 'Rotate world using origin at the cursor rather than the center of screen.',
type = 'bool',
value = true,
noHotkey = true,
path = rotatePath,
},
inverttilt = {
name = 'Invert tilt',
desc = 'Invert the tilt direction when using ctrl+mousewheel.',
type = 'bool',
value = false,
noHotkey = true,
path = rotatePath,
},
tiltfactor = {
name = 'Tilt speed',
type = 'number',
min = 2, max = 40, step = 2,
value = 10,
path = rotatePath,
},
groundrot = {
name = "Rotate When Camera Hits Ground",
desc = "If world-rotation motion causes the camera to hit the ground, camera-rotation motion takes over. Doesn't apply in Free Mode.",
type = 'bool',
value = true,
advanced = true,
noHotkey = true,
path = rotatePath,
},
speedFactor = {
name = 'Mouse scroll speed',
desc = 'This speed applies to scrolling with the middle button.',
type = 'number',
min = 10, max = 40,
value = 25,
path = scrollPath,
},
speedFactor_k = {
name = 'Keyboard/edge scroll speed',
desc = 'This speed applies to edge scrolling and keyboard keys.',
type = 'number',
min = 1, max = 50,
value = 40,
path = scrollPath,
},
invertscroll = {
name = "Invert scrolling direction",
desc = "Invert scrolling direction (doesn't apply to smoothscroll).",
type = 'bool',
value = true,
noHotkey = true,
path = scrollPath,
},
smoothscroll = {
name = 'Smooth scrolling',
desc = 'Use smoothscroll method when mouse scrolling.',
type = 'bool',
value = false,
noHotkey = true,
path = scrollPath,
},
smoothmeshscroll = {
name = 'Smooth Mesh Scrolling',
desc = 'A smoother way to scroll. Applies to all types of mouse/keyboard scrolling.',
type = 'bool',
value = true,
noHotkey = true,
path = scrollPath,
},
-- mingrounddist = {
-- name = 'Minimum Ground Distance',
-- desc = 'Getting too close to the ground allows strange camera positioning.',
-- type = 'number',
-- advanced = true,
-- min = 0, max = 100, step = 1,
-- value = 1,
-- OnChange = function(self) init = true; end,
-- },
fov = {
name = 'Field of View (Degrees)',
--desc = "FOV (25 deg - 100 deg).",
type = 'number',
min = 10, max = 100, step = 5,
value = Spring.GetCameraFOV(),
springsetting = 'CamFreeFOV', --save stuff in springsetting. reference: epicmenu_conf.lua
OnChange = function(self) SetFOV(self.value) end,
path=miscPath,
},
overviewmode = {
name = "COFC Overview",
desc = "Go to overview mode, then restore view to cursor position.",
type = 'button',
hotkey = {key='tab', mod=''},
OnChange = function(self) OverviewAction() end,
path=miscPath,
},
overviewset = {
name = "Set Overview Viewpoint",
desc = "Save the current view as the new overview mode viewpoint. Use 'Reset Camera' to remove it.",
type = 'button',
OnChange = function(self) OverviewSetAction() end,
path=miscPath,
},
rotatebackfromov = {
name = "Rotate Back From Overview",
desc = "When returning from overview mode, rotate the camera to its original position (only applies when you have set an overview viewpoint).",
type = 'bool',
value = true,
noHotkey = true,
path=miscPath,
},
resetcam = {
name = "Reset Camera",
desc = "Reset the camera position and orientation. Map a hotkey or use <Ctrl> + <Alt> + <Middleclick>",
type = 'button',
-- OnChange defined later
path=miscPath,
},
freemode = {
name = "FreeMode (risky)",
desc = "Be free. Camera movement not bound to map edge. USE AT YOUR OWN RISK!\nTips: press TAB if you get lost.",
type = 'bool',
advanced = true,
value = false,
OnChange = function(self) init = true; end,
noHotkey = true,
path=miscPath,
},
-- follow cursor
follow = {
name = "Follow player's cursor",
desc = "Follow the cursor of the player you're spectating (needs Ally Cursor widget to be on). Mouse midclick to pause tracking for 4 second.",
type = 'bool',
value = false,
hotkey = {key='l', mod='alt+'},
path = cameraFollowPath,
OnChange = function(self) Spring.Echo("COFC: follow cursor " .. (self.value and "active" or "inactive")) end,
},
followautozoom = {
name = "Auto zoom",
desc = "Auto zoom in and out while following player's cursor (zoom level will represent player's focus). \n\nDO NOT enable this if you want to control the zoom level yourself.",
type = 'bool',
value = false,
path = cameraFollowPath,
},
followinscrollspeed = {
name = "On Screen Tracking Speed",
desc = "Tracking speed while cursor is on-screen. \n\nRecommend: Lowest (prevent jerky movement)",
type = 'number',
min = 1, max = 14, step = 1,
mid = (14+1)/2,
value = 1,
OnChange = function(self) Spring.Echo("COFC: " ..self.mid*2 - self.value .. " second") end,
path = cameraFollowPath,
},
followoutscrollspeed = {
name = "Off Screen Tracking Speed",
desc = "Tracking speed while cursor is off-screen. \n\nRecommend: Highest (prevent missed action)",
type = 'number',
min = 2, max = 15, step = 1,
mid = (15+2)/2,
value = 15,
OnChange = function(self) Spring.Echo("COFC: " ..self.mid*2 - self.value .. " second") end,
path = cameraFollowPath,
},
followzoommindist = {
name = "Closest Zoom",
desc = "The closest zoom. Default: 500",
type = 'number',
min = 200, max = 10000, step = 100,
value = 500,
OnChange = function(self) Spring.Echo("COFC: " ..self.value .. " elmo") end,
path = cameraFollowPath,
},
followzoommaxdist = {
name = "Farthest Zoom",
desc = "The furthest zoom. Default: 2000",
type = 'number',
min = 200, max = 10000, step = 100,
value = 2000,
OnChange = function(self) Spring.Echo("COFC: " .. self.value .. " elmo") end,
path = cameraFollowPath,
},
followzoominspeed = {
name = "Zoom-in Speed",
desc = "Zoom-in speed when cursor is on-screen. Default: 50%",
type = 'number',
min = 0.1, max = 1, step = 0.05,
value = 0.5,
OnChange = function(self) Spring.Echo("COFC: " .. self.value*100 .. " percent") end,
path = cameraFollowPath,
},
followzoomoutspeed = {
name = "Zoom-out Speed",
desc = "Zoom-out speed when cursor is at screen edge and off-screen. Default: 50%",
type = 'number',
min = 0.1, max = 1, step = 0.05,
value = 0.5,
OnChange = function(self)Spring.Echo("COFC: " .. self.value*100 .. " percent") end,
path = cameraFollowPath,
},
-- end follow cursor
-- follow unit
trackmode = {
name = "Activate Trackmode",
desc = "Track the selected unit (mouse midclick to exit mode)",
type = 'button',
hotkey = {key='t', mod='alt+'},
path = cameraFollowPath,
OnChange = function(self)
if thirdperson_trackunit then --turn off 3rd person tracking if it is.
Spring.SendCommands('trackoff')
Spring.SendCommands('viewfree')
thirdperson_trackunit = false
end
trackmode = true;
Spring.Echo("COFC: Unit tracking ON")
end,
},
persistenttrackmode = {
name = "Persistent trackmode state",
desc = "Trackmode will not cancel when deselecting unit. Trackmode will always attempt to track newly selected unit. Press mouse midclick to cancel this mode.",
type = 'bool',
value = false,
path = cameraFollowPath,
},
thirdpersontrack = {
name = "Enter 3rd Person Trackmode",
desc = "3rd Person track the selected unit (mouse midclick to exit mode). Press arrow key to jump to nearby units, or move mouse to edge of screen to jump to current unit selection (will exit mode if no selection).",
type = 'button',
hotkey = {key='k', mod='alt+'},
path = cameraFollowPath,
OnChange = function(self)
local selUnits = Spring.GetSelectedUnits()
if selUnits and selUnits[1] and thirdperson_trackunit ~= selUnits[1] then --check if 3rd Person into same unit or if there's any unit at all
Spring.SendCommands('viewfps')
Spring.SendCommands('track')
thirdperson_trackunit = selUnits[1]
local cs = Spring.GetCameraState()
cs.px,cs.py,cs.pz =Spring.GetUnitPosition(selUnits[1]) --place FPS camera on the ground (prevent out of LOD case)
cs.py = cs.py + 25
-- Spring.SetCameraState(cs,0)
OverrideSetCameraStateInterpolate(cs,0)
else
Spring.SendCommands('trackoff')
Spring.SendCommands('viewfree')
thirdperson_trackunit = false
end
end,
},
label_controlgroups = {name='Pan To Cluster', type='label', path = 'Settings/Interface/Control Groups'},
enableCycleView = {
name = "Pan to cluster",
type = 'bool',
value = false,
path = 'Settings/Interface/Control Groups',
desc = "If you double-tap the group numbers (1,2,3 etc.) it will move the camera position to different clusters of units within the group rather than to the average position of the entire group.",
},
groupSelectionTapTimeout = {
name = 'Pan to cluster tap timeout',
desc = "How quickly do you have to double-tap group numbers to move the camera? Smaller timeout means faster tapping.",
type = 'number',
min = 0.0, max = 5.0, step = 0.1,
value = 2.0,
path = 'Settings/Interface/Control Groups',
},
-- end follow unit
}
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local GL_LINES = GL.LINES
local GL_GREATER = GL.GREATER
local GL_POINTS = GL.POINTS
local glBeginEnd = gl.BeginEnd
local glColor = gl.Color
local glLineWidth = gl.LineWidth
local glVertex = gl.Vertex
local glAlphaTest = gl.AlphaTest
local glPointSize = gl.PointSize
local glTexture = gl.Texture
local glTexRect = gl.TexRect
local red = { 1, 0, 0 }
local green = { 0, 1, 0 }
local black = { 0, 0, 0 }
local white = { 1, 1, 1 }
local spGetCameraState = Spring.GetCameraState
local spGetCameraVectors = Spring.GetCameraVectors
local spGetGroundHeight = Spring.GetGroundHeight
local spGetSmoothMeshHeight = Spring.GetSmoothMeshHeight
local spGetModKeyState = Spring.GetModKeyState
local spGetMouseState = Spring.GetMouseState
local spGetSelectedUnits = Spring.GetSelectedUnits
local spGetUnitPosition = Spring.GetUnitPosition
local spIsAboveMiniMap = Spring.IsAboveMiniMap
local spSendCommands = Spring.SendCommands
local spSetCameraState = Spring.SetCameraState
local spSetMouseCursor = Spring.SetMouseCursor
local spTraceScreenRay = Spring.TraceScreenRay
local spWarpMouse = Spring.WarpMouse
local spGetCameraDirection = Spring.GetCameraDirection
local spGetTimer = Spring.GetTimer
local spDiffTimers = Spring.DiffTimers
local spGetUnitDefID = Spring.GetUnitDefID
local spGetUnitSeparation = Spring.GetUnitSeparation
local abs = math.abs
local min = math.min
local max = math.max
local sqrt = math.sqrt
local sin = math.sin
local cos = math.cos
local echo = Spring.Echo
local helpText = {}
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local ls_x, ls_y, ls_z --lockspot position
local ls_dist, ls_have, ls_onmap --lockspot flag
local tilting
local overview_mode, last_rx, last_ry, last_ls_dist, ov_cs --overview_mode's variable
local follow_timer = 0
local epicmenuHkeyComp = {} --for saving & reapply hotkey system handled by epicmenu.lua
local initialBoundsSet = false
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local vsx, vsy = widgetHandler:GetViewSizes()
local cx,cy = vsx * 0.5,vsy * 0.5
local centerDriftFactor = 20/1080 * vsy
local horizAspectCorrectionFactor, vertAspectCorrectionFactor = 1, 1
if vsx > vsy then horizAspectCorrectionFactor = vsx/vsy
else vertAspectCorrectionFactor = vsy/vsx end
function widget:ViewResize(viewSizeX, viewSizeY)
vsx = viewSizeX
vsy = viewSizeY
cx = vsx * 0.5
cy = vsy * 0.5
centerDriftFactor = 20/1080 * vsy
horizAspectCorrectionFactor, vertAspectCorrectionFactor = 1, 1
if vsx > vsy then
horizAspectCorrectionFactor = vsx/vsy
else
vertAspectCorrectionFactor = vsy/vsx
end
SetFOV(Spring.GetCameraFOV())
end
local PI = 3.1415
--local TWOPI = PI*2
local HALFPI = PI/2
--local HALFPIPLUS = HALFPI+0.01
local HALFPIMINUS = HALFPI-0.01
local RADperDEGREE = PI/180
local fpsmode = false
local mx, my = 0,0
local msx, msy = 0,0
local smoothscroll = false
local springscroll = false
local lockspringscroll = false
local rotate, movekey
local move, rot, move2 = {}, {}, {}
local key_code = {
left = 276,
right = 275,
up = 273,
down = 274,
pageup = 280,
pagedown = 281,
}
local keys = {
[276] = 'left',
[275] = 'right',
[273] = 'up',
[274] = 'down',
}
local icon_size = 20
local cycle = 1
local camcycle = 1
local trackcycle = 1
local hideCursor = false
-- Mirrored in Interpolate.lua
-- local MWIDTH, MHEIGHT = Game.mapSizeX, Game.mapSizeZ
local minX, minZ, maxX, maxZ = 0, 0, MWIDTH, MHEIGHT
-- local mcx, mcz = MWIDTH / 2, MHEIGHT / 2
-- local mcy = spGetGroundHeight(mcx, mcz)
-- local maxDistY = max(MHEIGHT, MWIDTH) * 2
-- local mapEdgeBuffer = 1000
--Tilt Zoom constants
local onTiltZoomTrack = true
local lockPoint = {worldBegin = nil, worldEnd = nil, worldCenter = nil, world = nil, screen = nil, mode = nil}
local lastMouseX, lastMouseY
-- local zoomTime = 0
-- local zoomTimer
local groundMin, groundMax = Spring.GetGroundExtremes()
local topDownBufferZonePercent = 0.20
local groundBufferZone = 20
local topDownBufferZone = maxDistY * topDownBufferZonePercent
local minZoomTiltAngle = 35
local angleCorrectionMaximum = 5 * RADperDEGREE
-- local targetCenteringHeight = 1200
local mapEdgeProportion = 1.0/5.9 --map edge buffer is 1/5.9 of the length of the dimension fitted to screen
local currentFOVhalf_rad = 0
GetDistForBounds = function(width, height, maxGroundHeight, edgeBufferProportion, fov)
if not edgeBufferProportion then edgeBufferProportion = mapEdgeProportion end
local fittingDistance = height/2
if vsy/vsx > height/width then fittingDistance = (width * vsy/vsx)/2 end
local fittingEdge = fittingDistance/(1/(2 * edgeBufferProportion) - 1)
local edgeBuffer = math.max(maxGroundHeight, fittingEdge)
local totalFittingLength = fittingDistance + edgeBuffer
return totalFittingLength/math.tan(currentFOVhalf_rad), edgeBuffer
end
SetFOV = function(fov)
local cs = spGetCameraState()
currentFOVhalf_rad = (fov/2) * RADperDEGREE
maxDistY, mapEdgeBuffer = GetDistForBounds(MWIDTH, MHEIGHT, groundMax) --adjust maximum TAB/Overview distance based on camera FOV
cs.fov = fov
cs.py = overview_mode and maxDistY or math.min(cs.py, maxDistY)
--Update Tilt Zoom Constants
local tiltzoomfactor = options.tiltzoomfactor.value or 1.0
topDownBufferZone = maxDistY * topDownBufferZonePercent
minZoomTiltAngle = (30 + 17 * math.tan(currentFOVhalf_rad)) * RADperDEGREE * tiltzoomfactor
if cs.name == "free" then
OverrideSetCameraStateInterpolate(cs,options.smoothness.value)
else
spSetCameraState(cs,0)
end
end
local function SetSkyBufferProportion(cs)
local _,cs_py,_ = Spring.GetCameraPosition()
local topDownBufferZoneBottom = maxDistY - topDownBufferZone
WG.COFC_SkyBufferProportion = min(max((cs_py - topDownBufferZoneBottom)/topDownBufferZone + 0.2, 0.0), 1.0) --add 0.2 to start fading little before the straight-down zoomout
end
do SetFOV(Spring.GetCameraFOV()) end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local rotate_transit --switch for smoothing "rotate at mouse position instead of screen center"
local last_move = spGetTimer() --switch for reseting lockspot for Edgescroll
local thirdPerson_transit = spGetTimer() --switch for smoothing "3rd person trackmode edge screen scroll"
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local function DetermineInitCameraState()
local csOldpx, csOldpy, csOldpz = Spring.GetCameraPosition()
local csOlddx, csOlddy, csOlddz = Spring.GetCameraDirection()
local csOldrx = PI/2 - math.acos(csOlddy)
local csOldry = math.atan2(csOlddx, -csOlddz) - PI
spSendCommands("viewfree")
local cs = spGetCameraState()
-- if csOldpx == 0.0 * csOldpz == 0.0 then
-- cs.px = MWIDTH/2
-- cs.pz = MHEIGHT/2
-- else
cs.px = csOldpx
cs.pz = csOldpz
-- end
cs.py = csOldpy
cs.rx = csOldrx
cs.ry = csOldry
cs.rz = 0
return cs
end
local function GetPyramidBoundedCoords(x,z) --This is meant for minX,minZ,maxX,maxZ bounds, to get bounds without necessarily changing camera state
if x < minX then x = minX; end
if x > maxX then x = maxX; end
if z < minZ then z = minZ; end
if z > maxZ then z = maxZ; end
return x,z
end
local function GetMapBoundedCoords(x,z) --This should not use minX,minZ,maxX,maxZ bounds, as this is used specifically to keep things on the map
if x < 0 then x = 0; end
if x > MWIDTH then x=MWIDTH; end
if z < 0 then z = 0; end
if z > MHEIGHT then z = MHEIGHT; end
return x,z
end
local function GetDist(x1,y1,z1, x2,y2,z2)
local d1 = x2-x1
local d2 = y2-y1
local d3 = z2-z1
return sqrt(d1*d1 + d2*d2 + d3*d3)
end
local function explode(div,str)
if (div=='') then return false end
local pos,arr = 0,{}
-- for each divider found
for st,sp in function() return string.find(str,div,pos,true) end do
table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider
pos = sp + 1 -- Jump past current divider
end
table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider
return arr
end
local function LimitZoom(a,b,c,sp,limit)
--Check if anyone reach max speed
local zox,zoy,zoz = a*sp,b*sp,c*sp
local maxZoom = max(abs(zox),abs(zoy),abs(zoz))
--Limit speed
maxZoom = min(maxZoom,limit)
--Normalize
local total = sqrt(zox^2+zoy^2+zoz^2)
zox,zoy,zoz = zox/total,zoy/total,zoz/total
--Reapply speed
return zox*maxZoom,zoy*maxZoom,zoz*maxZoom
end
local function GetSmoothOrGroundHeight(x,z,checkFreeMode) --only ScrollCam seems to want to ignore this when FreeMode is on
if options.smoothmeshscroll.value then
return spGetSmoothMeshHeight(x, z) or 0
else
if not (checkFreeMode and options.freemode.value) then
return spGetGroundHeight(x, z) or 0
else
-- in this case `x` and `z` might be off-map due to free mode
-- however, GetGroundHeight still works fine (returns closest edge point)
return spGetGroundHeight(x, z) or 0
end
end
end
local function GetMapBoundedGroundHeight(x,z)
--out of map. Bound coordinate to within map
x,z = GetMapBoundedCoords(x,z)
return spGetGroundHeight(x,z)
end
local function GetMapBoundedSmoothOrGroundHeight(x,z)
--out of map. Bound coordinate to within map
x,z = GetMapBoundedCoords(x,z)
return GetSmoothOrGroundHeight(x,z)
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local scrnRay_cache = {result={0,0,0,0,0,0,0}, previous={fov=1,inclination=99,azimuth=299,x=9999,y=9999,plane=-100,sphere=-100}}
local function OverrideTraceScreenRay(x,y,cs,planeToHit,sphereToHit,returnRayDistance) --this function provide an adjusted TraceScreenRay for null-space outside of the map (by msafwan)
local viewSizeY = vsy
local viewSizeX = vsx
if not vsy or not vsx then
viewSizeX, viewSizeY = widgetHandler:GetViewSizes()
end
--//Speedup//--
if scrnRay_cache.previous.fov==cs.fov
and scrnRay_cache.previous.inclination == cs.rx
and scrnRay_cache.previous.azimuth == cs.ry
and scrnRay_cache.previous.x ==x
and scrnRay_cache.previous.y == y
and scrnRay_cache.previous.plane == planeToHit
and scrnRay_cache.previous.sphere == sphereToHit
then --if camera Sphere coordinate & mouse position not change then use cached value
return scrnRay_cache.result[1],
scrnRay_cache.result[2],
scrnRay_cache.result[3],
scrnRay_cache.result[4],
scrnRay_cache.result[5],
scrnRay_cache.result[6],
scrnRay_cache.result[7]
end
local camPos = {px=cs.px,py=cs.py,pz=cs.pz}
local camRot = {rx=cs.rx,ry=cs.ry,rz=cs.rz}
local gx,gy,gz,rx,ry,rz,rayDist,cancelCache = TraceCursorToGround(viewSizeX,viewSizeY,{x=x,y=y} ,cs.fov, camPos, camRot,planeToHit,sphereToHit,returnRayDistance)
--//caching for efficiency
if not cancelCache then
scrnRay_cache.result[1] = gx
scrnRay_cache.result[2] = gy
scrnRay_cache.result[3] = gz
scrnRay_cache.result[4] = rx
scrnRay_cache.result[5] = ry
scrnRay_cache.result[6] = rz
scrnRay_cache.result[7] = rayDist
scrnRay_cache.previous.inclination =cs.rx
scrnRay_cache.previous.azimuth = cs.ry
scrnRay_cache.previous.x = x
scrnRay_cache.previous.y = y
scrnRay_cache.previous.fov = cs.fov
scrnRay_cache.previous.plane = planeToHit
scrnRay_cache.previous.sphere = sphereToHit
end
return gx,gy,gz,rx,ry,rz,rayDist
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--[[ --NOTE: is not yet used for the moment
local function MoveRotatedCam(cs, mxm, mym)
if not cs.dy then
return cs
end
-- forward, up, right, top, bottom, left, right
local camVecs = spGetCameraVectors()
local cf = camVecs.forward
local len = sqrt((cf[1] * cf[1]) + (cf[3] * cf[3]))
local dfx = cf[1] / len
local dfz = cf[3] / len
local cr = camVecs.right
local len = sqrt((cr[1] * cr[1]) + (cr[3] * cr[3]))
local drx = cr[1] / len
local drz = cr[3] / len
local vecDist = (- cs.py) / cs.dy
local ddx = (mxm * drx) + (mym * dfx)
local ddz = (mxm * drz) + (mym * dfz)
local gx1, gz1 = cs.px + vecDist*cs.dx, cs.pz + vecDist*cs.dz --note me: what does cs.dx mean?
local gx2, gz2 = cs.px + vecDist*cs.dx + ddx, cs.pz + vecDist*cs.dz + ddz
local extra = 500
if gx2 > MWIDTH + extra then
ddx = MWIDTH + extra - gx1
elseif gx2 < 0 - extra then