Mercurial > boomslang
comparison pyntnclick/utils.py @ 824:9f542ef6e498 pyntnclick
Reorganise code
author | Neil Muller <neil@dip.sun.ac.za> |
---|---|
date | Tue, 29 Jan 2013 11:41:03 +0200 |
parents | pyntnclick/tools/utils.py@f4853f817a7a |
children | c5171ad0c3cd |
comparison
equal
deleted
inserted
replaced
823:1bf088e7865b | 824:9f542ef6e498 |
---|---|
1 # Misc utils I don't know where else to put | |
2 | |
3 import pygame | |
4 from pygame.locals import SRCALPHA | |
5 from pygame.surface import Surface | |
6 | |
7 | |
8 def list_scenes(scene_module, scene_list): | |
9 """List the scenes in the state""" | |
10 print "Available scenes and details:" | |
11 for scene in scene_list: | |
12 scenemod = __import__('%s.%s' % (scene_module, scene), | |
13 fromlist=[scene]) | |
14 if scenemod.SCENES: | |
15 print " * %s" % scene | |
16 else: | |
17 print " * %s (details only)" % scene | |
18 for detailcls in getattr(scenemod, 'DETAIL_VIEWS', []): | |
19 print " - %s" % detailcls.NAME | |
20 | |
21 | |
22 def draw_rect_image(surface, color, rect, thickness): | |
23 """Draw a rectangle with lines thickness wide""" | |
24 # top | |
25 surface.fill(color, (rect.left, rect.top, rect.width, thickness)) | |
26 # bottom | |
27 surface.fill(color, (rect.left, rect.bottom - thickness, rect.width, | |
28 thickness)) | |
29 # left | |
30 surface.fill(color, (rect.left, rect.top, thickness, rect.height)) | |
31 # right | |
32 surface.fill(color, (rect.right - thickness, rect.top, thickness, | |
33 rect.height)) | |
34 | |
35 | |
36 def convert_color(color): | |
37 """Give me a pygame Color, dammit""" | |
38 if isinstance(color, pygame.Color): | |
39 return color | |
40 if isinstance(color, basestring): | |
41 return pygame.Color(color) | |
42 return pygame.Color(*color) | |
43 | |
44 | |
45 def render_text(text, fontname, font_size, color, bg_color, resource, size): | |
46 """Render the text so it will fit in the given size, reducing font | |
47 size as needed. | |
48 | |
49 Note that this does not do any text wrapping.""" | |
50 done = False | |
51 width, height = size | |
52 surface = Surface(size, SRCALPHA).convert_alpha() | |
53 surface.fill(bg_color) | |
54 while not done and font_size > 0: | |
55 # We bail at font_size 1 and just clip in that case, since we're | |
56 # out of good options | |
57 font = resource.get_font(fontname, font_size) | |
58 text_surf = font.render(text, True, color) | |
59 if (text_surf.get_width() > width or text_surf.get_height() > height): | |
60 font_size -= 1 | |
61 else: | |
62 done = True | |
63 # Centre the text in the rect | |
64 x = max(0, (width - text_surf.get_width()) / 2) | |
65 y = max(0, (height - text_surf.get_height()) / 2) | |
66 surface.blit(text_surf, (x, y)) | |
67 return surface |