Advertisement
player_03

DisplayObjectArray.hx

Apr 5th, 2015
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 6.39 KB | None | 0 0
  1. /*
  2.  * The MIT License (MIT)
  3.  *
  4.  * Copyright (c) 2015 Joseph Cloutier
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  7.  * of this software and associated documentation files (the "Software"), to deal
  8.  * in the Software without restriction, including without limitation the rights
  9.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10.  * copies of the Software, and to permit persons to whom the Software is
  11.  * furnished to do so, subject to the following conditions:
  12.  *
  13.  * The above copyright notice and this permission notice shall be included in all
  14.  * copies or substantial portions of the Software.
  15.  *
  16.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22.  * SOFTWARE.
  23.  */
  24.  
  25. import flash.display.DisplayObject;
  26. import flash.display.DisplayObjectContainer;
  27. import flash.display.Sprite;
  28.  
  29. /**
  30.  * An API for DisplayObjectContainers, allowing you to treat them as
  31.  * Arrays. The functions provided are based on Haxe's Array class.
  32.  *
  33.  * Note that some functions may not perform as expected. Some examples:
  34.  * Any operation that would cause a child to be added twice will
  35.      * remove the first reference to the child.
  36.  * "array.push(array[0])" will move the first child to the end, keeping
  37.      * the array the same length.
  38.  * "array[0] = array[1]" is equivalent to "array.unshift()" - it'll
  39.      * shorten the array by removing the first child.
  40.  * Functions that copy the array will return Array<DisplayObject> rather
  41.      * than DisplayObjectArray. This is because it's impossible for two
  42.      * DisplayObjectContainers to share a child.
  43.  */
  44. abstract DisplayObjectArray(DisplayObjectContainer) from DisplayObjectContainer to DisplayObjectContainer {
  45.     public var length(get, never):Int;
  46.    
  47.     public inline function new() {
  48.         this = new Sprite();
  49.     }
  50.    
  51.     private inline function get_length():Int {
  52.         return this.numChildren;
  53.     }
  54.    
  55.     @:arrayAccess private inline function get(index:Int):DisplayObject {
  56.         return this.getChildAt(index);
  57.     }
  58.    
  59.     @:arrayAccess private inline function set(index:Int, child:DisplayObject):DisplayObject {
  60.         if(index < this.numChildren) {
  61.             this.removeChildAt(index);
  62.         }
  63.         this.addChildAt(child, index);
  64.         return child;
  65.     }
  66.    
  67.     public inline function concat(array:DisplayObjectArray):Array<DisplayObject> {
  68.         var result:Array<DisplayObject> = copy();
  69.         for(child in array) {
  70.             result.push(child);
  71.         }
  72.         return result;
  73.     }
  74.    
  75.     public function join(sep:String):String {
  76.         var result:StringBuf = new StringBuf();
  77.         for(i in 0...length) {
  78.             if(i > 0) {
  79.                 result.add(sep);
  80.             }
  81.             result.add(get(i));
  82.         }
  83.         return result.toString();
  84.     }
  85.    
  86.     public inline function pop():DisplayObject {
  87.         if(this.numChildren > 0) {
  88.             return this.removeChildAt(this.numChildren - 1);
  89.         } else {
  90.             return null;
  91.         }
  92.     }
  93.    
  94.     public inline function push(child:DisplayObject):Int {
  95.         this.addChild(child);
  96.         return this.numChildren;
  97.     }
  98.    
  99.     public inline function reverse():Void {
  100.         if(length > 1) {
  101.             for(child in [while(length > 0) pop()]) {
  102.                 push(child);
  103.             }
  104.         }
  105.     }
  106.    
  107.     public inline function shift():DisplayObject {
  108.         if(length > 0) {
  109.             return this.removeChildAt(0);
  110.         } else {
  111.             return null;
  112.         }
  113.     }
  114.    
  115.     public function slice(pos:Int, ?end:Int):Array<DisplayObject> {
  116.         if(pos < 0) {
  117.             pos += length;
  118.             if(pos < 0) {
  119.                 pos = 0;
  120.             }
  121.         }
  122.         if(end == null || end > length) {
  123.             end = length;
  124.         } else if(end < 0) {
  125.             end += length;
  126.             if(end < 0) {
  127.                 end = 0;
  128.             }
  129.         }
  130.         if(pos >= end) {
  131.             return [];
  132.         }
  133.        
  134.         return [for(i in pos...end) get(i)];
  135.     }
  136.    
  137.     public function sort(f:DisplayObject -> DisplayObject -> Int):Void {
  138.         var children:Array<DisplayObject> = copy();
  139.         children.sort(f);
  140.        
  141.         this.removeChildren();
  142.         for(child in children) {
  143.             push(child);
  144.         }
  145.     }
  146.    
  147.     public function splice(pos:Int, len:Int):Array<DisplayObject> {
  148.         if(pos < 0) {
  149.             pos += length;
  150.             if(pos < 0) {
  151.                 pos = 0;
  152.             }
  153.         }
  154.         if(length - pos < len) {
  155.             len = length - pos;
  156.         }
  157.         return [for(i in 0...len) this.removeChildAt(pos)];
  158.     }
  159.    
  160.     public inline function toString():String {
  161.         return "[" + join(",") + "]";
  162.     }
  163.    
  164.     public inline function unshift(child:DisplayObject):Void {
  165.         this.addChildAt(child, 0);
  166.     }
  167.    
  168.     public function insert(pos:Int, child:DisplayObject):Void {
  169.         if(pos > length) {
  170.             pos = length;
  171.         } else if(pos < 0) {
  172.             pos += length;
  173.             if(pos < 0) {
  174.                 pos = 0;
  175.             }
  176.         }
  177.         this.addChildAt(child, pos);
  178.     }
  179.    
  180.     public function remove(child:DisplayObject):Bool {
  181.         if(child != null && child.parent == this) {
  182.             this.removeChild(child);
  183.             return true;
  184.         } else {
  185.             return false;
  186.         }
  187.     }
  188.    
  189.     public function indexOf(child:DisplayObject, ?fromIndex:Int = 0):Int {
  190.         if(fromIndex < 0) {
  191.             fromIndex += length;
  192.         }
  193.        
  194.         var index:Int = this.getChildIndex(child);
  195.         if(index >= fromIndex) {
  196.             return index;
  197.         } else {
  198.             return -1;
  199.         }
  200.     }
  201.    
  202.     public function lastIndexOf(child:DisplayObject, ?fromIndex:Int = 0):Int {
  203.         if(fromIndex < 0) {
  204.             fromIndex += length;
  205.         }
  206.        
  207.         var index:Int = this.getChildIndex(child);
  208.         if(index <= fromIndex) {
  209.             return index;
  210.         } else {
  211.             return -1;
  212.         }
  213.     }
  214.    
  215.     public inline function copy():Array<DisplayObject> {
  216.         return [for(child in iterator()) child];
  217.     }
  218.    
  219.     public inline function iterator():DisplayObjectContainerIterator {
  220.         return new DisplayObjectContainerIterator(cast this);
  221.     }
  222.    
  223.     public inline function map<T>(f:DisplayObject -> T):Array<T> {
  224.         return [for(child in iterator()) f(child)];
  225.     }
  226.    
  227.     public inline function filter(f:DisplayObject -> Bool):Array<DisplayObject> {
  228.         return [for(child in iterator()) if(f(child)) child];
  229.     }
  230. }
  231.  
  232. private class DisplayObjectContainerIterator {
  233.     private var array:DisplayObjectArray;
  234.     private var index:Int = 0;
  235.    
  236.     public inline function new(array:DisplayObjectArray) {
  237.         this.array = array;
  238.     }
  239.    
  240.     public inline function hasNext():Bool {
  241.         return index < array.length;
  242.     }
  243.    
  244.     public inline function next():DisplayObject {
  245.         return array[index++];
  246.     }
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement