Progress on Guis and start on Fontrendering

This commit is contained in:
Speiger 2024-06-15 23:48:10 +02:00
parent 94f371b4a3
commit c01d317fdb
9 changed files with 119 additions and 13 deletions

View File

@ -51,7 +51,6 @@ public class GuiAnimator implements ObjectFloatConsumer<Target> {
heightDiff = 0F;
scaleDiff = 1F;
changeState = 0;
}
public void apply() {

View File

@ -102,17 +102,21 @@ public abstract non-sealed class GuiComponent extends FlagObject implements ICas
return true;
}
public GuiComponent addChild(GuiComponent child) { return addChild(child, null); }
public GuiComponent withConstraints(ConstraintContainer container) {
this.constraints = container;
if((screen != null || parent != null) && constraints != null) constraints.apply(this, parent);
return this;
}
public GuiComponent addChild(GuiComponent child, ConstraintContainer constraints) {
public GuiComponent addChild(GuiComponent child) {
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.screen = screen;
child.setScreen(screen);
child.init();
child.onChanged(true);
return child;
}
@ -124,6 +128,7 @@ public abstract non-sealed class GuiComponent extends FlagObject implements ICas
if(child.parent != this) throw new IllegalArgumentException("Child isn't owned by this Component");
child.close();
child.parent = null;
child.screen = null;
children.remove(child);
box.removeChild(child.getBox());
interactions.remove(child.interactions);

View File

@ -10,5 +10,6 @@ public interface IComponentScreen {
public void pushLayer();
public void popLayer();
public GuiComponent addComponent(GuiComponent component, ConstraintContainer constraints);
public boolean hasComponent(GuiComponent component);
public boolean removeComponent(GuiComponent component);
}

View File

@ -0,0 +1,49 @@
package speiger.src.coreengine.rendering.gui.font;
import java.util.List;
import speiger.src.collections.ints.maps.impl.hash.Int2ObjectOpenHashMap;
import speiger.src.collections.ints.maps.interfaces.Int2ObjectMap;
import speiger.src.coreengine.assets.AssetLocation;
import speiger.src.coreengine.rendering.gui.font.glyth.Glyth;
import speiger.src.coreengine.rendering.gui.font.glyth.GlythData;
import speiger.src.coreengine.rendering.gui.font.providers.IFontProvider;
public class FontGroup {
private static final int BOLD_FLAG = 1<<31;
AssetLocation locations;
List<IFontProvider> providers;
Int2ObjectMap<Glyth> bakedGlyths = new Int2ObjectOpenHashMap<>();
Int2ObjectMap<GlythData> dataGlyths = new Int2ObjectOpenHashMap<>();
public FontGroup(AssetLocation locations, List<IFontProvider> providers) {
this.locations = locations;
this.providers = providers;
}
public GlythData data(int codepoint) {
return dataGlyths.computeIfAbsent(codepoint, this::compute);
}
public Glyth glyth(int codepoint, boolean bold) {
return bakedGlyths.computeIfAbsent(codepoint | (bold ? 0 : BOLD_FLAG), this::bake);
}
private GlythData compute(int codepoint) {
for(int i = 0,m=providers.size();i<m;i++) {
GlythData data = providers.get(i).glythData(codepoint);
if(data != null) return data;
}
return null;
}
private Glyth bake(int codepoint) {
boolean bold = (codepoint & BOLD_FLAG) != 0;
codepoint &= ~BOLD_FLAG;
for(int i = 0,m=providers.size();i<m;i++) {
GlythData data = providers.get(i).glythData(codepoint);
if(data != null) return data.bake(bold);
}
return null;
}
}

View File

@ -0,0 +1,5 @@
package speiger.src.coreengine.rendering.gui.font;
public class FontManager {
}

View File

@ -0,0 +1,3 @@
package speiger.src.coreengine.rendering.gui.font.glyth;
public record Glyth(int texture, boolean bold, float minU, float minV, float maxU, float maxV) {}

View File

@ -0,0 +1,7 @@
package speiger.src.coreengine.rendering.gui.font.glyth;
public interface GlythData {
public float advance(boolean bold);
public Glyth bake(boolean bold);
}

View File

@ -0,0 +1,7 @@
package speiger.src.coreengine.rendering.gui.font.providers;
import speiger.src.coreengine.rendering.gui.font.glyth.GlythData;
public interface IFontProvider {
public GlythData glythData(int codepoint);
}

View File

@ -14,6 +14,7 @@ import speiger.src.coreengine.rendering.gui.components.base.IComponentScreen;
import speiger.src.coreengine.rendering.gui.components.base.IInteractable;
import speiger.src.coreengine.rendering.gui.components.base.IInteractableContainer;
import speiger.src.coreengine.rendering.gui.layout.constraints.ConstraintContainer;
import speiger.src.coreengine.rendering.gui.renderer.IUIRenderer;
import speiger.src.coreengine.rendering.guiOld.helper.box.IGuiBox;
public class GuiContainer implements IComponentScreen, IInteractableContainer {
@ -50,20 +51,42 @@ public class GuiContainer implements IComponentScreen, IInteractableContainer {
public void popLayer() { container.popLayer(); }
@Override
public GuiComponent addComponent(GuiComponent component, ConstraintContainer constraints) {
//TODO change constraints
if(component.screen() != null) throw new IllegalArgumentException("A Child can not have multiple Screens");
container.add(component);
component.setScreen(this);
component.init();
component.onChanged(true);
return component;
}
@Override
public boolean hasComponent(GuiComponent component) {
return component.screen() == this && container.contains(component);
}
@Override
public boolean removeComponent(GuiComponent component) {
return container.removeChild(component);
if(component.screen() == this && container.remove(component)) {
component.close();
component.setScreen(null);
return true;
}
return false;
}
public void render(IUIRenderer renderer, int mouseX, int mouseY, float partialTicks) {
for(Object2BooleanMap.Entry<GuiComponent> entry : container) {
boolean keepMouse = entry.getBooleanValue();
GuiComponent.renderComponent(entry.getKey(), renderer, keepMouse ? mouseX : -1, keepMouse ? mouseY : -1, partialTicks);
}
}
public void tick() {
for(Object2BooleanMap.Entry<GuiComponent> entry : container) {
entry.getKey().updateComponent();
}
}
public static class LayeredContainer implements Iterable<Object2BooleanMap.Entry<GuiComponent>> {
List<List<GuiComponent>> components = new ObjectArrayList<>();
List<List<IInteractable>> interactables = new ObjectArrayList<>();
@ -89,8 +112,15 @@ public class GuiContainer implements IComponentScreen, IInteractableContainer {
interactables().add(component.interactContainer());
}
public boolean removeChild(GuiComponent component) {
for(int i = components.size()-1;i>=0;i--) {
public boolean contains(GuiComponent component) {
for(int i = layer;i>=0;i--) {
if(components.get(i).contains(component)) return true;
}
return false;
}
public boolean remove(GuiComponent component) {
for(int i = layer;i>=0;i--) {
if(components.get(i).remove(component)) {
interactables.get(i).remove(component.interactContainer());
clearEmptyLayers();