Skip to content

Commit cf5972f

Browse files
committed
Refactor EnumRowBuilder again
1 parent 5c0f6b8 commit cf5972f

13 files changed

+250
-207
lines changed
Lines changed: 0 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package gregtech.api.cover;
22

3-
import gregtech.api.mui.GTGuiTextures;
43
import gregtech.api.mui.GTGuiTheme;
54
import gregtech.api.mui.GregTechGuiScreen;
65
import gregtech.api.mui.factory.CoverGuiFactory;
@@ -12,30 +11,22 @@
1211
import net.minecraftforge.fml.relauncher.SideOnly;
1312

1413
import com.cleanroommc.modularui.api.IGuiHolder;
15-
import com.cleanroommc.modularui.api.drawable.IDrawable;
1614
import com.cleanroommc.modularui.api.drawable.IKey;
1715
import com.cleanroommc.modularui.drawable.DynamicDrawable;
1816
import com.cleanroommc.modularui.drawable.ItemDrawable;
1917
import com.cleanroommc.modularui.factory.SidedPosGuiData;
2018
import com.cleanroommc.modularui.screen.ModularPanel;
2119
import com.cleanroommc.modularui.screen.ModularScreen;
2220
import com.cleanroommc.modularui.screen.UISettings;
23-
import com.cleanroommc.modularui.utils.Alignment;
2421
import com.cleanroommc.modularui.utils.Color;
2522
import com.cleanroommc.modularui.utils.MouseData;
26-
import com.cleanroommc.modularui.value.BoolValue;
27-
import com.cleanroommc.modularui.value.sync.EnumSyncValue;
28-
import com.cleanroommc.modularui.value.sync.IntSyncValue;
2923
import com.cleanroommc.modularui.value.sync.PanelSyncManager;
3024
import com.cleanroommc.modularui.widget.ParentWidget;
3125
import com.cleanroommc.modularui.widget.Widget;
32-
import com.cleanroommc.modularui.widgets.ToggleButton;
3326
import com.cleanroommc.modularui.widgets.layout.Flow;
3427
import org.jetbrains.annotations.ApiStatus;
3528
import org.jetbrains.annotations.NotNull;
36-
import org.jetbrains.annotations.Nullable;
3729

38-
import java.util.function.BiConsumer;
3930
import java.util.function.BooleanSupplier;
4031
import java.util.function.Supplier;
4132

