Browse Source

add cache in WeaponImageApplier

Bastien Sevajol 6 years ago
parent
commit
b1f9567bea
1 changed files with 6 additions and 2 deletions
  1. 6 2
      opencombat/gui/weapon.py

+ 6 - 2
opencombat/gui/weapon.py View File

@@ -26,6 +26,7 @@ class WeaponImageApplier(ImageApplier):
26 26
         self.actor = actor
27 27
         self._images_scheme = self.get_images_scheme()
28 28
         self.path_manager = PathManager(config.resolve('global.include_path.graphics'))
29
+        self._cache = {}  # type: typing.Dict[str, Image.Image]
29 30
 
30 31
     def get_images_scheme(self) -> typing.Dict[str, typing.Dict[str, str]]:
31 32
         from opencombat.gui.actor import MAN_STAND_UP
@@ -51,11 +52,14 @@ class WeaponImageApplier(ImageApplier):
51 52
 
52 53
     def get_default_image_for_weapon(self, mode: str, weapon_type: str) -> Image.Image:
53 54
         try:
54
-            # FIXME Cache
55 55
             image_file_path = self.path_manager.path(
56 56
                 self._images_scheme[mode][weapon_type][0],
57 57
             )
58
-            return Image.open(image_file_path)
58
+            try:
59
+                return self._cache[image_file_path]
60
+            except KeyError:
61
+                self._cache[image_file_path] = Image.open(image_file_path)
62
+                return self._cache[image_file_path]
59 63
         except KeyError:
60 64
             raise UnknownWeapon(
61 65
                 'Unknown weapon "{}" for mode "{}"'.format(weapon_type, mode),