/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

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