Class UIComponent

All Implemented Interfaces:
UIKeyHandler, Hintable, Savable
Direct Known Subclasses:
StateBasedUIComponent, UIContainer, UIState

public abstract class UIComponent extends Node implements UIKeyHandler
Base UI class. All UI components/widgets/controls extend this class. TODO: alert/dirty needed for font style changes.
  • Field Details

    • DEFAULT_FOREGROUND_COLOR

      public static ReadOnlyColorRGBA DEFAULT_FOREGROUND_COLOR
      White
    • _tooltipText

      protected String _tooltipText
      Text to display using UITooltip when hovered over. Inherited from parent if null.
    • _tooltipPopTime

      protected int _tooltipPopTime
      The amount of time (in ms) before we should show the tooltip on this component.
  • Constructor Details

    • UIComponent

      public UIComponent()
  • Method Details

    • applySkin

      protected void applySkin()
    • isEnabled

      public boolean isEnabled()
      Returns:
      true if this component should be considered "enabled"... a concept that is interpreted by each individual component type.
    • setEnabled

      public void setEnabled(boolean enabled)
      Parameters:
      enabled - true if this component should be considered "enabled"... a concept that is interpreted by each individual component type.
    • isVisible

      public boolean isVisible()
      Returns:
      true if this component should be drawn.
    • setVisible

      public void setVisible(boolean visible)
      Parameters:
      visible - true if this component should be drawn.
    • hasVirginContentArea

      public boolean hasVirginContentArea()
      Used primarily during rendering to determine how alpha blending should be done.
      Returns:
      true if nothing has been drawn by this component or its ancestors yet that would affect its content area.
    • setVirginContentArea

      public void setVirginContentArea(boolean virgin)
      Parameters:
      virgin - true if nothing has been drawn by this component yet that would affect its content area.
    • addFontStyle

      public void addFontStyle(String styleKey, Object styleValue)
      Add a new font style to this component. Will be inherited by children if they do not have the same key.
      Parameters:
      styleKey - style key
      styleValue - the value to associate with the key.
    • clearFontStyle

      public Object clearFontStyle(String styleKey)
      Removes a font style locally from this component, if present.
      Parameters:
      styleKey - style key
      Returns:
      the style value we were mapped to, or null if none (or was mapped to null).
    • getLocalFontStyles

      public Map<String,Object> getLocalFontStyles()
      Returns:
      the locally set font styles.
    • getFontStyles

      public Map<String,Object> getFontStyles()
      Returns:
      our combined font styles, merged downward from root, including defaults.
    • setFontStyles

      public void setFontStyles(Map<String,Object> styles)
      Parameters:
      styles - the font styles to use (as needed) for this component. Note that this can be inherited by child components if this is a container class and styles is null.
    • getLayoutData

      public UILayoutData getLayoutData()
      Returns:
      the set layout data object or null if none has been set.
    • setLayoutData

      public void setLayoutData(UILayoutData layoutData)
      Parameters:
      layoutData - the layout data object set on this component. The object would provide specific layout directions to the layout class of the container this component is placed in.
    • getLocalComponentWidth

      public int getLocalComponentWidth()
      Returns:
      the width of this entire component as a whole, including all margins, borders, padding and content (in the component's coordinate space.)
    • getLocalComponentHeight

      public int getLocalComponentHeight()
      Returns:
      the height of this entire component as a whole, including all margins, borders, padding and content (in the component's coordinate space.)
    • getRelativeMinComponentBounds

      public Rectangle2 getRelativeMinComponentBounds(Rectangle2 store)
      Parameters:
      store - the object to store our results in. If null, a new Rectangle2 is created and returned.
      Returns:
      the relative minimum component bounds
    • getRelativeMaxComponentBounds

      public Rectangle2 getRelativeMaxComponentBounds(Rectangle2 store)
      Parameters:
      store - the object to store our results in. If null, a new Rectangle2 is created and returned.
      Returns:
      the relative maximum component bounds
    • getRelativeComponentBounds

      public Rectangle2 getRelativeComponentBounds(Rectangle2 store)
      Parameters:
      store - the object to store our results in. If null, a new Rectangle2 is created and returned.
      Returns:
      the current bounds of this component, in the coordinate space of its parent.
    • setLocalComponentSize

      public void setLocalComponentSize(int width, int height)
      Sets the width and height of this component by forcing the content area to be of a proper size such that when the padding, margin and border are added, the total component size match those given.
      Parameters:
      width - the new width of the component
      height - the new height of the component
    • getMinimumContentWidth

      protected int getMinimumContentWidth()
    • getMinimumContentHeight

      protected int getMinimumContentHeight()
    • getMinimumLocalComponentWidth

      public int getMinimumLocalComponentWidth()
      Returns:
      the width contained in _minimumContentsSize + the margin, border and padding values for left and right.
    • getMinimumLocalComponentHeight

      public int getMinimumLocalComponentHeight()
      Returns:
      the height contained in _minimumContentsSize + the margin, border and padding values for top and bottom.
    • getMaximumContentWidth

      protected int getMaximumContentWidth()
    • getMaximumContentHeight

      protected int getMaximumContentHeight()
    • getMaximumLocalComponentWidth

      public int getMaximumLocalComponentWidth()
      Returns:
      the width contained in _maximumContentsSize + the margin, border and padding values for left and right.
    • getMaximumLocalComponentHeight

      public int getMaximumLocalComponentHeight()
      Returns:
      the height contained in _maximumContentsSize + the margin, border and padding values for top and bottom.
    • setContentSize

      public void setContentSize(int width, int height)
      Sets the width and height of the content area of this component.
      Parameters:
      width - the new width of the content area
      height - the new height of the content area
    • setContentHeight

      public void setContentHeight(int height)
      Sets the height of the content area of this component to that given, as long as we're between min and max content height.
      Parameters:
      height - the new height
    • setContentWidth

      public void setContentWidth(int width)
      Sets the width of the content area of this component to that given, as long as we're between min and max content width.
      Parameters:
      width - the new width
    • setLocalComponentHeight

      public void setLocalComponentHeight(int height)
      Sets the current component height to that given, as long as it would not violate min and max content height.
      Parameters:
      height - the new height
    • setLocalComponentWidth

      public void setLocalComponentWidth(int width)
      Sets the current component width to that given, as long as it would not violate min and max content width.
      Parameters:
      width - the new width
    • getContentWidth

      public int getContentWidth()
      Returns:
      the width of the content area of this component.
    • getContentHeight

      public int getContentHeight()
      Returns:
      the height of the content area of this component.
    • clearMaximumContentSize

      public void clearMaximumContentSize()
    • setMaximumContentSize

      public void setMaximumContentSize(int width, int height)
      Sets the maximum content size of this component to the values given.
      Parameters:
      width - the width
      height - the height
    • setMaximumContentWidth

      public void setMaximumContentWidth(int width)
      Sets the maximum content width of this component to the value given.
      Parameters:
      width - the width
    • setMaximumContentHeight

      public void setMaximumContentHeight(int height)
      Sets the maximum content height of this component to the value given.
      Parameters:
      height - the height
    • setLayoutMinimumContentSize

      public void setLayoutMinimumContentSize(int width, int height)
      Sets the minimum content size of this component to the values given.
      Parameters:
      width - the width
      height - the height
    • clearMinimumContentSize

      public void clearMinimumContentSize()
    • setMinimumContentSize

      public void setMinimumContentSize(int width, int height)
      Sets the minimum content size of this component to the values given.
      Parameters:
      width - the width
      height - the height
    • setMinimumContentWidth

      public void setMinimumContentWidth(int width)
      Sets the minimum content width of this component to the value given.
      Parameters:
      width - the width
    • setMinimumContentHeight

      public void setMinimumContentHeight(int height)
      Sets the minimum content height of this component to the value given.
      Parameters:
      height - the height
    • isConsumeKeyEvents

      public boolean isConsumeKeyEvents()
    • setConsumeKeyEvents

      public void setConsumeKeyEvents(boolean consume)
    • isConsumeMouseEvents

      public boolean isConsumeMouseEvents()
    • setConsumeMouseEvents

      public void setConsumeMouseEvents(boolean consume)
    • validateContentSize

      protected void validateContentSize()
      Ensures content size is between min and max.
    • compact

      public void compact()
      Sets the size of the content area of this component to the current width and height set on _minimumContentsSize (if component is set to allow such resizing.)
    • pack

      public void pack()
      Resize the container to fit the minimum size of its content panel.
    • fitComponentIn

      public void fitComponentIn(int width, int height)
      Attempt to force this component to fit in the given rectangle.
      Parameters:
      width - the width
      height - the height
    • layout

      public void layout()
      Override this to perform actual layout.
    • getTotalBottom

      public int getTotalBottom()
      Returns:
      the sum of the bottom side of this component's margin, border and padding (if they are set).
    • getTotalTop

      public int getTotalTop()
      Returns:
      the sum of the top side of this component's margin, border and padding (if they are set).
    • getTotalLeft

      public int getTotalLeft()
      Returns:
      the sum of the left side of this component's margin, border and padding (if they are set).
    • getTotalRight

      public int getTotalRight()
      Returns:
      the sum of the right side of this component's margin, border and padding (if they are set).
    • getBorder

      public UIBorder getBorder()
      Returns:
      the current border set on this component, if any.
    • setBorder

      public void setBorder(UIBorder border)
      Parameters:
      border - the border we wish this component to use. May be null.
    • getBackdrop

      public UIBackdrop getBackdrop()
      Returns:
      the current backdrop set on this component, if any.
    • setBackdrop

      public void setBackdrop(UIBackdrop backDrop)
      Parameters:
      backDrop - the backdrop we wish this component to use. May be null.
    • getMargin

      public Insets getMargin()
      Returns:
      the current margin set on this component, if any.
    • setMargin

      public void setMargin(Insets margin)
      Parameters:
      margin - the new margin (a spacing between the component's border and other components) to set on this component. Copied into the component and is allowed to be null.
    • getPadding

      public Insets getPadding()
      Returns:
      the current margin set on this component, if any.
    • setPadding

      public void setPadding(Insets padding)
      Parameters:
      padding - the new padding (a spacing between the component's border and its inner content area) to set on this component. Copied into the component and is allowed to be null.
    • isAttachedToHUD

      public boolean isAttachedToHUD()
      Returns:
      true if our parent is a UIHud.
    • getTopLevelComponent

      public UIComponent getTopLevelComponent()
      Returns:
      the first instance of UIComponent found in this Component's UIComponent ancestry that is attached to the hud, or null if none are found. Returns "this" component if it is directly attached to the hud.
    • getHud

      public UIHud getHud()
      Returns:
      the first instance of UIHud found in this Component's UIComponent ancestry or null if none are found.
    • attachedToHud

      public void attachedToHud()
      Override to provide an action to take when this component or its top level component are attached to a UIHud.
    • detachedFromHud

      public void detachedFromHud()
      Override to provide an action to take just before this component or its top level component are removed from a UIHud.
    • centerOn

      public void centerOn(UIHud hud)
      Centers this frame on the view of the camera
      Parameters:
      hud - the hud to center on.
    • centerOn

      public void centerOn(UIComponent comp)
      Centers this component on the location of the given component.
      Parameters:
      comp - the component to center on.
    • getHudX

      public int getHudX()
      Returns:
      current screen x coordinate of this component's origin (usually its lower left corner.)
    • getHudY

      public int getHudY()
      Returns:
      current screen y coordinate of this component's origin (usually its lower left corner.)
    • setHudXY

      public void setHudXY(int x, int y)
      Sets the screen x,y coordinate of this component's origin (usually its lower left corner.)
      Parameters:
      x - the new screen x coordinate of this component's origin (usually its lower left corner.)
      y - the new screen y coordinate of this component's origin (usually its lower left corner.)
    • setHudX

      public void setHudX(int x)
      Parameters:
      x - the new screen x coordinate of this component's origin (usually its lower left corner.)
    • setHudY

      public void setHudY(int y)
      Parameters:
      y - the new screen y coordinate of this component's origin (usually its lower left corner.)
    • getLocalX

      public int getLocalX()
      Returns:
      a local x translation from the parent component's content area.
    • getLocalY

      public int getLocalY()
      Returns:
      a local y translation from the parent component's content area.
    • setLocalXY

      public void setLocalXY(int x, int y)
      Set the x,y translation from the lower left corner of our parent's content area to the origin of this component.
      Parameters:
      x - the local x translation from the parent component's content area
      y - the local y translation from the parent component's content area
    • setLocalX

      public void setLocalX(int x)
      Set the x translation from the lower left corner of our parent's content area to the origin of this component.
      Parameters:
      x - the local x translation from the parent component's content area
    • setLocalY

      public void setLocalY(int y)
      Set the y translation from the lower left corner of our parent's content area to the origin of this component.
      Parameters:
      y - the local y translation from the parent component's content area
    • getLocalForegroundColor

      public ReadOnlyColorRGBA getLocalForegroundColor()
      Returns:
      the currently set foreground color on this component. Does not inherit from ancestors or default, so may return null.
    • getForegroundColor

      public ReadOnlyColorRGBA getForegroundColor()
      Returns:
      the foreground color associated with this component. If none has been set, we will ask our parent component and so on. If no component is found in our ancestry with a foreground color, we will use DEFAULT_FOREGROUND_COLOR
    • setForegroundColor

      public void setForegroundColor(ReadOnlyColorRGBA color)
      Parameters:
      color - the foreground color of this component.
    • getTooltipText

      public String getTooltipText()
      Returns:
      this component's tooltip text. If none has been set, we will ask our parent component and so on. returns null if no tooltips are found.
    • setTooltipText

      public void setTooltipText(String text)
      Parameters:
      text - the tooltip text of this component.
    • getTooltipPopTime

      public int getTooltipPopTime()
      Returns:
      the amount of time in ms to wait before showing a tooltip for this component.
    • setTooltipPopTime

      public void setTooltipPopTime(int ms)
      Parameters:
      ms - the amount of time in ms to wait before showing a tooltip for this component. This is only granular to a tenth of a second (or 100ms)
    • cancelTooltipTimer

      protected void cancelTooltipTimer()
      Check if our tooltip timer is active and cancel it.
    • insideMargin

      public boolean insideMargin(int hudX, int hudY)
      Parameters:
      hudX - the x screen coordinate
      hudY - the y screen coordinate
      Returns:
      true if the given screen coordinates fall inside the margin area of this component (or in other words, is at the border level or deeper.)
    • getUIComponent

      public UIComponent getUIComponent(int hudX, int hudY)
      Parameters:
      hudX - the x screen coordinate
      hudY - the y screen coordinate
      Returns:
      this component (or an appropriate child coordinate in the case of a container) if the given screen coordinates fall inside the margin area of this component.
    • updateWorldTransform

      public void updateWorldTransform(boolean recurse)
      Description copied from class: Spatial
      Updates the worldTransform.
      Overrides:
      updateWorldTransform in class Node
      Parameters:
      recurse - usually false when updating the tree. Set to true when you just want to update the world transforms for a branch without updating geometric state.
    • updateWorldTransform

      protected void updateWorldTransform(boolean recurse, boolean self)
      Allow skipping updating our own world transform.
      Parameters:
      recurse - true if done recursively on all its children
      self - true if applied on itself
    • draw

      public void draw(Renderer r)
      Description copied from class: Node
      draw calls the onDraw method for each child maintained by this node.
      Overrides:
      draw in class Node
      Parameters:
      r - the renderer to draw to.
      See Also:
    • setDefaultFontFamily

      public static void setDefaultFontFamily(String family)
      Parameters:
      family - the Font to use as "default" across all UI components that do not have one set.
    • getDefaultFontFamily

      public static String getDefaultFontFamily()
      Returns:
      the default Font family. Defaults to "Vera".
    • setDefaultFontSize

      public static void setDefaultFontSize(int size)
      Parameters:
      size - the Font size to use as "default" across all UI components that do not have one set.
    • getDefaultFontSize

      public static int getDefaultFontSize()
      Returns:
      the default Font size (height). Defaults to 18.
    • setDefaultFontStyles

      public static void setDefaultFontStyles(Map<String,Object> defaultStyles)
      Parameters:
      defaultStyles - the Font styles to use as "default" across all UI components that do not have one set.
    • getDefaultFontStyles

      public static Map<String,Object> getDefaultFontStyles()
      Returns:
      the default Font styles.
    • getCombinedOpacity

      public float getCombinedOpacity()
      Returns:
      the opacity level set on this component in [0,1], where 0 means completely transparent and 1 is completely opaque. If useTransparency is false, this will always return 1.
    • getLocalOpacity

      public float getLocalOpacity()
      Returns:
      the opacity set on this component directly, not accounting for parent opacity levels.
    • setOpacity

      public void setOpacity(float opacity)
      Set the opacity level of this component.
      Parameters:
      opacity - value in [0,1], where 0 means completely transparent and 1 is completely opaque.
    • fireComponentDirty

      public void fireComponentDirty()
      Tell all ancestors that use standins, if any, that they need to update any cached graphical representation.
    • fireStyleChanged

      public void fireStyleChanged()
      Let subcomponents know that style has been changed.
    • isUseTransparency

      public static boolean isUseTransparency()
      Returns:
      true if all components should use their opacity value to blend against other components (and/or the 3d background scene.)
    • setUseTransparency

      public static void setUseTransparency(boolean useTransparency)
      Parameters:
      useTransparency - true if all components should use their opacity value to blend against the 3d scene (and each other)
    • getCurrentOpacity

      public static float getCurrentOpacity()
      Returns:
      the currently rendering component's opacity level. Used by the renderer to alter alpha values based of component elements.
    • updateMinimumSizeFromContents

      protected void updateMinimumSizeFromContents()
      Ask this component to update its minimum allowed size, based on its contents.
    • predrawComponent

      protected void predrawComponent(Renderer renderer)
      Perform any pre-draw operations on this component.
      Parameters:
      renderer - the renderer
    • postdrawComponent

      protected void postdrawComponent(Renderer renderer)
      Perform any post-draw operations on this component.
      Parameters:
      renderer - the renderer
    • drawComponent

      protected void drawComponent(Renderer renderer)
      Draw this component's contents using the given renderer.
      Parameters:
      renderer - the renderer
    • mouseEntered

      public void mouseEntered(int mouseX, int mouseY, InputState state)
      Called when a mouse cursor enters this component.
      Parameters:
      mouseX - mouse x coordinate.
      mouseY - mouse y coordinate.
      state - the current tracked state of the input system.
    • mouseDeparted

      public void mouseDeparted(int mouseX, int mouseY, InputState state)
      Called when a mouse cursor leaves this component.
      Parameters:
      mouseX - mouse x coordinate.
      mouseY - mouse y coordinate.
      state - the current tracked state of the input system.
    • mousePressed

      public boolean mousePressed(MouseButton button, InputState state)
      Called when a mouse button is pressed while the cursor is over this component.
      Parameters:
      button - the button that was pressed
      state - the current tracked state of the input system.
      Returns:
      true if we want to consider the event "consumed" by the UI system.
    • mouseReleased

      public boolean mouseReleased(MouseButton button, InputState state)
      Called when a mouse button is released while the cursor is over this component.
      Parameters:
      button - the button that was released
      state - the current tracked state of the input system.
      Returns:
      true if we want to consider the event "consumed" by the UI system.
    • mouseClicked

      public boolean mouseClicked(MouseButton button, InputState state)
      Called when a mouse button is pressed and released in close proximity while the cursor is over this component.
      Parameters:
      button - the button that was released
      state - the current tracked state of the input system.
      Returns:
      true if we want to consider the event "consumed" by the UI system.
    • mouseMoved

      public boolean mouseMoved(int mouseX, int mouseY, InputState state)
      Called when a mouse is moved while the cursor is over this component.
      Parameters:
      mouseX - mouse x coordinate.
      mouseY - mouse y coordinate.
      state - the current tracked state of the input system.
      Returns:
      true if we want to consider the event "consumed" by the UI system.
    • mouseWheel

      public boolean mouseWheel(int wheelDx, InputState state)
      Called when the mouse wheel is moved while the cursor is over this component.
      Parameters:
      wheelDx - the last change of the wheel
      state - the current tracked state of the input system.
      Returns:
      true if we want to consider the event "consumed" by the UI system.
    • keyPressed

      public boolean keyPressed(Key key, InputState state)
      Called when this component has focus and a key is pressed.
      Specified by:
      keyPressed in interface UIKeyHandler
      Parameters:
      key - the key pressed.
      state - the current tracked state of the input system.
      Returns:
      true if we want to consider the event "consumed" by the UI system.
    • keyHeld

      public boolean keyHeld(Key key, InputState state)
      Called when this component has focus and a key is held down over more than 1 input cycle.
      Specified by:
      keyHeld in interface UIKeyHandler
      Parameters:
      key - the key held.
      state - the current tracked state of the input system.
      Returns:
      true if we want to consider the event "consumed" by the UI system.
    • keyReleased

      public boolean keyReleased(Key key, InputState state)
      Called when this component has focus and a key is released.
      Specified by:
      keyReleased in interface UIKeyHandler
      Parameters:
      key - the key released.
      state - the current tracked state of the input system.
      Returns:
      true if we want to consider the event "consumed" by the UI system.
    • gainedFocus

      public void gainedFocus()
      Called by the hud when a component is given focus.
    • lostFocus

      public void lostFocus()
      Called by the hud when a component loses focus.
    • requestFocus

      public void requestFocus()
      Looks up the scenegraph for a Hud and asks it to set us as the currently focused component.
    • getKeyFocusTarget

      public UIComponent getKeyFocusTarget()
      Returns:
      a component we defer to for key focus. Default is null.
    • setKeyFocusTarget

      public void setKeyFocusTarget(UIComponent target)
      Parameters:
      target - a component to set as the actual focused component if this component receives focus.