diff --git a/resources/graphs/state_machine/graph.py b/resources/graphs/state_machine/graph.py index e5e22b7ec4dc91979622737cbe0eaaaa59018c20..d8e9f06c81dc6582dd51f5e5f41d9af08975780d 100644 --- a/resources/graphs/state_machine/graph.py +++ b/resources/graphs/state_machine/graph.py @@ -75,59 +75,62 @@ class StateMachineGraph(DirectedGraph): shortest_x = (100, None) shortest_y = (100, None) - # Loop through all initial states, assigning y-coords based on a divided grid setup. - # Grids are from top to bottom, to evenly distribute space to all start nodes. - initial_state_count = len(self._initial_states) + # Loop through all final states, assigning y-coords based on a divided grid setup. + # Grids are from top to bottom, to evenly distribute space to all final nodes. + # Final states are done first, in case a node is both final and initial. In this case, we want it on the left. + # Going first means that the intial state logic will override the final state logic, putting it left like we + # want. + final_state_count = len(self._final_states) state_index = 1 - grid_length = max_direction / initial_state_count - for initial_state in self._initial_states.values(): + grid_length = max_direction / final_state_count + for final_state in self._final_states.values(): # Get grid values. current_grid_end = grid_length * state_index current_grid_start = current_grid_end - grid_length state_index += 1 # Assign node to calculated grid. - x_coord = 5 + x_coord = 95 y_coord = int((current_grid_start + current_grid_end) / 2) - self._nodes[initial_state.get_name()].x_coord = x_coord - self._nodes[initial_state.get_name()].y_coord = y_coord + self._nodes[final_state.get_name()].x_coord = x_coord + self._nodes[final_state.get_name()].y_coord = y_coord # Record extreme positions. if x_coord > farthest_x[0]: - farthest_x = (x_coord, initial_state) + farthest_x = (x_coord, final_state) if x_coord < shortest_x[0]: - shortest_x = (x_coord, initial_state) + shortest_x = (x_coord, final_state) if y_coord > farthest_y[0]: - farthest_y = (y_coord, initial_state) + farthest_y = (y_coord, final_state) if y_coord < shortest_y[0]: - shortest_y = (y_coord, initial_state) + shortest_y = (y_coord, final_state) - # Loop through all final states, assigning y-coords based on a divided grid setup. - # Grids are from top to bottom, to evenly distribute space to all final nodes. - final_state_count = len(self._final_states) + # Loop through all initial states, assigning y-coords based on a divided grid setup. + # Grids are from top to bottom, to evenly distribute space to all start nodes. + initial_state_count = len(self._initial_states) state_index = 1 - grid_length = max_direction / final_state_count - for final_state in self._final_states.values(): + grid_length = max_direction / initial_state_count + for initial_state in self._initial_states.values(): # Get grid values. current_grid_end = grid_length * state_index current_grid_start = current_grid_end - grid_length state_index += 1 # Assign node to calculated grid. - x_coord = 95 + x_coord = 5 y_coord = int((current_grid_start + current_grid_end) / 2) - self._nodes[final_state.get_name()].x_coord = x_coord - self._nodes[final_state.get_name()].y_coord = y_coord + self._nodes[initial_state.get_name()].x_coord = x_coord + self._nodes[initial_state.get_name()].y_coord = y_coord # Record extreme positions. if x_coord > farthest_x[0]: - farthest_x = (x_coord, final_state) + farthest_x = (x_coord, initial_state) if x_coord < shortest_x[0]: - shortest_x = (x_coord, final_state) + shortest_x = (x_coord, initial_state) if y_coord > farthest_y[0]: - farthest_y = (y_coord, final_state) + farthest_y = (y_coord, initial_state) if y_coord < shortest_y[0]: - shortest_y = (y_coord, final_state) + shortest_y = (y_coord, initial_state) # Now loop through all other nodes and assign coordinates. for node in self.get_all_nodes().values():