Skip to content

Commit 0c224ad

Browse files
committed
feature: add ctrl + f to search through options
1 parent 4a1a35a commit 0c224ad

File tree

3 files changed

+56
-7
lines changed

3 files changed

+56
-7
lines changed

src/main/java/org/mcsr/speedrunapi/config/screen/SpeedrunConfigScreen.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
import net.minecraft.client.gui.screen.Screen;
44
import net.minecraft.client.gui.screen.ScreenTexts;
55
import net.minecraft.client.gui.widget.ButtonWidget;
6+
import net.minecraft.client.gui.widget.TextFieldWidget;
67
import net.minecraft.client.util.InputUtil;
78
import net.minecraft.client.util.math.MatrixStack;
89
import net.minecraft.text.LiteralText;
10+
import net.minecraft.text.TranslatableText;
911
import org.jetbrains.annotations.ApiStatus;
1012
import org.jetbrains.annotations.Nullable;
13+
import org.lwjgl.glfw.GLFW;
1114
import org.mcsr.speedrunapi.SpeedrunAPI;
1215
import org.mcsr.speedrunapi.config.SpeedrunConfigContainer;
1316
import org.mcsr.speedrunapi.config.screen.widgets.list.SpeedrunOptionListWidget;
@@ -21,7 +24,10 @@ public class SpeedrunConfigScreen extends Screen {
2124
@Nullable
2225
private final Predicate<InputUtil.Key> inputListener;
2326
private final Screen parent;
27+
2428
private SpeedrunOptionListWidget list;
29+
private TextFieldWidget searchField;
30+
private boolean searchFieldOpen;
2531

2632
public SpeedrunConfigScreen(SpeedrunConfigContainer<?> config, @Nullable Predicate<InputUtil.Key> inputListener, Screen parent) {
2733
super(new LiteralText(config.getModContainer().getMetadata().getName()));
@@ -30,9 +36,26 @@ public SpeedrunConfigScreen(SpeedrunConfigContainer<?> config, @Nullable Predica
3036
this.parent = parent;
3137
}
3238

39+
private void toggleSearchField() {
40+
this.searchFieldOpen = !this.searchFieldOpen;
41+
this.searchField.setVisible(this.searchFieldOpen);
42+
if (this.searchFieldOpen) {
43+
this.setFocused(this.searchField);
44+
this.searchField.setSelected(true);
45+
this.list.adjustTop(50);
46+
} else {
47+
this.searchField.setText("");
48+
this.list.adjustTop(25);
49+
}
50+
}
51+
3352
@Override
3453
protected void init() {
35-
this.list = new SpeedrunOptionListWidget(this.config, this, this.client, this.width, this.height, 25, this.height - 32);
54+
this.searchField = new TextFieldWidget(this.textRenderer, this.width / 2 - 100, 25, 200, 20, this.searchField, new TranslatableText("speedrunapi.gui.config.search"));
55+
this.searchField.setVisible(this.searchFieldOpen);
56+
this.searchField.setChangedListener(string -> this.list.updateEntries(string));
57+
this.addChild(this.searchField);
58+
this.list = new SpeedrunOptionListWidget(this, this.config, this.client, this.width, this.height, 25, this.height - 32, this.searchField.getText());
3659
this.addChild(this.list);
3760
this.addButton(new ButtonWidget(this.width / 2 - 100, this.height - 27, 200, 20, ScreenTexts.DONE, button -> this.onClose()));
3861
}
@@ -41,6 +64,7 @@ protected void init() {
4164
public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
4265
this.renderBackground(matrices);
4366
this.list.render(matrices, mouseX, mouseY, delta);
67+
this.searchField.render(matrices, mouseX, mouseY, delta);
4468
this.drawCenteredText(matrices, this.textRenderer, this.title, this.width / 2, 10, 0xFFFFFF);
4569
super.render(matrices, mouseX, mouseY, delta);
4670
}
@@ -50,6 +74,10 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
5074
if (this.inputListener != null && this.inputListener.test(InputUtil.fromKeyCode(keyCode, scanCode))) {
5175
return true;
5276
}
77+
if (keyCode == GLFW.GLFW_KEY_F && Screen.hasControlDown()) {
78+
this.toggleSearchField();
79+
return true;
80+
}
5381
return super.keyPressed(keyCode, scanCode, modifiers);
5482
}
5583

src/main/java/org/mcsr/speedrunapi/config/screen/widgets/list/SpeedrunOptionListWidget.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,26 @@
2121
@ApiStatus.Internal
2222
public class SpeedrunOptionListWidget extends ElementListWidget<SpeedrunOptionListWidget.OptionListEntry> {
2323
private final SpeedrunConfigScreen parent;
24+
private final SpeedrunConfigContainer<?> config;
2425

25-
public SpeedrunOptionListWidget(SpeedrunConfigContainer<?> config, SpeedrunConfigScreen parent, MinecraftClient client, int width, int height, int top, int bottom) {
26+
public SpeedrunOptionListWidget(SpeedrunConfigScreen parent, SpeedrunConfigContainer<?> config, MinecraftClient client, int width, int height, int top, int bottom, String filter) {
2627
super(client, width, height, top, bottom, 30);
2728
this.parent = parent;
29+
this.config = config;
30+
this.updateEntries(filter);
31+
}
32+
33+
public void updateEntries(String filter) {
34+
this.clearEntries();
35+
36+
filter = filter.toLowerCase(Locale.ENGLISH);
2837

2938
Map<String, Set<SpeedrunOption<?>>> categorizedOptions = new LinkedHashMap<>();
30-
for (SpeedrunOption<?> option : config.getOptions()) {
31-
if (!option.hasWidget() || !config.getConfig().shouldShowOption(option.getID())) {
39+
for (SpeedrunOption<?> option : this.config.getOptions()) {
40+
if (!filter.isEmpty() && !option.getName().getString().toLowerCase(Locale.ENGLISH).contains(filter)) {
41+
continue;
42+
}
43+
if (!option.hasWidget() || !this.config.getConfig().shouldShowOption(option.getID())) {
3244
continue;
3345
}
3446
if (option.getCategory() != null) {
@@ -37,11 +49,12 @@ public SpeedrunOptionListWidget(SpeedrunConfigContainer<?> config, SpeedrunConfi
3749
}
3850
this.addEntry(new OptionEntry(option));
3951
}
52+
4053
for (Map.Entry<String, Set<SpeedrunOption<?>>> category : categorizedOptions.entrySet()) {
41-
if (!config.getConfig().shouldShowCategory(category.getKey())) {
54+
if (!this.config.getConfig().shouldShowCategory(category.getKey())) {
4255
continue;
4356
}
44-
String categoryTranslation = "speedrunapi.config." + config.getModContainer().getMetadata().getId() + ".category." + category.getKey();
57+
String categoryTranslation = "speedrunapi.config." + this.config.getModContainer().getMetadata().getId() + ".category." + category.getKey();
4558
if (!Language.getInstance().hasTranslation(categoryTranslation) && Language.getInstance().hasTranslation(category.getKey())) {
4659
categoryTranslation = category.getKey();
4760
}
@@ -50,6 +63,13 @@ public SpeedrunOptionListWidget(SpeedrunConfigContainer<?> config, SpeedrunConfi
5063
this.addEntry(new OptionEntry(option));
5164
}
5265
}
66+
67+
this.setScrollAmount(0.0);
68+
}
69+
70+
public void adjustTop(int top) {
71+
//this.height += this.top - top;
72+
this.top = top;
5373
}
5474

5575
@Override

src/main/resources/assets/speedrunapi/lang/en_us.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"speedrunapi.gui.config.button": "Mod Configs",
33
"speedrunapi.gui.config.title": "Speedrun API - Mod Configs",
44
"speedrunapi.gui.config.noConfigs": "§7There is no mods to configure here :(",
5-
"speedrunapi.gui.config.unavailable": "This Config is unavailable right now!"
5+
"speedrunapi.gui.config.unavailable": "This Config is unavailable right now!",
6+
"speedrunapi.gui.config.search": "Search for options"
67
}

0 commit comments

Comments
 (0)