Skip to content

Commit

Permalink
add mouse locking
Browse files Browse the repository at this point in the history
  • Loading branch information
xpenatan committed Oct 15, 2023
1 parent ed1fa1a commit 1841841
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- Add resources automatically when module contains META-INF/gdx-teavm.properties
- Fix freetype shadow error
- Improve rendering performance
- Add mouse locking

[1.0.0-b6]
- Update libgdx to version 1.12.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.github.xpenatan.gdx.backends.teavm.dom.TouchWrapper;
import com.github.xpenatan.gdx.backends.teavm.dom.WheelEventWrapper;
import com.github.xpenatan.gdx.backends.teavm.utils.KeyCodes;
import org.teavm.jso.JSBody;
import org.teavm.jso.browser.Window;

/**
Expand Down Expand Up @@ -306,7 +307,7 @@ private void handleKeyboardEvents(EventWrapper e) {
break;
}

if (isCatchKey(code)) {
if(isCatchKey(code)) {
e.preventDefault();
}

Expand All @@ -330,7 +331,7 @@ private void handleKeyboardEvents(EventWrapper e) {
}

// prevent TAB-key propagation, i.e. we handle ourselves!
if (code == Keys.TAB) {
if(code == Keys.TAB) {
e.preventDefault();
e.stopPropagation();
}
Expand All @@ -341,7 +342,7 @@ else if(type.equals("keypress") && hasFocus) {
if(processor != null) processor.keyTyped(c);

// prevent TAB-key propagation, i.e. we handle ourselves!
if (c == '\t') {
if(c == '\t') {
e.preventDefault();
e.stopPropagation();
}
Expand All @@ -351,7 +352,7 @@ else if(type.equals("keyup") && hasFocus) {
KeyboardEventWrapper keyboardEvent = (KeyboardEventWrapper)e;
int code = KeyCodes.keyForCode(keyboardEvent.getKeyCode());

if (isCatchKey(code)) {
if(isCatchKey(code)) {
e.preventDefault();
}

Expand All @@ -364,7 +365,7 @@ else if(type.equals("keyup") && hasFocus) {
}

// prevent TAB-key propagation, i.e. we handle ourselves!
if (code == Keys.TAB) {
if(code == Keys.TAB) {
e.preventDefault();
e.stopPropagation();
}
Expand Down Expand Up @@ -716,37 +717,38 @@ public long getCurrentEventTime() {
}

@Override
public boolean isCatchBackKey () {
public boolean isCatchBackKey() {
return keysToCatch.contains(Keys.BACK);
}

@Override
public void setCatchBackKey (boolean catchBack) {
public void setCatchBackKey(boolean catchBack) {
setCatchKey(Keys.BACK, catchBack);
}

@Override
public boolean isCatchMenuKey () {
public boolean isCatchMenuKey() {
return keysToCatch.contains(Keys.MENU);
}

@Override
public void setCatchMenuKey (boolean catchMenu) {
public void setCatchMenuKey(boolean catchMenu) {
setCatchKey(Keys.MENU, catchMenu);
}

@Override
public void setCatchKey (int keycode, boolean catchKey) {
if (!catchKey) {
public void setCatchKey(int keycode, boolean catchKey) {
if(!catchKey) {
keysToCatch.remove(keycode);
} else {
}
else {
keysToCatch.add(keycode);
}
}

@Override
public boolean isCatchKey (int keycode) {
return keysToCatch.contains(keycode);
public boolean isCatchKey(int keycode) {
return keysToCatch.contains(keycode);
}

@Override
Expand Down Expand Up @@ -789,14 +791,17 @@ public Orientation getNativeOrientation() {

@Override
public void setCursorCatched(boolean catched) {
// TODO Auto-generated method stub

if(catched) {
setCursorCatchedJSNI(canvas);
}
else {
exitCursorCatchedJSNI();
}
}

@Override
public boolean isCursorCatched() {
// TODO Auto-generated method stub
return false;
return isCursorCatchedJSNI(canvas);
}

@Override
Expand All @@ -815,4 +820,40 @@ public void setOnscreenKeyboardVisible(boolean visible, OnscreenKeyboardType typ
// TODO Auto-generated method stub

}

/**
* from https://github.com/toji/game-shim/blob/master/game-shim.js
*
* @param element Canvas
*/
@JSBody(params = "element", script =
"if (!element.requestPointerLock) {\n" +
" element.requestPointerLock = (function() {\n" +
" return element.webkitRequestPointerLock || element.mozRequestPointerLock;" +
" })();\n" +
"}\n" +
"element.requestPointerLock();"
)
private static native void setCursorCatchedJSNI(HTMLElementWrapper element);

/**
* from https://github.com/toji/game-shim/blob/master/game-shim.js
*/
@JSBody(script =
"document.exitPointerLock();"
)
private static native void exitCursorCatchedJSNI();

/**
* from https://github.com/toji/game-shim/blob/master/game-shim.js
*
* @return is Cursor catched
*/
@JSBody(params = "element", script =
"if (document.pointerLockElement === canvas || document.mozPointerLockElement === canvas) {\n" +
" return true;\n" +
"}\n" +
"return false;"
)
private static native boolean isCursorCatchedJSNI(HTMLElementWrapper canvas);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void create() {
renderer = new ShapeRenderer();
Gdx.input.setInputProcessor(this);
Gdx.app.setLogLevel(Application.LOG_DEBUG);
Gdx.input.setCursorCatched(true);
// Gdx.input.setCursorCatched(true);
}

@Override
Expand Down Expand Up @@ -112,6 +112,11 @@ public boolean keyTyped(char character) {
@Override
public boolean keyUp(int keycode) {
Gdx.app.log("GdxInputTest", "key up: " + keycode);

if(keycode == Keys.F2) {
boolean isCursorCatched = Gdx.input.isCursorCatched();
Gdx.input.setCursorCatched(!isCursorCatched);
}
return false;
}
}

0 comments on commit 1841841

Please sign in to comment.