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 index e2445cd..fa10830 100644 --- a/src/main/java/speiger/src/coreengine/rendering/newGui/layout/ConstraintContainer.java +++ b/src/main/java/speiger/src/coreengine/rendering/newGui/layout/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.java b/src/main/java/speiger/src/coreengine/rendering/newGui/layout/Constraints.java index 2e795e0..0006d92 100644 --- a/src/main/java/speiger/src/coreengine/rendering/newGui/layout/Constraints.java +++ b/src/main/java/speiger/src/coreengine/rendering/newGui/layout/Constraints.java @@ -1,6 +1,9 @@ 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 { @@ -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); + } + } +} \ 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 index c8a1cc7..d0f6761 100644 --- a/src/main/java/speiger/src/coreengine/rendering/newGui/layout/IConstraint.java +++ b/src/main/java/speiger/src/coreengine/rendering/newGui/layout/IConstraint.java @@ -1,11 +1,33 @@ 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); } 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() {