Skip to content

Commit ce47e33

Browse files
committed
add functional interfaces
1 parent 9b51e6b commit ce47e33

File tree

7 files changed

+717
-183
lines changed

7 files changed

+717
-183
lines changed

CHANGELOG.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,38 @@ index: 4
44
lang: en
55
---
66

7+
## 4.22.0
8+
9+
- 🚀 Feat: Add functional interfaces to the Sprite and Stage class. This allows you to use lambda expressions for when methods and the run method. This is especially useful if you do not want to use inheritance. For example, you can now write:
10+
```java
11+
Sprite sprite = new Sprite();
12+
sprite.setWhenKeyPressed((s, keyCode) -> {
13+
if (keyCode == KeyEvent.VK_SPACE) {
14+
s.move(10);
15+
}
16+
});
17+
```
18+
19+
All methods receive the sprite or stage as the first parameter, so you can access the sprite or stage directly in the lambda expression. If you use inheritance you are better off overwriting when and run methods, like before:
20+
21+
```java
22+
class MySprite extends Sprite {
23+
@Override
24+
public void whenKeyPressed(int keyCode) {
25+
if (keyCode == KeyEvent.VK_SPACE) {
26+
this.move(10);
27+
}
28+
}
29+
}
30+
31+
```
32+
33+
- 🚀 Feat: You can now check the library version with `getLibraryVersion()` on the window class. This is useful if you want to check the version of the library at runtime.
34+
35+
```java
36+
window.getLibraryVersion(); // returns the version as a string
37+
```
38+
739
## 4.21.0
840

941
- 🚀 Feat: Add `addAnimation` with a builder function to the AnimatedSprite

resources/build.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ author.url=https://openpatch.org
4040
# This is NOT a direct link to where to download it.
4141

4242
library.url=https://github.com/openpatch/scratch-for-java
43-
library.version=4.21.0
43+
library.version=4.22.0
4444

4545

4646
# Set the category (or categories) of your Library from the following list:

src/org/openpatch/scratch/Sprite.java

