TSExample.plugin.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /// <reference path="../../typings/Atomic/AtomicWork.d.ts" />
  2. /// <reference path="../../typings/Atomic/EditorWork.d.ts" />
  3. const ExamplePluginUILabel = "TS Example Plugin";
  4. const ExamplePluginTBPath = "EditorData/Example.tb.txt";
  5. const InfoboxTBPath = "EditorData/Infobox.tb.txt";
  6. class TSExamplePluginService implements Editor.HostExtensions.HostEditorService, Editor.HostExtensions.ProjectServicesEventListener, Editor.HostExtensions.UIServicesEventListener {
  7. name: string = "TSExampleService";
  8. description: string = "This service demonstrates plugin functionality functionality.";
  9. private serviceLocator: Editor.HostExtensions.HostServiceLocator = null;
  10. private extensionWindow: Editor.Modal.ExtensionWindow = null;
  11. private helloLabel: Atomic.UITextField;
  12. private nameField: Atomic.UIEditField;
  13. private lastObjectName: string = null;
  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. }
  38. playerStarted() {
  39. Atomic.print("TSExamplePluginService.playerStarted");
  40. }
  41. menuItemClicked(refId: string): boolean {
  42. Atomic.print("TSExamplePluginService.menuItemClicked: " + refId);
  43. if (refId == "tsexampleplugin open") {
  44. this.extensionWindow = this.serviceLocator.uiServices.showModalWindow(
  45. ExamplePluginUILabel, ExamplePluginTBPath, this.handleWidgetEvent);
  46. this.getWidgets();
  47. return true;
  48. }
  49. return false;
  50. }
  51. hierarchyContextItemClicked(node: Atomic.Node, refid: string): boolean {
  52. Atomic.print("TSExamplePluginService.hierarchyContextItemClicked: " + node.name + " " + refid);
  53. if (refid == "tsexampleplugin hierarchy context") {
  54. this.lastObjectName = "node " + node.name;
  55. this.showInfobox("Hierarchy Item Selected ", `Node: '${node.name}' was selected.`);
  56. return true;
  57. }
  58. return false;
  59. }
  60. projectContextItemClicked(asset: ToolCore.Asset, refid: string): boolean {
  61. Atomic.print("TSExamplePluginService.projectContextItemClicked: " + asset.name + " " + refid);
  62. if (refid == "tsexampleplugin project context") {
  63. this.lastObjectName = "asset " + asset.name;
  64. this.showInfobox("Project Asset Selected", `Asset: '${asset.name}' was selected.`);
  65. return true;
  66. }
  67. return false;
  68. }
  69. getWidgets() {
  70. if (!this.extensionWindow) {
  71. return;
  72. }
  73. this.helloLabel = <Atomic.UITextField>this.extensionWindow.getWidget("example_hello");
  74. this.nameField = <Atomic.UIEditField>this.extensionWindow.getWidget("example_name");
  75. if (this.lastObjectName) {
  76. this.nameField.text = this.lastObjectName;
  77. this.lastObjectName = null;
  78. }
  79. }
  80. showInfobox(title: string, msg: string) {
  81. const infobox = this.serviceLocator.uiServices.showModalWindow(
  82. title, InfoboxTBPath, (ev: Atomic.UIWidgetEvent) => {
  83. if (ev.type == Atomic.UI_EVENT_TYPE_CLICK && ev.target.id == "infobox_ok") {
  84. infobox.hide();
  85. return true;
  86. }
  87. });
  88. const msgLabel = <Atomic.UITextField>infobox.getWidget("infobox_msg");
  89. msgLabel.text = msg;
  90. }
  91. handleWidgetEvent = (ev: Atomic.UIWidgetEvent): boolean => { // => notation used to bind "this" to the method
  92. if (!this.extensionWindow) {
  93. return;
  94. }
  95. if (ev.type == Atomic.UI_EVENT_TYPE_CLICK) {
  96. if (ev.target.id == "example_cancel") {
  97. this.extensionWindow.hide();
  98. this.extensionWindow = null;
  99. return true;
  100. }
  101. if (ev.target.id == "example_speak") {
  102. this.helloLabel.text = "Hello " + this.nameField.text;
  103. return true;
  104. }
  105. }
  106. return false;
  107. };
  108. }
  109. export default new TSExamplePluginService();