TSExample.plugin.js 7.4 KB

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