pygame-widget-kit 0.0.3__tar.gz → 0.0.5__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (22) hide show
  1. {pygame_widget_kit-0.0.3 → pygame_widget_kit-0.0.5}/PKG-INFO +2 -2
  2. {pygame_widget_kit-0.0.3 → pygame_widget_kit-0.0.5}/README.md +1 -1
  3. {pygame_widget_kit-0.0.3 → pygame_widget_kit-0.0.5}/pyproject.toml +1 -1
  4. {pygame_widget_kit-0.0.3 → pygame_widget_kit-0.0.5}/src/main.py +4 -0
  5. pygame_widget_kit-0.0.5/src/pygame_widget_kit/TextArea.py +120 -0
  6. {pygame_widget_kit-0.0.3 → pygame_widget_kit-0.0.5}/src/pygame_widget_kit/TextInput.py +243 -1
  7. {pygame_widget_kit-0.0.3 → pygame_widget_kit-0.0.5}/src/pygame_widget_kit/__init__.py +2 -0
  8. {pygame_widget_kit-0.0.3 → pygame_widget_kit-0.0.5}/src/pygame_widget_kit.egg-info/PKG-INFO +2 -2
  9. {pygame_widget_kit-0.0.3 → pygame_widget_kit-0.0.5}/src/pygame_widget_kit.egg-info/SOURCES.txt +1 -0
  10. {pygame_widget_kit-0.0.3 → pygame_widget_kit-0.0.5}/LICENSE +0 -0
  11. {pygame_widget_kit-0.0.3 → pygame_widget_kit-0.0.5}/setup.cfg +0 -0
  12. {pygame_widget_kit-0.0.3 → pygame_widget_kit-0.0.5}/src/pygame_widget_kit/Button.py +0 -0
  13. {pygame_widget_kit-0.0.3 → pygame_widget_kit-0.0.5}/src/pygame_widget_kit/Radio.py +0 -0
  14. {pygame_widget_kit-0.0.3 → pygame_widget_kit-0.0.5}/src/pygame_widget_kit/Select.py +0 -0
  15. {pygame_widget_kit-0.0.3 → pygame_widget_kit-0.0.5}/src/pygame_widget_kit/Slider.py +0 -0
  16. {pygame_widget_kit-0.0.3 → pygame_widget_kit-0.0.5}/src/pygame_widget_kit/Text.py +0 -0
  17. {pygame_widget_kit-0.0.3 → pygame_widget_kit-0.0.5}/src/pygame_widget_kit/UIComponent.py +0 -0
  18. {pygame_widget_kit-0.0.3 → pygame_widget_kit-0.0.5}/src/pygame_widget_kit/UIManager.py +0 -0
  19. {pygame_widget_kit-0.0.3 → pygame_widget_kit-0.0.5}/src/pygame_widget_kit/Widget.py +0 -0
  20. {pygame_widget_kit-0.0.3 → pygame_widget_kit-0.0.5}/src/pygame_widget_kit.egg-info/dependency_links.txt +0 -0
  21. {pygame_widget_kit-0.0.3 → pygame_widget_kit-0.0.5}/src/pygame_widget_kit.egg-info/requires.txt +0 -0
  22. {pygame_widget_kit-0.0.3 → pygame_widget_kit-0.0.5}/src/pygame_widget_kit.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pygame-widget-kit
3
- Version: 0.0.3
3
+ Version: 0.0.5
4
4
  Summary: A GUI framework built on Pygame. Designed as a retained-mode UI system with a component tree, extensible widgets, and focus management.
5
5
  Author-email: Sinan Orgu <sinanorguuu@gmail.com>
6
6
  License: MIT
@@ -33,7 +33,7 @@ PyPI: https://pypi.org/project/pygame-widget-kit/
33
33
  - Retained-mode component tree with nested children
34
34
  - Event routing with hover, focus, and active state handling
35
35
  - Modal/dropdown support via `UIManager`
36
- - Built-in widgets: `Button`, `Text`, `Select`, `Radio`, `TextInput`, `Slider`
36
+ - Built-in widgets: `Button`, `Text`, `Select`, `Radio`, `TextInput`, `Slider`,`TextArea`,`TextInput2D`
37
37
  - Text input filtering modes (text, number, hex, binary, octal)
38
38
 
39
39
 
