Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/dominoview.py
blob: 2317ece0daf58a1e0c07fe9a97079d76c1066d1b (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/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 gi.repository import Gdk
import cairo
from gi.repository import Rsvg

from gettext import gettext as _

import cairoutils

from sugar3.graphics import style

# SIZE Es el ancho de una ficha (y la mitads del largo)
# podemos imaginar el tablero dividido en cuadrados de lado SIZE
SIZE = 60
# Si se quiere usar fichas mas grandes, se puede usar SIZE = 70 y
# cambiar _drawLabel el scale = 3

SCREEN_HEIGHT = Gdk.Screen.height() - style.GRID_CELL_SIZE
SCREEN_WIDTH = Gdk.Screen.width()


class Tile:

    """
    Informacion de cada posicion del tablero
    """

    UP = (0, -1)
    RIGHT = (1, 0)
    DOWN = (0, 1)
    LEFT = (-1, 0)

    def __init__(self, n, p):
        self.n = n
        self.p = p
        self.value = -1
        self.direction = (0, 0)
        # direction is a pair with the x, y increments where to put the next
        # tile. Then when we put the first tile, the start tile direction
        # will be (-1, 0) and the end tile direction will be (1, 0)
        # there are constants defined with the pairs


class DominoTableView():

    """
    Dibuja una grilla sobre la que se van a poner las fichas
    Ademas tiene metodos para saber a que casillero corresponde una
    posicion del mouse o donde ubicar una ficha
    """

    __gtype_name__ = 'DominoTableView'

    def __init__(self, **kwargs):
        self.cantX = int(SCREEN_WIDTH / SIZE) - 1
        self.cantY = int((SCREEN_HEIGHT - SIZE * 4) / SIZE)
        self._margin_x = int((SCREEN_WIDTH - SIZE * self.cantX) / 2)
        self._margin_y = SIZE * 2

        self.bottom_limit = self._margin_y + SIZE * self.cantY
        print "Table cantX", self.cantX, "cantY", self.cantY

    def paint(self, ctx):

        # agrego 5,5 para que la grilla quede en la base de las piezas
        # (por la perspectiva)
        alto = 5

        ctx.rectangle(self._margin_x + alto, self._margin_y + alto,
                      SIZE * self.cantX + alto,
                      SIZE * self.cantY + alto)
        ctx.set_source_rgb(204.0/255.0, 204.0/255.0, 204.0/255.0)
        ctx.fill()

        ctx.set_line_width(1)
        ctx.set_source_rgb(1, 1, 0)

        for n in range(0, self.cantY):
            ctx.move_to(self._margin_x + alto,
                        self._margin_y + alto + n * SIZE)
            ctx.line_to(self._margin_x + alto + SIZE * self.cantX,
                        self._margin_y + alto + n * SIZE)
            ctx.stroke()

        for n in range(0, self.cantX):
            ctx.move_to(self._margin_x + n * SIZE + alto,
                        self._margin_y + alto)
            ctx.line_to(self._margin_x + n * SIZE + alto,
                        self.bottom_limit + alto)
            ctx.stroke()

    def show_values(self, ctx, tiles):
        """
        To debug: display the value in every tile off the table matrix
        """
        for n in range(0, self.cantX):
            for p in range(0, self.cantY):
                ctx.move_to(self._margin_x + n * SIZE + SIZE / 4,
                            self._margin_y + (p + 1) * SIZE)
                ctx.set_source_rgb(1, 0, 0)
                ctx.show_text(str(tiles[n][p].value))

    def mark_tile(self, ctx, tile):
        """
        To debug: used to show the position of the start and end tiles
        """
        ctx.set_source_rgb(0, 1, 0)
        ctx.set_line_width(3)
        ctx.rectangle(self._margin_x + tile.n * SIZE,
                      self._margin_y + tile.p * SIZE,
                      SIZE, SIZE)
        ctx.stroke()

    def get_tile_position(self, n, p):
        return self._margin_x + n * SIZE, self._margin_y + p * SIZE

    def get_tile_coord(self, x, y):
        return (x - self._margin_x) / SIZE, (y / SIZE) + 1

    def show_status(self, ctx, text):
        xIni = 10
        yIni = 5

        stroke_r, stroke_g, stroke_b = 0, 0, 0
        alpha = 0.6
        ctx.set_source_rgba(stroke_r, stroke_g, stroke_b, alpha)

        ctx.select_font_face("Sans", cairo.FONT_SLANT_NORMAL,
                             cairo.FONT_WEIGHT_NORMAL)
        ctx.set_font_size(12)
        x_bearing, y_bearing, width, height, x_advance, y_advance = \
            ctx.text_extents(text)

        radio = 16

        cairoutils.draw_round_rect(ctx, xIni, yIni, width + radio * 2,
                                   height * 2, radio)
        ctx.stroke()

        ctx.move_to(xIni + radio + x_bearing, yIni + height)
        ctx.show_text(text)

    def show_scores(self, ctx, score_list):
        alto = 5
        ctx.move_to(alto, alto)

        ctx.rectangle(self._margin_x + alto, 0 + alto,
                      SIZE * self.cantX + alto, SIZE * (self.cantY) + alto)
        ctx.set_source_rgb(204.0 / 255.0, 204.0 / 255.0, 204.0 / 255.0)
        ctx.fill()

        ctx.set_line_width(1)
        ctx.set_source_rgb(1, 1, 0)
        ctx.rectangle(self._margin_x + alto, 0 + alto,
                      SIZE * self.cantX + alto, SIZE * (self.cantY) + alto)
        ctx.stroke()

        altoRenglon = 40

        stroke_r, stroke_g, stroke_b = 0, 0, 0
        ctx.set_source_rgb(stroke_r, stroke_g, stroke_b)

        ctx.select_font_face("Sans", cairo.FONT_SLANT_NORMAL,
                             cairo.FONT_WEIGHT_NORMAL)
        ctx.set_font_size(30)

        x = self._margin_x + 200
        y = altoRenglon * 2
        ctx.move_to(x, y)

        ctx.move_to(x + 300, y)
        ctx.show_text(_("Played"))
        ctx.move_to(x + 450, y)
        ctx.show_text(_("Win"))
        ctx.move_to(x + 600, y)
        ctx.show_text(_("Lost"))
        y = y + altoRenglon
        ctx.move_to(x, y)

        for game_points in score_list:
            ctx.show_text(game_points.name)
            ctx.move_to(x + 350, y)
            ctx.show_text(str(game_points.played))
            ctx.move_to(x + 500, y)
            ctx.show_text(str(game_points.win))
            ctx.move_to(x + 650, y)
            ctx.show_text(str(game_points.lost))
            y = y + altoRenglon
            ctx.move_to(x, y)

    def help(self, ctx):
        altoRenglon = 45
        x = self._margin_x + 20
        y = altoRenglon * 4
        ctx.set_line_width(1)
        ctx.set_source_rgb(1, 1, 0)
        ctx.rectangle(self._margin_x + 10, altoRenglon * 3,
                      SIZE * self.cantX - 10, y + altoRenglon * 4)
        ctx.stroke()

        stroke_r, stroke_g, stroke_b = 0, 0, 0
        ctx.set_source_rgb(stroke_r, stroke_g, stroke_b)

        ctx.select_font_face("Sans", cairo.FONT_SLANT_NORMAL,
                             cairo.FONT_WEIGHT_NORMAL)
        ctx.set_font_size(30)

        ctx.move_to(x, y)
        ctx.show_text(_("You can select a piece with the arrow keys "
                        "and use it with enter."))

        y = y + altoRenglon
        ctx.move_to(x, y)
        ctx.show_text(_("When you selected the piece, you can move it "
                        "to the place "))

        y = y + altoRenglon
        ctx.move_to(x, y)
        ctx.show_text(_("with the arrows , turn it with space and place "
                        "it with enter."))

        y = y + altoRenglon
        ctx.move_to(x, y)
        ctx.show_text(_("If you want use another piece, press escape."))

        y = y + altoRenglon
        ctx.move_to(x + 80, y)
        ctx.show_text(_("You can use the cursors to move too."))

        y = y + altoRenglon
        ctx.move_to(x + 80, y)
        ctx.show_text(_("And use check to select, circle to rotate"))

        y = y + altoRenglon
        ctx.move_to(x + 80, y)
        ctx.show_text(_("and X to use another piece."))

        self.show_svg(ctx, "icons/cursors.svg", x, y - (altoRenglon * 2.75))
        self.show_svg(ctx, "icons/gamekeys.svg", x, y - (altoRenglon * 1.25))

    def show_svg(self, ctx, file_name, x, y):
        h = Rsvg.Handle.new_from_file(file_name)
        surf = cairo.ImageSurface(cairo.FORMAT_ARGB32, 100, 100)
        temp_ctx = cairo.Context(surf)
        h.render_cairo(temp_ctx)
        ctx.translate(x, y)
        ctx.set_source_surface(surf)
        ctx.paint()
        ctx.translate(-x, -y)

    def msg_end_game(self, ctx, win):
        ctx.select_font_face("Sans", cairo.FONT_SLANT_NORMAL,
                             cairo.FONT_WEIGHT_BOLD)
        text = ""
        if (win):
            ctx.set_font_size(100)
            text = _("You win!!!")
        else:
            ctx.set_font_size(60)
            text = _("Sorry, you lost")

        x_bearing, y_bearing, width, height, x_advance, y_advance = \
            ctx.text_extents(text)
        x = (SCREEN_WIDTH - width) / 2
        y = (SCREEN_HEIGHT - height) / 2
        ctx.move_to(x, y)

        ctx.text_path(text)
        ctx.set_source_rgb(0.5, 0.5, 1)
        ctx.fill_preserve()
        ctx.set_source_rgb(0, 0, 0)
        ctx.set_line_width(2)
        ctx.stroke()