def on_mouse_press_left(self, event): """ Evento tasto sinistro del mouse premuto :param event: """ if DEBUG: print("on_mouse_press_left") # se non è stato aperto un livello non fa niente event.Skip() if self.__current_mode == MODE_NONE: return # se sono sulla griglia if Griglia.is_on_grid(self.mouse_pos): # click sulla griglia p = Griglia.get_index_from_mouse(self.mouse_pos) # se sono in modalità EDIT, inserisce la modalità PAINT # veifica che la postazione sia libera e che sia stato selezionato un colore # crea un nuovo brick if self.__current_mode == MODE_EDIT: self.__current_mode = MODE_PAINTING self.statusbar_set_text("Modalità PAINTING") if DEBUG: print("Modalità PAINTING") tipo, colore = self.parent.get_brick_select() if self.__level.is_position_free( p) and colore is not None: # paint self.new_brick() # se sono nella modalità MOVE, inserisce la modalità DRAGGING # salva le informazioni self.__drag_drop elif self.__current_mode == MODE_MOVE and not self.__level.is_position_free( p): # drag and drop self.__current_mode = MODE_DRAGGING self.statusbar_set_text("Modalità DRAGGING") if DEBUG: print("Modalità DRAGGING") brk = self.__level.take_brick(p) x, y = brk.get_position_on_screen() self.__drag_drop = dict(brk=brk, idx_old=p, offset_x=abs(self.mouse_pos[0] - x), offset_y=abs(self.mouse_pos[1] - y)) # se sono in modalità DELETE, inserisce la modalita ERESING # cancella il brick elif self.__current_mode == MODE_DELETE: # cancella self.__current_mode = MODE_ERASING self.statusbar_set_text("Modalità ERESING") if DEBUG: print("Modalità ERESING") self.delete_brick() # se sono in modalità SELECT, seleziona o deseleziona il brick elif self.__current_mode == MODE_SELECT: pos = Griglia.get_xy_from_mouse(self.mouse_pos) if not self.__select_brick[ 'idx'] == -1 and self.is_equal_position( pos, self.__select_brick['xy']): self.select_none() else: self.select_brick(p)
def on_mouse_release_left(self, event): """ Evento tasto sinistro del mouse rilasciato :param event: """ if DEBUG: print("on_mouse_release_left") event.Skip() # se non è stato aperto un livello non fa niente if self.__current_mode == MODE_NONE: return # se sono in modalità DRAGGING, torno alla modalità MOVE # se sono sulla griglia e la posizione è libera, aggiorno le proprietà del brick, # altrimenti setta idx al vecchio indice dell'array # posiziona il brick all'indice corretto, cancella la struttura self.__drag_drop if self.__current_mode == MODE_DRAGGING: self.__current_mode = MODE_MOVE self.statusbar_set_text("Modalità MOVE ripristinata ") if DEBUG: print("Modalità MOVE ripristinata ") idx = Griglia.get_index_from_mouse(self.mouse_pos) if Griglia.is_on_grid( self.mouse_pos) and self.__level.is_position_free(idx): xy = Griglia.get_xy_from_mouse(self.mouse_pos) rc = Griglia.get_row_column_from_mouse(self.mouse_pos) # aggiorna la coda undo con l'operazione eseguita log = (ID_MOVE, idx, self.__drag_drop['idx_old'], self.__drag_drop['brk'], xy, self.__drag_drop['brk'].get_position_on_screen(), rc, self.__drag_drop['brk'].get_position_on_grid()) self.__queue_log.put_undo(log) self.__queue_log.clear_redo() if DEBUG: print("log: " + str(log)) self.__drag_drop['brk'].change_pos(xy, rc) self.__level.set_context_modified() self.statusbar_set_text("Il brick è stato spostato") else: idx = self.__drag_drop['idx_old'] self.__level.put_brick(self.__drag_drop['brk'], idx) self.__drag_drop = None # se sono in modalità ERASING, torno in modalità DELETE elif self.__current_mode == MODE_ERASING: self.__current_mode = MODE_DELETE if DEBUG: print("Modalità DELETE ripristinata ") self.statusbar_set_text("Modalità DELETE ripristinata") # se sono in modalità PAINT, torno in modalità EDIT elif self.__current_mode == MODE_PAINTING: self.__current_mode = MODE_EDIT if DEBUG: print("Modalità EDIT ripristinata ") self.statusbar_set_text("Modalità EDIT ripristinata")
def select_brick(self, p): """ inserisce in self.__select_brick le informazione della posizione p :param p: indice arrey brick selezionato :return: """ if DEBUG: print("select_brick") brk = self.__level.get_brick(p) pos = Griglia.get_xy_from_mouse(self.mouse_pos) r_c = Griglia.get_row_column_from_mouse(self.mouse_pos) self.__select_brick = dict(idx=p, brk=brk, xy=pos, rc=r_c) self.statusbar_set_text("Posizione " + str(p) + " selezionata") if DEBUG: print(self.__select_brick)
def on_mouse_press_right(self, event): """ Evento tasto destro del mouse premuto :param event: :return: """ if DEBUG: print("on_mouse_press_right") # se non è stato aperto un livello non fa niente if self.__current_mode == MODE_NONE: return # se sono sulla griglia seleziono la casella e apro il menu a tendina if Griglia.is_on_grid(self.mouse_pos): p = Griglia.get_index_from_mouse(self.mouse_pos) self.select_brick(p) self.PopupMenu(MyPopupMenu(self), event.GetPosition())
def do_drawing(self, dc, printing=False): if self.__current_mode == MODE_NONE: return # non è stato aperto un nuovo livello # background dc.DrawBitmap(Immagini.background, SCREEN_LEVEL_X, SCREEN_LEVEL_Y, True) dc.DrawBitmap(Immagini.edge_top, EDGE_TOP, True) dc.DrawBitmap(Immagini.edge_vertical, EDGE_LEFT, True) dc.DrawBitmap(Immagini.edge_vertical, EDGE_RIGHT, True) # disegna il contorno del brick eccetto che in modalità select if Griglia.is_on_grid( self.mouse_pos) and not self.__current_mode == MODE_SELECT: dc.SetPen(wx.Pen('WHITE', 2)) x, y = Griglia.get_xy_from_mouse(self.mouse_pos) dc.DrawLine(x, y, x + BRICK_WIDTH, y) dc.DrawLine(x, y, x, y + BRICK_HEIGHT) dc.DrawLine(x, y + BRICK_HEIGHT, x + BRICK_WIDTH, y + BRICK_HEIGHT) dc.DrawLine(x + BRICK_WIDTH, y, x + BRICK_WIDTH, y + BRICK_HEIGHT) # in modalità seleziona disegna brk selezionato if self.__current_mode == MODE_SELECT and not self.__select_brick[ 'idx'] == -1: dc.SetPen(wx.Pen('WHITE', 2)) x, y = self.__select_brick['xy'] dc.DrawLine(x, y, x + BRICK_WIDTH, y) dc.DrawLine(x, y, x, y + BRICK_HEIGHT) dc.DrawLine(x, y + BRICK_HEIGHT, x + BRICK_WIDTH, y + BRICK_HEIGHT) dc.DrawLine(x + BRICK_WIDTH, y, x + BRICK_WIDTH, y + BRICK_HEIGHT) if self.__level is not None: self.__level.draw_briks(dc) # drag and drop if self.__current_mode == MODE_DRAGGING: x, y = (self.mouse_pos[0] - self.__drag_drop['offset_x']), ( self.mouse_pos[1] - self.__drag_drop['offset_y']) self.__drag_drop['brk'].draw_in_pos(dc, x, y)
def new_brick(self, row, column, tipo, colore): """ Crea un nuovo brick e lo posiziona nell'apposita struttura bricks :return: """ if not tipo == ID_IMMORTAL and colore == ID_NONE: LogMessage("Nessun colore selezionato") return -1, None p = Griglia.get_index_from_row_column(row, column) if self.__bricks[p] is None: self.__bricks[p] = Brick(row, column, tipo, colore) return p, self.__bricks[p] return -1, None
def __init__(self, row, column, tipo, colore): """ Nuovo brick :param row: riga :param column: colonna :param tipo: tipo di brick :param colore: colore del brick """ self.__type = tipo self.__color = colore self.__row = row self.__column = column # init position on screen self.__x, self.__y = Griglia.get_xy_from_row_column(row, column) self.__img = self.__init_img()
def serialize(self): """ :return: lista informazioni brikcs """ if DEBUG: print("serialize") list_bricks = [] for column in range(0, MAX_CLN): for row in range(0, MAX_ROW): p = Griglia.get_index_from_row_column(row, column) if self.__bricks[p] is None: list_bricks.append(None) else: list_bricks.append(self.__bricks[p].serialize()) return list_bricks
def on_mouse_motion(self, event): """ Evento muovo il mouse :param event: """ event.Skip() # se non è stato aperto un livello non fa niente if self.__current_mode == MODE_NONE: event.Skip() return self.set_xy(event) # aggiorna la posizione del mouse # se sono sulla griglia # modalità disegno, inserisce un nuovo brick # modalità cancellazione, cancella il brick if Griglia.is_on_grid(self.mouse_pos): if self.__current_mode == MODE_PAINTING: self.new_brick() if self.__current_mode == MODE_ERASING: self.delete_brick()
def delete_brick(self): """ cancella il brick sotto il puntatore del mouse :return """ if DEBUG: print("delete_brick") # se non è stato creato un livello non fa niente if self.__current_mode == MODE_NONE: return p = Griglia.get_index_from_mouse(self.mouse_pos) brk = self.__level.delete_brick(p) if brk is not None: brk = brk.copy_brk() self.__level.set_context_modified() # aggiorna la coda undo con l'operazione eseguita log = (ID_DELETE, p, brk) self.__queue_log.put_undo(log) self.statusbar_set_text("Brick cancellato") if DEBUG: print("log: " + str(log))
def new_brick(self): """ inseriesce un brick con le caratteristiche selezionate sotto il puntatore del mouse """ if DEBUG: print("new_brick") # se non è stato creato un livello non fa niente if self.__current_mode == MODE_NONE: return row, cln = Griglia.get_row_column_from_mouse(self.mouse_pos) tipo, colore = self.parent.get_brick_select() p, brk = self.__level.new_brick(row, cln, tipo, colore) if not p == -1: self.__level.set_context_modified() # aggiorna la coda undo con l'operazione eseguita log = (ID_NEW, p, brk) self.__queue_log.put_undo((ID_NEW, p, brk)) self.__queue_log.clear_redo() self.statusbar_set_text("Nuovo brick inserito") if DEBUG: print("log: " + str(log))