Package Green
Class Green
java.lang.Object
Green.Green
The main class of the library. This handles inputs, provides a few utility functions,
and provides a
A single instance of this class should be created in
World
-independent way to call handleAct()
and handleDraw()
.
A single instance of this class should be created in
PApplet.setup()
by supplying 'this
' as the parameter.- Author:
- Zacchary Dempsey-Plante
-
Field Summary
Modifier and TypeFieldDescriptionstatic int
Bilinear resizing algorithm.static int
Nearest-neighbour resizing algorithm.static int
Tile the image to fit (from top left), rather than adjusting it in any way. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic float
cot(float angle)
Calculates the cotangent ofangle
in radians.static float
csc(float angle)
Calculates the cosecant ofangle
in radians.float
Retrieves the deltaTime - the time since the last frame.static int
getDigits(int value)
Calculates the number of digits in a given integer.static Green
Retrieves the current instance of the library that is running.static boolean
getLinesIntersect(float a1x, float a1y, float a2x, float a2y, float b1x, float b1y, float b2x, float b2y)
Determines whether two line segments ([(a1x
,a1y
) - (a2x
,a2y
)] and [(b1x
,b1y
) - (b2x
,b2y
)]) intersect.int
Retrieves the amount that the mouse scrolled in this frame.float
Calculates the mouse's speed from last frame to this frame.Retrieves the parent instance of Processing.static float
getPointsAngle(float x1, float y1, float x2, float y2)
Calculates the angle between two points ((x1
,y1
) and (x2
,y2
)).static float
getPointsDist(float x1, float y1, float x2, float y2)
Calculates the distance between two points ((x1
,y1
) and (x2
,y2
)).static World
getWorld()
Retrieves the currently-loadedWorld
.void
void
Calls theActor.draw()
method on everyActor
, drawing all objects in the currently-loadedWorld
to screen.void
Handles all the frame-by-frame input.void
handleKeyDown(char key, int keyCode)
Handles the event of a key being pressed.void
handleKeyUp(char key, int keyCode)
Handles the event of a key being released.void
handleMouseDown(int mouseButton)
Handles the event of a mouse button being pressed.void
handleMousePosition(int mouseX, int mouseY)
Deprecated.void
handleMousePosition(int previousMouseX, int previousMouseY, int mouseX, int mouseY)
Handles the mouse position input.void
handleMouseUp(int mouseButton)
Handles the event of a mouse button being released.void
handleMouseWheel(int mouseScroll)
Handles when the mouse is scrolled.boolean
isKeyDown(char key)
Retrieves whether a specific key is currently down.boolean
isKeyDown(int keyCode)
Retrieves whether a specific key is currently down.boolean
Retrieves whether a specific key is currently down.boolean
isKeyDownThisFrame(char key)
Retrieves whether a specific key was pressed this frame.boolean
isKeyDownThisFrame(int keyCode)
Retrieves whether a specific key was pressed this frame.boolean
isKeyDownThisFrame(InputKey key)
Retrieves whether a specific key was pressed this frame.boolean
isKeyUpThisFrame(char key)
Retrieves whether a specific key was released this frame.boolean
isKeyUpThisFrame(int keyCode)
Retrieves whether a specific key was released this frame.boolean
isKeyUpThisFrame(InputKey key)
Retrieves whether a specific key was released this frame.boolean
isMouseButtonDown(int mouseButton)
Retrieves whether a specific mouse button is currently down.boolean
isMouseButtonDownThisFrame(int mouseButton)
Retrieves whether a specific mouse button was pressed this frame.boolean
isMouseButtonUpThisFrame(int mouseButton)
Retrieves whether a specific mouse button was released this frame.boolean
Retrieves whether the mouse has moved this frame.boolean
Retrieves whether the mouse is currently scrolling.void
Loads aWorld
to make it the one currently in use.static boolean
pointInRectangle(float pXx, float pXy, float pYx, float pYy, float pZx, float pZy, float pWx, float pWy, float pPx, float pPy)
Checks whether a point (pPx
,pPy
) is within the bounds of a rectangle ((pXx
,pXy
), (pYx
,pYy
), (pZx
,pZy
), (pWx
,pWy
)) where the points are provided in clockwise order.A nearest-neighbour resizing implementation.static float
sec(float angle)
Calculates the secant ofangle
in radians.Tiles a source image to fill a new image with different dimensions.
-
Field Details
-
BILINEAR
public static final int BILINEARBilinear resizing algorithm. This is great for upscaling pictures or other detailed images. This simply uses Processing's nativePImage.resize(int, int)
.- See Also:
- Constant Field Values
-
NEAREST_NEIGHBOR
public static final int NEAREST_NEIGHBORNearest-neighbour resizing algorithm. This is great for keeping pixel art looking crisp and sharp at higher resolutions. This uses the customresizeNN(PImage, int, int)
.- See Also:
- Constant Field Values
-
TILE
public static final int TILETile the image to fit (from top left), rather than adjusting it in any way. This uses the customtileImage(PImage, int, int)
.- See Also:
- Constant Field Values
-
-
Constructor Details
-
Green
Starts the backbone of the library.- Parameters:
theParent
- ThePApplet
running the library.- Throws:
SingleInstanceException
- Thrown when this constructor is called more than once.
-
-
Method Details
-
getInstance
Retrieves the current instance of the library that is running.- Returns:
- The current instance of the library that is running.
-
getWorld
Retrieves the currently-loadedWorld
.- Returns:
- The currently-loaded
World
.
-
getParent
Retrieves the parent instance of Processing.- Returns:
- The parent instance of Processing.
-
getDeltaTime
public float getDeltaTime()Retrieves the deltaTime - the time since the last frame.- Returns:
- The time, in seconds, since the last frame.
-
getPointsDist
public static float getPointsDist(float x1, float y1, float x2, float y2)Calculates the distance between two points ((x1
,y1
) and (x2
,y2
)).- Parameters:
x1
- The X-axis position of point 1.y1
- The Y-axis position of point 1.x2
- The X-axis position of point 2.y2
- The Y-axis position of point 2.- Returns:
- The distance between the two points.
-
getPointsAngle
public static float getPointsAngle(float x1, float y1, float x2, float y2)Calculates the angle between two points ((x1
,y1
) and (x2
,y2
)).- Parameters:
x1
- The X-axis position of point 1.y1
- The Y-axis position of point 1.x2
- The X-axis position of point 2.y2
- The Y-axis position of point 2.- Returns:
- The angle between the two points.
-
getLinesIntersect
public static boolean getLinesIntersect(float a1x, float a1y, float a2x, float a2y, float b1x, float b1y, float b2x, float b2y)Determines whether two line segments ([(a1x
,a1y
) - (a2x
,a2y
)] and [(b1x
,b1y
) - (b2x
,b2y
)]) intersect.- Parameters:
a1x
- The X-axis position of point 1 of line segment A.a1y
- The Y-axis position of point 1 of line segment A.a2x
- The X-axis position of point 2 of line segment A.a2y
- The Y-axis position of point 2 of line segment A.b1x
- The X-axis position of point 1 of line segment B.b1y
- The Y-axis position of point 1 of line segment B.b2x
- The X-axis position of point 2 of line segment B.b2y
- The Y-axis position of point 2 of line segment B.- Returns:
- Whether the two line segments intersect.
-
csc
public static float csc(float angle)Calculates the cosecant ofangle
in radians.- Parameters:
angle
- The angle in radians to calculate the cosecant of.- Returns:
- The cosecant of
angle
.
-
sec
public static float sec(float angle)Calculates the secant ofangle
in radians.- Parameters:
angle
- The angle in radians to calculate the secant of.- Returns:
- The secant of
angle
.
-
cot
public static float cot(float angle)Calculates the cotangent ofangle
in radians.- Parameters:
angle
- The angle in radians to calculate the cotangent of.- Returns:
- The cotangent of
angle
.
-
getDigits
public static int getDigits(int value)Calculates the number of digits in a given integer. Ifvalue
is0
,1
is returned. Ifvalue
is negative, the negative sign is ignored.- Parameters:
value
- The integer to measure the number of digits of.- Returns:
- The number of digits in
value
.
-
resizeNN
A nearest-neighbour resizing implementation. This is great for resizing pixel art while keeping it looking crisp and sharp.- Parameters:
src
- The original image to resize.w
- The width to resize to.h
- The height to resize to.- Returns:
- The resized image.
- See Also:
- https://gist.github.com/gncgnc/fc645b8db00ec43d43fecef37d58df73, https://gist.github.com/GoToLoop/2e12acf577506fd53267e1d186624d7c
-
tileImage
Tiles a source image to fill a new image with different dimensions. Primarily used for background images.- Parameters:
src
- The original image to tile.w
- The width to tile to.h
- The height to tile to.- Returns:
- The new image, composed of
src
tiled to fit.
-
pointInRectangle
public static boolean pointInRectangle(float pXx, float pXy, float pYx, float pYy, float pZx, float pZy, float pWx, float pWy, float pPx, float pPy)Checks whether a point (pPx
,pPy
) is within the bounds of a rectangle ((pXx
,pXy
), (pYx
,pYy
), (pZx
,pZy
), (pWx
,pWy
)) where the points are provided in clockwise order.- Parameters:
pXx
- The X-axis position of the X point.pXy
- The Y-axis position of the X point.pYx
- The X-axis position of the Y point.pYy
- The Y-axis position of the Y point.pZx
- The X-axis position of the Z point.pZy
- The Y-axis position of the Z point.pWx
- The X-axis position of the W point.pWy
- The Y-axis position of the W point.pPx
- The X-axis position of the point to check.pPy
- The Y-axis position of the point to check.- Returns:
- Whether the point to check is within the bounds of the provided rectangle.
- See Also:
- https://gamedev.stackexchange.com/a/110233
-
loadWorld
Loads aWorld
to make it the one currently in use. If there is a world already loaded, the new one will only be loaded at the end of the current frame.- Parameters:
world
- TheWorld
to load.
-
handleDraw
Calls theActor.draw()
method on everyActor
, drawing all objects in the currently-loadedWorld
to screen.- Throws:
NoWorldException
- Thrown when the method is called and there is noWorld
loaded.
-
handleAct
- Throws:
NoWorldException
- Thrown when the method is called and there is noWorld
loaded.
-
handleInput
public void handleInput()Handles all the frame-by-frame input. -
handleMouseDown
public void handleMouseDown(int mouseButton)Handles the event of a mouse button being pressed. Put this in thePApplet.mousePressed()
event.- Parameters:
mouseButton
- The mouse button that was pressed - eitherPConstants.LEFT
,PConstants.CENTER
, orPConstants.RIGHT
. Please pass inPApplet.mouseButton
in your code.
-
handleMouseUp
public void handleMouseUp(int mouseButton)Handles the event of a mouse button being released. Put this in thePApplet.mouseReleased()
event.- Parameters:
mouseButton
- The mouse button that was released - eitherPConstants.LEFT
,PConstants.CENTER
, orPConstants.RIGHT
. Please pass inPApplet.mouseButton
in your code.
-
handleMousePosition
public void handleMousePosition(int previousMouseX, int previousMouseY, int mouseX, int mouseY)Handles the mouse position input. Just put this in yourPApplet.draw()
method.- Parameters:
previousMouseX
- The mouse's previous X-axis position, in pixels. Please pass inPApplet.pmouseX
in your code.previousMouseY
- The mouse's previous Y-axis position, in pixels. Please pass inPApplet.pmouseY
in your code.mouseX
- The mouse's X-axis position, in pixels. Please pass inPApplet.mouseX
in your code.mouseY
- The mouse's Y-axis position, in pixels. Please pass inPApplet.mouseY
in your code.
-
handleMousePosition
Deprecated.Please usehandleMousePosition(int, int, int, int)
instead.Handles the mouse position input. Just put this in yourPApplet.draw()
method.- Parameters:
mouseX
- The mouse's X-axis position, in pixels. Please pass inPApplet.mouseX
in your code.mouseY
- The mouse's Y-axis position, in pixels. Please pass inPApplet.mouseY
in your code.
-
handleMouseWheel
public void handleMouseWheel(int mouseScroll)Handles when the mouse is scrolled. Put this in thePApplet.mouseWheel(processing.event.MouseEvent)
event.- Parameters:
mouseScroll
- The amount that the mouse has scrolled this frame. Please pass inMouseEvent.getCount()
.
-
isMouseMoving
public boolean isMouseMoving()Retrieves whether the mouse has moved this frame.- Returns:
- Whether the mouse has moved this frame.
-
getMouseSpeed
public float getMouseSpeed()Calculates the mouse's speed from last frame to this frame. This may not be very reliable, since it is calculated from frame-to-frame.- Returns:
- The mouse's speed from last frame to this frame, in pixels/second.
-
isMouseButtonDown
public boolean isMouseButtonDown(int mouseButton)Retrieves whether a specific mouse button is currently down.- Parameters:
mouseButton
- The mouse button to check for - eitherPConstants.LEFT
,PConstants.CENTER
, orPConstants.RIGHT
.- Returns:
- Whether
mouseButton
is down.
-
isMouseButtonDownThisFrame
public boolean isMouseButtonDownThisFrame(int mouseButton)Retrieves whether a specific mouse button was pressed this frame.- Parameters:
mouseButton
- The mouse button to check for - eitherPConstants.LEFT
,PConstants.CENTER
, orPConstants.RIGHT
.- Returns:
- Whether
mouseButton
was pressed in this frame.
-
isMouseButtonUpThisFrame
public boolean isMouseButtonUpThisFrame(int mouseButton)Retrieves whether a specific mouse button was released this frame.- Parameters:
mouseButton
- The mouse button to check for - eitherPConstants.LEFT
,PConstants.CENTER
, orPConstants.RIGHT
.- Returns:
- Whether
mouseButton
was released in this frame.
-
getMouseScroll
public int getMouseScroll()Retrieves the amount that the mouse scrolled in this frame.- Returns:
- The amount that the mouse scrolled in this frame.
-
isMouseScrolling
public boolean isMouseScrolling()Retrieves whether the mouse is currently scrolling.- Returns:
- Whether the mouse is currently scrolling.
-
handleKeyDown
public void handleKeyDown(char key, int keyCode)Handles the event of a key being pressed. Put this in thePApplet.keyPressed()
event.- Parameters:
key
- Thechar
representation of the key that was pressed. Please pass inPApplet.key
in your code.keyCode
- The key code of the key that was pressed. Please pass inPApplet.keyCode
in your code.- See Also:
- Java KeyEvent reference
-
handleKeyUp
public void handleKeyUp(char key, int keyCode)Handles the event of a key being released. Put this in thePApplet.keyReleased()
event.- Parameters:
key
- Thechar
representation of the key that was released. Please pass inPApplet.key
in your code.keyCode
- The key code of the key that was released. Please pass inPApplet.keyCode
in your code.- See Also:
- Java KeyEvent reference
-
isKeyDown
public boolean isKeyDown(char key)Retrieves whether a specific key is currently down.- Parameters:
key
- Thechar
representation of the key to check for.- Returns:
- Whether
key
is currently down.
-
isKeyDown
public boolean isKeyDown(int keyCode)Retrieves whether a specific key is currently down.- Parameters:
keyCode
- The key code of the key to check for.- Returns:
- Whether
key
is currently down. - See Also:
- Java KeyEvent reference
-
isKeyDown
Retrieves whether a specific key is currently down.- Parameters:
key
- The key to check for.- Returns:
- Whether
key
is currently down.
-
isKeyDownThisFrame
public boolean isKeyDownThisFrame(char key)Retrieves whether a specific key was pressed this frame.- Parameters:
key
- Thechar
representation of the key to check for.- Returns:
- Whether
key
was pressed in this frame.
-
isKeyDownThisFrame
public boolean isKeyDownThisFrame(int keyCode)Retrieves whether a specific key was pressed this frame.- Parameters:
keyCode
- The key code of the key to check for.- Returns:
- Whether
key
was pressed in this frame. - See Also:
- Java KeyEvent reference
-
isKeyDownThisFrame
Retrieves whether a specific key was pressed this frame.- Parameters:
key
- The key to check for.- Returns:
- Whether
key
was pressed in this frame.
-
isKeyUpThisFrame
public boolean isKeyUpThisFrame(char key)Retrieves whether a specific key was released this frame.- Parameters:
key
- Thechar
representation of the key to check for.- Returns:
- Whether
key
was released in this frame.
-
isKeyUpThisFrame
public boolean isKeyUpThisFrame(int keyCode)Retrieves whether a specific key was released this frame.- Parameters:
keyCode
- The key code of the key to check for.- Returns:
- Whether
key
was released in this frame. - See Also:
- Java KeyEvent reference
-
isKeyUpThisFrame
Retrieves whether a specific key was released this frame.- Parameters:
key
- The key to check for.- Returns:
- Whether
key
was released in this frame.
-
handleMousePosition(int, int, int, int)
instead.