Skip to content

Commit 1cb4bc6

Browse files
authored
Improve editing values in method invoke and clipboard windows (#102)
1 parent 679ede2 commit 1cb4bc6

File tree

4 files changed

+68
-15
lines changed

4 files changed

+68
-15
lines changed

RuntimeUnityEditor.Core/Utils/TomlTypeConverter.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,17 @@ static TomlTypeConverter()
116116
return string.Format(CultureInfo.InvariantCulture, "{{ \"x\":{0}, \"y\":{1}, \"width\":{2}, \"height\":{3} }}", new object[] { rect.x, rect.y, rect.width, rect.height });
117117
}
118118
});
119+
AddConverter(typeof(Type), new TypeConverter
120+
{
121+
ConvertToString = (obj, type) => ((Type)obj).AssemblyQualifiedName,
122+
ConvertToObject = (str, type) =>
123+
{
124+
var result = Type.GetType(str);
125+
if (result == null)
126+
throw new FormatException($"Invalid type string, expected assembly qualified name");
127+
return result;
128+
}
129+
});
119130
}
120131

121132
/// <summary>
@@ -179,7 +190,7 @@ public static bool AddConverter(Type type, TypeConverter converter)
179190
}
180191

181192
TypeConverters.Add(type, converter);
182-
ToStringConverter._canCovertCache[type] = true;
193+
ToStringConverter._canConvertCache[type] = true;
183194
return true;
184195
}
185196

RuntimeUnityEditor.Core/Windows/Clipboard/ClipboardWindow.cs

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public class ClipboardWindow : Window<ClipboardWindow>
2020
/// </summary>
2121
public static readonly List<object> Contents = new List<object>();
2222
private Vector2 _scrollPos;
23+
private string _editingIndex;
24+
private string _editingValue;
2325

2426
protected override void Initialize(InitSettings initSettings)
2527
{
@@ -60,6 +62,8 @@ protected override void DrawContents()
6062
}
6163
GUILayout.EndHorizontal();
6264

65+
const string editControlNamePrefix = "clipboard_edit_";
66+
6367
for (var index = 0; index < Contents.Count; index++)
6468
{
6569
GUILayout.BeginHorizontal(GUI.skin.box);
@@ -76,27 +80,51 @@ protected override void DrawContents()
7680

7781
var prevEnabled = GUI.enabled;
7882
GUI.enabled = type != null && typeof(IConvertible).IsAssignableFrom(type);
83+
7984
GUI.changed = false;
80-
var newVal = GUILayout.TextField(ToStringConverter.ObjectToString(content), IMGUIUtils.LayoutOptionsExpandWidthTrue);
85+
var controlName = editControlNamePrefix + index;
86+
GUI.SetNextControlName(controlName);
87+
88+
var isBeingEdited = _editingIndex == controlName;
89+
var prevColor = GUI.backgroundColor;
90+
if (isBeingEdited) GUI.backgroundColor = _editingValue == null ? Color.green : Color.yellow;
91+
92+
var newVal = GUILayout.TextField(isBeingEdited && _editingValue != null ? _editingValue : ToStringConverter.ObjectToString(content), IMGUIUtils.LayoutOptionsExpandWidthTrue);
93+
8194
if (GUI.changed && type != null)
8295
{
96+
_editingIndex = controlName;
97+
_editingValue = newVal;
8398
try
8499
{
85-
Contents[index] = Convert.ChangeType(newVal, type);
100+
var converter = TomlTypeConverter.GetConverter(type);
101+
if (converter != null)
102+
Contents[index] = converter.ConvertToObject(newVal, type);
103+
else
104+
Contents[index] = Convert.ChangeType(newVal, type);
105+
106+
_editingValue = null;
86107
}
87-
catch (Exception e)
108+
catch (Exception)
88109
{
89-
Console.WriteLine($"Could not convert string \"{newVal}\" to type \"{type.Name}\": {e.Message}");
110+
//Console.WriteLine($"Could not convert string \"{newVal}\" to type \"{type.Name}\": {e.Message}");
90111
}
91112
}
92113

93114
GUI.enabled = prevEnabled;
115+
GUI.backgroundColor = prevColor;
94116

95117
if (GUILayout.Button("X", IMGUIUtils.LayoutOptionsExpandWidthFalse))
96118
Contents.RemoveAt(index);
97119
}
98120
GUILayout.EndHorizontal();
99121
}
122+
123+
if (_editingIndex != null && GUI.GetNameOfFocusedControl() != _editingIndex)
124+
{
125+
_editingIndex = null;
126+
_editingValue = null;
127+
}
100128
}
101129
GUILayout.EndVertical();
102130
}
@@ -117,8 +145,8 @@ public static string ResolveString(string parameterString)
117145
private static bool TryExtractId(string parameterString, out int id)
118146
{
119147
id = 0;
120-
return parameterString.Length >= 2 && parameterString.StartsWith("#") &&
121-
int.TryParse(parameterString.Substring(1), out id) &&
148+
return parameterString.Length >= 2 && parameterString.StartsWith("#") &&
149+
int.TryParse(parameterString.Substring(1), out id) &&
122150
Contents.Count > id && id >= 0;
123151
}
124152

