Skip to content

Commit 728d582

Browse files
alexmoore04popsx0xli0nleo
authored
feat: added software cursor, closes #1090
Co-authored-by: popsx0x <[email protected]> Co-authored-by: li0nleo <[email protected]>
1 parent 7e4b1de commit 728d582

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* FXGL - JavaFX Game Library. The MIT License (MIT).
3+
* Copyright (c) AlmasB ([email protected]).
4+
* See LICENSE for details.
5+
*/
6+
7+
package sandbox;
8+
9+
import com.almasb.fxgl.app.GameApplication;
10+
import com.almasb.fxgl.app.GameSettings;
11+
import com.almasb.fxgl.ui.SoftwareCursor;
12+
import javafx.scene.paint.Color;
13+
14+
import static com.almasb.fxgl.dsl.FXGL.*;
15+
16+
/**
17+
* @author Alex Moore ([email protected])
18+
* @author Leo Waters ([email protected])
19+
* @author Poppy Eyres ([email protected])
20+
*/
21+
public class SoftwareCursorSample extends GameApplication {
22+
SoftwareCursor Cursor;
23+
24+
@Override
25+
protected void initSettings(GameSettings settings) {
26+
// settings.addEngineService(QuestService.class);
27+
}
28+
29+
30+
@Override
31+
protected void initInput() {
32+
//hide default cursor
33+
getGameScene().getRoot().setCursor(javafx.scene.Cursor.NONE);
34+
}
35+
@Override
36+
protected void initGame() {
37+
//create software cursor
38+
Cursor = new SoftwareCursor(Color.PINK);
39+
getGameScene().getRoot().getChildren().add(Cursor.getCursorNode());
40+
41+
//initialize cursor position
42+
Cursor.setPositionX((double) getGameScene().getAppWidth() / 2);
43+
Cursor.setPositionY((double) getGameScene().getAppHeight() / 2);
44+
}
45+
46+
47+
48+
@Override
49+
protected void onUpdate(double tpf) {
50+
//Cursor.setPosition(getInput().getMouseXWorld(), getInput().getMouseYWorld());
51+
//Cursor.setPositionX(getInput().getMouseXWorld());
52+
//Cursor.setPositionY(getInput().getMouseYWorld());
53+
Cursor.translatePosition(tpf,tpf);
54+
}
55+
56+
57+
public static void main(String[] args) {
58+
launch(args);
59+
}
60+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* FXGL - JavaFX Game Library. The MIT License (MIT).
3+
* Copyright (c) AlmasB ([email protected]).
4+
* See LICENSE for details.
5+
*/
6+
7+
package com.almasb.fxgl.ui;
8+
9+
import javafx.scene.Node;
10+
import javafx.scene.paint.Color;
11+
import javafx.scene.shape.Polygon;
12+
13+
/**
14+
* @author Alex Moore ([email protected])
15+
* @author Leo Waters ([email protected])
16+
* @author Poppy Eyres ([email protected])
17+
*/
18+
public class SoftwareCursor {
19+
Polygon Cursor;
20+
static Double[] defaultPoints = {0.0, 0.0,0.0, 20.0, 20.0, 5.0};
21+
22+
// creates software cursor with colour
23+
public SoftwareCursor(Color color){
24+
this(defaultPoints, color);
25+
}
26+
// creates software cursor with colour and points to draw
27+
public SoftwareCursor(Double[] Points, Color color){
28+
Cursor = new Polygon();
29+
Cursor.getPoints().addAll(Points);
30+
Cursor.setFill(color);
31+
32+
}
33+
//gets the node to be added to the scene
34+
public Node getCursorNode(){
35+
return Cursor;
36+
}
37+
//sets X position
38+
public void setPositionX(double x){
39+
Cursor.setTranslateX(x);
40+
}
41+
//sets Y position
42+
public void setPositionY(double y){
43+
Cursor.setTranslateY(y);
44+
}
45+
//sets X and Y position
46+
public void setPosition(double x, double y){
47+
Cursor.setTranslateX(x);
48+
Cursor.setTranslateY(y);
49+
}
50+
//moves X position by x
51+
public void translatePositionX(double x){
52+
Cursor.setTranslateX(Cursor.translateXProperty().doubleValue()+x);
53+
}
54+
//moves Y position by y
55+
public void translatePositionY(double y){
56+
Cursor.setTranslateY(Cursor.translateYProperty().doubleValue()+y);
57+
}
58+
//moves X & Y positions by x & y
59+
public void translatePosition(double x,double y){
60+
Cursor.setTranslateX(Cursor.translateXProperty().doubleValue()+x);
61+
Cursor.setTranslateY(Cursor.translateYProperty().doubleValue()+y);
62+
}
63+
}

0 commit comments

Comments
 (0)