ソースを参照

lifegame cosos: gui part of add/remove cell

Bastien Sevajol 7 年 前
コミット
50f6c2f787
共有1 個のファイルを変更した46 個の追加2 個の削除を含む
  1. 46 2
      sandbox/life_game/gui.py

+ 46 - 2
sandbox/life_game/gui.py ファイルの表示

@@ -17,7 +17,13 @@ cell_rotate = RotateBy(360, duration=30)
17 17
 
18 18
 
19 19
 class GridManager(object):
20
-    def __init__(self, square_width: int, border: int=0):
20
+    def __init__(
21
+        self,
22
+        layer: Layer,
23
+        square_width: int,
24
+        border: int=0,
25
+    ):
26
+        self.layer = layer
21 27
         self.square_width = square_width
22 28
         self.border = border
23 29
 
@@ -34,12 +40,32 @@ class GridManager(object):
34 40
         grid_y = grid_position[1]
35 41
         sprite.position = grid_x * self.final_width, grid_y * self.final_width
36 42
 
43
+    def get_window_position(self, grid_position_x, grid_position_y):
44
+        grid_x = grid_position_x
45
+        grid_y = grid_position_y
46
+        return grid_x * self.final_width, grid_y * self.final_width
47
+
48
+    def get_grid_position(self, window_x, window_y, z=0) -> tuple:
49
+        window_size = director.get_window_size()
50
+
51
+        window_center_x = window_size[0] // 2
52
+        window_center_y = window_size[1] // 2
53
+
54
+        window_relative_x = window_x - window_center_x
55
+        window_relative_y = window_y - window_center_y
56
+
57
+        real_width = self.final_width * self.layer.scale
58
+
59
+        return int(window_relative_x // real_width),\
60
+               int(window_relative_y // real_width),\
61
+               z
62
+
37 63
 
38 64
 class Cells(Layer):
39 65
     def __init__(self):
40 66
         super().__init__()
41 67
         self.cells = {}
42
-        self.grid_manager = GridManager(32, border=2)
68
+        self.grid_manager = GridManager(self, 32, border=2)
43 69
 
44 70
     def born(self, grid_position):
45 71
         cell = Sprite('resources/cells_l.png')
@@ -63,6 +89,7 @@ class MainLayer(ScrollableLayer):
63 89
         super().__init__()
64 90
 
65 91
         self.scroll_step = 100
92
+        self.grid_manager = GridManager(self, 32, border=2)
66 93
 
67 94
         self.background = Sprite('resources/banner-1711735_640.jpg')
68 95
         self.background.position = 0, 0
@@ -103,6 +130,23 @@ class MainLayer(ScrollableLayer):
103 130
             if self.scale <= 4:
104 131
                 self.scale += 0.2
105 132
 
133
+    def on_mouse_press(self, x, y, buttons, modifiers):
134
+        x, y = director.get_virtual_coordinates(x, y)
135
+        grid_position = self.grid_manager.get_grid_position(x, y)
136
+
137
+        # TODO: Have to inject in simulation ...
138
+        if self.cells.cells.get(grid_position):
139
+            self.cells.die(grid_position)
140
+        else:
141
+            self.cells.born(grid_position)
142
+
143
+    def on_mouse_motion(self, x, y, dx, dy):
144
+        x, y = director.get_virtual_coordinates(x, y)
145
+        grid_position = self.grid_manager.get_grid_position(x, y)
146
+        window_position = self.grid_manager.get_window_position(grid_position[0], grid_position[1])
147
+
148
+        self.cross.position = window_position
149
+
106 150
 
107 151
 class LifeGameGui(Gui):
108 152
     def __init__(