TSExample.plugin.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 TSExamplePluginService implements Editor.HostExtensions.HostEditorService, Editor.HostExtensions.ProjectServicesEventListener, Editor.HostExtensions.UIServicesEventListener {
  6. name: string = "TSExampleService";
  7. description: string = "This service demonstrates plugin functionality functionality.";
  8. private serviceLocator: Editor.HostExtensions.HostServiceLocator = null;
  9. private extensionWindow: Editor.Modal.ExtensionWindow = null;
  10. private helloLabel: Atomic.UITextField;
  11. private nameField: Atomic.UIEditField;
  12. private lastObjectName: string = null;
  13. private totalUses = 0;
  14. initialize(serviceLoader: Editor.HostExtensions.HostServiceLocator) {
  15. Atomic.print("TSExamplePluginService.initialize");
  16. this.serviceLocator = (serviceLoader);
  17. if (this.serviceLocator) {
  18. this.serviceLocator.projectServices.register(this);
  19. this.serviceLocator.uiServices.register(this);
  20. }
  21. }
  22. projectUnloaded() {
  23. this.serviceLocator.uiServices.removeProjectContextMenuItemSource(ExamplePluginUILabel);
  24. this.serviceLocator.uiServices.removeHierarchyContextMenuItemSource(ExamplePluginUILabel);
  25. this.serviceLocator.uiServices.removePluginMenuItemSource(ExamplePluginUILabel);
  26. Atomic.print("TSExamplePluginService.projectUnloaded");
  27. if (this.serviceLocator) {
  28. this.serviceLocator.projectServices.unregister(this);
  29. this.serviceLocator.uiServices.unregister(this);
  30. }
  31. }
  32. projectLoaded(ev: Editor.EditorEvents.LoadProjectEvent) {
  33. Atomic.print("TSExamplePluginService.projectLoaded");
  34. this.serviceLocator.uiServices.createPluginMenuItemSource(ExamplePluginUILabel, { "Open" : ["tsexampleplugin open"] });
  35. this.serviceLocator.uiServices.createHierarchyContextMenuItemSource(ExamplePluginUILabel, { "Get name" : ["tsexampleplugin hierarchy context"]});
  36. this.serviceLocator.uiServices.createProjectContextMenuItemSource(ExamplePluginUILabel, { "Get name" : ["tsexampleplugin project context"]});
  37. this.totalUses = this.serviceLocator.projectServices.getUserPreference(this.name, "UsageCount", 0);
  38. }
  39. playerStarted() {
  40. Atomic.print("TSExamplePluginService.playerStarted");
  41. }
  42. menuItemClicked(refId: string): boolean {
  43. Atomic.print("TSExamplePluginService.menuItemClicked: " + refId);
  44. if (refId == "tsexampleplugin open") {
  45. this.extensionWindow = this.serviceLocator.uiServices.showModalWindow(
  46. ExamplePluginUILabel, ExamplePluginTBPath, this.handleWidgetEvent);
  47. this.getWidgets();
  48. return true;
  49. }
  50. return false;
  51. }
  52. hierarchyContextItemClicked(node: Atomic.Node, refid: string): boolean {
  53. Atomic.print("TSExamplePluginService.hierarchyContextItemClicked: " + node.name + " " + refid);
  54. if (refid == "tsexampleplugin hierarchy context") {
  55. this.lastObjectName = "node " + node.name;
  56. this.showInfobox("Hierarchy Item Selected ", `Node: '${node.name}' was selected.`);
  57. return true;
  58. }
  59. return false;
  60. }
  61. projectContextItemClicked(asset: ToolCore.Asset, refid: string): boolean {
  62. Atomic.print("TSExamplePluginService.projectContextItemClicked: " + asset.name + " " + refid);
  63. if (refid == "tsexampleplugin project context") {
  64. this.lastObjectName = "asset " + asset.name;
  65. this.showInfobox("Project Asset Selected", `Asset: '${asset.name}' was selected.`);
  66. return true;
  67. }
  68. return false;
  69. }
  70. getWidgets() {
  71. if (!this.extensionWindow) {
  72. return;
  73. }
  74. this.helloLabel = <Atomic.UITextField>this.extensionWindow.getWidget("example_hello");
  75. this.nameField = <Atomic.UIEditField>this.extensionWindow.getWidget("example_name");
  76. if (this.lastObjectName) {
  77. this.nameField.text = this.lastObjectName;
  78. this.lastObjectName = null;
  79. }
  80. }
  81. showInfobox(title: string, msg: string) {
  82. const infobox = this.serviceLocator.uiServices.showModalWindow(
  83. title, InfoboxTBPath, (ev: Atomic.UIWidgetEvent) => {
  84. if (ev.type == Atomic.UI_EVENT_TYPE_CLICK && ev.target.id == "infobox_ok") {
  85. infobox.hide();
  86. return true;
  87. }
  88. });
  89. const msgLabel = <Atomic.UITextField>infobox.getWidget("infobox_msg");
  90. msgLabel.text = msg;
  91. }
  92. handleWidgetEvent = (ev: Atomic.UIWidgetEvent): boolean => { // => notation used to bind "this" to the method
  93. if (!this.extensionWindow) {
  94. return;
  95. }
  96. if (ev.type == Atomic.UI_EVENT_TYPE_CLICK) {
  97. if (ev.target.id == "example_cancel") {
  98. this.extensionWindow.hide();
  99. this.extensionWindow = null;
  100. return true;
  101. }
  102. if (ev.target.id == "example_speak") {
  103. this.serviceLocator.projectServices.setUserPreference(this.name, "UsageCount", ++this.totalUses);
  104. this.helloLabel.text = `Hello ${this.nameField.text}, This was used ${this.totalUses} times.`;
  105. return true;
  106. }
  107. }
  108. return false;
  109. };
  110. }
  111. export default new TSExamplePluginService();