JSExample.plugin.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. webClient.executeJavaScript('HOST_preferencesChanged();');
  92. });
  93. return editor;
  94. }
  95. // Definition of the plugin
  96. var JSExamplePlugin = {
  97. name: "JSExamplePlugin",
  98. description: "This service demonstrates plugin functionality"
  99. };
  100. JSExamplePlugin.initialize = function(serviceLoader) {
  101. console.log("JSExamplePluginService.initialize");
  102. serviceLocator = serviceLoader;
  103. if (serviceLocator) {
  104. serviceLocator.projectServices.register(JSExamplePlugin);
  105. serviceLocator.uiServices.register(JSExamplePlugin);
  106. }
  107. };
  108. JSExamplePlugin.projectUnloaded = function() {
  109. serviceLocator.uiServices.removeProjectContextMenuItemSource(ExamplePluginUILabel);
  110. serviceLocator.uiServices.removeHierarchyContextMenuItemSource(ExamplePluginUILabel);
  111. serviceLocator.uiServices.removePluginMenuItemSource(ExamplePluginUILabel);
  112. serviceLocator.uiServices.unregisterCustomEditor(CustomEditorBuilder);
  113. console.log("JSExamplePluginService.projectUnloaded");
  114. if (serviceLocator) {
  115. serviceLocator.projectServices.unregister(JSExamplePlugin);
  116. serviceLocator.uiServices.unregister(JSExamplePlugin);
  117. }
  118. };
  119. JSExamplePlugin.projectLoaded = function(ev) {
  120. console.log("JSExamplePluginService.projectLoaded");
  121. serviceLocator.uiServices.createPluginMenuItemSource(ExamplePluginUILabel, {
  122. "Open": ["jsexampleplugin open"]
  123. });
  124. serviceLocator.uiServices.createHierarchyContextMenuItemSource(ExamplePluginUILabel, {
  125. "Get name": ["jsexampleplugin hierarchy context"]
  126. });
  127. serviceLocator.uiServices.createProjectContextMenuItemSource(ExamplePluginUILabel, {
  128. "Get name": ["jsexampleplugin project context"]
  129. });
  130. serviceLocator.uiServices.registerCustomEditor(CustomEditorBuilder);
  131. totalUses = serviceLocator.projectServices.getUserPreference("JSExamplePlugin", "UsageCount", 0);
  132. };
  133. JSExamplePlugin.playerStarted = function() {
  134. console.log("JSExamplePluginService.playerStarted");
  135. };
  136. JSExamplePlugin.menuItemClicked = function(refId) {
  137. console.log("JSExamplePluginService.menuItemClicked: " + refId);
  138. if (refId == "jsexampleplugin open") {
  139. extensionWindow = serviceLocator.uiServices.showModalWindow(
  140. ExamplePluginUILabel, ExamplePluginTBPath, handleWidgetEvent);
  141. getWidgets();
  142. return true;
  143. }
  144. return false;
  145. };
  146. JSExamplePlugin.hierarchyContextItemClicked = function(node, refid) {
  147. console.log("JSExamplePluginService.hierarchyContextItemClicked: " + node.name + " " + refid);
  148. if (refid == "jsexampleplugin hierarchy context") {
  149. lastObjectName = "node " + node.name;
  150. showInfobox("Hierarchy Item Selected ", "Node: '" + node.name + "' was selected.");
  151. return true;
  152. }
  153. return false;
  154. }
  155. JSExamplePlugin.projectContextItemClicked = function(asset, refid) {
  156. console.log("JSExamplePluginService.projectContextItemClicked: " + asset.name + " " + refid);
  157. if (refid == "jsexampleplugin project context") {
  158. lastObjectName = "asset " + asset.name;
  159. showInfobox("Project Item Selected ", "Asset: '" + asset.name + "' was selected.");
  160. return true;
  161. }
  162. return false;
  163. }
  164. module.exports = JSExamplePlugin;