public LogicManager(MainWindow _window) { graphToVisualize = null; citiesLocations = null; citiesConnections = null; defaultCitiesLocations = new CitiesLocations(); defaultCitiesConnections = new CitiesConnections(); algCitiesLocations = new CitiesLocations(); startCity = null; finalCity = null; algSpeed = Alg_Speed.Fast; algHeuristic = Heuristic.Distance; resources = _window.Resources; window = _window; graphManager = new GraphManager(window); algorithm = null; algThread = null; IsRunningAlg = false; }
/// <summary> /// Updates the vertex internal information. /// Maintains consistency between the vertex's position on the /// screen and ins internal information. /// </summary> /// <param name="vertex"></param> /// <param name="graphLayout"></param> public void UpdateVertexInfo(ref VertexCity vertex, ref GraphLayoutCity graphLayout) { if (vertex != null) { // Get the vertex control VertexControl vertexControl = graphLayout.GetVertexControl(vertex); // If we could find the verex control if (vertexControl != null) { double tempXCoord, tempYCoord; // Get the vertex screen coordinates tempXCoord = GraphCanvas.GetX(vertexControl); tempYCoord = GraphCanvas.GetY(vertexControl); // Update the internal information if the coordinates // are in range from 0 to 800 if ((tempXCoord > 0) && (tempXCoord < 800) && (tempYCoord > 0) && (tempYCoord < 800)) { vertex.CityCoordinates.setX((int)tempXCoord); vertex.CityCoordinates.setY((int)tempYCoord); } // Set the new screen coordinates GraphCanvas.SetX(vertexControl, vertex.CityCoordinates.getX()); GraphCanvas.SetY(vertexControl, vertex.CityCoordinates.getY()); } } }
/// <summary> /// Start A-star algorithm in a separate thread /// </summary> /// <param name="graphLayout"></param> /// <param name="richTextBoxLog"></param> /// <param name="window"></param> public void StartAlgorithm(ref GraphLayoutCity graphLayout, ref TextBox richTextBoxLog, MainWindow window) { // If already running if (IsRunningAlg) { algorithm.WaitMutex.WaitOne(); algorithm.CanContinue = true; algorithm.WaitMutex.ReleaseMutex(); } // If not, then start a new thread else { algorithm = new Algorithm(citiesLocations, citiesConnections, startCity.City, finalCity.City, algSpeed, algHeuristic, ref graphLayout, ref richTextBoxLog, resources, graphManager); // Set window and create a mutex algorithm.Window = window; algorithm.WaitMutex = new Mutex(); // Null the start and end cities startCity = null; finalCity = null; // Create and start the thread algThread = new Thread(new ThreadStart(algorithm.RunAlgThread)); algThread.SetApartmentState(ApartmentState.STA); algThread.Start(); // Allow the algorithm to continue algorithm.WaitMutex.WaitOne(); algorithm.CanContinue = true; algorithm.WaitMutex.ReleaseMutex(); } }
private void FinalCityItem_Click(object sender, RoutedEventArgs e) { MenuItem menuItem = (MenuItem)sender; VertexCity vertex = (VertexCity)menuItem.DataContext; logicManager.MarkFinalCity(vertex, ref graphLayout); }
/// <summary> /// Apply the style to the vertex. Must be called from the UI /// thread because it does not use a deispatcher. /// </summary> /// <param name="vertex"> /// Tartget vertex for the style /// </param> /// <param name="graphLayout"> /// Graph layout /// </param> /// <param name="style"> /// The style to apply /// </param> public void SetStyle(VertexCity vertex, GraphLayoutCity graphLayout, Style style) { // Get vertex control and update style if possible VertexControl vertexControl = graphLayout.GetVertexControl(vertex); if (vertexControl != null) { vertexControl.Style = style; } }
/// <summary> /// Update the vertex information in the locations file and also update the /// vertex internal information /// </summary> /// <param name="vertex"></param> /// <param name="graphLayout"></param> public void UpdateVertexInfo(VertexCity vertex, ref GraphLayoutCity graphLayout) { graphManager.UpdateVertexInfo(ref vertex, ref graphLayout); if (vertex != null) { Coordinates tempCityCoordinates; if (citiesLocations.locations.TryGetValue(vertex.City, out tempCityCoordinates)) { tempCityCoordinates.setX(vertex.CityCoordinates.getX()); tempCityCoordinates.setY(vertex.CityCoordinates.getY()); } } }
/// <summary> /// Mark the city as Final City on the screen /// </summary> /// <param name="vertex"></param> /// <param name="graphLayout"></param> public void MarkFinalCity(VertexCity vertex, ref GraphLayoutCity graphLayout) { if (finalCity != null) { Style oldStyle = (Style)resources["DefaultCityStyle"]; graphManager.SetStyle(finalCity, graphLayout, oldStyle); } if ((startCity != null) && (startCity.Equals(vertex))) { startCity = null; } finalCity = vertex; Style newStyle = (Style)resources["FinalCityStyle"]; graphManager.SetStyle(vertex, graphLayout, newStyle); }
/// <summary> /// Delete the city and all connected edges /// </summary> /// <param name="cityToDelete"> /// City we want to delete /// </param> /// <param name="graphLayout"> /// Graph layout /// </param> public void DeleteCity(VertexCity cityToDelete, ref GraphLayoutCity graphLayout) { graphLayout.Graph.RemoveVertex(cityToDelete); }
/// <summary> /// Mark start city on the screen /// </summary> /// <param name="vertex"></param> /// <param name="graphLayout"></param> public void MarkStartCity(VertexCity vertex, ref GraphLayoutCity graphLayout) { if (startCity != null) { Style oldStyle = (Style)resources["DefaultCityStyle"]; graphManager.SetStyle(startCity, graphLayout, oldStyle); } if ((finalCity != null) && (finalCity.Equals(vertex))) { finalCity = null; } startCity = vertex; Style newStyle = (Style)resources["StartCityStyle"]; graphManager.SetStyle(vertex, graphLayout, newStyle); }
/// <summary> /// Delete city from the locations and the connections files. Also /// delete it from the screen. /// </summary> /// <param name="cityToDelete"></param> /// <param name="graphLayout"></param> public void DeleteCity(VertexCity cityToDelete, ref GraphLayoutCity graphLayout) { graphManager.DeleteCity(cityToDelete, ref graphLayout); DeleteCityFromData(cityToDelete.City); }
public bool Equals(VertexCity toCompare) { return((toCompare != null) && (City.Equals(toCompare.City))); }