PageRenderTime 290ms CodeModel.GetById 140ms app.highlight 5ms RepoModel.GetById 142ms app.codeStats 0ms

/src/away3d/library/utils/AssetLibraryIterator.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 81 lines | 60 code | 17 blank | 4 comment | 16 complexity | 79dc87da45a514bc0afd0a9910f78e04 MD5 | raw file
 1package away3d.library.utils
 2{
 3	import away3d.library.assets.IAsset;
 4	
 5	public class AssetLibraryIterator
 6	{
 7		private var _assets:Vector.<IAsset>;
 8		private var _filtered:Vector.<IAsset>;
 9		
10		private var _idx:uint;
11		
12		public function AssetLibraryIterator(assets:Vector.<IAsset>, assetTypeFilter:String, namespaceFilter:String, filterFunc:Function)
13		{
14			_assets = assets;
15			filter(assetTypeFilter, namespaceFilter, filterFunc);
16		}
17		
18		public function get currentAsset():IAsset
19		{
20			// Return current, or null if no current
21			return (_idx < _filtered.length)?
22				_filtered[_idx] : null;
23		}
24		
25		public function get numAssets():uint
26		{
27			return _filtered.length;
28		}
29		
30		public function next():IAsset
31		{
32			var next:IAsset = null;
33			
34			if (_idx < _filtered.length)
35				next = _filtered[_idx];
36			
37			_idx++;
38			
39			return next;
40		}
41		
42		public function reset():void
43		{
44			_idx = 0;
45		}
46		
47		public function setIndex(index:uint):void
48		{
49			_idx = index;
50		}
51		
52		private function filter(assetTypeFilter:String, namespaceFilter:String, filterFunc:Function):void
53		{
54			if (assetTypeFilter || namespaceFilter || filterFunc != null) {
55				var idx:uint;
56				var asset:IAsset;
57				
58				idx = 0;
59				_filtered = new Vector.<IAsset>;
60				
61				for each (asset in _assets) {
62					// Skip this assets if filtering on type and this is wrong type
63					if (assetTypeFilter && asset.assetType != assetTypeFilter)
64						continue;
65					
66					// Skip this asset if filtering on namespace and this is wrong namespace
67					if (namespaceFilter && asset.assetNamespace != namespaceFilter)
68						continue;
69					
70					// Skip this asset if a filter func has been provided and it returns false
71					if (filterFunc != null && !filterFunc(asset))
72						continue;
73					
74					_filtered[idx++] = asset;
75				}
76			} else {
77				_filtered = _assets;
78			}
79		}
80	}
81}