ResourceSelection.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. this.folderList.addRootItem("None", "", "");
  19. for (var i in assets) {
  20. var asset = assets[i];
  21. if (importerType == "JavascriptImporter") {
  22. if (!(<ToolCore.JavascriptImporter>asset.importer).isComponentFile())
  23. continue;
  24. }
  25. // TODO: Generalize this for other cache assets
  26. if (resourceType == "Animation") {
  27. var modelImporter = <ToolCore.ModelImporter>(asset.importer);
  28. var animations = modelImporter.getAnimations();
  29. if (animations.length) {
  30. for (var i in animations) {
  31. this.folderList.addRootItem(animations[i].animationName + " : " + asset.name, "", animations[i].name);
  32. }
  33. }
  34. } else {
  35. this.folderList.addRootItem(asset.relativePath, "", asset.guid);
  36. }
  37. }
  38. }
  39. constructor(windowText: string, importerType: string, resourceType: string, callback: (asset: ToolCore.Asset, args: any) => void, args: any) {
  40. super();
  41. this.resourceType = resourceType;
  42. this.callback = callback;
  43. this.args = args;
  44. this.load("AtomicEditor/editor/ui/resourceselection.tb.txt");
  45. var foldercontainer = this.getWidget("resourcecontainer");
  46. var folderList = this.folderList = new Atomic.UIListView();
  47. folderList.rootList.id = "resourceList_";
  48. foldercontainer.addChild(folderList);
  49. this.populate(importerType, resourceType);
  50. this.text = windowText;
  51. this.setSize(800, 600);
  52. this.center();
  53. }
  54. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  55. if (ev.count >= 2) {
  56. var id = ev.target.id;
  57. if (id == this.folderList.rootList.id) {
  58. this.selectFile();
  59. }
  60. }
  61. if (ev.type == Atomic.UI_EVENT_TYPE_CLICK) {
  62. var id = ev.target.id;
  63. if (id == "select") {
  64. return this.selectFile();
  65. }
  66. if (id == "cancel") {
  67. this.hide();
  68. return true;
  69. }
  70. }
  71. }
  72. selectFile(): boolean {
  73. var id = this.folderList.selectedItemID;
  74. if (id == "") {
  75. this.sendEvent(EditorEvents.RemoveCurrentAssetAssigned);
  76. this.hide();
  77. return true;
  78. }
  79. if (this.resourceType == "Animation") {
  80. if (id.length) {
  81. this.callback(Atomic.cache.getResource("Animation", id), this.args);
  82. this.hide();
  83. return true;
  84. }
  85. this.hide();
  86. return true;
  87. } else {
  88. if (id.length) {
  89. this.callback(ToolCore.assetDatabase.getAssetByGUID(id), this.args);
  90. this.hide();
  91. return true;
  92. }
  93. }
  94. return false;
  95. }
  96. }
  97. export = ResourceSelection;