ResourceSelection.ts 6.2 KB

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