TSExample.plugin.ts 8.1 KB

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