File: tbutton.py

package info (click to toggle)
teg 0.11.2%2Bdebian-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny, squeeze
  • size: 11,484 kB
  • ctags: 7,108
  • sloc: ansic: 16,775; sh: 8,559; python: 1,442; xml: 490; makefile: 385
file content (154 lines) | stat: -rw-r--r-- 5,434 bytes parent folder | download | duplicates (4)
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
# $Id: tbutton.py,v 1.1 2003/09/14 02:01:14 riq Exp $
###############################################################################################
# $Civil: button.py,v 1.3 2003/06/05 20:12:06 chakie Exp $
###############################################################################################

from twidget import TWidget
from tfont import TFont
import twidget
import pygame
from pygame.locals import *


class TButton(TWidget):
    """
    This class defines a simple pushbutton which can can be clicked. It needs a surfaces that
    represent the button. 

    This class plays a small sound when the button is clicked.
    """

    Spacing = (10,10)
    buttonFont = TFont.get('helvetica 16')


    def __init__ (self, label = '', icon = None, callbacks = None):
        """Initializes the widget. Loads the icon."""

        # firts call superclass constructor
        TWidget.__init__ (self, callbacks)

        # initialize label
        self._label = label
        self.__label_size = (0,0)

        # initialize icon
        self._icon = icon
        self.__icon_size = (0,0)

        # generate surfaces for label/icon
        self._generateSurfaces()
 
        # set our internal callbacks so that we can trap keys
        # use enter/leave up/down
        self.internal = {twidget.MOUSEENTEREVENT : self.mouseEnter,
                         twidget.MOUSELEAVEEVENT : self.mouseLeave,
                         twidget.MOUSEBUTTONUP   : self.mouseUp,
                         twidget.MOUSEBUTTONDOWN : self.mouseDown }
            


    def _calculateSize( self ):
        """Returns the size needed for the icon/label"""
        if self._icon:
            self.__icon_size = self._icon.get_rect()[2:]

        self.__label_size = TButton.buttonFont.size( self._label )
        if self.__icon_size[0] > self.__label_size[0]:
            size_x = self.__icon_size[0]
        else:
            size_x = self.__label_size[0]

        size_y = self.__icon_size[1] + self.__label_size[1]
        return (size_x + TButton.Spacing[0], size_y + TButton.Spacing[1] )

    def _generateSurfaces( self ):
        """Generates the surfaces for the button"""
        self.surface = pygame.Surface( self._calculateSize() )

        self._surface_outside = self._getSurfaceOutside()
        self._surface_inside = self._getSurfaceInside()
        self._surface_pressed = self._getSurfacePressed()
        self._surface_released = self._getSurfaceReleased()

        self.surface = self._surface_outside
    
    def mouseUp (self, event):
        """Internal callback triggered when the mouse is released when it is over a button. This
        sets a new icon for the button."""
        # set the new icon
        self.surface = self._surface_released

        # play a sound
#        scenario.audio.playSample ( 'button-clicked' )
        
        # we're dirty
        self.dirty = 1


    def mouseDown (self, event):
        """Internal callback triggered when the mouse is pressed when it is over a button. This
        sets a new icon for the button."""
        # set the new icon
        self.surface = self._surface_pressed

        # we're dirty
        self.dirty = 1


    def mouseEnter (self, event):
        """Internal callback triggered when the mouse enters a button. This sets a new icon for the
        button.""" 
        # set the new icon
        self.surface = self._surface_inside
        
        # we're dirty
        self.dirty = 1


    def mouseLeave (self, event):
        """Internal callback triggered when the mouse leaves a button. This sets a new icon for the
        button.""" 
        # set the new icon
        self.surface = self._surface_outside
        
        # we're dirty
        self.dirty = 1

    def __getSurface( self, color, offset = 0, drawBox = False ):

        surface = pygame.Surface( self._calculateSize() )

        rect = pygame.Rect( self.getX() + offset, self.getY() + offset, self.getWidth() - offset, self.getHeight() - offset )
        pygame.draw.rect( surface, color, rect, 0)

        if drawBox:
            rect = pygame.Rect( self.getX() + 2 , self.getY() + 2, self.getWidth() -4, self.getHeight() -4 )
            pygame.draw.rect( surface, (192,192,192,255), rect, 2)

        if self._icon:
            x = (self.getWidth() - self.__icon_size[0]) / 2
            surface.blit( self._icon, (self.getX() + x + offset , self.getY() + 5 + offset) )

        if self._label:
            x = (self.getWidth() - self.__label_size[0]) / 2
            render = TButton.buttonFont.render( self._label, 1, (0,0,0))
            surface.blit( render, (self.getX() + x + offset, self.getY() + 5 + self.__icon_size[1] + offset ) )

        return surface 

    def _getSurfaceOutside( self ):
        """Generates the surface when the mouse pointer is outside the surface"""
        return self.__getSurface( pygame.color.Color('white') )

    def _getSurfaceInside( self ):
        """Generates the surface when the mouse pointer is inside the surface"""
        return self.__getSurface( (240,240,240,255) )

    def _getSurfacePressed( self ):
        """Generates the surface when the mouse pointer is inside the surface"""
        return self.__getSurface( (240,240,240,255), 2, True )

    def _getSurfaceReleased( self ):
        """Generates the surface when the mouse pointer is inside the surface"""
        return self.__getSurface( (240,240,240,255), 0 )