Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/pilas/interfaz/boton.py
blob: 28043d488a6c45c2a6ac8698bfd3fad9642f06a1 (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
# -*- encoding: utf-8 -*-
# pilas engine - a video game framework.
#
# copyright 2010 - hugo ruscitti
# license: lgplv3 (see http://www.gnu.org/licenses/lgpl.html)
#
# website - http://www.pilas-engine.com.ar

import pilas

class Boton(pilas.actores.Actor):
    
    def __init__(self, texto="Sin texto", x=0, y=0, icono=None):
        pilas.actores.Actor.__init__(self, x=x, y=y)
        self.texto = texto
        self._crear_imagenes_de_botones()
        self.centro = ("centro", "centro")
        self.funcion = None

        if icono:
            self.icono = pilas.imagenes.cargar(icono)
        else:
            self.icono = None

        pilas.eventos.mueve_mouse.conectar(self.cuando_mueve_el_mouse)
        pilas.eventos.click_de_mouse.conectar(self.cuando_hace_click)

    def conectar(self, funcion):
        self.funcion = funcion

    def _crear_imagenes_de_botones(self):
        "Genera las 3 imagenes de los botones."
        ancho, alto = pilas.utils.obtener_area_de_texto(self.texto)
        tema = pilas.imagenes.cargar("boton/tema.png")

        self.imagen_normal = self._crear_imagen(tema, self.texto, ancho, 0)
        self.imagen_sobre = self._crear_imagen(tema, self.texto, ancho, 103)
        self.imagen_click = self._crear_imagen(tema, self.texto, ancho, 205)

        self.imagen = self.imagen_normal

    def cuando_mueve_el_mouse(self, evento):
        if self.colisiona_con_un_punto(evento.x, evento.y):
            self.imagen = self.imagen_sobre
        else:
            self.imagen = self.imagen_normal

    def cuando_hace_click(self, evento):
        if self.imagen == self.imagen_sobre:
            self.imagen = self.imagen_click

            if self.funcion:
                self.funcion()
        
    def _crear_imagen(self, tema, texto, ancho, dx):
        "Genera una imagen de superficie de boton."
        imagen = pilas.imagenes.cargar_superficie(20 + ancho, 30)
        imagen.pintar_parte_de_imagen(tema, dx, 0, 5, 25, 0, 0)

        for x in range(1, ancho + 20, 5):
            imagen.pintar_parte_de_imagen(tema, dx + 5, 0, 5, 25, x, 0)

        imagen.pintar_parte_de_imagen(tema, dx + 75, 0, 5, 25, ancho + 15, 0)
        imagen.texto(texto, 10, 17)
        return imagen