Class UIElement

java.lang.Object
net.risingworld.api.ui.UIElement
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
UILabel, UIMesh, UIPainter2D, UITextField

public class UIElement extends Object implements Serializable
Base UI element. It may act as container, button, panel etc. Other UI elements (like text fields or labels) are derived from this class.
The visual appearance of the element is described by an Style object. There is also a separate hover Style to define a separate appearance whenever the element is hovered with the mouse.
See Also:
  • Field Details

    • style

      public final Style style
      The style of this element. After changing it, remember to call updateStyle(), so the changes are synced with the player
    • hoverStyle

      public final Style hoverStyle
      The hover style of this element. Becomes active when hovering this element with the cursor, but only if setClickable(boolean) was set to true. After changing the style, remember to call updateStyle(), so the changes are synced with the player
  • Constructor Details

    • UIElement

      public UIElement()
  • Method Details

    • getID

      public int getID()
      Gets the unique ID of this element.
      Returns:
      the unique ID of this element.
    • updateStyle

      public void updateStyle()
      Synchronize pending style changes with all registered players. Call this method if you've changed the style of the element directly
    • addStyleSheet

      public void addStyleSheet(StyleSheetAsset styleSheetAsset)
      Adds an optional style sheet asset (USS). They're similar to CSS and describe the Style of a UI element. Style Sheets can be created in Unitys UI Builder (UI Toolkit).
      If you don't want to load a USS style sheet from file, you can ignore this method.
      Parameters:
      styleSheetAsset - the style sheet asset.
    • removeStyleSheet

      public void removeStyleSheet(StyleSheetAsset styleSheetAsset)
    • addToClassList

      public void addToClassList(String className)
    • removeFromClassList

      public void removeFromClassList(String className)
    • setPosition

      public void setPosition(float x, float y, boolean percent)
      Sets the screen position of this element. By default, the position coordinate is at the upper left of the element.

      Important: Absolute positions are always in relation to fullHD resolution (1920x1080), irrespective of the actual screen resolution. The game then automatically updates the element position depending on the screen resolution.

      Note: This is just a helper method. Internally it only changes the style left, top and position values!
      Parameters:
      x - the x (horizontal) position.
      y - the y (vertical) position.
      percent - set to false to use pixel values, or true to use percent values (0-100)
    • setPivot

      public void setPivot(Pivot pivot)
      Changes the origin of this element.

      Note: This is just a helper method. Internally it only changes the style transformOrigin and translate values!
      Parameters:
      pivot - the new pivot/origin you want to set.
    • resetPosition

      public void resetPosition()
      Resets the position, i.e internally this resets the left, bottom, top and right style parameters.
    • resetPivot

      public void resetPivot()
      Resets the pivot setting, i.e internally this resets the transformOrigin and translate style parameters.
    • setSize

      public void setSize(float width, float height, boolean percent)
      Sets the size (width and height) of this element.

      Important: Absolute positions are always in relation to fullHD resolution (1920x1080), irrespective of the actual screen resolution. The game then automatically updates the element position and size depending on the screen resolution.

      Note: This is just a helper method. Internally it only changes the style width and height values!
      Parameters:
      width - the horizontal width (x).
      height - the vertial height (y).
      percent - set to false to use pixel values, or true to use percent values (0-100)
    • setBorder

      public void setBorder(float thickness)
      Sets the border width/thickness of this element. Please bear in mind that the border expands to the inner of the element, which also shifts/shrinks the content of the element.

      Note: This is just a helper method. Internally it only changes the style borderLeftWidth, borderRightWidth, borderTopWidth and borderBottomWidth values!
      Parameters:
      thickness - the new width/thickness of the border (in pixels).
    • setBorderColor

      public void setBorderColor(int rgba)
    • setBorderEdgeRadius

      public void setBorderEdgeRadius(float radius, boolean percent)
      Sets the radius of the element border corners. Use this to create rounded edges. For example, if you set the radius to half the element width/height, you will get a fully round element. Set the radius to 0 to get sharp edges (default).

      Note: This is just a helper method. Internally it only changes the style borderTopLeftRadius, borderTopRightRadius, borderBottomLeftRadius and borderBottomRightRadius values!
      Parameters:
      radius - the radius of the edge.
      percent - set to false to use pixel values, or true to use percent values (0-100).
    • setBackgroundColor

      public void setBackgroundColor(float r, float g, float b, float a)
      Sets the background color of the element.

      Note: This is just a helper method. Internally it only changes the style backgroundColor value!
      Parameters:
      r - the new red value (0-1).
      g - the new green value (0-1).
      b - the new blue value (0-1).
      a - the new alpha/opacity value (0-1).
    • setBackgroundColor

      public void setBackgroundColor(int rgba)
      Sets the background color of the element.

      Note: This is just a helper method. Internally it only changes the style backgroundColor value!
      Parameters:
      rgba - an int RGBA color code.
    • setOpacity

      public void setOpacity(float opacity)
      Sets the opacity of this element. Set to 0 to make the element fully invisible, and 1 to make it fully opaque.

      Note: This is just a helper method. Internally it only changes the style opacity value!
      Parameters:
      opacity - the new opacity (0-1).
    • setVisible

      public void setVisible(boolean visible)
      Changes visibility of this element. When setting to false, the element still takes up space in the parent layout. To prevent that, change Style.display

      Note: This is just a helper method. Internally it only changes the style visibility value!
      Parameters:
      visible - true to make the element visible (default), false to hide it.
    • setClickable

      public void setClickable(boolean clickable)
      Sets this element clickable, i.e the element listens for mouse clicks and triggers a PlayerUIElementClickEvent event in this case (this also invokes the onClick(net.risingworld.api.events.player.ui.PlayerUIElementClickEvent) method).
      Parameters:
      clickable - true to let this element listen for mouse clicks, false if not
      See Also:
    • isClickable

      public boolean isClickable()
    • setPickable

      public void setPickable(boolean pickable)
      Not to be confused with setClickable(boolean)! This flag determines if the UI element "has collision", i.e if it consumes or "blocks" mouse clicks. By default, this is enabled. If you set it to false, mouse clicks will ignore this element, so you could click "through" this element (and click an element behind it, for example).
      Parameters:
      pickable - true to block mouse clicks (default behaviour), false to let mouse clicks pass through
    • addChild

      public void addChild(UIElement child)
      Attaches an UIElement as "child" to this element. The child position and style will then depend on the new parent. If you make any changes to the parent (i.e move it, resize it, change visibility), it will affect all childs automatically.
      By default, elements have no parents. If an element has no parent, the screen is considered the parent.

      Note: It's not necessary to add child elements to the player UI manually, because the game handles this automatically.
      Parameters:
      child - the element you want to attach as child to this element.
      Example: Create a label (as child) in front of a panel (parent)
      1//Create the "parent" element
      2UIElement panel = new UIElement();
      3panel.setPosition(100, 500, false);
      4panel.setSize(400, 300, false);
      5
      6//Change color (transparent gray)
      7panel.setBackgroundColor(0.5f, 0.5f, 0.5f, 0.5f);
      8
      9//Add panel to player (only necessary for parent element!)
      10player.addUIElement(panel);
      11
      12//Now create a label exactly in the center of the panel
      13//Since the label will be a child, the position is relative to the parent position!
      14UILabel label = new UILabel("I'm a child element!");
      15label.setPosition(50, 50, true);
      16
      17//Select "Center" pivot
      18label.setPivot(Pivot.MiddleCenter);
      19
      20//Change font size, for example
      21label.setFontSize(42);
      22
      23//Add label as child element to "panel" parent
      24panel.addChild(label);
    • removeChild

      public void removeChild(UIElement child)
      Removes a child from this element. It will also be detached from the player UI.
      Parameters:
      child - the child element you want to remove.
    • removeAllChilds

      public void removeAllChilds()
      Removes all children from this element. They will also be detached from the player UI.
    • getParent

      public UIElement getParent()
      Gets the current parent UI element, or null if this element has no parent.
      Returns:
      the parent element, or null if no parent is set.
    • removeFromParent

      public void removeFromParent()
      Removes this element from its parent. Same as calling removeChild(net.risingworld.api.ui.UIElement) on the parent element.
    • getChilds

      public List<UIElement> getChilds()
      Gets an unmodifiable list of all children of this element.
      Returns:
      a new, unmodifiable list containing all child elements.
    • getChildCount

      public int getChildCount()
      Gets the number of attached child elements on this element.
      Returns:
      the amount of children this element has. 0 if this element has no child elements.
    • onClick

      public void onClick(PlayerUIElementClickEvent event)
      Override this method if you want to get the click event (when a player clicks on the element) directly, without having to register an event listener.
      Parameters:
      event - the UI click event.
      Example:
      1//Create a label as "anonymous class", i.e override the onClick()
      2//method during initialization. If the player clicks on the label, both
      3//the "PlayerUIElementClickEvent" and the "onClick()"-method will be called
      4UILabel label = new UILabel("Click me!"){
      5 @Override
      6 public void onClick(PlayerUIElementClickEvent event){
      7 //Do something, e.g send text message
      8 event.getPlayer().sendTextMessage("You've clicked on a label!");
      9
      10 //Remove the label from player UI if you want
      11 //Since we're "inside" the UILabel class, this represents the actual label instance
      12 event.getPlayer().removeUIElement(this);
      13 }
      14};
      15
      16//Set label position and size
      17label.setPosition(50, 50, true);
      18label.setSize(100, 50, false);
      19
      20//Set label clickable
      21label.setClickable(true);
      22
      23//Remember to add label to player UI
      24player.addUIElement(label);
      See Also: