APIdia Launch Partner
Top Description Inners Fields Constructors Methods
com.badlogic.gdx.scenes.scene2d.ui

public Class Tree<N extends Tree.Node, V>

extends WidgetGroup
implements Styleable<Tree.TreeStyle>
Class Inheritance
All Implemented Interfaces
com.badlogic.gdx.scenes.scene2d.ui.Styleable
Type Parameters
<N>
The type of nodes in the tree.
<V>
The type of values for each node.
Imports
com.badlogic.gdx.Application.ApplicationType, .Gdx, com.badlogic.gdx.graphics.Color, com.badlogic.gdx.graphics.g2d.Batch, com.badlogic.gdx.math.Rectangle, .Vector2, com.badlogic.gdx.scenes.scene2d.Actor, .Group, .InputEvent, com.badlogic.gdx.scenes.scene2d.ui.Tree.Node, com.badlogic.gdx.scenes.scene2d.utils.ChangeListener.ChangeEvent, .ClickListener, .Drawable, .Layout, .Selection, .UIUtils, com.badlogic.gdx.utils.Array, .Null

A tree widget where each node has an icon, actor, and child nodes.

The preferred size of the tree is determined by the preferred size of the actors for the expanded nodes.

ChangeEvent is fired when the selected node changes.

Author
Nathan Sweet

Nested and Inner Type Summary

Modifier and TypeClass and Description
public abstract static class
Tree.Node<
The type for the node's parent and child nodes.
N extends Tree.Node
,
The type for the node's value.
V
,
The type for the node's actor.
A extends Actor
>

A Tree node which has an actor and value.

public static class
Tree.TreeStyle

The style for a Tree.

Field Summary

Modifier and TypeField and Description
private ClickListener
private N
pack-priv float
pack-priv float
pack-priv float
private N
pack-priv float
pack-priv float
private float
private float
pack-priv N
pack-priv final Array<N>
pack-priv final Selection<N>
private boolean
pack-priv Tree.TreeStyle
private static final Vector2
pack-priv float

Constructor Summary

AccessConstructor and Description
public
Tree(Skin skin)

public
Tree(Skin skin, String styleName)

public

Method Summary

Modifier and TypeMethod and Description
public void
add(N node)

public void
clearChildren(boolean unfocus)

Overrides com.badlogic.gdx.scenes.scene2d.Group.clearChildren.

Removes all tree nodes.

public void
pack-priv static void
collapseAll(Array<? extends Tree.Node<N extends Tree.Node, V, A extends Actor>> nodes)

private void
private void
computeSize(Array<N> nodes, float indent, float plusMinusWidth)

public void
draw(Batch batch, float parentAlpha)

Overrides com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup.draw.

If this method is overridden, the super method or validate() should be called to ensure the widget group is laid out.

protected void
drawBackground(Batch batch, float parentAlpha)

Called to draw the background.

protected void
drawExpandIcon(N node, Drawable expandIcon, Batch batch, float x, float y)

protected void
drawIcon(N node, Drawable icon, Batch batch, float x, float y)

protected float

Returns:

The Y position of the last visible actor for the nodes.
drawIcons
(Batch batch, float r, float g, float b, float a, N
null for the root nodes.
parent
,
Array<N> nodes, float indent, float plusMinusWidth)

Draws selection, icons, and expand icons.

protected void
drawOver(N node, Drawable over, Batch batch, float x, float y, float width, float height)

protected void
drawSelection(N node, Drawable selection, Batch batch, float x, float y, float width, float height)

public void
pack-priv static void
expandAll(Array<? extends Tree.Node<N extends Tree.Node, V, A extends Actor>> nodes)

public void
pack-priv static boolean
findExpandedValues(Array<? extends Tree.Node<N extends Tree.Node, V, A extends Actor>> nodes, Array<T> values)

public N
findNode(V value)

Returns the node with the specified value, or null.

