JSExample.plugin.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. // Definition of the plugin
  54. var JSExamplePlugin = {
  55. name: "JSExamplePlugin",
  56. description: "This service demonstrates plugin functionality"
  57. };
  58. JSExamplePlugin.initialize = function(serviceLoader) {
  59. console.log("JSExamplePluginService.initialize");
  60. serviceLocator = serviceLoader;
  61. if (serviceLocator) {
  62. serviceLocator.projectServices.register(JSExamplePlugin);
  63. serviceLocator.uiServices.register(JSExamplePlugin);
  64. }
  65. };
  66. JSExamplePlugin.projectUnloaded = function() {
  67. serviceLocator.uiServices.removeProjectContextMenuItemSource(ExamplePluginUILabel);
  68. serviceLocator.uiServices.removeHierarchyContextMenuItemSource(ExamplePluginUILabel);
  69. serviceLocator.uiServices.removePluginMenuItemSource(ExamplePluginUILabel);
  70. console.log("JSExamplePluginService.projectUnloaded");
  71. if (serviceLocator) {
  72. serviceLocator.projectServices.unregister(JSExamplePlugin);
  73. serviceLocator.uiServices.unregister(JSExamplePlugin);
  74. }
  75. };
  76. JSExamplePlugin.projectLoaded = function(ev) {
  77. console.log("JSExamplePluginService.projectLoaded");
  78. serviceLocator.uiServices.createPluginMenuItemSource(ExamplePluginUILabel, {
  79. "Open": ["jsexampleplugin open"]
  80. });
  81. serviceLocator.uiServices.createHierarchyContextMenuItemSource(ExamplePluginUILabel, {
  82. "Get name": ["jsexampleplugin hierarchy context"]
  83. });
  84. serviceLocator.uiServices.createProjectContextMenuItemSource(ExamplePluginUILabel, {
  85. "Get name": ["jsexampleplugin project context"]
  86. });
  87. totalUses = serviceLocator.projectServices.getUserPreference("JSExamplePlugin", "UsageCount", 0);
  88. };
  89. JSExamplePlugin.playerStarted = function() {
  90. console.log("JSExamplePluginService.playerStarted");
  91. };
  92. JSExamplePlugin.menuItemClicked = function(refId) {
  93. console.log("JSExamplePluginService.menuItemClicked: " + refId);
  94. if (refId == "jsexampleplugin open") {
  95. extensionWindow = serviceLocator.uiServices.showModalWindow(
  96. ExamplePluginUILabel, ExamplePluginTBPath, handleWidgetEvent);
  97. getWidgets();
  98. return true;
  99. }
  100. return false;
  101. };
  102. JSExamplePlugin.hierarchyContextItemClicked = function(node, refid) {
  103. console.log("JSExamplePluginService.hierarchyContextItemClicked: " + node.name + " " + refid);
  104. if (refid == "jsexampleplugin hierarchy context") {
  105. lastObjectName = "node " + node.name;
  106. showInfobox("Hierarchy Item Selected ", "Node: '" + node.name + "' was selected.");
  107. return true;
  108. }
  109. return false;
  110. }
  111. JSExamplePlugin.projectContextItemClicked = function(asset, refid) {
  112. console.log("JSExamplePluginService.projectContextItemClicked: " + asset.name + " " + refid);
  113. if (refid == "jsexampleplugin project context") {
  114. lastObjectName = "asset " + asset.name;
  115. showInfobox("Project Item Selected ", "Asset: '" + asset.name + "' was selected.");
  116. return true;
  117. }
  118. return false;
  119. }
  120. module.exports = JSExamplePlugin;