JSExample.plugin.js 6.8 KB

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