pack-priv static Tree.Node<N extends Tree.Node, V, A extends Actor>
findNode(Array<? extends Tree.Node<N extends Tree.Node, V, A extends Actor>> nodes, Object value)

public ClickListener
getClickListener()

Returns the click listener the tree uses for clicking on nodes and the over node.

protected Drawable
getExpandIcon(N node, float
The X coordinate of the over node's icon.
iconX
)

Returns the drawable for the expand icon.

public float
getIndentSpacing()

Returns the amount of horizontal space for indentation level.

public N
getNodeAt(float y)

Returns: May be null.

private float
getNodeAt(Array<N> nodes, float y, float rowY)

public Array<N>
getNodes()
Deprecated Use getRootNodes().

public N
getOverNode()

Returns: May be null.

public V
getOverValue()

Returns: May be null.

public float
public float
public Array<N>
getRootNodes()

If the order of the root nodes is changed, updateRootNodes() must be called to ensure the nodes' actors are in the correct order.

public N
getSelectedNode()

Returns the first selected node, or null.

public V
getSelectedValue()

Returns the first selected value, or null.

public Selection<N>
public Tree.TreeStyle
getStyle()

Implements com.badlogic.gdx.scenes.scene2d.ui.Styleable.getStyle.

Get the current style of the actor

public float
private void
public void
insert(int index, N node)

public void
invalidate()

Overrides com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup.invalidate.

Implements com.badlogic.gdx.scenes.scene2d.utils.Layout.invalidate.

Invalidates this actor's layout, causing layout() to happen the next time validate() is called.

public void
layout()

Overrides com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup.layout.

Implements com.badlogic.gdx.scenes.scene2d.utils.Layout.layout.

Computes and caches any information needed for drawing and, if this actor has children, positions and sizes each child, calls invalidate() on any each child whose width or height has changed, and calls validate() on each child.

private float
layout(Array<N> nodes, float indent, float y, float plusMinusWidth)

private float
public void
remove(N node)

public void
pack-priv void
selectNodes(Array<N> nodes, float low, float high)

public void
setIconSpacing(float left, float right)

Sets the amount of horizontal space left and right of the node's icon.

public void
setIndentSpacing(float indentSpacing)

public void
setOverNode(N
May be null.
overNode
)

public void
setPadding(float padding)

Sets the amount of horizontal space between the nodes and the left/right edges of the tree.

public void
setPadding(float left, float right)

Sets the amount of horizontal space between the nodes and the left/right edges of the tree.

public void
setStyle(Tree.TreeStyle style)

Implements com.badlogic.gdx.scenes.scene2d.ui.Styleable.setStyle.

Set the current style of the actor

public void
setYSpacing(float ySpacing)

Sets the amount of vertical space between nodes.

public void
updateRootNodes()

Updates the order of the actors in the tree for all root nodes and all child nodes.

Inherited from com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup:
childrenChangedgetMaxHeightgetMaxWidthgetMinHeightgetMinWidthhitinvalidateHierarchyneedsLayoutpacksetFillParentsetLayoutEnabledsizeChangedvalidate

Field Detail

clickListenerback to summary
private ClickListener clickListener
foundNodeback to summary
private N foundNode
iconSpacingLeftback to summary
pack-priv float iconSpacingLeft
iconSpacingRightback to summary
pack-priv float iconSpacingRight
indentSpacingback to summary
pack-priv float indentSpacing
overNodeback to summary
private N overNode
paddingLeftback to summary
pack-priv float paddingLeft
paddingRightback to summary
pack-priv float paddingRight
prefHeightback to summary
private float prefHeight
prefWidthback to summary
private float prefWidth
rangeStartback to summary
pack-priv N rangeStart
rootNodesback to summary
pack-priv final Array<N> rootNodes
selectionback to summary
pack-priv final Selection<N> selection
sizeInvalidback to summary
private boolean sizeInvalid
styleback to summary
pack-priv Tree.TreeStyle style
tmpback to summary
private static final Vector2 tmp

