Gemeinsamen Code wieder in Cairo* rein
authorJoachim Breitner <mail@joachim-breitner.de>
Sat, 7 Jul 2007 07:48:01 +0000 (07:48 +0000)
committerJoachim Breitner <mail@joachim-breitner.de>
Sat, 7 Jul 2007 07:48:01 +0000 (07:48 +0000)
cairoboard.py
grasstheme.py
seatheme.py

index f8d0eb8..8beade8 100644 (file)
@@ -7,7 +7,6 @@ pygtk.require('2.0')
 import gtk
 import cairo
 import math
-import random
 
 import engine
 
@@ -141,7 +140,6 @@ class CairoBoard(gtk.DrawingArea):
         cr.show_text(txt)
         cr.fill()
 
-
     def draw_cached_base(self,window):
         ocr = window.cairo_create()
         if not self.base_surface:
@@ -157,7 +155,52 @@ class CairoBoard(gtk.DrawingArea):
         ocr.fill()
 
 class CairoSlot:
-    pass
+    def __init__(self, fabric, slot):
+        self.fabric = fabric
+        self.slot = slot
+
+    def translate(self, cr):
+        raise "Abstract Method of Object" + str(self)
+
+    def draw(self, window):
+        if self.slot.card or self.slot.ship:
+            cr = window.cairo_create()
+            self.translate(cr)
+            cr.scale(SCALE, SCALE)
+
+            matrix = cr.get_matrix()
+            if self.slot.card:
+                self.fabric.setup(self.slot.card)
+                self.slot.card.guiinfo.draw(cr)
+                cr.set_matrix(matrix)
+            if self.slot.ship:
+                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)
+                cr.fill()
+
+class CairoBoardSlot:
+    def translate(self, cr):
+        cr.translate(
+                 GRIDPAD + (self.slot.x+0.5)*GRIDWIDTH,
+                 GRIDPAD + (self.slot.y+0.5)*GRIDWIDTH
+        )
+
+class CairoHandSlot:
+    def __init__(self):
+        self.x = 0
+        self.y = 0
+
+    def move_to(self, x,y):
+        self.x = x
+        self.y = y
+
+    def translate(self, cr):
+        cr.translate(self.x, self.y)
 
 class CairoFabric:
     def get_widget(self, board):
@@ -175,4 +218,139 @@ class CairoFabric:
         if not object.guiinfo:
             object.guiinfo = self.get_drawer(object)
 
