Class GuiComponent
java.lang.Object
de.gurkenlabs.litiengine.gui.GuiComponent
- All Implemented Interfaces:
IRenderable
,Tweenable
,MouseListener
,MouseMotionListener
,MouseWheelListener
,EventListener
- Direct Known Subclasses:
DropdownListField
,ImageComponent
,ImageComponentList
,ListField
,Screen
,Slider
,SpeechBubble
public abstract class GuiComponent
extends Object
implements MouseListener, MouseMotionListener, 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 -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
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 TypeMethodDescriptionvoid
addRenderedListener
(ComponentRenderedListener listener) void
addRenderListener
(ComponentRenderListener listener) Gets the default appearance object for this GuiComponent.Gets the appearance object for this GuiComponent while disabled.Gets the appearance object for this GuiComponent while hovered.Gets the bounding box of this GuiComponent.double
Gets x coordinate of this GuiComponent's center point.double
Gets y coordinate of this GuiComponent's center point.protected List
<Consumer<ComponentMouseEvent>> Gets the click consumer list.int
Gets the component id of this GuiComponent.Gets the child components of this GuiComponent.getFont()
Gets the font of this GuiComponent's text.double
Gets the height of this GuiComponent.protected List
<Consumer<ComponentMouseEvent>> Gets the hover consumer list.Gets the sound that is played when hovering the GuiComponent.Gets the screen location of this GuiComponent.protected List
<Consumer<ComponentMouseEvent>> Gets the mouse dragged consumer list.protected List
<Consumer<ComponentMouseEvent>> Gets the mouse enter consumer list.protected List
<Consumer<ComponentMouseEvent>> Gets the mouse leave consumer list.protected List
<Consumer<ComponentMouseEvent>> Gets the mouse moved consumer list.protected List
<Consumer<ComponentMouseEvent>> Gets the mouse pressed consumer list.protected List
<Consumer<ComponentMouseEvent>> Gets the mouse released consumer list.protected List
<Consumer<ComponentMouseWheelEvent>> Gets the mouse wheel consumer list.getName()
Gets the name of this GuiComponent.getShape()
getTag()
Gets the tag.getText()
Gets the entire Text associated with this GuiComponent.Gets the horizontal text alignment.int
Gets the text angle.float
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()
.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
boolean
Check whether text antialiasing is activated.boolean
Check whether text shadow is activated.protected void
Initialize child components.boolean
Checks if the GuiComponent is enabled.boolean
Checks if mouse events are being forwarded by this GuiComponent.boolean
Checks if the cursor bounding box intersects with this GuiComponent's bounding box.boolean
Checks if the mouse button is currently being pressed on this GuiComponent.boolean
Checks if the GuiComponent is currently selected.boolean
Checks if the GuiComponent is currently suspended.boolean
Checks if the GuiComponent is currently visible.void
void
void
protected boolean
Check if a Mouse event should be forwarded.void
void
void
void
void
void
onClicked
(Consumer<ComponentMouseEvent> callback) Add a callback that is being executed if this GuiComponent is clicked once.void
onHovered
(Consumer<ComponentMouseEvent> callback) Add a callback that is being executed if this GuiComponent is hovered with the mouse.void
onMouseDragged
(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
(Consumer<ComponentMouseEvent> callback) Add a callback that is being executed if the mouse enters the bounds of this GuiComponent.void
onMouseLeave
(Consumer<ComponentMouseEvent> callback) Add a callback that is being executed if the mouse leaves the bounds of this GuiComponent.void
onMouseMoved
(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
(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
(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
(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
(Consumer<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
removeListener
(ComponentRenderedListener listener) void
removeListener
(ComponentRenderListener listener) void
render
(Graphics2D g) Note: If you override this and are modifying swing components, be sure you are in the AWT thread when you do so!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
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 "hovered" 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
(Point2D location) Sets this GuiComponent's location.void
Sets this GuiComponent's name.void
setSelected
(boolean bool) Sets the "selected" property on this GuiComponent.void
Sets the tag.void
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
(Color textShadowColor) void
setTextShadowRadius
(float textShadowRadius) 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
Toggle this GuiComponent's selection.void
Toggle this GuiComponent's suspension state.
-
Field Details
-
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
-
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
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
-
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
Gets the screen location of this GuiComponent.- Returns:
- the screen location
-
getName
-
getTag
-
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()Check whether text antialiasing is activated.- Returns:
- true, if this GuiComponent is currently configured to draw its text with antialiasing.
-
hasTextShadow
public boolean hasTextShadow()Check whether text shadow is activated.- Returns:
- true, if this GuiComponent is currently configured to draw a shadow below its text.
-
getTextShadowColor
-
setTextShadowColor
-
getTextShadowRadius
public float getTextShadowRadius() -
setTextShadowRadius
public void setTextShadowRadius(float textShadowRadius) -
hasAutomaticLineBreaks
public boolean hasAutomaticLineBreaks() -
getTextToRender
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
-
getCenterX
public double getCenterX()Gets x coordinate of this GuiComponent's center point.- Returns:
- the center x coordinate
-
getCenterY
public double getCenterY()Gets y coordinate of this GuiComponent's center point.- Returns:
- the center y 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
- Specified by:
mouseClicked
in interfaceMouseListener
-
mouseDragged
- Specified by:
mouseDragged
in interfaceMouseMotionListener
-
mouseEntered
- Specified by:
mouseEntered
in interfaceMouseListener
-
mouseExited
- Specified by:
mouseExited
in interfaceMouseListener
-
mouseMoved
- Specified by:
mouseMoved
in interfaceMouseMotionListener
-
mousePressed
- Specified by:
mousePressed
in interfaceMouseListener
-
mouseReleased
- Specified by:
mouseReleased
in interfaceMouseListener
-
mouseWheelMoved
- Specified by:
mouseWheelMoved
in interfaceMouseWheelListener
-
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
-
addRenderListener
-
removeListener
-
addRenderedListener
-
removeListener
-
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
Note: If you override this and are modifying swing components, be sure you are in the AWT thread when you do so!- Specified by:
render
in interfaceIRenderable
- Parameters:
g
- The current graphics object onto which this instance will render its visual contents.- See Also:
-
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
-
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
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 "hovered" 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
Sets this GuiComponent's location.- Parameters:
location
- the new location
-
setName
-
setSelected
public void setSelected(boolean bool) Sets the "selected" property on this GuiComponent.- Parameters:
bool
- the new selected
-
setTag
-
setText
-
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
-
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
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
Gets the mouse pressed consumer list.- Returns:
- the mouse pressed consumer list
-
getMouseReleasedConsumer
Gets the mouse released consumer list.- Returns:
- the mouse released consumer list
-
getMouseWheelConsumer
Gets the mouse wheel consumer list.- Returns:
- the mouse wheel consumer list
-
initializeComponents
protected void initializeComponents()Initialize child components. -
mouseEventShouldBeForwarded
Check if a Mouse event should be forwarded.- Parameters:
e
- the mouse event- Returns:
- true, if the Mouse event should be forwarded
-