Browse Source

interiore manager: use image given at init

Bastien Sevajol 6 years ago
parent
commit
1b39ebc17c
2 changed files with 18 additions and 12 deletions
  1. 10 9
      opencombat/gui/base.py
  2. 8 3
      opencombat/simulation/interior.py

+ 10 - 9
opencombat/gui/base.py View File

@@ -73,9 +73,14 @@ class BackgroundLayer(cocos.layer.Layer):
73 73
             'game.building.draw_interior_gap',
74 74
             2,
75 75
         )
76
-        self.interior_manager = InteriorManager(TileMap(
77
-            layer_manager.middleware.get_map_file_path(),
76
+        self.background_image = Image.open(os.path.join(
77
+            self.layer_manager.middleware.map_dir_path,
78
+            'background.png',
78 79
         ))
80
+        self.interior_manager = InteriorManager(
81
+            TileMap(layer_manager.middleware.get_map_file_path()),
82
+            original_image=self.background_image,
83
+        )
79 84
         self.map_tile_width = self.layer_manager.middleware.get_cell_width()
80 85
         self.map_tile_height = self.layer_manager.middleware.get_cell_height()
81 86
 
@@ -94,19 +99,15 @@ class BackgroundLayer(cocos.layer.Layer):
94 99
             interiors = self.interior_manager.get_interiors(
95 100
                 where_positions=subject_grid_positions)
96 101
 
102
+            # FIXME BS 2018-01-25: if not, put original background image
97 103
             if interiors:
98
-                image = Image.open(os.path.join(
99
-                    self.layer_manager.middleware.map_dir_path,
100
-                    'background.png',
101
-                ))
102 104
                 image_fake_file = io.BytesIO()
103
-                self.interior_manager.update_image_for_interiors(
104
-                    image,
105
+                new_background_image = self.interior_manager.update_image_for_interiors(
105 106
                     interiors,
106 107
                     self.map_tile_width,
107 108
                     self.map_tile_height,
108 109
                 )
109
-                image.save(image_fake_file, format='PNG')
110
+                new_background_image.save(image_fake_file, format='PNG')
110 111
                 self.background_sprite.image = pyglet.image.load(
111 112
                     'new_background.png',
112 113
                     file=image_fake_file,

+ 8 - 3
opencombat/simulation/interior.py View File

@@ -24,10 +24,12 @@ class InteriorManager(object):
24 24
     def __init__(
25 25
         self,
26 26
         map_: TMXMap,
27
+        original_image: PngImageFile,
27 28
         configuration: InteriorMapConfiguration=None,
28 29
     ) -> None:
29 30
         self.interiors = []
30 31
         self.map = map_
32
+        self.original_image = original_image
31 33
         self.configuration = configuration or InteriorMapConfiguration()
32 34
         self.interiors = self._compute_interiors()
33 35
 
@@ -91,13 +93,14 @@ class InteriorManager(object):
91 93
 
92 94
     def update_image_for_interiors(
93 95
         self,
94
-        image: PngImageFile,
95 96
         interiors: typing.List[typing.List[typing.Tuple[int, int]]],
96 97
         tile_width: int,
97 98
         tile_height: int,
98 99
         invert_y: bool=True,
99
-    ) -> None:
100
+    ) -> PngImageFile:
100 101
         # TODO BS 20171213: Optimization can be done: keep in cache modifications on image instead change it entirely
102
+        image = self.original_image.copy()
103
+        image_height = image.height
101 104
         pixels = image.load()
102 105
 
103 106
         for interior in interiors:
@@ -109,6 +112,8 @@ class InteriorManager(object):
109 112
 
110 113
                         real_y = y
111 114
                         if invert_y:
112
-                            real_y = image.height - 1 - y
115
+                            real_y = image_height - 1 - y
113 116
 
114 117
                         pixels[x, real_y] = (0, 0, 0, 0)
118
+
119
+        return image