Class ConstrainedGeometry<T extends ConstrainedGeometry<T>>

java.lang.Object
codechicken.lib.gui.modular.lib.geometry.ConstrainedGeometry<T>
All Implemented Interfaces:
GuiParent<T>
Direct Known Subclasses:
GuiElement

public abstract class ConstrainedGeometry<T extends ConstrainedGeometry<T>> extends Object implements GuiParent<T>
This is the base class used to define the size and position of a GuiElement.

The geometry system is designed to be very user-friendly, yet very powerful, with the ability to do all sorts of fun and interesting things. But if all you want is the ability to set simple size and position values, then there is no need to read any further. Just use the basic setters setXPos, setYPos, setWidth, setHeight and everything should just work. If you want to dive deeper, read on.

The geometry system is based around 6 core parameters: xMin, xMax, xSize, yMin, yMax and ySize. See: GeoParam
These parameters are defined using 'Constraints' See: Constraint
When properly constrained, it will be possible to request values for any of these 6 parameters.
For each axis (x and y) there are 3 constraints: min, max and size.
In order for an axis to be properly defined 2 of these constraints need to be set.
e.g. (xMin and xSize) or (xMin and xMax) or (xMax and xSize)
Using any of these combinations the position and size of an axis can be computed.

Look at AxisConfig for details on the values that get returned when too few constraints are set.
TLDR: We will always try to return "reasonable" default values. e.g. if only size is constrained, xMin will default to 0 and xMax will default to 0 + xSize.
If all three constraints are set then the size constraint will be ignored.
You can use strictMode(boolean) to change this behavior.

There are a number of different types of constraints that can be used, including completely custom constraints.
Take a look at the Constraint class for a detailed list with explanations.

Note: All position and size values in ModularGui use doubles. This is because floating point position and size values can be very useful at times. But they can also cause a lot of ugly visual artifacts when not used properly. So by default, all the builtin constraints will cast their outputs to an integer value. If you need floating point precision, you can enable it by calling .precise() on any of the builtin constraints.

