ServiceLocator.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. import * as HostExtensionServices from "./HostExtensionServices";
  8. import * as EditorUI from "../ui/EditorUI";
  9. import TypescriptLanguageExtension from "./languageExtensions/TypscriptLanguageExtension";
  10. /**
  11. * Generic service locator of editor services that may be injected by either a plugin
  12. * or by the editor itself.
  13. */
  14. export class ServiceLocatorType implements Editor.HostExtensions.HostServiceLocator {
  15. constructor() {
  16. this.resourceServices = new HostExtensionServices.ResourceServiceRegistry();
  17. this.projectServices = new HostExtensionServices.ProjectServiceRegistry();
  18. }
  19. private eventDispatcher: Atomic.UIWidget = null;
  20. resourceServices: HostExtensionServices.ResourceServiceRegistry;
  21. projectServices: HostExtensionServices.ProjectServiceRegistry;
  22. loadService(service: Editor.HostExtensions.HostEditorService) {
  23. try {
  24. service.initialize(this);
  25. } catch (e) {
  26. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}\n \n ${e.stack}`);
  27. }
  28. }
  29. /**
  30. * This is where the top level window will allow the service locator to listen for events and act on them.
  31. * @param {Atomic.UIWidget} frame
  32. */
  33. subscribeToEvents(frame: Atomic.UIWidget) {
  34. this.eventDispatcher = frame;
  35. this.resourceServices.subscribeToEvents(this);
  36. this.projectServices.subscribeToEvents(this);
  37. }
  38. /**
  39. * Send a custom event. This can be used by services to publish custom events
  40. * @param {string} eventType
  41. * @param {any} data
  42. */
  43. sendEvent(eventType: string, data: any) {
  44. if (this.eventDispatcher) {
  45. this.eventDispatcher.sendEvent(eventType, data);
  46. }
  47. }
  48. /**
  49. * Subscribe to an event and provide a callback. This can be used by services to subscribe to custom events
  50. * @param {string} eventType
  51. * @param {any} callback
  52. */
  53. subscribeToEvent(eventType: string, callback: (data: any) => void) {
  54. if (this.eventDispatcher) {
  55. this.eventDispatcher.subscribeToEvent(eventType, callback);
  56. }
  57. }
  58. }
  59. // Singleton service locator that can be referenced
  60. const serviceLocator = new ServiceLocatorType();
  61. export default serviceLocator;
  62. // Load up all the internal services
  63. serviceLocator.loadService(new TypescriptLanguageExtension());