Class ModularGui

java.lang.Object
codechicken.lib.gui.modular.ModularGui
All Implemented Interfaces:
GuiParent<ModularGui>

public class ModularGui extends Object implements GuiParent<ModularGui>
The modular gui system is built around "Gui Elements" but those elements need to be rendered by a base parent element. That's what this class is. This class is essentially just a container for the root gui element.

Created by brandon3055 on 18/08/2023

See Also:
  • Constructor Details

    • ModularGui

      public ModularGui(GuiProvider provider)
      Parameters:
      provider - The gui builder that will be used to construct this modular gui when the screen is initialized.
    • ModularGui

      public ModularGui(GuiProvider provider, net.minecraft.client.gui.screens.Screen parentScreen)
  • Method Details

    • setScreen

      public void setScreen(net.minecraft.client.gui.screens.Screen screen)
    • getProvider

      public GuiProvider getProvider()
    • setGuiTitle

      public void setGuiTitle(@NotNull @NotNull net.minecraft.network.chat.Component guiTitle)
    • getGuiTitle

      @NotNull public @NotNull net.minecraft.network.chat.Component getGuiTitle()
    • setPauseScreen

      public void setPauseScreen(boolean pauseScreen)
      Parameters:
      pauseScreen - Should a single-player game pause while this screen is open?
    • isPauseScreen

      public boolean isPauseScreen()
    • setCloseOnEscape

      public void setCloseOnEscape(boolean closeOnEscape)
    • closeOnEscape

      public boolean closeOnEscape()
    • renderScreenBackground

      public void renderScreenBackground(boolean renderBackground)
      Enable / disable the default screen background. (Default Enabled) This will be the usual darkened background when in-game, or the dirt background when not in game.
    • renderBackground

      public boolean renderBackground()
    • getRoot

      public GuiElement<?> getRoot()
      Returns:
      the root element to which content elements should be added.
    • getDirectRoot

      public GuiElement<?> getDirectRoot()
    • initStandardGui

      public ModularGui initStandardGui(int guiWidth, int guiHeight)
      Sets up this gui to render like any other standards gui with the specified width and height. Meaning, the root element (usually the gui background image) will be centered on the screen, and will have the specified width and height.

      Parameters:
      guiWidth - Gui Width
      guiHeight - Gui Height
      See Also:
    • initFullscreenGui

      public ModularGui initFullscreenGui()
      Sets up this gui to render as a full screen gui. Meaning the root element's geometry will match that of the underlying minecraft screen.

      See Also:
    • setVanillaSlotRendering

      public void setVanillaSlotRendering(boolean vanillaSlotRendering)
      By default, modular gui completely overrides vanillas default slot rendering. This ensures slots render within the depth constraint of the slot element and avoids situations where stacks in slots render on top of other parts of the gui. Meaning you can do things like hide slots by disabling the slot element, Or render elements on top of the slots.

      This method allow you to return full rendering control to vanilla if you need to for whatever reason.

    • vanillaSlotRendering

      public boolean vanillaSlotRendering()
    • createRender

      @Deprecated public GuiRender createRender(net.minecraft.client.renderer.MultiBufferSource.BufferSource buffers)
      Deprecated.
      Create a new GuiRender for the current render call.
      Parameters:
      buffers - BufferSource can be retried from GuiGraphics
      Returns:
      A new GuiRender for the current render call.
    • render

      public void render(GuiRender render, float partialTicks)
      Primary render method for ModularGui. The screen implementing ModularGui must call this in its render method. Followed by the renderOverlay(GuiRender, float) method to handle overlay rendering.
      Parameters:
      render - GuiRender instance converted from Minecraft's GuiGraphics via GuiRender.convert(GuiGraphics)
    • renderOverlay

      public boolean renderOverlay(GuiRender render, float partialTicks)
      Handles gui overlay rendering. This is where things like tool tips are rendered. This should be called immediately after render(GuiRender, float)

      The reason this is split out from render(GuiRender, float) is to allow stack tool tips to override gui overlay rendering in ModularGuiContainer

      Parameters:
      render - This should be the same render instance that was passed to the previous render(GuiRender, float) call.
      Returns:
      true if an overlay such as a tooltip is currently being drawn.
    • tick

      public void tick()
      Primary update / tick method. Must be called from the tick method of the implementing screen.
    • mouseMoved

      public void mouseMoved(double mouseX, double mouseY)
      Pass through for the mouseMoved event. Any screen implementing ModularGui must pass through this event.
      Parameters:
      mouseX - new mouse X position
      mouseY - new mouse Y position
    • mouseClicked

      public boolean mouseClicked(double mouseX, double mouseY, int button)
      Pass through for the mouseClicked event. Any screen implementing ModularGui must pass through this event.
      Parameters:
      mouseX - Mouse X position
      mouseY - Mouse Y position
      button - Mouse Button
      Returns:
      true if this event has been consumed.
    • mouseReleased

      public boolean mouseReleased(double mouseX, double mouseY, int button)
      Pass through for the mouseReleased event. Any screen implementing ModularGui must pass through this event.
      Parameters:
      mouseX - Mouse X position
      mouseY - Mouse Y position
      button - Mouse Button
      Returns:
      true if this event has been consumed.
    • keyPressed

      public boolean keyPressed(int key, int scancode, int modifiers)
      Pass through for the keyPressed event. Any screen implementing ModularGui must pass through this event.
      Parameters:
      key - the keyboard key that was pressed.
      scancode - the system-specific scancode of the key
      modifiers - bitfield describing which modifier keys were held down.
      Returns:
      true if this event has been consumed.
    • keyReleased

      public boolean keyReleased(int key, int scancode, int modifiers)
      Pass through for the keyReleased event. Any screen implementing ModularGui must pass through this event.
      Parameters:
      key - the keyboard key that was released.
      scancode - the system-specific scancode of the key
      modifiers - bitfield describing which modifier keys were held down.
      Returns:
      true if this event has been consumed.
    • charTyped

      public boolean charTyped(char character, int modifiers)
      Pass through for the charTyped event. Any screen implementing ModularGui must pass through this event.
      Parameters:
      character - The character typed.
      modifiers - bitfield describing which modifier keys were held down.
      Returns:
      true if this event has been consumed.
    • mouseScrolled

      public boolean mouseScrolled(double mouseX, double mouseY, double scroll)
      Pass through for the mouseScrolled event. Any screen implementing ModularGui must pass through this event.
      Parameters:
      mouseX - Mouse X position
      mouseY - Mouse Y position
      scroll - Scroll direction and amount
      Returns:
      true if this event has been consumed.
    • onGuiClose

      public void onGuiClose()
      Must be called by the screen when this gui is closed.
    • updateScreenData

      protected void updateScreenData(net.minecraft.client.Minecraft mc, net.minecraft.client.gui.Font font, int screenWidth, int screenHeight)
    • 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<ModularGui>
      Parameters:
      mc - The Minecraft instance.
      font - The active font.
      screenWidth - The current guiScaledWidth.
      screenHeight - The current guiScaledHeight.
    • mc

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

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

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

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

      public ModularGui getModularGui()
      Specified by:
      getModularGui in interface GuiParent<ModularGui>
      Returns:
      the parent ModularGui instance.
    • getScreen

      public net.minecraft.client.gui.screens.Screen getScreen()
      Returns the Screen housing this ModularGui With custom ModularGui implementations this may be null.
    • getParentScreen

      @Nullable public @Nullable net.minecraft.client.gui.screens.Screen getParentScreen()
    • getChildren

      public List<GuiElement<?>> getChildren()
      Specified by:
      getChildren in interface GuiParent<ModularGui>
      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)
      Description copied from interface: GuiParent
      Adds a new child element to this parent. You should almost never need to use this because this is handled automatically when an element is created.

      Note: Due to the way relative coordinates work with the new geometry system, Transferring an element to a different parent can have unpredictable results. Therefor, to help avoid confusion it is not possible to transfer a child to a new parent using this method.

      Specified by:
      addChild in interface GuiParent<ModularGui>
      Parameters:
      child - The child element to be added.
      See Also:
    • addChild

      public ModularGui addChild(Consumer<ModularGui> createChild)
      Description copied from interface: GuiParent
      This meant to be a convenience method that allows builder style addition of a child element. I'm not sure how useful it will be yet, so it may or may not stay.
      Specified by:
      addChild in interface GuiParent<ModularGui>
      Parameters:
      createChild - A consumer that is given this element to be used in the construction of the child element.
      Returns:
      The parent 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<ModularGui>
      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<ModularGui>
      Parameters:
      child - The child element to be removed.
    • xMin

      public double xMin()
      Specified by:
      xMin in interface GuiParent<ModularGui>
      Returns:
      The position of the Left edge of this element.
    • xMax

      public double xMax()
      Specified by:
      xMax in interface GuiParent<ModularGui>
      Returns:
      The position of the Right edge of this element.
    • xSize

      public double xSize()
      Specified by:
      xSize in interface GuiParent<ModularGui>
      Returns:
      The Width of this element.
    • yMin

      public double yMin()
      Specified by:
      yMin in interface GuiParent<ModularGui>
      Returns:
      The position of the Top edge of this element.
    • yMax

      public double yMax()
      Specified by:
      yMax in interface GuiParent<ModularGui>
      Returns:
      The position of the Bottom edge of this element.
    • ySize

      public double ySize()
      Specified by:
      ySize in interface GuiParent<ModularGui>
      Returns:
      The Height of this element.
    • computeMouseX

      public double computeMouseX()
    • computeMouseY

      public double computeMouseY()
    • setSlotHandler

      public void setSlotHandler(net.minecraft.world.inventory.Slot slot, GuiElement<?> handler)
      Provides a way to later retrieve the gui element responsible for positioning and rendering a slot.
    • getSlotHandler

      public GuiElement<?> getSlotHandler(net.minecraft.world.inventory.Slot slot)
      Returns the gui element responsible for managing a gui slot.
    • setCursor

      public void setCursor(net.minecraft.resources.ResourceLocation cursor)
      Sets the current mouse cursor. The cursor is reset at the end of each UI tick so this must be set every tick for as long as you want your custom cursor to be active.
    • setFloatingItemDisablesToolTips

      public void setFloatingItemDisablesToolTips(boolean floatingItemDisablesToolTips)
      By default, tool tips are disabled while where is a floating item on screen. This can be used to re-enable them.
    • doesFloatingItemDisableToolTips

      public boolean doesFloatingItemDisableToolTips()
    • getJeiExclusions

      public List<GuiElement<?>> getJeiExclusions()
    • getJeiDropTargets

      public List<GuiElement<?>> getJeiDropTargets()
    • setJeiHighlightTime

      public void setJeiHighlightTime(int jeiHighlightTime)
    • getJeiHighlightTime

      public int getJeiHighlightTime()
    • onTick

      public void onTick(Runnable onTick)
      Allows you to attach a callback that will be fired at the start of each gui tick.
    • onResize

      public void onResize(Runnable onResize)
      Allows you to attach a callback that will be fired when the parent screen is resized.
    • onClose

      public void onClose(Runnable onClose)
    • onMouseClickPre

      public void onMouseClickPre(org.apache.logging.log4j.util.TriConsumer<Double,Double,Integer> onClick)
      Allows you to attach a callback that will be fired on mouse click, before the click is handled by the rest of the gui.
    • onMouseClickPost

      public void onMouseClickPost(org.apache.logging.log4j.util.TriConsumer<Double,Double,Integer> onClick)
      Allows you to attach a callback that will be fired on mouse click, after the click has been handled by the rest of the gui. Will only be fired if the event was not consumed by an element.
    • onKeyPressPre

      public void onKeyPressPre(org.apache.logging.log4j.util.TriConsumer<Integer,Integer,Integer> preKeyPress)
      Allows you to attach a callback that will be fired on key press, before the is handled by the rest of the gui.
    • onKeyPressPost

      public void onKeyPressPost(org.apache.logging.log4j.util.TriConsumer<Integer,Integer,Integer> postKeyPress)
      Allows you to attach a callback that will be fired on key press, after it has been handled by the rest of the gui. Will only be fired if the event was not consumed by an element.