Skip to content

make mouse stay in window area when camera movement is enabled #167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 36 additions & 5 deletions 61_UI/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1727,6 +1727,17 @@ class UISampleApp final : public examples::SimpleWindowedApplication
default: break;
}

{
if (ImGui::TreeNodeEx("Cursor Behaviour"))
{
if (ImGui::RadioButton("Clamp to the window", !resetCursorToCenter))
resetCursorToCenter = false;
if (ImGui::RadioButton("Reset to the window center", resetCursorToCenter))
resetCursorToCenter = true;
ImGui::TreePop();
}
}

{
ImGuiIO& io = ImGui::GetIO();

Expand All @@ -1735,21 +1746,39 @@ class UISampleApp final : public examples::SimpleWindowedApplication

if (enableActiveCameraMovement)
{
ImGui::TextColored(ImVec4(0.0f, 1.0f, 0.0f, 1.0f), "Bound Camera Movement: Enabled");
io.ConfigFlags |= ImGuiConfigFlags_NoMouse;
io.MouseDrawCursor = false;
io.WantCaptureMouse = false;

ImVec2 cursorPos = ImGui::GetMousePos();
ImVec2 viewportSize = io.DisplaySize;
auto* cc = m_window->getCursorControl();
int32_t posX = m_window->getX();
int32_t posY = m_window->getY();

if (resetCursorToCenter)
{
const ICursorControl::SPosition middle{ static_cast<int32_t>(viewportSize.x / 2 + posX), static_cast<int32_t>(viewportSize.y / 2 + posY) };
cc->setPosition(middle);
}
else
{
auto currentCursorPos = cc->getPosition();
ICursorControl::SPosition newPos{};
newPos.x = std::clamp<int32_t>(currentCursorPos.x, posX, viewportSize.x + posX);
newPos.y = std::clamp<int32_t>(currentCursorPos.y, posY, viewportSize.y + posY);
cc->setPosition(newPos);
}
}
else
{
ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "Bound Camera Movement: Disabled");
io.ConfigFlags &= ~ImGuiConfigFlags_NoMouse;
io.MouseDrawCursor = true;
io.WantCaptureMouse = true;
}

if (enableActiveCameraMovement)
ImGui::TextColored(ImVec4(0.0f, 1.0f, 0.0f, 1.0f), "Bound Camera Movement: Enabled");
else
ImGui::TextColored(ImVec4(1.0f, 0.0f, 0.0f, 1.0f), "Bound Camera Movement: Disabled");


if (ImGui::IsItemHovered())
{
Expand Down Expand Up @@ -2189,6 +2218,8 @@ class UISampleApp final : public examples::SimpleWindowedApplication

bool enableActiveCameraMovement = false;

bool resetCursorToCenter = false;

struct windowControlBinding
{
nbl::core::smart_refctd_ptr<CScene> scene;
Expand Down