Skip to content

Commit 3def879

Browse files
brachy84miozune
authored andcommitted
Detach Mui from GuiScreenWrapper (CleanroomMC#64)
* detach GuiScreenWrapper * fix mouse input * slightly scuffed gui overlays * fix text widget for unusual scales * lots of small stuff * setup tests & own matrix and vector impl * dont use deprecated field * move overlay test to own class * test button overlapping * allow creating custom gui wrappers from UIFactory * helpers & javadoc for ui factories (cherry picked from commit 47cf883)
1 parent e45d8d0 commit 3def879

File tree

73 files changed

+3291
-672
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+3291
-672
lines changed

addon.gradle

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
test {
2+
useJUnitPlatform()
3+
testLogging {
4+
events "passed", "skipped", "failed"
5+
}
6+
}
7+
8+
SourceSet functionalTestSet = null
9+
10+
sourceSets {
11+
functionalTestSet = create("functionalTest") {
12+
java {
13+
srcDir("src/functionalTest/java")
14+
compileClasspath += sourceSets.patchedMc.output + sourceSets.main.output
15+
}
16+
}
17+
}
18+
19+
configurations { configs ->
20+
// Keep all dependencies from the main mod in the functional test mod
21+
named(functionalTestSet.compileClasspathConfigurationName).configure {it.extendsFrom(named("compileClasspath").get())}
22+
named(functionalTestSet.runtimeClasspathConfigurationName).configure {it.extendsFrom(named("runtimeClasspath").get())}
23+
named(functionalTestSet.annotationProcessorConfigurationName).configure {it.extendsFrom(named("annotationProcessor").get())}
24+
}
25+
26+
tasks.register(functionalTestSet.jarTaskName, Jar) {
27+
from(functionalTestSet.output)
28+
archiveClassifier.set("functionalTests")
29+
// we don't care about the version number here, keep it stable to avoid polluting the tmp directory
30+
archiveVersion.set("1.0")
31+
destinationDirectory.set(new File(buildDir, "tmp"))
32+
}
33+
tasks.named("assemble").configure {
34+
dependsOn(functionalTestSet.jarTaskName)
35+
}
36+
37+
// Run tests in the default runServer/runClient configurations
38+
tasks.named("runServer", JavaExec).configure {
39+
dependsOn(functionalTestSet.jarTaskName)
40+
classpath(configurations.named(functionalTestSet.runtimeClasspathConfigurationName), tasks.named(functionalTestSet.jarTaskName))
41+
}
42+
43+
tasks.named("runClient", JavaExec).configure {
44+
dependsOn(functionalTestSet.jarTaskName)
45+
classpath(configurations.named(functionalTestSet.runtimeClasspathConfigurationName), tasks.named(functionalTestSet.jarTaskName))
46+
}

dependencies.gradle

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,14 @@ dependencies {
4040
//runtimeOnlyNonPublishable("com.github.GTNewHorizons:NotEnoughItems:2.6.34-GTNH:dev")
4141
// For Thaumcraft runtime
4242
//runtimeOnlyNonPublishable("com.github.GTNewHorizons:Baubles:1.0.4:dev")
43+
44+
testImplementation(platform('org.junit:junit-bom:5.9.2'))
45+
testImplementation('org.junit.jupiter:junit-jupiter')
46+
testImplementation("org.mockito:mockito-core:3.+")
47+
48+
functionalTestImplementation(platform('org.junit:junit-bom:5.9.2'))
49+
functionalTestImplementation('org.junit.jupiter:junit-jupiter')
50+
functionalTestImplementation('org.junit.platform:junit-platform-engine')
51+
functionalTestImplementation('org.junit.platform:junit-platform-launcher')
52+
functionalTestImplementation('org.junit.platform:junit-platform-reporting')
4353
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.cleanroommc.modularui.test;
2+
3+
import java.io.File;
4+
import java.io.PrintWriter;
5+
import java.nio.file.FileSystems;
6+
import java.nio.file.Path;
7+
8+
import net.minecraft.server.MinecraftServer;
9+
import net.minecraft.util.ChatComponentText;
10+
11+
import org.apache.commons.io.output.CloseShieldOutputStream;
12+
import org.junit.platform.engine.discovery.DiscoverySelectors;
13+
import org.junit.platform.launcher.Launcher;
14+
import org.junit.platform.launcher.LauncherDiscoveryRequest;
15+
import org.junit.platform.launcher.LauncherSession;
16+
import org.junit.platform.launcher.TestPlan;
17+
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
18+
import org.junit.platform.launcher.core.LauncherFactory;
19+
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
20+
import org.junit.platform.launcher.listeners.TestExecutionSummary;
21+
import org.junit.platform.reporting.legacy.xml.LegacyXmlReportGeneratingListener;
22+
23+
import cpw.mods.fml.common.FMLCommonHandler;
24+
import cpw.mods.fml.common.Mod;
25+
import cpw.mods.fml.common.event.FMLServerStartedEvent;
26+
27+
@Mod(modid = "mui-tests", name = "MUI Dev Tests", version = "1.0", dependencies = "required-after:modularui2")
28+
public class MUITestMod {
29+
30+
@Mod.EventHandler
31+
public void onServerStarted(FMLServerStartedEvent event) {
32+
MinecraftServer.getServer()
33+
.addChatMessage(new ChatComponentText("Running MUI unit tests..."));
34+
runTests();
35+
MinecraftServer.getServer()
36+
.addChatMessage(new ChatComponentText("Running MUI unit tests finished"));
37+
}
38+
39+
private void runTests() {
40+
// https://junit.org/junit5/docs/current/user-guide/#launcher-api
41+
System.setProperty("junit.platform.reporting.open.xml.enabled", "false");
42+
final Path testsXmlOutDir = FileSystems.getDefault()
43+
.getPath("./junit-out/")
44+
.toAbsolutePath();
45+
final File testsXmlOutDirFile = testsXmlOutDir.toFile();
46+
testsXmlOutDirFile.mkdirs();
47+
{
48+
File[] fileList = testsXmlOutDirFile.listFiles();
49+
if (fileList != null) {
50+
for (File child : fileList) {
51+
if (child.isFile() && child.getName()
52+
.endsWith(".xml")) {
53+
child.delete();
54+
}
55+
}
56+
}
57+
}
58+
final LauncherDiscoveryRequest discovery = LauncherDiscoveryRequestBuilder.request()
59+
.selectors(DiscoverySelectors.selectPackage("com.cleanroommc.modularui.test"))
60+
.build();
61+
final SummaryGeneratingListener summaryGenerator = new SummaryGeneratingListener();
62+
final TestExecutionSummary summary;
63+
try (PrintWriter stderrWriter = new PrintWriter(new CloseShieldOutputStream(System.err), true)) {
64+
final LegacyXmlReportGeneratingListener xmlGenerator = new LegacyXmlReportGeneratingListener(
65+
testsXmlOutDir,
66+
stderrWriter);
67+
try (LauncherSession session = LauncherFactory.openSession()) {
68+
final Launcher launcher = session.getLauncher();
69+
final TestPlan plan = launcher.discover(discovery);
70+
launcher.registerTestExecutionListeners(summaryGenerator, xmlGenerator);
71+
launcher.execute(plan);
72+
}
73+
summary = summaryGenerator.getSummary();
74+
75+
summary.printFailuresTo(stderrWriter, 32);
76+
summary.printTo(stderrWriter);
77+
stderrWriter.flush();
78+
}
79+
// Throw an exception if running via `runServer`
80+
if (summary.getTotalFailureCount() > 0 && FMLCommonHandler.instance()
81+
.getSide()
82+
.isServer()) {
83+
throw new RuntimeException("Some of the unit tests failed to execute, check the log for details");
84+
}
85+
}
86+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.cleanroommc.modularui.test;
2+
3+
import com.cleanroommc.modularui.overlay.ScreenWrapper;
4+
import com.cleanroommc.modularui.screen.ModularPanel;
5+
import com.cleanroommc.modularui.screen.ModularScreen;
6+
import com.cleanroommc.modularui.screen.NEISettingsImpl;
7+
import com.cleanroommc.modularui.widget.sizer.Area;
8+
import com.cleanroommc.modularui.widgets.ButtonWidget;
9+
10+
import org.junit.jupiter.api.Test;
11+
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
14+
public class SizerTest {
15+
16+
static final int W = 800, H = 450;
17+
18+
@Test
19+
void test() {
20+
ModularPanel panel = panel().child(new ButtonWidget<>().center());
21+
testPanel(panel);
22+
assertArea(panel.getArea(), W / 2 - 176 / 2, H / 2 - 166 / 2, 176, 166);
23+
}
24+
25+
ModularPanel panel(int w, int h) {
26+
return ModularPanel.defaultPanel("main", w, h);
27+
}
28+
29+
ModularPanel panel() {
30+
return ModularPanel.defaultPanel("main");
31+
}
32+
33+
void assertArea(Area area, int x, int y, int w, int h) {
34+
Area.SHARED.set(x, y, w, h);
35+
assertEquals(Area.SHARED, area);
36+
}
37+
38+
ModularScreen testPanel(ModularPanel panel) {
39+
ModularScreen screen = new ModularScreen(panel);
40+
screen.getContext().setNEISettings(new NEISettingsImpl());
41+
ScreenWrapper wrapper = new ScreenWrapper(null, screen);
42+
screen.construct(wrapper);
43+
screen.onResize(W, H);
44+
return screen;
45+
}
46+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[
2+
{
3+
"modid":"MUI-tests",
4+
"name":"MUI Dev Tests",
5+
"description":"MUI Tests to run in the development environment",
6+
"version":"1.0",
7+
"mcversion":"1.7.10",
8+
"url":"https://github.com/GTNewHorizons/ModularUI2",
9+
"updateUrl":"",
10+
"authorList":[],
11+
"credits":"",
12+
"logoFile":"",
13+
"screenshots":[]
14+
}
15+
]

src/main/java/com/cleanroommc/modularui/ClientEventHandler.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
package com.cleanroommc.modularui;
22

3+
import com.cleanroommc.modularui.api.event.KeyboardInputEvent;
4+
import com.cleanroommc.modularui.api.event.MouseInputEvent;
35
import com.cleanroommc.modularui.drawable.Stencil;
46

7+
import com.cleanroommc.modularui.screen.GuiContainerWrapper;
8+
9+
import cpw.mods.fml.common.eventhandler.EventPriority;
510
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
611
import cpw.mods.fml.common.gameevent.TickEvent;
712
import cpw.mods.fml.relauncher.Side;
813
import cpw.mods.fml.relauncher.SideOnly;
914

15+
import net.minecraftforge.client.event.GuiScreenEvent;
16+
1017
import org.lwjgl.opengl.GL11;
1118

1219
@SideOnly(Side.CLIENT)
@@ -32,4 +39,26 @@ public void preDraw(TickEvent.RenderTickEvent event) {
3239
}
3340
Stencil.reset();
3441
}
42+
43+
@SubscribeEvent(priority = EventPriority.HIGHEST)
44+
public static void onGuiInput(MouseInputEvent.Pre event) {
45+
if (hasDraggable(event)) {
46+
// cancel interactions with other mods
47+
event.gui.handleMouseInput();
48+
event.setCanceled(true);
49+
}
50+
}
51+
52+
@SubscribeEvent(priority = EventPriority.HIGHEST)
53+
public static void onGuiInput(KeyboardInputEvent.Pre event) {
54+
if (hasDraggable(event)) {
55+
// cancel interactions with other mods
56+
event.gui.handleKeyboardInput();
57+
event.setCanceled(true);
58+
}
59+
}
60+
61+
private static boolean hasDraggable(GuiScreenEvent event) {
62+
return event.gui instanceof GuiContainerWrapper screenWrapper && screenWrapper.getScreen().getContext().hasDraggable();
63+
}
3564
}

src/main/java/com/cleanroommc/modularui/ClientProxy.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package com.cleanroommc.modularui;
22

3-
import codechicken.nei.guihook.GuiContainerManager;
43
import com.cleanroommc.modularui.drawable.DrawableSerialization;
5-
import com.cleanroommc.modularui.factory.GuiManager;
64
import com.cleanroommc.modularui.holoui.HoloScreenEntity;
75
import com.cleanroommc.modularui.holoui.ScreenEntityRender;
8-
import com.cleanroommc.modularui.integration.nei.ModularUIContainerObjectHandler;
96
import com.cleanroommc.modularui.mixins.early.forge.ForgeHooksClientMixin;
7+
import com.cleanroommc.modularui.overlay.OverlayManager;
8+
import com.cleanroommc.modularui.screen.ClientScreenHandler;
109
import com.cleanroommc.modularui.test.EventHandler;
10+
import com.cleanroommc.modularui.test.OverlayTest;
1111
import com.cleanroommc.modularui.theme.ThemeManager;
1212
import com.cleanroommc.modularui.theme.ThemeReloadCommand;
1313
import cpw.mods.fml.client.registry.RenderingRegistry;
@@ -24,9 +24,6 @@
2424
import net.minecraftforge.client.MinecraftForgeClient;
2525
import net.minecraftforge.common.MinecraftForge;
2626

27-
import static com.cleanroommc.modularui.ModularUI.MODID_NEI;
28-
import static com.cleanroommc.modularui.ModularUI.isNEILoaded;
29-
3027
@SideOnly(Side.CLIENT)
3128
@SuppressWarnings("unused")
3229
public class ClientProxy extends CommonProxy {
@@ -37,14 +34,13 @@ public class ClientProxy extends CommonProxy {
3734
void preInit(FMLPreInitializationEvent event) {
3835
super.preInit(event);
3936

40-
if (isNEILoaded) {
41-
registerNEIHandler();
42-
}
43-
4437
FMLCommonHandler.instance().bus().register(new ClientEventHandler());
38+
MinecraftForge.EVENT_BUS.register(new ClientScreenHandler());
39+
MinecraftForge.EVENT_BUS.register(new OverlayManager());
4540

4641
if (ModularUIConfig.enableTestGuis) {
4742
MinecraftForge.EVENT_BUS.register(new EventHandler());
43+
OverlayTest.init();
4844
}
4945

5046
DrawableSerialization.init();
@@ -67,9 +63,4 @@ void postInit(FMLPostInitializationEvent event) {
6763
public Timer getTimer60Fps() {
6864
return this.timer60Fps;
6965
}
70-
71-
@Optional.Method(modid = MODID_NEI)
72-
private void registerNEIHandler() {
73-
GuiContainerManager.addObjectHandler(new ModularUIContainerObjectHandler());
74-
}
7566
}

src/main/java/com/cleanroommc/modularui/CommonProxy.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.cleanroommc.modularui;
22

3-
import com.cleanroommc.modularui.factory.GuiManager;
4-
import com.cleanroommc.modularui.factory.ItemGuiFactory;
5-
import com.cleanroommc.modularui.factory.SidedTileEntityGuiFactory;
6-
import com.cleanroommc.modularui.factory.TileEntityGuiFactory;
3+
import com.cleanroommc.modularui.factory.*;
74
import com.cleanroommc.modularui.holoui.HoloScreenEntity;
85
import com.cleanroommc.modularui.network.NetworkHandler;
96
import com.cleanroommc.modularui.test.ItemEditorGui;
@@ -39,9 +36,7 @@ void preInit(FMLPreInitializationEvent event) {
3936

4037
NetworkHandler.init();
4138

42-
GuiManager.registerFactory(TileEntityGuiFactory.INSTANCE);
43-
GuiManager.registerFactory(SidedTileEntityGuiFactory.INSTANCE);
44-
GuiManager.registerFactory(ItemGuiFactory.INSTANCE);
39+
GuiFactories.init();
4540
}
4641

4742
void postInit(FMLPostInitializationEvent event) {

src/main/java/com/cleanroommc/modularui/ModularUI.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,6 @@ public void postInit(FMLPostInitializationEvent event) {
5252
proxy.postInit(event);
5353
}
5454

55-
@Mod.EventHandler
56-
public void onLoadComplete(FMLLoadCompleteEvent event) {
57-
}
58-
5955
@Mod.EventHandler
6056
public void onServerLoad(FMLServerStartingEvent event) {
6157
proxy.onServerLoad(event);

0 commit comments

Comments
 (0)