Start of the layout new system

This commit is contained in:
Speiger 2024-04-23 22:17:21 +02:00
parent 7cedfaf9bd
commit b53bb321af
11 changed files with 193 additions and 275 deletions

View File

@ -32,10 +32,6 @@ public class GuiBox implements IGuiBox
onChanged(); onChanged();
} }
public static IGuiBox simple(float x, float y, float width, float height) {
return new GuiBox(x, y, width, height);
}
public static IGuiBox clone(IGuiBox box) { public static IGuiBox clone(IGuiBox box) {
return new GuiBox(box.getMinX(), box.getMinY(), box.getWidth(), box.getHeight()); return new GuiBox(box.getMinX(), box.getMinY(), box.getWidth(), box.getHeight());
} }

View File

@ -11,13 +11,14 @@ import speiger.src.coreengine.utils.collections.FlagObject;
import speiger.src.coreengine.utils.misc.ICastable; import speiger.src.coreengine.utils.misc.ICastable;
public non-sealed class GuiComponent extends FlagObject implements ICastable, ILayoutComponent, IListableComponent { public non-sealed class GuiComponent extends FlagObject implements ICastable, ILayoutComponent, IListableComponent {
private static final int FLAG_FOCUSED = 1<<0; private static final int FLAG_FOCUSED = 1 << 0;
private static final int FLAG_ENABLED = 1<<1; private static final int FLAG_ENABLED = 1 << 1;
private static final int FLAG_VISIBLE = 1<<2; private static final int FLAG_VISIBLE = 1 << 2;
private static final int FLAG_MANUAL_MANAGED = 1<<3; private static final int FLAG_MANUAL_MANAGED = 1 << 3;
private static final int FLAG_SCISSORED = 1<<4; private static final int FLAG_SCISSORED = 1 << 4;
IGuiBox box; IGuiBox box;
Object constraints; // TODO implement
Object animator; // TODO implement
GuiComponent parent; GuiComponent parent;
List<GuiComponent> children = new ObjectArrayList<>(); List<GuiComponent> children = new ObjectArrayList<>();
InteractionContainer interactions = new InteractionContainer(this::isInteractable); InteractionContainer interactions = new InteractionContainer(this::isInteractable);
@ -99,16 +100,35 @@ public non-sealed class GuiComponent extends FlagObject implements ICastable, IL
} }
} }
@Override public void onChanged(boolean repaint) {
public IGuiBox getBox() { // TODO implement changes xD
return box; if(constraints != null) {
// TODO implement constraints
}
if(animator != null) {
// TODO implement animations;
}
box.onChanged();
notifyListeners(LISTENER_ON_CHANGE);
if(repaint) repaint();
if(children.isEmpty()) return;
for(GuiComponent comp : children) {
comp.onChanged(repaint);
}
} }
protected void repaint() {
}
@Override
public IGuiBox getBox() { return box; }
@Override @Override
public GuiComponent set(float x, float y) { public GuiComponent set(float x, float y) {
if(box.getBaseX() != x || box.getBaseY() != y) { if(box.getBaseX() != x || box.getBaseY() != y) {
box.setXY(x, y); box.setXY(x, y);
//TODO implement onChanged onChanged(false);
} }
return this; return this;
} }
@ -117,7 +137,7 @@ public non-sealed class GuiComponent extends FlagObject implements ICastable, IL
public GuiComponent bounds(float width, float height) { public GuiComponent bounds(float width, float height) {
if(box.getBaseWidth() != width || box.getBaseHeight() != height) { if(box.getBaseWidth() != width || box.getBaseHeight() != height) {
box.setBounds(width, height); box.setBounds(width, height);
//TODO implement onChanged onChanged(true);
} }
return this; return this;
} }
@ -126,7 +146,7 @@ public non-sealed class GuiComponent extends FlagObject implements ICastable, IL
public GuiComponent resize(float width, float height) { public GuiComponent resize(float width, float height) {
if(width != 0F || height != 0F) { if(width != 0F || height != 0F) {
box.grow(width, height); box.grow(width, height);
//TODO implement onChanged onChanged(true);
} }
return this; return this;
} }
@ -135,34 +155,27 @@ public non-sealed class GuiComponent extends FlagObject implements ICastable, IL
public GuiComponent move(float moveX, float moveY) { public GuiComponent move(float moveX, float moveY) {
if(moveX != 0F || moveY != 0F) { if(moveX != 0F || moveY != 0F) {
box.move(moveX, moveY); box.move(moveX, moveY);
//TODO implement onChanged onChanged(false);
} }
return this; return this;
} }
private final boolean isInteractable() { public final GuiComponent scale(float newScale) {
return isFlagSet(FLAG_ENABLED | FLAG_VISIBLE); if(newScale != box.getBaseScale()) {
box.setScale(newScale);
// TODO implement onChanged
}
return this;
} }
public final boolean isFocused() { //@formatter:off
return isFlagSet(FLAG_FOCUSED); private final boolean isInteractable() { return isFlagSet(FLAG_ENABLED | FLAG_VISIBLE); }
} public final boolean isFocused() { return isFlagSet(FLAG_FOCUSED); }
public final boolean isEnabled() { return isFlagSet(FLAG_ENABLED); }
public final boolean isEnabled() { public final boolean isVisible() { return isFlagSet(FLAG_VISIBLE); }
return isFlagSet(FLAG_ENABLED); public final boolean isManualManaged() { return isFlagSet(FLAG_MANUAL_MANAGED); }
} public final boolean isScissored() { return isFlagSet(FLAG_SCISSORED); }
//@formatter:on
public final boolean isVisible() {
return isFlagSet(FLAG_VISIBLE);
}
public final boolean isManualManaged() {
return isFlagSet(FLAG_MANUAL_MANAGED);
}
public final boolean isScissored() {
return isFlagSet(FLAG_SCISSORED);
}
public final void setFocused(boolean value) { public final void setFocused(boolean value) {
setFlag(FLAG_FOCUSED, value); setFlag(FLAG_FOCUSED, value);

View File

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

View File

@ -0,0 +1,30 @@
package speiger.src.coreengine.rendering.newGui.layout;
import speiger.src.coreengine.rendering.gui.helper.box.IGuiBox;
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) {
//TODO implement
}
}
}

