]> Skullheadx's Git Forge - PygameGUIEngine.git/commitdiff
Label
authorSkullheadx <admonty1@gmail.com>
Fri, 14 Apr 2023 00:17:55 +0000 (20:17 -0400)
committerSkullheadx <admonty1@gmail.com>
Fri, 14 Apr 2023 00:17:55 +0000 (20:17 -0400)
text + rectangle

main.py
text.py

diff --git a/main.py b/main.py
index d454a3c11db204478ab84145625e15003aa79221..b1337e09ccc4c16943d7f216c525560c105a6723 100644 (file)
--- a/main.py
+++ b/main.py
@@ -1,7 +1,7 @@
 import pygame
 from color import Color
 from rectangle import Rectangle
-from text import Text
+from text import Text, Label
 
 pygame.init()
 
@@ -12,7 +12,7 @@ delta = 0
 
 # Testing Text
 a = Text(100, 100, "Hello World", "Arial", 20, color=Color.RED)
-b = Text(100, 200, "Word hunt", "Imprint Shadow", 20)
+b = Label(100, 200, "Word hunt", "Imprint Shadow", 20, Color.RED, Color.BLACK, Color.BLACK, 1, 15)
 c = Rectangle(100, 300, 100, 100, (255, 0, 0))
 
 is_running = True
diff --git a/text.py b/text.py
index 32f33841ad19187a4f91e2d729488d219178d915..81a285866010a305d730f77fc9fecdbf2050bd2f 100644 (file)
--- a/text.py
+++ b/text.py
@@ -11,12 +11,18 @@ class Text:
         self.text = text
         self.font_size = font_size
         self.font = pygame.font.SysFont(font, self.font_size)
-        self.color = color
+        self.text_color = color
 
-        self.text_surface = self.font.render(self.text, True, self.color)
+        self.text_surface = self.font.render(self.text, True, self.text_color)
+
+    def get_width(self):
+        return self.text_surface.get_width()
+
+    def get_height(self):
+        return self.text_surface.get_height()
 
     def update(self):
-        self.text_surface = self.font.render(self.text, True, self.color)
+        self.text_surface = self.font.render(self.text, True, self.text_color)
 
     def set_position(self, x, y):
         self.x = x
@@ -35,9 +41,42 @@ class Text:
         self.font = pygame.font.SysFont(font, font_size)
         self.update()
 
-    def set_color(self, color):
-        self.color = color
+    def set_color(self, text_color):
+        self.text_color = text_color
         self.update()
 
     def draw(self, screen):
         screen.blit(self.text_surface, (self.x, self.y))
+
+
+class Label(Text):
+    padding = 10
+    def __init__(self, x, y, text, font, font_size, background_color, text_color, border_color=Color.BLACK, border_width=0,
+                 border_radius=0):
+        super().__init__(x, y, text, font, font_size, text_color)
+        self.background_color = background_color
+        self.border_color = border_color
+        self.border_width = border_width
+        self.border_radius = border_radius
+
+        self.width = self.text_surface.get_width() + self.padding * 2
+        self.height = self.text_surface.get_height() + self.padding * 2
+        self.rect = Rectangle(self.x - self.padding, self.y - self.padding, self.width, self.height, self.background_color, self.border_color, self.border_width,self.border_radius)
+
+    def set_position(self, x, y):
+        super().set_position(x, y)
+        self.rect.set_position(self.x - self.padding, self.y - self.padding)
+
+    def move(self, dx, dy):
+        super().move(dx, dy)
+        self.rect.move(dx, dy)
+
+    def update(self):
+        super().update()
+        self.width = self.text_surface.get_width() + self.padding * 2
+        self.height = self.text_surface.get_height() + self.padding * 2
+        self.rect.set_size(self.width, self.height)
+
+    def draw(self, screen):
+        self.rect.draw(screen)
+        super().draw(screen)