Esempio n. 1
0
        /// <summary>
        /// Closes the window.
        /// </summary>
        public virtual void Close()
        {
            BoolEventArgs e = new BoolEventArgs();

            this.OnClosing(e);
            if (e.Value)
            {
                this.Dispose();
                SDL_DestroyWindow(this.SDL_Window);
                this.OnClosed(new BaseEventArgs());
            }
        }
Esempio n. 2
0
 public virtual void Closing(BoolEventArgs e)
 {
 }
Esempio n. 3
0
        public static void EvaluateEvent(SDL_Event e)
        {
            if (e.window.windowID == 0)
            {
                return;
            }
            IntPtr sdlwindow = SDL_GetWindowFromID(e.window.windowID);
            Window w         = Windows.Find(win => win != null && win.SDL_Window == sdlwindow);

            if (w == null)
            {
                return;
            }
            int idx = Windows.IndexOf(w);

            // After closing a window, there are still a few more events like losing focus;
            // We can skip these as the window was already destroyed.
            if (w == null)
            {
                return;
            }
            if (OldMouseX == -1)
            {
                OldMouseX = e.motion.x;
            }
            if (OldMouseY == -1)
            {
                OldMouseY = e.motion.y;
            }
            if (e.type == SDL_EventType.SDL_WINDOWEVENT)
            {
                switch (e.window.windowevent)
                {
                case SDL_WindowEventID.SDL_WINDOWEVENT_CLOSE:
                    BoolEventArgs ClosingArgs = new BoolEventArgs();
                    w.OnClosing(ClosingArgs);
                    if (!ClosingArgs.Value)
                    {
                        SDL_DestroyWindow(w.SDL_Window);
                        w.OnClosed(new BaseEventArgs());
                        w.Dispose();
                        Windows[idx] = null;
                    }
                    break;

                case SDL_WindowEventID.SDL_WINDOWEVENT_MOVED:
                    break;

                case SDL_WindowEventID.SDL_WINDOWEVENT_RESIZED:
                    int width1;
                    int height1;
                    SDL_GetWindowSize(w.SDL_Window, out width1, out height1);
                    w.OnSizeChanged(new BaseEventArgs());
                    break;

                case SDL_WindowEventID.SDL_WINDOWEVENT_SIZE_CHANGED:
                    int width2;
                    int height2;
                    SDL_GetWindowSize(w.SDL_Window, out width2, out height2);
                    w.OnSizeChanged(new BaseEventArgs());
                    break;

                case SDL_WindowEventID.SDL_WINDOWEVENT_FOCUS_GAINED:
                    w.Focus = true;
                    w.OnFocusGained(new BaseEventArgs());
                    break;

                case SDL_WindowEventID.SDL_WINDOWEVENT_FOCUS_LOST:
                    w.Focus = false;
                    w.OnFocusLost(new BaseEventArgs());
                    break;
                }
            }
            if (Windows[idx] == null)
            {
                return;                       // Just disposed this window
            }
            if (w.Focus)
            {
                switch (e.type)
                {
                case SDL_EventType.SDL_MOUSEMOTION:
                    if (e.motion.x != OldMouseX || e.motion.y != OldMouseY)
                    {
                        LeftDown   = (e.button.button & 1) == 1;
                        MiddleDown = (e.button.button & 2) == 2;
                        RightDown  = (e.button.button & 4) == 4;
                        w.OnMouseMoving(new MouseEventArgs(e.motion.x, e.motion.y,
                                                           oldleftdown, LeftDown,
                                                           oldrightdown, RightDown,
                                                           oldmiddledown, MiddleDown));
                    }
                    OldMouseX = e.motion.x;
                    OldMouseY = e.motion.y;
                    break;

                case SDL_EventType.SDL_MOUSEBUTTONDOWN:
                    if (e.button.button == 1)
                    {
                        LeftDown = true;
                    }
                    if (e.button.button == 2)
                    {
                        MiddleDown = true;
                    }
                    if (e.button.button == 3)
                    {
                        RightDown = true;
                    }
                    w.OnMouseDown(new MouseEventArgs(e.motion.x, e.motion.y,
                                                     oldleftdown, LeftDown,
                                                     oldrightdown, RightDown,
                                                     oldmiddledown, MiddleDown));
                    break;

                case SDL_EventType.SDL_MOUSEBUTTONUP:
                    if (e.button.button == 1)
                    {
                        LeftDown = false;
                    }
                    if (e.button.button == 2)
                    {
                        MiddleDown = false;
                    }
                    if (e.button.button == 3)
                    {
                        RightDown = false;
                    }
                    w.OnMouseUp(new MouseEventArgs(e.motion.x, e.motion.y,
                                                   oldleftdown, LeftDown,
                                                   oldrightdown, RightDown,
                                                   oldmiddledown, MiddleDown));
                    break;

                case SDL_EventType.SDL_MOUSEWHEEL:
                    w.OnMouseWheel(new MouseEventArgs(OldMouseX, OldMouseY,
                                                      oldleftdown, LeftDown,
                                                      oldrightdown, RightDown,
                                                      oldmiddledown, MiddleDown,
                                                      e.wheel.x, e.wheel.y));
                    break;

                case SDL_EventType.SDL_KEYDOWN:
                    SDL_Keycode sym1 = e.key.keysym.sym;
                    Input.Register(sym1, true);
                    string txt       = "";
                    bool   backspace = false;
                    bool   delete    = false;
                    if (sym1 == SDL_Keycode.SDLK_RETURN)
                    {
                        txt = "\n";
                    }
                    if (sym1 == SDL_Keycode.SDLK_BACKSPACE)
                    {
                        backspace = true;
                    }
                    if (sym1 == SDL_Keycode.SDLK_DELETE)
                    {
                        delete = true;
                    }
                    if (txt.Length > 0 || backspace || delete)
                    {
                        w.OnTextInput(new TextEventArgs(txt, backspace, delete));
                    }
                    break;

                case SDL_EventType.SDL_KEYUP:
                    SDL_Keycode sym2 = e.key.keysym.sym;
                    Input.Register(sym2, false);
                    break;

                case SDL_EventType.SDL_TEXTINPUT:
                    byte[] bytes = new byte[32];
                    unsafe
                    {
                        byte *ptr  = e.text.text;
                        byte *data = ptr;
                        int   i    = 0;
                        while (*data != 0)
                        {
                            bytes[i] = *data;
                            data++;
                            i++;
                        }
                    }
                    string text = "";
                    foreach (char c in Encoding.UTF8.GetChars(bytes))
                    {
                        text += c;
                    }
                    w.OnTextInput(new TextEventArgs(text.TrimEnd('\x00')));
                    break;
                }
            }
        }