Skip to content

Commit 4bdaa76

Browse files
committed
check overlay array length and javadocs
1 parent cf5972f commit 4bdaa76

File tree

10 files changed

+87
-50
lines changed

10 files changed

+87
-50
lines changed

src/main/java/gregtech/api/mui/widget/EnumButtonRow.java

Lines changed: 73 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {

src/main/java/gregtech/common/covers/CoverConveyor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ protected Flow createUI(GuiData data, PanelSyncManager guiSyncManager) {
554554
// noinspection DuplicatedCode
555555
column.child(EnumButtonRow.builder(manualIOModeSync)
556556
.rowDescription(IKey.lang("cover.generic.manual_io"))
557-
.overlay(val -> {
557+
.overlays(val -> {
558558
int textureIndex = val.ordinal();
559559
return new DynamicDrawable(() -> {
560560
if (conveyorModeSync.getValue().isImport()) {
@@ -572,7 +572,7 @@ protected Flow createUI(GuiData data, PanelSyncManager guiSyncManager) {
572572
if (createConveyorModeRow()) {
573573
column.child(EnumButtonRow.builder(conveyorModeSync)
574574
.rowDescription(IKey.lang("cover.generic.io"))
575-
.overlay(GTGuiTextures.CONVEYOR_MODE_OVERLAY)
575+
.overlays(GTGuiTextures.CONVEYOR_MODE_OVERLAY)
576576
.widgetExtras((ioMode, toggleButton) -> ioMode.handleTooltip(toggleButton, "conveyor"))
577577
.build());
578578
}
@@ -584,7 +584,7 @@ protected Flow createUI(GuiData data, PanelSyncManager guiSyncManager) {
584584

585585
column.child(EnumButtonRow.builder(distributionModeSync)
586586
.rowDescription(IKey.lang("cover.conveyor.distribution.name"))
587-
.overlay(16, GTGuiTextures.DISTRIBUTION_MODE_OVERLAY)
587+
.overlays(16, GTGuiTextures.DISTRIBUTION_MODE_OVERLAY)
588588
.widgetExtras(ITranslatable::handleTooltip)
589589
.build());
590590
}

src/main/java/gregtech/common/covers/CoverFluidFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public ModularPanel buildUI(SidedPosGuiData guiData, PanelSyncManager guiSyncMan
157157
.child(Flow.column().widthRel(1f).align(Alignment.TopLeft).top(22).coverChildrenHeight()
158158
.child(EnumButtonRow.builder(filteringMode)
159159
.rowDescription(IKey.lang("cover.filter.mode.title"))
160-
.overlay(16, GTGuiTextures.FILTER_MODE_OVERLAY)
160+
.overlays(16, GTGuiTextures.FILTER_MODE_OVERLAY)
161161
.widgetExtras(ITranslatable::handleTooltip)
162162
.build())
163163
.child(Flow.row()

src/main/java/gregtech/common/covers/CoverFluidRegulator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,13 @@ protected Flow createUI(GuiData data, PanelSyncManager syncManager) {
263263
return super.createUI(data, syncManager)
264264
.child(EnumButtonRow.builder(transferModeSync)
265265
.rowDescription(IKey.lang("cover.generic.transfer_mode"))
266-
.overlay(GTGuiTextures.FLUID_TRANSFER_MODE_OVERLAY)
266+
.overlays(GTGuiTextures.FLUID_TRANSFER_MODE_OVERLAY)
267267
.widgetExtras(
268268
(transferMode, toggleButton) -> transferMode.handleTooltip(toggleButton,
269269
"fluid_regulator"))
270270
.build())
271271
.child(EnumButtonRow.builder(bucketModeSync)
272-
.overlay(IKey.str("kL"), IKey.str("L"))
272+
.overlays(IKey.str("kL"), IKey.str("L"))
273273
.widgetExtras(ITranslatable::handleTooltip)
274274
.build()
275275
.child(new TextFieldWidget().widthRel(0.5f).right(0)

src/main/java/gregtech/common/covers/CoverFluidVoidingAdvanced.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,10 @@ protected Flow createUI(GuiData data, PanelSyncManager syncManager) {
124124
return super.createUI(data, syncManager)
125125
.child(EnumButtonRow.builder(voidingMode)
126126
.rowDescription(IKey.lang("cover.voiding.voiding_mode"))
127-
.overlay(16, GTGuiTextures.VOIDING_MODE_OVERLAY)
127+
.overlays(16, GTGuiTextures.VOIDING_MODE_OVERLAY)
128128
.build())
129129
.child(EnumButtonRow.builder(bucketMode)
130-
.overlay(IKey.str("kL"), IKey.str("L"))
130+
.overlays(IKey.str("kL"), IKey.str("L"))
131131
.build()
132132
.child(transferTextField
133133
.setEnabledIf(w -> this.fluidFilterContainer.showGlobalTransferLimitSlider() &&

src/main/java/gregtech/common/covers/CoverItemFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public ModularPanel buildUI(SidedPosGuiData guiData, PanelSyncManager guiSyncMan
158158
.child(Flow.column().widthRel(1f).align(Alignment.TopLeft).top(22).coverChildrenHeight()
159159
.child(EnumButtonRow.builder(filteringMode)
160160
.rowDescription(IKey.lang("cover.filter.mode.title"))
161-
.overlay(16, GTGuiTextures.FILTER_MODE_OVERLAY)
161+
.overlays(16, GTGuiTextures.FILTER_MODE_OVERLAY)
162162
.widgetExtras(ITranslatable::handleTooltip)
163163
.build())
164164
.child(Flow.row()

src/main/java/gregtech/common/covers/CoverItemVoidingAdvanced.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ protected Flow createUI(GuiData data, PanelSyncManager guiSyncManager) {
111111
return super.createUI(data, guiSyncManager)
112112
.child(EnumButtonRow.builder(voidingMode)
113113
.rowDescription(IKey.lang("cover.voiding.voiding_mode"))
114-
.overlay(16, GTGuiTextures.VOIDING_MODE_OVERLAY)
114+
.overlays(16, GTGuiTextures.VOIDING_MODE_OVERLAY)
115115
.build())
116116
.child(Flow.row()
117117
.right(0)

src/main/java/gregtech/common/covers/CoverPump.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ protected Flow createUI(GuiData data, PanelSyncManager syncManager) {
240240
// noinspection DuplicatedCode
241241
column.child(EnumButtonRow.builder(manualIOModeSync)
242242
.rowDescription(IKey.lang("cover.generic.manual_io"))
243-
.overlay(val -> {
243+
.overlays(val -> {
244244
int textureIndex = val.ordinal();
245245
return new DynamicDrawable(() -> {
246246
if (pumpModeSync.getValue().isImport()) {
@@ -258,7 +258,7 @@ protected Flow createUI(GuiData data, PanelSyncManager syncManager) {
258258
if (createPumpModeRow()) {
259259
column.child(EnumButtonRow.builder(pumpModeSync)
260260
.rowDescription(IKey.lang("cover.pump.mode"))
261-
.overlay(GTGuiTextures.CONVEYOR_MODE_OVERLAY) // todo pump mode overlays
261+
.overlays(GTGuiTextures.CONVEYOR_MODE_OVERLAY) // todo pump mode overlays
262262
.widgetExtras((ioMode, toggleButton) -> ioMode.handleTooltip(toggleButton, "pump"))
263263
.build());
264264
}

src/main/java/gregtech/common/covers/CoverRoboticArm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ protected Flow createUI(GuiData data, PanelSyncManager guiSyncManager) {
212212
return super.createUI(data, guiSyncManager)
213213
.child(EnumButtonRow.builder(transferModeSync)
214214
.rowDescription(IKey.lang("cover.generic.transfer_mode"))
215-
.overlay(GTGuiTextures.TRANSFER_MODE_OVERLAY)
215+
.overlays(GTGuiTextures.TRANSFER_MODE_OVERLAY)
216216
.widgetExtras(
217217
(transferMode, toggleButton) -> transferMode.handleTooltip(toggleButton, "robotic_arm"))
218218
.build())

src/main/java/gregtech/common/covers/ender/CoverEnderFluidLink.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ protected Flow createWidgets(GuiData data, PanelSyncManager syncManager) {
154154
return super.createWidgets(data, syncManager)
155155
.child(getFluidFilterContainer().initUI(data, syncManager))
156156
.child(EnumButtonRow.builder(pumpMode)
157-
.overlay(GTGuiTextures.CONVEYOR_MODE_OVERLAY)
157+
.overlays(GTGuiTextures.CONVEYOR_MODE_OVERLAY)
158158
.rowDescription(IKey.lang("cover.pump.mode"))
159159
.build());
160160
}

0 commit comments

Comments
 (0)