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

Source Code for Module pilas.interfaz.ingreso_de_texto

  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  import re 
 11  from pilas.interfaz.base_interfaz import BaseInterfaz 
 12   
13 -class IngresoDeTexto(BaseInterfaz):
14 """Representa una caja de texto para escribir sobre ella.""" 15
16 - def __init__(self, texto_inicial="", x=0, y=0, ancho=300, limite_de_caracteres=20, icono=None):
17 """Inicializa la caja de texto. 18 19 :param texto_inicial: La cadena de texto inicial del campo de texto. 20 :param x: Posición horizontal. 21 :param y: Posición vertical. 22 :param ancho: Ancho de la caja para ingresar texto. 23 :param limite_de_caracteres: Límite de la longitud de cadena (en cantidad de caracteres). 24 :param icono: Icono que se mostrará en la cada de texto. 25 """ 26 BaseInterfaz.__init__(self, x=x, y=y) 27 self.texto = texto_inicial 28 self.cursor = "" 29 self._cargar_lienzo(ancho) 30 31 if icono: 32 self.icono = pilas.imagenes.cargar(icono) 33 else: 34 self.icono = None 35 36 self.imagen_caja = pilas.imagenes.cargar("interfaz/caja.png") 37 self.centro = ("centro", "centro") 38 self._actualizar_imagen() 39 self.limite_de_caracteres = limite_de_caracteres 40 self.cualquier_caracter() 41 42 self.escena.suelta_tecla.conectar(self.cuando_pulsa_una_tecla) 43 pilas.mundo.agregar_tarea_siempre(0.40, self._actualizar_cursor) 44 self.fijo = True
45
46 - def _actualizar_cursor(self):
47 if (self.tiene_el_foco): 48 if self.cursor == "": 49 self.cursor = "_" 50 else: 51 self.cursor = "" 52 else: 53 self.cursor = "" 54 55 self._actualizar_imagen() 56 return True
57
58 - def cualquier_caracter(self):
59 self.caracteres_permitidos = re.compile(".*")
60
61 - def solo_numeros(self):
62 self.caracteres_permitidos = re.compile("\d+")
63
64 - def solo_letras(self):
65 self.caracteres_permitidos = re.compile("[a-z]+")
66
67 - def cuando_pulsa_una_tecla(self, evento):
68 if self.tiene_el_foco and self.activo: 69 if evento.codigo == '\x08' or evento.texto == '\x08': 70 # Indica que se quiere borrar un caracter 71 self.texto = self.texto[:-1] 72 else: 73 if len(self.texto) < self.limite_de_caracteres: 74 nuevo_texto = self.texto + evento.texto 75 76 if (self.caracteres_permitidos.match(evento.texto)): 77 self.texto = self.texto + evento.texto 78 else: 79 print "Rechazando el ingreso del caracter:", evento.texto 80 else: 81 print "Rechazando caracter por llegar al limite." 82 83 self._actualizar_imagen()
84
85 - def _cargar_lienzo(self, ancho):
87
88 - def _actualizar_imagen(self):
89 ancho = self.imagen_caja.ancho() 90 alto = self.imagen_caja.alto() 91 self.imagen.pintar_parte_de_imagen(self.imagen_caja, 0, 0, 40, ancho, 0, 0) 92 93 if self.icono: 94 dx = 20 95 self.imagen.pintar_parte_de_imagen(self.icono, 0, 0, 40, ancho, 7, 7) 96 else: 97 dx = 0 98 99 for x in range(40, self.imagen.ancho() - 40): 100 self.imagen.pintar_parte_de_imagen(self.imagen_caja, ancho - 40, 0, 40, alto, x, 0) 101 102 self.imagen.texto(self.texto + self.cursor, 15 + dx, 20)
103