HostExtensionServices.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. import * as EditorEvents from "../editor/EditorEvents";
  23. import * as EditorUI from "../ui/EditorUI";
  24. import MainFramMenu = require("../ui/frames/menus/MainFrameMenu");
  25. /**
  26. * Generic registry for storing Editor Extension Services
  27. */
  28. export class ServiceRegistry<T extends Editor.Extensions.EditorService> implements Editor.Extensions.ServiceRegistry<T> {
  29. registeredServices: T[] = [];
  30. /**
  31. * Adds a service to the registered services list for this type of service
  32. * @param {T} service the service to register
  33. */
  34. register(service: T) {
  35. this.registeredServices.push(service);
  36. }
  37. unregister(service: T) {
  38. var index = this.registeredServices.indexOf(service, 0);
  39. if (index > -1) {
  40. this.registeredServices.splice(index, 1);
  41. }
  42. }
  43. }
  44. export interface ServiceEventSubscriber {
  45. /**
  46. * Allow this service registry to subscribe to events that it is interested in
  47. * @param {Atomic.UIWidget} topLevelWindow The top level window that will be receiving these events
  48. */
  49. subscribeToEvents(topLevelWindow: Atomic.UIWidget);
  50. }
  51. /**
  52. * Registry for service extensions that are concerned about project events
  53. */
  54. export class ProjectServiceRegistry extends ServiceRegistry<Editor.HostExtensions.ProjectService> implements Editor.HostExtensions.ProjectServiceRegistry {
  55. constructor() {
  56. super();
  57. }
  58. /**
  59. * Allow this service registry to subscribe to events that it is interested in
  60. * @param {Atomic.UIWidget} topLevelWindow The top level window that will be receiving these events
  61. */
  62. subscribeToEvents(eventDispatcher: Editor.Extensions.EventDispatcher) {
  63. eventDispatcher.subscribeToEvent(EditorEvents.LoadProjectNotification, (ev) => this.projectLoaded(ev));
  64. eventDispatcher.subscribeToEvent(EditorEvents.CloseProject, (ev) => this.projectUnloaded(ev));
  65. eventDispatcher.subscribeToEvent(EditorEvents.PlayerStartRequest, () => this.playerStarted());
  66. }
  67. /**
  68. * Called when the project is unloaded
  69. * @param {[type]} data Event info from the project unloaded event
  70. */
  71. projectUnloaded(data) {
  72. // Need to use a for loop for length down to 0 because extensions *could* delete themselves from the list on projectUnloaded
  73. for (let i = this.registeredServices.length - 1; i >= 0; i--) {
  74. let service = this.registeredServices[i];
  75. // Notify services that the project has been unloaded
  76. try {
  77. if (service.projectUnloaded) {
  78. service.projectUnloaded();
  79. }
  80. } catch (e) {
  81. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}:\n${e} \n\n ${e.stack}`);
  82. }
  83. };
  84. }
  85. /**
  86. * Called when the project is loaded
  87. * @param {[type]} data Event info from the project unloaded event
  88. */
  89. projectLoaded(ev: Editor.EditorEvents.LoadProjectEvent) {
  90. // Need to use a for loop and don't cache the length because the list of services *may* change while processing. Extensions could be appended to the end
  91. for (let i = 0; i < this.registeredServices.length; i++) {
  92. let service = this.registeredServices[i];
  93. try {
  94. // Notify services that the project has just been loaded
  95. if (service.projectLoaded) {
  96. service.projectLoaded(ev);
  97. }
  98. } catch (e) {
  99. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}:\n${e}\n\n ${e.stack}`);
  100. }
  101. };
  102. }
  103. playerStarted() {
  104. this.registeredServices.forEach((service) => {
  105. try {
  106. // Notify services that the project has just been loaded
  107. if (service.playerStarted) {
  108. service.playerStarted();
  109. }
  110. } catch (e) {
  111. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}\n \n ${e.stack}`);
  112. }
  113. });
  114. }
  115. }
  116. /**
  117. * Registry for service extensions that are concerned about Resources
  118. */
  119. export class ResourceServiceRegistry extends ServiceRegistry<Editor.HostExtensions.ResourceService> implements Editor.HostExtensions.ResourceServiceRegistry {
  120. constructor() {
  121. super();
  122. }
  123. /**
  124. * Allow this service registry to subscribe to events that it is interested in
  125. * @param {Atomic.UIWidget} topLevelWindow The top level window that will be receiving these events
  126. */
  127. subscribeToEvents(eventDispatcher: Editor.Extensions.EventDispatcher) {
  128. eventDispatcher.subscribeToEvent(EditorEvents.SaveResourceNotification, (ev) => this.saveResource(ev));
  129. eventDispatcher.subscribeToEvent(EditorEvents.DeleteResourceNotification, (ev) => this.deleteResource(ev));
  130. eventDispatcher.subscribeToEvent(EditorEvents.RenameResourceNotification, (ev) => this.renameResource(ev));
  131. }
  132. /**
  133. * Called after a resource has been saved
  134. * @param {Editor.EditorEvents.SaveResourceEvent} ev
  135. */
  136. saveResource(ev: Editor.EditorEvents.SaveResourceEvent) {
  137. // run through and find any services that can handle this.
  138. this.registeredServices.forEach((service) => {
  139. try {
  140. // Verify that the service contains the appropriate methods and that it can save
  141. if (service.save) {
  142. service.save(ev);
  143. }
  144. } catch (e) {
  145. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}:\n${e}\n\n ${e.stack}`);
  146. }
  147. });
  148. }
  149. /**
  150. * Called when a resource has been deleted
  151. */
  152. deleteResource(ev: Editor.EditorEvents.DeleteResourceEvent) {
  153. this.registeredServices.forEach((service) => {
  154. try {
  155. // Verify that the service contains the appropriate methods and that it can delete
  156. if (service.delete) {
  157. service.delete(ev);
  158. }
  159. } catch (e) {
  160. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}:\n${e}\n\n ${e.stack}`);
  161. }
  162. });
  163. }
  164. /**
  165. * Called when a resource has been renamed
  166. * @param {Editor.EditorEvents.RenameResourceEvent} ev
  167. */
  168. renameResource(ev: Editor.EditorEvents.RenameResourceEvent) {
  169. this.registeredServices.forEach((service) => {
  170. try {
  171. // Verify that the service contains the appropriate methods and that it can handle the rename
  172. if (service.rename) {
  173. service.rename(ev);
  174. }
  175. } catch (e) {
  176. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}:\n${e}\n\n ${e.stack}`);
  177. }
  178. });
  179. }
  180. }
  181. /**
  182. * Registry for service extensions that are concerned about and need access to parts of the editor user interface
  183. * Note: we may want to move this out into it's own file since it has a bunch of editor dependencies
  184. */
  185. export class UIServiceRegistry extends ServiceRegistry<Editor.HostExtensions.UIService> implements Editor.HostExtensions.UIServiceRegistry {
  186. constructor() {
  187. super();
  188. }
  189. private mainFrameMenu: MainFramMenu = null;
  190. setMainFrameMenu(menu: MainFramMenu) {
  191. // Only set this once
  192. if (this.mainFrameMenu == null) {
  193. this.mainFrameMenu = menu;
  194. }
  195. }
  196. /**
  197. * Adds a new menu to the plugin menu
  198. * @param {string} id
  199. * @param {any} items
  200. * @return {Atomic.UIMenuItemSource}
  201. */
  202. createPluginMenuItemSource(id: string, items: any): Atomic.UIMenuItemSource {
  203. return this.mainFrameMenu.createPluginMenuItemSource(id, items);
  204. }
  205. /**
  206. * Removes a previously added menu from the plugin menu
  207. * @param {string} id
  208. */
  209. removePluginMenuItemSource(id: string) {
  210. this.mainFrameMenu.removePluginMenuItemSource(id);
  211. }
  212. /**
  213. * Called when a menu item has been clicked
  214. * @param {string} refId
  215. * @type {boolean} return true if handled
  216. */
  217. menuItemClicked(refId: string): boolean {
  218. // run through and find any services that can handle this.
  219. let holdResult = false;
  220. this.registeredServices.forEach((service) => {
  221. try {
  222. // Verify that the service contains the appropriate methods and that it can handle it
  223. if (service.menuItemClicked) {
  224. if (service.menuItemClicked(refId)) {
  225. holdResult = true;
  226. }
  227. }
  228. } catch (e) {
  229. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}:\n${e}\n\n ${e.stack}`);
  230. }
  231. });
  232. return holdResult;
  233. }
  234. /**
  235. * Allow this service registry to subscribe to events that it is interested in
  236. * @param {Atomic.UIWidget} topLevelWindow The top level window that will be receiving these events
  237. */
  238. subscribeToEvents(eventDispatcher: Editor.Extensions.EventDispatcher) {
  239. // Placeholder
  240. //eventDispatcher.subscribeToEvent(EditorEvents.SaveResourceNotification, (ev) => this.doSomeUiMessage(ev));
  241. }
  242. /**
  243. * Called after a resource has been saved
  244. * @param {Editor.EditorEvents.SaveResourceEvent} ev
  245. */
  246. doSomeUiMessage(ev: Editor.EditorEvents.SaveResourceEvent) {
  247. // PLACEHOLDER
  248. // run through and find any services that can handle this.
  249. this.registeredServices.forEach((service) => {
  250. // try {
  251. // // Verify that the service contains the appropriate methods and that it can save
  252. // if (service.save) {
  253. // service.save(ev);
  254. // }
  255. // } catch (e) {
  256. // EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}:\n${e}\n\n ${e.stack}`);
  257. // }
  258. });
  259. }
  260. }