Skip to content

Commit

Permalink
Remove old config logic, replace with new Config GUI completely
Browse files Browse the repository at this point in the history
  • Loading branch information
shedaniel committed Nov 3, 2023
1 parent 259880a commit 4d396d1
Show file tree
Hide file tree
Showing 14 changed files with 160 additions and 961 deletions.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@
import java.util.function.Function;

public class FilteringAddRuleScreen extends Screen {
private final FilteringEntry entry;
private final List<FilteringRule<?>> rules;
private RulesList rulesList;
Screen parent;

public FilteringAddRuleScreen(FilteringEntry entry) {
public FilteringAddRuleScreen(List<FilteringRule<?>> rules) {
super(new TranslatableComponent("config.roughlyenoughitems.filteringRulesScreen.new"));
this.entry = entry;
this.rules = rules;
}

@Override
Expand All @@ -68,7 +68,7 @@ public void init() {
rulesList = addWidget(new RulesList(minecraft, width, height, 30, height, BACKGROUND_LOCATION));
for (FilteringRuleType<?> rule : FilteringRuleTypeRegistry.getInstance()) {
if (!rule.isSingular())
rulesList.addItem(new DefaultRuleEntry(parent, entry, rule.createNew(), null));
rulesList.addItem(new DefaultRuleEntry(parent, rules, rule.createNew(), null));
}
rulesList.selectItem(rulesList.children().get(0));
}
Expand Down Expand Up @@ -155,13 +155,12 @@ public static class DefaultRuleEntry extends RuleEntry {
private final Button addButton;
private final Function<Screen, Screen> screenFunction;

public DefaultRuleEntry(Screen parent, FilteringEntry entry, FilteringRule<?> rule, Function<Screen, Screen> screenFunction) {
public DefaultRuleEntry(Screen parent, List<FilteringRule<?>> rules, FilteringRule<?> rule, Function<Screen, Screen> screenFunction) {
super(rule);
this.screenFunction = (screenFunction == null ? ((FilteringRuleType<FilteringRule<?>>) rule.getType()).createEntryScreen(rule) : screenFunction);
addButton = new Button(0, 0, 20, 20, Component.nullToEmpty(" + "), button -> {
entry.edited = true;
Minecraft.getInstance().setScreen(this.screenFunction.apply(parent));
entry.rules.add(0, rule);
rules.add(0, rule);
});
addButton.active = this.screenFunction != null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,94 +23,25 @@

package me.shedaniel.rei.impl.client.config.entries;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.mojang.blaze3d.platform.Window;
import com.mojang.blaze3d.vertex.PoseStack;
import me.shedaniel.clothconfig2.api.AbstractConfigListEntry;
import me.shedaniel.rei.api.client.entry.filtering.FilteringRule;
import me.shedaniel.rei.api.common.entry.EntryStack;
import me.shedaniel.rei.api.common.util.EntryStacks;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.chat.NarratorChatListener;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.gui.narration.NarratableEntry;
import net.minecraft.network.chat.TranslatableComponent;
import org.jetbrains.annotations.ApiStatus;

import java.util.*;
import java.util.function.Consumer;

@ApiStatus.Internal
public class FilteringEntry extends AbstractConfigListEntry<List<EntryStack<?>>> {
private int width;
Consumer<List<EntryStack<?>>> saveConsumer;
Consumer<List<FilteringRule<?>>> rulesSaveConsumer;
List<EntryStack<?>> defaultValue;
Set<EntryStack<?>> configFiltered;
List<FilteringRule<?>> rules;
boolean edited = false;
final FilteringScreen filteringScreen = new FilteringScreen(this);
final FilteringRulesScreen filteringRulesScreen = new FilteringRulesScreen(this);
private final AbstractWidget buttonWidget = new Button(0, 0, 0, 20, new TranslatableComponent("config.roughlyenoughitems.filteringScreen"), button -> {
filteringRulesScreen.parent = Minecraft.getInstance().screen;
Minecraft.getInstance().setScreen(filteringRulesScreen);
});
private final List<AbstractWidget> children = ImmutableList.of(buttonWidget);

public FilteringEntry(int width, List<EntryStack<?>> configFiltered, List<FilteringRule<?>> rules, List<EntryStack<?>> defaultValue, Consumer<List<EntryStack<?>>> saveConsumer, Consumer<List<FilteringRule<?>>> rulesSaveConsumer) {
super(NarratorChatListener.NO_TITLE, false);
this.width = width;
this.configFiltered = new TreeSet<>(Comparator.comparing(EntryStacks::hashExact));
this.configFiltered.addAll(configFiltered);
this.rules = Lists.newArrayList(rules);
this.defaultValue = defaultValue;
this.saveConsumer = saveConsumer;
this.rulesSaveConsumer = rulesSaveConsumer;
}

@Override
public List<EntryStack<?>> getValue() {
return Lists.newArrayList(configFiltered);
}

@Override
public Optional<List<EntryStack<?>>> getDefaultValue() {
return Optional.ofNullable(defaultValue);
}

@Override
public void save() {
saveConsumer.accept(getValue());
rulesSaveConsumer.accept(rules);
this.edited = false;
}

@Override
public void render(PoseStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean isSelected, float delta) {
super.render(matrices, index, y, x, entryWidth, entryHeight, mouseX, mouseY, isSelected, delta);
Window window = Minecraft.getInstance().getWindow();
this.buttonWidget.active = this.isEditable();
this.buttonWidget.y = y;
this.buttonWidget.x = x + entryWidth / 2 - width / 2;
this.buttonWidget.setWidth(width);
this.buttonWidget.render(matrices, mouseX, mouseY, delta);
}

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

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

@Override
public boolean isEdited() {
return super.isEdited() || edited;
public record FilteringEntry(
Set<EntryStack<?>> configFiltered,
List<FilteringRule<?>> rules,
FilteringScreen filteringScreen,
FilteringRulesScreen filteringRulesScreen
) {
public static FilteringEntry of(List<EntryStack<?>> configFiltered, List<FilteringRule<?>> rules) {
TreeSet<EntryStack<?>> set = new TreeSet<>(Comparator.comparing(EntryStacks::hashExact));
set.addAll(configFiltered);
ArrayList<FilteringRule<?>> list = new ArrayList<>(rules);
FilteringScreen filteringScreen = new FilteringScreen(set);
return new FilteringEntry(set, list, filteringScreen, new FilteringRulesScreen(filteringScreen, list));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@
import java.util.stream.Collectors;

public class FilteringRulesScreen extends Screen {
private final FilteringEntry entry;
private final FilteringScreen filteringScreen;
private final List<FilteringRule<?>> rules;
private RulesList rulesList;
Screen parent;
public Screen parent;

public FilteringRulesScreen(FilteringEntry entry) {
public FilteringRulesScreen(FilteringScreen filteringScreen, List<FilteringRule<?>> rules) {
super(new TranslatableComponent("config.roughlyenoughitems.filteringRulesScreen"));
this.entry = entry;
this.filteringScreen = filteringScreen;
this.rules = rules;
}

@Override
Expand All @@ -83,20 +85,20 @@ public void init() {
{
Component addText = new TextComponent(" + ");
addRenderableWidget(new Button(width - 4 - 20, 4, 20, 20, addText, button -> {
FilteringAddRuleScreen screen = new FilteringAddRuleScreen(entry);
FilteringAddRuleScreen screen = new FilteringAddRuleScreen(rules);
screen.parent = this;
minecraft.setScreen(screen);
}));
}
rulesList = addWidget(new RulesList(minecraft, width, height, 30, height, BACKGROUND_LOCATION));
for (int i = entry.rules.size() - 1; i >= 0; i--) {
FilteringRule<?> rule = entry.rules.get(i);
for (int i = rules.size() - 1; i >= 0; i--) {
FilteringRule<?> rule = rules.get(i);
if (rule instanceof ManualFilteringRule)
rulesList.addItem(new DefaultRuleEntry(rule, entry, (screen) -> {
entry.filteringScreen.parent = screen;
return entry.filteringScreen;
rulesList.addItem(new DefaultRuleEntry(rule, rules, (screen) -> {
filteringScreen.parent = screen;
return filteringScreen;
}));
else rulesList.addItem(new DefaultRuleEntry(rule, entry, null));
else rulesList.addItem(new DefaultRuleEntry(rule, rules, null));
}
rulesList.selectItem(rulesList.children().get(0));
}
Expand Down Expand Up @@ -199,11 +201,10 @@ public static class DefaultRuleEntry extends RuleEntry {
private final Button deleteButton;
private final Function<Screen, Screen> screenFunction;

public DefaultRuleEntry(FilteringRule<?> rule, FilteringEntry entry, Function<Screen, Screen> screenFunction) {
public DefaultRuleEntry(FilteringRule<?> rule, List<FilteringRule<?>> rules, Function<Screen, Screen> screenFunction) {
super(rule);
this.screenFunction = Objects.requireNonNullElseGet(screenFunction == null ? ((FilteringRuleType<FilteringRule<?>>) rule.getType()).createEntryScreen(rule) : screenFunction, () -> placeholderScreen(rule));
configureButton = new Button(0, 0, 20, 20, Component.nullToEmpty(null), button -> {
entry.edited = true;
Minecraft.getInstance().setScreen(this.screenFunction.apply(Minecraft.getInstance().screen));
}) {
@Override
Expand All @@ -217,8 +218,7 @@ protected void renderBg(PoseStack matrices, Minecraft client, int mouseX, int mo
Component deleteText = new TranslatableComponent("config.roughlyenoughitems.filteringRulesScreen.delete");
deleteButton = new Button(0, 0, Minecraft.getInstance().font.width(deleteText) + 10, 20, deleteText, button -> {
final Screen screen = Minecraft.getInstance().screen;
entry.edited = true;
entry.rules.remove(rule);
rules.remove(rule);
screen.init(Minecraft.getInstance(), screen.width, screen.height);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,15 @@ public int getScrollBarX(int maxX) {
};

Screen parent;
private FilteringEntry filteringEntry;
private Set<EntryStack<?>> configFiltered;
private Tooltip tooltip = null;
private List<EntryStack<?>> entryStacks = null;
private Rectangle innerBounds;
private List<FilteringListEntry> entries = Collections.emptyList();
private List<GuiEventListener> elements = Collections.emptyList();

private record PointPair(Point firstPoint, @Nullable Point secondPoint) {}
private record PointPair(Point firstPoint, @Nullable Point secondPoint) {
}

private List<PointPair> points = new ArrayList<>();

Expand All @@ -108,9 +109,9 @@ private record PointPair(Point firstPoint, @Nullable Point secondPoint) {}

private SearchFilter lastFilter = SearchFilter.matchAll();

public FilteringScreen(FilteringEntry filteringEntry) {
public FilteringScreen(Set<EntryStack<?>> configFiltered) {
super(new TranslatableComponent("config.roughlyenoughitems.filteringScreen"));
this.filteringEntry = filteringEntry;
this.configFiltered = configFiltered;
this.searchField = new OverlaySearchField(0, 0, 0, 0);
{
Component selectAllText = new TranslatableComponent("config.roughlyenoughitems.filteredEntries.selectAll");
Expand All @@ -133,8 +134,7 @@ public FilteringScreen(FilteringEntry filteringEntry) {
FilteringListEntry entry = entries.get(i);
entry.getBounds().y = entry.backupY - scrolling.scrollAmountInt();
if (entry.isSelected() && !entry.isFiltered()) {
filteringEntry.configFiltered.add(stack);
filteringEntry.edited = true;
configFiltered.add(stack);
entry.dirty = true;
}
}
Expand All @@ -147,8 +147,7 @@ public FilteringScreen(FilteringEntry filteringEntry) {
EntryStack<?> stack = entryStacks.get(i);
FilteringListEntry entry = entries.get(i);
entry.getBounds().y = entry.backupY - scrolling.scrollAmountInt();
if (entry.isSelected() && filteringEntry.configFiltered.remove(stack)) {
filteringEntry.edited = true;
if (entry.isSelected() && configFiltered.remove(stack)) {
entry.dirty = true;
}
}
Expand Down Expand Up @@ -491,7 +490,7 @@ public boolean isSelected() {

public boolean isFiltered() {
if (dirty) {
filtered = filteringEntry.configFiltered.contains(getCurrentEntry());
filtered = configFiltered.contains(getCurrentEntry());
dirty = false;
}
return filtered;
Expand Down
Loading

0 comments on commit 4d396d1

Please sign in to comment.