Skip to content

Commit

Permalink
Add some webpage helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugh Sanderson committed Sep 20, 2024
1 parent d5c2675 commit 126e6ef
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 32 deletions.
34 changes: 34 additions & 0 deletions project/src/common/ExternalInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 -----------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions project/src/opengl/OpenGLContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) );
Expand Down
60 changes: 41 additions & 19 deletions samples/AcadnmeBoot/src/AcadnmeBoot.hx
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,34 @@ class AcadnmeBoot extends Screen implements IBoot
var storeData:Dynamic;
var button:Button;
var nmeVersion:String;
var isWeb:Bool;

public function new()
{
super();

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);
Expand Down Expand Up @@ -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();

Expand All @@ -103,6 +114,11 @@ class AcadnmeBoot extends Screen implements IBoot

build();
makeCurrent();

if (isWeb)
{
var q = nme.Lib.getWebpageParam("prog");
}
}

function onEnable(inValue:Bool)
Expand All @@ -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(_)
Expand Down
12 changes: 12 additions & 0 deletions src/nme/Lib.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
24 changes: 11 additions & 13 deletions tools/nme/src/platforms/EmscriptenPlatform.hx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class EmscriptenPlatform extends DesktopPlatform

override public function run(arguments:Array<String>):Void
{
if (false)
if (project.hasDef("pythonServe"))
{
runPython(arguments);
}
Expand Down Expand Up @@ -126,32 +126,31 @@ class EmscriptenPlatform extends DesktopPlatform

public function runPython(arguments:Array<String>):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("");
Expand All @@ -173,7 +172,6 @@ class EmscriptenPlatform extends DesktopPlatform
}
}
}

}

}
Expand Down

0 comments on commit 126e6ef

Please sign in to comment.