TSExample.plugin.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. "use strict";
  2. var ExamplePluginUILabel = "TS Example Plugin";
  3. var ExamplePluginTBPath = "EditorData/Example.tb.txt";
  4. var InfoboxTBPath = "EditorData/Infobox.tb.txt";
  5. var CustomEditorBuilder = (function () {
  6. function CustomEditorBuilder() {
  7. }
  8. CustomEditorBuilder.prototype.canHandleResource = function (resourcePath) {
  9. return resourcePath.indexOf("custom.editor.txt") > 0;
  10. };
  11. CustomEditorBuilder.prototype.getNormalizedPath = function (path) {
  12. var RESOURCES_MARKER = "resources/";
  13. return path.substring(path.toLowerCase().indexOf(RESOURCES_MARKER));
  14. };
  15. CustomEditorBuilder.prototype.getEditor = function (resourceFrame, resourcePath, tabContainer) {
  16. var _this = this;
  17. var editorUrl = "atomic://" + ToolCore.toolSystem.project.resourcePath + "EditorData/customEditor.html";
  18. console.log(editorUrl);
  19. var editor = new Editor.JSResourceEditor(resourcePath, tabContainer, editorUrl);
  20. editor.subscribeToEvent("WebViewLoadEnd", function (data) {
  21. editor.unsubscribeFromEvent("WebViewLoadEnd");
  22. var webClient = editor.webView.webClient;
  23. webClient.executeJavaScript("HOST_loadCode(\"atomic://" + _this.getNormalizedPath(editor.fullPath) + "\");");
  24. });
  25. editor.subscribeToEvent("DeleteResourceNotification", function (data) {
  26. var webClient = editor.webView.webClient;
  27. webClient.executeJavaScript("HOST_resourceDeleted(\"atomic://" + _this.getNormalizedPath(data.path) + "\");");
  28. });
  29. editor.subscribeToEvent("UserPreferencesChangedNotification", function (data) {
  30. var prefsPath = ToolCore.toolSystem.project.userPrefsFullPath;
  31. if (Atomic.fileSystem.fileExists(prefsPath)) {
  32. var webClient = editor.webView.webClient;
  33. webClient.executeJavaScript("HOST_loadPreferences(\"atomic://" + prefsPath + "\");");
  34. }
  35. });
  36. return editor;
  37. };
  38. return CustomEditorBuilder;
  39. }());
  40. var customEditorBuilder = new CustomEditorBuilder();
  41. var TSExamplePluginService = (function () {
  42. function TSExamplePluginService() {
  43. var _this = this;
  44. this.name = "TSExampleService";
  45. this.description = "This service demonstrates plugin functionality functionality.";
  46. this.serviceLocator = null;
  47. this.extensionWindow = null;
  48. this.lastObjectName = null;
  49. this.totalUses = 0;
  50. this.handleWidgetEvent = function (ev) {
  51. if (!_this.extensionWindow) {
  52. return;
  53. }
  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.serviceLocator.projectServices.setUserPreference(_this.name, "UsageCount", ++_this.totalUses);
  62. _this.helloLabel.text = "Hello " + _this.nameField.text + ", This was used " + _this.totalUses + " times.";
  63. return true;
  64. }
  65. }
  66. return false;
  67. };
  68. }
  69. TSExamplePluginService.prototype.initialize = function (serviceLoader) {
  70. Atomic.print("TSExamplePluginService.initialize");
  71. this.serviceLocator = (serviceLoader);
  72. if (this.serviceLocator) {
  73. this.serviceLocator.projectServices.register(this);
  74. this.serviceLocator.uiServices.register(this);
  75. }
  76. };
  77. TSExamplePluginService.prototype.projectUnloaded = function () {
  78. this.serviceLocator.uiServices.removeProjectContextMenuItemSource(ExamplePluginUILabel);
  79. this.serviceLocator.uiServices.removeHierarchyContextMenuItemSource(ExamplePluginUILabel);
  80. this.serviceLocator.uiServices.removePluginMenuItemSource(ExamplePluginUILabel);
  81. this.serviceLocator.uiServices.unregisterCustomEditor(customEditorBuilder);
  82. Atomic.print("TSExamplePluginService.projectUnloaded");
  83. if (this.serviceLocator) {
  84. this.serviceLocator.projectServices.unregister(this);
  85. this.serviceLocator.uiServices.unregister(this);
  86. }
  87. };
  88. TSExamplePluginService.prototype.projectLoaded = function (ev) {
  89. Atomic.print("TSExamplePluginService.projectLoaded");
  90. this.serviceLocator.uiServices.createPluginMenuItemSource(ExamplePluginUILabel, { "Open": ["tsexampleplugin open"] });
  91. this.serviceLocator.uiServices.createHierarchyContextMenuItemSource(ExamplePluginUILabel, { "Get name": ["tsexampleplugin hierarchy context"] });
  92. this.serviceLocator.uiServices.createProjectContextMenuItemSource(ExamplePluginUILabel, { "Get name": ["tsexampleplugin project context"] });
  93. this.totalUses = this.serviceLocator.projectServices.getUserPreference(this.name, "UsageCount", 0);
  94. this.serviceLocator.uiServices.registerCustomEditor(customEditorBuilder);
  95. };
  96. TSExamplePluginService.prototype.playerStarted = function () {
  97. Atomic.print("TSExamplePluginService.playerStarted");
  98. };
  99. TSExamplePluginService.prototype.menuItemClicked = function (refId) {
  100. Atomic.print("TSExamplePluginService.menuItemClicked: " + refId);
  101. if (refId == "tsexampleplugin open") {
  102. this.extensionWindow = this.serviceLocator.uiServices.showModalWindow(ExamplePluginUILabel, ExamplePluginTBPath, this.handleWidgetEvent);
  103. this.getWidgets();
  104. return true;
  105. }
  106. return false;
  107. };
  108. TSExamplePluginService.prototype.hierarchyContextItemClicked = function (node, refid) {
  109. Atomic.print("TSExamplePluginService.hierarchyContextItemClicked: " + node.name + " " + refid);
  110. if (refid == "tsexampleplugin hierarchy context") {
  111. this.lastObjectName = "node " + node.name;
  112. this.showInfobox("Hierarchy Item Selected ", "Node: '" + node.name + "' was selected.");
  113. return true;
  114. }
  115. return false;
  116. };
  117. TSExamplePluginService.prototype.projectContextItemClicked = function (asset, refid) {
  118. Atomic.print("TSExamplePluginService.projectContextItemClicked: " + asset.name + " " + refid);
  119. if (refid == "tsexampleplugin project context") {
  120. this.lastObjectName = "asset " + asset.name;
  121. this.showInfobox("Project Asset Selected", "Asset: '" + asset.name + "' was selected.");
  122. return true;
  123. }
  124. return false;
  125. };
  126. TSExamplePluginService.prototype.getWidgets = function () {
  127. if (!this.extensionWindow) {
  128. return;
  129. }
  130. this.helloLabel = this.extensionWindow.getWidget("example_hello");
  131. this.nameField = this.extensionWindow.getWidget("example_name");
  132. if (this.lastObjectName) {
  133. this.nameField.text = this.lastObjectName;
  134. this.lastObjectName = null;
  135. }
  136. };
  137. TSExamplePluginService.prototype.showInfobox = function (title, msg) {
  138. var infobox = this.serviceLocator.uiServices.showModalWindow(title, InfoboxTBPath, function (ev) {
  139. if (ev.type == Atomic.UI_EVENT_TYPE_CLICK && ev.target.id == "infobox_ok") {
  140. infobox.hide();
  141. return true;
  142. }
  143. });
  144. var msgLabel = infobox.getWidget("infobox_msg");
  145. msgLabel.text = msg;
  146. };
  147. return TSExamplePluginService;
  148. }());
  149. Object.defineProperty(exports, "__esModule", { value: true });
  150. exports.default = new TSExamplePluginService();