Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/dominopiece.py
blob: d9db237955f19f67774a2d68242114b513342dd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python

# Piezas del domino
# By Gonzalo Odiard, 2006 godiard at gmail.com
# GPL License - http://www.gnu.org/copyleft/gpl.html

from sugar3.graphics import style
import dominoview
import cairoutils


class DominoPiece:
    """ Informacion de cada pieza del juego (visible o no) """

    PIECE_PLAYER = 1
    PIECE_IN_MOVE = 2
    PIECE_PLACED = 3
    PIECE_WAITING = 0

    def __init__(self, a, b):
        # print "creando pieza",n,p

        self.a = a
        self.b = b
        self.textA = str(a)
        self.textB = str(b)
        self.vertical = False
        self.reversed = False
        self.player = None
        self.state = DominoPiece.PIECE_WAITING
        self.visible = False
        self._itemA = None
        self._itemB = None

    def check_touched(self, x, y):
        # check if the x, y position touch the piece
        if self.vertical:
            width = dominoview.SIZE
            height = dominoview.SIZE * 2
        else:
            width = dominoview.SIZE * 2
            height = dominoview.SIZE
        return (self.x < x < self.x + width) and (self.y < y < self.y + height)

    def draw(self, ctx, selected, fipped=False):
        SIZE = dominoview.SIZE
        # if self.vertical:
        #   width = SIZE + SIZE/8 + 2
        #   height = SIZE*2 + SIZE/8 + 2
        # else:
        #   width = SIZE*2 + SIZE/8 + 2
        #   height = SIZE + SIZE/8 + 2

        ctx.save()
        ctx.translate(self.x, self.y)
        ctx.move_to(self.x, self.y)
        # ctx.translate(1,1)
        # ctx.move_to(1, 1)
        r = 10
        stroke_r, stroke_g, stroke_b = 0, 0, 0
        fill_r, fill_g, fill_b = 1, 1, 1
        alpha = 1
        if self.player.color is not None:
            xocolor = self.player.color
            stroke_r, stroke_g, stroke_b, alpha = style.Color(
                xocolor.get_stroke_color(), 1.0).get_rgba()
            fill_r, fill_g, fill_b, alpha = style.Color(
                xocolor.get_fill_color(), 1.0).get_rgba()

        if self.vertical:
            cairoutils.draw_round_rect(ctx, -1, -1, SIZE + 2, SIZE * 2 + 2, r)

            ctx.set_source_rgb(stroke_r, stroke_g, stroke_b)
            ctx.fill()

            cairoutils.draw_round_rect(ctx, 0, 0, SIZE, SIZE * 2, r)
            if selected:
                ctx.set_source_rgb(1, 1, 204.0 / 255.0)
            else:
                ctx.set_source_rgb(fill_r, fill_g, fill_b)
            ctx.fill()

            ctx.move_to(SIZE, 0)
            ctx.rel_line_to((SIZE / 8), (SIZE / 8))
            ctx.rel_line_to(0, SIZE * 2)
            ctx.rel_line_to(-(SIZE / 8), -(SIZE / 8))
            ctx.rel_line_to(0, -SIZE * 2)
            ctx.close_path()
            ctx.fill_preserve()
            ctx.set_source_rgb(stroke_r, stroke_g, stroke_b)
            ctx.stroke()

            ctx.move_to(0, SIZE * 2)
            ctx.rel_line_to((SIZE / 8), (SIZE / 8))
            ctx.rel_line_to(SIZE, 0)
            ctx.rel_line_to(- (SIZE / 8), -(SIZE / 8))
            ctx.rel_line_to(-SIZE, 0)
            ctx.close_path()
            ctx.set_source_rgb(fill_r, fill_g, fill_b)
            ctx.fill_preserve()
            ctx.set_source_rgb(stroke_r, stroke_g, stroke_b)
            ctx.stroke()

            if not fipped:
                if not self.reversed:
                    self._draw_label_a(ctx, 0, 0)
                    self._draw_label_b(ctx, 0, SIZE)
                else:
                    self._draw_label_b(ctx, 0, 0)
                    self._draw_label_a(ctx, 0, SIZE)

        else:
            cairoutils.draw_round_rect(ctx, -1, -1, SIZE * 2 + 2, SIZE + 2, r)
            ctx.set_source_rgb(stroke_r, stroke_g, stroke_b)
            ctx.fill()

            cairoutils.draw_round_rect(ctx, 0, 0, SIZE * 2, SIZE, r)
            if selected:
                ctx.set_source_rgb(1, 1, 204.0 / 255.0)
            else:
                ctx.set_source_rgb(fill_r, fill_g, fill_b)
            ctx.fill()

            ctx.move_to(SIZE * 2, 0)
            ctx.rel_line_to((SIZE / 8), (SIZE / 8))
            ctx.rel_line_to(0, SIZE)
            ctx.rel_line_to(-(SIZE / 8), -(SIZE / 8))
            ctx.rel_line_to(0, -SIZE)
            ctx.close_path()
            ctx.fill_preserve()
            ctx.set_source_rgb(stroke_r, stroke_g, stroke_b)
            ctx.stroke()

            ctx.move_to(0, SIZE)
            ctx.rel_line_to((SIZE / 8), (SIZE / 8))
            ctx.rel_line_to(SIZE * 2, 0)
            ctx.rel_line_to(-(SIZE / 8), -(SIZE / 8))
            ctx.rel_line_to(-SIZE * 2, 0)
            ctx.close_path()
            ctx.set_source_rgb(fill_r, fill_g, fill_b)
            ctx.fill_preserve()
            ctx.set_source_rgb(stroke_r, stroke_g, stroke_b)
            ctx.stroke()

            if not fipped:
                if not self.reversed:
                    self._draw_label_a(ctx, 0, 0)
                    self._draw_label_b(ctx, SIZE, 0)
                else:
                    self._draw_label_b(ctx, 0, 0)
                    self._draw_label_a(ctx, SIZE, 0)

        ctx.restore()

    def _draw_label_a(self, ctx, x, y):
        self.player.game.processor.draw_label(ctx, self, self.textA, x, y)

    def _draw_label_b(self, ctx, x, y):
        self.player.game.processor.draw_label(ctx, self, self.textB, x, y)