From 728d5825b9249b8fa6f6b3e37a2c2d254d413a91 Mon Sep 17 00:00:00 2001 From: alexmoore04 <119882949+alexmoore04@users.noreply.github.com> Date: Sun, 24 Mar 2024 08:14:40 +0000 Subject: [PATCH] feat: added software cursor, closes #1090 Co-authored-by: popsx0x <125997098+popsx0x@users.noreply.github.com> Co-authored-by: li0nleo <41073073+li0nleo@users.noreply.github.com> --- .../java/sandbox/SoftwareCursorSample.java | 60 ++++++++++++++++++ .../com/almasb/fxgl/ui/SoftwareCursor.java | 63 +++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 fxgl-samples/src/main/java/sandbox/SoftwareCursorSample.java create mode 100644 fxgl-scene/src/main/java/com/almasb/fxgl/ui/SoftwareCursor.java diff --git a/fxgl-samples/src/main/java/sandbox/SoftwareCursorSample.java b/fxgl-samples/src/main/java/sandbox/SoftwareCursorSample.java new file mode 100644 index 000000000..b41a5a73b --- /dev/null +++ b/fxgl-samples/src/main/java/sandbox/SoftwareCursorSample.java @@ -0,0 +1,60 @@ +/* + * FXGL - JavaFX Game Library. The MIT License (MIT). + * Copyright (c) AlmasB (almaslvl@gmail.com). + * See LICENSE for details. + */ + +package sandbox; + +import com.almasb.fxgl.app.GameApplication; +import com.almasb.fxgl.app.GameSettings; +import com.almasb.fxgl.ui.SoftwareCursor; +import javafx.scene.paint.Color; + +import static com.almasb.fxgl.dsl.FXGL.*; + +/** + * @author Alex Moore (alexstephenmoore@gmail.com) + * @author Leo Waters (li0nleo117@gmail.com) + * @author Poppy Eyres (eyres.poppy@gmail.com) + */ +public class SoftwareCursorSample extends GameApplication { + SoftwareCursor Cursor; + + @Override + protected void initSettings(GameSettings settings) { + // settings.addEngineService(QuestService.class); + } + + + @Override + protected void initInput() { + //hide default cursor + getGameScene().getRoot().setCursor(javafx.scene.Cursor.NONE); + } + @Override + protected void initGame() { + //create software cursor + Cursor = new SoftwareCursor(Color.PINK); + getGameScene().getRoot().getChildren().add(Cursor.getCursorNode()); + + //initialize cursor position + Cursor.setPositionX((double) getGameScene().getAppWidth() / 2); + Cursor.setPositionY((double) getGameScene().getAppHeight() / 2); + } + + + + @Override + protected void onUpdate(double tpf) { + //Cursor.setPosition(getInput().getMouseXWorld(), getInput().getMouseYWorld()); + //Cursor.setPositionX(getInput().getMouseXWorld()); + //Cursor.setPositionY(getInput().getMouseYWorld()); + Cursor.translatePosition(tpf,tpf); + } + + + public static void main(String[] args) { + launch(args); + } +} diff --git a/fxgl-scene/src/main/java/com/almasb/fxgl/ui/SoftwareCursor.java b/fxgl-scene/src/main/java/com/almasb/fxgl/ui/SoftwareCursor.java new file mode 100644 index 000000000..2c459e25f --- /dev/null +++ b/fxgl-scene/src/main/java/com/almasb/fxgl/ui/SoftwareCursor.java @@ -0,0 +1,63 @@ +/* + * FXGL - JavaFX Game Library. The MIT License (MIT). + * Copyright (c) AlmasB (almaslvl@gmail.com). + * See LICENSE for details. + */ + +package com.almasb.fxgl.ui; + +import javafx.scene.Node; +import javafx.scene.paint.Color; +import javafx.scene.shape.Polygon; + +/** + * @author Alex Moore (alexstephenmoore@gmail.com) + * @author Leo Waters (li0nleo117@gmail.com) + * @author Poppy Eyres (eyres.poppy@gmail.com) + */ +public class SoftwareCursor { + Polygon Cursor; + static Double[] defaultPoints = {0.0, 0.0,0.0, 20.0, 20.0, 5.0}; + + // creates software cursor with colour + public SoftwareCursor(Color color){ + this(defaultPoints, color); + } + // creates software cursor with colour and points to draw + public SoftwareCursor(Double[] Points, Color color){ + Cursor = new Polygon(); + Cursor.getPoints().addAll(Points); + Cursor.setFill(color); + + } + //gets the node to be added to the scene + public Node getCursorNode(){ + return Cursor; + } + //sets X position + public void setPositionX(double x){ + Cursor.setTranslateX(x); + } + //sets Y position + public void setPositionY(double y){ + Cursor.setTranslateY(y); + } + //sets X and Y position + public void setPosition(double x, double y){ + Cursor.setTranslateX(x); + Cursor.setTranslateY(y); + } + //moves X position by x + public void translatePositionX(double x){ + Cursor.setTranslateX(Cursor.translateXProperty().doubleValue()+x); + } + //moves Y position by y + public void translatePositionY(double y){ + Cursor.setTranslateY(Cursor.translateYProperty().doubleValue()+y); + } + //moves X & Y positions by x & y + public void translatePosition(double x,double y){ + Cursor.setTranslateX(Cursor.translateXProperty().doubleValue()+x); + Cursor.setTranslateY(Cursor.translateYProperty().doubleValue()+y); + } +}