Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/BiblioJAM/JAMBoardEntryText.py
blob: e84571771573e2e8e858914ce25b7914b3e35dc3 (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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#   BiblioJAM (Versión 2.0) - 21/04/2011 - CeibalJAM! - Uruguay
#   JAMBoardEntryText.py por: Flavio Danesse fdanesse@gmail.com
#   https://sites.google.com/site/flaviodanesse/
#   https://sites.google.com/site/sugaractivities/
#   http://codigosdeejemplo.blogspot.com/

import pygame, gc, sys, tempfile
from pygame.locals import *
gc.enable()

import JAMGlobals as VG
from JAMBoard import JAMBoard
from JAMEntryText import JAMEntryText

class JAMBoardEntryText(pygame.sprite.OrderedUpdates):
	def __init__(self):
		pygame.sprite.OrderedUpdates.__init__(self)

		self.posicion= (100,25)
		self.text_buffer= []
		self.tamanio_buffer= 87
		self.tilde= False
		self.tildes= VG.get_tildes_up()
		self.callback_enter= None

		# Colores de la base
		self.colores= {"base":VG.get_blanco(), "bordes":VG.get_negro()}

		self.board= None
		self.entry= None
		self.frame_entry= pygame.sprite.Sprite()

		self.Reconstruye_JAMBoardEntryText(["todo"])

		self.JAMObjects= {"Buffer": self.text_buffer, "Frame": self.frame_entry, "Board": self.board, "EntryText": self.entry}

	# ------------- GETS ------------------------
	def get_tamanio(self):
		return (self.frame_entry.rect.w, self.frame_entry.rect.h)

	# ------------ SETEOS -----------------------
	def set_posicion(self, punto=(0,0)):
		''' Setea la posición de JAMBoardEntryText. '''
		if type(punto) == tuple and type(punto[0]) == int and type(punto[1]) == int:
			self.posicion= punto
			self.frame_entry.rect.x, self.frame_entry.rect.y= self.posicion
			x,y= (self.posicion[0], self.posicion[1] + self.frame_entry.rect.h - self.board.grosor_borde1/2)
			self.board.set_posicion(punto= (x,y))
			centro= (self.frame_entry.rect.centerx, self.frame_entry.rect.centery)
			x,y= (centro[0]-self.entry.frame.rect.w/2, centro[1]-self.entry.frame.rect.h/2)
			self.entry.set_posicion(punto= (x, y))

	def set_center(self, punto= None):
		''' Centra JAMBoardEntryText en el punto indicado. '''
		w,h= (0,0)
		if not punto or type(punto) != tuple or type(punto[0]) != int or type(punto[1]) != int:
			w,h= (pygame.display.Info().current_w, pygame.display.Info().current_h)
			posicion= (w/2-self.frame_entry.rect.w/2, h/2-self.frame_entry.rect.h/2-self.board.frame.rect.h/2)
			self.set_posicion(punto=posicion)
		elif type(punto) == tuple and type(punto[0]) == int and type(punto[1]) == int:
			posicion= (punto[0]-self.frame_entry.rect.w/2, punto[1]-self.frame_entry.rect.h/2-self.board.frame.rect.h/2)
			self.set_posicion(punto=posicion)

	def  set_estilo(self, colorbase, colorborde, colorcara):
		''' Setea los colores del control a partir de un estilo predefinido. '''
		self.board.set_estilo("todos", colorbase, colorborde, colorcara)
		self.entry.set_entry(tipo_letra=None, tamanio_letra=None, color_texto=None, color_fondo=colorborde)
		self.Reconstruye_JAMBoardEntryText(["estilo"])

	def connect_callback_enter(self, callback):
		''' Conecta click sobre el botón enter a una función que recibirá el texto en el buffer de JAMBoardEntryText.'''
		self.callback_enter= callback
		
	# ------------ CONSTRUCCIÓN -----------------------
	def Reconstruye_JAMBoardEntryText(self, cambios):
		'''Reconstruye JAMBoardEntryText cuando se setean valores en él. '''
		if "todo" in cambios:
			self.board= JAMBoard()
			self.entry= JAMEntryText()
			self.frame_entry.image= self.get_frame_entry()
			self.frame_entry.rect= self.frame_entry.image.get_rect()

			self.add(self.frame_entry)
			self.add(self.board)
			self.add(self.entry)

			# Reconectando Click sobre los botones de los JAMBoardTypes
			self.board.board_simbolos.set_callback(self.handle_click)
			self.board.board_letras.set_callback(self.handle_click)
			self.board.board_numeros.set_callback(self.handle_click)
			self.board.board_matematicas.set_callback(self.handle_click)
			self.board.board_otros.set_callback(self.handle_click)
			self.board.board_especiales.set_callback(self.handle_click)

			self.set_posicion(punto=self.posicion)
		
		if "estilo" in cambios:
			self.frame_entry.image= self.get_frame_entry()
			self.set_posicion(punto=self.posicion)

	def get_frame_entry(self):
		w= self.board.frame.rect.w - (self.board.grosor_borde1 + self.board.separador1)*2
		h= self.entry.frame.rect.h + (self.board.grosor_borde2 + self.board.separador2)*2
		contenedor1= VG.get_Rectangulo(self.board.colores["base"], (w,h))
		contenedor1= VG.get_my_surface_whit_border(contenedor1, self.board.colores["bordes"], self.board.grosor_borde2)
		a,b,w,h= contenedor1.get_rect()
		w += (self.board.grosor_borde1 + self.board.separador1)*2
		h += (self.board.grosor_borde1 + self.board.separador1)*2
		contenedor2= VG.get_Rectangulo(self.board.colores["base"], (w,h))
		contenedor2= VG.pegar_imagenes_centradas(contenedor1, contenedor2)
		contenedor2= VG.get_my_surface_whit_border(contenedor2, self.board.colores["bordes"], self.board.grosor_borde1)
		return contenedor2
	# ------------ CONSTRUCCIÓN -----------------------
	
	# ----------- DETECTOR DE EVENTOS ----------------
	def handle_click(self, boton=None):
		''' Detecta los click sobre los botones devolviendo la letra o simbolo que el mismo contiene. '''
		''' Correcciones del 29 de mayo de 2011 tras modificar callback de JAMButton pasandose a si mismo en la llamada. '''
		'''
		posicion = pygame.mouse.get_pos()
		for boton in self.board.board_letras.botones:
			if boton.rect.collidepoint(posicion):
				texto= boton.get_text()
				if self.tilde:
					if texto== "A":
						texto= self.tildes[0]
					elif texto== "E":
						texto= self.tildes[1]
					elif texto== "I":
						texto= self.tildes[2]
					elif texto== "O":
						texto= self.tildes[3]
					elif texto== "U":
						texto= self.tildes[4]
				
				self.text_buffer.append( texto )
				self.tilde= False
				return self.set_bufferentry() '''
		'''
		for boton in self.board.board_numeros.botones:
			if boton.rect.collidepoint(posicion):
				self.text_buffer.append( boton.get_text() )
				self.tilde= False
				return self.set_bufferentry()'''
		'''
		for boton in self.board.board_simbolos.botones:
			if boton.rect.collidepoint(posicion):
				self.text_buffer.append( boton.get_text() )
				self.tilde= False
				return self.set_bufferentry()'''
		'''
		for boton in self.board.board_otros.botones:
			if boton.rect.collidepoint(posicion):
				self.text_buffer.append( boton.get_text() )
				self.tilde= False
				return self.set_bufferentry()'''
		'''
		for boton in self.board.board_matematicas.botones:
			if boton.rect.collidepoint(posicion):
				self.text_buffer.append( boton.get_text() )
				self.tilde= False
				return self.set_bufferentry()'''
		'''
		for boton in self.board.board_especiales.botones:
			if boton.rect.collidepoint(posicion):
				texto= boton.get_text()
				if texto== "Espacio":
				# agrega un espacio en el buffer
					self.text_buffer.append( " " )
					self.tilde= False
					return self.set_bufferentry()

				elif texto== "Borrar":
				# Borra el último caracter ingresado
					if len(self.text_buffer) <= 1:
						self.text_buffer= [ " " ]
					else:
						self.text_buffer= self.text_buffer[0:-1]
					self.tilde= False
					return self.set_bufferentry()

				elif texto== "Enter":
				# Llama a la función conectada al click del botón enter, pasandole como parámetro el texto en el buffer
					self.tilde= False
					if self.callback_enter:
						buf= ""
						for x in self.text_buffer:
							buf += x
						return self.callback_enter(buf)

				elif texto== "´":
					self.tilde= True '''

		if boton in self.board.board_letras.botones:
			texto= boton.get_text()
			if self.tilde:
				if texto== "A":
					texto= self.tildes[0]
				elif texto== "E":
					texto= self.tildes[1]
				elif texto== "I":
					texto= self.tildes[2]
				elif texto== "O":
					texto= self.tildes[3]
				elif texto== "U":
					texto= self.tildes[4]
		
			self.text_buffer.append( texto )
			self.tilde= False
			return self.set_bufferentry()

		elif boton in self.board.board_numeros.botones:
			self.text_buffer.append( boton.get_text() )
			self.tilde= False
			return self.set_bufferentry()

		elif boton in self.board.board_simbolos.botones:
			self.text_buffer.append( boton.get_text() )
			self.tilde= False
			return self.set_bufferentry()

		elif boton in self.board.board_otros.botones:
			self.text_buffer.append( boton.get_text() )
			self.tilde= False
			return self.set_bufferentry()

		elif boton in self.board.board_matematicas.botones:
			self.text_buffer.append( boton.get_text() )
			self.tilde= False
			return self.set_bufferentry()

		elif boton in self.board.board_especiales.botones:
			texto= boton.get_text()
			if texto== "Espacio":
			# agrega un espacio en el buffer
				self.text_buffer.append( " " )
				self.tilde= False
				return self.set_bufferentry()

			elif texto== "Borrar":
			# Borra el último caracter ingresado
				if len(self.text_buffer) <= 1:
					self.text_buffer= [ " " ]
				else:
					self.text_buffer= self.text_buffer[0:-1]
				self.tilde= False
				return self.set_bufferentry()

			elif texto== "Enter":
			# Llama a la función conectada al click del botón enter, pasandole como parámetro el texto en el buffer
				self.tilde= False
				if self.callback_enter:
					buf= ""
					for x in self.text_buffer:
						buf += x
					return self.callback_enter(buf)

			elif texto== "´":
				self.tilde= True
				

	def set_bufferentry(self):
		if len(self.text_buffer) < self.tamanio_buffer:
			buf= ""
			for x in self.text_buffer:
				buf += x
			self.entry.set_buffer(buf)
			self.set_posicion(punto=self.posicion)

	def Describe(self):
		''' Describe la Estructura de Este Control. '''
		estructura =  '''
		Estructura JAMBoardEntryText:

			JAMObject:
				Frame
				Text_Buffer
				JAMBoard
				EntryText'''

		print estructura, "\n"
		print "Ejemplo, Configuración actual:\n"
		print "\t", self.JAMObjects.keys(), "\n"
		for k in self.JAMObjects.items():
			print k, "\n"

# ----- FIN DE CLASE JAMBoardEntryText - INICIO DE DEBUG Y EJEMPLO DE LA CLASE -----
class Ejemplo(object):
	def __init__(self):
		self.ventana = None
		self.reloj = None
		self.nivel = "menu_0"

		self.fondo = None
		self.widgets = None

		self.resolucion = (1000,500)

		self.setup()
		self.Run()

	def setup(self):
		pygame.init()
		pygame.display.set_mode(self.resolucion , 0, 0)
		pygame.display.set_caption("Ejemplo")

		self.fondo = self.get_Fondo()

		self.widgets = JAMBoardEntryText()
		self.widgets.connect_callback_enter(self.Imprime_buffer)
		a,b,c= VG.get_estilo_naranja()
		self.widgets.set_estilo(a, b, c)
		self.widgets.set_center(punto= (500,250))

		self.ventana = pygame.display.get_surface()
		self.reloj = pygame.time.Clock()

		pygame.event.set_blocked([JOYAXISMOTION, JOYBALLMOTION, JOYHATMOTION, JOYBUTTONUP, JOYBUTTONDOWN,
					KEYUP, USEREVENT, QUIT, ACTIVEEVENT])
		pygame.event.set_allowed([MOUSEMOTION, MOUSEBUTTONUP, MOUSEBUTTONDOWN, KEYDOWN, VIDEORESIZE, VIDEOEXPOSE])
		pygame.mouse.set_visible(True)

	def Run(self):
		self.ventana.blit(self.fondo, (0,0))
		self.widgets.draw(self.ventana)
		pygame.display.update()

		contador = 0
		while self.nivel == "menu_0":
			self.reloj.tick(35)
	
			cambios=[]
			self.widgets.clear(self.ventana, self.fondo)
			
			if contador == 150:
				# Activa la siguiente línea para provocar cambios en el tipo de botones en JAMBoardEntryText
				#contador= self.ejemplo_cambia_tipo_de_botones()
				# Activa la siguiente línea para provocar cambios de colores en JAMBoardEntryText
				#contador= self.ejemplo_cambia_colors_Board()
				# Activa la siguiente línea para provocar cambios de texto en Botones
				#contador= self.ejemplo_cambia_texto_en_JAMBoardEntryText()
				# Activa la siguiente línea para provocar cambios de posicion en JAMBoardEntryText
				#contador= self.ejemplo_cambia_posicion_de_JAMBoardEntryText()
				# Activa la siguiente línea para desconectar el sonido select sobre los botones
				#self.widgets.set_reconnect_sound(None)
				# Activa la siguiente línea para ver el contenido del buffer de JAMBoardEntryText
				#print self.widgets.text_buffer
				# Activa la siguiente línea para cambiar los estilos de colores en JAMBoardEntryText
				#contador= self.ejemplo_cambia_estilos()
				contador= 0

			self.widgets.update()
			self.handle_event()
			pygame.event.clear()
			cambios.extend ( self.widgets.draw(self.ventana) )
			pygame.display.update(cambios)
			contador += 1

	'''
	def ejemplo_cambia_estilos(self):
		import random
		estilos= [VG.get_estilo_naranja(), VG.get_estilo_gris(), VG.get_estilo_celeste()]
		a,b,c= random.choice(estilos)
		cambios = ["simbolos", "letras", "numeros", "matematicas", "otros", "especiales",
				"simbolos", "letras", "numeros", "matematicas", "otros", "especiales", "todos"]
		tipo = random.choice(cambios)
		self.widgets.set_estilo(tipo,a,b,c)

	def ejemplo_cambia_tipo_de_botones(self):
		import random
		cambios = ["rectangulo", "elipse"]
		tipo = random.choice(cambios)
		self.widgets.set_tipo_button(tipo)

	def ejemplo_cambia_texto_en_JAMBoardEntryText(self):
		import random
		cambios = ["tipo", "tamanio", "color"]
		modificar = random.choice(cambios)
		if modificar == "tipo":
			tipos= ["Arial", "Purisa", "Times New Roman", "Vardana", "Impact", pygame.font.get_default_font()]
			tipo=random.choice(tipos)
			self.widgets.set_text(tipo=random.choice(tipos), tamanio=None, color=None)
		if modificar == "tamanio":
			tamanios= [10,15,20,25]
			tamanio=random.choice(tamanios)
			self.widgets.set_text(tipo=None, tamanio=tamanio, color=None)
		if modificar == "color":
			colores= [(0,0,0,255), (100,100,255,255), (110,25,255,255), (255,125,55,255)]
			color = random.choice(colores)
			self.widgets.set_text(tipo=None, tamanio=None, color=color)
		return 0

	def ejemplo_cambia_colors_Board(self):
		import random
		cambios = ["colorbas", "colorbor", "colorcara"]
		modificar = random.choice(cambios)

		colores= [(10,20,100,255), (128,128,128,255), (255,255,255,255), (200,100,0,255), (240,150,0,255), (255,220,0,255)]
		color=random.choice(colores)

		self.widgets.set_color_base(random.choice(colores), random.choice(colores))
		
		if modificar == "colorbor":
			self.widgets.set_colors_buttons(base=None, bordes=color, cara=None)
		if modificar == "colorbas":
			self.widgets.set_colors_buttons(base=color, bordes=None, cara=None)
		if modificar == "colorcara":
			self.widgets.set_colors_buttons(base=None, bordes=None, cara=color)
		return 0
	
	def ejemplo_cambia_posicion_de_JAMBoardEntryText(self):
		import random
		tipos= ["centro", "normal"]
		tipo=random.choice(tipos)
		if tipo == "centro":
			posiciones= [(500,400), None]
			posicion= (random.choice(posiciones))
			self.widgets.set_center(punto= posicion)
		elif tipo == "normal":
			return
			posiciones= [(10,20),(25,25),(50,20), (85,100), (100,150)]
			posicion=random.choice(posiciones)
			self.widgets.set_posicion(punto=posicion)
		return 0 '''

	def Imprime_buffer(self, buf):
		print buf
		self.salir()

	def get_Fondo(self):
		superficie = pygame.Surface( self.resolucion, flags=HWSURFACE )
		superficie.fill(VG.get_gris1())
		return superficie

	def handle_event(self):
		for event in pygame.event.get():
			if event.type == pygame.KEYDOWN:
				self.salir()
		pygame.event.clear()

	def salir(self):
		print "\n"
		self.widgets.Describe()
		pygame.quit()
		sys.exit()



if __name__ == "__main__":
	Ejemplo()