From: <umg...@us...> - 2006-10-08 15:02:05
|
Revision: 347 http://svn.sourceforge.net/pybridge/?rev=347&view=rev Author: umgangee Date: 2006-10-08 08:01:55 -0700 (Sun, 08 Oct 2006) Log Message: ----------- Enable border around player labels, draw individual cards in trick with canvas code, indicate turn by setting transparency on inactive hands. Modified Paths: -------------- trunk/pybridge/pybridge/ui/canvas.py trunk/pybridge/pybridge/ui/cardarea.py Modified: trunk/pybridge/pybridge/ui/canvas.py =================================================================== --- trunk/pybridge/pybridge/ui/canvas.py 2006-10-06 16:25:53 UTC (rev 346) +++ trunk/pybridge/pybridge/ui/canvas.py 2006-10-08 15:01:55 UTC (rev 347) @@ -58,18 +58,19 @@ self.window.invalidate_rect((0, 0, width, height), False) - def add_item(self, id, source, xy, z_index): + def add_item(self, id, source, xy, z_index, opacity=1): """Places source item into items list. @param id: unique identifier for source. @param source: ImageSurface. @param xy: tuple providing (x, y) coords for source in backing. @param z_index: integer. + @param opacity: integer in range 0 to 1. """ # Calculate and cache the on-screen area of the item. area = self.get_area(source, xy) self.items[id] = {'source': source, 'area': area, 'xy': xy, - 'z-index': z_index, } + 'z-index': z_index, 'opacity' : opacity, } self.redraw(*area) @@ -84,12 +85,17 @@ self.redraw(*area) - def update_item(self, id, source=None, xy=None, z_index=0): + def update_item(self, id, source=None, xy=None, z_index=0, opacity=0): """ - + @param id: unique identifier for source. + @param source: if specified, ImageSurface. + @param xy: if specified, tuple providing (x, y) coords for source in backing. + @param z_index: if specified, integer. + @param opacity: if specified, integer in range 0 to 1. """ # If optional parameters are not specified, use stored values. z_index = z_index or self.items[id]['z-index'] + opacity = opacity or self.items[id]['opacity'] if source or xy: # If source or xy coords changed, recalculate on-screen area. source = source or self.items[id]['source'] @@ -106,7 +112,7 @@ area = self.items[id]['area'] self.items[id] = {'source': source, 'area': area, 'xy' : xy, - 'z-index': z_index, } + 'z-index': z_index, 'opacity' : opacity, } self.redraw(*area) @@ -135,7 +141,8 @@ for item in items: pos_x, pos_y = item['area'][0:2] context.set_source_surface(item['source'], pos_x, pos_y) - context.paint() +# context.paint() + context.paint_with_alpha(item['opacity']) context.reset_clip() self.window.invalidate_rect((x, y, width, height), False) # Expose. Modified: trunk/pybridge/pybridge/ui/cardarea.py =================================================================== --- trunk/pybridge/pybridge/ui/cardarea.py 2006-10-06 16:25:53 UTC (rev 346) +++ trunk/pybridge/pybridge/ui/cardarea.py 2006-10-08 15:01:55 UTC (rev 347) @@ -196,26 +196,26 @@ layout.set_text(name) # Create an ImageSurface respective to dimensions of text. width, height = layout.get_pixel_size() + width += 8; height += 4 surface, context = self.new_surface(width, height) context = pangocairo.CairoContext(context) -# context.set_line_width(4) -# context.rectangle(0, 0, width, height) -# context.set_source_rgb(0, 0.5, 0) -# context.fill_preserve() -# context.set_source_rgb(0, 0.25, 0) -# context.stroke() -# context.move_to(4, 2) + # Draw background box, text to ImageSurface. + context.set_line_width(4) + context.rectangle(0, 0, width, height) + context.set_source_rgb(0, 0.5, 0) + context.fill_preserve() + context.set_source_rgb(0, 0.25, 0) + context.stroke() + context.move_to(4, 2) context.set_source_rgb(1, 1, 1) context.show_layout(layout) if id in self.items: self.update_item(id, source=surface) else: - xy = {self.TOP : (0.5, 0.05), self.BOTTOM : (0.5, 0.95), - self.LEFT : (0.15, 0.7), self.RIGHT : (0.85, 0.7), } -# xy = {self.TOP : (0.5, 0.3), self.BOTTOM : (0.5, 0.7), -# self.LEFT : (0.15, 0.7), self.RIGHT : (0.85, 0.7), } + xy = {self.TOP : (0.5, 0.15), self.BOTTOM : (0.5, 0.85), + self.LEFT : (0.15, 0.6), self.RIGHT : (0.85, 0.6), } self.add_item(id, surface, xy[seat], 2) @@ -237,35 +237,34 @@ @param trick: a (leader, cards_played) pair, or None. """ - id = 'trick' - self.trick = trick - if trick is None: - self.remove_item(id) - return + xy = {self.TOP : (0.5, 0.425), self.BOTTOM : (0.5, 0.575), + self.LEFT : (0.425, 0.5), self.RIGHT : (0.575, 0.5), } - width, height = 200, 200 - # (x, y) positions to fit within (width, height) bound box. - pos = {Seat.North : ((width - self.card_width)/2, 0 ), - Seat.East : ((width - self.card_width), (height - self.card_height)/2 ), - Seat.South : ((width - self.card_width)/2, (height - self.card_height) ), - Seat.West : (0, (height - self.card_height)/2 ), } - - surface, context = self.new_surface(width, height) - if trick: - leader, cards_played = trick # The order of play is the leader, then clockwise around Seat. - for seat in Seat[leader.index:] + Seat[:leader.index]: - card = cards_played.get(seat) - if card: - pos_x, pos_y = pos[seat] - self.draw_card(context, pos_x, pos_y, card) + leader = trick[0] + order = Seat[leader.index:] + Seat[:leader.index] + for i, seat in enumerate(order): + id = 'trick-%s' % seat + old_card = self.trick and self.trick[1].get(seat) or None + new_card = trick[1].get(seat) + # If old card matches new card, take no action. + if old_card is None and new_card is not None: + surface, context = self.new_surface(self.card_width, self.card_height) + self.draw_card(context, 0, 0, new_card) + self.add_item(id, surface, xy[seat], z_index=i+1) + elif new_card is None and old_card is not None: + self.remove_item(id) + elif old_card != new_card: + surface, context = self.new_surface(self.card_width, self.card_height) + self.draw_card(context, 0, 0, new_card) + self.update_item(id, surface, z_index=i+1) - if id in self.items: - self.update_item(id, source=surface) - else: - xy = (0.5, 0.5) - self.add_item(id, surface, xy, 0) + elif self.trick: # Remove all cards from previous trick. + for seat in self.trick[1]: + self.remove_item('trick-%s' % seat) + + self.trick = trick # Save trick and return. def set_turn(self, turn): @@ -276,27 +275,29 @@ @param turn: a member of Seat, or None. """ - id = 'turn' if turn is None: - self.remove_item(id) return - # TODO: select colours that don't clash with the background. - # TODO: one colour if user can play card from hand, another if not. - width = self.hands[turn]['surface'].get_width() + 20 - height = self.hands[turn]['surface'].get_height() + 20 - surface, context = self.new_surface(width, height) - context.set_source_rgb(0.3, 0.6, 0) # Green. - context.paint_with_alpha(0.5) - - xy = self.items['hand-%s' % turn]['xy'] # Use same xy as hand. - - if id in self.items: - self.update_item(id, source=surface, xy=xy) - else: - self.add_item(id, surface, xy, -1) + for seat in Seat: + opacity = (seat is turn) and 1 or 0.5 + self.update_item('hand-%s' % seat, opacity=opacity) +# # TODO: select colours that don't clash with the background. +# # TODO: one colour if user can play card from hand, another if not. +# width = self.hands[turn]['surface'].get_width() + 20 +# height = self.hands[turn]['surface'].get_height() + 20 +# surface, context = self.new_surface(width, height) +# context.set_source_rgb(0.3, 0.6, 0) # Green. +# context.paint_with_alpha(0.5) +# +# xy = self.items['hand-%s' % turn]['xy'] # Use same xy as hand. +# +# if id in self.items: +# self.update_item(id, source=surface, xy=xy) +# else: +# self.add_item(id, surface, xy, -1) + def button_release(self, widget, event): """Determines if a card was clicked: if so, calls card_selected.""" if event.button == 1: This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |