diff --git a/src/main/java/speiger/src/coreengine/rendering/newGui/components/TestComponent.java b/src/main/java/speiger/src/coreengine/rendering/newGui/components/TestComponent.java index 875aded..cef1248 100644 --- a/src/main/java/speiger/src/coreengine/rendering/newGui/components/TestComponent.java +++ b/src/main/java/speiger/src/coreengine/rendering/newGui/components/TestComponent.java @@ -8,4 +8,7 @@ public class TestComponent extends GuiComponent implements IInteractable { public TestComponent(IGuiBox box) { super(box); } + + @Override + public void init() {} } diff --git a/src/main/java/speiger/src/coreengine/rendering/newGui/components/base/GuiComponent.java b/src/main/java/speiger/src/coreengine/rendering/newGui/components/base/GuiComponent.java index c6cf34a..987ebd1 100644 --- a/src/main/java/speiger/src/coreengine/rendering/newGui/components/base/GuiComponent.java +++ b/src/main/java/speiger/src/coreengine/rendering/newGui/components/base/GuiComponent.java @@ -5,19 +5,19 @@ import java.util.function.Consumer; import speiger.src.collections.objects.lists.ObjectArrayList; import speiger.src.coreengine.rendering.gui.helper.box.IGuiBox; -import speiger.src.coreengine.rendering.newGui.layout.ConstraintContainer; +import speiger.src.coreengine.rendering.newGui.layout.constraints.ConstraintContainer; import speiger.src.coreengine.rendering.newGui.renderer.IUIRenderer; import speiger.src.coreengine.utils.collections.CollectionUtils; import speiger.src.coreengine.utils.collections.FlagObject; import speiger.src.coreengine.utils.misc.ICastable; -public non-sealed class GuiComponent extends FlagObject implements ICastable, ILayoutComponent, IListableComponent { +public abstract non-sealed class GuiComponent extends FlagObject implements ICastable, ILayoutComponent, IListableComponent { private static final int FLAG_FOCUSED = 1 << 0; private static final int FLAG_ENABLED = 1 << 1; private static final int FLAG_VISIBLE = 1 << 2; private static final int FLAG_MANUAL_MANAGED = 1 << 3; private static final int FLAG_SCISSORED = 1 << 4; - IGuiBox box; + final IGuiBox box; ConstraintContainer constraints; Object animator; // TODO implement GuiComponent parent; @@ -47,6 +47,12 @@ public non-sealed class GuiComponent extends FlagObject implements ICastable, IL } } + public abstract void init(); + public void close() { + notifyListeners(LISTENER_CLOSED); + } + + public void updateComponent() { children.forEach(GuiComponent::updateComponent); } @@ -81,6 +87,32 @@ public non-sealed class GuiComponent extends FlagObject implements ICastable, IL return true; } + public GuiComponent addChild(GuiComponent child) { return addChild(child, null); } + + public GuiComponent addChild(GuiComponent child, ConstraintContainer constraints) { + if(child.parent != null) throw new IllegalArgumentException("A Child can not have multiple parents"); + child.constraints = constraints; + child.parent = this; + children.add(child); + box.addChild(child.getBox()); + interactions.add(child.interactions); + child.init(); + return child; + } + + public boolean containsChild(GuiComponent child) { + return child.parent == this && children.contains(child); + } + + public void removeChild(GuiComponent child) { + if(child.parent != this) throw new IllegalArgumentException("Child isn't owned by this Component"); + child.close(); + child.parent = null; + children.remove(child); + box.removeChild(child.getBox()); + interactions.remove(child.interactions); + } + @Override public GuiComponent addListener(Consumer listener, int index) { listeners[index].add(listener); @@ -116,9 +148,7 @@ public non-sealed class GuiComponent extends FlagObject implements ICastable, IL } } - protected void repaint() { - - } + protected void repaint() {} @Override public IGuiBox getBox() { return box; } diff --git a/src/main/java/speiger/src/coreengine/rendering/newGui/layout/ConstraintContainer.java b/src/main/java/speiger/src/coreengine/rendering/newGui/layout/ConstraintContainer.java deleted file mode 100644 index fa10830..0000000 --- a/src/main/java/speiger/src/coreengine/rendering/newGui/layout/ConstraintContainer.java +++ /dev/null @@ -1,56 +0,0 @@ -package speiger.src.coreengine.rendering.newGui.layout; - -import speiger.src.coreengine.rendering.newGui.components.base.GuiComponent; -import speiger.src.coreengine.rendering.newGui.layout.IConstraint.Target; - -public class ConstraintContainer { - private static final int CONSTRAINT_LENGTH = 4; - IConstraint[] constraints = new IConstraint[CONSTRAINT_LENGTH]; - - private ConstraintContainer() {} - - public void apply(GuiComponent owner, GuiComponent parent) { - for(int i = 0;i<4;i++) { - if(constraints[i] == null) continue; - constraints[i].apply(owner, parent, Target.by(i)); - } - } - - public static Builder builder() { return new Builder(); } - public Builder copy() { return new Builder(this); } - - public static class Builder { - ConstraintContainer container = new ConstraintContainer(); - - private Builder() {} - private Builder(ConstraintContainer container) { - System.arraycopy(container.constraints, 0, this.container.constraints, 0, CONSTRAINT_LENGTH); - } - - public Builder x(IConstraint value) { - container.constraints[Target.X.ordinal()] = value; - return this; - } - - public Builder y(IConstraint value) { - container.constraints[Target.Y.ordinal()] = value; - return this; - } - - public Builder width(IConstraint value) { - container.constraints[Target.WIDTH.ordinal()] = value; - return this; - } - - public Builder height(IConstraint value) { - container.constraints[Target.HEIGHT.ordinal()] = value; - return this; - } - - public ConstraintContainer build() { - var result = container; - container = null; - return result; - } - } -} diff --git a/src/main/java/speiger/src/coreengine/rendering/newGui/layout/Constraints.java b/src/main/java/speiger/src/coreengine/rendering/newGui/layout/Constraints.java deleted file mode 100644 index 0006d92..0000000 --- a/src/main/java/speiger/src/coreengine/rendering/newGui/layout/Constraints.java +++ /dev/null @@ -1,67 +0,0 @@ -package speiger.src.coreengine.rendering.newGui.layout; - -import java.util.function.BooleanSupplier; - -import speiger.src.coreengine.rendering.gui.helper.box.IGuiBox; -import speiger.src.coreengine.rendering.newGui.components.base.GuiComponent; -import speiger.src.coreengine.rendering.newGui.layout.IConstraint.ISimpleConstraint; - -public class Constraints { - public static record Pixels(float value, boolean inverted) implements ISimpleConstraint { - - public static Pixels of(float value) { return new Pixels(value, false); } - public static Pixels inverted(float value) { return new Pixels(value, true); } - - @Override - public void apply(IGuiBox owner, IGuiBox parent, Target target) { - target.set(owner, inverted ? target.asArea().get(parent) - value : value); - } - } - - public static record Parent(float padding, boolean inv) implements ISimpleConstraint { - - public static Parent of() { return new Parent(0F, false); } - public static Parent of(float padding) { return new Parent(padding, false); } - public static Parent inverted() { return new Parent(0F, true); } - public static Parent inverted(float padding) { return new Parent(padding, true); } - - @Override - public void apply(IGuiBox owner, IGuiBox parent, Target target) { - if(inv) target.set(owner, target.isPosition() ? target.asArea().get(parent) - padding : padding * 2); - else target.set(owner, target.isPosition() ? padding : target.get(parent) - padding * 2); - } - } - - public static record Relative(float value, float padding) implements ISimpleConstraint { - - public static Relative of(float value) { return new Relative(value, 0F); } - public static Relative of(float value, float padding) { return new Relative(value, padding); } - - @Override - public void apply(IGuiBox owner, IGuiBox parent, Target target) { - target.set(owner, value * (target.asArea().get(parent) - padding)); - } - } - - public static record Center() implements ISimpleConstraint { - private static final Center INSTANCE = new Center(); - - public static Center of() { return INSTANCE; } - - @Override - public void apply(IGuiBox owner, IGuiBox parent, Target target) { - Target bounds = target.asArea(); - target.set(owner, (bounds.get(parent) * 0.5F) - (bounds.get(owner) * 0.5F)); - } - } - - public static record Conditional(BooleanSupplier supplier, IConstraint onTrue, IConstraint onFalse) implements IConstraint { - - public static Conditional of(BooleanSupplier supplier, IConstraint onTrue, IConstraint onFalse) { return new Conditional(supplier, onTrue, onFalse); } - - @Override - public void apply(GuiComponent owner, GuiComponent child, Target target) { - (supplier.getAsBoolean() ? onTrue : onFalse).apply(owner, child, target); - } - } -} \ No newline at end of file diff --git a/src/main/java/speiger/src/coreengine/rendering/newGui/layout/IConstraint.java b/src/main/java/speiger/src/coreengine/rendering/newGui/layout/IConstraint.java deleted file mode 100644 index d0f6761..0000000 --- a/src/main/java/speiger/src/coreengine/rendering/newGui/layout/IConstraint.java +++ /dev/null @@ -1,78 +0,0 @@ -package speiger.src.coreengine.rendering.newGui.layout; - -import java.util.function.BooleanSupplier; - -import speiger.src.coreengine.rendering.gui.helper.box.IGuiBox; -import speiger.src.coreengine.rendering.newGui.components.base.GuiComponent; -import speiger.src.coreengine.rendering.newGui.layout.Constraints.Center; -import speiger.src.coreengine.rendering.newGui.layout.Constraints.Conditional; -import speiger.src.coreengine.rendering.newGui.layout.Constraints.Parent; -import speiger.src.coreengine.rendering.newGui.layout.Constraints.Pixels; -import speiger.src.coreengine.rendering.newGui.layout.Constraints.Relative; - -public interface IConstraint { - public void apply(GuiComponent owner, GuiComponent child, Target target); - - - public static IConstraint pixels(float value) { return Pixels.of(value); } - public static IConstraint pixelsInv(float value) { return Pixels.inverted(value); } - - public static IConstraint parent() { return Parent.of(); } - public static IConstraint parent(float padding) { return Parent.of(padding); } - public static IConstraint parentInv() { return Parent.inverted(); } - public static IConstraint parentInv(float padding) { return Parent.inverted(padding); } - - public static IConstraint relative(float value) { return Relative.of(value); } - public static IConstraint relative(float value, float padding) { return Relative.of(value, padding); } - - public static IConstraint center() { return Center.of(); } - public static IConstraint conditiojnal(BooleanSupplier supplier, IConstraint onTrue, IConstraint onFalse) { return Conditional.of(supplier, onTrue, onFalse); } - - public static interface ISimpleConstraint extends IConstraint { - @Override - default void apply(GuiComponent owner, GuiComponent parent, Target target) { apply(owner.getBox(), parent.getBox(), target); } - public void apply(IGuiBox owner, IGuiBox parent, Target target); - } - - public static enum Target { - X, - Y, - WIDTH, - HEIGHT; - - static final Target[] BY_INDEX = values(); - - public static Target by(int index) { return BY_INDEX[index & 3]; } - public static Target pos(boolean x) { return x ? X : Y; } - public static Target bounds(boolean x) { return x ? WIDTH : HEIGHT; } - public boolean isPosition() { return this == X || this == Y; } - public Target asArea() { - return switch(this) { - case X -> WIDTH; - case Y -> HEIGHT; - default -> this; - }; - } - - public float get(GuiComponent comp) { return get(comp.getBox()); } - public void set(GuiComponent comp, float value) { set(comp.getBox(), value); } - - public float get(IGuiBox box) { - return switch(this) { - case X -> box.getMinX(); - case Y -> box.getMinY(); - case WIDTH -> box.getBaseWidth(); - case HEIGHT -> box.getBaseHeight(); - }; - } - - public void set(IGuiBox box, float value) { - switch(this) { - case X -> box.setX(value); - case Y -> box.setY(value); - case WIDTH -> box.setWidth(value); - case HEIGHT -> box.setHeight(value); - } - } - } -} diff --git a/src/main/java/speiger/src/coreengine/rendering/newGui/layout/constraints/ConstraintContainer.java b/src/main/java/speiger/src/coreengine/rendering/newGui/layout/constraints/ConstraintContainer.java index 29a86f1..3d23c88 100644 --- a/src/main/java/speiger/src/coreengine/rendering/newGui/layout/constraints/ConstraintContainer.java +++ b/src/main/java/speiger/src/coreengine/rendering/newGui/layout/constraints/ConstraintContainer.java @@ -45,7 +45,7 @@ public class ConstraintContainer { public Builder height(IConstraint value) { container.constraints[Target.HEIGHT.ordinal()] = value; return this; - } + } public ConstraintContainer build() { var result = container; diff --git a/src/main/java/speiger/src/coreengine/rendering/newGui/layout/constraints/Constraints.java b/src/main/java/speiger/src/coreengine/rendering/newGui/layout/constraints/Constraints.java index 4b5b36b..b586c2e 100644 --- a/src/main/java/speiger/src/coreengine/rendering/newGui/layout/constraints/Constraints.java +++ b/src/main/java/speiger/src/coreengine/rendering/newGui/layout/constraints/Constraints.java @@ -1,6 +1,9 @@ package speiger.src.coreengine.rendering.newGui.layout.constraints; +import java.util.function.BooleanSupplier; + import speiger.src.coreengine.rendering.gui.helper.box.IGuiBox; +import speiger.src.coreengine.rendering.newGui.components.base.GuiComponent; import speiger.src.coreengine.rendering.newGui.layout.constraints.IConstraint.ISimpleConstraint; public class Constraints { @@ -24,7 +27,41 @@ public class Constraints { @Override public void apply(IGuiBox owner, IGuiBox parent, Target target) { - //TODO implement + if(inv) target.set(owner, target.isPosition() ? target.asArea().get(parent) - padding : padding * 2); + else target.set(owner, target.isPosition() ? padding : target.get(parent) - padding * 2); + } + } + + public static record Relative(float value, float padding) implements ISimpleConstraint { + + public static Relative of(float value) { return new Relative(value, 0F); } + public static Relative of(float value, float padding) { return new Relative(value, padding); } + + @Override + public void apply(IGuiBox owner, IGuiBox parent, Target target) { + target.set(owner, value * (target.asArea().get(parent) - padding)); + } + } + + public static record Center() implements ISimpleConstraint { + private static final Center INSTANCE = new Center(); + + public static Center of() { return INSTANCE; } + + @Override + public void apply(IGuiBox owner, IGuiBox parent, Target target) { + Target bounds = target.asArea(); + target.set(owner, (bounds.get(parent) * 0.5F) - (bounds.get(owner) * 0.5F)); + } + } + + public static record Conditional(BooleanSupplier supplier, IConstraint onTrue, IConstraint onFalse) implements IConstraint { + + public static Conditional of(BooleanSupplier supplier, IConstraint onTrue, IConstraint onFalse) { return new Conditional(supplier, onTrue, onFalse); } + + @Override + public void apply(GuiComponent owner, GuiComponent child, Target target) { + (supplier.getAsBoolean() ? onTrue : onFalse).apply(owner, child, target); } } } diff --git a/src/main/java/speiger/src/coreengine/rendering/newGui/layout/constraints/IConstraint.java b/src/main/java/speiger/src/coreengine/rendering/newGui/layout/constraints/IConstraint.java index 9d10426..2bc9bf2 100644 --- a/src/main/java/speiger/src/coreengine/rendering/newGui/layout/constraints/IConstraint.java +++ b/src/main/java/speiger/src/coreengine/rendering/newGui/layout/constraints/IConstraint.java @@ -1,11 +1,33 @@ package speiger.src.coreengine.rendering.newGui.layout.constraints; +import java.util.function.BooleanSupplier; + import speiger.src.coreengine.rendering.gui.helper.box.IGuiBox; import speiger.src.coreengine.rendering.newGui.components.base.GuiComponent; +import speiger.src.coreengine.rendering.newGui.layout.constraints.Constraints.Center; +import speiger.src.coreengine.rendering.newGui.layout.constraints.Constraints.Conditional; +import speiger.src.coreengine.rendering.newGui.layout.constraints.Constraints.Parent; +import speiger.src.coreengine.rendering.newGui.layout.constraints.Constraints.Pixels; +import speiger.src.coreengine.rendering.newGui.layout.constraints.Constraints.Relative; public interface IConstraint { public void apply(GuiComponent owner, GuiComponent child, Target target); + + public static IConstraint pixels(float value) { return Pixels.of(value); } + public static IConstraint pixelsInv(float value) { return Pixels.inverted(value); } + + public static IConstraint parent() { return Parent.of(); } + public static IConstraint parent(float padding) { return Parent.of(padding); } + public static IConstraint parentInv() { return Parent.inverted(); } + public static IConstraint parentInv(float padding) { return Parent.inverted(padding); } + + public static IConstraint relative(float value) { return Relative.of(value); } + public static IConstraint relative(float value, float padding) { return Relative.of(value, padding); } + + public static IConstraint center() { return Center.of(); } + public static IConstraint conditiojnal(BooleanSupplier supplier, IConstraint onTrue, IConstraint onFalse) { return Conditional.of(supplier, onTrue, onFalse); } + public static interface ISimpleConstraint extends IConstraint { @Override default void apply(GuiComponent owner, GuiComponent parent, Target target) { apply(owner.getBox(), parent.getBox(), target); }