@@ -174,157 +165,4 @@ default IKey createAdjustOverlay(boolean increment) {
174165
.color(Color.WHITE.main)
175166
.scale(scale);
176167
}
177-
178-
/**
179-
* Get a BoolValue for use with toggle buttons which are "linked together,"
180-
* meaning only one of them can be pressed at a time.
181-
*/
182-
default <T extends Enum<T>> BoolValue.Dynamic boolValueOf(EnumSyncValue<T> syncValue, T value) {
183-
return new BoolValue.Dynamic(() -> syncValue.getValue() == value, $ -> syncValue.setValue(value));
184-
}
185-
186-
/**
187-
* Get a BoolValue for use with toggle buttons which are "linked together,"
188-
* meaning only one of them can be pressed at a time.
189-
*/
190-
default BoolValue.Dynamic boolValueOf(IntSyncValue syncValue, int value) {
191-
return new BoolValue.Dynamic(() -> syncValue.getValue() == value, $ -> syncValue.setValue(value));
192-
}
193-
194-
class EnumRowBuilder<T extends Enum<T>> {
195-
196-
private EnumSyncValue<T> syncValue;
197-
private final Class<T> enumValue;
198-
@Nullable
199-
private IKey rowDescription;
200-
@Nullable
201-
private IDrawable[] background;
202-
@Nullable
203-
private IDrawable selectedBackground;
204-
@Nullable
205-
private IDrawable[] overlay;
206-
@Nullable
207-
private BiConsumer<T, ToggleButton> widgetExtras;
208-
209-
public EnumRowBuilder(Class<T> enumValue) {
210-
this.enumValue = enumValue;
211-
}
212-
213-
public EnumRowBuilder<T> value(EnumSyncValue<T> syncValue) {
214-
this.syncValue = syncValue;
215-
return this;
216-
}
217-
218-
/**
219-
* Add an {@link IKey} to the row that will be right aligned at the end.
220-
*/
221-
public EnumRowBuilder<T> rowDescription(IKey lang) {
222-
this.rowDescription = lang;
223-
return this;
224-
}
225-
226-
/**
227-
* Add a background to each {@link ToggleButton} when the button is not selected.
228-
*
229-
* @param background an array of {@link IDrawable}s in the same order as the {@link Enum} used to make this
230-
* {@link EnumRowBuilder}
231-
*/
232-
public EnumRowBuilder<T> background(IDrawable... background) {
233-
this.background = background;
234-
return this;
235-
}
236-
237-
/**
238-
* Add a background to each {@link ToggleButton} when the button is selected.
239-
*
240-
* @param selectedBackground an array of {@link IDrawable}s in the same order as the {@link Enum} used to make
241-
* this {@link EnumRowBuilder}
242-
*/
243-
public EnumRowBuilder<T> selectedBackground(IDrawable selectedBackground) {
244-
this.selectedBackground = selectedBackground;
245-
return this;
246-
}
247-
248-
/**
249-
* Add an overlay to each {@link ToggleButton}.
250-
*
251-
* @param overlay an array of {@link IDrawable}s in the same order as the {@link Enum} used to make this
252-
* {@link EnumRowBuilder}
253-
*/
254-
public EnumRowBuilder<T> overlay(IDrawable... overlay) {
255-
this.overlay = overlay;
256-
return this;
257-
}
258-
259-
/**
260-
* Add an overlay to each {@link ToggleButton} with a specific size.
261-
*
262-
* @param size the size to set the {@link IDrawable} to.
263-
* @param overlay an array of {@link IDrawable}s in the same order as the {@link Enum} used to make this
264-
* {@link EnumRowBuilder}
265-
*/
266-
public EnumRowBuilder<T> overlay(int size, IDrawable... overlay) {
267-
this.overlay = new IDrawable[overlay.length];
268-
for (int i = 0; i < overlay.length; i++) {
269-
this.overlay[i] = overlay[i].asIcon()
270-
.size(size);
271-
}
272-
return this;
273-
}
274-
275-
public EnumRowBuilder<T> widgetExtras(BiConsumer<T, ToggleButton> widgetExtras) {
276-
this.widgetExtras = widgetExtras;
277-
return this;
278-
}
279-
280-
private BoolValue.Dynamic boolValueOf(EnumSyncValue<T> syncValue, T value) {
281-
return new BoolValue.Dynamic(() -> syncValue.getValue() == value, $ -> syncValue.setValue(value));
282-
}
283-
284-
public Flow build() {
285-
Flow row = Flow.row()
286-
.marginBottom(2)
287-
.widthRel(1f)
288-
.coverChildrenHeight();
289-
290-
if (this.enumValue != null && this.syncValue != null) {
291-
for (T enumVal : enumValue.getEnumConstants()) {
292-
ToggleButton button = new ToggleButton()
293-
.marginRight(2)
294-
.size(18)
295-
.value(boolValueOf(this.syncValue, enumVal));
296-
297-
if (this.background != null && this.background.length > 0) {
298-
button.background(this.background);
299-
} else {
300-
button.background(GTGuiTextures.MC_BUTTON);
301-
}
302-
303-
if (this.selectedBackground != null) {
304-
button.selectedBackground(this.selectedBackground);
305-
} else {
306-
button.selectedBackground(GTGuiTextures.MC_BUTTON_DISABLED);
307-
}
308-
309-
if (this.overlay != null) {
310-
button.overlay(this.overlay[enumVal.ordinal()]);
311-
}
312-
313-
if (this.widgetExtras != null) {
314-
this.widgetExtras.accept(enumVal, button);
315-
}
316-
317-
row.child(button);
318-
}
319-
}
320-
321-
if (this.rowDescription != null && !this.rowDescription.get().isEmpty()) {
322-
row.child(this.rowDescription.asWidget()
323-
.align(Alignment.CenterRight)
324-
.height(18));
325-
}
326-
327-
return row;
328-
}
329-
}
330168
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package gregtech.api.mui.util;
2+
3+
import com.cleanroommc.modularui.api.value.IBoolValue;
4+
import com.cleanroommc.modularui.api.value.IByteValue;
5+
import com.cleanroommc.modularui.api.value.IDoubleValue;
6+
import com.cleanroommc.modularui.api.value.IEnumValue;
7+
import com.cleanroommc.modularui.api.value.IIntValue;
8+
import com.cleanroommc.modularui.api.value.ILongValue;
9+
import com.cleanroommc.modularui.api.value.IStringValue;
10+
import com.cleanroommc.modularui.api.value.IValue;
11+
import com.cleanroommc.modularui.value.BoolValue;
12+
13+
import java.util.Objects;
14+
15+
public final class ValueHelper {
16+
17+
public static BoolValue.Dynamic boolValueOf(IBoolValue<?> value, boolean target) {
18+
return new BoolValue.Dynamic(() -> value.getBoolValue() == target, $ -> value.setBoolValue(target));
19+
}
20+
21+
public static BoolValue.Dynamic boolValueOf(IByteValue<?> value, byte target) {
22+
return new BoolValue.Dynamic(() -> value.getByteValue() == target, $ -> value.setByteValue(target));
23+
}
24+
25+
public static BoolValue.Dynamic boolValueOf(IDoubleValue<?> value, double target) {
26+
return new BoolValue.Dynamic(() -> value.getDoubleValue() == target, $ -> value.setDoubleValue(target));
27+
}
28+
29+
public static <T extends Enum<T>> BoolValue.Dynamic boolValueOf(IEnumValue<T> value, T target) {
30+
return new BoolValue.Dynamic(() -> value.getValue() == target, $ -> value.setValue(target));
31+
}
32+
33+
public static BoolValue.Dynamic boolValueOf(IIntValue<?> value, int target) {
34+
return new BoolValue.Dynamic(() -> value.getIntValue() == target, $ -> value.setIntValue(target));
35+
}
36+
37+
public static BoolValue.Dynamic boolValueOf(ILongValue<?> value, long target) {
38+
return new BoolValue.Dynamic(() -> value.getLongValue() == target, $ -> value.setLongValue(target));
39+
}
40+
41+
public static BoolValue.Dynamic boolValueOf(IStringValue<?> value, String target) {
42+
return new BoolValue.Dynamic(() -> Objects.equals(value.getStringValue(), target),
43+
$ -> value.setStringValue(target));
44+
}
45+
46+
public static <T> BoolValue.Dynamic boolValueOf(IValue<T> value, T target) {
47+
return new BoolValue.Dynamic(() -> Objects.equals(value.getValue(), target), $ -> value.setValue(target));
48+
}
49+
}

0 commit comments

Comments
 (0)