Flipping Cards from the Pile
authorJoachim Breitner <mail@joachim-breitner.de>
Thu, 10 May 2007 09:08:09 +0000 (09:08 +0000)
committerJoachim Breitner <mail@joachim-breitner.de>
Thu, 10 May 2007 09:08:09 +0000 (09:08 +0000)
cairoboard.py
engine.py

index 8beade8..92a58cb 100644 (file)
@@ -5,6 +5,7 @@
 import pygtk
 pygtk.require('2.0')
 import gtk
+import gobject
 import cairo
 import math
 
@@ -177,7 +178,6 @@ class CairoSlot:
                 self.fabric.setup(self.slot.ship)
                 self.slot.ship.guiinfo.draw(cr)
                 cr.set_matrix(matrix)
-            
             if self.slot.highlighted:
                 cr.set_source_rgba(1, 1, 1, 0.5)
                 cr.rectangle(-1, -1, 2, 2)
@@ -204,9 +204,12 @@ class CairoHandSlot:
 
 class CairoFabric:
     def get_widget(self, board):
-        board_draw = self.get_drawer(board)
-        board.guiinfo = board_draw
-        return board_draw
+        self.board_draw = self.get_drawer(board)
+        board.guiinfo = self.board_draw
+        return self.board_draw
+
+    def redraw(self):
+        self.board_draw.queue_draw()
 
     def get_drawer(self, object):
         for c, drawer in self.classes:
@@ -247,6 +250,18 @@ class CairoShip:
 class CairoPile:
     def __init__(self, fabric, slot):
         self.pile = self.slot
+        self.flip_d = None # How far the card has been flipped
+
+    def card_flipped(self):
+        def update():
+            self.flip_d += 0.06
+            if self.flip_d > 1:
+                self.flip_d = 1
+            self.fabric.redraw()
+            return self.flip_d < 1
+
+        self.flip_d = 0
+        gobject.timeout_add(40, update)
 
     def translate(self, cr):
         cr.translate(
@@ -285,9 +300,8 @@ class CairoPile:
         cr.set_line_cap(cairo.LINE_CAP_SQUARE)
         cr.translate(0,-3)
         if self.pile.cards:
-            cr.set_source_rgb(0, 0, 0.5)
-            cr.rectangle(-1, -1, 2, 2)
-            cr.fill()
+            self.fabric.setup(self.pile.cards[0])
+            self.pile.cards[0].guiinfo.draw_back(cr)
         else:
             cr.set_source_rgb(0, 0, 0)
             cr.set_line_width(0.05)
@@ -302,14 +316,40 @@ class CairoPile:
         cr.show_text(str(len(self.pile.cards)))
         cr.fill()
 
-        self.super.draw(self, window)
+        if self.flip_d == 1:
+            self.super.draw(self, window)
+        elif self.slot.card:
+            cr = window.cairo_create()
+            cr.translate( PILE_X, PILE_Y )
+            cr.scale(SCALE, SCALE)
+            if self.flip_d != 0.5: # Da gibts nichts zu malen
+                cr.translate(0, (1-self.flip_d) * (-3))
+                cr.scale(1, (2 * self.flip_d - 1) )
 
+                self.fabric.setup(self.slot.card)
+                if self.flip_d < 0.5: # back side
+                    self.slot.card.guiinfo.draw_back(cr)
+                else:
+                    self.slot.card.guiinfo.draw(cr)
+             
 class CairoCard:
     def __init__(self, fabric, card):
         self.fabric = fabric
         self.card = card
         self.cache = None
 
+    def draw_back(self, cr):
+        matrix = cr.get_matrix()
+        cr.rotate(-(self.card.rotation * math.pi / 2))
+
+        cr.rectangle(-1,-1, 2,2)
+        cr.set_source_rgb(0.2, 0.2, 0.8)
+        cr.fill_preserve()
+        cr.set_source_rgb(0.2, 0.2, 0.2)
+        cr.set_line_width(0.05)
+        cr.stroke()
+        cr.set_matrix(matrix)
+
     def draw(self, cr):
         cr.rotate(-(self.card.rotation * math.pi / 2))
 
index 2e87807..b217069 100644 (file)
--- a/engine.py
+++ b/engine.py
@@ -258,6 +258,8 @@ class Pile(Slot):
         assert state == SHIPPING
         state = PLACING
         self.card = self.cards.pop()
+        if self.guiinfo:
+            self.guiinfo.card_flipped()
 
     def can_drop_card(self, card):
         return False