1
1
#include " SpectatorCameraComponent.h"
2
+
3
+ #include " AzCore/Settings/SettingsRegistry.h"
4
+
2
5
#include < AzCore/Component/TransformBus.h>
3
6
#include < AzCore/Math/MathUtils.h>
4
7
#include < AzCore/Serialization/EditContext.h>
7
10
8
11
namespace RobotecSpectatorCamera
9
12
{
13
+ constexpr AZStd::string_view CenterTheCursorConfigurationKey = " /O3DE/SpectatorCamera/MoveCursorToTheCenter" ;
14
+
10
15
SpectatorCameraComponent::SpectatorCameraComponent (
11
16
const RobotecSpectatorCamera::SpectatorCameraConfiguration& spectatorCameraConfiguration)
12
17
: m_configuration(spectatorCameraConfiguration)
@@ -31,6 +36,13 @@ namespace RobotecSpectatorCamera
31
36
AzFramework::InputChannelEventListener::Connect ();
32
37
33
38
AZ::TransformBus::EventResult (m_currentTransform, GetEntityId (), &AZ::TransformBus::Events::GetWorldTM);
39
+
40
+ auto * registry = AZ::SettingsRegistry::Get ();
41
+ AZ_Assert (registry, " No Registry available" );
42
+ if (registry)
43
+ {
44
+ registry->Get (m_centerTheCursor, CenterTheCursorConfigurationKey);
45
+ }
34
46
}
35
47
36
48
void SpectatorCameraComponent::Deactivate ()
@@ -106,7 +118,7 @@ namespace RobotecSpectatorCamera
106
118
SpectatorCameraConfiguration::OrbitRadiusMax);
107
119
}
108
120
109
- if (channelId == AzFramework::InputDeviceMouse::Button::Right || m_configuration.m_cameraMode == CameraMode::FreeFlying )
121
+ if (channelId == AzFramework::InputDeviceMouse::Button::Right && m_configuration.m_cameraMode == CameraMode::ThirdPerson )
110
122
{
111
123
AzFramework::SystemCursorState currentCursorState;
112
124
AzFramework::InputSystemCursorRequestBus::EventResult (
@@ -131,14 +143,17 @@ namespace RobotecSpectatorCamera
131
143
{
132
144
m_isRightMouseButtonPressed = false ;
133
145
134
- // Restore the cursor's original position
135
- AzFramework::InputSystemCursorRequestBus::Event (
136
- AzFramework::InputDeviceMouse::Id,
137
- &AzFramework::InputSystemCursorRequests::SetSystemCursorPositionNormalized,
138
- m_initialMousePosition);
139
-
140
- // Update m_lastMousePosition to the restored position to prevent the jump on the next rotation start
141
- m_lastMousePosition = m_initialMousePosition;
146
+ if (m_centerTheCursor)
147
+ {
148
+ // Restore the cursor's original position
149
+ AzFramework::InputSystemCursorRequestBus::Event (
150
+ AzFramework::InputDeviceMouse::Id,
151
+ &AzFramework::InputSystemCursorRequests::SetSystemCursorPositionNormalized,
152
+ m_initialMousePosition);
153
+
154
+ // Update m_lastMousePosition to the restored position to prevent the jump on the next rotation start
155
+ m_lastMousePosition = m_initialMousePosition;
156
+ }
142
157
AzFramework::InputSystemCursorRequestBus::Event (
143
158
AzFramework::InputDeviceMouse::Id,
144
159
&AzFramework::InputSystemCursorRequests::SetSystemCursorState,
@@ -160,16 +175,23 @@ namespace RobotecSpectatorCamera
160
175
AZ::Vector2 currentMousePosition = GetCurrentMousePosition ();
161
176
AZ::Vector2 mouseDelta = currentMousePosition - m_lastMousePosition;
162
177
163
- RotateCameraOnMouse (mouseDelta);
178
+ if (m_centerTheCursor)
179
+ {
180
+ const auto center = AZ::Vector2 (0 .5f , 0 .5f );
164
181
165
- const auto center = AZ::Vector2 (0 .5f , 0 .5f );
182
+ // Recenter the cursor to avoid edge constraints
183
+ AzFramework::InputSystemCursorRequestBus::Event (
184
+ AzFramework::InputDeviceMouse::Id, &AzFramework::InputSystemCursorRequests::SetSystemCursorPositionNormalized, center);
166
185
167
- // Recenter the cursor to avoid edge constraints
168
- AzFramework::InputSystemCursorRequestBus::Event (
169
- AzFramework::InputDeviceMouse::Id, &AzFramework::InputSystemCursorRequests::SetSystemCursorPositionNormalized, center);
186
+ // Then update m_lastMousePosition accordingly
187
+ m_lastMousePosition = center;
188
+ }
189
+ else
190
+ {
191
+ m_lastMousePosition = currentMousePosition;
192
+ }
170
193
171
- // Then update m_lastMousePosition accordingly
172
- m_lastMousePosition = center;
194
+ RotateCameraOnMouse (mouseDelta);
173
195
}
174
196
}
175
197
@@ -275,6 +297,9 @@ namespace RobotecSpectatorCamera
275
297
276
298
void SpectatorCameraComponent::ToggleCameraMode ()
277
299
{
300
+ // RMB should be handled only in the ThirdPerson mode. This line fixes problem with such combination:
301
+ // RMB pressed -> Key C pressed -> FreeFlying -> RMB released -> Key C pressed -> ThirdPerson -> Camera rotates without pressing RMB
302
+ m_isRightMouseButtonPressed = false ;
278
303
if (m_configuration.m_cameraMode == CameraMode::ThirdPerson)
279
304
{
280
305
m_configuration.m_cameraMode = CameraMode::FreeFlying;
0 commit comments