@@ -11,7 +11,7 @@ PyPI: https://pypi.org/project/pygame-widget-kit/
11
11
  - Retained-mode component tree with nested children
12
12
  - Event routing with hover, focus, and active state handling
13
13
  - Modal/dropdown support via `UIManager`
14
- - Built-in widgets: `Button`, `Text`, `Select`, `Radio`, `TextInput`, `Slider`
14
+ - Built-in widgets: `Button`, `Text`, `Select`, `Radio`, `TextInput`, `Slider`,`TextArea`,`TextInput2D`
15
15
  - Text input filtering modes (text, number, hex, binary, octal)
16
16
 
17
17
 
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "pygame-widget-kit"
7
- version = "0.0.3"
7
+ version = "0.0.5"
8
8
  authors = [
9
9
  { name="Sinan Orgu", email="sinanorguuu@gmail.com" },
10
10
  ]
@@ -96,6 +96,10 @@ def main():
96
96
  root.add_child(slider)
97
97
  root.add_child(slider_value)
98
98
 
99
+ text_area_str = "hello bu bir deneme yazisidir bakalaim bu bolme islemi duzgun bir sekilde yapilabilcek mi acaba??"
100
+ text_area = TextArea((20,650,300,200),text_area_str,text_color=(0,0,0),max_chars_per_line=20)
101
+ root.add_child(text_area)
102
+
99
103
 
100
104
 
101
105
 
@@ -0,0 +1,120 @@
1
+ import pygame
2
+ from .UIComponent import *
3
+
4
+
5
+ class TextArea(UIComponent):
6
+ def __init__(
7
+ self,
8
+ rect,
9
+ text_str="",
10
+ font_size=25,
11
+ font_type="Veranda",
12
+ text_color: tuple[int, int, int] = (127, 127, 127),
13
+ bg_color=None,
14
+ hover_color=None,
15
+ padding=6,
16
+ line_spacing=0,
17
+ max_chars_per_line=None,
18
+ style=None,
19
+ z_index=0,
20
+ border_color=None,
21
+ ):
22
+ self.font_size = font_size
23
+ self.font_type = font_type
24
+ self.font = pygame.font.SysFont(self.font_type, self.font_size)
25
+
26
+ self.text_str = text_str
27
+ self.text_color = text_color
28
+ self.padding = padding
29
+ self.line_spacing = line_spacing
30
+ self.max_chars_per_line = max_chars_per_line
31
+
32
+ super().__init__(rect, style, z_index, bg_color, border_color, hover_color)
33
+
34
+ self._rebuild_lines()
35
+
36
+ def _wrap_line(self, line):
37
+ if self.max_chars_per_line is None or self.max_chars_per_line <= 0:
38
+ return [line]
39
+
40
+ words = line.split(" ")
41
+ if not words:
42
+ return [line]
43
+
44
+ wrapped = []
45
+ current = ""
46
+
47
+ for word in words:
48
+ if current == "":
49
+ if len(word) > self.max_chars_per_line:
50
+ wrapped.append(word)
51
+ else:
52
+ current = word
53
+ continue
54
+
55
+ candidate_len = len(current) + 1 + len(word)
56
+ if candidate_len <= self.max_chars_per_line:
57
+ current = f"{current} {word}"
58
+ else:
59
+ wrapped.append(current)
60
+ if len(word) > self.max_chars_per_line:
61
+ wrapped.append(word)
62
+ current = ""
63
+ else:
64
+ current = word
65
+
66
+ if current != "":
67
+ wrapped.append(current)
68
+
69
+ return wrapped
70
+
71
+ def _rebuild_lines(self):
72
+ raw_lines = self.text_str.split("\n")
73
+ self.lines = []
74
+ for raw_line in raw_lines:
75
+ if raw_line == "":
76
+ self.lines.append("")
77
+ continue
78
+ self.lines.extend(self._wrap_line(raw_line))
79
+ self.line_height = self.font.get_linesize()
80
+ self.line_renders = [
81
+ self.font.render(line, True, self.text_color, None) for line in self.lines
82
+ ]
83
+
84
+ def set_text(self, new_text_str):
85
+ self.text_str = new_text_str
86
+ self._rebuild_lines()
87
+
88
+ def update_font_size(self, new_size):
89
+ self.font_size = new_size
90
+ self.font = pygame.font.SysFont(self.font_type, self.font_size)
91
+ self._rebuild_lines()
92
+
93
+ def update_font_type(self, new_type):
94
+ self.font_type = new_type
95
+ self.font = pygame.font.SysFont(self.font_type, self.font_size)
96
+ self._rebuild_lines()
97
+
98
+ def draw(self, surface):
99
+ if not self.visible:
100
+ return
101
+
102
+ super().draw(surface)
103
+
104
+ if not self.lines:
105
+ return
106
+
107
+ color = self.text_color
108
+ if not self.enabled:
109
+ color = tuple(c // 2 for c in color)
110
+
111
+ x = self.absolute_rect[0] + self.padding
112
+ y = self.absolute_rect[1] + self.padding
113
+ step = self.line_height + self.line_spacing
114
+
115
+ for i, line in enumerate(self.lines):
116
+ if color == self.text_color:
117
+ render = self.line_renders[i]
118
+ else:
119
+ render = self.font.render(line, True, color, None)
120
+ surface.blit(render, (x, y + i * step))
@@ -197,7 +197,7 @@ class TextInput(UIComponent):
197
197
  self.selection_end = None
198
198
 
199
199
 
200
- def handle_event(self, event:pygame.Event):
200
+ def handle_event(self, event:pygame.event.Event):
201
201
  if not self.focused or not self.enabled:
202
202
  return
203
203
 
@@ -343,3 +343,245 @@ class TextInput(UIComponent):
343
343
  self.caret_color,
344
344
  (cx, cy, 2, self.text.render.get_height())
345
345
  )
