diff --git a/src/org/openpatch/scratch/Stage.java b/src/org/openpatch/scratch/Stage.java index 29964db..a9518ff 100644 --- a/src/org/openpatch/scratch/Stage.java +++ b/src/org/openpatch/scratch/Stage.java @@ -14,7 +14,6 @@ import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.CopyOnWriteArrayList; import java.util.stream.Collectors; - import org.openpatch.scratch.extensions.camera.Camera; import org.openpatch.scratch.extensions.color.Color; import org.openpatch.scratch.extensions.hitbox.Hitbox; @@ -29,17 +28,14 @@ import org.openpatch.scratch.internal.Image; import org.openpatch.scratch.internal.Sound; import org.openpatch.scratch.internal.Stamp; - import processing.core.PConstants; import processing.core.PGraphics; import processing.event.KeyEvent; import processing.event.MouseEvent; /** - * The Stage class represents a stage where various elements such as sprites, - * texts, pens, and - * backdrops can be added and manipulated. It provides methods to manage these - * elements, handle + * The Stage class represents a stage where various elements such as sprites, texts, pens, and + * backdrops can be added and manipulated. It provides methods to manage these elements, handle * events, and control the stage's appearance and behavior. */ public class Stage { @@ -91,8 +87,7 @@ public class Stage { private Camera camera; /** - * Constructs a new Stage with default dimensions. The default width is 480 - * pixels and the default + * Constructs a new Stage with default dimensions. The default width is 480 pixels and the default * height is 360 pixels. */ public Stage() { @@ -102,7 +97,7 @@ public Stage() { /** * Constructs a new Stage with the specified width and height. * - * @param width the width of the stage + * @param width the width of the stage * @param height the height of the stage */ public Stage(int width, final int height) { @@ -112,7 +107,7 @@ public Stage(int width, final int height) { /** * Constructs a new Stage with the specified width, height, and assets path. * - * @param width the width of the stage + * @param width the width of the stage * @param height the height of the stage * @param assets the path to the assets directory */ @@ -123,8 +118,7 @@ public Stage(int width, int height, String assets) { /** * Constructs a new Stage. * - * @param fullScreen a boolean indicating whether the stage should be in full - * screen mode. + * @param fullScreen a boolean indicating whether the stage should be in full screen mode. */ public Stage(boolean fullScreen) { this(480, 360, fullScreen, null); @@ -133,9 +127,8 @@ public Stage(boolean fullScreen) { /** * Constructs a new Stage with the specified fullscreen mode and assets path. * - * @param fullScreen a boolean indicating whether the stage should be in - * fullscreen mode - * @param assets the path to the assets directory + * @param fullScreen a boolean indicating whether the stage should be in fullscreen mode + * @param assets the path to the assets directory */ public Stage(boolean fullScreen, String assets) { this(480, 360, fullScreen, assets); @@ -144,10 +137,10 @@ public Stage(boolean fullScreen, String assets) { /** * Constructs a new Stage with the specified parameters. * - * @param width the width of the stage - * @param height the height of the stage + * @param width the width of the stage + * @param height the height of the stage * @param fullScreen whether the stage should be in full screen mode - * @param assets the path to the assets directory + * @param assets the path to the assets directory */ public Stage(int width, final int height, boolean fullScreen, String assets) { this.cursor = null; @@ -171,9 +164,12 @@ public Stage(int width, final int height, boolean fullScreen, String assets) { } Applet applet = Applet.getInstance(); this.mainBuffer = applet.createGraphics(applet.width, applet.height, applet.sketchRenderer()); - this.backdropBuffer = applet.createGraphics(applet.width, applet.height, applet.sketchRenderer()); - this.backgroundBuffer = applet.createGraphics(applet.width, applet.height, applet.sketchRenderer()); - this.foregroundBuffer = applet.createGraphics(applet.width, applet.height, applet.sketchRenderer()); + this.backdropBuffer = + applet.createGraphics(applet.width, applet.height, applet.sketchRenderer()); + this.backgroundBuffer = + applet.createGraphics(applet.width, applet.height, applet.sketchRenderer()); + this.foregroundBuffer = + applet.createGraphics(applet.width, applet.height, applet.sketchRenderer()); this.uiBuffer = applet.createGraphics(applet.width, applet.height, applet.sketchRenderer()); this.debugBuffer = applet.createGraphics(applet.width, applet.height, applet.sketchRenderer()); /** @@ -186,7 +182,8 @@ public Stage(int width, final int height, boolean fullScreen, String assets) { this.uiBuffer.smooth(4); this.debugBuffer.smooth(4); this.timer.put("default", new Timer()); - this.display = new Text(null, -applet.width / 2, -applet.height / 2, applet.width, TextStyle.BOX); + this.display = + new Text(null, -applet.width / 2, -applet.height / 2, applet.width, TextStyle.BOX); this.display.addedToStage(this); var p = new Polygon(); @@ -221,9 +218,8 @@ public Stage(int width, final int height, boolean fullScreen, String assets) { /** * Enables or disables the debug mode for the application. * - * @param debug a boolean value where {@code true} enables debug mode and - * {@code false} disables - * it. + * @param debug a boolean value where {@code true} enables debug mode and {@code false} disables + * it. */ public void setDebug(boolean debug) { Applet.getInstance().setDebug(debug); @@ -269,8 +265,7 @@ public void add(Pen pen) { } /** - * Adds a new shader to the sprite. If a shader with the received name already - * exists, this method + * Adds a new shader to the sprite. If a shader with the received name already exists, this method * does nothing. * * @param name @@ -321,8 +316,7 @@ public void resetShader() { * Retrieves a shader by name. * * @param name the name of a shader - * @return the shader with the specified name, or null if no shader with that - * name exists + * @return the shader with the specified name, or null if no shader with that name exists */ public Shader getShader(String name) { for (Shader shader : this.shaders) { @@ -344,8 +338,7 @@ public void nextShader() { * @return the name of the current shader, or null if no shaders exist */ public String getCurrentShaderName() { - if (this.shaders.size() == 0 || this.currentShader == -1) - return null; + if (this.shaders.size() == 0 || this.currentShader == -1) return null; return this.shaders.get(this.currentShader).getName(); } @@ -365,51 +358,41 @@ public int getCurrentShaderIndex() { * @return the current shader, or null if no shaders exist */ public Shader getCurrentShader() { - if (this.shaders.size() == 0 || this.currentShader == -1) - return null; + if (this.shaders.size() == 0 || this.currentShader == -1) return null; return this.shaders.get(this.currentShader); } /** - * Moves the specified sprite backwards by a given number of layers in the - * sprite list. If the - * resulting position is less than zero, the sprite is moved to the first - * position. If the - * resulting position is greater than the last index, the sprite is moved to the - * last position. + * Moves the specified sprite backwards by a given number of layers in the sprite list. If the + * resulting position is less than zero, the sprite is moved to the first position. If the + * resulting position is greater than the last index, the sprite is moved to the last position. * * @param sprite the sprite to be moved backwards in the layer order * @param number the number of layers to move the sprite backwards */ public void goLayersBackwards(Sprite sprite, int number) { int index = this.sprites.indexOf(sprite); - if (index == -1) - return; + if (index == -1) return; int newIndex = index - number; - if (newIndex < 0) - newIndex = 0; + if (newIndex < 0) newIndex = 0; newIndex = Math.min(newIndex, this.sprites.size() - 1); this.sprites.remove(index); this.sprites.add(newIndex, sprite); } /** - * Moves the specified sprite forward by a given number of layers in the sprite - * list. If the - * resulting position is out of bounds, it will be adjusted to the nearest valid - * position. + * Moves the specified sprite forward by a given number of layers in the sprite list. If the + * resulting position is out of bounds, it will be adjusted to the nearest valid position. * * @param sprite the sprite to be moved forward in the layer order * @param number the number of layers to move the sprite forward */ public void goLayersForwards(Sprite sprite, int number) { int index = this.sprites.indexOf(sprite); - if (index == -1) - return; + if (index == -1) return; int newIndex = index + number; - if (newIndex < 0) - newIndex = 0; + if (newIndex < 0) newIndex = 0; newIndex = Math.min(newIndex, this.sprites.size() - 1); this.sprites.remove(index); this.sprites.add(newIndex, sprite); @@ -436,8 +419,7 @@ public void goToBackLayer(Sprite sprite) { } /** - * Moves the specified sprite to the UI layer by removing it from the current - * list of sprites. + * Moves the specified sprite to the UI layer by removing it from the current list of sprites. * * @param sprite the sprite to be moved to the UI layer */ @@ -446,10 +428,10 @@ public void goToUILayer(Sprite sprite) { } /** - * Sets a custom sorter for the sprites. Use enableYSort() to enable the sorting - * of sprites using the y-coordinates. This overwrites goToBackLayer(), - * goToFrontLayer(), goLayersBackwards() and goLayersForwards(). - * + * Sets a custom sorter for the sprites. Use enableYSort() to enable the sorting of sprites using + * the y-coordinates. This overwrites goToBackLayer(), goToFrontLayer(), goLayersBackwards() and + * goLayersForwards(). + * * @see #enableYSort() * @param sorter the comparator used to sort the sprites */ @@ -458,26 +440,24 @@ public void setSorter(Comparator sorter) { } /** - * Enables the sorting of sprites using y-sorting. This means that sprites with - * a lower y-coordinate will be drawn on top of sprites with a higher - * y-coordinate. This sorting respects the height of the sprites. This - * overwrites goToBackLayer(), goToFrontLayer(), goLayersBackwards() and - * goLayersForwards(). + * Enables the sorting of sprites using y-sorting. This means that sprites with a lower + * y-coordinate will be drawn on top of sprites with a higher y-coordinate. This sorting respects + * the height of the sprites. This overwrites goToBackLayer(), goToFrontLayer(), + * goLayersBackwards() and goLayersForwards(). */ public void enableYSort() { - this.sorter = (s1, s2) -> (int) ((s2.getY() - s2.getHeight() / 2) - (s1.getY() - s1.getHeight() / 2)); + this.sorter = + (s1, s2) -> (int) ((s2.getY() - s2.getHeight() / 2) - (s1.getY() - s1.getHeight() / 2)); } - /** - * Disables the sorting of sprites. - */ + /** Disables the sorting of sprites. */ public void disableSort() { this.sorter = null; } /** * Checks if the sorting of sprites is enabled. - * + * * @return true if sorting is enabled, false otherwise */ public boolean isSortEnabled() { @@ -670,12 +650,11 @@ public long countPensOf(Class c) { } /** - * Add a backdrop to the stage. If a backdrop with the received name already - * exists do nothing. + * Add a backdrop to the stage. If a backdrop with the received name already exists do nothing. * - * @param name a unique name + * @param name a unique name * @param imagePath a image path - * @param stretch stretch image to window size + * @param stretch stretch image to window size */ public void addBackdrop(String name, final String imagePath, boolean stretch) { for (Image backdrop : this.backdrops) { @@ -691,10 +670,9 @@ public void addBackdrop(String name, final String imagePath, boolean stretch) { } /** - * Add a backdrop to the stage. If a backdrop with the received name already - * exists do nothing. + * Add a backdrop to the stage. If a backdrop with the received name already exists do nothing. * - * @param name a unique name + * @param name a unique name * @param imagePath a image path */ public void addBackdrop(String name, final String imagePath) { @@ -740,14 +718,12 @@ private void emitBackdropSwitch() { } /** - * This method is called when the backdrop switches to the specified name. - * Override this method to + * This method is called when the backdrop switches to the specified name. Override this method to * add custom behavior. * * @param name the name of the backdrop to switch to */ - public void whenBackdropSwitches(String name) { - } + public void whenBackdropSwitches(String name) {} /** Switch to the next backdrop. */ public void nextBackdrop() { @@ -819,18 +795,16 @@ public int[] getPixels() { } /** - * This method marks the UI buffer to be erased, which will be processed in the - * next update cycle. + * This method marks the UI buffer to be erased, which will be processed in the next update cycle. */ public void eraseUI() { this.eraseUIBuffer = true; } /** - * Add a sound to the stage. If a sound with the received name already exists do - * nothing. + * Add a sound to the stage. If a sound with the received name already exists do nothing. * - * @param name a unique name + * @param name a unique name * @param soundPath a sound path */ public void addSound(String name, final String soundPath) { @@ -956,8 +930,7 @@ public void changeColor(double h) { * @param b a blue value [0...255] */ public void setTint(double r, double g, double b) { - if (this.backdrops.size() == 0) - return; + if (this.backdrops.size() == 0) return; this.backdrops.get(this.currentBackdrop).setTint(r, g, b); } @@ -967,8 +940,7 @@ public void setTint(double r, double g, double b) { * @see Image#setTint(double) */ public void setTint(double h) { - if (this.backdrops.size() == 0) - return; + if (this.backdrops.size() == 0) return; this.backdrops.get(this.currentBackdrop).setTint(h); } @@ -979,8 +951,7 @@ public void setTint(double h) { * @param step a step value */ public void changeTint(double step) { - if (this.backdrops.size() == 0) - return; + if (this.backdrops.size() == 0) return; this.backdrops.get(this.currentBackdrop).changeTint(step); } @@ -1002,15 +973,13 @@ public void setTransparency(double transparency) { * @param step a step value */ public void changeTransparency(double step) { - if (this.backdrops.size() == 0) - return; + if (this.backdrops.size() == 0) return; this.backdrops.get(this.currentBackdrop).changeTransparency(step); } /** - * Return the width of the current costume or the pen size, when no costume is - * available. + * Return the width of the current costume or the pen size, when no costume is available. * * @return the width of the sprite */ @@ -1019,8 +988,7 @@ public int getWidth() { } /** - * Return the height of the current costume or the pen size, when no costume is - * available. + * Return the height of the current costume or the pen size, when no costume is available. * * @return the height of the sprite */ @@ -1053,8 +1021,7 @@ public Timer getTimer(String name) { * @param name the name of the timer */ public void addTimer(String name) { - if ("default".equals(name)) - return; + if ("default".equals(name)) return; this.timer.put(name, new Timer()); } @@ -1065,8 +1032,7 @@ public void addTimer(String name) { * @param name the name of the timer */ public void removeTimer(String name) { - if ("default".equals(name)) - return; + if ("default".equals(name)) return; this.timer.remove(name); } @@ -1107,27 +1073,21 @@ public void mouseEvent(MouseEvent e) { } /** - * This method is called when a mouse click event occurs. Overwrite this method - * to add custom + * This method is called when a mouse click event occurs. Overwrite this method to add custom * behavior. * * @param mouseEvent The mouse event that triggered this method. */ - public void whenMouseClicked(MouseCode mouseEvent) { - } + public void whenMouseClicked(MouseCode mouseEvent) {} /** - * This method is called when the mouse wheel is moved. Overwrite this method to - * add custom + * This method is called when the mouse wheel is moved. Overwrite this method to add custom * behavior. * - * @param steps the number of steps the mouse wheel has moved. Positive values - * indicate movement - * away from the user, while negative values indicate movement - * towards the user. + * @param steps the number of steps the mouse wheel has moved. Positive values indicate movement + * away from the user, while negative values indicate movement towards the user. */ - public void whenMouseWheelMoved(int steps) { - } + public void whenMouseWheelMoved(int steps) {} /** * Returns the current x-position of the mouse cursor @@ -1161,22 +1121,18 @@ public boolean isMouseDown() { } /** - * This method is called when a key is pressed. Override this method to add - * custom behavior. + * This method is called when a key is pressed. Override this method to add custom behavior. * * @param keyCode the code of the key that was pressed */ - public void whenKeyPressed(int keyCode) { - } + public void whenKeyPressed(int keyCode) {} /** - * This method is called when a key is released. Override this method to add - * custom behavior. + * This method is called when a key is released. Override this method to add custom behavior. * * @param keyCode the code of the key that was released */ - public void whenKeyReleased(int keyCode) { - } + public void whenKeyReleased(int keyCode) {} public void keyEvent(KeyEvent e) { switch (e.getAction()) { @@ -1311,7 +1267,7 @@ public int getDaysSince2000() { * Returns a random integer between the specified range (inclusive). * * @param from the lower bound of the range (inclusive) - * @param to the upper bound of the range (inclusive) + * @param to the upper bound of the range (inclusive) * @return a random integer between {@code from} and {@code to} (inclusive) */ public int pickRandom(int from, final int to) { @@ -1333,17 +1289,15 @@ public void display(String text) { /** * Displays the given text on the screen for a specified duration. * - * @param text The text to be displayed. - * @param millis The duration in milliseconds for which the text will be - * displayed. + * @param text The text to be displayed. + * @param millis The duration in milliseconds for which the text will be displayed. */ public void display(String text, final int millis) { this.display.showText(text, millis); } /** - * Broadcasts a message to all sprites in the stage. Each sprite will execute - * its `whenIReceive` + * Broadcasts a message to all sprites in the stage. Each sprite will execute its `whenIReceive` * method with the given message. * * @param message The message to broadcast to all sprites. @@ -1353,8 +1307,7 @@ public void broadcast(String message) { } /** - * Broadcasts a message to all sprites in the stage. Each sprite will execute - * its `whenIReceive` + * Broadcasts a message to all sprites in the stage. Each sprite will execute its `whenIReceive` * method with the given message. * * @param message The message to broadcast to all sprites. @@ -1364,22 +1317,19 @@ public void broadcast(Object message) { } /** - * This method is called when a specific message is received. Override this - * method to add custom + * This method is called when a specific message is received. Override this method to add custom * behavior. * * @param message The message that triggers this method. */ - public void whenIReceive(String message) { - } + public void whenIReceive(String message) {} /** * This method is called when a message is received. * * @param message The message object that is received. */ - public void whenIReceive(Object message) { - } + public void whenIReceive(Object message) {} /** * Sets the cursor image for the stage. @@ -1396,8 +1346,8 @@ public void setCursor(String path) { * Sets the cursor image and its active spot coordinates. * * @param path the file path to the cursor image - * @param x the x-coordinate of the cursor's active spot - * @param y the y-coordinate of the cursor's active spot + * @param x the x-coordinate of the cursor's active spot + * @param y the y-coordinate of the cursor's active spot */ public void setCursor(String path, int x, int y) { this.cursor = path; @@ -1431,17 +1381,14 @@ public double getFrameRate() { } /** - * Executes the main logic of the stage. This method should be overridden by - * subclasses to define + * Executes the main logic of the stage. This method should be overridden by subclasses to define * the specific behavior of the stage. */ - public void run() { - } + public void run() {} public void pre() { Applet applet = Applet.getInstance(); - if (applet == null) - return; + if (applet == null) return; var globalMouseX = applet.mouseX - this.getWidth() / 2; var globalMouseY = -(applet.mouseY - this.getHeight() / 2); this.mouseX = this.getCamera().toLocalX(globalMouseX); @@ -1458,8 +1405,7 @@ public void exit() { public void draw() { Applet applet = Applet.getInstance(); - if (applet == null) - return; + if (applet == null) return; var shader = this.getCurrentShader(); if (shader != null) {