class Example(Frame): def __init__(self, parent): Frame.__init__(self, parent) self.parent = parent self.buttons = {} self.nodes = {} self.edges = {} self.active_node = None self.active_edge = None self.start = None self.x = None self.y = None self.cycles = None self.show_cycles_only_mode = False self.steps = None self.step_index = None self.parent.title("Demonstrační aplikace - nalezení elementárních cyklů v orientovaném grafu") self.style = Style() self.style.theme_use("default") self.pack(fill=BOTH, expand=1) self.columnconfigure(1, weight=1) self.columnconfigure(3, pad=7) self.rowconfigure(5, weight=1) self.rowconfigure(6, pad=7) self.label = Label(self, text="graf1.graphml") self.label.grid(sticky=W, pady=4, padx=5) self.canvas = Canvas(self) self.canvas.bind('<Double-Button-1>', self.event_add_node) self.canvas.bind('<Button-1>', self.event_add_edge_start) self.canvas.bind('<B1-Motion>', self.event_add_edge_move) self.canvas.bind('<ButtonRelease-1>', self.event_add_edge_end) self.canvas.bind('<Button-3>', self.event_move_node_start) self.canvas.bind('<B3-Motion>', self.event_move_node) self.canvas.pack() self.canvas.grid(row=1, column=0, columnspan=2, rowspan=6, padx=5, sticky=E + W + S + N) self.buttons['start'] = b = Button(self, text="Start", width=15) b.bind('<Button-1>', self.event_start) b.grid(row=1, column=3) self.buttons['next'] = b = Button(self, text=">>", width=15, state=DISABLED) b.bind('<Button-1>', self.event_next_step) b.grid(row=2, column=3, pady=4) self.buttons['prev'] = b = Button(self, text="<<", width=15, state=DISABLED) b.bind('<Button-1>', self.event_prev_step) b.grid(row=3, column=3, pady=4) b = Checkbutton(self, text="Pouze cykly", command=self.event_change_mode) b.grid(row=4, column=3, pady=4) self.buttons['reset'] = b = Button(self, text="Reset", width=15) b.bind('<Button-1>', self.event_reset) b.grid(row=6, column=3) menubar = Menu(self.parent) self.parent.config(menu=menubar) fileMenu = Menu(menubar) fileMenu.add_command(label="Načíst", command=self.onLoad) fileMenu.add_command(label="Uložit", command=self.onSave) fileMenu.add_separator() fileMenu.add_command(label="Konec", command=self.onExit) menubar.add_cascade(label="Soubor", menu=fileMenu) fileMenu = Menu(menubar) fileMenu.add_command(label="O aplikaci", command=self.onAbout) menubar.add_cascade(label="Nápověda", menu=fileMenu) def onExit(self): self.quit() def onLoad(self): fileTypes = [('Soubory typu GraphML', '*.graphml')] dialog = tkFileDialog.Open(self, filetypes=fileTypes) filename = dialog.show() if filename != '': self.readFile(filename) def onSave(self): fileTypes = [('GraphML files', '*.graphml')] dialog = tkFileDialog.SaveAs(self, filetypes=fileTypes) filename = dialog.show() if filename != '': if not filename.endswith(".graphml"): filename += ".graphml" self.writeFile(filename) def onAbout(self): box.showinfo("O aplikaci", "Demonstrace algoritmu nalezení elementárních cyklů v orientovaném grafu podle D. B. Johnsona. \n\n" "Autoři:\n" "Paulík Miroslav\n" "Pavlů Igor\n" "FIT VUT v Brně 2013") def readFile(self, filename): self.reset() try: parser = GraphMLParser() g = parser.parse(filename) except Exception: box.showerror("Chyba při zpracování vstupního souboru", "Chybný formát souboru.") return nodeMap = {} try: for gnode in g.nodes(): nodeMap[gnode.id] = self.__add_node(int(gnode['x']), int(gnode['y'])) except KeyError: box.showerror("Chyba při zpracování vstupního souboru", "Uzlum chybi udaje o pozici (atributy x a y).") self.reset() return try: for gedge in g.edges(): start = nodeMap[gedge.node1.id] end = nodeMap[gedge.node2.id] isCurve = gedge.node1.id == gedge.node2.id self.__add_edge(start, end, isCurve) self.label.configure(text=os.path.basename(filename)) except KeyError: box.showerror("Chyba při zpracování vstupního souboru", "Soubor obsahuje hrany spojujici neexistujici hrany") self.reset() return self.repaint() def writeFile(self, filename): g = Graph() for i in self.nodes: node = self.nodes[i] node.name = str(i) gnode = g.add_node(i) gnode['label'] = i gnode['x'] = node.x gnode['y'] = node.y for i in self.edges: edge = self.edges[i] edge.name = i parser = GraphMLParser() parser.write(g, filename) def repaint(self): for e in self.edges: edge = self.edges[e] self.canvas.itemconfigure(e, fill=edge.color) for v in self.nodes: node = self.nodes[v] self.canvas.itemconfigure(v, fill=node.color) def reset_colors(self): for n in self.nodes: self.nodes[n].color = "white" for e in self.edges: self.edges[e].color = "grey" def reset(self): self.nodes = {} self.edges = {} self.canvas.delete("all") self.buttons['prev'].config(state=DISABLED) self.buttons['next'].config(state=DISABLED) def run(self): x = ElementaryCircuitsDetector(self.nodes, self.edges) x.detect_cycles() self.cycles = x.cycles self.step_index = 0 self.steps = x.get_all_steps() self.algorithm_step_move(0) if len(self.steps) > 0: self.buttons['prev'].config(state=1) self.buttons['next'].config(state=1) def event_reset(self, event): self.reset() def event_prev_step(self, event): if str(self.buttons['prev'].cget("state")) != str(DISABLED): self.algorithm_step_move(-1) def event_next_step(self, event): if str(self.buttons['next'].cget("state")) != str(DISABLED): self.algorithm_step_move(1) def event_start(self, event): self.run() def event_change_mode(self): self.show_cycles_only_mode = not self.show_cycles_only_mode self.run() def event_add_edge_start(self, event): self.x = event.x self.y = event.y def event_add_edge_move(self, event): if self.active_edge is None: self.active_edge = self.canvas.create_line(self.x, self.y, event.x, event.y, arrow="last", width=2) else: x1, y1, x2, y2 = self.canvas.coords(self.active_edge) self.canvas.coords(self.active_edge, x1, y1, event.x, event.y) def event_add_edge_end(self, event): if self.active_edge is None: return x1, y1, x2, y2 = self.canvas.coords(self.active_edge) start = self.__get_node_from_position(x1, y1) end = self.__get_node_from_position(x2, y2) if start is None or end is None: self.canvas.delete(self.active_edge) elif start == end: self.canvas.delete(self.active_edge) edge = Edge(start, start, True) points = edge.get_coords() self.active_edge = self.canvas.create_line(points, width=2, smooth=True, arrow="last") self.canvas.tag_lower(self.active_edge, min(self.nodes.keys())) self.edges[self.active_edge] = edge else: x, y = self.__calculate_edge_end_from_nodes(start, end) self.canvas.coords(self.active_edge, start.x, start.y, x, y) self.canvas.tag_lower(self.active_edge, min(self.nodes.keys())) edge = Edge(start, end) self.edges[self.active_edge] = edge self.active_edge = None self.x = None self.y = None def event_move_node_start(self, event): id = self.__get_id_from_position(event.x, event.y) if id is None: return self.__activate_node(id) self.x = event.x self.y = event.y def event_move_node(self, event): id = self.active_node if id is None: return deltax = event.x - self.x deltay = event.y - self.y self.canvas.move(id, deltax, deltay) self.x = event.x self.y = event.y coord = self.canvas.coords(id) self.nodes[self.active_node].x = (coord[2] - coord[0]) / 2 + coord[0] self.nodes[self.active_node].y = (coord[3] - coord[1]) / 2 + coord[1] self.__repair_edge_starting_in_node(self.nodes[self.active_node]) self.__repair_edge_ending_in_node(self.nodes[self.active_node]) def event_add_node(self, event): id = self.__get_id_from_position(event.x, event.y, reverse=True) if id is None or id not in self.nodes: self.__add_node(event.x, event.y) def __repair_edge_ending_in_node(self, node): list_of_edge_ids = [] for edge_id in self.edges: edge = self.edges[edge_id] if edge.end == node: list_of_edge_ids.append(edge_id) for edge_id in list_of_edge_ids: edge = self.edges[edge_id] x, y = self.__calculate_edge_end_from_nodes(edge.start, edge.end) if edge.is_curve: coords = edge.get_coords() self.canvas.coords(edge_id, coords[0][0], coords[0][1], coords[1][0], coords[1][1], coords[2][0], coords[2][1], coords[3][0], coords[3][1]) else: self.canvas.coords(edge_id, edge.start.x, edge.start.y, x, y) def __repair_edge_starting_in_node(self, node): list_of_edge_ids = [] for edge_id in self.edges: edge = self.edges[edge_id] if edge.start == node: list_of_edge_ids.append(edge_id) for edge_id in list_of_edge_ids: edge = self.edges[edge_id] x, y = self.__calculate_edge_end_from_nodes(edge.start, edge.end) if edge.is_curve: coords = edge.get_coords() self.canvas.coords(edge_id, coords[0][0], coords[0][1], coords[1][0], coords[1][1], coords[2][0], coords[2][1], coords[3][0], coords[3][1]) else: self.canvas.coords(edge_id, edge.start.x, edge.start.y, x, y) def __calculate_edge_end_from_nodes(self, start_node, end_node): diffx = end_node.x - start_node.x diffy = end_node.y - start_node.y distance = math.sqrt(diffx ** 2 + diffy ** 2) if distance > 0: ratio = NODE_SIZE / 2 / distance x = end_node.x - diffx * ratio y = end_node.y - diffy * ratio return x, y return end_node.x, end_node.y def __activate_node(self, id): self.__deactivate_node() if id in self.nodes: self.active_node = id def __deactivate_node(self): self.active_node = None def __get_id_from_position(self, x, y, reverse=False): overlaping = self.canvas.find_overlapping(x, y, x, y) if len(overlaping) > 0: if reverse: return overlaping[-1] else: return overlaping[0] else: return None def __get_node_from_position(self, x, y): id = self.__get_id_from_position(x, y) if id is not None and id in self.nodes: return self.nodes[id] else: return None def __add_node(self, x, y): node = Node(x, y) id = self.canvas.create_oval(node.get_coord(), fill="blue") self.nodes[id] = node return node def __add_edge(self, start, end, is_curve=False): edge = Edge(start, end, is_curve) if is_curve: id = self.canvas.create_line(edge.get_coords(), width=2, smooth=True, arrow="last") else: id = self.canvas.create_line(start.x, start.y, end.x, end.y, arrow="last", width=2) self.edges[id] = edge self.canvas.tag_lower(id, min(self.nodes.keys())) self.__repair_edge_starting_in_node(start) return edge def algorithm_step_move(self, move): if self.show_cycles_only_mode: # cycles only if (self.step_index + move) < len(self.cycles) and self.step_index + move >= 0: self.step_index += move self.reset_colors() colors = ['green', 'blue', 'red', 'yellow', 'purple', 'brown'] color_index = self.step_index % len(colors) for edge in self.cycles[self.step_index]: edge.color = edge.start.color = edge.end.color = colors[color_index] self.repaint() else: if (self.step_index + move) < len(self.steps) and self.step_index + move >= 0: self.step_index += move self.reset_colors() for i in range(self.step_index + 1): colors = self.steps[i] for id in colors: if id in self.nodes.keys(): self.nodes[id].color = colors[id] elif id in self.edges.keys(): self.edges[id].color = colors[id] self.repaint()
class MazePlannerCanvas(Frame): """ MazePlannerCanvas contains the main frontend workhorse functionality of the entire application. it allows the user to graphically place nodes and define the edges between them """ def __init__(self, parent, status=None, manager=DataStore()): """ Construct an instance of the MazePlannerCanvas :param parent: The parent widget that the mazePlannerCanvas will sit in :param status: The statusbar that will receive mouse updates :type manager: DataStore :return: """ Frame.__init__(self, parent) self._manager = manager self._canvas = Canvas(self, bg="grey", cursor="tcross") self._canvas.pack(fill=BOTH, expand=1) self._commands = { (ControlSpecifier.DRAG_NODE, ExecutionStage.START) : self._begin_node_drag, (ControlSpecifier.CREATE_EDGE, ExecutionStage.START) : self._begin_edge, (ControlSpecifier.DRAG_NODE, ExecutionStage.END) : self._end_node_drag, (ControlSpecifier.CREATE_EDGE, ExecutionStage.END) : self._end_edge, (ControlSpecifier.DRAG_NODE, ExecutionStage.EXECUTE) : self._execute_drag, (ControlSpecifier.CREATE_EDGE, ExecutionStage.EXECUTE) : self._execute_edge, (ControlSpecifier.MENU, ExecutionStage.EXECUTE) : self._launch_menu, (ControlSpecifier.CREATE_NODE, ExecutionStage.EXECUTE) : self.create_new_node, } self._commands = load_controls(self._commands) self._edge_cache = \ { "x_start" : None, "y_start" : None, "x_end" : None, "y_end" : None, "item_start" : None, "item_end" : None, "edge" : None } self._command_cache = None self._cache = \ { "item" : None, "x" : 0, "y" : 0, "event" : None } self._status = status self._edge_bindings = {} self._node_listing = {} self._object_listing = {} self._curr_start = None self._construct(parent) def _construct(self, parent): """ Construct all of the event bindings and callbacks for mouse events """ self._canvas.focus_set() self._canvas.bind("<B1-Motion>", lambda event, m_event=Input_Event.DRAG_M1: self._handle_mouse_events(m_event, event)) self._canvas.bind("<B2-Motion>", lambda event, m_event=Input_Event.DRAG_M2: self._handle_mouse_events(m_event, event)) self._canvas.bind("<B3-Motion>", lambda event, m_event=Input_Event.DRAG_M3: self._handle_mouse_events(m_event, event)) self._canvas.bind("<ButtonPress-2>", lambda event, m_event=Input_Event.CLICK_M2: self._handle_mouse_events(m_event, event)) self._canvas.bind("<ButtonRelease-2>", lambda event, m_event=Input_Event.RELEASE_M2: self._handle_mouse_events(m_event, event)) self._canvas.bind("<ButtonPress-1>", lambda event, m_event=Input_Event.CLICK_M1: self._handle_mouse_events(m_event, event)) self._canvas.bind("<ButtonPress-3>", lambda event, m_event=Input_Event.CLICK_M3: self._handle_mouse_events(m_event, event)) self._canvas.bind("<ButtonRelease-1>", lambda event, m_event=Input_Event.RELEASE_M1: self._handle_mouse_events(m_event, event)) self._canvas.bind("<ButtonRelease-3>", lambda event, m_event=Input_Event.RELEASE_M3: self._handle_mouse_events(m_event, event)) self._canvas.bind("<Return>", lambda event, m_event=Input_Event.RETURN: self._handle_mouse_events(m_event, event)) self._canvas.bind("<Double-Button-1>", lambda event, m_event=Input_Event.D_CLICK_M1: self._handle_mouse_events(m_event, event)) self._canvas.bind("<Double-Button-2>", lambda event, m_event=Input_Event.D_CLICK_M2: self._handle_mouse_events(m_event, event)) self._canvas.bind("<Double-Button-3>", lambda event, m_event=Input_Event.D_CLICK_M3: self._handle_mouse_events(m_event, event)) self._canvas.bind("<Motion>", lambda event, m_event=None : self._handle_mot(m_event, event)) self._canvas.bind("<Enter>", lambda event: self._canvas.focus_set()) self._canvas.bind("<space>", lambda event, m_event=Input_Event.SPACE: self._handle_mouse_events(m_event, event)) def _handle_mot(self, m_event, event): """ Callback function to handle movement of the mouse Function updates the mouse location status bar as well as setting cache values to the current location of the mouse :m_event: The specifier for the type of event that has been generated :event: The tk provided event object """ event.x = int(self._canvas.canvasx(event.x)) event.y = int(self._canvas.canvasy(event.y)) self._status.set_text("Mouse X:" + str(event.x) + "\tMouse Y:" + str(event.y)) item = self._get_current_item((event.x, event.y)) if self._is_node(item): Debug.printi("Node: " + str(item), Debug.Level.INFO) if self._is_edge(item): d_x = self._edge_bindings[item].x_start - self._edge_bindings[item].x_end d_y = self._edge_bindings[item].y_start - self._edge_bindings[item].y_end square = (d_x * d_x) + (d_y * d_y) distance = int(math.sqrt(square)) Debug.printi("Edge: " + str(item) + " | Source: " + str(self._edge_bindings[item].item_start) + " | Target: " + str(self._edge_bindings[item].item_end) + " | Length: " + str(distance)) self._cache["x"] = event.x self._cache["y"] = event.y def _handle_mouse_events(self, m_event, event): """ Function that routes mouse events to the appropriate handlers Prints logging and UI information about the state of the mouse and then routes the mouse event to the appropriate handler :m_event: The specifier for the tupe of event that has been generated :event: The tk provided event object """ event.x = int(self._canvas.canvasx(event.x)) event.y = int(self._canvas.canvasy(event.y)) self._status.set_text("Mouse X:" + str(self._cache["x"]) + "\tMouse Y:" + str(self._cache["y"])) Debug.printet(event, m_event, Debug.Level.INFO) self._cache["event"] = event try: self._commands[m_event]((event.x, event.y)) except KeyError: Debug.printi("Warning, no control mapped to " + m_event, Debug.Level.ERROR) self._command_cache = m_event def _begin_node_drag(self, coords): """ Handles starting operations for dragging a node Updates the cache information regarding a node drag event, we will used this cache value as the handle on which node to update the information for :coords: The mouse coordinates associated with this event """ # Determine which node has been selected, cache this information item = self._get_current_item(coords) if item in self._node_listing: self._update_cache(item, coords) def _end_node_drag(self, coords): """ Performs actions to complete a node drag operation Validates node location, and other associated object information and updates the cache when a node drag is completed :coords: The coordinates associated with this event """ if self._cache["item"] is None: return # Obtain the final points x = coords[0] y = coords[1] item = self._cache["item"] self._validate_node_position(coords) container = self._manager.request(DataStore.DATATYPE.NODE, item) container.x_coordinate = x container.y_coordinate = y self._manager.inform(DataStore.EVENT.NODE_EDIT, container.empty_container(), self._cache["item"]) Debug.printi("Node " + str(self._cache["item"]) + " has been moved", Debug.Level.INFO) # Clean the cache self._clear_cache(coords) def _validate_node_position(self, coords): """ if x < 0: x = 0 if y < 0: y = 0 if x > self._canvas.winfo_width(): x = self._canvas.winfo_width()-25 if y > self._canvas.winfo_height(): y = self._canvas.winfo_height()-25 self._canvas.move(item, x, y) """ pass def _execute_drag(self, coords): """ Updates object position on canvas when user is dragging a node :param coords: The coordinates associated with this event """ # Abort drag if the item is not a node if self._cache["item"] not in self._node_listing: return # Update the drawing information delta_x = coords[0] - self._cache["x"] delta_y = coords[1] - self._cache["y"] # move the object the appropriate amount as long as the drag event has not been done on the empty canvas if not self._cache["item"] is None: self._canvas.move(self._cache["item"], delta_x, delta_y) # record the new position self._cache["x"] = coords[0] self._cache["y"] = coords[1] self._update_attached_edges(self._cache["item"], coords) self._update_attached_objects(self._cache["item"], coords) def _update_attached_objects(self, item, coords): if item not in self._object_listing: return container = self._manager.request(DataStore.DATATYPE.OBJECT, item) container.x_coordinate = coords[0] container.y_coordinate = coords[1] self._manager.inform(DataStore.EVENT.OBJECT_EDIT, container.empty_container(), item) def _update_attached_edges(self, node, coords): """ Updates all associated edges related to a node drag event :param node: The node that has been dragged :param coords: The mouse coordinates which are the new coordinates of the node """ # Go through dictionary and gather list of all attached edge bindings for a node start_bindings = [] for key, binding in self._edge_bindings.iteritems(): if binding.item_start == node: start_bindings.append(binding) end_bindings = [] for key, binding in self._edge_bindings.iteritems(): if binding.item_end == node: end_bindings.append(binding) # Adjust the bindings with this node as the starting edge for binding in start_bindings: self._canvas.delete(binding.edge) del self._edge_bindings[binding.edge] old_edge = binding.edge binding.edge = self._canvas.create_line(coords[0], coords[1], binding.x_end, binding.y_end, tags="edge", activefill="RoyalBlue1", tag="edge") self._edge_bindings[binding.edge] = binding self._manager.update_key(DataStore.EVENT.EDGE_EDIT, binding.edge, old_edge) binding.x_start = coords[0] binding.y_start = coords[1] # Adjust the bindings with this node as the ending edge for binding in end_bindings: self._canvas.delete(binding.edge) del self._edge_bindings[binding.edge] old_edge = binding.edge binding.edge = self._canvas.create_line(binding.x_start, binding.y_start, coords[0], coords[1], tags="edge", activefill="RoyalBlue1", tag="edge") self._edge_bindings[binding.edge] = binding self._manager.update_key(DataStore.EVENT.EDGE_EDIT, binding.edge, old_edge) binding.x_end = coords[0] binding.y_end = coords[1] # Remember to adjust all of the edges so that they sit under the node images self._canvas.tag_lower("edge") def _launch_menu(self, coords): """ Callback function in response to the pressing of the Return key Launches a context menu based on the location of the mouse :param coords: :return: """ # Configure the "static" menu entries -- they can't be static without seriously destroying readability # due to the Python version that is being used -.- so now it has to be not optimal until I find a better # solution p_menu = Menu(self._canvas) item = self._get_current_item((self._cache["x"], self._cache["y"])) updated_coords = self._canvas_to_screen((self._cache["x"], self._cache["y"])) if item is None: # No node is currently selected, create the general menu p_menu.add_command(label="Place Room", command=lambda: self.create_new_node((self._cache["x"], self._cache["y"]))) p_menu.add_command(label="Delete All", command=lambda: self.delete_all()) p_menu.tk_popup(updated_coords[0], updated_coords[1]) return if self._is_node(item): # Create the node specific menu p_menu.add_command(label="Place Object", command=lambda: self._mark_object((self._cache["x"], self._cache["y"]))) p_menu.add_command(label="Edit Room", command=lambda: self._selection_operation((self._cache["x"], self._cache["y"]))) p_menu.add_command(label="Delete Room", command=lambda: self.delete_node(self._get_current_item((self._cache["x"], self._cache["y"])))) p_menu.add_command(label="Mark as start", command=lambda: self._mark_start_node(self._get_current_item((self._cache["x"], self._cache["y"])))) if self._is_object(item): # Launch the node menu as well as an an added option for selecting stuff to edit an object p_menu.add_command(label="Edit Object", command=lambda: self._edit_object(coords)) p_menu.add_command(label="Delete Object", command=lambda: self._delete_object(self._get_current_item((self._cache["x"], self._cache["y"])))) p_menu.delete(0) p_menu.tk_popup(updated_coords[0], updated_coords[1]) return if self._is_edge(item): p_menu.add_command(label="Edit Corridor", command=lambda: self._selection_operation((self._cache["x"], self._cache["y"]))) p_menu.add_command(label="Delete Corridor", command=lambda: self.delete_edge(self._get_current_item((self._cache["x"], self._cache["y"])))) p_menu.tk_popup(updated_coords[0], updated_coords[1]) return self._clear_cache(coords) def _edit_object(self, coords): """ Awkward moment when you find a threading related bug in the Tkinter library, caused by some Tcl issue or something like that. The below line must be left commented out otherwise the window_wait call in the dialog will crash out with a Tcl ponter based issue :/ item = self._get_current_item((self._cache["x"], self._cache["y"])) This means that we can only use the mouse to edit objects """ item = self._get_current_item(coords) if item not in self._object_listing: Debug.printi("Not a valid object to edit", Debug.Level.ERROR) return obj = ObjectDialog(self, coords[0] + 10, coords[1] + 10, populator=self._manager.request(DataStore.DATATYPE.OBJECT, item)) Debug.printi("Editing object " + str(item), Debug.Level.INFO) self._manager.inform(DataStore.EVENT.OBJECT_EDIT, obj._entries, item) Debug.printi("Editing object " + str(item), Debug.Level.INFO) def _delete_object(self, item): if item not in self._object_listing: Debug.printi("Object does not exist to delete", Debug.Level.ERROR) return del self._object_listing[item] self._manager.inform(DataStore.EVENT.OBJECT_DELETE, data_id=item) self._canvas.itemconfig(item, outline="red", fill="black", activeoutline="black", activefill="red") def _mark_object(self, coords, prog=False, data=None): """ Mark a node as containing an object :param coords: :return: """ # Retrieve the item item = self._get_current_item(coords) if not prog: if item not in self._node_listing: Debug.printi("Invalid object placement selection", Debug.Level.ERROR) return if item in self._object_listing: Debug.printi("This room already has an object in it", Debug.Level.ERROR) return # Retrieve its coordinates # Launch the object maker dialog obj = ObjectDialog(self, coords[0] + 10, coords[1] + 10, populator=Containers.ObjectContainer(key_val={ "x_coordinate" : coords[0], "y_coordinate" : coords[1], "name" : "Object_"+str(item), "mesh" : None, "scale" : None })) entries = obj._entries else: entries = { "x_coordinate": coords[0], "y_coordinate": coords[1], "name": data["name"], "mesh": data["mesh"], "scale": data["scale"] } # Save informatoin to the manager self._manager.inform(DataStore.EVENT.OBJECT_CREATE, entries, item) self._object_listing[item] = item self._canvas.itemconfig(item, fill="blue") Debug.printi("Object created in room " + str(item), Debug.Level.INFO) def _valid_edge_cache(self): """ Return true if the edge cache contains a valid edge descriptor A valid edge descriptor is when the edge has a valid starting node, if the edge does not contain a valid starting node, this means that the edge was not created in the proper manner and should thus be ignored by any edge operations """ valid = not self._edge_cache["item_start"] == (None,) return valid def _canvas_to_screen(self, coords): """ Convert canvas coordinates into screen coordinates :param coords: The current canvas coordinates :return: """ """ # upper left corner of the visible region x0 = self._canvas.winfo_rootx() y0 = self._canvas.winfo_rooty() # given a canvas coordinate cx/cy, convert it to window coordinates: wx0 = x0 + coords[0] wy0 = y0 + coords[1] # upper left corner of the visible region x0 = self._canvas.canvasx(0) y0 = self._canvas.canvasy(0) # given a canvas coordinate cx/cy, convert it to window coordinates: wx0 = coords[0] - x0 wy0 = coords[1] - y0 #""" return (self._cache["event"].x_root, self._cache["event"].y_root) def _begin_edge(self, coords): """ Begin recording information regarding the placement of an edge :param coords: The coordinates associated with this event """ # Record the starting node self._edge_cache["item_start"] = self._get_current_item((self._cache["x"], self._cache["y"])) # Abort the operation if the item was not a valid node to be selecting if self._edge_cache["item_start"] is None or self._edge_cache["item_start"] not in self._node_listing: self._clear_edge_cache() return self._edge_cache["x_start"] = self._cache["x"] self._edge_cache["y_start"] = self._cache["y"] def _end_edge(self, coords, prog=False, data=None): """ Perform the operations required to complete an edge creation operation :param coords: :return: """ # Check if the cursor is over a node, if so continue, else abort curr = self._get_current_item((coords[0], coords[1])) if not prog: if curr is None or not self._valid_edge_cache() or curr not in self._node_listing: # Abort the edge creation process self._canvas.delete(self._edge_cache["edge"]) self._clear_edge_cache() return # Check if this edge already exists in the program if self._check_duplicate_edges(self._edge_cache["item_start"], curr): self.delete_edge(self._edge_cache["edge"]) Debug.printi("Multiple edges between rooms not permitted", Debug.Level.ERROR) return #Ensure that edges arent made between the same room if curr == self._edge_cache["item_start"]: Debug.printi("Cannot allow paths starting and ending in the same room", Debug.Level.ERROR) return self._canvas.tag_lower("edge") self._edge_cache["item_end"] = curr # Note that we use the edge ID as the key self._edge_bindings[self._edge_cache["edge"]] = EdgeBind(self._edge_cache) self._edge_bindings[self._edge_cache["edge"]].x_end = coords[0] self._edge_bindings[self._edge_cache["edge"]].y_end = coords[1] # Inform the manager if not prog: self._manager.inform( DataStore.EVENT.EDGE_CREATE, { "source" : self._edge_cache["item_start"], "target" : self._edge_cache["item_end"], "height" : None, "wall1" : { "height":Defaults.Edge.WALL_HEIGHT, "textures":{ Defaults.Wall.PATH: { "path":Defaults.Wall.PATH, "tile_x":Defaults.Wall.TILE_X, "tile_y":Defaults.Wall.TILE_Y, "height":None } } } if Defaults.Config.EASY_MAZE else None, "wall2" : { "height": Defaults.Edge.WALL_HEIGHT, "textures": { Defaults.Wall.PATH: { "path": Defaults.Wall.PATH, "tile_x": Defaults.Wall.TILE_X, "tile_y": Defaults.Wall.TILE_Y, "height":None } } } } if Defaults.Config.EASY_MAZE else None, self._edge_cache["edge"]) else: # We are programmatically adding the edges in self._manager.inform( DataStore.EVENT.EDGE_CREATE, { "source": self._edge_cache["item_start"], "target": self._edge_cache["item_end"], "height": None, "wall1": data["wall1"], "wall2": data["wall2"] }, self._edge_cache["edge"]) Debug.printi("Edge created between rooms " + str(self._edge_cache["item_start"]) + " and " + str(self._edge_cache["item_end"]) , Debug.Level.INFO) self._clear_edge_cache() self._clear_cache(coords) def _check_duplicate_edges(self, start_node, end_node): for binding in self._edge_bindings.itervalues(): if ( start_node == binding.item_start and end_node == binding.item_end )\ or ( start_node == binding.item_end and end_node == binding.item_start): return True return False def _execute_edge(self, coords): """ Perform the operations that occur during the motion of an edge drag :param coords: :return: """ # Update the line position # We will update the line position by deleting and redrawing if not self._valid_edge_cache(): return self._canvas.delete(self._edge_cache["edge"]) self._edge_cache["edge"] = self._canvas.create_line( \ self._edge_cache["x_start"], self._edge_cache["y_start"], coords[0]-1, coords[1]-1, tags="edge", activefill="RoyalBlue1", tag="edge") d_x = self._edge_cache["x_start"] - coords[0] d_y = self._edge_cache["y_start"] - coords[1] square = (d_x * d_x) + (d_y * d_y) distance = math.sqrt(square) Debug.printi("Current corridor distance: " + str(int(distance))) def _update_cache(self, item, coords): """ Update the local cache with the item id and coordinates of the mouse :param item: The item with which to update the cache :param coords: The current event coordinates """ self._cache["item"] = item self._cache["x"] = coords[0] self._cache["y"] = coords[1] def _clear_cache(self, coords): """ Clear the cache Set the cache values to the current mouse position and None the item :param coords: The coordinates of the mouse at that event time """ self._cache["item"] = None self._cache["x"] = coords[0] self._cache["y"] = coords[1] def _clear_edge_cache(self): """ Clear the edge cache to None for all values :return: """ self._edge_cache["x_start"] = None, self._edge_cache["y_start"] = None, self._edge_cache["x_end"] = None, self._edge_cache["y_end"] = None, self._edge_cache["item_start"] = None, self._edge_cache["item_end"] = None, self._edge_cache["edge"] = None def _get_current_item(self, coords): """ Return the item(if any) that the mouse is currently over :param coords: The current coordinates of the mouse :return: """ item = self._canvas.find_overlapping(coords[0]-1, coords[1]-1, coords[0]+1, coords[1]+1) if item is (): return None # Hacky solution # Return the first node that we come across, since they seem to be returned by tkinter # in reverse order to their visual positioning, we'll go through the list backwards for val in item[::-1]: if val in self._node_listing: return val # Else, just return the first item and be done with it return item[0] def _is_node(self, obj): """ Returns true if the supplied object is a node :param obj: The object id to id :return: """ return obj in self._node_listing def _is_edge(self, obj): """ Returns true if the supplied object is an edge :param obj: The object id to id :return: """ return obj in self._edge_bindings def _is_object(self, obj): """ Returns true if the supplied object is an object :param obj: The object id to id :return: """ return obj in self._object_listing def _get_obj_type(self, obj): """ Returns the Object type of the supplied object :param obj: The object to identify :return: """ if self._is_node(obj): return EditableObject.NODE if self._is_edge(obj): return EditableObject.EDGE if self._is_object(obj): return EditableObject.OBJECT return None def _selection_operation(self, coords): """ Contextually create or edit a node :param coords: :return: """ # Determine the item ID item = self._get_current_item(coords) self._cache["item"] = item true_coords = self._canvas_to_screen((self._cache["x"], self._cache["y"])) if self._is_node(item): Debug.printi("Node Selected : " + str(item) + " | Launching Editor", Debug.Level.INFO) # Make request from object manager using the tag assigned populator = self._manager.request(DataStore.DATATYPE.NODE, item) updated_node = NodeDialog(self, true_coords[0] + 10, true_coords[1] + 10, populator=populator) # post information to object manager, or let the dialog handle it, or whatever self._manager.inform(DataStore.EVENT.NODE_EDIT, updated_node._entries, item) return if self._is_edge(item): Debug.printi("Edge Selected : " + str(item) + " | Launching Editor", Debug.Level.INFO) # Make a request from the object manager to populate the dialog populator = self._manager.request(DataStore.DATATYPE.EDGE, item) updated_edge = EdgeDialog(self, true_coords[0] + 10, true_coords[1] + 10, populator=populator) # Make sure that information is posted to the object manager self._manager.inform(DataStore.EVENT.EDGE_EDIT, updated_edge._entries, item) return if self._is_object(item): self._edit_object(coords) return def create_new_node(self, coords, prog = False, data=None): """ Creates a new node on the Canvas and adds it to the datastore :param coords: :return: """ # Create the node on Canvas self._cache["item"] = self._canvas.create_rectangle(coords[0], coords[1], coords[0]+25, coords[1]+25, outline="red", fill="black", activeoutline="black", activefill="red", tag="node") self._node_listing[self._cache["item"]] = self._cache["item"] if not prog: if not Defaults.Config.EASY_MAZE: true_coords = self._canvas_to_screen((self._cache["x"], self._cache["y"])) new_node = NodeDialog(self, true_coords[0] + 25, true_coords[1] + 25, populator=Containers.NodeContainer( { "node_id": self._cache["item"], "x_coordinate": self._cache["x"], "y_coordinate": self._cache["y"], "room_texture": None, "wall_pictures": None })) entries = new_node._entries else: entries = { "node_id": self._cache["item"], "x_coordinate": self._cache["x"], "y_coordinate": self._cache["y"], "room_texture": Defaults.Node.ROOM_TEXTURE, "wall_pictures": None } else: pics = data[1] data = data[0] entries = { "node_id": data["id"], "x_coordinate": data["x"], "y_coordinate": data["y"], "room_texture": data["texture"], "wall_pictures": pics } # Inform the datastore self._manager.inform(DataStore.EVENT.NODE_CREATE, entries, self._cache["item"]) self._clear_cache(coords) def delete_all(self): """ Delete all nodes and associated edges and objects from the canvas """ # Iterate over each node in the node listing and delete it using delete node for key in self._node_listing.keys(): self.delete_node(key) # Delete any rouge edge bindings that may exist for binding in self._edge_bindings: self.delete_edge(binding) self._object_listing.clear() # Delete any naughty objects that are left self._canvas.delete("all") self._manager.inform(DataStore.EVENT.DELETE_ALL) def delete_node(self, node_id): """ Delete a node and all its associated edges and object from the canvas :param node_id: The tkinter id of the node to be deleted """ # Delete from our internal representations if node_id not in self._node_listing: return del self._node_listing[node_id] # Delete from the canvas self._canvas.delete(node_id) # Iterate through the edge bindings and delete all of those for key in self._edge_bindings.keys(): if self._edge_bindings[key].item_start == node_id or self._edge_bindings[key].item_end == node_id: self.delete_edge(key) # Inform the object manager that a node as been deleted if node_id in self._object_listing: self._delete_object(node_id) self._manager.inform(DataStore.EVENT.NODE_DELETE, data_id=node_id) def delete_edge(self, edge_id): """ Delete the specified edge from the MazeCanvas :param edge_id: The edge to be deleted :return: """ # Go through the edge bindings and delete the appropriate edge try: # try to delete the edge binding if it exists del self._edge_bindings[edge_id] except KeyError: # Terrible I know, but I dont have the time to find the root cause pass # Delete the edge from the canvas self._canvas.delete(edge_id) # Inform the object manager that an edge has been deleted self._manager.inform(DataStore.EVENT.EDGE_DELETE, data_id=edge_id) def _mark_start_node(self, node_id): """ Mark the passed in node as the starting node :param node_id: :return: """ # Print the debug information # Mark as the new starting node on the canvas, first check that it is a node if node_id in self._node_listing: Debug.printi("Node:" + str(node_id) + " has been marked as the new starting node", Debug.Level.INFO) if self._curr_start is not None: # Return the old starting node to its normal colour self._canvas.itemconfig(self._curr_start, outline="red", fill="black", activeoutline="black", activefill="red") self._curr_start = node_id self._canvas.itemconfig(node_id, outline="black", fill="green", activeoutline="green", activefill="black") # Inform the object manager that there is a new starting node environment_container = self._manager.request(DataStore.DATATYPE.ENVIRONMENT) environment_container.start_node = node_id self._manager.inform(DataStore.EVENT.ENVIRONMENT_EDIT, environment_container)