+class CairoShip:
+    def __init__(self, fabric, ship):
+        self.fabric = fabric
+        self.ship = ship
+        self.cache = None
+    
+    def draw_cache(self, cr):
+        raise "Abstrac Method: "+str(self)
+
+    def draw(self, cr):
+        cr.rotate(-(self.ship.rotation * math.pi / 2))
+        cr.rotate(math.pi / 2) # Koordinaten unten passen sonst nicht
+
+        if not self.cache:
+            self.cache = cr.get_target().create_similar(cairo.CONTENT_COLOR_ALPHA, 
+                    GRIDWIDTH + GRIDPAD, GRIDWIDTH + GRIDPAD)
+            scr = cairo.Context(self.cache)
+            scr.translate((GRIDWIDTH + GRIDPAD)/2, (GRIDWIDTH+GRIDPAD)/2)
+            scr.scale(SCALE, SCALE)
+
+            self.draw_cache(scr)
+
+        cr.scale(1.0/SCALE, 1.0/SCALE)
+        cr.set_source_surface(self.cache,-(GRIDWIDTH + GRIDPAD)/2,-(GRIDWIDTH + GRIDPAD)/2)
+        cr.paint()
+
+class CairoPile:
+    def __init__(self, fabric, slot):
+        self.pile = self.slot
+
+    def translate(self, cr):
+        cr.translate(
+                PILE_X,
+                PILE_Y,
+        )
+    
+    def draw_arrow(self, cr):
+        cr.arc(1, 1, 0.5, 0, math.pi/2)
+        cr.set_source_rgb(0, 0, 0)
+        cr.set_line_width(0.1)
+        cr.set_line_cap(cairo.LINE_CAP_ROUND)
+        cr.move_to(1,    1.5)
+        cr.line_to(0.75, 1.5)
+        cr.move_to(0.75, 1.5)
+        cr.line_to(1   , 1.75)
+        cr.move_to(0.75, 1.5)
+        cr.line_to(1   , 1.25)
+        cr.stroke()
+
+    def draw(self, window):
+        cr = window.cairo_create()
+        cr.translate(PILE_X, PILE_Y)
+        cr.scale(SCALE, SCALE)
+
+        cr.set_source_rgb(0.8, 0.8, 0.8)
+        cr.rectangle(-1, -1, 2, 2)
+        cr.fill()
+
+        matrix = cr.get_matrix()
+        self.draw_arrow(cr)
+        cr.scale(-1,1)
+        self.draw_arrow(cr)
+        cr.set_matrix(matrix)
+
+        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()
+        else:
+            cr.set_source_rgb(0, 0, 0)
+            cr.set_line_width(0.05)
+            cr.rectangle(-1, -1, 2, 2)
+            cr.stroke()
+
+        cr.set_font_size(1)
+        cr.set_source_rgb(1,1,1)
+        (_,_,w,h,_,_) = cr.text_extents(str(len(self.pile.cards)))
+
+        cr.move_to(-w/2, h/2)
+        cr.show_text(str(len(self.pile.cards)))
+        cr.fill()
+
+        self.super.draw(self, window)
+
+class CairoCard:
+    def __init__(self, fabric, card):
+        self.fabric = fabric
+        self.card = card
+        self.cache = None
+
+    def draw(self, cr):
+        cr.rotate(-(self.card.rotation * math.pi / 2))
+
+        if not self.cache:
+            self.cache = cr.get_target().create_similar(cairo.CONTENT_COLOR_ALPHA, 
+                    GRIDWIDTH + GRIDPAD, GRIDWIDTH + GRIDPAD)
+            scr = cairo.Context(self.cache)
+            scr.translate((GRIDWIDTH + GRIDPAD)/2, (GRIDWIDTH+GRIDPAD)/2)
+            scr.scale(SCALE, SCALE)
+
+            self.draw_cache(scr)
+
+        cr.scale(1.0/SCALE, 1.0/SCALE)
+        cr.set_source_surface(self.cache,-(GRIDWIDTH + GRIDPAD)/2,-(GRIDWIDTH + GRIDPAD)/2)
+        cr.paint()
+
+class CairoBlackHole:
+    def __init__(self, fabric, bh):
+        self.fabric = fabric
+        self.bh = bh
+
+    def draw(self, window):
+        cr = window.cairo_create()
+        cr.translate(BH_X, BH_Y)
+        cr.scale(SCALE, SCALE)
+
+        cr.rectangle(-1, -1, 2, 2)
+        cr.clip()
+
+        cr.set_source_rgb(0,0,0)
+        cr.arc(0, 0, 0.8, 0, 2*math.pi)
+        cr.fill()
+        
+        cr.set_font_size(1)
+        cr.set_source_rgb(1,1,1)
+        (_,_,w,h,_,_) = cr.text_extents(str(self.bh.count))
+
+        cr.move_to(-w/2, h/2)
+        cr.show_text(str(self.bh.count))
+        cr.fill()
+
+
+
 # vim:ts=4:sw=4:sts=4:et
index 5b0efd6..5a683ed 100644 (file)
@@ -99,51 +99,15 @@ class GrassBoard(CairoBoard):
 
 class GrassSlot(CairoSlot):
     def __init__(self, fabric, slot):
-        self.fabric = fabric
-        self.slot = slot
+        CairoSlot.__init__(self, fabric, slot)
 
