This repository has been archived by the owner on Dec 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
CustomEditorsUtil.cs
142 lines (124 loc) · 4.87 KB
/
CustomEditorsUtil.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright (c) 2012-2019 Wojciech Figat. All rights reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using FlaxEditor.CustomEditors.Editors;
using FlaxEngine;
namespace FlaxEditor.CustomEditors
{
internal static class CustomEditorsUtil
{
private static readonly StringBuilder CachedSb = new StringBuilder(256);
/// <summary>
/// Gets the property name for UI. Removes unnecessary characters and filters text. Makes it more user-friendly.
/// </summary>
/// <param name="name">The name.</param>
/// <returns>The result.</returns>
public static string GetPropertyNameUI(string name)
{
int length = name.Length;
StringBuilder sb = CachedSb;
sb.Clear();
sb.EnsureCapacity(length + 8);
int startIndex = 0;
// Skip some prefixes
if (name.StartsWith("g_") || name.StartsWith("m_"))
startIndex = 2;
// Filter text
for (int i = startIndex; i < length; i++)
{
var c = name[i];
// Space before word starting with uppercase letter
if (char.IsUpper(c) && i > 0)
{
if (i + 2 < length && !char.IsUpper(name[i + 1]) && !char.IsUpper(name[i + 2]))
sb.Append(' ');
}
// Space instead of underscore
else if (c == '_')
{
if (sb.Length > 0)
sb.Append(' ');
continue;
}
// Space before digits sequence
else if (i > 1 && char.IsDigit(c) && !char.IsDigit(name[i - 1]))
sb.Append(' ');
sb.Append(c);
}
return sb.ToString();
}
internal static CustomEditor CreateEditor(ValueContainer values, CustomEditor overrideEditor, bool canUseRefPicker = true)
{
// Check if use provided editor
if (overrideEditor != null)
return overrideEditor;
// Special case if property is a pure object type and all values are the same type
if (values.Type == typeof(object) && values.Count > 0 && values[0] != null && !values.HasDifferentTypes)
return CreateEditor(values[0].GetType(), canUseRefPicker);
// Use editor for the property type
return CreateEditor(values.Type, canUseRefPicker);
}
internal static CustomEditor CreateEditor(Type targetType, bool canUseRefPicker = true)
{
if (targetType.IsArray)
{
return new ArrayEditor();
}
if (canUseRefPicker)
{
if (targetType.IsSubclassOf(typeof(Asset)))
{
return new AssetRefEditor();
}
if (targetType.IsSubclassOf(typeof(FlaxEngine.Object)))
{
return new FlaxObjectRefEditor();
}
}
// Use custom editor
{
var checkType = targetType;
do
{
var type = Internal_GetCustomEditor(checkType);
if (type != null)
{
return (CustomEditor)Activator.CreateInstance(type);
}
checkType = checkType.BaseType;
// Skip if cannot use ref editors
if (!canUseRefPicker && checkType == typeof(FlaxEngine.Object))
break;
} while (checkType != null);
}
// Use attribute editor
var attributes = targetType.GetCustomAttributes(false);
var customEditorAttribute = (CustomEditorAttribute)attributes.FirstOrDefault(x => x is CustomEditorAttribute);
if (customEditorAttribute != null)
return (CustomEditor)Activator.CreateInstance(customEditorAttribute.Type);
// Select default editor (based on type)
if (targetType.IsEnum)
{
return new EnumEditor();
}
if (targetType.IsGenericType)
{
if (targetType.GetGenericTypeDefinition() == typeof(List<>))
{
return new ListEditor();
}
if (DictionaryEditor.CanEditType(targetType))
{
return new DictionaryEditor();
}
}
// The most generic editor
return new GenericEditor();
}
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern Type Internal_GetCustomEditor(Type targetType);
}
}