TSExample.plugin.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. const ExamplePluginUILabel = "TS Example Plugin";
  2. const ExamplePluginTBPath = "EditorData/Example.tb.txt";
  3. const InfoboxTBPath = "EditorData/Infobox.tb.txt";
  4. class CustomEditorBuilder implements Editor.Extensions.ResourceEditorBuilder {
  5. /**
  6. * Returns true if this builder can generate an editor for this resource type
  7. */
  8. canHandleResource(resourcePath: string) {
  9. return resourcePath.indexOf("custom.editor.json") > 0;
  10. }
  11. /**
  12. * Full path is the fully qualified path from the root of the filesystem. In order to take advantage
  13. * of the resource caching system, let's trim it down to just the path inside the resources directory
  14. * including the Resources directory so that the casing is correct
  15. */
  16. private getNormalizedPath(path: string) {
  17. const RESOURCES_MARKER = "resources/";
  18. return path.substring(path.toLowerCase().indexOf(RESOURCES_MARKER));
  19. }
  20. /**
  21. * Generates a resource editor for the provided resource type
  22. * @param resourcePath
  23. * @param tabContainer
  24. */
  25. getEditor(resourceFrame: Atomic.UIWidget, resourcePath: string, tabContainer: Atomic.UITabContainer) : Editor.ResourceEditor {
  26. // point to a custom page
  27. const editorUrl = "atomic://" + ToolCore.toolSystem.project.resourcePath + "EditorData/customEditor.html";
  28. const editor = new Editor.JSResourceEditor(resourcePath, tabContainer, editorUrl);
  29. // one time subscriptions waiting for the web view to finish loading. This event
  30. // actually hits the editor instance before we can hook it, so listen to it on the
  31. // frame and then unhook it
  32. editor.subscribeToEvent(WebView.WebViewLoadEndEvent((data) => {
  33. editor.unsubscribeFromEvent(WebView.WebViewLoadEndEventType);
  34. const webClient = editor.webView.webClient;
  35. webClient.executeJavaScript(`HOST_loadCode("atomic://${this.getNormalizedPath(editor.fullPath)}");`);
  36. }));
  37. editor.subscribeToEvent(Editor.EditorDeleteResourceNotificationEvent((data) => {
  38. const webClient = editor.webView.webClient;
  39. webClient.executeJavaScript(`HOST_resourceDeleted("atomic://${this.getNormalizedPath(data.path)}");`);
  40. }));
  41. editor.subscribeToEvent(Editor.UserPreferencesChangedNotificationEvent((data) => {
  42. const webClient = editor.webView.webClient;
  43. webClient.executeJavaScript("HOST_preferencesChanged();");
  44. }));
  45. return editor;
  46. }
  47. }
  48. const customEditorBuilder = new CustomEditorBuilder();
  49. class TSExamplePluginService implements Editor.HostExtensions.HostEditorService, Editor.HostExtensions.ProjectServicesEventListener, Editor.HostExtensions.UIServicesEventListener {
  50. name: string = "TSExampleService";
  51. description: string = "This service demonstrates plugin functionality functionality.";
  52. private serviceLocator: Editor.HostExtensions.HostServiceLocator = null;
  53. private extensionWindow: Editor.Modal.ExtensionWindow = null;
  54. private helloLabel: Atomic.UITextField;
  55. private nameField: Atomic.UIEditField;
  56. private lastObjectName: string = null;
  57. private totalUses = 0;
  58. initialize(serviceLoader: Editor.HostExtensions.HostServiceLocator) {
  59. Atomic.print("TSExamplePluginService.initialize");
  60. this.serviceLocator = (serviceLoader);
  61. if (this.serviceLocator) {
  62. this.serviceLocator.projectServices.register(this);
  63. this.serviceLocator.uiServices.register(this);
  64. }
  65. }
  66. projectUnloaded() {
  67. this.serviceLocator.uiServices.removeProjectContextMenuItemSource(ExamplePluginUILabel);
  68. this.serviceLocator.uiServices.removeHierarchyContextMenuItemSource(ExamplePluginUILabel);
  69. this.serviceLocator.uiServices.removePluginMenuItemSource(ExamplePluginUILabel);
  70. this.serviceLocator.uiServices.unregisterCustomEditor(customEditorBuilder);
  71. Atomic.print("TSExamplePluginService.projectUnloaded");
  72. if (this.serviceLocator) {
  73. this.serviceLocator.projectServices.unregister(this);
  74. this.serviceLocator.uiServices.unregister(this);
  75. }
  76. }
  77. projectLoaded(ev: Editor.EditorLoadProjectEvent) {
  78. Atomic.print("TSExamplePluginService.projectLoaded");
  79. this.serviceLocator.uiServices.createPluginMenuItemSource(ExamplePluginUILabel, { "Open" : ["tsexampleplugin open"] });
  80. this.serviceLocator.uiServices.createHierarchyContextMenuItemSource(ExamplePluginUILabel, { "Get name" : ["tsexampleplugin hierarchy context"]});
  81. this.serviceLocator.uiServices.createProjectContextMenuItemSource(ExamplePluginUILabel, { "Get name" : ["tsexampleplugin project context"]});
  82. this.totalUses = this.serviceLocator.projectServices.getUserPreference(this.name, "UsageCount", 0);
  83. this.serviceLocator.uiServices.registerCustomEditor(customEditorBuilder);
  84. }
  85. playerStarted() {
  86. Atomic.print("TSExamplePluginService.playerStarted");
  87. }
  88. menuItemClicked(refId: string): boolean {
  89. Atomic.print("TSExamplePluginService.menuItemClicked: " + refId);
  90. if (refId == "tsexampleplugin open") {
  91. this.extensionWindow = this.serviceLocator.uiServices.showModalWindow(
  92. ExamplePluginUILabel, ExamplePluginTBPath, this.handleWidgetEvent);
  93. this.getWidgets();
  94. return true;
  95. }
  96. return false;
  97. }
  98. hierarchyContextItemClicked(node: Atomic.Node, refid: string): boolean {
  99. Atomic.print("TSExamplePluginService.hierarchyContextItemClicked: " + node.name + " " + refid);
  100. if (refid == "tsexampleplugin hierarchy context") {
  101. this.lastObjectName = "node " + node.name;
  102. this.showInfobox("Hierarchy Item Selected ", `Node: '${node.name}' was selected.`);
  103. return true;
  104. }
  105. return false;
  106. }
  107. projectContextItemClicked(asset: ToolCore.Asset, refid: string): boolean {
  108. Atomic.print("TSExamplePluginService.projectContextItemClicked: " + asset.name + " " + refid);
  109. if (refid == "tsexampleplugin project context") {
  110. this.lastObjectName = "asset " + asset.name;
  111. this.showInfobox("Project Asset Selected", `Asset: '${asset.name}' was selected.`);
  112. return true;
  113. }
  114. return false;
  115. }
  116. getWidgets() {
  117. if (!this.extensionWindow) {
  118. return;
  119. }
  120. this.helloLabel = <Atomic.UITextField>this.extensionWindow.getWidget("example_hello");
  121. this.nameField = <Atomic.UIEditField>this.extensionWindow.getWidget("example_name");
  122. if (this.lastObjectName) {
  123. this.nameField.text = this.lastObjectName;
  124. this.lastObjectName = null;
  125. }
  126. }
  127. showInfobox(title: string, msg: string) {
  128. const infobox = this.serviceLocator.uiServices.showModalWindow(
  129. title, InfoboxTBPath, (ev: Atomic.UIWidgetEvent) => {
  130. if (ev.type == Atomic.UI_EVENT_TYPE.UI_EVENT_TYPE_CLICK && ev.target.id == "infobox_ok") {
  131. infobox.hide();
  132. return true;
  133. }
  134. });
  135. const msgLabel = <Atomic.UITextField>infobox.getWidget("infobox_msg");
  136. msgLabel.text = msg;
  137. }
  138. handleWidgetEvent = (ev: Atomic.UIWidgetEvent): boolean => { // => notation used to bind "this" to the method
  139. if (!this.extensionWindow) {
  140. return;
  141. }
  142. if (ev.type == Atomic.UI_EVENT_TYPE.UI_EVENT_TYPE_CLICK) {
  143. if (ev.target.id == "example_cancel") {
  144. this.extensionWindow.hide();
  145. this.extensionWindow = null;
  146. return true;
  147. }
  148. if (ev.target.id == "example_speak") {
  149. this.serviceLocator.projectServices.setUserPreference(this.name, "UsageCount", ++this.totalUses);
  150. this.helloLabel.text = `Hello ${this.nameField.text}, This was used ${this.totalUses} times.`;
  151. return true;
  152. }
  153. }
  154. return false;
  155. };
  156. }
  157. export default new TSExamplePluginService();