-    def translate(self, cr):
-        raise "Abstract Method of Object" + str(self)
-
-    def draw(self, window):
-        if self.slot.card or self.slot.ship:
-            cr = window.cairo_create()
-            self.translate(cr)
-            cr.scale(SCALE, SCALE)
-
-            #cr.rectangle(-1, -1, 2, 2)
-            #cr.clip()
-
-            matrix = cr.get_matrix()
-            if self.slot.card:
-                self.fabric.setup(self.slot.card)
-                self.slot.card.guiinfo.draw(cr)
-                cr.set_matrix(matrix)
-            if self.slot.ship:
-                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)
-                cr.fill()
-
-class GrassBoardSlot(GrassSlot):
+class GrassBoardSlot(CairoBoardSlot, GrassSlot):
     def __init__(self, fabric, slot):
         GrassSlot.__init__(self, fabric, slot)
 
-    def translate(self, cr):
-        cr.translate(
-                 GRIDPAD + (self.slot.x+0.5)*GRIDWIDTH,
-                 GRIDPAD + (self.slot.y+0.5)*GRIDWIDTH
-        )
-
-class GrassShip:
+class GrassShip(CairoShip):
     def __init__(self, fabric, ship):
-        self.fabric = fabric
-        self.ship = ship
-        self.cache = None
+        CairoShip.__init__(self, fabric, ship)
     
     def draw_cache(self, cr):
         # Grundkörper (noch abzurunden)
@@ -185,118 +149,21 @@ class GrassShip:
             cr.set_source_rgb(0, 0, 0)
             cr.fill()
 
-    def draw(self, cr):
-        cr.rotate(-(self.ship.rotation * math.pi / 2))
-        cr.rotate(math.pi / 2) # Koordinaten unten passen sonst nicht
-
-        if not self.cache:
-            self.cache = cr.get_target().create_similar(cairo.CONTENT_COLOR_ALPHA, 
-                    GRIDWIDTH + GRIDPAD, GRIDWIDTH + GRIDPAD)
-            scr = cairo.Context(self.cache)
-            scr.translate((GRIDWIDTH + GRIDPAD)/2, (GRIDWIDTH+GRIDPAD)/2)
-            scr.scale(SCALE, SCALE)
-
-            self.draw_cache(scr)
-
-        cr.scale(1.0/SCALE, 1.0/SCALE)
-        cr.set_source_surface(self.cache,-(GRIDWIDTH + GRIDPAD)/2,-(GRIDWIDTH + GRIDPAD)/2)
-        cr.paint()
 
-class GrassHandSlot(GrassSlot):
+class GrassHandSlot(CairoHandSlot, GrassSlot):
     def __init__(self, fabric, slot):
         GrassSlot.__init__(self, fabric, slot)
-        self.x = 0
-        self.y = 0
+        CairoHandSlot.__init__(self)
 
-    def move_to(self, x,y):
-        self.x = x
-        self.y = y
-
-    def translate(self, cr):
-        cr.translate(self.x, self.y)
-
-class GrassPile(GrassSlot):
+class GrassPile(CairoPile, GrassSlot):
     def __init__(self, fabric, slot):
         GrassSlot.__init__(self, fabric, slot)
