From a15e0367dee4d075ea1bfc0f6d3d8c1331346367 Mon Sep 17 00:00:00 2001 From: Alan Aguiar Date: Mon, 30 Dec 2013 04:00:42 +0000 Subject: add color custom board and pieces --- diff --git a/activity.py b/activity.py index 6fe8c90..d507833 100755 --- a/activity.py +++ b/activity.py @@ -3,15 +3,14 @@ from gettext import gettext as _ -import sys import gtk -import pygame from sugar.activity import activity from sugar.graphics.toolbarbox import ToolbarBox from sugar.activity.widgets import ActivityToolbarButton -from sugar.graphics.toolbutton import ToolButton from sugar.activity.widgets import StopButton +from sugar.graphics.colorbutton import ColorToolButton +from sugar.graphics.toolbarbox import ToolbarButton import sugargame.canvas @@ -38,6 +37,8 @@ class ReversiActivity(activity.Activity): toolbar_box.toolbar.insert(activity_button, -1) activity_button.show() + self.build_colors_toolbar(toolbar_box) + separator = gtk.SeparatorToolItem() separator.props.draw = False separator.set_expand(True) @@ -48,3 +49,153 @@ class ReversiActivity(activity.Activity): toolbar_box.toolbar.insert(stop_button, -1) stop_button.show() + self.show_all() + + def build_colors_toolbar(self, toolbox): + + colors_bar = gtk.Toolbar() + + ######################################################################## + # Point color + item = gtk.ToolItem() + label = gtk.Label() + label.set_text('%s ' % _('Player 1')) + item.add(label) + colors_bar.insert(item, -1) + + # select color + item = gtk.ToolItem() + _fill_color = ColorToolButton() + c = gtk.gdk.Color() + c.red = 65535 + c.green = 65535 + c.blue = 65535 + _fill_color.set_color(c) + _fill_color.connect('notify::color', self.color_player1_change) + item.add(_fill_color) + colors_bar.insert(item, -1) + + # Separator + separator = gtk.SeparatorToolItem() + colors_bar.insert(separator, -1) + separator.show() + + ######################################################################## + # Back color + item = gtk.ToolItem() + label = gtk.Label() + label.set_text('%s ' % _('Player 2')) + item.add(label) + colors_bar.insert(item, -1) + + # select color + item = gtk.ToolItem() + _fill_color = ColorToolButton() + c = gtk.gdk.Color() + c.red = 0 + c.green = 0 + c.blue = 0 + _fill_color.set_color(c) + _fill_color.connect('notify::color', self.color_player2_change) + item.add(_fill_color) + colors_bar.insert(item, -1) + + # Separator + separator = gtk.SeparatorToolItem() + colors_bar.insert(separator, -1) + separator.show() + + ######################################################################## + # Line color + item = gtk.ToolItem() + label = gtk.Label() + label.set_text('%s ' % _('Lines')) + item.add(label) + colors_bar.insert(item, -1) + + # select color + item = gtk.ToolItem() + _fill_color = ColorToolButton() + _fill_color.connect('notify::color', self.color_line_change) + item.add(_fill_color) + colors_bar.insert(item, -1) + + # Separator + separator = gtk.SeparatorToolItem() + colors_bar.insert(separator, -1) + separator.show() + + ######################################################################## + # Line color + item = gtk.ToolItem() + label = gtk.Label() + label.set_text('%s ' % _('Background')) + item.add(label) + colors_bar.insert(item, -1) + + # select color + item = gtk.ToolItem() + _fill_color = ColorToolButton() + _fill_color.connect('notify::color', self.color_back_change) + item.add(_fill_color) + colors_bar.insert(item, -1) + + # Separator + separator = gtk.SeparatorToolItem() + colors_bar.insert(separator, -1) + separator.show() + + ######################################################################## + # Line color + item = gtk.ToolItem() + label = gtk.Label() + label.set_text('%s ' % _('Board')) + item.add(label) + colors_bar.insert(item, -1) + + # select color + item = gtk.ToolItem() + _fill_color = ColorToolButton() + _fill_color.connect('notify::color', self.color_board_change) + item.add(_fill_color) + colors_bar.insert(item, -1) + + ######################################################################## + colors_bar.show_all() + colors_button = ToolbarButton(label=_('Colors'), + page=colors_bar, + icon_name='toolbar-colors') + toolbox.toolbar.insert(colors_button, -1) + colors_button.show() + + def color_player1_change(self, widget, pspec): + color = widget.get_color() + new_color = self.color_to_rgb(color) + self.game.set_player1_color(new_color) + + def color_player2_change(self, widget, pspec): + color = widget.get_color() + new_color = self.color_to_rgb(color) + self.game.set_player2_color(new_color) + + def color_line_change(self, widget, pspec): + color = widget.get_color() + new_color = self.color_to_rgb(color) + self.game.set_line_color(new_color) + + def color_back_change(self, widget, pspec): + color = widget.get_color() + new_color = self.color_to_rgb(color) + self.game.set_back_color(new_color) + + def color_board_change(self, widget, pspec): + color = widget.get_color() + new_color = self.color_to_rgb(color) + self.game.set_board_color(new_color) + + def color_to_rgb(self, color): + r = color.red *255 / 65535 + g = color.green *255 / 65535 + b = color.blue *255 / 65535 + return (r, g, b) + diff --git a/reversi.py b/reversi.py index ab2c1a8..4c42d78 100755 --- a/reversi.py +++ b/reversi.py @@ -31,6 +31,7 @@ from gettext import gettext as _ # Immutable Globals / Settings screen_size = (1200, 825) background_color = (255, 255, 255) +background_board_color = (255, 255, 255) cell_padding_color = (0, 0, 0) available_cell_color = (0, 0, 0) player_view_outline_color = (0, 0, 0) @@ -41,6 +42,9 @@ num_columns = 8 num_rows = 8 cell_padding = 4 +BLACK = (0, 0, 0) +WHITE = (255, 255, 255) + player_numbers_to_piece_names = [None, "White", "Black"] @@ -71,25 +75,28 @@ class CellView(pygame.sprite.Sprite): def show_piece(self, color): """Shows a piece in the cell. Set color to "Black" or "White".""" - global background_color + global background_board_color - self.image.fill(background_color) + self.image.fill(background_board_color) piece_width = self.rect.width * 0.8 border_size = self.rect.width * 0.05 pos = (self.rect.centerx - self.rect.left, self.rect.centery - self.rect.top) #Colors of black and white circles if color == "Black": - pygame.draw.circle(self.image, (0, 0, 0), pos, int(piece_width / 2)) + pygame.draw.circle(self.image, BLACK, pos, int(piece_width / 2)) elif color == "White": - pygame.draw.circle(self.image, (0, 0, 0), pos, int(piece_width / 2), int(border_size)) + if WHITE == (255, 255, 255): + pygame.draw.circle(self.image, (0, 0, 0), pos, int(piece_width / 2), int(border_size)) + else: + pygame.draw.circle(self.image, WHITE, pos, int(piece_width / 2)) def show_no_piece(self): """Clears the cell, showing no piece at all.""" - global background_color + global background_board_color self.image = pygame.Surface(self.rect.size) - self.image.fill(background_color) + self.image.fill(background_board_color) def show_as_available(self, piece_color_name = "Black"): #self.draw_corners() @@ -149,19 +156,24 @@ class BoardView: self.controller = controller self.top_left = top_left - + self.grid_size = grid_size self.num_columns = grid_size[0] self.num_rows = grid_size[1] - + self.size_in_pixels = size_in_pixels + # Init cells cell_width = (size_in_pixels[0] - (cell_padding * (grid_size[0] + 1))) / grid_size[0] cell_height = (size_in_pixels[1] - (cell_padding * (grid_size[1] + 1))) / grid_size[1] - cell_size = (cell_width, cell_height) - self.init_cell_views(cell_size, grid_size) + self.cell_size = (cell_width, cell_height) + + self.background = pygame.Surface(self.size_in_pixels) + self.init_cell_views(self.cell_size, self.grid_size) + + self.redraw_background() + def redraw_background(self): # Draw board background - self.background = pygame.Surface(size_in_pixels) - self.draw_board_background(self.background, cell_size, grid_size) + self.draw_board_background(self.cell_size, self.grid_size) def init_cell_views(self, cell_size, grid_size): # Create a group to store the cells in. @@ -187,20 +199,22 @@ class BoardView: self.cell_view_grid.append(grid_column) - def draw_board_background(self, surface, cell_size, grid_size): - surface.fill(background_color) + def draw_board_background(self, cell_size, grid_size): + # tablero + background_color = (100, 100, 100) + self.background.fill(background_color) drawn_height = cell_padding + (grid_size[1] * (cell_padding + cell_size[1])) tmp_rect = pygame.Rect(0, 0, cell_padding, drawn_height) for column_index in range(grid_size[0] + 1): tmp_rect.left = column_index * (cell_padding + cell_size[0]) - surface.fill(cell_padding_color, tmp_rect) + self.background.fill(cell_padding_color, tmp_rect) drawn_width = cell_padding + (grid_size[0] * (cell_padding + cell_size[0])) tmp_rect = pygame.Rect(0, 0, drawn_width, cell_padding) for row_index in range(grid_size[0] + 1): tmp_rect.top = row_index * (cell_padding + cell_size[1]) - surface.fill(cell_padding_color, tmp_rect) + self.background.fill(cell_padding_color, tmp_rect) def get_num_columns(self): return self.num_columns @@ -330,9 +344,12 @@ class PlayerView: image.fill(background_color) if piece_name == "Black": - pygame.draw.circle(image, (0, 0, 0), (width/2, width/2), width/2) + pygame.draw.circle(image, BLACK, (width/2, width/2), width/2) elif piece_name == "White": - pygame.draw.circle(image, (0, 0, 0), (width/2, width/2), width/2, 2) + if WHITE == (255, 255, 255): + pygame.draw.circle(image, (0, 0, 0), (width/2, width/2), width/2, 2) + else: + pygame.draw.circle(image, WHITE, (width/2, width/2), width/2) return image @@ -414,6 +431,9 @@ class ReversiView: player_view = self.player_views[player_number] player_view.update_from_model(model) + def redraw_back(self): + self.board_view.redraw_background() + def draw(self, surface): surface.fill(background_color) @@ -724,6 +744,32 @@ class ReversiController: #piece_color_name = self.model. #self.model.get_board_model().get_all_toggleable_cells() pass + + def set_player1_color(self, color): + global WHITE + WHITE = color + self.view.update_from_model(self.model) + + def set_player2_color(self, color): + global BLACK + BLACK = color + self.view.update_from_model(self.model) + + def set_line_color(self, color): + global cell_padding_color + cell_padding_color = color + self.view.redraw_back() + + def set_back_color(self, color): + global background_color + background_color = color + self.view.redraw_back() + self.view.update_from_model(self.model) + + def set_board_color(self, color): + global background_board_color + background_board_color = color + self.view.update_from_model(self.model) def run(self): global screen_size -- cgit v0.9.1