|
- using System;
- using System.Collections.Generic;
- using CIGEnums;
- using SUISS.Core;
- using UnityEngine;
-
- namespace SUISSEngine
- {
- public class GridTileIconManager : MonoBehaviour
- {
- private void Start()
- {
- SingletonMonobehaviour<OverlayManager>.Instance.InteractionEnabledChangedEvent += this.OnInteractionEnabledChanged;
- }
-
- private void OnDestroy()
- {
- if (SingletonMonobehaviour<OverlayManager>.IsAvailable)
- {
- SingletonMonobehaviour<OverlayManager>.Instance.InteractionEnabledChangedEvent -= this.OnInteractionEnabledChanged;
- }
- }
-
- public T SetIcon<T>(GridTileIconType iconType) where T : GridTileIcon
- {
- if (this._icons.ContainsKey(iconType))
- {
- return (T)((object)this._icons[iconType]);
- }
- GridTileIcon asset = SingletonMonobehaviour<GridTileIconAssetCollection>.Instance.GetAsset(iconType);
- GridTileIcon gridTileIcon = SingletonMonobehaviour<OverlayManager>.Instance.CreateOverlay<GridTileIcon>(base.gameObject, asset.gameObject);
- gridTileIcon.GridTileIconDestroyedEvent += this.OnGridTileIconDestroyed;
- this._icons.Add(iconType, gridTileIcon);
- this.RefreshCurrentIcon();
- return (T)((object)gridTileIcon);
- }
-
- public void RemoveIcon(GridTileIconType iconType)
- {
- GridTileIcon gridTileIcon;
- if (this._icons.TryGetValue(iconType, out gridTileIcon))
- {
- gridTileIcon.Remove();
- this._icons.Remove(iconType);
- this.RefreshCurrentIcon();
- }
- }
-
- public void HideIcon()
- {
- if (this._currentIcon != null)
- {
- this._currentIcon.Hide();
- }
- }
-
- public void ShowIcon()
- {
- if (this._currentIcon != null && !SingletonMonobehaviour<OverlayManager>.Instance.InteractionDisabled)
- {
- this._currentIcon.Show();
- }
- }
-
- private void RefreshCurrentIcon()
- {
- GridTileIcon gridTileIcon = null;
- foreach (KeyValuePair<GridTileIconType, GridTileIcon> keyValuePair in this._icons)
- {
- if (gridTileIcon == null || keyValuePair.Value.Priority > gridTileIcon.Priority)
- {
- gridTileIcon = keyValuePair.Value;
- }
- keyValuePair.Value.Hide();
- }
- this._currentIcon = gridTileIcon;
- this.ShowIcon();
- }
-
- private void OnInteractionEnabledChanged(bool interactionEnabled)
- {
- if (interactionEnabled)
- {
- this.ShowIcon();
- }
- else
- {
- this.HideIcon();
- }
- }
-
- private void OnGridTileIconDestroyed(GridTileIcon icon)
- {
- foreach (KeyValuePair<GridTileIconType, GridTileIcon> keyValuePair in this._icons)
- {
- if (keyValuePair.Value == icon)
- {
- this._icons.Remove(keyValuePair.Key);
- break;
- }
- }
- icon.GridTileIconDestroyedEvent -= this.OnGridTileIconDestroyed;
- }
-
- private readonly Dictionary<GridTileIconType, GridTileIcon> _icons = new Dictionary<GridTileIconType, GridTileIcon>();
-
- private GridTileIcon _currentIcon;
- }
- }
|