From 0382c1709fc2be7b59ee6cd9088d69b981074dbf Mon Sep 17 00:00:00 2001 From: GeoKureli Date: Fri, 24 Mar 2017 23:40:07 -0700 Subject: [PATCH 1/5] add swapParent --- .../com/geokureli/rapella/utils/SwfUtils.hx | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/source/com/geokureli/rapella/utils/SwfUtils.hx b/source/com/geokureli/rapella/utils/SwfUtils.hx index 98d6139..d08233a 100644 --- a/source/com/geokureli/rapella/utils/SwfUtils.hx +++ b/source/com/geokureli/rapella/utils/SwfUtils.hx @@ -4,6 +4,8 @@ import hx.debug.Assert; import openfl.display.DisplayObjectContainer; import openfl.display.DisplayObject; import openfl.display.MovieClip; +import openfl.geom.Point; +import openfl.geom.Rectangle; /** * ... @@ -84,4 +86,45 @@ class SwfUtils { return get(parent, path); } + + static public function swapParent( + child :DisplayObject, + parent:DisplayObjectContainer, + index :Int = -1):DisplayObject { + + var rect = new Rectangle(child.x, child.y, child.scaleX, child.scaleY); + var p = rect.topLeft; + + if (child.parent != null) + p = child.parent.localToGlobal(p); + p = parent.globalToLocal(p); + rect.x = p.x; + rect.y = p.y; + + p = rect.size; + var o = new Point(); + if (child.parent != null) { + + o = child.parent.localToGlobal(o); + p = child.parent.localToGlobal(p); + } + o = parent.globalToLocal(o); + p = parent.globalToLocal(p); + + p.offset( -o.x, -o.y); + var len = p.length; + p = rect.size; + p.normalize(len); + + if (index > -1) + parent.addChildAt(child, index); + else + parent.addChild(child); + + child.x = rect.x; + child.y = rect.y; + child.scaleX = rect.width; + child.scaleY = rect.height; + return child; + } } \ No newline at end of file From 0d047b7fa80fd954eb71ffb2c5424edd568a0de1 Mon Sep 17 00:00:00 2001 From: GeoKureli Date: Fri, 24 Mar 2017 23:41:50 -0700 Subject: [PATCH 2/5] rename var --- source/com/geokureli/rapella/art/Wrapper.hx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/com/geokureli/rapella/art/Wrapper.hx b/source/com/geokureli/rapella/art/Wrapper.hx index e6f005e..87f4151 100644 --- a/source/com/geokureli/rapella/art/Wrapper.hx +++ b/source/com/geokureli/rapella/art/Wrapper.hx @@ -29,7 +29,7 @@ class Wrapper extends Sprite var _actionMap:ActionMap; var _childMap:Map; var _childMapper:ChildMap; - var _childList:Array; + var _children:Array; public function new(target:DisplayObjectContainer) { super(); @@ -88,7 +88,7 @@ class Wrapper extends Sprite function initChildren():Void { - _childList = _childMapper.map(this, _target); + _children = _childMapper.map(this, _target); } function onAddedToStage(e:Event = null) { From b0fcbfbf2be02632050e42db62e9b944cfa0a72e Mon Sep 17 00:00:00 2001 From: GeoKureli Date: Fri, 24 Mar 2017 23:45:15 -0700 Subject: [PATCH 3/5] add children to menu bg --- .../geokureli/rapella/art/ui/MenuWrapper.hx | 1 + .../com/geokureli/rapella/utils/ChildMap.hx | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/source/com/geokureli/rapella/art/ui/MenuWrapper.hx b/source/com/geokureli/rapella/art/ui/MenuWrapper.hx index ceeeb60..0b9dbbb 100644 --- a/source/com/geokureli/rapella/art/ui/MenuWrapper.hx +++ b/source/com/geokureli/rapella/art/ui/MenuWrapper.hx @@ -33,6 +33,7 @@ class MenuWrapper extends Wrapper { _scriptId = "menu"; + _childMapper.sortChildren = true; _childMap["bg" ] = { field:"_bg" , priority:ChildPriority.Optional }; _childMap["message"] = { field:"_message", priority:ChildPriority.Optional }; } diff --git a/source/com/geokureli/rapella/utils/ChildMap.hx b/source/com/geokureli/rapella/utils/ChildMap.hx index c57a3b8..73dbdf0 100644 --- a/source/com/geokureli/rapella/utils/ChildMap.hx +++ b/source/com/geokureli/rapella/utils/ChildMap.hx @@ -14,6 +14,7 @@ class ChildMap { static var ARRAY_EX:EReg = ~/\[(\d*)\]/; @unreflective public var mapSuccessful(default, null):Bool; + @unreflective public var sortChildren:Bool; @:unreflective var _map:Map; @:unreflective var _onFail:Signal; @@ -80,6 +81,9 @@ class ChildMap { else if (_logPriority == ChildPriority.Normal) Expect.fail(_mapLog); + if(sortChildren) + children.sort(sortByIndex.bind(target)); + return children; } @@ -132,6 +136,28 @@ class ChildMap { return asset; } + function sortByIndex(target:DisplayObjectContainer, child1:DisplayObject, child2:DisplayObject):Int { + + if (child1 == child2 + || (!target.contains(child1) && !target.contains(child2))) + return 0; + if (!target.contains(child1)) + return 1; + if (!target.contains(child2)) + return -1; + + while (!child1.parent.contains(child2)) { + + child1 = child1.parent; + target = child1.parent; + } + + while (child2.parent != target) + child2 = child2.parent; + + return target.getChildIndex(child2) - target.getChildIndex(child1); + } + public function unMap(target:DisplayObjectContainer):Void { var field:String; From 6e251a19fca9cd04eb2c4c2841b374573c35f128 Mon Sep 17 00:00:00 2001 From: GeoKureli Date: Sat, 25 Mar 2017 01:44:16 -0700 Subject: [PATCH 4/5] fix compile condition --- source/com/geokureli/rapella/art/AssetManager.hx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/source/com/geokureli/rapella/art/AssetManager.hx b/source/com/geokureli/rapella/art/AssetManager.hx index 0faabbd..f141b30 100644 --- a/source/com/geokureli/rapella/art/AssetManager.hx +++ b/source/com/geokureli/rapella/art/AssetManager.hx @@ -82,9 +82,8 @@ class AssetManager { #if debug if (_debugAssets.exists(id)) return cast _debugAssets[id]; - #else - return Assets.getText(id); #end + return Assets.getText(id); } static public function getScene(name:String):MovieClip { From 606f9973b5741c397bc63fd3d8b54b5c7e2eaaae Mon Sep 17 00:00:00 2001 From: GeoKureli Date: Sat, 25 Mar 2017 01:44:51 -0700 Subject: [PATCH 5/5] specific fail handling --- source/com/geokureli/rapella/art/Wrapper.hx | 26 +++++++--- .../geokureli/rapella/art/ui/MenuWrapper.hx | 24 +++++++-- .../com/geokureli/rapella/utils/ChildMap.hx | 51 ++++++++++++++----- 3 files changed, 76 insertions(+), 25 deletions(-) diff --git a/source/com/geokureli/rapella/art/Wrapper.hx b/source/com/geokureli/rapella/art/Wrapper.hx index 87f4151..a00c00f 100644 --- a/source/com/geokureli/rapella/art/Wrapper.hx +++ b/source/com/geokureli/rapella/art/Wrapper.hx @@ -78,24 +78,36 @@ class Wrapper extends Sprite if(_scriptId != null) ScriptInterpreter.addInterpreter(_scriptId, _actionMap); - initChildren(); - - if (target.stage == null) - _target.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage); - else - onAddedToStage(); + mapChildren(); + if (_childMapper.mapSuccessful) { + + initChildren(); + + if (target.stage == null) + _target.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage); + else + onAddedToStage(); + } else + abort(); } - function initChildren():Void { + function mapChildren():Void { _children = _childMapper.map(this, _target); } + function initChildren():Void { } + function onAddedToStage(e:Event = null) { _target.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage); } + function abort():Void { + + destroy(); + } + public function update():Void { for (child in _childWrappers) { diff --git a/source/com/geokureli/rapella/art/ui/MenuWrapper.hx b/source/com/geokureli/rapella/art/ui/MenuWrapper.hx index 0b9dbbb..82479a9 100644 --- a/source/com/geokureli/rapella/art/ui/MenuWrapper.hx +++ b/source/com/geokureli/rapella/art/ui/MenuWrapper.hx @@ -42,13 +42,29 @@ class MenuWrapper extends Wrapper { super.onAddedToStage(e); _autoDestroyListener = FuncUtils.addListenerOnce(_target, Event.REMOVED, function(_):Void { destroy(); }); + } + + override function initChildren():Void { + super.initChildren(); - if (_isSelfContained) { + var tweenTarget = _target; + + if (_bg != null) { + + tweenTarget = _bg; + _children.remove(_bg); + _isSelfContained = true; - _target.alpha = 0; - Actuate.tween(_target, .5, { alpha:1 } ).onComplete(handleShowComplete); + for (child in _children) + SwfUtils.swapParent(child, _bg); } - else + + if (_isSelfContained) { + + tweenTarget.alpha = 0; + Actuate.tween(tweenTarget, .5, { alpha:1 } ).onComplete(handleShowComplete); + + } else handleShowComplete(); } diff --git a/source/com/geokureli/rapella/utils/ChildMap.hx b/source/com/geokureli/rapella/utils/ChildMap.hx index 73dbdf0..71c5fde 100644 --- a/source/com/geokureli/rapella/utils/ChildMap.hx +++ b/source/com/geokureli/rapella/utils/ChildMap.hx @@ -2,6 +2,7 @@ package com.geokureli.rapella.utils; import hx.debug.Expect; import hx.debug.Assert; +import hx.debug.Require; import openfl.display.DisplayObject; import openfl.display.DisplayObjectContainer; import haxe.PosInfos; @@ -17,7 +18,8 @@ class ChildMap { @unreflective public var sortChildren:Bool; @:unreflective var _map:Map; - @:unreflective var _onFail:Signal; + @:unreflective var _onFail:Signal; + @:unreflective var _failListeners:Int; @:unreflective var _priorityMap:Map; @:unreflective var _mapLog:String; @:unreflective var _logPriority:ChildPriority; @@ -30,17 +32,32 @@ class ChildMap { function setDefaults():Void { - _onFail = new Signal(); + _onFail = new Signal(); + _failListeners = 0; _priorityMap = [ - ChildPriority.Strict => new AssertLogger(onAssertFail), - ChildPriority.Normal => new AssertLogger(onExpectFail), - ChildPriority.Optional => new AssertLogger(doNothing ) + ChildPriority.Strict => new AssertLogger(handleStrictFail), + ChildPriority.Normal => new AssertLogger(handleFail ), + ChildPriority.Optional => new AssertLogger(doNothing ) ]; } - public function map(target:Dynamic, parent:DisplayObjectContainer):Array - { + public function addStrictFailListener(listener:String->Void):Void { + + _onFail.add(listener); + _failListeners++; + } + + public function removeStrictFailListener(listener:String->Void):Void { + + if (_onFail.get(listener) != null) + _failListeners++; + + _onFail.remove(listener); + } + + public function map(target:Dynamic, parent:DisplayObjectContainer):Array { + mapSuccessful = true; _mapLog = ""; _logPriority = ChildPriority.Optional; @@ -76,10 +93,15 @@ class ChildMap { } } - if (_logPriority == ChildPriority.Strict) + if (_logPriority == ChildPriority.Strict) { + + if (_failListeners > 0) + _onFail.dispatch(_mapLog); + else + Require.fail(_mapLog); + + } else if (_logPriority == ChildPriority.Normal) Assert.fail(_mapLog); - else if (_logPriority == ChildPriority.Normal) - Expect.fail(_mapLog); if(sortChildren) children.sort(sortByIndex.bind(target)); @@ -198,20 +220,21 @@ class ChildMap { _mapLog = null; } - function onAssertFail(?msg:String, ?pos:PosInfos):Void { + function handleStrictFail(?msg:String, ?pos:PosInfos):Void { mapSuccessful = false; _logPriority = ChildPriority.Strict; - _mapLog += 'ASSERT FAIL: $msg\n'; + _mapLog += 'REQUIRE FAIL: $msg\n'; } - function onExpectFail(?msg:String, ?pos:PosInfos):Void { + function handleFail(?msg:String, ?pos:PosInfos):Void { + mapSuccessful = false; if(_logPriority != ChildPriority.Strict) _logPriority = ChildPriority.Normal; - _mapLog += 'EXPECT FAIL: $msg\n'; + _mapLog += 'ASSERT FAIL: $msg\n'; } function doNothing(?msg:String, ?pos:PosInfos):Void {}