From 800c45033be33f27d05b72cc95efc7bce7ef9ad9 Mon Sep 17 00:00:00 2001 From: austin Date: Mon, 5 May 2025 17:53:33 -0400 Subject: [PATCH] proof of concept of loading html to define ui --- DEPENDENCIES.md | 3 +- assets/Data/menu/npcintro.html | 1 + docs/src/progress/renderertodo.md | 2 + pom.xml | 9 ++++ .../ui/menu/debug/ImGuiUIFramework.java | 5 ++ .../ui/menu/dialog/DialogMenuGenerator.java | 41 +++++++++++++++++ .../client/ui/parsing/HtmlParser.java | 46 +++++++++++++++++++ 7 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 assets/Data/menu/npcintro.html create mode 100644 src/main/java/electrosphere/client/ui/menu/dialog/DialogMenuGenerator.java create mode 100644 src/main/java/electrosphere/client/ui/parsing/HtmlParser.java diff --git a/DEPENDENCIES.md b/DEPENDENCIES.md index b0bca28f..7552d0f7 100644 --- a/DEPENDENCIES.md +++ b/DEPENDENCIES.md @@ -20,4 +20,5 @@ - [yoga](https://github.com/facebook/yoga) --- [MIT](https://github.com/facebook/yoga/blob/main/LICENSE) - [MathUtils](https://github.com/StudioRailgun/MathUtils) --- [MIT](https://github.com/StudioRailgun/MathUtils/blob/main/LICENSE) - [DataStructures](https://github.com/StudioRailgun/DataStructures) --- [MIT](https://github.com/StudioRailgun/DataStructures/blob/main/LICENSE) - - [Typescript](https://github.com/microsoft/TypeScript) --- [Apache 2](https://github.com/microsoft/TypeScript/blob/main/LICENSE.txt) \ No newline at end of file + - [Typescript](https://github.com/microsoft/TypeScript) --- [Apache 2](https://github.com/microsoft/TypeScript/blob/main/LICENSE.txt) + - [JSoup](https://jsoup.org/) --- [MIT](https://github.com/jhy/jsoup/blob/master/LICENSE) \ No newline at end of file diff --git a/assets/Data/menu/npcintro.html b/assets/Data/menu/npcintro.html new file mode 100644 index 00000000..e5342827 --- /dev/null +++ b/assets/Data/menu/npcintro.html @@ -0,0 +1 @@ +

Hello!

\ No newline at end of file diff --git a/docs/src/progress/renderertodo.md b/docs/src/progress/renderertodo.md index 1344e345..8a42833b 100644 --- a/docs/src/progress/renderertodo.md +++ b/docs/src/progress/renderertodo.md @@ -1680,6 +1680,8 @@ Block pathing work (05/05/2025) Scaffolding for structure scanning service +Add JSoup dependency +Proof of concept of loading html to define ui diff --git a/pom.xml b/pom.xml index db1fa2fa..edfe2fdf 100644 --- a/pom.xml +++ b/pom.xml @@ -296,6 +296,14 @@ ${lwjgl.natives} + + + + org.jsoup + jsoup + 1.20.1 + + @@ -313,6 +321,7 @@ + diff --git a/src/main/java/electrosphere/client/ui/menu/debug/ImGuiUIFramework.java b/src/main/java/electrosphere/client/ui/menu/debug/ImGuiUIFramework.java index da262cd4..1b5c9452 100644 --- a/src/main/java/electrosphere/client/ui/menu/debug/ImGuiUIFramework.java +++ b/src/main/java/electrosphere/client/ui/menu/debug/ImGuiUIFramework.java @@ -1,5 +1,6 @@ package electrosphere.client.ui.menu.debug; +import electrosphere.client.ui.menu.dialog.DialogMenuGenerator; import electrosphere.engine.Globals; import electrosphere.logger.LoggerInterface; import electrosphere.renderer.debug.DebugRendering; @@ -44,6 +45,10 @@ public class ImGuiUIFramework { printUITrees(); } + if(ImGui.button("Test load dialog")){ + DialogMenuGenerator.displayDialog("Data/menu/npcintro.html"); + } + } }); uiFrameworkWindow.setOpen(false); diff --git a/src/main/java/electrosphere/client/ui/menu/dialog/DialogMenuGenerator.java b/src/main/java/electrosphere/client/ui/menu/dialog/DialogMenuGenerator.java new file mode 100644 index 00000000..aa7b67d7 --- /dev/null +++ b/src/main/java/electrosphere/client/ui/menu/dialog/DialogMenuGenerator.java @@ -0,0 +1,41 @@ +package electrosphere.client.ui.menu.dialog; + +import java.io.IOException; + +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Node; + +import electrosphere.client.ui.parsing.HtmlParser; +import electrosphere.renderer.ui.elements.Div; +import electrosphere.renderer.ui.elementtypes.Element; +import electrosphere.util.FileUtils; + +/** + * Generates dialog menus + */ +public class DialogMenuGenerator { + + /** + * Displays a dialog menu + * @param path The path to the html + * @return The element that is the root for the window + */ + public static Element displayDialog(String path){ + Div container = Div.createCol(); + try { + String content = FileUtils.getAssetFileAsString(path); + Document doc = Jsoup.parseBodyFragment(content); + Node bodyNode = doc.getElementsByTag("body").first(); + for(Node node : bodyNode.childNodes()){ + container.addChild( + HtmlParser.recursivelyParseChildren(node)); + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return container; + } + +} diff --git a/src/main/java/electrosphere/client/ui/parsing/HtmlParser.java b/src/main/java/electrosphere/client/ui/parsing/HtmlParser.java new file mode 100644 index 00000000..b0940a55 --- /dev/null +++ b/src/main/java/electrosphere/client/ui/parsing/HtmlParser.java @@ -0,0 +1,46 @@ +package electrosphere.client.ui.parsing; + +import org.jsoup.nodes.Node; + +import electrosphere.renderer.ui.elements.Div; +import electrosphere.renderer.ui.elements.Label; +import electrosphere.renderer.ui.elementtypes.Element; + +/** + * Converts HTML to engine elements + */ +public class HtmlParser { + + /** + * Recursively parses a jsoup node into engine ui elements + * @param jsoupNode The jsoup node + * @return The engine ui elements + */ + public static Element recursivelyParseChildren(Node jsoupNode){ + String tag = jsoupNode.nodeName(); + Element rVal = null; + switch(tag){ + case "p": { + rVal = Div.createDiv(); + for(Node child : jsoupNode.childNodes()){ + ((Div)rVal).addChild(HtmlParser.recursivelyParseChildren(child)); + } + } break; + case "#text": { + String content = jsoupNode.outerHtml(); + rVal = Label.createLabel(content); + } break; + case "div": { + rVal = Div.createDiv(); + for(Node child : jsoupNode.childNodes()){ + ((Div)rVal).addChild(HtmlParser.recursivelyParseChildren(child)); + } + } break; + default: { + throw new Error("Unsupported element type " + tag); + } + } + return Div.createDiv(); + } + +}