Create simple rectangle widget master
authorStan_Lewry <stanley.jml@gmail.com>
Sat, 24 Jul 2021 10:45:31 +0000 (11:45 +0100)
committerStan_Lewry <stanley.jml@gmail.com>
Sat, 24 Jul 2021 10:45:31 +0000 (11:45 +0100)
Assets/GUIScenes/TestScene.json
GUI/GuiManager.py
GUI/RectangleWidget.py [new file with mode: 0644]

index dba393d..e5d00fc 100644 (file)
@@ -1,5 +1,34 @@
 {
     "widgets": [
+        {
+            "type" : "Rectangle",
+            "props" : {
+                "x" : 0,
+                "y" : 0,
+                "w" : 1000,
+                "h" : 1000,
+                "fillColour" : "0, 255, 0",
+                "opacity"   : 0.5,
+                "border"    : false,
+                "borderThickness" : 5,
+                "borderColour" :  "0, 158, 0"
+            }
+        },
+        {
+            "type" : "Rectangle",
+            "props" : {
+                "x" : 380,
+                "y" : 380,
+                "w" : 400,
+                "h" : 600,
+                "fill" : true,
+                "fillColour" : "255, 130, 228",
+                "opacity"   : 1,
+                "border"    : true,
+                "borderThickness" : 2,
+                "borderColour" :  "181, 38, 150"
+            }
+        },
         {
             "type" : "BasicButton",
             "props" : {
index d3593cc..e96f982 100644 (file)
@@ -1,7 +1,8 @@
+from GUI.RectangleWidget import RectangleWidget
 import json
 from dataclasses import dataclass
-import GUI.ImageWidget as ImageWidget
-import GUI.BasicButtonWidget as BasicButtonWidget
+from GUI.ImageWidget import ImageWidget
+from GUI.BasicButtonWidget import BasicButtonWidget
 
 @dataclass
 class Event:
@@ -44,13 +45,29 @@ class GuiManager:
                         props["fontSize"],
                         tuple(map(int, props["colour"].split(', ')))
                     )
+                elif widget["type"] == "Rectangle" :
+                    props = widget["props"]
+                    self.createRectangleWidget(
+                        props["x"],
+                        props["y"],
+                        props["w"],
+                        props["h"],
+                        tuple(map(int, props["fillColour"].split(', '))),
+                        props["opacity"],
+                        props["border"],
+                        props["borderThickness"],
+                        tuple(map(int, props["borderColour"].split(', ')))
+                    )
 
     
     def createImageWidget(self, x, y, w, h, path, fit) -> None:
-        self.widgetList.append(ImageWidget.ImageWidget(self, x, y, w, h, path, fit))
+        self.widgetList.append(ImageWidget(self, x, y, w, h, path, fit))
 
     def createBasicButtonWidget(self, x, y, w, h, event, text, font, fontSize, colour) -> None:
-        self.widgetList.append(BasicButtonWidget.BasicButtonWidget(self, x, y, w, h, event, text, font, fontSize, colour))
+        self.widgetList.append(BasicButtonWidget(self, x, y, w, h, event, text, font, fontSize, colour))
+
+    def createRectangleWidget(self, x, y, w, h, fillColour, opacity, border, borderThickness, borderColour):
+        self.widgetList.append(RectangleWidget(self, x, y, w, h, fillColour, opacity, border, borderThickness, borderColour))
 
     def handleMouseEvent(self, mouseX, mouseY, mouseDown) -> None:
         for widget in self.widgetList:
diff --git a/GUI/RectangleWidget.py b/GUI/RectangleWidget.py
new file mode 100644 (file)
index 0000000..7ae121f
--- /dev/null
@@ -0,0 +1,40 @@
+import GUI.Widget as Widget
+import pygame
+
+class RectangleWidget(Widget.Widget):
+    def __init__(self, gui, x, y, w, h, fillColour, opacity, border,
+    borderThickness, borderColour) -> None:
+        super().__init__(gui)
+
+        self.screenX = x
+        self.screenY = y
+        self.screenW = w
+        self.screenH = h
+
+        self.fillColour = fillColour
+        self.opacity = opacity
+        self.border = border
+        self.borderThickness = borderThickness
+        self.borderColour = borderColour
+
+        
+        self.surface = pygame.Surface((self.screenW, self.screenH))
+        self.surface.set_alpha(256 * self.opacity)
+        self.surface.fill(self.fillColour)
+
+        if self.border:
+            borderSurf = pygame.Surface((self.screenW, self.borderThickness))
+            borderSurf.fill(self.borderColour)
+            self.surface.blit(borderSurf, (0, 0))
+
+            borderSurf = pygame.Surface((self.screenW, self.borderThickness))
+            borderSurf.fill(self.borderColour)
+            self.surface.blit(borderSurf, (0, self.screenH - self.borderThickness))
+
+            borderSurf = pygame.Surface((self.borderThickness, self.screenH))
+            borderSurf.fill(self.borderColour)
+            self.surface.blit(borderSurf, (0, 0))
+
+            borderSurf = pygame.Surface((self.borderThickness, self.screenH))
+            borderSurf.fill(self.borderColour)
+            self.surface.blit(borderSurf, (self.screenW - self.borderThickness, 0))
\ No newline at end of file