From 126e6eff827db532bc18bb6c8b3983dc0ba78932 Mon Sep 17 00:00:00 2001 From: Hugh Sanderson Date: Fri, 20 Sep 2024 17:31:52 +0800 Subject: [PATCH] Add some webpage helpers --- project/src/common/ExternalInterface.cpp | 34 +++++++++++ project/src/opengl/OpenGLContext.cpp | 1 + samples/AcadnmeBoot/src/AcadnmeBoot.hx | 60 +++++++++++++------ src/nme/Lib.hx | 12 ++++ tools/nme/src/platforms/EmscriptenPlatform.hx | 24 ++++---- 5 files changed, 99 insertions(+), 32 deletions(-) diff --git a/project/src/common/ExternalInterface.cpp b/project/src/common/ExternalInterface.cpp index ad8d1afff..5b31d959c 100644 --- a/project/src/common/ExternalInterface.cpp +++ b/project/src/common/ExternalInterface.cpp @@ -815,6 +815,40 @@ void nme_set_renderer(HxString inRenderer) DEFINE_PRIME1v(nme_set_renderer); +value nme_get_webpage_url() +{ + #ifdef EMSCRIPTEN + const char* url = emscripten_run_script_string("window.location.href"); + return alloc_string(url); + #else + return alloc_null(); + #endif +} +DEFINE_PRIM(nme_get_webpage_url,0); + +value nme_get_webpage_param(value param) +{ + #ifdef EMSCRIPTEN + const char *q = val_string(param); + char *str = (char*)EM_ASM_PTR({ + const params = new URLSearchParams(window.location.search); + const val = params.get(UTF8ToString($0)); + if (val==null) return 0; + return stringToNewUTF8(val); + }, q); + if (!str) + return alloc_null(); + value result = alloc_string(str); + free(str); + return result; + #else + return alloc_null(); + #endif +} +DEFINE_PRIM(nme_get_webpage_param,1); + + + // --- ByteArray ----------------------------------------------------- diff --git a/project/src/opengl/OpenGLContext.cpp b/project/src/opengl/OpenGLContext.cpp index 4b623e953..25f64e25e 100644 --- a/project/src/opengl/OpenGLContext.cpp +++ b/project/src/opengl/OpenGLContext.cpp @@ -109,6 +109,7 @@ class OGLContext : public HardwareRenderer makeCurrent(); + #ifdef NME_GFX_DEBUG printf("Vendor: %s\n", (char *)glGetString(GL_VENDOR) ); printf("Renderer: %s\n", (char *)glGetString(GL_RENDERER) ); diff --git a/samples/AcadnmeBoot/src/AcadnmeBoot.hx b/samples/AcadnmeBoot/src/AcadnmeBoot.hx index a6bf7853f..ac940dafb 100644 --- a/samples/AcadnmeBoot/src/AcadnmeBoot.hx +++ b/samples/AcadnmeBoot/src/AcadnmeBoot.hx @@ -34,6 +34,7 @@ class AcadnmeBoot extends Screen implements IBoot var storeData:Dynamic; var button:Button; var nmeVersion:String; + var isWeb:Bool; public function new() { @@ -41,20 +42,26 @@ class AcadnmeBoot extends Screen implements IBoot Acadnme.boot = this; - store = SharedObject.getLocal("acadnme-server"); - storeData = store.data; + var url = nme.Lib.getWebpageUrl(); + isWeb = url!=null; - serverPassword = storeData.serverPassword ==null ? "" : storeData.serverPassword; - serverEnabled = storeData.serverEnabled==null ? true : storeData.serverEnabled; - Server.setEnabled(serverEnabled); - Server.setPassword(serverPassword); + if (!isWeb) + { + store = SharedObject.getLocal("acadnme-server"); + storeData = store.data; + + serverPassword = storeData.serverPassword ==null ? "" : storeData.serverPassword; + serverEnabled = storeData.serverEnabled==null ? true : storeData.serverEnabled; + Server.setEnabled(serverEnabled); + Server.setPassword(serverPassword); - Server.functions["launch"] = launch; - Server.functions["apps"] = apps; - Server.functions["uninstall"] = uninstall; - Server.functions["reload"] = reloadSync; - defaultDir = getDefaultDir(); + Server.functions["launch"] = launch; + Server.functions["apps"] = apps; + Server.functions["uninstall"] = uninstall; + Server.functions["reload"] = reloadSync; + defaultDir = getDefaultDir(); + } var skin = new gm2d.skin.Skin(false); var pad = skin.scale(2); @@ -89,7 +96,11 @@ class AcadnmeBoot extends Screen implements IBoot var hostBar = new Widget({ align:Layout.AlignCenterY }); hostBar.setItemLayout( new HorizontalLayout([1,0]).stretch() ); - hostBar.addWidget(new TextLabel("Host:" + getConnectionStatus(),{ textColor:accent, align:Layout.AlignCenterY|Layout.AlignStretch }) ); + if (url==null) + hostBar.addWidget(new TextLabel("Host:" + getConnectionStatus(),{ textColor:accent, align:Layout.AlignCenterY|Layout.AlignStretch }) ); + else + hostBar.addWidget(new TextLabel("",{ textColor:accent, align:Layout.AlignCenterY|Layout.AlignStretch }) ); + hostBar.addWidget( button = Button.BMPButton( createMenuIcon(), onMenu, { shape:ShapeNone } ) ); hostBar.build(); @@ -103,6 +114,11 @@ class AcadnmeBoot extends Screen implements IBoot build(); makeCurrent(); + + if (isWeb) + { + var q = nme.Lib.getWebpageParam("prog"); + } } function onEnable(inValue:Bool) @@ -123,14 +139,20 @@ class AcadnmeBoot extends Screen implements IBoot function onMenu() { - var menuItemm = new MenuItem("Options"); - menuItemm.add( new MenuItem("Settings..", onSettings ) ); - menuItemm.add( new MenuItem("Open..", onOpen ) ); - - var popup = new PopupMenu(menuItemm); - var pos = button.localToGlobal( new Point(0,0) ); - gm2d.Game.popup( popup, pos.x, pos.y); + if (isWeb) + { + onOpen(null); + } + else + { + var menuItemm = new MenuItem("Options"); + menuItemm.add( new MenuItem("Settings..", onSettings ) ); + menuItemm.add( new MenuItem("Open..", onOpen ) ); + var popup = new PopupMenu(menuItemm); + var pos = button.localToGlobal( new Point(0,0) ); + gm2d.Game.popup( popup, pos.x, pos.y); + } } function onOpen(_) diff --git a/src/nme/Lib.hx b/src/nme/Lib.hx index f22c3524e..12252fdb8 100644 --- a/src/nme/Lib.hx +++ b/src/nme/Lib.hx @@ -218,6 +218,16 @@ class Lib return result; } + public static function getWebpageUrl() : String + { + return nme_get_webpage_url(); + } + + public static function getWebpageParam(param:String) : String + { + return nme_get_webpage_param(param); + } + // haxe flash compat public static function getURL(url:URLRequest, ?target:String):Void @@ -289,6 +299,8 @@ class Lib private static var nme_log = PrimeLoader.load("nme_log", "sv"); private static var nme_crash = PrimeLoader.load("nme_crash","v"); private static var nme_set_renderer = PrimeLoader.load("nme_set_renderer","sv"); + private static var nme_get_webpage_url = Loader.load("nme_get_webpage_url",0); + private static var nme_get_webpage_param = Loader.load("nme_get_webpage_param",1); } #else diff --git a/tools/nme/src/platforms/EmscriptenPlatform.hx b/tools/nme/src/platforms/EmscriptenPlatform.hx index f715ae3f7..b011ea098 100644 --- a/tools/nme/src/platforms/EmscriptenPlatform.hx +++ b/tools/nme/src/platforms/EmscriptenPlatform.hx @@ -97,7 +97,7 @@ class EmscriptenPlatform extends DesktopPlatform override public function run(arguments:Array):Void { - if (false) + if (project.hasDef("pythonServe")) { runPython(arguments); } @@ -126,32 +126,31 @@ class EmscriptenPlatform extends DesktopPlatform public function runPython(arguments:Array):Void { - var command = sdkPath==null ? "emrun" : sdkPath + "/emrun"; + var command = sdkPath==null ? [ "-m", "http.server" ] : [sdkPath + "/upstream/emscripten/emrun.py"]; var source = "index.html"; //var source = ext == ".html" ? Path.withoutDirectory(executablePath) : "index.html"; var browser = CommandLineTools.browser; var browserOps = browser=="none" ? ["--no_browser"] : browser==null ? [] : ["--browser",browser]; + if (python!=null) - { PathHelper.addExePath( haxe.io.Path.directory(python) ); - ProcessHelper.runCommand(applicationDirectory, "python", [command].concat(browserOps).concat([source]).concat(arguments) ); - } - else - ProcessHelper.runCommand(applicationDirectory, command, [source].concat(browserOps).concat(arguments) ); + + ProcessHelper.runCommand(applicationDirectory, "python", command.concat(browserOps).concat([source]).concat(arguments) ); } public function setupSdk() { - var hasSdk = project.hasDef("EMSCRIPTEN_SDK"); + var hasSdk = project.hasDef("EMSDK"); if (hasSdk) - sdkPath = project.getDef("EMSCRIPTEN_SDK"); - var hasPython = project.hasDef("EMSCRIPTEN_PYTHON"); + sdkPath = project.getDef("EMSDK"); + + var hasPython = project.hasDef("EMSDK_PYTHON"); if (hasPython) - python = project.getDef("EMSCRIPTEN_PYTHON"); + python = project.getDef("EMSDK_PYTHON"); var home = CommandLineTools.home; var file = home + "/.emscripten"; - if (FileSystem.exists(file)) + if (!hasSdk && FileSystem.exists(file)) { var content = sys.io.File.getContent(file); content = content.split("\r").join(""); @@ -173,7 +172,6 @@ class EmscriptenPlatform extends DesktopPlatform } } } - } }