Package de.gurkenlabs.litiengine.gui
Class GuiComponent
java.lang.Object
de.gurkenlabs.litiengine.gui.GuiComponent
- All Implemented Interfaces:
IRenderable
,Tweenable
,java.awt.event.MouseListener
,java.awt.event.MouseMotionListener
,java.awt.event.MouseWheelListener
,java.util.EventListener
- Direct Known Subclasses:
DropdownListField
,ImageComponent
,ImageComponentList
,ListField
,Screen
,Slider
public abstract class GuiComponent
extends java.lang.Object
implements java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.awt.event.MouseWheelListener, IRenderable, Tweenable
The abstract Class GuiComponent provides all properties and methods needed for screens, built-in, and custom GUI components such as buttons,
sliders, etc... It includes mouse event handling, different hovering states and appearances, and texts to be rendered.
Field Summary
Fields Modifier and Type Field Description protected static java.awt.Font
ICON_FONT
Constructor Summary
Constructors Modifier Constructor Description protected
GuiComponent(double x, double y)
Instantiates a new gui component with the dimension (0,0) at the given location.protected
GuiComponent(double x, double y, double width, double height)
Instantiates a new gui component at the point (x,y) with the dimension (width,height).Method Summary
Modifier and Type Method Description boolean
drawTextShadow()
Draw text shadow.Appearance
getAppearance()
Gets the default appearance object for this GuiComponent.Appearance
getAppearanceDisabled()
Gets the appearance object for this GuiComponent while disabled.Appearance
getAppearanceHovered()
Gets the appearance object for this GuiComponent while hovered.java.awt.geom.Rectangle2D
getBoundingBox()
Gets the bounding box of this GuiComponent.protected java.util.List<java.util.function.Consumer<ComponentMouseEvent>>
getClickConsumer()
Gets the click consumer list.int
getComponentId()
Gets the component id of this GuiComponent.java.util.List<GuiComponent>
getComponents()
Gets the child components of this GuiComponent.protected Appearance
getCurrentAppearance()
java.awt.Font
getFont()
Gets the font of this GuiComponent's text.double
getHeight()
Gets the height of this GuiComponent.protected java.util.List<java.util.function.Consumer<ComponentMouseEvent>>
getHoverConsumer()
Gets the hover consumer list.Sound
getHoverSound()
Gets the sound that is played when hovering the GuiComponent.java.awt.geom.Point2D
getLocation()
Gets the screen location of this GuiComponent.protected java.util.List<java.util.function.Consumer<ComponentMouseEvent>>
getMouseDraggedConsumer()
Gets the mouse dragged consumer list.protected java.util.List<java.util.function.Consumer<ComponentMouseEvent>>
getMouseEnterConsumer()
Gets the mouse enter consumer list.protected java.util.List<java.util.function.Consumer<ComponentMouseEvent>>
getMouseLeaveConsumer()
Gets the mouse leave consumer list.protected java.util.List<java.util.function.Consumer<ComponentMouseEvent>>
getMouseMovedConsumer()
Gets the mouse moved consumer list.protected java.util.List<java.util.function.Consumer<ComponentMouseEvent>>
getMousePressedConsumer()
Gets the mouse pressed consumer list.protected java.util.List<java.util.function.Consumer<ComponentMouseEvent>>
getMouseReleasedConsumer()
Gets the mouse released consumer list.protected java.util.List<java.util.function.Consumer<ComponentMouseWheelEvent>>
getMouseWheelConsumer()
Gets the mouse wheel consumer list.java.lang.String
getName()
Gets the name of this GuiComponent.java.awt.geom.RectangularShape
getShape()
java.lang.Object
getTag()
Gets the tag.java.lang.String
getText()
Gets the entire Text associated with this GuiComponent.Align
getTextAlign()
Gets the horizontal text alignment.int
getTextAngle()
Gets the text angle.java.awt.Color
getTextShadowColor()
Gets the text shadow color.float
getTextShadowStroke()
Gets the text shadow thickness.java.lang.String
getTextToRender(java.awt.Graphics2D g)
Gets only the non-cropped bits of Text visible on this GuiComponent.m To retrieve only the entire text associated with this GuiComponent, useGuiComponent.getText()
.Valign
getTextValign()
Gets the vertical text alignment.double
getTextX()
Gets the text X coordinate.double
getTextY()
Gets the text Y coordinate.float[]
getTweenValues(TweenType tweenType)
Gets one or many values from the target object associated to the given tween type.double
getWidth()
Gets the width of this GuiComponent.double
getX()
Gets the x coordinate of this GuiComponent.double
getY()
Gets the y coordinate of this GuiComponent.boolean
hasAutomaticLineBreaks()
boolean
hasTextAntialiasing()
protected void
initializeComponents()
Initialize child components.boolean
isEnabled()
Checks if the GuiComponent is enabled.boolean
isForwardMouseEvents()
Checks if mouse events are being forwarded by this GuiComponent.boolean
isHovered()
Checks if the cursor bounding box intersects with this GuiComponent's bounding box.boolean
isPressed()
Checks if the mouse button is currently being pressed on this GuiComponent.boolean
isSelected()
Checks if the GuiComponent is currently selected.boolean
isSuspended()
Checks if the GuiComponent is currently suspended.boolean
isVisible()
Checks if the GuiComponent is currently visible.void
mouseClicked(java.awt.event.MouseEvent e)
void
mouseDragged(java.awt.event.MouseEvent e)
void
mouseEntered(java.awt.event.MouseEvent e)
protected boolean
mouseEventShouldBeForwarded(java.awt.event.MouseEvent e)
Check if a Mouse event should be forwarded.void
mouseExited(java.awt.event.MouseEvent e)
void
mouseMoved(java.awt.event.MouseEvent e)
void
mousePressed(java.awt.event.MouseEvent e)
void
mouseReleased(java.awt.event.MouseEvent e)
void
mouseWheelMoved(java.awt.event.MouseWheelEvent e)
void
onClicked(java.util.function.Consumer<ComponentMouseEvent> callback)
Add a callback that is being executed if this GuiComponent is clicked once.void
onHovered(java.util.function.Consumer<ComponentMouseEvent> callback)
Add a callback that is being executed if this GuiComponent is hovered with the mouse.void
onMouseDragged(java.util.function.Consumer<ComponentMouseEvent> callback)
Add a callback that is being executed if the mouse is pressed and moving around while within the bounds of this GuiComponent.void
onMouseEnter(java.util.function.Consumer<ComponentMouseEvent> callback)
Add a callback that is being executed if the mouse enters the bounds of this GuiComponent.void
onMouseLeave(java.util.function.Consumer<ComponentMouseEvent> callback)
Add a callback that is being executed if the mouse leaves the bounds of this GuiComponent.void
onMouseMoved(java.util.function.Consumer<ComponentMouseEvent> callback)
Add a callback that is being executed if the mouse is moving around while within the bounds of this GuiComponent.void
onMousePressed(java.util.function.Consumer<ComponentMouseEvent> callback)
Add a callback that is being executed if the mouse is continually pressed while within the bounds of this GuiComponent.void
onMouseReleased(java.util.function.Consumer<ComponentMouseEvent> callback)
Add a callback that is being executed if the mouse button is released while within the bounds of this GuiComponent.void
onMouseWheelScrolled(java.util.function.Consumer<ComponentMouseWheelEvent> callback)
Add a callback that is being executed if the mouse wheel is scrolled while within the bounds of this GuiComponent.void
onTextChanged(java.util.function.Consumer<java.lang.String> cons)
Add a callback that is being executed if the text on this GuiComponent changes.void
prepare()
Prepare the GuiComponent and all its child Components (Makes the GuiComponent visible and adds mouse listeners.).void
render(java.awt.Graphics2D g)
Renders the visual contents of this instance onto the provided graphics context.void
setAutomaticLineBreaks(boolean automaticLineBreaks)
void
setDimension(double width, double height)
Sets the width and height of this GuiComponent.void
setEnabled(boolean enabled)
Sets the "enabled" property on this GuiComponent and its child components.void
setFont(java.awt.Font font)
Sets the font for this GuiComponent's text.void
setFontSize(float size)
Sets the font size for this GuiComponent's text.void
setForwardMouseEvents(boolean forwardMouseEvents)
Enable or disable forwarding mouse events by this GuiComponent.void
setHeight(double height)
Sets the GuiComponent's height.void
setHovered(boolean hovered)
Sets the "enabled" property on this GuiComponent.void
setHoverSound(Sound hoverSound)
Sets the hover sound.void
setLocation(double x, double y)
Sets this GuiComponent's location.void
setLocation(java.awt.geom.Point2D location)
Sets this GuiComponent's location.void
setName(java.lang.String name)
Sets this GuiComponent's name.void
setSelected(boolean bool)
Sets the "selected" property on this GuiComponent.void
setTag(java.lang.Object tag)
Sets the tag.void
setText(java.lang.String text)
Sets the text.void
setTextAlign(Align textAlign)
Sets the horizontal text alignment.void
setTextAngle(int textAngle)
Sets the text angle in degrees.void
setTextAntialiasing(boolean antialiasing)
Sets theRenderingHints.KEY_TEXT_ANTIALIASING
settings for the rendered text.void
setTextShadow(boolean drawTextShadow)
Enable or disable the shadow being drawn below the textvoid
setTextShadowColor(java.awt.Color textShadowColor)
Sets the text shadow color.void
setTextShadowStroke(float textShadowStroke)
Sets the text shadow thickness.void
setTextValign(Valign textValign)
Sets the vertical text alignment.void
setTextX(double x)
Sets the text X coordinate.void
setTextY(double y)
Sets the text Y coordinate.void
setTweenValues(TweenType tweenType, float[] newValues)
This method is called in a Tween's update() method to set the new interpolated values.void
setVisible(boolean visible)
Sets the "visible" property on this GuiComponent.void
setWidth(double width)
Sets the GuiComponent's width.void
setX(double x)
Sets the GuiComponent's x coordinate.void
setY(double y)
Sets the GuiComponent's y coordinate.void
suspend()
Suspend the GuiComponent and all its child Components (Makes the GuiComponent invisible and removes mouse listeners.).void
toggleSelection()
Toggle this GuiComponent's selection.void
toggleSuspension()
Toggle this GuiComponent's suspension state.
Field Details
ICON_FONT
protected static final java.awt.Font ICON_FONT
Constructor Details
GuiComponent
protected GuiComponent(double x, double y)Instantiates a new gui component with the dimension (0,0) at the given location.- Parameters:
x
- the xy
- the y
GuiComponent
protected GuiComponent(double x, double y, double width, double height)Instantiates a new gui component at the point (x,y) with the dimension (width,height).- Parameters:
x
- the xy
- the ywidth
- the widthheight
- the height
Method Details
drawTextShadow
public boolean drawTextShadow()Draw text shadow.- Returns:
- true, if successful
getAppearance
Gets the default appearance object for this GuiComponent.- Returns:
- the appearance
getAppearanceDisabled
Gets the appearance object for this GuiComponent while disabled.- Returns:
- the appearance disabled
getAppearanceHovered
Gets the appearance object for this GuiComponent while hovered.- Returns:
- the hovered appearance
getBoundingBox
public java.awt.geom.Rectangle2D getBoundingBox()Gets the bounding box of this GuiComponent.- Returns:
- the bounding box
getComponentId
public int getComponentId()Gets the component id of this GuiComponent.- Returns:
- the component id
getComponents
Gets the child components of this GuiComponent.- Returns:
- the child components
getFont
public java.awt.Font getFont()Gets the font of this GuiComponent's text.- Returns:
- the GuiComponent's font
getHeight
public double getHeight()Gets the height of this GuiComponent.- Returns:
- the height
getHoverSound
Gets the sound that is played when hovering the GuiComponent.- Returns:
- the hover sound
getLocation
public java.awt.geom.Point2D getLocation()Gets the screen location of this GuiComponent.- Returns:
- the screen location
getName
public java.lang.String getName()Gets the name of this GuiComponent.- Returns:
- the name
getTag
public java.lang.Object getTag()Gets the tag.- Returns:
- the tag
getText
public java.lang.String getText()Gets the entire Text associated with this GuiComponent. Parts of the Text may get cropped and can therefore be invisible. To retrieve only the visible part of the text, useGuiComponent.getTextToRender(Graphics2D g)
.- Returns:
- the entire text on this GuiComponent
getTextAlign
Gets the horizontal text alignment.- Returns:
- the horizontal text alignment
getTextValign
Gets the vertical text alignment.- Returns:
- the vertical text alignment
getTextAngle
public int getTextAngle()Gets the text angle.- Returns:
- the text angle
hasTextAntialiasing
public boolean hasTextAntialiasing()hasAutomaticLineBreaks
public boolean hasAutomaticLineBreaks()getTextShadowColor
public java.awt.Color getTextShadowColor()Gets the text shadow color.- Returns:
- the text shadow color
getTextShadowStroke
public float getTextShadowStroke()Gets the text shadow thickness.- Returns:
- the float value determining how thick the outline will be rendered.
getTextToRender
public java.lang.String getTextToRender(java.awt.Graphics2D g)Gets only the non-cropped bits of Text visible on this GuiComponent.m To retrieve only the entire text associated with this GuiComponent, useGuiComponent.getText()
.- Parameters:
g
- The graphics object to render on.- Returns:
- the text to render
getTextX
public double getTextX()Gets the text X coordinate.- Returns:
- the text X
getTextY
public double getTextY()Gets the text Y coordinate.- Returns:
- the text Y
getWidth
public double getWidth()Gets the width of this GuiComponent.- Returns:
- the width
getX
public double getX()Gets the x coordinate of this GuiComponent.- Returns:
- the x coordinate
getY
public double getY()Gets the y coordinate of this GuiComponent.- Returns:
- the y coordinate
isEnabled
public boolean isEnabled()Checks if the GuiComponent is enabled.- Returns:
- true, if is enabled
isForwardMouseEvents
public boolean isForwardMouseEvents()Checks if mouse events are being forwarded by this GuiComponent.- Returns:
- true, the GuiComponent forwards mouse events
isHovered
public boolean isHovered()Checks if the cursor bounding box intersects with this GuiComponent's bounding box.- Returns:
- true, if the GuiComponent is hovered
isPressed
public boolean isPressed()Checks if the mouse button is currently being pressed on this GuiComponent.- Returns:
- true, if the mouse is currently pressed on the GuiComponent
isSelected
public boolean isSelected()Checks if the GuiComponent is currently selected.- Returns:
- true, if the GuiComponent is selected
isSuspended
public boolean isSuspended()Checks if the GuiComponent is currently suspended.- Returns:
- true, if the GuiComponent is suspended
isVisible
public boolean isVisible()Checks if the GuiComponent is currently visible.- Returns:
- true, if the GuiComponent is visible
mouseClicked
public void mouseClicked(java.awt.event.MouseEvent e)- Specified by:
mouseClicked
in interfacejava.awt.event.MouseListener
mouseDragged
public void mouseDragged(java.awt.event.MouseEvent e)- Specified by:
mouseDragged
in interfacejava.awt.event.MouseMotionListener
mouseEntered
public void mouseEntered(java.awt.event.MouseEvent e)- Specified by:
mouseEntered
in interfacejava.awt.event.MouseListener
mouseExited
public void mouseExited(java.awt.event.MouseEvent e)- Specified by:
mouseExited
in interfacejava.awt.event.MouseListener
mouseMoved
public void mouseMoved(java.awt.event.MouseEvent e)- Specified by:
mouseMoved
in interfacejava.awt.event.MouseMotionListener
mousePressed
public void mousePressed(java.awt.event.MouseEvent e)- Specified by:
mousePressed
in interfacejava.awt.event.MouseListener
mouseReleased
public void mouseReleased(java.awt.event.MouseEvent e)- Specified by:
mouseReleased
in interfacejava.awt.event.MouseListener
mouseWheelMoved
public void mouseWheelMoved(java.awt.event.MouseWheelEvent e)- Specified by:
mouseWheelMoved
in interfacejava.awt.event.MouseWheelListener
onClicked
Add a callback that is being executed if this GuiComponent is clicked once.- Parameters:
callback
- the callback
onHovered
Add a callback that is being executed if this GuiComponent is hovered with the mouse.- Parameters:
callback
- the callback
onMouseDragged
Add a callback that is being executed if the mouse is pressed and moving around while within the bounds of this GuiComponent.- Parameters:
callback
- the callback
onMouseEnter
Add a callback that is being executed if the mouse enters the bounds of this GuiComponent.- Parameters:
callback
- the callback
onMouseLeave
Add a callback that is being executed if the mouse leaves the bounds of this GuiComponent.- Parameters:
callback
- the callback
onMouseMoved
Add a callback that is being executed if the mouse is moving around while within the bounds of this GuiComponent.- Parameters:
callback
- the callback
onMousePressed
Add a callback that is being executed if the mouse is continually pressed while within the bounds of this GuiComponent.- Parameters:
callback
- the callback
onMouseReleased
Add a callback that is being executed if the mouse button is released while within the bounds of this GuiComponent.- Parameters:
callback
- the callback
onMouseWheelScrolled
Add a callback that is being executed if the mouse wheel is scrolled while within the bounds of this GuiComponent.- Parameters:
callback
- the callback
onTextChanged
public void onTextChanged(java.util.function.Consumer<java.lang.String> cons)Add a callback that is being executed if the text on this GuiComponent changes.- Parameters:
cons
- the cons
prepare
public void prepare()Prepare the GuiComponent and all its child Components (Makes the GuiComponent visible and adds mouse listeners.). This is, for example, done right before switching to a new screen.render
public void render(java.awt.Graphics2D g)Description copied from interface:IRenderable
Renders the visual contents of this instance onto the provided graphics context.If an
Entity
implements this interface, this method will be called right after the entity was rendered from the environment. Allowing for a custom rendering mechanism.This interface can be implemented in general by anything that should be rendered to the game's screen.
- Specified by:
render
in interfaceIRenderable
- Parameters:
g
- The current graphics object onto which this instance will render its visual contents.- See Also:
RenderEngine.renderEntity(Graphics2D, de.gurkenlabs.litiengine.entities.IEntity)
getTweenValues
Description copied from interface:Tweenable
Gets one or many values from the target object associated to the given tween type. It is used by the Tween Engine to determine starting values.- Specified by:
getTweenValues
in interfaceTweenable
- Parameters:
tweenType
- The tween type of this interpolation, determining which values are modified.- Returns:
- The array of current tween values.
setTweenValues
Description copied from interface:Tweenable
This method is called in a Tween's update() method to set the new interpolated values.- Specified by:
setTweenValues
in interfaceTweenable
- Parameters:
tweenType
- The tween type of this interpolation, determining which values are modified.newValues
- The new values determined by the tween equation.
getShape
public java.awt.geom.RectangularShape getShape()setDimension
public void setDimension(double width, double height)Sets the width and height of this GuiComponent.- Parameters:
width
- the widthheight
- the height
setEnabled
public void setEnabled(boolean enabled)Sets the "enabled" property on this GuiComponent and its child components.- Parameters:
enabled
- the new enabled property
setFont
public void setFont(java.awt.Font font)Sets the font for this GuiComponent's text.- Parameters:
font
- the new font
setFontSize
public void setFontSize(float size)Sets the font size for this GuiComponent's text.- Parameters:
size
- the new font size
setForwardMouseEvents
public void setForwardMouseEvents(boolean forwardMouseEvents)Enable or disable forwarding mouse events by this GuiComponent.- Parameters:
forwardMouseEvents
- the new forward mouse events
setHeight
public void setHeight(double height)Sets the GuiComponent's height.- Parameters:
height
- the new height
setHovered
public void setHovered(boolean hovered)Sets the "enabled" property on this GuiComponent.- Parameters:
hovered
- the new hovered
setHoverSound
Sets the hover sound.- Parameters:
hoverSound
- the new hover sound
setLocation
public void setLocation(double x, double y)Sets this GuiComponent's location.- Parameters:
x
- the new x coordinatey
- the new y coordinate
setLocation
public void setLocation(java.awt.geom.Point2D location)Sets this GuiComponent's location.- Parameters:
location
- the new location
setName
public void setName(java.lang.String name)Sets this GuiComponent's name.- Parameters:
name
- the new name
setSelected
public void setSelected(boolean bool)Sets the "selected" property on this GuiComponent.- Parameters:
bool
- the new selected
setTag
public void setTag(java.lang.Object tag)Sets the tag.- Parameters:
tag
- the new tag
setText
public void setText(java.lang.String text)Sets the text.- Parameters:
text
- the new text
setTextAntialiasing
public void setTextAntialiasing(boolean antialiasing)Sets theRenderingHints.KEY_TEXT_ANTIALIASING
settings for the rendered text.- Parameters:
antialiasing
- EitherRenderingHints.VALUE_TEXT_ANTIALIAS_ON
orRenderingHints.VALUE_TEXT_ANTIALIAS_OFF
setAutomaticLineBreaks
public void setAutomaticLineBreaks(boolean automaticLineBreaks)setTextAlign
Sets the horizontal text alignment.- Parameters:
textAlign
- the new text align
setTextValign
Sets the vertical text alignment.- Parameters:
textValign
- the new text align
setTextAngle
public void setTextAngle(int textAngle)Sets the text angle in degrees.- Parameters:
textAngle
- the new text angle in degrees
setTextShadow
public void setTextShadow(boolean drawTextShadow)Enable or disable the shadow being drawn below the text- Parameters:
drawTextShadow
- the boolean determining if a text shadow should be drawn
setTextShadowColor
public void setTextShadowColor(java.awt.Color textShadowColor)Sets the text shadow color.- Parameters:
textShadowColor
- the new text shadow color
setTextShadowStroke
public void setTextShadowStroke(float textShadowStroke)Sets the text shadow thickness.- Parameters:
textShadowStroke
- the thickness of the outline.
setTextX
public void setTextX(double x)Sets the text X coordinate.- Parameters:
x
- the new text X
setTextY
public void setTextY(double y)Sets the text Y coordinate.- Parameters:
y
- the new text Y
setVisible
public void setVisible(boolean visible)Sets the "visible" property on this GuiComponent.- Parameters:
visible
- the new visible
setWidth
public void setWidth(double width)Sets the GuiComponent's width.- Parameters:
width
- the new width
setX
public void setX(double x)Sets the GuiComponent's x coordinate.- Parameters:
x
- the new x coordinate
setY
public void setY(double y)Sets the GuiComponent's y coordinate.- Parameters:
y
- the new y coordinate
suspend
public void suspend()Suspend the GuiComponent and all its child Components (Makes the GuiComponent invisible and removes mouse listeners.).toggleSelection
public void toggleSelection()Toggle this GuiComponent's selection.toggleSuspension
public void toggleSuspension()Toggle this GuiComponent's suspension state. If it's suspended, prepare it. If it's prepared, suspend it.getCurrentAppearance
getClickConsumer
Gets the click consumer list.- Returns:
- the click consumer list
getHoverConsumer
Gets the hover consumer list.- Returns:
- the hover consumer list
getMouseDraggedConsumer
protected java.util.List<java.util.function.Consumer<ComponentMouseEvent>> getMouseDraggedConsumer()Gets the mouse dragged consumer list.- Returns:
- the mouse dragged consumer list
getMouseEnterConsumer
Gets the mouse enter consumer list.- Returns:
- the mouse enter consumer list
getMouseLeaveConsumer
Gets the mouse leave consumer list.- Returns:
- the mouse leave consumer list
getMouseMovedConsumer
Gets the mouse moved consumer list.- Returns:
- the mouse moved consumer list
getMousePressedConsumer
protected java.util.List<java.util.function.Consumer<ComponentMouseEvent>> getMousePressedConsumer()Gets the mouse pressed consumer list.- Returns:
- the mouse pressed consumer list
getMouseReleasedConsumer
protected java.util.List<java.util.function.Consumer<ComponentMouseEvent>> getMouseReleasedConsumer()Gets the mouse released consumer list.- Returns:
- the mouse released consumer list
getMouseWheelConsumer
protected java.util.List<java.util.function.Consumer<ComponentMouseWheelEvent>> getMouseWheelConsumer()Gets the mouse wheel consumer list.- Returns:
- the mouse wheel consumer list
initializeComponents
protected void initializeComponents()Initialize child components.mouseEventShouldBeForwarded
protected boolean mouseEventShouldBeForwarded(java.awt.event.MouseEvent e)Check if a Mouse event should be forwarded.- Parameters:
e
- the mouse event- Returns:
- true, if the Mouse event should be forwarded