Mercurial > boomslang
annotate pyntnclick/resources.py @ 658:2703924c8c70 pyntnclick
Custom mouse cursors return
author | Stefano Rivera <stefano@rivera.za.net> |
---|---|
date | Sun, 12 Feb 2012 14:25:27 +0200 |
parents | d1ec9e739e23 |
children | 43b49f1de828 |
rev | line source |
---|---|
561 | 1 # -*- test-case-name: pyntnclick.tests.test_resources -*- |
2 | |
3 import os | |
4 from pkg_resources import resource_filename | |
5 | |
571
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
6 import pygame |
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
7 |
561 | 8 |
9 class ResourceNotFound(Exception): | |
10 pass | |
11 | |
12 | |
13 class Resources(object): | |
584
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
14 """Resource loader and manager. |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
15 |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
16 The `CONVERT_ALPHA` flag allows alpha conversions to be disabled so that |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
17 images may be loaded without having a display initialised. This is useful |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
18 in unit tests, for example. |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
19 """ |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
20 |
561 | 21 DEFAULT_RESOURCE_MODULE = "pyntnclick.data" |
571
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
22 CONVERT_ALPHA = True |
561 | 23 |
24 def __init__(self, resource_module, language=None): | |
25 self.resource_module = resource_module | |
26 self.language = language | |
571
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
27 self._image_cache = {} |
638
d1ec9e739e23
Make TextWidget work (probably)
Stefano Rivera <stefano@rivera.za.net>
parents:
625
diff
changeset
|
28 self._font_cache = {} |
591 | 29 self._transformed_image_cache = {} |
561 | 30 |
571
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
31 def get_resource_path(self, *resource_path_fragments): |
584
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
32 """Find the resource in one of a number of different places. |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
33 |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
34 The following directories are searched, in order: |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
35 |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
36 * <resource_module>/<lang>/ |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
37 * <resource_module>/ |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
38 * <default_resource_module>/<lang>/ |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
39 * <default_resource_module>/ |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
40 |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
41 If the `language` attribute is `None`, the paths with <lang> in them |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
42 are skipped. |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
43 """ |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
44 resource_name = '/'.join(resource_path_fragments) |
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
45 resource_name = os.path.join(*resource_name.split('/')) |
561 | 46 for path in self.get_paths(resource_name): |
620
38f75cef86d0
Resources are files. As stipulated by $DEITY
Stefano Rivera <stefano@rivera.za.net>
parents:
593
diff
changeset
|
47 if os.path.isfile(path): |
561 | 48 return path |
49 raise ResourceNotFound(resource_name) | |
50 | |
51 def get_paths(self, resource_path): | |
587
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
52 """Get list of resource paths to search. |
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
53 """ |
561 | 54 paths = [] |
55 for module in [self.resource_module, self.DEFAULT_RESOURCE_MODULE]: | |
56 if self.language: | |
57 fn = os.path.join(self.language, resource_path) | |
58 paths.append(resource_filename(module, fn)) | |
59 paths.append(resource_filename(module, resource_path)) | |
60 return paths | |
571
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
61 |
625
85153f422522
Differently yuck get_image().
Jeremy Thurgood <firxen@gmail.com>
parents:
620
diff
changeset
|
62 def get_image(self, *image_name_fragments, **kw): |
587
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
63 """Load an image and optionally apply mutators. |
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
64 |
625
85153f422522
Differently yuck get_image().
Jeremy Thurgood <firxen@gmail.com>
parents:
620
diff
changeset
|
65 All positional params end up in `image_name_fragments` and are joined |
85153f422522
Differently yuck get_image().
Jeremy Thurgood <firxen@gmail.com>
parents:
620
diff
changeset
|
66 with the path separator. |
85153f422522
Differently yuck get_image().
Jeremy Thurgood <firxen@gmail.com>
parents:
620
diff
changeset
|
67 |
85153f422522
Differently yuck get_image().
Jeremy Thurgood <firxen@gmail.com>
parents:
620
diff
changeset
|
68 Two keyword parameters are also accepted: |
587
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
69 |
625
85153f422522
Differently yuck get_image().
Jeremy Thurgood <firxen@gmail.com>
parents:
620
diff
changeset
|
70 * `transforms` may contain transforms, which modify an image in-place |
85153f422522
Differently yuck get_image().
Jeremy Thurgood <firxen@gmail.com>
parents:
620
diff
changeset
|
71 to apply various effects. |
587
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
72 |
625
85153f422522
Differently yuck get_image().
Jeremy Thurgood <firxen@gmail.com>
parents:
620
diff
changeset
|
73 * `basedir` defaults to 'images', but may be overridden to load images |
85153f422522
Differently yuck get_image().
Jeremy Thurgood <firxen@gmail.com>
parents:
620
diff
changeset
|
74 from other places. ('icons', for example.) |
587
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
75 """ |
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
76 |
625
85153f422522
Differently yuck get_image().
Jeremy Thurgood <firxen@gmail.com>
parents:
620
diff
changeset
|
77 transforms = kw.get('transforms', ()) |
85153f422522
Differently yuck get_image().
Jeremy Thurgood <firxen@gmail.com>
parents:
620
diff
changeset
|
78 basedir = kw.get('basedir', 'images') |
85153f422522
Differently yuck get_image().
Jeremy Thurgood <firxen@gmail.com>
parents:
620
diff
changeset
|
79 |
584
96ff2d8a8a9a
Allow list of image name fragments in load_image. Also, some docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
580
diff
changeset
|
80 image_path = self.get_resource_path(basedir, *image_name_fragments) |
575
970cdc219e15
Add image mutation to resource loader.
Jeremy Thurgood <firxen@gmail.com>
parents:
571
diff
changeset
|
81 |
591 | 82 key = (image_path, transforms) |
83 if key in self._transformed_image_cache: | |
587
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
84 # We already have this cached, so shortcut the whole process. |
591 | 85 return self._transformed_image_cache[key] |
587
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
86 |
571
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
87 if image_path not in self._image_cache: |
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
88 image = pygame.image.load(image_path) |
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
89 if self.CONVERT_ALPHA: |
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
90 image = image.convert_alpha(pygame.display.get_surface()) |
20e296d4a3a5
Add load_image to Resources.
Jeremy Thurgood <firxen@gmail.com>
parents:
561
diff
changeset
|
91 self._image_cache[image_path] = image |
575
970cdc219e15
Add image mutation to resource loader.
Jeremy Thurgood <firxen@gmail.com>
parents:
571
diff
changeset
|
92 image = self._image_cache[image_path] |
970cdc219e15
Add image mutation to resource loader.
Jeremy Thurgood <firxen@gmail.com>
parents:
571
diff
changeset
|
93 |
593
1eb1537173ef
Add some image transforms.
Jeremy Thurgood <firxen@gmail.com>
parents:
591
diff
changeset
|
94 # Apply any transforms we're given. |
591 | 95 for transform in transforms: |
96 image = transform(image) | |
97 self._transformed_image_cache[key] = image | |
575
970cdc219e15
Add image mutation to resource loader.
Jeremy Thurgood <firxen@gmail.com>
parents:
571
diff
changeset
|
98 |
587
f20d211d2c91
load_image() -> get_image() and more docs.
Jeremy Thurgood <firxen@gmail.com>
parents:
584
diff
changeset
|
99 return image |
638
d1ec9e739e23
Make TextWidget work (probably)
Stefano Rivera <stefano@rivera.za.net>
parents:
625
diff
changeset
|
100 |
d1ec9e739e23
Make TextWidget work (probably)
Stefano Rivera <stefano@rivera.za.net>
parents:
625
diff
changeset
|
101 def get_font(self, file_name, font_size, basedir=None): |
d1ec9e739e23
Make TextWidget work (probably)
Stefano Rivera <stefano@rivera.za.net>
parents:
625
diff
changeset
|
102 """Load a a font, cached if possible.""" |
d1ec9e739e23
Make TextWidget work (probably)
Stefano Rivera <stefano@rivera.za.net>
parents:
625
diff
changeset
|
103 if basedir is None: |
d1ec9e739e23
Make TextWidget work (probably)
Stefano Rivera <stefano@rivera.za.net>
parents:
625
diff
changeset
|
104 basedir = 'fonts' |
d1ec9e739e23
Make TextWidget work (probably)
Stefano Rivera <stefano@rivera.za.net>
parents:
625
diff
changeset
|
105 key = (basedir, file_name, font_size) |
d1ec9e739e23
Make TextWidget work (probably)
Stefano Rivera <stefano@rivera.za.net>
parents:
625
diff
changeset
|
106 if key not in self._font_cache: |
d1ec9e739e23
Make TextWidget work (probably)
Stefano Rivera <stefano@rivera.za.net>
parents:
625
diff
changeset
|
107 fontfn = self.get_resource_path(basedir, file_name) |
d1ec9e739e23
Make TextWidget work (probably)
Stefano Rivera <stefano@rivera.za.net>
parents:
625
diff
changeset
|
108 self._font_cache[key] = pygame.font.Font(fontfn, font_size) |
d1ec9e739e23
Make TextWidget work (probably)
Stefano Rivera <stefano@rivera.za.net>
parents:
625
diff
changeset
|
109 return self._font_cache[key] |