#!/usr/bin/python
# encoding:utf8

import pygtk
pygtk.require('2.0')
import gtk
import cairo
import math
import random

from cairoboard import *
import engine


class GrassBoard(CairoBoard):
    def draw_base(self,cr):
        cr.translate(GRIDPAD, GRIDPAD)
        cr.scale(GRIDWIDTH, GRIDWIDTH)

        # Earth Background

        # Unten
        pat = cairo.LinearGradient (0, 5, 0, 5.5)
        pat.add_color_stop_rgba(0.3, 0.6, 0.4, 0.4,1)
        pat.add_color_stop_rgba(1, 0.6, 0.4, 0.4,0)
        cr.rectangle(1, 5, 7, 0.5)
        cr.set_source(pat)
        cr.fill()

        # Oben
        pat = cairo.LinearGradient (0, 0, 0, -0.5)
        pat.add_color_stop_rgba(0.3, 0.6, 0.4, 0.4,1)
        pat.add_color_stop_rgba(1, 0.6, 0.4, 0.4,0)
        cr.rectangle(1, -0.5, 7, 0.5)
        cr.set_source(pat)
        cr.fill()

        # Links
        pat = cairo.LinearGradient (1, 0, 0.5, 0)
        pat.add_color_stop_rgba(0.3, 0.6, 0.4, 0.4,1)
        pat.add_color_stop_rgba(1, 0.6, 0.4, 0.4,0)
        cr.rectangle(0.5, 0, 0.5, 5)
        cr.set_source(pat)
        cr.fill()

        # Rechts
        pat = cairo.LinearGradient (8, 0, 8.5, 0)
        pat.add_color_stop_rgba(0.3, 0.6, 0.4, 0.4,1)
        pat.add_color_stop_rgba(1, 0.6, 0.4, 0.4,0)
        cr.rectangle(8, 0, 0.5, 5)
        cr.set_source(pat)
        cr.fill()

        # Oben Links
        pat = cairo.RadialGradient(1,0,0, 1,0, 0.5)
        pat.add_color_stop_rgba(0.3, 0.6, 0.4, 0.4, 1)
        pat.add_color_stop_rgba(1, 0.6, 0.4, 0.4, 0)
        cr.rectangle(0.5, -0.5, 0.5, 0.5)
        cr.set_source(pat)
        cr.fill()

        # Oben Rechts
        pat = cairo.RadialGradient(8,0,0, 8,0, 0.5)
        pat.add_color_stop_rgba(0.3, 0.6, 0.4, 0.4, 1)
        pat.add_color_stop_rgba(1, 0.6, 0.4, 0.4, 0)
        cr.rectangle(8, -0.5, 8.5, 0.5)
        cr.set_source(pat)
        cr.fill()

        # Unten Rechts
        pat = cairo.RadialGradient(8,5,0, 8,5, 0.5)
        pat.add_color_stop_rgba(0.3, 0.6, 0.4, 0.4, 1)
        pat.add_color_stop_rgba(1, 0.6, 0.4, 0.4, 0)
        cr.rectangle(8, 5, 8.5, 5.5)
        cr.set_source(pat)
        cr.fill()

        # Unten Links
        pat = cairo.RadialGradient(1,5,0, 1,5, 0.5)
        pat.add_color_stop_rgba(0.3, 0.6, 0.4, 0.4, 1)
        pat.add_color_stop_rgba(1, 0.6, 0.4, 0.4, 0)
        cr.rectangle(0.5, 5, 0.5, 5.5)
        cr.set_source(pat)
        cr.fill()


        # Frame
        cr.set_source_rgb(1,1,1)
        cr.set_line_width(0.05)
        cr.set_line_cap(cairo.LINE_CAP_ROUND)
        for x in range(0,8):
            cr.move_to(1+x, 0)
            cr.line_to(1+x, 5.5)
            cr.stroke()
        for y in range(0,6):
            cr.move_to(0.5, y)
            cr.line_to(8.5, y)
            cr.stroke()

class GrassSlot(CairoSlot):
    def __init__(self, fabric, slot):
        CairoSlot.__init__(self, fabric, slot)

class GrassBoardSlot(CairoBoardSlot, GrassSlot):
    def __init__(self, fabric, slot):
        GrassSlot.__init__(self, fabric, slot)

class GrassShip(CairoShip):
    def __init__(self, fabric, ship):
        CairoShip.__init__(self, fabric, ship)
    
    def draw_cache(self, cr):
        # Grundkörper (noch abzurunden)
        cr.move_to(-0.5, -0.75)
        cr.line_to(0.5, -0.75)
        cr.line_to(0.5, 0.5)
        cr.line_to(-0.5, 0.5)
        cr.close_path()
        cr.set_source_rgb(1, 0.2, 0.2)
        cr.fill_preserve()
        cr.set_source_rgb(0, 0, 0)
        cr.set_line_width(0.05)
        cr.stroke()

        # Griff
        cr.move_to(-0.25, 0.25)
        cr.line_to(-0.55,  0.75)
        cr.line_to( 0.55,  0.75)
        cr.line_to( 0.25, 0.25)
        cr.set_line_width(0.1)
        cr.set_line_join(cairo.LINE_JOIN_ROUND)
        cr.stroke()
        
        # Motor
        cr.arc(0, -0.1 , 0.3, 0, 2*math.pi)
        cr.set_source_rgb(0, 0, 0)
        cr.fill()

        # Augen
        for _ in range(2):
            cr.scale(-1,1)
            cr.arc(0.3, -0.55 , 0.15, 0, 2*math.pi)
            cr.set_source_rgb(1, 1, 1)
            cr.fill_preserve()
            cr.set_source_rgb(0, 0, 0)
            cr.set_line_width(0.05)
            cr.stroke()
            cr.arc(0.25, -0.6 , 0.05, 0, 2*math.pi)
            cr.set_source_rgb(0, 0, 0)
            cr.fill()


