diff --git a/src/main/java/vazkii/botania/client/gui/loki/GuiLokiSchematics.java b/src/main/java/vazkii/botania/client/gui/loki/GuiLokiSchematics.java index ffffce8dc..61f368981 100644 --- a/src/main/java/vazkii/botania/client/gui/loki/GuiLokiSchematics.java +++ b/src/main/java/vazkii/botania/client/gui/loki/GuiLokiSchematics.java @@ -10,9 +10,11 @@ import com.cleanroommc.modularui.utils.Alignment; import com.cleanroommc.modularui.utils.Color; import com.cleanroommc.modularui.widget.ParentWidget; import com.cleanroommc.modularui.widget.ScrollWidget; +import com.cleanroommc.modularui.widget.scroll.ScrollArea; import com.cleanroommc.modularui.widget.scroll.VerticalScrollData; import com.cleanroommc.modularui.widgets.ButtonWidget; import com.cleanroommc.modularui.widgets.TextWidget; +import com.cleanroommc.modularui.widgets.layout.Flow; import com.cleanroommc.modularui.widgets.layout.Row; import net.minecraft.item.ItemStack; import net.minecraft.util.StatCollector; @@ -22,20 +24,17 @@ import vazkii.botania.common.network.PacketHandler; import vazkii.botania.common.network.PacketLokiChangeSchematic; import vazkii.botania.common.network.PacketLokiDeleteSchematic; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.ArrayList; public class GuiLokiSchematics { private static final int WINDOW_WIDTH = 350, WINDOW_HEIGHT = 225, SCROLL_AREA_WIDTH = 350, SCROLL_AREA_HEIGHT = 200, SAVED_SCHEMATICS_HEADER_HEIGHT = 25, X_PADDING = 5, Y_PADDING = 5; + private ArrayList schematicNames; public static void open(ItemStack ring) { ClientGUI.open(new ModularScreen(new GuiLokiSchematics().buildUI(ring))); } - private final Set deleted = new HashSet<>(); - private String selectedSchematic; private GuiLokiSchematics() { @@ -47,65 +46,68 @@ public class GuiLokiSchematics { } selectedSchematic = ItemLokiRing.getSelectedSchematic(itemStack); - List schematicNames = ItemLokiRing.getSchematicNames(itemStack); + schematicNames = new ArrayList<>(ItemLokiRing.getSchematicNames(itemStack)); final ScrollWidget scrollWidget = new ScrollWidget<>(new VerticalScrollData()); - scrollWidget.getScrollArea().getScrollY().setScrollSize(schematicNames.size() * (SCROLL_AREA_HEIGHT / 5 + 1)); - for (int idx = 0; idx < schematicNames.size(); idx++) { - String key = schematicNames.get(idx); - scrollWidget.child( - new Row() - .sizeRel(1f, 0.2f) - .pos(0, schematicNames.indexOf(key) * (SCROLL_AREA_HEIGHT / 5 + 1)) - .padding(5, 0, 5, 5) - .setEnabledIf((w) -> !deleted.contains(key)) - .background(new Rectangle() - .setColor(idx % 2 == 0 ? Color.ORANGE.brighter(3) : Color.ORANGE.brighter(2))) - .child( - new TextWidget(key) - .widthRel(0.72f)) - .child( - new ParentWidget<>() - .tooltip(new RichTooltip(new TextWidget("Activate Schematic"))) - .widthRel(0.083f) - .heightRel(1f) - .setEnabledIf((w) -> !key.equals(selectedSchematic)) - .child( - new ButtonWidget<>() - .onMousePressed(ignored -> { - PacketHandler.INSTANCE.sendToServer(new PacketLokiChangeSchematic(key)); - selectedSchematic = key; - return true; - }) - .background(Textures.CHECK_ICON) - .hoverBackground(Textures.CHECK_ICON) - .size(20,20) - .center())) - .child( - new ParentWidget<>() - .widthRel(0.083f) - .heightRel(1f) - .child( - new ButtonWidget<>() + setScrollArea(scrollWidget.getScrollArea()); + for (int i = 0; i < schematicNames.size(); i++) { + // Final value that can be captured by lambdas + final int rowId = i; + Flow row = new Row(); + row.sizeRel(1f, 0.2f) + .pos(0, rowId * (SCROLL_AREA_HEIGHT / 5 + 1)) + .padding(5, 0, 5, 5) + .setEnabledIf((w) -> rowId < schematicNames.size()) + .background(new Rectangle() + .setColor(i % 2 == 0 ? Color.ORANGE.brighter(3) : Color.ORANGE.brighter(2))) + .child( + new TextWidget(IKey.dynamic(()-> rowId < schematicNames.size() ? schematicNames.get(rowId) : "")) + .widthRel(0.72f)) + .child( + new ParentWidget<>() + .tooltip(new RichTooltip(new TextWidget("Activate Schematic"))) + .widthRel(0.083f) + .heightRel(1f) + .setEnabledIf((w) -> rowId < schematicNames.size() && !schematicNames.get(rowId).equals(selectedSchematic)) + .child( + new ButtonWidget<>() + .onMousePressed(ignored -> { + String key = schematicNames.get(rowId); + PacketHandler.INSTANCE.sendToServer(new PacketLokiChangeSchematic(key)); + selectedSchematic = key; + return true; + }) + .background(Textures.CHECK_ICON) + .hoverBackground(Textures.CHECK_ICON) + .size(20, 20) + .center())) + .child( + new ParentWidget<>() + .widthRel(0.083f) + .heightRel(1f) + .child( + new ButtonWidget<>() .tooltip(new RichTooltip(new TextWidget("Rename Schematic"))) .onMousePressed(ignored -> { - ClientGUI.open(new RenameGui(key)); + ClientGUI.open(new RenameGui(schematicNames.get(rowId))); return true; }) .background(Textures.RENAME_ICON) .hoverBackground(Textures.RENAME_ICON) .size(20, 20) .center())) - .child( - new ParentWidget<>() - .widthRel(0.083f) - .heightRel(1f) - .child( - new ButtonWidget<>() + .child( + new ParentWidget<>() + .widthRel(0.083f) + .heightRel(1f) + .child( + new ButtonWidget<>() .tooltip(new RichTooltip(new TextWidget("Delete Schematic"))) .onMousePressed(ignored -> { + String key = schematicNames.get(rowId); PacketHandler.INSTANCE.sendToServer(new PacketLokiDeleteSchematic(key)); - deleted.add(key); + schematicNames.remove(rowId); + setScrollArea(scrollWidget.getScrollArea()); if(key.equals(selectedSchematic)) selectedSchematic = null; return true; @@ -113,7 +115,9 @@ public class GuiLokiSchematics { .background(Textures.DELETE_ICON) .hoverBackground(Textures.DELETE_ICON) .size(20, 20) - .center()))); + .center())); + scrollWidget.child( + row); } return ModularPanel.defaultPanel("lokiSchematics") @@ -132,4 +136,8 @@ public class GuiLokiSchematics { .size(WINDOW_WIDTH, WINDOW_HEIGHT); } + private void setScrollArea(ScrollArea scrollWidget) { + scrollWidget.getScrollY().setScrollSize(schematicNames.size() * (SCROLL_AREA_HEIGHT / 5 + 1)); + } + }