public
Last active

LibGDX: Hide Mouse Cursor (Desktop)

  • Download Gist
HideCursor.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
import java.nio.IntBuffer;
 
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Mouse;
 
import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.GdxRuntimeException;
 
/** NOTE: Presumably LibGDX will implement this functionality in time, making
* this snippet obsolete. */
public class HideCursor implements ApplicationListener {
 
public static void setHWCursorVisible(boolean visible) throws LWJGLException {
if (Gdx.app.getType() != ApplicationType.Desktop && Gdx.app instanceof LwjglApplication)
return;
if (emptyCursor == null) {
if (Mouse.isCreated()) {
int min = org.lwjgl.input.Cursor.getMinCursorSize();
IntBuffer tmp = BufferUtils.createIntBuffer(min * min);
emptyCursor = new org.lwjgl.input.Cursor(min, min, min / 2, min / 2, 1, tmp, null);
} else {
throw new LWJGLException(
"Could not create empty cursor before Mouse object is created");
}
}
if (Mouse.isInsideWindow())
Mouse.setNativeCursor(visible ? null : emptyCursor);
}
 
public static void main(String[] args) {
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
cfg.title = "atmos";
cfg.useGL20 = true;
cfg.width = 480;
cfg.height = 320;
LwjglApplication app = new LwjglApplication(new HideCursor(), cfg);
 
}
 
SpriteBatch batch;
OrthographicCamera camera;
static org.lwjgl.input.Cursor emptyCursor;
boolean hwVisible = false;
Texture cursor;
int xHotspot, yHotspot;
 
@Override
public void create() {
batch = new SpriteBatch();
camera = new OrthographicCamera();
 
cursor = new Texture(Gdx.files.internal("data/cursor.png"));
xHotspot = 0;
yHotspot = cursor.getHeight(); // lower left origin!
 
// press SPACE to toggle software cursor
Gdx.input.setInputProcessor(new InputAdapter() {
public boolean keyDown(int key) {
if (key == Keys.SPACE) {
hwVisible = !hwVisible;
return true;
}
return false;
}
});
}
 
@Override
public void resize(int width, int height) {
camera.setToOrtho(false, width, height);
batch.setProjectionMatrix(camera.combined);
}
 
@Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
Gdx.gl.glClearColor(0.5f, 0.5f, 0.5f, 1f);
 
try {
setHWCursorVisible(hwVisible);
} catch (LWJGLException e) {
throw new GdxRuntimeException(e);
}
 
batch.begin();
 
// ... draw sprites ...
 
// draw SW cursor
if (!hwVisible) {
int x = Gdx.input.getX();
int y = Gdx.input.getY();
batch.draw(cursor, x - xHotspot, Gdx.graphics.getHeight() - y - 1 - yHotspot);
}
batch.end();
}
 
@Override
public void pause() {
 
}
 
@Override
public void resume() {
 
}
 
@Override
public void dispose() {
cursor.dispose();
batch.dispose();
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.