Skip to content

Commit d379f29

Browse files
committed
feat: Implemented the UseBuiltInNames option in the drawing classes
1 parent 8f27554 commit d379f29

6 files changed

Lines changed: 110 additions & 24 deletions

File tree

Dependencies/SolidUtilities/Editor/Helpers/EditorDrawHelper.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,5 +199,26 @@ public static string FocusedTextField(Rect rect, string text, string placeholder
199199

200200
return text;
201201
}
202+
203+
/// <summary>
204+
/// Sets <see cref="EditorGUI.showMixedValue"/> to the needed value temporarily and draws the content.
205+
/// </summary>
206+
/// <param name="showMixedValue">Whether to show mixed value.</param>
207+
/// <param name="drawAction">
208+
/// The action to draw content while <see cref="EditorGUI.showMixedValue"/> is set to
209+
/// <paramref name="showMixedValue"/>.
210+
/// </param>
211+
/// <example><code>
212+
/// EditorDrawHelper.WhileShowingMixedValue(
213+
/// _serializedTypeRef.TypeNameHasMultipleDifferentValues,
214+
/// DrawTypeSelectionControl);
215+
/// </code></example>
216+
public static void WhileShowingMixedValue(bool showMixedValue, Action drawAction)
217+
{
218+
bool valueToRestore = EditorGUI.showMixedValue;
219+
EditorGUI.showMixedValue = showMixedValue;
220+
drawAction();
221+
EditorGUI.showMixedValue = valueToRestore;
222+
}
202223
}
203224
}

Editor/Drawers/TypeDropdownDrawer.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Linq;
66
using System.Reflection;
7+
using Editor.Util;
78
using TypeDropdown;
89
using UnityEngine;
910
using Util;
@@ -75,18 +76,22 @@ private SortedSet<TypeItem> GetFilteredTypes()
7576
typeRelatedAssemblies,
7677
_attribute);
7778

78-
bool systemAssemblyIsIncluded = typeRelatedAssemblies.Any(assembly => assembly.FullName == "System");
79+
bool replaceBuiltInNames = _attribute.UseBuiltInNames && typeRelatedAssemblies
80+
.Any(assembly => assembly.GetName().Name == "mscorlib");
7981

8082
var sortedTypes = new SortedSet<TypeItem>(new TypeItemComparer());
8183

8284
for (int i = 0; i < filteredTypes.Count; i++)
8385
{
8486
var type = filteredTypes[i];
85-
if (type.FullName != null)
86-
{
87-
sortedTypes.Add(new TypeItem(type, _attribute.Grouping));
88-
}
87+
string fullTypeName = type.FullName;
88+
if (fullTypeName == null)
89+
continue;
90+
91+
if (replaceBuiltInNames)
92+
TypeNameFormatter.TryReplaceWithBuiltInName(ref fullTypeName);
8993

94+
sortedTypes.Add(new TypeItem(type, fullTypeName, _attribute.Grouping));
9095
}
9196

9297
return sortedTypes;

Editor/Drawers/TypeFieldDrawer.cs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System;
44
using System.Linq;
55
using Editor.Util;
6+
using SolidUtilities.Editor.Helpers;
67
using TypeReferences;
78
using UnityEditor;
89
using UnityEngine;
@@ -19,6 +20,7 @@ internal class TypeFieldDrawer
1920
private readonly SerializedTypeReference _serializedTypeRef;
2021
private readonly TypeDropdownDrawer _dropdownDrawer;
2122
private readonly bool _showShortName;
23+
private readonly bool _useBuiltInNames;
2224

2325
private Rect _position;
2426
private bool _triggerDropDown;
@@ -27,20 +29,21 @@ public TypeFieldDrawer(
2729
SerializedTypeReference serializedTypeRef,
2830
Rect position,
2931
TypeDropdownDrawer dropdownDrawer,
30-
bool showShortName)
32+
bool showShortName,
33+
bool useBuiltInNames)
3134
{
3235
_serializedTypeRef = serializedTypeRef;
3336
_position = position;
3437
_dropdownDrawer = dropdownDrawer;
3538
_showShortName = showShortName;
39+
_useBuiltInNames = useBuiltInNames;
3640
}
3741

3842
public void Draw()
3943
{
40-
bool valueToRestore = EditorGUI.showMixedValue;
41-
EditorGUI.showMixedValue = _serializedTypeRef.TypeNameHasMultipleDifferentValues;
42-
DrawTypeSelectionControl();
43-
EditorGUI.showMixedValue = valueToRestore;
44+
EditorDrawHelper.WhileShowingMixedValue(
45+
_serializedTypeRef.TypeNameHasMultipleDifferentValues,
46+
DrawTypeSelectionControl);
4447
}
4548

4649
private void DrawTypeSelectionControl()
@@ -101,35 +104,34 @@ private void OnKeyDown(int controlID)
101104

102105
private void DrawFieldContent(int controlID)
103106
{
104-
var fieldContent = new GUIContent(GetTypeNameForField());
107+
var typeParts = _serializedTypeRef.TypeNameAndAssembly.Split(',');
108+
string fullTypeName = typeParts[0].Trim();
109+
var fieldContent = new GUIContent(GetTypeToShow(fullTypeName));
105110
EditorStyles.popup.Draw(_position, fieldContent, controlID);
106111
}
107112