Hides com.badlogic.gdx.scenes.scene2d.Group.tmp.

ySpacingback to summary
pack-priv float ySpacing

Constructor Detail

Treeback to summary
public Tree(Skin skin)
Treeback to summary
public Tree(Skin skin, String styleName)
Treeback to summary
public Tree(Tree.TreeStyle style)

Method Detail

addback to summary
public void add(N node)
clearChildrenback to summary
public void clearChildren(boolean unfocus)

Overrides com.badlogic.gdx.scenes.scene2d.Group.clearChildren.

Removes all tree nodes.

collapseAllback to summary
public void collapseAll()
collapseAllback to summary
pack-priv static void collapseAll(Array<? extends Tree.Node<N extends Tree.Node, V, A extends Actor>> nodes)
computeSizeback to summary
private void computeSize()
computeSizeback to summary
private void computeSize(Array<N> nodes, float indent, float plusMinusWidth)
drawback to summary
public void draw(Batch batch, float parentAlpha)

Overrides com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup.draw.

Doc from com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup.draw.

If this method is overridden, the super method or validate() should be called to ensure the widget group is laid out.

drawBackgroundback to summary
protected void drawBackground(Batch batch, float parentAlpha)

Called to draw the background. Default implementation draws the style background drawable.

drawExpandIconback to summary
protected void drawExpandIcon(N node, Drawable expandIcon, Batch batch, float x, float y)
drawIconback to summary
protected void drawIcon(N node, Drawable icon, Batch batch, float x, float y)
drawIconsback to summary
protected float drawIcons(Batch batch, float r, float g, float b, float a, N parent, Array<N> nodes, float indent, float plusMinusWidth)

Draws selection, icons, and expand icons.

Parameters
parent:N

null for the root nodes.

Returns:float

The Y position of the last visible actor for the nodes.

drawOverback to summary
protected void drawOver(N node, Drawable over, Batch batch, float x, float y, float width, float height)
drawSelectionback to summary
protected void drawSelection(N node, Drawable selection, Batch batch, float x, float y, float width, float height)
expandAllback to summary
public void expandAll()
expandAllback to summary
pack-priv static void expandAll(Array<? extends Tree.Node<N extends Tree.Node, V, A extends Actor>> nodes)
findExpandedValuesback to summary
public void findExpandedValues(Array<V> values)
findExpandedValuesback to summary
pack-priv static boolean findExpandedValues(Array<? extends Tree.Node<N extends Tree.Node, V, A extends Actor>> nodes, Array<T> values)
findNodeback to summary
public N findNode(V value)

Returns the node with the specified value, or null.

Annotations
@Null
findNodeback to summary
pack-priv static Tree.Node<N extends Tree.Node, V, A extends Actor> findNode(Array<? extends Tree.Node<N extends Tree.Node, V, A extends Actor>> nodes, Object value)
Annotations
@Null
getClickListenerback to summary
public ClickListener getClickListener()

Returns the click listener the tree uses for clicking on nodes and the over node.

getExpandIconback to summary
protected Drawable getExpandIcon(N node, float iconX)

Returns the drawable for the expand icon. The default implementation returns TreeStyle#plusOver or TreeStyle#minusOver on the desktop if the node is the over node, the mouse is left of iconX, and clicking would expand the node.

Parameters
iconX:float

The X coordinate of the over node's icon.

getIndentSpacingback to summary
public float getIndentSpacing()

Returns the amount of horizontal space for indentation level.

getNodeAtback to summary
public N getNodeAt(float y)
Returns:N

May be null.

Annotations
@Null
getNodeAtback to summary
private float getNodeAt(Array<N> nodes, float y, float rowY)
getNodesback to summary
public Array<N> getNodes()

Deprecated

Use getRootNodes().

