public class Stage extends InputAdapter implements Disposable
actors. Stage handles the viewport and distributes input events.
A stage fills the whole screen. setViewport(float, float, boolean) 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.
| Modifier and Type | Class and Description |
|---|---|
static class |
Stage.TouchFocus
Internal class for managing touch focus.
|
| Constructor and Description |
|---|
Stage()
Creates a stage with a
viewport equal to the device screen resolution. |
Stage(float width,
float height,
boolean keepAspectRatio)
Creates a stage with the specified
viewport. |
Stage(float width,
float height,
boolean keepAspectRatio,
SpriteBatch batch)
Creates a stage with the specified
viewport and SpriteBatch. |
| Modifier and Type | Method and Description |
|---|---|
void |
act()
Calls
act(float) with Graphics.getDeltaTime(). |
void |
act(float delta)
Calls the
Actor.act(float) method on each actor in the stage. |
void |
addAction(Action action)
Adds an action to the root of the stage.
|
void |
addActor(Actor actor)
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 |
cancelTouchFocus()
Sends a touchUp event to all listeners that are registered to receive touchDragged and touchUp events and removes their
touch focus.
|
void |
cancelTouchFocus(EventListener listener,
Actor actor)
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() |
Array<Actor> |
getActors()
Returns the root's child actors.
|
Camera |
getCamera() |
float |
getGutterHeight()
Half the amount in the y direction that the stage's viewport was lengthened to fill the screen.
|
float |
getGutterWidth()
Half the amount in the x direction that the stage's viewport was lengthened to fill the screen.
|
float |
getHeight()
The height of the stage's viewport.
|
Actor |
getKeyboardFocus()
Gets the actor that will receive key events.
|
Group |
getRoot()
Returns the root group which holds all actors in the stage.
|
Actor |
getScrollFocus()
Gets the actor that will receive scroll events.
|
SpriteBatch |
getSpriteBatch() |
float |
getWidth()
The width of the stage's viewport.
|
Actor |
hit(float stageX,
float stageY,
boolean touchable)
Returns the
Actor at the specified location in stage coordinates. |
boolean |
keyDown(int keyCode)
Applies a key down event to the actor that has
keyboard focus, if any, and returns
true if the event was handled. |
boolean |
keyTyped(char character)
Applies a key typed event to the actor that has
keyboard focus, if any, and returns
true if the event was handled. |
boolean |
keyUp(int keyCode)
Applies a key up event to the actor that has
keyboard focus, if any, and returns true
if the event was handled. |
boolean |
mouseMoved(int screenX,
int screenY)
Applies a mouse moved event to the stage and returns true if an actor in the scene
handled 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 the listener from being notified for all touchDragged and touchUp events for the specified pointer and button.
|
Vector2 |
screenToStageCoordinates(Vector2 screenCoords)
Transforms the screen coordinates to stage coordinates.
|
boolean |
scrolled(int amount)
Applies a mouse scroll event to the stage and returns true if an actor in the scene
handled the
event. |
void |
setCamera(Camera camera)
Sets the stage's camera.
|
void |
setKeyboardFocus(Actor actor)
Sets the actor that will receive key events.
|
void |
setScrollFocus(Actor actor)
Sets the actor that will receive scroll events.
|
void |
setViewport(float width,
float height,
boolean keepAspectRatio)
Sets the dimensions of the stage's viewport.
|
Vector2 |
stageToScreenCoordinates(Vector2 stageCoords)
Transforms the stage coordinates to screen coordinates.
|
Vector2 |
toScreenCoordinates(Vector2 coords,
Matrix4 transformMatrix)
Transforms the coordinates to screen coordinates.
|
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 scene
handled 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 scene
handled 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 scene
handled the event. |
void |
unfocus(Actor actor)
Removes the touch, keyboard, and scroll focus for the specified actor and any descendants.
|
void |
unfocusAll()
Removes the touch, keyboard, and scroll focused actors.
|
public Stage()
viewport equal to the device screen resolution. The stage
will use its own SpriteBatch.public Stage(float width,
float height,
boolean keepAspectRatio)
viewport. The stage will use its own
SpriteBatch, which will be disposed when the stage is disposed.public Stage(float width,
float height,
boolean keepAspectRatio,
SpriteBatch batch)
viewport and SpriteBatch. This can be
used to avoid creating a new SpriteBatch (which can be somewhat slow) if multiple stages are used during an applications
life time.batch - Will not be disposed if dispose() is called. Handle disposal yourself.public void setViewport(float width,
float height,
boolean keepAspectRatio)
getGutterWidth() and getGutterHeight() provide access to the amount that was
lengthened.public void draw()
public void act()
act(float) with Graphics.getDeltaTime().public void act(float delta)
Actor.act(float) method on each actor in the stage. Typically called each frame. This method also fires
enter and exit events.delta - Time in seconds since the last frame.public boolean touchDown(int screenX,
int screenY,
int pointer,
int button)
handled the event.touchDown in interface InputProcessortouchDown in class InputAdapterscreenX - 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 buttonpublic boolean touchDragged(int screenX,
int screenY,
int pointer)
handled the event.
Only listeners that returned true for touchDown will receive this event.touchDragged in interface InputProcessortouchDragged in class InputAdapterpointer - the pointer for the event.public boolean touchUp(int screenX,
int screenY,
int pointer,
int button)
handled the event.
Only listeners that returned true for touchDown will receive this event.touchUp in interface InputProcessortouchUp in class InputAdapterpointer - the pointer for the event.button - the buttonpublic boolean mouseMoved(int screenX,
int screenY)
handled the event.
This event only occurs on the desktop.mouseMoved in interface InputProcessormouseMoved in class InputAdapterpublic boolean scrolled(int amount)
handled the
event. This event only occurs on the desktop.scrolled in interface InputProcessorscrolled in class InputAdapteramount - the scroll amount, -1 or 1 depending on the direction the wheel was scrolled.public boolean keyDown(int keyCode)
keyboard focus, if any, and returns
true if the event was handled.keyDown in interface InputProcessorkeyDown in class InputAdapterkeyCode - one of the constants in Input.Keyspublic boolean keyUp(int keyCode)
keyboard focus, if any, and returns true
if the event was handled.keyUp in interface InputProcessorkeyUp in class InputAdapterkeyCode - one of the constants in Input.Keyspublic boolean keyTyped(char character)
keyboard focus, if any, and returns
true if the event was handled.keyTyped in interface InputProcessorkeyTyped in class InputAdaptercharacter - The characterpublic void addTouchFocus(EventListener listener, Actor listenerActor, Actor target, int pointer, int button)
listener actor and target.public void removeTouchFocus(EventListener listener, Actor listenerActor, Actor target, int pointer, int button)
public void cancelTouchFocus()
Integer.MIN_VALUE. This method removes all touch focus listeners, but
sends a touchUp event so that the state of the listeners remains consistent (listeners typically expect to receive touchUp
eventually).public void cancelTouchFocus(EventListener listener, Actor actor)
cancelTouchFocus()public void addActor(Actor actor)
Group.addActor(Actor),
Actor.remove()public void addAction(Action action)
Actor.addAction(Action)public Array<Actor> getActors()
Group.getChildren()public boolean addListener(EventListener listener)
Actor.addListener(EventListener)public boolean removeListener(EventListener listener)
Actor.removeListener(EventListener)public boolean addCaptureListener(EventListener listener)
public boolean removeCaptureListener(EventListener listener)
public void clear()
public void unfocusAll()
public void unfocus(Actor actor)
public void setKeyboardFocus(Actor actor)
actor - May be null.public Actor getKeyboardFocus()
public void setScrollFocus(Actor actor)
actor - May be null.public Actor getScrollFocus()
public float getWidth()
setViewport(float, float, boolean)public float getHeight()
setViewport(float, float, boolean)public float getGutterWidth()
setViewport(float, float, boolean)public float getGutterHeight()
setViewport(float, float, boolean)public SpriteBatch getSpriteBatch()
public Camera getCamera()
public void setCamera(Camera camera)
setViewport(float, float, boolean) can be called
after the camera is set. draw() will call Camera.update() and use the Camera.combined matrix
for the SpriteBatch projection matrix.public Group getRoot()
public Actor hit(float stageX, float stageY, boolean touchable)
Actor 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, use
screenToStageCoordinates(Vector2).touchable - If true, the hit detection will respect the touchability.public Vector2 screenToStageCoordinates(Vector2 screenCoords)
screenCoords - Input screen coordinates and output for resulting stage coordinates.public Vector2 stageToScreenCoordinates(Vector2 stageCoords)
stageCoords - Input stage coordinates and output for resulting screen coordinates.public Vector2 toScreenCoordinates(Vector2 coords, Matrix4 transformMatrix)
SpriteBatch.getTransformMatrix()
during Actor.draw(SpriteBatch, float).public void dispose()
Disposabledispose in interface Disposable