|
@@ -1,5 +1,7 @@
|
1
|
1
|
|
|
2
|
+import typing
|
2
|
3
|
import weakref
|
|
4
|
+from math import floor
|
3
|
5
|
|
4
|
6
|
import pyglet
|
5
|
7
|
from pyglet.window import mouse
|
|
@@ -16,65 +18,98 @@ from synergine2.log import SynergineLogger
|
16
|
18
|
from synergine2.terminals import Terminal
|
17
|
19
|
from synergine2.terminals import TerminalPackage
|
18
|
20
|
from synergine2_cocos2d.actor import Actor
|
|
21
|
+from synergine2_cocos2d.exception import OuterWorldPosition
|
19
|
22
|
from synergine2_cocos2d.layer import LayerManager
|
20
|
23
|
from synergine2_cocos2d.middleware import TMXMiddleware
|
21
|
24
|
|
22
|
25
|
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
|
23
|
83
|
class GridManager(object):
|
24
|
84
|
def __init__(
|
25
|
85
|
self,
|
26
|
|
- layer: Layer,
|
27
|
|
- square_width: int,
|
28
|
|
- border: int=0,
|
29
|
|
- ):
|
30
|
|
- self.layer = layer
|
31
|
|
- self.square_width = square_width
|
32
|
|
- self.border = border
|
33
|
|
-
|
34
|
|
- @property
|
35
|
|
- def final_width(self):
|
36
|
|
- return self.square_width + self.border
|
37
|
|
-
|
38
|
|
- def scale_sprite(self, sprite: Sprite):
|
39
|
|
- sprite.scale_x = self.final_width / sprite.image.width
|
40
|
|
- sprite.scale_y = self.final_width / sprite.image.height
|
41
|
|
-
|
42
|
|
- def position_sprite(self, sprite: Sprite, grid_position):
|
43
|
|
- grid_x = grid_position[0]
|
44
|
|
- grid_y = grid_position[1]
|
45
|
|
- sprite.position = grid_x * self.final_width, grid_y * self.final_width
|
46
|
|
-
|
47
|
|
- def get_window_position(self, grid_position_x, grid_position_y):
|
48
|
|
- grid_x = grid_position_x
|
49
|
|
- grid_y = grid_position_y
|
50
|
|
- return grid_x * self.final_width, grid_y * self.final_width
|
51
|
|
-
|
52
|
|
- def get_grid_position(self, window_x, window_y, z=0) -> tuple:
|
53
|
|
- window_size = director.get_window_size()
|
54
|
|
-
|
55
|
|
- window_center_x = window_size[0] // 2
|
56
|
|
- window_center_y = window_size[1] // 2
|
|
86
|
+ cell_width: int,
|
|
87
|
+ cell_height: int,
|
|
88
|
+ world_width: int,
|
|
89
|
+ world_height: int,
|
|
90
|
+ ) -> None:
|
|
91
|
+ self.cell_width = cell_width
|
|
92
|
+ self.cell_height = cell_height
|
|
93
|
+ self.world_width = world_width
|
|
94
|
+ self.world_height = world_height
|
57
|
95
|
|
58
|
|
- window_relative_x = window_x - window_center_x
|
59
|
|
- window_relative_y = window_y - window_center_y
|
|
96
|
+ def get_grid_position(self, pixel_position: typing.Tuple[int, int]) -> typing.Tuple[int, int]:
|
|
97
|
+ pixel_x, pixel_y = pixel_position
|
60
|
98
|
|
61
|
|
- real_width = self.final_width * self.layer.scale
|
|
99
|
+ cell_x = int(floor(pixel_x / self.cell_width))
|
|
100
|
+ cell_y = int(floor(pixel_y / self.cell_height))
|
62
|
101
|
|
63
|
|
- return int(window_relative_x // real_width),\
|
64
|
|
- int(window_relative_y // real_width),\
|
65
|
|
- z
|
|
102
|
+ if cell_x > self.world_width or cell_y > self.world_height or cell_x < 0 or cell_y < 0:
|
|
103
|
+ raise OuterWorldPosition('Position "{}" is outer world ({}x{})'.format(
|
|
104
|
+ (cell_x, cell_y),
|
|
105
|
+ self.world_width,
|
|
106
|
+ self.world_height,
|
|
107
|
+ ))
|
66
|
108
|
|
|
109
|
+ return cell_x, cell_y
|
67
|
110
|
|
68
|
|
-class GridLayerMixin(object):
|
69
|
|
- def __init__(self, *args, **kwargs):
|
70
|
|
- square_width = kwargs.pop('square_width', 32)
|
71
|
|
- square_border = kwargs.pop('square_border', 2)
|
72
|
|
- self.grid_manager = GridManager(
|
73
|
|
- self,
|
74
|
|
- square_width=square_width,
|
75
|
|
- border=square_border,
|
76
|
|
- )
|
77
|
|
- super().__init__(*args, **kwargs)
|
|
111
|
+ def get_pixel_position_of_grid_position(self, grid_position: typing.Tuple[int, int]) -> typing.Tuple[int, int]:
|
|
112
|
+ return grid_position[0] * self.cell_width, grid_position[1] * self.cell_height
|
78
|
113
|
|
79
|
114
|
|
80
|
115
|
class MinMaxRect(cocos.cocosnode.CocosNode):
|
|
@@ -123,6 +158,7 @@ class EditLayer(cocos.layer.Layer):
|
123
|
158
|
config: Config,
|
124
|
159
|
logger: SynergineLogger,
|
125
|
160
|
layer_manager: LayerManager,
|
|
161
|
+ grid_manager: GridManager,
|
126
|
162
|
worldview,
|
127
|
163
|
bindings=None,
|
128
|
164
|
fastness=None,
|
|
@@ -135,11 +171,13 @@ class EditLayer(cocos.layer.Layer):
|
135
|
171
|
mod_modify_selection=None,
|
136
|
172
|
mod_restricted_mov=None,
|
137
|
173
|
):
|
|
174
|
+
|
138
|
175
|
super(EditLayer, self).__init__()
|
139
|
176
|
|
140
|
177
|
self.config = config
|
141
|
178
|
self.logger = logger
|
142
|
179
|
self.layer_manager = layer_manager
|
|
180
|
+ self.grid_manager = grid_manager
|
143
|
181
|
|
144
|
182
|
self.bindings = bindings
|
145
|
183
|
buttons = {}
|
|
@@ -217,7 +255,6 @@ class EditLayer(cocos.layer.Layer):
|
217
|
255
|
self.collision_manager.remove_tricky(actor)
|
218
|
256
|
actor.update_cshape()
|
219
|
257
|
self.collision_manager.add(actor)
|
220
|
|
- actor.need_update_cshape = False
|
221
|
258
|
|
222
|
259
|
def on_enter(self):
|
223
|
260
|
super(EditLayer, self).on_enter()
|
|
@@ -265,8 +302,10 @@ class EditLayer(cocos.layer.Layer):
|
265
|
302
|
for actor in self.selection:
|
266
|
303
|
old_pos = self.selection[actor].center
|
267
|
304
|
new_pos = old_pos + dpos
|
|
305
|
+ grid_pos = self.grid_manager.get_grid_position(new_pos)
|
|
306
|
+ grid_pixel_pos = self.grid_manager.get_pixel_position_of_grid_position(grid_pos)
|
268
|
307
|
|
269
|
|
- actor.update_position(new_pos)
|
|
308
|
+ actor.update_position(grid_pixel_pos)
|
270
|
309
|
|
271
|
310
|
scroller = self.weak_scroller()
|
272
|
311
|
|
|
@@ -541,6 +580,7 @@ class MainLayer(ScrollableLayer):
|
541
|
580
|
def __init__(
|
542
|
581
|
self,
|
543
|
582
|
layer_manager: LayerManager,
|
|
583
|
+ grid_manager: GridManager,
|
544
|
584
|
width: int,
|
545
|
585
|
height: int,
|
546
|
586
|
scroll_step: int=100,
|
|
@@ -548,7 +588,7 @@ class MainLayer(ScrollableLayer):
|
548
|
588
|
super().__init__()
|
549
|
589
|
self.layer_manager = layer_manager
|
550
|
590
|
self.scroll_step = scroll_step
|
551
|
|
- self.grid_manager = GridManager(self, 32, border=2)
|
|
591
|
+ self.grid_manager = grid_manager
|
552
|
592
|
|
553
|
593
|
self.width = width
|
554
|
594
|
self.height = height
|