Skip to content

Commit

Permalink
add api to add custom screens, use a color theme builder instead of a…
Browse files Browse the repository at this point in the history
… direct method
  • Loading branch information
douira committed Oct 25, 2024
1 parent 754041c commit 6fd07b4
Show file tree
Hide file tree
Showing 22 changed files with 228 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.caffeinemc.mods.sodium.api.config.structure;

public interface ColorThemeBuilder {
ColorThemeBuilder setBaseThemeRGB(int theme);

ColorThemeBuilder setFullThemeRGB(int theme, int themeHighlight, int themeDisabled);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ public interface ConfigBuilder {

ModOptionsBuilder registerOwnModOptions();

ColorThemeBuilder createColorTheme();

OptionPageBuilder createOptionPage();

ExternalPageBuilder createExternalPage();

OptionGroupBuilder createOptionGroup();

BooleanOptionBuilder createBooleanOption(ResourceLocation id);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.caffeinemc.mods.sodium.api.config.structure;

import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;

import java.util.function.Consumer;

public interface ExternalPageBuilder extends PageBuilder {
ExternalPageBuilder setName(Component name);

ExternalPageBuilder setScreenProvider(Consumer<Screen> currentScreenConsumer);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ public interface ModOptionsBuilder {

ModOptionsBuilder setVersion(String version);

ModOptionsBuilder setColorThemeRGB(int theme, int themeHighlight, int themeDisabled);
ModOptionsBuilder setColorTheme(ColorThemeBuilder colorTheme);

ModOptionsBuilder setColorThemeRGB(int theme);

ModOptionsBuilder addPage(OptionPageBuilder page);
ModOptionsBuilder addPage(PageBuilder page);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import net.minecraft.network.chat.Component;

public interface OptionPageBuilder {
public interface OptionPageBuilder extends PageBuilder {
OptionPageBuilder setName(Component name);

OptionPageBuilder addOptionGroup(OptionGroupBuilder group);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package net.caffeinemc.mods.sodium.api.config.structure;

public interface PageBuilder {
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private static void registerConfigs(BiConsumer<ConfigEntryPoint, ConfigBuilder>
registerMethod.accept(entryPoint, builder);
builtConfigs = builder.build();
} catch (Exception e) {
Minecraft.getInstance().emergencySaveAndCrash(new CrashReport("Failed to build options config for mod " + configUser.modId, e));
crashWithMessage("Failed to build options config for mod " + configUser.modId, e);
return;
}

Expand Down Expand Up @@ -114,7 +114,12 @@ private static void registerConfigs(BiConsumer<ConfigEntryPoint, ConfigBuilder>
try {
CONFIG = new Config(ImmutableList.copyOf(modConfigs));
} catch (Exception e) {
Minecraft.getInstance().emergencySaveAndCrash(new CrashReport("Failed to build options config", e));
crashWithMessage("Failed to build options config", e);
}
}

private static void crashWithMessage(String message, Exception e) {
var instance = Minecraft.getInstance();
Minecraft.crash(instance, instance.gameDirectory, new CrashReport(message, e));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package net.caffeinemc.mods.sodium.client.config.structure;

import net.caffeinemc.mods.sodium.api.config.structure.ColorThemeBuilder;
import net.caffeinemc.mods.sodium.client.gui.ColorTheme;

public class ColorThemeBuilderImpl implements ColorThemeBuilder {
private int baseTheme;
private int themeHighlight;
private int themeDisabled;

ColorTheme build() {
if (this.baseTheme == 0) {
throw new IllegalStateException("Base theme must be set");
}

if (this.themeHighlight == 0 || this.themeDisabled == 0) {
return new ColorTheme(this.baseTheme);
} else {
return new ColorTheme(this.baseTheme, this.themeHighlight, this.themeDisabled);
}
}

@Override
public ColorThemeBuilder setBaseThemeRGB(int theme) {
this.baseTheme = theme;
return this;
}

@Override
public ColorThemeBuilder setFullThemeRGB(int theme, int themeHighlight, int themeDisabled) {
this.baseTheme = theme;
this.themeHighlight = themeHighlight;
this.themeDisabled = themeDisabled;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,21 @@ public ModOptionsBuilder registerOwnModOptions() {
return this.registerModOptions(this.defaultNamespace, this.defaultName, this.defaultVersion);
}

@Override
public ColorThemeBuilder createColorTheme() {
return new ColorThemeBuilderImpl();
}

@Override
public OptionPageBuilder createOptionPage() {
return new OptionPageBuilderImpl();
}

@Override
public ExternalPageBuilder createExternalPage() {
return new ExternalPageBuilderImpl();
}

@Override
public OptionGroupBuilder createOptionGroup() {
return new OptionGroupBuilderImpl();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package net.caffeinemc.mods.sodium.client.config.structure;

import com.google.common.collect.ImmutableList;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;

import java.util.function.Consumer;

public record ExternalPage(Component name, Consumer<Screen> currentScreenConsumer) implements Page {
@Override
public ImmutableList<OptionGroup> groups() {
return ImmutableList.of();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package net.caffeinemc.mods.sodium.client.config.structure;

import net.caffeinemc.mods.sodium.api.config.structure.ExternalPageBuilder;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import org.apache.commons.lang3.Validate;

import java.util.function.Consumer;

public class ExternalPageBuilderImpl extends PageBuilderImpl implements ExternalPageBuilder {
private Component name;
private Consumer<Screen> currentScreenConsumer;

@Override
ExternalPage build() {
Validate.notNull(this.name, "Name must not be null");
Validate.notNull(this.currentScreenConsumer, "Screen consumer must not be null");

return new ExternalPage(this.name, this.currentScreenConsumer);
}

@Override
public ExternalPageBuilder setName(Component name) {
this.name = name;
return this;
}

@Override
public ExternalPageBuilder setScreenProvider(Consumer<Screen> currentScreenConsumer) {
this.currentScreenConsumer = currentScreenConsumer;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import com.google.common.collect.ImmutableList;
import net.caffeinemc.mods.sodium.client.gui.ColorTheme;

public record ModOptions(String namespace, String name, String version, ColorTheme theme, ImmutableList<OptionPage> pages) {
public record ModOptions(String namespace, String name, String version, ColorTheme theme, ImmutableList<Page> pages) {
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package net.caffeinemc.mods.sodium.client.config.structure;

import com.google.common.collect.ImmutableList;
import net.caffeinemc.mods.sodium.api.config.structure.ColorThemeBuilder;
import net.caffeinemc.mods.sodium.api.config.structure.ModOptionsBuilder;
import net.caffeinemc.mods.sodium.api.config.structure.OptionPageBuilder;
import net.caffeinemc.mods.sodium.api.config.structure.PageBuilder;
import net.caffeinemc.mods.sodium.client.gui.ColorTheme;
import net.caffeinemc.mods.sodium.client.gui.Colors;
import org.apache.commons.lang3.Validate;
Expand All @@ -15,7 +16,7 @@ class ModOptionsBuilderImpl implements ModOptionsBuilder {
private String name;
private String version;
private ColorTheme theme;
private final List<OptionPage> pages = new ArrayList<>();
private final List<Page> pages = new ArrayList<>();

ModOptionsBuilderImpl(String namespace, String name, String version) {
this.namespace = namespace;
Expand All @@ -29,7 +30,7 @@ ModOptions build() {
Validate.notEmpty(this.pages, "At least one page must be added");

if (this.theme == null) {
this.theme = ColorTheme.DEFAULT;
this.theme = ColorTheme.PRESETS[Math.abs(this.namespace.hashCode()) % ColorTheme.PRESETS.length];
}

return new ModOptions(this.namespace, this.name, this.version, this.theme, ImmutableList.copyOf(this.pages));
Expand All @@ -48,19 +49,14 @@ public ModOptionsBuilder setVersion(String version) {
}

@Override
public ModOptionsBuilder setColorThemeRGB(int theme, int themeLighter, int themeDarker) {
this.theme = new ColorTheme(theme, themeLighter, themeDarker);
public ModOptionsBuilder setColorTheme(ColorThemeBuilder theme) {
this.theme = ((ColorThemeBuilderImpl) theme).build();
return this;
}

@Override
public ModOptionsBuilder setColorThemeRGB(int theme) {
return this.setColorThemeRGB(theme, Colors.lighten(theme), Colors.darken(theme));
}

@Override
public ModOptionsBuilder addPage(OptionPageBuilder builder) {
this.pages.add(((OptionPageBuilderImpl) builder).build());
public ModOptionsBuilder addPage(PageBuilder builder) {
this.pages.add(((PageBuilderImpl) builder).build());
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import com.google.common.collect.ImmutableList;
import net.minecraft.network.chat.Component;

public record OptionPage(Component name, ImmutableList<OptionGroup> groups) {
public record OptionPage(Component name, ImmutableList<OptionGroup> groups) implements Page {
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
import java.util.ArrayList;
import java.util.List;

class OptionPageBuilderImpl implements OptionPageBuilder {
class OptionPageBuilderImpl extends PageBuilderImpl implements OptionPageBuilder {
private Component name;
private final List<OptionGroup> groups = new ArrayList<>();

@Override
OptionPage build() {
Validate.notNull(this.name, "Name must not be null");
Validate.notEmpty(this.groups, "At least one group must be added");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package net.caffeinemc.mods.sodium.client.config.structure;

import com.google.common.collect.ImmutableList;
import net.minecraft.network.chat.Component;

public interface Page {
Component name();

ImmutableList<OptionGroup> groups();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package net.caffeinemc.mods.sodium.client.config.structure;

public abstract class PageBuilderImpl {
abstract Page build();
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
package net.caffeinemc.mods.sodium.client.gui;

import java.util.stream.Stream;

public class ColorTheme {
public final int theme;
public final int themeLighter;
public final int themeDarker;

public static final ColorTheme DEFAULT = new ColorTheme(Colors.THEME, Colors.THEME_LIGHTER, Colors.THEME_DARKER);

public static final ColorTheme[] PRESETS = Stream.of(
0xFFE494A5, 0xFFAB94E4, 0xFFCDE494, 0xFFD394E4, 0xFFE4D394
).map(ColorTheme::new).toArray(ColorTheme[]::new);

public ColorTheme(int theme, int themeLighter, int themeDarker) {
this.theme = theme;
this.themeLighter = themeLighter;
this.themeDarker = themeDarker;
}

public ColorTheme(int theme) {
this(theme, Colors.lighten(theme), Colors.darken(theme));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import net.caffeinemc.mods.sodium.client.gui.options.control.ControlValueFormatterImpls;
import net.caffeinemc.mods.sodium.client.services.PlatformRuntimeInformation;
import net.minecraft.client.*;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -87,6 +89,8 @@ private void buildEarlyConfig(ConfigBuilder builder) {

private void buildFullConfig(ConfigBuilder builder) {
builder.registerOwnModOptions()
.setColorTheme(builder.createColorTheme().setFullThemeRGB(
Colors.THEME, Colors.THEME_LIGHTER, Colors.THEME_DARKER))
.setName("Sodium Renderer")
.addPage(this.buildGeneralPage(builder))
.addPage(this.buildQualityPage(builder))
Expand Down Expand Up @@ -119,9 +123,20 @@ public V load() {
// for testing cycle detection
// .setEnabledProvider((state) -> state.readIntOption(ResourceLocation.parse("foo:baz")) == 0, ResourceLocation.parse("foo:baz"))

// more colors: 0xFFAB94E4, 0xFFCDE494, 0xFFD394E4, 0xFFE4D394
ModOptionsBuilder options = builder.registerModOptions("foo", "Foo", "1.0")
.setColorThemeRGB(0xFFE494A5)
.addPage(
builder.createExternalPage()
.setName(Component.literal("External Page"))
.setScreenProvider((prevScreen) -> {
Minecraft.getInstance().setScreen(new Screen(Component.literal("External Page")) {
@Override
public void render(GuiGraphics graphics, int mouseX, int mouseY, float delta) {
super.render(graphics, mouseX, mouseY, delta);
graphics.drawString(Minecraft.getInstance().font, Component.literal("Hello, world!"), 10, 10, 0xFFFFFF);
}
});
})
)
.addPage(builder.createOptionPage()
.setName(Component.literal("Foo Pagej fdjfjfl jfdskl fdjkllfffsdldfskjl j"))
.addOptionGroup(builder.createOptionGroup()
Expand Down
Loading

0 comments on commit 6fd07b4

Please sign in to comment.