From 2fcbe4233c27e2dbcc81b5bebbd036f662678614 Mon Sep 17 00:00:00 2001 From: Integer Limit <103940576+IntegerLimit@users.noreply.github.com> Date: Sat, 28 Dec 2024 20:25:36 +1100 Subject: [PATCH] Sorting Modes --- .../com/nomiceu/nomilabs/LabsTextures.java | 32 +++++++--- .../AccessibleGuiAdvancedMemoryCard.java | 4 ++ .../betterp2p/AccessibleInfoList.java | 6 ++ .../betterp2p/LabsClientCache.java | 1 + .../integration/betterp2p/SortModes.java | 42 ++++++++++--- .../betterp2p/SortWidgetButton.java | 49 +++++++++++++++ .../betterp2p/GuiAdvancedMemoryCardMixin.java | 58 +++++++++++++++--- .../mixin/betterp2p/InfoListMixin.java | 29 ++++++++- .../resources/assets/nomilabs/lang/en_us.lang | 6 ++ .../advanced_memory_card/sorting_modes.png | Bin 0 -> 681 bytes 10 files changed, 204 insertions(+), 23 deletions(-) create mode 100644 src/main/java/com/nomiceu/nomilabs/integration/betterp2p/SortWidgetButton.java create mode 100644 src/main/resources/assets/nomilabs/textures/gui/advanced_memory_card/sorting_modes.png diff --git a/src/main/java/com/nomiceu/nomilabs/LabsTextures.java b/src/main/java/com/nomiceu/nomilabs/LabsTextures.java index 05f12340..132dec7f 100644 --- a/src/main/java/com/nomiceu/nomilabs/LabsTextures.java +++ b/src/main/java/com/nomiceu/nomilabs/LabsTextures.java @@ -8,6 +8,15 @@ public class LabsTextures { + /* Constants */ + public static int P2P_SIZE = 32; + + public static String P2P_SORTING_LOC = "textures/gui/advanced_memory_card/sorting_modes.png"; + public static int P2P_SORTING_AMT = 3; + + public static String P2P_CUSTOM_LOC = "textures/gui/advanced_memory_card/custom_modes.png"; + public static int P2P_CUSTOM_AMT = 2; + /* Overlays (Machine) */ public static OrientedOverlayRenderer GROWTH_CHAMBER_OVERLAY; @@ -18,6 +27,7 @@ public class LabsTextures { public static TextureArea PROGRESS_BAR_ROCKET; /* Gui Textures */ + public static TextureArea[] P2P_SORTING_MODES; public static TextureArea[] P2P_CUSTOM_MODES; public static TextureArea P2P_INPUT_ICON; public static TextureArea P2P_OUTPUT_ICON; @@ -27,19 +37,25 @@ public static void preInit() { MICROVERSE_CASING = new SimpleOverlayRenderer("nomilabs:microverse_casing"); PROGRESS_BAR_ROCKET = labsFullImage("textures/gui/progress_bar/progress_bar_rocket.png"); - var imageSizeX = 64; - var imageSizeY = 32; - var imageLoc = "textures/gui/advanced_memory_card/custom_modes.png"; - var spriteSize = 32; - P2P_CUSTOM_MODES = new TextureArea[] { - labsAreaImage(imageLoc, imageSizeX, imageSizeY, 0, 0, spriteSize, spriteSize), - labsAreaImage(imageLoc, imageSizeX, imageSizeY, spriteSize, 0, spriteSize, spriteSize), - }; + P2P_SORTING_MODES = labsAreasImageHorizontal(P2P_SORTING_LOC, P2P_SIZE, P2P_SIZE, P2P_SORTING_AMT); + P2P_CUSTOM_MODES = labsAreasImageHorizontal(P2P_CUSTOM_LOC, P2P_SIZE, P2P_SIZE, P2P_CUSTOM_AMT); P2P_INPUT_ICON = labsFullImage("textures/gui/advanced_memory_card/input.png"); P2P_OUTPUT_ICON = labsFullImage("textures/gui/advanced_memory_card/output.png"); } + /** + * Returns an array of all texture areas, that are stacked horizontally, in an image. + */ + public static TextureArea[] labsAreasImageHorizontal(String imageLoc, int iconSizeX, int iconSizeY, int amt) { + TextureArea[] areas = new TextureArea[amt]; + for (int i = 0; i < amt; i++) { + areas[i] = labsAreaImage(imageLoc, iconSizeX * amt, iconSizeY, + i * iconSizeX, 0, iconSizeX, iconSizeY); + } + return areas; + } + /** * Like the one in TextureArea, but with Labs Registry. */ diff --git a/src/main/java/com/nomiceu/nomilabs/integration/betterp2p/AccessibleGuiAdvancedMemoryCard.java b/src/main/java/com/nomiceu/nomilabs/integration/betterp2p/AccessibleGuiAdvancedMemoryCard.java index a4ae2432..d2783279 100644 --- a/src/main/java/com/nomiceu/nomilabs/integration/betterp2p/AccessibleGuiAdvancedMemoryCard.java +++ b/src/main/java/com/nomiceu/nomilabs/integration/betterp2p/AccessibleGuiAdvancedMemoryCard.java @@ -11,4 +11,8 @@ public interface AccessibleGuiAdvancedMemoryCard { void labs$syncMemoryInfo(); void labs$closeTypeSelector(); + + void labs$changeSort(boolean forwards); + + SortModes labs$getSortMode(); } diff --git a/src/main/java/com/nomiceu/nomilabs/integration/betterp2p/AccessibleInfoList.java b/src/main/java/com/nomiceu/nomilabs/integration/betterp2p/AccessibleInfoList.java index 30d5c12c..55b24bc4 100644 --- a/src/main/java/com/nomiceu/nomilabs/integration/betterp2p/AccessibleInfoList.java +++ b/src/main/java/com/nomiceu/nomilabs/integration/betterp2p/AccessibleInfoList.java @@ -9,4 +9,10 @@ public interface AccessibleInfoList { void labs$setPlayerPos(Vec3d pos); void labs$properlyResetScrollbar(WidgetScrollBar scrollBar, int numEntries); + + void labs$setSortMode(SortModes mode); + + SortModes labs$getSortMode(); + + void labs$changeSortMode(boolean forwards); } diff --git a/src/main/java/com/nomiceu/nomilabs/integration/betterp2p/LabsClientCache.java b/src/main/java/com/nomiceu/nomilabs/integration/betterp2p/LabsClientCache.java index 9bd5f5ab..a84e68e0 100644 --- a/src/main/java/com/nomiceu/nomilabs/integration/betterp2p/LabsClientCache.java +++ b/src/main/java/com/nomiceu/nomilabs/integration/betterp2p/LabsClientCache.java @@ -12,4 +12,5 @@ public class LabsClientCache { public static final List> inputLoc = new ObjectArrayList<>(); public static final List> outputLoc = new ObjectArrayList<>(); + public static SortModes sortMode = SortModes.DEFAULT; } diff --git a/src/main/java/com/nomiceu/nomilabs/integration/betterp2p/SortModes.java b/src/main/java/com/nomiceu/nomilabs/integration/betterp2p/SortModes.java index 1fac540a..3c25d996 100644 --- a/src/main/java/com/nomiceu/nomilabs/integration/betterp2p/SortModes.java +++ b/src/main/java/com/nomiceu/nomilabs/integration/betterp2p/SortModes.java @@ -5,26 +5,39 @@ import org.apache.commons.lang3.StringUtils; +import com.nomiceu.nomilabs.util.LabsTranslate; import com.projecturanus.betterp2p.client.gui.InfoWrapper; public enum SortModes { - DEFAULT(SortModes::compDefault); + DEFAULT("nomilabs.gui.advanced_memory_card.sort.default", SortModes::compDefault), + DISTANCE("nomilabs.gui.advanced_memory_card.sort.distance", wrapComp(SortModes::compareDistThenName)), + NAME("nomilabs.gui.advanced_memory_card.sort.name", wrapComp((a, b) -> { + if (a.getName().equals(b.getName())) return compareTypeThenDist(a, b, false); + return StringUtils.compare(a.getName(), b.getName()); + })); + private final String translationKey; private final Function> compFromSelected; /** * Create a Sort Mode. - * + * + * @param key Translation Key * @param compFromSelected Function that takes the selected p2p and returns a comparator. * You do not need to handle the case where either one is the selected p2p. * Note that item smaller = in front! * Selected p2p may be null! */ - SortModes(Function> compFromSelected) { + SortModes(String key, Function> compFromSelected) { + this.translationKey = key; this.compFromSelected = compFromSelected; } + public String getName() { + return LabsTranslate.translate(translationKey); + } + public Comparator getComp(InfoWrapper selected) { var applyComp = compFromSelected.apply(selected); return (a, b) -> { @@ -38,6 +51,11 @@ public Comparator getComp(InfoWrapper selected) { }; } + /* Util */ + private static Function> wrapComp(Comparator comp) { + return (a) -> comp; + } + /* Sorters */ private static Comparator compDefault(InfoWrapper selected) { return (a, b) -> { @@ -46,7 +64,7 @@ private static Comparator compDefault(InfoWrapper selected) { // Checking for unbound is not needed, just have all unbound at front if selected is unbound if (a.getFrequency() == selected.getFrequency()) { if (b.getFrequency() != selected.getFrequency()) return -1; - return compareTypeThenDist(a, b); + return compareTypeThenDist(a, b, true); } if (b.getFrequency() == selected.getFrequency()) return 1; @@ -70,14 +88,24 @@ private static Comparator compDefault(InfoWrapper selected) { return StringUtils.compare(a.getFreqDisplay(), b.getFreqDisplay()); } - return compareTypeThenDist(a, b); + return compareTypeThenDist(a, b, true); }; } - private static int compareTypeThenDist(InfoWrapper a, InfoWrapper b) { + private static int compareTypeThenDist(InfoWrapper a, InfoWrapper b, boolean compareNameBackup) { if (a.getOutput() != b.getOutput()) return a.getOutput() ? 1 : -1; // Inputs First - return getDistance(a) > getDistance(b) ? 1 : -1; // Furthest Last + if (!compareNameBackup) + return Double.compare(getDistance(a), getDistance(b)); // Furthest Last + return compareDistThenName(a, b); + } + + private static int compareDistThenName(InfoWrapper a, InfoWrapper b) { + double distA = getDistance(a); + double distB = getDistance(b); + + if (distA == distB) return StringUtils.compare(a.getName(), b.getName()); + return Double.compare(distA, distB); } private static double getDistance(InfoWrapper info) { diff --git a/src/main/java/com/nomiceu/nomilabs/integration/betterp2p/SortWidgetButton.java b/src/main/java/com/nomiceu/nomilabs/integration/betterp2p/SortWidgetButton.java new file mode 100644 index 00000000..d48d46c0 --- /dev/null +++ b/src/main/java/com/nomiceu/nomilabs/integration/betterp2p/SortWidgetButton.java @@ -0,0 +1,49 @@ +package com.nomiceu.nomilabs.integration.betterp2p; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.Tessellator; + +import org.jetbrains.annotations.NotNull; + +import com.google.common.collect.ImmutableList; +import com.nomiceu.nomilabs.LabsTextures; +import com.projecturanus.betterp2p.client.gui.GuiAdvancedMemoryCard; +import com.projecturanus.betterp2p.client.gui.widget.WidgetButton; + +public class SortWidgetButton extends WidgetButton { + + public SortWidgetButton(@NotNull GuiAdvancedMemoryCard gui, int x, int y, int width, int height) { + super(gui, x, y, width, height); + + setHoverText(ImmutableList.of(getAccessibleGui().labs$getSortMode().getName())); + } + + @Override + public boolean mousePressed(int mouseX, int mouseY, int button) { + if (!canPress(mouseX, mouseY)) return false; + + getAccessibleGui().labs$changeSort(button == 0); + setHoverText(ImmutableList.of(getAccessibleGui().labs$getSortMode().getName())); + return true; + } + + @Override + public void draw(@NotNull Minecraft mc, int mouseX, int mouseY, float partial) { + var tessellator = Tessellator.getInstance(); + + drawBG(tessellator, mouseX, mouseY, partial); + + // Button Icon + LabsTextures.P2P_SORTING_MODES[getAccessibleGui().labs$getSortMode().ordinal()] + .draw(x + 1.0, y + 1.0, width - 2, height - 2); + } + + // From GuiButton#mousePressed + private boolean canPress(int mouseX, int mouseY) { + return enabled && visible && mouseX >= x && mouseY >= y && mouseX < x + width && mouseY < y + height; + } + + private AccessibleGuiAdvancedMemoryCard getAccessibleGui() { + return ((AccessibleGuiAdvancedMemoryCard) (Object) getGui()); + } +} diff --git a/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/GuiAdvancedMemoryCardMixin.java b/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/GuiAdvancedMemoryCardMixin.java index bc82aad6..63f5ea60 100644 --- a/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/GuiAdvancedMemoryCardMixin.java +++ b/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/GuiAdvancedMemoryCardMixin.java @@ -12,13 +12,12 @@ import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import com.nomiceu.nomilabs.integration.betterp2p.AccessibleGuiAdvancedMemoryCard; -import com.nomiceu.nomilabs.integration.betterp2p.AccessibleInfoList; -import com.nomiceu.nomilabs.integration.betterp2p.LabsClientCache; +import com.nomiceu.nomilabs.integration.betterp2p.*; import com.projecturanus.betterp2p.client.gui.GuiAdvancedMemoryCard; import com.projecturanus.betterp2p.client.gui.InfoList; import com.projecturanus.betterp2p.client.gui.InfoWrapper; import com.projecturanus.betterp2p.client.gui.widget.GuiScale; +import com.projecturanus.betterp2p.client.gui.widget.WidgetButton; import com.projecturanus.betterp2p.client.gui.widget.WidgetScrollBar; import com.projecturanus.betterp2p.client.gui.widget.WidgetTypeSelector; import com.projecturanus.betterp2p.item.BetterMemoryCardModes; @@ -57,6 +56,19 @@ public abstract class GuiAdvancedMemoryCardMixin extends GuiScreen implements Ac @Shadow private GuiScale scale; + @Shadow + @Final + private WidgetButton refreshButton; + + @Shadow + private int guiLeft; + + @Shadow + private int guiTop; + + @Shadow + protected abstract void refreshOverlay(); + @Override @Unique public BetterMemoryCardModes labs$getMode() { @@ -81,14 +93,36 @@ public abstract class GuiAdvancedMemoryCardMixin extends GuiScreen implements Ac typeSelector.setVisible(false); } + @Override + @Unique + public void labs$changeSort(boolean forwards) { + labs$getAccessibleInfo().labs$changeSortMode(forwards); + infos.resort(); + infos.refilter(); + refreshOverlay(); + } + + @Override + @Unique + public SortModes labs$getSortMode() { + return labs$getAccessibleInfo().labs$getSortMode(); + } + @Inject(method = "initGui", at = @At("HEAD"), remap = true) - private void setupInfoListPlayerPos(CallbackInfo ci) { - ((AccessibleInfoList) (Object) infos).labs$setPlayerPos(mc.player.getPositionVector()); + private void setup(CallbackInfo ci) { + labs$getAccessibleInfo().labs$setPlayerPos(mc.player.getPositionVector()); + labs$getAccessibleInfo().labs$setSortMode(LabsClientCache.sortMode); } @Inject(method = "initGui", at = @At("TAIL"), remap = true) - private void properlySetScrollbarInit(CallbackInfo ci) { + private void handleEndInit(CallbackInfo ci) { labs$properlyResetScrollbar(); + // Refresh button position: 1 button below normal + refreshButton.setPosition(guiLeft - 32, guiTop + 130); + + // Add sort change button + buttonList.add(new SortWidgetButton((GuiAdvancedMemoryCard) (Object) this, + guiLeft - 32, guiTop + 98, 32, 32)); } @Redirect(method = "initGui", @@ -165,9 +199,19 @@ private void fillLabsCache(CallbackInfo ci) { .add(new Pair<>(pair.getSecond().getPos(), pair.getSecond().getFacing()))); } + @Inject(method = "onGuiClosed", at = @At("HEAD")) + private void save(CallbackInfo ci) { + LabsClientCache.sortMode = labs$getAccessibleInfo().labs$getSortMode(); + } + @Unique private void labs$properlyResetScrollbar() { - ((AccessibleInfoList) (Object) infos).labs$properlyResetScrollbar(scrollBar, + labs$getAccessibleInfo().labs$properlyResetScrollbar(scrollBar, scale.getSize().invoke(height - 75)); } + + @Unique + private AccessibleInfoList labs$getAccessibleInfo() { + return ((AccessibleInfoList) (Object) infos); + } } diff --git a/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/InfoListMixin.java b/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/InfoListMixin.java index 5d42ce2a..e8397b15 100644 --- a/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/InfoListMixin.java +++ b/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/InfoListMixin.java @@ -51,6 +51,33 @@ public abstract class InfoListMixin implements AccessibleInfoList { @Unique private Vec3d labs$playerPos; + @Unique + private SortModes labs$sortMode = SortModes.DEFAULT; + + @Unique + @Override + public void labs$setSortMode(SortModes mode) { + this.labs$sortMode = mode; + } + + @Unique + @Override + public SortModes labs$getSortMode() { + return labs$sortMode; + } + + @Unique + @Override + public void labs$changeSortMode(boolean forwards) { + int newModeOrdinal = labs$sortMode.ordinal() + (forwards ? 1 : -1); + int maxSize = SortModes.values().length; + + if (newModeOrdinal < 0) newModeOrdinal = maxSize - 1; + else newModeOrdinal = newModeOrdinal % maxSize; + + labs$sortMode = SortModes.values()[newModeOrdinal]; + } + @Unique @Override public void labs$setPlayerPos(Vec3d pos) { @@ -165,7 +192,7 @@ private void replaceOldUpdateScrollbar(Collection updateList, Widge @Inject(method = "resort", at = @At("HEAD"), cancellable = true) private void customSortLogic(CallbackInfo ci) { - labs$getThis().getSorted().sort(SortModes.DEFAULT.getComp(getSelectedInfo())); + labs$getThis().getSorted().sort(labs$sortMode.getComp(getSelectedInfo())); ci.cancel(); } diff --git a/src/main/resources/assets/nomilabs/lang/en_us.lang b/src/main/resources/assets/nomilabs/lang/en_us.lang index cf8a1c91..eca6176b 100644 --- a/src/main/resources/assets/nomilabs/lang/en_us.lang +++ b/src/main/resources/assets/nomilabs/lang/en_us.lang @@ -160,6 +160,8 @@ nomilabs.gui.better_p2p.input=Input nomilabs.gui.better_p2p.output=Output nomilabs.gui.better_p2p.error.same_output=P2P is already an {}. +nomilabs.gui.advanced_memory_card.button.refresh=Refresh List + nomilabs.gui.advanced_memory_card.info.dist=Distance: %sm nomilabs.gui.advanced_memory_card.info.type=Type: %s - %s @@ -173,6 +175,10 @@ nomilabs.gui.advanced_memory_card.hover_info.connections.output.none=§cNo Outpu nomilabs.gui.advanced_memory_card.hover_info.connections.output.one=§a%s Output nomilabs.gui.advanced_memory_card.hover_info.connections.output.multi=§b%s Outputs +nomilabs.gui.advanced_memory_card.sort.default=Sorting Mode: §bDefault +nomilabs.gui.advanced_memory_card.sort.distance=Sorting Mode: §bDistance +nomilabs.gui.advanced_memory_card.sort.name=Sorting Mode: §bName + gui.advanced_memory_card.mode.add_as_input=Current Mode: Add as Input nomilabs.gui.advanced_memory_card.mode.add_input.desc.1=Before binding, the §aselected P2P§7 has its connections removed. nomilabs.gui.advanced_memory_card.mode.add_input.desc.2=The §aselected P2P§7 is set as an §9input§7, with the §bbind target's§7 frequency. diff --git a/src/main/resources/assets/nomilabs/textures/gui/advanced_memory_card/sorting_modes.png b/src/main/resources/assets/nomilabs/textures/gui/advanced_memory_card/sorting_modes.png new file mode 100644 index 0000000000000000000000000000000000000000..c744707044b2a6e8a4b7e18f95223fe4a60c7d29 GIT binary patch literal 681 zcmV;a0#^NrP)2~wtwWg6`i>%4?ur-cx!RD#c7Ay}k{ zEVyB^nY*2x`!|VqclF+VGxOif-hX$Eq(0VKfyP?tH9T1mAWhR*k(!Lh<0V0F4z_3A z)06pSAKu=R{>K+9JeqIVS^01=tb;!tgUjIv@t$A*K7w)?t>I0HO$EfEuE#e?sX#5WM-26S#j