Fix mouse capture behavior for Editor Viewport (#3417)

* first pass of fixes for cursor capture and context menu

Signed-off-by: hultonha <hultonha@amazon.co.uk>

* restore previous behavior of HandleMouseMoveEvent

Signed-off-by: hultonha <hultonha@amazon.co.uk>

* tidy-up from previous cursor/input changes

Signed-off-by: hultonha <hultonha@amazon.co.uk>

* add missing casts

Signed-off-by: hultonha <hultonha@amazon.co.uk>

* small updates to support tests

Signed-off-by: hultonha <hultonha@amazon.co.uk>

* additional tests and some tidy-up

Signed-off-by: hultonha <hultonha@amazon.co.uk>

* small updates before publishing PR (comment/naming updates)

Signed-off-by: hultonha <hultonha@amazon.co.uk>

* add missing parameter to MouseInteractionEvent constructor

Signed-off-by: hultonha <hultonha@amazon.co.uk>
This commit is contained in:
hultonha
2021-08-25 09:28:41 +01:00
committed by GitHub
parent bb90c4ddfe
commit 7fbfda0371
15 changed files with 231 additions and 49 deletions
@@ -144,6 +144,7 @@ namespace AzFramework
if (const auto& cursor = AZStd::get_if<CursorEvent>(&event))
{
m_cursorState.SetCurrentPosition(cursor->m_position);
m_cursorState.SetCaptured(cursor->m_captured);
}
else if (const auto& horizontalMotion = AZStd::get_if<HorizontalMotionEvent>(&event))
{
@@ -790,17 +791,24 @@ namespace AzFramework
const auto* position = inputChannel.GetCustomData<AzFramework::InputChannel::PositionData2D>();
AZ_Assert(position, "Expected PositionData2D but found nullptr");
return CursorEvent{ ScreenPoint(
static_cast<int>(position->m_normalizedPosition.GetX() * windowSize.m_width),
static_cast<int>(position->m_normalizedPosition.GetY() * windowSize.m_height)) };
auto currentCursorState = AzFramework::SystemCursorState::Unknown;
AzFramework::InputSystemCursorRequestBus::EventResult(
currentCursorState, inputDeviceId, &AzFramework::InputSystemCursorRequestBus::Events::GetSystemCursorState);
const auto x = position->m_normalizedPosition.GetX() * aznumeric_cast<float>(windowSize.m_width);
const auto y = position->m_normalizedPosition.GetY() * aznumeric_cast<float>(windowSize.m_height);
return CursorEvent{ ScreenPoint(aznumeric_cast<int>(AZStd::lround(x)), aznumeric_cast<int>(AZStd::lround(y))),
currentCursorState == AzFramework::SystemCursorState::ConstrainedAndHidden };
}
else if (inputChannelId == InputDeviceMouse::Movement::X)
{
return HorizontalMotionEvent{ aznumeric_cast<int>(inputChannel.GetValue()) };
const auto x = inputChannel.GetValue();
return HorizontalMotionEvent{ aznumeric_cast<int>(AZStd::lround(x)) };
}
else if (inputChannelId == InputDeviceMouse::Movement::Y)
{
return VerticalMotionEvent{ aznumeric_cast<int>(inputChannel.GetValue()) };
const auto y = inputChannel.GetValue();
return VerticalMotionEvent{ aznumeric_cast<int>(AZStd::lround(y)) };
}
else if (inputChannelId == InputDeviceMouse::Movement::Z)
{
@@ -88,6 +88,7 @@ namespace AzFramework
struct CursorEvent
{
ScreenPoint m_position;
bool m_captured = false;
};
struct ScrollEvent
@@ -21,6 +21,8 @@ namespace AzFramework
[[nodiscard]] ScreenVector CursorDelta() const;
//! Call this in a 'handle event' call to update the most recent cursor position.
void SetCurrentPosition(const ScreenPoint& currentPosition);
//! Set whether the cursor is currently being constrained (and hidden).
void SetCaptured(bool captured);
//! Call this in an 'update' call to copy the current cursor position to the last
//! cursor position.
void Update();
@@ -28,8 +30,14 @@ namespace AzFramework
private:
AZStd::optional<ScreenPoint> m_lastCursorPosition;
AZStd::optional<ScreenPoint> m_currentCursorPosition;
bool m_captured = false;
};
inline void CursorState::SetCaptured(const bool captured)
{
m_captured = captured;
}
inline void CursorState::SetCurrentPosition(const ScreenPoint& currentPosition)
{
m_currentCursorPosition = currentPosition;
@@ -44,9 +52,16 @@ namespace AzFramework
inline void CursorState::Update()
{
if (m_currentCursorPosition.has_value())
if (!m_captured)
{
m_lastCursorPosition = m_currentCursorPosition;
if (m_currentCursorPosition.has_value())
{
m_lastCursorPosition = m_currentCursorPosition;
}
}
else
{
m_currentCursorPosition = m_lastCursorPosition;
}
}
} // namespace AzFramework