You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

110 lines
2.8 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using CIGEnums;
  4. using SUISS.Core;
  5. using UnityEngine;
  6. namespace SUISSEngine
  7. {
  8. public class GridTileIconManager : MonoBehaviour
  9. {
  10. private void Start()
  11. {
  12. SingletonMonobehaviour<OverlayManager>.Instance.InteractionEnabledChangedEvent += this.OnInteractionEnabledChanged;
  13. }
  14. private void OnDestroy()
  15. {
  16. if (SingletonMonobehaviour<OverlayManager>.IsAvailable)
  17. {
  18. SingletonMonobehaviour<OverlayManager>.Instance.InteractionEnabledChangedEvent -= this.OnInteractionEnabledChanged;
  19. }
  20. }
  21. public T SetIcon<T>(GridTileIconType iconType) where T : GridTileIcon
  22. {
  23. if (this._icons.ContainsKey(iconType))
  24. {
  25. return (T)((object)this._icons[iconType]);
  26. }
  27. GridTileIcon asset = SingletonMonobehaviour<GridTileIconAssetCollection>.Instance.GetAsset(iconType);
  28. GridTileIcon gridTileIcon = SingletonMonobehaviour<OverlayManager>.Instance.CreateOverlay<GridTileIcon>(base.gameObject, asset.gameObject);
  29. gridTileIcon.GridTileIconDestroyedEvent += this.OnGridTileIconDestroyed;
  30. this._icons.Add(iconType, gridTileIcon);
  31. this.RefreshCurrentIcon();
  32. return (T)((object)gridTileIcon);
  33. }
  34. public void RemoveIcon(GridTileIconType iconType)
  35. {
  36. GridTileIcon gridTileIcon;
  37. if (this._icons.TryGetValue(iconType, out gridTileIcon))
  38. {
  39. gridTileIcon.Remove();
  40. this._icons.Remove(iconType);
  41. this.RefreshCurrentIcon();
  42. }
  43. }
  44. public void HideIcon()
  45. {
  46. if (this._currentIcon != null)
  47. {
  48. this._currentIcon.Hide();
  49. }
  50. }
  51. public void ShowIcon()
  52. {
  53. if (this._currentIcon != null && !SingletonMonobehaviour<OverlayManager>.Instance.InteractionDisabled)
  54. {
  55. this._currentIcon.Show();
  56. }
  57. }
  58. private void RefreshCurrentIcon()
  59. {
  60. GridTileIcon gridTileIcon = null;
  61. foreach (KeyValuePair<GridTileIconType, GridTileIcon> keyValuePair in this._icons)
  62. {
  63. if (gridTileIcon == null || keyValuePair.Value.Priority > gridTileIcon.Priority)
  64. {
  65. gridTileIcon = keyValuePair.Value;
  66. }
  67. keyValuePair.Value.Hide();
  68. }
  69. this._currentIcon = gridTileIcon;
  70. this.ShowIcon();
  71. }
  72. private void OnInteractionEnabledChanged(bool interactionEnabled)
  73. {
  74. if (interactionEnabled)
  75. {
  76. this.ShowIcon();
  77. }
  78. else
  79. {
  80. this.HideIcon();
  81. }
  82. }
  83. private void OnGridTileIconDestroyed(GridTileIcon icon)
  84. {
  85. foreach (KeyValuePair<GridTileIconType, GridTileIcon> keyValuePair in this._icons)
  86. {
  87. if (keyValuePair.Value == icon)
  88. {
  89. this._icons.Remove(keyValuePair.Key);
  90. break;
  91. }
  92. }
  93. icon.GridTileIconDestroyedEvent -= this.OnGridTileIconDestroyed;
  94. }
  95. private readonly Dictionary<GridTileIconType, GridTileIcon> _icons = new Dictionary<GridTileIconType, GridTileIcon>();
  96. private GridTileIcon _currentIcon;
  97. }
  98. }