ResourceSelection.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. import SearchBarFiltering = require("resources/SearchBarFiltering");
  26. class ResourceSelection extends ModalWindow {
  27. uiSearchBar: SearchBarFiltering.UISearchBar = new SearchBarFiltering.UISearchBar();
  28. folderList: Atomic.UIListView;
  29. callback: (returnObject: any, args: any) => void;
  30. args: any;
  31. resourceType: string;
  32. importerType: string;
  33. searchEdit: Atomic.UIEditField;
  34. constructor(windowText: string, importerType: string, resourceType: string, callback: (asset: ToolCore.Asset, args: any) => void, args: any) {
  35. super();
  36. this.importerType = importerType;
  37. this.resourceType = resourceType;
  38. this.callback = callback;
  39. this.args = args;
  40. this.load("AtomicEditor/editor/ui/resourceselection.tb.txt");
  41. this.searchEdit = <Atomic.UIEditField>this.getWidget("filter");
  42. this.populate(importerType, resourceType, false);
  43. this.text = windowText;
  44. this.setSize(800, 600);
  45. this.center();
  46. //Activates the search as the user types
  47. this.searchEdit.subscribeToEvent(this.searchEdit, "WidgetEvent", (data) => {
  48. if (data.type == Atomic.UI_EVENT_TYPE_KEY_UP) {
  49. this.populate(this.importerType, this.resourceType, true);
  50. }
  51. });
  52. }
  53. //adjusted to delete current folderlist and replace with search list if search is activated
  54. populate(importerType: string, resourceType: string, search: boolean) {
  55. var db = ToolCore.assetDatabase;
  56. var assets = db.getAssetsByImporterType(importerType, resourceType);
  57. if (search)
  58. this.folderList.remove();
  59. //Constructor Stuff
  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.folderList.addRootItem("None", "", "");
  65. for (var i in assets) {
  66. var asset = assets[i];
  67. if (importerType == "JavascriptImporter") {
  68. if (!(<ToolCore.JavascriptImporter>asset.importer).isComponentFile())
  69. continue;
  70. }
  71. // TODO: Generalize this for other cache assets
  72. if (resourceType == "Animation") {
  73. var modelImporter = <ToolCore.ModelImporter>(asset.importer);
  74. var animations = modelImporter.getAnimations();
  75. if (animations.length) {
  76. for (var i in animations) {
  77. if (!search || this.searchEdit.text == "")
  78. this.folderList.addRootItem(animations[i].animationName + " : " + asset.name, "", animations[i].name);
  79. else if (search) {
  80. if (this.uiSearchBar.searchPopulate(this.searchEdit.text, animations[i].animationName + " : " + asset.name))
  81. this.folderList.addRootItem(animations[i].animationName + " : " + asset.name, "", animations[i].name);
  82. }
  83. }
  84. }
  85. } else {
  86. if (!search || this.searchEdit.text == "")
  87. this.folderList.addRootItem(asset.relativePath, "", asset.guid);
  88. else if (search) {
  89. if (this.uiSearchBar.searchPopulate(this.searchEdit.text, asset.relativePath))
  90. this.folderList.addRootItem(asset.relativePath, "", asset.guid);
  91. }
  92. }
  93. }
  94. }
  95. handleWidgetEvent(ev: Atomic.UIWidgetEvent) {
  96. if (ev.count >= 2) {
  97. var id = ev.target.id;
  98. if (id == this.folderList.rootList.id) {
  99. this.selectFile();
  100. }
  101. }
  102. if (ev.type == Atomic.UI_EVENT_TYPE_CLICK) {
  103. var id = ev.target.id;
  104. if (id == "select") {
  105. return this.selectFile();
  106. }
  107. if (id == "cancel") {
  108. this.hide();
  109. return true;
  110. }
  111. if (id == "cancel search") {
  112. this.searchEdit.text = "";
  113. this.populate(this.importerType, this.resourceType, true);
  114. }
  115. }
  116. }
  117. selectFile(): boolean {
  118. var id = this.folderList.selectedItemID;
  119. if (id == "") {
  120. this.sendEvent(EditorEvents.RemoveCurrentAssetAssigned);
  121. this.hide();
  122. return true;
  123. }
  124. if (this.resourceType == "Animation") {
  125. if (id.length) {
  126. this.callback(Atomic.cache.getResource("Animation", id), this.args);
  127. this.hide();
  128. return true;
  129. }
  130. this.hide();
  131. return true;
  132. } else {
  133. if (id.length) {
  134. this.callback(ToolCore.assetDatabase.getAssetByGUID(id), this.args);
  135. this.hide();
  136. return true;
  137. }
  138. }
  139. return false;
  140. }
  141. }
  142. export = ResourceSelection;