Ejemplo n.º 1
0
        private static unsafe void ProcessTextInputEvent(Sdl2NativeWindow window, TextInputEvent ev)
        {
            // Calculate the length of the typed text string
            int length;

            for (length = 0; length < TextInputEvent.TextSize && ev.Text[length] != '\0'; length++)
            {
                ;
            }

            // Make sure we have enough space to decode this string
            int decoded_length = Encoding.UTF8.GetCharCount(ev.Text, length);

            if (window.DecodeTextBuffer.Length < decoded_length)
            {
                Array.Resize(
                    ref window.DecodeTextBuffer,
                    2 * Math.Max(decoded_length, window.DecodeTextBuffer.Length));
            }

            // Decode the string from UTF8 to .Net UTF16
            fixed(char *pBuffer = window.DecodeTextBuffer)
            {
                decoded_length = System.Text.Encoding.UTF8.GetChars(
                    ev.Text,
                    length,
                    pBuffer,
                    window.DecodeTextBuffer.Length);
            }

            for (int i = 0; i < decoded_length; i++)
            {
                window.OnKeyPress(window.DecodeTextBuffer[i]);
            }
        }
Ejemplo n.º 2
0
        private static void ProcessMouseMotionEvent(Sdl2NativeWindow window, MouseMotionEvent ev)
        {
            float scale = window.ClientSize.Width / (float)window.Size.Width;

            window.OnMouseMove(
                (int)Math.Round(ev.X * scale),
                (int)Math.Round(ev.Y * scale));
            Sdl2Mouse.Scale = scale;
        }
Ejemplo n.º 3
0
        private static void ProcessKeyEvent(Sdl2NativeWindow window, Event ev)
        {
            bool key_pressed = ev.Key.State == State.Pressed;
            Key  key         = TranslateKey(ev.Key.Keysym.Scancode);

            if (key_pressed)
            {
                window.OnKeyDown(key, ev.Key.Repeat > 0);
            }
            else
            {
                window.OnKeyUp(key);
            }
        }
Ejemplo n.º 4
0
        private static void ProcessMouseButtonEvent(Sdl2NativeWindow window, MouseButtonEvent ev)
        {
            bool button_pressed = ev.State == State.Pressed;

            // We need MouseUp events to be reported even if they occur
            // outside the window. CaptureMouse ensures we get them.
            if (!window.is_cursor_grabbed)
            {
                SDL.CaptureMouse(button_pressed);
            }

            MouseButton button = Sdl2Mouse.TranslateButton(ev.Button);

            if (button_pressed)
            {
                window.OnMouseDown(button);
            }
            else
            {
                window.OnMouseUp(button);
            }
        }
Ejemplo n.º 5
0
        private static void ProcessWindowEvent(Sdl2NativeWindow window, WindowEvent e)
        {
            switch (e.Event)
            {
            case WindowEventID.CLOSE:
                var close_args = new System.ComponentModel.CancelEventArgs();
                try
                {
                    window.is_in_closing_event = true;
                    window.OnClosing(close_args);
                }
                finally
                {
                    window.is_in_closing_event = false;
                }

                if (!close_args.Cancel)
                {
                    window.OnClosed(EventArgs.Empty);
                    window.must_destroy = true;
                }
                break;

            case WindowEventID.ENTER:
                window.OnMouseEnter(EventArgs.Empty);
                break;

            case WindowEventID.LEAVE:
                window.OnMouseLeave(EventArgs.Empty);
                break;

            case WindowEventID.EXPOSED:
                // do nothing
                break;

            case WindowEventID.FOCUS_GAINED:
                window.is_focused = true;
                window.OnFocusedChanged(EventArgs.Empty);
                break;

            case WindowEventID.FOCUS_LOST:
                window.is_focused = false;
                window.OnFocusedChanged(EventArgs.Empty);
                break;

            case WindowEventID.HIDDEN:
                window.is_visible = false;
                window.OnVisibleChanged(EventArgs.Empty);
                break;

            case WindowEventID.SHOWN:
                window.is_visible = true;
                window.OnVisibleChanged(EventArgs.Empty);
                break;

            case WindowEventID.MAXIMIZED:
                window.window_state = WindowState.Maximized;
                window.OnWindowStateChanged(EventArgs.Empty);
                break;

            case WindowEventID.MINIMIZED:
                window.previous_window_state = window.window_state;
                window.window_state          = WindowState.Minimized;
                window.OnWindowStateChanged(EventArgs.Empty);
                break;

            case WindowEventID.RESTORED:
                window.window_state = window.previous_window_state;
                window.OnWindowStateChanged(EventArgs.Empty);
                break;

            case WindowEventID.MOVED:
                window.OnMove(EventArgs.Empty);
                break;

            case WindowEventID.RESIZED:
            case WindowEventID.SIZE_CHANGED:
                window.OnResize(EventArgs.Empty);
                break;

            default:
                Debug.Print("SDL2 unhandled event: {0}", e.Type);
                break;
            }
        }
Ejemplo n.º 6
0
        private static unsafe void ProcessDropEvent(Sdl2NativeWindow window, DropEvent ev)
        {
            string dropString = PtrToStringAuto(ev.File);

            window.OnFileDrop(new[] { dropString });
        }
Ejemplo n.º 7
0
 private static void ProcessMouseWheelEvent(Sdl2NativeWindow window, MouseWheelEvent ev)
 {
     window.OnMouseWheel(ev.X, ev.Y, false);
 }
Ejemplo n.º 8
0
        private int ProcessEvent(ref Event ev)
        {
            bool processed = false;

            try
            {
                Sdl2NativeWindow window = null;

                switch (ev.Type)
                {
                case EventType.WINDOWEVENT:
                    if (windows.TryGetValue(ev.Window.WindowID, out window))
                    {
                        ProcessWindowEvent(window, ev.Window);
                        processed = true;
                    }
                    break;

                case EventType.TEXTINPUT:
                    if (windows.TryGetValue(ev.Text.WindowID, out window))
                    {
                        ProcessTextInputEvent(window, ev.Text);
                        processed = true;
                    }
                    break;

                case EventType.KEYDOWN:
                case EventType.KEYUP:
                    if (windows.TryGetValue(ev.Key.WindowID, out window))
                    {
                        ProcessKeyEvent(window, ev);
                        processed = true;
                    }
                    break;

                case EventType.MOUSEBUTTONDOWN:
                case EventType.MOUSEBUTTONUP:
                    if (windows.TryGetValue(ev.Button.WindowID, out window))
                    {
                        ProcessMouseButtonEvent(window, ev.Button);
                        processed = true;
                    }
                    break;

                case EventType.MOUSEMOTION:
                    if (windows.TryGetValue(ev.Motion.WindowID, out window))
                    {
                        ProcessMouseMotionEvent(window, ev.Motion);
                        processed = true;
                    }
                    break;

                case EventType.MOUSEWHEEL:
                    if (windows.TryGetValue(ev.Wheel.WindowID, out window))
                    {
                        ProcessMouseWheelEvent(window, ev.Wheel);
                        processed = true;
                    }
                    break;

                case EventType.DROPFILE:
                    if (windows.TryGetValue(ev.Drop.WindowID, out window))
                    {
                        ProcessDropEvent(window, ev.Drop);
                        SDL.Free(ev.Drop.File);
                        processed = true;
                    }
                    break;

                case EventType.QUIT:
                    Debug.WriteLine("Sdl2 application quit");
                    break;
                }
            }
            catch (Exception ex)
            {
                Debug.Print(ex.ToString());
            }

            return(processed ? 0 : 1);
        }