def insert(self, pos, type_):
     """Inserts an entity on the grid
     """
     # DEBUG
     #debug("insert", ("pos", pos), ("type", type_))
     entity_t = None
     if self._grid[pos] != type_:
         entity_t = ALL_ENTITIES[type_].type
     else:
         return  # Entity already exist
     self._grid[pos] = entity_t
     if ALL_ENTITIES[entity_t].neighborhood is not None:
         if ALL_ENTITIES[entity_t].neighborhood == NEIGHBORHOOD_TYPES[
                 "moore"]:
             x, y = pos
             for new_x in range(x - 1, x + 2):
                 for new_y in range(y - 1, y + 2):
                     pos = Point(new_x, new_y)
                     if self._grid[pos] != entity_t:
                         self._grid[pos] = ALL_ENTITIES[entity_t].neighbors
         elif ALL_ENTITIES[entity_t].neighborhood == NEIGHBORHOOD_TYPES[
                 "1D"]:
             x, y = pos
             for new_x in range(-1, 2, 2):
                 pos = Point(x + new_x, y)
                 if self._grid[pos] != entity_t:
                     self._grid[pos] = ALL_ENTITIES[entity_t].neighbors
 def move_all(self, x, y):
     """Moves the entire grid and simulates view motion
     """
     new_dict = dict()
     for pos in self._my_links.viewkeys():
         new_pos = Point(pos.x - x, pos.y - y)
         new_dict[new_pos] = self._my_links[pos]
     self._my_links.clear()
     self._my_links = new_dict
     new_dict = dict()
     for pos in self._links.viewkeys():
         new_pos = Point(pos.x - x, pos.y - y)
         new_dict[new_pos] = self._links[pos]
     self._links.clear()
     self._links = new_dict
     new_dict = BaseGrid(int)
     for pos, entity_t in self._grid.viewitems():
         new_pos = Point(pos.x - x, pos.y - y)
         new_dict[new_pos] = entity_t
     self._grid = new_dict
     new_dict = BaseGrid(int)
     for pos, entity_t in self._grid_sel.viewitems():
         new_pos = Point(pos.x - x, pos.y - y)
         new_dict[new_pos] = entity_t
     self._grid_sel = new_dict
     self.__update_selection()
 def flip_h(self):
     """Flips horizontally the entities selected
     and update the selection list
     """
     # DEBUG
     #debug("flip_h", ("selection", self.__selection_list))
     max_x = max(self.__all_selection)[0]
     min_x = min(self.__all_selection)[0]
     mid_x = (max_x - min_x) / 2
     new_dict = BaseGrid(int)
     for point in self.__selection_list:
         my_x, my_y = point
         if my_x > mid_x:
             new_x = min_x + (max_x - my_x)
         else:
             new_x = max_x - (my_x - min_x)
         new_pos = Point(new_x, my_y)
         new_dict[new_pos] = ALL_ENTITIES[self._grid[point]].flip_h()
         self.delete(point)
     for pos, entity in self._grid.viewitems():
         if pos not in new_dict and pos not in self.__selection_list:
             new_dict[pos] = entity
     self._grid.clear()
     self._grid.update(new_dict)
     self.__update_selection()
 def rotate(self, deg=90):
     """Rotates the entities selected and
     update the selction list
     """
     if len(self.__selection_list) == 0:
         return
     max_ = max(self.__all_selection)
     min_ = min(self.__all_selection)
     pivot = Point(
         min_.x + ((max_.x - min_.x) / 2.),
         min_.y + ((max_.y - min_.y) / 2.),
     )
     new_dict = BaseGrid(int)
     new_selection = list()
     new_all = list()
     for point in self.__selection_list:
         new_pos = rotate_point(point, pivot, deg)
         new_selection.append(new_pos)
         new_dict[new_pos] = ALL_ENTITIES[self._grid[point]].rotate(deg)
         self.delete(point)
     self.__selection_list = new_selection
     self.__all_selection = list()
     for point in self.__all_selection:
         new_all.append(rotate_point(point, pivot, deg))
     self.__all_selection = new_all
     for pos, entity in self._grid.viewitems():
         if pos not in new_dict:
             new_dict[pos] = entity
     self._grid.clear()
     self._grid.update(new_dict)
     self.__update_selection()
    def load(self, filename="stored.cg"):
        """Loads a grid from a file
        """
        base_path = path.dirname(filename)
        #base_path = "/".join(filename.split("/")[:-1])
        # DEBUG
        #debug("load", ("base_path", base_path), ("filename", filename))
        self.__filename = filename

        with open(filename, "r") as fp:
            stored_dict = json.load(fp)

        for type_, list_ in stored_dict.viewitems():
            if type_ == "my_links":
                for sub_type, sub_list in list_.viewitems():
                    for pos in sub_list:
                        pos = Point(pos[0], pos[1])
                        if sub_type == LINK_TYPE_IDS[LINK_TYPE_NAMES["IN"]]:
                            self.insert_link(pos, LINK_TYPE_NAMES["IN"], True)
                        elif sub_type == LINK_TYPE_IDS[LINK_TYPE_NAMES["OUT"]]:
                            self.insert_link(pos, LINK_TYPE_NAMES["OUT"], True)
            # "links": {"10,6": [[-1, 36574928, [12, 5]]]}
            elif type_ == "links":
                added = dict()
                grids_container = dict()
                for point, data in list_.viewitems():
                    pos = map(int, point.split(","))
                    pos = Point(pos[0], pos[1])
                    type_, id_, id_pos = data
                    id_pos = Point(id_pos[0], id_pos[1])
                    if id_ not in added:
                        new_grid = CellularGrid()
                        new_grid.load(
                            path.join(base_path,
                                      stored_dict["linked_names"][str(id_)]))
                        added[id_] = id(new_grid)
                        grids_container[id_] = new_grid
                    self._linked_names[
                        added[id_]] = stored_dict["linked_names"][str(id_)]
                    self._linked_grids[added[id_]] = grids_container[id_]
                    self._links[pos] = (type_, added[id_], id_pos)
            elif type_ in ENTITIES_NAMES:
                for pos in list_:
                    pos = Point(pos[0], pos[1])
                    self.insert(pos, ENTITIES_NAMES[type_])
 def move_selected_entities(self, x, y):
     """Moves all selected entities
     """
     new_selection = list()
     new_all = list()
     new_dict = BaseGrid(int)
     for pos in self.__selection_list:
         new_pos = Point(pos.x - x, pos.y - y)
         # DEBUG
         #debug("move_selected_entities", ("NEW POS", new_pos != pos))
         if new_pos != pos:
             new_selection.append(new_pos)
             new_dict[new_pos] = self._grid_sel.pop(pos)
         else:
             return
     self._grid_sel = new_dict
     self.__all_selection = list()
     for point in self.__all_selection:
         new_all.append(Point(point.x - x, point.y - y))
     self.__all_selection = new_all
     # DEBUG
     #debug("move_selected_entities", ("NEW SELECTION", new_selection))
     if len(new_selection) != 0:
         self.__selection_list = new_selection
 def flip_v(self):
     """Flips vertically the entities selected
     and update the selection list
     """
     max_y = max(self.__all_selection)[1]
     min_y = min(self.__all_selection)[1]
     mid_y = (max_y - min_y) / 2
     new_dict = BaseGrid(int)
     for point in self.__selection_list:
         my_x, my_y = point
         if my_y > mid_y:
             new_y = min_y + (max_y - my_y)
         else:
             new_y = max_y - (my_y - min_y)
         new_pos = Point(my_x, new_y)
         new_dict[new_pos] = ALL_ENTITIES[self._grid[point]].flip_v()
         self.delete(point)
     for pos, entity in self._grid.viewitems():
         if pos not in new_dict:
             new_dict[pos] = entity
     self._grid.clear()
     self._grid.update(new_dict)
     self.__update_selection()