Initial implementation of GUI framework. Created GuiManager and to manage gui widgets...
authorStan_Lewry <stanley.jml@gmail.com>
Wed, 21 Jul 2021 23:48:12 +0000 (00:48 +0100)
committerStan_Lewry <stanley.jml@gmail.com>
Wed, 21 Jul 2021 23:48:12 +0000 (00:48 +0100)
Assets/GUIScenes/TestScene.json [new file with mode: 0644]
Assets/smile.png [new file with mode: 0644]
Assets/test.png [new file with mode: 0644]
GUI/GuiManager.py [new file with mode: 0644]
GUI/ImageWidget.py [new file with mode: 0644]
GUI/Widget.py [new file with mode: 0644]
Main.py
Renderer.py
Writeup/worldgen_vision.png [new file with mode: 0644]

diff --git a/Assets/GUIScenes/TestScene.json b/Assets/GUIScenes/TestScene.json
new file mode 100644 (file)
index 0000000..76c1329
--- /dev/null
@@ -0,0 +1,26 @@
+{
+    "widgets": [
+        {
+            "type" : "ImageWidget",
+            "props" : {
+                "source" : "smile.png",
+                "x" : 100,
+                "y" : 100,
+                "w" : 400,
+                "h" : 500,
+                "fit" : true
+            }
+        },
+        {
+            "type": "ImageWidget",
+            "props" : {
+                "w" : 250,
+                "h" : 250,
+                "x" : 1200,
+                "y" : 600,
+                "source" : "test.png",
+                "fit" : true
+            }
+        }
+    ]
+}
\ No newline at end of file
diff --git a/Assets/smile.png b/Assets/smile.png
new file mode 100644 (file)
index 0000000..2c8de3b
Binary files /dev/null and b/Assets/smile.png differ
diff --git a/Assets/test.png b/Assets/test.png
new file mode 100644 (file)
index 0000000..6d985db
Binary files /dev/null and b/Assets/test.png differ
diff --git a/GUI/GuiManager.py b/GUI/GuiManager.py
new file mode 100644 (file)
index 0000000..848f967
--- /dev/null
@@ -0,0 +1,42 @@
+import json
+from dataclasses import dataclass
+import GUI.ImageWidget as ImageWidget
+
+@dataclass
+class Event:
+    event : str
+
+class GuiManager:
+    def __init__(self) -> None:
+        self.widgetList = []
+        self.eventList = []
+    
+    def loadWidget(self, jsonStr):
+        j = json.loads(jsonStr)
+        print(j)
+
+    def loadScene(self, path):
+        with open(path, "r") as file:
+            data = json.loads(file.read())
+            for widget in data["widgets"]:
+                if widget["type"] == "ImageWidget" :
+                    props = widget["props"]
+                    self.createImageWidget(
+                        props["x"],
+                        props["y"],
+                        props["w"],
+                        props["h"],
+                        props["source"],
+                        props["fit"]
+                    )
+    
+    def createImageWidget(self, x, y, w, h, path, fit) -> None:
+        self.widgetList.append(ImageWidget.ImageWidget(self, x, y, w, h, path, fit))
+
+    def mouseClicked(self, mouseX, mouseY) -> None:
+        # look through all widgets and see if the mouse hit it, if so call it's
+        # onClicked function
+        pass
+    
+    def registerEvent(self, event) -> None:
+        self.eventList.append(event)
\ No newline at end of file
diff --git a/GUI/ImageWidget.py b/GUI/ImageWidget.py
new file mode 100644 (file)
index 0000000..6cb6af9
--- /dev/null
@@ -0,0 +1,18 @@
+import GUI.Widget as Widget
+import pygame
+import os
+
+class ImageWidget(Widget.Widget):
+    def __init__(self, gui, x, y, w, h, imagePath, fit) -> None:
+        super().__init__(gui)
+        self.screenX = x
+        self.screenY = y
+        self.screenW = w
+        self.screenH = h
+
+        image = pygame.image.load(os.path.join('Assets', imagePath))
+        if fit:
+            self.surface = pygame.transform.scale(image, (self.screenW, self.screenH))
+        else :
+            self.surface = image
+
diff --git a/GUI/Widget.py b/GUI/Widget.py
new file mode 100644 (file)
index 0000000..96f227c
--- /dev/null
@@ -0,0 +1,20 @@
+# This class needs a constructor, a list of pygame surfaces to be drawn, an emit function
+# A widget also needs a screenSpace XY coord
+# onClicked function
+
+import GUI.GuiManager as GuiManager
+
+class Widget:
+    def __init__(self, gui) -> None:
+        self.guiManager = gui
+        self.surface = None
+        self.screenX = 0
+        self.screenY = 0
+        self.width = 0
+        self.height = 0
+    
+    def onClicked(self) -> None:
+        pass
+
+    def emit(self, emitString) -> None:
+        self.guiManager.registerEvent(GuiManager.Event(emitString))
\ No newline at end of file
diff --git a/Main.py b/Main.py
index 8e7cf2e..f672bfe 100644 (file)
--- a/Main.py
+++ b/Main.py
@@ -1,3 +1,4 @@
+import os
 import pygame
 import pygame.font
 import Player
@@ -8,12 +9,16 @@ import Renderer
 import Algorithm
 import Debug
 import random
-
+import GUI.GuiManager as GM
 
 def main():
     random.seed(None)
     renderer = Renderer.Renderer(1920, 1080)
     
+    guiManager = GM.GuiManager()
+
+    guiManager.loadScene(os.path.join('Assets', 'GUIScenes', 'TestScene.json'))
+
     World.world = WorldGen.generateWorldGrowingRectangles((40, 60), 40)
 
     player = Player.Player(World.worldWidth / 2, World.worldHeight / 2)
@@ -39,6 +44,9 @@ def main():
         cam = (player.worldX, player.worldY)
         
         renderer.renderWorld(cam, InputHandler.globalInputState.zoomLevel, player)
+
+        renderer.renderGuiObjects(guiManager.widgetList)
+
         if InputHandler.globalInputState.debugLevel > 0:
             renderer.renderDebugUI(player, deltaTime)
             if InputHandler.globalInputState.debugLevel > 1:
index 1662461..86d9ab1 100644 (file)
@@ -2,6 +2,7 @@ import pygame
 import Texture
 import World
 import Debug
+#import GUI.Widget as Widget
 
 class Renderer:
     def __init__(self, screenWidth, screenHeight):
@@ -11,6 +12,11 @@ class Renderer:
         pygame.display.set_caption("pyRogue")
         self.screen = pygame.display.set_mode([self.screenWidth, self.screenHeight])
     
+    def renderGuiObjects(self, widgetList):
+        for widget in widgetList:
+            self.screen.blit(widget.surface, (widget.screenX, widget.screenY))
+        pass
+
     def renderWorld(self, cam, zoomLevel, player):
         actualWidth = int(Texture.spriteSize * zoomLevel)
         actualHeight = int(Texture.spriteSize * zoomLevel)
diff --git a/Writeup/worldgen_vision.png b/Writeup/worldgen_vision.png
new file mode 100644 (file)
index 0000000..07b68fc
Binary files /dev/null and b/Writeup/worldgen_vision.png differ