private void DoOrbitcamUpdate(float deltaTime) { float orbitSensitivityScale = 0.02f; // Angle Axis expects rads, but we have... pixels, so we just move it way down by a factor. Quaternion deltaRot = Quaternion.FromAxisAngle(new Vector3(1, 0, 0), -WInput.MouseDelta.Y * MouseSensitivity * deltaTime * orbitSensitivityScale) * Quaternion.FromAxisAngle(new Vector3(0, 1, 0), -WInput.MouseDelta.X * MouseSensitivity * deltaTime * orbitSensitivityScale); Transform.Rotation *= deltaRot; Vector3 moveDir = Vector3.Zero; if (WInput.GetKey(System.Windows.Input.Key.Q)) { moveDir -= Vector3.UnitY; } if (WInput.GetKey(System.Windows.Input.Key.E)) { moveDir += Vector3.UnitY; } if (moveDir.Length > 0) { moveDir.Normalize(); m_orbitPivot += moveDir * (MoveSpeed / 2) * deltaTime; } m_orbitCameraDistance += -WInput.MouseScrollDelta * 50 * deltaTime; m_orbitCameraDistance = WMath.Clamp(m_orbitCameraDistance, 100, 10000); Transform.Position = m_orbitPivot + Vector3.Transform(new Vector3(0, 0, m_orbitCameraDistance), Transform.Rotation); }
private void DoFlycamUpdate(float deltaTime) { Vector3 moveDir = Vector3.Zero; if (WInput.GetKey(System.Windows.Input.Key.W)) { moveDir -= Vector3.UnitZ; } if (WInput.GetKey(System.Windows.Input.Key.S)) { moveDir += Vector3.UnitZ; } if (WInput.GetKey(System.Windows.Input.Key.D)) { moveDir += Vector3.UnitX; } if (WInput.GetKey(System.Windows.Input.Key.A)) { moveDir -= Vector3.UnitX; } // If they're holding down the shift key adjust their FOV when they scroll, otherwise adjust move speed. MoveSpeed += WInput.MouseScrollDelta * 100 * deltaTime; MoveSpeed = WMath.Clamp(MoveSpeed, 100, 8000); if (WInput.GetMouseButton(1)) { Rotate(deltaTime, WInput.MouseDelta.X, WInput.MouseDelta.Y); } float moveSpeed = WInput.GetKey(System.Windows.Input.Key.LeftShift) ? MoveSpeed * 3f : MoveSpeed; // Make it relative to the current rotation. moveDir = Vector3.Transform(moveDir, Transform.Rotation); // Do Q and E after we transform the moveDir so they're always in worldspace. if (WInput.GetKey(System.Windows.Input.Key.Q)) { moveDir -= Vector3.UnitY; } if (WInput.GetKey(System.Windows.Input.Key.E)) { moveDir += Vector3.UnitY; } // Normalize the move direction moveDir.NormalizeFast(); // Early out if we're not moving this frame. if (moveDir.LengthFast < 0.1f) { return; } Transform.Position += Vector3.Multiply(moveDir, moveSpeed * deltaTime); }
private void CheckForObjectSelectionChange(WSceneView view) { // If we have a gizmo and we're transforming it, don't check for selection change. if (m_transformGizmo != null && m_transformGizmo.IsTransforming) { return; } if (WInput.GetMouseButtonDown(0) && !WInput.GetMouseButton(1)) { FRay mouseRay = view.ProjectScreenToWorld(WInput.MousePosition); WActorNode addedActor = Raycast(mouseRay); // Check the behaviour of this click to determine appropriate selection modification behaviour. // Click w/o Modifiers = Clear Selection, add result to selection // Click /w Ctrl = Toggle Selection State // Click /w Shift = Add to Selection bool ctrlPressed = WInput.GetKey(Key.LeftCtrl) || WInput.GetKey(Key.RightCtrl); bool shiftPressed = WInput.GetKey(Key.LeftShift) || WInput.GetKey(Key.RightShift); if (!ctrlPressed & !shiftPressed) { ModifySelection(SelectionType.Add, addedActor, true); //m_selectionList.Clear(); //if (addedActor != null) m_selectionList.Add(addedActor); } else if (addedActor != null && (ctrlPressed && !shiftPressed)) { if (m_selectionList.Contains(addedActor)) { ModifySelection(SelectionType.Remove, addedActor, false); } //m_selectionList.Remove(addedActor); else { ModifySelection(SelectionType.Add, addedActor, false); } //m_selectionList.Add(addedActor); } else if (addedActor != null && shiftPressed) { if (!m_selectionList.Contains(addedActor)) { ModifySelection(SelectionType.Add, addedActor, false); } //m_selectionList.Add(addedActor); } if (m_transformGizmo != null && m_selectionList.Count > 0) { m_transformGizmo.SetPosition(m_selectionList[0].Transform.Position); m_transformGizmo.SetLocalRotation(m_selectionList[0].Transform.Rotation); } } }
private void GlControlHost_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e) { System.Windows.Input.Key input_key = e.Key; if (input_key == System.Windows.Input.Key.System) { input_key = e.SystemKey; } WInput.SetKeyboardState(input_key, true); }
private void CheckForObjectSelectionChange(WSceneView view) { // If we have a gizmo and we're transforming it, don't check for selection change. if (m_transformGizmo != null && m_transformGizmo.IsTransforming) { return; } if (WInput.GetMouseButtonDown(0) && !WInput.GetMouseButton(1)) { FRay mouseRay = view.ProjectScreenToWorld(WInput.MousePosition); var addedActor = Raycast(mouseRay); // Check the behaviour of this click to determine appropriate selection modification behaviour. // Click w/o Modifiers = Clear Selection, add result to selection // Click /w Ctrl = Toggle Selection State // Click /w Shift = Add to Selection bool ctrlPressed = WInput.GetKey(Key.LeftCtrl) || WInput.GetKey(Key.RightCtrl); bool shiftPressed = WInput.GetKey(Key.LeftShift) || WInput.GetKey(Key.RightShift); if (!ctrlPressed & !shiftPressed) { EditorSelection.ClearSelection(); if (addedActor != null) { EditorSelection.AddToSelection(addedActor); } } else if (addedActor != null && (ctrlPressed && !shiftPressed)) { if (addedActor.IsSelected) { EditorSelection.RemoveFromSelection(addedActor); } else { EditorSelection.AddToSelection(addedActor); } } else if (addedActor != null && shiftPressed) { if (!EditorSelection.SelectedObjects.Contains(addedActor)) { EditorSelection.AddToSelection(addedActor); } } UpdateGizmoTransform(); } }
private void UpdateSceneViews() { // If they've clicked, check which view is in focus. if (WInput.GetMouseButtonDown(0) || WInput.GetMouseButtonDown(1) || WInput.GetMouseButtonDown(2)) { WSceneView focusedScene = GetFocusedSceneView(); foreach (var scene in m_sceneViews) { scene.IsFocused = false; FRect viewport = scene.GetViewportDimensions(); if (viewport.Contains(WInput.MousePosition.X, WInput.MousePosition.Y)) { focusedScene = scene; } } focusedScene.IsFocused = true; } }
public void Tick(float deltaTime) { if (WInput.GetKeyDown(System.Windows.Input.Key.OemTilde)) { MoveType = MoveType == CameraMode.Flycam ? CameraMode.Orbit : CameraMode.Flycam; } if (!WInput.GetMouseButton(1)) { return; } if (MoveType == CameraMode.Flycam) { DoFlycamUpdate(deltaTime); } else { DoOrbitcamUpdate(deltaTime); } }
public bool TransformFromInput(FRay ray, WSceneView view) { if (m_mode != FTransformMode.Translation) { WrapCursor(); } // Store the cursor position in viewport coordinates. Vector2 screenDimensions = App.GetScreenGeometry(); Vector2 cursorPos = App.GetCursorPosition(); Vector2 mouseCoords = new Vector2(((2f * cursorPos.X) / screenDimensions.X) - 1f, (1f - ((2f * cursorPos.Y) / screenDimensions.Y))); //[-1,1] range bool shiftPressed = WInput.GetKey(Key.LeftShift) || WInput.GetKey(Key.RightShift); if (m_mode == FTransformMode.Translation) { // Create a Translation Plane Vector3 axisA, axisB; if (GetNumSelectedAxes() == 1) { if (m_selectedAxes == FSelectedAxes.X) { axisB = Vector3.UnitX; } else if (m_selectedAxes == FSelectedAxes.Y) { axisB = Vector3.UnitY; } else { axisB = Vector3.UnitZ; } Vector3 dirToCamera = (m_position - view.GetCameraPos()).Normalized(); axisA = Vector3.Cross(axisB, dirToCamera); } else { axisA = ContainsAxis(m_selectedAxes, FSelectedAxes.X) ? Vector3.UnitX : Vector3.UnitZ; axisB = ContainsAxis(m_selectedAxes, FSelectedAxes.Y) ? Vector3.UnitY : Vector3.UnitZ; } Vector3 planeNormal = Vector3.Cross(axisA, axisB).Normalized(); m_translationPlane = new FPlane(planeNormal, m_position); float intersectDist; if (m_translationPlane.RayIntersectsPlane(ray, out intersectDist)) { Vector3 hitPos = ray.Origin + (ray.Direction * intersectDist); Vector3 localDelta = Vector3.Transform(hitPos - m_position, m_rotation.Inverted()); // Calculate a new position Vector3 newPos = m_position; if (ContainsAxis(m_selectedAxes, FSelectedAxes.X)) { newPos += Vector3.Transform(Vector3.UnitX, m_rotation) * localDelta.X; } if (ContainsAxis(m_selectedAxes, FSelectedAxes.Y)) { newPos += Vector3.Transform(Vector3.UnitY, m_rotation) * localDelta.Y; } if (ContainsAxis(m_selectedAxes, FSelectedAxes.Z)) { newPos += Vector3.Transform(Vector3.UnitZ, m_rotation) * localDelta.Z; } if (shiftPressed) { // Round to nearest 100 unit increment while shift is held down. newPos.X = (float)Math.Round(newPos.X / 100f) * 100f; newPos.Y = (float)Math.Round(newPos.Y / 100f) * 100f; newPos.Z = (float)Math.Round(newPos.Z / 100f) * 100f; } // Check the new location to see if it's skyrocked off into the distance due to near-plane raytracing issues. Vector3 newPosDirToCamera = (newPos - view.GetCameraPos()).Normalized(); float dot = Math.Abs(Vector3.Dot(planeNormal, newPosDirToCamera)); //Console.WriteLine("hitPos: {0} localOffset: {1} newPos: {2}, dotResult: {3}", hitPos, localOffset, newPos, dot); if (dot < 0.02f) { return(false); } // This is used to set the offset to the gizmo the mouse cursor is from the origin of the gizmo on the first frame // that you click on the gizmo. if (!m_hasSetMouseOffset) { m_translateOffset = m_position - newPos; m_deltaTranslation = Vector3.Zero; m_hasSetMouseOffset = true; return(false); } // Apply Translation m_deltaTranslation = Vector3.Transform(newPos - m_position + m_translateOffset, m_rotation.Inverted()); if (!ContainsAxis(m_selectedAxes, FSelectedAxes.X)) { m_deltaTranslation.X = 0f; } if (!ContainsAxis(m_selectedAxes, FSelectedAxes.Y)) { m_deltaTranslation.Y = 0f; } if (!ContainsAxis(m_selectedAxes, FSelectedAxes.Z)) { m_deltaTranslation.Z = 0f; } m_totalTranslation += m_deltaTranslation; m_position += Vector3.Transform(m_deltaTranslation, m_rotation); if (!m_hasTransformed && (m_deltaTranslation != Vector3.Zero)) { m_hasTransformed = true; } return(m_hasTransformed); } else { // Our raycast missed the plane m_deltaTranslation = Vector3.Zero; return(false); } } else if (m_mode == FTransformMode.Rotation) { Vector3 rotationAxis; if (m_selectedAxes == FSelectedAxes.X) { rotationAxis = Vector3.UnitX; } else if (m_selectedAxes == FSelectedAxes.Y) { rotationAxis = Vector3.UnitY; } else { rotationAxis = Vector3.UnitZ; } // Convert these from [0-1] to [-1, 1] to match our mouse coords. Vector2 lineOrigin = (view.UnprojectWorldToViewport(m_hitPoint) * 2) - Vector2.One; Vector2 lineEnd = (view.UnprojectWorldToViewport(m_hitPoint + m_moveDir) * 2) - Vector2.One; lineOrigin.Y = -lineOrigin.Y; lineEnd.Y = -lineEnd.Y; Vector2 lineDir = (lineEnd - lineOrigin).Normalized(); float rotAmount = Vector2.Dot(lineDir, mouseCoords + m_wrapOffset - lineOrigin) * 180f; if (float.IsNaN(rotAmount)) { Console.WriteLine("rotAmountNaN!"); return(false); } if (!m_hasSetMouseOffset) { m_rotateOffset = -rotAmount; m_deltaRotation = Quaternion.Identity; m_hasSetMouseOffset = true; return(false); } // Apply Rotation rotAmount += m_rotateOffset; if (shiftPressed) { // Round to nearest 45 degree increment while shift is held down. rotAmount = (float)Math.Round(rotAmount / 45f) * 45f; } Quaternion oldRot = m_currentRotation; m_currentRotation = Quaternion.FromAxisAngle(rotationAxis, WMath.DegreesToRadians(rotAmount)); m_deltaRotation = m_currentRotation * oldRot.Inverted(); if (m_transformSpace == FTransformSpace.Local) { m_rotation *= m_deltaRotation; } // Add to Total Rotation recorded for UI. if (m_selectedAxes == FSelectedAxes.X) { m_totalRotation.X = rotAmount; } else if (m_selectedAxes == FSelectedAxes.Y) { m_totalRotation.Y = rotAmount; } else { m_totalRotation.Z = rotAmount; } if (!m_hasTransformed && rotAmount != 0f) { m_hasTransformed = true; } return(m_hasTransformed); } else if (m_mode == FTransformMode.Scale) { // Create a line in screen space. // Convert these from [0-1] to [-1, 1] to match our mouse coords. Vector2 lineOrigin = (view.UnprojectWorldToViewport(m_position) * 2) - Vector2.One; lineOrigin.Y = -lineOrigin.Y; // Determine the appropriate world space directoin using the selected axes and then conver this for use with // screen-space controlls. This has to be done every frame because the axes can be flipped while the gizmo // is transforming, so we can't pre-calculate this. Vector3 dirX = Vector3.Transform(mFlipScaleX ? -Vector3.UnitX : Vector3.UnitX, m_rotation); Vector3 dirY = Vector3.Transform(mFlipScaleY ? -Vector3.UnitY : Vector3.UnitY, m_rotation); Vector3 dirZ = Vector3.Transform(mFlipScaleZ ? -Vector3.UnitZ : Vector3.UnitZ, m_rotation); Vector2 lineDir; // If there is only one axis, then the world space direction is the selected axis. if (GetNumSelectedAxes() == 1) { Vector3 worldDir; if (ContainsAxis(m_selectedAxes, FSelectedAxes.X)) { worldDir = dirX; } if (ContainsAxis(m_selectedAxes, FSelectedAxes.Y)) { worldDir = dirY; } else { worldDir = dirZ; } Vector2 worldPoint = (view.UnprojectWorldToViewport(m_position + worldDir) * 2) - Vector2.One; worldPoint.Y = -lineOrigin.Y; lineDir = (worldPoint - lineOrigin).Normalized(); } // If there's two axii selected, then convert both to screen space and average them out to get the line direction. else if (GetNumSelectedAxes() == 2) { Vector3 axisA = ContainsAxis(m_selectedAxes, FSelectedAxes.X) ? dirX : dirY; Vector3 axisB = ContainsAxis(m_selectedAxes, FSelectedAxes.Z) ? dirZ : dirY; Vector2 screenA = (view.UnprojectWorldToViewport(m_position + axisA) * 2) - Vector2.One; screenA.Y = -screenA.Y; Vector2 screenB = (view.UnprojectWorldToViewport(m_position + axisB) * 2) - Vector2.One; screenB.Y = -screenB.Y; screenA = (screenA - lineOrigin).Normalized(); screenB = (screenB - lineOrigin).Normalized(); lineDir = ((screenA + screenB) / 2f).Normalized(); } // There's three axis, just use up. else { lineDir = Vector2.UnitY; } float scaleAmount = Vector2.Dot(lineDir, mouseCoords + m_wrapOffset - lineOrigin) * 5f; if (shiftPressed) { // Round to nearest whole number scale while shift is held down. scaleAmount = (float)Math.Round(scaleAmount); } // Set their initial offset if we haven't already if (!m_hasSetMouseOffset) { m_scaleOffset = -scaleAmount; m_deltaScale = Vector3.One; m_hasSetMouseOffset = true; return(false); } // Apply the scale scaleAmount = scaleAmount + m_scaleOffset + 1f; // A multiplier is applied to the scale amount if it's less than one to prevent it dropping into the negatives. // ??? if (scaleAmount < 1f) { scaleAmount = 1f / (-(scaleAmount - 1f) + 1f); } Vector3 oldScale = m_totalScale; m_totalScale = Vector3.One; if (ContainsAxis(m_selectedAxes, FSelectedAxes.X)) { m_totalScale.X = scaleAmount; } if (ContainsAxis(m_selectedAxes, FSelectedAxes.Y)) { m_totalScale.Y = scaleAmount; } if (ContainsAxis(m_selectedAxes, FSelectedAxes.Z)) { m_totalScale.Z = scaleAmount; } m_deltaScale = new Vector3(m_totalScale.X / oldScale.X, m_totalScale.Y / oldScale.Y, m_totalScale.Z / oldScale.Z); if (!m_hasTransformed && (scaleAmount != 1f)) { m_hasTransformed = true; } return(m_hasTransformed); } return(false); }
private void UpdateSelectionGizmo(WSceneView view) { if (!m_transformGizmo.Enabled && m_selectionList.Count > 0) { // Show the Transform Gizmo. m_transformGizmo.Enabled = true; m_transformGizmo.SetPosition(m_selectionList[0].Transform.Position); m_transformGizmo.SetLocalRotation(m_selectionList[0].Transform.Rotation); } else if (m_transformGizmo.Enabled && m_selectionList.Count == 0) { // Hide the Transform Gizmo. m_transformGizmo.Enabled = false; } if (!m_transformGizmo.Enabled) { return; } if (WInput.GetKeyDown(Key.Q) && !WInput.GetMouseButton(1)) { m_transformGizmo.SetMode(FTransformMode.None); } if (WInput.GetKeyDown(Key.W) && !WInput.GetMouseButton(1)) { m_transformGizmo.SetMode(FTransformMode.Translation); } if (WInput.GetKeyDown(Key.E) && !WInput.GetMouseButton(1)) { m_transformGizmo.SetMode(FTransformMode.Rotation); } if (WInput.GetKeyDown(Key.R) && !WInput.GetMouseButton(1)) { m_transformGizmo.SetMode(FTransformMode.Scale); } if (WInput.GetKeyDown(Key.OemOpenBrackets)) { m_transformGizmo.DecrementSize(); } if (WInput.GetKeyDown(Key.OemCloseBrackets)) { m_transformGizmo.IncrementSize(); } if (WInput.GetKeyDown(Key.OemTilde)) { if (m_transformGizmo.TransformSpace == FTransformSpace.World) { m_transformGizmo.SetTransformSpace(FTransformSpace.Local); } else { m_transformGizmo.SetTransformSpace(FTransformSpace.World); } UpdateGizmoTransform(); } if (WInput.GetMouseButtonDown(0)) { FRay mouseRay = view.ProjectScreenToWorld(WInput.MousePosition); if (m_transformGizmo.CheckSelectedAxes(mouseRay)) { m_transformGizmo.StartTransform(); } } if (WInput.GetMouseButtonUp(0)) { if (m_transformGizmo.IsTransforming) { // When we end let go of the gizmo, we want to make one last action which specifies that it is done, // so that the next gizmo move doesn't merge with the previous. WUndoCommand undoAction = CreateUndoActionForGizmo(true); if (undoAction != null) { m_world.UndoStack.Push(undoAction); } m_transformGizmo.EndTransform(); } } if (m_transformGizmo.IsTransforming) { FRay mouseRay = view.ProjectScreenToWorld(WInput.MousePosition); if (m_transformGizmo.TransformFromInput(mouseRay, view)) { WUndoCommand undoAction = CreateUndoActionForGizmo(false); if (undoAction != null) { m_world.UndoStack.Push(undoAction); } } } m_transformGizmo.UpdateForSceneView(view); }
private void GlControlHost_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e) { WInput.SetKeyboardState(e.Key, true); }
private void GlControlHost_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e) { WInput.SetMouseScrollDelta(e.Delta); }
private void GlControlHost_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { WInput.SetMouseState(WinFormToWPFMouseButton(e), true); }
private void Window_Deactivated(object sender, EventArgs e) { WInput.ClearInput(); }
private void GlControlHost_LostFocus(object sender, RoutedEventArgs e) { WInput.ClearInput(); }
private void GlControlHost_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { WInput.SetMouseState(WinFormToWPFMouseButton(e), false); object test = Details; }