ResourceEditorProvider.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 JavascriptResourceEditorBuilder from "./resourceEditors/JavascriptResourceEditorBuilder";
  23. import JsonResourceEditorBuilder from "./resourceEditors/JsonResourceEditorBuilder";
  24. import Scene3dResourceEditorBuilder from "./resourceEditors/Scene3dResourceEditorBuilder";
  25. import TextFileResourceEditorBuilder from "./resourceEditors/TextFileResourceEditorBuilder";
  26. import TurboBadgerResourceEditorBuilder from "./resourceEditors/TurboBadgerResourceEditorBuilder";
  27. import TypescriptResourceEditorBuilder from "./resourceEditors/TypescriptResourceEditorBuilder";
  28. import XMLResourceEditorBuilder from "./resourceEditors/XMLResourceEditorBuilder";
  29. import ShaderResourceEditorBuilder from "./resourceEditors/ShaderResourceEditorBuilder";
  30. export default class ResourceEditorProvider {
  31. private standardEditorRegistry: Editor.Extensions.ResourceEditorBuilder[] = [];
  32. private customEditorRegistry: Editor.Extensions.ResourceEditorBuilder[] = [];
  33. private resourceFrame: Atomic.UIWidget;
  34. constructor(resourceFrame: Atomic.UIWidget) {
  35. this.resourceFrame = resourceFrame;
  36. }
  37. /**
  38. * Register an internal core editor.
  39. */
  40. registerStandardEditor(editorBuilder: Editor.Extensions.ResourceEditorBuilder) {
  41. this.standardEditorRegistry.push(editorBuilder);
  42. }
  43. /**
  44. * Register a custom editor. These editors will override editors in the standard editor list if
  45. * they both resolve the ```canHandleResource``` call.
  46. */
  47. registerCustomEditor(editorBuilder: Editor.Extensions.ResourceEditorBuilder) {
  48. this.customEditorRegistry.push(editorBuilder);
  49. }
  50. /**
  51. * Will unregister a previously registered editor builder
  52. * @param {Editor.Extensions.ResourceEditorBuilder} editorBuilder
  53. */
  54. unregisterCustomEditor(editorBuilder: Editor.Extensions.ResourceEditorBuilder) {
  55. var index = this.customEditorRegistry.indexOf(editorBuilder, 0);
  56. if (index > -1) {
  57. this.customEditorRegistry.splice(index, 1);
  58. }
  59. }
  60. /**
  61. * Returns an editor for the provided resource type or null
  62. */
  63. getEditor(resourcePath: string, tabContainer) {
  64. let editorBuilder: Editor.Extensions.ResourceEditorBuilder;
  65. this.customEditorRegistry.forEach((builder) => {
  66. if (builder.canHandleResource(resourcePath)) {
  67. editorBuilder = builder;
  68. return;
  69. }
  70. });
  71. if (!editorBuilder) {
  72. this.standardEditorRegistry.forEach((builder) => {
  73. if (builder.canHandleResource(resourcePath)) {
  74. editorBuilder = builder;
  75. return;
  76. }
  77. });
  78. }
  79. if (editorBuilder) {
  80. return editorBuilder.getEditor(this.resourceFrame, resourcePath, tabContainer);
  81. } else {
  82. return null;
  83. }
  84. }
  85. /**
  86. * Loads the built-in standard editors
  87. */
  88. loadStandardEditors() {
  89. this.registerStandardEditor(new TextFileResourceEditorBuilder());
  90. this.registerStandardEditor(new JavascriptResourceEditorBuilder());
  91. this.registerStandardEditor(new JsonResourceEditorBuilder());
  92. this.registerStandardEditor(new TypescriptResourceEditorBuilder());
  93. this.registerStandardEditor(new Scene3dResourceEditorBuilder());
  94. this.registerStandardEditor(new XMLResourceEditorBuilder());
  95. this.registerStandardEditor(new ShaderResourceEditorBuilder());
  96. // this overrides the test resource editor so need to put it in the custom bucket
  97. this.registerCustomEditor(new TurboBadgerResourceEditorBuilder());
  98. }
  99. }