static private void ValidateValue(RSValue inValue, RSTypeInfo inExpectedType, RSValidationFlags inFlags, RSValidationState ioState, RSValidationContext inContext) { Type systemType = inExpectedType.SystemType; if (systemType.IsEnum) { try { Enum currentValue = inValue.AsEnum(); } catch (Exception e) { Debug.LogException(e); ioState.Error("Enum {0} cannot be represented as type {1}", inValue, inExpectedType); } return; } if (inExpectedType == RSBuiltInTypes.Entity) { EntityScopeData scope = inValue.AsEntity; ValidateEntityScope(scope, inFlags.ForEntityValue(), ioState, inContext); } else if (inExpectedType == RSBuiltInTypes.GroupId) { RSGroupId group = inValue.AsGroupId; ValidateGroupId(group, inFlags, ioState, inContext); } else if (inExpectedType == RSBuiltInTypes.TriggerId) { RSTriggerId triggerId = inValue.AsTriggerId; ValidateTriggerId(triggerId, inFlags, ioState, inContext); } }
static private RSValue DoRSValueField(GUIContent inLabel, RSValue inValue, RSTypeInfo inExpectedType, RSValidationFlags inFlags, RSValidationContext inContext) { Type systemType = inExpectedType.SystemType; if (systemType.IsEnum) { Enum currentValue; try { currentValue = inValue.AsEnum(); } catch (Exception e) { Debug.LogException(e); currentValue = inExpectedType.DefaultValue.AsEnum(); } Enum nextValue = EnumGUILayout.EnumField(inLabel, currentValue); return(RSValue.FromEnum(nextValue)); } if (inExpectedType == RSBuiltInTypes.Int) { int currentValue = inValue.AsInt; int nextValue = EditorGUILayout.DelayedIntField(inLabel, currentValue); return(RSValue.FromInt(nextValue)); } else if (inExpectedType == RSBuiltInTypes.Float) { float currentValue = inValue.AsFloat; float nextValue = EditorGUILayout.DelayedFloatField(inLabel, currentValue); return(RSValue.FromFloat(nextValue)); } else if (inExpectedType == RSBuiltInTypes.Bool) { bool currentValue = inValue.AsBool; bool nextValue = EditorGUILayout.Toggle(inLabel, currentValue); return(RSValue.FromBool(nextValue)); } else if (inExpectedType == RSBuiltInTypes.Color) { Color currentValue = inValue.AsColor; Color nextValue = EditorGUILayout.ColorField(inLabel, currentValue); return(RSValue.FromColor(nextValue)); } else if (inExpectedType == RSBuiltInTypes.String) { string currentValue = inValue.AsString; string nextValue = EditorGUILayout.TextField(inLabel, currentValue); return(RSValue.FromString(nextValue)); } else if (inExpectedType == RSBuiltInTypes.Vector2) { Vector2 currentValue = inValue.AsVector2; Vector2 nextValue = EditorGUILayout.Vector2Field(inLabel, currentValue); return(RSValue.FromVector2(nextValue)); } else if (inExpectedType == RSBuiltInTypes.Vector3) { Vector3 currentValue = inValue.AsVector3; Vector3 nextValue = EditorGUILayout.Vector3Field(inLabel, currentValue); return(RSValue.FromVector3(nextValue)); } else if (inExpectedType == RSBuiltInTypes.Vector4) { Vector4 currentValue = inValue.AsVector4; Vector4 nextValue = EditorGUILayout.Vector4Field(inLabel, currentValue); return(RSValue.FromVector4(nextValue)); } else if (inExpectedType == RSBuiltInTypes.Entity) { EntityScopeData currentValue = inValue.AsEntity; EntityScopeData nextValue = EntityScopeField(inLabel, currentValue, inFlags.ForEntityValue(), inContext); return(RSValue.FromEntity(nextValue)); } else if (inExpectedType == RSBuiltInTypes.GroupId) { RSGroupId currentValue = inValue.AsGroupId; RSGroupId nextValue = LibraryGUILayout.GroupSelector(inLabel, currentValue, inContext.Library); return(RSValue.FromGroupId(nextValue)); } else if (inExpectedType == RSBuiltInTypes.TriggerId) { RSTriggerId currentValue = inValue.AsTriggerId; RSTypeInfo restrictTriggerType = inContext.Parameter?.TriggerParameterType; RSTriggerId nextValue; if (restrictTriggerType != null) { nextValue = LibraryGUILayout.TriggerSelector(inLabel, currentValue, restrictTriggerType, inContext.Library); } else { nextValue = LibraryGUILayout.TriggerSelector(inLabel, currentValue, inContext.Library); } return(RSValue.FromTriggerId(nextValue)); } else { EditorGUILayout.HelpBox(string.Format("Unable to display editor for type {0}", inExpectedType), MessageType.Error); } return(inValue); }
/// <summary> /// Converts an RSValue to the given C# type. /// </summary> static public object ToObject(Type inType, RSValue inValue, ExecutionScope inContext) { if (inType == null || inType == typeof(void)) { return(null); } if (inType == typeof(object)) { return(inValue); } if (inType == typeof(RSValue)) { return(inValue); } if (inType.IsEnum) { return(inValue.AsEnum(inType)); } switch (Type.GetTypeCode(inType)) { case TypeCode.Boolean: return(inValue.AsBool); case TypeCode.Byte: return((byte)inValue.AsInt); case TypeCode.Char: return((char)inValue.AsInt); case TypeCode.Double: return((double)inValue.AsFloat); case TypeCode.Int16: return((Int16)inValue.AsInt); case TypeCode.Int32: return(inValue.AsInt); case TypeCode.Int64: return((Int64)inValue.AsInt); case TypeCode.SByte: return((sbyte)inValue.AsInt); case TypeCode.Single: return(inValue.AsFloat); case TypeCode.String: return(inValue.AsString); case TypeCode.UInt16: return((UInt16)inValue.AsInt); case TypeCode.UInt32: return((UInt32)inValue.AsInt); case TypeCode.UInt64: return((UInt64)inValue.AsInt); case TypeCode.Object: { if (inType == typeof(Color)) { return(inValue.AsColor);; } if (inType == typeof(Vector2)) { return(inValue.AsVector2); } if (inType == typeof(Vector3)) { return(inValue.AsVector3); } if (inType == typeof(Vector4)) { return(inValue.AsVector4); } if (inType == typeof(RSGroupId)) { return(inValue.AsGroupId); } if (inType == typeof(RSTriggerId)) { return(inValue.AsTriggerId); } if (typeof(IRSEntity).IsAssignableFrom(inType)) { return(inContext.ResolveEntity(inValue.AsEntity).ForceSingle()); } break; } } throw new ArgumentException(string.Format("Unable to convert RSValue {0} to object of type {1}", inValue, inType), "inValue"); }