-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
154 lines (124 loc) · 4.5 KB
/
main.py
File metadata and controls
154 lines (124 loc) · 4.5 KB
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
150
151
152
153
154
# Module Imports
import pygame
import os
import sys
# File Imports
import configs as cfg
from surfaces import text_editor
from surfaces.base_surfaces import UIElement
from tools.collision import collision
# Pygame Initialization
pygame.init()
# Screen Variables
screen_info = pygame.display.Info()
WIDTH = screen_info.current_w
HEIGHT = screen_info.current_h
screen = pygame.display.set_mode((WIDTH - cfg.WINDOW_OFFSET_X, HEIGHT - cfg.WINDOW_OFFSET_Y), pygame.RESIZABLE)
os.environ['SDL_VIDEO_WINDOW_POS'] = f"{cfg.WINDOW_OFFSET_X}, {cfg.WINDOW_OFFSET_Y}"
pygame.display.set_caption("Pygame UI")
# Clock for controlling the frame rate
clock = pygame.time.Clock()
# Scrap Initialization
pygame.scrap.init()
# Main function to run the game loop
#----------------------------------------------------------------------------------------------
def main():
# Running variables
running = True
# Starter variables
mouse_pos = (0,0)
# Initialize surfaces
# Text Editor Surface
text_editor_rect = (0, 0, (WIDTH - cfg.WINDOW_OFFSET_X) // 2, HEIGHT - cfg.WINDOW_OFFSET_Y)
text_editor_surface = text_editor.Surface(text_editor_rect)
text_editor_surface.set_active()
text_editor_surface.set_border()
te2_rect = ((WIDTH - cfg.WINDOW_OFFSET_X) // 2, 0, (WIDTH - cfg.WINDOW_OFFSET_X) // 2, HEIGHT - cfg.WINDOW_OFFSET_Y)
te2_surface = text_editor.Surface(te2_rect)
te2_surface.set_border()
# Game loop
while running:
# Event handler
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEMOTION:
mouse_pos = pygame.mouse.get_pos()
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if collision(mouse_pos, text_editor_rect):
text_editor_surface.set_active()
te2_surface.set_inactive()
elif collision(mouse_pos, te2_rect):
te2_surface.set_active()
text_editor_surface.set_inactive()
else:
te2_surface.set_inactive()
text_editor_surface.set_inactive()
elif event.button == 3:
print("Right click pressed")
elif event.button == 4:
print("Scroll up")
elif event.button == 5:
print("Scroll down")
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
# Pass events to surfaces
text_editor_surface.handle_event(events)
te2_surface.handle_event(events)
# Drawing to screen
screen.fill(cfg.WHITE)
text_editor_surface.draw(screen)
te2_surface.draw(screen)
# Update the display
pygame.display.flip()
clock.tick(cfg.FPS)
# Clean up
pygame.quit()
sys.exit()
def newmain():
# Running variables
running = True
# Starter variables
mouse_pos = (0,0)
# Initialize surfaces
rect1 = (500, 100, 500, 500)
surface1 = UIElement(rect1)
rect2 = (200, 200, 100, 100)
surface2 = UIElement(rect2, cfg.RED, surface1)
# Game loop
while running:
# Event handler
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEMOTION:
mouse_pos = pygame.mouse.get_pos()
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
print("Left click pressed")
elif event.button == 3:
print("Right click pressed")
elif event.button == 4:
print("Scroll up")
elif event.button == 5:
print("Scroll down")
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
# Pass events to surfaces
# Drawing to screen
screen.fill(cfg.WHITE)
surface1.draw(screen)
surface2.draw()
# Update the display
pygame.display.flip()
clock.tick(cfg.FPS)
# Clean up
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()