Add CursorService for custom cursor handling and refactor DiceOS
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package be.seeseemelk.diceos;
|
package be.seeseemelk.diceos;
|
||||||
|
|
||||||
|
import be.seeseemelk.diceos.system.DiceOS;
|
||||||
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
|
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
|
||||||
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
|
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
|
||||||
import com.badlogic.gdx.graphics.glutils.HdpiMode;
|
import com.badlogic.gdx.graphics.glutils.HdpiMode;
|
||||||
|
|||||||
43
src/main/java/be/seeseemelk/diceos/system/CursorService.java
Normal file
43
src/main/java/be/seeseemelk/diceos/system/CursorService.java
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package be.seeseemelk.diceos.system;
|
||||||
|
|
||||||
|
import com.badlogic.gdx.Gdx;
|
||||||
|
import com.badlogic.gdx.graphics.Cursor;
|
||||||
|
import com.badlogic.gdx.graphics.Pixmap;
|
||||||
|
import io.avaje.inject.Component;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class CursorService {
|
||||||
|
private int cursorScale = 1;
|
||||||
|
private Cursor currentCursor;
|
||||||
|
|
||||||
|
void setScale(int scale) {
|
||||||
|
if (cursorScale != scale) {
|
||||||
|
cursorScale = scale;
|
||||||
|
if (currentCursor != null) {
|
||||||
|
currentCursor.dispose();
|
||||||
|
currentCursor = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void updateCursor() {
|
||||||
|
if (currentCursor != null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var original = new Pixmap(Gdx.files.internal("cursor.png"));
|
||||||
|
|
||||||
|
var scaled = new Pixmap(original.getWidth() * cursorScale, original.getHeight() * cursorScale, original.getFormat());
|
||||||
|
scaled.setFilter(Pixmap.Filter.NearestNeighbour);
|
||||||
|
scaled.drawPixmap(original, 0, 0, original.getWidth(), original.getHeight(),
|
||||||
|
0, 0, scaled.getWidth(), scaled.getHeight());
|
||||||
|
|
||||||
|
currentCursor = Gdx.graphics.newCursor(scaled, 0, 0);
|
||||||
|
Gdx.graphics.setCursor(currentCursor);
|
||||||
|
|
||||||
|
original.dispose();
|
||||||
|
scaled.dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,11 +1,7 @@
|
|||||||
package be.seeseemelk.diceos;
|
package be.seeseemelk.diceos.system;
|
||||||
|
|
||||||
import be.seeseemelk.diceos.system.DisplayService;
|
|
||||||
import be.seeseemelk.diceos.system.OnStartup;
|
|
||||||
import com.badlogic.gdx.ApplicationAdapter;
|
import com.badlogic.gdx.ApplicationAdapter;
|
||||||
import com.badlogic.gdx.Gdx;
|
|
||||||
import com.badlogic.gdx.graphics.Color;
|
import com.badlogic.gdx.graphics.Color;
|
||||||
import com.badlogic.gdx.graphics.Pixmap;
|
|
||||||
import com.badlogic.gdx.graphics.Texture;
|
import com.badlogic.gdx.graphics.Texture;
|
||||||
import com.badlogic.gdx.graphics.g2d.NinePatch;
|
import com.badlogic.gdx.graphics.g2d.NinePatch;
|
||||||
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
import com.badlogic.gdx.graphics.g2d.TextureRegion;
|
||||||
@@ -23,6 +19,7 @@ import java.util.List;
|
|||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class DiceOS extends ApplicationAdapter {
|
public class DiceOS extends ApplicationAdapter {
|
||||||
private final DisplayService display;
|
private final DisplayService display;
|
||||||
|
private final CursorService cursorService;
|
||||||
private final List<OnStartup> startupTasks;
|
private final List<OnStartup> startupTasks;
|
||||||
private Texture background;
|
private Texture background;
|
||||||
private Texture clouds;
|
private Texture clouds;
|
||||||
@@ -41,7 +38,6 @@ public class DiceOS extends ApplicationAdapter {
|
|||||||
clouds = new Texture("src/main/resources/clouds.png");
|
clouds = new Texture("src/main/resources/clouds.png");
|
||||||
border = new Texture("src/main/resources/border.png");
|
border = new Texture("src/main/resources/border.png");
|
||||||
loadMenubar();
|
loadMenubar();
|
||||||
loadCursor();
|
|
||||||
|
|
||||||
screenViewport = new ScreenViewport();
|
screenViewport = new ScreenViewport();
|
||||||
vmViewport = new ScreenViewport();
|
vmViewport = new ScreenViewport();
|
||||||
@@ -60,19 +56,6 @@ public class DiceOS extends ApplicationAdapter {
|
|||||||
menubar = new NinePatch(texture, 9, 9, 15, 1);
|
menubar = new NinePatch(texture, 9, 9, 15, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadCursor() {
|
|
||||||
var pixmap = new Pixmap(Gdx.files.internal("cursor.png"));
|
|
||||||
|
|
||||||
var scaled = new Pixmap(pixmap.getWidth() * scaling, pixmap.getHeight() * scaling, pixmap.getFormat());
|
|
||||||
scaled.setFilter(Pixmap.Filter.NearestNeighbour);
|
|
||||||
scaled.drawPixmap(pixmap, 0, 0, pixmap.getWidth(), pixmap.getHeight(),
|
|
||||||
0, 0, scaled.getWidth(), scaled.getHeight());
|
|
||||||
|
|
||||||
var cursor = Gdx.graphics.newCursor(scaled, 0, 0);
|
|
||||||
Gdx.graphics.setCursor(cursor);
|
|
||||||
pixmap.dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void resize(int width, int height) {
|
public void resize(int width, int height) {
|
||||||
screenViewport.update(width, height, true);
|
screenViewport.update(width, height, true);
|
||||||
@@ -82,11 +65,14 @@ public class DiceOS extends ApplicationAdapter {
|
|||||||
scaling = Math.min(width / displayWidth, height / displayHeight);
|
scaling = Math.min(width / displayWidth, height / displayHeight);
|
||||||
offsetX = (width - (displayWidth * scaling)) / 2;
|
offsetX = (width - (displayWidth * scaling)) / 2;
|
||||||
offsetY = (height - (displayHeight * scaling)) / 2;
|
offsetY = (height - (displayHeight * scaling)) / 2;
|
||||||
|
|
||||||
|
cursorService.setScale(scaling);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void render() {
|
public void render() {
|
||||||
loadCursor(); // Dispose the old cursor first
|
cursorService.updateCursor();
|
||||||
|
|
||||||
ScreenUtils.clear(Color.BLACK);
|
ScreenUtils.clear(Color.BLACK);
|
||||||
vmViewport.apply();
|
vmViewport.apply();
|
||||||
display.getScreenBuffer().begin();
|
display.getScreenBuffer().begin();
|
||||||
@@ -18,7 +18,6 @@ public class DisplayService implements OnStartup {
|
|||||||
private FrameBuffer screenBuffer;
|
private FrameBuffer screenBuffer;
|
||||||
@Getter
|
@Getter
|
||||||
private SpriteBatch batch;
|
private SpriteBatch batch;
|
||||||
// private final GraphicsFactory graphicsFactory;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStartup() {
|
public void onStartup() {
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package be.seeseemelk.diceos.system.utils;
|
||||||
|
|
||||||
|
import com.badlogic.gdx.utils.Disposable;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
|
import java.io.Closeable;
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class Disposer<T extends Disposable> implements Closeable {
|
||||||
|
private final T disposable;
|
||||||
|
|
||||||
|
public T get() {
|
||||||
|
return disposable;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
disposable.dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package be.seeseemelk.diceos.system.utils;
|
||||||
|
|
||||||
|
import com.badlogic.gdx.utils.Disposable;
|
||||||
|
|
||||||
|
public class Utils {
|
||||||
|
public static <T extends Disposable> Disposer<T> disposing(T disposable) {
|
||||||
|
return new Disposer<>(disposable);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user