Example.plugin.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /// <reference path="../../typings/Atomic/AtomicWork.d.ts" />
  2. /// <reference path="../../typings/Atomic/EditorWork.d.ts" />
  3. const ExamplePluginUILabel = "Example Plugin";
  4. const ExamplePluginTBPath = "EditorData/Example.tb.txt";
  5. class ExamplePluginService implements Editor.HostExtensions.HostEditorService, Editor.HostExtensions.ProjectServicesEventListener, Editor.HostExtensions.UIServicesEventListener {
  6. name: string = "ExampleService";
  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. initialize(serviceLoader: Editor.HostExtensions.HostServiceLocator) {
  14. Atomic.print("ExamplePluginService.initialize");
  15. this.serviceLocator = (serviceLoader);
  16. if (this.serviceLocator) {
  17. this.serviceLocator.projectServices.register(this);
  18. this.serviceLocator.uiServices.register(this);
  19. }
  20. }
  21. projectUnloaded() {
  22. this.serviceLocator.uiServices.removeProjectContextMenuItemSource(ExamplePluginUILabel);
  23. this.serviceLocator.uiServices.removeHierarchyContextMenuItemSource(ExamplePluginUILabel);
  24. this.serviceLocator.uiServices.removePluginMenuItemSource(ExamplePluginUILabel);
  25. Atomic.print("ExamplePluginService.projectUnloaded");
  26. if (this.serviceLocator) {
  27. this.serviceLocator.projectServices.unregister(this);
  28. this.serviceLocator.uiServices.unregister(this);
  29. }
  30. }
  31. projectLoaded(ev: Editor.EditorEvents.LoadProjectEvent) {
  32. Atomic.print("ExamplePluginService.projectLoaded");
  33. this.serviceLocator.uiServices.createPluginMenuItemSource(ExamplePluginUILabel, { "Open" : ["exampleplugin open"] });
  34. this.serviceLocator.uiServices.createHierarchyContextMenuItemSource(ExamplePluginUILabel, { "Get name" : ["exampleplugin hierarchy context"]});
  35. this.serviceLocator.uiServices.createProjectContextMenuItemSource(ExamplePluginUILabel, { "Get name" : ["exampleplugin project context"]});
  36. }
  37. playerStarted() {
  38. Atomic.print("ExamplePluginService.playerStarted");
  39. }
  40. menuItemClicked(refId: string): boolean {
  41. Atomic.print("ExamplePluginService.menuItemClicked: " + refId);
  42. if (refId == "exampleplugin open") {
  43. this.extensionWindow = this.serviceLocator.uiServices.showModalWindow(
  44. ExamplePluginUILabel, ExamplePluginTBPath, this.handleWidgetEvent);
  45. this.getWidgets();
  46. return true;
  47. }
  48. return false;
  49. }
  50. hierarchyContextItemClicked(node: Atomic.Node, refid: string): boolean {
  51. Atomic.print("ExamplePluginService.hierarchyContextItemClicked: " + node.name + " " + refid);
  52. if (refid == "exampleplugin hierarchy context") {
  53. this.lastObjectName = "node " + node.name;
  54. return true;
  55. }
  56. return false;
  57. }
  58. projectContextItemClicked(asset: ToolCore.Asset, refid: string): boolean {
  59. Atomic.print("ExamplePluginService.projectContextItemClicked: " + asset.name + " " + refid);
  60. if (refid == "exampleplugin project context") {
  61. this.lastObjectName = "asset " + asset.name;
  62. return true;
  63. }
  64. return false;
  65. }
  66. getWidgets() {
  67. if (!this.extensionWindow)
  68. return;
  69. this.helloLabel = <Atomic.UITextField>this.extensionWindow.getWidget("example_hello");
  70. this.nameField = <Atomic.UIEditField>this.extensionWindow.getWidget("example_name");
  71. if (this.lastObjectName) {
  72. this.nameField.text = this.lastObjectName;
  73. this.lastObjectName = null;
  74. }
  75. }
  76. handleWidgetEvent = (ev: Atomic.UIWidgetEvent): boolean => { // => notation used to bind "this" to the method
  77. if (!this.extensionWindow)
  78. return;
  79. if (ev.type == Atomic.UI_EVENT_TYPE_CLICK) {
  80. if (ev.target.id == "example_cancel") {
  81. this.extensionWindow.hide();
  82. this.extensionWindow = null;
  83. return true;
  84. }
  85. if (ev.target.id == "example_speak") {
  86. this.helloLabel.text = "Hello " + this.nameField.text;
  87. return true;
  88. }
  89. }
  90. return false;
  91. }
  92. }
  93. const examplePluginService = new ExamplePluginService();
  94. export default examplePluginService;