Ejemplo n.º 1
0
    void sendFeedback(string address, WSQuery query)
    {
        CompInfo info = compInfoMap[address];

        object oldData = null;

        propQueryPreviousValues.TryGetValue(address, out oldData);

        object data = null;

        if (info.propInfo != null)
        {
            data = info.propInfo.GetValue(info.comp);
        }
        else if (info.fieldInfo != null)
        {
            data = info.fieldInfo.GetValue(info.comp);
        }
        else if (info.infoType == CompInfo.InfoType.VFX)
        {
            data = getVFXPropValue(info.comp as VisualEffect, info.genericInfo.type, info.genericInfo.name);
        }

        if (data == null)
        {
            return;
        }

        propQueryPreviousValues[address] = data;

        OSCMessage m = new OSCMessage(address);

        string dataType = data.GetType().Name;

        switch (dataType)
        {
        case "Boolean":
        {
            bool val = (bool)data;
            if (oldData != null && val == (bool)oldData)
            {
                return;
            }
            m.Append(val ? 1 : 0);
        }
        break;

        case "Vector2":
        {
            Vector2 v = (Vector2)data;
            if (oldData != null && v == (Vector2)oldData)
            {
                return;
            }
            m.Append(v.x);
            m.Append(v.y);
        }
        break;

        case "Vector3":
        {
            Vector3 v = (Vector3)data;
            if (oldData != null && v == (Vector3)oldData)
            {
                return;
            }
            m.Append(v.x);
            m.Append(v.y);
            m.Append(v.z);
        }
        break;

        case "Color":
        case "Vector4":
        {
            Color color    = dataType == "Color" ? (Color)data : (Color)(Vector4)data;
            Color oldColor = dataType == "Color" ? (Color)oldData : (Color)(Vector4)oldData;
            if (oldData != null && color == (Color)oldColor)
            {
                return;
            }
            m.Append(color.r);
            m.Append(color.g);
            m.Append(color.b);
            m.Append(color.a);
        }
        break;

        case "Quaternion":
        {
            Vector3 v = ((Quaternion)data).eulerAngles;
            if (oldData != null && v == ((Quaternion)oldData).eulerAngles)
            {
                return;
            }

            m.Append(v.x);
            m.Append(v.y);
            m.Append(v.z);
        }
        break;

        default:
            if (oldData != null && data.ToString() == oldData.ToString())
            {
                return;
            }
            if (info.type.IsEnum)
            {
                m.Append(data.ToString());
            }
            else
            {
                m.Append(data);
            }
            break;
        }

        query.sendData(m.BinaryData);
    }
Ejemplo n.º 2
0
    void ProcessIncomingMessages()
    {
        while (receiver.hasWaitingMessages())
        {
            OSCMessage msg = receiver.getNextMessage();

            string args = "";
            msg.Data.ForEach((arg) => args += arg.ToString() + ", ");

            //Debug.Log("info received : " + msg.Address);
            if (compInfoMap.ContainsKey(msg.Address))
            {
                CompInfo info = compInfoMap[msg.Address];
                object   data = null;

                if (info == null)
                {
                    //Debug.LogWarning("Address not found : " + msg.Address);
                    continue;
                }


                if (info.infoType == CompInfo.InfoType.Method)
                {
                    int numParams = info.methodInfo.GetParameters().Length;
                    info.methodInfo.Invoke(info.comp, Enumerable.Repeat(Type.Missing, numParams).ToArray());
                    continue;
                }

                string typeString = info.type.ToString();

                switch (typeString)
                {
                case "System.String":
                case "System.Char":
                    data = msg.Data[0].ToString();
                    break;

                case "System.Boolean":
                    data = ((int)msg.Data[0] == 1);
                    break;

                case "System.Int32":
                case "System.Int64":
                case "System.UInt32":
                case "System.Int16":
                case "System.UInt16":
                case "System.Byte":
                case "System.SByte":
                    data = (int)msg.Data[0];
                    break;

                case "System.Double":
                case "System.Single":
                    data = getFloatArg(msg.Data[0]);
                    break;

                case "UnityEngine.Vector2":
                    data = new Vector2(getFloatArg(msg.Data[0]), getFloatArg(msg.Data[1]));
                    break;

                case "UnityEngine.Vector3":
                    data = new Vector3(getFloatArg(msg.Data[0]), getFloatArg(msg.Data[1]), getFloatArg(msg.Data[2]));

                    break;

                case "UnityEngine.Quaternion":
                    data = Quaternion.Euler(new Vector3(getFloatArg(msg.Data[0]), getFloatArg(msg.Data[1]), getFloatArg(msg.Data[2])));
                    break;

                case "UnityEngine.Color":
                case "UnityEngine.Vector4":
                {
                    if (msg.Data.Count == 1)
                    {
                        data = (Color)msg.Data[0];
                    }
                    else if (msg.Data.Count >= 3)
                    {
                        data = new Color(getFloatArg(msg.Data[0]), getFloatArg(msg.Data[1]), getFloatArg(msg.Data[2]), msg.Data.Count > 3 ? getFloatArg(msg.Data[2]): 1.0f);
                    }
                }
                break;


                default:
                    if (info.type.IsEnum)
                    {
                        JSONObject enumO = new JSONObject();

                        FieldInfo[] fields = info.type.GetFields();

                        foreach (var field in fields)
                        {
                            if (field.Name.Equals("value__"))
                            {
                                continue;
                            }
                            if (field.Name == msg.Data[0].ToString())
                            {
                                //Debug.Log("Found enum " + field.Name + " > " + field.GetRawConstantValue());
                                data = field.GetRawConstantValue();
                            }
                        }
                    }
                    break;
                }

                if (data != null)
                {
                    switch (info.infoType)
                    {
                    case CompInfo.InfoType.Field:
                        info.fieldInfo.SetValue(info.comp, data);
                        break;

                    case CompInfo.InfoType.Property:
                        info.propInfo.SetValue(info.comp, data);
                        break;

                    case CompInfo.InfoType.VFX:
                    {
                        int    dotIndex = info.comp.GetType().ToString().LastIndexOf(".");
                        string compType = info.comp.GetType().ToString().Substring(Mathf.Max(dotIndex + 1, 0));
                        setVFXPropValue((info.comp as VisualEffect), info.genericInfo.type, info.genericInfo.name, data);
                    }
                    break;

                    case CompInfo.InfoType.Material:
                        /* {
                         *   int dotIndex = info.comp.GetType().ToString().LastIndexOf(".");
                         *   string compType = info.comp.GetType().ToString().Substring(Mathf.Max(dotIndex + 1, 0));
                         *   setMaterialPropValue((info.comp as VisualEffect), info.genericInfo.type, info.genericInfo.name, data);
                         * }*/
                        break;
                    }
                }
                else
                {
                    Debug.LogWarning("Type not handled : " + typeString + ", address : " + msg.Address);
                }
            }
            else
            {
                Debug.LogWarning("Property not found for address : " + msg.Address);
            }
        }
    }