Class ConstrainedGeometry<T extends ConstrainedGeometry<T>>
- All Implemented Interfaces:
GuiParent<T>
- Direct Known Subclasses:
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddBoundsToRect(Rectangle.Mutable enclosingRect) Expands the bounds of the given rectangle (if needed) so that they enclose this element.Clear any configured constraints and reset this element to default unconstrained state.voidconstrain(GeoParam param, @Nullable Constraint constraint) Returns a reference to the specified geometry parameter.Returns a newRectanglethe bounds of which will enclose this element and all of its child elements recursively.abstract @NotNull GuiParent<?>Returns aPositionthat is permanently bound to this element.Returns aRectanglethat is permanently bound to this element.placeInside(ConstrainedGeometry<?> reference, Constraints.LayoutPos position) Constrain this element to a position inside the specified targetElement.placeInside(ConstrainedGeometry<?> reference, Constraints.LayoutPos position, double xOffset, double yOffset) Constrain this element to a position inside the specified targetElement.placeOutside(ConstrainedGeometry<?> reference, Constraints.LayoutPos position) Constrain this element to a position outside the specified targetElement.placeOutside(ConstrainedGeometry<?> reference, Constraints.LayoutPos position, double xOffset, double yOffset) Constrain this element to a position outside the specified targetElement.setHeight(double height) Simple method for setting the height of this element.setPos(double x, double y) Convenience method for setting both x and y positions.setSize(double width, double height) Convenience method for setting both width and height.setWidth(double width) Simple method for setting the width of this element.setXPos(double x) Simple method for setting the x position of this element.setYPos(double y) Simple method for setting the y position of this element.strictMode(boolean strictMode) Strict mode is intended to help catch potential mistakes when writing modular GUIsvoidvalidate()doublexCenter()doublexMax()doublexMin()doublexSize()doubleyCenter()doubleyMax()doubleyMin()doubleySize()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface codechicken.lib.gui.modular.lib.geometry.GuiParent
addChild, addChild, adoptChild, blockMouseOver, font, getChildren, getModularGui, getValue, isDescendantOf, mc, onScreenInit, removeChild, scaledScreenHeight, scaledScreenWidth
-
Constructor Details
-
ConstrainedGeometry
public ConstrainedGeometry()
-
-
Method Details
-
getParent
-
getParent
-
setXPos
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
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
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
Simple method for setting the width of this element.- Parameters:
width- The width to apply.- Returns:
- This Element.
-
setHeight
Simple method for setting the height of this element.- Parameters:
height- The height to apply.- Returns:
- This Element.
-
setSize
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:
xMinin interfaceGuiParent<T extends ConstrainedGeometry<T>>- Returns:
- The position of the Left edge of this element.
-
xMax
public double xMax()- Specified by:
xMaxin interfaceGuiParent<T extends ConstrainedGeometry<T>>- Returns:
- The position of the Right edge of this element.
-
xSize
public double xSize()- Specified by:
xSizein interfaceGuiParent<T extends ConstrainedGeometry<T>>- Returns:
- The Width of this element.
-
yMin
public double yMin()- Specified by:
yMinin interfaceGuiParent<T extends ConstrainedGeometry<T>>- Returns:
- The position of the Top edge of this element.
-
yMax
public double yMax()- Specified by:
yMaxin interfaceGuiParent<T extends ConstrainedGeometry<T>>- Returns:
- The position of the Bottom edge of this element.
-
ySize
public double ySize()- Specified by:
ySizein interfaceGuiParent<T extends ConstrainedGeometry<T>>- Returns:
- The Height of this element.
-
get
Returns a reference to the specified geometry parameter. This is primarily used when defining geometry constraints. But it can also be used as a simpleSupplier<Integer>that will return the current parameter value when requested.Note: The returned geometry reference will always be valid
- Specified by:
getin interfaceGuiParent<T extends ConstrainedGeometry<T>>- Parameters:
param- The geometry parameter.- Returns:
- A Geometry Reference
-
constrain
- Parameters:
param- The geometry parameter to be constrained.constraint- The constraint to apply- Returns:
- This Element.
-
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
Strict mode is intended to help catch potential mistakes when writing modular GUIsEnforces 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
Returns aPositionthat is permanently bound to this element. -
getRectangle
Returns aRectanglethat is permanently bound to this element. -
xCenter
public double xCenter() -
yCenter
public double yCenter() -
getEnclosingRect
Returns a newRectanglethe bounds of which will enclose this element and all of its child elements recursively. -
addBoundsToRect
Expands the bounds of the given rectangle (if needed) so that they enclose this element. And all of its child elements recursively. -
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
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
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.
-