ResourceEditorProvider.ts 4.8 KB

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