Skip to content

Commit

Permalink
Merge branch 'development' into feat/static-analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhazen committed Sep 17, 2024
2 parents 897f5ef + 6bb7bdb commit d1022c1
Show file tree
Hide file tree
Showing 48 changed files with 1,806 additions and 222 deletions.
26 changes: 18 additions & 8 deletions Assets/Plugins/Source/Editor/ConfigEditors/ConfigEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ namespace PlayEveryWare.EpicOnlineServices.Editor
using UnityEngine.Events;
using Utility;
using Task = System.Threading.Tasks.Task;
using Config = EpicOnlineServices.Config;


/// <summary>
/// Contains implementations of IConfigEditor that are common to all
Expand All @@ -51,6 +49,11 @@ public class ConfigEditor<T> : IConfigEditor where T :
/// </summary>
protected string _labelText;

/// <summary>
/// The labels for each group.
/// </summary>
protected string[] _groupLabels;

/// <summary>
/// Event that triggers when the config editor is expanded.
/// </summary>
Expand Down Expand Up @@ -103,6 +106,7 @@ public ConfigEditor(
{
_collapsible = attribute.Collapsible;
_labelText = attribute.Label;
_groupLabels = attribute.GroupLabels;
}

_animExpanded = new(_collapsible);
Expand All @@ -124,7 +128,7 @@ public ConfigEditor(
/// </summary>
public void Expand()
{
// Don't do anything if already expanded, or if cannot expand
// Don't do anything if already expanded, or cannot expand
if (_expanded || !_collapsible)
{
return;
Expand Down Expand Up @@ -162,13 +166,11 @@ protected virtual void OnExpanded(EventArgs e)
/// <returns>A collection of config fields.</returns>
private static IOrderedEnumerable<IGrouping<int, (FieldInfo FieldInfo, ConfigFieldAttribute FieldDetails)>> GetFieldsByGroup()
{
var returnValue = typeof(T).GetFields(BindingFlags.Public | BindingFlags.Instance)
return typeof(T).GetFields(BindingFlags.Public | BindingFlags.Instance)
.Where(field => field.GetCustomAttribute<ConfigFieldAttribute>() != null)
.Select(info => (info, info.GetCustomAttribute<ConfigFieldAttribute>()))
.GroupBy(r => r.Item2.Group)
.OrderBy(group => group.Key);

return returnValue;
}

/// <summary>
Expand Down Expand Up @@ -214,11 +216,16 @@ private static float GetMaximumLabelWidth(IEnumerable<(FieldInfo, ConfigFieldAtt
/// </exception>
protected void RenderConfigFields()
{
var fieldGroups = GetFieldsByGroup();
foreach (var fieldGroup in fieldGroups)
foreach (var fieldGroup in GetFieldsByGroup())
{
float labelWidth = GetMaximumLabelWidth(fieldGroup);

// If there is a label for the field group, then display it.
if (0 >= fieldGroup.Key && _groupLabels?.Length > fieldGroup.Key)
{
GUILayout.Label(_groupLabels[fieldGroup.Key], EditorStyles.boldLabel);
}

foreach (var field in fieldGroup)
{
switch (field.FieldDetails.FieldType)
Expand All @@ -238,6 +245,9 @@ protected void RenderConfigFields()
case ConfigFieldType.Ulong:
field.FieldInfo.SetValue(config, GUIEditorUtility.RenderInputField(field.FieldDetails, (ulong)field.FieldInfo.GetValue(config), labelWidth));
break;
case ConfigFieldType.Double:
field.FieldInfo.SetValue(config, GUIEditorUtility.RenderInputField(field.FieldDetails, (double)field.FieldInfo.GetValue(config), labelWidth));
break;
case ConfigFieldType.TextList:
field.FieldInfo.SetValue(config, GUIEditorUtility.RenderInputField(field.FieldDetails, (List<string>)field.FieldInfo.GetValue(config), labelWidth));
break;
Expand Down
102 changes: 14 additions & 88 deletions Assets/Plugins/Source/Editor/Utility/GUIEditorUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,94 +96,6 @@ public static void AssigningTextField(string label, ref string value, float labe
EditorGUIUtility.labelWidth = originalLabelWidth;
}

public static void AssigningPath(string label, ref string filePath, string prompt, string directory = "", string extension = "", bool selectFolder = false, bool horizontalLayout = true, float maxButtonWidth = 100, float labelWidth = -1, string tooltip = null)
{
if (horizontalLayout)
{
EditorGUILayout.BeginHorizontal();
}

AssigningTextField(label, ref filePath, labelWidth, tooltip);

bool buttonPressed = maxButtonWidth > 0 ? GUILayout.Button("Select", GUILayout.MaxWidth(maxButtonWidth)) : GUILayout.Button("Select");

if (buttonPressed)
{
var newFilePath = selectFolder ? EditorUtility.OpenFolderPanel(prompt, "", "") : EditorUtility.OpenFilePanel(prompt, directory, extension);
if (!string.IsNullOrWhiteSpace(newFilePath))
{
filePath = newFilePath;
}
}

if (horizontalLayout)
{
EditorGUILayout.EndHorizontal();
}
}

private delegate T InputRenderDelegate<T>(string label, T value, float labelWidth, string tooltip);

public static void AssigningULongField(string label, ref ulong value, float labelWidth = -1, string tooltip = null)
{
float originalLabelWidth = EditorGUIUtility.labelWidth;
if (labelWidth >= 0)
{
EditorGUIUtility.labelWidth = labelWidth;
}

ulong newValue = value;
var newValueAsString = EditorGUILayout.TextField(CreateGUIContent(label, tooltip), value.ToString(), GUILayout.ExpandWidth(true));
if (string.IsNullOrWhiteSpace(newValueAsString))
{
newValueAsString = "0";
}

try
{
newValue = ulong.Parse(newValueAsString);
value = newValue;
}
catch (FormatException)
{
}
catch (OverflowException)
{
}

EditorGUIUtility.labelWidth = originalLabelWidth;
}

public static void AssigningUintField(string label, ref uint value, float labelWidth = -1, string tooltip = null)
{
float originalLabelWidth = EditorGUIUtility.labelWidth;
if (labelWidth >= 0)
{
EditorGUIUtility.labelWidth = labelWidth;
}

uint newValue = value;
var newValueAsString = EditorGUILayout.TextField(CreateGUIContent(label, tooltip), value.ToString(), GUILayout.ExpandWidth(true));
if (string.IsNullOrWhiteSpace(newValueAsString))
{
newValueAsString = "0";
}

try
{
newValue = uint.Parse(newValueAsString);
value = newValue;
}
catch (FormatException)
{
}
catch (OverflowException)
{
}

EditorGUIUtility.labelWidth = originalLabelWidth;
}

public static void AssigningULongToStringField(string label, ref string value, float labelWidth = -1, string tooltip = null)
{
float originalLabelWidth = EditorGUIUtility.labelWidth;
Expand Down Expand Up @@ -288,6 +200,8 @@ public static void AssigningFloatToStringField(string label, ref string value, f

#region New methods for rendering input fields

private delegate T InputRenderDelegate<T>(string label, T value, float labelWidth, string tooltip);

public static List<string> RenderInputField(ConfigFieldAttribute configFieldDetails, List<string> value,
float labelWidth, string tooltip = null)
{
Expand Down Expand Up @@ -391,6 +305,18 @@ public static string RenderInputField(FilePathFieldAttribute configFieldAttribut
return filePath;
}

public static double RenderInputField(ConfigFieldAttribute configFieldDetails, double value, float labelWidth, string tooltip = null)
{
return InputRendererWrapper(configFieldDetails.Label, value, labelWidth, tooltip,
(label, value1, width, s) =>
{
return EditorGUILayout.DoubleField(
CreateGUIContent(configFieldDetails.Label, tooltip),
value,
GUILayout.ExpandWidth(true));
});
}

public static string RenderInputField(ConfigFieldAttribute configFieldDetails, string value, float labelWidth,
string tooltip = null)
{
Expand Down
Loading

0 comments on commit d1022c1

Please sign in to comment.