-
Notifications
You must be signed in to change notification settings - Fork 0
/
button.py
163 lines (131 loc) · 4.96 KB
/
button.py
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
import pygame
from constants import *
from font import small_font
__all__ = ['Button', 'load_img', 'Exit_button']
def load_img(filename, num_pics=(1,1)):
try:
image = pygame.image.load(filename)
colorkey = image.get_at((0,0))
image.set_colorkey(colorkey)
image.convert_alpha()
w, h = image.get_size()
x_step = float(w) / num_pics[0] #width of each sub image
y_step = float(h) / num_pics[1] #height of each sub image
images = []
for x in range(num_pics[0]):
for y in range(num_pics[1]):
images.append(image.subsurface((x_step*x, y_step*y, x_step, y_step)))
except Exception, msg:
print "Error in Load Image: ", msg
images = None
return images
class Button(pygame.sprite.Sprite):
def __init__(self, pos, containers, obj=None,
padding=(10,10), num_pics=(1,1)):
txt = None
img = None
if '.' in obj:
img = obj
else:
txt = obj
pygame.sprite.Sprite.__init__(self, containers)
self.pos = pos
self.containers = containers
self.img_index = 0 #currently indexed image
self.num_images = num_pics[0]*num_pics[1]
if txt:
self.text = small_font(txt, TEXT_COL)
else:
self.text = None
if img:
self.icons = load_img(img, num_pics)
else:
self.icons = None
self.current_state = BUTTON_OFF
self.colours = { BUTTON_OFF: BUTTON_INNER_BASE, \
BUTTON_ON: BUTTON_INNER_HOVER}
self.shown = False
self.pad = padding
self.redraw()
self.kill()
def show(self, containers=None):
try:
if containers != None:
self.containers = [containers]
except Exception, msg:
print msg
self.add(self.containers)
self.redraw()
def redraw(self, colour=BUTTON_INNER_BASE):
if self.text:
button_rect = self.text.get_rect().inflate(self.pad[0], self.pad[1])
else:
button_rect = pygame.Rect(0,0,0,0)
if not self.icons is None:
width, height = self.icons[self.img_index].get_size()
button_rect.inflate_ip(self.pad[0] + width, height + self.pad[1])
#make outer box
button = pygame.Surface(button_rect.size)
button.fill(BUTTON_BACK)
# make the inner box
inner = pygame.Surface((button_rect.width - 2, button_rect.height - 2))
inner.fill(colour)
button.blit(inner, (1,1))
self.__blit_button(button)
def __blit_button(self, button):
self.image = button
self.rect = button.get_rect()
self.rect.topleft = self.pos #absolute position
self.__render_content()
self.shown = True
def __render_content(self):
if self.text:
textpos = self.text.get_rect(center = (
self.image.get_rect().centerx,
self.image.get_rect().centery
)
)
self.image.blit(self.text, textpos)
if self.icons:
imagepos = self.icons[self.img_index].get_rect(center = (
self.image.get_rect().centerx,
self.image.get_rect().centery
)
)
self.image.blit(self.icons[self.img_index], imagepos)
def __contains__(self, item):
if self.shown:
return self.rect.collidepoint(item)
return False
def mouse_over(self, new_state):
#state = 1: on 0: off
if self.current_state != new_state:
if self.current_state == 0 and new_state == 1:
self.play_sound()
self.current_state = new_state
colour = self.colours[new_state] #get colour
self.redraw(colour)
def update(self, *args, **kwargs):
if not self.shown:
self.redraw()
def set_pos(self, pos):
self.pos = pos
self.rect.topleft = pos
def get_state(self):
return self.current_state == BUTTON_ON
def click(self, button):
if button == 1: #left click
return self.left_click()
elif button == 3: #right click
return self.right_click()
def increment_image(self,increment = 1):
self.img_index = (self.img_index + increment) % self.num_images
self.redraw()
class Exit_button(Button):
def __init__(self, pos, containers, left_click, obj, padding = (10,10)):
Button.__init__(self, pos, containers, obj, padding)
self.left_click = left_click
def right_click(self):
return None
def play_sound(self):
pass