-        self.pile = self.slot
-
-    def translate(self, cr):
-        cr.translate(
-                PILE_X,
-                PILE_Y,
-        )
-    
-    def draw_arrow(self, cr):
-        cr.arc(1, 1, 0.5, 0, math.pi/2)
-        cr.set_source_rgb(0, 0, 0)
-        cr.set_line_width(0.1)
-        cr.set_line_cap(cairo.LINE_CAP_ROUND)
-        cr.move_to(1,    1.5)
-        cr.line_to(0.75, 1.5)
-        cr.move_to(0.75, 1.5)
-        cr.line_to(1   , 1.75)
-        cr.move_to(0.75, 1.5)
-        cr.line_to(1   , 1.25)
-        cr.stroke()
-
-    def draw(self, window):
-        cr = window.cairo_create()
-        cr.translate(PILE_X, PILE_Y)
-        cr.scale(SCALE, SCALE)
-
-        cr.set_source_rgb(0.8, 0.8, 0.8)
-        cr.rectangle(-1, -1, 2, 2)
-        cr.fill()
-
-        matrix = cr.get_matrix()
-        self.draw_arrow(cr)
-        cr.scale(-1,1)
-        self.draw_arrow(cr)
-        cr.set_matrix(matrix)
+        CairoPile.__init__(self, fabric, slot)
+        self.super = GrassSlot
 
-        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()
-        else:
-            cr.set_source_rgb(0, 0, 0)
-            cr.set_line_width(0.05)
-            cr.rectangle(-1, -1, 2, 2)
-            cr.stroke()
-
-        cr.set_font_size(1)
-        cr.set_source_rgb(1,1,1)
-        (_,_,w,h,_,_) = cr.text_extents(str(len(self.pile.cards)))
-
-        cr.move_to(-w/2, h/2)
-        cr.show_text(str(len(self.pile.cards)))
-        cr.fill()
-
-        GrassSlot.draw(self, window)
-
-class GrassCard:
+class GrassCard(CairoCard):
     def __init__(self, fabric, card):
-        self.fabric = fabric
-        self.card = card
-        self.cache = None
-
-    def draw(self, cr):
-        cr.rotate(-(self.card.rotation * math.pi / 2))
-
-        if not self.cache:
-            self.cache = cr.get_target().create_similar(cairo.CONTENT_COLOR_ALPHA, 
-                    GRIDWIDTH + GRIDPAD, GRIDWIDTH + GRIDPAD)
-            scr = cairo.Context(self.cache)
-            scr.translate((GRIDWIDTH + GRIDPAD)/2, (GRIDWIDTH+GRIDPAD)/2)
-            scr.scale(SCALE, SCALE)
-
-            self.draw_cache(scr)
-
-        cr.scale(1.0/SCALE, 1.0/SCALE)
-        cr.set_source_surface(self.cache,-(GRIDWIDTH + GRIDPAD)/2,-(GRIDWIDTH + GRIDPAD)/2)
-        cr.paint()
+        CairoCard.__init__(self, fabric, card)
 
     #Karte füllen
     def draw_cache(self, cr):
@@ -387,7 +254,6 @@ class GrassCard:
                 random.random(),
                 random.random() )
 
-
     #Eine Blume malen
     def draw_flower(self, cr):
         cr.rotate(random.uniform(0,2*math.pi))
@@ -406,10 +272,15 @@ class GrassCard:
             cr.rotate(2* math.pi / 5)
         cr.set_matrix(matrix)
     
+class GrassBlackHole(CairoBlackHole):
+    def __init__(self, fabric, bh):
+        CairoBlackHole.__init__(self, fabric, bh)
+
 class GrassFabric(CairoFabric):
     classes = [ 
             (engine.Board,      GrassBoard),
             (engine.BoardSlot,  GrassBoardSlot),
+            (engine.BlackHole,  GrassBlackHole),
             (engine.HandSlot,   GrassHandSlot),
             (engine.Pile,       GrassPile),
             (engine.Slot,       GrassSlot),
index d0b455a..9e63922 100644 (file)
@@ -6,7 +6,6 @@ pygtk.require('2.0')
 import gtk
 import cairo
 import math
-import random
 
 from cairoboard import *
 import engine
@@ -97,166 +96,30 @@ class SeaBoard(CairoBoard):
 
 class SeaSlot(CairoSlot):
     def __init__(self, fabric, slot):
-        self.fabric = fabric
-        self.slot = slot
+        CairoSlot.__init__(self, fabric, slot)
 
-    def translate(self, cr):
-        raise "Abstract Method of Object" + str(self)
-
-    def draw(self, window):
-        if self.slot.card or self.slot.ship:
-            cr = window.cairo_create()
-            self.translate(cr)
-            cr.scale(SCALE, SCALE)
-
-            #cr.rectangle(-1, -1, 2, 2)
-            #cr.clip()
-
-            matrix = cr.get_matrix()
-            if self.slot.card:
-                self.fabric.setup(self.slot.card)
-                self.slot.card.guiinfo.draw(cr)
-                cr.set_matrix(matrix)
-            if self.slot.ship:
-                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)
-                cr.fill()
-
-class SeaHandSlot(SeaSlot):
+class SeaHandSlot(CairoHandSlot, SeaSlot):
     def __init__(self, fabric, slot):
         SeaSlot.__init__(self, fabric, slot)
