Skip to content

Commit

Permalink
Add jngl::hideMouse to hide the mouse cursor using RAII
Browse files Browse the repository at this point in the history
  • Loading branch information
jhasse committed Nov 24, 2024
1 parent e23a7b3 commit 1aa3de0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/jngl/input.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2012-2021 Jan Niklas Hasse <jhasse@bixense.com>
// Copyright 2012-2024 Jan Niklas Hasse <jhasse@bixense.com>
// For conditions of distribution and use, see copyright notice in LICENSE.txt

#include "../windowptr.hpp"
Expand Down Expand Up @@ -53,4 +53,9 @@ std::string getTextInput() {
return pWindow->getTextInput();
}

Finally hideMouse() {
pWindow->increaseMouseHiddenCount();
return Finally([] { pWindow->decreaseMouseHiddenCount(); });
}

} // namespace jngl
4 changes: 4 additions & 0 deletions src/jngl/input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/// @file
#pragma once

#include "Finally.hpp"
#include "Vec2.hpp"

#include <functional>
Expand Down Expand Up @@ -128,6 +129,9 @@ bool getRelativeMouseMode();
/// By default the mouse cursor of the OS is visible and can be hidden by passing false
void setMouseVisible(bool visible);

/// Hides the mouse cursor; destroying the returned Finally object will show it again
[[nodiscard]] Finally hideMouse();

/// Returns whether the mouse cursor of the OS is currently visible
///
/// Even when this method returns true, it could still be visible outside of the window or hidden by
Expand Down
15 changes: 15 additions & 0 deletions src/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,21 @@ bool Window::getRelativeMouseMode() const {
return relativeMouseMode;
}

void Window::increaseMouseHiddenCount() {
if (mouseHiddenCount == 0) {
setMouseVisible(false);
}
++mouseHiddenCount;
}

void Window::decreaseMouseHiddenCount() {
assert(mouseHiddenCount > 0);
--mouseHiddenCount;
if (mouseHiddenCount == 0) {
setMouseVisible(true);
}
}

int Window::getCanvasWidth() const {
return canvasWidth;
}
Expand Down
3 changes: 3 additions & 0 deletions src/window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class Window {
void SetRelativeMouseMode(bool relative);
bool getRelativeMouseMode() const;
void SetMouseVisible(bool visible);
void increaseMouseHiddenCount();
void decreaseMouseHiddenCount();
bool getMouseVisible() const;
bool isMultitouch() const;
std::vector<Vec2> getTouchPositions() const;
Expand Down Expand Up @@ -177,6 +179,7 @@ class Window {
unsigned int previousStepsPerFrame = 1;
double lastCheckTime;
unsigned int stepsSinceLastCheck;
int mouseHiddenCount = 0;

/// When VSYNC is active we will try to find out to what FPS/Hz the display is limiting us
double maxFPS;
Expand Down

0 comments on commit 1aa3de0

Please sign in to comment.