-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextInput.py
executable file
·145 lines (143 loc) · 6.75 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
# This file is part of BobGUI, a free GUI library for Pygame.
# Copyright (C) 2016 Lumidify Productions <lumidify@openmailbox.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import sys
import pygame
from Cursor import Cursor
from pygame.locals import *
from SelectableText import SelectableText
class TextInput(SelectableText):
def __init__(self, screen, **kwargs):
super().__init__(screen, **kwargs)
self.rect = Rect(0, 0, kwargs.get("height", 0), kwargs.get("width", 0))
self.multiline = kwargs.get("multiline", True)
self.cursor = Cursor(height=self.textheight)
self.focused = False
self.highlighted = False
def update(self, event=None):
super().update(event)
if not None in [self.x2, self.y2]:
self.cursor.x_index = self.x2
self.cursor.y_index = self.y2
significant_event = False
mouse_pos = pygame.mouse.get_pos()
if event.type == MOUSEBUTTONDOWN and event.button == 1 and self.rect.collidepoint(mouse_pos):
self.focused = True
self.cursor.start()
elif event.type == MOUSEBUTTONDOWN and event.button == 1 and not self.rect.collidepoint(mouse_pos):
self.focused = False
self.cursor.stop()
elif event.type == MOUSEMOTION:
if self.pressed:
significant_event = True
if self.rect.collidepoint(mouse_pos):
self.highlighted = True
else:
self.highlighted = False
elif event.type == KEYDOWN and self.focused:
significant_event = True
if event.key == K_BACKSPACE:
new_indeces = self.delete_selection()
if not new_indeces:
self.cursor.x_index -= 1
if self.cursor.x_index < 0:
if self.cursor.y_index > 0:
self.cursor.y_index -= 1
self.cursor.x_index = len(self.final_lines[self.cursor.y_index].text)
self.join_lines(self.cursor.y_index, self.cursor.y_index + 1)
else:
self.cursor.x_index = 0
else:
self.delete((self.cursor.x_index, self.cursor.y_index))
else:
self.cursor.x_index, self.cursor.y_index = new_indeces
elif event.key == K_DELETE:
new_indeces = self.delete_selection()
if not new_indeces:
if self.cursor.x_index < len(self.final_lines[self.cursor.y_index].text):
self.delete((self.cursor.x_index, self.cursor.y_index))
else:
if self.cursor.y_index < len(self.final_lines) - 1:
self.join_lines(self.cursor.x_index, self.cursor.y_index + 1)
else:
self.cursor.x_index, self.cursor.y_index = new_indeces
elif event.key == K_RETURN:
if self.multiline:
new_indeces = self.delete_selection()
if new_indeces:
self.cursor.x_index, self.cursor.y_index = new_indeces
self.split_line((self.cursor.x_index, self.cursor.y_index))
self.cursor.x_index = 0
self.cursor.y_index += 1
elif event.key == K_UP:
pos = self.get_index_pos((self.cursor.x_index, self.cursor.y_index))
self.cursor.x_index, self.cursor.y_index = self.get_nearest_index((pos[0], pos[1] - self.textheight))
elif event.key == K_DOWN:
pos = self.get_index_pos((self.cursor.x_index, self.cursor.y_index))
self.cursor.x_index, self.cursor.y_index = self.get_nearest_index((pos[0], pos[1] + self.lineheight))
elif event.key == K_LEFT:
if self.cursor.x_index > 0:
self.cursor.x_index -= 1
elif self.cursor.y_index > 0:
self.cursor.y_index -= 1
self.cursor.x_index = len(self.final_lines[self.cursor.y_index].text)
elif event.key == K_RIGHT:
if self.cursor.x_index < len(self.final_lines[self.cursor.y_index].text):
self.cursor.x_index += 1
elif self.cursor.y_index < len(self.final_lines) - 1:
self.cursor.y_index += 1
self.cursor.x_index = 0
else:
new_indeces = self.delete_selection()
if new_indeces:
self.cursor.x_index, self.cursor.y_index = new_indeces
character = event.unicode
if len(character) > 0:
self.select_none()
self.insert((self.cursor.x_index, self.cursor.y_index), character)
self.cursor.x_index += 1
if significant_event:
self.cursor.event()
self.change_color()
def change_color(self):
if self.highlighted:
self.color = (0, 0, 200)
elif self.focused:
self.color = (0, 0, 150)
else:
self.color = (0, 0, 100)
def draw(self):
self.cursor.update()
pygame.draw.rect(self.screen, self.color, self.rect, 2)
super().draw()
self.cursor.draw(self.screen, self.get_index_pos((self.cursor.x_index, self.cursor.y_index)))
if __name__ == "__main__":
pygame.init()
pygame.key.set_repeat(500, 20)
screen = pygame.display.set_mode((500, 500))
gui = [TextInput(screen, width=500, height=500, text="Hello, how are you?dsfjsdafsdkl;fjsdlafjkasd;lfjs;lfj; sdfjksdl;jf dfsdfj safljsdfj sdfkjsdl sdjfsdklfjas jfj klajsdfklja lfdj sdfa kjdlsf\nsfsdfkshdfjklshka fhsdk dfj asdfhklh kfhasdfkj", align="center")]
while True:
screen.fill((255, 255, 255))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
else:
for widget in gui:
widget.update(event)
for widget in gui:
widget.draw()
pygame.display.update()