-
Notifications
You must be signed in to change notification settings - Fork 0
/
widgets.py
267 lines (242 loc) · 10.7 KB
/
widgets.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/env python
#Wizards Magic
#Copyright (C) 2011-2014 https://code.google.com/p/wizards-magic/
#This program is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public License
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# vim: set fileencoding=utf-8 :
try:
import pygame
from pygame.locals import *
from pygame import Surface, draw
yes_pygame = True
except ImportError:
yes_pygame = False
import globals
class TxtInput(pygame.sprite.Sprite):
''' class handling input box:
pos: # of line from the top of form
label: box label
text: text inside box
length: box length in chars
numeric: True=accept only numbers
enabled: false=disabled box
key: configuration key
'''
def __init__(self, pos=0, label="", text="", length=0, numeric=False, enabled=True, key=""):
pygame.sprite.Sprite.__init__(self)
self.type = 'txtinput'
self.color = (255,255,255)
self.color_disable = (95,245,244)
self.font = pygame.font.Font(globals.current_folder + "/misc/DroidSans.ttf", 18)
self.pos = pos
self.label = label
self.text = text[:length]
self.length = length
self.key = key
self.size = self.font.size("O" * (self.length+1))[0]
self.focus = False
self.currpos = len(self.text)
self.numeric = numeric
self.enabled = enabled
self.label_image = self.font.render(self.label,True,self.color)
if self.enabled:
self.text_image = self.font.render(self.text,True,self.color)
else:
self.text_image = self.font.render(self.text,True,(self.color_disable))
self.label_rect = self.label_image.get_rect()
self.text_rect = self.text_image.get_rect()
self.image = None
self.rect = None
globals.menu_group.add(self)
def draw(self):
bgrect = globals.background.get_rect()
menupos = globals.menu_bg.get_rect()
menupos.centery = globals.background.get_rect().centery
self.label_rect.left = bgrect.centerx - self.label_rect.width - 7 #label X coord (left side from center)
self.label_rect.top = menupos.top + 50 + (self.label_rect.height + 5) * self.pos
self.text_rect.left = bgrect.centerx + 7 # text X coord (right size from center)
self.text_rect.top = self.label_rect.top
self.image = pygame.Surface((self.label_rect.width + 14 + self.size, self.font.get_linesize()), pygame.SRCALPHA)
self.rect = self.image.get_rect()
self.rect.left = self.label_rect.left
self.rect.top = self.label_rect.top
self.image.blit(self.label_image, (0,0))
self.image.blit(self.text_image, (self.label_rect.width + 14,0))
if self.focus:
#X coordinate of the cursor
cursorx = self.font.size(self.text[:self.currpos])[0]
cursorx += self.label_rect.width + 14
draw.line(self.image, (255,255,255), (cursorx ,0),(cursorx , self.font.get_height()))
globals.background.blit(self.image, self.label_rect)
def update(self):
self.draw()
def enable(self):
self.enable=True
self.text_image = self.font.render(self.text,True,self.color)
def disable(self):
self.enable=False
self.text_image = self.font.render(self.text,True,self.color_disable)
def change(self, event):
if self.currpos > len(self.text):
self.currpos = len(self.text)
if self.enabled:
if event.type == pygame.KEYUP:
if event.key==K_RETURN:
self.focus=False
globals.itemfocus=None
if event.key == pygame.K_BACKSPACE:
if self.currpos == 0:
return
self.text = self.text[:self.currpos-1] + self.text[self.currpos:]
self.currpos -= 1
if self.currpos < 0:
self.currpos = 0
elif event.key == pygame.K_DELETE:
self.text = self.text[:self.currpos] + self.text[self.currpos+1:]
elif event.key == pygame.K_LEFT:
self.currpos -= 1
if self.currpos < 0:
self.currpos = 0
elif event.key == pygame.K_RIGHT:
self.currpos += 1
if self.currpos > len(self.text):
self.currpos = len(self.text)
elif event.key == pygame.K_HOME:
self.currpos = 0
elif event.key == pygame.K_END:
self.currpos = len(self.text)
elif event.key in (pygame.K_RSHIFT, pygame.K_LSHIFT, pygame.K_RETURN, pygame.K_TAB):
pass
else:
if len(self.text)<self.length:
self.text = self.text[:self.currpos] + pygame.key.name(event.key) + self.text[self.currpos:]
self.currpos += 1
if self.enabled:
self.text_image = self.font.render(self.text,True,self.color)
else:
self.text_image = self.font.render(self.text,True,(self.color_disable))
self.draw()
def onmousedown(self):
if self.enabled:
if globals.itemfocus:
globals.itemfocus.focus = False
self.focus = True
globals.itemfocus = self
def onmouseup(self):
pass
class CheckBox(pygame.sprite.Sprite):
''' class handling checkbox:
pos: # of line from the top of form
label: CheckBox label
value: True/False
enabled: false=disabled box
key: configuration key
'''
def __init__(self, pos=0, label="", value=False, enabled=True, key=""):
pygame.sprite.Sprite.__init__(self)
self.type = 'checkbox'
self.color = (255,255,255)
self.color_disable = (95,245,244)
self.font = pygame.font.Font(globals.current_folder + "/misc/DroidSans.ttf", 18)
self.pos = pos
self.label = label
self.value = value
self.key = key
self.focus = False
self.enabled = enabled
if self.enabled:
self.label_image = self.font.render(self.label,True,self.color)
else:
self.label_image = self.font.render(self.label,True,(self.color_disable))
self.label_rect = self.label_image.get_rect()
self.value_on = pygame.image.load(globals.current_folder + '/misc/checkbox_on.gif').convert()
self.value_off = pygame.image.load(globals.current_folder + '/misc/checkbox_off.gif').convert()
if value:
self.value_image = self.value_on
else:
self.value_image = self.value_off
self.image = None
self.rect = None
globals.menu_group.add(self)
def draw(self):
bgrect = globals.background.get_rect()
menupos = globals.menu_bg.get_rect()
menupos.centery = globals.background.get_rect().centery
self.label_rect.left = bgrect.centerx - self.label_rect.width - 7 #label X coord (left side from center)
self.label_rect.top = menupos.top + 50 + (self.label_rect.height + 5) * self.pos
self.image = pygame.Surface((self.label_rect.width + 14 + self.value_image.get_rect().width, self.font.get_linesize()), pygame.SRCALPHA)
self.rect = self.image.get_rect()
self.rect.left = self.label_rect.left
self.rect.top = self.label_rect.top
self.image.blit(self.label_image, (0,0))
self.image.blit(self.value_image, (self.label_rect.width + 14,0))
globals.background.blit(self.image, self.label_rect)
def update(self):
self.draw()
def enable(self):
self.enable = True
self.label_image = self.font.render(self.label,True,self.color)
def disable(self):
self.enable = False
self.label_image = self.font.render(self.label,True,(self.color_disable))
def change(self): #Hot Fix
return 0
def onmousedown(self):
if self.enabled:
if globals.itemfocus:
globals.itemfocus.focus = False
self.focus = True
globals.itemfocus = self
if self.value:
self.value_image = self.value_off
self.value = False
else:
self.value_image = self.value_on
self.value = True
def onmouseup(self):
pass
def onmouseout(self):
pass
def onmouse(self):
pass
def main():
pygame.init()
globals.screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption('Wizards Magic')
globals.background = pygame.Surface(globals.screen.get_size())
globals.background = globals.background.convert()
globals.background.fill((250, 250, 250))
ti = TxtInput(0,"SOUND","192.168.111.255","",5)
globals.background = pygame.image.load(globals.current_folder + '/misc/menu_bg.jpg').convert_alpha()
globals.menu_bg = pygame.image.load(globals.current_folder + '/misc/menu_selections_bg.jpg').convert_alpha()
menupos = globals.menu_bg.get_rect()
menupos.centerx = globals.background.get_rect().centerx -2 # '-2' hack due lazy designer :)
menupos.centery = globals.background.get_rect().centery -1 # '-1' hack due lazy designer :)
globals.background.blit(globals.menu_bg, menupos)
globals.screen.blit(globals.background, (0, 0))
pygame.display.flip()
while 1:
globals.menu_group.update()
for event in pygame.event.get():
if event.type == QUIT:
return
print event
if event.type == pygame.KEYUP:
if event.key == pygame.K_ESCAPE:
print "qqqqqqqqqqqqqqqqqq"
else:
print event.key
print pygame.key.name(event.key)
globals.screen.blit(globals.background, (0, 0))
pygame.display.flip()
pygame.time.wait(200)
if __name__ == '__main__': main()