346
+
347
+
348
+
349
+ class TextInput2D(UIComponent):
350
+ def __init__(
351
+ self,
352
+ rect,
353
+ initial_text="",
354
+ text_color=(0, 0, 0),
355
+ bg_color=(220, 220, 220),
356
+ hover_color=(240, 240, 240),
357
+ selection_color=(100, 150, 255),
358
+ caret_color=(0, 0, 0),
359
+ padding=6,
360
+ font_size=25,
361
+ font_type='Veranda',
362
+ z_index=0
363
+ ):
364
+ super().__init__(
365
+ rect=rect,
366
+ z_index=z_index,
367
+ color=bg_color,
368
+ hover_color=hover_color
369
+ )
370
+
371
+ self.text_color = text_color
372
+ self.selection_color = selection_color
373
+ self.caret_color = caret_color
374
+ self.padding = padding
375
+ self.font_size = font_size
376
+ self.font_type = font_type
377
+ self.font = pygame.font.SysFont(self.font_type, self.font_size)
378
+
379
+ self.lines = initial_text.split('\n') if initial_text else ['']
380
+ self.cursor_line = 0
381
+ self.cursor_col = 0
382
+
383
+ self.selection_start = None
384
+ self.selection_end = None
385
+ self.dragging = False
386
+
387
+ # caret blink
388
+ self.caret_visible = True
389
+ self._caret_timer = 0
390
+ self._caret_interval = 0.5
391
+ self.last_blinked_at = time.time()
392
+
393
+ def get_text(self):
394
+ return '\n'.join(self.lines)
395
+
396
+ def set_text(self, text):
397
+ self.lines = text.split('\n') if text else ['']
398
+ self.cursor_line = min(self.cursor_line, len(self.lines) - 1)
399
+ self.cursor_col = min(self.cursor_col, len(self.lines[self.cursor_line]))
400
+
401
+ def _get_line_height(self):
402
+ return self.font.get_height()
403
+
404
+ def _get_text_width(self, text):
405
+ return self.font.size(text)[0]
406
+
407
+ def _mouse_to_pos(self, mouse_x, mouse_y):
408
+ local_x = mouse_x - self.absolute_rect[0] - self.padding
409
+ local_y = mouse_y - self.absolute_rect[1] - self.padding
410
+
411
+ line_height = self._get_line_height()
412
+ line = int(local_y // line_height)
413
+ line = max(0, min(line, len(self.lines) - 1))
414
+
415
+ line_text = self.lines[line]
416
+ col = 0
417
+ for i in range(len(line_text) + 1):
418
+ if self._get_text_width(line_text[:i]) > local_x:
419
+ col = i - 1
420
+ break
421
+ else:
422
+ col = len(line_text)
423
+
424
+ return line, col
425
+
426
+ def on_click(self, event):
427
+ self.dragging = True
428
+ self.cursor_line, self.cursor_col = self._mouse_to_pos(event.pos[0], event.pos[1])
429
+ self.selection_start = (self.cursor_line, self.cursor_col)
430
+ self.selection_end = None
431
+
432
+ def handle_event(self, event: pygame.event.Event):
433
+ if not self.focused or not self.enabled:
434
+ return
435
+
436
+ # MOUSE DRAG SELECTION
437
+ if event.type == pygame.MOUSEMOTION and self.dragging:
438
+ line, col = self._mouse_to_pos(event.pos[0], event.pos[1])
439
+ self.selection_end = (line, col)
440
+ self.cursor_line, self.cursor_col = line, col
441
+
442
+ if event.type == pygame.MOUSEBUTTONUP:
443
+ self.dragging = False
444
+ if self.selection_start == self.selection_end:
445
+ self.selection_start = self.selection_end = None
446
+
447
+ if event.type == pygame.KEYDOWN:
448
+ # BACKSPACE
449
+ if event.key == pygame.K_BACKSPACE:
450
+ if self.has_selection():
451
+ self.delete_selection()
452
+ elif self.cursor_col > 0:
453
+ self.lines[self.cursor_line] = (
454
+ self.lines[self.cursor_line][:self.cursor_col - 1]
455
+ + self.lines[self.cursor_line][self.cursor_col:]
456
+ )
457
+ self.cursor_col -= 1
458
+ elif self.cursor_line > 0:
459
+ # Join with previous line
460
+ prev_line = self.lines[self.cursor_line - 1]
461
+ self.cursor_col = len(prev_line)
462
+ self.lines[self.cursor_line - 1] += self.lines[self.cursor_line]
463
+ del self.lines[self.cursor_line]
464
+ self.cursor_line -= 1
465
+
466
+ # ENTER
467
+ elif event.key == pygame.K_RETURN:
468
+ if self.has_selection():
469
+ self.delete_selection()
470
+ line = self.lines[self.cursor_line]
471
+ self.lines[self.cursor_line] = line[:self.cursor_col]
472
+ self.lines.insert(self.cursor_line + 1, line[self.cursor_col:])
473
+ self.cursor_line += 1
474
+ self.cursor_col = 0
475
+
476
+ # ARROW KEYS
477
+ elif event.key == pygame.K_LEFT:
478
+ if self.cursor_col > 0:
479
+ self.cursor_col -= 1
480
+ elif self.cursor_line > 0:
481
+ self.cursor_line -= 1
482
+ self.cursor_col = len(self.lines[self.cursor_line])
483
+ elif event.key == pygame.K_RIGHT:
484
+ if self.cursor_col < len(self.lines[self.cursor_line]):
485
+ self.cursor_col += 1
486
+ elif self.cursor_line < len(self.lines) - 1:
487
+ self.cursor_line += 1
488
+ self.cursor_col = 0
489
+ elif event.key == pygame.K_UP:
490
+ if self.cursor_line > 0:
491
+ self.cursor_line -= 1
492
+ self.cursor_col = min(self.cursor_col, len(self.lines[self.cursor_line]))
493
+ elif event.key == pygame.K_DOWN:
494
+ if self.cursor_line < len(self.lines) - 1:
495
+ self.cursor_line += 1
496
+ self.cursor_col = min(self.cursor_col, len(self.lines[self.cursor_line]))
497
+
498
+ # TYPING
499
+ elif event.unicode and event.unicode.isprintable():
500
+ if self.has_selection():
501
+ self.delete_selection()
502
+ line = self.lines[self.cursor_line]
503
+ self.lines[self.cursor_line] = line[:self.cursor_col] + event.unicode + line[self.cursor_col:]
504
+ self.cursor_col += 1
505
+
506
+ def has_selection(self):
507
+ return (
508
+ self.selection_start is not None
509
+ and self.selection_end is not None
510
+ and self.selection_start != self.selection_end
511
+ )
512
+
513
+ def get_selection_range(self):
514
+ # For simplicity, assume single line selection or handle multi-line later
515
+ # This is complex, for now assume same line
516
+ if not self.has_selection():
517
+ return None
518
+ start_line, start_col = self.selection_start
519
+ end_line, end_col = self.selection_end
520
+ if start_line != end_line:
521
+ # Multi-line selection, for now just clear
522
+ return None
523
+ return sorted((start_col, end_col))
524
+
525
+ def delete_selection(self):
526
+ if not self.has_selection():
527
+ return
528
+ start_line, start_col = self.selection_start
529
+ end_line, end_col = self.selection_end
530
+ if start_line == end_line:
531
+ line = self.lines[start_line]
532
+ a, b = sorted((start_col, end_col))
533
+ self.lines[start_line] = line[:a] + line[b:]
534
+ self.cursor_line = start_line
535
+ self.cursor_col = a
536
+ # Multi-line delete not implemented yet
537
+ self.selection_start = self.selection_end = None
538
+
539
+ def update(self):
540
+ if not self.focused:
541
+ self.caret_visible = False
542
+ return
543
+
544
+ if time.time() - self.last_blinked_at >= self._caret_interval:
545
+ self.last_blinked_at = time.time()
546
+ self.caret_visible = not self.caret_visible
547
+
548
+ def draw(self, surface):
549
+ super().draw(surface)
550
+ self.update()
551
+
552
+ line_height = self._get_line_height()
553
+ y = self.absolute_rect[1] + self.padding
554
+
555
+ for i, line in enumerate(self.lines):
556
+ render = self.font.render(line, True, self.text_color)
557
+ surface.blit(render, (self.absolute_rect[0] + self.padding, y))
558
+ y += line_height
559
+
560
+ # CARET
561
+ if self.focused and self.caret_visible:
562
+ caret_x = self.absolute_rect[0] + self.padding + self._get_text_width(self.lines[self.cursor_line][:self.cursor_col])
563
+ caret_y = self.absolute_rect[1] + self.padding + self.cursor_line * line_height
564
+
565
+ pygame.draw.rect(
566
+ surface,
567
+ self.caret_color,
568
+ (caret_x, caret_y, 2, line_height)
569
+ )
570
+
571
+ # SELECTION - simplified, only same line
572
+ if self.has_selection():
573
+ sel_range = self.get_selection_range()
574
+ if sel_range:
575
+ a, b = sel_range
576
+ line = self.lines[self.cursor_line]
577
+ x1 = self.absolute_rect[0] + self.padding + self._get_text_width(line[:a])
578
+ x2 = self.absolute_rect[0] + self.padding + self._get_text_width(line[:b])
579
+ y = self.absolute_rect[1] + self.padding + self.cursor_line * line_height
580
+
581
+ pygame.draw.rect(
582
+ surface,
583
+ self.selection_color,
584
+ (x1, y, x2 - x1, line_height)
585
+ )
586
+
587
+
@@ -7,3 +7,5 @@ from .Select import*
7
7
  from .TextInput import*
8
8
  from .Radio import*
9
9
  from .Slider import*
10
+ from .TextArea import*
11
+ from .TextArea import*
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pygame-widget-kit
3
- Version: 0.0.3
3
+ Version: 0.0.5
4
4
  Summary: A GUI framework built on Pygame. Designed as a retained-mode UI system with a component tree, extensible widgets, and focus management.
5
5
  Author-email: Sinan Orgu <sinanorguuu@gmail.com>
6
6
  License: MIT
@@ -33,7 +33,7 @@ PyPI: https://pypi.org/project/pygame-widget-kit/
33
33
  - Retained-mode component tree with nested children
34
34
  - Event routing with hover, focus, and active state handling
35
35
  - Modal/dropdown support via `UIManager`
36
- - Built-in widgets: `Button`, `Text`, `Select`, `Radio`, `TextInput`, `Slider`
36
+ - Built-in widgets: `Button`, `Text`, `Select`, `Radio`, `TextInput`, `Slider`,`TextArea`,`TextInput2D`
37
37
  - Text input filtering modes (text, number, hex, binary, octal)
38
38
 
39
39
 
@@ -7,6 +7,7 @@ src/pygame_widget_kit/Radio.py
7
7
  src/pygame_widget_kit/Select.py
8
8
  src/pygame_widget_kit/Slider.py
9
9
  src/pygame_widget_kit/Text.py
10
+ src/pygame_widget_kit/TextArea.py
10
11
  src/pygame_widget_kit/TextInput.py
11
12
  src/pygame_widget_kit/UIComponent.py
12
13
  src/pygame_widget_kit/UIManager.py