public class Stage extends InputAdapter implements 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.
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
ScalingViewport set to Scaling.stretch . |
Stage(Viewport viewport)
Creates a stage with the specified viewport.
|
Stage(Viewport viewport,
Batch batch)
Creates a stage with the specified viewport and batch.
|
Modifier and Type | Method and Description |
---|---|
void |
act()
Calls
act(float) with Graphics.getDeltaTime() , limited to a minimum of 30fps. |
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 |
calculateScissors(Rectangle localRect,
Rectangle scissorRect)
Calculates window scissor coordinates from local coordinates using the batch's current transformation matrix.
|
void |
cancelTouchFocus()
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 |
getActionsRequestRendering() |
Array<Actor> |
getActors()
Returns the root's child actors.
|
Batch |
getBatch() |
Camera |
getCamera()
The viewport's camera.
|
Color |
getDebugColor()
The default color that can be used by actors to draw debug lines.
|
float |
getHeight()
The viewport's world height.
|
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.
|
Viewport |
getViewport() |
float |
getWidth()
The viewport's world width.
|
Actor |
hit(float stageX,
float stageY,
boolean touchable)
Returns the
Actor at the specified location in stage coordinates. |
boolean |
isDebugAll() |
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 touch focus for the specified listener, pointer, and button.
|
Vector2 |
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 scene
handled the
event. |
void |
setActionsRequestRendering(boolean actionsRequestRendering)
If true, any actions executed during a call to
act() ) will result in a call to
Graphics.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 when
Actor.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 not
Table.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 |
setRoot(Group root)
Replaces the root group.
|
boolean |
setScrollFocus(Actor actor)
Sets the actor that will receive scroll events.
|
void |
setViewport(Viewport 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()
ScalingViewport
set to Scaling.stretch
. The stage will use its own Batch
which will be disposed when the stage is disposed.public Stage(Viewport viewport)
Batch
which will be disposed when the stage
is disposed.public void draw()
public void act()
act(float)
with Graphics.getDeltaTime()
, limited to a minimum of 30fps.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 InputProcessor
touchDown
in class InputAdapter
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 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 InputProcessor
touchDragged
in class InputAdapter
pointer
- 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 InputProcessor
touchUp
in class InputAdapter
pointer
- 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 InputProcessor
mouseMoved
in class InputAdapter
public boolean scrolled(float amountX, float amountY)
handled
the
event. This event only occurs on the desktop.scrolled
in interface InputProcessor
scrolled
in class InputAdapter
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.public boolean keyDown(int keyCode)
keyboard focus
, if any, and returns
true if the event was handled
.keyDown
in interface InputProcessor
keyDown
in class InputAdapter
keyCode
- one of the constants in Input.Keys
public boolean keyUp(int keyCode)
keyboard focus
, if any, and returns true
if the event was handled
.keyUp
in interface InputProcessor
keyUp
in class InputAdapter
keyCode
- one of the constants in Input.Keys
public boolean keyTyped(char character)
keyboard focus
, if any, and returns
true if the event was handled
.keyTyped
in interface InputProcessor
keyTyped
in class InputAdapter
character
- The characterpublic void addTouchFocus(EventListener listener, Actor listenerActor, Actor target, int pointer, int button)
touchDown
. The specified actors will be used as the listener actor
and
target
for the touchDragged and touchUp events.public void removeTouchFocus(EventListener listener, Actor listenerActor, Actor target, int pointer, int button)
public void cancelTouchFocus(Actor listenerActor)
cancelTouchFocus()
public void cancelTouchFocus()
Integer.MIN_VALUE
. Listeners can use
InputEvent.isTouchFocusCancel()
to ignore this event if needed.public void cancelTouchFocusExcept(@Null EventListener exceptListener, @Null Actor exceptActor)
cancelTouchFocus()
public void addActor(Actor actor)
Group.addActor(Actor)
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)
Actor.addCaptureListener(EventListener)
public boolean removeCaptureListener(EventListener listener)
public void clear()
public void unfocusAll()
public void unfocus(Actor actor)
public boolean setKeyboardFocus(@Null Actor actor)
actor
- May be null.FocusListener
.@Null public Actor getKeyboardFocus()
public boolean setScrollFocus(@Null Actor actor)
actor
- May be null.FocusListener
.@Null public Actor getScrollFocus()
public Batch getBatch()
public Viewport getViewport()
public void setViewport(Viewport viewport)
public float getWidth()
public float getHeight()
public Camera getCamera()
public Group getRoot()
public void setRoot(Group root)
Group.childrenChanged()
.@Null 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)
Batch.getTransformMatrix()
during
Actor.draw(Batch, float)
.Actor.localToStageCoordinates(Vector2)
public void calculateScissors(Rectangle localRect, Rectangle scissorRect)
public void setActionsRequestRendering(boolean actionsRequestRendering)
act()
) will result in a call to
Graphics.requestRendering()
. Widgets that animate or otherwise require additional rendering may check this setting
before calling Graphics.requestRendering()
. Default is true.public boolean getActionsRequestRendering()
public Color getDebugColor()
public void setDebugInvisible(boolean debugInvisible)
Actor.isVisible()
is false.public void setDebugAll(boolean debugAll)
public boolean isDebugAll()
public void setDebugUnderMouse(boolean debugUnderMouse)
setDebugAll(boolean)
.public void setDebugParentUnderMouse(boolean debugParentUnderMouse)
setDebugAll(boolean)
.public void setDebugTableUnderMouse(@Null Table.Debug debugTableUnderMouse)
Table.Debug.none
, debug is enabled only for the first ascendant of the actor under the mouse that is a table. Can
be combined with setDebugAll(boolean)
.debugTableUnderMouse
- May be null for Table.Debug.none
.public void setDebugTableUnderMouse(boolean debugTableUnderMouse)
setDebugAll(boolean)
.public void dispose()
Disposable
dispose
in interface Disposable
Copyright © 2021. All rights reserved.