Skip to content

Commit

Permalink
Rough cut of a 'change set' api for config GUIs
Browse files Browse the repository at this point in the history
  • Loading branch information
quat1024 committed Oct 24, 2023
1 parent f58e013 commit b9640cd
Show file tree
Hide file tree
Showing 23 changed files with 495 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,26 @@
import vazkii.quark.api.config.IConfigElement;
import vazkii.quark.api.config.IConfigObject;
import vazkii.quark.base.Quark;
import vazkii.quark.base.module.config.IConfigCallback;
import vazkii.zeta.config.ChangeSet;
import vazkii.zeta.module.ZetaCategory;

import java.util.LinkedHashMap;
import java.util.Map;

@OnlyIn(Dist.CLIENT)
public final class IngameConfigHandler implements IConfigCallback {
@Deprecated
public final class IngameConfigHandler {

@Deprecated
public static final IngameConfigHandler INSTANCE = new IngameConfigHandler();

public Map<String, TopLevelCategory> topLevelCategories = new LinkedHashMap<>();
//TODO: make this non-static mayybe
public final ChangeSet changeSet = new ChangeSet(Quark.ZETA.configInternals);

private IConfigCategory currCategory = null;
public Map<String, TopLevelCategory> topLevelCategories = new LinkedHashMap<>();

private IngameConfigHandler() {}

@Override
public void push(String s, String comment, Object holderObject) {
IConfigCategory newCategory;
if(currCategory == null) {
newCategory = new TopLevelCategory(s, comment, null);
topLevelCategories.put(s, (TopLevelCategory) newCategory);
} else newCategory = currCategory.addCategory(s, comment, holderObject);

currCategory = newCategory;
}

@Override
public void pop() {
if(currCategory != null) {
currCategory.close();
currCategory = currCategory.getParent();
}
}

public IConfigObject<Boolean> getCategoryEnabledObject(ZetaCategory category) {
return topLevelCategories.get("categories").getModuleOption(category);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package vazkii.quark.base.client.config;

import com.google.common.collect.ImmutableSet;
import net.minecraft.Util;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.events.GuiEventListener;
Expand All @@ -11,7 +10,6 @@
import net.minecraft.client.resources.language.I18n;
import net.minecraftforge.client.event.ScreenEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.loading.FMLPaths;
import vazkii.quark.base.client.config.screen.widgets.QButton;
import vazkii.quark.base.handler.GeneralConfig;

Expand Down Expand Up @@ -40,8 +38,4 @@ public static void onGuiInit(ScreenEvent.Init event) {
}
}

public static void openFile() {
Util.getPlatform().openFile(FMLPaths.CONFIGDIR.get().toFile());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public BooleanObject(ConfigValue<Boolean> value, String comment, Boolean default

@Override
public void addWidgets(CategoryScreen parent, IConfigElement element, List<WidgetWrapper> widgets) {
widgets.add(new WidgetWrapper(new CheckboxButton(230, 3, this)));
//widgets.add(new WidgetWrapper(new CheckboxButton(230, 3, this))); //TODO
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import vazkii.quark.api.config.IConfigCategory;
import vazkii.quark.base.client.config.IngameConfigHandler;
import vazkii.quark.base.client.config.obj.AbstractStringInputObject;
import vazkii.quark.base.client.config.obj.ListObject;
import vazkii.zeta.config.ChangeSet;
import vazkii.zeta.config.SectionDefinition;

public abstract class AbstractQScreen extends Screen {

Expand All @@ -22,11 +25,6 @@ public AbstractQScreen(Screen parent) {
this.parent = parent;
}

@Override
public void render(@Nonnull PoseStack mstack, int mouseX, int mouseY, float partialTicks) {
super.render(mstack, mouseX, mouseY, partialTicks);
}

public void returnToParent(Button button) {
minecraft.setScreen(parent);
}
Expand All @@ -39,6 +37,10 @@ public OnPress categoryLink(IConfigCategory category) {
return b -> minecraft.setScreen(new CategoryScreen(this, category));
}

public OnPress sectionLink(SectionDefinition section) {
return b -> minecraft.setScreen(new SectionScreen(this, section));
}

public <T> OnPress stringInput(AbstractStringInputObject<T> object) {
return b -> minecraft.setScreen(new StringInputScreen<>(this, object));
}
Expand All @@ -47,4 +49,12 @@ public OnPress listInput(ListObject object) {
return b -> minecraft.setScreen(new ListInputScreen(this, object));
}

public OnPress nothing_TODO_STUB() {
return b -> {};
}

//it's recommended to access it through here for now, since I dunno if this should be a singleton
public ChangeSet getChangeSet() {
return IngameConfigHandler.INSTANCE.changeSet;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.resources.ResourceLocation;
import vazkii.quark.api.config.IConfigCategory;
import vazkii.quark.base.Quark;
import vazkii.quark.base.client.config.IngameConfigHandler;
import vazkii.quark.base.client.config.screen.widgets.CheckboxButton;
import vazkii.quark.base.client.config.screen.widgets.IconButton;
import vazkii.quark.base.client.config.screen.widgets.SocialButton;
import vazkii.quark.base.handler.ContributorRewardHandler;
import vazkii.quark.base.handler.GeneralConfig;
import vazkii.zeta.config.SectionDefinition;
import vazkii.zeta.config.ValueDefinition;
import vazkii.zeta.module.ZetaCategory;

public class QuarkConfigHomeScreen extends AbstractQScreen {
Expand Down Expand Up @@ -62,19 +62,20 @@ protected void init() {
if(i < categories.size()) {
//a category button
ZetaCategory category = categories.get(i);
IConfigCategory ingameCategory = IngameConfigHandler.INSTANCE.getConfigCategory(category);
ValueDefinition<Boolean> categoryEnabled = Quark.ZETA.weirdConfigSingleton.getCategoryEnabledOption(category);
SectionDefinition categorySection = Quark.ZETA.weirdConfigSingleton.getCategorySection(category);

bWidth -= 20; //room for the checkbox
Button mainButton = addRenderableWidget(new IconButton(x, y, bWidth, 20, componentFor(ingameCategory), category.icon.get(), categoryLink(ingameCategory)));
Button checkButton = addRenderableWidget(new CheckboxButton(x + bWidth, y, IngameConfigHandler.INSTANCE.getCategoryEnabledObject(category)));
Button mainButton = addRenderableWidget(new IconButton(x, y, bWidth, 20, componentFor(categorySection), category.icon.get(), sectionLink(categorySection)));
Button checkButton = addRenderableWidget(new CheckboxButton(x + bWidth, y, categoryEnabled, getChangeSet()));

boolean active = category.modsLoaded(Quark.ZETA);
mainButton.active = active;
checkButton.active = active;
} else {
//"General Settings"
IConfigCategory generalSettings = IngameConfigHandler.INSTANCE.getConfigCategory(null);
addRenderableWidget(new Button(x, y, bWidth, 20, componentFor(generalSettings), categoryLink(generalSettings)));
SectionDefinition generalSection = Quark.ZETA.weirdConfigSingleton.getGeneralSection();
addRenderableWidget(new Button(x, y, bWidth, 20, componentFor(generalSection), sectionLink(generalSection)));
}
}

Expand Down Expand Up @@ -108,17 +109,18 @@ private static List<Integer> centeredRow(int centerX, int buttonWidth, int hpad,
return result;
}

private static Component componentFor(IConfigCategory c) {
MutableComponent comp = Component.translatable("quark.category." + c.getName());
private Component componentFor(SectionDefinition section) {
MutableComponent comp = Component.translatable("quark.category." + section.name);

if(c.isDirty())
if(getChangeSet().isDirty(section))
comp.append(Component.literal("*").withStyle(ChatFormatting.GOLD));

return comp;
}

public void commit(Button button) {
IngameConfigHandler.INSTANCE.commit();
//IngameConfigHandler.INSTANCE.commit();
getChangeSet().applyAllChanges();
returnToParent(button);
}

Expand Down Expand Up @@ -149,6 +151,11 @@ public void render(@Nonnull PoseStack mstack, int mouseX, int mouseY, float part
drawCenteredString(mstack, font, ChatFormatting.BOLD + I18n.get("quark.gui.config.header", WordUtils.capitalizeFully(Quark.MOD_ID)), width / 2, 15, 0x48ddbc);
drawCenteredString(mstack, font, I18n.get("quark.gui.config.subheader1", ChatFormatting.LIGHT_PURPLE, ContributorRewardHandler.featuredPatron, ChatFormatting.RESET), width / 2, 28, 0x9EFFFE);
drawCenteredString(mstack, font, I18n.get("quark.gui.config.subheader2"), width / 2, 38, 0x9EFFFE);

//TODO TODO TODO flesh this out
int changeCount = getChangeSet().changeCount();
if(changeCount != 0)
drawCenteredString(mstack, font, changeCount + " unsaved change" + (changeCount > 1 ? "s" : ""), width/2 - 150, height-30, 0xFF8800);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package vazkii.quark.base.client.config.screen;

import javax.annotation.Nonnull;

import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.resources.language.I18n;
import org.apache.commons.lang3.text.WordUtils;
import vazkii.quark.base.Quark;
import vazkii.quark.base.client.config.screen.widgets.ScrollableWidgetList;
import vazkii.quark.base.client.config.screen.widgets.SectionList;
import vazkii.zeta.config.SectionDefinition;

public class SectionScreen extends AbstractScrollingWidgetScreen {

public final SectionDefinition section;
private String breadcrumbs;

public SectionScreen(Screen parent, SectionDefinition section) {
super(parent);
this.section = section;
}

@Override
protected void init() {
super.init();

// breadcrumbs = section.getName();
// IConfigCategory currCategory = section.getParent();
// while(currCategory != null) {
// breadcrumbs = String.format("%s > %s", currCategory.getName(), breadcrumbs);
// currCategory = currCategory.getParent();
// }
// breadcrumbs = String.format("> %s", breadcrumbs);
breadcrumbs = section.name; //TODO readd breadcrumbs
}

@Override
public void render(@Nonnull PoseStack mstack, int mouseX, int mouseY, float partialTicks) {
super.render(mstack, mouseX, mouseY, partialTicks);

int left = 20;

String modName = WordUtils.capitalizeFully(Quark.MOD_ID);
font.draw(mstack, ChatFormatting.BOLD + I18n.get("quark.gui.config.header", modName), left, 10, 0x48ddbc);
font.draw(mstack, breadcrumbs, left, 20, 0xFFFFFF);
}

@Override
protected ScrollableWidgetList<?, ?> createWidgetList() {
return new SectionList<>(this);
}

@Override
protected void onClickDefault(Button b) {
getChangeSet().resetToDefault(section);
}

@Override
protected void onClickDiscard(Button b) {
getChangeSet().removeChange(section);
}

@Override
protected boolean isDirty() {
return getChangeSet().isDirty(section);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package vazkii.quark.base.client.config.screen.widgets;

import java.util.function.Supplier;

import javax.annotation.Nonnull;

import com.mojang.blaze3d.systems.RenderSystem;
Expand All @@ -10,20 +8,25 @@
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.network.chat.Component;
import vazkii.quark.api.config.IConfigObject;
import vazkii.quark.base.handler.MiscUtil;
import vazkii.zeta.config.ChangeSet;
import vazkii.zeta.config.ValueDefinition;

public class CheckboxButton extends Button {

private final Supplier<Boolean> checkedSupplier;
private final ValueDefinition<Boolean> value;
private final ChangeSet changes;

public CheckboxButton(int x, int y, Supplier<Boolean> checkedSupplier, OnPress onClick) {
super(x, y, 20, 20, Component.literal(""), onClick);
this.checkedSupplier = checkedSupplier;
public CheckboxButton(int x, int y, ValueDefinition<Boolean> value, ChangeSet changes) {
super(x, y, 20, 20, Component.literal(""), CheckboxButton::toggle);
this.value = value;
this.changes = changes;
}

public CheckboxButton(int x, int y, IConfigObject<Boolean> configObj) {
this(x, y, configObj::getCurrentObj, (b) -> configObj.setCurrentObj(!configObj.getCurrentObj()));
private static void toggle(Button press) {
if(press instanceof CheckboxButton checkbox) {
checkbox.changes.toggle(checkbox.value);
}
}

@Override
Expand All @@ -33,7 +36,7 @@ public void renderButton(@Nonnull PoseStack mstack, int mouseX, int mouseY, floa
RenderSystem.setShader(GameRenderer::getPositionTexShader);
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
RenderSystem.setShaderTexture(0, MiscUtil.GENERAL_ICONS);
boolean enabled = checkedSupplier.get() && active;
boolean enabled = changes.get(value) && active;
int u = enabled ? 0 : 16;
int v = 93;

Expand Down
Loading

0 comments on commit b9640cd

Please sign in to comment.