Class Stage

java.lang.Object
org.openpatch.scratch.Stage

public class Stage extends Object
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.
  • Field Details

    • backgroundStamps

      public Queue<Stamp> backgroundStamps
    • foregroundStamps

      public Queue<Stamp> foregroundStamps
    • uiStamps

      public Queue<Stamp> uiStamps
  • Constructor Details

    • Stage

      public Stage()
      Constructs a new Stage with default dimensions. The default width is 480 pixels and the default height is 360 pixels.
    • Stage

      public Stage(int width, int height)
      Constructs a new Stage with the specified width and height.
      Parameters:
      width - the width of the stage
      height - the height of the stage
    • Stage

      public Stage(int width, int height, String assets)
      Constructs a new Stage with the specified width, height, and assets path.
      Parameters:
      width - the width of the stage
      height - the height of the stage
      assets - the path to the assets directory
    • Stage

      public Stage(boolean fullScreen)
      Constructs a new Stage.
      Parameters:
      fullScreen - a boolean indicating whether the stage should be in full screen mode.
    • Stage

      public Stage(boolean fullScreen, String assets)
      Constructs a new Stage with the specified fullscreen mode and assets path.
      Parameters:
      fullScreen - a boolean indicating whether the stage should be in fullscreen mode
      assets - the path to the assets directory
    • Stage

      public Stage(int width, int height, boolean fullScreen, String assets)
      Constructs a new Stage with the specified parameters.
      Parameters:
      width - the width of the stage
      height - the height of the stage
      fullScreen - whether the stage should be in full screen mode
      assets - the path to the assets directory
  • Method Details

    • setDebug

      public void setDebug(boolean debug)
      Enables or disables the debug mode for the application.
      Parameters:
      debug - a boolean value where true enables debug mode and false disables it.
    • isDebug

      public boolean isDebug()
      Checks if the application is in debug mode.
      Returns:
      true if the application is in debug mode, false otherwise.
    • add

      public void add(Sprite sprite)
      Add a sprite object to the stage
      Parameters:
      sprite - a sprite
    • add

      public void add(Text text)
      Add a text object to the stage
      Parameters:
      text - a text
    • add

      public void add(Pen pen)
      Adds a Pen object to the stage.
      Parameters:
      pen - the Pen object to be added to the stage
    • addShader

      public Shader addShader(String name, String shaderPath)
      Adds a new shader to the sprite. If a shader with the received name already exists, this method does nothing.
      Parameters:
      name -
      shaderPath -
      Returns:
      the shader
    • switchShader

      public void switchShader(String name)
      Switch to a shader by name.
      Parameters:
      name - the name of a shader
    • switchShader

      public void switchShader(double index)
      Switch to a shader by index.
      Parameters:
      index - the index of a shader
    • resetShader

      public void resetShader()
    • getShader

      public Shader getShader(String name)
      Retrieves a shader by name.
      Parameters:
      name - the name of a shader
      Returns:
      the shader with the specified name, or null if no shader with that name exists
    • nextShader

      public void nextShader()
      Sets the next shader as the current shader.
    • getCurrentShaderName

      public String getCurrentShaderName()
      Retrieves the name of the current shader.
      Returns:
      the name of the current shader, or null if no shaders exist
    • getCurrentShaderIndex

      public int getCurrentShaderIndex()
      Retrieves the index of the current shader.
      Returns:
      the index of the current shader
    • getCurrentShader

      public Shader getCurrentShader()
      Retrieves the current shader.
      Returns:
      the current shader, or null if no shaders exist
    • goLayersBackwards

      public void goLayersBackwards(Sprite sprite, int number)
      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.
      Parameters:
      sprite - the sprite to be moved backwards in the layer order
      number - the number of layers to move the sprite backwards
    • goLayersForwards

      public void goLayersForwards(Sprite sprite, int number)
      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.
      Parameters:
      sprite - the sprite to be moved forward in the layer order
      number - the number of layers to move the sprite forward
    • goToFrontLayer

      public void goToFrontLayer(Sprite sprite)
      Moves the specified sprite to the front layer.
      Parameters:
      sprite - the sprite to be moved to the front layer
    • goToBackLayer

      public void goToBackLayer(Sprite sprite)
      Moves the specified sprite to the back layer of the stage.
      Parameters:
      sprite - the sprite to be moved to the back layer
    • goToUILayer

      public void goToUILayer(Sprite sprite)
      Moves the specified sprite to the UI layer by removing it from the current list of sprites.
      Parameters:
      sprite - the sprite to be moved to the UI layer
    • setSorter

      public void setSorter(Comparator<? super Sprite> sorter)
      Sets the sorter for the sprites.
      Parameters:
      sorter - the comparator used to sort the sprites
    • getAll

      public List<Sprite> getAll()
      Retrieves a list of all sprites in the current stage.
      Returns:
      a list containing all sp
    • remove

      public void remove(Sprite sprite)
      Removes the specified sprite from the stage.
      Parameters:
      sprite - the sprite to be removed
    • remove

      public void remove(Pen pen)
      Removes the specified pen from the stage.
      Parameters:
      pen - the pen to be removed
    • remove

      public void remove(Text text)
      Removes the specified text from the stage.
      Parameters:
      text - the text to be removed
    • removeAll

      public void removeAll()
      Removes all elements from the stage.
    • removeAllSprites

      public void removeAllSprites()
      Removes all sprites from the stage.
    • removeAllTexts

      public void removeAllTexts()
      Removes all texts from the stage.
    • removeAllPens

      public void removeAllPens()
      Removes all pens from the stage.
    • remove

      public void remove(Class<? extends Sprite> c)
      Removes all sprites of the specified class from the stage.
      Parameters:
      c - the class of the sprites to remove
    • find

      public <T extends Sprite> List<T> find(Class<T> c)
      Find sprites of a given class.
      Parameters:
      c - Class
    • findSpritesOf

      public <T extends Sprite> List<T> findSpritesOf(Class<T> c)
      Find sprites of a given class.
      Parameters:
      c - Class
    • findTextsOf

      public <T extends Text> List<T> findTextsOf(Class<T> c)
      Find texts of a given class.
      Parameters:
      c - Class
    • findPensOf

      public <T extends Pen> List<T> findPensOf(Class<T> c)
      Find texts of a given class.
      Parameters:
      c - Class
    • count

      public <T extends Sprite> long count(Class<T> c)
      Returns the number of sprites of the specified class.
      Parameters:
      c - the class of the sprites to count
      Returns:
      the number of sprites of the specified class
    • countSprites

      public long countSprites()
      Returns the number of sprites in the stage.
      Returns:
      the number of sprites
    • countSpritesOf

      public <T extends Sprite> long countSpritesOf(Class<T> c)
      Returns the number of sprites of the specified class.
      Parameters:
      c - the class of the sprites to count
      Returns:
      the number of sprites of the specified class
    • countTexts

      public long countTexts()
      Returns the number of texts in the stage.
      Returns:
      the number of texts
    • countTextsOf

      public <T extends Text> long countTextsOf(Class<T> c)
      Returns the number of texts of the specified class.
      Parameters:
      c - the class of the texts to count
    • countPens

      public long countPens()
      Returns the number of pens
      Returns:
      the number of pens
    • countPensOf

      public <T extends Pen> long countPensOf(Class<T> c)
      Returns the number of pens of the specified class.
      Parameters:
      c - the class of the pens to count
    • addBackdrop

      public void addBackdrop(String name, String imagePath, boolean stretch)
      Add a backdrop to the stage. If a backdrop with the received name already exists do nothing.
      Parameters:
      name - a unique name
      imagePath - a image path
      stretch - stretch image to window size
    • addBackdrop

      public void addBackdrop(String name, String imagePath)
      Add a backdrop to the stage. If a backdrop with the received name already exists do nothing.
      Parameters:
      name - a unique name
      imagePath - a image path
    • removeBackdrop

      public void removeBackdrop(String name)
      Remove a backdrop from the stage.
      Parameters:
      name - of the backdrop
    • switchBackdrop

      public void switchBackdrop(String name)
      Switch to a backdrop by name.
      Parameters:
      name - the name of a backdrop
    • whenBackdropSwitches

      public void whenBackdropSwitches(String name)
      This method is called when the backdrop switches to the specified name. Override this method to add custom behavior.
      Parameters:
      name - the name of the backdrop to switch to
    • nextBackdrop

      public void nextBackdrop()
      Switch to the next backdrop.
    • previousBackdrop

      public void previousBackdrop()
      Switch to the previous backdrop.
    • randomBackdrop

      public void randomBackdrop()
      Switch to a random backdrop.
    • getCurrentBackdropName

      public String getCurrentBackdropName()
      Returns the current backdrop name
      Returns:
      a backdrop name
    • getCurrentBackdropIndex

      public int getCurrentBackdropIndex()
      Returns the current backdrop index
      Returns:
      a backdrop index
    • eraseAll

      public void eraseAll()
      Erases all lines on the pen layer.
    • eraseBackground

      public void eraseBackground()
      When this method is called, the background buffer will be erased.
    • eraseForeground

      public void eraseForeground()
      When this method is called, the foreground buffer will be erased.
    • eraseUI

      public void eraseUI()
      This method marks the UI buffer to be erased, which will be processed in the next update cycle.
    • addSound

      public void addSound(String name, String soundPath)
      Add a sound to the stage. If a sound with the received name already exists do nothing.
      Parameters:
      name - a unique name
      soundPath - a sound path
    • removeSound

      public void removeSound(String name)
      Remove a sound from the stage.
      Parameters:
      name - the sound name
    • playSound

      public void playSound(String name)
      Plays a sound.
      Parameters:
      name - the sound name
    • stopAllSounds

      public void stopAllSounds()
      Stops the playing of all sounds of the stage.
    • stopSound

      public void stopSound(String name)
      Stops the playing of the sound with the given name
      Parameters:
      name - Name of the sound
    • isSoundPlaying

      public boolean isSoundPlaying(String name)
      Returns true if the sound if playing
      Returns:
      playing
    • getBackgroundBuffer

      public processing.core.PGraphics getBackgroundBuffer()
      Returns the pen buffer
      Returns:
      the pen buffer
    • getForegroundBuffer

      public processing.core.PGraphics getForegroundBuffer()
      Returns the foreground buffer.
      Returns:
      the PGraphics object representing the foreground buffer.
    • getUIBuffer

      public processing.core.PGraphics getUIBuffer()
      Returns the PGraphics object representing the UI buffer.
      Returns:
      the PGraphics object used for the UI buffer.
    • getDebugBuffer

      public processing.core.PGraphics getDebugBuffer()
      Returns the debug buffer.
      Returns:
      the PGraphics object representing the debug buffer.
    • setColor

      public void setColor(double h)
      Sets the background color via a hue value
      Parameters:
      h - a hue value [0...255]
    • setColor

      public void setColor(double r, double g, double b)
      Sets the background color via a rgb value
      Parameters:
      r - a red value [0...255]
      g - a green value [0...255]
      b - a blue value [0...255]
    • setColor

      public void setColor(Color c)
      Sets the color of the stage.
      Parameters:
      c - the new color to be set
      See Also:
    • changeColor

      public void changeColor(double h)
      Changes the background color by adding a step to the hue value.
      Parameters:
      h - a step value
    • setTint

      public void setTint(double r, double g, double b)
      Sets the tint for the current backdrop with rgb.
      Parameters:
      r - a red value [0...255]
      g - a green value [0...255]
      b - a blue value [0...255]
      See Also:
    • setTint

      public void setTint(double h)
      Sets the tint for the current backdrop with a hue.
      See Also:
    • changeTint

      public void changeTint(double step)
      Changes the tint for the current backdrop
      Parameters:
      step - a step value
      See Also:
    • setTransparency

      public void setTransparency(double transparency)
      Sets the transparency of the current backdrop.
      Parameters:
      transparency - a transparency value [0...1]
      See Also:
    • changeTransparency

      public void changeTransparency(double step)
      Changes the transparency for the current costume.
      Parameters:
      step - a step value
      See Also:
    • getWidth

      public int getWidth()
      Return the width of the current costume or the pen size, when no costume is available.
      Returns:
      the width of the sprite
    • getHeight

      public int getHeight()
      Return the height of the current costume or the pen size, when no costume is available.
      Returns:
      the height of the sprite
    • getTimer

      public Timer getTimer()
      Returns the timer
      Returns:
      the timer
    • getTimer

      public Timer getTimer(String name)
      Returns a timer by name
      Parameters:
      name - a name
      Returns:
      the timer
    • addTimer

      public void addTimer(String name)
      Add a new timer by name. Overwriting default is not permitted.
      Parameters:
      name - the name of the timer
    • removeTimer

      public void removeTimer(String name)
      Remove a timer by name. Removing of default is not permitted.
      Parameters:
      name - the name of the timer
    • mouseEvent

      public void mouseEvent(processing.event.MouseEvent e)
    • whenMouseClicked

      public void whenMouseClicked(MouseCode mouseEvent)
      This method is called when a mouse click event occurs. Overwrite this method to add custom behavior.
      Parameters:
      mouseEvent - The mouse event that triggered this method.
    • whenMouseWheelMoved

      public void whenMouseWheelMoved(int steps)
      This method is called when the mouse wheel is moved. Overwrite this method to add custom behavior.
      Parameters:
      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.
    • getMouseX

      public double getMouseX()
      Returns the current x-position of the mouse cursor
      Returns:
      x-position
    • getMouseY

      public double getMouseY()
      Returns the current y-position of the mouse cursor
      Returns:
      y-position
    • getMouse

      public Vector2 getMouse()
    • isMouseDown

      public boolean isMouseDown()
      Returns true is the mouse button is down
      Returns:
      mouse button down
    • whenKeyPressed

      public void whenKeyPressed(int keyCode)
      This method is called when a key is pressed. Override this method to add custom behavior.
      Parameters:
      keyCode - the code of the key that was pressed
    • whenKeyReleased

      public void whenKeyReleased(int keyCode)
      This method is called when a key is released. Override this method to add custom behavior.
      Parameters:
      keyCode - the code of the key that was released
    • keyEvent

      public void keyEvent(processing.event.KeyEvent e)
    • isKeyPressed

      public boolean isKeyPressed(int keyCode)
      Returns true if the key is pressed
      Parameters:
      keyCode - a key code
      Returns:
      key pressed
    • getDeltaTime

      public double getDeltaTime()
      Gets the seconds passed since the last frame.
      Returns:
      secons since last frame
    • getCurrentYear

      public int getCurrentYear()
      Returns the current year
      Returns:
      current year
    • getCurrentMonth

      public int getCurrentMonth()
      Returns the current month
      Returns:
      current month
    • getCurrentDayOfWeek

      public int getCurrentDayOfWeek()
      Returns the current day of the week
      Returns:
      current day of the week
    • getCurrentDay

      public int getCurrentDay()
      Returns the current day of the month
      Returns:
      current day of the month
    • getCurrentHour

      public int getCurrentHour()
      Returns the current hour
      Returns:
      current hour
    • getCurrentMinute

      public int getCurrentMinute()
      Returns the current minute
      Returns:
      current minute
    • getCurrentSecond

      public int getCurrentSecond()
      Returns the current second
      Returns:
      current second
    • getCurrentMillisecond

      public int getCurrentMillisecond()
      Returns the current millisecond
      Returns:
      current millisecond
    • getDaysSince2000

      public int getDaysSince2000()
      Returns the days since 2010/01/01
      Returns:
      days since 2010/01/01
    • pickRandom

      public int pickRandom(int from, int to)
      Returns a random integer between the specified range (inclusive).
      Parameters:
      from - the lower bound of the range (inclusive)
      to - the upper bound of the range (inclusive)
      Returns:
      a random integer between from and to (inclusive)
    • display

      public void display(String text)
      Displays the given text on the stage.
      Parameters:
      text - the text to be displayed
    • display

      public void display(String text, int millis)
      Displays the given text on the screen for a specified duration.
      Parameters:
      text - The text to be displayed.
      millis - The duration in milliseconds for which the text will be displayed.
    • broadcast

      public void broadcast(String message)
      Broadcasts a message to all sprites in the stage. Each sprite will execute its `whenIReceive` method with the given message.
      Parameters:
      message - The message to broadcast to all sprites.
    • broadcast

      public void broadcast(Object message)
      Broadcasts a message to all sprites in the stage. Each sprite will execute its `whenIReceive` method with the given message.
      Parameters:
      message - The message to broadcast to all sprites.
    • whenIReceive

      public void whenIReceive(String message)
      This method is called when a specific message is received. Override this method to add custom behavior.
      Parameters:
      message - The message that triggers this method.
    • whenIReceive

      public void whenIReceive(Object message)
      This method is called when a message is received.
      Parameters:
      message - The message object that is received.
    • setCursor

      public void setCursor(String path)
      Sets the cursor image for the stage.
      Parameters:
      path - the file path to the cursor image
    • setCursor

      public void setCursor(String path, int x, int y)
      Sets the cursor image and its active spot coordinates.
      Parameters:
      path - the file path to the cursor image
      x - the x-coordinate of the cursor's active spot
      y - the y-coordinate of the cursor's active spot
    • getCamera

      public Camera getCamera()
      Retrieves the current camera instance associated with this stage.
      Returns:
      the current Camera object.
    • wait

      public void wait(int millis)
      Stop the execution of the whole applications for the given milliseconds.
      Parameters:
      millis - Milliseconds
    • run

      public void run()
      Executes the main logic of the stage. This method should be overridden by subclasses to define the specific behavior of the stage.
    • pre

      public void pre()
    • exit

      public void exit()
      Close the window and therefore the whole application.
    • draw

      public void draw()