} // Load #endregion #region Update Tasks /// <summary> /// Tasks executed during the update. /// This is the place to put the application logic. /// </summary> protected override void UpdateTasks() { if (Keyboard.KeyJustPressed(Keys.Space)) { if (directionalLight.DirectionalLight.Intensity == 1) { directionalLight.DirectionalLight.Intensity = 4f; } else { directionalLight.DirectionalLight.Intensity = 1f; } } } // UpdateTasks
} // Start /// <summary> /// Tasks executed during the update. /// This is the place to put the application logic. /// </summary> public override void Update() { if (beginInEditorMode) { EditorManager.EnableEditorMode(); beginInEditorMode = false; } // Enable editor mode if (Keyboard.KeyJustPressed(Keys.E) && Keyboard.KeyPressed(Keys.LeftControl)) { if (EditorManager.EditorModeEnabled) { EditorManager.DisableEditorMode(); } else { EditorManager.EnableEditorMode(); } } } // UpdateTasks
} // Load #endregion #region Update Tasks /// <summary> /// Tasks executed during the update. /// This is the place to put the application logic. /// </summary> protected override void UpdateTasks() { // This is intended to be used in Xbox tests. if (XNAFinalEngine.Input.GamePad.PlayerOne.BackJustPressed) throw new Exception("Quick exit in Xbox 360 tests."); //demoLegend.Transform.Position = new Vector3(20, Screen.Height - 50f, 0f); #region Doors if (Keyboard.KeyJustPressed(Keys.Space) || XNAFinalEngine.Input.GamePad.PlayerOne.AJustPressed) { openingDoors = !openingDoors; } if (openingDoors) { lamborghiniMurcielagoLoader.LeftDoorAngle += Time.SmoothFrameTime * 30; lamborghiniMurcielagoLoader.RightDoorAngle += Time.SmoothFrameTime * 30; } else { lamborghiniMurcielagoLoader.LeftDoorAngle -= Time.SmoothFrameTime * 30; lamborghiniMurcielagoLoader.RightDoorAngle -= Time.SmoothFrameTime * 30; } #endregion #region Tires if (Keyboard.KeyPressed(Keys.Left) || XNAFinalEngine.Input.GamePad.PlayerOne.LeftStickX < 0) { lamborghiniMurcielagoLoader.TiresAngle += Time.SmoothFrameTime * 80; } if (Keyboard.KeyPressed(Keys.Right) || XNAFinalEngine.Input.GamePad.PlayerOne.LeftStickX > 0) { lamborghiniMurcielagoLoader.TiresAngle -= Time.SmoothFrameTime * 80; } #endregion } // UpdateTasks
} // SetPosition #endregion #region Update /// <summary> /// Update camera. /// </summary> public override void Update() { if (!((GameObject3D)Owner).Camera.Enabled) { return; } if (CouldBeManipulated() || ThisCameraIsBeingManipulated) { if (mode == ModeType.Softimage || mode == ModeType.NoManipulationKey) { #region Softimage mode #region Is it in manipulation mode? if (ThisCameraIsBeingManipulated) { // To exit the manipulation mode. if (Keyboard.KeyJustPressed(Keys.Escape) || Keyboard.KeyJustPressed(Keys.Space)) { cameraBeingManipulated = null; } } // To enter or exit the manipulation mode using the s key. if (Keyboard.KeyJustPressed(Keys.S)) { sJustPressed = true; sJustPressedTime = Time.ApplicationTime; } else { if (sJustPressed && !Keyboard.KeyPressed(Keys.S)) { sJustPressed = false; if (Time.ApplicationTime - sJustPressedTime < 0.3f) { cameraBeingManipulated = ThisCameraIsBeingManipulated ? null : this; } } } #endregion #region Manipulate if (ThisCameraIsBeingManipulated || Keyboard.KeyPressed(Keys.S) || mode == ModeType.NoManipulationKey) { // Translation if (Mouse.LeftButtonPressed) { LookAtPosition -= ((GameObject3D)Owner).Transform.Right * Mouse.DeltaX * Distance / 1000; LookAtPosition += ((GameObject3D)Owner).Transform.Up * Mouse.DeltaY * Distance / 1000; } // Orientation if (Mouse.RightButtonPressed && !OrthographicMode) { Yaw += Mouse.DeltaX * 0.005f; Pitch += Mouse.DeltaY * 0.005f; } // Zoom if (Mouse.MiddleButtonPressed) { Distance -= (-Mouse.DeltaX + Mouse.DeltaY) * Distance / 1300; // The minus is because I'm a little perfectionist. } } // Zoom Distance -= Mouse.WheelDelta * Distance / 1300; #endregion #endregion } else { #region Maya mode if ((Keyboard.KeyPressed(Keys.LeftAlt) || Keyboard.KeyPressed(Keys.RightAlt)) && (Mouse.LeftButtonPressed || Mouse.RightButtonPressed || Mouse.MiddleButtonPressed)) { cameraBeingManipulated = this; // Translation if (Mouse.MiddleButtonPressed) { LookAtPosition -= ((GameObject3D)Owner).Transform.Right * Mouse.DeltaX * Distance / 1000; LookAtPosition += ((GameObject3D)Owner).Transform.Up * Mouse.DeltaY * Distance / 1000; } // Orientation if (Mouse.LeftButtonPressed && !OrthographicMode) { Yaw += Mouse.DeltaX * 0.005f; Pitch += Mouse.DeltaY * 0.005f; } if (Mouse.RightButtonPressed) { // Distance or zoom Distance -= (Mouse.DeltaX + Mouse.DeltaY) * Distance / 1300; } } else { cameraBeingManipulated = null; } // Distance or zoom Distance -= Mouse.WheelDelta * Distance / 1300; // 1300 is an arbitrary number. #endregion } } #region Gamepad LookAtPosition -= ((GameObject3D)Owner).Transform.Right * -GamePad.PlayerOne.LeftStickX * Distance / 1000; LookAtPosition += ((GameObject3D)Owner).Transform.Up * GamePad.PlayerOne.LeftStickY * Distance / 1000; // Orientation Yaw -= GamePad.PlayerOne.RightStickX * 0.008f; Pitch += GamePad.PlayerOne.RightStickY * 0.008f; // Distance or zoom Distance -= (GamePad.PlayerOne.RightTrigger - GamePad.PlayerOne.LeftTrigger) * Distance / 300; #endregion #region Bounds // Orientation bounds if (Yaw >= twoPi) { Yaw -= twoPi; } if (Yaw < 0) { Yaw += twoPi; } if (Pitch >= twoPi) { Pitch -= twoPi; } if (Pitch < 0) { Pitch += twoPi; } // Distance bounds. if (Distance > ((GameObject3D)Owner).Camera.FarPlane) { Distance = ((GameObject3D)Owner).Camera.FarPlane; } if (Distance < ((GameObject3D)Owner).Camera.NearPlane) { Distance = ((GameObject3D)Owner).Camera.NearPlane; } #endregion // Calculate Rotation // Quaternion rotation = Quaternion.Identity; rotation *= Quaternion.CreateFromYawPitchRoll(0, Pitch, 0); rotation *= Quaternion.CreateFromYawPitchRoll(Yaw, 0, 0); rotation *= Quaternion.CreateFromYawPitchRoll(0, 0, Roll); // Its actually the invert... ((GameObject3D)Owner).Transform.Rotation = Quaternion.Inverse(rotation); // Now the position. Matrix rotationMatrix = Matrix.CreateFromQuaternion(rotation); ((GameObject3D)Owner).Transform.Position = LookAtPosition + new Vector3(rotationMatrix.M13, rotationMatrix.M23, rotationMatrix.M33) * Distance; if (OrthographicMode) { ((GameObject3D)Owner).Camera.OrthographicVerticalSize = Distance; } } // Update
} // DisableGizmo #endregion #region Update /// <summary> /// Update. /// </summary> internal void Update(GameObject3D gizmoCamera, Control clientArea) { #region Active if (Active) { // If the manipulation is over... if (!Mouse.LeftButtonPressed) { Active = false; // Store new previous matrix. if (selectedObjects[0].Transform.LocalMatrix != selectedObjectsLocalMatrix[0]) { using (Transaction.Create()) { for (int i = 0; i < selectedObjects.Count; i++) { // I store the action on the undo system. It seems complex. But it is pretty simple actually. Matrix oldMatrix = selectedObjectsLocalMatrix[i]; Matrix newMatrix = selectedObjects[i].Transform.LocalMatrix; GameObject3D gameObject3D = selectedObjects[i]; ActionManager.CallMethod( // Redo delegate { gameObject3D.Transform.LocalMatrix = newMatrix; }, // Undo delegate { gameObject3D.Transform.LocalMatrix = oldMatrix; }); } } } } // Transformate object... else { Vector2 transformationAmount; Vector3 translation = Vector3.Zero; // First we have to know how much to move the object in each axis. if (redAxisSelected) { Calculate2DMouseDirection(selectedObject, gizmoCamera, new Vector3(1, 0, 0), out transformationAmount); translation.X = (Mouse.DeltaX * transformationAmount.X / 100.0f); translation.X += (Mouse.DeltaY * transformationAmount.Y / 100.0f); } if (greenAxisSelected) { Calculate2DMouseDirection(selectedObject, gizmoCamera, new Vector3(0, 1, 0), out transformationAmount); translation.Y = (Mouse.DeltaX * transformationAmount.X / 100.0f); translation.Y += (Mouse.DeltaY * transformationAmount.Y / 100.0f); } if (blueAxisSelected) { Calculate2DMouseDirection(selectedObject, gizmoCamera, new Vector3(0, 0, 1), out transformationAmount); translation.Z = (Mouse.DeltaX * transformationAmount.X / 100.0f); translation.Z += (Mouse.DeltaY * transformationAmount.Y / 100.0f); } // Calculate the scale to do transformation proportional to the camera distance to the object. // The calculations are doing once from only one object to move everything at the same rate. Vector3 center; Quaternion orientation; float scale; GizmoScaleCenterOrientation(selectedObject, gizmoCamera, out scale, out center, out orientation); // Transform each object. foreach (GameObject3D gameObject3D in selectedObjects) { gameObject3D.Transform.Translate(translation * scale, Space == SpaceMode.Local ? Components.Space.Local : Components.Space.World); } } if (Keyboard.KeyJustPressed(Keys.Escape)) { Active = false; // Revert transformation to all selected objects. for (int i = 0; i < selectedObjects.Count; i++) { selectedObjects[i].Transform.LocalMatrix = selectedObjectsLocalMatrix[i]; } } } #endregion #region Inactive else { if (!Keyboard.KeyPressed(Keys.LeftAlt) && !Keyboard.KeyPressed(Keys.RightAlt)) { // If we press the left mouse button the manipulator activates. if (Mouse.LeftButtonJustPressed) { Active = true; // Stores initial matrix because maybe the user press escape; i.e. maybe he cancel the transformation. for (int i = 0; i < selectedObjects.Count; i++) { selectedObjectsLocalMatrix[i] = selectedObjects[i].Transform.LocalMatrix; } } // Perform a pick around the mouse pointer. Viewport viewport = new Viewport(gizmoCamera.Camera.Viewport.X + clientArea.ControlLeftAbsoluteCoordinate, gizmoCamera.Camera.Viewport.Y + clientArea.ControlTopAbsoluteCoordinate, gizmoCamera.Camera.Viewport.Width, gizmoCamera.Camera.Viewport.Height); Picker.BeginManualPicking(gizmoCamera.Camera.ViewMatrix, gizmoCamera.Camera.ProjectionMatrix, viewport); RenderGizmoForPicker(gizmoCamera); Color[] colorArray = Picker.EndManualPicking(new Rectangle(Mouse.Position.X - 5, Mouse.Position.Y - 5, RegionSize, RegionSize)); #region Find Selected Axis redAxisSelected = true; greenAxisSelected = true; blueAxisSelected = true; bool allAxis = false; for (int i = 0; i < RegionSize * RegionSize; i++) { // This is the order of preference: // 1) All axis. // 2) 1 axis. // 3) 2 axis. if (redAxisSelected && greenAxisSelected && blueAxisSelected) { // X and Y axis. if (colorArray[i].R == 255 && colorArray[i].G == 255 && colorArray[i].B == 0) { redAxisSelected = true; greenAxisSelected = true; blueAxisSelected = false; } // X and Z axis. if (colorArray[i].R == 255 && colorArray[i].G == 0 && colorArray[i].B == 255) { redAxisSelected = true; greenAxisSelected = false; blueAxisSelected = true; } // Y and Z axis. if (colorArray[i].R == 0 && colorArray[i].G == 255 && colorArray[i].B == 255) { redAxisSelected = false; greenAxisSelected = true; blueAxisSelected = true; } } // X axis. if (colorArray[i].R == 255 && colorArray[i].G == 0 && colorArray[i].B == 0) { redAxisSelected = true; greenAxisSelected = false; blueAxisSelected = false; } // Y axis. if (colorArray[i].R == 0 && colorArray[i].G == 255 && colorArray[i].B == 0) { redAxisSelected = false; greenAxisSelected = true; blueAxisSelected = false; } // Z axis. if (colorArray[i].R == 0 && colorArray[i].G == 0 && colorArray[i].B == 255) { redAxisSelected = false; greenAxisSelected = false; blueAxisSelected = true; } // All axis. if (colorArray[i].R == 255 && colorArray[i].G == 255 && colorArray[i].B == 255) { redAxisSelected = true; greenAxisSelected = true; blueAxisSelected = true; allAxis = true; i = RegionSize * RegionSize; // Or break. } } if (redAxisSelected && greenAxisSelected && blueAxisSelected && !allAxis) { redAxisSelected = false; greenAxisSelected = false; blueAxisSelected = false; } #endregion } else { redAxisSelected = false; greenAxisSelected = false; blueAxisSelected = false; } } #endregion } // Update
} // OnMouseUp #endregion #region On Key Press protected override void OnKeyPress(KeyEventArgs e) { flashTime = 0; if (!e.Handled) { if (e.Key == Keys.Enter && mode != TextBoxMode.Multiline && !readOnly) { initialText = Text; selection = new Selection(-1, -1); Focused = false; } if (e.Key == Keys.Escape) { if (initialText != null) Text = initialText; selection = new Selection(-1, -1); Focused = false; } if (e.Key == Keys.A && e.Control && mode != TextBoxMode.Password) { SelectAll(); } if (e.Key == Keys.Up) { e.Handled = true; if (e.Shift && selection.IsEmpty && mode != TextBoxMode.Password) { selection.Start = Position; } if (!e.Control) { PositionY -= 1; } } else if (e.Key == Keys.Down) { e.Handled = true; if (e.Shift && selection.IsEmpty && mode != TextBoxMode.Password) { selection.Start = Position; } if (!e.Control) { PositionY += 1; } } else if (e.Key == Keys.Back && !readOnly) { e.Handled = true; if (!selection.IsEmpty) { Text = Text.Remove(selection.Start, selection.Length); Position = selection.Start; } else if (Text.Length > 0 && Position > 0) { Position -= 1; Text = Text.Remove(Position, 1); } selection.Clear(); } else if (e.Key == Keys.Delete && !readOnly) { e.Handled = true; if (!selection.IsEmpty) { Text = Text.Remove(selection.Start, selection.Length); Position = selection.Start; } else if (Position < Text.Length) { Text = Text.Remove(Position, 1); } selection.Clear(); } else if (e.Key == Keys.Left) { e.Handled = true; if (e.Shift && selection.IsEmpty && mode != TextBoxMode.Password) { selection.Start = Position; } if (!e.Control) { Position -= 1; } if (e.Control) { Position = FindPreviousWord(shownText); } } else if (e.Key == Keys.Right) { e.Handled = true; if (e.Shift && selection.IsEmpty && mode != TextBoxMode.Password) { selection.Start = Position; } if (!e.Control) { Position += 1; } if (e.Control) { Position = FindNextWord(shownText); } } else if (e.Key == Keys.Home) { e.Handled = true; if (e.Shift && selection.IsEmpty && mode != TextBoxMode.Password) { selection.Start = Position; } if (!e.Control) { PositionX = 0; } if (e.Control) { Position = 0; } } else if (e.Key == Keys.End) { e.Handled = true; if (e.Shift && selection.IsEmpty && mode != TextBoxMode.Password) { selection.Start = Position; } if (!e.Control) { PositionX = lines[PositionY].Length; } if (e.Control) { Position = Text.Length; } } else if (e.Key == Keys.PageUp) { e.Handled = true; if (e.Shift && selection.IsEmpty && mode != TextBoxMode.Password) { selection.Start = Position; } if (!e.Control) { PositionY -= linesDrawn; } } else if (e.Key == Keys.PageDown) { e.Handled = true; if (e.Shift && selection.IsEmpty && mode != TextBoxMode.Password) { selection.Start = Position; } if (!e.Control) { PositionY += linesDrawn; } } else if (e.Key == Keys.Enter && mode == TextBoxMode.Multiline && !readOnly) { e.Handled = true; Text = Text.Insert(Position, Separator); PositionX = 0; PositionY += 1; } else if (e.Key == Keys.Tab) { } else if (!readOnly && !e.Control) { string c = Keyboard.KeyToString(e.Key, e.Shift, e.Caps); if (selection.IsEmpty) { Text = Text.Insert(Position, c); if (c != "") PositionX += 1; } else { if (Text.Length > 0) { // Avoid out of range. if (selection.Start + selection.Length > Text.Length) Text = Text.Remove(selection.Start, Text.Length - selection.Start); else Text = Text.Remove(selection.Start, selection.Length); Text = Text.Insert(selection.Start, c); Position = selection.Start + 1; } selection.Clear(); } } if (e.Shift && !selection.IsEmpty) { selection.End = Position; } #region Copy Paste // Windows only because it uses the Clipboard class. Of course this could be implemented manually in the XBOX 360 if you want it. #if (WINDOWS) if (e.Control && e.Key == Keys.C && mode != TextBoxMode.Password) { System.Windows.Forms.Clipboard.Clear(); if (mode != TextBoxMode.Password && !selection.IsEmpty) { System.Windows.Forms.Clipboard.SetText((Text.Substring(selection.Start, selection.Length)).Replace("\n", Environment.NewLine)); } } else if (e.Control && e.Key == Keys.V && !readOnly && mode != TextBoxMode.Password) { string t = System.Windows.Forms.Clipboard.GetText().Replace(Environment.NewLine, "\n"); if (selection.IsEmpty) { Text = Text.Insert(Position, t); Position = Position + t.Length; } else { Text = Text.Remove(selection.Start, selection.Length); Text = Text.Insert(selection.Start, t); PositionX = selection.Start + t.Length; selection.Clear(); } } #endif #endregion if ((!e.Shift && !e.Control) || Text.Length <= 0) { selection.Clear(); } if (e.Control && e.Key == Keys.Down) { e.Handled = true; HandleGuide(PlayerIndex.One); } flashTime = 0; if (ClientArea != null) ClientArea.Invalidate(); DeterminePages(); ProcessScrolling(); } base.OnKeyPress(e); } // OnKeyPress
} // LoadContent #endregion #region Update Tasks /// <summary> /// Tasks executed during the update. /// This is the place to put the application logic. /// </summary> protected override void UpdateTasks() { // This is intended to be used in Xbox tests. //if (XNAFinalEngine.Input.GamePad.PlayerOne.BackJustPressed) // throw new Exception("Quick exit in Xbox 360 tests."); // Move the dynamic box // if (Keyboard.KeyJustPressed(Keys.Space) || XNAFinalEngine.Input.GamePad.PlayerOne.AJustPressed) { cube.RigidBody.Entity.ApplyImpulse(cube.Transform.Position, Vector3.Up * 5); } // Move the obstacle // if (Keyboard.KeyPressed(Keys.W) || XNAFinalEngine.Input.GamePad.PlayerOne.DPadUpPressed) { obstacle.Transform.Translate(Vector3.Forward * Time.GameDeltaTime * 2, Space.World); } if (Keyboard.KeyPressed(Keys.S) || XNAFinalEngine.Input.GamePad.PlayerOne.DPadDownPressed) { obstacle.Transform.Translate(Vector3.Backward * Time.GameDeltaTime * 2, Space.World); } if (Keyboard.KeyPressed(Keys.A) || XNAFinalEngine.Input.GamePad.PlayerOne.DPadLeftPressed) { obstacle.Transform.Translate(Vector3.Left * Time.GameDeltaTime * 2, Space.World); } if (Keyboard.KeyPressed(Keys.D) || XNAFinalEngine.Input.GamePad.PlayerOne.DPadRightPressed) { obstacle.Transform.Translate(Vector3.Right * Time.GameDeltaTime * 2, Space.World); } if (Keyboard.KeyPressed(Keys.Q) || XNAFinalEngine.Input.GamePad.PlayerOne.LeftButtonPressed) { obstacle.Transform.Rotate(new Vector3(0, Time.GameDeltaTime * 35, 0)); } if (Keyboard.KeyPressed(Keys.E) || XNAFinalEngine.Input.GamePad.PlayerOne.RightButtonPressed) { obstacle.Transform.Rotate(new Vector3(0, -Time.GameDeltaTime * 35, 0)); } // Fire a box // if (Keyboard.KeyJustPressed(Keys.LeftControl) || XNAFinalEngine.Input.GamePad.PlayerOne.XJustPressed) { var bullet = MakeBox(camera.Transform.Position, 1, 1, 1, 1, hitMaterial); var rb = bullet.RigidBody; rb.Entity.Orientation = camera.Transform.Rotation; rb.Entity.LinearVelocity = camera.Transform.Forward * 45f; } // Cast a ray and apply an impulse to the first object touched by the ray // if (Keyboard.KeyJustPressed(Keys.LeftShift) || XNAFinalEngine.Input.GamePad.PlayerOne.BJustPressed) { RayCastResult raycastResult; GameObject3D go = PhysicsManager.Raycast(new Ray(camera.Transform.Position, camera.Transform.Forward), 100f, out raycastResult); if (go != null && go != floor && go != cube && go != obstacle) { // Change the color of the hitted GO go.ModelRenderer.Material = new BlinnPhong { DiffuseColor = new Color(1.0f, 1.0f, 0.0f), SpecularPower = 500, SpecularIntensity = 0.5f }; // Apply Up impulse to the GO go.RigidBody.Entity.LinearMomentum = Vector3.Up * 25f; } } base.UpdateTasks(); } // UpdateTasks