Exemplo n.º 1
0
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            // For accelerometer readings
            motionManager = new CMMotionManager();
            motionManager.AccelerometerUpdateInterval = 0.01; // 100Hz

            //To handle long presses and bring up path start/end menu
            var longPressManager = new UILongPressGestureRecognizer();

            //Graph loading code
			//Graph loading code
			var assembly = Assembly.GetExecutingAssembly();
			var asset = assembly.GetManifestResourceStream("Navigator.iOS.Resources.dcsfloorWideDoors.xml");

			pf = new Pathfinding.Pathfinding(new Dictionary<int, Stream>()
				{
					{0,assembly.GetManifestResourceStream("Navigator.iOS.Resources.dcsfloorWideDoors.xml")},
					{1,assembly.GetManifestResourceStream("Navigator.iOS.Resources.dcsFloor1.xml")}
				},assembly.GetManifestResourceStream("Navigator.iOS.Resources.Rooms.xml") );
			pf.CurrentFloor = 0;

			while (true)
			{
				if (pf.Ready)            

					break;
				Thread.Sleep(500);
			}
				
			//set up the search bar and prediction box
			var searchController = new CustomSearchController(this, SearchBar, SearchPredictionTable, pf.Rooms);
		
            floorPlanGraph = Graph.Load(asset);

            wallCollImg = UIImage.FromBundle("Images/dcsfloorWideDoors.png");

            col = new Collision(floorPlanGraph, new StepDetector());

            ((Collision)col).WallCol = new WallCollision ((x,y) => GetPixelColor(new PointF(x, y), wallCollImg));
            wallColTest = new WallCollision ((x,y) => GetPixelColor(new PointF(x, y), wallCollImg));

            pathView = new PathView (wallColTest);

            col.SetLocation(707.0f, 677.0f);
            col.PassHeading(90);
            col.PositionChanged += HandleStepsTaken;

			//Container for floorplan and any overlaid images
            var container = new UIView();

			//Will contain floorplan images
            floorplanImageView = new UIImageView();

			//Load floorplan images
            floorplanImageNoGrid = UIImage.FromBundle("Images/FinalDcsFloor1.png");
            floorplanImageWithGrid = UIImage.FromBundle("Images/dcsFloorWideDoorsGrid.png");

			//Initiate the location arrow
            locationArrow = new LocationArrowImageView();
            locationArrow.ScaleFactor = floorplanView.ZoomScale;
            pathView.ScaleFactor = floorplanView.ZoomScale;
            setStartPoint(690.0f, 840.0f);

			//Set sizes for floorplan view and path view
            floorplanView.ContentSize = floorplanImageNoGrid.Size;
            pathView.Frame = new CGRect(new CGPoint(0, 0), floorplanImageNoGrid.Size);

			//Add subviews to the container (including pathview and floorplanview)
            container.AddSubview(floorplanImageView);
            container.AddSubview(locationArrow);
            floorplanImageView.AddSubview(pathView);
            changeFloorPlanImage(floorplanImageView, floorplanImageNoGrid);
            container.SizeToFit();

			//Adjust scrolling and zooming properties for the floorplanView
            floorplanView.MaximumZoomScale = 1f;
            floorplanView.MinimumZoomScale = .25f;
            floorplanView.AddSubview(container);
            floorplanView.ViewForZoomingInScrollView += (UIScrollView sv) => { return floorplanImageView; };

			//Variables needed to convert device acceleration to world z direction acceleration
			double accelX = 0, accelY = 0, accelZ = 0;

			//Scale location arrow and paths when zooming the floorplan
            floorplanView.DidZoom += (sender, e) =>
            {
                locationArrow.ScaleFactor = floorplanView.ZoomScale;
                pathView.ScaleFactor = floorplanView.ZoomScale;
            };

			//Pass acceleremoter values to the collision class
            motionManager.StartAccelerometerUpdates(NSOperationQueue.CurrentQueue,
                (data, error) =>
                {
					accelX = data.Acceleration.X*9.8;
					accelY = data.Acceleration.Y*9.8;
					accelZ = Math.Sqrt(Math.Pow(accelX, 2) + Math.Pow(accelY, 2) + Math.Pow(data.Acceleration.Z*9.8, 2));

                    col.PassSensorReadings(CollisionSensorType.Accelometer, accelX,
                        accelY, accelZ);
                    //displayAccelVal((float)accelZ);
                });

			/*
			motionManager.StartDeviceMotionUpdates(NSOperationQueue.CurrentQueue, (data, error) =>
				{ 
					//data.Attitude.MultiplyByInverseOfAttitude(data.Attitude);
					var test = data.UserAcceleration.X;
					var accelRelZ = data.Attitude.RotationMatrix.m31 * accelX + data.Attitude.RotationMatrix.m32 * accelY + data.Attitude.RotationMatrix.m33 * accelZ;
					debugLabel.Text = "" + Math.Round(test, 2);//Math.Round(accelRelZ, 2);
				}
			);
			*/

			//LongPressManager will cause the path input menu to appear after a stationary long press
            longPressManager.AllowableMovement = 0;
            longPressManager.AddTarget(() => handleLongPress(longPressManager, floorPlanGraph));
            floorplanView.AddGestureRecognizer(longPressManager);

			//the location manager handles the phone heading
            locationManager = new CLLocationManager();
            locationManager.DesiredAccuracy = CLLocation.AccuracyBest;
            locationManager.HeadingFilter = 1;
            locationManager.UpdatedHeading += HandleUpdatedHeading;
            locationManager.StartUpdatingHeading();

			//Button currently used for testing purposes only

			//Another testing button
            simulationButton.TouchUpInside += delegate { col.StepTaken(false); };

            returnButton.TouchUpInside += delegate{ returnToMenu(); };

        }