diff --git a/src/main/java/speiger/src/coreengine/rendering/gui/screen/GuiContainer.java b/src/main/java/speiger/src/coreengine/rendering/gui/screen/GuiContainer.java index bad7c2b..8153267 100644 --- a/src/main/java/speiger/src/coreengine/rendering/gui/screen/GuiContainer.java +++ b/src/main/java/speiger/src/coreengine/rendering/gui/screen/GuiContainer.java @@ -1,16 +1,19 @@ package speiger.src.coreengine.rendering.gui.screen; +import java.util.Iterator; import java.util.List; import speiger.src.collections.ints.sets.IntOpenHashSet; import speiger.src.collections.ints.sets.IntSet; import speiger.src.collections.objects.lists.ObjectArrayList; -import speiger.src.collections.objects.utils.ObjectIterables; -import speiger.src.collections.objects.utils.ObjectIterators; +import speiger.src.collections.objects.maps.abstracts.AbstractObject2BooleanMap.BasicEntry; +import speiger.src.collections.objects.maps.interfaces.Object2BooleanMap; +import speiger.src.collections.objects.maps.interfaces.Object2BooleanMap.Entry; import speiger.src.coreengine.rendering.gui.components.base.GuiComponent; 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.guiOld.helper.box.IGuiBox; public class GuiContainer implements IComponentScreen, IInteractableContainer { @@ -41,7 +44,27 @@ public class GuiContainer implements IComponentScreen, IInteractableContainer { @Override public IntSet getActiveButtons() { return activeButtons; } - public static class LayeredContainer { + @Override + public void pushLayer() { container.pushLayer(); } + @Override + public void popLayer() { container.popLayer(); } + @Override + public GuiComponent addComponent(GuiComponent component, ConstraintContainer constraints) { + //TODO change constraints + container.add(component); + component.setScreen(this); + component.init(); + return component; + } + @Override + public boolean removeComponent(GuiComponent component) { + return container.removeChild(component); + } + + + + + public static class LayeredContainer implements Iterable> { List> components = new ObjectArrayList<>(); List> interactables = new ObjectArrayList<>(); int layer = -1; @@ -66,6 +89,48 @@ public class GuiContainer implements IComponentScreen, IInteractableContainer { interactables().add(component.interactContainer()); } + public boolean removeChild(GuiComponent component) { + for(int i = components.size()-1;i>=0;i--) { + if(components.get(i).remove(component)) { + interactables.get(i).remove(component.interactContainer()); + clearEmptyLayers(); + return true; + } + } + return false; + } + + public void clearEmptyLayers() { + while(components.getLast().isEmpty() && layer > 0) { + popLayer(); + } + } + + @Override + public Iterator> iterator() { + return new Iterator<>() { + BasicEntry entry = new BasicEntry<>(); + int myLayer = layer; + Iterator childIterator = components.getLast().iterator(); + + @Override + public boolean hasNext() { + return childIterator.hasNext(); + } + + @Override + public Entry next() { + if(!hasNext()) throw new IllegalStateException("Out of Elements to poll"); + entry.set(childIterator.next(), myLayer == layer); + if(!childIterator.hasNext() && myLayer > 0) { + childIterator = components.get(--myLayer).iterator(); + } + return entry; + } + + }; + } + public List children() { return components.get(layer); }