-
Notifications
You must be signed in to change notification settings - Fork 43
/
colour_picker_test.py
74 lines (55 loc) · 2.96 KB
/
colour_picker_test.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
import pygame
import pygame_gui
from pygame_gui.elements import UIButton
from pygame_gui.windows import UIColourPickerDialog
class ColourPickingApp:
def __init__(self):
pygame.init()
pygame.display.set_caption('Colour Picking App')
self.window_surface = pygame.display.set_mode((800, 600))
self.ui_manager = pygame_gui.UIManager((800, 600),
'data/themes/colour_picker_app_theme.json')
self.background = pygame.Surface((800, 600))
self.background.fill(self.ui_manager.ui_theme.get_colour('dark_bg'))
self.pick_colour_button = UIButton(relative_rect=pygame.Rect(-180, -60, 150, 30),
text='Pick Colour',
manager=self.ui_manager,
anchors={'left': 'right',
'right': 'right',
'top': 'bottom',
'bottom': 'bottom'})
self.colour_picker = None
self.current_colour = pygame.Color(0, 0, 0)
self.picked_colour_surface = pygame.Surface((400, 400))
self.picked_colour_surface.fill(self.current_colour)
self.clock = pygame.time.Clock()
self.is_running = True
def run(self):
while self.is_running:
time_delta = self.clock.tick(60) / 1000.0
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.is_running = False
if (event.type == pygame_gui.UI_BUTTON_PRESSED and
event.ui_element == self.pick_colour_button):
self.colour_picker = UIColourPickerDialog(pygame.Rect(160, 50, 420, 400),
self.ui_manager,
window_title='Change Colour...',
initial_colour=self.current_colour)
self.pick_colour_button.disable()
if event.type == pygame_gui.UI_COLOUR_PICKER_COLOUR_PICKED:
self.current_colour = event.colour
self.picked_colour_surface.fill(self.current_colour)
if (event.type == pygame_gui.UI_WINDOW_CLOSE and
event.ui_element == self.colour_picker):
self.pick_colour_button.enable()
self.colour_picker = None
self.ui_manager.process_events(event)
self.ui_manager.update(time_delta)
self.window_surface.blit(self.background, (0, 0))
self.window_surface.blit(self.picked_colour_surface, (200, 100))
self.ui_manager.draw_ui(self.window_surface)
pygame.display.update()
if __name__ == "__main__":
app = ColourPickingApp()
app.run()