comparison pyntnclick/resources.py @ 587:f20d211d2c91 pyntnclick

load_image() -> get_image() and more docs.
author Jeremy Thurgood <firxen@gmail.com>
date Sat, 11 Feb 2012 17:02:26 +0200
parents 96ff2d8a8a9a
children 4117d7b201a4
comparison
equal deleted inserted replaced
586:cf65e91b30b1 587:f20d211d2c91
46 if os.path.exists(path): 46 if os.path.exists(path):
47 return path 47 return path
48 raise ResourceNotFound(resource_name) 48 raise ResourceNotFound(resource_name)
49 49
50 def get_paths(self, resource_path): 50 def get_paths(self, resource_path):
51 """Get list of resource paths to search.
52 """
51 paths = [] 53 paths = []
52 for module in [self.resource_module, self.DEFAULT_RESOURCE_MODULE]: 54 for module in [self.resource_module, self.DEFAULT_RESOURCE_MODULE]:
53 if self.language: 55 if self.language:
54 fn = os.path.join(self.language, resource_path) 56 fn = os.path.join(self.language, resource_path)
55 paths.append(resource_filename(module, fn)) 57 paths.append(resource_filename(module, fn))
56 paths.append(resource_filename(module, resource_path)) 58 paths.append(resource_filename(module, resource_path))
57 return paths 59 return paths
58 60
59 def load_image(self, image_name_fragments, mutators=(), basedir='images'): 61 def get_image(self, image_name_fragments, mutators=(), basedir='images'):
62 """Load an image and optionally apply mutators.
63
64 The `image_name_fragments` parameter may be either a string or a list
65 of strings. The latter is a convenience for things that compose an
66 image name out of several fragments.
67
68 The `mutators` param may contain mutators, which modify an image
69 in-place to apply various effects. TODO: Implement mutators somewhere,
70 so this becomes useful.
71
72 The `basedir` param defaults to 'images', but may be overriden to load
73 images from other places. ('icons', for example.)
74 """
75
76 # Juggle our various params and find an appropriate image path.
60 if isinstance(image_name_fragments, basestring): 77 if isinstance(image_name_fragments, basestring):
61 image_name_fragments = [image_name_fragments] 78 image_name_fragments = [image_name_fragments]
62 image_path = self.get_resource_path(basedir, *image_name_fragments) 79 image_path = self.get_resource_path(basedir, *image_name_fragments)
80
81 key = (image_path, mutators)
82 if key in self._mutated_image_cache:
83 # We already have this cached, so shortcut the whole process.
84 return self._mutated_image_cache[key]
63 85
64 if image_path not in self._image_cache: 86 if image_path not in self._image_cache:
65 image = pygame.image.load(image_path) 87 image = pygame.image.load(image_path)
66 if self.CONVERT_ALPHA: 88 if self.CONVERT_ALPHA:
67 image = image.convert_alpha(pygame.display.get_surface()) 89 image = image.convert_alpha(pygame.display.get_surface())
68 self._image_cache[image_path] = image 90 self._image_cache[image_path] = image
91 image = self._image_cache[image_path]
69 92
70 image = self._image_cache[image_path] 93 # Apply any mutators we're given.
71 key = (image_path, mutators) 94 for mutator in mutators:
95 image = mutator(image)
96 self._mutated_image_cache[key] = image
72 97
73 if key not in self._mutated_image_cache: 98 return image
74 for mutator in mutators:
75 image = mutator(image)
76 self._mutated_image_cache[key] = image
77
78 return self._mutated_image_cache[key]