annotate pyntnclick/utils.py @ 825:c5171ad0c3cd pyntnclick

Make engine computer start using text for alerts
author Neil Muller <neil@dip.sun.ac.za>
date Tue, 29 Jan 2013 12:45:54 +0200
parents 9f542ef6e498
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
589
ebc48b397fd5 Turn rect_drawer into a command line option
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
1 # Misc utils I don't know where else to put
ebc48b397fd5 Turn rect_drawer into a command line option
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
2
824
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
3 import pygame
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
4 from pygame.locals import SRCALPHA
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
5 from pygame.surface import Surface
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
6
589
ebc48b397fd5 Turn rect_drawer into a command line option
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
7
758
f4853f817a7a Refactor scene listing, to avoid having to create a window
Stefano Rivera <stefano@rivera.za.net>
parents: 691
diff changeset
8 def list_scenes(scene_module, scene_list):
589
ebc48b397fd5 Turn rect_drawer into a command line option
Neil Muller <neil@dip.sun.ac.za>
parents:
diff changeset
9 """List the scenes in the state"""
758
f4853f817a7a Refactor scene listing, to avoid having to create a window
Stefano Rivera <stefano@rivera.za.net>
parents: 691
diff changeset
10 print "Available scenes and details:"
f4853f817a7a Refactor scene listing, to avoid having to create a window
Stefano Rivera <stefano@rivera.za.net>
parents: 691
diff changeset
11 for scene in scene_list:
f4853f817a7a Refactor scene listing, to avoid having to create a window
Stefano Rivera <stefano@rivera.za.net>
parents: 691
diff changeset
12 scenemod = __import__('%s.%s' % (scene_module, scene),
f4853f817a7a Refactor scene listing, to avoid having to create a window
Stefano Rivera <stefano@rivera.za.net>
parents: 691
diff changeset
13 fromlist=[scene])
f4853f817a7a Refactor scene listing, to avoid having to create a window
Stefano Rivera <stefano@rivera.za.net>
parents: 691
diff changeset
14 if scenemod.SCENES:
f4853f817a7a Refactor scene listing, to avoid having to create a window
Stefano Rivera <stefano@rivera.za.net>
parents: 691
diff changeset
15 print " * %s" % scene
f4853f817a7a Refactor scene listing, to avoid having to create a window
Stefano Rivera <stefano@rivera.za.net>
parents: 691
diff changeset
16 else:
f4853f817a7a Refactor scene listing, to avoid having to create a window
Stefano Rivera <stefano@rivera.za.net>
parents: 691
diff changeset
17 print " * %s (details only)" % scene
f4853f817a7a Refactor scene listing, to avoid having to create a window
Stefano Rivera <stefano@rivera.za.net>
parents: 691
diff changeset
18 for detailcls in getattr(scenemod, 'DETAIL_VIEWS', []):
f4853f817a7a Refactor scene listing, to avoid having to create a window
Stefano Rivera <stefano@rivera.za.net>
parents: 691
diff changeset
19 print " - %s" % detailcls.NAME
690
4a933444c99b The return of the rects to the rect drawer
Neil Muller <neil@dip.sun.ac.za>
parents: 589
diff changeset
20
691
60bf20849231 Fix detail loading in rect_drawer. Improve error reporting when loading fails
Neil Muller <neil@dip.sun.ac.za>
parents: 690
diff changeset
21
690
4a933444c99b The return of the rects to the rect drawer
Neil Muller <neil@dip.sun.ac.za>
parents: 589
diff changeset
22 def draw_rect_image(surface, color, rect, thickness):
4a933444c99b The return of the rects to the rect drawer
Neil Muller <neil@dip.sun.ac.za>
parents: 589
diff changeset
23 """Draw a rectangle with lines thickness wide"""
4a933444c99b The return of the rects to the rect drawer
Neil Muller <neil@dip.sun.ac.za>
parents: 589
diff changeset
24 # top
4a933444c99b The return of the rects to the rect drawer
Neil Muller <neil@dip.sun.ac.za>
parents: 589
diff changeset
25 surface.fill(color, (rect.left, rect.top, rect.width, thickness))
4a933444c99b The return of the rects to the rect drawer
Neil Muller <neil@dip.sun.ac.za>
parents: 589
diff changeset
26 # bottom
4a933444c99b The return of the rects to the rect drawer
Neil Muller <neil@dip.sun.ac.za>
parents: 589
diff changeset
27 surface.fill(color, (rect.left, rect.bottom - thickness, rect.width,
4a933444c99b The return of the rects to the rect drawer
Neil Muller <neil@dip.sun.ac.za>
parents: 589
diff changeset
28 thickness))
4a933444c99b The return of the rects to the rect drawer
Neil Muller <neil@dip.sun.ac.za>
parents: 589
diff changeset
29 # left
4a933444c99b The return of the rects to the rect drawer
Neil Muller <neil@dip.sun.ac.za>
parents: 589
diff changeset
30 surface.fill(color, (rect.left, rect.top, thickness, rect.height))
4a933444c99b The return of the rects to the rect drawer
Neil Muller <neil@dip.sun.ac.za>
parents: 589
diff changeset
31 # right
4a933444c99b The return of the rects to the rect drawer
Neil Muller <neil@dip.sun.ac.za>
parents: 589
diff changeset
32 surface.fill(color, (rect.right - thickness, rect.top, thickness,
4a933444c99b The return of the rects to the rect drawer
Neil Muller <neil@dip.sun.ac.za>
parents: 589
diff changeset
33 rect.height))
824
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
34
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
35
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
36 def convert_color(color):
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
37 """Give me a pygame Color, dammit"""
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
38 if isinstance(color, pygame.Color):
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
39 return color
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
40 if isinstance(color, basestring):
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
41 return pygame.Color(color)
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
42 return pygame.Color(*color)
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
43
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
44
825
c5171ad0c3cd Make engine computer start using text for alerts
Neil Muller <neil@dip.sun.ac.za>
parents: 824
diff changeset
45 def render_text(text, fontname, font_size, color, bg_color, resource, size,
c5171ad0c3cd Make engine computer start using text for alerts
Neil Muller <neil@dip.sun.ac.za>
parents: 824
diff changeset
46 centre=True):
824
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
47 """Render the text so it will fit in the given size, reducing font
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
48 size as needed.
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
49
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
50 Note that this does not do any text wrapping."""
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
51 done = False
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
52 width, height = size
825
c5171ad0c3cd Make engine computer start using text for alerts
Neil Muller <neil@dip.sun.ac.za>
parents: 824
diff changeset
53 color = convert_color(color)
c5171ad0c3cd Make engine computer start using text for alerts
Neil Muller <neil@dip.sun.ac.za>
parents: 824
diff changeset
54 bg_color = convert_color(bg_color)
824
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
55 surface = Surface(size, SRCALPHA).convert_alpha()
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
56 surface.fill(bg_color)
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
57 while not done and font_size > 0:
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
58 # We bail at font_size 1 and just clip in that case, since we're
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
59 # out of good options
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
60 font = resource.get_font(fontname, font_size)
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
61 text_surf = font.render(text, True, color)
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
62 if (text_surf.get_width() > width or text_surf.get_height() > height):
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
63 font_size -= 1
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
64 else:
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
65 done = True
825
c5171ad0c3cd Make engine computer start using text for alerts
Neil Muller <neil@dip.sun.ac.za>
parents: 824
diff changeset
66 if centre:
c5171ad0c3cd Make engine computer start using text for alerts
Neil Muller <neil@dip.sun.ac.za>
parents: 824
diff changeset
67 # Centre the text in the rect
c5171ad0c3cd Make engine computer start using text for alerts
Neil Muller <neil@dip.sun.ac.za>
parents: 824
diff changeset
68 x = max(0, (width - text_surf.get_width()) / 2)
c5171ad0c3cd Make engine computer start using text for alerts
Neil Muller <neil@dip.sun.ac.za>
parents: 824
diff changeset
69 y = max(0, (height - text_surf.get_height()) / 2)
c5171ad0c3cd Make engine computer start using text for alerts
Neil Muller <neil@dip.sun.ac.za>
parents: 824
diff changeset
70 else:
c5171ad0c3cd Make engine computer start using text for alerts
Neil Muller <neil@dip.sun.ac.za>
parents: 824
diff changeset
71 x = y = 0
824
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
72 surface.blit(text_surf, (x, y))
9f542ef6e498 Reorganise code
Neil Muller <neil@dip.sun.ac.za>
parents: 758
diff changeset
73 return surface