Skip to content

Instantly share code, notes, and snippets.

@Fintan
Created December 28, 2011 21:34
Show Gist options
  • Save Fintan/1529887 to your computer and use it in GitHub Desktop.
Save Fintan/1529887 to your computer and use it in GitHub Desktop.
Implementation of IViewContainer for RobotHaxe based on Mike Cann's gist: https://gist.github.com/1502132
package demo.view;
import flash.display.Sprite;
import flash.display.DisplayObject;
import robothaxe.core.IViewContainer;
class BaseView extends Sprite, implements IViewContainer {
public var viewAdded:Dynamic -> Void;
public var viewRemoved:Dynamic -> Void;
var children:Array<BaseView>;
function new() {
super();
this.children = [];
}
public function isAdded(view:Dynamic):Bool {
return Lambda.has(children,view);
}
override public function addChild(child:DisplayObject):DisplayObject{
if(Std.is(child, BaseView)) {
var c = cast child;
c.viewAdded = viewAdded;
c.viewRemoved = viewRemoved;
viewAdded(c);
children.push(c);
}
return super.addChild(child);
}
override public function removeChild(child:DisplayObject):DisplayObject{
if(Std.is(child, BaseView)) {
var c = cast child;
c.viewAdded = null;
c.viewRemoved = null;
if (viewRemoved != null) viewRemoved(c);
children.remove(c);
}
return super.removeChild(child);
}
public function destroy():Void {
for (c in children) {
c.viewAdded = null;
c.viewRemoved = null;
if (viewRemoved != null) viewRemoved(c);
}
children = [];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment