Beispiel #1
0
        public virtual void OnCommandResponded(IntPtr respondheader, IntPtr respondData)
        {
            CommandPack pack = new CommandPack();

            try
            {
                pack = (CommandPack)Marshal.PtrToStructure(respondheader, typeof(CommandPack));
            }
            catch (Exception e)
            {
                Debug.LogException(e);
            }

            switch (pack.CommandId)
            {
            case CmdId.Cmd_AvatarCount:
            {
                int actorCount = BitConverter.ToInt32(pack.CmdParaments, 0);
                OnReceivedActorCount(actorCount);
            }
            break;

            case CmdId.Cmd_DataFrequency:
            {
                int actorId   = BitConverter.ToInt32(pack.CmdParaments, 0);
                int frequency = BitConverter.ToInt32(pack.CmdParaments, sizeof(int));

                NeuronActor actor = FindActiveActor(actorId);
                if (actor == null)
                {
                    actor = FindSuspendedActor(actorId);
                }

                if (actor != null)
                {
                    actor.OnReceivedDataFrequency(frequency);
                }
            }
            break;

            case CmdId.Cmd_BoneSize:
            {
                int actorId = BitConverter.ToInt32(pack.CmdParaments, 0);
                CmdResponseBoneSize[] boneSizes = new CmdResponseBoneSize[(int)pack.DataCount];

                // fill bone size
                IntPtr offset = respondData;
                Int64  len    = Marshal.SizeOf(boneSizes[0]);

                for (int i = 0; i < boneSizes.Length; i++)
                {
                    try
                    {
                        boneSizes[i] = (CmdResponseBoneSize)Marshal.PtrToStructure(offset, typeof(CmdResponseBoneSize));
                    }
                    catch (Exception e)
                    {
                        Debug.LogException(e);
                    }

                    offset = new IntPtr(offset.ToInt64() + len);
                }

                NeuronActor actor = FindActiveActor(actorId);
                if (actor == null)
                {
                    actor = FindSuspendedActor(actorId);
                }

                actor.OnReceivedBoneSizes(boneSizes);
            }
            break;

            case CmdId.Cmd_CombinationMode:
            {
                int actorId = BitConverter.ToInt32(pack.CmdParaments, 0);
                SensorCombinationModes combMode = (SensorCombinationModes)BitConverter.ToInt32(pack.CmdParaments, sizeof(int));
                NeuronActor            actor    = FindActiveActor(actorId);
                if (actor == null)
                {
                    actor = FindSuspendedActor(actorId);
                }

                actor.OnReceivedCombinationMode(combMode);
            }
            break;
            }
        }
        private void cmdDataReceived(IntPtr customObject, IntPtr sockRef, IntPtr header, IntPtr data)
        {
            _cmdDataHeader = (CommandPack)Marshal.PtrToStructure(header, typeof(CommandPack));

            switch (_cmdDataHeader.CommandId)
            {
            case CmdId.Cmd_BoneSize:
            {
                // Change the buffer length if necessory
                if (_cmdDataHeader.DataCount != _CmdDataBuffer.Length)
                {
                    _CmdDataBuffer = new CmdResponseBoneSize[_cmdDataHeader.DataCount];
                }

                int offset    = 0;
                int structLen = Marshal.SizeOf(_CmdDataBuffer[0]);

                for (int i = 0; i < (int)_cmdDataHeader.DataCount; i++)
                {
                    _CmdDataBuffer[i] = (CmdResponseBoneSize)Marshal.PtrToStructure(data + offset, typeof(CmdResponseBoneSize));
                    offset           += structLen;
                }

                txtCommandLog.Dispatcher.Invoke(new Action(delegate()
                    {
                        string log = "";
                        foreach (var item in _CmdDataBuffer)
                        {
                            log += item.BoneName + " : ";
                            log += item.BoneLength;
                            log += "\n";
                        }
                        txtCommandLog.Text = log;
                    }));
            }
            break;

            case CmdId.Cmd_AvatarName:
                break;

            case CmdId.Cmd_FaceDirection:
                break;

            case CmdId.Cmd_DataFrequency:
            {
                int avatarIndex = BitConverter.ToInt32(_cmdDataHeader.CmdParaments, 0);
                int datafreq    = BitConverter.ToInt32(_cmdDataHeader.CmdParaments, 4);

                txtCommandLog.Dispatcher.Invoke(new Action(delegate()
                    {
                        txtCommandLog.Text  = "avatarIndex : " + avatarIndex.ToString() + "\n";
                        txtCommandLog.Text += "dataFreq : " + datafreq.ToString() + "\n";
                    }));
            }
            break;

            case CmdId.Cmd_BvhInheritance:
            {
                if (_cmdDataHeader.DataCount <= 0)
                {
                    return;
                }
                byte[] bvhText = new byte[_cmdDataHeader.DataCount];

                Marshal.Copy(data, bvhText, 0, (int)_cmdDataHeader.DataCount);

                _CmdBvhInheritance = System.Text.Encoding.Default.GetString(bvhText);

                txtCommandLog.Dispatcher.Invoke(new Action(delegate()
                    {
                        txtCommandLog.Text = _CmdBvhInheritance;
                    }));
            }
            break;

            case CmdId.Cmd_AvatarCount:
            {
                int avatarCount = BitConverter.ToInt32(_cmdDataHeader.CmdParaments, 0);
                txtCommandLog.Dispatcher.Invoke(new Action(delegate()
                    {
                        txtCommandLog.Text = "AvatarCount : " + avatarCount.ToString();
                    }));
            }
            break;

            case CmdId.Cmd_CombinationMode:
            {
                int avatarIndex = BitConverter.ToInt32(_cmdDataHeader.CmdParaments, 0);
                SensorCombinationModes combMode = (SensorCombinationModes)BitConverter.ToInt32(_cmdDataHeader.CmdParaments, Marshal.SizeOf(avatarIndex));
                txtCommandLog.Dispatcher.Invoke(new Action(delegate()
                    {
                        switch (combMode)
                        {
                        case SensorCombinationModes.SC_ArmOnly:
                            txtCommandLog.Text = "Combination mode : " + "SC_ArmOnly";
                            break;

                        case SensorCombinationModes.SC_UpperBody:
                            txtCommandLog.Text = "Combination mode : " + "SC_UpperBody";
                            break;

                        case SensorCombinationModes.SC_FullBody:
                            txtCommandLog.Text = "Combination mode : " + "SC_FullBody";
                            break;
                        }
                    }));
            }
            break;
            }
        }