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 classInternal class for managing touch focus. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidact()Callsact(float)withGraphics.getDeltaTime(), limited to a minimum of 30fps.voidact(float delta) Calls theActor.act(float)method on each actor in the stage.voidAdds an action to the root of the stage.voidAdds an actor to the root of the stage.booleanaddCaptureListener(EventListener listener) Adds a capture listener to the root.booleanaddListener(EventListener listener) Adds a listener to the root.voidaddTouchFocus(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.voidcalculateScissors(Rectangle localRect, Rectangle scissorRect) Calculates window scissor coordinates from local coordinates using the batch's current transformation matrix.voidRemoves all touch focus listeners, sending a touchUp event to each listener.voidcancelTouchFocus(Actor listenerActor) Cancels touch focus for all listeners with the specified listener actor.voidcancelTouchFocusExcept(EventListener exceptListener, Actor exceptActor) Cancels touch focus for all listeners except the specified listener.voidclear()Removes the root's children, actions, and listeners.voiddispose()Releases all resources of this object.voiddraw()booleanReturns the root's child actors.getBatch()The viewport's camera.The default color that can be used by actors to draw debug lines.floatThe 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.floatgetWidth()The viewport's world width.hit(float stageX, float stageY, boolean touchable) Returns theActorat the specified location in stage coordinates.booleanbooleankeyDown(int keyCode) Applies a key down event to the actor that haskeyboard focus, if any, and returns true if the event washandled.booleankeyTyped(char character) Applies a key typed event to the actor that haskeyboard focus, if any, and returns true if the event washandled.booleankeyUp(int keyCode) Applies a key up event to the actor that haskeyboard focus, if any, and returns true if the event washandled.booleanmouseMoved(int screenX, int screenY) Applies a mouse moved event to the stage and returns true if an actor in the scenehandledthe event.booleanremoveCaptureListener(EventListener listener) Removes a listener from the root.booleanremoveListener(EventListener listener) Removes a listener from the root.voidremoveTouchFocus(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.booleanscrolled(float amountX, float amountY) Applies a mouse scroll event to the stage and returns true if an actor in the scenehandledthe event.voidsetActionsRequestRendering(boolean actionsRequestRendering) If true, any actions executed during a call toact()) will result in a call toGraphics.requestRendering().voidsetDebugAll(boolean debugAll) If true, debug lines are shown for all actors.voidsetDebugInvisible(boolean debugInvisible) If true, debug lines are shown for actors even whenActor.isVisible()is false.voidsetDebugParentUnderMouse(boolean debugParentUnderMouse) If true, debug is enabled only for the parent of the actor under the mouse.voidsetDebugTableUnderMouse(boolean debugTableUnderMouse) If true, debug is enabled only for the first ascendant of the actor under the mouse that is a table.voidsetDebugTableUnderMouse(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.voidsetDebugUnderMouse(boolean debugUnderMouse) If true, debug is enabled only for the actor under the mouse.booleansetKeyboardFocus(Actor actor) Sets the actor that will receive key events.voidReplaces the root group.booleansetScrollFocus(Actor actor) Sets the actor that will receive scroll events.voidsetViewport(Viewport viewport) stageToScreenCoordinates(Vector2 stageCoords) Transforms the stage coordinates to screen coordinates.toScreenCoordinates(Vector2 coords, Matrix4 transformMatrix) Transforms the coordinates to screen coordinates.booleantouchCancelled(int screenX, int screenY, int pointer, int button) Called when the touch gesture is cancelled.booleantouchDown(int screenX, int screenY, int pointer, int button) Applies a touch down event to the stage and returns true if an actor in the scenehandledthe event.booleantouchDragged(int screenX, int screenY, int pointer) Applies a touch moved event to the stage and returns true if an actor in the scenehandledthe event.booleantouchUp(int screenX, int screenY, int pointer, int button) Applies a touch up event to the stage and returns true if an actor in the scenehandledthe event.voidRemoves the touch, keyboard, and scroll focus for the specified actor and any descendants.voidRemoves the touch, keyboard, and scroll focused actors.
-
Constructor Details
-
Stage
Creates a stage with the specified viewport. The stage will use its ownBatchwhich 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 scenehandledthe event.- Specified by:
touchDownin interfaceInputProcessor- Overrides:
touchDownin 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 scenehandledthe event. Onlylistenersthat returned true for touchDown will receive this event.- Specified by:
touchDraggedin interfaceInputProcessor- Overrides:
touchDraggedin classInputAdapterpointer- 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 scenehandledthe event. Onlylistenersthat returned true for touchDown will receive this event.- Specified by:
touchUpin interfaceInputProcessor- Overrides:
touchUpin classInputAdapterpointer- 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.InputProcessorCalled 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.LEFTon iOS.- Specified by:
touchCancelledin interfaceInputProcessor- Overrides:
touchCancelledin classInputAdapterpointer- 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 scenehandledthe event. This event only occurs on the desktop.- Specified by:
mouseMovedin interfaceInputProcessor- Overrides:
mouseMovedin 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 scenehandledthe event. This event only occurs on the desktop.- Specified by:
scrolledin interfaceInputProcessor- Overrides:
scrolledin 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:
keyDownin interfaceInputProcessor- Overrides:
keyDownin 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:
keyUpin interfaceInputProcessor- Overrides:
keyUpin 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:
keyTypedin interfaceInputProcessor- Overrides:
keyTypedin 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 actorandtargetfor 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 theActorat 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.DisposableReleases all resources of this object.- Specified by:
disposein interfaceDisposable
-