Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/BiblioJAM/JAMComboBox.py
blob: b0ec19641b1a55c89915707b75056ad3a30313a8 (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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#   BiblioJAM (Versión 2.0) - 21/05/2011 - CeibalJAM! - Uruguay
#   JAMComboBox.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, gobject, time, datetime, os
from pygame.locals import *
gc.enable()
pygame.font.init()

import JAMGlobals as VG
from JAMButton import JAMButton

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

		self.buttoncaption= None
		self.buttonaction= None
		self.items= {}
		self.buttonsitems= []

		self.limitecaracteres= 40
		self.text= {"tipo": None, "tamanio": None, "color": None, "font_from_file": None}
		self.colores= {"colorbas": None, "colorbor": None, "colorcara": None}

		self.posicion= (0,0)

		self.Reconstruye("todo")

	def Reconstruye(self, cambios):
		''' Reconstruye según cambios.'''
		if "todo" in cambios:
			self.buttoncaption= ButtonCaption(self)
			self.buttonaction= ButtonAction(self)

			self.set_posicion(punto= self.posicion)
			
			self.add(self.buttoncaption)
			self.add(self.buttonaction)
			
	def set_item_inicial(self):
		''' Pone el primer item en el caption del combo. '''
		self.set_item_in_caption(0)

	def set_item_in_caption(self, num):
		''' Pone el item indicado en el caption del combo. '''
		if self.buttonsitems and len(self.buttonsitems) >= num:
			boton_item= self.buttonsitems[num]
			self.buttoncaption.set_text(texto= boton_item.get_text())

	def ordena_items(self):
		''' Todos del mismo tamanio. '''
		self.buttoncaption.set_tamanios(tamanio=(0,0), grosorbor=1, detalle=1, espesor=1)
		self.buttonaction.set_tamanios(tamanio=(0,0), grosorbor=1, detalle=1, espesor=1)
		for item in self.buttonsitems:
			item.set_tamanios(tamanio=(0,0), grosorbor=1, detalle=1, espesor=1)


		ancho, alto= self.buttoncaption.get_tamanio()
		w,h= self.buttonaction.get_tamanio()
		if alto < h: alto= h
		for item in self.buttonsitems:
			ww,hh= item.get_tamanio()
			if ancho < ww: ancho= ww
			if alto < hh: alto= hh

		self.buttoncaption.set_tamanios(tamanio=(ancho,alto), grosorbor=1, detalle=1, espesor=1)
		self.buttonaction.set_tamanios(tamanio=(self.buttonaction.get_tamanio()[0],alto), grosorbor=1, detalle=1, espesor=1)
		for item in self.buttonsitems:
			item.set_tamanios(tamanio=(ancho,alto), grosorbor=1, detalle=1, espesor=1)

		if self.buttoncaption.get_text()== "JAMComboBox":
			self.set_item_inicial()
		self.set_posicion(punto= self.posicion)

	def show_items(self, buttonaction):
		''' Muestra los Items en el Combo. '''
		if self.buttonsitems:
			self.empty()
			self.add(self.buttonsitems)
		else:
			self.buttoncaption.set_text(texto= "JAMComboBox")

	def callback(self, button):
		''' Cuando se hace click sobre un item, la lista se contrae y muestra solo el elegido.
		Luego ejecuta el callback de ese boton.'''
		if button:
			self.empty()
			self.buttoncaption.set_text(texto= button.get_text())
			#self.buttoncaption.set_imagen(origen= button"direccion de archivo" , tamanio=(ancho,alto))
			self.add(self.buttoncaption)
			self.add(self.buttonaction)
			callback= self.items[button]
			if callback: return callback(button)
		else:
		# cuando se borra un item mientras se está mostrando la lista de items
			self.empty()
			self.buttoncaption.set_text(texto= "JAMComboBox")
			self.add(self.buttoncaption)
			self.add(self.buttonaction)

	# ----------- SETEOS ------------------------------
	def add_item(self, item, callback):
		''' Agrega un Item y su callback al Combo. '''
		if len(item) > self.limitecaracteres:
			item= "%s" % (item[:self.limitecaracteres])

		# hay que verificar que el nuevo item no exista ya en el combo.
		button= JAMButton(item, None)
		button.set_text(tipo= self.text["tipo"], tamanio= self.text["tamanio"], color= self.text["color"])
		button.connect(callback= self.callback, sonido_select= None)
		if self.colores["colorbas"] and self.colores["colorbor"] and self.colores["colorcara"]:
			a= self.colores["colorbas"]
			b= self.colores["colorbor"]
			c= self.colores["colorcara"]
			button.set_colores(colorbas= a, colorbor= b, colorcara= c)

		if self.text["font_from_file"]:
			button.set_font_from_file(self.text["font_from_file"], tamanio= self.text["tamanio"])

		self.items[button]= callback

		self.buttonsitems.append(button)
		self.ordena_items()

	def remove_item(self, item):
		''' Elimina un item del combo segun caption. '''
		for boton in self.buttonsitems:
			if boton.get_text()== item:
				del self.items[boton]
				self.buttonsitems.remove(boton)
				boton.kill()
				if not self.buttonsitems:
					self.callback(None)
				self.ordena_items()

	def remove_item_index(self, index):
		''' Elimina un item del combo segun indice. '''
		if len(self.buttonsitems) > 0 and index < len(self.buttonsitems):
			boton= self.buttonsitems[index]
			del self.items[boton]
			self.buttonsitems.remove(boton)
			boton.kill()
			if not self.buttonsitems:
				self.callback(None)
			self.ordena_items()

	def clear_items(self):
		''' Elimina todos los items en el combo.'''
		for boton in self.buttonsitems:
			self.items.clear()
			#self.buttonsitems.remove(boton)
			self.buttonsitems= []
			boton.kill()
			if not self.buttonsitems:
				self.callback(None)
			self.ordena_items()

	def connect_item(self, item, callback, sound):
		''' Conecta un item a una función. '''
		for boton in self.buttonsitems:
			if boton.get_text()== item:
				self.items[boton]= callback
				boton.connect(callback=callback, sonido_select=sound)

	def set_colores(self, colorbas= None, colorbor= None, colorcara= None):
		''' Setea los colores de todos los items. '''
		if colorbas and type(colorbas)== tuple:
			self.colores["colorbas"]= colorbas
		if colorbor and type(colorbor)== tuple:
			self.colores["colorbor"]= colorbor
		if colorcara and type(colorcara)== tuple:
			self.colores["colorcara"]= colorcara

		self.buttoncaption.set_colores(colorbas=colorbas, colorbor=colorbor, colorcara=colorcara)
		self.buttonaction.set_colores(colorbas=colorbas, colorbor=colorbor, colorcara=colorcara)
		for boton in self.buttonsitems:
			boton.set_colores(colorbas=colorbas, colorbor=colorbor, colorcara=colorcara)

	def set_text(self, tipo= None, tamanio= None, color= None):
		''' Seteo de tipo, tamaño y color de letra. '''
		cambios= False
		if tipo:
			self.text["font_from_file"]= None
			self.text["tipo"]= tipo
			cambios= True
		if tamanio and type(tamanio)== int:
			self.text["tamanio"]= tamanio
			cambios= True
		if color:
			self.text["color"]= color
			cambios= True

		if not cambios: return
		self.buttoncaption.set_text(tipo= self.text["tipo"], tamanio= self.text["tamanio"], color= self.text["color"])
		self.buttonaction.set_text(tipo= self.text["tipo"], tamanio= self.text["tamanio"], color= self.text["color"])
		for boton in self.buttonsitems:
			boton.set_text(tipo= self.text["tipo"], tamanio= self.text["tamanio"], color= self.text["color"])

		self.ordena_items()

	def set_font_from_file(self, fuente, tamanio= None):
		''' Seteo de tipo y tamaño de letra desde un archivo defuentes. '''
		if tamanio and type(tamanio)== int:
			self.text["tamanio"]= tamanio
		if fuente:
			self.text["font_from_file"]= fuente
			self.buttoncaption.set_font_from_file(self.text["font_from_file"], tamanio= self.text["tamanio"])
			self.buttonaction.set_font_from_file(self.text["font_from_file"], tamanio= self.text["tamanio"])
			for boton in self.buttonsitems:
				boton.set_font_from_file(self.text["font_from_file"], tamanio= self.text["tamanio"])

			self.ordena_items()

	def set_tamanios(self, tamanio=(0,0)):
		''' Setea el tamaño de los items. '''
		self.buttoncaption.set_tamanios(tamanio= tamanio)#, grosorbor=1, detalle=1, espesor=1)
		self.buttonaction.set_tamanios(tamanio= (self.buttonaction.get_tamanio()[0],tamanio[1]) )
		for item in self.buttonsitems:
			item.set_tamanios(tamanio= tamanio)

		ancho, alto= self.buttoncaption.get_tamanio()
		w,h= self.buttonaction.get_tamanio()
		if alto < h: alto= h
		for item in self.buttonsitems:
			ww,hh= item.get_tamanio()
			if ancho < ww: ancho= ww
			if alto < hh: alto= hh

		self.buttoncaption.set_tamanios(tamanio=(ancho,alto), grosorbor=1, detalle=1, espesor=1)
		self.buttonaction.set_tamanios(tamanio=(self.buttonaction.get_tamanio()[0],alto), grosorbor=1, detalle=1, espesor=1)
		for item in self.buttonsitems:
			item.set_tamanios(tamanio=(ancho,alto), grosorbor=1, detalle=1, espesor=1)

		self.set_posicion(punto= self.posicion)

	def set_alineacion_text(self, alineacion):
		''' La alineación del texto en los items. '''
		for item in self.buttonsitems:
			item.set_alineacion_label(alineacion)
		
	# ----------- SETEOS ------------------------------

	# -----------------SETEOS Que no afectan al tamaño -------------------
	def set_posicion(self, punto= (0,0)):
		if type(punto)== tuple and len(punto)== 2:
			if type(punto[0])== int and type(punto[1])== int:
				self.posicion= punto
				self.buttoncaption.set_posicion(punto= self.posicion)
				w,h= self.buttoncaption.get_tamanio()
				self.buttonaction.set_posicion(punto= (self.posicion[0]+w, self.posicion[1]) )

				x,y= self.posicion
				for item in self.buttonsitems:
					item.set_posicion(punto= (x,y))
					y+= item.get_tamanio()[1]

	def get_caption(self):
		''' Devuelve el texto del item en el caption de JAMComboBox. '''
		return self.buttoncaption.get_text()

	def get_tamanio_caption(self):
		''' Devuelve el ancho de caption y la altura de action.'''
		w= self.buttoncaption.get_tamanio()[0]
		h= self.buttonaction.get_tamanio()[1]
		return (w,h)

	def get_tamanio(self):
		''' Devuelve el tamaño de JAMComboBox. '''
		alto= 0
		ancho= self.buttoncaption.get_tamanio()[0] + self.buttonaction.get_tamanio()[0]
		if self.buttonsitems:
			for item in self.buttonsitems:
				alto+= item.get_tamanio()[1]
		else:
			alto= self.buttoncaption.get_tamanio()[1]
		return (ancho, alto)

	def get_posicion(self):
		''' Devuelve la posicion de JAMComboBox. '''
		return self.posicion

class ButtonCaption(JAMButton):
	def __init__(self, main):
		JAMButton.__init__(self, "JAMComboBox", None)
		self.main= main
		self.set_tamanios(tamanio=(0,0), grosorbor=1, detalle=1, espesor=1)
		if self.main.colores["colorbas"] and self.main.colores["colorbor"] and self.main.colores["colorcara"]:
			a= self.main.colores["colorbas"]
			b= self.main.colores["colorbor"]
			c= self.main.colores["colorcara"]
			self.set_colores(colorbas= a, colorbor= b, colorcara= c)
		if self.main.text["font_from_file"]:
			self.set_font_from_file(self.main.text["font_from_file"], tamanio= self.main.text["tamanio"])

	def update(self):
		''' Sobrecarga de update para anular la detección de eventos.'''
		pass

class ButtonAction(JAMButton):
	def __init__(self, main):
		JAMButton.__init__(self, ">", None)
		self.main= main
		self.set_tamanios(tamanio=(0,0), grosorbor=1, detalle=1, espesor=1)
		self.connect(callback= main.show_items, sonido_select= None)
		if self.main.colores["colorbas"] and self.main.colores["colorbor"] and self.main.colores["colorcara"]:
			a= self.main.colores["colorbas"]
			b= self.main.colores["colorbor"]
			c= self.main.colores["colorcara"]
			self.set_colores(colorbas= a, colorbor= b, colorcara= c)
		if self.main.text["font_from_file"]:
			self.set_font_from_file(self.main.text["font_from_file"], tamanio= self.main.text["tamanio"])

# ----- FIN DE CLASE JAMComboBox - 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 = (1024,768)

		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 = JAMComboBox()
		self.widgets.add_item("Salir", callback= self.salir)
		self.widgets.add_item("Prueba de Item 1", callback= None)
		self.widgets.add_item("Ultima Prueba de Item", callback= None)
		self.widgets.set_posicion(punto= (50,50))

		self.ventana = pygame.display.get_surface()
		self.reloj = pygame.time.Clock()
		pygame.event.set_blocked([JOYAXISMOTION, JOYBALLMOTION, JOYHATMOTION, JOYBUTTONUP, JOYBUTTONDOWN,
					USEREVENT, QUIT, ACTIVEEVENT])
		pygame.event.set_allowed([MOUSEMOTION, MOUSEBUTTONUP, MOUSEBUTTONDOWN, KEYDOWN, KEYUP, 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)

			self.widgets.update()
			self.handle_event()
			#pygame.event.clear()
			cambios.extend ( self.widgets.draw(self.ventana) )
			pygame.display.update(cambios)
			contador += 1
			if contador== 50:
				colorbase,colorborde,colorcara= VG.get_estilo_papel_quemado()
				self.widgets.set_colores(colorbas= colorbase, colorbor= colorborde, colorcara= colorcara)
				#self.widgets.set_font_from_file(VG.get_Font_fawn()[0], tamanio= 20)
	
			if contador== 200:
				self.widgets.set_tamanios(tamanio=(300,10))
				#self.widgets.set_text(tipo= "Purisa", tamanio= 10, color= VG.get_azul1())
				#self.widgets.remove_item("Prueba de Item 1")
				self.widgets.remove_item_index(0)
				#self.widgets.clear_items()
				contador= 0
			'''
			if contador== 300:
				self.widgets.set_alineacion_text("izquierda")'''

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

	def handle_event(self):
		for event in pygame.event.get():
			if event.type == pygame.KEYDOWN:
				teclas = pygame.key.get_pressed()
				if teclas[pygame.K_ESCAPE]:
					self.salir(None)
		pygame.event.clear()

	def salir(self, button):
		pygame.quit()
		sys.exit()

if __name__ == "__main__":
	Ejemplo()