diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 26d33521..00000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml diff --git a/.idea/.name b/.idea/.name deleted file mode 100644 index 31575ca8..00000000 --- a/.idea/.name +++ /dev/null @@ -1 +0,0 @@ -scratch-for-java \ No newline at end of file diff --git a/.idea/ant.xml b/.idea/ant.xml deleted file mode 100644 index c80cfe41..00000000 --- a/.idea/ant.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/kotlinc.xml b/.idea/kotlinc.xml deleted file mode 100644 index 0dd4b354..00000000 --- a/.idea/kotlinc.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - \ No newline at end of file diff --git a/.idea/libraries/KotlinJavaRuntime.xml b/.idea/libraries/KotlinJavaRuntime.xml deleted file mode 100644 index d0ec564a..00000000 --- a/.idea/libraries/KotlinJavaRuntime.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/libraries/libs.xml b/.idea/libraries/libs.xml deleted file mode 100644 index 8faa3ad1..00000000 --- a/.idea/libraries/libs.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 36382c86..00000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index e50170cf..00000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml deleted file mode 100644 index 2b63946d..00000000 --- a/.idea/uiDesigner.xml +++ /dev/null @@ -1,124 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7f..00000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 2649c051..cab9e5a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ name: Changelog index: 4 --- +## 3.9.0 + +- 🚀 Feat: Add support for broadcast and when i receive blocks to the Sprite and the Stage class. + ## 3.8.0 - 🚀 Feat: Support for vertical spritesheets was added. diff --git a/docs/de/book/reference/sprite/events/broadcast.json b/docs/de/book/reference/sprite/events/broadcast.json new file mode 100644 index 00000000..7a448abe --- /dev/null +++ b/docs/de/book/reference/sprite/events/broadcast.json @@ -0,0 +1,24 @@ +{ + "template": "method", + "related": [], + "scratchblock": "broadcast [message v]", + "name": "broadcast(message)", + "class": "Sprite", + "description": "Diese Methode sendet eine Nachricht an alle Figuren und die Bühne. Die Nachricht kann mit whenIReceive verarbeitet werden.", + "syntax": ["void broadcast(message)"], + "returns": "void", + "examples": [ + { + "src": "SpriteBroadcast.java", + "preview": "SpriteBroadcast.gif", + "lines": "reg:[Rr]ecorder" + } + ], + "parameters": [ + { + "name": "message", + "description": "Die Nachricht des Broadcasts.", + "type": "String" + } + ] +} diff --git a/docs/de/book/reference/sprite/events/whenIReceive.json b/docs/de/book/reference/sprite/events/whenIReceive.json new file mode 100644 index 00000000..d9e4dc1f --- /dev/null +++ b/docs/de/book/reference/sprite/events/whenIReceive.json @@ -0,0 +1,24 @@ +{ + "template": "method", + "related": [], + "scratchblock": "when I receive [message v]", + "name": "whenIReceive(message)", + "class": "Sprite", + "description": "Diese Methode wird aufgerufen, wenn ein Broadcast empfangen wird.", + "syntax": ["void whenIReceive(message)"], + "returns": "void", + "examples": [ + { + "src": "SpriteBroadcast.java", + "preview": "SpriteBroadcast.gif", + "lines": "reg:[Rr]ecorder" + } + ], + "parameters": [ + { + "name": "message", + "description": "Die Nachricht des Broadcasts.", + "type": "String" + } + ] +} diff --git a/docs/de/book/reference/stage/events/broadcast.json b/docs/de/book/reference/stage/events/broadcast.json new file mode 100644 index 00000000..dccf9c72 --- /dev/null +++ b/docs/de/book/reference/stage/events/broadcast.json @@ -0,0 +1,24 @@ +{ + "template": "method", + "related": [], + "scratchblock": "broadcast [message v]", + "name": "broadcast(message)", + "class": "Stage", + "description": "Diese Methode sendet eine Nachricht an alle Figuren und die Bühne. Die Nachricht kann mit whenIReceive verarbeitet werden.", + "syntax": ["void broadcast(message)"], + "returns": "void", + "examples": [ + { + "src": "SpriteBroadcast.java", + "preview": "SpriteBroadcast.gif", + "lines": "reg:[Rr]ecorder" + } + ], + "parameters": [ + { + "name": "message", + "description": "Die Nachricht des Broadcasts.", + "type": "String" + } + ] +} diff --git a/docs/de/book/reference/stage/events/whenIReceive.json b/docs/de/book/reference/stage/events/whenIReceive.json new file mode 100644 index 00000000..717192ee --- /dev/null +++ b/docs/de/book/reference/stage/events/whenIReceive.json @@ -0,0 +1,24 @@ +{ + "template": "method", + "related": [], + "scratchblock": "when I receive [message v]", + "name": "whenIReceive(message)", + "class": "Stage", + "description": "Diese Methode wird aufgerufen, wenn ein Broadcast empfangen wird.", + "syntax": ["void whenIReceive(message)"], + "returns": "void", + "examples": [ + { + "src": "SpriteBroadcast.java", + "preview": "SpriteBroadcast.gif", + "lines": "reg:[Rr]ecorder" + } + ], + "parameters": [ + { + "name": "message", + "description": "Die Nachricht des Broadcasts.", + "type": "String" + } + ] +} diff --git a/docs/en/book/reference/sprite/events/broadcast.json b/docs/en/book/reference/sprite/events/broadcast.json new file mode 100644 index 00000000..719b6924 --- /dev/null +++ b/docs/en/book/reference/sprite/events/broadcast.json @@ -0,0 +1,24 @@ +{ + "template": "method", + "related": [], + "scratchblock": "broadcast [message v]", + "name": "broadcast(message)", + "class": "Sprite", + "description": "This method sends a broadcast to all sprites and the stage. The broadcast can be received by using the whenIReceived method.", + "syntax": ["void broadcast(message)"], + "returns": "void", + "examples": [ + { + "src": "SpriteBroadcast.java", + "preview": "SpriteBroadcast.gif", + "lines": "reg:[Rr]ecorder" + } + ], + "parameters": [ + { + "name": "message", + "description": "The message to broadcast.", + "type": "String" + } + ] +} diff --git a/docs/en/book/reference/sprite/events/whenIReceive.json b/docs/en/book/reference/sprite/events/whenIReceive.json new file mode 100644 index 00000000..adc450b7 --- /dev/null +++ b/docs/en/book/reference/sprite/events/whenIReceive.json @@ -0,0 +1,24 @@ +{ + "template": "method", + "related": [], + "scratchblock": "when I receive [message v]", + "name": "whenIReceive(message)", + "class": "Sprite", + "description": "This method is called when a broadcast is received.", + "syntax": ["void whenIReceive(message)"], + "returns": "void", + "examples": [ + { + "src": "SpriteBroadcast.java", + "preview": "SpriteBroadcast.gif", + "lines": "reg:[Rr]ecorder" + } + ], + "parameters": [ + { + "name": "message", + "description": "The message of the broadcast.", + "type": "String" + } + ] +} diff --git a/docs/en/book/reference/stage/events/broadcast.json b/docs/en/book/reference/stage/events/broadcast.json new file mode 100644 index 00000000..ad9c83dd --- /dev/null +++ b/docs/en/book/reference/stage/events/broadcast.json @@ -0,0 +1,24 @@ +{ + "template": "method", + "related": [], + "scratchblock": "broadcast [message v]", + "name": "broadcast(message)", + "class": "Stage", + "description": "This method sends a broadcast to all sprites and the stage. The broadcast can be received by using the whenIReceived method.", + "syntax": ["void broadcast(message)"], + "returns": "void", + "examples": [ + { + "src": "SpriteBroadcast.java", + "preview": "SpriteBroadcast.gif", + "lines": "reg:[Rr]ecorder" + } + ], + "parameters": [ + { + "name": "message", + "description": "The message to broadcast.", + "type": "String" + } + ] +} diff --git a/docs/en/book/reference/stage/events/whenIReceive.json b/docs/en/book/reference/stage/events/whenIReceive.json new file mode 100644 index 00000000..7cd9048a --- /dev/null +++ b/docs/en/book/reference/stage/events/whenIReceive.json @@ -0,0 +1,24 @@ +{ + "template": "method", + "related": [], + "scratchblock": "when I receive [message v]", + "name": "whenIReceive(message)", + "class": "Stage", + "description": "This method is called when a broadcast is received.", + "syntax": ["void whenIReceive(message)"], + "returns": "void", + "examples": [ + { + "src": "SpriteBroadcast.java", + "preview": "SpriteBroadcast.gif", + "lines": "reg:[Rr]ecorder" + } + ], + "parameters": [ + { + "name": "message", + "description": "The message of the broadcast.", + "type": "String" + } + ] +} diff --git a/examples/reference/SpriteBroadcast.gif b/examples/reference/SpriteBroadcast.gif new file mode 100644 index 00000000..9ea1f7df Binary files /dev/null and b/examples/reference/SpriteBroadcast.gif differ diff --git a/examples/reference/SpriteBroadcast.java b/examples/reference/SpriteBroadcast.java new file mode 100644 index 00000000..422b049d --- /dev/null +++ b/examples/reference/SpriteBroadcast.java @@ -0,0 +1,64 @@ +import org.openpatch.scratch.*; +import org.openpatch.scratch.extensions.recorder.*; + +public class SpriteBroadcast { + + public SpriteBroadcast() { + Stage myStage = new ReceiveStage(); + myStage.add(new BroadcastSprite()); + myStage.add(new ReceiveSprite()); + GifRecorder recorder = new GifRecorder( + "" + this.getClass().getName() + ".gif" + ); + recorder.start(); + while (myStage.getTimer().forMillis(3000)) {} + recorder.stop(); + Window.getInstance().exit(); + } + + public static void main(String[] args) { + new SpriteBroadcast(); + } +} + +class ReceiveStage extends Stage { + public ReceiveStage() { + super(254, 100); + } + + public void whenIReceive(String message) { + if (message.equals("change-custome")) { + this.changeColor(50); + } + } +} + +class BroadcastSprite extends Sprite { + + public BroadcastSprite() { + this.addCostume("zeta", "assets/zeta_green_badge.png"); + this.addCostume("gamma", "assets/gamma_purple_badge.png"); + } + + public void whenClicked() { + this.broadcast("change-custome"); + this.nextCostume(); + } +} + +class ReceiveSprite extends Sprite { + + public ReceiveSprite() { + this.addCostume("zeta", "assets/zeta_green_badge.png"); + this.addCostume("gamma", "assets/gamma_purple_badge.png"); + this.changeX(50); + } + + @Override + public void whenIReceive(String message) { + if ("change-custome".equals(message)) { + this.nextCostume(); + this.say("Got it!"); + } + } +} diff --git a/src/org/openpatch/scratch/Sprite.java b/src/org/openpatch/scratch/Sprite.java index a4b2af30..748ac951 100644 --- a/src/org/openpatch/scratch/Sprite.java +++ b/src/org/openpatch/scratch/Sprite.java @@ -47,9 +47,9 @@ public Sprite() { this.timer.put("default", new Timer()); } - public Sprite(final String name, final String imagePath) { + public Sprite(String name, final String imagePath) { this(); - final Image costume = new Image(name, imagePath); + Image costume = new Image(name, imagePath); this.costumes.add(costume); } @@ -58,14 +58,14 @@ public Sprite(final String name, final String imagePath) { * * @param s a Sprite object to copy */ - public Sprite(final Sprite s) { + public Sprite(Sprite s) { this.costumes = new CopyOnWriteArrayList<>(); - for (final Image costume : s.costumes) { + for (Image costume : s.costumes) { this.costumes.add(new Image(costume)); } this.currentCostume = s.currentCostume; this.sounds = new CopyOnWriteArrayList<>(); - for (final Sound sound : s.sounds) { + for (Sound sound : s.sounds) { this.sounds.add(new Sound(sound)); } this.show = s.show; @@ -80,7 +80,7 @@ public Sprite(final Sprite s) { this.stage = s.stage; } - public void addedToStage(final Stage stage) { + public void addedToStage(Stage stage) { this.stage = stage; this.pen.addedToStage(stage); this.text.addedToStage(stage); @@ -88,7 +88,7 @@ public void addedToStage(final Stage stage) { Applet.getInstance().registerMethod("mouseEvent", this); } - public void removedFromStage(final Stage stage) { + public void removedFromStage(Stage stage) { this.pen.removedFromStage(stage); Applet.getInstance().unregisterMethod("keyEvent", this); Applet.getInstance().unregisterMethod("mouseEvent", this); @@ -112,25 +112,26 @@ public Stage getStage() { * @param name a unique name * @param imagePath a image path */ - public void addCostume(final String name, final String imagePath) { - for (final Image costume : this.costumes) { + public void addCostume(String name, final String imagePath) { + for (Image costume : this.costumes) { if (costume.getName().equals(name)) { return; } } - final Image costume = new Image(name, imagePath); + Image costume = new Image(name, imagePath); this.costumes.add(costume); } - public void addCostume(final String name, final String spriteSheetPath, final int x, final int y, final int width, final int height) { - for (final Image costume : this.costumes) { + public void addCostume(String name, final String spriteSheetPath, final int x, final int y, final int width, + final int height) { + for (Image costume : this.costumes) { if (costume.getName().equals(name)) { return; } } - final Image costume = new Image(name, spriteSheetPath, x, y, width, height); + Image costume = new Image(name, spriteSheetPath, x, y, width, height); this.costumes.add(costume); } @@ -139,9 +140,9 @@ public void addCostume(final String name, final String spriteSheetPath, final in * * @param name the name of a costume */ - public void switchCostume(final String name) { + public void switchCostume(String name) { for (int i = 0; i < this.costumes.size(); i++) { - final Image costume = this.costumes.get(i); + Image costume = this.costumes.get(i); if (costume.getName().equals(name)) { this.currentCostume = i; return; @@ -182,14 +183,14 @@ public int getCurrentCostumeIndex() { * @param name a unique name * @param soundPath a sound path */ - public void addSound(final String name, final String soundPath) { - for (final Sound sound : this.sounds) { + public void addSound(String name, final String soundPath) { + for (Sound sound : this.sounds) { if (sound.getName().equals(name)) { return; } } - final Sound sound = new Sound(name, soundPath); + Sound sound = new Sound(name, soundPath); this.sounds.add(sound); } @@ -198,9 +199,9 @@ public void addSound(final String name, final String soundPath) { * * @param name the sound name */ - public void removeSound(final String name) { + public void removeSound(String name) { for (int i = 0; i < this.sounds.size(); i++) { - final Sound sound = this.sounds.get(i); + Sound sound = this.sounds.get(i); if (sound.getName().equals(name)) { this.sounds.remove(i); return; @@ -213,8 +214,8 @@ public void removeSound(final String name) { * * @param name the sound name */ - public void playSound(final String name) { - for (final Sound sound : this.sounds) { + public void playSound(String name) { + for (Sound sound : this.sounds) { if (sound.getName().equals(name) && !sound.isPlaying()) { sound.play(); } @@ -223,7 +224,7 @@ public void playSound(final String name) { /** Stops the playing of all sounds of the sprite. */ public void stopAllSounds() { - for (final Sound sound : this.sounds) { + for (Sound sound : this.sounds) { sound.stop(); } } @@ -233,8 +234,8 @@ public void stopAllSounds() { * * @param name Name of the sound */ - public void stopSound(final String name) { - for (final Sound sound : this.sounds) { + public void stopSound(String name) { + for (Sound sound : this.sounds) { if (sound.getName().equals(name)) { sound.stop(); break; @@ -247,8 +248,8 @@ public void stopSound(final String name) { * * @return playing */ - public boolean isSoundPlaying(final String name) { - for (final Sound sound : this.sounds) { + public boolean isSoundPlaying(String name) { + for (Sound sound : this.sounds) { if (sound.getName().equals(name)) { return sound.isPlaying(); } @@ -256,11 +257,11 @@ public boolean isSoundPlaying(final String name) { return false; } - public void setTint(final int r, final int g, final int b) { + public void setTint(int r, final int g, final int b) { this.setTint((float) b, (float) g, (float) b); } - public void setTint(final Color c) { + public void setTint(Color c) { this.setTint(c.getRed(), c.getGreen(), c.getBlue()); } @@ -269,11 +270,11 @@ public void setTint(final Color c) { * * @see Image#setTint(float, float, float) */ - public void setTint(final float r, final float g, final float b) { + public void setTint(float r, final float g, final float b) { if (this.costumes.size() == 0) return; - for (final Image costume : this.costumes) { + for (Image costume : this.costumes) { costume.setTint(r, g, b); } } @@ -283,11 +284,11 @@ public void setTint(final float r, final float g, final float b) { * * @see Image#setTint(float) */ - public void setTint(final float h) { + public void setTint(float h) { if (this.costumes.size() == 0) return; - for (final Image costume : this.costumes) { + for (Image costume : this.costumes) { costume.setTint(h); } } @@ -297,16 +298,16 @@ public void setTint(final float h) { * * @see Image#changeTint(float) */ - public void changeTint(final float step) { + public void changeTint(float step) { if (this.costumes.size() == 0) return; - for (final Image costume : this.costumes) { + for (Image costume : this.costumes) { costume.changeTint(step); } } - public void changeTint(final double step) { + public void changeTint(double step) { this.changeTint((float) step); } @@ -316,11 +317,11 @@ public void changeTint(final double step) { * @see Image#setTransparency(float) * @param transparency 0 full transparency, 255 no transparency */ - public void setTransparency(final float transparency) { + public void setTransparency(float transparency) { if (this.costumes.size() == 0) return; - for (final Image costume : this.costumes) { + for (Image costume : this.costumes) { costume.setTransparency(transparency); } } @@ -330,16 +331,16 @@ public void setTransparency(final float transparency) { * * @see Image#changeTransparency(float) */ - public void changeTransparency(final float step) { + public void changeTransparency(float step) { if (this.costumes.size() == 0) return; - for (final Image costume : this.costumes) { + for (Image costume : this.costumes) { costume.changeTransparency(step); } } - public void changeTransparency(final double step) { + public void changeTransparency(double step) { this.changeTransparency((float) step); } @@ -376,14 +377,14 @@ public float getSize() { * * @param percentage a percentage [0...100] */ - public void setSize(final float percentage) { + public void setSize(float percentage) { this.size = percentage; - for (final Image costume : this.costumes) { + for (Image costume : this.costumes) { costume.setSize(percentage); } } - public void setSize(final double percentage) { + public void setSize(double percentage) { this.setSize((float) percentage); } @@ -392,11 +393,11 @@ public void setSize(final double percentage) { * * @param amount a percentage [0...100] */ - public void changeSize(final float amount) { + public void changeSize(float amount) { this.size += amount; } - public void changeSize(final double amount) { + public void changeSize(double amount) { this.changeSize((float) amount); } @@ -407,33 +408,33 @@ public void changeSize(final double amount) { * * @param b */ - public void setOnEdgeBounce(final boolean b) { + public void setOnEdgeBounce(boolean b) { this.onEdgeBounce = b; } public void ifOnEdgeBounce() { - final float newX = this.x; - final float newY = this.y; + float newX = this.x; + float newY = this.y; Image currentCostume = null; if (this.costumes.size() > 0) { currentCostume = this.costumes.get(this.currentCostume); } - final float costumeWidth = currentCostume != null ? currentCostume.getWidth() : this.pen.getSize(); - final float costumeHeight = currentCostume != null ? currentCostume.getHeight() : this.pen.getSize(); + float costumeWidth = currentCostume != null ? currentCostume.getWidth() : this.pen.getSize(); + float costumeHeight = currentCostume != null ? currentCostume.getHeight() : this.pen.getSize(); - final float spriteWidth = this.show ? costumeWidth : this.pen.getSize(); + float spriteWidth = this.show ? costumeWidth : this.pen.getSize(); if (newX > Applet.getInstance().getWidth() - spriteWidth / 2 || newX < spriteWidth / 2) { this.setDirection(this.calculateAngleOfReflection(this.direction, false)); } - final float spriteHeight = this.show ? costumeHeight : this.pen.getSize(); + float spriteHeight = this.show ? costumeHeight : this.pen.getSize(); if (newY > Applet.getInstance().getHeight() - spriteHeight / 2 || newY < spriteHeight / 2) { this.setDirection(this.calculateAngleOfReflection(this.direction, true)); } this.setPosition(newX, newY); } - public void setRotationStyle(final RotationStyle style) { + public void setRotationStyle(RotationStyle style) { this.rotationStyle = style; } @@ -443,17 +444,17 @@ public void setRotationStyle(final RotationStyle style) { * @param x a x coordinate * @param y a y coordinate */ - public void setPosition(final int x, final int y) { + public void setPosition(int x, final int y) { this.x = x; this.y = y; this.getPen().setPosition(x, y); } - public void setPosition(final float x, final float y) { + public void setPosition(float x, final float y) { this.setPosition(Math.round(x), Math.round(y)); } - public void setPosition(final double x, final double y) { + public void setPosition(double x, final double y) { this.setPosition((float) x, (float) y); } @@ -462,7 +463,7 @@ public void setPosition(final double x, final double y) { * * @param v a vector */ - public void setPosition(final Vector2 v) { + public void setPosition(Vector2 v) { this.setPosition(v.getX(), v.getY()); } @@ -471,7 +472,7 @@ public void setPosition(final Vector2 v) { * * @param degrees between 0 and 360 */ - public void turnLeft(final float degrees) { + public void turnLeft(float degrees) { this.setDirection(this.direction - degrees); } @@ -480,7 +481,7 @@ public void turnLeft(final float degrees) { * * @param degrees between 0 and 360 */ - public void turnRight(final float degrees) { + public void turnRight(float degrees) { this.setDirection(this.direction + degrees); } @@ -491,7 +492,7 @@ public void turnRight(final float degrees) { * * @param degrees between 0 and 360 */ - public void setDirection(final float degrees) { + public void setDirection(float degrees) { this.direction = degrees; if (this.direction < 0) { this.direction += 360; @@ -499,7 +500,7 @@ public void setDirection(final float degrees) { this.direction %= 360; } - public void setDirection(final double degrees) { + public void setDirection(double degrees) { this.setDirection((float) degrees); } @@ -508,35 +509,35 @@ public void setDirection(final double degrees) { * * @param v a vector */ - public void setDirection(final Vector2 v) { + public void setDirection(Vector2 v) { this.setDirection(v.angle()); } - public void pointInDirection(final float degrees) { + public void pointInDirection(float degrees) { this.setDirection(degrees); } - public void pointInDirection(final double degrees) { + public void pointInDirection(double degrees) { this.setDirection(degrees); } - public void pointInDirection(final Vector2 v) { + public void pointInDirection(Vector2 v) { this.setDirection(v); } public void pointTowardsMousePointer() { - final float mx = this.getMouseX(); - final float my = this.getMouseY(); + float mx = this.getMouseX(); + float my = this.getMouseY(); - final double angle = new Vector2(mx - this.x, my - this.y).angle(); + double angle = new Vector2(mx - this.x, my - this.y).angle(); this.setDirection(angle); } - public void pointTowardsSprite(final Sprite s) { - final float mx = s.getX(); - final float my = s.getY(); + public void pointTowardsSprite(Sprite s) { + float mx = s.getX(); + float my = s.getY(); - final double angle = new Vector2(mx - this.x, my - this.y).angle(); + double angle = new Vector2(mx - this.x, my - this.y).angle(); this.setDirection(angle); } @@ -563,10 +564,10 @@ public Pen getPen() { * * @param steps a number of pixels */ - public void move(final float steps) { + public void move(float steps) { // convert degrees to radians - final float newX = steps * (float) Math.cos(this.direction * Math.PI / 180) + this.x; - final float newY = steps * (float) Math.sin(this.direction * Math.PI / 180) + this.y; + float newX = steps * (float) Math.cos(this.direction * Math.PI / 180) + this.x; + float newY = steps * (float) Math.sin(this.direction * Math.PI / 180) + this.y; this.x = newX; this.y = newY; @@ -578,7 +579,7 @@ public void move(final float steps) { this.pen.setPosition(this.x, this.y); } - public void move(final double steps) { + public void move(double steps) { this.move((float) steps); } @@ -589,7 +590,7 @@ public void move(final double steps) { * * @param v a vector */ - public void move(final Vector2 v) { + public void move(Vector2 v) { this.setDirection(v.angle()); this.move(v.length()); } @@ -608,12 +609,12 @@ public float getX() { * * @param x a x coordinate */ - public void setX(final float x) { + public void setX(float x) { this.x = x; this.pen.setPosition(this.x, this.y); } - public void setX(final double x) { + public void setX(double x) { this.setX((float) x); } @@ -622,12 +623,12 @@ public void setX(final double x) { * * @param x number in pixels */ - public void changeX(final float x) { + public void changeX(float x) { this.x += x; this.pen.setPosition(this.x, this.y); } - public void changeX(final double x) { + public void changeX(double x) { this.changeX((float) x); } @@ -645,12 +646,12 @@ public float getY() { * * @param y a y coordinate */ - public void setY(final float y) { + public void setY(float y) { this.y = y; this.pen.setPosition(this.x, this.y); } - public void setY(final double y) { + public void setY(double y) { this.setY((float) y); } @@ -659,12 +660,12 @@ public void setY(final double y) { * * @param y number in pixels */ - public void changeY(final float y) { + public void changeY(float y) { this.y += y; this.pen.setPosition(this.x, this.y); } - public void changeY(final double y) { + public void changeY(double y) { this.changeY((float) y); } @@ -708,7 +709,7 @@ public Timer getTimer() { * * @return a timer */ - public Timer getTimer(final String name) { + public Timer getTimer(String name) { return this.timer.get(name); } @@ -717,7 +718,7 @@ public Timer getTimer(final String name) { * * @param name the name of the timer */ - public void addTimer(final String name) { + public void addTimer(String name) { if ("default".equals(name)) return; @@ -729,14 +730,14 @@ public void addTimer(final String name) { * * @param name the name of the timer */ - public void removeTimer(final String name) { + public void removeTimer(String name) { if ("default".equals(name)) return; this.timer.remove(name); } - private float calculateAngleOfReflection(final float angleOfIncidence, final boolean horizontalWall) { + private float calculateAngleOfReflection(float angleOfIncidence, final boolean horizontalWall) { if (horizontalWall) { float angleOfReflection = 360 - angleOfIncidence; while (angleOfReflection < 0) @@ -757,25 +758,25 @@ private float calculateAngleOfReflection(final float angleOfIncidence, final boo * @return true if touching */ public boolean isTouchingMousePointer() { - final float topLeftCornerX = this.x - this.getWidth() / 2.0f; - final float topLeftCornerY = this.y - this.getHeight() / 2.0f; + float topLeftCornerX = this.x - this.getWidth() / 2.0f; + float topLeftCornerY = this.y - this.getHeight() / 2.0f; - final float bottomRightCornerX = this.x + this.getWidth() / 2.0f; - final float bottomRightCornerY = this.y + this.getHeight() / 2.0f; + float bottomRightCornerX = this.x + this.getWidth() / 2.0f; + float bottomRightCornerY = this.y + this.getHeight() / 2.0f; - final float[] mouse = Stage.rotateXY(this.getMouseX(), this.getMouseY(), this.x, this.y, -this.direction); + float[] mouse = Stage.rotateXY(this.getMouseX(), this.getMouseY(), this.x, this.y, -this.direction); - final boolean touching = mouse[0] > topLeftCornerX + boolean touching = mouse[0] > topLeftCornerX && mouse[1] > topLeftCornerY && mouse[0] < bottomRightCornerX && mouse[1] < bottomRightCornerY; if (touching) { - final int relativeMouseX = Math.round(mouse[0] - topLeftCornerX); - final int relativeMouseY = Math.round(mouse[1] - topLeftCornerY); + int relativeMouseX = Math.round(mouse[0] - topLeftCornerX); + int relativeMouseY = Math.round(mouse[1] - topLeftCornerY); if (this.costumes.size() > this.getCurrentCostumeIndex()) { - final int color = this.costumes + int color = this.costumes .get(this.getCurrentCostumeIndex()) .getPixel(relativeMouseX, relativeMouseY); return Applet.getInstance().alpha(color) != 0; @@ -796,24 +797,28 @@ public boolean isTouchingEdge() { if (this.costumes.size() > this.getCurrentCostumeIndex()) { currentCostume = this.costumes.get(this.getCurrentCostumeIndex()); } - final PApplet parent = Applet.getInstance(); - final float costumeWidth = currentCostume != null ? currentCostume.getWidth() : this.pen.getSize(); - final float costumeHeight = currentCostume != null ? currentCostume.getHeight() : this.pen.getSize(); - final float spriteWidth = this.show ? costumeWidth : this.pen.getSize(); - final float spriteHeight = this.show ? costumeHeight : this.pen.getSize(); - - final float[] cornerTopLeft = Stage.rotateXY(this.x - spriteWidth / 2.0f, this.y - spriteHeight / 2.0f, this.x, this.y, this.direction); - final float[] cornerTopRight = Stage.rotateXY(this.x + spriteWidth / 2.0f, this.y - spriteHeight / 2.0f, this.x, this.y, this.direction); - final float[] cornerBottomLeft = Stage.rotateXY(this.x - spriteWidth / 2.0f, this.y + spriteHeight / 2.0f, this.x, this.y, this.direction); - final float[] cornerBottomRight = Stage.rotateXY(this.x + spriteWidth / 2.0f, this.y + spriteHeight / 2.0f, this.x, this.y, this.direction); - - final float[][] corners = { + PApplet parent = Applet.getInstance(); + float costumeWidth = currentCostume != null ? currentCostume.getWidth() : this.pen.getSize(); + float costumeHeight = currentCostume != null ? currentCostume.getHeight() : this.pen.getSize(); + float spriteWidth = this.show ? costumeWidth : this.pen.getSize(); + float spriteHeight = this.show ? costumeHeight : this.pen.getSize(); + + float[] cornerTopLeft = Stage.rotateXY(this.x - spriteWidth / 2.0f, this.y - spriteHeight / 2.0f, this.x, this.y, + this.direction); + float[] cornerTopRight = Stage.rotateXY(this.x + spriteWidth / 2.0f, this.y - spriteHeight / 2.0f, this.x, this.y, + this.direction); + float[] cornerBottomLeft = Stage.rotateXY(this.x - spriteWidth / 2.0f, this.y + spriteHeight / 2.0f, this.x, this.y, + this.direction); + float[] cornerBottomRight = Stage.rotateXY(this.x + spriteWidth / 2.0f, this.y + spriteHeight / 2.0f, this.x, + this.y, this.direction); + + float[][] corners = { cornerTopLeft, cornerTopRight, cornerBottomLeft, cornerBottomRight, }; - for (final float[] corner : corners) { - final float cornerX = corner[0]; - final float cornerY = corner[1]; + for (float[] corner : corners) { + float cornerX = corner[0]; + float cornerY = corner[1]; if (cornerX > parent.width || cornerX < 0) { return true; } @@ -827,25 +832,25 @@ public boolean isTouchingEdge() { } public float distanceToMousePointer() { - final float x2 = this.getMouseX(); - final float y2 = this.getMouseY(); - final float x1 = this.getX(); - final float y1 = this.getY(); + float x2 = this.getMouseX(); + float y2 = this.getMouseY(); + float x1 = this.getX(); + float y1 = this.getY(); return (float) Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2)); } - public float distanceToSprite(final Sprite sprite) { - final float x2 = sprite.getX(); - final float y2 = sprite.getY(); - final float x1 = this.getX(); - final float y1 = this.getY(); + public float distanceToSprite(Sprite sprite) { + float x2 = sprite.getX(); + float y2 = sprite.getY(); + float x1 = this.getX(); + float y1 = this.getY(); return (float) Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2)); } - public void setHitbox(final int... points) { - final int l = points.length / 2; - final int[] xPoints = new int[l]; - final int[] yPoints = new int[l]; + public void setHitbox(int... points) { + int l = points.length / 2; + int[] xPoints = new int[l]; + int[] yPoints = new int[l]; for (int i = 0; i < points.length; i += 2) { xPoints[i / 2] = points[i]; yPoints[i / 2] = points[i + 1]; @@ -853,11 +858,11 @@ public void setHitbox(final int... points) { this.hitbox = new Hitbox(xPoints, yPoints); } - public void setHitbox(final int[] xPoints, final int[] yPoints) { + public void setHitbox(int[] xPoints, final int[] yPoints) { this.hitbox = new Hitbox(xPoints, yPoints); } - public void setHitbox(final Hitbox hitbox) { + public void setHitbox(Hitbox hitbox) { this.hitbox = hitbox; } @@ -866,10 +871,10 @@ public Hitbox getHitbox() { if (this.costumes.size() > this.getCurrentCostumeIndex()) { currentCostume = this.costumes.get(this.getCurrentCostumeIndex()); } - final float costumeWidth = currentCostume != null ? currentCostume.getWidth() : this.pen.getSize(); - final float costumeHeight = currentCostume != null ? currentCostume.getHeight() : this.pen.getSize(); - final float spriteWidth = this.show ? costumeWidth : this.pen.getSize(); - final float spriteHeight = this.show ? costumeHeight : this.pen.getSize(); + float costumeWidth = currentCostume != null ? currentCostume.getWidth() : this.pen.getSize(); + float costumeHeight = currentCostume != null ? currentCostume.getHeight() : this.pen.getSize(); + float spriteWidth = this.show ? costumeWidth : this.pen.getSize(); + float spriteHeight = this.show ? costumeHeight : this.pen.getSize(); if (this.hitbox != null) { this.hitbox.translateAndRotateAndResize( @@ -877,13 +882,17 @@ public Hitbox getHitbox() { return this.hitbox; } - final float[] cornerTopLeft = Stage.rotateXY(this.x - spriteWidth / 2.0f, this.y - spriteHeight / 2.0f, this.x, this.y, this.direction); - final float[] cornerTopRight = Stage.rotateXY(this.x + spriteWidth / 2.0f, this.y - spriteHeight / 2.0f, this.x, this.y, this.direction); - final float[] cornerBottomLeft = Stage.rotateXY(this.x - spriteWidth / 2.0f, this.y + spriteHeight / 2.0f, this.x, this.y, this.direction); - final float[] cornerBottomRight = Stage.rotateXY(this.x + spriteWidth / 2.0f, this.y + spriteHeight / 2.0f, this.x, this.y, this.direction); - - final int[] xPoints = new int[4]; - final int[] yPoints = new int[4]; + float[] cornerTopLeft = Stage.rotateXY(this.x - spriteWidth / 2.0f, this.y - spriteHeight / 2.0f, this.x, this.y, + this.direction); + float[] cornerTopRight = Stage.rotateXY(this.x + spriteWidth / 2.0f, this.y - spriteHeight / 2.0f, this.x, this.y, + this.direction); + float[] cornerBottomLeft = Stage.rotateXY(this.x - spriteWidth / 2.0f, this.y + spriteHeight / 2.0f, this.x, this.y, + this.direction); + float[] cornerBottomRight = Stage.rotateXY(this.x + spriteWidth / 2.0f, this.y + spriteHeight / 2.0f, this.x, + this.y, this.direction); + + int[] xPoints = new int[4]; + int[] yPoints = new int[4]; xPoints[0] = Math.round(cornerTopLeft[0]); yPoints[0] = Math.round(cornerTopLeft[1]); xPoints[1] = Math.round(cornerTopRight[0]); @@ -893,18 +902,18 @@ public Hitbox getHitbox() { xPoints[3] = Math.round(cornerBottomLeft[0]); yPoints[3] = Math.round(cornerBottomLeft[1]); - final Hitbox hitbox = new Hitbox(xPoints, yPoints); + Hitbox hitbox = new Hitbox(xPoints, yPoints); return hitbox; } - public boolean isTouchingSprite(final Sprite sprite) { + public boolean isTouchingSprite(Sprite sprite) { if (sprite == null || !sprite.show) return false; return this.getHitbox().intersects(sprite.getHitbox()); } - public boolean isTouchingSprite(final Class c) { - for (final Drawable d : this.stage.drawables) { + public boolean isTouchingSprite(Class c) { + for (Drawable d : this.stage.drawables) { if (c.isInstance(d) && this.isTouchingSprite((Sprite) d)) { return true; } @@ -912,8 +921,8 @@ public boolean isTouchingSprite(final Class c) { return false; } - public Sprite getTouchingSprite(final Class c) { - for (final Drawable d : this.stage.drawables) { + public Sprite getTouchingSprite(Class c) { + for (Drawable d : this.stage.drawables) { if (c.isInstance(d) && this.isTouchingSprite((Sprite) d)) { return (Sprite) d; } @@ -921,9 +930,9 @@ public Sprite getTouchingSprite(final Class c) { return null; } - public ArrayList getTouchingSprites(final Class c) { - final ArrayList sprites = new ArrayList<>(); - for (final Drawable d : this.stage.drawables) { + public ArrayList getTouchingSprites(Class c) { + ArrayList sprites = new ArrayList<>(); + for (Drawable d : this.stage.drawables) { if (c.isInstance(d) && this.isTouchingSprite((Sprite) d)) { sprites.add((Sprite) d); } @@ -964,7 +973,7 @@ public boolean isMouseDown() { * @param keyCode a key code * @return key pressed */ - public boolean isKeyPressed(final int keyCode) { + public boolean isKeyPressed(int keyCode) { return this.stage.isKeyPressed(keyCode); } @@ -1049,7 +1058,7 @@ public int getDaysSince2000() { return this.stage.getDaysSince2000(); } - public void keyEvent(final KeyEvent e) { + public void keyEvent(KeyEvent e) { switch (e.getAction()) { case KeyEvent.PRESS: this.whenKeyPressed(e.getKeyCode()); @@ -1060,17 +1069,17 @@ public void keyEvent(final KeyEvent e) { } } - public void whenKeyPressed(final int keyCode) { + public void whenKeyPressed(int keyCode) { } - public void whenKeyReleased(final int keyCode) { + public void whenKeyReleased(int keyCode) { } - public void mouseEvent(final MouseEvent e) { + public void mouseEvent(MouseEvent e) { this.whenMouseMoved(e.getX(), e.getY()); } - public void whenMouseMoved(final float x, final float y) { + public void whenMouseMoved(float x, final float y) { } public void whenClicked() { @@ -1084,18 +1093,18 @@ public void goToBackLayer() { this.stage.goToBackLayer(this); } - public void goLayersForwards(final int number) { + public void goLayersForwards(int number) { this.stage.goLayersForwards(this, number); } - public void goLayersBackwards(final int number) { + public void goLayersBackwards(int number) { this.stage.goLayersBackwards(this, number); } - public void whenBackdropSwitches(final String name) { + public void whenBackdropSwitches(String name) { } - public int pickRandom(final int from, final int to) { + public int pickRandom(int from, final int to) { if (to < from) { return to + (int) (Math.random() * (from - to)); } @@ -1106,26 +1115,37 @@ public Text getText() { return this.text; } - public void think(final String text) { + public void think(String text) { this.text.setStyle(TextStyle.THINK); this.text.showText(text); } - public void think(final String text, final int millis) { + public void think(String text, final int millis) { this.text.setStyle(TextStyle.THINK); this.text.showText(text, millis); } - public void say(final String text) { + public void say(String text) { this.text.setStyle(TextStyle.SPEAK); this.text.showText(text); } - public void say(final String text, final int millis) { + public void say(String text, final int millis) { this.text.setStyle(TextStyle.SPEAK); this.text.showText(text, millis); } + public void broadcast(String message) { + if (this.getStage() != null) { + this.getStage().broadcast(message); + this.getStage().whenIReceive(message); + } + } + + public void whenIReceive(String message) { + + } + /** Draws the sprite if it is not hidden. */ public void draw() { if (this.stage == null) diff --git a/src/org/openpatch/scratch/Stage.java b/src/org/openpatch/scratch/Stage.java index d1bbe3ad..4e783d12 100644 --- a/src/org/openpatch/scratch/Stage.java +++ b/src/org/openpatch/scratch/Stage.java @@ -42,20 +42,20 @@ public Stage() { this(480, 360); } - public Stage(final int width, final int height) { + public Stage(int width, final int height) { this(width, height, false); } - public Stage(final int width, final int height, final boolean debug) { + public Stage(int width, final int height, final boolean debug) { this.drawables = new CopyOnWriteArrayList<>(); this.timer = new ConcurrentHashMap<>(); if (Window.getInstance() == null) { new Window(width, height); - final Applet a = Applet.getInstance(); + Applet a = Applet.getInstance(); a.setDebug(debug); a.addStage("main", this); } - final Applet applet = Applet.getInstance(); + Applet applet = Applet.getInstance(); this.penBuffer = applet.createGraphics(applet.width, applet.height, applet.sketchRenderer()); /** * Smooth does currently not work on Apple Silicon @@ -70,7 +70,7 @@ public Stage(final int width, final int height, final boolean debug) { /** * @deprecated since v3.2.0: Use stage.getWindow().setDebug(debug) instead */ - public void setDebug(final boolean debug) { + public void setDebug(boolean debug) { Applet.getInstance().setDebug(debug); } @@ -86,13 +86,13 @@ public boolean isDebug() { * * @param drawable */ - public void add(final Drawable drawable) { + public void add(Drawable drawable) { this.drawables.add(drawable); drawable.addedToStage(this); } - public void goLayersBackwards(final Drawable drawable, final int number) { - final int index = this.drawables.indexOf(drawable); + public void goLayersBackwards(Drawable drawable, final int number) { + int index = this.drawables.indexOf(drawable); if (index == -1) return; int newIndex = index - number; @@ -103,8 +103,8 @@ public void goLayersBackwards(final Drawable drawable, final int number) { this.drawables.add(newIndex, drawable); } - public void goLayersForwards(final Drawable drawable, final int number) { - final int index = this.drawables.indexOf(drawable); + public void goLayersForwards(Drawable drawable, final int number) { + int index = this.drawables.indexOf(drawable); if (index == -1) return; int newIndex = index + number; @@ -115,12 +115,12 @@ public void goLayersForwards(final Drawable drawable, final int number) { this.drawables.add(newIndex, drawable); } - public void goToFrontLayer(final Drawable drawable) { + public void goToFrontLayer(Drawable drawable) { this.drawables.remove(drawable); this.drawables.add(drawable); } - public void goToBackLayer(final Drawable drawable) { + public void goToBackLayer(Drawable drawable) { this.drawables.remove(drawable); this.drawables.add(0, drawable); } @@ -134,20 +134,20 @@ public List getAll() { * * @param drawable */ - public void remove(final Drawable drawable) { + public void remove(Drawable drawable) { this.drawables.remove(drawable); drawable.removedFromStage(this); } public void removeAll() { - for (final Drawable drawable : this.drawables) { + for (Drawable drawable : this.drawables) { drawable.removedFromStage(this); } this.drawables.clear(); } - public void remove(final Class c) { - for (final Drawable drawable : this.drawables) { + public void remove(Class c) { + for (Drawable drawable : this.drawables) { if (c.isInstance(drawable)) { drawable.removedFromStage(this); } @@ -160,9 +160,9 @@ public void remove(final Class c) { * * @param c Class */ - public List find(final Class c) { - final ArrayList drawables = new ArrayList<>(); - for (final Drawable d : this.drawables) { + public List find(Class c) { + ArrayList drawables = new ArrayList<>(); + for (Drawable d : this.drawables) { if (c.isInstance(d)) { drawables.add(d); } @@ -177,13 +177,13 @@ public List find(final Class c) { * @param name a unique name * @param imagePath a image path */ - public void addBackdrop(final String name, final String imagePath) { - for (final Image backdrop : this.backdrops) { + public void addBackdrop(String name, final String imagePath) { + for (Image backdrop : this.backdrops) { if (backdrop.getName().equals(name)) { return; } } - final Image backdrop = new Image(name, imagePath); + Image backdrop = new Image(name, imagePath); this.backdrops.add(backdrop); backdrop.addedToStage(this); } @@ -193,9 +193,9 @@ public void addBackdrop(final String name, final String imagePath) { * * @param name of the backdrop */ - public void removeBackdrop(final String name) { + public void removeBackdrop(String name) { for (int i = 0; i < this.backdrops.size(); i++) { - final Image backdrop = this.backdrops.get(i); + Image backdrop = this.backdrops.get(i); if (backdrop.getName().equals(name)) { this.backdrops.remove(i); return; @@ -208,9 +208,9 @@ public void removeBackdrop(final String name) { * * @param name the name of a backdrop */ - public void switchBackdrop(final String name) { + public void switchBackdrop(String name) { for (int i = 0; i < this.backdrops.size(); i++) { - final Image backdrop = this.backdrops.get(i); + Image backdrop = this.backdrops.get(i); if (backdrop.getName().equals(name)) { this.currentBackdrop = i; this.emitBackdropSwitch(); @@ -220,19 +220,19 @@ public void switchBackdrop(final String name) { } private void emitBackdropSwitch() { - final Image backdrop = this.backdrops.get(this.currentBackdrop); - final String name = backdrop.getName(); + Image backdrop = this.backdrops.get(this.currentBackdrop); + String name = backdrop.getName(); this.drawables.stream() .forEach( d -> { - if (d instanceof Sprite) { - ((Sprite) d).whenBackdropSwitches(name); - } + if (d instanceof Sprite) { + ((Sprite) d).whenBackdropSwitches(name); + } }); this.whenBackdropSwitches(name); } - public void whenBackdropSwitches(final String name) { + public void whenBackdropSwitches(String name) { } /** Switch to the next backdrop. */ @@ -249,7 +249,7 @@ public void previousBackdrop() { /** Switch to a random backdrop. */ public void randomBackdrop() { - final int size = this.backdrops.size(); + int size = this.backdrops.size(); this.currentBackdrop = this.pickRandom(0, size - 1) % size; this.emitBackdropSwitch(); } @@ -276,7 +276,7 @@ public int getCurrentBackdropIndex() { public void eraseAll() { try { this.penBuffer = Applet.getInstance().createGraphics(this.getWidth(), this.getHeight()); - } catch (final Exception e) { + } catch (Exception e) { } } @@ -287,14 +287,14 @@ public void eraseAll() { * @param name a unique name * @param soundPath a sound path */ - public void addSound(final String name, final String soundPath) { - for (final Sound sound : this.sounds) { + public void addSound(String name, final String soundPath) { + for (Sound sound : this.sounds) { if (sound.getName().equals(name)) { return; } } - final Sound sound = new Sound(name, soundPath); + Sound sound = new Sound(name, soundPath); this.sounds.add(sound); } @@ -303,9 +303,9 @@ public void addSound(final String name, final String soundPath) { * * @param name the sound name */ - public void removeSound(final String name) { + public void removeSound(String name) { for (int i = 0; i < this.sounds.size(); i++) { - final Sound sound = this.sounds.get(i); + Sound sound = this.sounds.get(i); if (sound.getName().equals(name)) { this.sounds.remove(i); return; @@ -318,8 +318,8 @@ public void removeSound(final String name) { * * @param name the sound name */ - public void playSound(final String name) { - for (final Sound sound : this.sounds) { + public void playSound(String name) { + for (Sound sound : this.sounds) { if (sound.getName().equals(name) && !sound.isPlaying()) { sound.play(); } @@ -328,7 +328,7 @@ public void playSound(final String name) { /** Stops the playing of all sounds of the stage. */ public void stopAllSounds() { - for (final Sound sound : this.sounds) { + for (Sound sound : this.sounds) { sound.stop(); } } @@ -338,8 +338,8 @@ public void stopAllSounds() { * * @param name Name of the sound */ - public void stopSound(final String name) { - for (final Sound sound : this.sounds) { + public void stopSound(String name) { + for (Sound sound : this.sounds) { if (sound.getName().equals(name)) { sound.stop(); break; @@ -352,8 +352,8 @@ public void stopSound(final String name) { * * @return playing */ - public boolean isSoundPlaying(final String name) { - for (final Sound sound : this.sounds) { + public boolean isSoundPlaying(String name) { + for (Sound sound : this.sounds) { if (sound.getName().equals(name)) { return sound.isPlaying(); } @@ -376,7 +376,7 @@ public PGraphics getPenBuffer() { * * @param h a hue value [0...255] */ - public void setColor(final float h) { + public void setColor(float h) { this.color.setHSB(h); } @@ -387,11 +387,11 @@ public void setColor(final float h) { * @param g a green value [0...255] * @param b a blue value [0...255] */ - public void setColor(final float r, final float g, final float b) { + public void setColor(float r, final float g, final float b) { this.color.setRGB(r, g, b); } - public void setColor(final Color c) { + public void setColor(Color c) { this.color = c; } @@ -400,11 +400,11 @@ public void setColor(final Color c) { * * @param h a step value */ - public void changeColor(final float h) { + public void changeColor(float h) { this.color.changeColor(h); } - public void changeColor(final double h) { + public void changeColor(double h) { this.changeColor((float) h); } @@ -413,7 +413,7 @@ public void changeColor(final double h) { * * @see Image#setTint(float, float, float) */ - public void setTint(final int r, final int g, final int b) { + public void setTint(int r, final int g, final int b) { if (this.backdrops.size() == 0) return; this.backdrops.get(this.currentBackdrop).setTint(r, g, b); @@ -424,7 +424,7 @@ public void setTint(final int r, final int g, final int b) { * * @see Image#setTint(float) */ - public void setTint(final float h) { + public void setTint(float h) { if (this.backdrops.size() == 0) return; this.backdrops.get(this.currentBackdrop).setTint(h); @@ -435,7 +435,7 @@ public void setTint(final float h) { * * @see Image#changeTint(float) */ - public void changeTint(final float step) { + public void changeTint(float step) { if (this.backdrops.size() == 0) return; @@ -447,11 +447,11 @@ public void changeTint(final float step) { * * @see Image#setTransparency(float) */ - public void setTransparency(final float transparency) { + public void setTransparency(float transparency) { this.backdrops.get(this.currentBackdrop).setTransparency(transparency); } - public void setTransparency(final double transparency) { + public void setTransparency(double transparency) { this.setTransparency((float) transparency); } @@ -460,7 +460,7 @@ public void setTransparency(final double transparency) { * * @see Image#changeTransparency(float) */ - public void changeTransparency(final float step) { + public void changeTransparency(float step) { if (this.backdrops.size() == 0) return; @@ -502,7 +502,7 @@ public Timer getTimer() { * @param name a name * @return the timer */ - public Timer getTimer(final String name) { + public Timer getTimer(String name) { return this.timer.get(name); } @@ -511,7 +511,7 @@ public Timer getTimer(final String name) { * * @param name the name of the timer */ - public void addTimer(final String name) { + public void addTimer(String name) { if ("default".equals(name)) return; @@ -523,14 +523,14 @@ public void addTimer(final String name) { * * @param name the name of the timer */ - public void removeTimer(final String name) { + public void removeTimer(String name) { if ("default".equals(name)) return; this.timer.remove(name); } - public void mouseEvent(final MouseEvent e) { + public void mouseEvent(MouseEvent e) { this.mouseX = e.getX(); this.mouseY = e.getY(); this.mouseDown = false; @@ -577,10 +577,10 @@ public boolean isMouseDown() { return this.mouseDown; } - public void whenKeyPressed(final int keyCode) { + public void whenKeyPressed(int keyCode) { } - public void keyEvent(final KeyEvent e) { + public void keyEvent(KeyEvent e) { switch (e.getAction()) { case KeyEvent.PRESS: this.whenKeyPressed(e.getKeyCode()); @@ -598,8 +598,8 @@ public void keyEvent(final KeyEvent e) { * @param keyCode a key code * @return key pressed */ - public boolean isKeyPressed(final int keyCode) { - final Boolean isPressed = this.keyCodePressed.get(keyCode); + public boolean isKeyPressed(int keyCode) { + Boolean isPressed = this.keyCodePressed.get(keyCode); if (isPressed == null) { return false; } @@ -612,7 +612,7 @@ public boolean isKeyPressed(final int keyCode) { * @return current year */ public int getCurrentYear() { - final LocalDateTime now = LocalDateTime.now(); + LocalDateTime now = LocalDateTime.now(); return now.getYear(); } @@ -622,7 +622,7 @@ public int getCurrentYear() { * @return current month */ public int getCurrentMonth() { - final LocalDateTime now = LocalDateTime.now(); + LocalDateTime now = LocalDateTime.now(); return now.getMonthValue(); } @@ -632,7 +632,7 @@ public int getCurrentMonth() { * @return current day of the week */ public int getCurrentDayOfWeek() { - final LocalDateTime now = LocalDateTime.now(); + LocalDateTime now = LocalDateTime.now(); return now.getDayOfWeek().getValue(); } @@ -642,7 +642,7 @@ public int getCurrentDayOfWeek() { * @return current day of the month */ public int getCurrentDay() { - final LocalDateTime now = LocalDateTime.now(); + LocalDateTime now = LocalDateTime.now(); return now.getDayOfMonth(); } @@ -652,7 +652,7 @@ public int getCurrentDay() { * @return current hour */ public int getCurrentHour() { - final LocalDateTime now = LocalDateTime.now(); + LocalDateTime now = LocalDateTime.now(); return now.getHour(); } @@ -662,7 +662,7 @@ public int getCurrentHour() { * @return current minute */ public int getCurrentMinute() { - final LocalDateTime now = LocalDateTime.now(); + LocalDateTime now = LocalDateTime.now(); return now.getMinute(); } @@ -672,7 +672,7 @@ public int getCurrentMinute() { * @return current second */ public int getCurrentSecond() { - final LocalDateTime now = LocalDateTime.now(); + LocalDateTime now = LocalDateTime.now(); return now.getSecond(); } @@ -682,7 +682,7 @@ public int getCurrentSecond() { * @return current millisecond */ public int getCurrentMillisecond() { - final LocalDateTime now = LocalDateTime.now(); + LocalDateTime now = LocalDateTime.now(); return (int) Math.round(now.getNano() / 1000000.0); } @@ -692,30 +692,44 @@ public int getCurrentMillisecond() { * @return days since 2010/01/01 */ public int getDaysSince2000() { - final LocalDate now = LocalDate.now(); - final LocalDate then = LocalDate.of(2000, Month.JANUARY, 1); - final long c = ChronoUnit.DAYS.between(then, now); + LocalDate now = LocalDate.now(); + LocalDate then = LocalDate.of(2000, Month.JANUARY, 1); + long c = ChronoUnit.DAYS.between(then, now); return (int) c; } - public int pickRandom(final int from, final int to) { + public int pickRandom(int from, final int to) { if (to < from) { return to + (int) (Math.random() * (from - to)); } return from + (int) (Math.random() * (to - from)); } - public void display(final String text) { + public void display(String text) { this.display.showText(text); } - public void display(final String text, final int millis) { + public void display(String text, final int millis) { this.display.showText(text, millis); } + public void broadcast(String message) { + this.drawables.stream() + .forEach( + d -> { + if (d instanceof Sprite) { + ((Sprite) d).whenIReceive(message); + } + }); + } + + public void whenIReceive(String message) { + + } + /** Draws the current backdrop or if none a solid color */ public void pre() { - final Applet applet = Applet.getInstance(); + Applet applet = Applet.getInstance(); if (applet == null) return; // redraw background to clear screen @@ -730,7 +744,7 @@ public void pre() { } else { try { this.penBuffer.loadPixels(); - } catch (final Exception e) { + } catch (Exception e) { } } } @@ -740,10 +754,10 @@ public void pre() { * * @param millis Milliseconds */ - public void wait(final int millis) { + public void wait(int millis) { try { Thread.sleep(millis); - } catch (final InterruptedException e) { + } catch (InterruptedException e) { } } @@ -751,10 +765,10 @@ public void run() { } public void draw() { - final Applet applet = Applet.getInstance(); + Applet applet = Applet.getInstance(); if (applet == null) return; - for (final Drawable d : this.drawables) { + for (Drawable d : this.drawables) { d.draw(); } if (this.display != null) { @@ -774,9 +788,9 @@ public void draw() { } public static float[] rotateXY(float x, float y, final float originX, final float originY, final float degrees) { - final float[] rotatedXY = new float[2]; + float[] rotatedXY = new float[2]; - final double radians = degrees * Math.PI / 180.0; + double radians = degrees * Math.PI / 180.0; x = x - originX; y = y - originY; rotatedXY[0] = (float) (x * Math.cos(radians) - y * Math.sin(radians)) + originX; diff --git a/src/org/openpatch/scratch/Window.java b/src/org/openpatch/scratch/Window.java index e01e059e..b0a94a65 100644 --- a/src/org/openpatch/scratch/Window.java +++ b/src/org/openpatch/scratch/Window.java @@ -11,15 +11,15 @@ public Window() { this(480, 360); } - public Window(final String assets) { + public Window(String assets) { this(480, 360, assets); } - public Window(final int width, final int height) { + public Window(int width, final int height) { this(width, height, null); } - public Window(final int width, final int height, final String assets) { + public Window(int width, final int height, final String assets) { super(); if (Window.instance != null) { throw new Error("You can only have one Window."); @@ -37,7 +37,7 @@ public boolean isDebug() { return Applet.getInstance().isDebug(); } - public void setDebug(final boolean debug) { + public void setDebug(boolean debug) { Applet.getInstance().setDebug(debug); } @@ -49,19 +49,19 @@ public int getHeight() { return Applet.getInstance().getHeight(); } - public void addStage(final String name, final Stage stage) { + public void addStage(String name, final Stage stage) { Applet.getInstance().addStage(name, stage); } - public Stage getStage(final String name) { + public Stage getStage(String name) { return Applet.getInstance().getStage(name); } - public void removeStage(final String name) { + public void removeStage(String name) { Applet.getInstance().removeStage(name); } - public void switchStage(final String name) { + public void switchStage(String name) { Applet.getInstance().switchStage(name); } diff --git a/src/org/openpatch/scratch/internal/AnimatedGifEncoder.java b/src/org/openpatch/scratch/internal/AnimatedGifEncoder.java index ec7f1c34..76bf0844 100644 --- a/src/org/openpatch/scratch/internal/AnimatedGifEncoder.java +++ b/src/org/openpatch/scratch/internal/AnimatedGifEncoder.java @@ -5,7 +5,8 @@ import java.io.*; /** - * Class AnimatedGifEncoder - Encodes a GIF file consisting of one or more frames. + * Class AnimatedGifEncoder - Encodes a GIF file consisting of one or more + * frames. * *
  *  Example:
