Beispiel #1
0
        private void UpdateView()
        {
            if (sceneView == null)
            {
                return;
            }
            if (controller == null || !controller.IsConnected)
            {
                return;
            }
            //The Camera might change whil doing all the calculations...
            var originalCamera = sceneView.Camera;

            if (originalCamera == null)
            {
                return;
            }
            var  newCamera = originalCamera;
            var  state     = controller.GetState();
            var  time2     = DateTime.Now;
            bool isRightShoulderPressed = state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.RightShoulder);

            //Choose if staying at the same alitude or not
            if (isRightShoulderPressed)
            {
                var elapsed = time2 - time;
                if (elapsed != TimeSpan.Zero && elapsed > TimeSpan.FromSeconds(0.25))
                {
                    timer.Stop();
                    timer.Start();
                    relativeToDirection = !relativeToDirection;
                    time = time2;
                }
            }
            else
            {
                time = time2;
            }

            double LeftThumbStickX  = 0;
            double LeftThumbStickY  = 0;
            double RightThumbStickX = 0;
            double RightThumbStickY = 0;
            var    gamePad          = new NormalizedGamepad(state.Gamepad);

            LeftThumbStickX  = gamePad.LeftThumbXNormalized;
            LeftThumbStickY  = gamePad.LeftThumbYNormalized;
            RightThumbStickX = gamePad.RightThumbXNormalized;
            RightThumbStickY = gamePad.RightThumbYNormalized;

            if ((LeftThumbStickX != 0) || (LeftThumbStickY != 0) || (RightThumbStickX != 0) || (RightThumbStickY != 0))
            {
                //Move sideways (crab move), so calling move forward after modifying heading and tilt, then resetting heading and tilt
                if (LeftThumbStickX != 0)
                {
                    //Distance depends on the altitude, but must have minimum values (one way and the other) to not be almost nothing when altitude gets to 0)
                    var distance = (double)LeftThumbStickX * newCamera.Location.Z / 40;
                    if (distance > 0 && distance < 0.75)
                    {
                        distance = 0.75;
                    }
                    if (distance < 0 && distance > -0.75)
                    {
                        distance = -0.75;
                    }
                    var pitch = newCamera.Pitch;
                    newCamera = newCamera.RotateTo(newCamera.Heading, 90, newCamera.Roll).RotateTo(90, 0, 0).MoveForward(distance).RotateTo(-90, 0, 0);
                    newCamera = newCamera.RotateTo(newCamera.Heading, pitch, newCamera.Roll);
                }

                var distance2 = (double)LeftThumbStickY * newCamera.Location.Z / 40;
                if (distance2 > 0 && distance2 < 1)
                {
                    distance2 = 1;
                }
                if (distance2 < 0 && distance2 > -1)
                {
                    distance2 = -1;
                }
                //Move in the direction of the camera (modify the elevation)
                newCamera = newCamera.MoveForward(distance2).RotateTo((double)RightThumbStickX, newCamera.Pitch > 2 ? -(double)RightThumbStickY : RightThumbStickY < 0 ? -(double)RightThumbStickY : 0, 0);

                //If move in the direction of the camera but staying at the same elevation.
                if (!relativeToDirection)
                {
                    newCamera = newCamera.MoveTo(new MapPoint(newCamera.Location.X, newCamera.Location.Y, originalCamera.Location.Z, newCamera.Location.SpatialReference));
                }
            }

            //Triggers are just elevating the camera

            double elevationDistance = (double)(state.Gamepad.RightTrigger - state.Gamepad.LeftTrigger);

            if ((elevationDistance != 0) && (newCamera.Location.Z != double.NaN))
            {
                var distance = elevationDistance * Math.Abs(newCamera.Location.Z) / 10000;
                newCamera = newCamera.Elevate(distance > 1 ? distance : distance < 1 ? distance : distance < 0 ? -1 : 1);
            }


            if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.DPadUp))
            {
                newCamera = newCamera.RotateTo(0, newCamera.Pitch, newCamera.Roll);
            }

            if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.DPadDown))
            {
                newCamera = newCamera.RotateTo(180, newCamera.Pitch, newCamera.Roll);
            }

            if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.DPadLeft))
            {
                newCamera = newCamera.RotateTo(270, newCamera.Pitch, newCamera.Roll);
            }

            if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.DPadRight))
            {
                newCamera = newCamera.RotateTo(90, newCamera.Pitch, newCamera.Roll);
            }

            if (!newCamera.IsEqual(originalCamera))
            {
                sceneView.SetViewpointCamera(newCamera);
            }
        }
		private void UpdateView()
		{
			if (sceneView == null)
				return;
			if (controller == null || !controller.IsConnected)
				return;
			//The Camera might change whil doing all the calculations...
			var originalCamera = sceneView.Camera;
			if (originalCamera == null)
				return;
			var newCamera = originalCamera;
			var state = controller.GetState();
			var time2 = DateTime.Now;
			bool isRightShoulderPressed = state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.RightShoulder);
			//Choose if staying at the same alitude or not
			if (isRightShoulderPressed)
			{
				var elapsed = time2 - time;
				if (elapsed != TimeSpan.Zero && elapsed > TimeSpan.FromSeconds(0.25))
				{
					timer.Stop();
					timer.Start();
					relativeToDirection = !relativeToDirection;
					time = time2;
				}
			}
			else
				time = time2;

			double LeftThumbStickX = 0;
			double LeftThumbStickY = 0;
			double RightThumbStickX = 0;
			double RightThumbStickY = 0;
			var gamePad = new NormalizedGamepad(state.Gamepad);
			LeftThumbStickX = gamePad.LeftThumbXNormalized;
			LeftThumbStickY = gamePad.LeftThumbYNormalized;
			RightThumbStickX = gamePad.RightThumbXNormalized;
			RightThumbStickY = gamePad.RightThumbYNormalized;

			if ((LeftThumbStickX != 0) || (LeftThumbStickY != 0) || (RightThumbStickX != 0) || (RightThumbStickY != 0))
			{
				//Move sideways (crab move), so calling move forward after modifying heading and tilt, then resetting heading and tilt 
				if (LeftThumbStickX != 0)
				{
					//Distance depends on the altitude, but must have minimum values (one way and the other) to not be almost nothing when altitude gets to 0)
					var distance = (double)LeftThumbStickX * newCamera.Location.Z / 40;
					if (distance > 0 && distance < 0.75)
						distance = 0.75;
					if (distance < 0 && distance > -0.75)
						distance = -0.75;
					newCamera = newCamera.SetPitch(90).Rotate(90, 0).MoveForward(distance).Rotate(-90, 0).SetPitch(newCamera.Pitch);
				}

				var distance2 = (double)LeftThumbStickY * newCamera.Location.Z / 40;
				if (distance2 > 0 && distance2 < 1)
					distance2 = 1;
				if (distance2 < 0 && distance2 > -1)
					distance2 = -1;
				//Move in the direction of the camera (modify the elevation)
				newCamera = newCamera.MoveForward(distance2).Rotate((double)RightThumbStickX, newCamera.Pitch > 2 ? -(double)RightThumbStickY : RightThumbStickY < 0 ? -(double)RightThumbStickY : 0);

				//If move in the direction of the camera but staying at the same elevation.
				if (!relativeToDirection)
					newCamera = newCamera.SetLocation(new MapPoint(newCamera.Location.X, newCamera.Location.Y, originalCamera.Location.Z, newCamera.Location.SpatialReference));
			}

			//Triggers are just elevating the camera

			double elevationDistance = (double)(state.Gamepad.RightTrigger - state.Gamepad.LeftTrigger);
			if ((elevationDistance != 0) && (newCamera.Location.Z != double.NaN))
			{
				var distance = elevationDistance * Math.Abs(newCamera.Location.Z) / 10000;
				newCamera = newCamera.Elevate(distance > 1 ? distance : distance < 1 ? distance : distance < 0 ? -1 : 1);
			}


			if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.DPadUp))
				newCamera = newCamera.SetHeading(0);

			if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.DPadDown))
				newCamera = newCamera.SetHeading(180);

			if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.DPadLeft))
				newCamera = newCamera.SetHeading(270);

			if (state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.DPadRight))
				newCamera = newCamera.SetHeading(90);

			if (!newCamera.IsEqual(originalCamera))
				sceneView.SetView(newCamera);

		}