From 06dbecde668d9a576a9a1e95a9b698783e20bbb5 Mon Sep 17 00:00:00 2001 From: Joshua Granick Date: Tue, 28 Feb 2017 15:33:51 -0800 Subject: [PATCH] Rebuild how lime.embed works, fix multiple embeds, allow passing window config values (resolve #933) --- lime/app/Config.hx | 7 +- lime/system/System.hx | 166 ++++++++++++++++++++++++++++ templates/haxe/ApplicationMain.hx | 101 +++-------------- templates/haxe/ManifestResources.hx | 7 +- 4 files changed, 188 insertions(+), 93 deletions(-) diff --git a/lime/app/Config.hx b/lime/app/Config.hx index b051ed3e5..946c70bb4 100644 --- a/lime/app/Config.hx +++ b/lime/app/Config.hx @@ -3,8 +3,8 @@ package lime.app; typedef Config = { - #if (js && html5) - @:optional var assetsPrefix:String; + #if ((js && html5) && lime < "5.0.0") + @:deprecated @:optional var assetsPrefix:String; #end @:optional var build:String; @:optional var company:String; @@ -13,6 +13,7 @@ typedef Config = { @:optional var name:String; @:optional var orientation:String; @:optional var packageName:String; + @:optional var rootPath:String; @:optional var version:String; @:optional var windows:Array; @@ -36,7 +37,7 @@ typedef WindowConfig = { @:optional var hidden:Bool; @:optional var maximized:Bool; @:optional var minimized:Bool; - @:optional var parameters:String; + @:optional var parameters:Dynamic; @:optional var resizable:Bool; @:optional var stencilBuffer:Bool; @:optional var title:String; diff --git a/lime/system/System.hx b/lime/system/System.hx index 9e6277d61..afcbe097c 100644 --- a/lime/system/System.hx +++ b/lime/system/System.hx @@ -1,8 +1,10 @@ package lime.system; +import haxe.Constraints; import lime._backend.native.NativeCFFI; import lime.app.Application; +import lime.app.Config; import lime.math.Rectangle; #if flash @@ -12,6 +14,7 @@ import flash.Lib; #end #if (js && html5) +import js.html.Element; import js.Browser; #end @@ -39,9 +42,135 @@ class System { public static var numDisplays (get, null):Int; public static var userDirectory (get, null):String; + @:noCompletion private static var __applicationConfig:Map; + @:noCompletion private static var __applicationEntryPoint:Map; @:noCompletion private static var __directories = new Map (); + #if (js && html5) + @:keep @:expose("lime.embed") + public static function embed (projectName:String, element:Dynamic, width:Null = null, height:Null = null, windowConfig:Dynamic = null):Void { + + if (__applicationEntryPoint == null || __applicationConfig == null) return; + + if (__applicationEntryPoint.exists (projectName)) { + + var htmlElement:Element = null; + + if (Std.is (element, String)) { + + htmlElement = cast Browser.document.getElementById (element); + + } else if (element == null) { + + htmlElement = cast Browser.document.createElement ("div"); + + } else { + + htmlElement = cast element; + + } + + if (htmlElement == null) { + + Browser.window.console.log ("[lime.embed] ERROR: Cannot find target element: " + element); + return; + + } + + if (width == null) { + + width = 0; + + } + + if (height == null) { + + height = 0; + + } + + var defaultConfig = __applicationConfig[projectName]; + var config:Config = {}; + + __copyMissingFields (config, defaultConfig); + + if (windowConfig != null) { + + config.windows = []; + + if (Std.is (windowConfig, Array)) { + + config.windows = windowConfig; + + } else { + + config.windows[0] = windowConfig; + + } + + for (i in 0...config.windows.length) { + + if (i < defaultConfig.windows.length) { + + __copyMissingFields (config.windows[i], defaultConfig.windows[i]); + + } + + __copyMissingFields (config.windows[i].parameters, defaultConfig.windows[i].parameters); + + if (Std.is (windowConfig.background, String)) { + + var background = StringTools.replace (Std.string (windowConfig.background), "#", ""); + + if (background.indexOf ("0x") > -1) { + + windowConfig.background = Std.parseInt (background); + + } else { + + windowConfig.background = Std.parseInt ("0x" + background); + + } + + } + + } + + } + + if (Reflect.field (config.windows[0], "rootPath")) { + + config.rootPath = Reflect.field (config.windows[0], "rootPath"); + #if (lime < "5.0.0") + Reflect.setField (config, "assetsPrefix", config.rootPath); + #end + Reflect.deleteField (config.windows[0], "rootPath"); + + } + + #if (lime < "5.0.0") + if (Reflect.field (config.windows[0], "assetsPrefix")) { + + config.rootPath = Reflect.field (config.windows[0], "assetsPrefix"); + Reflect.setField (config, "assetsPrefix", config.rootPath); + Reflect.deleteField (config.windows[0], "assetsPrefix"); + + } + #end + + config.windows[0].element = htmlElement; + config.windows[0].width = width; + config.windows[0].height = height; + + __applicationEntryPoint[projectName] (config); + + } + + } + #end + + public static function exit (code:Int):Void { #if android @@ -273,6 +402,23 @@ class System { } + @:noCompletion private static function __copyMissingFields (target:Dynamic, source:Dynamic):Void { + + if (source == null || target == null) return; + + for (field in Reflect.fields (source)) { + + if (!Reflect.hasField (target, field)) { + + Reflect.setField (target, field, Reflect.field (source, field)); + + } + + } + + } + + @:noCompletion private static function __getDirectory (type:SystemDirectory):String { #if (lime_cffi && !macro) @@ -356,6 +502,26 @@ class System { } + private static function __registerEntryPoint (projectName:String, entryPoint:Function, config:Config):Void { + + if (__applicationConfig == null) { + + __applicationConfig = new Map (); + + } + + if (__applicationEntryPoint == null) { + + __applicationEntryPoint = new Map (); + + } + + __applicationEntryPoint[projectName] = entryPoint; + __applicationConfig[projectName] = config; + + } + + // Get & Set Methods diff --git a/templates/haxe/ApplicationMain.hx b/templates/haxe/ApplicationMain.hx index 5afc68bd6..50f84fa30 100644 --- a/templates/haxe/ApplicationMain.hx +++ b/templates/haxe/ApplicationMain.hx @@ -2,22 +2,17 @@ package; @:access(lime.app.Application) -@:access(lime.utils.AssetLibrary) +@:access(lime.system.System) @:dox(hide) class ApplicationMain { - public static var config:lime.app.Config; - public static var preloader:lime.app.Preloader; - - private static var app:lime.app.Application; - private static var rootPath:String; - - public static function main () { - config = { + var projectName = "::APP_FILE::"; + + var config = { build: "::meta.buildNumber::", company: "::meta.company::", @@ -42,7 +37,7 @@ package; hidden: #if munit true #else ::hidden:: #end, maximized: ::maximized::, minimized: ::minimized::, - parameters: "::parameters::", + parameters: ::parameters::, resizable: ::resizable::, stencilBuffer: ::stencilBuffer::, title: "::title::", @@ -55,21 +50,23 @@ package; }; + lime.system.System.__registerEntryPoint (projectName, create, config); + #if (!html5 || munit) - create (); + create (config); #end } - public static function create ():Void { + public static function create (config:lime.app.Config):Void { - ManifestResources.init (); + ManifestResources.init (config); - preloader = new ::if (PRELOADER_NAME != "")::::PRELOADER_NAME::::else::lime.app.Preloader::end:: (); + var preloader = new ::if (PRELOADER_NAME != "")::::PRELOADER_NAME::::else::lime.app.Preloader::end:: (); #if !munit - app = new ::APP_MAIN:: (); + var app = new ::APP_MAIN:: (); app.setPreloader (preloader); app.create (config); #end @@ -90,82 +87,12 @@ package; preloader.load (); - start (); + start (app); } - #if (js && html5) - @:keep @:expose("::APP_FILE::.embed") - public static function embed (element:Dynamic, width:Null = null, height:Null = null, background:String = null, assetsPrefix:String = null) { - - var htmlElement:js.html.Element = null; - - if (Std.is (element, String)) { - - htmlElement = cast js.Browser.document.getElementById (cast (element, String)); - - } else if (element == null) { - - htmlElement = cast js.Browser.document.createElement ("div"); - - } else { - - htmlElement = cast element; - - } - - var color = null; - - if (background != null && background != "") { - - background = StringTools.replace (background, "#", ""); - - if (background.indexOf ("0x") > -1) { - - color = Std.parseInt (background); - - } else { - - color = Std.parseInt ("0x" + background); - - } - - } - - if (width == null) { - - width = 0; - - } - - if (height == null) { - - height = 0; - - } - - config.windows[0].background = color; - config.windows[0].element = htmlElement; - config.windows[0].width = width; - config.windows[0].height = height; - config.assetsPrefix = assetsPrefix; - - create (); - - } - - - @:keep @:expose("lime.embed") - public static function _embed (element:Dynamic, width:Null = null, height:Null = null, background:String = null, assetsPrefix:String = null) { - - embed (element, width, height, background, assetsPrefix); - - } - #end - - - public static function start ():Void { + public static function start (app:lime.app.Application = null):Void { #if !munit diff --git a/templates/haxe/ManifestResources.hx b/templates/haxe/ManifestResources.hx index 17d31024c..b23cbb18c 100644 --- a/templates/haxe/ManifestResources.hx +++ b/templates/haxe/ManifestResources.hx @@ -1,6 +1,7 @@ package; +import lime.app.Config; import lime.utils.AssetLibrary; import lime.utils.AssetManifest; import lime.utils.Assets; @@ -19,15 +20,15 @@ import sys.FileSystem; public static var preloadLibraryNames:Array; - public static function init ():Void { + public static function init (config:Config):Void { preloadLibraries = new Array (); preloadLibraryNames = new Array (); var rootPath = null; - if (ApplicationMain.config != null && Reflect.hasField (ApplicationMain.config, "assetsPrefix")) { + if (config != null && Reflect.hasField (config, "assetsPrefix")) { - rootPath = Reflect.field (ApplicationMain.config, "assetsPrefix"); + rootPath = Reflect.field (config, "assetsPrefix"); }