Annotations
@Deprecated
getOverNodeback to summary
public N getOverNode()
Returns:N

May be null.

Annotations
@Null
getOverValueback to summary
public V getOverValue()
Returns:V

May be null.

Annotations
@Null
getPrefHeightback to summary
public float getPrefHeight()

Overrides com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup.getPrefHeight.

Implements com.badlogic.gdx.scenes.scene2d.utils.Layout.getPrefHeight.

getPrefWidthback to summary
public float getPrefWidth()

Overrides com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup.getPrefWidth.

Implements com.badlogic.gdx.scenes.scene2d.utils.Layout.getPrefWidth.

getRootNodesback to summary
public Array<N> getRootNodes()

If the order of the root nodes is changed, updateRootNodes() must be called to ensure the nodes' actors are in the correct order.

getSelectedNodeback to summary
public N getSelectedNode()

Returns the first selected node, or null.

Annotations
@Null
getSelectedValueback to summary
public V getSelectedValue()

Returns the first selected value, or null.

Annotations
@Null
getSelectionback to summary
public Selection<N> getSelection()
getStyleback to summary
public Tree.TreeStyle getStyle()

Implements com.badlogic.gdx.scenes.scene2d.ui.Styleable.getStyle.

Doc from com.badlogic.gdx.scenes.scene2d.ui.Styleable.getStyle.

Get the current style of the actor

getYSpacingback to summary
public float getYSpacing()
initializeback to summary
private void initialize()
insertback to summary
public void insert(int index, N node)
invalidateback to summary
public void invalidate()

Overrides com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup.invalidate.

Implements com.badlogic.gdx.scenes.scene2d.utils.Layout.invalidate.

Doc from com.badlogic.gdx.scenes.scene2d.utils.Layout.invalidate.

Invalidates this actor's layout, causing layout() to happen the next time validate() is called. This method should be called when state changes in the actor that requires a layout but does not change the minimum, preferred, maximum, or actual size of the actor (meaning it does not affect the parent actor's layout).

layoutback to summary
public void layout()

Overrides com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup.layout.

Implements com.badlogic.gdx.scenes.scene2d.utils.Layout.layout.

Doc from com.badlogic.gdx.scenes.scene2d.utils.Layout.layout.

Computes and caches any information needed for drawing and, if this actor has children, positions and sizes each child, calls invalidate() on any each child whose width or height has changed, and calls validate() on each child. This method should almost never be called directly, instead validate() should be used.

layoutback to summary
private float layout(Array<N> nodes, float indent, float y, float plusMinusWidth)
plusMinusWidthback to summary
private float plusMinusWidth()
removeback to summary
public void remove(N node)
restoreExpandedValuesback to summary
public void restoreExpandedValues(Array<V> values)
selectNodesback to summary
pack-priv void selectNodes(Array<N> nodes, float low, float high)
setIconSpacingback to summary
public void setIconSpacing(float left, float right)

Sets the amount of horizontal space left and right of the node's icon. If a node has no icon, the left spacing is used between the plus/minus drawable and the node's actor.

setIndentSpacingback to summary
public void setIndentSpacing(float indentSpacing)
setOverNodeback to summary
public void setOverNode(N overNode)
Parameters
overNode:N

May be null.

setPaddingback to summary
public void setPadding(float padding)

Sets the amount of horizontal space between the nodes and the left/right edges of the tree.

setPaddingback to summary
public void setPadding(float left, float right)

Sets the amount of horizontal space between the nodes and the left/right edges of the tree.

setStyleback to summary
public void setStyle(Tree.TreeStyle style)

Implements com.badlogic.gdx.scenes.scene2d.ui.Styleable.setStyle.

Doc from com.badlogic.gdx.scenes.scene2d.ui.Styleable.setStyle.

Set the current style of the actor

setYSpacingback to summary
public void setYSpacing(float ySpacing)

Sets the amount of vertical space between nodes.

updateRootNodesback to summary
public void updateRootNodes()

