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 / EditorUtils / Gui / ConfigurableEditorWindow.cs
Size: Mime:
using System.ComponentModel;
using Fluctio.FluctioSim.Common.Configuration;
using Fluctio.FluctioSim.Common.Icons;
using UnityEditor;
using UnityEngine;

namespace Fluctio.FluctioSim.EditorUtils.Gui
{
	public abstract class ConfigurableEditorWindow : EditorWindow
	{
		
		#region Properties
		
		public virtual Vector2 MinSize => new(400, 500);
		public virtual Vector2 MaxSize => new(600, 700);
		public virtual Texture2D Icon => DefinedIcons.Logo;
		public abstract string Title { get; }
		public virtual bool RequiresConstantRepaint => false;
		public virtual EditorWindowType WindowType => EditorWindowType.AlwaysOnTop;
		[field: SerializeField] public virtual bool CanClose { get; set; } = true;
		[field: SerializeField] private bool IsInitialized { get; set; } = false;
		
		#endregion

		#region Static methods

		public static void ToggleBanner<T>(bool shouldBeOpen) where T : ConfigurableEditorWindow
		{
			if (!shouldBeOpen && !HasOpenInstances<T>())
			{
				return;
			}

			var window = GetWindow<T>();
			window.CanClose = !shouldBeOpen;
		}
		
		#endregion
		
		#region Unity callbacks
		
		protected virtual void OnDestroy()
		{
			if (!CanClose)
			{
				Instantiate(this);
			}
			else
			{
				IsInitialized = false;
			}
		}
		
		protected virtual void OnEnable()
		{
			if (!IsInitialized)
			{
				Initialize();
			}
			minSize = MinSize;
			maxSize = MaxSize;
			titleContent = new GUIContent($"{Title} | {Config.RawName}", Icon);
			ProcessWindowType();
			IsInitialized = true;
		}

		private void ProcessWindowType()
		{
			if (!IsInitialized)
			{
				// for some reason, delayed call does not work when opening window for the first time and/or on editor launch
				ProcessWindowTypeRaw();
			}
			else
			{
				// when unity reloads scripts, we need to delay call, or second window will be created
				EditorApplication.delayCall += () =>
				{
					if (this == null)
					{
						return;
					}
					ProcessWindowTypeRaw();
				};
			}
		}

		/// https://www.foundations.unity.com/patterns/windows
		private void ProcessWindowTypeRaw()
		{
			switch (WindowType)
			{
				case EditorWindowType.Dockable:
					Show();
					break;
				case EditorWindowType.Blocking:
					ShowModalUtility();
					break;
				case EditorWindowType.AlwaysOnTop:
					ShowUtility();
					break;
				case EditorWindowType.NoBorders:
					ShowPopup();
					break;
				case EditorWindowType.CloseOnLostFocus:
					ShowAuxWindow();
					break;
				default:
					throw new InvalidEnumArgumentException(
						nameof(WindowType),
						(int)WindowType,
						WindowType.GetType());
			}
		}

		protected virtual void Update()
		{
			if (RequiresConstantRepaint)
			{
				Repaint();
			}
		}

		protected virtual void Initialize() {}
		protected virtual void OnGUI() {}
		protected virtual void OnDisable() {}
		
		#endregion
		
	}
}