Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Child map #2

Merged
merged 6 commits into from
Mar 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions source/com/geokureli/rapella/art/AssetManager.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
30 changes: 21 additions & 9 deletions source/com/geokureli/rapella/art/Wrapper.hx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Wrapper extends Sprite
var _actionMap:ActionMap;
var _childMap:Map<String, Dynamic>;
var _childMapper:ChildMap;
var _childList:Array<DisplayObject>;
var _children:Array<DisplayObject>;

public function new(target:DisplayObjectContainer) {
super();
Expand Down Expand Up @@ -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 {

_childList = _childMapper.map(this, _target);
_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) {
Expand Down
25 changes: 21 additions & 4 deletions source/com/geokureli/rapella/art/ui/MenuWrapper.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
Expand All @@ -41,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();
}

Expand Down
77 changes: 63 additions & 14 deletions source/com/geokureli/rapella/utils/ChildMap.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,9 +15,11 @@ class ChildMap {
static var ARRAY_EX:EReg = ~/\[(\d*)\]/;

@unreflective public var mapSuccessful(default, null):Bool;
@unreflective public var sortChildren:Bool;

@:unreflective var _map:Map<String, Dynamic>;
@:unreflective var _onFail:Signal<String, String>;
@:unreflective var _onFail:Signal<String>;
@:unreflective var _failListeners:Int;
@:unreflective var _priorityMap:Map<ChildPriority, AssertLogger>;
@:unreflective var _mapLog:String;
@:unreflective var _logPriority:ChildPriority;
Expand All @@ -29,17 +32,32 @@ class ChildMap {

function setDefaults():Void {

_onFail = new Signal<String, String>();
_onFail = new Signal<String>();
_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<DisplayObject>
{
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<DisplayObject> {

mapSuccessful = true;
_mapLog = "";
_logPriority = ChildPriority.Optional;
Expand Down Expand Up @@ -75,10 +93,18 @@ 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));

return children;
}
Expand Down Expand Up @@ -132,6 +158,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;
Expand Down Expand Up @@ -172,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 {}
Expand Down
43 changes: 43 additions & 0 deletions source/com/geokureli/rapella/utils/SwfUtils.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
* ...
Expand Down Expand Up @@ -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;
}
}