protected override void Update(GameTime gameTime) { if (Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } // Verify if the left or right mouse button was pressed var mouseState = Mouse.GetState(); bool isLeftButtonPressed = _lastMouseState.LeftButton == ButtonState.Released && mouseState.LeftButton == ButtonState.Pressed; bool isRightButtonPressed = _lastMouseState.RightButton == ButtonState.Released && mouseState.RightButton == ButtonState.Pressed; // For each airport that the mouse is hovering foreach (var(_, airport) in GraphUtils.Airports.Where(airport => airport.Value.Area.Contains(mouseState.Position))) { if (isLeftButtonPressed) { // Sets the node as the starting airport GraphUtils.StartAirport = GraphUtils.AirportIndex[airport.Name]; // Find the shortest path GraphUtils.DesiredPath = GraphUtils.GetPath(GraphUtils.UsedGraph, GraphUtils.StartAirport, GraphUtils.EndAirport); // Run the traveling merchant problem _worldTravel = WorldTravel.Heuristic(GraphUtils.PriceGraph, GraphUtils.Airports.Count, GraphUtils.StartAirport); } else if (isRightButtonPressed) { // Sets the node as the final airport GraphUtils.EndAirport = GraphUtils.AirportIndex[airport.Name]; // Find the shortest path GraphUtils.DesiredPath = GraphUtils.GetPath(GraphUtils.UsedGraph, GraphUtils.StartAirport, GraphUtils.EndAirport); } } // Update the GUI buttons Utils.UpdateButtons(mouseState.Position, isLeftButtonPressed); // Update the state of the mouse and keboard _lastKeyboardState = Keyboard.GetState(); _lastMouseState = Mouse.GetState(); base.Update(gameTime); }
protected override void LoadContent() { _spriteBatch = new SpriteBatch(GraphicsDevice); // Loads the map and the font _map = Content.Load <Texture2D>("mundi"); _font = Content.Load <SpriteFont>("Arial"); // Creates the white pixel _pixel = new Texture2D(GraphicsDevice, 1, 1); _pixel.SetData(new[] { Color.White }); // Creates a button Utils.GraphButtons.Add("distance", new GraphButton("Distancia", 10, 710, _font.MeasureString("Distancia"), Color.Aquamarine, Color.Aqua, Color.Black)); // Activates the button by default Utils.GraphButtons["distance"].IsActive = true; // Declares a custom function to be executed when the button is clicked Utils.GraphButtons["distance"].OnPress = () => { // Toggle the two buttons Utils.GraphButtons["distance"].IsActive = true; Utils.GraphButtons["price"].IsActive = false; // Selects the distance matrix as the default one GraphUtils.UsedGraph = GraphUtils.DistanceGraph; // Recalculates the shortest path GraphUtils.DesiredPath = GraphUtils.GetPath(GraphUtils.UsedGraph, GraphUtils.StartAirport, GraphUtils.EndAirport); return(Utils.GraphButtons["distance"].IsActive); }; // Creates a button Utils.GraphButtons.Add("price", new GraphButton("Preco", 10 + Utils.GraphButtons["distance"].Area.Width + Utils.GraphButtons["distance"].Area.X, 710, _font.MeasureString("Preco"), Color.Aquamarine, Color.Aqua, Color.Black)); Utils.GraphButtons["price"].OnPress = () => { Utils.GraphButtons["price"].IsActive = true; Utils.GraphButtons["distance"].IsActive = false; GraphUtils.UsedGraph = GraphUtils.PriceGraph; GraphUtils.DesiredPath = GraphUtils.GetPath(GraphUtils.UsedGraph, GraphUtils.StartAirport, GraphUtils.EndAirport); return(Utils.GraphButtons["price"].IsActive); }; // Creates a button Utils.GraphButtons.Add("height", new GraphButton("Altitude", 10 + Utils.GraphButtons["price"].Area.Width + +Utils.GraphButtons["price"].Area.X, 710, _font.MeasureString("Altitude"), Color.Aquamarine, Color.Aqua, Color.Black)); // Creates a button Utils.GraphButtons.Add("tour", new GraphButton("Volta ao Mundo", 10 + Utils.GraphButtons["height"].Area.Width + Utils.GraphButtons["height"].Area.X, 710, _font.MeasureString("Volta ao Mundo"), Color.Aquamarine, Color.Aqua, Color.Black)); // Creates a button Utils.GraphButtons.Add("brute", new GraphButton("Forca Bruta", 10 + Utils.GraphButtons["tour"].Area.Width + Utils.GraphButtons["tour"].Area.X, 710, _font.MeasureString("Forca Bruta"), Color.Aquamarine, Color.Aqua, Color.Black)); GraphUtils.DesiredPath = new List <(int, int)>(); // Read the files GraphUtils.ReadAirports(_airportPath ?? @"D:\Development\_Projects\Graphs\Graphs\Content\aer_teste.txt", _map); GraphUtils.ReadFlights(_fligthPath ?? @"D:\Development\_Projects\Graphs\Graphs\Content\voos_teste.txt"); // Calculate the flight intersections GraphUtils.ComputeIntersections(); // Run the traveling merchant problem with the first node _worldTravel = WorldTravel.Heuristic(GraphUtils.PriceGraph, GraphUtils.Airports.Count, 0); // Run it with the brute force method _bruteWorldTravel = WorldTravel.BruteForce(GraphUtils.PriceGraph, GraphUtils.Airports.Count); // Selects the distance matrix as the default one GraphUtils.UsedGraph = GraphUtils.DistanceGraph; }