From 1f50f74d739464cb3dfde95ed46c05ccfe36c20f Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Mon, 14 Feb 2022 16:50:56 -0500 Subject: [PATCH] v1.2.0: added custom cursor visuals --- .../java/com/watabou/noosa/ui/Cursor.java | 88 +++++++++++++++++++ .../scenes/PixelScene.java | 3 + 2 files changed, 91 insertions(+) create mode 100644 SPD-classes/src/main/java/com/watabou/noosa/ui/Cursor.java diff --git a/SPD-classes/src/main/java/com/watabou/noosa/ui/Cursor.java b/SPD-classes/src/main/java/com/watabou/noosa/ui/Cursor.java new file mode 100644 index 000000000..a9b2ffa53 --- /dev/null +++ b/SPD-classes/src/main/java/com/watabou/noosa/ui/Cursor.java @@ -0,0 +1,88 @@ +/* + * Pixel Dungeon + * Copyright (C) 2012-2015 Oleg Dolya + * + * Shattered Pixel Dungeon + * Copyright (C) 2014-2022 Evan Debenham + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see + */ + +package com.watabou.noosa.ui; + +import com.badlogic.gdx.Files; +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.graphics.Pixmap; +import com.watabou.utils.FileUtils; + +public class Cursor { + + private static com.badlogic.gdx.graphics.Cursor currentCursor; + + public enum Type { + + //TODO if we ever add more cursors, should cache their pixmaps rather than always remaking + DEFAULT("gdx/cursor.png"); + + private String file; + + Type(String file){ + this.file = file; + } + + } + + private static Type lastType; + private static int lastZoom; + + public static void setCustomCursor(Type type, int zoom){ + + if (currentCursor != null){ + if (lastType == type && lastZoom == zoom){ + return; + } + + currentCursor.dispose(); + currentCursor = null; + } + + Pixmap cursorImg = new Pixmap(FileUtils.getFileHandle(Files.FileType.Internal, type.file)); + + int scaledWidth = cursorImg.getWidth()*zoom; + int width2 = 2; + while (width2 < scaledWidth) { + width2 <<= 1; + } + + int scaledHeight = cursorImg.getHeight()*zoom; + int height2 = 2; + while (height2 < scaledHeight) { + height2 <<= 1; + } + + Pixmap scaledImg = new Pixmap(width2, height2, cursorImg.getFormat()); + scaledImg.setFilter(Pixmap.Filter.NearestNeighbour); + scaledImg.drawPixmap(cursorImg, 0, 0, cursorImg.getWidth(), cursorImg.getHeight(), 0, 0, scaledWidth, scaledHeight); + + currentCursor = Gdx.graphics.newCursor(scaledImg, 0, 0); + Gdx.graphics.setCursor(currentCursor); + scaledImg.dispose(); + cursorImg.dispose(); + + lastType = type; + lastZoom = zoom; + + } + +} diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/PixelScene.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/PixelScene.java index 9ade34dbb..dc7363adf 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/PixelScene.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/scenes/PixelScene.java @@ -42,6 +42,7 @@ import com.watabou.noosa.Gizmo; import com.watabou.noosa.Scene; import com.watabou.noosa.Visual; import com.watabou.noosa.ui.Component; +import com.watabou.noosa.ui.Cursor; import com.watabou.utils.GameMath; import com.watabou.utils.Reflection; @@ -151,6 +152,8 @@ public class PixelScene extends Scene { Tooltip.resetLastUsedTime(); + Cursor.setCustomCursor(Cursor.Type.DEFAULT, defaultZoom); + } //FIXME this system currently only works for a subset of windows