@@ -19,89 +19,123 @@ public class EnumButtonRow<T extends Enum<T>> {
1919
2020 @ NotNull
2121 private final IEnumValue <T > value ;
22- @ NotNull
23- private final Class <T > enumValue ;
22+ private final T [] enumValues ;
2423 private int margin = 2 ;
2524 @ Nullable
2625 private IKey rowDescription ;
2726 @ Nullable
28- private Function <@ NotNull T , @ Nullable IDrawable > background ;
27+ private Function <@ NotNull T , @ Nullable IDrawable > backgrounds ;
2928 @ Nullable
30- private Function <@ NotNull T , @ Nullable IDrawable > selectedBackground ;
29+ private Function <@ NotNull T , @ Nullable IDrawable > selectedBackgrounds ;
3130 @ Nullable
32- private Function <@ NotNull T , @ NotNull IDrawable > overlay ;
31+ private Function <@ NotNull T , @ Nullable IDrawable > overlays ;
3332 @ Nullable
34- private BiConsumer <T , ToggleButton > widgetExtras ;
33+ private BiConsumer <@ NotNull T , @ NotNull ToggleButton > widgetExtras ;
3534
3635 public static <T extends Enum <T >> EnumButtonRow <T > builder (@ NotNull IEnumValue <T > value ) {
3736 return new EnumButtonRow <>(value );
3837 }
3938
40- private EnumButtonRow (IEnumValue <T > value ) {
39+ private EnumButtonRow (@ NotNull IEnumValue <T > value ) {
4140 this .value = value ;
42- this .enumValue = value .getEnumClass ();
41+ this .enumValues = value .getEnumClass (). getEnumConstants ();
4342 }
4443
4544 /**
4645 * Set the margin applied to the right side of each button. <br/>
4746 * The default is {@code 2}.
4847 */
49- public EnumButtonRow <T > buttonMargin (int margin ) {
48+ public EnumButtonRow <T > buttonMargins (int margin ) {
5049 this .margin = margin ;
5150 return this ;
5251 }
5352
5453 /**
5554 * Add an {@link IKey} to the row that will be right aligned at the end.
5655 */
57- public EnumButtonRow <T > rowDescription (IKey lang ) {
56+ public EnumButtonRow <T > rowDescription (@ NotNull IKey lang ) {
5857 this .rowDescription = lang ;
5958 return this ;
6059 }
6160
6261 /**
63- * Add a background to each {@link ToggleButton} when the button is not selected.
62+ * Add a background to each {@link ToggleButton} when the button is not selected. <br/>
63+ * Return {@code null} from the function to skip setting a background on the button associated with the enum value.
6464 */
65- public EnumButtonRow <T > background (Function <T , IDrawable > background ) {
66- this .background = background ;
65+ public EnumButtonRow <T > backgrounds (Function <@ NotNull T , @ Nullable IDrawable > background ) {
66+ this .backgrounds = background ;
6767 return this ;
6868 }
6969
7070 /**
71- * Add a background to each {@link ToggleButton} when the button is selected.
71+ * Add a background to each {@link ToggleButton} when the button is selected. <br/>
72+ * Return {@code null} from the function to skip setting a selected background on the button associated with the
73+ * enum value.
7274 */
73- public EnumButtonRow <T > selectedBackground (Function <T , IDrawable > selectedBackground ) {
74- this .selectedBackground = selectedBackground ;
75+ public EnumButtonRow <T > selectedBackgrounds (Function <@ NotNull T , @ Nullable IDrawable > selectedBackground ) {
76+ this .selectedBackgrounds = selectedBackground ;
7577 return this ;
7678 }
7779
7880 /**
79- * Add an overlay to each {@link ToggleButton}.
81+ * Add an overlay to each {@link ToggleButton}. <br/>
82+ * Return {@code null} from the function to skip setting an overlay on the button associated with the enum value.
8083 */
81- public EnumButtonRow <T > overlay (Function <T , IDrawable > overlay ) {
82- this .overlay = overlay ;
84+ public EnumButtonRow <T > overlays (Function <@ NotNull T , @ Nullable IDrawable > overlay ) {
85+ this .overlays = overlay ;
8386 return this ;
8487 }
8588
8689 /**
87- * Add an overlay to each {@link ToggleButton}.
90+ * Add an overlay to each {@link ToggleButton}. <br/>
91+ * The array can either have a length of 1 to apply the same overlay to every button, or it must have the same
92+ * number of elements as the enum does. <br/>
93+ * Use {@link #overlays(Function)} if you need more granular control over each button's overlay.
94+ *
95+ * @throws IllegalArgumentException if the two array length conditions aren't met
8896 */
89- public EnumButtonRow <T > overlay (IDrawable ... overlay ) {
90- this .overlay = val -> overlay [val .ordinal ()];
91- return this ;
97+ public EnumButtonRow <T > overlays (@ NotNull IDrawable @ NotNull ... overlay ) {
98+ int len = overlay .length ;
99+ if (len == 1 ) {
100+ return overlays ($ -> overlay [0 ]);
101+ } else if (len != enumValues .length ) {
102+ throw new IllegalArgumentException (
103+ "Number of elements in the overlay array must be 1 or the same as the enum!" );
104+ }
105+
106+ return overlays (val -> overlay [val .ordinal ()]);
92107 }
93108
94109 /**
95- * Add an overlay to each {@link ToggleButton}.
110+ * Add an overlay with a certain size to each {@link ToggleButton}. <br/>
111+ * The array can either have a length of 1 to apply the same overlay to every button, or it must have the same
112+ * number of elements as the enum does. <br/>
113+ * Use {@link #overlays(Function)} if you need more granular control over each button's overlay.
114+ *
115+ * @throws IllegalArgumentException if the two array length conditions aren't met
116+ *
96117 */
97- public EnumButtonRow <T > overlay (int size , IDrawable ... overlay ) {
98- this .overlay = val -> overlay [val .ordinal ()]
118+ public EnumButtonRow <T > overlays (int size , @ NotNull IDrawable @ NotNull ... overlay ) {
119+ int len = overlay .length ;
120+ if (len == 1 ) {
121+ IDrawable singleOverlay = overlay [0 ]
122+ .asIcon ()
123+ .size (size );
124+ return overlays ($ -> singleOverlay );
125+ } else if (len != enumValues .length ) {
126+ throw new IllegalArgumentException (
127+ "Number of elements in the overlay array must be 1 or the same as the enum!" );
128+ }
129+
130+ return overlays (val -> overlay [val .ordinal ()]
99131 .asIcon ()
100- .size (size );
101- return this ;
132+ .size (size ));
102133 }
103134
104- public EnumButtonRow <T > widgetExtras (BiConsumer <T , ToggleButton > widgetExtras ) {
135+ /**
136+ * Configure each toggle button directly.
137+ */
138+ public EnumButtonRow <T > widgetExtras (@ NotNull BiConsumer <@ NotNull T , @ NotNull ToggleButton > widgetExtras ) {
105139 this .widgetExtras = widgetExtras ;
106140 return this ;
107141 }
@@ -112,32 +146,35 @@ public Flow build() {
112146 .widthRel (1f )
113147 .coverChildrenHeight ();
114148
115- for (T enumVal : enumValue . getEnumConstants () ) {
149+ for (T enumVal : enumValues ) {
116150 ToggleButton button = new ToggleButton ()
117151 .marginRight (margin )
118152 .size (18 )
119153 .value (ValueHelper .boolValueOf (value , enumVal ));
120154
121155 IDrawable background = GTGuiTextures .MC_BUTTON ;
122- if (this .background != null ) {
123- IDrawable backgroundReplacement = this .background .apply (enumVal );
156+ if (this .backgrounds != null ) {
157+ IDrawable backgroundReplacement = this .backgrounds .apply (enumVal );
124158 if (backgroundReplacement != null ) {
125159 background = backgroundReplacement ;
126160 }
127161 }
128162 button .background (background );
129163
130164 IDrawable selectedBackground = GTGuiTextures .MC_BUTTON_DISABLED ;
131- if (this .selectedBackground != null ) {
132- IDrawable selectedBackgroundReplacement = this .selectedBackground .apply (enumVal );
165+ if (this .selectedBackgrounds != null ) {
166+ IDrawable selectedBackgroundReplacement = this .selectedBackgrounds .apply (enumVal );
133167 if (selectedBackgroundReplacement != null ) {
134168 selectedBackground = selectedBackgroundReplacement ;
135169 }
136170 }
137171 button .selectedBackground (selectedBackground );
138172
139- if (overlay != null ) {
140- button .overlay (overlay .apply (enumVal ));
173+ if (overlays != null ) {
174+ IDrawable overlay = this .overlays .apply (enumVal );
175+ if (overlay != null ) {
176+ button .overlay (overlay );
177+ }
141178 }
142179
143180 if (this .widgetExtras != null ) {
0 commit comments