diff --git a/src/main/java/com/troblecodings/signals/core/OSSupplier.java b/src/main/java/com/troblecodings/signals/core/OSSupplier.java new file mode 100644 index 000000000..58886f89b --- /dev/null +++ b/src/main/java/com/troblecodings/signals/core/OSSupplier.java @@ -0,0 +1,10 @@ +package com.troblecodings.signals.core; + +import com.troblecodings.signals.signalbox.MainSignalIdentifier.SignalState; + +@FunctionalInterface +public interface OSSupplier { + + T get(final SignalState type); + +} diff --git a/src/main/java/com/troblecodings/signals/enums/EnumGuiMode.java b/src/main/java/com/troblecodings/signals/enums/EnumGuiMode.java index 73d41b449..a8e8bfb8e 100644 --- a/src/main/java/com/troblecodings/signals/enums/EnumGuiMode.java +++ b/src/main/java/com/troblecodings/signals/enums/EnumGuiMode.java @@ -1,11 +1,11 @@ package com.troblecodings.signals.enums; -import java.util.function.Supplier; - import com.troblecodings.core.ReadBuffer; import com.troblecodings.guilib.ecs.entitys.render.UILines; import com.troblecodings.guilib.ecs.entitys.render.UITexture; +import com.troblecodings.signals.core.OSSupplier; import com.troblecodings.signals.guis.UISignalBoxTile; +import com.troblecodings.signals.signalbox.MainSignalIdentifier.SignalState; public enum EnumGuiMode { STRAIGHT(new float[] { @@ -14,29 +14,41 @@ public enum EnumGuiMode { 0, 0.5f, 0.5f, 1 }), END(new float[] { 0.9f, 0.2f, 0.9f, 0.8f - }), PLATFORM(() -> new UILines(new float[] { + }), PLATFORM((state) -> new UILines(new float[] { 0, 0.15f, 1, 0.15f }, 3)), BUE(new float[] { 0.3f, 0, 0.3f, 1, 0.7f, 0, 0.7f, 1 - }), HP(0), VP(1), RS(2), RA10(3), SH2(4), - IN_CONNECTION(() -> new UITexture(UISignalBoxTile.ARROW_ICON)), - OUT_CONNECTION(() -> new UITexture(UISignalBoxTile.ARROW_ICON)); + }), HP(0, true), VP(1, true), RS(2, true), RA10(3), SH2(4), + IN_CONNECTION((_u) -> new UITexture(UISignalBoxTile.ARROW_ICON)), + OUT_CONNECTION((_u) -> new UITexture(UISignalBoxTile.ARROW_ICON)); /** * Naming */ - public final Supplier consumer; + public final OSSupplier consumer; private EnumGuiMode(final int id) { - this(() -> new UITexture(UISignalBoxTile.ICON, id * 0.2, 0, id * 0.2 + 0.2, 0.5)); + this((_u) -> new UITexture(UISignalBoxTile.ICON, id * 0.2, 0, id * 0.2 + 0.2, 0.5)); + } + + private EnumGuiMode(final int id, final boolean unused) { + this((state) -> { + if (state.equals(SignalState.RED)) { + return new UITexture(UISignalBoxTile.SIGNALS, id * 2 * 0.1666, 0, + id * 2 * 0.1666 + 0.1666, 1); + } else { + return new UITexture(UISignalBoxTile.SIGNALS, (id + 0.5) * 2 * 0.1666, 0, + (id + 0.5) * 2 * 0.1666 + 0.1666, 1); + } + }); } private EnumGuiMode(final float[] array) { - this(() -> new UILines(array, 2)); + this((_u) -> new UILines(array, 2)); } - private EnumGuiMode(final Supplier consumer) { + private EnumGuiMode(final OSSupplier consumer) { this.consumer = consumer; } diff --git a/src/main/java/com/troblecodings/signals/guis/ContainerSignalBox.java b/src/main/java/com/troblecodings/signals/guis/ContainerSignalBox.java index 59a6ea755..ebb5d0de0 100644 --- a/src/main/java/com/troblecodings/signals/guis/ContainerSignalBox.java +++ b/src/main/java/com/troblecodings/signals/guis/ContainerSignalBox.java @@ -38,7 +38,7 @@ public class ContainerSignalBox extends ContainerBase implements UIClientSync { - protected final List greenSignals = new ArrayList<>(); + protected final Map> greenSignals = new HashMap<>(); protected final Map> possibleSubsidiaries = new HashMap<>(); protected final Map> enabledSubsidiaryTypes = new HashMap<>(); protected final List> nextPathways = new ArrayList<>(); @@ -48,6 +48,7 @@ public class ContainerSignalBox extends ContainerBase implements UIClientSync { private SignalBoxTileEntity tile; private Consumer infoUpdates; private Consumer> colorUpdates; + private Consumer> signalUpdates; public ContainerSignalBox(final GuiInfo info) { super(info); @@ -157,7 +158,10 @@ public void deserializeClient(final ReadBuffer buffer) { } final int greenSignalsSize = buffer.getInt(); for (int i = 0; i < greenSignalsSize; i++) { - greenSignals.add(ModeIdentifier.of(buffer)); + final ModeIdentifier identifier = ModeIdentifier.of(buffer); + final List greenSignals = this.greenSignals + .computeIfAbsent(identifier.point, _u -> new ArrayList<>()); + greenSignals.add(identifier.mode); } update(); break; @@ -196,16 +200,24 @@ public void deserializeClient(final ReadBuffer buffer) { break; } case SET_SIGNALS: { + final List pointUpdates = new ArrayList<>(); final int redSignalSize = buffer.getByteToUnsignedInt(); for (int i = 0; i < redSignalSize; i++) { - greenSignals.remove(ModeIdentifier.of(buffer)); + final ModeIdentifier identifier = ModeIdentifier.of(buffer); + greenSignals.remove(identifier.point); + pointUpdates.add(identifier.point); } final int greenSignalSize = buffer.getByteToUnsignedInt(); for (int i = 0; i < greenSignalSize; i++) { final ModeIdentifier modeIdentifier = ModeIdentifier.of(buffer); - if (!greenSignals.contains(modeIdentifier)) - greenSignals.add(modeIdentifier); + if (!greenSignals.containsKey(modeIdentifier.point)) { + final List greenSignals = this.greenSignals + .computeIfAbsent(modeIdentifier.point, _u -> new ArrayList<>()); + greenSignals.add(modeIdentifier.mode); + } + pointUpdates.add(modeIdentifier.point); } + signalUpdates.accept(pointUpdates); break; } default: @@ -411,4 +423,8 @@ protected void setInfoConsumer(final Consumer consumer) { protected void setColorUpdater(final Consumer> updater) { this.colorUpdates = updater; } + + protected void setSignalUpdater(final Consumer> updater) { + this.signalUpdates = updater; + } } \ No newline at end of file diff --git a/src/main/java/com/troblecodings/signals/guis/GuiSignalBox.java b/src/main/java/com/troblecodings/signals/guis/GuiSignalBox.java index bee5e19d3..7934f7a7f 100644 --- a/src/main/java/com/troblecodings/signals/guis/GuiSignalBox.java +++ b/src/main/java/com/troblecodings/signals/guis/GuiSignalBox.java @@ -112,6 +112,7 @@ public GuiSignalBox(final GuiInfo info) { this.container = (ContainerSignalBox) info.base; container.setInfoConsumer(this::infoUpdate); container.setColorUpdater(this::applyColorChanges); + container.setSignalUpdater(this::updateSignals); this.info = info; } @@ -130,6 +131,19 @@ public void infoUpdate(final String errorString) { return; } + public void updateSignals(final List updated) { + updated.forEach(point -> { + final UISignalBoxTile tile = allTiles.get(point); + tile.setGreenSignals(container.greenSignals.getOrDefault(point, new ArrayList<>())); + tile.getNode().forEach(mode -> { + if (mode.mode.equals(EnumGuiMode.HP) || mode.mode.equals(EnumGuiMode.VP) + || mode.mode.equals(EnumGuiMode.RS)) { + tile.updateModeSet(mode); + } + }); + }); + } + protected void resetTileSelection() { colors.values().forEach(color -> color.getParent().remove(color)); colors.clear(); @@ -806,7 +820,8 @@ private void initializeFieldTemplate(final BiConsumer if (node == null) { node = new SignalBoxNode(name); } - final UISignalBoxTile sbt = new UISignalBoxTile(node); + final UISignalBoxTile sbt = new UISignalBoxTile(node, + container.greenSignals.getOrDefault(name, new ArrayList<>())); if (!node.isEmpty()) allTiles.put(name, sbt); tile.add(sbt); diff --git a/src/main/java/com/troblecodings/signals/guis/SidePanel.java b/src/main/java/com/troblecodings/signals/guis/SidePanel.java index f5f289882..5d8c7928a 100644 --- a/src/main/java/com/troblecodings/signals/guis/SidePanel.java +++ b/src/main/java/com/troblecodings/signals/guis/SidePanel.java @@ -1,5 +1,6 @@ package com.troblecodings.signals.guis; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -168,7 +169,7 @@ public void updateNextNode(final int selection, final int rotation) { final SignalBoxNode node = new SignalBoxNode(new Point(-1, -1)); final EnumGuiMode modes = EnumGuiMode.values()[selection]; node.add(new ModeSet(modes, Rotation.values()[rotation])); - final UISignalBoxTile sbt = new UISignalBoxTile(node); + final UISignalBoxTile sbt = new UISignalBoxTile(node, new ArrayList<>()); preview.add(sbt); preview.add(new UIBorder(new UIEntity().getBasicTextColor())); diff --git a/src/main/java/com/troblecodings/signals/guis/UIMenu.java b/src/main/java/com/troblecodings/signals/guis/UIMenu.java index e6353ac57..a63113f8f 100644 --- a/src/main/java/com/troblecodings/signals/guis/UIMenu.java +++ b/src/main/java/com/troblecodings/signals/guis/UIMenu.java @@ -1,5 +1,6 @@ package com.troblecodings.signals.guis; +import java.util.ArrayList; import java.util.function.BiConsumer; import com.troblecodings.guilib.ecs.entitys.DrawInfo; @@ -50,7 +51,7 @@ public void postDraw(final DrawInfo info) { preview.add(new UIColor(0xFFAFAFAF)); final SignalBoxNode node = new SignalBoxNode(new Point(-1, -1)); node.add(new ModeSet(mode, Rotation.values()[this.rotation])); - final UISignalBoxTile sbt = new UISignalBoxTile(node); + final UISignalBoxTile sbt = new UISignalBoxTile(node, new ArrayList<>()); preview.add(sbt); preview.setHeight(20); preview.setWidth(20); diff --git a/src/main/java/com/troblecodings/signals/guis/UISignalBoxTile.java b/src/main/java/com/troblecodings/signals/guis/UISignalBoxTile.java index 0cbfcbca7..62a00fcd4 100644 --- a/src/main/java/com/troblecodings/signals/guis/UISignalBoxTile.java +++ b/src/main/java/com/troblecodings/signals/guis/UISignalBoxTile.java @@ -1,6 +1,8 @@ package com.troblecodings.signals.guis; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import com.troblecodings.guilib.ecs.entitys.UIComponent; @@ -11,6 +13,7 @@ import com.troblecodings.guilib.ecs.entitys.transform.UIIndependentTranslate; import com.troblecodings.guilib.ecs.entitys.transform.UIRotate; import com.troblecodings.signals.OpenSignalsMain; +import com.troblecodings.signals.signalbox.MainSignalIdentifier.SignalState; import com.troblecodings.signals.signalbox.ModeSet; import com.troblecodings.signals.signalbox.Point; import com.troblecodings.signals.signalbox.SignalBoxNode; @@ -24,17 +27,32 @@ public class UISignalBoxTile extends UIComponentEntity { "gui/textures/symbols.png"); public static final ResourceLocation ARROW_ICON = new ResourceLocation(OpenSignalsMain.MODID, "gui/textures/connection.png"); + public static final ResourceLocation SIGNALS = new ResourceLocation(OpenSignalsMain.MODID, + "gui/textures/signals.png"); private SignalBoxNode node; private final Map setToEntity = new HashMap<>(); + private final List greenSignals; - public UISignalBoxTile(final SignalBoxNode node) { + public UISignalBoxTile(final SignalBoxNode node, final List greenSignals) { super(new UIEntity()); this.node = node; + this.greenSignals = new ArrayList<>(greenSignals); if (this.node != null) this.node.forEach(this::localAdd); } + public void setGreenSignals(final List list) { + greenSignals.clear(); + greenSignals.addAll(list); + } + + public void updateModeSet(final ModeSet mode) { + localRemove(mode); + localAdd(mode); + update(); + } + public void setNode(final SignalBoxNode node) { if (this.node != null) this.node.forEach(this::localRemove); @@ -51,7 +69,8 @@ private void localAdd(final ModeSet modeSet) { entity.add(rotation); } entity.add(new UIIndependentTranslate(0, 0, 1)); - entity.add((UIComponent) modeSet.mode.consumer.get()); + entity.add((UIComponent) modeSet.mode.consumer + .get(greenSignals.contains(modeSet) ? SignalState.GREEN : SignalState.RED)); this.entity.add(entity); setToEntity.put(modeSet, entity); this.entity.setVisible(!setToEntity.isEmpty());