-
Notifications
You must be signed in to change notification settings - Fork 1
/
UI.py
149 lines (144 loc) · 5.59 KB
/
UI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import pygame as pg
import math
import Connector as C
import SceneLoader as SL
#The UIHandler Handles most of the UI in a scene
class UIHandler():
#The Subscribers of the UIHandler
Subscribers = []
#Updates all of the UI
def Update(self, MouseX, MouseY, UpdateText):
for x in self.Subscribers:
x.Update(MouseX, MouseY, UpdateText)
pass
#Addds a UIElement to the Subscribers
def AddSubscriber(self, Object):
self.Subscribers.append(Object)
pass
#Adds multiple UIElements to the Subscribers
def AddSubscribers(self, Objects):
for x in Objects:
self.Subscribers.append(x)
#Delets subscribers from the List and memory
def DeleteSubscribers(self):
for x in self.Subscribers:
del x
self.Subscribers.clear()
pass
#Draws the Subscribers on the Screen
def LoadObjects(self):
for x in self.Subscribers:
x.Draw()
pass
pass
#The base class for a UI Class
class UIElement():
def __init__(self, Xpos, Ypos) -> None:
pass
#The update function, Only Triggered by UIHandler
def Update(self, MouseX, MouseY, UpdateText):
return
#The Draw function, only Triggered by UIHandler
def Draw(self):
pass
pass
#Button class. A UIElement that reacts when clicked
class Button(UIElement):
#XPos : the X Position of the Button
#YPos : the Y Position of the Button
#SizeX : The X Size of the Button
#SizeY : The Y Size of the Button
#Color : The Color of the Button
#Optional-
#Text : What the Button Says
#Font : The Font of the Text
#FontSize : The size of the Font
def __init__(self, Xpos, Ypos, SizeX, SizeY, Color, Text = "", Font = "", FontSize = 0) -> None:
self.ButtonRect = pg.Rect(Xpos, Ypos, SizeX, SizeY)
self.Color = Color
pg.draw.rect(C.Main.Inst.mainSurface, self.Color, self.ButtonRect)
self.Text = Text
self.Font = Font
self.FontSize = FontSize
if Text != "":
Source = pg.font.SysFont(self.Font, self.FontSize, False, False).render(self.Text,False,(0,0,0), None)
C.Main.Inst.mainSurface.blit(Source, ((self.ButtonRect.topleft[0]+self.ButtonRect.centerx + + self.FontSize)/2, (self.ButtonRect.topleft[1]+ self.ButtonRect.centery)/2), area=None, special_flags = 0)
pass
pass
#The Update Function
def Update(self, MouseX, MouseY, UpdateText):
#If a Mouseclick was Pressed AND the mouse was over the Button, Interact.
if UpdateText == "MOUSEBUTTONUP":
if self.Pressed(MouseX, MouseY):
self.Interaction()
pass
#The Interaction Function.
def Interaction(self):
pass
#Checks if the Mouse is over the Button
def Pressed(self, MouseX, MouseY) -> bool:
return self.ButtonRect.collidepoint(MouseX, MouseY)
#Draws the Button
def Draw(self):
pg.draw.rect(C.Main.Inst.mainSurface, self.Color, self.ButtonRect)
if self.Text != "":
Source = pg.font.SysFont(self.Font, self.FontSize, False, False).render(self.Text,False,(0,0,0), None)
C.Main.Inst.mainSurface.blit(Source, ((self.ButtonRect.topleft[0]+self.ButtonRect.centerx + self.FontSize)/2, (self.ButtonRect.topleft[1]+ self.ButtonRect.centery)/2), area=None, special_flags = 0)
pass
#The Main Menu Button
class MainMenuButton(Button):
def __init__(self, Xpos, Ypos, SizeX, SizeY, Color, Text = "", Font = "", FontSize = 0) -> None:
super().__init__(Xpos, Ypos, SizeX, SizeY, Color, Text, Font, FontSize)
#Upon Interaction, Load the Main Scene
def Interaction(self):
print("I am Main Button, and I am being Pressed")
C.Main.Inst.SceneLoader.LoadScene(SL.MainScene())
return super().Interaction()
#Renders Text
class Text(UIElement):
RenderText = ""
#XPos : the X Position of the Text
#YPos : the Y Position of the Text
#Size : the Font Size
#RenderText : The Text wished to be displayed
#Font : the Sys Font of your Choosing
#Bold : Whether or not the Text is Bolded
#Italicized : Whether or not the Text is Italicized
#Color : The Color of the Text
def __init__(self, Xpos, Ypos,RenderText : str, Font : str, Size : int, Bold : bool, Italicized : bool, Color = (0,0,0)):
self.Xpos = Xpos
self.Ypos = Ypos
self.Size = Size
self.RenderText = RenderText
self.Font = Font
self.Bold = Bold
self.Italicized = Italicized
self.Color = Color
self.Draw()
pass
#Changes the Text
def ChangeText(self, Text):
self.RenderText = Text
self.Draw()
pass
#Draws the Text
def Draw(self):
self.RenderFont = pg.font.SysFont(self.Font, self.Size, self.Bold, self.Italicized)
self.RenderSurface = self.RenderFont.render(self.RenderText, False, self.Color, None)
C.Main.Inst.mainSurface.blit(self.RenderSurface, (self.Xpos, self.Ypos))
return super().Draw()
#Renders an Image
class Image(UIElement):
#XPos : the X Position of the Image
#YPos : the Y Position of the Image
#Texture : the path to the Texture of the Image
def __init__(self, Xpos, Ypos, Texture :str) -> None:
self.Xpos = Xpos
self.Ypos = Ypos
self.Texture = pg.image.load(Texture).convert()
C.Main.Inst.mainSurface.blit(self.Texture, (self.Xpos, self.Ypos))
super().__init__(Xpos, Ypos)
#Draws the Image
def Draw(self):
C.Main.Inst.mainSurface.blit(self.Texture, (self.Xpos, self.Ypos))
super().Draw()