Class Stage
- All Implemented Interfaces:
InputProcessor
,Disposable
actors
. Stage handles the viewport and distributes input events.
setViewport(Viewport)
controls the coordinates used within the stage and sets up the camera used to convert between
stage coordinates and screen coordinates.
A stage must receive input events so it can distribute them to actors. This is typically done by passing the stage to
Gdx.input.setInputProcessor
. An InputMultiplexer
may
be used to handle input events before or after the stage does. If an actor handles an event by returning true from the input
method, then the stage's input method will also return true, causing subsequent InputProcessors to not receive the event.
The Stage and its constituents (like Actors and Listeners) are not thread-safe and should only be updated and queried from a single thread (presumably the main render thread). Methods should be reentrant, so you can update Actors and Stages from within callbacks and handlers.
- Author:
- mzechner, Nathan Sweet
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final class
Internal class for managing touch focus. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
act()
Callsact(float)
withGraphics.getDeltaTime()
, limited to a minimum of 30fps.void
act
(float delta) Calls theActor.act(float)
method on each actor in the stage.void
Adds an action to the root of the stage.void
Adds an actor to the root of the stage.boolean
addCaptureListener
(EventListener listener) Adds a capture listener to the root.boolean
addListener
(EventListener listener) Adds a listener to the root.void
addTouchFocus
(EventListener listener, Actor listenerActor, Actor target, int pointer, int button) Adds the listener to be notified for all touchDragged and touchUp events for the specified pointer and button.void
calculateScissors
(Rectangle localRect, Rectangle scissorRect) Calculates window scissor coordinates from local coordinates using the batch's current transformation matrix.void
Removes all touch focus listeners, sending a touchUp event to each listener.void
cancelTouchFocus
(Actor listenerActor) Cancels touch focus for all listeners with the specified listener actor.void
cancelTouchFocusExcept
(EventListener exceptListener, Actor exceptActor) Cancels touch focus for all listeners except the specified listener.void
clear()
Removes the root's children, actions, and listeners.void
dispose()
Releases all resources of this object.void
draw()
boolean
Returns the root's child actors.getBatch()
The viewport's camera.The default color that can be used by actors to draw debug lines.float
The viewport's world height.Gets the actor that will receive key events.getRoot()
Returns the root group which holds all actors in the stage.Gets the actor that will receive scroll events.float
getWidth()
The viewport's world width.hit
(float stageX, float stageY, boolean touchable) Returns theActor
at the specified location in stage coordinates.boolean
boolean
keyDown
(int keyCode) Applies a key down event to the actor that haskeyboard focus
, if any, and returns true if the event washandled
.boolean
keyTyped
(char character) Applies a key typed event to the actor that haskeyboard focus
, if any, and returns true if the event washandled
.boolean
keyUp
(int keyCode) Applies a key up event to the actor that haskeyboard focus
, if any, and returns true if the event washandled
.boolean
mouseMoved
(int screenX, int screenY) Applies a mouse moved event to the stage and returns true if an actor in the scenehandled
the event.boolean
removeCaptureListener
(EventListener listener) Removes a listener from the root.boolean
removeListener
(EventListener listener) Removes a listener from the root.void
removeTouchFocus
(EventListener listener, Actor listenerActor, Actor target, int pointer, int button) Removes touch focus for the specified listener, pointer, and button.screenToStageCoordinates
(Vector2 screenCoords) Transforms the screen coordinates to stage coordinates.boolean
scrolled
(float amountX, float amountY) Applies a mouse scroll event to the stage and returns true if an actor in the scenehandled
the event.void
setActionsRequestRendering
(boolean actionsRequestRendering) If true, any actions executed during a call toact()
) will result in a call toGraphics.requestRendering()
.void
setDebugAll
(boolean debugAll) If true, debug lines are shown for all actors.void
setDebugInvisible
(boolean debugInvisible) If true, debug lines are shown for actors even whenActor.isVisible()
is false.void
setDebugParentUnderMouse
(boolean debugParentUnderMouse) If true, debug is enabled only for the parent of the actor under the mouse.void
setDebugTableUnderMouse
(boolean debugTableUnderMouse) If true, debug is enabled only for the first ascendant of the actor under the mouse that is a table.void
setDebugTableUnderMouse
(Table.Debug debugTableUnderMouse) If notTable.Debug.none
, debug is enabled only for the first ascendant of the actor under the mouse that is a table.void
setDebugUnderMouse
(boolean debugUnderMouse) If true, debug is enabled only for the actor under the mouse.boolean
setKeyboardFocus
(Actor actor) Sets the actor that will receive key events.void
Replaces the root group.boolean
setScrollFocus
(Actor actor) Sets the actor that will receive scroll events.void
setViewport
(Viewport viewport) stageToScreenCoordinates
(Vector2 stageCoords) Transforms the stage coordinates to screen coordinates.toScreenCoordinates
(Vector2 coords, Matrix4 transformMatrix) Transforms the coordinates to screen coordinates.boolean
touchCancelled
(int screenX, int screenY, int pointer, int button) Called when the touch gesture is cancelled.boolean
touchDown
(int screenX, int screenY, int pointer, int button) Applies a touch down event to the stage and returns true if an actor in the scenehandled
the event.boolean
touchDragged
(int screenX, int screenY, int pointer) Applies a touch moved event to the stage and returns true if an actor in the scenehandled
the event.boolean
touchUp
(int screenX, int screenY, int pointer, int button) Applies a touch up event to the stage and returns true if an actor in the scenehandled
the event.void
Removes the touch, keyboard, and scroll focus for the specified actor and any descendants.void
Removes the touch, keyboard, and scroll focused actors.
-
Constructor Details
-
Stage
Creates a stage with the specified viewport. The stage will use its ownBatch
which will be disposed when the stage is disposed. -
Stage
Creates a stage with the specified viewport and batch. This can be used to specify an existing batch or to customize which batch implementation is used.- Parameters:
batch
- Will not be disposed ifdispose()
is called, handle disposal yourself.
-
-
Method Details
-
draw
public void draw() -
act
public void act()Callsact(float)
withGraphics.getDeltaTime()
, limited to a minimum of 30fps. -
act
public void act(float delta) Calls theActor.act(float)
method on each actor in the stage. Typically called each frame. This method also fires enter and exit events.- Parameters:
delta
- Time in seconds since the last frame.
-
touchDown
public boolean touchDown(int screenX, int screenY, int pointer, int button) Applies a touch down event to the stage and returns true if an actor in the scenehandled
the event.- Specified by:
touchDown
in interfaceInputProcessor
- Overrides:
touchDown
in classInputAdapter
- Parameters:
screenX
- The x coordinate, origin is in the upper left cornerscreenY
- The y coordinate, origin is in the upper left cornerpointer
- the pointer for the event.button
- the button- Returns:
- whether the input was processed
-
touchDragged
public boolean touchDragged(int screenX, int screenY, int pointer) Applies a touch moved event to the stage and returns true if an actor in the scenehandled
the event. Onlylisteners
that returned true for touchDown will receive this event.- Specified by:
touchDragged
in interfaceInputProcessor
- Overrides:
touchDragged
in classInputAdapter
pointer
- the pointer for the event.- Returns:
- whether the input was processed
-
touchUp
public boolean touchUp(int screenX, int screenY, int pointer, int button) Applies a touch up event to the stage and returns true if an actor in the scenehandled
the event. Onlylisteners
that returned true for touchDown will receive this event.- Specified by:
touchUp
in interfaceInputProcessor
- Overrides:
touchUp
in classInputAdapter
pointer
- the pointer for the event.button
- the button- Returns:
- whether the input was processed
-
touchCancelled
public boolean touchCancelled(int screenX, int screenY, int pointer, int button) Description copied from interface:com.badlogic.gdx.InputProcessor
Called when the touch gesture is cancelled. Reason may be from OS interruption to touch becoming a large surface such as the user cheek). Relevant on Android and iOS only. The button parameter will beInput.Buttons.LEFT
on iOS.- Specified by:
touchCancelled
in interfaceInputProcessor
- Overrides:
touchCancelled
in classInputAdapter
pointer
- the pointer for the event.button
- the button- Returns:
- whether the input was processed
-
mouseMoved
public boolean mouseMoved(int screenX, int screenY) Applies a mouse moved event to the stage and returns true if an actor in the scenehandled
the event. This event only occurs on the desktop.- Specified by:
mouseMoved
in interfaceInputProcessor
- Overrides:
mouseMoved
in classInputAdapter
- Returns:
- whether the input was processed
-
scrolled
public boolean scrolled(float amountX, float amountY) Applies a mouse scroll event to the stage and returns true if an actor in the scenehandled
the event. This event only occurs on the desktop.- Specified by:
scrolled
in interfaceInputProcessor
- Overrides:
scrolled
in classInputAdapter
- Parameters:
amountX
- the horizontal scroll amount, negative or positive depending on the direction the wheel was scrolled.amountY
- the vertical scroll amount, negative or positive depending on the direction the wheel was scrolled.- Returns:
- whether the input was processed.
-
keyDown
public boolean keyDown(int keyCode) Applies a key down event to the actor that haskeyboard focus
, if any, and returns true if the event washandled
.- Specified by:
keyDown
in interfaceInputProcessor
- Overrides:
keyDown
in classInputAdapter
- Parameters:
keyCode
- one of the constants inInput.Keys
- Returns:
- whether the input was processed
-
keyUp
public boolean keyUp(int keyCode) Applies a key up event to the actor that haskeyboard focus
, if any, and returns true if the event washandled
.- Specified by:
keyUp
in interfaceInputProcessor
- Overrides:
keyUp
in classInputAdapter
- Parameters:
keyCode
- one of the constants inInput.Keys
- Returns:
- whether the input was processed
-
keyTyped
public boolean keyTyped(char character) Applies a key typed event to the actor that haskeyboard focus
, if any, and returns true if the event washandled
.- Specified by:
keyTyped
in interfaceInputProcessor
- Overrides:
keyTyped
in classInputAdapter
- Parameters:
character
- The character- Returns:
- whether the input was processed
-
addTouchFocus
public void addTouchFocus(EventListener listener, Actor listenerActor, Actor target, int pointer, int button) Adds the listener to be notified for all touchDragged and touchUp events for the specified pointer and button. Touch focus is added automatically when true is returned fromtouchDown
. The specified actors will be used as thelistener actor
andtarget
for the touchDragged and touchUp events. -
removeTouchFocus
public void removeTouchFocus(EventListener listener, Actor listenerActor, Actor target, int pointer, int button) Removes touch focus for the specified listener, pointer, and button. Note the listener will not receive a touchUp event when this method is used. -
cancelTouchFocus
Cancels touch focus for all listeners with the specified listener actor.- See Also:
-
cancelTouchFocus
public void cancelTouchFocus()Removes all touch focus listeners, sending a touchUp event to each listener. Listeners typically expect to receive a touchUp event when they have touch focus. The location of the touchUp isInteger.MIN_VALUE
. Listeners can useInputEvent.isTouchFocusCancel()
to ignore this event if needed. -
cancelTouchFocusExcept
Cancels touch focus for all listeners except the specified listener.- See Also:
-
addActor
Adds an actor to the root of the stage.- See Also:
-
addAction
Adds an action to the root of the stage.- See Also:
-
getActors
Returns the root's child actors.- See Also:
-
addListener
Adds a listener to the root.- See Also:
-
removeListener
Removes a listener from the root.- See Also:
-
addCaptureListener
Adds a capture listener to the root.- See Also:
-
removeCaptureListener
Removes a listener from the root. -
clear
public void clear()Removes the root's children, actions, and listeners. -
unfocusAll
public void unfocusAll()Removes the touch, keyboard, and scroll focused actors. -
unfocus
Removes the touch, keyboard, and scroll focus for the specified actor and any descendants. -
setKeyboardFocus
Sets the actor that will receive key events.- Parameters:
actor
- May be null.- Returns:
- true if the unfocus and focus events were not cancelled by a
FocusListener
.
-
getKeyboardFocus
Gets the actor that will receive key events.- Returns:
- May be null.
-
setScrollFocus
Sets the actor that will receive scroll events.- Parameters:
actor
- May be null.- Returns:
- true if the unfocus and focus events were not cancelled by a
FocusListener
.
-
getScrollFocus
Gets the actor that will receive scroll events.- Returns:
- May be null.
-
getBatch
-
getViewport
-
setViewport
-
getWidth
public float getWidth()The viewport's world width. -
getHeight
public float getHeight()The viewport's world height. -
getCamera
The viewport's camera. -
getRoot
Returns the root group which holds all actors in the stage. -
setRoot
Replaces the root group. This can be useful, for example, to subclass the root group to be notified byGroup.childrenChanged()
. -
hit
Returns theActor
at the specified location in stage coordinates. Hit testing is performed in the order the actors were inserted into the stage, last inserted actors being tested first. To get stage coordinates from screen coordinates, usescreenToStageCoordinates(Vector2)
.- Parameters:
touchable
- If true, the hit detection will respect thetouchability
.- Returns:
- May be null if no actor was hit.
-
screenToStageCoordinates
Transforms the screen coordinates to stage coordinates.- Parameters:
screenCoords
- Input screen coordinates and output for resulting stage coordinates.
-
stageToScreenCoordinates
Transforms the stage coordinates to screen coordinates.- Parameters:
stageCoords
- Input stage coordinates and output for resulting screen coordinates.
-
toScreenCoordinates
Transforms the coordinates to screen coordinates. The coordinates can be anywhere in the stage since the transform matrix describes how to convert them. The transform matrix is typically obtained fromBatch.getTransformMatrix()
duringActor.draw(Batch, float)
.- See Also:
-
calculateScissors
Calculates window scissor coordinates from local coordinates using the batch's current transformation matrix. -
setActionsRequestRendering
public void setActionsRequestRendering(boolean actionsRequestRendering) If true, any actions executed during a call toact()
) will result in a call toGraphics.requestRendering()
. Widgets that animate or otherwise require additional rendering may check this setting before callingGraphics.requestRendering()
. Default is true. -
getActionsRequestRendering
public boolean getActionsRequestRendering() -
getDebugColor
The default color that can be used by actors to draw debug lines. -
setDebugInvisible
public void setDebugInvisible(boolean debugInvisible) If true, debug lines are shown for actors even whenActor.isVisible()
is false. -
setDebugAll
public void setDebugAll(boolean debugAll) If true, debug lines are shown for all actors. -
isDebugAll
public boolean isDebugAll() -
setDebugUnderMouse
public void setDebugUnderMouse(boolean debugUnderMouse) If true, debug is enabled only for the actor under the mouse. Can be combined withsetDebugAll(boolean)
. -
setDebugParentUnderMouse
public void setDebugParentUnderMouse(boolean debugParentUnderMouse) If true, debug is enabled only for the parent of the actor under the mouse. Can be combined withsetDebugAll(boolean)
. -
setDebugTableUnderMouse
If notTable.Debug.none
, debug is enabled only for the first ascendant of the actor under the mouse that is a table. Can be combined withsetDebugAll(boolean)
.- Parameters:
debugTableUnderMouse
- May be null forTable.Debug.none
.
-
setDebugTableUnderMouse
public void setDebugTableUnderMouse(boolean debugTableUnderMouse) If true, debug is enabled only for the first ascendant of the actor under the mouse that is a table. Can be combined withsetDebugAll(boolean)
. -
dispose
public void dispose()Description copied from interface:com.badlogic.gdx.utils.Disposable
Releases all resources of this object.- Specified by:
dispose
in interfaceDisposable
-