Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
ai.fluctio.fluctio-sim / EditorCore / HierarchyIcons / HierarchyIconsGetter.cs
Size: Mime:
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Fluctio.FluctioSim.Core.Components.Prefabs.Primitives.Base;
using Fluctio.FluctioSim.Utils.Extensions;
using Fluctio.FluctioSim.Utils.General;
using UnityEditor;
using UnityEngine;
using Component = UnityEngine.Component;

namespace Fluctio.FluctioSim.EditorCore.HierarchyIcons
{
	public class HierarchyIconsGetter
	{
		private GameObject GameObject { get; }
		public Texture2D GameObjectIcon { get; private set; }
		private LinkedList<Texture2D> ComponentIconsList { get; set; }
		public IEnumerable<Texture2D> ComponentIcons { get; private set; }
		public bool AreIconsOverflown { get; private set; }
		
		public HierarchyIconsGetter(GameObject gameObject)
		{
			GameObject = gameObject;
			InitializeGameObjectIcon();
			InitializeComponentIcons();
			ApplyGameObjectIconMode();
			LimitIconsAmount();
		}

		private void InitializeGameObjectIcon()
		{
			if (!HierarchyIconsSettings.GameObjectIconInfo.IsEnabled)
			{
				return;
			}
			GameObjectIcon = EditorGUIUtility.GetIconForObject(GameObject);
		}

		private void InitializeComponentIcons()
		{
			var componentIconsEnumerable =
				from component in GetComponents()
				let icon = EditorGUIUtility.GetIconForObject(component)
				where icon != null
				let iconInfo = HierarchyIconsSettings.IconsInfo[icon]
				where iconInfo is { IsEnabled: true }
				select icon;
			ComponentIconsList = componentIconsEnumerable
				.Distinct()
				.Take(HierarchyIconsSettings.IconsLimit + 1)
				.ToLinkedList();
		}

		private IEnumerable<Component> GetComponents()
		{
			var components = GameObject.GetComponents<Component>().Where(Util.NotNull);
			if (!HierarchyIconsSettings.ShowHiddenComponents)
			{
				components = components.Where(component => !component.hideFlags.HasAnyFlag(HideFlags.HideInInspector));
			}
			if (!HierarchyIconsSettings.ShowDisabledComponents)
			{
				components = components.Where(component => component is not Behaviour or Behaviour { enabled : true });
			}
			if (!HierarchyIconsSettings.ShowNoneJoints)
			{
				components = components.Where(component => component is not PrimitiveJoint or PrimitiveJoint { jointType: not JointType.None });
			}
			return components;
		}

		private void ApplyGameObjectIconMode()
		{
			if (GameObjectIcon == null || ComponentIconsList.Count == 0)
			{
				return;
			}

			if (ComponentIconsList.First.Value != GameObjectIcon)
			{
				return;
			}

			switch (HierarchyIconsSettings.GameObjectIconMode)
			{
				case GameObjectIconMode.ShowAll:
					break;
				case GameObjectIconMode.HideComponentIcon:
					ComponentIconsList.RemoveFirst();
					break;
				case GameObjectIconMode.HideGameObjectIcon:
					GameObjectIcon = null;
					break;
				default:
					throw new InvalidEnumArgumentException(
						nameof(HierarchyIconsSettings.GameObjectIconMode),
						(int)HierarchyIconsSettings.GameObjectIconMode,
						HierarchyIconsSettings.GameObjectIconMode.GetType());
			}
		}

		private void LimitIconsAmount()
		{
			var allowedComponentIconsAmount = HierarchyIconsSettings.IconsLimit;
			if (GameObjectIcon != null)
			{
				allowedComponentIconsAmount--;
			}

			if (ComponentIconsList.Count > allowedComponentIconsAmount)
			{
				allowedComponentIconsAmount--; // make room for "more components" icon
				AreIconsOverflown = true;
			}

			ComponentIcons = ComponentIconsList.Take(allowedComponentIconsAmount);
		}
	}
}