Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add syntax highlighting to searches #498

Draft
wants to merge 8 commits into
base: 1.19.3
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/main/java/com/terraformersmc/modmenu/gui/ModsScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.terraformersmc.modmenu.util.TranslationUtil;
import com.terraformersmc.modmenu.util.mod.Mod;
import com.terraformersmc.modmenu.util.mod.ModBadgeRenderer;
import com.terraformersmc.modmenu.util.mod.search.ModSearch;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.gui.DrawableHelper;
import net.minecraft.client.gui.screen.ConfirmLinkScreen;
Expand Down Expand Up @@ -52,6 +53,7 @@ public class ModsScreen extends Screen {
private static final Text CONFIGURE = Text.translatable("modmenu.configure");
private static final Logger LOGGER = LoggerFactory.getLogger("Mod Menu | ModsScreen");
private TextFieldWidget searchBox;
private ModSearch search;
private DescriptionListWidget descriptionListWidget;
private final Screen previousScreen;
private ModListWidget modList;
Expand Down Expand Up @@ -104,7 +106,6 @@ protected void init() {
int searchBoxWidth = ModMenuConfig.CONFIG_MODE.getValue() ? Math.min(200, searchWidthMax) : searchWidthMax;
searchBoxX = paneWidth / 2 - searchBoxWidth / 2 - filtersButtonSize / 2;
this.searchBox = new TextFieldWidget(this.textRenderer, searchBoxX, 22, searchBoxWidth, 20, this.searchBox, Text.translatable("modmenu.search"));
this.searchBox.setChangedListener((string_1) -> this.modList.filter(string_1, false));

for (Mod mod : ModMenu.MODS.values()) {
if (!modHasConfigScreen.containsKey(mod.getId())) {
Expand All @@ -122,8 +123,9 @@ protected void init() {
}

this.modList = new ModListWidget(this.client, paneWidth, this.height, paneY, this.height - 36, ModMenuConfig.COMPACT_LIST.getValue() ? 23 : 36, this.searchBox.getText(), this.modList, this);
this.search = new ModSearch(this, this.modList, this.searchBox);
this.modList.setLeftPos(0);
modList.reloadFilters();
modList.refreshEntries();

this.descriptionListWidget = new DescriptionListWidget(this.client, paneWidth, this.height, RIGHT_PANE_Y + 60, this.height - 36, textRenderer.fontHeight + 1, this);
this.descriptionListWidget.setLeftPos(rightPaneX);
Expand Down Expand Up @@ -208,7 +210,7 @@ public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
this.addDrawableChild(new ButtonWidget(filtersX, 45, sortingWidth, 20, sortingText, button -> {
ModMenuConfig.SORTING.cycleValue();
ModMenuConfigManager.save();
modList.reloadFilters();
modList.refreshEntries();
}, Supplier::get) {
@Override
public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
Expand All @@ -221,7 +223,7 @@ public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
this.addDrawableChild(new ButtonWidget(filtersX + sortingWidth + 2, 45, showLibrariesWidth, 20, showLibrariesText, button -> {
ModMenuConfig.SHOW_LIBRARIES.toggleValue();
ModMenuConfigManager.save();
modList.reloadFilters();
modList.refreshEntries();
}, Supplier::get) {
@Override
public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
Expand Down Expand Up @@ -421,8 +423,8 @@ public void updateScrollPercent(double scrollPercent) {
this.scrollPercent = scrollPercent;
}

public String getSearchInput() {
return searchBox.getText();
public ModSearch getSearch() {
return search;
}

private boolean updateFiltersX() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.terraformersmc.modmenu.gui.widget.entries.ParentEntry;
import com.terraformersmc.modmenu.util.mod.Mod;
import com.terraformersmc.modmenu.util.mod.fabric.FabricIconHandler;
import com.terraformersmc.modmenu.util.mod.ModSearch;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.widget.AlwaysSelectedEntryListWidget;
import net.minecraft.client.render.BufferBuilder;
Expand Down Expand Up @@ -42,7 +41,6 @@ public ModListWidget(MinecraftClient client, int width, int height, int y1, int
if (list != null) {
this.mods = list.mods;
}
this.filter(searchTerm, false);
setScrollAmount(parent.getScrollPercent() * Math.max(0, this.getMaxPosition() - (this.bottom - this.top - 4)));
}

Expand Down Expand Up @@ -108,16 +106,7 @@ protected ModListEntry remove(int index) {
return super.remove(index);
}

public void reloadFilters() {
filter(parent.getSearchInput(), true, false);
}


public void filter(String searchTerm, boolean refresh) {
filter(searchTerm, refresh, true);
}

private void filter(String searchTerm, boolean refresh, boolean search) {
public void refreshEntries() {
this.clearEntries();
addedMods.clear();
Collection<Mod> mods = ModMenu.MODS.values().stream().filter(mod -> {
Expand All @@ -136,13 +125,13 @@ private void filter(String searchTerm, boolean refresh, boolean search) {
// mods.addAll(TestModContainer.getTestModContainers());
}

if (this.mods == null || refresh) {
if (this.mods == null) {
this.mods = new ArrayList<>();
this.mods.addAll(mods);
this.mods.sort(ModMenuConfig.SORTING.getValue().getComparator());
}

List<Mod> matched = ModSearch.search(parent, searchTerm, this.mods);
List<Mod> matched = this.parent.getSearch().getResults(mods);

for (Mod mod : matched) {
String modId = mod.getId();
Expand All @@ -157,11 +146,14 @@ private void filter(String searchTerm, boolean refresh, boolean search) {
//Add parent mods when not searching
List<Mod> children = ModMenu.PARENT_MAP.get(mod);
children.sort(ModMenuConfig.SORTING.getValue().getComparator());
ParentEntry parent = new ParentEntry(mod, children, this);

//Get valid children
List<Mod> validChildren = children.stream().filter(matched::contains).collect(Collectors.toList());

ParentEntry parent = new ParentEntry(mod, children, validChildren.size(), this);
this.addEntry(parent);
//Add children if they are meant to be shown
if (this.parent.showModChildren.contains(modId)) {
List<Mod> validChildren = ModSearch.search(this.parent, searchTerm, children);
for (Mod child : validChildren) {
this.addEntry(new ChildEntry(child, parent, this, validChildren.indexOf(child) == validChildren.size() - 1));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.terraformersmc.modmenu.config.ModMenuConfig;
import com.terraformersmc.modmenu.gui.widget.ModListWidget;
import com.terraformersmc.modmenu.util.mod.Mod;
import com.terraformersmc.modmenu.util.mod.ModSearch;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawableHelper;
import net.minecraft.client.util.math.MatrixStack;
Expand All @@ -20,12 +19,14 @@
public class ParentEntry extends ModListEntry {
private static final Identifier PARENT_MOD_TEXTURE = new Identifier(ModMenu.MOD_ID, "textures/gui/parent_mod.png");
protected List<Mod> children;
protected int shownChildren;
protected ModListWidget list;
protected boolean hoveringIcon = false;

public ParentEntry(Mod parent, List<Mod> children, ModListWidget list) {
public ParentEntry(Mod parent, List<Mod> children, int shownChildren, ModListWidget list) {
super(parent, list);
this.children = children;
this.shownChildren = shownChildren;
this.list = list;
}

Expand All @@ -35,7 +36,6 @@ public void render(MatrixStack matrices, int index, int y, int x, int rowWidth,
TextRenderer font = client.textRenderer;
int childrenBadgeHeight = font.fontHeight;
int childrenBadgeWidth = font.fontHeight;
int shownChildren = ModSearch.search(list.getParent(), list.getParent().getSearchInput(), getChildren()).size();
Text str = shownChildren == children.size() ? Text.literal(String.valueOf(shownChildren)) : Text.literal(shownChildren + "/" + children.size());
int childrenWidth = font.getWidth(str) - 1;
if (childrenBadgeWidth < childrenWidth + 4) {
Expand Down Expand Up @@ -72,7 +72,7 @@ public boolean mouseClicked(double mouseX, double mouseY, int i) {
} else {
list.getParent().showModChildren.add(id);
}
list.filter(list.getParent().getSearchInput(), false);
list.refreshEntries();
}
return super.mouseClicked(mouseX, mouseY, i);
}
Expand All @@ -86,18 +86,18 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
} else {
list.getParent().showModChildren.add(modId);
}
list.filter(list.getParent().getSearchInput(), false);
list.refreshEntries();
return true;
} else if (keyCode == GLFW.GLFW_KEY_LEFT) {
if (list.getParent().showModChildren.contains(modId)) {
list.getParent().showModChildren.remove(modId);
list.filter(list.getParent().getSearchInput(), false);
list.refreshEntries();
}
return true;
} else if (keyCode == GLFW.GLFW_KEY_RIGHT) {
if (!list.getParent().showModChildren.contains(modId)) {
list.getParent().showModChildren.add(modId);
list.filter(list.getParent().getSearchInput(), false);
list.refreshEntries();
} else {
return list.keyPressed(GLFW.GLFW_KEY_DOWN, 0, 0);
}
Expand Down
27 changes: 20 additions & 7 deletions src/main/java/com/terraformersmc/modmenu/util/mod/Mod.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.minecraft.client.resource.language.I18n;
import net.minecraft.client.texture.NativeImageBackedTexture;
import net.minecraft.text.Text;
import net.minecraft.text.TextColor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.quiltmc.loader.api.QuiltLoader;
Expand Down Expand Up @@ -115,22 +116,26 @@ default String getTranslatedDescription() {
boolean getChildHasUpdate();

enum Badge {
LIBRARY("modmenu.badge.library", 0xff107454, 0xff093929, "library"),
CLIENT("modmenu.badge.clientsideOnly", 0xff2b4b7c, 0xff0e2a55, null),
DEPRECATED("modmenu.badge.deprecated", 0xff841426, 0xff530C17, "deprecated"),
PATCHWORK_FORGE("modmenu.badge.forge", 0xff1f2d42, 0xff101721, null),
MODPACK("modmenu.badge.modpack", 0xff7a2b7c, 0xff510d54, null),
MINECRAFT("modmenu.badge.minecraft", 0xff6f6c6a, 0xff31302f, null);
LIBRARY("modmenu.badge.library", "modmenu.searchTerms.library", 0xff107454, 0xff093929, 0xff4ce6b5, "library"),
CLIENT("modmenu.badge.clientsideOnly", "modmenu.searchTerms.clientside", 0xff2b4b7c, 0xff0e2a55, 0xff3484fe, null),
DEPRECATED("modmenu.badge.deprecated", "modmenu.searchTerms.deprecated", 0xff841426, 0xff530C17, 0xffe44e66, "deprecated"),
PATCHWORK_FORGE("modmenu.badge.forge", "modmenu.searchTerms.patchwork", 0xff1f2d42, 0xff101721, 0xff7a93b8, null),
MODPACK("modmenu.badge.modpack", "modmenu.searchTerms.modpack", 0xff7a2b7c, 0xff510d54, 0xffc868ca, null),
MINECRAFT("modmenu.badge.minecraft", null, 0xff6f6c6a, 0xff31302f, 0xff9b9997, null);

private final Text text;
private final int outlineColor, fillColor;
private final TextColor searchColor;
private final String key;
private final String searchKey;
private static final Map<String, Badge> KEY_MAP = new HashMap<>();

Badge(String translationKey, int outlineColor, int fillColor, String key) {
Badge(String translationKey, String searchKey, int outlineColor, int fillColor, int searchColor, String key) {
this.text = Text.translatable(translationKey);
this.searchKey = searchKey;
this.outlineColor = outlineColor;
this.fillColor = fillColor;
this.searchColor = TextColor.fromRgb(searchColor);
this.key = key;
}

Expand All @@ -146,6 +151,14 @@ public int getFillColor() {
return this.fillColor;
}

public TextColor getSearchColor() {
return this.searchColor;
}

public String getSearchKey() {
return this.searchKey;
}

public static Set<Badge> convert(Set<String> badgeKeys) {
return badgeKeys.stream().map(KEY_MAP::get).collect(Collectors.toSet());
}
Expand Down
78 changes: 0 additions & 78 deletions src/main/java/com/terraformersmc/modmenu/util/mod/ModSearch.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.terraformersmc.modmenu.util.mod.search;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import com.terraformersmc.modmenu.ModMenu;
import com.terraformersmc.modmenu.gui.ModsScreen;
import com.terraformersmc.modmenu.gui.widget.ModListWidget;
import com.terraformersmc.modmenu.util.mod.Mod;

import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.text.OrderedText;

public class ModSearch {
private final ModsScreen screen;
private final ModListWidget modList;

private SearchQuery query;

public ModSearch(ModsScreen screen, ModListWidget modList, TextFieldWidget searchBox) {
this.screen = screen;
this.modList = modList;

this.query = SearchQuery.parse("", this.screen);

searchBox.setChangedListener(this::updateSearch);
searchBox.setRenderTextProvider(this::provideRenderText);
}

private void updateSearch(String string) {
this.query = SearchQuery.parse(string, this.screen);
this.modList.refreshEntries();
}

private OrderedText provideRenderText(String original, int firstCharacterIndex) {
return this.query.provideRenderText(firstCharacterIndex, original.length());
}

private boolean matches(Mod mod) {
if (this.query.matches(mod)) {
return true;
}

// Allow parent to pass filter if a child passes
if (ModMenu.PARENT_MAP.keySet().contains(mod)) {
for (Mod child : ModMenu.PARENT_MAP.get(mod)) {
if (this.query.matches(child)) {
return true;
}
}
}

return false;
}

public List<Mod> getResults(Collection<Mod> mods) {
return mods.stream().filter(this::matches).collect(Collectors.toList());
}
}
Loading