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
ConstructorsModifierConstructorDescriptionprotectedGuiComponent(double x, double y) Instantiates a new gui component with the dimension (0,0) at the given location.protectedGuiComponent(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 TypeMethodDescriptionvoidaddRenderedListener(ComponentRenderedListener listener) voidaddRenderListener(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.doubleGets x coordinate of this GuiComponent's center point.doubleGets y coordinate of this GuiComponent's center point.protected List<Consumer<ComponentMouseEvent>> Gets the click consumer list.intGets the component id of this GuiComponent.Gets the child components of this GuiComponent.getFont()Gets the font of this GuiComponent's text.doubleGets 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.intGets the text angle.floatGets 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.doublegetTextX()Gets the text X coordinate.doublegetTextY()Gets the text Y coordinate.float[]getTweenValues(TweenType tweenType) Gets one or many values from the target object associated to the given tween type.doublegetWidth()Gets the width of this GuiComponent.doublegetX()Gets the x coordinate of this GuiComponent.doublegetY()Gets the y coordinate of this GuiComponent.booleanbooleanCheck whether text antialiasing is activated.booleanCheck whether text shadow is activated.protected voidInitialize child components.booleanChecks if the GuiComponent is enabled.booleanChecks if mouse events are being forwarded by this GuiComponent.booleanChecks if the cursor bounding box intersects with this GuiComponent's bounding box.booleanChecks if the mouse button is currently being pressed on this GuiComponent.booleanChecks if the GuiComponent is currently selected.booleanChecks if the GuiComponent is currently suspended.booleanChecks if the GuiComponent is currently visible.voidvoidvoidprotected booleanCheck if a Mouse event should be forwarded.voidvoidvoidvoidvoidvoidonClicked(Consumer<ComponentMouseEvent> callback) Add a callback that is being executed if this GuiComponent is clicked once.voidonHovered(Consumer<ComponentMouseEvent> callback) Add a callback that is being executed if this GuiComponent is hovered with the mouse.voidonMouseDragged(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.voidonMouseEnter(Consumer<ComponentMouseEvent> callback) Add a callback that is being executed if the mouse enters the bounds of this GuiComponent.voidonMouseLeave(Consumer<ComponentMouseEvent> callback) Add a callback that is being executed if the mouse leaves the bounds of this GuiComponent.voidonMouseMoved(Consumer<ComponentMouseEvent> callback) Add a callback that is being executed if the mouse is moving around while within the bounds of this GuiComponent.voidonMousePressed(Consumer<ComponentMouseEvent> callback) Add a callback that is being executed if the mouse is continually pressed while within the bounds of this GuiComponent.voidonMouseReleased(Consumer<ComponentMouseEvent> callback) Add a callback that is being executed if the mouse button is released while within the bounds of this GuiComponent.voidonMouseWheelScrolled(Consumer<ComponentMouseWheelEvent> callback) Add a callback that is being executed if the mouse wheel is scrolled while within the bounds of this GuiComponent.voidonTextChanged(Consumer<String> cons) Add a callback that is being executed if the text on this GuiComponent changes.voidprepare()Prepare the GuiComponent and all its child Components (Makes the GuiComponent visible and adds mouse listeners.).voidremoveListener(ComponentRenderedListener listener) voidremoveListener(ComponentRenderListener listener) voidrender(Graphics2D g) Note: If you override this and are modifying swing components, be sure you are in the AWT thread when you do so!voidsetAutomaticLineBreaks(boolean automaticLineBreaks) voidsetDimension(double width, double height) Sets the width and height of this GuiComponent.voidsetEnabled(boolean enabled) Sets the "enabled" property on this GuiComponent and its child components.voidSets the font for this GuiComponent's text.voidsetFontSize(float size) Sets the font size for this GuiComponent's text.voidsetForwardMouseEvents(boolean forwardMouseEvents) Enable or disable forwarding mouse events by this GuiComponent.voidsetHeight(double height) Sets the GuiComponent's height.voidsetHovered(boolean hovered) Sets the "hovered" property on this GuiComponent.voidsetHoverSound(Sound hoverSound) Sets the hover sound.voidsetLocation(double x, double y) Sets this GuiComponent's location.voidsetLocation(Point2D location) Sets this GuiComponent's location.voidSets this GuiComponent's name.voidsetSelected(boolean bool) Sets the "selected" property on this GuiComponent.voidSets the tag.voidSets the text.voidsetTextAlign(Align textAlign) Sets the horizontal text alignment.voidsetTextAngle(int textAngle) Sets the text angle in degrees.voidsetTextAntialiasing(boolean antialiasing) Sets theRenderingHints.KEY_TEXT_ANTIALIASINGsettings for the rendered text.voidsetTextShadow(boolean drawTextShadow) Enable or disable the shadow being drawn below the textvoidsetTextShadowColor(Color textShadowColor) voidsetTextShadowRadius(float textShadowRadius) voidsetTextValign(Valign textValign) Sets the vertical text alignment.voidsetTextX(double x) Sets the text X coordinate.voidsetTextY(double y) Sets the text Y coordinate.voidsetTweenValues(TweenType tweenType, float[] newValues) This method is called in a Tween's update() method to set the new interpolated values.voidsetVisible(boolean visible) Sets the "visible" property on this GuiComponent.voidsetWidth(double width) Sets the GuiComponent's width.voidsetX(double x) Sets the GuiComponent's x coordinate.voidsetY(double y) Sets the GuiComponent's y coordinate.voidsuspend()Suspend the GuiComponent and all its child Components (Makes the GuiComponent invisible and removes mouse listeners.).voidToggle this GuiComponent's selection.voidToggle 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:
mouseClickedin interfaceMouseListener
-
mouseDragged
- Specified by:
mouseDraggedin interfaceMouseMotionListener
-
mouseEntered
- Specified by:
mouseEnteredin interfaceMouseListener
-
mouseExited
- Specified by:
mouseExitedin interfaceMouseListener
-
mouseMoved
- Specified by:
mouseMovedin interfaceMouseMotionListener
-
mousePressed
- Specified by:
mousePressedin interfaceMouseListener
-
mouseReleased
- Specified by:
mouseReleasedin interfaceMouseListener
-
mouseWheelMoved
- Specified by:
mouseWheelMovedin 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:
renderin interfaceIRenderable- Parameters:
g- The current graphics object onto which this instance will render its visual contents.- See Also:
-
getTweenValues
Description copied from interface:TweenableGets 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:
getTweenValuesin 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:TweenableThis method is called in a Tween's update() method to set the new interpolated values.- Specified by:
setTweenValuesin 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_ANTIALIASINGsettings for the rendered text.- Parameters:
antialiasing- EitherRenderingHints.VALUE_TEXT_ANTIALIAS_ONorRenderingHints.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
-