TSExample.plugin.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. class TSExamplePluginService implements Editor.HostExtensions.HostEditorService, Editor.HostExtensions.ProjectServicesEventListener, Editor.HostExtensions.UIServicesEventListener {
  6. name: string = "TSExampleService";
  7. description: string = "This service demonstrates plugin functionality written in TypeScript.";
  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. initialize(serviceLoader: Editor.HostExtensions.HostServiceLocator) {
  13. Atomic.print("ExamplePluginService.initialize");
  14. this.serviceLocator = (serviceLoader);
  15. if (this.serviceLocator) {
  16. this.serviceLocator.projectServices.register(this);
  17. this.serviceLocator.uiServices.register(this);
  18. }
  19. }
  20. projectUnloaded() {
  21. this.serviceLocator.uiServices.removePluginMenuItemSource(ExamplePluginUILabel);
  22. Atomic.print("ExamplePluginService.projectUnloaded");
  23. if (this.serviceLocator) {
  24. this.serviceLocator.projectServices.unregister(this);
  25. this.serviceLocator.uiServices.unregister(this);
  26. }
  27. }
  28. projectLoaded(ev: Editor.EditorEvents.LoadProjectEvent) {
  29. Atomic.print("ExamplePluginService.projectLoaded");
  30. let menu = this.serviceLocator.uiServices.createPluginMenuItemSource(ExamplePluginUILabel, { "Open" : ["tsexampleplugin open"] });
  31. }
  32. playerStarted() {
  33. Atomic.print("ExamplePluginService.playerStarted");
  34. }
  35. menuItemClicked(refId: string): boolean {
  36. Atomic.print("ExamplePluginService.menuItemClicked: " + refId);
  37. if (refId == "tsexampleplugin open") {
  38. this.extensionWindow = this.serviceLocator.uiServices.showModalWindow(
  39. ExamplePluginUILabel, ExamplePluginTBPath, this.handleWidgetEvent);
  40. this.getWidgets();
  41. return true;
  42. }
  43. return false;
  44. }
  45. getWidgets() {
  46. if (!this.extensionWindow) {
  47. return;
  48. }
  49. this.helloLabel = <Atomic.UITextField>this.extensionWindow.getWidget("example_hello");
  50. this.nameField = <Atomic.UIEditField>this.extensionWindow.getWidget("example_name");
  51. }
  52. handleWidgetEvent = (ev: Atomic.UIWidgetEvent): boolean => { // => notation used to bind "this" to the method
  53. if (!this.extensionWindow) {
  54. return;
  55. }
  56. if (ev.type == Atomic.UI_EVENT_TYPE_CLICK) {
  57. if (ev.target.id == "example_cancel") {
  58. this.extensionWindow.hide();
  59. this.extensionWindow = null;
  60. return true;
  61. }
  62. if (ev.target.id == "example_speak") {
  63. this.helloLabel.text = "Hello " + this.nameField.text;
  64. return true;
  65. }
  66. }
  67. return false;
  68. };
  69. }
  70. const examplePluginService = new TSExamplePluginService();
  71. export default examplePluginService;