-        self.x = 0
-        self.y = 0
-
-    def move_to(self, x,y):
-        self.x = x
-        self.y = y
+        CairoHandSlot.__init__(self)
 
-    def translate(self, cr):
-        cr.translate(self.x, self.y)
-
-class SeaBoardSlot(SeaSlot):
+class SeaBoardSlot(CairoBoardSlot, SeaSlot):
     def __init__(self, fabric, slot):
         SeaSlot.__init__(self, fabric, slot)
 
-    def translate(self, cr):
-        cr.translate(
-                 GRIDPAD + (self.slot.x+0.5)*GRIDWIDTH,
-                 GRIDPAD + (self.slot.y+0.5)*GRIDWIDTH
-        )
-
-class SeaPile(SeaSlot):
+class SeaPile(CairoPile, SeaSlot):
     def __init__(self, fabric, slot):
         SeaSlot.__init__(self, fabric, slot)
-        self.pile = self.slot
-
-    def translate(self, cr):
-        cr.translate(
-                PILE_X,
-                PILE_Y,
-        )
-    
-    def draw_arrow(self, cr):
-        cr.arc(1, 1, 0.5, 0, math.pi/2)
-        cr.set_source_rgb(0, 0, 0)
-        cr.set_line_width(0.1)
-        cr.set_line_cap(cairo.LINE_CAP_ROUND)
-        cr.move_to(1,    1.5)
-        cr.line_to(0.75, 1.5)
-        cr.move_to(0.75, 1.5)
-        cr.line_to(1   , 1.75)
-        cr.move_to(0.75, 1.5)
-        cr.line_to(1   , 1.25)
-        cr.stroke()
-
-    def draw(self, window):
-        cr = window.cairo_create()
-        cr.translate(PILE_X, PILE_Y)
-        cr.scale(SCALE, SCALE)
-
-        cr.set_source_rgb(0.8, 0.8, 0.8)
-        cr.rectangle(-1, -1, 2, 2)
-        cr.fill()
-
-        matrix = cr.get_matrix()
-        self.draw_arrow(cr)
-        cr.scale(-1,1)
-        self.draw_arrow(cr)
-        cr.set_matrix(matrix)
-
-        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()
-        else:
-            cr.set_source_rgb(0, 0, 0)
-            cr.set_line_width(0.05)
-            cr.rectangle(-1, -1, 2, 2)
-            cr.stroke()
-
-        cr.set_font_size(1)
-        cr.set_source_rgb(1,1,1)
-        (_,_,w,h,_,_) = cr.text_extents(str(len(self.pile.cards)))
+        CairoPile.__init__(self, fabric, slot)
+        self.super = SeaSlot
 
-        cr.move_to(-w/2, h/2)
-        cr.show_text(str(len(self.pile.cards)))
-        cr.fill()
-
-        SeaSlot.draw(self, window)
-
-class SeaBlackHole:
+class SeaBlackHole(CairoBlackHole):
     def __init__(self, fabric, bh):
