Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * The MIT License (MIT)
- *
- * Copyright (c) 2015 Joseph Cloutier
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
- import flash.display.DisplayObject;
- import flash.display.DisplayObjectContainer;
- import flash.display.Sprite;
- /**
- * An API for DisplayObjectContainers, allowing you to treat them as
- * Arrays. The functions provided are based on Haxe's Array class.
- *
- * Note that some functions may not perform as expected. Some examples:
- * Any operation that would cause a child to be added twice will
- * remove the first reference to the child.
- * "array.push(array[0])" will move the first child to the end, keeping
- * the array the same length.
- * "array[0] = array[1]" is equivalent to "array.unshift()" - it'll
- * shorten the array by removing the first child.
- * Functions that copy the array will return Array<DisplayObject> rather
- * than DisplayObjectArray. This is because it's impossible for two
- * DisplayObjectContainers to share a child.
- */
- abstract DisplayObjectArray(DisplayObjectContainer) from DisplayObjectContainer to DisplayObjectContainer {
- public var length(get, never):Int;
- public inline function new() {
- this = new Sprite();
- }
- private inline function get_length():Int {
- return this.numChildren;
- }
- @:arrayAccess private inline function get(index:Int):DisplayObject {
- return this.getChildAt(index);
- }
- @:arrayAccess private inline function set(index:Int, child:DisplayObject):DisplayObject {
- if(index < this.numChildren) {
- this.removeChildAt(index);
- }
- this.addChildAt(child, index);
- return child;
- }
- public inline function concat(array:DisplayObjectArray):Array<DisplayObject> {
- var result:Array<DisplayObject> = copy();
- for(child in array) {
- result.push(child);
- }
- return result;
- }
- public function join(sep:String):String {
- var result:StringBuf = new StringBuf();
- for(i in 0...length) {
- if(i > 0) {
- result.add(sep);
- }
- result.add(get(i));
- }
- return result.toString();
- }
- public inline function pop():DisplayObject {
- if(this.numChildren > 0) {
- return this.removeChildAt(this.numChildren - 1);
- } else {
- return null;
- }
- }
- public inline function push(child:DisplayObject):Int {
- this.addChild(child);
- return this.numChildren;
- }
- public inline function reverse():Void {
- if(length > 1) {
- for(child in [while(length > 0) pop()]) {
- push(child);
- }
- }
- }
- public inline function shift():DisplayObject {
- if(length > 0) {
- return this.removeChildAt(0);
- } else {
- return null;
- }
- }
- public function slice(pos:Int, ?end:Int):Array<DisplayObject> {
- if(pos < 0) {
- pos += length;
- if(pos < 0) {
- pos = 0;
- }
- }
- if(end == null || end > length) {
- end = length;
- } else if(end < 0) {
- end += length;
- if(end < 0) {
- end = 0;
- }
- }
- if(pos >= end) {
- return [];
- }
- return [for(i in pos...end) get(i)];
- }
- public function sort(f:DisplayObject -> DisplayObject -> Int):Void {
- var children:Array<DisplayObject> = copy();
- children.sort(f);
- this.removeChildren();
- for(child in children) {
- push(child);
- }
- }
- public function splice(pos:Int, len:Int):Array<DisplayObject> {
- if(pos < 0) {
- pos += length;
- if(pos < 0) {
- pos = 0;
- }
- }
- if(length - pos < len) {
- len = length - pos;
- }
- return [for(i in 0...len) this.removeChildAt(pos)];
- }
- public inline function toString():String {
- return "[" + join(",") + "]";
- }
- public inline function unshift(child:DisplayObject):Void {
- this.addChildAt(child, 0);
- }
- public function insert(pos:Int, child:DisplayObject):Void {
- if(pos > length) {
- pos = length;
- } else if(pos < 0) {
- pos += length;
- if(pos < 0) {
- pos = 0;
- }
- }
- this.addChildAt(child, pos);
- }
- public function remove(child:DisplayObject):Bool {
- if(child != null && child.parent == this) {
- this.removeChild(child);
- return true;
- } else {
- return false;
- }
- }
- public function indexOf(child:DisplayObject, ?fromIndex:Int = 0):Int {
- if(fromIndex < 0) {
- fromIndex += length;
- }
- var index:Int = this.getChildIndex(child);
- if(index >= fromIndex) {
- return index;
- } else {
- return -1;
- }
- }
- public function lastIndexOf(child:DisplayObject, ?fromIndex:Int = 0):Int {
- if(fromIndex < 0) {
- fromIndex += length;
- }
- var index:Int = this.getChildIndex(child);
- if(index <= fromIndex) {
- return index;
- } else {
- return -1;
- }
- }
- public inline function copy():Array<DisplayObject> {
- return [for(child in iterator()) child];
- }
- public inline function iterator():DisplayObjectContainerIterator {
- return new DisplayObjectContainerIterator(cast this);
- }
- public inline function map<T>(f:DisplayObject -> T):Array<T> {
- return [for(child in iterator()) f(child)];
- }
- public inline function filter(f:DisplayObject -> Bool):Array<DisplayObject> {
- return [for(child in iterator()) if(f(child)) child];
- }
- }
- private class DisplayObjectContainerIterator {
- private var array:DisplayObjectArray;
- private var index:Int = 0;
- public inline function new(array:DisplayObjectArray) {
- this.array = array;
- }
- public inline function hasNext():Bool {
- return index < array.length;
- }
- public inline function next():DisplayObject {
- return array[index++];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement