示例#1
0
 private void MainWindowUpdateFrame(FrameEventArgs obj)
 {
     // Use seperate locks for update and render to avoid stuttering
     lock (_updateFrameLock)
     {
         var userEvent = new UserInputStateEvent(KeyboardState.GetSnapshot(), MouseState.GetSnapshot(), obj.Time);
         var task      = Task.Run(async() =>
         {
             await EventChannel.Writer.WriteAsync(userEvent, _cancellationTokenManager.Token).AsTask();
         });
         task.Wait(_cancellationTokenManager.Token);
     }
 }
示例#2
0
        private async Task ProcessUserEvent(UserInputStateEvent userInputEvent)
        {
            if (userInputEvent.MouseState.IsButtonDown(MouseButton.Button1) && !pickingInfoRequested)
            {
                await _scene.AssertionUpdatesChannel.Writer.WriteAsync(new AssertionBatch
                {
                    Assertions = new IAssertion[]
                    {
                        new GetPickingInfo((int)userInputEvent.MouseState.X, (int)userInputEvent.MouseState.Y)
                    }
                });

                pickingInfoRequested = true;
            }

            // Escape = quit
            if (userInputEvent.KeyboardState.WasKeyDown(Keys.Escape))
            {
                if (!exitCalled)
                {
                    exitCalled = true;
                    _renderWindow.Close();
                }
            }

            // Take screenshot
            if (userInputEvent.KeyboardState.WasKeyDown(Keys.S))
            {
                if (!screenshotEnqueued)
                {
                    await _scene.AssertionUpdatesChannel.Writer.WriteAsync(new AssertionBatch
                    {
                        Assertions = new IAssertion[]
                        {
                            new GrabScreenshot()
                        }
                    });

                    screenshotEnqueued = true;
                }
            }

            // Move left & right
            bool hasLeft     = userInputEvent.KeyboardState.IsKeyDown(Keys.Left);
            bool hasRight    = userInputEvent.KeyboardState.IsKeyDown(Keys.Right);
            bool hasForward  = userInputEvent.KeyboardState.IsKeyDown(Keys.Up);
            bool hasBackward = userInputEvent.KeyboardState.IsKeyDown(Keys.Down);

            // TODO: Cleanup
            if (hasLeft || hasRight || hasForward || hasBackward)
            {
                var dX = (hasLeft, hasRight) switch {
                    (true, false) => 1.0f,
                    (false, true) => - 1.0f,
                    _ => 0.0f
                };

                var dZ = (hasForward, hasBackward) switch
                {
                    (true, false) => 1.0f,
                    (false, true) => - 1.0f,
                    _ => 0.0f
                };

                // Left/Right
                dX *= (float)userInputEvent.TimeSinceLastEventSeconds;
                Vector3 moveVecLeftRight = new (-1, 0, 0); // we are looking in the negative z direction, therefore "right" is -1
                moveVecLeftRight *= dX;

                // Forward/Backward
                dZ *= (float)userInputEvent.TimeSinceLastEventSeconds;
                Vector3 moveVecForwardBackward = new(0, 0, -1); // forward = -z
                moveVecForwardBackward *= dZ;

                Vector3 newLocation = _currentCameraOrientation.CameraLocation.ToOpenTkVec3() + moveVecLeftRight + moveVecForwardBackward;

                _currentCameraOrientation = _currentCameraOrientation with
                {
                    CameraLocation = new (newLocation.X, newLocation.Y, newLocation.Z)
                };
                await _scene.AssertionUpdatesChannel.Writer.WriteAsync(new AssertionBatch
                {
                    Assertions = new IAssertion[]
                    {
                        _currentCameraOrientation
                    }
                });
            }
        }