java.lang.Object
io.github.somesourcecode.someguiapi.scene.Node
Direct Known Subclasses:
GuiItem, Parent

public abstract class Node extends Object
The base class for all nodes in the scene graph. A scene graph is a set of tree data structures where every item has zero or one parent, and each item is either a "leaf" with zero sub-items or a "branch" with zero or more sub-items.

Each item in the scene graph is called a Node. Branch nodes are of type Parent.

If a program adds a child node to a Parent and that node is already a child of a different Parent or the root of a Scene, the node is automatically (and silently) removed from its former parent.

It is possible to rearrange the scene graph, e.g. to move a node or subtree from one location in the scene graph to another. This is normally done by removing the node from its current location before inserting it at the new location. However, a subtree will be automatically removed if it is added to a new parent, as described above.


Coordinate System

The Node class defines a computer graphics "local" coordinate system. Coordinates are specified in integer pixels, with the origin (0,0) in the top-left corner of the node. The x-coordinate increases to the right, and the y-coordinate increases downwards.


Transformations

Translation coordinates are specified in integer pixels, and move the node's origin by the specified amount in the x and y directions. The translation is applied after the layout has been computed, but before the node is rendered.

Since:
1.0.0
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    abstract int
    Returns the height of the node.
    final String
    Returns the id of this node.
    int
    Returns the x coordinate of the node's origin.
    int
    Returns the y coordinate of the node's origin.
    java.util.function.Consumer<NodeClickContext>
    Returns the consumer that is called when the node is clicked.
    java.util.function.Consumer<NodeClickContext>
    Returns the consumer that is called when the node receives a hot bar click.
    java.util.function.Consumer<NodeClickContext>
    Returns the consumer that is called when the node is left-clicked.
    java.util.function.Consumer<NodeClickContext>
    Returns the consumer that is called when the node is right-clicked.
    java.util.function.Consumer<NodeClickContext>
    Returns the consumer that is called when the node is shift-clicked.
    Returns the parent of this node.
    Returns the scene that this node is in.
    int
    Returns the x translation of the node.
    int
    Returns the y translation of the node.
    abstract int
    Returns the width of the node.
    boolean
    Returns whether the node is clipping its children.
    boolean
    Returns whether the node is visible.
    lookup(String selector)
    Finds this Node or the first sub-node by the given selector.
    lookupAll(String selector)
    Finds all nodes that match the given selector.
    protected Set<Node>
    lookupAll(String selector, Set<Node> results)
    Used by Node and Parent to traverse the scene graph to find all nodes that match the given selector.
    abstract Node
    nodeAt(int x, int y)
    Returns the node at the given coordinates.
    void
    relocate(int layoutX, int layoutY)
    Moves the node to the specified location.
    abstract Pixel
    renderPixelAt(int x, int y)
    Returns a Pixel that should be rendered at the given coordinates.
    void
    Requests a layout update for this node's parent.
    void
    setClipping(boolean clipping)
    Sets whether the node is clipping its children.
    final void
    Sets the id of this node.
    void
    setLayoutX(int layoutX)
    Sets the x coordinate of the node's origin.
    void
    setLayoutY(int layoutY)
    Sets the y coordinate of the node's origin.
    void
    setOnClick(java.util.function.Consumer<NodeClickContext> onClick)
    Sets the consumer that is called when the node is clicked.
    void
    setOnHotBarClick(java.util.function.Consumer<NodeClickContext> onHotBarClick)
    Sets the consumer that is called when the node receives a hot bar click.
    void
    setOnLeftClick(java.util.function.Consumer<NodeClickContext> onLeftClick)
    Sets the consumer that is called when the node is left-clicked.
    void
    setOnRightClick(java.util.function.Consumer<NodeClickContext> onRightClick)
    Sets the consumer that is called when the node is right-clicked.
    void
    setOnShiftClick(java.util.function.Consumer<NodeClickContext> onShiftClick)
    Sets the consumer that is called when the node is shift-clicked.
    void
    setTranslateX(int translateX)
    Sets the x translation of the node.
    void
    setTranslateY(int translateY)
    Sets the y translation of the node.
    void
    setVisible(boolean visible)
    Sets whether the node is visible.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Node

      public Node()
  • Method Details

    • getScene

      public Scene getScene()
      Returns the scene that this node is in. If this node is not in a scene, this method returns null.
      Returns:
      the scene that this node is in
      Since:
      2.0.0
    • getParent

      public Parent getParent()
      Returns the parent of this node. If this node has no parent, this method returns null.
      Returns:
      the parent of this node
      Since:
      1.0.0
    • getId

      public final String getId()
      Returns the id of this node.
      Returns:
      the id of this node
      Since:
      2.0.0
    • setId

      public final void setId(String id)
      Sets the id of this node. While the id should be unique, this uniqueness not enforced.
      Parameters:
      id - the id of this node
      Since:
      2.0.0
    • lookup

      public Node lookup(String selector)
      Finds this Node or the first sub-node by the given selector.

      For example, to find a node with the id "my-node", the method can be used like this: scene.lookup("#my-node").

      Parameters:
      selector - the selector
      Returns:
      the first node that matches the selector, null if none is found
      Since:
      2.0.0
    • lookupAll

      public Set<Node> lookupAll(String selector)
      Finds all nodes that match the given selector.

      For example, to find all nodes with the class "my-class", the method can be used like this: scene.lookupAll(".my-class").

      Parameters:
      selector - the selector
      Returns:
      a set of nodes that match the selector. This is always non-null and unmodifiable.
      Since:
      2.0.0
    • lookupAll

      protected Set<Node> lookupAll(String selector, Set<Node> results)
      Used by Node and Parent to traverse the scene graph to find all nodes that match the given selector.
      Parameters:
      selector - the selector
      results - the results
      Returns:
      a set of nodes that match the selector; null if none is found
      Since:
      2.0.0
    • requestParentLayout

      public void requestParentLayout()
      Requests a layout update for this node's parent. Layout will be applied on the next layout pass.
      Since:
      2.0.0
    • getLayoutX

      public int getLayoutX()
      Returns the x coordinate of the node's origin. The origin is the top-left corner of the node.
      Returns:
      the x coordinate of the node's origin
      Since:
      1.0.0
    • setLayoutX

      public void setLayoutX(int layoutX)
      Sets the x coordinate of the node's origin. The origin is the top-left corner of the node.
      Parameters:
      layoutX - the x coordinate of the node's origin
      Since:
      1.0.0
    • getLayoutY

      public int getLayoutY()
      Returns the y coordinate of the node's origin. The origin is the top-left corner of the node.
      Returns:
      the y coordinate of the node's origin
      Since:
      1.0.0
    • setLayoutY

      public void setLayoutY(int layoutY)
      Sets the y coordinate of the node's origin. The origin is the top-left corner of the node.
      Parameters:
      layoutY - the y coordinate of the node's origin
      Since:
      1.0.0
    • relocate

      public void relocate(int layoutX, int layoutY)
      Moves the node to the specified location. This is equivalent to setting the layoutX and layoutY properties.
      Parameters:
      layoutX - the x coordinate of the node's origin
      layoutY - the y coordinate of the node's origin
      Since:
      1.0.0
    • getTranslateX

      public int getTranslateX()
      Returns the x translation of the node. The translation is applied after the layout has been computed, but before the node is rendered.
      Returns:
      the x translation of the node
      Since:
      1.0.0
    • setTranslateX

      public void setTranslateX(int translateX)
      Sets the x translation of the node. The translation is applied after the layout has been computed, but before the node is rendered.
      Parameters:
      translateX - the x translation of the node
      Since:
      1.0.0
    • getTranslateY

      public int getTranslateY()
      Returns the y translation of the node. The translation is applied after the layout has been computed, but before the node is rendered.
      Returns:
      the y translation of the node
      Since:
      1.0.0
    • setTranslateY

      public void setTranslateY(int translateY)
      Sets the y translation of the node. The translation is applied after the layout has been computed, but before the node is rendered.
      Parameters:
      translateY - the y translation of the node
      Since:
      1.0.0
    • getWidth

      public abstract int getWidth()
      Returns the width of the node.
      Returns:
      the width of the node
      Since:
      1.0.0
    • getHeight

      public abstract int getHeight()
      Returns the height of the node.
      Returns:
      the height of the node
      Since:
      1.0.0
    • isVisible

      public boolean isVisible()
      Returns whether the node is visible. If a node is not visible, it will not be rendered, but it will still be considered for layout calculations.
      Returns:
      whether the node is visible
      Since:
      1.0.0
    • setVisible

      public void setVisible(boolean visible)
      Sets whether the node is visible. If a node is not visible, it will not be rendered, but it will still be considered for layout calculations.
      Parameters:
      visible - whether the node is visible
      Since:
      1.0.0
    • isClipping

      public boolean isClipping()
      Returns whether the node is clipping its children. If a node is clipping its children, children that are outside the bounds of the node will not be rendered.
      Returns:
      whether the node is clipping its children
      Since:
      1.0.0
    • setClipping

      public void setClipping(boolean clipping)
      Sets whether the node is clipping its children. If a node is clipping its children, children that are outside the bounds of the node will not be rendered.
      Parameters:
      clipping - whether the node is clipping its children
      Since:
      1.0.0
    • getOnClick

      public java.util.function.Consumer<NodeClickContext> getOnClick()
      Returns the consumer that is called when the node is clicked.

      This consumer will be called when the node is clicked, regardless of the type of click.

      Returns:
      the consumer that is called when the node is clicked
      Since:
      1.0.0
    • setOnClick

      public void setOnClick(java.util.function.Consumer<NodeClickContext> onClick)
      Sets the consumer that is called when the node is clicked.

      This consumer will be called when the node is clicked, regardless of the type of click.

      Parameters:
      onClick - the consumer that is called when the node is clicked
      Since:
      1.0.0
    • getOnLeftClick

      public java.util.function.Consumer<NodeClickContext> getOnLeftClick()
      Returns the consumer that is called when the node is left-clicked.
      Returns:
      the consumer that is called when the node is left-clicked
      Since:
      1.0.0
    • setOnLeftClick

      public void setOnLeftClick(java.util.function.Consumer<NodeClickContext> onLeftClick)
      Sets the consumer that is called when the node is left-clicked.
      Parameters:
      onLeftClick - the consumer that is called when the node is left-clicked
      Since:
      1.0.0
    • getOnRightClick

      public java.util.function.Consumer<NodeClickContext> getOnRightClick()
      Returns the consumer that is called when the node is right-clicked.
      Returns:
      the consumer that is called when the node is right-clicked
      Since:
      1.0.0
    • setOnRightClick

      public void setOnRightClick(java.util.function.Consumer<NodeClickContext> onRightClick)
      Sets the consumer that is called when the node is right-clicked.
      Parameters:
      onRightClick - the consumer that is called when the node is right-clicked
      Since:
      1.0.0
    • getOnShiftClick

      public java.util.function.Consumer<NodeClickContext> getOnShiftClick()
      Returns the consumer that is called when the node is shift-clicked.
      Returns:
      the consumer that is called when the node is shift-clicked
      Since:
      1.0.0
    • setOnShiftClick

      public void setOnShiftClick(java.util.function.Consumer<NodeClickContext> onShiftClick)
      Sets the consumer that is called when the node is shift-clicked.
      Parameters:
      onShiftClick - the consumer that is called when the node is shift-clicked
      Since:
      1.0.0
    • getOnHotBarClick

      public java.util.function.Consumer<NodeClickContext> getOnHotBarClick()
      Returns the consumer that is called when the node receives a hot bar click.
      Returns:
      the consumer that is called when the node receives a hot bar click
      Since:
      1.0.0
    • setOnHotBarClick

      public void setOnHotBarClick(java.util.function.Consumer<NodeClickContext> onHotBarClick)
      Sets the consumer that is called when the node receives a hot bar click.
      Parameters:
      onHotBarClick - the consumer that is called when the node receives a hot bar click
      Since:
      1.0.0
    • renderPixelAt

      public abstract Pixel renderPixelAt(int x, int y)
      Returns a Pixel that should be rendered at the given coordinates. The coordinates are relative to this parent's bounds.
      Parameters:
      x - the x coordinate
      y - the y coordinate
      Returns:
      the pixel at the given coordinates, or null if there is no pixel at the given coordinates
      Since:
      2.0.0
    • nodeAt

      public abstract Node nodeAt(int x, int y)
      Returns the node at the given coordinates. The coordinates are relative to this parent's bounds.
      Parameters:
      x - the x coordinate
      y - the y coordinate
      Returns:
      the node at the given coordinates, or null if there is no node at the given coordinates
      Since:
      1.0.0