Skip to content

Commit

Permalink
Sorting Modes
Browse files Browse the repository at this point in the history
  • Loading branch information
IntegerLimit committed Dec 28, 2024
1 parent b487d9c commit 2fcbe42
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 23 deletions.
32 changes: 24 additions & 8 deletions src/main/java/com/nomiceu/nomilabs/LabsTextures.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ public interface AccessibleGuiAdvancedMemoryCard {
void labs$syncMemoryInfo();

void labs$closeTypeSelector();

void labs$changeSort(boolean forwards);

SortModes labs$getSortMode();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public class LabsClientCache {

public static final List<Pair<BlockPos, EnumFacing>> inputLoc = new ObjectArrayList<>();
public static final List<Pair<BlockPos, EnumFacing>> outputLoc = new ObjectArrayList<>();
public static SortModes sortMode = SortModes.DEFAULT;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<InfoWrapper, Comparator<InfoWrapper>> 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<InfoWrapper, Comparator<InfoWrapper>> compFromSelected) {
SortModes(String key, Function<InfoWrapper, Comparator<InfoWrapper>> compFromSelected) {
this.translationKey = key;
this.compFromSelected = compFromSelected;
}

public String getName() {
return LabsTranslate.translate(translationKey);
}

public Comparator<InfoWrapper> getComp(InfoWrapper selected) {
var applyComp = compFromSelected.apply(selected);
return (a, b) -> {
Expand All @@ -38,6 +51,11 @@ public Comparator<InfoWrapper> getComp(InfoWrapper selected) {
};
}

/* Util */
private static Function<InfoWrapper, Comparator<InfoWrapper>> wrapComp(Comparator<InfoWrapper> comp) {
return (a) -> comp;
}

/* Sorters */
private static Comparator<InfoWrapper> compDefault(InfoWrapper selected) {
return (a, b) -> {
Expand All @@ -46,7 +64,7 @@ private static Comparator<InfoWrapper> 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;
Expand All @@ -70,14 +88,24 @@ private static Comparator<InfoWrapper> 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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand All @@ -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",
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -165,7 +192,7 @@ private void replaceOldUpdateScrollbar(Collection<InfoWrapper> 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();
}

Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/assets/nomilabs/lang/en_us.lang
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2fcbe42

Please sign in to comment.