More work on Components

This commit is contained in:
Speiger 2024-04-28 20:21:36 +02:00
parent 9dca822b21
commit 4292ea266f
8 changed files with 100 additions and 209 deletions

View File

@ -8,4 +8,7 @@ public class TestComponent extends GuiComponent implements IInteractable {
public TestComponent(IGuiBox box) {
super(box);
}
@Override
public void init() {}
}

View File

@ -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<GuiComponent> 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; }

View File

@ -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;
}
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}
}

View File

@ -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;

View File

@ -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);
}
}
}

View File

@ -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); }