Updates the order of the actors in the tree for all root nodes and all child nodes. This is useful after changing the order of getRootNodes().

See Also
Node#updateChildren()
com.badlogic.gdx.scenes.scene2d.ui back to summary

public abstract Class Tree.Node<N extends Tree.Node, V, A extends Actor>

extends Object
Class Inheritance
Type Parameters
<N>
The type for the node's parent and child nodes.
<V>
The type for the node's value.
<A>
The type for the node's actor.

A Tree node which has an actor and value.

A subclass can be used so the generic type parameters don't need to be specified repeatedly.

Author
Nathan Sweet

Field Summary

Modifier and TypeField and Description
pack-priv A
pack-priv final Array<N>
pack-priv boolean
pack-priv float
pack-priv Drawable
pack-priv N
pack-priv boolean
pack-priv V

Constructor Summary

AccessConstructor and Description
public
Node(A actor)

public
Node()

Creates a node without an actor.

Method Summary

Modifier and TypeMethod and Description
public void
add(N node)

public void
addAll(Array<N> nodes)

protected int

Returns:

The number of node actors added to the tree.
addToTree
(Tree<N, V> tree, int actorIndex)

Called to add the actor to the tree when the node's parent is expanded.

public void
clearChildren()

Removes all children from this node.

public void
collapseAll()

Collapses all nodes under and including this node.

pack-priv int
public void
expandAll()

Expands all nodes under and including this node.

public void
expandTo()

Expands all parent nodes of this node.

public void
public N
findNode(V value)

Returns this node or the child node with the specified value, or null.

public A
public Array<N>
getChildren()

If the children order is changed, updateChildren() must be called to ensure the node's actors are in the correct order.

public float
getHeight()

Returns the height of the node as calculated for layout.

public Drawable
public int
public N
getParent()

Returns: May be null.

public Tree<N, V>
getTree()

Returns the tree this node's actor is currently in, or null.

public V
public boolean
public void
insert(int childIndex, N node)

public boolean
isAscendantOf(N node)

Returns true if the specified node is this node or an ascendant of this node.

public boolean
isDescendantOf(N node)

Returns true if the specified node is this node or an descendant of this node.

public boolean
public boolean
public void
remove()

Remove this node from its parent.

public void
remove(N node)

Remove the specified child node from this node.

protected void
removeFromTree(Tree<N, V> tree, int actorIndex)

Called to remove the actor from the tree, eg when the node is removed or the node's parent is collapsed.

public void
public void
setActor(A newActor)

public void
setExpanded(boolean expanded)

public void
setIcon(Drawable icon)

Sets an icon that will be drawn to the left of the actor.

public void
setSelectable(boolean selectable)

public void
setValue(V value)

Sets an application specific value for this node.

public void
updateChildren()

Updates the order of the actors in the tree for this node and all child nodes.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

actorback to summary
pack-priv A actor
childrenback to summary
pack-priv final Array<N> children
expandedback to summary
pack-priv boolean expanded
heightback to summary
pack-priv float height
iconback to summary
pack-priv Drawable icon
parentback to summary
pack-priv N parent
selectableback to summary
pack-priv boolean selectable
valueback to summary
pack-priv V value

Constructor Detail

Nodeback to summary
public Node(A actor)
Nodeback to summary
public Node()

Creates a node without an actor. An actor must be set using setActor(Actor) before this node can be used.

Method Detail

addback to summary
public void add(N node)
addAllback to summary
public void addAll(Array<N> nodes)
addToTreeback to summary
protected int addToTree(Tree<N, V> tree, int actorIndex)

Called to add the actor to the tree when the node's parent is expanded.

Returns:int

The number of node actors added to the tree.

clearChildrenback to summary
public void clearChildren()

Removes all children from this node.

collapseAllback to summary
public void collapseAll()

Collapses all nodes under and including this node.

