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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
import cairo
import math
import random
from gettext import gettext as _
from sugar3.graphics import style
import dominoview
M_PI = 3.14159265358979323846
class PieceProcessorMathSimple:
"""
Este es el primer ejemplo de PieceProccesor
Altera los valores de textA y textB de la pieza y la dibuja
"""
def get_name(self):
return _("Simple")
def alter_labels(self, pieces):
for piece in pieces:
# altero textA
if random.random() > .5:
r = int(random.random() * 10)
if r < piece.a:
piece.textA = str(piece.a - r) + "+" + str(r)
else:
piece.textA = str(r) + "-" + str(r - piece.a)
# altero textB
if random.random() > .5:
r = int(random.random() * 10)
if r < piece.b:
piece.textB = str(piece.b - r) + "+" + str(r)
else:
piece.textB = str(r) + "-" + str(r - piece.b)
def draw_label(self, ctx, piece, text, xIni, yIni):
stroke_r, stroke_g, stroke_b = 0, 0, 0
alpha = 1
if piece.player.color is not None:
xocolor = piece.player.color
stroke_r, stroke_g, stroke_b, alpha = style.Color(
xocolor.get_stroke_color(), 1.0).get_rgba()
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(20)
x_bearing, y_bearing, width, height, x_advance, y_advance = \
ctx.text_extents(text)
x = 0.5 - (width / 2 + x_bearing)
y = 0.5 - (height / 2 + y_bearing)
ctx.move_to(xIni + x + (dominoview.SIZE / 2),
yIni + y + (dominoview.SIZE / 2))
ctx.show_text(text)
class PieceProcessorProductTable:
def __init__(self, n):
self.product = n
def get_name(self):
return _("Table of") + " " + str(self.product)
"""
Este es un procesor para las tablas de multiplicar
Altera los valores de textA y textB de la pieza y la dibuja
"""
def alter_labels(self, pieces):
for piece in pieces:
# altero textA
if (random.random() > .5):
piece.textA = str((piece.a + 2) * self.product)
else:
piece.textA = str((piece.a + 2)) + "*" + str(self.product)
# altero textB
if (random.random() > .5):
piece.textB = str((piece.b + 2) * self.product)
else:
piece.textB = str((piece.b + 2)) + "*" + str(self.product)
def draw_label(self, ctx, piece, text, xIni, yIni):
stroke_r, stroke_g, stroke_b = 0, 0, 0
alpha = 1
if piece.player.color is not None:
xocolor = piece.player.color
stroke_r, stroke_g, stroke_b, alpha = style.Color(
xocolor.get_stroke_color(), 1.0).get_rgba()
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(20)
x_bearing, y_bearing, width, height, x_advance, y_advance = \
ctx.text_extents(text)
x = 0.5 - (width / 2 + x_bearing)
y = 0.5 - (height / 2 + y_bearing)
ctx.move_to(xIni + x + (dominoview.SIZE / 2),
yIni + y + (dominoview.SIZE / 2))
ctx.show_text(text)
class PieceProcessorFractions:
"""
Este es un PieceProccesor que dibuja fracciones en forma numerica y
grafica vamos a trabajar con:
0
1/6
2/6 = 1/3
3/6 = 1/2 = 2/4
4/6 = 2/3
5/6
6/6 = 1 = 2/2 = 3/3
A su vez se puede mostrar en forma grafica o en forma numerica
textA lo voy a representar siempre numericamente y textB lo voy
a representar graficamente
"""
def get_name(self):
return _("Fractions")
def alter_labels(self, pieces):
for piece in pieces:
# altero textA
piece.textA = self.alter_label(piece.a)
# pongo una G al comienzo para saber
# que las tengo que representar graficamente
piece.textB = "G" + self.alter_label(piece.b)
def alter_label(self, val):
if val == 0:
return "0"
elif val == 1:
return "1/6"
elif val == 2:
if random.random() > .5:
return "2/6"
else:
return "1/3"
elif val == 3:
rand = random.random()
if rand < .33:
return "3/6"
elif rand < .66:
return "1/2"
else:
return "2/4"
elif val == 4:
if random.random() > .5:
return "4/6"
else:
return "2/3"
elif val == 5:
return "5/6"
elif val == 6:
rand = random.random()
if rand < .25:
return "6/6"
elif rand < .5:
return "3/3"
elif rand < .75:
return "2/2"
else:
return "1"
def draw_label(self, ctx, piece, text, xIni, yIni):
stroke_r, stroke_g, stroke_b = 0, 0, 0
border_r, border_g, border_b = 0.5, 0.5, 0.5
alpha = 1
if piece.player.color is not None:
xocolor = piece.player.color
stroke_r, stroke_g, stroke_b, alpha = style.Color(
xocolor.get_stroke_color(), 1.0).get_rgba()
ctx.set_source_rgb(stroke_r, stroke_g, stroke_b)
if (text[0] != "G"):
ctx.select_font_face("Sans", cairo.FONT_SLANT_NORMAL,
cairo.FONT_WEIGHT_NORMAL)
ctx.set_font_size(20)
x_bearing, y_bearing, width, height, x_advance, y_advance = \
ctx.text_extents(text)
x = 0.5 - (width / 2 + x_bearing)
y = 0.5 - (height / 2 + y_bearing)
ctx.move_to(xIni + x + (dominoview.SIZE / 2),
yIni + y + (dominoview.SIZE / 2))
ctx.show_text(text)
else:
text = text[1:]
xCenter = xIni + (dominoview.SIZE / 2)
yCenter = yIni + (dominoview.SIZE / 2)
radio = 22
ctx.set_line_width(2)
if (text == "0") or (text == "1"):
ctx.move_to(xCenter + radio, yCenter)
ctx.arc(xCenter, yCenter, radio, 0, 2 * M_PI)
if text == "0":
ctx.stroke()
if text == "1":
ctx.fill_preserve()
else:
numerador = int(text[0])
denominador = int(text[2])
ctx.move_to(xCenter, yCenter)
angulo_porcion = 2 * M_PI / denominador
angulo_inicial = 0
# relleno las fracciones del numerador
for n in range(0, numerador):
xIni = math.cos(angulo_inicial) * radio
yIni = math.sin(angulo_inicial) * radio
ctx.line_to(xCenter + xIni, yCenter + yIni)
ctx.arc(xCenter, yCenter, radio, angulo_inicial,
angulo_inicial + angulo_porcion)
ctx.line_to(xCenter, yCenter)
ctx.close_path()
ctx.set_source_rgb(stroke_r, stroke_g, stroke_b)
ctx.fill()
angulo_inicial = angulo_inicial + angulo_porcion
# pinto los bordes
ctx.move_to(xCenter, yCenter)
angulo_porcion = 2 * M_PI / denominador
angulo_inicial = 0
ctx.set_source_rgb(border_r, border_g, border_b)
for n in range(0, denominador):
xIni = math.cos(angulo_inicial) * radio
yIni = math.sin(angulo_inicial) * radio
ctx.line_to(xCenter + xIni, yCenter + yIni)
ctx.arc(xCenter, yCenter, radio, angulo_inicial,
angulo_inicial + angulo_porcion)
ctx.line_to(xCenter, yCenter)
ctx.close_path()
ctx.stroke()
angulo_inicial = angulo_inicial + angulo_porcion
class PieceProcessorPoints:
"""
Este es un PieceProccesor que dibuja los valores con puntos
Es interesante ver como seria un caso en
el que no sea texto lo que pongamos sino imagenes
"""
def get_name(self):
return _("Points")
def alter_labels(self, pieces):
pass
def draw_label(self, ctx, piece, text, xIni, yIni):
stroke_r, stroke_g, stroke_b = 0, 0, 0
alpha = 1
if piece.player.color is not None:
xocolor = piece.player.color
stroke_r, stroke_g, stroke_b, alpha = style.Color(
xocolor.get_stroke_color(), 1.0).get_rgba()
ctx.set_source_rgb(stroke_r, stroke_g, stroke_b)
xCenter = xIni + (dominoview.SIZE / 2)
yCenter = yIni + (dominoview.SIZE / 2)
separa = (dominoview.SIZE / 4)
radio = 4
#
# *1 *2
# *3 *4 *5
# *6 *7
#
# 1
if text in ("2", "3", "4", "5", "6"):
ctx.arc(xCenter-separa, yCenter - separa, radio, 0, 2 * M_PI)
ctx.fill()
# 2
if text in ("4", "5", "6"):
ctx.arc(xCenter+separa, yCenter - separa, radio, 0, 2 * M_PI)
ctx.fill()
# 3
if text == "6":
ctx.arc(xCenter - separa, yCenter, radio, 0, 2 * M_PI)
ctx.fill()
# 4
if text in ("1", "3", "5"):
ctx.arc(xCenter, yCenter, radio, 0, 2 * M_PI)
ctx.fill()
# 5
if text == "6":
ctx.arc(xCenter + separa, yCenter, radio, 0, 2 * M_PI)
ctx.fill()
# 6
if text in ("4", "5", "6"):
ctx.arc(xCenter-separa, yCenter+separa, radio, 0, 2 * M_PI)
ctx.fill()
# 7
if text in ("2", "3", "4", "5", "6"):
ctx.arc(xCenter + separa, yCenter + separa, radio, 0, 2 * M_PI)
ctx.fill()
|