From c7a20a975f94ab14ebe242a8916b098a777d49c7 Mon Sep 17 00:00:00 2001 From: Daniel Carta <79732052+immortaldan10@users.noreply.github.com> Date: Tue, 2 Jul 2024 21:05:58 -0600 Subject: [PATCH] Lord of lightning shifts his gaze Added comments and renamed all instances of "robot" to "node" --- main.py | 89 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 46 insertions(+), 43 deletions(-) diff --git a/main.py b/main.py index 6fc2d69..5a829fc 100644 --- a/main.py +++ b/main.py @@ -45,19 +45,20 @@ class MainWindow(QMainWindow): self.coordinates = np.empty((0, 2), dtype=int) self.last_click_time = 0 - self.robot_size = 35 + self.node_size = 35 self.handle_size = 15 self.rotation_handle_distance = 35 self.handles = [] self.rotation_handles = [] - self.robot_angles = [] + self.node_angles = [] self.dragging_handle = False - self.dragging_robot = False + self.dragging_node = False self.dragging_rotation_handle = False self.dragging_handle_index = -1 - self.dragging_robot_index = -1 + self.dragging_node_index = -1 self.dragging_rotation_handle_index = -1 + #Tells when either the left or right mouse button is pressed def mousePressEvent(self, event: QMouseEvent): pos = self.image_label.mapFrom(self, event.position().toPoint()) x, y = pos.x(), pos.y() @@ -74,20 +75,19 @@ class MainWindow(QMainWindow): current_time = event.timestamp() if (current_time - self.last_click_time < 300 and (pos - self.last_click_pos).manhattanLength() < 5): - # Double click detected - robot_index = self.is_point_in_robot(x, y) - if robot_index != -1: - self.delete_robot(robot_index) + node_index = self.is_point_in_node(x, y) + if node_index != -1: + self.delete_node(node_index) else: handle_index = self.is_point_in_handle(x, y) if handle_index != -1: self.dragging_handle = True self.dragging_handle_index = handle_index else: - robot_index = self.is_point_in_robot(x, y) - if robot_index != -1: - self.dragging_robot = True - self.dragging_robot_index = robot_index + node_index = self.is_point_in_node(x, y) + if node_index != -1: + self.dragging_node = True + self.dragging_node_index = node_index else: rotation_handle_index = self.is_point_in_rotation_handle(x, y) if rotation_handle_index != -1: @@ -97,7 +97,8 @@ class MainWindow(QMainWindow): self.last_click_time = current_time self.last_click_pos = pos - def delete_robot(self, index): + #Deletes node + def delete_node(self, index): self.coordinates = np.delete(self.coordinates, index, axis=0) if index < len(self.handles): del self.handles[index] @@ -105,48 +106,47 @@ class MainWindow(QMainWindow): del self.rotation_handles[index] self.draw_scene() + # def mouseMoveEvent(self, event: QMouseEvent): if self.dragging_handle: pos = self.image_label.mapFrom(self, event.position().toPoint()) x, y = pos.x(), pos.y() self.handles[self.dragging_handle_index] = QPoint(x, y) self.draw_scene() - elif self.dragging_robot: + elif self.dragging_node: pos = self.image_label.mapFrom(self, event.position().toPoint()) x, y = pos.x(), pos.y() - self.coordinates[self.dragging_robot_index] = [x, y] + self.coordinates[self.dragging_node_index] = [x, y] self.calculate_handle_pos() self.calculate_rotation_handle_pos() self.draw_scene() elif self.dragging_rotation_handle: pos = self.image_label.mapFrom(self, event.position().toPoint()) x, y = pos.x(), pos.y() - robot_x, robot_y = self.coordinates[self.dragging_rotation_handle_index] - - # Calculate the angle between the robot and the rotation handle - dx = x - robot_x - dy = y - robot_y + node_x, node_y = self.coordinates[self.dragging_rotation_handle_index] + dx = x - node_x + dy = y - node_y angle = np.arctan2(dy, dx) - # Update the robot angle - self.robot_angles[self.dragging_rotation_handle_index] = angle + self.node_angles[self.dragging_rotation_handle_index] = angle - # Update the position of the rotation handle based on the angle and distance - rotation_handle_x = int(robot_x + self.rotation_handle_distance * np.cos(angle)) - rotation_handle_y = int(robot_y + self.rotation_handle_distance * np.sin(angle)) + rotation_handle_x = int(node_x + self.rotation_handle_distance * np.cos(angle)) + rotation_handle_y = int(node_y + self.rotation_handle_distance * np.sin(angle)) self.rotation_handles[self.dragging_rotation_handle_index] = QPoint(rotation_handle_x, rotation_handle_y) self.draw_scene() self.draw_scene() + #Resets dragging when mouse is released def mouseReleaseEvent(self, event: QMouseEvent): if event.button() == Qt.LeftButton: self.dragging_handle = False self.dragging_handle_index = -1 - self.dragging_robot = False - self.dragging_robot_index = -1 + self.dragging_node = False + self.dragging_node_index = -1 self.dragging_rotation_handle = False self.dragging_rotation_handle_index = -1 + #These 3 are for distinguishing what the user is clicking def is_point_in_rotation_handle(self, x, y): for i, handle in enumerate(self.rotation_handles): if (abs(x - handle.x()) <= self.handle_size // 2 and @@ -161,13 +161,14 @@ class MainWindow(QMainWindow): return i return -1 - def is_point_in_robot(self, x, y): - for i, (robot_x, robot_y) in enumerate(self.coordinates): - if (abs(x - robot_x) <= self.robot_size // 2 and - abs(y - robot_y) <= self.robot_size // 2): + def is_point_in_node(self, x, y): + for i, (node_x, node_y) in enumerate(self.coordinates): + if (abs(x - node_x) <= self.node_size // 2 and + abs(y - node_y) <= self.node_size // 2): return i return -1 + #Calculating the handle positions def calculate_handle_pos(self): if len(self.coordinates) >= 2: x1, y1 = self.coordinates[-2] @@ -176,10 +177,10 @@ class MainWindow(QMainWindow): def calculate_rotation_handle_pos(self): for i, (x, y) in enumerate(self.coordinates): - if i >= len(self.robot_angles): - self.robot_angles.append(np.pi) # Default angle for new robots + if i >= len(self.node_angles): + self.node_angles.append(np.pi) # Default angle for new nodes - angle = self.robot_angles[i] + angle = self.node_angles[i] rotation_handle_x = int(x + self.rotation_handle_distance * np.cos(angle)) rotation_handle_y = int(y + self.rotation_handle_distance * np.sin(angle)) @@ -189,6 +190,7 @@ class MainWindow(QMainWindow): else: self.rotation_handles[i] = QPoint(rotation_handle_x, rotation_handle_y) + #Draws the scene, big important function def draw_scene(self): self.pixmap = QPixmap(os.path.join(os.path.dirname(os.path.abspath(__file__)), "images", "Field.png")) painter = QPainter(self.pixmap) @@ -198,10 +200,10 @@ class MainWindow(QMainWindow): painter.setPen(grey_pen) for i, handle in enumerate(self.handles): if i < len(self.coordinates) and i + 1 < len(self.coordinates): - robot1_pos = QPoint(self.coordinates[i][0], self.coordinates[i][1]) - robot2_pos = QPoint(self.coordinates[i+1][0], self.coordinates[i+1][1]) - painter.drawLine(handle, robot1_pos) - painter.drawLine(handle, robot2_pos) + node1_pos = QPoint(self.coordinates[i][0], self.coordinates[i][1]) + node2_pos = QPoint(self.coordinates[i+1][0], self.coordinates[i+1][1]) + painter.drawLine(handle, node1_pos) + painter.drawLine(handle, node2_pos) if len(self.coordinates) >= 2: for i in range(len(self.coordinates) - 1): @@ -233,7 +235,7 @@ class MainWindow(QMainWindow): for i, (x, y) in enumerate(self.coordinates): if i < len(self.rotation_handles): - angle = self.robot_angles[i] + angle = self.node_angles[i] painter.save() painter.translate(x, y) @@ -243,8 +245,8 @@ class MainWindow(QMainWindow): pen.setWidth(2) painter.setPen(pen) painter.setBrush(Qt.NoBrush) - painter.drawRect(-self.robot_size // 2, -self.robot_size // 2, - self.robot_size, self.robot_size) + painter.drawRect(-self.node_size // 2, -self.node_size // 2, + self.node_size, self.node_size) painter.restore() @@ -252,8 +254,8 @@ class MainWindow(QMainWindow): font = painter.font() font.setPointSize(25) painter.setFont(font) - painter.drawText(QRect(x - self.robot_size//2, y - self.robot_size//2, - self.robot_size, self.robot_size), + painter.drawText(QRect(x - self.node_size//2, y - self.node_size//2, + self.node_size, self.node_size), Qt.AlignCenter, str(i + 1)) for i, handle in enumerate(self.rotation_handles): @@ -267,6 +269,7 @@ class MainWindow(QMainWindow): self.image_label.setPixmap(self.pixmap) + #Clears all def clear_points(self): self.coordinates = np.empty((0, 2), dtype=int) self.handles = []