Exemple #1
0
    public void PumpEvents(double deltaSeconds)
    {
        SetTitle();
        Sdl2Events.ProcessEvents();
        var snapshot = _window.PumpEvents();

        if (_window != null)
        {
            if (_pendingCursorUpdate.HasValue && _window.Focused)
            {
                using (PerfTracker.FrameEvent("3 Warping mouse"))
                {
                    Sdl2Native.SDL_WarpMouseInWindow(
                        _window.SdlWindowHandle,
                        (int)_pendingCursorUpdate.Value.X,
                        (int)_pendingCursorUpdate.Value.Y);

                    _pendingCursorUpdate = null;
                }
            }

            using (PerfTracker.FrameEvent("4 Raising input event"))
                Raise(new InputEvent(deltaSeconds, snapshot, _window.MouseDelta));
        }
    }
Exemple #2
0
 public WindowHolder()
 {
     On <SetCursorPositionEvent>(e => _pendingCursorUpdate = new Vector2(e.X, e.Y));
     On <ToggleFullscreenEvent>(e => ToggleFullscreenState());
     On <ToggleHardwareCursorEvent>(e => { if (_window != null)
                                           {
                                               _window.CursorVisible = !_window.CursorVisible;
                                           }
                                    });
     On <ToggleResizableEvent>(e => { if (_window != null)
                                      {
                                          _window.Resizable = !_window.Resizable;
                                      }
                               });
     On <ToggleVisibleBorderEvent>(e => { if (_window != null)
                                          {
                                              _window.BorderVisible = !_window.BorderVisible;
                                          }
                                   });
     On <ConfineMouseToWindowEvent>(e => { if (_window != null)
                                           {
                                               Sdl2Native.SDL_SetWindowGrab(_window.SdlWindowHandle, e.Enabled);
                                           }
                                    });
     On <SetRelativeMouseModeEvent>(e =>
     {
         if (_window == null)
         {
             return;
         }
         Sdl2Native.SDL_SetRelativeMouseMode(e.Enabled);
         if (!e.Enabled)
         {
             Sdl2Native.SDL_WarpMouseInWindow(_window.SdlWindowHandle, _window.Width / 2, _window.Height / 2);
         }
     });
     _windowManager = AttachChild(new WindowManager());
 }
        public bool UpdateInput(Sdl2Window window, float dt)
        {
            if (DisableAllInput)
            {
                //oldWheel = Mouse.GetState(game.Window).ScrollWheelValue;
                return false;
            }

            float clampedLerpF = Utils.Clamp(30 * dt, 0, 1);

            mousePos = new Vector2(Utils.Lerp(oldMouse.X, InputTracker.MousePosition.X, clampedLerpF),
                Utils.Lerp(oldMouse.Y, InputTracker.MousePosition.Y, clampedLerpF));



            //KeyboardState keyboard = DBG.EnableKeyboardInput ? Keyboard.GetState() : DBG.DisabledKeyboardState;
            //int currentWheel = mouse.ScrollWheelValue;

            //bool mouseInWindow = MapStudio.Active && mousePos.X >= game.ClientBounds.Left && mousePos.X < game.ClientBounds.Right && mousePos.Y > game.ClientBounds.Top && mousePos.Y < game.ClientBounds.Bottom;

            currentClickType = MouseClickType.None;

            if (InputTracker.GetMouseButton(Veldrid.MouseButton.Left))
                currentClickType = MouseClickType.Left;
            else if (InputTracker.GetMouseButton(Veldrid.MouseButton.Right))
                currentClickType = MouseClickType.Right;
            else if (InputTracker.GetMouseButton(Veldrid.MouseButton.Middle))
                currentClickType = MouseClickType.Middle;
            else if (InputTracker.GetMouseButton(Veldrid.MouseButton.Button1))
                currentClickType = MouseClickType.Extra1;
            else if (InputTracker.GetMouseButton(Veldrid.MouseButton.Button2))
                currentClickType = MouseClickType.Extra2;
            else
                currentClickType = MouseClickType.None;

            currentMouseClickL = currentClickType == MouseClickType.Left;
            currentMouseClickR = currentClickType == MouseClickType.Right;
            currentMouseClickM = currentClickType == MouseClickType.Middle;

            if (currentClickType != MouseClickType.None && oldClickType == MouseClickType.None)
                currentMouseClickStartedInWindow = true;

            if (currentClickType == MouseClickType.None)
            {
                // If nothing is pressed, just dont bother lerping
                //mousePos = new Vector2(mouse.X, mouse.Y);
                if (MousePressed)
                {
                    mousePos = InputTracker.MousePosition;
                    Sdl2Native.SDL_WarpMouseInWindow(window.SdlWindowHandle, (int)MousePressedPos.X, (int)MousePressedPos.Y);
                    Sdl2Native.SDL_SetWindowGrab(window.SdlWindowHandle, false);
                    Sdl2Native.SDL_ShowCursor(1);
                    MousePressed = false;
                }
                return false;
            }

            bool isSpeedupKeyPressed = InputTracker.GetKey(Veldrid.Key.LShift) || InputTracker.GetKey(Veldrid.Key.RShift);
            bool isSlowdownKeyPressed = InputTracker.GetKey(Veldrid.Key.LControl) || InputTracker.GetKey(Veldrid.Key.RControl);
            bool isResetKeyPressed = InputTracker.GetKey(Veldrid.Key.R);
            bool isMoveLightKeyPressed = InputTracker.GetKey(Veldrid.Key.Space);
            bool isOrbitCamToggleKeyPressed = false;// keyboard.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.F);
            bool isPointCamAtObjectKeyPressed = false;// keyboard.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.T);


            if (!currentMouseClickStartedInWindow)
            {
                oldMouse = mousePos;

                if (IsOrbitCam)
                {
                    OrbitCamDistance = Math.Max(OrbitCamDistance, SHITTY_CAM_ZOOM_MIN_DIST);

                    var distanceVectorAfterMove = -Vector3.Transform(Vector3.UnitX, CameraTransform.RotationMatrixXYZ * Matrix4x4.CreateRotationY(Utils.Pi)) * new Vector3(-1, 1, 1);
                    CameraTransform.Position = (OrbitCamCenter + (distanceVectorAfterMove * (OrbitCamDistance * OrbitCamDistance)));


                }
                else
                {
                    var eu = CameraTransform.EulerRotation;
                    eu.X = Utils.Clamp(CameraTransform.EulerRotation.X, -Utils.PiOver2, Utils.PiOver2);
                    CameraTransform.EulerRotation = eu;
                }

                LightRotation.X = Utils.Clamp(LightRotation.X, -Utils.PiOver2, Utils.PiOver2);

                oldClickType = currentClickType;

                oldMouseClickL = currentMouseClickL;
                oldMouseClickR = currentMouseClickR;
                oldMouseClickM = currentMouseClickM;

                oldOrbitCamToggleKeyPressed = isOrbitCamToggleKeyPressed;

                return true;
            }

            if (currentMouseClickM && !oldMouseClickM && IsOrbitCam)
            {
                OrbitCamReset();
            }


            if (isResetKeyPressed && !oldResetKeyPressed)
            {
                ResetCameraLocation();
            }

            oldResetKeyPressed = isResetKeyPressed;

            if (isOrbitCamToggleKeyPressed && !oldOrbitCamToggleKeyPressed)
            {
                if (!IsOrbitCam)
                {
                    CameraOrigin.Position.Y = CameraPositionDefault.Position.Y;
                    OrbitCamDistance = (CameraOrigin.Position - (CameraTransform.Position)).Length();
                }
                IsOrbitCam = !IsOrbitCam;
            }

            if (isPointCamAtObjectKeyPressed)
            {
                PointCameraToLocation(CameraPositionDefault.Position);
            }

            float moveMult = dt * CameraMoveSpeed;

            if (isSpeedupKeyPressed)
            {
                moveMult = dt * CameraMoveSpeedFast;
            }

            if (isSlowdownKeyPressed)
            {
                moveMult = dt * CameraMoveSpeedSlow;
            }

            var cameraDist = CameraOrigin.Position - CameraTransform.Position;

            if (IsOrbitCam)
            {
                if (currentMouseClickL)
                {
                    float x = 0;
                    float z = 0;
                    float y = 0;

                    if (InputTracker.GetKeyDown(Veldrid.Key.W) && Math.Abs(cameraDist.Length()) > 0.1f)
                        z += 1;
                    if (InputTracker.GetKeyDown(Veldrid.Key.S))
                        z -= 1;
                    if (InputTracker.GetKeyDown(Veldrid.Key.E))
                        y += 1;
                    if (InputTracker.GetKeyDown(Veldrid.Key.Q))
                        y -= 1;
                    if (InputTracker.GetKeyDown(Veldrid.Key.A))
                        x -= 1;
                    if (InputTracker.GetKeyDown(Veldrid.Key.D))
                        x += 1;


                    if (Math.Abs(cameraDist.Length()) <= SHITTY_CAM_ZOOM_MIN_DIST)
                    {
                        z = Math.Min(z, 0);
                    }

                }
                else if (currentMouseClickR)
                {
                    MoveCamera_OrbitCenterPoint_MouseDelta(mousePos, oldMouse);
                    //Vector2 mouseDelta = mousePos - oldMouse;
                    //MoveCamera_OrbitCenterPoint(-mouseDelta.X, mouseDelta.Y, 0, moveMult);
                }


                //if (GFX.LastViewport.Bounds.Contains(mouse.Position))
                //    OrbitCamDistance -= ((currentWheel - oldWheel) / 150f) * 0.25f;

            }
            else
            {
                float x = 0;
                float y = 0;
                float z = 0;

                if (InputTracker.GetKey(Veldrid.Key.D))
                    x += 1;
                if (InputTracker.GetKey(Veldrid.Key.A))
                    x -= 1;
                if (InputTracker.GetKey(Veldrid.Key.E))
                    y += 1;
                if (InputTracker.GetKey(Veldrid.Key.Q))
                    y -= 1;
                if (InputTracker.GetKey(Veldrid.Key.W))
                    z += 1;
                if (InputTracker.GetKey(Veldrid.Key.S))
                    z -= 1;

                MoveCamera(x, y, z, moveMult);
            }

            if (currentMouseClickR)
            {

                if (!MousePressed)
                {
                    var x = InputTracker.MousePosition.X;
                    var y = InputTracker.MousePosition.Y;
                    if (x >= BoundingRect.Left && x < BoundingRect.Right && y >= BoundingRect.Top && y < BoundingRect.Bottom)
                    {
                        MousePressed = true;
                        MousePressedPos = InputTracker.MousePosition;
                        Sdl2Native.SDL_ShowCursor(0);
                        Sdl2Native.SDL_SetWindowGrab(window.SdlWindowHandle, true);
                    }
                }
                else
                {
                    Vector2 mouseDelta = MousePressedPos - InputTracker.MousePosition;
                    Sdl2Native.SDL_WarpMouseInWindow(window.SdlWindowHandle, (int)MousePressedPos.X, (int)MousePressedPos.Y);

                    if (mouseDelta.LengthSquared() == 0)
                    {
                        // Prevents a meme
                        //oldWheel = currentWheel;
                        return true;
                    }

                    //Mouse.SetPosition(game.ClientBounds.X + game.ClientBounds.Width / 2, game.ClientBounds.Y + game.ClientBounds.Height / 2);



                    float camH = mouseDelta.X * 1 * CameraTurnSpeedMouse * 0.0160f;
                    float camV = mouseDelta.Y * -1 * CameraTurnSpeedMouse * 0.0160f;

                    if (IsOrbitCam && !isMoveLightKeyPressed)
                    {
                        if (CameraTransform.EulerRotation.X >= Utils.PiOver2 * SHITTY_CAM_PITCH_LIMIT_FATCAT)
                        {
                            camV = Math.Min(camV, 0);
                        }
                        if (CameraTransform.EulerRotation.X <= -Utils.PiOver2 * SHITTY_CAM_PITCH_LIMIT_FATCAT)
                        {
                            camV = Math.Max(camV, 0);
                        }

                        RotateCameraOrbit(camH, camV, Utils.PiOver2);
                        //PointCameraToModel();
                    }
                    else if (isMoveLightKeyPressed)
                    {
                        LightRotation.Y += camH;
                        LightRotation.X -= camV;
                    }
                    else
                    {
                        var eu = CameraTransform.EulerRotation;
                        eu.Y -= camH;
                        eu.X += camV;
                        CameraTransform.EulerRotation = eu;
                    }
                }


                //CameraTransform.Rotation.Z -= (float)Math.Cos(MathHelper.PiOver2 - CameraTransform.Rotation.Y) * camV;

                //RotateCamera(mouseDelta.Y * -0.01f * (float)moveMult, 0, 0, moveMult);
                //RotateCamera(0, mouseDelta.X * 0.01f * (float)moveMult, 0, moveMult);
            }
            else
            {
                if (MousePressed)
                {
                    Sdl2Native.SDL_WarpMouseInWindow(window.SdlWindowHandle, (int)MousePressedPos.X, (int)MousePressedPos.Y);
                    Sdl2Native.SDL_SetWindowGrab(window.SdlWindowHandle, false);
                    Sdl2Native.SDL_ShowCursor(1);
                    MousePressed = false;
                }
                if (IsOrbitCam)
                {
                    RotateCameraOrbit(0, 0, Utils.PiOver2);
                }

                if (oldMouseClickL)
                {
                    //Mouse.SetPosition((int)oldMouse.X, (int)oldMouse.Y);
                }
                //game.IsMouseVisible = true;
            }


            if (IsOrbitCam)
            {
                OrbitCamDistance = Math.Max(OrbitCamDistance, SHITTY_CAM_ZOOM_MIN_DIST);

                var distanceVectorAfterMove = -Vector3.Transform(Vector3.UnitX, CameraTransform.RotationMatrixXYZ * Matrix4x4.CreateRotationY(Utils.Pi)) * new Vector3(-1, 1, 1);
                CameraTransform.Position = (OrbitCamCenter + (distanceVectorAfterMove * (OrbitCamDistance * OrbitCamDistance)));
            }
            else
            {
                var eu = CameraTransform.EulerRotation;
                eu.X = Utils.Clamp(CameraTransform.EulerRotation.X, -Utils.PiOver2, Utils.PiOver2);
                CameraTransform.EulerRotation = eu;
            }


            LightRotation.X = Utils.Clamp(LightRotation.X, -Utils.PiOver2, Utils.PiOver2);

            oldClickType = currentClickType;

            oldMouseClickL = currentMouseClickL;
            oldMouseClickR = currentMouseClickR;
            oldMouseClickM = currentMouseClickM;

            oldOrbitCamToggleKeyPressed = isOrbitCamToggleKeyPressed;

            oldMouse = mousePos;
            return true;
        }
