Ejemplo n.º 1
0
        static private EntityScopeData DoEntityScopeField(GUIContent inLabel, EntityScopeData inScope, RSValidationFlags inFlags, RSValidationContext inContext)
        {
            bool bForceFirst = inFlags.Has(RSValidationFlags.RequireSingleEntity);

            EditorGUILayout.BeginVertical();

            EntityScopeType currentType = inScope.Type;
            EntityScopeType nextType    = ListGUILayout.Popup(inLabel, inScope.Type, RSEditorUtility.s_EntityScopeTypes);

            RSEntityId    entityId      = RSEntityId.Null;
            RSGroupId     groupId       = RSGroupId.Null;
            string        search        = string.Empty;
            bool          useFirst      = bForceFirst;
            string        links         = string.Empty;
            bool          useFirstLinks = bForceFirst;
            RegisterIndex register      = RegisterIndex.Register0;

            using (new EditorGUI.IndentLevelScope())
            {
                switch (currentType)
                {
                case EntityScopeType.ObjectById:
                {
                    entityId = RSEditorUtility.EditorPlugin.EntityIdGUIField(Content.EntityScopeEntityIdLabel, inScope.IdArg, inContext.Manager);
                    if (entityId == RSEntityId.Null && !inFlags.Has(RSValidationFlags.AllowNullEntity))
                    {
                        EditorGUILayout.HelpBox("Null entity not allowed in this context", MessageType.Error);
                    }
                    break;
                }

                case EntityScopeType.ObjectInRegister:
                {
                    if (inFlags.Has(RSValidationFlags.DisallowRegisters))
                    {
                        EditorGUILayout.HelpBox("Local vars not allowed in this context", MessageType.Error);
                    }
                    else
                    {
                        register = inScope.RegisterArg;
                        register = (RegisterIndex)EnumGUILayout.EnumField(Content.EntityScopeRegisterLabel, register);
                    }
                    break;
                }

                case EntityScopeType.ObjectsWithGroup:
                {
                    groupId  = inScope.GroupArg;
                    useFirst = bForceFirst || inScope.UseFirst;

                    using (new EditorGUILayout.HorizontalScope())
                    {
                        groupId = LibraryGUILayout.GroupSelector(Content.EntityScopeGroupLabel, groupId, inContext.Library);
                        using (new EditorGUI.DisabledScope(bForceFirst))
                            using (new RSGUI.LabelWidthScope(100))
                            {
                                useFirst = EditorGUILayout.Toggle(Content.EntityScopeGroupLabel, useFirst, GUILayout.Width(120));
                            }
                    }
                    break;
                }

                case EntityScopeType.ObjectsWithName:
                case EntityScopeType.ObjectsWithPrefab:
                {
                    search   = inScope.SearchArg;
                    useFirst = bForceFirst || inScope.UseFirst;

                    using (new EditorGUILayout.HorizontalScope())
                    {
                        search = EditorGUILayout.TextField(currentType == EntityScopeType.ObjectsWithName ? Content.EntityScopeNameLabel : Content.EntityScopePrefabLabel, search);
                        using (new EditorGUI.DisabledScope(bForceFirst))
                            using (new RSGUI.LabelWidthScope(100))
                            {
                                useFirst = EditorGUILayout.Toggle(Content.EntityScopeUseFirstLabel, useFirst, GUILayout.Width(120));
                            }
                    }
                    break;
                }

                case EntityScopeType.Null:
                {
                    if (!inFlags.Has(RSValidationFlags.AllowNullEntity))
                    {
                        EditorGUILayout.HelpBox("Null entity not allowed in this context", MessageType.Error);
                    }
                    break;
                }

                case EntityScopeType.Invalid:
                {
                    EditorGUILayout.HelpBox("Missing entity not allowed", MessageType.Error);
                    break;
                }

                case EntityScopeType.Global:
                {
                    if (!inFlags.Has(RSValidationFlags.AllowGlobalEntity))
                    {
                        EditorGUILayout.HelpBox("Global entity not allowed in this context", MessageType.Error);
                    }
                    break;
                }

                case EntityScopeType.Argument:
                {
                    if (inContext.Trigger == null)
                    {
                        EditorGUILayout.HelpBox("No argument available: No Trigger", MessageType.Error);
                    }
                    else if (inContext.Trigger.ParameterType == null)
                    {
                        EditorGUILayout.HelpBox(string.Format("No argument available: Trigger {0} has no argument", inContext.Trigger.Name), MessageType.Error);
                    }
                    else if (!inContext.Trigger.ParameterType.Type.CanConvert(RSBuiltInTypes.Entity))
                    {
                        EditorGUILayout.HelpBox(string.Format("No argument available: Trigger {0} has incompatible argument type {1}, which cannot convert to an Entity", inContext.Trigger.Name, inContext.Trigger.ParameterType.Type), MessageType.Error);
                    }
                    break;
                }
                }

                if (inScope.SupportsLinks())
                {
                    links         = inScope.LinksArg;
                    useFirstLinks = bForceFirst || inScope.UseFirstLink;

                    using (new EditorGUILayout.HorizontalScope())
                    {
                        links = EditorGUILayout.TextField(Content.EntityScopeLinksLabel, links);
                        using (new EditorGUI.DisabledScope(bForceFirst))
                            using (new RSGUI.LabelWidthScope(100))
                            {
                                useFirstLinks = EditorGUILayout.Toggle(Content.EntityScopeUseFirstLinkLabel, useFirstLinks, GUILayout.Width(120));
                            }
                    }
                }
            }

            EditorGUILayout.EndVertical();

            switch (nextType)
            {
            case EntityScopeType.Self:
                return(EntityScopeData.Self().WithLinks(links, useFirstLinks));

            case EntityScopeType.Argument:
                return(EntityScopeData.Argument().WithLinks(links, useFirstLinks));

            case EntityScopeType.Global:
                return(EntityScopeData.Global());

            case EntityScopeType.ObjectById:
                return(EntityScopeData.Entity(entityId).WithLinks(links, useFirstLinks));

            case EntityScopeType.ObjectInRegister:
                return(EntityScopeData.Register(register).WithLinks(links, useFirstLinks));

            case EntityScopeType.ObjectsWithGroup:
                return(EntityScopeData.WithGroup(groupId, useFirst).WithLinks(links, useFirstLinks));

            case EntityScopeType.ObjectsWithName:
                return(EntityScopeData.WithName(search, useFirst).WithLinks(links, useFirstLinks));

            case EntityScopeType.ObjectsWithPrefab:
                return(EntityScopeData.WithPrefab(search, useFirst).WithLinks(links, useFirstLinks));

            case EntityScopeType.Invalid:
                return(EntityScopeData.Invalid());

            case EntityScopeType.Null:
            default:
                return(EntityScopeData.Null());
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Converts a C# object to an RSValue.
        /// </summary>
        static public RSValue ToRSValue(object inObject)
        {
            if (inObject == null)
            {
                return(RSValue.Null);
            }

            Type objType = inObject.GetType();

            if (objType == typeof(RSValue))
            {
                return((RSValue)inObject);
            }

            if (objType.IsEnum)
            {
                return(RSValue.FromEnum((Enum)inObject));
            }

            switch (Type.GetTypeCode(objType))
            {
            case TypeCode.Boolean:
                return(RSValue.FromBool((bool)inObject));

            case TypeCode.Byte:
                return(RSValue.FromInt((byte)inObject));

            case TypeCode.Char:
                return(RSValue.FromInt((char)inObject));

            case TypeCode.Double:
                Log.Warn("[RSInterop] Truncation from Double to Single");
                return(RSValue.FromFloat((float)(double)inObject));

            case TypeCode.Int16:
                return(RSValue.FromInt((Int16)inObject));

            case TypeCode.Int32:
                return(RSValue.FromInt((Int32)inObject));

            case TypeCode.SByte:
                return(RSValue.FromInt((sbyte)inObject));

            case TypeCode.Single:
                return(RSValue.FromFloat((float)inObject));

            case TypeCode.String:
                return(RSValue.FromString((string)inObject));

            case TypeCode.UInt16:
                return(RSValue.FromInt((UInt16)inObject));

            case TypeCode.UInt32:
                Log.Warn("[RSInterop] Truncation from UInt32 to Int32");
                return(RSValue.FromInt((int)(UInt32)inObject));

            case TypeCode.UInt64:
                Log.Warn("[RSInterop] Truncation from UInt64 to Int32");
                return(RSValue.FromInt((int)(UInt64)inObject));

            case TypeCode.Object:
            {
                if (objType == typeof(Color))
                {
                    return(RSValue.FromColor((Color)inObject));
                }
                if (objType == typeof(Vector2))
                {
                    return(RSValue.FromVector2((Vector2)inObject));
                }
                if (objType == typeof(Vector3))
                {
                    return(RSValue.FromVector3((Vector3)inObject));
                }
                if (objType == typeof(Vector4))
                {
                    return(RSValue.FromVector4((Vector4)inObject));
                }
                if (objType == typeof(RSGroupId))
                {
                    return(RSValue.FromGroupId((RSGroupId)inObject));
                }
                if (objType == typeof(RSTriggerId))
                {
                    return(RSValue.FromTriggerId((RSTriggerId)inObject));
                }
                if (typeof(IRSEntity).IsAssignableFrom(objType))
                {
                    IRSEntity       entity = (IRSEntity)inObject;
                    EntityScopeData scope  = EntityScopeData.Entity(entity.Id);
                    return(RSValue.FromEntity(scope));
                }
                break;
            }
            }

            throw new ArgumentException(string.Format("Unable to convert object of type {0} to RSValue", objType.Name), "inObject");
        }