Skip to content

Commit 3db7630

Browse files
committed
This no longer confines the mouse to the window. This was an annoying issue where the mouse could never leave the game when in windowed mode. The mouse is still confined when playing the game with the mouse and unconfined when in the menu.
1 parent 226066d commit 3db7630

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

main/res/imgs/cursorHidden.png

1.26 KB
Loading

main/src/com/miloshpetrov/sol2/ui/SolInputManager.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
import com.badlogic.gdx.audio.Sound;
55
import com.badlogic.gdx.files.FileHandle;
66
import com.badlogic.gdx.graphics.Color;
7+
import com.badlogic.gdx.graphics.Pixmap;
78
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
89
import com.badlogic.gdx.math.Rectangle;
910
import com.badlogic.gdx.math.Vector2;
1011
import com.miloshpetrov.sol2.*;
1112
import com.miloshpetrov.sol2.common.SolColor;
1213
import com.miloshpetrov.sol2.common.SolMath;
1314
import com.miloshpetrov.sol2.files.FileManager;
15+
import com.miloshpetrov.sol2.game.DebugOptions;
1416
import com.miloshpetrov.sol2.game.SolGame;
1517
import com.miloshpetrov.sol2.GameOptions;
1618

@@ -53,7 +55,10 @@ public SolInputManager(TextureManager textureManager, float r) {
5355
myFlashPtr = new Ptr();
5456
myMousePos = new Vector2();
5557
myMousePrevPos = new Vector2();
56-
Gdx.input.setCursorCatched(true);
58+
59+
// We want the original mouse cursor to be hidden as we draw our own mouse cursor.
60+
Gdx.input.setCursorCatched(false);
61+
setMouseCursorHidden();
5762
myUiCursor = textureManager.getTex("ui/cursor", null);
5863
myScreens = new ArrayList<SolUiScreen>();
5964
myToRemove = new ArrayList<SolUiScreen>();
@@ -64,6 +69,19 @@ public SolInputManager(TextureManager textureManager, float r) {
6469
myHoverSound = Gdx.audio.newSound(hoverSoundFile);
6570
}
6671

72+
/**
73+
* Hides the mouse cursor by setting it to a transparent image.
74+
*/
75+
private void setMouseCursorHidden() {
76+
// When debugging the path has a "main" directory prepended
77+
String debugPath = (DebugOptions.DEV_ROOT_PATH == null) ? "" : DebugOptions.DEV_ROOT_PATH;
78+
79+
// Load the cursor image - the hotspot is where the click point is which is in the middle in this image.
80+
Pixmap pm = new Pixmap(Gdx.files.internal(debugPath + "res/imgs/cursorHidden.png"));
81+
Gdx.input.setCursorImage(pm, 0, 0);
82+
pm.dispose();
83+
}
84+
6785
public void maybeFlashPressed(int keyCode) {
6886
for (int i = 0, myScreensSize = myScreens.size(); i < myScreensSize; i++) {
6987
SolUiScreen screen = myScreens.get(i);
@@ -129,7 +147,17 @@ private static void setPtrPos(Ptr ptr, int screenX, int screenY) {
129147

130148
public void update(SolApplication cmp) {
131149
boolean mobile = cmp.isMobile();
132-
if (!mobile) maybeFixMousePos();
150+
SolGame game = cmp.getGame();
151+
152+
// This keeps the mouse within the window, but only when playing the game with the mouse.
153+
// All other times the mouse can freely leave and return.
154+
if (!mobile && (cmp.getOptions().controlType == GameOptions.CONTROL_MIXED || cmp.getOptions().controlType == GameOptions.CONTROL_MOUSE) &&
155+
game != null && getTopScreen() != game.getScreens().menuScreen) {
156+
if(!Gdx.input.isCursorCatched()) Gdx.input.setCursorCatched(true);
157+
maybeFixMousePos();
158+
} else {
159+
if(Gdx.input.isCursorCatched()) Gdx.input.setCursorCatched(false);
160+
}
133161

134162
updatePtrs();
135163

@@ -172,7 +200,6 @@ public void update(SolApplication cmp) {
172200
screen.updateCustom(cmp, myPtrs, clickedOutside);
173201
}
174202

175-
SolGame game = cmp.getGame();
176203
TutorialManager tutorialManager = game == null ? null : game.getTutMan();
177204
if (tutorialManager != null && tutorialManager.isFinished()) {
178205
cmp.finishGame();

0 commit comments

Comments
 (0)