View File

@ -0,0 +1,56 @@
package speiger.src.coreengine.rendering.newGui.layout;
import speiger.src.coreengine.rendering.gui.helper.box.IGuiBox;
import speiger.src.coreengine.rendering.newGui.components.base.GuiComponent;
public interface IConstraint {
public void apply(GuiComponent owner, GuiComponent child, Target target);
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

@ -46,7 +46,9 @@ public interface DataTag
public DataTag copy(); public DataTag copy();
@SuppressWarnings("unchecked")
public default <T> T cast(){return (T)this;} public default <T> T cast(){return (T)this;}
@SuppressWarnings("unchecked")
public default <T> T cast(Class<T> clz){return (T)this;} public default <T> T cast(Class<T> clz){return (T)this;}
public default String prettyPrint(){return prettyPrint(" ", 0);} public default String prettyPrint(){return prettyPrint(" ", 0);}

View File

@ -1,37 +0,0 @@
package speiger.src.coreengine.utils.io.settings;
import java.util.List;
import java.util.function.Consumer;
import speiger.src.collections.objects.lists.ObjectArrayList;
public abstract class BaseSetting<T extends ISetting<T>> implements ISetting<T>
{
protected final String id;
List<Consumer<T>> listeners = new ObjectArrayList<Consumer<T>>();
public BaseSetting(String id)
{
this.id = id;
}
@Override
public T addListener(Consumer<T> listener)
{
listeners.add(listener);
return (T)this;
}
protected void notifyListeners()
{
if(listeners.isEmpty())
{
return;
}
T value = (T)this;
for(int i = 0,m=listeners.size();i<m;i++)
{
listeners.get(i).accept(value);
}
}
}

View File

@ -1,66 +0,0 @@
package speiger.src.coreengine.utils.io.settings;
import com.google.gson.JsonObject;
import speiger.src.coreengine.utils.helpers.JsonUtil;
public class BooleanSetting extends BaseSetting<BooleanSetting>
{
final boolean defaultValue;
boolean value;
public BooleanSetting(String id, boolean defaultValue)
{
super(id);
this.defaultValue = defaultValue;
value = defaultValue;
}
@Override
public boolean isDefault()
{
return value == defaultValue;
}
@Override
public void setDefault()
{
if(value != defaultValue)
{
value = defaultValue;
notifyListeners();
}
}
public BooleanSetting setValue(boolean value)
{
if(this.value != value)
{
this.value = value;
notifyListeners();
}
return this;
}
public boolean getDefaultValue()
{
return defaultValue;
}
public boolean getValue()
{
return value;
}
@Override
public void write(JsonObject object)
{
object.addProperty(id, value);
}
@Override
public void read(JsonObject object)
{
value = JsonUtil.getOrDefault(object, id, defaultValue);
}
}

View File

@ -1,16 +0,0 @@
package speiger.src.coreengine.utils.io.settings;
import java.util.function.Consumer;
import com.google.gson.JsonObject;
public interface ISetting<T extends ISetting<T>>
{
public T addListener(Consumer<T> listener);
public boolean isDefault();
public void setDefault();
public void write(JsonObject object);
public void read(JsonObject object);
}

View File

@ -1,66 +0,0 @@
package speiger.src.coreengine.utils.io.settings;
import com.google.gson.JsonObject;
import speiger.src.coreengine.utils.helpers.JsonUtil;
public class IntSetting extends BaseSetting<IntSetting>
{
final int defaultValue;
int value;
public IntSetting(String id, int defaultValue)
{
super(id);
this.defaultValue = defaultValue;
value = defaultValue;
}
@Override
public boolean isDefault()
{
return value == defaultValue;
}
@Override
public void setDefault()
{
if(value != defaultValue)
{
value = defaultValue;
notifyListeners();
}
}
public int getValue()
{
return value;
}
public int getDefaultValue()
{
return defaultValue;
}
public IntSetting setValue(int value)
{
if(this.value != value)
{
this.value = value;
notifyListeners();
}
return this;
}
@Override
public void write(JsonObject object)
{
object.addProperty(id, value);
}
@Override
public void read(JsonObject object)
{
value = JsonUtil.getOrDefault(object, id, defaultValue);
}
}

View File

@ -1,50 +0,0 @@
package speiger.src.coreengine.utils.io.settings;
import java.io.File;
import java.io.FileWriter;
import com.google.gson.JsonObject;
import com.google.gson.internal.Streams;
import com.google.gson.stream.JsonWriter;
import speiger.src.coreengine.utils.helpers.JsonUtil;
public class Settings
{
File file;
ISetting<?>[] settings;
public Settings(File path, ISetting<?>[] settings)
{
file = path;
this.settings = settings;
}
public void load()
{
JsonObject obj = JsonUtil.loadFile(file);
for(int i = 0,m=settings.length;i<m;i++)
{
settings[i].read(obj);
}
}
public void save()
{
JsonObject obj = new JsonObject();
for(int i = 0,m=settings.length;i<m;i++)
{
settings[i].write(obj);
}
try(JsonWriter writer = new JsonWriter(new FileWriter(file)))
{
writer.setIndent(" ");
Streams.write(obj, writer);
writer.flush();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}