Exemple #1
0
 void ProcessTouchMoveEvent(TouchState[] touches)
 {
     for (var i = 0; i < touches.Length; i++)
     {
         UnityInputSystem.QueueStateEvent(RemoteTouchscreen, touches[i]);
     }
 }
Exemple #2
0
 void ProcessMouseMoveEvent(short x, short y, byte button)
 {
     UnityEngine.Vector2Int pos   = new UnityEngine.Vector2Int(x, y);
     UnityEngine.Vector2Int delta = pos - m_prevMousePos;
     UnityInputSystem.QueueStateEvent(RemoteMouse, new MouseState {
         position = pos,
         delta    = delta,
         buttons  = button
     });
     m_prevMousePos = pos;
 }
Exemple #3
0
        public static RemoteInput Create()
        {
            InputUser user = InputUser.CreateUserWithoutPairedDevices();

            user = InputUser.PerformPairingWithDevice(UnityInputSystem.AddDevice <Mouse>(), user);
            user = InputUser.PerformPairingWithDevice(UnityInputSystem.AddDevice <Keyboard>(), user);
            user = InputUser.PerformPairingWithDevice(UnityInputSystem.AddDevice <Gamepad>(), user);
            user = InputUser.PerformPairingWithDevice(UnityInputSystem.AddDevice <Touchscreen>(), user);
            RemoteInput remoteInput = new RemoteInput(ref user);

            s_mapRemoteInputAndInputUserId.Add(remoteInput, user.id);
            s_listRemoteInput.Add(remoteInput);

            return(remoteInput);
        }
Exemple #4
0
        void ChangeEndStateUnusedTouches(TouchState[] touches)
        {
            int touchCount = Touch.activeTouches.Count;

            for (var i = 0; i < touchCount; i++)
            {
                int touchId = Touch.activeTouches[i].touchId;
                if (!Array.Exists(touches, v => v.touchId == touchId))
                {
                    if (Touch.activeTouches[i].phase == TouchPhase.Ended)
                    {
                        continue;
                    }
                    UnityInputSystem.QueueStateEvent(RemoteTouchscreen, new TouchState
                    {
                        touchId  = touchId,
                        phase    = TouchPhase.Ended,
                        position = Touch.activeTouches[i].screenPosition
                    });
                }
            }
        }
Exemple #5
0
        void ProcessKeyEvent(KeyboardEventType state, bool repeat, byte keyCode, char character)
        {
            switch (state)
            {
            case KeyboardEventType.KeyDown:
                if (!repeat)
                {
                    m_keyboardState.Set((Key)keyCode, true);
                    UnityInputSystem.QueueStateEvent(RemoteKeyboard, m_keyboardState);
                }
                if (character != 0)
                {
                    UnityInputSystem.QueueTextEvent(RemoteKeyboard, character);
                }
                break;

            case KeyboardEventType.KeyUp:
                m_keyboardState.Set((Key)keyCode, false);
                UnityInputSystem.QueueStateEvent(RemoteKeyboard, m_keyboardState);
                break;
            }
        }
Exemple #6
0
        internal static void Delete(RemoteInput remoteInput)
        {
            if (remoteInput == null)
            {
                throw new ArgumentException("The instance of argument is null");
            }
            bool found = s_mapRemoteInputAndInputUserId.TryGetValue(remoteInput, out uint userId);

            if (!found)
            {
                throw new ArgumentException("The instance of argument is not found");
            }
            InputUser user          = InputUser.all.First(_user => _user.id == userId);
            var       arrayDeviceId = user.pairedDevices.Select(device => device.deviceId).ToArray();

            user.UnpairDevicesAndRemoveUser();
            foreach (var deviceId in arrayDeviceId)
            {
                UnityInputSystem.RemoveDevice(UnityInputSystem.GetDeviceById(deviceId));
            }
            s_mapRemoteInputAndInputUserId.Remove(remoteInput);
            s_listRemoteInput.Remove(remoteInput);
        }
