added edge types
This commit is contained in:
parent
60416e447c
commit
b3366e5cfe
@ -26,6 +26,14 @@ class Editor:
|
||||
ZOOMS: tuple[float] = (0.25, 0.5, 1, 2, 4)
|
||||
CROSSHAIR_SIZE: int = 10
|
||||
|
||||
EDGE_TYPE_KEYS: dict[int, str] = {
|
||||
pygame.K_p: "path",
|
||||
pygame.K_n: "narrow_path",
|
||||
pygame.K_f: "ferry",
|
||||
pygame.K_b: "boat",
|
||||
pygame.K_r: "rails"
|
||||
}
|
||||
|
||||
def __init__(self):
|
||||
pygame.init()
|
||||
self.config: Config = Config(self.CONFIG_PATH)
|
||||
@ -138,6 +146,8 @@ class Editor:
|
||||
if len(self.selected_nodes) == 1:
|
||||
self.typing_text = self.graph.nodes[self.selected_nodes[0]].name
|
||||
self.is_renaming_node = True
|
||||
elif event.key in self.EDGE_TYPE_KEYS.keys():
|
||||
self.change_types(self.EDGE_TYPE_KEYS[event.key])
|
||||
elif event.type == pygame.KEYUP:
|
||||
if event.key == pygame.K_m:
|
||||
if self.original_move_pos is not None:
|
||||
@ -327,7 +337,7 @@ class Editor:
|
||||
def render_edges(self) -> None:
|
||||
for edge in self.graph.edges:
|
||||
node_1, node_2 = self.graph.get_edge_nodes(edge)
|
||||
color = (0, 255, 255) if edge.index in self.selected_edges else (255, 0, 0)
|
||||
color = (0, 255, 255) if edge.index in self.selected_edges else self.graph.TYPE_COLORS[edge.type]
|
||||
start = self.world_to_screen(node_1.x, node_1.z)
|
||||
end = self.world_to_screen(node_2.x, node_2.z)
|
||||
pygame.draw.line(self.win, color, start, end, self.line_size)
|
||||
@ -355,7 +365,7 @@ class Editor:
|
||||
start = self.world_to_screen(node_1.x, node_1.z)
|
||||
end = self.world_to_screen(node_2.x, node_2.z)
|
||||
pygame.draw.line(self.win, (0, 0, 0), start, end, self.edge_detect_radius)
|
||||
color = (0, 255, 255) if edge_index in self.selected_edges else (255, 0, 0)
|
||||
color = (0, 255, 255) if edge_index in self.selected_edges else self.graph.TYPE_COLORS[self.graph.edges[edge_index].type]
|
||||
pygame.draw.line(self.win, color, start, end, self.line_size)
|
||||
|
||||
def render_selection_rect(self):
|
||||
@ -449,6 +459,12 @@ class Editor:
|
||||
self.selected_nodes = []
|
||||
self.selected_edges = []
|
||||
|
||||
def clear_node_selection(self) -> None:
|
||||
self.selected_nodes = []
|
||||
|
||||
def clear_edge_selection(self) -> None:
|
||||
self.selected_edges = []
|
||||
|
||||
def rename_nodes(self) -> None:
|
||||
self.dirty = True
|
||||
for node in self.selected_nodes:
|
||||
@ -456,6 +472,14 @@ class Editor:
|
||||
self.typing_text = ""
|
||||
self.is_renaming_node = False
|
||||
|
||||
def get_type(self) -> str:
|
||||
type = ""
|
||||
for key, value in self.EDGE_TYPE_KEYS.items():
|
||||
if pygame.key.get_pressed()[key]:
|
||||
type = value
|
||||
break
|
||||
return type
|
||||
|
||||
def create_node(self, pos: tuple[int, int], typing_text: str = "") -> None:
|
||||
self.dirty = True
|
||||
self.graph.add_node(pos[0], pos[1], typing_text)
|
||||
@ -465,7 +489,7 @@ class Editor:
|
||||
|
||||
def create_edge(self, node_1: int, node_2: int) -> None:
|
||||
self.dirty = True
|
||||
self.graph.add_edge(node_1, node_2)
|
||||
self.graph.add_edge(node_1, node_2, type=self.get_type())
|
||||
|
||||
def get_hovering_nodes(self) -> tuple[list[int], list[float]]:
|
||||
hovering = []
|
||||
@ -578,15 +602,11 @@ class Editor:
|
||||
if left <= pos[0] <= right and top <= pos[1] <= bottom:
|
||||
if node.index not in self.selected_nodes:
|
||||
self.selected_nodes.append(node.index)
|
||||
print(left, "<=", pos[0], "<=", right)
|
||||
print(top, "<=", pos[1], "<=", bottom)
|
||||
for edge in self.graph.edges:
|
||||
pos = self.world_to_screen(*self.graph.get_edge_center(edge.index))
|
||||
if left <= pos[0] <= right and top <= pos[1] <= bottom:
|
||||
if edge.index not in self.selected_edges:
|
||||
self.selected_edges.append(edge.index)
|
||||
print(left, "<=", pos[0], "<=", right)
|
||||
print(top, "<=", pos[1], "<=", bottom)
|
||||
self.selection_rectangle = None
|
||||
|
||||
def start_moving(self):
|
||||
@ -648,6 +668,12 @@ class Editor:
|
||||
self.config.save()
|
||||
self.dirty = False
|
||||
|
||||
def change_types(self, type: str = "path"):
|
||||
self.dirty = True
|
||||
for edge in self.selected_edges:
|
||||
self.graph.set_edge_type(edge, type)
|
||||
self.clear_edge_selection()
|
||||
|
||||
|
||||
class State(Enum):
|
||||
STOPPING = auto()
|
||||
|
@ -1,6 +1,8 @@
|
||||
class Edge:
|
||||
def __init__(self, start: int, end: int, length: float, index: int):
|
||||
|
||||
def __init__(self, start: int, end: int, length: float, index: int, type: int):
|
||||
self.length: float = length
|
||||
self.start: int = start
|
||||
self.end: int = end
|
||||
self.index: int = index
|
||||
self.type: int = type
|
||||
|
@ -7,6 +7,23 @@ from src.graph.edge import Edge
|
||||
|
||||
|
||||
class Graph:
|
||||
|
||||
EDGE_TYPES: list[str] = [
|
||||
"path",
|
||||
"narrow_path",
|
||||
"ferry",
|
||||
"boat",
|
||||
"rails"
|
||||
]
|
||||
|
||||
TYPE_COLORS: list[tuple[int, int, int]] = [
|
||||
(255, 0, 0),
|
||||
(127, 0, 0),
|
||||
(0, 255, 0),
|
||||
(0, 127, 0),
|
||||
(0, 0, 255)
|
||||
]
|
||||
|
||||
def __init__(self):
|
||||
self.edges: list[Edge] = []
|
||||
self.nodes: list[Node] = []
|
||||
@ -14,12 +31,14 @@ class Graph:
|
||||
def add_node(self, x: int, z: int, name: str = "") -> None:
|
||||
self.nodes.append(Node(x, z, len(self.nodes), name))
|
||||
|
||||
def add_edge(self, start_index: int, end_index: int, auto_length: bool = True) -> None:
|
||||
def add_edge(self, start_index: int, end_index: int, type: str = "path", auto_length: bool = True) -> None:
|
||||
length = 0
|
||||
if auto_length:
|
||||
n1, n2 = self.nodes[start_index], self.nodes[end_index]
|
||||
length = sqrt((n1.x - n2.x)**2 + (n1.z - n2.z)**2)
|
||||
self.edges.append(Edge(start_index, end_index, length, len(self.edges)))
|
||||
type_n = 0 if type not in self.EDGE_TYPES else self.EDGE_TYPES.index(type)
|
||||
print("Type in add_edge:", type)
|
||||
self.edges.append(Edge(start_index, end_index, length, len(self.edges), type_n))
|
||||
|
||||
def delete_edge(self, edge: Edge) -> None:
|
||||
self.edges.remove(edge)
|
||||
@ -72,6 +91,10 @@ class Graph:
|
||||
def edge_exists(self, node_1: int, node_2: int) -> bool:
|
||||
return self.get_edge(node_1, node_2) != -1
|
||||
|
||||
def set_edge_type(self, edge_index: int, type: str = "path") -> None:
|
||||
edge = self.edges[edge_index]
|
||||
edge.type = 0 if type not in self.EDGE_TYPES else self.EDGE_TYPES.index(type)
|
||||
|
||||
def dijkstra(self, source_index: int, target_index: int) -> Optional[list[int]]:
|
||||
n = len(self.nodes)
|
||||
|
||||
@ -114,7 +137,7 @@ class Graph:
|
||||
f.write(f"n {node.x} {node.z} {node.name}\n")
|
||||
f.write("\n")
|
||||
for edge in self.edges:
|
||||
f.write(f"e {edge.start} {edge.end}\n")
|
||||
f.write(f"e {edge.start} {edge.end} {edge.type}\n")
|
||||
|
||||
@staticmethod
|
||||
def load(path: str) -> Graph:
|
||||
@ -131,9 +154,9 @@ class Graph:
|
||||
x, z = int(x), int(z)
|
||||
graph.add_node(x, z, name)
|
||||
elif entry_type == "e":
|
||||
start, end = values.split(" ", 2)
|
||||
start, end = int(start), int(end)
|
||||
graph.add_edge(start, end, False)
|
||||
start, end, type = values.split(" ", 3)
|
||||
start, end, type = int(start), int(end), int(type)
|
||||
graph.add_edge(start, end, auto_length=False, type=graph.EDGE_TYPES[type])
|
||||
|
||||
graph.recompute_lengths()
|
||||
return graph
|
||||
|
Binary file not shown.
Binary file not shown.
Before Width: | Height: | Size: 20 MiB |
Loading…
Reference in New Issue
Block a user