-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.py
245 lines (226 loc) · 14.5 KB
/
input.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
import logging
logger = logging.getLogger('input')
import pygame
from pygame.locals import *
import lib
import map
import programmer
class MOUSE(object):
def __init__(self):
#self.prev_selected = None
##currently dragging/dropping, or currently selected item(out of pgrid GUI):
self.cur_sel = None
##active is for old pgrid active tile selection:
self.active = None
# @lib.decorators.propget
# def cur_selected(self):
# return self.cur_sel
# @lib.decorators.propset
# def cur_selected(self, value):
# self.prev_selected = self.cur_sel
# self.cur_sel = value
def cur_pos(self):
return pygame.mouse.get_pos()
def click_engine(self,pos):
pass
mouse=MOUSE()
class ConfigError(KeyError): pass
class Config(object):
""" A utility for configuration """
def __init__(self, options, *look_for):
assertions = []
for key in look_for:
if key[0] in options.keys(): exec('self.'+key[0]+' = options[\''+key[0]+'\']')
else: exec('self.'+key[0]+' = '+key[1])
assertions.append(key[0])
for key in options.keys():
if key not in assertions: raise ConfigError(key+' not expected as option')
class Input(object):
""" A text input for pygame apps
blatently stolen from http://www.pygame.org/project/920/
also this is a bit modified, helps make it work for me in this situation better..."""
def __init__(self, **options):
""" Options: x, y, font, color, restricted, maxlength, prompt """
self.options = Config(options, ['x', '0'], ['y', '0'], ['font', 'pygame.font.Font(lib.common.os.path.join(lib.common.curdir,"data","fonts","freesansbold.ttf"), 32)'],
['color', '(0,0,0)'], ['restricted', '\'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"#$%&\\\'()*+,-./:;<=>?@[\]^_`{|}~\''],
['maxlength', '-1'], ['prompt', '\'\''])
self.initopts=options
self.font = self.options.font
self.color = self.options.color
self.restricted = self.options.restricted
self.maxlength = self.options.maxlength
self.prompt = self.options.prompt; self._value = ''
self.shifted = False
##note to self::: fix this class to make it work ALOT better and also force it to fit in this game better
if type(self.color[0]) != tuple:
self.color=(self.color,(0,0,0))
self.text = self.font.render(self.prompt+self.value, 1, self.color[0])
##how big should the rect be???
self.rect=self.text.get_rect()
self.rect.midtop = self.options.x,self.options.y
self.active = False
def __getstate__(self):
state=self.__dict__.copy()
state.pop('font')
state.pop('options')
state.pop('text')
return state
def __setstate__(self,state):
Input.__init__(self,**state['initopts'])
self.__dict__.update(state)
@lib.decorators.propget
def value(self):
return self._value
@lib.decorators.propset
def value(self,mod):
self._value=mod
if self.active:
self.text = self.font.render(self.prompt+self._value, 1, self.color[0])
else:
self.text = self.font.render(self.prompt+self._value, 1, self.color[1])
self.rect = self.text.get_rect()
self.rect.midtop = self.options.x,self.options.y
map.cur_map.render()
def set_pos(self, x, y=None):
""" Set the position to x, y
or x can be a tuple of (x,y)"""
if y is None:
self.rect.midtop = x
else:
self.rect.midtop = x,y
def set_font(self, font):
""" Set the font for the input """
self.font = font
def draw(self, surface,override):
""" Draw the text input to a surface """
if self.active or override:
pygame.draw.rect(surface,(0,200,0),self.rect,2)
else:
pygame.draw.rect(surface,(0,0,0),self.rect,2)
surface.blit(self.text, self.rect)
def update(self, events, override=False):
""" Update the input based on passed events """
for event in events:
#dont update active, something else is taking care of that.
if event.type == MOUSEBUTTONDOWN and override==False:
if self.rect.collidepoint(event.pos):
self.active = True
else:
self.active = False
if event.type == KEYUP:
if event.key == K_LSHIFT or event.key == K_RSHIFT: self.shifted = False
#again, added override,just in case
if event.type == KEYDOWN and (self.active or override):
if event.key == K_BACKSPACE: self.value = self.value[:-1]
elif event.key == K_LSHIFT or event.key == K_RSHIFT: self.shifted = True
elif event.key == K_SPACE: self.value += ' '
if not self.shifted:
if event.key == K_a and 'a' in self.restricted: self.value += 'a'
elif event.key == K_b and 'b' in self.restricted: self.value += 'b'
elif event.key == K_c and 'c' in self.restricted: self.value += 'c'
elif event.key == K_d and 'd' in self.restricted: self.value += 'd'
elif event.key == K_e and 'e' in self.restricted: self.value += 'e'
elif event.key == K_f and 'f' in self.restricted: self.value += 'f'
elif event.key == K_g and 'g' in self.restricted: self.value += 'g'
elif event.key == K_h and 'h' in self.restricted: self.value += 'h'
elif event.key == K_i and 'i' in self.restricted: self.value += 'i'
elif event.key == K_j and 'j' in self.restricted: self.value += 'j'
elif event.key == K_k and 'k' in self.restricted: self.value += 'k'
elif event.key == K_l and 'l' in self.restricted: self.value += 'l'
elif event.key == K_m and 'm' in self.restricted: self.value += 'm'
elif event.key == K_n and 'n' in self.restricted: self.value += 'n'
elif event.key == K_o and 'o' in self.restricted: self.value += 'o'
elif event.key == K_p and 'p' in self.restricted: self.value += 'p'
elif event.key == K_q and 'q' in self.restricted: self.value += 'q'
elif event.key == K_r and 'r' in self.restricted: self.value += 'r'
elif event.key == K_s and 's' in self.restricted: self.value += 's'
elif event.key == K_t and 't' in self.restricted: self.value += 't'
elif event.key == K_u and 'u' in self.restricted: self.value += 'u'
elif event.key == K_v and 'v' in self.restricted: self.value += 'v'
elif event.key == K_w and 'w' in self.restricted: self.value += 'w'
elif event.key == K_x and 'x' in self.restricted: self.value += 'x'
elif event.key == K_y and 'y' in self.restricted: self.value += 'y'
elif event.key == K_z and 'z' in self.restricted: self.value += 'z'
elif event.key == K_0 and '0' in self.restricted: self.value += '0'
elif event.key == K_1 and '1' in self.restricted: self.value += '1'
elif event.key == K_2 and '2' in self.restricted: self.value += '2'
elif event.key == K_3 and '3' in self.restricted: self.value += '3'
elif event.key == K_4 and '4' in self.restricted: self.value += '4'
elif event.key == K_5 and '5' in self.restricted: self.value += '5'
elif event.key == K_6 and '6' in self.restricted: self.value += '6'
elif event.key == K_7 and '7' in self.restricted: self.value += '7'
elif event.key == K_8 and '8' in self.restricted: self.value += '8'
elif event.key == K_9 and '9' in self.restricted: self.value += '9'
##added numpad support:::
elif event.key == K_KP0 and '0' in self.restricted: self.value += '0'
elif event.key == K_KP1 and '1' in self.restricted: self.value += '1'
elif event.key == K_KP2 and '2' in self.restricted: self.value += '2'
elif event.key == K_KP3 and '3' in self.restricted: self.value += '3'
elif event.key == K_KP4 and '4' in self.restricted: self.value += '4'
elif event.key == K_KP5 and '5' in self.restricted: self.value += '5'
elif event.key == K_KP6 and '6' in self.restricted: self.value += '6'
elif event.key == K_KP7 and '7' in self.restricted: self.value += '7'
elif event.key == K_KP8 and '8' in self.restricted: self.value += '8'
elif event.key == K_KP9 and '9' in self.restricted: self.value += '9'
elif event.key == K_KP_MINUS and '-' in self.restricted: self.value += '-'
elif event.key == K_KP_PLUS and '+' in self.restricted: self.value += '+'
elif event.key == K_BACKQUOTE and '`' in self.restricted: self.value += '`'
elif event.key == K_MINUS and '-' in self.restricted: self.value += '-'
elif event.key == K_EQUALS and '=' in self.restricted: self.value += '='
elif event.key == K_LEFTBRACKET and '[' in self.restricted: self.value += '['
elif event.key == K_RIGHTBRACKET and ']' in self.restricted: self.value += ']'
elif event.key == K_BACKSLASH and '\\' in self.restricted: self.value += '\\'
elif event.key == K_SEMICOLON and ';' in self.restricted: self.value += ';'
elif event.key == K_QUOTE and '\'' in self.restricted: self.value += '\''
elif event.key == K_COMMA and ',' in self.restricted: self.value += ','
elif event.key == K_PERIOD and '.' in self.restricted: self.value += '.'
elif event.key == K_SLASH and '/' in self.restricted: self.value += '/'
elif self.shifted:
if event.key == K_a and 'A' in self.restricted: self.value += 'A'
elif event.key == K_b and 'B' in self.restricted: self.value += 'B'
elif event.key == K_c and 'C' in self.restricted: self.value += 'C'
elif event.key == K_d and 'D' in self.restricted: self.value += 'D'
elif event.key == K_e and 'E' in self.restricted: self.value += 'E'
elif event.key == K_f and 'F' in self.restricted: self.value += 'F'
elif event.key == K_g and 'G' in self.restricted: self.value += 'G'
elif event.key == K_h and 'H' in self.restricted: self.value += 'H'
elif event.key == K_i and 'I' in self.restricted: self.value += 'I'
elif event.key == K_j and 'J' in self.restricted: self.value += 'J'
elif event.key == K_k and 'K' in self.restricted: self.value += 'K'
elif event.key == K_l and 'L' in self.restricted: self.value += 'L'
elif event.key == K_m and 'M' in self.restricted: self.value += 'M'
elif event.key == K_n and 'N' in self.restricted: self.value += 'N'
elif event.key == K_o and 'O' in self.restricted: self.value += 'O'
elif event.key == K_p and 'P' in self.restricted: self.value += 'P'
elif event.key == K_q and 'Q' in self.restricted: self.value += 'Q'
elif event.key == K_r and 'R' in self.restricted: self.value += 'R'
elif event.key == K_s and 'S' in self.restricted: self.value += 'S'
elif event.key == K_t and 'T' in self.restricted: self.value += 'T'
elif event.key == K_u and 'U' in self.restricted: self.value += 'U'
elif event.key == K_v and 'V' in self.restricted: self.value += 'V'
elif event.key == K_w and 'W' in self.restricted: self.value += 'W'
elif event.key == K_x and 'X' in self.restricted: self.value += 'X'
elif event.key == K_y and 'Y' in self.restricted: self.value += 'Y'
elif event.key == K_z and 'Z' in self.restricted: self.value += 'Z'
elif event.key == K_0 and ')' in self.restricted: self.value += ')'
elif event.key == K_1 and '!' in self.restricted: self.value += '!'
elif event.key == K_2 and '@' in self.restricted: self.value += '@'
elif event.key == K_3 and '#' in self.restricted: self.value += '#'
elif event.key == K_4 and '$' in self.restricted: self.value += '$'
elif event.key == K_5 and '%' in self.restricted: self.value += '%'
elif event.key == K_6 and '^' in self.restricted: self.value += '^'
elif event.key == K_7 and '&' in self.restricted: self.value += '&'
elif event.key == K_8 and '*' in self.restricted: self.value += '*'
elif event.key == K_9 and '(' in self.restricted: self.value += '('
elif event.key == K_BACKQUOTE and '~' in self.restricted: self.value += '~'
elif event.key == K_MINUS and '_' in self.restricted: self.value += '_'
elif event.key == K_EQUALS and '+' in self.restricted: self.value += '+'
elif event.key == K_LEFTBRACKET and '{' in self.restricted: self.value += '{'
elif event.key == K_RIGHTBRACKET and '}' in self.restricted: self.value += '}'
elif event.key == K_BACKSLASH and '|' in self.restricted: self.value += '|'
elif event.key == K_SEMICOLON and ':' in self.restricted: self.value += ':'
elif event.key == K_QUOTE and '"' in self.restricted: self.value += '"'
elif event.key == K_COMMA and '<' in self.restricted: self.value += '<'
elif event.key == K_PERIOD and '>' in self.restricted: self.value += '>'
elif event.key == K_SLASH and '?' in self.restricted: self.value += '?'
if len(self.value) > self.maxlength and self.maxlength >= 0: self.value = self.value[:-1]