Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
AlmasB committed Mar 24, 2024
1 parent f89f28d commit ef85470
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 40 deletions.
38 changes: 16 additions & 22 deletions fxgl-samples/src/main/java/sandbox/SoftwareCursorSample.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.almasb.fxgl.app.GameApplication;
import com.almasb.fxgl.app.GameSettings;
import com.almasb.fxgl.ui.SoftwareCursor;
import javafx.scene.Cursor;
import javafx.scene.input.KeyCode;
import javafx.scene.paint.Color;

import static com.almasb.fxgl.dsl.FXGL.*;
Expand All @@ -19,41 +21,33 @@
* @author Poppy Eyres (eyres.poppy@gmail.com)
*/
public class SoftwareCursorSample extends GameApplication {
SoftwareCursor Cursor;
private SoftwareCursor softwareCursor;

@Override
protected void initSettings(GameSettings settings) {
// settings.addEngineService(QuestService.class);
}

protected void initSettings(GameSettings settings) { }

@Override
protected void initInput() {
//hide default cursor
getGameScene().getRoot().setCursor(javafx.scene.Cursor.NONE);
onKey(KeyCode.UP, () -> softwareCursor.translatePositionY(-5));
onKey(KeyCode.DOWN, () -> softwareCursor.translatePositionY(5));
onKey(KeyCode.LEFT, () -> softwareCursor.translatePositionX(-5));
onKey(KeyCode.RIGHT, () -> softwareCursor.translatePositionX(5));
}

@Override
protected void initGame() {
//hide default cursor
getGameScene().setCursor(Cursor.NONE);

//create software cursor
Cursor = new SoftwareCursor(Color.PINK);
getGameScene().getRoot().getChildren().add(Cursor.getCursorNode());
softwareCursor = new SoftwareCursor(Color.PINK);
getGameScene().addUINode(softwareCursor.getCursorNode());

//initialize cursor position
Cursor.setPositionX((double) getGameScene().getAppWidth() / 2);
Cursor.setPositionY((double) getGameScene().getAppHeight() / 2);
softwareCursor.setPositionX(getGameScene().getAppWidth() / 2.0);
softwareCursor.setPositionY(getGameScene().getAppHeight() / 2.0);
}



@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);
}
Expand Down
44 changes: 26 additions & 18 deletions fxgl-scene/src/main/java/com/almasb/fxgl/ui/SoftwareCursor.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,49 +15,57 @@
* @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};
public final class SoftwareCursor {

private static final Double[] DEFAULT_POINTS = { 0.0, 0.0, 0.0, 20.0, 20.0, 5.0 };
private Polygon cursor;

// creates software cursor with colour
public SoftwareCursor(Color color){
this(defaultPoints, color);
this(DEFAULT_POINTS, 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);

// 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;
return cursor;
}

//sets X position
public void setPositionX(double x){
Cursor.setTranslateX(x);
cursor.setTranslateX(x);
}

//sets Y position
public void setPositionY(double y){
Cursor.setTranslateY(y);
cursor.setTranslateY(y);
}

//sets X and Y position
public void setPosition(double x, double y){
Cursor.setTranslateX(x);
Cursor.setTranslateY(y);
cursor.setTranslateX(x);
cursor.setTranslateY(y);
}

//moves X position by x
public void translatePositionX(double x){
Cursor.setTranslateX(Cursor.translateXProperty().doubleValue()+x);
cursor.setTranslateX(cursor.translateXProperty().doubleValue()+x);
}

//moves Y position by y
public void translatePositionY(double y){
Cursor.setTranslateY(Cursor.translateYProperty().doubleValue()+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);
cursor.setTranslateX(cursor.translateXProperty().doubleValue()+x);
cursor.setTranslateY(cursor.translateYProperty().doubleValue()+y);
}
}

0 comments on commit ef85470

Please sign in to comment.