Created by brandon3055 on 29/06/2023

  • Constructor Details

    • ConstrainedGeometry

      public ConstrainedGeometry()
  • Method Details

    • getParent

      @NotNull public abstract @NotNull GuiParent<?> getParent()
    • getParent

      public GeoRef getParent(GeoParam param)
    • setXPos

      public T setXPos(double x)
      Simple method for setting the x position of this element.

      Constrains the left side of this element, to the left side of the parent element, with an offset that is calculated by subtracting parent's current x pos from the given x pos.

      In other words, if the parent element's position changes, this element will move with it.

      Parameters:
      x - The X position in screen space.
      Returns:
      This Element.
    • setYPos

      public T setYPos(double y)
      Simple method for setting the y position of this element.

      Constrains the top side of this element, to the top side of the parent element, with an offset that is calculated by subtracting parent's current y pos from the given y pos.

      In other words, if the parent element's position changes, this element will move with it.

      Parameters:
      y - The Y position in screen space.
      Returns:
      This Element.
    • setPos

      public T setPos(double x, double y)
      Convenience method for setting both x and y positions.
      Parameters:
      x - The X position in screen space.
      y - The Y position in screen space.
      Returns:
      This Element.
      See Also:
    • setWidth

      public T setWidth(double width)
      Simple method for setting the width of this element.
      Parameters:
      width - The width to apply.
      Returns:
      This Element.
    • setHeight

      public T setHeight(double height)
      Simple method for setting the height of this element.
      Parameters:
      height - The height to apply.
      Returns:
      This Element.
    • setSize

      public T setSize(double width, double height)
      Convenience method for setting both width and height.
      Parameters:
      width - The width to apply.
      height - The height to apply.
      Returns:
      This Element.
      See Also:
    • xMin

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

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

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

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

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

      public double ySize()
      Specified by:
      ySize in interface GuiParent<T extends ConstrainedGeometry<T>>
      Returns:
      The Height of this element.
    • get

      public GeoRef get(GeoParam param)
      Returns a reference to the specified geometry parameter. This is primarily used when defining geometry constraints. But it can also be used as a simple Supplier<Integer> that will return the current parameter value when requested.

      Note: The returned geometry reference will always be valid

      Specified by:
      get in interface GuiParent<T extends ConstrainedGeometry<T>>
      Parameters:
      param - The geometry parameter.
      Returns:
      A Geometry Reference
    • constrain

      public T constrain(GeoParam param, @Nullable @Nullable Constraint constraint)
      Parameters:
      param - The geometry parameter to be constrained.
      constraint - The constraint to apply
      Returns:
      This Element.
    • clearConstraints

      public T clearConstraints()
      Clear any configured constraints and reset this element to default unconstrained state. Convenient when reconfiguring an elements constraints or applying constraints to an element with an existing, unknown constraint configuration.
    • strictMode

      public T strictMode(boolean strictMode)
      Strict mode is intended to help catch potential mistakes when writing modular GUIs

      Enforces a strict requirement for each exist to have two and only two constraints. Any attempt to over-constrain an axis will throw an immediate fatal exception. If an axis is under-constrained then a fatal exception will be thrown when a value from the axis is queried.

      Strict mode applies to this element, and recursively to all children of this element.

      Parameters:
      strictMode - Enable strict mode.
      Returns:
      the geometry object.
    • validate

      public void validate()
    • clearGeometryCache

      public void clearGeometryCache()
    • getPosition

      public Position getPosition()
      Returns a Position that is permanently bound to this element.
    • getRectangle

      public Rectangle getRectangle()
      Returns a Rectangle that is permanently bound to this element.
    • xCenter

      public double xCenter()
    • yCenter

      public double yCenter()
    • getEnclosingRect

      public Rectangle.Mutable getEnclosingRect()
      Returns a new Rectangle the bounds of which will enclose this element and all of its child elements recursively.
    • addBoundsToRect

      public Rectangle.Mutable addBoundsToRect(Rectangle.Mutable enclosingRect)
      Expands the bounds of the given rectangle (if needed) so that they enclose this element. And all of its child elements recursively.
    • getChildBounds

      public Rectangle.Mutable getChildBounds()
      Returns:
      a rectangle, the bounds of which enclose all enabled child elements. If there are no enabled child elements the returned rect will have the position of this element, with zero size.
    • placeInside

      public T placeInside(ConstrainedGeometry<?> reference, Constraints.LayoutPos position)
      Constrain this element to a position inside the specified targetElement. See the following image for an example of what each LayoutPos does: https://ss.brandon3055.com/e89a6
      Parameters:
      reference - The reference element, the element that target will be placed inside.
      position - The layout position.
    • placeInside

      public T placeInside(ConstrainedGeometry<?> reference, Constraints.LayoutPos position, double xOffset, double yOffset)
      Constrain this element to a position inside the specified targetElement. See the following image for an example of what each LayoutPos does: https://ss.brandon3055.com/e89a6
      Parameters:
      reference - The reference element, the element that target will be placed inside.
      position - The layout position.
      xOffset - Optional X offset to be applied to the final position.
      yOffset - Optional Y offset to be applied to the final position.
    • placeOutside

      public T placeOutside(ConstrainedGeometry<?> reference, Constraints.LayoutPos position)
      Constrain this element to a position outside the specified targetElement. See the following image for an example of what each LayoutPos does: https://ss.brandon3055.com/baa7c
      Parameters:
      reference - The reference element, the element that target will be placed outside of.
      position - The layout position.
    • placeOutside

      public T placeOutside(ConstrainedGeometry<?> reference, Constraints.LayoutPos position, double xOffset, double yOffset)
      Constrain this element to a position outside the specified targetElement. See the following image for an example of what each LayoutPos does: https://ss.brandon3055.com/baa7c
      Parameters:
      reference - The reference element, the element that target will be placed outside of.
      position - The layout position.
      xOffset - Optional X offset to be applied to the final position.
      yOffset - Optional Y offset to be applied to the final position.