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 083028b..c6cf34a 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,6 +5,7 @@ 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.renderer.IUIRenderer; import speiger.src.coreengine.utils.collections.CollectionUtils; import speiger.src.coreengine.utils.collections.FlagObject; @@ -17,7 +18,7 @@ public non-sealed class GuiComponent extends FlagObject implements ICastable, IL private static final int FLAG_MANUAL_MANAGED = 1 << 3; private static final int FLAG_SCISSORED = 1 << 4; IGuiBox box; - Object constraints; // TODO implement + ConstraintContainer constraints; Object animator; // TODO implement GuiComponent parent; List children = new ObjectArrayList<>(); @@ -102,9 +103,7 @@ public non-sealed class GuiComponent extends FlagObject implements ICastable, IL public void onChanged(boolean repaint) { // TODO implement changes xD - if(constraints != null) { - // TODO implement constraints - } + if(constraints != null) constraints.apply(this, parent); if(animator != null) { // TODO implement animations; } @@ -163,7 +162,7 @@ public non-sealed class GuiComponent extends FlagObject implements ICastable, IL public final GuiComponent scale(float newScale) { if(newScale != box.getBaseScale()) { box.setScale(newScale); - // TODO implement onChanged + onChanged(true); } return this; } 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 new file mode 100644 index 0000000..fa10830 --- /dev/null +++ b/src/main/java/speiger/src/coreengine/rendering/newGui/layout/ConstraintContainer.java @@ -0,0 +1,56 @@ +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 new file mode 100644 index 0000000..0006d92 --- /dev/null +++ b/src/main/java/speiger/src/coreengine/rendering/newGui/layout/Constraints.java @@ -0,0 +1,67 @@ +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 new file mode 100644 index 0000000..d0f6761 --- /dev/null +++ b/src/main/java/speiger/src/coreengine/rendering/newGui/layout/IConstraint.java @@ -0,0 +1,78 @@ +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/tesselation/buffer/VertexBuilder.java b/src/main/java/speiger/src/coreengine/rendering/tesselation/buffer/VertexBuilder.java index 30b0071..f3e8db1 100644 --- a/src/main/java/speiger/src/coreengine/rendering/tesselation/buffer/VertexBuilder.java +++ b/src/main/java/speiger/src/coreengine/rendering/tesselation/buffer/VertexBuilder.java @@ -2,6 +2,8 @@ package speiger.src.coreengine.rendering.tesselation.buffer; import java.nio.ByteBuffer; +import org.lwjgl.system.MemoryUtil; + import speiger.src.coreengine.rendering.tesselation.format.VertexElement; import speiger.src.coreengine.rendering.tesselation.format.VertexFormat; import speiger.src.coreengine.rendering.utils.values.GLMode; @@ -17,7 +19,7 @@ public class VertexBuilder implements IVertexBuilder { int elementBytes; public VertexBuilder(int size) { - this(ByteBuffer.allocate(size)); + this(MemoryUtil.memAlloc(size)); } public VertexBuilder(ByteBuffer buffer) { diff --git a/src/main/java/speiger/src/coreengine/rendering/textures/base/BaseTexture.java b/src/main/java/speiger/src/coreengine/rendering/textures/base/BaseTexture.java index 82f7349..ef6c765 100644 --- a/src/main/java/speiger/src/coreengine/rendering/textures/base/BaseTexture.java +++ b/src/main/java/speiger/src/coreengine/rendering/textures/base/BaseTexture.java @@ -32,7 +32,7 @@ public abstract class BaseTexture implements ITexture { } protected void track() { - GLStateTracker.instance().texture_tracker.registerTexture(this); + GLStateTracker.TEXTURE_TRACKER.registerTexture(this); } @Override @@ -49,7 +49,7 @@ public abstract class BaseTexture implements ITexture { public void delete(boolean untrack) { removeTexture(); if(untrack) { - GLStateTracker.instance().texture_tracker.deleteTexture(this); + GLStateTracker.TEXTURE_TRACKER.deleteTexture(this); } } } diff --git a/src/main/java/speiger/src/coreengine/rendering/utils/GLStateTracker.java b/src/main/java/speiger/src/coreengine/rendering/utils/GLStateTracker.java index 5c51d09..ca2090d 100644 --- a/src/main/java/speiger/src/coreengine/rendering/utils/GLStateTracker.java +++ b/src/main/java/speiger/src/coreengine/rendering/utils/GLStateTracker.java @@ -50,7 +50,7 @@ public class GLStateTracker { //Trackers public final ShaderTracker shaders = new ShaderTracker(); - public final TextureTracker texture_tracker = new TextureTracker(); + public static final TextureTracker TEXTURE_TRACKER = new TextureTracker(); public final GlobalUniforms uniforms = new GlobalUniforms(); public static GLStateTracker instance() {