Skip to content

Commit 53f51ca

Browse files
committed
Add arrow key navigation for name input field in TextBox
1 parent 656dcdf commit 53f51ca

File tree

1 file changed

+42
-16
lines changed

1 file changed

+42
-16
lines changed

GUI/GUIObjects.py

+42-16
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def check_click(self):
6464
class Text_box():
6565
def __init__(self,x,y,width,height,bg_color=(155,155,155),active_color=(200,200,200),
6666
text_size=24, text_color=(0,0,0), border=0, border_color=(0,0,0), only_letters=False,
67-
only_numbers=False, placeholder_txt="Text", placeholder_color=(100,100,100), max_chars=-1):
67+
only_numbers=False, placeholder_txt="Text", placeholder_color=(100,100,100), max_chars=-1, ):
6868
self.x = x - width / 2
6969
self.y = y - height / 2
7070
self.width = width
@@ -231,6 +231,7 @@ def __init__(self,rect,**kwargs):
231231
self.render_area = None
232232
self.blink = True
233233
self.blink_timer = 0.0
234+
self.cursor_position = 0
234235
self.process_kwargs(kwargs)
235236

236237
def process_kwargs(self,kwargs):
@@ -253,19 +254,27 @@ def process_kwargs(self,kwargs):
253254
raise KeyError(f"InputBox accepts no keyword {kwarg}.")
254255
self.__dict__.update(defaults)
255256

256-
def get_event(self,event):
257+
def get_event(self, event):
257258
if event.type == pg.KEYDOWN and self.active:
258-
if event.key in (pg.K_RETURN,pg.K_KP_ENTER):
259+
if event.key in (pg.K_RETURN, pg.K_KP_ENTER):
259260
self.execute()
260261
elif event.key == pg.K_BACKSPACE:
261-
if self.buffer:
262-
self.buffer.pop()
262+
if self.cursor_position > 0:
263+
self.buffer.pop(self.cursor_position - 1)
264+
self.cursor_position -= 1
265+
elif event.key == pg.K_LEFT:
266+
if self.cursor_position > 0:
267+
self.cursor_position -= 1
268+
elif event.key == pg.K_RIGHT:
269+
if self.cursor_position < len(self.buffer):
270+
self.cursor_position += 1
263271
elif event.unicode in ACCEPTED:
264272
if len(self.buffer) < self.max_length:
265-
self.buffer.append(event.unicode)
266-
273+
self.buffer.insert(self.cursor_position, event.unicode)
274+
self.cursor_position += 1
267275
elif event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
268276
self.active = self.rect.collidepoint(event.pos)
277+
self.buffer = []
269278

270279
def execute(self):
271280
if self.command:
@@ -291,14 +300,31 @@ def update(self):
291300
self.blink = not self.blink
292301
self.blink_timer = pg.time.get_ticks()
293302

294-
def draw(self,surface):
303+
304+
305+
def draw(self, surface):
306+
# Draw the outline and text box
295307
outline_color = self.active_color if self.active else self.outline_color
296-
outline = self.rect.inflate(self.outline_width*2,self.outline_width*2)
297-
surface.fill(outline_color,outline)
298-
surface.fill(self.color,self.rect)
299-
if self.rendered:
300-
surface.blit(self.rendered,self.render_rect,self.render_area)
308+
outline = self.rect.inflate(self.outline_width * 2, self.outline_width * 2)
309+
surface.fill(outline_color, outline)
310+
surface.fill(self.color, self.rect)
311+
312+
# Render the text in the buffer
313+
if self.buffer:
314+
text_surface = self.font.render("".join(self.buffer), True, self.font_color)
315+
self.rendered = text_surface # Update rendered text
316+
surface.blit(self.rendered, self.rect.move(5, (self.rect.height - text_surface.get_height()) // 2))
317+
318+
# Calculate the cursor position based on buffer content up to cursor_position
319+
cursor_text = self.font.render("".join(self.buffer[:self.cursor_position]), True, self.font_color)
320+
cursor_x = self.rect.x + 5 + cursor_text.get_width() # Position based on text width up to cursor
321+
322+
# Draw the blinking cursor line
301323
if self.blink and self.active:
302-
curse = self.render_area.copy()
303-
curse.topleft = self.render_rect.topleft
304-
surface.fill(self.font_color,(curse.right+1,curse.y,2,curse.h))
324+
pygame.draw.line(surface, self.font_color, (cursor_x, self.rect.y + 5), (cursor_x, self.rect.y + self.rect.height - 5))
325+
326+
# Toggle cursor blink (optional)
327+
self.blink_timer += 0.05
328+
if self.blink_timer >= 1.0:
329+
self.blink = not self.blink
330+
self.blink_timer = 0.0

0 commit comments

Comments
 (0)