forked from MyreMylar/pygame_gui_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshape_packing_cache_test.py
74 lines (55 loc) · 2.28 KB
/
shape_packing_cache_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 random
import math
import pygame
import pygame_gui
GOLDEN_RATIO = (math.sqrt(5) - 1) / 2
def add_random_rectangle_to_cache(added_surfaces_list):
width = random.randint(20, 200)
height = random.randint(20, 200)
surface = pygame.Surface((width, height))
color = pygame.Color("#000000")
color.hsla = 360 * ((width * GOLDEN_RATIO) % 1), 50, 70, 100
color.a = 128
surface.fill(color)
added_surfaces_list.append(str(color.hsla))
manager.ui_theme.shape_cache.add_surface_to_cache(surface, str(color.hsla))
def remove_use_from_cache_surface(surf_id):
added_surfaces.remove(surf_id)
manager.ui_theme.shape_cache.remove_user_from_cache_item(surf_id)
pygame.init()
pygame.display.set_caption('Quick Start')
window_surface = pygame.display.set_mode((1024, 1024))
manager = pygame_gui.UIManager((1024, 1024), 'data/themes/quick_theme.json')
background = pygame.Surface((1024, 1024))
background.fill(manager.get_theme().get_colour('dark_bg'))
clock = pygame.time.Clock()
is_running = True
current_surf = 0
added_surfaces = []
while is_running:
time_delta = clock.tick(60)/1000.0
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_running = False
manager.process_events(event)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
add_random_rectangle_to_cache(added_surfaces)
if event.key == pygame.K_BACKSPACE:
if len(added_surfaces) > 0:
remove_use_from_cache_surface(random.choice(added_surfaces))
if event.key == pygame.K_RIGHT:
if current_surf < len(manager.ui_theme.shape_cache.cache_surfaces) - 1:
current_surf += 1
if event.key == pygame.K_LEFT:
if current_surf > 0:
current_surf -= 1
manager.update(time_delta)
window_surface.blit(background, (0, 0))
manager.draw_ui(window_surface)
cache_surface = manager.ui_theme.shape_cache.cache_surfaces[current_surf]
window_surface.blit(cache_surface['surface'],
(0, 0))
for rectangle in cache_surface['free_space_rectangles']:
pygame.draw.rect(window_surface, pygame.Color('#A00000'), rectangle, 2)
pygame.display.update()