Lines changed: 226 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,49 @@
6868
* @see Text
6969
* @see Timer
7070
* @see Hitbox
71-
* @see RotationStyle
7271
*/
7372
public class Sprite {
7473

74+
public interface WhenIReceiveHandler {
75+
void handle(Sprite s, Object msg);
76+
}
77+
78+
public interface WhenAddedToStageHandler {
79+
void handle(Sprite s, Stage stage);
80+
}
81+
82+
public interface WhenRemovedFromStageHandler {
83+
void handle(Sprite s, Stage stage);
84+
}
85+
86+
public interface RunHandler {
87+
void handle(Sprite s);
88+
}
89+
90+
public interface WhenBackdropSwitchesHandler {
91+
void handle(Sprite s, String backdropName);
92+
}
93+
94+
public interface WhenClickedHandler {
95+
void handle(Sprite s);
96+
}
97+
98+
public interface WhenMouseClickedHandler {
99+
void handle(Sprite s, MouseCode mouseCode);
100+
}
101+
102+
public interface WhenMouseMovedHandler {
103+
void handle(Sprite s, double mouseX, double mouseY);
104+
}
105+
106+
public interface WhenKeyReleasedHandler {
107+
void handle(Sprite s, Integer keyCode);
108+
}
109+
110+
public interface WhenKeyPressedHandler {
111+
void handle(Sprite s, Integer keyCode);
112+
}
113+
75114
private List<Image> costumes = new CopyOnWriteArrayList<>();
76115
private int currentCostume = 0;
77116
private List<Sound> sounds = new CopyOnWriteArrayList<>();
@@ -92,6 +131,27 @@ public class Sprite {
92131
private final Text text;
93132
private boolean isUI;
94133

134+
private WhenIReceiveHandler whenIReceiveHandler = (sprite, msg) -> {
135+
};
136+
private WhenAddedToStageHandler whenAddedToStageHandler = (sprite, stage) -> {
137+
};
138+
private WhenRemovedFromStageHandler whenRemovedFromStageHandler = (sprite, stage) -> {
139+
};
140+
private RunHandler runHandler = s -> {
141+
};
142+
private WhenBackdropSwitchesHandler whenBackdropSwitchesHandler = (sprite, backdropName) -> {
143+
};
144+
private WhenClickedHandler whenClickedHandler = (sprite) -> {
145+
};
146+
private WhenMouseClickedHandler whenMouseClickedHandler = (sprite, mouseCode) -> {
147+
};
148+
private WhenMouseMovedHandler whenMouseMovedHandler = (sprite, mouseX, mouseY) -> {
149+
};
150+
private WhenKeyReleasedHandler whenKeyReleasedHandler = (sprite, keyCode) -> {
151+
};
152+
private WhenKeyPressedHandler whenKeyPressedHandler = (sprite, keyCode) -> {
153+
};
154+
95155
public Sprite() {
96156
this.pen = new Pen(this);
97157
this.timer = new ConcurrentHashMap<>();
@@ -140,6 +200,17 @@ public Sprite(Sprite s) {
140200
this.hitbox = s.hitbox;
141201
this.hitboxDisabled = s.hitboxDisabled;
142202
this.text = new Text(s.text);
203+
this.whenIReceiveHandler = s.whenIReceiveHandler;
204+
this.whenAddedToStageHandler = s.whenAddedToStageHandler;
205+
this.whenRemovedFromStageHandler = s.whenRemovedFromStageHandler;
206+
this.runHandler = s.runHandler;
207+
this.whenBackdropSwitchesHandler = s.whenBackdropSwitchesHandler;
208+
this.whenClickedHandler = s.whenClickedHandler;
209+
this.whenMouseClickedHandler = s.whenMouseClickedHandler;
210+
this.whenMouseMovedHandler = s.whenMouseMovedHandler;
211+
this.whenKeyReleasedHandler = s.whenKeyReleasedHandler;
212+
this.whenKeyPressedHandler = s.whenKeyPressedHandler;
213+
this.isUI = s.isUI;
143214
}
144215

145216
/**
@@ -158,6 +229,18 @@ public void whenAddedToStage() {
158229
* @param stage The stage to which the sprite is added.
159230
*/
160231
public void whenAddedToStage(Stage stage) {
232+
whenAddedToStageHandler.handle(this, stage);
233+
}
234+
235+
/**
236+
* Sets the handler for the whenAddedToStage event. This handler will be
237+
* called when the sprite
238+
* is added to a stage.
239+
*
240+
* @param handler an AddedToStageHandler that takes a Stage as an argument.
241+
*/
242+
public void setWhenAddedToStageHandler(WhenAddedToStageHandler handler) {
243+
this.whenAddedToStageHandler = handler;
161244
}
162245

163246
/**
@@ -176,6 +259,19 @@ public void whenRemovedFromStage() {
176259
* @param stage The stage from which the sprite is removed from.
177260
*/
178261
public void whenRemovedFromStage(Stage stage) {
262+
this.whenRemovedFromStageHandler.handle(this, stage);
263+
}
264+
265+
/**
266+
* Sets the handler for the whenRemovedFromStage event. This handler
267+
* will be called when the sprite
268+
* is removed from a stage.
269+
*
270+
* @param handler an RemovedFromStageHandler that takes a Stage as an
271+
*
272+
*/
273+
public void setWhenRemovedFromStageHandler(WhenRemovedFromStageHandler handler) {
274+
this.whenRemovedFromStageHandler = handler;
179275
}
180276

181277
/** Removes this sprite from its current stage. */
@@ -241,6 +337,11 @@ public void switchShader(double index) {
241337
this.currentShader = (int) index % this.shaders.size();
242338
}
243339

340+
/**
341+
* Resets the current shader to -1, which means no shader is currently active.
342+
* This method can be used
343+
* to disable the shader effect on the sprite.
344+
*/
244345
public void resetShader() {
245346
this.currentShader = -1;
246347
}
@@ -641,6 +742,12 @@ public void changeTransparency(double step) {
641742
}
642743
}
643744

745+
/**
746+
* Gets the transparency of the current costume.
747+
*
748+
* @see Image#getTransparency()
749+
* @return the transparency of the current costume, or 0 if there are no
750+
*/
644751
public double getTransparency() {
645752
if (this.costumes.size() == 0)
646753
return 0;
@@ -1557,6 +1664,18 @@ public void keyEvent(KeyEvent e) {
15571664
* @param keyCode the code of the key that was pressed
15581665
*/
15591666
public void whenKeyPressed(int keyCode) {
1667+
this.whenKeyPressedHandler.handle(this, keyCode);
1668+
}
1669+
1670+
/**
1671+
* Sets the handler for when a key is pressed. This allows you to define custom
1672+
* behavior when
1673+
* a key is pressed.
1674+
*
1675+
* @param whenKeyPressed A KeyPressedHandler that takes a Sprite and an
1676+
*/
1677+
public void setWhenKeyPressed(WhenKeyPressedHandler whenKeyPressed) {
1678+
this.whenKeyPressedHandler = whenKeyPressed;
15601679
}
15611680

15621681
/**
@@ -1567,6 +1686,19 @@ public void whenKeyPressed(int keyCode) {
15671686
* @param keyCode the code of the key that was released
15681687
*/
15691688
public void whenKeyReleased(int keyCode) {
1689+
this.whenKeyReleasedHandler.handle(this, keyCode);
1690+
}
1691+
1692+
/**
1693+
* Sets the handle for when a key is released. This allows you to define
1694+
* custom behavior when
1695+
* a key is released.
1696+
*
1697+
* @param whenKeyReleased A KeyReleasedHandler that takes a Sprite and an
1698+
*
1699+
*/
1700+
public void setWhenKeyReleased(WhenKeyReleasedHandler whenKeyReleased) {
1701+
this.whenKeyReleasedHandler = whenKeyReleased;
15701702
}
15711703

15721704
/**
@@ -1585,6 +1717,19 @@ public void mouseEvent(MouseEvent e) {
15851717
* @param y The y-coordinate of the mouse pointer.
15861718
*/
15871719
public void whenMouseMoved(double x, double y) {
1720+
this.whenMouseMovedHandler.handle(this, x, y);
1721+
}
1722+
1723+
/**
1724+
* Sets the handler for when the mouse is moved. This allows you to define
1725+
* custom behavior when
1726+
* the mouse is moved.
1727+
*
1728+
* @param whenMouseMoved A MouseMovedHandler that takes a Sprite, x, and y
1729+
*
1730+
*/
1731+
public void setWhenMouseMoved(WhenMouseMovedHandler whenMouseMoved) {
1732+
this.whenMouseMovedHandler = whenMouseMoved;
15881733
}
15891734

15901735
/**
@@ -1595,6 +1740,18 @@ public void whenMouseMoved(double x, double y) {
15951740
* @param mouseCode The code representing the mouse button that was clicked.
15961741
*/
15971742
public void whenMouseClicked(MouseCode mouseCode) {
1743+
this.whenMouseClickedHandler.handle(this, mouseCode);
1744+
}
1745+
1746+
/**
1747+
* Sets the handler for when the mouse is clicked. This allows you to define
1748+
* custom behavior
1749+
* when the mouse is clicked.
1750+
*
1751+
* @param whenMouseClicked A MouseClickedHandler that takes a Sprite and a
1752+
*/
1753+
public void setWhenMouseClicked(WhenMouseClickedHandler whenMouseClicked) {
1754+
this.whenMouseClickedHandler = whenMouseClicked;
15981755
}
15991756

16001757
/**
@@ -1603,6 +1760,18 @@ public void whenMouseClicked(MouseCode mouseCode) {
16031760
* behavior for the sprite when it is clicked.
16041761
*/
16051762
public void whenClicked() {
1763+
this.whenClickedHandler.handle(this);
1764+
}
1765+
1766+
/**
1767+
* Sets the handler for when the sprite is clicked. This allows you to define
1768+
* custom behavior
1769+
* when the sprite is clicked.
1770+
*
1771+
* @param whenClicked A ClickedHandler that takes a Sprite as input and
1772+
*/
1773+
public void setWhenClicked(WhenClickedHandler whenClicked) {
1774+
this.whenClickedHandler = whenClicked;
16061775
}
16071776

16081777
/**
@@ -1689,6 +1858,16 @@ public void goLayersBackwards(int number) {
16891858
* @param name the name of the backdrop to switch to
16901859
*/
16911860
public void whenBackdropSwitches(String name) {
1861+
this.whenBackdropSwitchesHandler.handle(this, name);
1862+
}
1863+
1864+
/**
1865+
* Sets the handler for when the backdrop switches to a specified name.
1866+
*
1867+
* @param whenBackdropSwitches A BackdropSwitchHandler that takes a Sprite and
1868+
*/
1869+
public void setWhenBackdropSwitches(WhenBackdropSwitchesHandler whenBackdropSwitches) {
1870+
this.whenBackdropSwitchesHandler = whenBackdropSwitches;
16921871
}
16931872

16941873
/**
@@ -1798,6 +1977,7 @@ public void broadcast(Object message) {
17981977
* @param message The message that is received.
17991978
*/
18001979
public void whenIReceive(String message) {
1980+
this.whenIReceiveHandler.handle(this, message);
18011981
}
18021982

18031983
/**
@@ -1809,6 +1989,28 @@ public void whenIReceive(String message) {
18091989
* @param message The message that is received.
18101990
*/
18111991
public void whenIReceive(Object message) {
1992+
this.whenIReceiveHandler.handle(this, message);
1993+
}
1994+
1995+
/**
1996+
* Sets the handler for when a message is received. This allows you to
1997+
* define custom behavior. This is useful when you do not want to extend the
1998+
* sprite class and want to use the sprite class directly.
1999+
*
2000+
* For example:
2001+
*
2002+
* <pre>
2003+
* Sprite sprite = new Sprite();
2004+
* sprite.setWhenIReceive((message) -> {
2005+
* System.out.println("Received message: " + message);
2006+
* });
2007+
*
2008+
* </pre>
2009+
*
2010+
* @param whenIReceive An IReceiveHandler that takes a Sprite and a message
2011+
*/
2012+
public void setWhenIReceive(WhenIReceiveHandler whenIReceive) {
2013+
this.whenIReceiveHandler = whenIReceive;
18122014
}
18132015

18142016
/**
@@ -1886,6 +2088,18 @@ public Window getWindow() {
18862088
* It is called every frame.
18872089
*/
18882090
public void run() {
2091+
this.runHandler.handle(this);
2092+
}
2093+
2094+
/**
2095+
* Sets the run handler for the sprite. This allows you to define custom
2096+
* behavior when the sprite
2097+
* is run.
2098+
*
2099+
* @param run A RunHandler that takes a Sprite as input and defines the
2100+
*/
2101+
public void setRun(RunHandler run) {
2102+
this.runHandler = run;
18892103
}
18902104

18912105
protected void addedToStage(Stage stage) {
@@ -1944,4 +2158,15 @@ private Stamp getStamp() {
19442158

19452159
return stamp;
19462160
}
2161+
2162+
/**
2163+
* Creates a clone of the current sprite. The cloned sprite will have the same
2164+
* properties as the
2165+
* original sprite, including its costumes, position, direction, and pen.
2166+
*
2167+
* @return a new Sprite object that is a clone of the current sprite
2168+
*/
2169+
public Sprite clone() {
2170+
return new Sprite(this);
2171+
}
19472172
}

0 commit comments

Comments
 (0)