ResourceSelection.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. import EditorUI = require("../EditorUI");
  8. import EditorEvents = require("../../editor/EditorEvents");
  9. import ModalWindow = require("./ModalWindow");
  10. class ResourceSelection extends ModalWindow {
  11. folderList: Atomic.UIListView;
  12. callback: (returnObject: any, args: any) => void;
  13. args: any;
  14. resourceType: string;
  15. populate(importerType: string, resourceType: string) {
  16. var db = ToolCore.assetDatabase;
  17. var assets = db.getAssetsByImporterType(importerType, resourceType);
  18. for (var i in assets) {
  19. var asset = assets[i];
  20. if (importerType == "JavascriptImporter") {
  21. if (!(<ToolCore.JavascriptImporter>asset.importer).isComponentFile())
  22. continue;
  23. }
  24. // TODO: Generalize this for other cache assets
  25. if (resourceType == "Animation") {
  26. var modelImporter = <ToolCore.ModelImporter>(asset.importer);
  27. var animations = modelImporter.getAnimations();
  28. if (animations.length) {
  29. for (var i in animations) {
  30. this.folderList.addRootItem(animations[i].animationName + " : " + asset.name, "", animations[i].name);
  31. }
  32. }
  33. } else {
  34. this.folderList.addRootItem(asset.relativePath, "", asset.guid);
  35. }
  36. }
  37. }
  38. constructor(windowText: string, importerType: string, resourceType: string, callback: (asset: ToolCore.Asset, args: any) => void, args: any) {
  39. super();
  40. this.resourceType = resourceType;
  41. this.callback = callback;
  42. this.args = args;
  43. this.load("AtomicEditor/editor/ui/resourceselection.tb.txt");
  44. var foldercontainer = this.getWidget("resourcecontainer");
  45. var folderList = this.folderList = new Atomic.UIListView();
  46. folderList.rootList.id = "resourceList_";
  47. foldercontainer.addChild(folderList);
  48. this.populate(importerType, resourceType);
  49. this.text = windowText;
  50. this.setSize(800, 600);
  51. this.center();
  52. }
  53. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  54. if (ev.count >= 2) {
  55. var id = ev.target.id;
  56. if (id == this.folderList.rootList.id) {
  57. this.selectFile();
  58. }
  59. }
  60. if (ev.type == Atomic.UI_EVENT_TYPE_CLICK) {
  61. var id = ev.target.id;
  62. if (id == "select") {
  63. return this.selectFile();
  64. }
  65. if (id == "cancel") {
  66. this.hide();
  67. return true;
  68. }
  69. }
  70. }
  71. selectFile(): boolean {
  72. var id = this.folderList.selectedItemID;
  73. if (this.resourceType == "Animation") {
  74. if (id.length) {
  75. this.callback(Atomic.cache.getResource("Animation", id), this.args);
  76. this.hide();
  77. return true;
  78. }
  79. this.hide();
  80. return true;
  81. } else {
  82. if (id.length) {
  83. this.callback(ToolCore.assetDatabase.getAssetByGUID(id), this.args);
  84. this.hide();
  85. return true;
  86. }
  87. }
  88. return false;
  89. }
  90. }
  91. export = ResourceSelection;