diff --git a/src/main/java/speiger/src/coreengine/rendering/gui/components/base/ILayoutComponent.java b/src/main/java/speiger/src/coreengine/rendering/gui/components/base/ILayoutComponent.java index 11c5ec3..a56a770 100644 --- a/src/main/java/speiger/src/coreengine/rendering/gui/components/base/ILayoutComponent.java +++ b/src/main/java/speiger/src/coreengine/rendering/gui/components/base/ILayoutComponent.java @@ -47,7 +47,7 @@ public interface ILayoutComponent { public ArrayFetcher(boolean includeInvisible) { this.includeInvisible = includeInvisible; } - + @Override public boolean acceptsInvisble() { return includeInvisible; @@ -61,8 +61,27 @@ public interface ILayoutComponent { result[INDEX_MAX_Y] = Math.max(maxY, result[INDEX_MAX_Y]); } - public float[] getResult() { + public void reset() { + result[0] = Float.MAX_VALUE; + result[1] = Float.MAX_VALUE; + result[2] = -Float.MAX_VALUE; + result[3] = -Float.MAX_VALUE; + } + + public void hardReset() { + result = new float[] {Float.MAX_VALUE, Float.MAX_VALUE, -Float.MAX_VALUE, -Float.MAX_VALUE}; + } + + public float[] result() { return result; } + + public float[] unscaledResult(IGuiBox owner) { + float[] unscaled = new float[4]; + for(int i = 0;i<4;i++) { + unscaled[i] = result[i] / owner.getScale(); + } + return unscaled; + } } } diff --git a/src/main/java/speiger/src/coreengine/rendering/gui/font/Font.java b/src/main/java/speiger/src/coreengine/rendering/gui/font/Font.java index 7d1942d..605ed50 100644 --- a/src/main/java/speiger/src/coreengine/rendering/gui/font/Font.java +++ b/src/main/java/speiger/src/coreengine/rendering/gui/font/Font.java @@ -10,7 +10,7 @@ import speiger.src.coreengine.rendering.gui.font.glyth.UnbakedGlyth.GlythBaker; import speiger.src.coreengine.rendering.tesselation.buffer.IVertexBuilder; public class Font { - public static final FontStyle DEFAULT = new FontStyle(AssetLocation.of("default"), 18); + public static final FontStyle DEFAULT = new FontStyle(AssetLocation.of("default"), 0, 18F); Map fonts; GlythBaker baker; @@ -30,12 +30,12 @@ public class Font { } } - public GlythData data(FontStyle font, int codepoint, int style) { - return styledCache[style & 0x3].data(font, codepoint); + public GlythData data(FontStyle font, int codepoint) { + return styledCache[font.style() & 0x3].data(font, codepoint); } - public Glyth glyth(FontStyle font, int codepoint, int style) { - return styledCache[style & 0x3].glyth(font, codepoint); + public Glyth glyth(FontStyle font, int codepoint) { + return styledCache[font.style() & 0x3].glyth(font, codepoint); } protected FontGroup font(AssetLocation font) { @@ -51,11 +51,11 @@ public class Font { int previousCodepoint = -1; for(int i = 0,m=text.length();i 3) throw new IllegalArgumentException("Style can only be between 0-3. Style found: "+style); + } + + public FontStyle(AssetLocation font, float size) { + this(font, REGULAR, size); + } + + public FontStyle(AssetLocation font, boolean bold, boolean italic, float size) { + this(font, (bold ? BOLD : 0) | (italic ? ITALIC : 0), size); + } + + public FontStyle with(AssetLocation font, int style, float size) { + return new FontStyle(font, style, size); + } public FontStyle with(AssetLocation font, float size) { - return new FontStyle(font, size); + return new FontStyle(font, style, size); } public FontStyle with(AssetLocation font) { - return new FontStyle(font, size); + return new FontStyle(font, style, size); } public FontStyle with(float size) { - return new FontStyle(font, size); + return new FontStyle(font, style, size); } -} + + public FontStyle with(int style) { + return new FontStyle(font, style, size); + } + + public FontStyle withBold(boolean bold) { + return new FontStyle(font, bold ? (style | BOLD) : (style & ~BOLD), size); + } + + public FontStyle withItalic(boolean italic) { + return new FontStyle(font, italic ? (style | ITALIC) : (style & ~ITALIC), size); + } + + public boolean bold() { + return (style() & BOLD) != 0; + } + + public boolean italic() { + return (style() & ITALIC) != 0; + } +} \ No newline at end of file diff --git a/src/main/java/speiger/src/coreengine/rendering/gui/layout/constraints/Constraints.java b/src/main/java/speiger/src/coreengine/rendering/gui/layout/constraints/Constraints.java index 5e5ec37..cf86828 100644 --- a/src/main/java/speiger/src/coreengine/rendering/gui/layout/constraints/Constraints.java +++ b/src/main/java/speiger/src/coreengine/rendering/gui/layout/constraints/Constraints.java @@ -1,5 +1,6 @@ package speiger.src.coreengine.rendering.gui.layout.constraints; +import java.util.List; import java.util.function.BooleanSupplier; import speiger.src.coreengine.rendering.gui.components.base.GuiComponent; @@ -32,6 +33,26 @@ public class Constraints { } } + public static record Children(float padding, boolean includeChildOffsets) implements ISimpleConstraint { + + public static Children onlyBounds() { return new Children(0F, false); } + public static Children onlyBounds(float padding) { return new Children(padding, false); } + public static Children withPos() { return new Children(0F, true); } + public static Children withPos(float padding) { return new Children(padding, true); } + + @Override + public void apply(IGuiBox owner, IGuiBox parent, Target target) { + if(target.isPosition()) return; + float value = 0F; + List children = owner.children(); + for(int i = 0,m=children.size();i box.getMinX(); - case Y -> box.getMinY(); + case X -> box.getBaseX(); + case Y -> box.getBaseY(); case WIDTH -> box.getBaseWidth(); case HEIGHT -> box.getBaseHeight(); }; diff --git a/src/main/java/speiger/src/coreengine/rendering/gui/layout/layouts/FlowLayout.java b/src/main/java/speiger/src/coreengine/rendering/gui/layout/layouts/FlowLayout.java new file mode 100644 index 0000000..e938110 --- /dev/null +++ b/src/main/java/speiger/src/coreengine/rendering/gui/layout/layouts/FlowLayout.java @@ -0,0 +1,157 @@ +package speiger.src.coreengine.rendering.gui.layout.layouts; + +import java.util.List; + +import speiger.src.collections.objects.lists.ObjectArrayList; +import speiger.src.coreengine.rendering.gui.components.base.GuiComponent; +import speiger.src.coreengine.rendering.gui.components.base.ILayoutComponent.ArrayFetcher; +import speiger.src.coreengine.rendering.guiOld.helper.box.IGuiBox; + +public class FlowLayout implements ILayout { + private static final int ALIGN_DISABLED = -1; + public static final int ALIGN_START = 0; + public static final int ALIGN_CENTER = 1; + public static final int ALIGN_END = 2; + + private List comps = new ObjectArrayList<>(); + protected float xGap; + protected float yGap; + protected int direction = ALIGN_END; + protected int verticalAlignement = ALIGN_DISABLED; + protected boolean includeInvisible = false; + protected boolean isVertical = false; + + + public FlowLayout(int direction) { + this.direction = direction; + } + + public FlowLayout(float xGap, float yGap) { + this.xGap = xGap; + this.yGap = yGap; + } + + public FlowLayout(float xGap, float yGap, int direction) { + this.xGap = xGap; + this.yGap = yGap; + this.direction = direction; + } + + public FlowLayout verticalAlign(int alignment) { + verticalAlignement = Math.clamp(alignment, -1, 2); + return this; + } + + public FlowLayout includeInvisible() { + includeInvisible = true; + return this; + } + + public FlowLayout vertical(boolean value) { + isVertical = value; + return this; + } + + @Override + public FlowLayout add(GuiComponent comp, Object value) { + comps.add(comp); + return this; + } + + @Override + public FlowLayout remove(GuiComponent comp) { + comps.remove(comp); + return this; + } + + @Override + public void apply(GuiComponent owner) { + int size = comps.size(); + float max = isVertical ? owner.getBox().getBaseHeight() : owner.getBox().getBaseWidth(); + float x = xGap; + float y = yGap; + float[] heights = new float[size]; + float[] widths = new float[size]; + int startIndex = 0; + ArrayFetcher fetcher = new ArrayFetcher(includeInvisible); + for(int i = 0,m=comps.size();i max) { + x += processComponents(startIndex, i, x, max, y, widths, heights); + y = yGap; + startIndex = i; + } + y += height + yGap; + fetcher.reset(); + continue; + } + else if(x + width + xGap > max) { + y += processComponents(startIndex, i, y, max, x, widths, heights); + x = xGap; + startIndex = i; + } + x += width + xGap; + fetcher.reset(); + } + processComponents(startIndex, size, y, max, x, widths, heights); + } + + private void setX(IGuiBox box, float x) { + if(isVertical) box.setY(x); + else box.setX(x); + } + + private void setY(IGuiBox box, float y) { + if(isVertical) box.setX(y); + else box.setY(y); + } + + private float processComponents(int start, int end, float yStart, float maxWidth, float widthSum, float[] widths, float[] heights) { + float x = calculateStartPos(start, end, maxWidth, widthSum); + float yAligner = 0F; + if(verticalAlignement > 0) { + for(int i = start;i ownerWidth * 0.5F - widthSum * 0.5F; + case ALIGN_END -> ownerWidth - xGap; + default -> xGap; + }; + } +} diff --git a/src/main/java/speiger/src/coreengine/rendering/gui/layout/layouts/ILayout.java b/src/main/java/speiger/src/coreengine/rendering/gui/layout/layouts/ILayout.java index 63d574a..880e0bc 100644 --- a/src/main/java/speiger/src/coreengine/rendering/gui/layout/layouts/ILayout.java +++ b/src/main/java/speiger/src/coreengine/rendering/gui/layout/layouts/ILayout.java @@ -1,11 +1,15 @@ package speiger.src.coreengine.rendering.gui.layout.layouts; +import java.util.function.Consumer; + import speiger.src.coreengine.rendering.gui.components.base.GuiComponent; -public interface ILayout { - public default ILayout add(GuiComponent comp) { return add(comp, null); } - public ILayout add(GuiComponent comp, Object value); - public ILayout remove(GuiComponent comp); +public interface ILayout extends Consumer { + public default ILayout add(GuiComponent comp) { return add(comp, null); } + public ILayout add(GuiComponent comp, T value); + public ILayout remove(GuiComponent comp); - public void apply(); + public void apply(GuiComponent owner); + @Override + default void accept(GuiComponent t) { apply(t); } } diff --git a/src/main/java/speiger/src/coreengine/rendering/guiOld/helper/box/GuiBox.java b/src/main/java/speiger/src/coreengine/rendering/guiOld/helper/box/GuiBox.java index 6a62c15..dc8b11b 100644 --- a/src/main/java/speiger/src/coreengine/rendering/guiOld/helper/box/GuiBox.java +++ b/src/main/java/speiger/src/coreengine/rendering/guiOld/helper/box/GuiBox.java @@ -3,10 +3,11 @@ package speiger.src.coreengine.rendering.guiOld.helper.box; import java.util.List; import speiger.src.collections.objects.lists.ObjectArrayList; +import speiger.src.collections.objects.lists.ObjectList; public class GuiBox implements IGuiBox { - List children = new ObjectArrayList<>(); + ObjectList children = new ObjectArrayList<>(); IGuiBox parent; float minX; @@ -62,6 +63,11 @@ public class GuiBox implements IGuiBox return this; } + @Override + public List children() { + return children.unmodifiable(); + } + @Override public IGuiBox onChanged() { minX = parent == null ? baseX : parent.getMinX(baseX); diff --git a/src/main/java/speiger/src/coreengine/rendering/guiOld/helper/box/IGuiBox.java b/src/main/java/speiger/src/coreengine/rendering/guiOld/helper/box/IGuiBox.java index e191630..02c9f8b 100644 --- a/src/main/java/speiger/src/coreengine/rendering/guiOld/helper/box/IGuiBox.java +++ b/src/main/java/speiger/src/coreengine/rendering/guiOld/helper/box/IGuiBox.java @@ -1,5 +1,7 @@ package speiger.src.coreengine.rendering.guiOld.helper.box; +import java.util.List; + import speiger.src.coreengine.math.misc.Facing; import speiger.src.coreengine.math.misc.FacingList; @@ -8,6 +10,7 @@ public interface IGuiBox extends IScreenBox public IGuiBox addChild(IGuiBox box); public IGuiBox removeChild(IGuiBox box); public IGuiBox clearChildren(); + public List children(); public IGuiBox setParent(IGuiBox box); public IGuiBox getParent(); public IGuiBox onChanged(); diff --git a/src/main/java/speiger/src/coreengine/rendering/guiOld/helper/box/ParentBox.java b/src/main/java/speiger/src/coreengine/rendering/guiOld/helper/box/ParentBox.java index 9028d3c..fcbaf58 100644 --- a/src/main/java/speiger/src/coreengine/rendering/guiOld/helper/box/ParentBox.java +++ b/src/main/java/speiger/src/coreengine/rendering/guiOld/helper/box/ParentBox.java @@ -3,10 +3,11 @@ package speiger.src.coreengine.rendering.guiOld.helper.box; import java.util.List; import speiger.src.collections.objects.lists.ObjectArrayList; +import speiger.src.collections.objects.lists.ObjectList; public class ParentBox implements IGuiBox { - List children = new ObjectArrayList<>(); + ObjectList children = new ObjectArrayList<>(); IGuiBox parent; float minX; @@ -50,6 +51,11 @@ public class ParentBox implements IGuiBox return this; } + @Override + public List children() { + return children.unmodifiable(); + } + @Override public IGuiBox onChanged() { for(int i = 0,m=children.size();i