SimpleJavaEngine/src/main/java/speiger/src/coreengine/rendering/newGui/components/base/GuiComponent.java

176 lines
4.4 KiB
Java

package speiger.src.coreengine.rendering.newGui.components.base;
import java.util.List;
import java.util.function.Consumer;
import speiger.src.collections.ints.sets.IntOpenHashSet;
import speiger.src.collections.ints.sets.IntSet;
import speiger.src.collections.objects.lists.ObjectArrayList;
import speiger.src.coreengine.rendering.gui.helper.box.IGuiBox;
import speiger.src.coreengine.utils.collections.CollectionUtils;
import speiger.src.coreengine.utils.collections.FlagObject;
public non-sealed class GuiComponent extends FlagObject implements IInteractableContainer, ICastable, ILayoutComponent, IListableComponent {
private static final int FLAG_FOCUSED = 1<<0;
private static final int FLAG_ENABLED = 1<<1;
private static final int FLAG_VISIBLE = 1<<2;
private static final int FLAG_MANUAL_MANAGED = 1<<3;
private static final int FLAG_SCISSORED = 1<<4;
IGuiBox box;
GuiComponent parent;
List<GuiComponent> children = new ObjectArrayList<>();
List<IInteractable> interactions = new ObjectArrayList<>();
List<Consumer<GuiComponent>>[] listeners = CollectionUtils.createList(IListableComponent.MAX_LISTENER_TYPES);
IInteractable focused;
IntSet activeButtons = new IntOpenHashSet();
@Override
public List<? extends IInteractable> getChildren() {
return interactions;
}
@Override
public void setFocusedChild(IInteractable child) {
if(focused != null) focused.setFocused(false);
this.focused = child;
if(focused != null) focused.setFocused(true);
}
@Override
public IInteractable getFocusedChild() {
return focused;
}
@Override
public IntSet getActiveButtons() {
return activeButtons;
}
@Override
public void calculateBounds(ILayoutScanner output) {
output.accept(getBox());
for(GuiComponent child : children) {
if(child.isVisible() || output.acceptsInvisble()) {
child.calculateBounds(output);
}
}
}
@Override
public GuiComponent addListener(Consumer<GuiComponent> listener, int index) {
listeners[index].add(listener);
return this;
}
@Override
public GuiComponent removeListener(Consumer<GuiComponent> listener, int index) {
listeners[index].remove(listener);
return this;
}
@Override
public void notifyListeners(int index) {
if(listeners[index].isEmpty()) return;
for(Consumer<GuiComponent> comp : listeners[index]) {
comp.accept(this);
}
}
@Override
public IGuiBox getBox() {
return box;
}
@Override
public boolean isMouseColliding(int mouseX, int mouseY) {
return isVisible() && box.isColiding(mouseX, mouseY);
}
@Override
public GuiComponent set(float x, float y) {
if(box.getBaseX() != x || box.getBaseY() != y) {
box.setXY(x, y);
//TODO implement onChanged
}
return this;
}
@Override
public GuiComponent bounds(float width, float height) {
if(box.getBaseWidth() != width || box.getBaseHeight() != height) {
box.setBounds(width, height);
//TODO implement onChanged
}
return this;
}
@Override
public GuiComponent resize(float width, float height) {
if(width != 0F || height != 0F) {
box.grow(width, height);
//TODO implement onChanged
}
return this;
}
@Override
public GuiComponent move(float moveX, float moveY) {
if(moveX != 0F || moveY != 0F) {
box.move(moveX, moveY);
//TODO implement onChanged
}
return this;
}
@Override
public final boolean isFocused() {
return isFlagSet(FLAG_FOCUSED);
}
public final boolean isEnabled() {
return isFlagSet(FLAG_ENABLED);
}
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);
}
@Override
public final void setFocused(boolean value) {
setFlag(FLAG_FOCUSED, value);
if(!value) setFocusedChild(null);
}
public final GuiComponent setEnabled(boolean value) {
setFlag(FLAG_ENABLED, value);
return this;
}
public final GuiComponent setVisible(boolean value) {
setFlag(FLAG_VISIBLE, value);
return this;
}
public final GuiComponent setManualManaged(boolean value) {
setFlag(FLAG_MANUAL_MANAGED, value);
return this;
}
public final GuiComponent setScissored(boolean value) {
setFlag(FLAG_SCISSORED, value);
return this;
}
}