Skip to content

Commit 51a6f51

Browse files
committed
rewrite TestGuis to be able to select any test
1 parent 512c744 commit 51a6f51

1 file changed

Lines changed: 47 additions & 4 deletions

File tree

src/main/java/com/cleanroommc/modularui/test/TestGuis.java

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
import com.cleanroommc.modularui.drawable.ItemDrawable;
1515
import com.cleanroommc.modularui.drawable.Rectangle;
1616
import com.cleanroommc.modularui.drawable.SpriteDrawable;
17+
import com.cleanroommc.modularui.factory.ClientGUI;
1718
import com.cleanroommc.modularui.screen.CustomModularScreen;
1819
import com.cleanroommc.modularui.screen.ModularPanel;
20+
import com.cleanroommc.modularui.screen.ModularScreen;
1921
import com.cleanroommc.modularui.screen.RichTooltip;
2022
import com.cleanroommc.modularui.screen.viewport.GuiContext;
2123
import com.cleanroommc.modularui.screen.viewport.ModularGuiContext;
@@ -37,7 +39,6 @@
3739
import com.cleanroommc.modularui.widget.Widget;
3840
import com.cleanroommc.modularui.widgets.ButtonWidget;
3941
import com.cleanroommc.modularui.widgets.ColorPickerDialog;
40-
import com.cleanroommc.modularui.widgets.CycleButtonWidget;
4142
import com.cleanroommc.modularui.widgets.ListWidget;
4243
import com.cleanroommc.modularui.widgets.RichTextWidget;
4344
import com.cleanroommc.modularui.widgets.SchemaWidget;
@@ -65,8 +66,12 @@
6566
import org.apache.commons.lang3.tuple.Pair;
6667
import org.jetbrains.annotations.NotNull;
6768

69+
import java.lang.reflect.InvocationTargetException;
70+
import java.lang.reflect.Method;
71+
import java.lang.reflect.Modifier;
6872
import java.util.ArrayList;
6973
import java.util.Arrays;
74+
import java.util.Comparator;
7075
import java.util.List;
7176
import java.util.Random;
7277
import java.util.stream.IntStream;
@@ -75,7 +80,46 @@ public class TestGuis extends CustomModularScreen {
7580

7681
@Override
7782
public @NotNull ModularPanel buildUI(ModularGuiContext context) {
78-
return buildColorUI(context);
83+
84+
// collect all test from all build methods in this class via reflection
85+
List<Method> uiMethods = new ArrayList<>();
86+
for (Method method : TestGuis.class.getDeclaredMethods()) {
87+
if (!"buildUI".equals(method.getName()) && // exclude this method
88+
!Modifier.isStatic(method.getModifiers()) &&
89+
Modifier.isPublic(method.getModifiers()) &&
90+
ModularPanel.class.isAssignableFrom(method.getReturnType()) &&
91+
method.getParameterCount() == 1 &&
92+
method.getParameterTypes()[0] == ModularGuiContext.class) {
93+
uiMethods.add(method);
94+
}
95+
}
96+
uiMethods.sort(Comparator.comparing(Method::getName));
97+
98+
return new ModularPanel("client_tests").height(200).width(170)
99+
.padding(7)
100+
.child(Flow.column()
101+
.child(IKey.str("Client Test UIs").asWidget().margin(1))
102+
.child(new ListWidget<>().widthRel(1f).expanded()
103+
.children(uiMethods.size(), i -> {
104+
Method m = uiMethods.get(i);
105+
String name = m.getName();
106+
if (name.startsWith("build")) name = name.substring(5);
107+
if (name.endsWith("UI")) name = name.substring(0, name.length() - 2);
108+
name = name.replaceAll("([a-z])([A-Z])", "$1 $2");
109+
return new ButtonWidget<>()
110+
.height(16)
111+
.widthRel(1f)
112+
.margin(0, 1)
113+
.overlay(IKey.str(name))
114+
.onMousePressed(button -> {
115+
try {
116+
ClientGUI.open(new ModularScreen((ModularPanel) m.invoke(TestGuis.this, context)).openParentOnClose(true));
117+
} catch (IllegalAccessException | InvocationTargetException e) {
118+
ModularUI.LOGGER.throwing(e);
119+
}
120+
return true;
121+
});
122+
})));
79123
}
80124

81125
public @NotNull ModularPanel buildToggleGridListUI(ModularGuiContext context) {
@@ -323,7 +367,7 @@ public void draw(GuiContext context, int x, int y, int width, int height, Widget
323367
return panel;
324368
}
325369

326-
public ModularPanel buildListUi(ModularGuiContext context) {
370+
public ModularPanel buildListUI(ModularGuiContext context) {
327371
Random rnd = new Random();
328372
return ModularPanel.defaultPanel("list", 100, 150)
329373
.padding(7)
@@ -435,7 +479,6 @@ public ModularPanel buildListUi(ModularGuiContext context) {
435479
colorPicker1.openPanel();
436480
return true;
437481
}))
438-
.child(new CycleButtonWidget())
439482
.child(new ButtonWidget<>()
440483
.debugName("color picker button 2")
441484
.background(color2)

0 commit comments

Comments
 (0)