ResourceSelection.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. import EditorUI = require("../EditorUI");
  23. import EditorEvents = require("../../editor/EditorEvents");
  24. import ModalWindow = require("./ModalWindow");
  25. class ResourceSelection extends ModalWindow {
  26. folderList: Atomic.UIListView;
  27. callback: (returnObject: any, args: any) => void;
  28. args: any;
  29. resourceType: string;
  30. populate(importerType: string, resourceType: string) {
  31. var db = ToolCore.assetDatabase;
  32. var assets = db.getAssetsByImporterType(importerType, resourceType);
  33. this.folderList.addRootItem("None", "", "");
  34. for (var i in assets) {
  35. var asset = assets[i];
  36. if (importerType == "JavascriptImporter") {
  37. if (!(<ToolCore.JavascriptImporter>asset.importer).isComponentFile())
  38. continue;
  39. }
  40. // TODO: Generalize this for other cache assets
  41. if (resourceType == "Animation") {
  42. var modelImporter = <ToolCore.ModelImporter>(asset.importer);
  43. var animations = modelImporter.getAnimations();
  44. if (animations.length) {
  45. for (var i in animations) {
  46. this.folderList.addRootItem(animations[i].animationName + " : " + asset.name, "", animations[i].name);
  47. }
  48. }
  49. } else {
  50. this.folderList.addRootItem(asset.relativePath, "", asset.guid);
  51. }
  52. }
  53. }
  54. constructor(windowText: string, importerType: string, resourceType: string, callback: (asset: ToolCore.Asset, args: any) => void, args: any) {
  55. super();
  56. this.resourceType = resourceType;
  57. this.callback = callback;
  58. this.args = args;
  59. this.load("AtomicEditor/editor/ui/resourceselection.tb.txt");
  60. var foldercontainer = this.getWidget("resourcecontainer");
  61. var folderList = this.folderList = new Atomic.UIListView();
  62. folderList.rootList.id = "resourceList_";
  63. foldercontainer.addChild(folderList);
  64. this.populate(importerType, resourceType);
  65. this.text = windowText;
  66. this.setSize(800, 600);
  67. this.center();
  68. }
  69. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  70. if (ev.count >= 2) {
  71. var id = ev.target.id;
  72. if (id == this.folderList.rootList.id) {
  73. this.selectFile();
  74. }
  75. }
  76. if (ev.type == Atomic.UI_EVENT_TYPE_CLICK) {
  77. var id = ev.target.id;
  78. if (id == "select") {
  79. return this.selectFile();
  80. }
  81. if (id == "cancel") {
  82. this.hide();
  83. return true;
  84. }
  85. }
  86. }
  87. selectFile(): boolean {
  88. var id = this.folderList.selectedItemID;
  89. if (id == "") {
  90. this.sendEvent(EditorEvents.RemoveCurrentAssetAssigned);
  91. this.hide();
  92. return true;
  93. }
  94. if (this.resourceType == "Animation") {
  95. if (id.length) {
  96. this.callback(Atomic.cache.getResource("Animation", id), this.args);
  97. this.hide();
  98. return true;
  99. }
  100. this.hide();
  101. return true;
  102. } else {
  103. if (id.length) {
  104. this.callback(ToolCore.assetDatabase.getAssetByGUID(id), this.args);
  105. this.hide();
  106. return true;
  107. }
  108. }
  109. return false;
  110. }
  111. }
  112. export = ResourceSelection;