class GrassHandSlot(CairoHandSlot, GrassSlot):
    def __init__(self, fabric, slot):
        GrassSlot.__init__(self, fabric, slot)
        CairoHandSlot.__init__(self)

class GrassPile(CairoPile, GrassSlot):
    def __init__(self, fabric, slot):
        GrassSlot.__init__(self, fabric, slot)
        CairoPile.__init__(self, fabric, slot)
        self.super = GrassSlot

class GrassCard(CairoCard):
    def __init__(self, fabric, card):
        CairoCard.__init__(self, fabric, card)

    #Karte füllen
    def draw_cache(self, cr):
        orientation=0;
        cr.set_source_rgb(0.6, 0.4, 0.3)
        cr.rectangle(-1, -1, 2, 2)
        cr.fill()
        p2c = [ (0.7,0), (0,-0.7), (-0.7,0), (0,0.7) ]
        between_p2c = [ (0.6,-0.6), (-0.6,-0.6), (-0.6,0.6), (0.6,0.6) ]
        matrix=cr.get_matrix()
        for c in [
                    (-0.7,-0.6), 
                    (0,-0.65),
                    (-0.5,-0.6),
                    (0.4,-0.55),
                    (0.8,-0.6),
                    (-0.3,-0.4),
                    (0.6,-0.35),
                    (-0.85,-0.3),
                    (0.3,-0.3),
                    (0,-0.25),
                    (-0.6,-0.2),
                    (0.75,-0.2),
                    (0.5,-0.1),
                    (-0.5,0),
                    (0.3,0.1),
                    (-0.1,0.1),
                    (-0.8,0.2),
                    (0.7,0.2),
                    (-0.4,0.3),
                    (0.85,0.3),
                    (-0.1,0.35),
                    (0.25,0.4),
                    (-0.75,0.4),
                    (0.6,0.45),
                    (-0.45,0.5),
                    (0,0.6),
                    (-0.8,0.7),
                    (0.75,0.7),
                    (-0.4,0.75),
                    (0.5,0.75),
                    (0.15,0.8),
                    (-0.2,0.85),
                    (-0.85,0.9),
                    (0.85,0.9),
                    (0,0.95),
                    (-0.5,1),
                    (0.45,1),
                    
                ]:
            cr.translate(*c)
            self.draw_gras(cr, 0)
            self.draw_gras(cr, 1)
            cr.set_matrix(matrix)

        for island in self.card.islands:
            for dir in island:
                cr.translate(*p2c[dir])
                self.draw_flower(cr)
                cr.set_matrix(matrix)
            for dir in island:
                if (dir+1)%4 in island:
                    cr.translate(*between_p2c[dir])
                    self.draw_flower(cr)
                    cr.set_matrix(matrix)
            if len(island) == 4:
                self.draw_flower(cr)

    #Einen Grashalm malen
    def draw_gras(self, cr, orientation):
        x=random.uniform(0.1,0.2)
        y=random.uniform(0.2,0.3)
        matrix=cr.get_matrix()
        if orientation == 0: 
            cr.scale(x, y)
        else:
            cr.scale(-x, y)
        cr.translate(-1,-1)
        cr.set_source_rgb(0, 1, 0)
        cr.move_to(-1,-1)
        cr.curve_to(0,-1,1,0,1,1)
        cr.curve_to(0,1,0,-1,-1,-1)
        cr.fill_preserve()
        cr.set_source_rgb(0, 0, 0)
        cr.set_line_width(0.1)
        cr.stroke()
        cr.set_matrix(matrix)
            
    def choose_flower_color(self):
        return (1,random.random(),random.random())

    #Eine Blume malen
    def draw_flower(self, cr):
        cr.rotate(random.uniform(0,2*math.pi))
        matrix=cr.get_matrix()
        cr.scale(0.25, 0.25)
        color = self.choose_flower_color()
        for n in range(5):
            cr.set_source_rgb(*color)
            cr.move_to(0,0)
            cr.curve_to(0,-0.8,-0.2,-1,-1,-1)
            cr.curve_to(-1,-0.2,-0.8,0.2,0,0)
            cr.fill_preserve()
            cr.set_source_rgb(0, 0, 0)
            cr.set_line_width(0.1)
            cr.stroke()
            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),
            (engine.Ship,       GrassShip),
            (engine.Card,       GrassCard),
    ]


# vim:ts=4:sw=4:sts=4:et
