Example.plugin.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. 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" : ["exampleplugin open"] });
  31. }
  32. playerStarted() {
  33. Atomic.print("ExamplePluginService.playerStarted");
  34. }
  35. menuItemClicked(refId: string): boolean {
  36. Atomic.print("ExamplePluginService.menuItemClicked: " + refId);
  37. if (refId == "exampleplugin 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. this.helloLabel = <Atomic.UITextField>this.extensionWindow.getWidget("example_hello");
  49. this.nameField = <Atomic.UIEditField>this.extensionWindow.getWidget("example_name");
  50. }
  51. handleWidgetEvent = (ev: Atomic.UIWidgetEvent): boolean => { // => notation used to bind "this" to the method
  52. if (!this.extensionWindow)
  53. return;
  54. if (ev.type == Atomic.UI_EVENT_TYPE_CLICK) {
  55. if (ev.target.id == "example_cancel") {
  56. this.extensionWindow.hide();
  57. this.extensionWindow = null;
  58. return true;
  59. }
  60. if (ev.target.id == "example_speak") {
  61. this.helloLabel.text = "Hello "+this.nameField.text;
  62. return true;
  63. }
  64. }
  65. return false;
  66. }
  67. }
  68. const examplePluginService = new ExamplePluginService();
  69. export default examplePluginService;