/// <inheritdoc />
        protected override void DataAvailable(object sender, DataAvailableEventArgs <EvEvent> e)
        {
            var data = e.Data;

            var eventType = (DigitizerEventType)data.Type;

            switch (eventType)
            {
            case DigitizerEventType.Syn:
                State = new StylusState(_currentTool, _currentPosition, _currentPressure, _currentDistance, _currentTilt);
                StylusUpdate?.Invoke(null, State);
                break;

            case DigitizerEventType.Key:
                var key   = (DigitizerEventKeyCode)data.Code;
                var state = (ButtonState)data.Value;

                ButtonStates[key] = state;

                switch (key)
                {
                case DigitizerEventKeyCode.BtnToolPen:
                    _currentTool = StylusTool.Pen;
                    ToolChanged?.Invoke(null, _currentTool);
                    break;

                case DigitizerEventKeyCode.BtnToolRubber:
                    _currentTool = StylusTool.Eraser;
                    ToolChanged?.Invoke(null, _currentTool);
                    break;

                case DigitizerEventKeyCode.BtnTouch:
                case DigitizerEventKeyCode.BtnStylus:
                case DigitizerEventKeyCode.BtnStylus2:
                    if (state == ButtonState.Pressed)
                    {
                        Pressed?.Invoke(null, key);
                    }
                    else
                    {
                        Released?.Invoke(null, key);
                    }
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(key), key, key.GetType().Name);
                }

                break;

            case DigitizerEventType.Abs:
                var eventCode = (DigitizerEventAbsCode)data.Code;

                switch (eventCode)
                {
                case DigitizerEventAbsCode.AbsX:
                    _currentPosition.X = data.Value;
                    break;

                case DigitizerEventAbsCode.AbsY:
                    _currentPosition.Y = data.Value;
                    break;

                case DigitizerEventAbsCode.AbsPressure:
                    _currentPressure = data.Value;
                    break;

                case DigitizerEventAbsCode.AbsDistance:
                    _currentDistance = data.Value;
                    break;

                case DigitizerEventAbsCode.AbsTiltX:
                    _currentTilt.X = data.Value;
                    break;

                case DigitizerEventAbsCode.AbsTiltY:
                    _currentTilt.Y = data.Value;
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(eventCode), eventCode, eventCode.GetType().Name);
                }

                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(eventType), eventType, eventType.GetType().Name);
            }
        }
Exemple #2
0
        /// <inheritdoc />
        protected override void DataAvailable(object sender, DataAvailableEventArgs <EvEvent> e)
        {
            var data = e.Data;

            var eventType = (DigitizerEventType)data.Type;

            switch (eventType)
            {
            case DigitizerEventType.Syn:
                State = new StylusState(_currentTool, _currentPosition, _currentPressure, _currentDistance,
                                        _currentTilt);
                StylusUpdate?.Invoke(null, State);

                if (_currentTool == StylusTool.None)
                {
                    _currentDistance = 255;
                    _currentPressure = 0;
                    _currentPosition = Point.Empty;
                    _currentTilt     = Point.Empty;
                }
                else if (_currentDistance > 0)
                {
                    _currentPressure = 0;
                }
                else if (_currentPressure > 0)
                {
                    _currentDistance = 0;
                }

                break;

            case DigitizerEventType.Key:
                var key   = (DigitizerEventKeyCode)data.Code;
                var state = (ButtonState)data.Value;

                ButtonStates[key] = state;

                switch (key)
                {
                case DigitizerEventKeyCode.ToolPen:
                    _currentTool = state == ButtonState.Pressed ? StylusTool.Pen : StylusTool.None;
                    ToolChanged?.Invoke(null, _currentTool);
                    break;

                case DigitizerEventKeyCode.ToolRubber:
                    _currentTool = state == ButtonState.Pressed ? StylusTool.Eraser : StylusTool.None;
                    ToolChanged?.Invoke(null, _currentTool);
                    break;

                case DigitizerEventKeyCode.Touch:
                    // Stylus touch input unreliable, but data is redundant
                    // because of ABS_PRESSURE
                    break;

                case DigitizerEventKeyCode.Stylus:
                case DigitizerEventKeyCode.Stylus2:
                    if (state == ButtonState.Pressed)
                    {
                        Pressed?.Invoke(null, key);
                    }
                    else
                    {
                        Released?.Invoke(null, key);
                    }
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(key), key, key.GetType().Name);
                }

                break;

            case DigitizerEventType.Abs:
                var eventCode = (DigitizerEventAbsCode)data.Code;

                switch (eventCode)
                {
                case DigitizerEventAbsCode.AbsX:
                    _currentPosition.X = data.Value;
                    break;

                case DigitizerEventAbsCode.AbsY:
                    _currentPosition.Y = data.Value;
                    break;

                case DigitizerEventAbsCode.AbsPressure:
                    _currentPressure = data.Value;
                    break;

                case DigitizerEventAbsCode.AbsDistance:
                    _currentDistance = data.Value;
                    break;

                case DigitizerEventAbsCode.AbsTiltX:
                    _currentTilt.X = data.Value;
                    break;

                case DigitizerEventAbsCode.AbsTiltY:
                    _currentTilt.Y = data.Value;
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(eventCode), eventCode,
                                                          eventCode.GetType().Name);
                }

                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(eventType), eventType, eventType.GetType().Name);
            }
        }