Exemple #7
0
 void ProcessMouseWheelEvent(float scrollX, float scrollY)
 {
     UnityInputSystem.QueueStateEvent(RemoteMouse, new MouseState {
         scroll = new UnityEngine.Vector2(scrollX, scrollY)
     });
 }
Exemple #8
0
//---------------------------------------------------------------------------------------------------------------------

        public void ProcessInput(byte[] bytes)
        {
            if (bytes == null)
            {
                throw new ArgumentNullException();
            }
            if (bytes.Length == 0)
            {
                throw new ArgumentException("byte length is zero");
            }

            switch ((EventType)bytes[0])
            {
            case EventType.Keyboard:
                var type      = (KeyboardEventType)bytes[1];
                var repeat    = bytes[2] == 1;
                var key       = bytes[3];
                var character = (char)bytes[4];
                ProcessKeyEvent(type, repeat, key, character);
                break;

            case EventType.Mouse:
                var deltaX = BitConverter.ToInt16(bytes, 1);
                var deltaY = BitConverter.ToInt16(bytes, 3);
                var button = bytes[5];
                ProcessMouseMoveEvent(deltaX, deltaY, button);
                break;

            case EventType.MouseWheel:
                var scrollX = BitConverter.ToSingle(bytes, 1);
                var scrollY = BitConverter.ToSingle(bytes, 5);
                ProcessMouseWheelEvent(scrollX, scrollY);
                break;

            case EventType.Touch:
            {
                var length  = bytes[1];
                var index   = 2;
                var touches = new TouchState[length];
                for (int i = 0; i < length; i++)
                {
                    const int INPUTSYSTEM_ZERO_ID_GUARD = 128;         //ID 0 is reserved by inputsystem
                    int       identifier = BitConverter.ToInt32(bytes, index) + INPUTSYSTEM_ZERO_ID_GUARD;
                    index += 4;
                    var phase = (UnityEngine.InputSystem.TouchPhase)bytes[index];
                    index += 1;
                    var pageX = BitConverter.ToInt16(bytes, index);
                    index += 2;
                    var pageY = BitConverter.ToInt16(bytes, index);
                    index += 2;
                    var force = BitConverter.ToSingle(bytes, index);
                    index     += 4;
                    touches[i] = new TouchState
                    {
                        touchId  = identifier,
                        phase    = phase,
                        position = new UnityEngine.Vector2Int(pageX, pageY),
                        pressure = force
                    };
                }
                ProcessTouchMoveEvent(touches);
                if (Touch.activeTouches.Count > length)
                {
                    ChangeEndStateUnusedTouches(touches);
                }
            }

            break;

            case EventType.ButtonClick:
                var elementId = BitConverter.ToInt16(bytes, 1);
                ProcessButtonClickEvent(elementId);
                break;

            case EventType.Gamepad:
            {
                GamepadEventType gamepadEventType = (GamepadEventType)bytes[1];

                switch (gamepadEventType)
                {
                case GamepadEventType.ButtonDown:
                case GamepadEventType.ButtonUp:
                case GamepadEventType.ButtonPressed:
                {
                    var buttonIndex = bytes[2];
                    var value       = BitConverter.ToDouble(bytes, 3);
                    ProcessGamepadButtonEvent(gamepadEventType, (GamepadKeyCode)buttonIndex, value);
                }
                break;

                case GamepadEventType.Axis:
                {
                    var buttonIndex = bytes[2];
                    var x           = BitConverter.ToDouble(bytes, 3);
                    var y           = BitConverter.ToDouble(bytes, 11);
                    ProcessGamepadAxisEvent(x, y, (GamepadKeyCode)buttonIndex);
                }
                break;
                }
                UnityInputSystem.QueueStateEvent(RemoteGamepad, m_gamepadState);
            }
            break;
            }
        }