1
- using System ;
1
+ using HarmonyLib ;
2
+ using System ;
2
3
using System . Collections . Generic ;
3
4
using System . Linq ;
5
+ using System . Reflection ;
4
6
using System . Text ;
5
7
using UnityEngine ;
6
8
using UnityEngine . EventSystems ;
@@ -21,90 +23,141 @@ public FreeCamPanel(UIBase owner) : base(owner)
21
23
22
24
public override string Name => "Freecam" ;
23
25
public override UIManager . Panels PanelType => UIManager . Panels . Freecam ;
24
-
25
26
public override int MinWidth => 400 ;
26
- public override int MinHeight => 300 ;
27
+ public override int MinHeight => 320 ;
27
28
public override Vector2 DefaultAnchorMin => new ( 0.4f , 0.4f ) ;
28
29
public override Vector2 DefaultAnchorMax => new ( 0.6f , 0.6f ) ;
29
30
public override bool NavButtonWanted => true ;
30
31
public override bool ShouldSaveActiveState => true ;
31
32
32
33
internal static bool inFreeCamMode ;
34
+ internal static bool usingGameCamera ;
33
35
internal static Camera ourCamera ;
34
36
internal static Camera lastMainCamera ;
35
- internal static Vector3 lastCameraMainPos ;
36
- internal static Quaternion lastCameraMainRot ;
37
+ internal static FreeCamBehaviour freeCamScript ;
37
38
38
39
internal static float desiredMoveSpeed = 10f ;
40
+
41
+ internal static Vector3 originalCameraPosition ;
42
+ internal static Quaternion originalCameraRotation ;
43
+
44
+ internal static Vector3 ? currentUserCameraPosition ;
45
+ internal static Quaternion ? currentUserCameraRotation ;
46
+
39
47
internal static Vector3 previousMousePosition ;
48
+
40
49
internal static Vector3 lastSetCameraPosition ;
41
50
42
51
static ButtonRef startStopButton ;
52
+ static Toggle useGameCameraToggle ;
43
53
static InputFieldRef positionInput ;
44
54
static InputFieldRef moveSpeedInput ;
45
55
static ButtonRef inspectButton ;
46
56
47
- void BeginFreecam ( )
57
+ internal static void BeginFreecam ( )
48
58
{
49
59
inFreeCamMode = true ;
50
60
51
61
previousMousePosition = InputManager . MousePosition ;
52
62
63
+ CacheMainCamera ( ) ;
64
+ SetupFreeCamera ( ) ;
65
+
66
+ inspectButton . GameObject . SetActive ( true ) ;
67
+ }
68
+
69
+ static void CacheMainCamera ( )
70
+ {
53
71
Camera currentMain = Camera . main ;
54
72
if ( currentMain )
55
73
{
56
74
lastMainCamera = currentMain ;
57
- lastCameraMainPos = currentMain . transform . position ;
58
- lastCameraMainRot = currentMain . transform . rotation ;
59
- lastMainCamera . enabled = false ;
75
+ originalCameraPosition = currentMain . transform . position ;
76
+ originalCameraRotation = currentMain . transform . rotation ;
77
+
78
+ if ( currentUserCameraPosition == null )
79
+ {
80
+ currentUserCameraPosition = currentMain . transform . position ;
81
+ currentUserCameraRotation = currentMain . transform . rotation ;
82
+ }
60
83
}
61
84
else
62
- lastCameraMainRot = Quaternion . identity ;
85
+ originalCameraRotation = Quaternion . identity ;
86
+ }
87
+
88
+ static void SetupFreeCamera ( )
89
+ {
90
+ if ( useGameCameraToggle . isOn )
91
+ {
92
+ if ( ! lastMainCamera )
93
+ {
94
+ ExplorerCore . LogWarning ( $ "There is no previous Camera found, reverting to default Free Cam.") ;
95
+ useGameCameraToggle . isOn = false ;
96
+ }
97
+ else
98
+ {
99
+ usingGameCamera = true ;
100
+ ourCamera = lastMainCamera ;
101
+ }
102
+ }
103
+
104
+ if ( ! useGameCameraToggle . isOn )
105
+ {
106
+ usingGameCamera = false ;
107
+
108
+ if ( lastMainCamera )
109
+ lastMainCamera . enabled = false ;
110
+ }
63
111
64
112
if ( ! ourCamera )
65
113
{
66
114
ourCamera = new GameObject ( "UE_Freecam" ) . AddComponent < Camera > ( ) ;
67
115
ourCamera . gameObject . tag = "MainCamera" ;
68
116
GameObject . DontDestroyOnLoad ( ourCamera . gameObject ) ;
69
117
ourCamera . gameObject . hideFlags = HideFlags . HideAndDontSave ;
70
- ourCamera . gameObject . AddComponent < FreeCamBehaviour > ( ) ;
71
118
}
72
119
73
- ourCamera . gameObject . SetActive ( true ) ;
74
- ourCamera . transform . position = lastCameraMainPos ;
75
- ourCamera . transform . rotation = lastCameraMainRot ;
120
+ if ( ! freeCamScript )
121
+ freeCamScript = ourCamera . gameObject . AddComponent < FreeCamBehaviour > ( ) ;
76
122
77
- inspectButton . GameObject . SetActive ( true ) ;
123
+ ourCamera . transform . position = ( Vector3 ) currentUserCameraPosition ;
124
+ ourCamera . transform . rotation = ( Quaternion ) currentUserCameraRotation ;
125
+
126
+ ourCamera . gameObject . SetActive ( true ) ;
127
+ ourCamera . enabled = true ;
78
128
}
79
129
80
- void EndFreecam ( )
130
+ internal static void EndFreecam ( )
81
131
{
82
132
inFreeCamMode = false ;
83
133
134
+ if ( usingGameCamera )
135
+ {
136
+ ourCamera = null ;
137
+
138
+ if ( lastMainCamera )
139
+ {
140
+ lastMainCamera . transform . position = originalCameraPosition ;
141
+ lastMainCamera . transform . rotation = originalCameraRotation ;
142
+ }
143
+ }
144
+
84
145
if ( ourCamera )
85
146
ourCamera . gameObject . SetActive ( false ) ;
86
147
else
87
148
inspectButton . GameObject . SetActive ( false ) ;
88
149
89
- if ( lastMainCamera )
90
- lastMainCamera . enabled = true ;
91
- }
92
-
93
- void SetToggleButtonState ( )
94
- {
95
- if ( inFreeCamMode )
150
+ if ( freeCamScript )
96
151
{
97
- RuntimeHelper . SetColorBlockAuto ( startStopButton . Component , new ( 0.4f , 0.2f , 0.2f ) ) ;
98
- startStopButton . ButtonText . text = "End Freecam" ;
99
- }
100
- else
101
- {
102
- RuntimeHelper . SetColorBlockAuto ( startStopButton . Component , new ( 0.2f , 0.4f , 0.2f ) ) ;
103
- startStopButton . ButtonText . text = "Begin Freecam" ;
152
+ GameObject . Destroy ( freeCamScript ) ;
153
+ freeCamScript = null ;
104
154
}
155
+
156
+ if ( lastMainCamera )
157
+ lastMainCamera . enabled = true ;
105
158
}
106
159
107
- void SetCameraPosition ( Vector3 pos )
160
+ static void SetCameraPosition ( Vector3 pos )
108
161
{
109
162
if ( ! ourCamera || lastSetCameraPosition == pos )
110
163
return ;
@@ -122,16 +175,30 @@ internal static void UpdatePositionInput()
122
175
positionInput . Text = ParseUtility . ToStringForInput < Vector3 > ( lastSetCameraPosition ) ;
123
176
}
124
177
178
+ // ~~~~~~~~ UI construction / callbacks ~~~~~~~~
179
+
125
180
protected override void ConstructPanelContent ( )
126
181
{
127
182
startStopButton = UIFactory . CreateButton ( ContentRoot , "ToggleButton" , "Freecam" ) ;
128
183
UIFactory . SetLayoutElement ( startStopButton . GameObject , minWidth : 150 , minHeight : 25 , flexibleWidth : 9999 ) ;
129
- startStopButton . OnClick += ToggleButton_OnClick ;
184
+ startStopButton . OnClick += StartStopButton_OnClick ;
130
185
SetToggleButtonState ( ) ;
131
186
132
187
AddSpacer ( 5 ) ;
133
188
134
- AddInputField ( "Position" , "Camera Pos:" , "eg. 0 0 0" , out positionInput , PositionInput_OnEndEdit ) ;
189
+ GameObject toggleObj = UIFactory . CreateToggle ( ContentRoot , "UseGameCameraToggle" , out useGameCameraToggle , out Text toggleText ) ;
190
+ UIFactory . SetLayoutElement ( toggleObj , minHeight : 25 , flexibleWidth : 9999 ) ;
191
+ useGameCameraToggle . onValueChanged . AddListener ( OnUseGameCameraToggled ) ;
192
+ useGameCameraToggle . isOn = false ;
193
+ toggleText . text = "Use Game Camera?" ;
194
+
195
+ AddSpacer ( 5 ) ;
196
+
197
+ GameObject posRow = AddInputField ( "Position" , "Freecam Pos:" , "eg. 0 0 0" , out positionInput , PositionInput_OnEndEdit ) ;
198
+
199
+ ButtonRef resetPosButton = UIFactory . CreateButton ( posRow , "ResetButton" , "Reset" ) ;
200
+ UIFactory . SetLayoutElement ( resetPosButton . GameObject , minWidth : 70 , minHeight : 25 ) ;
201
+ resetPosButton . OnClick += OnResetPosButtonClicked ;
135
202
136
203
AddSpacer ( 5 ) ;
137
204
@@ -141,11 +208,11 @@ protected override void ConstructPanelContent()
141
208
AddSpacer ( 5 ) ;
142
209
143
210
string instructions = @"Controls:
144
- - WASD/ Arrows: Movement
145
- - Space/ PgUp: Move up
146
- - LeftControl/ PgDown: Move down
211
+ - WASD / Arrows: Movement
212
+ - Space / PgUp: Move up
213
+ - LeftCtrl / PgDown: Move down
147
214
- Right Mouse Button: Free look
148
- - Left Shift: Super speed" ;
215
+ - Shift: Super speed" ;
149
216
150
217
Text instructionsText = UIFactory . CreateLabel ( ContentRoot , "Instructions" , instructions , TextAnchor . UpperLeft ) ;
151
218
UIFactory . SetLayoutElement ( instructionsText . gameObject , flexibleWidth : 9999 , flexibleHeight : 9999 ) ;
@@ -180,7 +247,7 @@ GameObject AddInputField(string name, string labelText, string placeHolder, out
180
247
return row ;
181
248
}
182
249
183
- void ToggleButton_OnClick ( )
250
+ void StartStopButton_OnClick ( )
184
251
{
185
252
EventSystemHelper . SetSelectedGameObject ( null ) ;
186
253
@@ -192,7 +259,44 @@ void ToggleButton_OnClick()
192
259
SetToggleButtonState ( ) ;
193
260
}
194
261
195
- private void PositionInput_OnEndEdit ( string input )
262
+ void SetToggleButtonState ( )
263
+ {
264
+ if ( inFreeCamMode )
265
+ {
266
+ RuntimeHelper . SetColorBlockAuto ( startStopButton . Component , new ( 0.4f , 0.2f , 0.2f ) ) ;
267
+ startStopButton . ButtonText . text = "End Freecam" ;
268
+ }
269
+ else
270
+ {
271
+ RuntimeHelper . SetColorBlockAuto ( startStopButton . Component , new ( 0.2f , 0.4f , 0.2f ) ) ;
272
+ startStopButton . ButtonText . text = "Begin Freecam" ;
273
+ }
274
+ }
275
+
276
+ void OnUseGameCameraToggled ( bool value )
277
+ {
278
+ EventSystemHelper . SetSelectedGameObject ( null ) ;
279
+
280
+ if ( ! inFreeCamMode )
281
+ return ;
282
+
283
+ EndFreecam ( ) ;
284
+ BeginFreecam ( ) ;
285
+ }
286
+
287
+ void OnResetPosButtonClicked ( )
288
+ {
289
+ currentUserCameraPosition = originalCameraPosition ;
290
+ currentUserCameraRotation = originalCameraRotation ;
291
+
292
+ if ( inFreeCamMode && ourCamera )
293
+ {
294
+ ourCamera . transform . position = ( Vector3 ) currentUserCameraPosition ;
295
+ ourCamera . transform . rotation = ( Quaternion ) currentUserCameraRotation ;
296
+ }
297
+ }
298
+
299
+ void PositionInput_OnEndEdit ( string input )
196
300
{
197
301
EventSystemHelper . SetSelectedGameObject ( null ) ;
198
302
@@ -206,7 +310,7 @@ private void PositionInput_OnEndEdit(string input)
206
310
SetCameraPosition ( parsed ) ;
207
311
}
208
312
209
- private void MoveSpeedInput_OnEndEdit ( string input )
313
+ void MoveSpeedInput_OnEndEdit ( string input )
210
314
{
211
315
EventSystemHelper . SetSelectedGameObject ( null ) ;
212
316
@@ -236,8 +340,17 @@ internal void Update()
236
340
{
237
341
if ( FreeCamPanel . inFreeCamMode )
238
342
{
343
+ if ( ! FreeCamPanel . ourCamera )
344
+ {
345
+ FreeCamPanel . EndFreecam ( ) ;
346
+ return ;
347
+ }
348
+
239
349
Transform transform = FreeCamPanel . ourCamera . transform ;
240
350
351
+ FreeCamPanel . currentUserCameraPosition = transform . position ;
352
+ FreeCamPanel . currentUserCameraRotation = transform . rotation ;
353
+
241
354
float moveSpeed = FreeCamPanel . desiredMoveSpeed * Time . deltaTime ;
242
355
243
356
if ( InputManager . GetKey ( KeyCode . LeftShift ) || InputManager . GetKey ( KeyCode . RightShift ) )
0 commit comments