Skip to content

Commit

Permalink
feat: Add toggle button to show or hide child mods, and a search widg…
Browse files Browse the repository at this point in the history
…et to filter visible mods
  • Loading branch information
magicus committed Apr 27, 2024
1 parent 19eb140 commit b215f96
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
package se.icus.mag.modsettings.gui.screen;

import java.util.LinkedList;
import java.util.List;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.tooltip.Tooltip;
import net.minecraft.screen.ScreenTexts;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import se.icus.mag.modsettings.Main;
import se.icus.mag.modsettings.ModRegistry;

import java.util.LinkedList;
import java.util.List;
import se.icus.mag.modsettings.gui.ModConfigInfo;
import se.icus.mag.modsettings.gui.widget.Button;
import se.icus.mag.modsettings.gui.widget.IconToggleButtonWidget;
import se.icus.mag.modsettings.gui.widget.ModListWidget;
import se.icus.mag.modsettings.gui.widget.SearchWidget;

public class ModSettingsScreen extends TitledScreen {
private static final int FULL_BUTTON_WIDTH = 200;
private static final int BUTTON_HEIGHT = 20;

private ModListWidget list;
private boolean initIsProcessing;
private ModListWidget list;
private SearchWidget searchWidget;

public ModSettingsScreen(Screen previous) {
super(Text.translatable("modsettings.screen.title"), previous);
Expand All @@ -30,14 +34,37 @@ protected void init() {
if (initIsProcessing) return;
initIsProcessing = true;

// Put list between 32 pixels from top and bottom
// Add the toggle show indirect mods button
IconToggleButtonWidget showIndirectButton = new IconToggleButtonWidget(10, 6,
BUTTON_HEIGHT, BUTTON_HEIGHT, 15, 15,
List.of(new Identifier("modsettings", "expand"),
new Identifier("modsettings", "collapse")),
List.of(Tooltip.of(Text.translatable("modsettings.indirect.show")),
Tooltip.of(Text.translatable("modsettings.indirect.hide"))),
Main.OPTIONS.showIndirect ? 1 : 0, selection -> {
Main.OPTIONS.showIndirect = (selection == 1);
updateModButtons();
});
this.addDrawableChild(showIndirectButton);

// Add the search widget
searchWidget = new SearchWidget(40, 6, 100,
Main.OPTIONS.filterText, this.textRenderer, text -> {
Main.OPTIONS.filterText = text;
updateModButtons();
}, () -> this.setFocused(searchWidget));

this.addDrawableChild(searchWidget);
this.setInitialFocus(searchWidget);

// Add the actual mod list buttons
// Put the list between 32 pixels from top and bottom
this.list = new ModListWidget(this.client, this.width, this.height - 64, 32, 25);

this.addSelectableChild(this.list);
this.addDrawableChild(this.list);
this.addDrawableChild(new Button(this.width / 2 - FULL_BUTTON_WIDTH / 2, this.height - 27,
FULL_BUTTON_WIDTH, BUTTON_HEIGHT, ScreenTexts.DONE,
button -> this.client.setScreen(this.previous)));

// Add the Done button
this.addDrawableChild(new Button(this.width / 2 - FULL_BUTTON_WIDTH / 2, this.height - 27, FULL_BUTTON_WIDTH, BUTTON_HEIGHT, ScreenTexts.DONE, button -> this.client.setScreen(this.previous)));

updateModButtons();
initIsProcessing = false;
Expand Down
96 changes: 96 additions & 0 deletions src/main/java/se/icus/mag/modsettings/gui/widget/SearchWidget.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package se.icus.mag.modsettings.gui.widget;

import java.util.List;
import java.util.function.Consumer;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.Element;
import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder;
import net.minecraft.client.gui.tooltip.Tooltip;
import net.minecraft.client.gui.widget.ClickableWidget;
import net.minecraft.client.gui.widget.ContainerWidget;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

public class SearchWidget extends ContainerWidget {
private static final int BUTTON_HEIGHT = 20;

private final List<ClickableWidget> children;
private final TextRenderer textRenderer;
private final Runnable requestFocus;

private final TextFieldWidget textBox;
private boolean showTextBox;

public SearchWidget(int x, int y, int width, String text, TextRenderer textRenderer,
Consumer<String> changedListener, Runnable requestFocus) {
super(x, y, width, BUTTON_HEIGHT, Text.empty());

this.textRenderer = textRenderer;
this.requestFocus = requestFocus;

IconButtonWidget searchButton = new IconButtonWidget(x, y, BUTTON_HEIGHT, BUTTON_HEIGHT,
15, 15, new Identifier("modsettings", "search"),
b -> {
this.setText("");

this.showTextBox = !this.showTextBox;
updateTextBoxVisibility();
});
searchButton.setTooltip(Tooltip.of(Text.translatable("modsettings.search.tooltip")));

textBox = new TextFieldWidget(this.textRenderer, x + 26, y + 2, width - 26, 16,
Text.empty());
textBox.setText(text);
textBox.setChangedListener(changedListener);

this.showTextBox = !text.isEmpty();
updateTextBoxVisibility();

children = List.of(searchButton, textBox);
}

private void updateTextBoxVisibility() {
if (this.showTextBox) {
this.textBox.setVisible(true);
this.textBox.setFocusUnlocked(false);
this.textBox.setFocused(true);
this.requestFocus.run();
} else {
this.textBox.setVisible(false);
this.textBox.setFocusUnlocked(true);
this.textBox.setFocused(false);
}
}

@Override
public void setFocused(Element focused) {
// Force focus to textbox even if button was clicked
super.setFocused(textBox);
}

@Override
public List<? extends Element> children() {
return children;
}

@Override
protected void renderWidget(DrawContext context, int mouseX, int mouseY, float delta) {
children.forEach(c -> c.render(context, mouseX, mouseY, delta));
}

public String getText() {
return textBox.getText();
}

public void setText(String filterText) {
if (!filterText.equals(textBox.getText())) {
textBox.setText(filterText);
}
}

@Override
protected void appendClickableNarrations(NarrationMessageBuilder builder) {
}
}
5 changes: 4 additions & 1 deletion src/main/resources/assets/modsettings/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"modsettings.screen.title": "Mod Settings",
"modsettings.button.title": "Mod Settings...",
"modsettings.key.open": "Open Mod Settings",
"modsettings.key.category": "Mod Settings"
"modsettings.key.category": "Mod Settings",
"modsettings.indirect.show": "Show all mods",
"modsettings.indirect.hide": "Hide indirect mods",
"modsettings.search.tooltip": "Search for mods"
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/textures/collapse.xcf
Binary file not shown.
Binary file added src/main/textures/expand.xcf
Binary file not shown.
Binary file added src/main/textures/search.xcf
Binary file not shown.

0 comments on commit b215f96

Please sign in to comment.