Package pilas :: Package interfaz :: Module lista_seleccion
[hide private]
[frames] | no frames]

Source Code for Module pilas.interfaz.lista_seleccion

 1  # -*- encoding: utf-8 -*- 
 2  # pilas engine - a video game framework. 
 3  # 
 4  # copyright 2010 - hugo ruscitti 
 5  # license: lgplv3 (see http://www.gnu.org/licenses/lgpl.html) 
 6  # 
 7  # website - http://www.pilas-engine.com.ar 
 8   
 9  import pilas 
10  from pilas.interfaz.base_interfaz import BaseInterfaz 
11   
12   
13 -class ListaSeleccion(BaseInterfaz):
14
15 - def __init__(self, opciones, funcion_a_ejecutar=None, x=0, y=0):
16 BaseInterfaz.__init__(self, x=x, y=y) 17 self.opciones = opciones 18 self.funcion_a_ejecutar = funcion_a_ejecutar 19 20 ancho, alto = pilas.mundo.motor.obtener_area_de_texto("\n".join(opciones)) 21 22 self.alto_opcion = pilas.mundo.motor.obtener_area_de_texto("texto")[1] 23 self.alto_opciones = self.alto_opcion * len(self.opciones) 24 self.ancho_opciones = ancho 25 self.separacion_entre_opciones = 2 # en pixels 26 27 self.imagen = pilas.imagenes.cargar_superficie(int(ancho + 35), int(self.alto_opciones + (self.separacion_entre_opciones * len(self.opciones) * 2))) 28 29 self._pintar_opciones() 30 31 pilas.eventos.mueve_mouse.conectar(self.cuando_mueve_el_mouse) 32 pilas.eventos.click_de_mouse.conectar(self.cuando_hace_click_con_el_mouse) 33 34 self.centro = ("centro", "centro") 35 self.fijo = True
36
37 - def _pintar_opciones(self, pinta_indice_opcion=None):
38 self.imagen.pintar(pilas.colores.blanco) 39 40 if pinta_indice_opcion != None: 41 self.imagen.rectangulo(0, pinta_indice_opcion * (self.alto_opcion + (self.separacion_entre_opciones * 2)), self.imagen.ancho(), self.alto_opcion + (self.separacion_entre_opciones * 2), relleno=True, color=pilas.colores.naranja) 42 43 for indice, opcion in enumerate(self.opciones): 44 self.imagen.texto(opcion, 15, y=self.alto_opcion * indice + self.alto_opcion + (self.separacion_entre_opciones * 2 * indice), color=pilas.colores.negro)
45
46 - def cuando_mueve_el_mouse(self, evento):
47 if (self.activo): 48 if self.colisiona_con_un_punto(evento.x, evento.y): 49 opcion_seleccionada = self._detectar_opcion_bajo_el_mouse(evento) 50 self._pintar_opciones(opcion_seleccionada)
51
52 - def cuando_hace_click_con_el_mouse(self, evento):
53 if (self.activo): 54 if self.colisiona_con_un_punto(evento.x, evento.y): 55 opcion = self._detectar_opcion_bajo_el_mouse(evento) 56 if self.funcion_a_ejecutar: 57 self.funcion_a_ejecutar(self.opciones[opcion]) 58 else: 59 print "Cuidado, no has definido funcion a ejecutar en la lista de seleccion."
60
61 - def _detectar_opcion_bajo_el_mouse(self, evento):
62 opcion = int((self.arriba - evento.y ) / (self.alto_opcion + (self.separacion_entre_opciones * 2))) 63 if opcion in range(0, len(self.opciones)): 64 return opcion
65