ResourceEditorProvider.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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) {
  48. this.customEditorRegistry.push(editorBuilder);
  49. }
  50. /**
  51. * Returns an editor for the provided resource type or null
  52. */
  53. getEditor(resourcePath: string, tabContainer) {
  54. let editorBuilder: Editor.Extensions.ResourceEditorBuilder;
  55. this.customEditorRegistry.forEach((builder) => {
  56. if (builder.canHandleResource(resourcePath)) {
  57. editorBuilder = builder;
  58. return;
  59. }
  60. });
  61. if (!editorBuilder) {
  62. this.standardEditorRegistry.forEach((builder) => {
  63. if (builder.canHandleResource(resourcePath)) {
  64. editorBuilder = builder;
  65. return;
  66. }
  67. });
  68. }
  69. if (editorBuilder) {
  70. return editorBuilder.getEditor(this.resourceFrame, resourcePath, tabContainer);
  71. } else {
  72. return null;
  73. }
  74. }
  75. /**
  76. * Loads the built-in standard editors
  77. */
  78. loadStandardEditors() {
  79. this.registerStandardEditor(new TextFileResourceEditorBuilder());
  80. this.registerStandardEditor(new JavascriptResourceEditorBuilder());
  81. this.registerStandardEditor(new JsonResourceEditorBuilder());
  82. this.registerStandardEditor(new TypescriptResourceEditorBuilder());
  83. this.registerStandardEditor(new Scene3dResourceEditorBuilder());
  84. this.registerStandardEditor(new XMLResourceEditorBuilder());
  85. this.registerStandardEditor(new ShaderResourceEditorBuilder());
  86. // this overrides the test resource editor so need to put it in the custom bucket
  87. this.registerCustomEditor(new TurboBadgerResourceEditorBuilder());
  88. }
  89. }