Package org.openpatch.scratch
Class Stage
java.lang.Object
org.openpatch.scratch.Stage
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 Summary
Modifier and TypeFieldDescription -
Constructor Summary
ConstructorDescriptionStage()
Constructs a new Stage with default dimensions.Stage
(boolean fullScreen) Constructs a new Stage.Constructs a new Stage with the specified fullscreen mode and assets path.Stage
(int width, int height) Constructs a new Stage with the specified width and height.Constructs a new Stage with the specified parameters.Constructs a new Stage with the specified width, height, and assets path. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds a Pen object to the stage.void
Add a text object to the stagevoid
Add a sprite object to the stagevoid
addBackdrop
(String name, String imagePath) Add a backdrop to the stage.void
addBackdrop
(String name, String imagePath, boolean stretch) Add a backdrop to the stage.Adds a new shader to the sprite.void
Add a sound to the stage.void
Add a new timer by name.void
Broadcasts a message to all sprites in the stage.void
Broadcasts a message to all sprites in the stage.void
changeColor
(double h) Changes the background color by adding a step to the hue value.void
changeTint
(double step) Changes the tint for the current backdropvoid
changeTransparency
(double step) Changes the transparency for the current costume.<T extends Sprite>
longReturns the number of sprites of the specified class.long
Returns the number of pens<T extends Pen>
longcountPensOf
(Class<T> c) Returns the number of pens of the specified class.long
Returns the number of sprites in the stage.<T extends Sprite>
longcountSpritesOf
(Class<T> c) Returns the number of sprites of the specified class.long
Returns the number of texts in the stage.<T extends Text>
longcountTextsOf
(Class<T> c) Returns the number of texts of the specified class.void
Displays the given text on the stage.void
Displays the given text on the screen for a specified duration.void
draw()
void
eraseAll()
Erases all lines on the pen layer.void
When this method is called, the background buffer will be erased.void
When this method is called, the foreground buffer will be erased.void
eraseUI()
This method marks the UI buffer to be erased, which will be processed in the next update cycle.void
exit()
Close the window and therefore the whole application.Find sprites of a given class.findPensOf
(Class<T> c) Find texts of a given class.findSpritesOf
(Class<T> c) Find sprites of a given class.findTextsOf
(Class<T> c) Find texts of a given class.getAll()
Retrieves a list of all sprites in the current stage.processing.core.PGraphics
Returns the pen bufferRetrieves the current camera instance associated with this stage.int
Returns the current backdrop indexReturns the current backdrop nameint
Returns the current day of the monthint
Returns the current day of the weekint
Returns the current hourint
Returns the current millisecondint
Returns the current minuteint
Returns the current monthint
Returns the current secondRetrieves the current shader.int
Retrieves the index of the current shader.Retrieves the name of the current shader.int
Returns the current yearint
Returns the days since 2010/01/01processing.core.PGraphics
Returns the debug buffer.double
Gets the seconds passed since the last frame.processing.core.PGraphics
Returns the foreground buffer.int
Return the height of the current costume or the pen size, when no costume is available.getMouse()
double
Returns the current x-position of the mouse cursordouble
Returns the current y-position of the mouse cursorRetrieves a shader by name.getTimer()
Returns the timerReturns a timer by nameprocessing.core.PGraphics
Returns the PGraphics object representing the UI buffer.int
getWidth()
Return the width of the current costume or the pen size, when no costume is available.void
goLayersBackwards
(Sprite sprite, int number) Moves the specified sprite backwards by a given number of layers in the sprite list.void
goLayersForwards
(Sprite sprite, int number) Moves the specified sprite forward by a given number of layers in the sprite list.void
goToBackLayer
(Sprite sprite) Moves the specified sprite to the back layer of the stage.void
goToFrontLayer
(Sprite sprite) Moves the specified sprite to the front layer.void
goToUILayer
(Sprite sprite) Moves the specified sprite to the UI layer by removing it from the current list of sprites.boolean
isDebug()
Checks if the application is in debug mode.boolean
isKeyPressed
(int keyCode) Returns true if the key is pressedboolean
Returns true is the mouse button is downboolean
isSoundPlaying
(String name) Returns true if the sound if playingvoid
keyEvent
(processing.event.KeyEvent e) void
mouseEvent
(processing.event.MouseEvent e) void
Switch to the next backdrop.void
Sets the next shader as the current shader.int
pickRandom
(int from, int to) Returns a random integer between the specified range (inclusive).void
Plays a sound.void
pre()
void
Switch to the previous backdrop.void
Switch to a random backdrop.void
Removes all sprites of the specified class from the stage.void
Removes the specified pen from the stage.void
Removes the specified text from the stage.void
Removes the specified sprite from the stage.void
Removes all elements from the stage.void
Removes all pens from the stage.void
Removes all sprites from the stage.void
Removes all texts from the stage.void
removeBackdrop
(String name) Remove a backdrop from the stage.void
removeSound
(String name) Remove a sound from the stage.void
removeTimer
(String name) Remove a timer by name.void
void
run()
Executes the main logic of the stage.void
setColor
(double h) Sets the background color via a hue valuevoid
setColor
(double r, double g, double b) Sets the background color via a rgb valuevoid
Sets the color of the stage.void
Sets the cursor image for the stage.void
Sets the cursor image and its active spot coordinates.void
setDebug
(boolean debug) Enables or disables the debug mode for the application.void
setSorter
(Comparator<? super Sprite> sorter) Sets the sorter for the sprites.void
setTint
(double h) Sets the tint for the current backdrop with a hue.void
setTint
(double r, double g, double b) Sets the tint for the current backdrop with rgb.void
setTransparency
(double transparency) Sets the transparency of the current backdrop.void
Stops the playing of all sounds of the stage.void
Stops the playing of the sound with the given namevoid
switchBackdrop
(String name) Switch to a backdrop by name.void
switchShader
(double index) Switch to a shader by index.void
switchShader
(String name) Switch to a shader by name.void
wait
(int millis) Stop the execution of the whole applications for the given milliseconds.void
whenBackdropSwitches
(String name) This method is called when the backdrop switches to the specified name.void
whenIReceive
(Object message) This method is called when a message is received.void
whenIReceive
(String message) This method is called when a specific message is received.void
whenKeyPressed
(int keyCode) This method is called when a key is pressed.void
whenKeyReleased
(int keyCode) This method is called when a key is released.void
whenMouseClicked
(MouseCode mouseEvent) This method is called when a mouse click event occurs.void
whenMouseWheelMoved
(int steps) This method is called when the mouse wheel is moved.
-
Field Details
-
backgroundStamps
-
foregroundStamps
-
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 stageheight
- the height of the stage
-
Stage
Constructs a new Stage with the specified width, height, and assets path.- Parameters:
width
- the width of the stageheight
- the height of the stageassets
- 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
Constructs a new Stage with the specified fullscreen mode and assets path.- Parameters:
fullScreen
- a boolean indicating whether the stage should be in fullscreen modeassets
- the path to the assets directory
-
Stage
Constructs a new Stage with the specified parameters.- Parameters:
width
- the width of the stageheight
- the height of the stagefullScreen
- whether the stage should be in full screen modeassets
- 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 wheretrue
enables debug mode andfalse
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
Add a sprite object to the stage- Parameters:
sprite
- a sprite
-
add
Add a text object to the stage- Parameters:
text
- a text
-
add
Adds a Pen object to the stage.- Parameters:
pen
- the Pen object to be added to the stage
-
addShader
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
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
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
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
Retrieves the current shader.- Returns:
- the current shader, or null if no shaders exist
-
goLayersBackwards
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 ordernumber
- the number of layers to move the sprite backwards
-
goLayersForwards
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 ordernumber
- the number of layers to move the sprite forward
-
goToFrontLayer
Moves the specified sprite to the front layer.- Parameters:
sprite
- the sprite to be moved to the front layer
-
goToBackLayer
Moves the specified sprite to the back layer of the stage.- Parameters:
sprite
- the sprite to be moved to the back layer
-
goToUILayer
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
Sets the sorter for the sprites.- Parameters:
sorter
- the comparator used to sort the sprites
-
getAll
Retrieves a list of all sprites in the current stage.- Returns:
- a list containing all sp
-
remove
Removes the specified sprite from the stage.- Parameters:
sprite
- the sprite to be removed
-
remove
Removes the specified pen from the stage.- Parameters:
pen
- the pen to be removed
-
remove
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
Removes all sprites of the specified class from the stage.- Parameters:
c
- the class of the sprites to remove
-
find
Find sprites of a given class.- Parameters:
c
- Class
-
findSpritesOf
Find sprites of a given class.- Parameters:
c
- Class
-
findTextsOf
Find texts of a given class.- Parameters:
c
- Class
-
findPensOf
Find texts of a given class.- Parameters:
c
- Class
-
count
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
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
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
Returns the number of pens of the specified class.- Parameters:
c
- the class of the pens to count
-
addBackdrop
Add a backdrop to the stage. If a backdrop with the received name already exists do nothing.- Parameters:
name
- a unique nameimagePath
- a image pathstretch
- stretch image to window size
-
addBackdrop
Add a backdrop to the stage. If a backdrop with the received name already exists do nothing.- Parameters:
name
- a unique nameimagePath
- a image path
-
removeBackdrop
Remove a backdrop from the stage.- Parameters:
name
- of the backdrop
-
switchBackdrop
Switch to a backdrop by name.- Parameters:
name
- the name of a backdrop
-
whenBackdropSwitches
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
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
Add a sound to the stage. If a sound with the received name already exists do nothing.- Parameters:
name
- a unique namesoundPath
- a sound path
-
removeSound
Remove a sound from the stage.- Parameters:
name
- the sound name
-
playSound
Plays a sound.- Parameters:
name
- the sound name
-
stopAllSounds
public void stopAllSounds()Stops the playing of all sounds of the stage. -
stopSound
Stops the playing of the sound with the given name- Parameters:
name
- Name of the sound
-
isSoundPlaying
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
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
Returns the timer- Returns:
- the timer
-
getTimer
Returns a timer by name- Parameters:
name
- a name- Returns:
- the timer
-
addTimer
Add a new timer by name. Overwriting default is not permitted.- Parameters:
name
- the name of the timer
-
removeTimer
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
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
-
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
andto
(inclusive)
-
display
Displays the given text on the stage.- Parameters:
text
- the text to be displayed
-
display
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
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
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
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
This method is called when a message is received.- Parameters:
message
- The message object that is received.
-
setCursor
Sets the cursor image for the stage.- Parameters:
path
- the file path to the cursor image
-
setCursor
Sets the cursor image and its active spot coordinates.- Parameters:
path
- the file path to the cursor imagex
- the x-coordinate of the cursor's active spoty
- the y-coordinate of the cursor's active spot
-
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()
-