countActorsback to summary
pack-priv int countActors()
expandAllback to summary
public void expandAll()

Expands all nodes under and including this node.

expandToback to summary
public void expandTo()

Expands all parent nodes of this node.

findExpandedValuesback to summary
public void findExpandedValues(Array<V> values)
findNodeback to summary
public N findNode(V value)

Returns this node or the child node with the specified value, or null.

Annotations
@Null
getActorback to summary
public A getActor()
getChildrenback to summary
public Array<N> getChildren()

If the children order is changed, updateChildren() must be called to ensure the node's actors are in the correct order. That is not necessary if this node is not in the tree or is not expanded, because then the child node's actors are not in the tree.

getHeightback to summary
public float getHeight()

Returns the height of the node as calculated for layout. A subclass may override and increase the returned height to create a blank space in the tree above the node, eg for a separator.

getIconback to summary
public Drawable getIcon()
Annotations
@Null
getLevelback to summary
public int getLevel()
getParentback to summary
public N getParent()
Returns:N

May be null.

Annotations
@Null
getTreeback to summary
public Tree<N, V> getTree()

Returns the tree this node's actor is currently in, or null. The actor is only in the tree when all of its parent nodes are expanded.

Annotations
@Null
getValueback to summary
public V getValue()
Annotations
@Null
hasChildrenback to summary
public boolean hasChildren()
insertback to summary
public void insert(int childIndex, N node)
isAscendantOfback to summary
public boolean isAscendantOf(N node)

Returns true if the specified node is this node or an ascendant of this node.

isDescendantOfback to summary
public boolean isDescendantOf(N node)

Returns true if the specified node is this node or an descendant of this node.

isExpandedback to summary
public boolean isExpanded()
isSelectableback to summary
public boolean isSelectable()
removeback to summary
public void remove()

Remove this node from its parent.

removeback to summary
public void remove(N node)

Remove the specified child node from this node. Does nothing if the node is not a child of this node.

removeFromTreeback to summary
protected void removeFromTree(Tree<N, V> tree, int actorIndex)

Called to remove the actor from the tree, eg when the node is removed or the node's parent is collapsed.

restoreExpandedValuesback to summary
public void restoreExpandedValues(Array<V> values)
setActorback to summary
public void setActor(A newActor)
setExpandedback to summary
public void setExpanded(boolean expanded)
setIconback to summary
public void setIcon(Drawable icon)

Sets an icon that will be drawn to the left of the actor.

setSelectableback to summary
public void setSelectable(boolean selectable)
setValueback to summary
public void setValue(V value)

Sets an application specific value for this node.

updateChildrenback to summary
public void updateChildren()

Updates the order of the actors in the tree for this node and all child nodes. This is useful after changing the order of getChildren().

See Also
Tree#updateRootNodes()
com.badlogic.gdx.scenes.scene2d.ui back to summary

public Class Tree.TreeStyle

extends Object
Class Inheritance

The style for a Tree.
Author
Nathan Sweet

Field Summary

Modifier and TypeField and Description
public Drawable
public Drawable
public Drawable
public Drawable
public Drawable
public Drawable
public Drawable

Constructor Summary

AccessConstructor and Description
public
public
TreeStyle(Drawable plus, Drawable minus, Drawable selection)

public

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

backgroundback to summary
public Drawable background
Annotations
@Null
minusback to summary
public Drawable minus
minusOverback to summary
public Drawable minusOver
Annotations
@Null
overback to summary
public Drawable over
Annotations
@Null
plusback to summary
public Drawable plus
plusOverback to summary
public Drawable plusOver
Annotations
@Null
selectionback to summary
public Drawable selection
Annotations
@Null

Constructor Detail

TreeStyleback to summary
public TreeStyle()
TreeStyleback to summary
public TreeStyle(Drawable plus, Drawable minus, Drawable selection)
TreeStyleback to summary
public TreeStyle(Tree.TreeStyle style)