108-
private string GetTypeNameForField()
113+
private string GetTypeToShow(string typeName)
109114
{
110-
var typeParts = _serializedTypeRef.TypeNameAndAssembly.Split(',');
111-
string typeName = typeParts[0].Trim();
115+
if (_useBuiltInNames && TypeNameFormatter.TryReplaceWithBuiltInName(ref typeName, true))
116+
return typeName;
112117

113118
if (_showShortName)
114-
typeName = GetShortName(typeName);
119+
typeName = TypeNameFormatter.GetShortName(typeName);
115120

116121
if (typeName == string.Empty)
117-
{
118-
typeName = TypeReference.NoneElement;
119-
}
120-
else if (TypeCache.GetType(_serializedTypeRef.TypeNameAndAssembly) == null)
122+
return TypeReference.NoneElement;
123+
124+
if (TypeCache.GetType(_serializedTypeRef.TypeNameAndAssembly) == null)
121125
{
122126
_serializedTypeRef.TryUpdatingTypeUsingGUID();
123127

124128
if (TypeCache.GetType(_serializedTypeRef.TypeNameAndAssembly) == null)
125-
typeName += MissingSuffix;
129+
return typeName + MissingSuffix;
126130
}
127131

128132
return typeName;
129133
}
130134

131-
private static string GetShortName(string fullTypeName) => fullTypeName.Split('.').Last();
132-
133135
private void OnTypeSelected(Type selectedType)
134136
{
135137
string selectedTypeNameAndAssembly = TypeReference.GetTypeNameAndAssembly(selectedType);

Editor/Drawers/TypeReferencePropertyDrawer.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ private void DrawTypeReferenceField(Rect position, SerializedProperty property)
4949
}
5050

5151
var dropdownDrawer = new TypeDropdownDrawer(selectedType, typeOptionsAttribute, fieldInfo?.DeclaringType);
52-
var fieldDrawer = new TypeFieldDrawer(serializedTypeRef, position, dropdownDrawer, typeOptionsAttribute.ShortName);
52+
53+
var fieldDrawer = new TypeFieldDrawer(
54+
serializedTypeRef,
55+
position,
56+
dropdownDrawer,
57+
typeOptionsAttribute.ShortName,
58+
typeOptionsAttribute.UseBuiltInNames);
5359

5460
fieldDrawer.Draw();
5561
}

Editor/Util/TypeNameFormatter.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
namespace TypeReferences.Editor.Util
22
{
33
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
46
using TypeReferences;
57
using UnityEngine;
68

@@ -10,6 +12,27 @@
1012
/// </summary>
1113
internal static class TypeNameFormatter
1214
{
15+
private const string BuiltInTypesPrefix = "Built-in.";
16+
17+
private static readonly Dictionary<string, string> BuiltInTypes = new Dictionary<string, string>
18+
{
19+
{ "System.Boolean", "bool" },
20+
{ "System.Byte", "byte" },
21+
{ "System.SByte", "sbyte" },
22+
{ "System.Char", "char" },
23+
{ "System.Decimal", "decimal" },
24+
{ "System.Double", "double" },
25+
{ "System.Single", "float" },
26+
{ "System.Int32", "int" },
27+
{ "System.UInt32", "uint" },
28+
{ "System.Int64", "long" },
29+
{ "System.UInt64", "ulong" },
30+
{ "System.Int16", "short" },
31+
{ "System.UInt16", "ushort" },
32+
{ "System.Object", "object" },
33+
{ "System.String", "string" }
34+
};
35+
1336
/// <summary>Generates a path for a dropdown item according to <paramref name="grouping"/>.</summary>
1437
/// <param name="type">Type to generate the path for.</param>
1538
/// <param name="fullTypeName">Full name of the type.</param>
@@ -35,6 +58,35 @@ public static string Format(Type type, string fullTypeName, Grouping grouping)
3558
}
3659
}
3760

61+
/// <summary>
62+
/// Replaces <paramref name="fullTypeName"/> with a built-in name if the built-in analogue exists.
63+
/// </summary>
64+
/// <param name="fullTypeName">Full name of the type.</param>
65+
/// <param name="withoutFolder">Whether to append a folder to the built-in type.</param>
66+
/// <returns>Name of the built-in type.</returns>
67+
/// <example><code>
68+
/// string intName = typeof(System.Int32).FullName;
69+
/// if (TryReplaceWithBuiltInName(intName))
70+
/// Debug.Log(intName); // prints "Built-in.int"
71+
///
72+
/// string intName = typeof(System.Int32).FullName;
73+
/// if (TryReplaceWithBuiltInName(intName), true)
74+
/// Debug.Log(intName); // prints "int"
75+
/// </code></example>
76+
public static bool TryReplaceWithBuiltInName(ref string fullTypeName, bool withoutFolder = false)
77+
{
78+
if ( ! BuiltInTypes.TryGetValue(fullTypeName, out string builtInName))
79+
return false;
80+
81+
fullTypeName = withoutFolder ? builtInName : BuiltInTypesPrefix + builtInName;
82+
return true;
83+
}
84+
85+
/// <summary>Gets the name of the type without its namespace.</summary>
86+
/// <param name="fullTypeName">Full name of the type including its namespace.</param>
87+
/// <returns>The type name without namespaces.</returns>
88+
public static string GetShortName(string fullTypeName) => fullTypeName.Split('.').Last();
89+
3890
private static string FormatByNamespace(string name)
3991
{
4092
return name.Replace('.', '/');

Runtime/Attributes/TypeOptionsAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class TypeOptionsAttribute : PropertyAttribute
5858
/// If the dropdown shows built-in types, it will show them by their keyword names (int) instead of full names
5959
/// (System.Int32).
6060
/// </summary>
61-
public bool UseBuiltInTypeNames = true;
61+
public bool UseBuiltInNames = true;
6262

6363
/// <summary>
6464
/// Determines whether the specified <see cref="Type"/> matches requirements set in the attribute.

0 commit comments

Comments
 (0)