Add **Dictionaries to Visual Scripting**

This commit is contained in:
Wojtek Figat
2022-04-27 22:47:54 +02:00
parent 3c841b1be1
commit 158c29e598
20 changed files with 852 additions and 114 deletions

View File

@@ -714,7 +714,7 @@ namespace FlaxEditor.Scripting
public IScriptType IScriptType => _custom;
/// <summary>
/// Gets a type name (eg. name of the class or enum without leading namespace).
/// Gets a type display name (eg. name of the class or enum without leading namespace).
/// </summary>
public string Name
{
@@ -724,19 +724,7 @@ namespace FlaxEditor.Scripting
return _custom.Name;
if (_managed == null)
return string.Empty;
if (_managed == typeof(float))
return "Float";
if (_managed == typeof(int))
return "Int";
if (_managed == typeof(uint))
return "Uint";
if (_managed == typeof(short))
return "Int16";
if (_managed == typeof(ushort))
return "Uint16";
if (_managed == typeof(bool))
return "Bool";
return _managed.Name;
return _managed.GetTypeDisplayName();
}
}
@@ -790,6 +778,11 @@ namespace FlaxEditor.Scripting
/// </summary>
public bool IsArray => _managed != null ? _managed.IsArray : _custom != null && _custom.IsArray;
/// <summary>
/// Gets a value indicating whether the type is a dictionary.
/// </summary>
public bool IsDictionary => IsGenericType && GetGenericTypeDefinition() == typeof(Dictionary<,>);
/// <summary>
/// Gets a value indicating whether the type is a value type (basic type, enumeration or a structure).
/// </summary>
@@ -981,7 +974,7 @@ namespace FlaxEditor.Scripting
public override string ToString()
{
if (_managed != null)
return _managed.FullName ?? string.Empty;
return _managed.GetTypeDisplayName() ?? string.Empty;
if (_custom != null)
return _custom.TypeName;
return "<null>";
@@ -1122,6 +1115,15 @@ namespace FlaxEditor.Scripting
throw new NotImplementedException("TODO: Script.Type.MakeArrayType for custom types");
}
/// <summary>
/// Returns a type object that represents a dictionary of the key and value types.
/// </summary>
/// <returns>A type object representing a dictionary of the key and value types.</returns>
public static ScriptType MakeDictionaryType(ScriptType keyType, ScriptType valueType)
{
return new ScriptType(typeof(Dictionary<,>).MakeGenericType(TypeUtils.GetType(keyType), TypeUtils.GetType(valueType)));
}
/// <summary>
/// Searches for the specified members of the specified member type, using the specified binding constraints.
/// </summary>

View File

@@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using FlaxEngine;
namespace FlaxEditor.Scripting
@@ -33,6 +34,74 @@ namespace FlaxEditor.Scripting
return o != null ? new ScriptType(o.GetType()) : ScriptType.Null;
}
/// <summary>
/// Gets the typename full name.
/// </summary>
/// <param name="type">The type.</param>
/// <returns>The full typename of the type.</returns>
public static string GetTypeName(this Type type)
{
if (type.IsGenericType)
{
// For generic types (eg. Dictionary) FullName returns generic parameter types with fully qualified name so simplify it manually
var sb = new StringBuilder();
sb.Append(type.Namespace);
sb.Append('.');
sb.Append(type.Name);
sb.Append('[');
var genericArgs = type.GetGenericArguments();
for (var i = 0; i < genericArgs.Length; i++)
{
if (i != 0)
sb.Append(',');
sb.Append(genericArgs[i].GetTypeName());
}
sb.Append(']');
return sb.ToString();
}
return type.FullName;
}
/// <summary>
/// Gets the typename name for UI.
/// </summary>
/// <param name="type">The type.</param>
/// <returns>The display of the type.</returns>
public static string GetTypeDisplayName(this Type type)
{
// Special display for in-built basic types
if (type == typeof(bool))
return "Bool";
if (type == typeof(float))
return "Float";
if (type == typeof(int))
return "Int";
if (type == typeof(uint))
return "Uint";
// For generic types (eg. Dictionary) Name returns generic parameter types with fully qualified name so simplify it manually
if (type.IsGenericType)
{
var sb = new StringBuilder();
var name = type.Name;
var idx = name.IndexOf('`');
sb.Append(idx != -1 ? name.Substring(0, idx) : name);
sb.Append('<');
var genericArgs = type.GetGenericArguments();
for (var i = 0; i < genericArgs.Length; i++)
{
if (i != 0)
sb.Append(", ");
sb.Append(genericArgs[i].GetTypeDisplayName());
}
sb.Append('>');
return sb.ToString();
}
// Default name
return type.Name;
}
/// <summary>
/// Gets the default value for the given type (can be value type or reference type).
/// </summary>