Add ResourceLoader for efficient Pixmap management and enhance CursorService functionality
This commit is contained in:
@@ -1,22 +1,37 @@
|
|||||||
package be.seeseemelk.diceos.system;
|
package be.seeseemelk.diceos.system;
|
||||||
|
|
||||||
|
import be.seeseemelk.diceos.system.utils.Utils;
|
||||||
import com.badlogic.gdx.Gdx;
|
import com.badlogic.gdx.Gdx;
|
||||||
import com.badlogic.gdx.graphics.Cursor;
|
import com.badlogic.gdx.graphics.Cursor;
|
||||||
import com.badlogic.gdx.graphics.Pixmap;
|
import com.badlogic.gdx.graphics.Pixmap;
|
||||||
import io.avaje.inject.Component;
|
import io.avaje.inject.Component;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class CursorService {
|
public class CursorService {
|
||||||
|
private final ResourceLoader resourceLoader;
|
||||||
|
private final Map<Key, Cursor> cursors = new HashMap<>();
|
||||||
private int cursorScale = 1;
|
private int cursorScale = 1;
|
||||||
private Cursor currentCursor;
|
private Cursor currentCursor;
|
||||||
|
|
||||||
|
private record Key(
|
||||||
|
int scale
|
||||||
|
) {}
|
||||||
|
|
||||||
void setScale(int scale) {
|
void setScale(int scale) {
|
||||||
|
if (!Utils.isPowerOfTwo(scale)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (cursorScale != scale) {
|
if (cursorScale != scale) {
|
||||||
cursorScale = scale;
|
cursorScale = scale;
|
||||||
if (currentCursor != null) {
|
if (currentCursor != null) {
|
||||||
currentCursor.dispose();
|
|
||||||
currentCursor = null;
|
currentCursor = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -27,17 +42,22 @@ public class CursorService {
|
|||||||
return;
|
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());
|
var scaled = new Pixmap(original.getWidth() * cursorScale, original.getHeight() * cursorScale, original.getFormat());
|
||||||
scaled.setFilter(Pixmap.Filter.NearestNeighbour);
|
try {
|
||||||
scaled.drawPixmap(original, 0, 0, original.getWidth(), original.getHeight(),
|
scaled.setFilter(Pixmap.Filter.NearestNeighbour);
|
||||||
0, 0, scaled.getWidth(), scaled.getHeight());
|
scaled.drawPixmap(original, 0, 0, original.getWidth(), original.getHeight(),
|
||||||
|
0, 0, scaled.getWidth(), scaled.getHeight());
|
||||||
|
|
||||||
currentCursor = Gdx.graphics.newCursor(scaled, 0, 0);
|
return Gdx.graphics.newCursor(scaled, 0, 0);
|
||||||
Gdx.graphics.setCursor(currentCursor);
|
} finally {
|
||||||
|
scaled.dispose();
|
||||||
original.dispose();
|
}
|
||||||
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;
|
package be.seeseemelk.diceos.system.utils;
|
||||||
|
|
||||||
import com.badlogic.gdx.utils.Disposable;
|
import com.badlogic.gdx.utils.Disposable;
|
||||||
|
import lombok.experimental.UtilityClass;
|
||||||
|
|
||||||
|
@UtilityClass
|
||||||
public class Utils {
|
public class Utils {
|
||||||
public static <T extends Disposable> Disposer<T> disposing(T disposable) {
|
public static <T extends Disposable> Disposer<T> disposing(T disposable) {
|
||||||
return new Disposer<>(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