Bläddra i källkod

lifegame cosos: gui part of add/remove cell

Bastien Sevajol 8 år sedan
förälder
incheckning
50f6c2f787
1 ändrade filer med 46 tillägg och 2 borttagningar
  1. 46 2
      sandbox/life_game/gui.py

+ 46 - 2
sandbox/life_game/gui.py Visa fil

17
 
17
 
18
 
18
 
19
 class GridManager(object):
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
         self.square_width = square_width
27
         self.square_width = square_width
22
         self.border = border
28
         self.border = border
23
 
29
 
34
         grid_y = grid_position[1]
40
         grid_y = grid_position[1]
35
         sprite.position = grid_x * self.final_width, grid_y * self.final_width
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
 class Cells(Layer):
64
 class Cells(Layer):
39
     def __init__(self):
65
     def __init__(self):
40
         super().__init__()
66
         super().__init__()
41
         self.cells = {}
67
         self.cells = {}
42
-        self.grid_manager = GridManager(32, border=2)
68
+        self.grid_manager = GridManager(self, 32, border=2)
43
 
69
 
44
     def born(self, grid_position):
70
     def born(self, grid_position):
45
         cell = Sprite('resources/cells_l.png')
71
         cell = Sprite('resources/cells_l.png')
63
         super().__init__()
89
         super().__init__()
64
 
90
 
65
         self.scroll_step = 100
91
         self.scroll_step = 100
92
+        self.grid_manager = GridManager(self, 32, border=2)
66
 
93
 
67
         self.background = Sprite('resources/banner-1711735_640.jpg')
94
         self.background = Sprite('resources/banner-1711735_640.jpg')
68
         self.background.position = 0, 0
95
         self.background.position = 0, 0
103
             if self.scale <= 4:
130
             if self.scale <= 4:
104
                 self.scale += 0.2
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
 class LifeGameGui(Gui):
151
 class LifeGameGui(Gui):
108
     def __init__(
152
     def __init__(