TSExample.plugin.js 8.2 KB

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