/// <summary> /// This update method gets called on each update pass for each scenes /// this component is attached to. /// </summary> /// <param name="updateState">Current update state.</param> /// <param name="correspondingView">The view which attached this component (may be null).</param> protected override void Update(SceneRelatedUpdateState updateState, ViewInformation correspondingView) { Camera3DBase actCamera = correspondingView.Camera; if (actCamera == null) { return; } foreach (InputFrame actInputFrame in updateState.InputFrames) { foreach (var actInputState in actInputFrame.GetInputStates(correspondingView)) { // Handle keyboard KeyboardState actKeyboardState = actInputState as KeyboardState; bool isControlKeyDown = false; if (actKeyboardState != null) { UpdateForKeyboard(actCamera, actKeyboardState, out isControlKeyDown); continue; } // Handle mouse (or pointer) MouseOrPointerState mouseState = actInputState as MouseOrPointerState; if (mouseState != null) { UpdateForMouse(actCamera, isControlKeyDown, mouseState); } } } }
/// <summary> /// Update camera for mouse input. /// </summary> private static void UpdateForMouse( PerSceneContext componentContext, Camera3DBase actCamera, MouseOrPointerState mouseState) { // Handle mouse move if (mouseState.MoveDistanceDip != Vector2.Zero) { Vector2 moving = mouseState.MoveDistanceDip; if (mouseState.IsButtonDown(MouseButton.Left) && mouseState.IsButtonDown(MouseButton.Right)) { float multiplyer = 1.05f; if (moving.Y < 0f) { multiplyer = 0.95f; } componentContext.CameraDistance = componentContext.CameraDistance * multiplyer; } else if (mouseState.IsButtonDown(MouseButton.Left) || mouseState.IsButtonDown(MouseButton.Right)) { componentContext.CameraHVRotation = componentContext.CameraHVRotation + new Vector2( SINGLE_ROTATION_H * (moving.X / 4f), SINGLE_ROTATION_V * (moving.Y / 4f)); } } // Handle mouse wheel if (mouseState.WheelDelta != 0) { float multiplyer = 0.95f - (Math.Abs(mouseState.WheelDelta) / 1000f); if (mouseState.WheelDelta < 0) { multiplyer = 1.05f + (Math.Abs(mouseState.WheelDelta) / 1000f); } componentContext.CameraDistance = componentContext.CameraDistance * multiplyer; } }
protected override void Update(SceneRelatedUpdateState updateState, ViewInformation correspondingView, PerSceneContext componentContext) { Camera3DBase actCamera = correspondingView.Camera; if (actCamera == null) { return; } foreach (InputFrame actInputFrame in updateState.InputFrames) { foreach (var actInputState in actInputFrame.GetInputStates(correspondingView)) { // Handle keyboard KeyboardState actKeyboardState = actInputState as KeyboardState; if (actKeyboardState != null) { UpdateForKeyboard(componentContext, actCamera, actKeyboardState); continue; } // Handle mouse (or pointer) MouseOrPointerState mouseState = actInputState as MouseOrPointerState; if (mouseState != null) { UpdateForMouse(componentContext, actCamera, mouseState); } } } // Ensure that our values are in allowed ranges float maxRad = EngineMath.RAD_90DEG * 0.99f; float minRad = EngineMath.RAD_90DEG * -0.99f; componentContext.CameraHVRotation.X = componentContext.CameraHVRotation.X % EngineMath.RAD_360DEG; if (componentContext.CameraDistance < this.CameraDistanceMin) { componentContext.CameraDistance = this.CameraDistanceMin; } if (componentContext.CameraDistance > this.CameraDistanceMax) { componentContext.CameraDistance = this.CameraDistanceMax; } if (componentContext.CameraHVRotation.Y <= minRad) { componentContext.CameraHVRotation.Y = minRad; } if (componentContext.CameraHVRotation.Y >= maxRad) { componentContext.CameraHVRotation.Y = maxRad; } // Update camera position and rotation Vector3 cameraOffset = Vector3.UnitX; cameraOffset = Vector3.TransformNormal( cameraOffset, Matrix4x4.CreateRotationY(componentContext.CameraHVRotation.X)); cameraOffset = Vector3.TransformNormal( cameraOffset, Matrix4x4.CreateFromAxisAngle(Vector3.Cross(cameraOffset, Vector3.UnitY), componentContext.CameraHVRotation.Y)); Vector3 focusedLocation = this.GetFocusedLocation(); actCamera.Position = focusedLocation + cameraOffset * componentContext.CameraDistance; actCamera.Target = focusedLocation; }
/// <summary> /// Update camera for mouse input. /// </summary> private static void UpdateForMouse(Camera3DBase actCamera, bool isControlKeyDown, MouseOrPointerState mouseState) { // Handle mouse move if (mouseState.MoveDistanceDip != Vector2.Zero) { var moving = mouseState.MoveDistanceDip; if (mouseState.IsButtonDown(MouseButton.Left) && mouseState.IsButtonDown(MouseButton.Right)) { actCamera.Zoom(moving.Y / -50f); } else if (mouseState.IsButtonDown(MouseButton.Left)) { actCamera.Strave(moving.X / 50f); actCamera.UpDown(-moving.Y / 50f); } else if (mouseState.IsButtonDown(MouseButton.Right)) { actCamera.Rotate(-moving.X / 200f, -moving.Y / 200f); } } // Handle mouse wheel if (mouseState.WheelDelta != 0) { var multiplier = 1f; if (isControlKeyDown) { multiplier = 2f; } actCamera.Zoom(mouseState.WheelDelta / 100f * multiplier); } }