Exemple #1
0
        //left to do: maximizing/minimizing

        /// <summary>
        /// Handles all window related sdl events.
        /// </summary>
        internal static void WindowEvent(SDL.Event e)
        {
            switch (e.window.windowEvent)
            {
            case SDL.WindowEventID.WINDOWEVENT_SIZE_CHANGED:
                _Size = new Point(e.window.data1, e.window.data2);
                break;
            }
        }
Exemple #2
0
        internal static void InputEvent(SDL.Event e)
        {
            switch (e.type)
            {
            case SDL.EventType.MOUSEBUTTONDOWN:
            {
                CurrentState.Add((Button)(1000 + e.button.button));
                break;
            }

            case SDL.EventType.MOUSEBUTTONUP:
            {
                CurrentState.Remove((Button)(1000 + e.button.button));
                break;
            }

            case SDL.EventType.MOUSEWHEEL:
            {
                if (e.wheel.y > 0)
                {
                    CurrentState.Add(Button.MousewheelUp);
                }
                if (e.wheel.y < 0)
                {
                    CurrentState.Add(Button.MousewheelDown);
                }
                if (e.wheel.x < 0)
                {
                    CurrentState.Add(Button.MousewheelLeft);
                }
                if (e.wheel.x > 0)
                {
                    CurrentState.Add(Button.MousewheelRight);
                }
                break;
            }

            case SDL.EventType.MOUSEMOTION:
            {
                UntranslatedMousePosition = new Point(e.motion.x, e.motion.y);
                break;
            }

            case SDL.EventType.KEYDOWN:
            {
                if (e.key.repeat == 0)                         //we dont do repeats (yet?)
                {
                    int SDLKeyDown = (int)e.key.keysym.sym;
                    if (SDLKeyDown < 65536)
                    {
                        CurrentState.Add((Button)(2000 + SDLKeyDown));
                    }
                    else
                    {
                        CurrentState.Add((Button)(SDLKeyDown - 1073739381));
                    }
                }

                //textinput special keys
                switch (e.key.keysym.sym)
                {
                //backspace removes last character from all text input builders
                case SDL.Keycode.SDLK_BACKSPACE:
                    TextInputReceivers.ForEach(builder =>
                        {
                            int length = builder.Length;
                            if (length > 0)
                            {
                                builder.Remove(length - 1, 1);
                            }
                        });
                    break;

                case SDL.Keycode.SDLK_RETURN:
                    TextInputReceivers.ForEach(builder => builder.Append('\n'));
                    break;

                case SDL.Keycode.SDLK_TAB:
                    TextInputReceivers.ForEach(builder => builder.Append('\t'));
                    break;
                }

                break;
            }

            case SDL.EventType.KEYUP:
            {
                int SDLKeyUp = (int)e.key.keysym.sym;
                if (SDLKeyUp < 65536)
                {
                    CurrentState.Remove((Button)(2000 + SDLKeyUp));
                }
                else
                {
                    CurrentState.Remove((Button)(SDLKeyUp - 1073739381));
                }
                break;
            }

            case SDL.EventType.TEXTINPUT:
            {
                byte[] rawBytes = new byte[SDL.TEXTINPUTEVENT_TEXT_SIZE];
                unsafe { Marshal.Copy((IntPtr)e.text.text, rawBytes, 0, SDL.TEXTINPUTEVENT_TEXT_SIZE); }
                int    length = Array.IndexOf <byte>(rawBytes, 0);
                string text   = Encoding.UTF8.GetString(rawBytes, 0, length);
                //add text to all stringbuilders in TextInputReceivers
                TextInputReceivers.ForEach(builder => builder.Append(text));
                break;
            }

            case SDL.EventType.CONTROLLERDEVICEADDED:
            {
                //get first free gamepad slot
                int newGamepadID = -1;
                while (GamepadHandles[++newGamepadID] != IntPtr.Zero)
                {
                    ;
                }
                GamepadHandles[newGamepadID] = SDL.GameControllerOpen(e.cdevice.which);

                IntPtr joystick = SDL.GameControllerGetJoystick(GamepadHandles[newGamepadID]);
                GamepadInstanceIDs.Add(SDL.JoystickInstanceID(joystick), newGamepadID);
                GamepadCurrentStates[newGamepadID]  = new List <Button>();
                GamepadPreviousStates[newGamepadID] = new List <Button>();

                //init rumble if supported
                if (SDL.JoystickIsHaptic(joystick) == 1)
                {
                    IntPtr hapticHandle = SDL.HapticOpenFromJoystick(joystick);
                    if (SDL.HapticRumbleSupported(hapticHandle) == 1)
                    {
                        SDL.HapticRumbleInit(hapticHandle);
                        GamepadHapticHandles[newGamepadID] = hapticHandle;
                    }
                    else
                    {
                        SDL.HapticClose(hapticHandle);
                    }
                }
                break;
            }

            case SDL.EventType.CONTROLLERDEVICEREMOVED:
            {
                int gamepadID = GamepadInstanceIDs[e.cdevice.which];
                SDL.GameControllerClose(GamepadHandles[gamepadID]);
                GamepadHandles[gamepadID] = IntPtr.Zero;
                GamepadInstanceIDs.Remove(e.cdevice.which);

                if (GamepadHapticHandles[gamepadID] != IntPtr.Zero)
                {
                    SDL.HapticClose(GamepadHapticHandles[gamepadID]);
                    GamepadHapticHandles[gamepadID] = IntPtr.Zero;
                }
                break;
            }

            case SDL.EventType.CONTROLLERBUTTONDOWN:
            {
                GamepadCurrentStates[GamepadInstanceIDs[e.cbutton.which]].Add((Button)(3000 + e.cbutton.button));
                break;
            }

            case SDL.EventType.CONTROLLERBUTTONUP:
            {
                GamepadCurrentStates[GamepadInstanceIDs[e.cbutton.which]].Remove((Button)(3000 + e.cbutton.button));
                break;
            }

            case SDL.EventType.CONTROLLERAXISMOTION:
            {
                int   gamepad = GamepadInstanceIDs[e.caxis.which];
                byte  axis    = e.caxis.axis;
                float value   = (float)e.caxis.axisValue / short.MaxValue;

                GamepadAxisValues[gamepad, axis] = value;

                if (value <= -GamepadDeadZone)
                {
                    //add button if it's not added yet
                    Button button = (Button)(3015 + e.caxis.axis * 2);
                    if (!GamepadCurrentStates[gamepad].Contains(button))
                    {
                        GamepadCurrentStates[gamepad].Add(button);
                    }
                }
                else if (value >= GamepadDeadZone)
                {
                    Button button = (Button)(3016 + e.caxis.axis * 2);
                    if (!GamepadCurrentStates[gamepad].Contains(button))
                    {
                        GamepadCurrentStates[gamepad].Add(button);
                    }
                }
                else
                {
                    //remove both buttons for this axis if it's not in range
                    if (e.caxis.axis < 4)
                    {
                        GamepadCurrentStates[gamepad].Remove((Button)(3015 + e.caxis.axis * 2));
                    }
                    GamepadCurrentStates[gamepad].Remove((Button)(3016 + e.caxis.axis * 2));
                }
                break;
            }
            }
        }