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 / SettingsManagement / SettingsGUIExtension.cs
Size: Mime:
using System;
using Fluctio.FluctioSim.Common.Icons;
using Fluctio.FluctioSim.EditorUtils.EditorGeneral;
using Fluctio.FluctioSim.EditorUtils.Gui;
using Fluctio.FluctioSim.EditorUtils.SettingsManagement.Table;
using UnityEditor;
using UnityEditor.SettingsManagement;
using UnityEngine;

namespace Fluctio.FluctioSim.EditorUtils.SettingsManagement
{
	// TODO: handle searchContext correctly
	// Settings' search in Unity is broken even if you use recommended functions.
	// Since the search is broken anyway, I did not bother implementing it.
	// If Unity fixes settings' search, its better to fix these functions too.
	// Bug reports:
	// (2022) https://forum.unity.com/threads/the-search-in-the-project-settings-is-completely-useless-and-broken.1323183/
	// (2023) https://forum.unity.com/threads/the-state-of-the-project-settings-is-unacceptable.1516804/
	// (2024) https://forum.unity.com/threads/the-search-in-the-project-settings-is-unacceptable.1582377/
	public static class SettingsGUIExtension
	{
		public static string SettingsFilePicker(string label, UserSetting<string> setting, string searchContext)
		{
			if (!string.IsNullOrWhiteSpace(searchContext))
			{
				return setting.value;
			}
			
			using var horizontalScope = new EditorGUILayout.HorizontalScope();
			var path = SettingsGUILayout.SettingsTextField(label, setting, searchContext);
			if (EditorGUIExtensions.LeftClickButton(DefinedIcons.FilePicker, EditorUtil.InlineIconStyle))
			{
				path = EditorUtility.OpenFilePanel(label, "", "");
			}
			SettingsGUILayout.DoResetContextMenuForLastRect(setting);
			return path;
		}
		
		public static T SettingsDropdown<T>(GUIContent label, UserSetting<T> setting, string searchContext)
			where T : Enum
		{
			if (!string.IsNullOrWhiteSpace(searchContext))
			{
				return setting.value;
			}

			var selected = (T)EditorGUILayout.EnumPopup(label, setting.value);
			SettingsGUILayout.DoResetContextMenuForLastRect(setting);
			return selected;
		}
		
		public static void SettingsTable<TLabel, TInfo>(TableSetting<TLabel, TInfo> setting, string searchContext)
		{
			if (!string.IsNullOrWhiteSpace(searchContext))
			{
				return;
			}
			
			var tableDefinition = setting.TableDefinition;
			var tableValues = setting.value;
			
			EditorGUIExtensions.Foldout(
				tableDefinition.Title,
				tableValues.foldoutState,
				() => DrawTableBody(setting, searchContext),
				tableDefinition.IsFoldout);
			SettingsGUILayout.DoResetContextMenuForLastRect(setting);
		}

		private static void DrawTableBody<TLabel, TInfo>(TableSetting<TLabel, TInfo> setting, string searchContext)
		{
			var tableDefinition = setting.TableDefinition;
			var tableValues = setting.value;
			
			using var indent = new SettingsGUILayout.IndentedGroup();
			using (new EditorGUILayout.HorizontalScope())
			{
				tableDefinition.DrawHeader();	
			}
			foreach (var (key, rowDefinition) in tableDefinition.Rows)
			{
				// TODO: add reset context menu for separate rows
				// TODO: search for separate rows
				using var rowScope = new EditorGUILayout.HorizontalScope();
				var rowLabel = rowDefinition.Label;
				var rowInfo = setting[key];
				tableDefinition.DrawRow(rowLabel, rowInfo, searchContext);
			}
			setting.ApplyModifiedProperties();
		}
	}
}