-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathTextInput.py
214 lines (168 loc) · 8.28 KB
/
TextInput.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
# -*- coding: utf-8 -*-
import RenderObject, Configuration, Footer
import os, Keys, RenderControl, TaskHandler
import pygame, sys
import Theme
class TextInput(RenderObject.RenderObject):
config = Configuration.getConfiguration()
theme = Configuration.getTheme()
pygame.font.init()
footer = None
background = None
textFont = pygame.font.Font('theme/NotoSans-Regular.ttf', 20)
buttonFont = pygame.font.Font('theme/NotoSans-Regular.ttf', int(config["screenWidth"] * 0.03))
fontCache = {}
buttonSurface = None
selectedChar = "1"
currentRow = 0
currentCol = 0
buttonWidth = config["screenWidth"] * 0.0625 #30px at 480px wide
buttonHeight = buttonWidth
buttonSpace = config["screenWidth"] * 0.02 #30px at 480px wide
chars = []
upperChars = []
lowChars = []
special = []
lowChars.append(["1","2","3","4","5","6","7","8","9","0"])
lowChars.append(["a","b","c","d","e","f","g","h","i","j"])
lowChars.append(["k","l","m","n","o","p","q","r","s","t"])
lowChars.append([u"\u2191",u"\u2026" , "u","v","w","x","y","z", u"\u033A", "OK"])
upperChars.append(["!","\"","§","$","%","&","/","(",")","="])
upperChars.append(["A","B","C","D","E","F","G","H","I","J"])
upperChars.append(["K","L","M","N","O","P","Q","R","S","T"])
upperChars.append([u"\u2191",u"\u2026", "U","V","W","X","Y","Z", u"\u033A", "OK"])
special.append(["","","","","","","","","","?"])
special.append(["°","^","","","","","","","+","*"])
special.append(["<",">","","","","","","\\","'","#"])
special.append(["",u"\u2026", ",",";",".",":","-","_", "", "OK"])
chars = lowChars
def initBackground(self):
self.background = pygame.Surface((self.config["screenWidth"],self.config["screenHeight"]))
self.background.fill(Theme.getColor("textInput/backgroundColor", (131,139,139)))
self.buttonSurface = pygame.Surface((self.buttonWidth, self.buttonHeight))
self.buttonSurface.fill(Theme.getColor("textInput/buttonColor", (191,191,191)))
self.buttonSelectSurface = pygame.Surface((self.buttonWidth, self.buttonHeight))
self.buttonSelectSurface.fill(Theme.getColor("textInput/selectionColor", (255,255,255)))
def renderTextField(self, screen):
field = pygame.Surface((self.config["screenWidth"] - (self.config["screenWidth"] * 0.2) ,30))
field.fill(Theme.getColor("textInput/buttonColor", (191,191,191)))
textSurface = self.textFont.render(self.currentText, True,Theme.getColor("textInput/fontColor", (0,0,0)))
field.blit(textSurface, (0, (field.get_height() - textSurface.get_height()) / 2))
pygame.draw.line(field, Theme.getColor("textInput/fontColor", (0,0,0)), (textSurface.get_width(), 25), (textSurface.get_width() + 10, 25), 2)
textX = (self.config["screenWidth"] - field.get_width()) /2
screen.blit(field, (textX,20))
def renderButtons(self, screen):
leftOffset = (self.config["screenWidth"] - 10* self.buttonWidth - 9 * self.buttonSpace) / 2
yOffset = 80
self.renderButtonLine(screen, self.chars[0],0, leftOffset, yOffset)
yOffset+=self.buttonHeight+self.buttonSpace
self.renderButtonLine(screen, self.chars[1],1, leftOffset, yOffset)
yOffset+=self.buttonHeight+self.buttonSpace
self.renderButtonLine(screen, self.chars[2],2, leftOffset, yOffset)
yOffset+=self.buttonHeight+self.buttonSpace
self.renderButtonLine(screen, self.chars[3],3, leftOffset, yOffset)
def setFooter(self, footer):
self.footer = footer
def updateFooterPos(self, start, target, current, finished):
if(not finished):
self.footer.setYPosition(current)
else:
self.footer.setEnabled(False)
RenderControl.setDirty()
def renderButtonLine(self, screen, line,row, xOffset, yOffset):
offset = xOffset
for i, char in enumerate(line):
charSurface = self.fontCache.get(char)
if(charSurface == None):
charSurface = self.buttonFont.render(char, True, Theme.getColor("textInput/fontColor", (0,0,0)))
self.fontCache[char] = charSurface
if(self.selectedChar == char and i == self.currentCol and row == self.currentRow):
screen.blit(self.buttonSelectSurface, (offset, yOffset))
else:
screen.blit(self.buttonSurface, (offset, yOffset))
charXOffset = (self.buttonWidth - charSurface.get_width()) / 2
charYOffset = (self.buttonHeight - charSurface.get_height()) / 2
screen.blit(charSurface, (offset + charXOffset, yOffset + charYOffset))
offset = offset + self.buttonWidth + self.buttonSpace
def setSelctedChar(self):
if(self.currentRow < 0):
self.currentRow = len(self.chars) - 1
if(self.currentRow > len(self.chars) -1):
self.currentRow = 0
if(self.currentCol < 0):
self.currentCol = len(self.chars[self.currentRow]) -1
if(self.currentCol > len(self.chars[self.currentRow]) -1):
self.currentCol = 0
self.selectedChar = self.chars[self.currentRow][self.currentCol]
def selectChar(self):
#shif
if(self.selectedChar == u"\u2191"):
if(self.chars == self.lowChars):
self.chars = self.upperChars
else:
self.chars = self.lowChars
return
#dots
if(self.selectedChar == u"\u2026"):
if(self.chars != self.special):
self.chars = self.special
else:
self.chars = self.lowChars
return
#space
if(self.selectedChar == u"\u033A"):
self.currentText = self.currentText + " "
return
#enter
if(self.selectedChar == "OK" ):
if(self.callback != None):
self.callback(self.currentText)
return
self.currentText = self.currentText + self.selectedChar
def render(self, screen):
screen.blit(self.background, (0,0))
self.renderTextField(screen)
self.renderButtons(screen)
self.footer.render(screen)
def handleEvents(self, events):
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == Keys.DINGOO_BUTTON_UP:
self.currentRow = self.currentRow - 1
self.setSelctedChar()
RenderControl.setDirty()
if event.key == Keys.DINGOO_BUTTON_DOWN:
self.currentRow = self.currentRow + 1
self.setSelctedChar()
RenderControl.setDirty()
if event.key == Keys.DINGOO_BUTTON_LEFT:
self.currentCol = self.currentCol - 1
self.setSelctedChar()
RenderControl.setDirty()
if event.key == Keys.DINGOO_BUTTON_RIGHT:
self.currentCol = self.currentCol + 1
self.setSelctedChar()
RenderControl.setDirty()
if event.key == Keys.DINGOO_BUTTON_A:
self.selectChar()
RenderControl.setDirty()
if event.key == Keys.DINGOO_BUTTON_Y:
self.currentText = self.currentText[:-1]
RenderControl.setDirty()
if event.key == Keys.DINGOO_BUTTON_B:
if(self.callback != None):
self.callback(self.initialText)
RenderControl.setDirty()
if event.type == pygame.KEYUP:
if event.key == Keys.DINGOO_BUTTON_START:
if(self.callback != None):
self.callback(self.currentText)
RenderControl.setDirty()
def __init__(self, screen, initialText, callback):
self.screen = screen
self.initialText = initialText
self.currentText = str(initialText)
self.callback = callback
self.initBackground()
self.setFooter(Footer.Footer([],[],(255,255,255)))
TaskHandler.addAnimation(self.config["screenHeight"] - self.footer.getHeight(), self.config["screenHeight"], 600, self.updateFooterPos, 1000)