Exemplo n.º 1
0
        void Update()
        {
            float curTime = Time.realtimeSinceStartup;

            // check if ControlInputs have to be created (needed for multiinputs which can dynamically grow)

            foreach (CreateQueueEntry entry in createQueue)
            {
                ControlInput parent        = entry.ciParent;
                string       multiId       = entry.multiId;
                string       multiFullName = entry.name;

                if (ControlInputMulti.allMultiControls.ContainsKey(multiFullName) == false)
                {
                    EWUnityExtensions.Log("Create new multi val controlinput child for '" + multiFullName + "'");
                    ControlInputMulti ciChild = this.gameObject.AddComponent <ControlInputMulti> ();
                    ciChild.multiId = multiId;
                    ciChild.RegisterMulti(multiFullName);
                    ciChild.multiParent = parent;
                    ControlInput.MultiInputEntry multiEntry = new ControlInput.MultiInputEntry();
                    multiEntry.multiId      = multiId;
                    multiEntry.name         = multiFullName;
                    multiEntry.controlInput = ciChild;
                    parent.multiInputs.Add(multiEntry);
                    ciChild.SetCurrentValue(entry.initVal);
                }
                else
                {
                    EWUnityExtensions.LogWarning("'" + multiFullName + "' already created!");
                }
            }
            createQueue.Clear();


            // TODO: restart client if closed... look for server (rendevous/zeroconf ? )
            if (IsConnectionLost() && (curTime - timeLastConnectionTry) > cooldownConnectionTry)
            {
                timeLastConnectionTry = curTime;
                EWUnityExtensions.Log("Try to connect to server '" + linker_ip + ":" + linker_tcp_port + "'");
                connectThread = new Thread(ConnectToServer);
                connectThread.Start();
            }

            foreach (var kV in inValues)
            {
                EWUnityExtensions.Log("in Values => " + kV.Key + " -> " + kV.Value);
            }
        }
Exemplo n.º 2
0
        private void UpdateValues(string[] lines)
        {
            // EWUnityExtensions.Log("Update values: " + lines[0]);

            // TODO: actually only one line is needed ...
            // for (int i=0; i < lines.Length ; i++) {
            string line = lines [0];

            if (debugShowData)
            {
                EWUnityExtensions.Log("Line to parse is '" + line + "'");
            }

            JSONNode rootNode = JSON.Parse(line);
            // EWUnityExtensions.Log ("input interpreted as Json has values # " + rootNode.Count);
            JSONNode values = (JSONNode)rootNode ["values"];

            if (values != null)
            {
                // EWUnityExtensions.Log ("=> values has => # " + values.Count);
            }


            for (int j = 0; j < inNames.Length; j++)
            {
                string inValName = inNames [j];

                JSONNode valueArray = values [j];

                // TODO: parse the values and split them appropriately. inputs can be look:
                //			[[], [], [], [], [0.990325079762794], [-0.2852937250435257]]
                //      OR	[[0.990325079762794,0.123], [], [], [], [], [-0.2852937250435257]]
                //      OR	[[], [], [], [], [], []]
                //		OR	[[0.123], [0.123], [0.123], [0.123], [0.123], [0.123]]


                bool foundFitting = false;
                if (this.debugShowData)
                {
                    // EWUnityExtensions.Log("# controlInputs to update: " + ControlInput.allControlsList.Count);
                }

                foreach (ControlInput ci in ControlInput.allControlsList)
                {
                    if (ci.controlName == inValName)
                    {
                        float avg_val = 0;

                        for (int ik = 0; ik < valueArray.Count; ik++)
                        {
                            JSONNode val = valueArray [ik];

                            bool isJsonClass = val is JSONClass;
                            bool isJsonData  = val is SimpleJSON.JSONData;


                            if (debugShowData)
                            {
                                EWUnityExtensions.Log("val has count # " + val.Count + " and is json class ? " + isJsonClass);
                            }
                            if (isJsonClass && ci.isMultiInput)
                            {
                                JSONClass valDict = (JSONClass)val;
                                if (debugShowData)
                                {
                                    EWUnityExtensions.Log("valDict has children: #" + valDict.Count);
                                }
                                foreach (string key in valDict.GetKeys())
                                {
                                    float childVal = float.Parse(valDict [key]);
                                    if (debugShowData)
                                    {
                                        EWUnityExtensions.Log("=> found key " + key + " with value " + childVal);
                                    }
                                    string            multiFullName = ControlInputMulti.MakeMultiValueName(ci.controlName, key);
                                    ControlInputMulti ciChild       = null;
                                    if (ControlInputMulti.allMultiControls.ContainsKey(multiFullName) == false)
                                    {
                                        CreateQueueEntry entry = new CreateQueueEntry();
                                        entry.ciParent = ci;
                                        entry.multiId  = key;
                                        entry.name     = multiFullName;
                                        entry.initVal  = childVal;
                                        createQueue.Add(entry);
                                    }
                                    else
                                    {
                                        ciChild = ControlInputMulti.allMultiControls [multiFullName];
                                        ciChild.SetCurrentValue(childVal);
                                    }
                                }
                            }
                            else if (isJsonData)
                            {
                                avg_val += val.AsFloat;
                            }
                            else
                            {
                                EWUnityExtensions.LogWarning("Unknown json type" + val.ToString() + " " + val.GetType() + ", isMulti ? " + ci.isMultiInput);
                            }
                        }

                        ci.SetCurrentValue(avg_val);

                        foundFitting = true;
                        break;
                    }
                }


                if (foundFitting)
                {
                    // EWUnityExtensions.Log("Found fitting control!");
                }
                else
                {
                    // EWUnityExtensions.Log("Found NOOOO fitting control!");
                }
            }
        }