TSExample.plugin.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. /**
  9. * Returns true if this builder can generate an editor for this resource type
  10. */
  11. CustomEditorBuilder.prototype.canHandleResource = function (resourcePath) {
  12. return resourcePath.indexOf("custom.editor.json") > 0;
  13. };
  14. /**
  15. * Full path is the fully qualified path from the root of the filesystem. In order to take advantage
  16. * of the resource caching system, let's trim it down to just the path inside the resources directory
  17. * including the Resources directory so that the casing is correct
  18. */
  19. CustomEditorBuilder.prototype.getNormalizedPath = function (path) {
  20. var RESOURCES_MARKER = "resources/";
  21. return path.substring(path.toLowerCase().indexOf(RESOURCES_MARKER));
  22. };
  23. /**
  24. * Generates a resource editor for the provided resource type
  25. * @param resourcePath
  26. * @param tabContainer
  27. */
  28. CustomEditorBuilder.prototype.getEditor = function (resourceFrame, resourcePath, tabContainer) {
  29. var _this = this;
  30. // point to a custom page
  31. var editorUrl = "atomic://" + ToolCore.toolSystem.project.resourcePath + "EditorData/customEditor.html";
  32. var editor = new Editor.JSResourceEditor(resourcePath, tabContainer, editorUrl);
  33. // one time subscriptions waiting for the web view to finish loading. This event
  34. // actually hits the editor instance before we can hook it, so listen to it on the
  35. // frame and then unhook it
  36. editor.subscribeToEvent(WebView.WebViewLoadEndEvent(function (data) {
  37. editor.unsubscribeFromEvent(WebView.WebViewLoadEndEventType);
  38. var webClient = editor.webView.webClient;
  39. webClient.executeJavaScript("HOST_loadCode(\"atomic://" + _this.getNormalizedPath(editor.fullPath) + "\");");
  40. }));
  41. editor.subscribeToEvent(Editor.EditorDeleteResourceNotificationEvent(function (data) {
  42. var webClient = editor.webView.webClient;
  43. webClient.executeJavaScript("HOST_resourceDeleted(\"atomic://" + _this.getNormalizedPath(data.path) + "\");");
  44. }));
  45. editor.subscribeToEvent(Editor.UserPreferencesChangedNotificationEvent(function (data) {
  46. var webClient = editor.webView.webClient;
  47. webClient.executeJavaScript("HOST_preferencesChanged();");
  48. }));
  49. return editor;
  50. };
  51. return CustomEditorBuilder;
  52. }());
  53. var customEditorBuilder = new CustomEditorBuilder();
  54. var TSExamplePluginService = (function () {
  55. function TSExamplePluginService() {
  56. var _this = this;
  57. this.name = "TSExampleService";
  58. this.description = "This service demonstrates plugin functionality functionality.";
  59. this.serviceLocator = null;
  60. this.extensionWindow = null;
  61. this.lastObjectName = null;
  62. this.totalUses = 0;
  63. this.handleWidgetEvent = function (ev) {
  64. if (!_this.extensionWindow) {
  65. return;
  66. }
  67. if (ev.type == Atomic.UI_EVENT_TYPE.UI_EVENT_TYPE_CLICK) {
  68. if (ev.target.id == "example_cancel") {
  69. _this.extensionWindow.hide();
  70. _this.extensionWindow = null;
  71. return true;
  72. }
  73. if (ev.target.id == "example_speak") {
  74. _this.serviceLocator.projectServices.setUserPreference(_this.name, "UsageCount", ++_this.totalUses);
  75. _this.helloLabel.text = "Hello " + _this.nameField.text + ", This was used " + _this.totalUses + " times.";
  76. return true;
  77. }
  78. }
  79. return false;
  80. };
  81. }
  82. TSExamplePluginService.prototype.initialize = function (serviceLoader) {
  83. Atomic.print("TSExamplePluginService.initialize");
  84. this.serviceLocator = (serviceLoader);
  85. if (this.serviceLocator) {
  86. this.serviceLocator.projectServices.register(this);
  87. this.serviceLocator.uiServices.register(this);
  88. }
  89. };
  90. TSExamplePluginService.prototype.projectUnloaded = function () {
  91. this.serviceLocator.uiServices.removeProjectContextMenuItemSource(ExamplePluginUILabel);
  92. this.serviceLocator.uiServices.removeHierarchyContextMenuItemSource(ExamplePluginUILabel);
  93. this.serviceLocator.uiServices.removePluginMenuItemSource(ExamplePluginUILabel);
  94. this.serviceLocator.uiServices.unregisterCustomEditor(customEditorBuilder);
  95. Atomic.print("TSExamplePluginService.projectUnloaded");
  96. if (this.serviceLocator) {
  97. this.serviceLocator.projectServices.unregister(this);
  98. this.serviceLocator.uiServices.unregister(this);
  99. }
  100. };
  101. TSExamplePluginService.prototype.projectLoaded = function (ev) {
  102. Atomic.print("TSExamplePluginService.projectLoaded");
  103. this.serviceLocator.uiServices.createPluginMenuItemSource(ExamplePluginUILabel, { "Open": ["tsexampleplugin open"] });
  104. this.serviceLocator.uiServices.createHierarchyContextMenuItemSource(ExamplePluginUILabel, { "Get name": ["tsexampleplugin hierarchy context"] });
  105. this.serviceLocator.uiServices.createProjectContextMenuItemSource(ExamplePluginUILabel, { "Get name": ["tsexampleplugin project context"] });
  106. this.totalUses = this.serviceLocator.projectServices.getUserPreference(this.name, "UsageCount", 0);
  107. this.serviceLocator.uiServices.registerCustomEditor(customEditorBuilder);
  108. };
  109. TSExamplePluginService.prototype.playerStarted = function () {
  110. Atomic.print("TSExamplePluginService.playerStarted");
  111. };
  112. TSExamplePluginService.prototype.menuItemClicked = function (refId) {
  113. Atomic.print("TSExamplePluginService.menuItemClicked: " + refId);
  114. if (refId == "tsexampleplugin open") {
  115. this.extensionWindow = this.serviceLocator.uiServices.showModalWindow(ExamplePluginUILabel, ExamplePluginTBPath, this.handleWidgetEvent);
  116. this.getWidgets();
  117. return true;
  118. }
  119. return false;
  120. };
  121. TSExamplePluginService.prototype.hierarchyContextItemClicked = function (node, refid) {
  122. Atomic.print("TSExamplePluginService.hierarchyContextItemClicked: " + node.name + " " + refid);
  123. if (refid == "tsexampleplugin hierarchy context") {
  124. this.lastObjectName = "node " + node.name;
  125. this.showInfobox("Hierarchy Item Selected ", "Node: '" + node.name + "' was selected.");
  126. return true;
  127. }
  128. return false;
  129. };
  130. TSExamplePluginService.prototype.projectContextItemClicked = function (asset, refid) {
  131. Atomic.print("TSExamplePluginService.projectContextItemClicked: " + asset.name + " " + refid);
  132. if (refid == "tsexampleplugin project context") {
  133. this.lastObjectName = "asset " + asset.name;
  134. this.showInfobox("Project Asset Selected", "Asset: '" + asset.name + "' was selected.");
  135. return true;
  136. }
  137. return false;
  138. };
  139. TSExamplePluginService.prototype.getWidgets = function () {
  140. if (!this.extensionWindow) {
  141. return;
  142. }
  143. this.helloLabel = this.extensionWindow.getWidget("example_hello");
  144. this.nameField = this.extensionWindow.getWidget("example_name");
  145. if (this.lastObjectName) {
  146. this.nameField.text = this.lastObjectName;
  147. this.lastObjectName = null;
  148. }
  149. };
  150. TSExamplePluginService.prototype.showInfobox = function (title, msg) {
  151. var infobox = this.serviceLocator.uiServices.showModalWindow(title, InfoboxTBPath, function (ev) {
  152. if (ev.type == Atomic.UI_EVENT_TYPE.UI_EVENT_TYPE_CLICK && ev.target.id == "infobox_ok") {
  153. infobox.hide();
  154. return true;
  155. }
  156. });
  157. var msgLabel = infobox.getWidget("infobox_msg");
  158. msgLabel.text = msg;
  159. };
  160. return TSExamplePluginService;
  161. }());
  162. Object.defineProperty(exports, "__esModule", { value: true });
  163. exports.default = new TSExamplePluginService();