Class GuiElement<T extends GuiElement<T>>

java.lang.Object
codechicken.lib.gui.modular.lib.geometry.ConstrainedGeometry<T>
codechicken.lib.gui.modular.elements.GuiElement<T>
All Implemented Interfaces:
ElementEvents, GuiParent<T>, TooltipHandler<T>
Direct Known Subclasses:
GuiButton, GuiColourPicker.ColourPreview, GuiColourPicker.SliderBG, GuiContextMenu, GuiDialog, GuiDVD, GuiEnergyBar, GuiEntityRenderer, GuiEventProvider, GuiFluidTank, GuiItemStack, GuiList, GuiManipulable, GuiProgressIcon, GuiRectangle, GuiScrolling, GuiSlider, GuiSlots, GuiText, GuiTextField, GuiTextList, GuiTexture

public class GuiElement<T extends GuiElement<T>> extends ConstrainedGeometry<T> implements ElementEvents, TooltipHandler<T>
This is the Base class for all gui elements in Modular Gui Version 3.

In v2 this vas a massive monolithic class that had way too much crammed into it. The primary goals of v3 are the following: - Build a new, Extremely flexible system for handling element geometry, including relative positions, anchoring, etc. This was archived using the new Geometry system. For details see GuiParent and ConstrainedGeometry - Implement a system to properly handle element z offsets. This was archived by giving all elements a 'depth' property which defines an elements size on the z axis. This is then used to properly layer elements and child elements when they are rendered. - Switch everything over to the new RenderType system. (This is mostly handled behind the scenes. You don't need to mess with it when creating a GUI) - Consolidate all the various rendering helper methods into one convenient utility class. The new GuiGraphics system showed me a good way to implement this. - Reduce the amount of ambiguity when building GUIs. (Whether I succeeded here is up for debate xD) - Cut out a lot of random bloat that was never used in v2.

Created by brandon3055 on 04/07/2023

  • Field Details

    • initialized

      public boolean initialized
    • hoverTime

      protected int hoverTime
  • Constructor Details

    • GuiElement

      public GuiElement(@NotNull @NotNull GuiParent<?> parent)
      Parameters:
      parent - parent GuiParent.
  • Method Details

    • getParent

      @NotNull public @NotNull GuiParent<?> getParent()
      Specified by:
      getParent in class ConstrainedGeometry<T extends GuiElement<T>>
    • getChildren

      public List<GuiElement<?>> getChildren()
      Specified by:
      getChildren in interface ElementEvents
      Specified by:
      getChildren in interface GuiParent<T extends GuiElement<T>>
      Returns:
      An unmodifiable list of all assigned child elements assigned to this parent. The list should be sorted in the order they were added.
    • addChild

      public void addChild(GuiElement<?> child)
      In Modular GUI v3, The add child method is primarily for internal use, Child elements are automatically added to their parent on construction.
      Specified by:
      addChild in interface GuiParent<T extends GuiElement<T>>
      Parameters:
      child - The child element to be added.
      See Also:
    • applyQueuedChildUpdates

      protected void applyQueuedChildUpdates()
    • initElement

      public void initElement(GuiParent<?> parent)
      Called immediately after an element is added to its parent, use to initialize the child element.
    • adoptChild

      public void adoptChild(GuiElement<?> child)
      Description copied from interface: GuiParent
      This method can be used to transfer an already initialized child to this parent element. This automatically handles removing the element from its previous parent, adds it to this element. Note: This will most likely break any relative constraints on the child's geometry. To fix this you will need to re-apply geometry constraints after the transfer.
      Specified by:
      adoptChild in interface GuiParent<T extends GuiElement<T>>
      Parameters:
      child - The child element to be adopted.
    • removeChild

      public void removeChild(GuiElement<?> child)
      Description copied from interface: GuiParent
      Allows the removal of a child element. Child removal is not instantaneous, Instead all removals occur at the end of the current screen thick. This is to avoid any possible concurrency issues.
      Specified by:
      removeChild in interface GuiParent<T extends GuiElement<T>>
      Parameters:
      child - The child element to be removed.
    • bringChildToForeground

      public void bringChildToForeground(GuiElement<?> child)
      Sets a child as the last element in the child lest, meaning it will render over all other elements.
    • sendChildToIndex

      public void sendChildToIndex(GuiElement<?> child, int index)
      sends a child to a specific index in the child lest.
    • sendChildToBackground

      public void sendChildToBackground(GuiElement<?> child)
      Sets a child as the first element in the child lest, meaning it will render under all other elements.
    • isDescendantOf

      public boolean isDescendantOf(GuiElement<?> ancestor)
      Description copied from interface: GuiParent
      Checks if this element is a descendant of the specified.
      Specified by:
      isDescendantOf in interface GuiParent<T extends GuiElement<T>>
      Returns:
      true if the specified element is a parent or grandparent etc... of this element.
    • mc

      public net.minecraft.client.Minecraft mc()
      Specified by:
      mc in interface GuiParent<T extends GuiElement<T>>
      Returns:
      The minecraft instance.
    • font

      public net.minecraft.client.gui.Font font()
      Specified by:
      font in interface GuiParent<T extends GuiElement<T>>
      Returns:
      The active font instance.
    • scaledScreenWidth

      public int scaledScreenWidth()
      Specified by:
      scaledScreenWidth in interface GuiParent<T extends GuiElement<T>>
      Returns:
      The current gui screen width, As returned by mc.getWindow().getGuiScaledWidth()
    • scaledScreenHeight

      public int scaledScreenHeight()
      Specified by:
      scaledScreenHeight in interface GuiParent<T extends GuiElement<T>>
      Returns:
      The current gui screen height, As returned by mc.getWindow().getGuiScaledHeight()
    • getModularGui

      public ModularGui getModularGui()
      Specified by:
      getModularGui in interface GuiParent<T extends GuiElement<T>>
      Returns:
      the parent ModularGui instance.
    • onScreenInit

      public void onScreenInit(net.minecraft.client.Minecraft mc, net.minecraft.client.gui.Font font, int screenWidth, int screenHeight)
      Description copied from interface: GuiParent
      Called when the minecraft Screen is initialised or resized.
      Specified by:
      onScreenInit in interface GuiParent<T extends GuiElement<T>>
      Parameters:
      mc - The Minecraft instance.
      font - The active font.
      screenWidth - The current guiScaledWidth.
      screenHeight - The current guiScaledHeight.
    • updateScreenData

      protected void updateScreenData(net.minecraft.client.Minecraft mc, net.minecraft.client.gui.Font font, int screenWidth, int screenHeight)
    • setEnabled

      public T setEnabled(boolean enabled)
    • setEnabled

      public T setEnabled(@Nullable @Nullable Supplier<Boolean> enabled)
    • isEnabled

      public boolean isEnabled()
    • isRemoved

      public boolean isRemoved()
    • setEnableToolTip

      public T setEnableToolTip(Supplier<Boolean> enableToolTip)
    • isTooltipEnabled

      public boolean isTooltipEnabled()
    • blockMouseOver

      public boolean blockMouseOver(GuiElement<?> element, double mouseX, double mouseY)
      Description copied from interface: GuiParent
      Allows an element to override the isMouseOver() method of its children. This is primarily used for things like scroll elements where mouseover interactions need to be blocked outside the view area.
      Specified by:
      blockMouseOver in interface GuiParent<T extends GuiElement<T>>
      Parameters:
      element - The element on which isMouseOver is getting called.
      Returns:
      true if mouse-over interaction should be blocked for this child element.
    • blockMouseEvents

      public boolean blockMouseEvents()
      Specified by:
      blockMouseEvents in interface ElementEvents
      Returns:
      True to prevent mouse events from being passed to elements bellow this element.
    • isMouseOver

      public boolean isMouseOver()
      Returns:
      True if the cursor is within the bounds of this element, and there is no opaque element above this one obstructing the cursor.
    • isOpaque

      public boolean isOpaque()
    • setOpaque

      public T setOpaque(boolean opaque)
      If an element is marked as opaque it will consume mouseOver updates, thereby preventing elements bellow from accepting mouseOver input. Also prevents mouse events within this element from being passed to elements bellow.
    • hoverTime

      public int hoverTime()
      Returns:
      the amount of time the cursor has spent inside this element's bounds, resets to zero when the cursor leaves this element's bounds.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • setRenderCull

      public T setRenderCull(@Nullable @Nullable Rectangle renderCull)
      Any child elements completely outside this rectangle will not be rendered at all. By default, this is set to the screen bounds (meaning the minecraft window) Setting this to null will disable culling.
    • setZStacking

      public T setZStacking(boolean zStacking)
      Allows you to disable child z-stacking, Meaning all child elements will be rendered at the same z-level rather than being stacked. (Not Recursive, children their sub elements with stacking)

      This can be useful when rendering a lot of high z depth elements such as ItemStacks. As long as you know for sure none of the elements intersect, it should be safe to disable stacking.

      Parameters:
      zStacking - Enable z stacking (default true)
    • zStacking

      public boolean zStacking()
    • getCombinedElementDepth

      public double getCombinedElementDepth()
      Returns the depth of this element plus all of its children (recursively) Note: You should almost never need to override this! Depth of background and / or foreground content should be specified via BackgroundRender.getBackgroundDepth() and ForegroundRender.getForegroundDepth()
      Returns:
      The depth (z height) of this element plus all of its children.
    • render

      public void render(GuiRender render, double mouseX, double mouseY, float partialTicks)
      This is the main render method that handles rendering this element and any child elements it may have. This method almost never needs to be overridden, instead when creating custom elements with custom rendering, your element should implement BackgroundRender and / or ForegroundRender in or order to implement its rendering.

      Note: After the render is complete, the poseStack's z pos will be offset by the total depth of this element and its children. This is intended behavior,

      Parameters:
      render - Contains gui context information as well as essential render methods/utils including the PoseStack.
      mouseX - Current mouse X position
      mouseY - Current mouse Y position
      partialTicks - Partial render ticks
    • renderChild

      protected boolean renderChild(GuiElement<?> child, GuiRender render, double mouseX, double mouseY, float partialTicks)
    • renderOverlay

      public boolean renderOverlay(GuiRender render, double mouseX, double mouseY, float partialTicks, boolean consumed)
      Used to render overlay's such as hover text. Anything rendered in this method will be rendered on top of everything else on the screen. Only one overlay should be rendered at a time, When an element renders content via the overlay method it must return true to indicate the render call has been 'consumed' If the render call has already been consumed (Check via the consumed boolean) then this element should avoid rendering its overlay.

      When rendering overlay content, always use the PoseStack available via the provided GuiRender This stack will already have the correct Z translation to ensure the overlay renders above everything else on the screen.

      To check if the cursor is over this element, use 'render.hoveredElement() == this' isMouseOver() Will also work, but may be problematic when multiple, stacked elements have overlay content.

      Parameters:
      render - Contains gui context information as well as essential render methods/utils including the PoseStack.
      mouseX - Current mouse X position
      mouseY - Current mouse Y position
      partialTicks - Partial render ticks
      consumed - Will be true if the overlay render call has already been consumed by another element.
      Returns:
      true if the render call has been consumed.
    • showToolTip

      public boolean showToolTip()
      Returns:
      true if all conditions are bet for this element to be rendering its tool tip. Meaning mouse is over the element, hover time has elapsed and tooltip is enabled.
    • tick

      public void tick(double mouseX, double mouseY)
      Called every tick to update the element. Note this is called regardless of weather or not the element is actually enabled.
      Parameters:
      mouseX - Current mouse X position
      mouseY - Current mouse Y position
    • updateMouseOver

      public boolean updateMouseOver(double mouseX, double mouseY, boolean consumed)
      Called at the start of each tick to update the 'mouseOver' state of each element. If the cursor is over an element that is marked as opaque, the update will be consumed. This ensures no elements below the opaque element will have their mouseOver flag set to true.
      Parameters:
      mouseX - Mouse X position
      mouseY - Mouse Y position
      consumed - True if mouseover event has been consumed.
      Returns:
      true if this event has been consumed.
    • getTooltip

      public Supplier<List<net.minecraft.network.chat.Component>> getTooltip()
      Specified by:
      getTooltip in interface TooltipHandler<T extends GuiElement<T>>
    • setTooltipDelay

      public T setTooltipDelay(int tooltipDelay)
      Description copied from interface: TooltipHandler
      Set a delay before element tooltip is displayed. Default delay is 10 ticks.
      Specified by:
      setTooltipDelay in interface TooltipHandler<T extends GuiElement<T>>
    • getTooltipDelay

      public int getTooltipDelay()
      Specified by:
      getTooltipDelay in interface TooltipHandler<T extends GuiElement<T>>
    • setTooltip

      public T setTooltip(@Nullable @Nullable Supplier<List<net.minecraft.network.chat.Component>> tooltip)
      Description copied from interface: TooltipHandler
      Add hover text that is to be displayed when the user hovers their cursor over this element. (with a delay of 10 ticks) If you have multiple stacked elements with tooltips, only the top most element under the cursor will display its hover text.
      Specified by:
      setTooltip in interface TooltipHandler<T extends GuiElement<T>>
      Parameters:
      tooltip - The tooltip lines. If null or the returned list is empty, hover text will be disabled
      See Also:
    • jeiExclude

      public T jeiExclude()
      Add this element to the list of jei exclusions. Use this for any elements that render outside the normal gui bounds. This will ensure JEI does not try to render on top of these elements.
    • setJeiExcluded

      public T setJeiExcluded(boolean jeiExcluded)
    • isJeiExcluded

      public boolean isJeiExcluded()
    • setJeiDropTarget

      public T setJeiDropTarget(Consumer<net.minecraft.world.item.ItemStack> onDrop, boolean installStandardHighlight)
      Allows you get notified when a player drags and drops an ItemStack from jai onto this element. If using the standard highlight then you should call this after adding any other required child elements to ensure the highlight is on top.
    • getJeiDropConsumer

      public Consumer<net.minecraft.world.item.ItemStack> getJeiDropConsumer()
    • isJeiDropTarget

      public boolean isJeiDropTarget()
    • addJeiExclusions

      public List<GuiElement<?>> addJeiExclusions(List<GuiElement<?>> list)
    • addJeiDropTargets

      public List<GuiElement<?>> addJeiDropTargets(List<GuiElement<?>> list)