/// <summary> /// Attaches this component to a scene. /// Be careful, this method gets called from a background thread of seeing#! /// It may also be called from multiple scenes in parallel or simply withoud previous Detach call. /// </summary> /// <param name="manipulator">The manipulator of the scene we attach to.</param> /// <param name="correspondingView">The view which attached this component.</param> /// <returns></returns> protected override PerSceneContext Attach(SceneManipulator manipulator, ViewInformation correspondingView) { PerSceneContext result = new PerSceneContext(); result.CameraDistance = this.CameraDistanceInitial; result.CameraHVRotation = m_hvRotation; return(result); }
protected override PerSceneContext Attach(SceneManipulator manipulator, ViewInformation correspondingView) { PerSceneContext context = new PerSceneContext(); NamedOrGenericKey resCubeGeometry = manipulator.AddGeometry(new CubeType()); context.CubeObject = manipulator.AddGeneric(resCubeGeometry); context.CubeObject.Color = Color4.RedColor; return(context); }
/// <summary> /// Attaches this component to a scene. /// Be careful, this method gets called from a background thread of seeing#! /// It may also be called from multiple scenes in parallel or simply withoud previous Detach call. /// </summary> /// <param name="manipulator">The manipulator of the scene we attach to.</param> /// <param name="correspondingView">The view which attached this component.</param> protected override PerSceneContext Attach(SceneManipulator manipulator, ViewInformation correspondingView) { PerSceneContext context = new PerSceneContext(); switch (m_gradientDirection) { case GradientDirection.LeftToRight: context.BrushResource = new LinearGradientBrushResource( new System.Numerics.Vector2(0f, 0f), new System.Numerics.Vector2(m_textureWidth, 0f), new GradientStop[] { new GradientStop(m_colorStart, 0f), new GradientStop(m_colorEnd, 1f) }); break; case GradientDirection.TopToBottom: context.BrushResource = new LinearGradientBrushResource( new System.Numerics.Vector2(0f, 0f), new System.Numerics.Vector2(0f, m_textureHeight), new GradientStop[] { new GradientStop(m_colorStart, 0f), new GradientStop(m_colorEnd, 1f) }); break; case GradientDirection.Directional: context.BrushResource = new LinearGradientBrushResource( new System.Numerics.Vector2(0f, 0f), new System.Numerics.Vector2(m_textureWidth, m_textureHeight), new GradientStop[] { new GradientStop(m_colorStart, 0f), new GradientStop(m_colorEnd, 1f) }); break; } // Create the background layer if not available already base.CreateLayerIfNotAvailable(manipulator); // Create and add the background context.BackgroundTextureKey = manipulator.AddResource( () => new Direct2DSingleRenderTextureResource(context.BrushResource, m_textureWidth, m_textureHeight)); context.BackgroundPainter = new FullscreenTextureObject(context.BackgroundTextureKey); context.BackgroundPainter.Scaling = 1.1f; manipulator.Add(context.BackgroundPainter, DEFAULT_LAYER); return(context); }
/// <summary> /// Update camera for keyboard input. /// </summary> private static void UpdateForKeyboard( PerSceneContext componentContext, Camera3DBase actCamera, KeyboardState actKeyboardState) { foreach (WinVirtualKey actKey in actKeyboardState.KeysDown) { switch (actKey) { case WinVirtualKey.Up: case WinVirtualKey.W: case WinVirtualKey.NumPad8: componentContext.CameraHVRotation = componentContext.CameraHVRotation + new Vector2(0f, SINGLE_ROTATION_V); break; case WinVirtualKey.Down: case WinVirtualKey.S: case WinVirtualKey.NumPad2: componentContext.CameraHVRotation = componentContext.CameraHVRotation - new Vector2(0f, SINGLE_ROTATION_V); break; case WinVirtualKey.Left: case WinVirtualKey.A: case WinVirtualKey.NumPad4: componentContext.CameraHVRotation = componentContext.CameraHVRotation - new Vector2(SINGLE_ROTATION_H, 0f); break; case WinVirtualKey.Right: case WinVirtualKey.D: case WinVirtualKey.NumPad6: componentContext.CameraHVRotation = componentContext.CameraHVRotation + new Vector2(SINGLE_ROTATION_H, 0f); break; case WinVirtualKey.Q: case WinVirtualKey.NumPad3: componentContext.CameraDistance = componentContext.CameraDistance * 1.05f; break; case WinVirtualKey.E: case WinVirtualKey.NumPad9: componentContext.CameraDistance = componentContext.CameraDistance * 0.95f; break; } } }
/// <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 Detach(SceneManipulator manipulator, ViewInformation correspondingView, PerSceneContext context) { manipulator.Remove(context.CubeObject); }
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> /// Detaches this component from a scene. /// Be careful, this method gets called from a background thread of seeing#! /// It may also be called from multiple scenes in parallel. /// </summary> /// <param name="manipulator">The manipulator of the scene we attach to.</param> /// <param name="correspondingView">The view which attached this component.</param> /// <param name="componentContext">A context variable containing all createded objects during call of Attach.</param> protected override void Detach(SceneManipulator manipulator, ViewInformation correspondingView, PerSceneContext componentContext) { }
/// <summary> /// Detaches the specified manipulator. /// </summary> /// <param name="manipulator">The manipulator.</param> /// <param name="correspondingView">The corresponding view.</param> /// <param name="context">The context.</param> protected override void Detach(SceneManipulator manipulator, ViewInformation correspondingView, PerSceneContext context) { manipulator.Remove(context.BackgroundPainter); manipulator.RemoveResource(context.BackgroundTextureKey); GraphicsHelper.SafeDispose(ref context.BrushResource); }