RuntimeUnityEditor.Core/Windows/Inspector/ToStringConverter.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public static void AddConverter<TObj>(Func<TObj, string> objectToString)
3030

3131
var type = typeof(TObj);
3232
_toStringConverters[type] = o => objectToString.Invoke((TObj)o);
33+
_canConvertCache.Remove(typeof(TObj));
3334
}
3435

3536
/// <summary>
@@ -124,7 +125,7 @@ internal static string EventEntryToString(UnityEventBase eventObj, int i)
124125
return $"{eventObj.GetPersistentTarget(i)?.GetType().GetSourceCodeRepresentation() ?? "[NULL]"}.{eventObj.GetPersistentMethodName(i)}";
125126
}
126127

127-
internal static readonly Dictionary<Type, bool> _canCovertCache = new Dictionary<Type, bool>();
128+
internal static readonly Dictionary<Type, bool> _canConvertCache = new Dictionary<Type, bool>();
128129

129130
/// <summary>
130131
/// Check if the value can be converted to a string and back to the original type.
@@ -139,25 +140,25 @@ public static bool CanEditValue(ICacheEntry field, object value)
139140
if (valueType == typeof(string))
140141
return true;
141142

142-
if (_canCovertCache.TryGetValue(valueType, out var stored))
143+
if (_canConvertCache.TryGetValue(valueType, out var stored))
143144
return stored;
144145

145146
if (TomlTypeConverter.GetConverter(valueType) != null)
146147
{
147-
_canCovertCache[valueType] = true;
148+
_canConvertCache[valueType] = true;
148149
return true;
149150
}
150151

151152
try
152153
{
153154
var converted = ToStringConverter.ObjectToString(value);
154155
_ = Convert.ChangeType(converted, valueType);
155-
_canCovertCache[valueType] = true;
156+
_canConvertCache[valueType] = true;
156157
return true;
157158
}
158159
catch
159160
{
160-
_canCovertCache[valueType] = false;
161+
_canConvertCache[valueType] = false;
161162
return false;
162163
}
163164
}
@@ -176,7 +177,10 @@ public static void SetEditValue(ICacheEntry field, object currentValue, string n
176177
else
177178
{
178179
var typeConverter = TomlTypeConverter.GetConverter(valueType);
179-
converted = typeConverter != null ? typeConverter.ConvertToObject(newValue, valueType) : Convert.ChangeType(newValue, valueType);
180+
if (typeConverter != null)
181+
converted = typeConverter.ConvertToObject(newValue, valueType);
182+
else
183+
converted = Convert.ChangeType(newValue, valueType);
180184
}
181185

182186
if (!Equals(converted, currentValue))

RuntimeUnityEditor.Core/Windows/Inspector/VariableFieldDrawer.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -623,8 +623,18 @@ private static void DrawInvokeWindowFunc(int id)
623623
paramType = nullableParamType;
624624
}
625625

626-
var obj = ClipboardWindow.TryGetObject(arg, out var clipboardObj) ? clipboardObj : Convert.ChangeType(arg, paramType);
627-
paramArgs[index] = obj;
626+
if (ClipboardWindow.TryGetObject(arg, out var clipboardObj))
627+
{
628+
paramArgs[index] = clipboardObj;
629+
}
630+
else
631+
{
632+
var converter = TomlTypeConverter.GetConverter(paramType);
633+
if (converter != null)
634+
paramArgs[index] = converter.ConvertToObject(arg, paramType);
635+
else
636+
paramArgs[index] = Convert.ChangeType(arg, paramType);
637+
}
628638
}
629639
catch (Exception e)
630640
{

0 commit comments

Comments
 (0)