@@ -64,7 +64,7 @@ def check_click(self):
64
64
class Text_box ():
65
65
def __init__ (self ,x ,y ,width ,height ,bg_color = (155 ,155 ,155 ),active_color = (200 ,200 ,200 ),
66
66
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 , ):
68
68
self .x = x - width / 2
69
69
self .y = y - height / 2
70
70
self .width = width
@@ -231,6 +231,7 @@ def __init__(self,rect,**kwargs):
231
231
self .render_area = None
232
232
self .blink = True
233
233
self .blink_timer = 0.0
234
+ self .cursor_position = 0
234
235
self .process_kwargs (kwargs )
235
236
236
237
def process_kwargs (self ,kwargs ):
@@ -253,19 +254,27 @@ def process_kwargs(self,kwargs):
253
254
raise KeyError (f"InputBox accepts no keyword { kwarg } ." )
254
255
self .__dict__ .update (defaults )
255
256
256
- def get_event (self ,event ):
257
+ def get_event (self , event ):
257
258
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 ):
259
260
self .execute ()
260
261
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
263
271
elif event .unicode in ACCEPTED :
264
272
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
267
275
elif event .type == pg .MOUSEBUTTONDOWN and event .button == 1 :
268
276
self .active = self .rect .collidepoint (event .pos )
277
+ self .buffer = []
269
278
270
279
def execute (self ):
271
280
if self .command :
@@ -291,14 +300,31 @@ def update(self):
291
300
self .blink = not self .blink
292
301
self .blink_timer = pg .time .get_ticks ()
293
302
294
- def draw (self ,surface ):
303
+
304
+
305
+ def draw (self , surface ):
306
+ # Draw the outline and text box
295
307
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
301
323
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