-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.py
60 lines (49 loc) · 2.09 KB
/
App.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
import pygame
class AppWindow:
def __init__(self, title="myapp", window_size=(450, 250), font='Arial'):
window_size = (window_size[0] + 20, window_size[1]) # for title bar
self.window = pygame.Surface(window_size)
self.title = title
self.window_size = window_size
self.running = False
self.dragging = False
self.drag_offset = (0, 0)
self.pos = (0, 0)
self.font = pygame.font.SysFont(font, 15)
self.titletext = self.font.render(title, True, (200, 200, 200))
def set_window_size(self, window_size=(450, 250)):
window_size = (window_size[0] + 20, window_size[1]) # for title bar
self.window = pygame.Surface(window_size)
self.window_size = window_size
def set_title(self, title):
self.titletext = self.font.render(title, True, (200, 200, 200))
self.title = title
def handle_event(self, event):
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # Left mouse button
mouse_pos = pygame.mouse.get_pos()
if 0 <= mouse_pos[0] < self.window_size[0] and 0 <= mouse_pos[1] < 20: # Check if clicked on title bar
self.dragging = True
self.drag_offset = (mouse_pos[0], mouse_pos[1])
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1: # Left mouse button
self.dragging = False
if event.type == pygame.MOUSEMOTION:
if self.dragging:
mouse_pos = pygame.mouse.get_pos()
new_pos = (mouse_pos[0] - self.drag_offset[0], mouse_pos[1] - self.drag_offset[1])
print(new_pos)
def titlebar(self):
rect = self.titletext.get_rect()
rect.left = 2
pygame.draw.rect(self.window, (0, 0, 0), (0, 0, self.window_size[0], 20))
self.window.blit(self.titletext, rect)
def init(self):
"""call on start app"""
pass
def render(self):
"""call on app running"""
pass
def exit(self):
"""call on app exiting"""
pass