-
Notifications
You must be signed in to change notification settings - Fork 0
/
button.py
227 lines (181 loc) · 6.17 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import pygame
from data import images
from imagesprite import ImageSprite
from spritecontainer import ExtendSprite
from consts import *
class Button(ExtendSprite):
def __init__(self, normal, over, pressed):
super(Button, self).__init__()
self.normal = normal
self.over = over
self.pressed = pressed
self.setimage(self.normal)
self.mouseListener = None
def setMouseListener(self, callback):
self.mouseListener = callback
def recover(self):
self.setimage(self.normal)
def shade(self):
self.setimage(self.over)
def under(self):
self.setimage(self.pressed)
def onMouseMove(self, x, y):
if not self.contains(x, y):
self.recover()
else:
self.shade()
def onMouseDown(self, x, y, button):
if button != 1:
return
if not self.contains(x, y):
return
self.under()
def onMouseUp(self, x, y, button):
if button != 1:
return
if not self.image == self.pressed:
return
self.recover()
if self.contains(x, y):
if self.mouseListener is not None:
self.mouseListener()
class PressedButton(Button):
def __init__(self, normal, pressed):
super(PressedButton, self).__init__(normal, normal, pressed)
self.ispressed = False
def onMouseMove(self, x, y):
pass
def onMouseUp(self, x, y, button):
pass
def onMouseDown(self, x, y, button):
if button != 1:
return
if self.contains(x, y):
if self.ispressed:
self.recover()
self.ispressed = False
else:
self.under()
self.ispressed = True
if self.mouseListener is not None:
if self.mouseListener is not None:
self.mouseListener()
class TextButton(Button):
def __init__(self, text, font, normal, over, pressed):
textimg = font.render(text, False, WHITE)
textrect = textimg.get_rect()
normalimg = normal.copy()
textrect.center = normalimg.get_rect().center
normalimg.blit(textimg, textrect)
overimg = over.copy()
textrect.center = overimg.get_rect().center
overimg.blit(textimg, textrect)
pressedimg = pressed.copy()
textrect.center = pressedimg.get_rect().center
pressedimg.blit(textimg, textrect)
super(TextButton, self).__init__(normalimg, overimg, pressedimg)
class RA2Button(TextButton):
def __init__(self, text):
font = pygame.font.Font(None, 26)
img = images["menubttn"]
btnwidth = img.get_rect().width/3
btnheight = img.get_rect().height
normalimg = images["menubttn"].subsurface(0, 0, btnwidth, btnheight)
overimg = images["menubttn"].subsurface(btnwidth, 0, btnwidth, btnheight)
pressedimg = images["menubttn"].subsurface(
btnwidth*2, 0, btnwidth, btnheight)
super(RA2Button, self).__init__(text, font, normalimg, overimg, pressedimg)
class CreateButton(Button):
def __init__(self, name):
normal = images["create%s" % name]
if "disable%s" % name not in images:
images["disable%s" % name] = images["create%s" % name].copy()
images["disable%s" % name].blit(images["black"], (0, 0))
over = normal
pressed = normal
super(CreateButton, self).__init__(normal, over, pressed)
self.disableimg = images["disable%s" % name]
self.disabled = False
self.name = name
self.rightMouseListener = None
def disable(self):
self.disabled = True
self.setimage(self.disableimg)
def recover(self):
self.disabled = False
self.setimage(self.normal)
def setRightMouseListener(self, callback):
self.rightMouseListener = callback
def onMouseDown(self, x, y, button):
if button == 1:
if self.contains(x, y):
if self.mouseListener is not None:
self.mouseListener(self.name)
elif button == 3:
if self.contains(x, y):
if self.rightMouseListener is not None:
self.rightMouseLisener(self.name)
def onMouseUp(self, x, y, button):
pass
def onMouseMove(self, x, y, button1=None, button2=None, button3=None):
pass
class GameCtrlButton(Button):
def __init__(self, index):
img = images["button%d%d" % (index/10, index % 10)]
normal = img.subsurface(0, 0, img.get_width()/2, img.get_height())
over = img.subsurface(img.get_width()/2, 0,
img.get_width()/2, img.get_height())
pressed = over
super(GameCtrlButton, self).__init__(normal, over, pressed)
class GamePanelPressedButton(PressedButton):
def __init__(self, name):
img = images[name]
normal = img.subsurface(0, 0, img.get_width()/2, img.get_height())
pressed = img.subsurface(
img.get_width()/2, 0, img.get_width()/2, img.get_height())
super(GamePanelPressedButton, self).__init__(normal, pressed)
class GamePanelButton(Button):
def __init__(self, name):
img = images[name]
normal = img.subsurface(0, 0, img.get_width()/2, img.get_height())
over = normal
pressed = img.subsurface(
img.get_width()/2, 0, img.get_width()/2, img.get_height())
super(GamePanelButton, self).__init__(normal, over, pressed)
class TabButton(Button):
def __init__(self, index, tab, children):
img = images["tabbtn%d" % index]
w = img.get_width()/5
h = img.get_height()
normal = img.subsurface(0, 0, w, h)
over = normal
pressed = img.subsurface(w, 0, w, h)
super(TabButton, self).__init__(normal, over, pressed)
self.tab = tab
self.children = children
def onMouseDown(self, x, y, button):
if button != 1:
return
if not self.contains(x, y):
return
for tabb in self.tab:
tabb.recover()
self.under()
def onMouseUp(self, x, y, button):
pass
def under(self):
self.setimage(self.pressed)
self.children.visible = True
def recover(self):
self.setimage(self.normal)
self.children.visible = False
def onMouseMove(self, x, y):
pass
class ButtonSet(set):
def __init__(self):
super(ButtonSet, self).__init__()
self.visible = False
self.group = pygame.sprite.Group()
self.overgroup = None
self.createProgress = None
self.scroll = 0