Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/VennActivity.py
blob: c871bca7eb7e9678d41c8f269417d8a0ce93d83f (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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
* Se crea la interfaz.
* Se cargan los tipos de conjuntos en el combo de la barra lateral.
* Cuando se selecciona uno de esos tipos, se emite "SET_CONJUNTOS", entonces:
	* Se borran todos los objetos del mapa, de los conjuntos y de la barra lateral.
	* Se cargan en la barra lateral todos los objetos para el nuevo tipo de conjunto.
	* Se setea en el combo de cada conjunto, los conjuntos posibles para el tipo seleccionado en la barra lateral.
	* Se selecciona un item en el combo de cada conjunto en el mapa.

* Cuando se selecciona una opcion en el combo de uno de los conjuntos, se emite "CHANGE_CONJUNTO", entonces:
	* Todos los objetos vuelven a la barra lateral.
	* El otro conjunto, selecciona un item de su combo distinto al que tiene el combo que emitió la señal.

* Cuando se arrastra un objeto desde la barra lateral hasta el mapa:
	* Verificamos si la región a la que se intenta arrastrar se corresponde con un conjunto, con ambos, o con ninguno.
	* Cuando se produce el arrastre, se emite "LOGRO", si su valor es True, se felicita, de lo contrario, se advierte.
'''

import gtk, pygtk, os, sys, gobject, commands, pygame

from Barras import BarraSuperior, BarraLateral
from Mapa import Mapa

import Globals as G

pygame.mixer.init()

class VentanaGTK(gtk.Window):
	def __init__(self):
		gtk.Window.__init__(self, type=gtk.WINDOW_TOPLEVEL)
		self.resolucion= (self.get_screen().get_width(),self.get_screen().get_height())
		self.set_title("Venn Activity")
		#self.fullscreen() # Sin Barra de Título
		self.set_default_size(self.resolucion[0],self.resolucion[1])
		#self.set_decorated(False)
		self.connect('delete-event', self.salir)

		self.panel= Panel()
		self.add(self.panel)

		self.show_all()
		self.realize()

		self.imagen1= gtk.gdk.pixbuf_new_from_file_at_size(os.path.join(G.Imagenes, "dedo1.png"), 150, 150)
		self.imagen2= gtk.gdk.pixbuf_new_from_file_at_size(os.path.join(G.Imagenes, "dedo2.png"), 150, 150)
		self.sonido1= pygame.mixer.Sound(os.path.join(G.Sonidos,"si.ogg"))
		self.sonido2= pygame.mixer.Sound(os.path.join(G.Sonidos,"no.ogg"))

		self.aciertos= 0
		self.errores= 0

		self.panel.set_up()
		self.panel.connect("LOGRO", self.logro)
		self.panel.connect("RESET", self.reset)

	def reset(self, widget, dato):
		self.aciertos= 0
		self.errores= 0
		self.panel.label.set_text("Aciertos: 0    Errores: 0")

	def logro(self, widget, dato):
		imagen= gtk.Image()
	        ventana = gtk.Window(gtk.WINDOW_POPUP)
		if dato:
			imagen.set_from_pixbuf(self.imagen1)
			self.sonido1.play()
			self.aciertos += 1
		else:
			imagen.set_from_pixbuf(self.imagen2)
			self.sonido2.play()
			self.errores += 1
		ventana.set_position(gtk.WIN_POS_MOUSE)
		ventana.add(imagen)
		ventana.show_all()
		ventana.present()
		gobject.timeout_add(1500, self.close, ventana)
		self.panel.label.set_text("Aciertos: %s    Errores: %s" % (self.aciertos, self.errores))

	def close(self, ventana):
		ventana.destroy()
		return False

	def salir(self, widget=None, extras= None):
		sys.exit()

targets = [("Moviendo", gtk.TARGET_SAME_APP, 1)]
class Panel(gtk.HPaned):
	__gsignals__ = {"LOGRO":(gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, (gobject.TYPE_BOOLEAN,)),
	"RESET":(gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, (gobject.TYPE_BOOLEAN,))}
	def __init__(self):
		gtk.HPaned.__init__(self)
		self.mapa= Mapa()
		self.barra_lateral= BarraLateral()

		scroll= gtk.ScrolledWindow()
		scroll.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
		scroll.add_with_viewport (self.mapa)

		caja= gtk.VBox()
		barramenu= gtk.HBox()
		self.label= gtk.Label("Aciertos: 0   Errores: 0")
		barramenu.pack_end(self.label, False, False)
		caja.pack_start(barramenu, False, False)
		caja.pack_start(scroll, True, True)
		self.pack1(caja, resize=True, shrink=True)
		self.pack2(self.barra_lateral, resize=False, shrink=True)

		self.barra_lateral.connect("SET_CONJUNTOS", self.set_conjuntos) # cuando se selecciona un tipo de conjuntos
		self.mapa.connect("RESET", self.reset) # cuando se selecciona una categoría en un conjunto

	def reset(self, mapa, objetos):
		for objeto in objetos:
			objeto.marca= False
			objeto.reparent(self.barra_lateral.objetos)
		self.emit("RESET", True)

	def set_up(self):
		''' Estado inicial de la aplicación.'''
		self.mapa.set_up()
		self.barra_lateral.set_up()
		self.mapa.drag_dest_set(gtk.DEST_DEFAULT_ALL, targets, gtk.gdk.ACTION_MOVE)
		self.mapa.connect("drag-drop", self.drag_drop)

	def set_conjuntos(self, barra, datos):
		''' Cuando la barra lateral emite "SET_CONJUNTOS", se borran los objetos
		del mapa y se setean las opciones en combos de los conjuntos del mapa.'''
		self.mapa.vaciar()
		self.mapa.set_opciones_conjuntos(datos)
		for child in self.barra_lateral.objetos.get_children():
			self.barra_lateral.objetos.remove(child) # remover objetos actuales
			child.destroy()
		for objeto in G.FUNCIONESOBJETOS[self.barra_lateral.tipos_conjuntos.get_active_text()]():
			self.barra_lateral.objetos.pack_start(objeto, False, False, 5) # empaquetar objetos nuevos
		for child in self.barra_lateral.objetos.get_children():
			child.drag_source_set(gtk.gdk.BUTTON1_MASK, targets, gtk.gdk.ACTION_MOVE)
			child.drag_source_set_icon_pixbuf(child.imagen.pixbuf)
		self.emit("RESET", True)

	def drag_drop(self, destino, drag_context, x, y, cosas):
		objeto= drag_context.get_source_widget()
		# verificar pertenencia a conjuntos.
		isA= self.verificar_pertenencia(self.mapa.conjunto1.opciones.get_active_text(), objeto)
		isB= self.verificar_pertenencia(self.mapa.conjunto2.opciones.get_active_text(), objeto)

		punto= gtk.gdk.Rectangle(x,y,1,1)
		#print self.mapa.conjunto2.get_allocation().intersect(punto)
		if self.mapa.conjunto1.get_allocation().intersect(punto) and self.mapa.conjunto2.get_allocation().intersect(punto):
			inA= self.verificar_punto_en_conjunto(x, y, self.mapa.conjunto1)
			inB= self.verificar_punto_en_conjunto(x, y, self.mapa.conjunto2)

			if inA and inB: # Drop en área comun a A y B
				if isA and isB:
					self.reparent_on_map_comun(objeto, x, y)
					#print "Correcto 1"
					if not objeto.marca:
						objeto.marca= True
						self.emit("LOGRO", True)
				else:
					#print "Incorrecto"
					if not objeto.marca:
						self.emit("LOGRO", False)

			elif inA: # Drop en A
				if isA and not isB:
					self.reparent_on_conjunto(objeto, self.mapa.conjunto1, x, y)
					#print "Correcto 2"
					if not objeto.marca:
						objeto.marca= True
						self.emit("LOGRO", True)
				else:
					#print "Incorrecto"
					if not objeto.marca:
						self.emit("LOGRO", False)

			elif inB: # Drop en B
				if not isA and isB:
					self.reparent_on_conjunto(objeto, self.mapa.conjunto2, x, y)
					#print "Correcto 3"
					if not objeto.marca:
						objeto.marca= True
						self.emit("LOGRO", True)
				else:
					#print "Incorrecto"
					if not objeto.marca:
						self.emit("LOGRO", False)

			else: # Drop en mapa
				if not isA and not isB:
					self.reparent_on_map(objeto, x, y)
					#print "Correcto 4"
					if not objeto.marca:
						objeto.marca= True
						self.emit("LOGRO", True)
				else:
					#print "Incorrecto"
					if not objeto.marca:
						self.emit("LOGRO", False)

		elif self.mapa.conjunto1.get_allocation().intersect(punto): # Drop en A
			if self.verificar_punto_en_conjunto(x, y, self.mapa.conjunto1):

				if isA and not isB:
					self.reparent_on_conjunto(objeto, self.mapa.conjunto1, x, y)
					#print "Correcto 5"
					if not objeto.marca:
						objeto.marca= True
						self.emit("LOGRO", True)
				else:
					#print "Incorrecto"
					if not objeto.marca:
						self.emit("LOGRO", False)

			else: # Drop en mapa
				if not isA and not isB:
					self.reparent_on_map(objeto, x, y)
					#print "Correcto 6"
					if not objeto.marca:
						objeto.marca= True
						self.emit("LOGRO", True)
				else:
					#print "Incorrecto"
					if not objeto.marca:
						self.emit("LOGRO", False)

		elif self.mapa.conjunto2.get_allocation().intersect(punto): # Drop en B
			if self.verificar_punto_en_conjunto(x, y, self.mapa.conjunto2):

				if not isA and isB:
					self.reparent_on_conjunto(objeto, self.mapa.conjunto2, x, y)
					#print "Correcto 7"
					if not objeto.marca:
						objeto.marca= True
						self.emit("LOGRO", True)
				else:
					#print "Incorrecto"
					if not objeto.marca:
						self.emit("LOGRO", False)

			else: # Drop en mapa
				if not isA and not isB:
					self.reparent_on_map(objeto, x, y)
					#print "Correcto 8"
					if not objeto.marca:
						objeto.marca= True
						self.emit("LOGRO", True)
				else:
					#print "Incorrecto"
					if not objeto.marca:
						self.emit("LOGRO", False)

		else: # Drop en mapa
				if not isA and not isB:
					self.reparent_on_map(objeto, x, y)
					#print "Correcto 9"
					if not objeto.marca:
						objeto.marca= True
						self.emit("LOGRO", True)
				else:
					#print "Incorrecto"
					if not objeto.marca:
						self.emit("LOGRO", False)
		return True

	def verificar_punto_en_conjunto(self, x, y, conjunto):
		''' verifica si un punto se encuentra dentro de un conjunto.'''
		xpixbuf= x-conjunto.get_allocation()[0]
		ypixbuf= y-conjunto.get_allocation()[1]
		if xpixbuf < 0 or ypixbuf < 0: return False
		if xpixbuf >= conjunto.get_allocation()[2] or ypixbuf >= conjunto.get_allocation()[3]: return False
		color= conjunto.imagen.pixbuf.get_pixels_array()[xpixbuf][ypixbuf]
		if color[3]:
			return True
		else:
			return False

	def verificar_pertenencia(self, opcion_en_conjunto, objeto):
		''' Verifica si un objeto puede pertenecer a un conjunto.'''
		if opcion_en_conjunto in objeto.conjuntos:
			return True
		else:
			return False

	def reparent_on_conjunto(self, objeto, conjunto, x, y):
		''' Hace drop en un conjunto.'''
		xpixbuf= x-conjunto.get_allocation()[0]
		ypixbuf= y-conjunto.get_allocation()[1]
		wpixbuf= conjunto.get_allocation()[2]
		hpixbuf= conjunto.get_allocation()[3]

		xx, yy, ww, hh= objeto.get_allocation()

		if conjunto == self.mapa.conjunto2:
			if xpixbuf <= wpixbuf/2 and ypixbuf <= hpixbuf/2:
			# cuando está arriba y a la izquierda
				newx= x
				while self.verificar_punto_en_conjunto(newx, y+hh, self.mapa.conjunto1):
					newx+= 1
				xpixbuf= newx-conjunto.get_allocation()[0]

			elif xpixbuf > wpixbuf/2 and ypixbuf <= hpixbuf/2:
			# cuando está arriba y a la derecha
				newx= x
				if newx+ww >= conjunto.get_allocation()[0]+conjunto.get_allocation()[2]:
					newx= conjunto.get_allocation()[0]+conjunto.get_allocation()[2]-ww-1
				while not self.verificar_punto_en_conjunto(newx+ww, y, conjunto):
					newx-= 1
				xpixbuf= newx-conjunto.get_allocation()[0]

			elif xpixbuf <= wpixbuf/2 and ypixbuf > hpixbuf/2:
			# cuando está abajo y a la izquierda
				newy= y
				if newy+hh >= conjunto.get_allocation()[1]+conjunto.get_allocation()[3]:
					newy= conjunto.get_allocation()[1]+conjunto.get_allocation()[3]-hh-1
				while not self.verificar_punto_en_conjunto(x, newy+hh, conjunto):
					newy-= 1
				ypixbuf= newy-conjunto.get_allocation()[1]
				newx= x
				while self.verificar_punto_en_conjunto(newx, newy, self.mapa.conjunto1):
					newx+= 1
				xpixbuf= newx-conjunto.get_allocation()[0]
			
			elif xpixbuf > wpixbuf/2 and ypixbuf > hpixbuf/2:
			# cuando está abajo y a la derecha
				newx= x
				if newx+ww >= conjunto.get_allocation()[0]+conjunto.get_allocation()[2]:
					newx= conjunto.get_allocation()[0]+conjunto.get_allocation()[2]-ww-1
				while not self.verificar_punto_en_conjunto(newx+ww, y, conjunto):
					newx-= 1
				xpixbuf= newx-conjunto.get_allocation()[0]

				newy= y
				if newy+hh >= conjunto.get_allocation()[1]+conjunto.get_allocation()[3]:
					newy= conjunto.get_allocation()[1]+conjunto.get_allocation()[3]-hh-1
				while not self.verificar_punto_en_conjunto(newx+ww, newy+hh, conjunto):
					newy-= 1
				ypixbuf= newy-conjunto.get_allocation()[1]

		elif conjunto == self.mapa.conjunto1:
			if ypixbuf <= hpixbuf/2:
			# cuando está arriba y a la izquierda
				newx= x
				while self.verificar_punto_en_conjunto(newx+ww, y+hh, self.mapa.conjunto2):
					newx-= 1
				xpixbuf= newx-conjunto.get_allocation()[0]

			elif xpixbuf <= wpixbuf/2 and ypixbuf > hpixbuf/2:
			# cuando está abajo y a la izquierda
				newy= y
				if newy+hh >= conjunto.get_allocation()[1]+conjunto.get_allocation()[3]:
					newy= conjunto.get_allocation()[1]+conjunto.get_allocation()[3]-hh-1
				while not self.verificar_punto_en_conjunto(x, newy+hh, conjunto):
					newy-= 1
				ypixbuf= newy-conjunto.get_allocation()[1]
				newx= x
				while self.verificar_punto_en_conjunto(newx+ww, newy, self.mapa.conjunto2):
					newx-= 1
				xpixbuf= newx-conjunto.get_allocation()[0]
			
			elif xpixbuf > wpixbuf/2 and ypixbuf > hpixbuf/2:
			# cuando está abajo y a la derecha
				newy= y
				if newy+hh >= conjunto.get_allocation()[1]+conjunto.get_allocation()[3]:
					newy= conjunto.get_allocation()[1]+conjunto.get_allocation()[3]-hh-1
				while not self.verificar_punto_en_conjunto(x+ww, newy+hh, conjunto):
					newy-= 1
				ypixbuf= newy-conjunto.get_allocation()[1]

				newx= x
				while self.verificar_punto_en_conjunto(newx+ww, newy, self.mapa.conjunto2):
					newx-= 1
				xpixbuf= newx-conjunto.get_allocation()[0]

		if objeto.get_parent() != conjunto:
			objeto.reparent(conjunto)
		conjunto.move(objeto, xpixbuf, ypixbuf)

	def reparent_on_map(self, objeto, x, y):
		'''Hace drop fuera de los conjuntos.'''
		if x <= self.mapa.conjunto2.get_allocation()[0] + self.mapa.conjunto2.get_allocation()[3]:
			x= self.mapa.conjunto2.get_allocation()[0] + self.mapa.conjunto2.get_allocation()[3] + 1
		if objeto.get_parent() != self.mapa:
			objeto.reparent(self.mapa)
		self.mapa.move(objeto, x, y)

	def reparent_on_map_comun(self, objeto, x, y):
		'''Hace drop dentro de ambos conjuntos.'''
		xx, yy, ww, hh= objeto.get_allocation()
		a,b,c,d= self.mapa.conjunto1.get_allocation().intersect(self.mapa.conjunto2.get_allocation())

		newx= x
		newy= y

		if y <= b+d/2:
		# cuando está arriba
			newx= x
			if newx+ww >= a+c:
				newx= a+c-1-ww
			newy= y
			while not self.verificar_punto_en_conjunto(newx+ww, newy, self.mapa.conjunto1):
				newy+= 1

		elif y > b+d/2 and x > a+c/2:
		# cuando está abajo y a la derecha
			newx= x
			if newx+ww >= a+c:
				newx= a+c-1-ww
			newy= y
			while not self.verificar_punto_en_conjunto(newx+ww, newy+hh, self.mapa.conjunto1):
				newy-= 1

		elif y > b+d/2 and x <= a+c/2:
		# cuando está abajo y a la izquierda
			newy= y
			if newy+hh >= b+d:
				newy= b+d-1-hh
			while not self.verificar_punto_en_conjunto(newx, newy+hh, self.mapa.conjunto2):
				newy-= 1
			while not self.verificar_punto_en_conjunto(newx+ww, newy+hh, self.mapa.conjunto1):
				newx-= 1
			while not self.verificar_punto_en_conjunto(newx, newy+hh, self.mapa.conjunto2):
				newy-= 1

		if objeto.get_parent() != self.mapa:
			objeto.reparent(self.mapa)
		self.mapa.move(objeto, newx, newy)

if __name__=="__main__":
	VentanaGTK()
	gtk.main()