Add ResourceLoader for efficient Pixmap management and enhance CursorService functionality
This commit is contained in:
@@ -1,22 +1,37 @@
|
||||
package be.seeseemelk.diceos.system;
|
||||
|
||||
import be.seeseemelk.diceos.system.utils.Utils;
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.graphics.Cursor;
|
||||
import com.badlogic.gdx.graphics.Pixmap;
|
||||
import io.avaje.inject.Component;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class CursorService {
|
||||
private final ResourceLoader resourceLoader;
|
||||
private final Map<Key, Cursor> cursors = new HashMap<>();
|
||||
private int cursorScale = 1;
|
||||
private Cursor currentCursor;
|
||||
|
||||
private record Key(
|
||||
int scale
|
||||
) {}
|
||||
|
||||
void setScale(int scale) {
|
||||
if (!Utils.isPowerOfTwo(scale)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (cursorScale != scale) {
|
||||
cursorScale = scale;
|
||||
if (currentCursor != null) {
|
||||
currentCursor.dispose();
|
||||
currentCursor = null;
|
||||
}
|
||||
}
|
||||
@@ -27,17 +42,22 @@ public class CursorService {
|
||||
return;
|
||||
}
|
||||
|
||||
var original = new Pixmap(Gdx.files.internal("cursor.png"));
|
||||
currentCursor = cursors.computeIfAbsent(new Key(cursorScale), this::createCursor);
|
||||
Gdx.graphics.setCursor(currentCursor);
|
||||
}
|
||||
|
||||
private Cursor createCursor(Key key) {
|
||||
var original = resourceLoader.loadPixmap("system/cursor.png");
|
||||
|
||||
var scaled = new Pixmap(original.getWidth() * cursorScale, original.getHeight() * cursorScale, original.getFormat());
|
||||
try {
|
||||
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();
|
||||
return Gdx.graphics.newCursor(scaled, 0, 0);
|
||||
} finally {
|
||||
scaled.dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package be.seeseemelk.diceos.system;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.graphics.Pixmap;
|
||||
import io.avaje.inject.Component;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Component
|
||||
public class ResourceLoader {
|
||||
private final Map<String, Pixmap> pixmaps = new HashMap<>();
|
||||
|
||||
public Pixmap loadPixmap(String path) {
|
||||
return pixmaps.computeIfAbsent(path, p -> new Pixmap(Gdx.files.internal(p)));
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,15 @@
|
||||
package be.seeseemelk.diceos.system.utils;
|
||||
|
||||
import com.badlogic.gdx.utils.Disposable;
|
||||
import lombok.experimental.UtilityClass;
|
||||
|
||||
@UtilityClass
|
||||
public class Utils {
|
||||
public static <T extends Disposable> Disposer<T> disposing(T disposable) {
|
||||
return new Disposer<>(disposable);
|
||||
}
|
||||
|
||||
public static boolean isPowerOfTwo(int value) {
|
||||
return (value & (value - 1)) == 0 && value != 0;
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 303 B After Width: | Height: | Size: 305 B |
|
Before Width: | Height: | Size: 234 B After Width: | Height: | Size: 234 B |
Reference in New Issue
Block a user