-        self.fabric = fabric
-        self.bh = bh
-
-    def draw(self, window):
-        cr = window.cairo_create()
-        cr.translate(BH_X, BH_Y)
-        cr.scale(SCALE, SCALE)
-
-        cr.rectangle(-1, -1, 2, 2)
-        cr.clip()
+        CairoBlackHole.__init__(self, fabric, bh)
 
-        cr.set_source_rgb(0,0,0)
-        cr.arc(0, 0, 0.8, 0, 2*math.pi)
-        cr.fill()
-        
-        cr.set_font_size(1)
-        cr.set_source_rgb(1,1,1)
-        (_,_,w,h,_,_) = cr.text_extents(str(self.bh.count))
-
-        cr.move_to(-w/2, h/2)
-        cr.show_text(str(self.bh.count))
-        cr.fill()
-
-class SeaCard:
+class SeaCard(CairoCard):
     def __init__(self, fabric, card):
-        self.fabric = fabric
-        self.card = card
-        self.cache = None
-
-    def draw(self, cr):
-        cr.rotate(-(self.card.rotation * math.pi / 2))
-
-        if not self.cache:
-            self.cache = cr.get_target().create_similar(cairo.CONTENT_COLOR_ALPHA, 
-                    GRIDWIDTH + GRIDPAD, GRIDWIDTH + GRIDPAD)
-            scr = cairo.Context(self.cache)
-            scr.translate((GRIDWIDTH + GRIDPAD)/2, (GRIDWIDTH+GRIDPAD)/2)
-            scr.scale(SCALE, SCALE)
-
-            self.draw_cache(scr)
-
-        cr.scale(1.0/SCALE, 1.0/SCALE)
-        cr.set_source_surface(self.cache,-(GRIDWIDTH + GRIDPAD)/2,-(GRIDWIDTH + GRIDPAD)/2)
-        cr.paint()
+        CairoCard.__init__(self, fabric, card)
 
     def draw_cache(self, cr):
         # Meer
@@ -287,12 +150,10 @@ class SeaCard:
         cr.rectangle(-1, -1, 2, 2)
         cr.stroke()
 
-class SeaShip:
+class SeaShip(CairoShip):
     def __init__(self, fabric, ship):
-        self.fabric = fabric
-        self.ship = ship
-        self.cache = None
-    
+        CairoShip.__init__(self, fabric, ship)
+
     def draw_cache(self, cr):
         cr.move_to(0.9, 0)
         cr.arc(0, 0, 0.3, math.pi, 0) 
@@ -303,27 +164,11 @@ class SeaShip:
         cr.set_source_rgb(1, 0, 0)
         cr.fill()
 
-    def draw(self, cr):
-        cr.rotate(-(self.ship.rotation * math.pi / 2))
-        cr.rotate(math.pi / 2) # Koordinaten unten passen sonst nicht
-
-        if not self.cache:
-            self.cache = cr.get_target().create_similar(cairo.CONTENT_COLOR_ALPHA, 
-                    GRIDWIDTH + GRIDPAD, GRIDWIDTH + GRIDPAD)
-            scr = cairo.Context(self.cache)
-            scr.translate((GRIDWIDTH + GRIDPAD)/2, (GRIDWIDTH+GRIDPAD)/2)
-            scr.scale(SCALE, SCALE)
-
-            self.draw_cache(scr)
-
-        cr.scale(1.0/SCALE, 1.0/SCALE)
-        cr.set_source_surface(self.cache,-(GRIDWIDTH + GRIDPAD)/2,-(GRIDWIDTH + GRIDPAD)/2)
-        cr.paint()
-
 class SeaFabric(CairoFabric):
     classes = [ 
             (engine.Board,      SeaBoard),
             (engine.BoardSlot,  SeaBoardSlot),
+            (engine.BlackHole,  SeaBlackHole),
             (engine.HandSlot,   SeaHandSlot),
             (engine.Pile,       SeaPile),
             (engine.Slot,       SeaSlot),