JSExample.plugin.js 7.0 KB

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