Skip to content

Commit

Permalink
Split Requirement into Enable/DisableIf
Browse files Browse the repository at this point in the history
Instead of having `Requirement` be a separate annotation, simplify the UX by copying its parameters directly to both `EnableIf` and `DisplayIf`.

This is kinda painful, since it leads to effectively duplicated annotation code, however it dramatically cleans up the user experience (as seen in `ExampleConfig`)...
  • Loading branch information
MattSturgeon committed Aug 2, 2023
1 parent cca4a0f commit 6019831
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
import me.shedaniel.clothconfig2.api.DisableableWidget;
import me.shedaniel.clothconfig2.api.HideableWidget;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.*;

public class ConfigEntry {

Expand Down Expand Up @@ -163,37 +160,12 @@ public static class Requirements {
private Requirements() {}

/**
* Defines zero or more {@link Requirement} definitions that will control whether this Config Entry GUI is
* enabled or disabled.
*
* @see Requirement
* @see DisableableWidget
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface EnableIf {
Requirement[] value();
Quantifier quantifier() default Quantifier.ALL;
}

/**
* Defines zero or more {@link Requirement} definitions that will control whether this Config Entry GUI is
* displayed on the screen.
* Defines a requirement that will control whether this Config Entry GUI is enabled or disabled.
*
* <p>
* The requirement either references a handler method or another Config Entry GUI.
* </p>
*
* @see Requirement
* @see HideableWidget
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface DisplayIf {
Requirement[] value();
Quantifier quantifier() default Quantifier.ALL;
}

/**
* Defines a Requirement, which is a reference to a method handler
* along with a set of arguments to be passed to the Handler.
*
* <p>
* If a handler method is referenced, it will be passed {@link #refArgs()} and {@link #staticArgs()}.
* </p>
Expand All @@ -207,20 +179,24 @@ private Requirements() {}
* However if the referenced Config Entry has a <strong>boolean value</strong>, a default condition
* of {@code "true"} will be assumed.
* </p>
*
* @see DisableableWidget
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface Requirement {
@Target(ElementType.FIELD)
@Repeatable(EnableIfGroup.class)
public @interface EnableIf {

/**
* A {@link Ref reference} to a Handler method or a Config Entry.
*
*
* @see DefaultRequirements
*/
Ref value();

/**
* One or more conditions to be compared with the depended-on Config Entry's value.
* Will be parsed in the same way as {@link Requirement#staticArgs()}.
* Will be parsed in the same way as {@link #staticArgs()}.
*/
String[] conditions() default {};

Expand All @@ -231,7 +207,7 @@ private Requirements() {}

/**
* Zero or more static values to be passed to the handler method.
*
*
* <p>The following parameter types are supported:
* <ul>
* <li>{@link String}: The arg will be used as-is.
Expand All @@ -251,6 +227,43 @@ private Requirements() {}
String[] staticArgs() default {};
}

/**
* Defines a requirement that will control whether this Config Entry GUI is displayed on the screen.
*
* <p>
* Otherwise identical to {@link EnableIf}
* </p>
*
* @see EnableIf
* @see HideableWidget
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@Repeatable(DisplayIfGroup.class)
public @interface DisplayIf {

/**
* @see EnableIf#value()
*/
Ref value();

/**
* @see EnableIf#conditions()
*/
String[] conditions() default {};

/**
* @see EnableIf#refArgs()
*/
Ref[] refArgs() default {};

/**
* @see EnableIf#staticArgs()
*/
String[] staticArgs() default {};
}


/**
* Can be applied to a handler method to declare a list of {@link Ref refs} that should be passed to the handler
* as its initial arguments.
Expand All @@ -260,6 +273,20 @@ private Requirements() {}
public @interface ConstParams {
Ref[] value();
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface EnableIfGroup {
EnableIf[] value();
Quantifier quantifier() default Quantifier.ALL;
}

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface DisplayIfGroup {
DisplayIf[] value();
Quantifier quantifier() default Quantifier.ALL;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import me.shedaniel.autoconfig.annotation.ConfigEntry;
import me.shedaniel.autoconfig.annotation.ConfigEntry.Gui.EnumHandler.EnumDisplayOption;
import me.shedaniel.autoconfig.annotation.ConfigEntry.Ref;
import me.shedaniel.autoconfig.annotation.ConfigEntry.Requirements.Requirement;
import me.shedaniel.autoconfig.requirements.DefaultRequirements;
import me.shedaniel.autoconfig.serializer.PartitioningSerializer;
import org.jetbrains.annotations.ApiStatus;
Expand Down Expand Up @@ -134,71 +133,58 @@ public static class DependencySubCategory {
@ConfigEntry.BoundedDiscrete(min = -100, max = 100)
public int intSlider = 50;

@ConfigEntry.Requirements.EnableIf(
@Requirement(@Ref("coolToggle"))
)
@ConfigEntry.Requirements.EnableIf(@Ref("coolToggle"))
public boolean dependsOnCoolToggle1 = false;

@ConfigEntry.Requirements.DisplayIf(
@Requirement(@Ref("coolToggle"))
)
@ConfigEntry.Requirements.DisplayIf(@Ref("coolToggle"))

public boolean dependsOnCoolToggle2 = false;

@ConfigEntry.Requirements.EnableIf(@Requirement(
@ConfigEntry.Requirements.EnableIf(
value = @Ref(cls = DefaultRequirements.class, value = DefaultRequirements.IS),
refArgs = { @Ref("coolToggle"), @Ref("lameToggle") }
))
)
public boolean dependsOnToggleMatch = false;

@ConfigEntry.Requirements.EnableIf(@Requirement(@Ref(cls = Handlers.class, value = "intSliderIsBigOrSmall")))
@ConfigEntry.Requirements.EnableIf(@Ref(cls = Handlers.class, value = "intSliderIsBigOrSmall"))
public boolean dependsOnIntSlider = true;

@ConfigEntry.Gui.TransitiveObject
@ConfigEntry.Requirements.EnableIf(@Ref("coolToggle"))
@ConfigEntry.Requirements.EnableIf(
value = {
@Requirement(@Ref("coolToggle")),
@Requirement(
value = @Ref("coolEnum"),
conditions = { "GOOD", "EXCELLENT" }
)
},
quantifier = ConfigEntry.Quantifier.ALL
value = @Ref("coolEnum"),
conditions = { "GOOD", "EXCELLENT" }
)
public DependantObject dependantObject = new DependantObject();
public static class DependantObject {
@ConfigEntry.Gui.PrefixText
public boolean toggle1 = false;

@ConfigEntry.Requirements.EnableIf(
@Requirement(
value = @Ref(cls = DefaultRequirements.class, value = DefaultRequirements.ANY_MATCH),
refArgs = @Ref(cls = DependencySubCategory.class, value = "intSlider"),
staticArgs = { "50", "100" }
)
value = @Ref(cls = DefaultRequirements.class, value = DefaultRequirements.ANY_MATCH),
refArgs = @Ref(cls = DependencySubCategory.class, value = "intSlider"),
staticArgs = { "50", "100" }
)
public boolean toggle2 = true;
}

@ConfigEntry.Gui.CollapsibleObject(startExpanded = true)
@ConfigEntry.Requirements.EnableIf(
@Requirement(@Ref("coolToggle"))
)
@ConfigEntry.Requirements.EnableIf(@Ref("coolToggle"))
public DependantCollapsible dependantCollapsible = new DependantCollapsible();
public static class DependantCollapsible {
public boolean toggle1 = false;
public boolean toggle2 = true;
}

@ConfigEntry.Requirements.EnableIf(
@Requirement(@Ref(cls = DependencySubCategory.class, value = "coolToggle"))
@Ref(cls = DependencySubCategory.class, value = "coolToggle")
)
public List<Integer> list = Arrays.asList(1, 2, 3);

}

@ConfigEntry.Requirements.EnableIf(
@Requirement(@Ref(cls = DependencySubCategory.class, value = "coolToggle"))
@Ref(cls = DependencySubCategory.class, value = "coolToggle")
)
public boolean dependsOnCoolToggleOutside = false;

Expand Down

0 comments on commit 6019831

Please sign in to comment.