MapTextSystem.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using Content.Shared.MapText;
  2. using Robust.Client.Graphics;
  3. using Robust.Client.ResourceManagement;
  4. using Robust.Client.UserInterface;
  5. using Robust.Client.UserInterface.RichText;
  6. using Robust.Shared.Configuration;
  7. using Robust.Shared.GameStates;
  8. using Robust.Shared.Prototypes;
  9. using Robust.Shared.Utility;
  10. namespace Content.Client.MapText;
  11. /// <inheritdoc/>
  12. public sealed class MapTextSystem : SharedMapTextSystem
  13. {
  14. [Dependency] private readonly IConfigurationManager _configManager = default!;
  15. [Dependency] private readonly IUserInterfaceManager _uiManager = default!;
  16. [Dependency] private readonly SharedTransformSystem _transform = default!;
  17. [Dependency] private readonly IResourceCache _resourceCache = default!;
  18. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  19. [Dependency] private readonly IOverlayManager _overlayManager = default!;
  20. private MapTextOverlay _overlay = default!;
  21. /// <inheritdoc/>
  22. public override void Initialize()
  23. {
  24. SubscribeLocalEvent<MapTextComponent, ComponentStartup>(OnComponentStartup);
  25. SubscribeLocalEvent<MapTextComponent, ComponentHandleState>(HandleCompState);
  26. _overlay = new MapTextOverlay(_configManager, EntityManager, _uiManager, _transform, _resourceCache, _prototypeManager);
  27. _overlayManager.AddOverlay(_overlay);
  28. // TODO move font prototype to robust.shared, then use ProtoId<FontPrototype>
  29. DebugTools.Assert(_prototypeManager.HasIndex<FontPrototype>(SharedMapTextComponent.DefaultFont));
  30. }
  31. private void OnComponentStartup(Entity<MapTextComponent> ent, ref ComponentStartup args)
  32. {
  33. CacheText(ent.Comp);
  34. // TODO move font prototype to robust.shared, then use ProtoId<FontPrototype>
  35. DebugTools.Assert(_prototypeManager.HasIndex<FontPrototype>(ent.Comp.FontId));
  36. }
  37. private void HandleCompState(Entity<MapTextComponent> ent, ref ComponentHandleState args)
  38. {
  39. if (args.Current is not MapTextComponentState state)
  40. return;
  41. ent.Comp.Text = state.Text;
  42. ent.Comp.LocText = state.LocText;
  43. ent.Comp.Color = state.Color;
  44. ent.Comp.FontId = state.FontId;
  45. ent.Comp.FontSize = state.FontSize;
  46. ent.Comp.Offset = state.Offset;
  47. CacheText(ent.Comp);
  48. }
  49. private void CacheText(MapTextComponent component)
  50. {
  51. component.CachedFont = null;
  52. component.CachedText = string.IsNullOrWhiteSpace(component.Text)
  53. ? Loc.GetString(component.LocText)
  54. : component.Text;
  55. if (!_prototypeManager.TryIndex<FontPrototype>(component.FontId, out var fontPrototype))
  56. {
  57. component.CachedText = Loc.GetString("map-text-font-error");
  58. component.Color = Color.Red;
  59. if(_prototypeManager.TryIndex<FontPrototype>(SharedMapTextComponent.DefaultFont, out var @default))
  60. component.CachedFont = new VectorFont(_resourceCache.GetResource<FontResource>(@default.Path), 14);
  61. return;
  62. }
  63. var fontResource = _resourceCache.GetResource<FontResource>(fontPrototype.Path);
  64. component.CachedFont = new VectorFont(fontResource, component.FontSize);
  65. }
  66. }