Exemple #4
0
        public void Run()
        {
            ChangeBackend();
            PerfTracker.StartupEvent("Set up backend");
            Sdl2Native.SDL_Init(SDLInitFlags.GameController);
            ImGui.StyleColorsClassic();
            Raise(new WindowResizedEvent(Window.Width, Window.Height));
            Raise(new BeginFrameEvent());

            var frameCounter = new FrameCounter();

            PerfTracker.StartupEvent("Startup done, rendering first frame");
            while (!_done)
            {
                ChangeBackend();

                PerfTracker.BeginFrame();
                double deltaSeconds = frameCounter.StartFrame();
                using (PerfTracker.FrameEvent("1 Raising begin frame"))
                    Raise(new BeginFrameEvent());

                InputSnapshot snapshot;
                using (PerfTracker.FrameEvent("2 Processing SDL events"))
                {
                    Sdl2Events.ProcessEvents();
                    snapshot = Window.PumpEvents();
                }

                if (!Window.Exists)
                {
                    break;
                }

                if (_pendingCursorUpdate.HasValue)
                {
                    using (PerfTracker.FrameEvent("3 Warping mouse"))
                    {
                        Sdl2Native.SDL_WarpMouseInWindow(
                            Window.SdlWindowHandle,
                            (int)_pendingCursorUpdate.Value.X,
                            (int)_pendingCursorUpdate.Value.Y);

                        _pendingCursorUpdate = null;
                    }
                }

                using (PerfTracker.FrameEvent("4 Raising input event"))
                    Raise(new InputEvent(deltaSeconds, snapshot, Window.MouseDelta));

                using (PerfTracker.FrameEvent("5 Performing update"))
                    Update((float)deltaSeconds);

                if (!Window.Exists)
                {
                    break;
                }

                using (PerfTracker.FrameEvent("6 Drawing"))
                    Draw();

                var flags = Resolve <IEngineSettings>().Flags;
                if (GraphicsDevice.SyncToVerticalBlank != flags.HasFlag(EngineFlags.VSync))
                {
                    GraphicsDevice.SyncToVerticalBlank = flags.HasFlag(EngineFlags.VSync);
                }

                using (PerfTracker.FrameEvent("7 Swap buffers"))
                {
                    CoreTrace.Log.Info("Engine", "Swapping buffers...");
                    GraphicsDevice.SwapBuffers();
                    CoreTrace.Log.Info("Engine", "Draw complete");
                }
            }

            DestroyAllObjects();
            GraphicsDevice.Dispose();
            Window.Close();
        }