[UI] Capture mouse.

This commit is contained in:
Joel Linn 2020-04-20 02:16:14 +02:00 committed by Rick Gibbed
parent d1315438f3
commit 6a1c134cf3
5 changed files with 57 additions and 10 deletions

View File

@ -268,16 +268,27 @@ void ImGuiDrawer::OnKeyChar(KeyEvent* e) {
void ImGuiDrawer::OnMouseDown(MouseEvent* e) { void ImGuiDrawer::OnMouseDown(MouseEvent* e) {
auto& io = GetIO(); auto& io = GetIO();
io.MousePos = ImVec2(float(e->x()), float(e->y())); io.MousePos = ImVec2(float(e->x()), float(e->y()));
int button = -1;
switch (e->button()) { switch (e->button()) {
case xe::ui::MouseEvent::Button::kLeft: { case xe::ui::MouseEvent::Button::kLeft: {
io.MouseDown[0] = true; button = 0;
} break; break;
}
case xe::ui::MouseEvent::Button::kRight: { case xe::ui::MouseEvent::Button::kRight: {
io.MouseDown[1] = true; button = 1;
} break; break;
}
default: { default: {
// Ignored. // Ignored.
} break; break;
}
}
if (button >= 0 && button < std::size(io.MouseDown)) {
if (!ImGui::IsAnyMouseDown()) {
window_->CaptureMouse();
}
io.MouseDown[button] = true;
} }
} }
@ -289,16 +300,27 @@ void ImGuiDrawer::OnMouseMove(MouseEvent* e) {
void ImGuiDrawer::OnMouseUp(MouseEvent* e) { void ImGuiDrawer::OnMouseUp(MouseEvent* e) {
auto& io = GetIO(); auto& io = GetIO();
io.MousePos = ImVec2(float(e->x()), float(e->y())); io.MousePos = ImVec2(float(e->x()), float(e->y()));
int button = -1;
switch (e->button()) { switch (e->button()) {
case xe::ui::MouseEvent::Button::kLeft: { case xe::ui::MouseEvent::Button::kLeft: {
io.MouseDown[0] = false; button = 0;
} break; break;
}
case xe::ui::MouseEvent::Button::kRight: { case xe::ui::MouseEvent::Button::kRight: {
io.MouseDown[1] = false; button = 1;
} break; break;
}
default: { default: {
// Ignored. // Ignored.
} break; break;
}
}
if (button >= 0 && button < std::size(io.MouseDown)) {
io.MouseDown[button] = false;
if (!ImGui::IsAnyMouseDown()) {
window_->ReleaseMouse();
}
} }
} }

View File

@ -59,6 +59,9 @@ class Window {
virtual bool SetIcon(const void* buffer, size_t size) = 0; virtual bool SetIcon(const void* buffer, size_t size) = 0;
void ResetIcon() { SetIcon(nullptr, 0); } void ResetIcon() { SetIcon(nullptr, 0); }
virtual bool CaptureMouse() = 0;
virtual bool ReleaseMouse() = 0;
virtual bool is_fullscreen() const { return false; } virtual bool is_fullscreen() const { return false; }
virtual void ToggleFullscreen(bool fullscreen) {} virtual void ToggleFullscreen(bool fullscreen) {}

View File

@ -42,6 +42,10 @@ class GTKWindow : public Window {
bool SetIcon(const void* buffer, size_t size) override; bool SetIcon(const void* buffer, size_t size) override;
// This seems to happen implicitly compared to Windows.
bool CaptureMouse() override { return true; };
bool ReleaseMouse() override { return true; };
bool is_fullscreen() const override; bool is_fullscreen() const override;
void ToggleFullscreen(bool fullscreen) override; void ToggleFullscreen(bool fullscreen) override;

View File

@ -236,6 +236,21 @@ bool Win32Window::SetIcon(const void* buffer, size_t size) {
return false; return false;
} }
bool Win32Window::CaptureMouse() {
if (GetCapture() != nullptr) {
return false;
}
SetCapture(hwnd_);
return true;
}
bool Win32Window::ReleaseMouse() {
if (GetCapture() != hwnd_) {
return false;
}
return ReleaseCapture() != 0;
}
bool Win32Window::is_fullscreen() const { return fullscreen_; } bool Win32Window::is_fullscreen() const { return fullscreen_; }
// https://blogs.msdn.microsoft.com/oldnewthing/20131017-00/?p=2903 // https://blogs.msdn.microsoft.com/oldnewthing/20131017-00/?p=2903

View File

@ -38,6 +38,9 @@ class Win32Window : public Window {
bool SetIcon(const void* buffer, size_t size) override; bool SetIcon(const void* buffer, size_t size) override;
bool CaptureMouse() override;
bool ReleaseMouse() override;
bool is_fullscreen() const override; bool is_fullscreen() const override;
void ToggleFullscreen(bool fullscreen) override; void ToggleFullscreen(bool fullscreen) override;