@@ -17,8 +18,10 @@
  *     e.finish();
  * 
* - * No copyright asserted on the source code of this class. May be used for any purpose, however, - * refer to the Unisys LZW patent for restrictions on use of the associated LZWEncoder class. Please + * No copyright asserted on the source code of this class. May be used for any + * purpose, however, + * refer to the Unisys LZW patent for restrictions on use of the associated + * LZWEncoder class. Please * forward any corrections to kweiner@fmsware.com. * * @author Kevin Weiner, FM Software @@ -67,7 +70,8 @@ public class AnimatedGifEncoder { protected int sample = 10; // default sample interval for quantizer /** - * Sets the delay time between each frame, or changes it for subsequent frames (applies to last + * Sets the delay time between each frame, or changes it for subsequent frames + * (applies to last * frame added). * * @param ms int delay time in milliseconds @@ -81,7 +85,8 @@ public OutputStream getOut() { } /** - * Sets the GIF frame disposal code for the last added frame and any subsequent frames. Default is + * Sets the GIF frame disposal code for the last added frame and any subsequent + * frames. Default is * 0 if no transparent color has been set, otherwise 2. * * @param code int disposal code. @@ -93,7 +98,8 @@ public void setDispose(int code) { } /** - * Sets the number of times the set of GIF frames should be played. Default is 1; 0 means play + * Sets the number of times the set of GIF frames should be played. Default is + * 1; 0 means play * indefinitely. Must be invoked before the first image is added. * * @param iter int number of iterations. @@ -106,9 +112,12 @@ public void setRepeat(int iter) { } /** - * Sets the transparent color for the last added frame and any subsequent frames. Since all colors - * are subject to modification in the quantization process, the color in the final palette for - * each frame closest to the given color becomes the transparent color for that frame. May be set + * Sets the transparent color for the last added frame and any subsequent + * frames. Since all colors + * are subject to modification in the quantization process, the color in the + * final palette for + * each frame closest to the given color becomes the transparent color for that + * frame. May be set * to null to indicate no transparent color. * * @param c Color to be treated as transparent on display. @@ -130,9 +139,12 @@ public boolean addFrame(int[] pixels, int width, int height) { } /** - * Adds next GIF frame. The frame is not written immediately, but is actually deferred until the - * next frame is received so that timing data can be inserted. Invoking finish() - * flushes all frames. If setSize was not invoked, the size of the first image is + * Adds next GIF frame. The frame is not written immediately, but is actually + * deferred until the + * next frame is received so that timing data can be inserted. Invoking + * finish() + * flushes all frames. If setSize was not invoked, the size of the + * first image is * used for all subsequent frames. * * @param im BufferedImage containing frame to write. @@ -174,11 +186,13 @@ public boolean addFrame(BufferedImage im) { } /** - * Flushes any pending data and closes output file. If writing to an OutputStream, the stream is + * Flushes any pending data and closes output file. If writing to an + * OutputStream, the stream is * not closed. */ public boolean finish() { - if (!started) return false; + if (!started) + return false; boolean ok = true; started = false; try { @@ -205,7 +219,8 @@ public boolean finish() { } /** - * Sets frame rate in frames per second. Equivalent to setDelay(1000/fps). + * Sets frame rate in frames per second. Equivalent to + * setDelay(1000/fps). * * @param fps float frame rate (frames per second) */ @@ -216,43 +231,53 @@ public void setFrameRate(float fps) { } /** - * Sets quality of color quantization (conversion of images to the maximum 256 colors allowed by - * the GIF specification). Lower values (minimum = 1) produce better colors, but slow processing - * significantly. 10 is the default, and produces good color mapping at reasonable speeds. Values + * Sets quality of color quantization (conversion of images to the maximum 256 + * colors allowed by + * the GIF specification). Lower values (minimum = 1) produce better colors, but + * slow processing + * significantly. 10 is the default, and produces good color mapping at + * reasonable speeds. Values * greater than 20 do not yield significant improvements in speed. * * @param quality int greater than 0. * @return */ public void setQuality(int quality) { - if (quality < 1) quality = 1; + if (quality < 1) + quality = 1; sample = quality; } /** - * Sets the GIF frame size. The default size is the size of the first frame added if this method + * Sets the GIF frame size. The default size is the size of the first frame + * added if this method * is not invoked. * * @param w int frame width. * @param h int frame width. */ public void setSize(int w, int h) { - if (started && !firstFrame) return; + if (started && !firstFrame) + return; width = w; height = h; - if (width < 1) width = 320; - if (height < 1) height = 240; + if (width < 1) + width = 320; + if (height < 1) + height = 240; sizeSet = true; } /** - * Initiates GIF file creation on the given stream. The stream is not closed automatically. + * Initiates GIF file creation on the given stream. The stream is not closed + * automatically. * * @param os OutputStream on which GIF images are written. * @return false if initial write failed. */ public boolean start(OutputStream os) { - if (os == null) return false; + if (os == null) + return false; boolean ok = true; closeStream = false; out = os; @@ -315,14 +340,15 @@ protected void analyzePixels() { /** Returns index of palette color closest to c */ protected int findClosest(Color c) { - if (colorTab == null) return -1; + if (colorTab == null) + return -1; int r = (int) c.getRed(); int g = (int) c.getGreen(); int b = (int) c.getBlue(); int minpos = 0; int dmin = 256 * 256 * 256; int len = colorTab.length; - for (int i = 0; i < len; ) { + for (int i = 0; i < len;) { int dr = r - (colorTab[i++] & 0xff); int dg = g - (colorTab[i++] & 0xff); int db = b - (colorTab[i] & 0xff); @@ -620,7 +646,8 @@ public NeuQuant(byte[] thepic, int len, int sample) { public byte[] colorMap() { byte[] map = new byte[3 * netsize]; int[] index = new int[netsize]; - for (int i = 0; i < netsize; i++) index[network[i][3]] = i; + for (int i = 0; i < netsize; i++) + index[network[i][3]] = i; int k = 0; for (int i = 0; i < netsize; i++) { int j = index[i]; @@ -678,13 +705,15 @@ public void inxbuild() { /* smallval entry is now in position i */ if (smallval != previouscol) { netindex[previouscol] = (startpos + i) >> 1; - for (j = previouscol + 1; j < smallval; j++) netindex[j] = i; + for (j = previouscol + 1; j < smallval; j++) + netindex[j] = i; previouscol = smallval; startpos = i; } } netindex[previouscol] = (startpos + maxnetpos) >> 1; - for (j = previouscol + 1; j < 256; j++) netindex[j] = maxnetpos; /* really 256 */ + for (j = previouscol + 1; j < 256; j++) + netindex[j] = maxnetpos; /* really 256 */ } /* @@ -697,7 +726,8 @@ public void learn() { byte[] p; int pix, lim; - if (lengthcount < minpicturebytes) samplefac = 1; + if (lengthcount < minpicturebytes) + samplefac = 1; alphadec = 30 + ((samplefac - 1) / 3); p = thepicture; pix = 0; @@ -708,18 +738,25 @@ public void learn() { radius = initradius; rad = radius >> radiusbiasshift; - if (rad <= 1) rad = 0; - for (i = 0; i < rad; i++) radpower[i] = alpha * (((rad * rad - i * i) * radbias) / (rad * rad)); + if (rad <= 1) + rad = 0; + for (i = 0; i < rad; i++) + radpower[i] = alpha * (((rad * rad - i * i) * radbias) / (rad * rad)); // fprintf(stderr,"beginning 1D learning: initial radius=%d\n", rad); - if (lengthcount < minpicturebytes) step = 3; - else if ((lengthcount % prime1) != 0) step = 3 * prime1; + if (lengthcount < minpicturebytes) + step = 3; + else if ((lengthcount % prime1) != 0) + step = 3 * prime1; else { - if ((lengthcount % prime2) != 0) step = 3 * prime2; + if ((lengthcount % prime2) != 0) + step = 3 * prime2; else { - if ((lengthcount % prime3) != 0) step = 3 * prime3; - else step = 3 * prime4; + if ((lengthcount % prime3) != 0) + step = 3 * prime3; + else + step = 3 * prime4; } } @@ -731,18 +768,22 @@ public void learn() { j = contest(b, g, r); altersingle(alpha, j, b, g, r); - if (rad != 0) alterneigh(rad, j, b, g, r); /* alter neighbours */ + if (rad != 0) + alterneigh(rad, j, b, g, r); /* alter neighbours */ pix += step; - if (pix >= lim) pix -= lengthcount; + if (pix >= lim) + pix -= lengthcount; i++; - if (delta == 0) delta = 1; + if (delta == 0) + delta = 1; if (i % delta == 0) { alpha -= alpha / alphadec; radius -= radius / radiusdec; rad = radius >> radiusbiasshift; - if (rad <= 1) rad = 0; + if (rad <= 1) + rad = 0; for (j = 0; j < rad; j++) radpower[j] = alpha * (((rad * rad - j * j) * radbias) / (rad * rad)); } @@ -771,16 +812,20 @@ public int map(int b, int g, int r) { if (i < netsize) { p = network[i]; dist = p[1] - g; /* inx key */ - if (dist >= bestd) i = netsize; /* stop iter */ + if (dist >= bestd) + i = netsize; /* stop iter */ else { i++; - if (dist < 0) dist = -dist; + if (dist < 0) + dist = -dist; a = p[0] - b; - if (a < 0) a = -a; + if (a < 0) + a = -a; dist += a; if (dist < bestd) { a = p[2] - r; - if (a < 0) a = -a; + if (a < 0) + a = -a; dist += a; if (dist < bestd) { bestd = dist; @@ -792,16 +837,20 @@ public int map(int b, int g, int r) { if (j >= 0) { p = network[j]; dist = g - p[1]; /* inx key - reverse dif */ - if (dist >= bestd) j = -1; /* stop iter */ + if (dist >= bestd) + j = -1; /* stop iter */ else { j--; - if (dist < 0) dist = -dist; + if (dist < 0) + dist = -dist; a = p[0] - b; - if (a < 0) a = -a; + if (a < 0) + a = -a; dist += a; if (dist < bestd) { a = p[2] - r; - if (a < 0) a = -a; + if (a < 0) + a = -a; dist += a; if (dist < bestd) { bestd = dist; @@ -851,9 +900,11 @@ protected void alterneigh(int rad, int i, int b, int g, int r) { int[] p; lo = i - rad; - if (lo < -1) lo = -1; + if (lo < -1) + lo = -1; hi = i + rad; - if (hi > netsize) hi = netsize; + if (hi > netsize) + hi = netsize; j = i + 1; k = i - 1; @@ -916,12 +967,15 @@ protected int contest(int b, int g, int r) { for (i = 0; i < netsize; i++) { n = network[i]; dist = n[0] - b; - if (dist < 0) dist = -dist; + if (dist < 0) + dist = -dist; a = n[1] - g; - if (a < 0) a = -a; + if (a < 0) + a = -a; dist += a; a = n[2] - r; - if (a < 0) a = -a; + if (a < 0) + a = -a; dist += a; if (dist < bestd) { bestd = dist; @@ -1040,8 +1094,8 @@ class LZWEncoder { int cur_bits = 0; int masks[] = { - 0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, 0x01FF, 0x03FF, 0x07FF, - 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF + 0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, 0x01FF, 0x03FF, 0x07FF, + 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF }; // Number of characters so far in this 'packet' @@ -1062,7 +1116,8 @@ class LZWEncoder { // characters, flush the packet to disk. void char_out(byte c, OutputStream outs) throws IOException { accum[a_count++] = c; - if (a_count >= 254) flush_char(outs); + if (a_count >= 254) + flush_char(outs); } // Clear out the hash table @@ -1078,7 +1133,8 @@ void cl_block(OutputStream outs) throws IOException { // reset code table void cl_hash(int hsize) { - for (int i = 0; i < hsize; ++i) htab[i] = -1; + for (int i = 0; i < hsize; ++i) + htab[i] = -1; } void compress(int init_bits, OutputStream outs) throws IOException { @@ -1107,7 +1163,8 @@ void compress(int init_bits, OutputStream outs) throws IOException { ent = nextPixel(); hshift = 0; - for (fcode = hsize; fcode < 65536; fcode *= 2) ++hshift; + for (fcode = hsize; fcode < 65536; fcode *= 2) + ++hshift; hshift = 8 - hshift; // set hash code range bound hsize_reg = hsize; @@ -1115,8 +1172,7 @@ void compress(int init_bits, OutputStream outs) throws IOException { output(ClearCode, outs); - outer_loop: - while ((c = nextPixel()) != EOF) { + outer_loop: while ((c = nextPixel()) != EOF) { fcode = (c << maxbits) + ent; i = (c << hshift) ^ ent; // xor hashing @@ -1126,9 +1182,11 @@ void compress(int init_bits, OutputStream outs) throws IOException { } else if (htab[i] >= 0) // non-empty slot { disp = hsize_reg - i; // secondary hash (after G. Knott) - if (i == 0) disp = 1; + if (i == 0) + disp = 1; do { - if ((i -= disp) < 0) i += hsize_reg; + if ((i -= disp) < 0) + i += hsize_reg; if (htab[i] == fcode) { ent = codetab[i]; @@ -1141,7 +1199,8 @@ void compress(int init_bits, OutputStream outs) throws IOException { if (free_ent < maxmaxcode) { codetab[i] = free_ent++; // code -> hashtable htab[i] = fcode; - } else cl_block(outs); + } else + cl_block(outs); } // Put out the final code. output(ent, outs); @@ -1169,7 +1228,7 @@ void flush_char(OutputStream outs) throws IOException { } } - final int MAXCODE(int n_bits) { + int MAXCODE(int n_bits) { return (1 << n_bits) - 1; } @@ -1177,7 +1236,8 @@ final int MAXCODE(int n_bits) { // Return the next pixel from the image // ---------------------------------------------------------------------------- private int nextPixel() { - if (remaining == 0) return EOF; + if (remaining == 0) + return EOF; --remaining; @@ -1189,8 +1249,10 @@ private int nextPixel() { void output(int code, OutputStream outs) throws IOException { cur_accum &= masks[cur_bits]; - if (cur_bits > 0) cur_accum |= (code << cur_bits); - else cur_accum = code; + if (cur_bits > 0) + cur_accum |= (code << cur_bits); + else + cur_accum = code; cur_bits += n_bits; @@ -1208,8 +1270,10 @@ void output(int code, OutputStream outs) throws IOException { clear_flg = false; } else { ++n_bits; - if (n_bits == maxbits) maxcode = maxmaxcode; - else maxcode = MAXCODE(n_bits); + if (n_bits == maxbits) + maxcode = maxmaxcode; + else + maxcode = MAXCODE(n_bits); } } diff --git a/src/org/openpatch/scratch/internal/Applet.java b/src/org/openpatch/scratch/internal/Applet.java index 4321e515..18a7bffe 100644 --- a/src/org/openpatch/scratch/internal/Applet.java +++ b/src/org/openpatch/scratch/internal/Applet.java @@ -37,13 +37,13 @@ private class StageBox { public Stage stage; public String name; - public StageBox(final String name, final Stage stage) { + public StageBox(String name, final Stage stage) { this.name = name; this.stage = stage; } } - public Applet(final int width, final int height, final String assets) { + public Applet(int width, final int height, final String assets) { this.INITIAL_HEIGHT = height; this.INITIAL_WIDTH = width; this.assets = assets; @@ -62,7 +62,7 @@ public static Applet getInstance() { return instance; } - public void setDebug(final boolean debug) { + public void setDebug(boolean debug) { this.debug = debug; } @@ -78,8 +78,8 @@ public int getHeight() { return this.height; } - public void addStage(final String name, final Stage stage) { - for (final StageBox s : this.stages) { + public void addStage(String name, final Stage stage) { + for (StageBox s : this.stages) { if (s.name.equals(name)) { return; } @@ -92,8 +92,8 @@ public void addStage(final String name, final Stage stage) { } } - public Stage getStage(final String name) { - for (final StageBox s : this.stages) { + public Stage getStage(String name) { + for (StageBox s : this.stages) { if (s.name.equals(name)) { return s.stage; } @@ -101,13 +101,13 @@ public Stage getStage(final String name) { return null; } - public void removeStage(final String name) { + public void removeStage(String name) { this.stages.removeIf(sb -> sb.name.equals(name)); } - public void switchStage(final String name) { + public void switchStage(String name) { for (int i = 0; i < this.stages.size(); i++) { - final StageBox stageBox = this.stages.get(i); + StageBox stageBox = this.stages.get(i); if (stageBox.name.equals(name)) { this.currentStage = i; return; @@ -156,13 +156,13 @@ public void setup() { this.imageMode(PConstants.CENTER); this.rectMode(PConstants.CENTER); this.loading = this.loadImage("loading.png"); - final var loadingScaleX = this.INITIAL_WIDTH / 480.0; - final var loadingScaleY = this.INITIAL_HEIGHT / (360.0 + 150); // normal height + padding for loading text - final var scale = Math.min(1, Math.min(loadingScaleX, loadingScaleY)); - this.loading.resize((int) (this.loading.width * scale), (int)(this.loading.height * scale)); + var loadingScaleX = this.INITIAL_WIDTH / 480.0; + var loadingScaleY = this.INITIAL_HEIGHT / (360.0 + 150); // normal height + padding for loading text + var scale = Math.min(1, Math.min(loadingScaleX, loadingScaleY)); + this.loading.resize((int) (this.loading.width * scale), (int) (this.loading.height * scale)); } - private void setLoadingText(final String type, final String path) { + private void setLoadingText(String type, final String path) { this.loadingText = "Loading " + type + ": "; if (path.length() > 40) { this.loadingText += "..." + path.substring(path.length() - 40); @@ -176,22 +176,22 @@ public void loadAssets() { if (this.assets != null) { try { this.loadingText = "Finding files..."; - final var p =Path.of(ClassLoader.getSystemResource(this.assets).toURI()); - final var imageFiles = Files.find( + var p = Path.of(ClassLoader.getSystemResource(this.assets).toURI()); + var imageFiles = Files.find( p, Integer.MAX_VALUE, (filePath, fileAttr) -> fileAttr.isRegularFile()) .map(f -> f.toString()) .filter(f -> f.endsWith(".png") || f.endsWith(".jpg") || f.endsWith(".jpeg")) .collect(Collectors.toList()); - final var soundFiles = Files.find( + var soundFiles = Files.find( p, Integer.MAX_VALUE, (filePath, fileAttr) -> fileAttr.isRegularFile()) .map(f -> f.toString()) .filter(f -> f.endsWith(".mp3") || f.endsWith(".wav")) .collect(Collectors.toList()); - final var fontFiles = Files.find( + var fontFiles = Files.find( p, Integer.MAX_VALUE, (filePath, fileAttr) -> fileAttr.isRegularFile()) @@ -201,22 +201,22 @@ public void loadAssets() { this.numberAssets += imageFiles.size(); this.numberAssets += soundFiles.size(); this.numberAssets += fontFiles.size(); - for (final var file : imageFiles) { + for (var file : imageFiles) { this.setLoadingText("Image", file); Image.loadImage(file); this.loadedAssets += 1; } - for (final var file : fontFiles) { + for (var file : fontFiles) { this.setLoadingText("Font", file); Font.loadFont(file); this.loadedAssets += 1; } - for (final var file : soundFiles) { + for (var file : soundFiles) { this.setLoadingText("Sound", file); new SoundFile(this, file, true); this.loadedAssets += 1; } - } catch (final IOException | URISyntaxException e) { + } catch (IOException | URISyntaxException e) { } } } @@ -228,23 +228,23 @@ private float loadingStatus() { public void pre() { if (this.loadingStatus() == 1 && this.stages.size() > 0) { try { - final StageBox box = this.stages.get(this.currentStage); + StageBox box = this.stages.get(this.currentStage); box.stage.pre(); - } catch (final ArrayIndexOutOfBoundsException e) { + } catch (ArrayIndexOutOfBoundsException e) { } } } - public void mouseEvent(final MouseEvent e) { + public void mouseEvent(MouseEvent e) { if (this.loadingStatus() == 1 && this.stages.size() > 0) { - final StageBox box = this.stages.get(this.currentStage); + StageBox box = this.stages.get(this.currentStage); box.stage.mouseEvent(e); } } - public void keyEvent(final KeyEvent e) { + public void keyEvent(KeyEvent e) { if (this.loadingStatus() == 1 && this.stages.size() > 0) { - final StageBox box = this.stages.get(this.currentStage); + StageBox box = this.stages.get(this.currentStage); box.stage.keyEvent(e); } if (e.getKeyCode() == KeyCode.VK_F11) { @@ -253,7 +253,7 @@ public void keyEvent(final KeyEvent e) { } public void draw() { - final int sizeStages = this.stages.size(); + int sizeStages = this.stages.size(); if (this.loadingStatus() < 1) { this.background(0x222222); this.image(this.loading, this.width / 2, this.height / 2); @@ -262,10 +262,10 @@ public void draw() { this.textSize(20); this.textSize(14); this.text(this.loadingText, this.width / 2, this.height / 2 + - this.loading.height / 2 + 20); + this.loading.height / 2 + 20); this.textSize(20); this.text(round(this.loadingStatus() * 100) + "%", this.width / 2, this.height / 2 + - this.loading.height / 2 + 40); + this.loading.height / 2 + 40); this.textSize(14); } else if (sizeStages > 0) { if (this.currentStage > sizeStages - 1) { @@ -274,9 +274,9 @@ public void draw() { this.currentStage = 0; } try { - final StageBox box = this.stages.get(this.currentStage); + StageBox box = this.stages.get(this.currentStage); box.stage.draw(); - } catch (final ArrayIndexOutOfBoundsException e) { + } catch (ArrayIndexOutOfBoundsException e) { } } }