|
@@ -9,79 +9,20 @@ from pyglet.window import mouse
|
9
|
9
|
import cocos
|
10
|
10
|
from cocos import collision_model
|
11
|
11
|
from cocos import euclid
|
12
|
|
-from cocos.director import director
|
13
|
|
-from cocos.layer import Layer
|
14
|
12
|
from cocos.layer import ScrollableLayer
|
15
|
|
-from cocos.sprite import Sprite
|
16
|
13
|
from synergine2.config import Config
|
17
|
14
|
from synergine2.log import SynergineLogger
|
18
|
|
-from synergine2.simulation import Subject
|
19
|
15
|
from synergine2.terminals import Terminal
|
20
|
16
|
from synergine2.terminals import TerminalPackage
|
21
|
17
|
from synergine2.xyz import XYZSubjectMixin
|
22
|
18
|
from synergine2_cocos2d.actor import Actor
|
23
|
19
|
from synergine2_cocos2d.exception import OuterWorldPosition
|
|
20
|
+from synergine2_cocos2d.gl import rectangle_positions_type
|
|
21
|
+from synergine2_cocos2d.gl import draw_rectangle
|
24
|
22
|
from synergine2_cocos2d.layer import LayerManager
|
25
|
23
|
from synergine2_cocos2d.middleware import TMXMiddleware
|
26
|
24
|
|
27
|
25
|
|
28
|
|
-# class GridManager(object):
|
29
|
|
-# def __init__(
|
30
|
|
-# self,
|
31
|
|
-# layer: Layer,
|
32
|
|
-# square_width: int,
|
33
|
|
-# border: int=0,
|
34
|
|
-# ):
|
35
|
|
-# self.layer = layer
|
36
|
|
-# self.square_width = square_width
|
37
|
|
-# self.border = border
|
38
|
|
-#
|
39
|
|
-# @property
|
40
|
|
-# def final_width(self):
|
41
|
|
-# return self.square_width + self.border
|
42
|
|
-#
|
43
|
|
-# def scale_sprite(self, sprite: Sprite):
|
44
|
|
-# sprite.scale_x = self.final_width / sprite.image.width
|
45
|
|
-# sprite.scale_y = self.final_width / sprite.image.height
|
46
|
|
-#
|
47
|
|
-# def position_sprite(self, sprite: Sprite, grid_position):
|
48
|
|
-# grid_x = grid_position[0]
|
49
|
|
-# grid_y = grid_position[1]
|
50
|
|
-# sprite.position = grid_x * self.final_width, grid_y * self.final_width
|
51
|
|
-#
|
52
|
|
-# def get_window_position(self, grid_position_x, grid_position_y):
|
53
|
|
-# grid_x = grid_position_x
|
54
|
|
-# grid_y = grid_position_y
|
55
|
|
-# return grid_x * self.final_width, grid_y * self.final_width
|
56
|
|
-#
|
57
|
|
-# def get_grid_position(self, window_x, window_y, z=0) -> tuple:
|
58
|
|
-# window_size = director.get_window_size()
|
59
|
|
-#
|
60
|
|
-# window_center_x = window_size[0] // 2
|
61
|
|
-# window_center_y = window_size[1] // 2
|
62
|
|
-#
|
63
|
|
-# window_relative_x = window_x - window_center_x
|
64
|
|
-# window_relative_y = window_y - window_center_y
|
65
|
|
-#
|
66
|
|
-# real_width = self.final_width * self.layer.scale
|
67
|
|
-#
|
68
|
|
-# return int(window_relative_x // real_width),\
|
69
|
|
-# int(window_relative_y // real_width),\
|
70
|
|
-# z
|
71
|
|
-#
|
72
|
|
-#
|
73
|
|
-# class GridLayerMixin(object):
|
74
|
|
-# def __init__(self, *args, **kwargs):
|
75
|
|
-# square_width = kwargs.pop('square_width', 32)
|
76
|
|
-# square_border = kwargs.pop('square_border', 2)
|
77
|
|
-# self.grid_manager = GridManager(
|
78
|
|
-# self,
|
79
|
|
-# square_width=square_width,
|
80
|
|
-# border=square_border,
|
81
|
|
-# )
|
82
|
|
-# super().__init__(*args, **kwargs)
|
83
|
|
-
|
84
|
|
-
|
85
|
26
|
class GridManager(object):
|
86
|
27
|
def __init__(
|
87
|
28
|
self,
|
|
@@ -111,8 +52,28 @@ class GridManager(object):
|
111
|
52
|
return cell_x, cell_y
|
112
|
53
|
|
113
|
54
|
def get_pixel_position_of_grid_position(self, grid_position: typing.Tuple[int, int]) -> typing.Tuple[int, int]:
|
114
|
|
- return grid_position[0] * self.cell_width + self.cell_width,\
|
115
|
|
- grid_position[1] * self.cell_height + self.cell_height
|
|
55
|
+ return grid_position[0] * self.cell_width + (self.cell_width // 2),\
|
|
56
|
+ grid_position[1] * self.cell_height + (self.cell_height // 2)
|
|
57
|
+
|
|
58
|
+ def get_rectangle_positions(
|
|
59
|
+ self,
|
|
60
|
+ grid_position: typing.Tuple[int, int],
|
|
61
|
+ ) -> rectangle_positions_type:
|
|
62
|
+ """
|
|
63
|
+ A<---D
|
|
64
|
+ | |
|
|
65
|
+ B--->C
|
|
66
|
+ :param grid_position:grid position to exploit
|
|
67
|
+ :return: grid pixel corners positions
|
|
68
|
+ """
|
|
69
|
+ grid_x, grid_y = grid_position
|
|
70
|
+
|
|
71
|
+ a = grid_x * self.cell_width, grid_y * self.cell_height + self.cell_height
|
|
72
|
+ b = grid_x * self.cell_width, grid_y * self.cell_height
|
|
73
|
+ c = grid_x * self.cell_width + self.cell_width, grid_y * self.cell_height
|
|
74
|
+ d = grid_x * self.cell_width + self.cell_width, grid_y * self.cell_height + self.cell_height
|
|
75
|
+
|
|
76
|
+ return a, d, c, b
|
116
|
77
|
|
117
|
78
|
|
118
|
79
|
class MinMaxRect(cocos.cocosnode.CocosNode):
|
|
@@ -120,6 +81,7 @@ class MinMaxRect(cocos.cocosnode.CocosNode):
|
120
|
81
|
super(MinMaxRect, self).__init__()
|
121
|
82
|
self.layer_manager = layer_manager
|
122
|
83
|
self.color3 = (20, 20, 20)
|
|
84
|
+ self.color3f = (0, 0, 0, 0.2)
|
123
|
85
|
self.vertexes = [(0.0, 0.0), (0.0, 0.0), (0.0, 0.0), (0.0, 0.0)]
|
124
|
86
|
self.visible = False
|
125
|
87
|
|
|
@@ -132,22 +94,12 @@ class MinMaxRect(cocos.cocosnode.CocosNode):
|
132
|
94
|
def draw(self):
|
133
|
95
|
if not self.visible:
|
134
|
96
|
return
|
135
|
|
- pyglet.gl.glLineWidth(1) # deprecated
|
136
|
|
- pyglet.gl.glColor3ub(*self.color3)
|
137
|
|
- pyglet.gl.glBegin(pyglet.gl.GL_LINE_STRIP)
|
138
|
|
- for v in self.vertexes:
|
139
|
|
- pyglet.gl.glVertex2f(*v)
|
140
|
|
- pyglet.gl.glVertex2f(*self.vertexes[0])
|
141
|
|
- pyglet.gl.glEnd()
|
142
|
|
-
|
143
|
|
- # rectangle
|
144
|
|
- pyglet.gl.glColor4f(0, 0, 0, 0.5)
|
145
|
|
- pyglet.gl.glBegin(pyglet.gl.GL_QUADS)
|
146
|
|
- pyglet.gl.glVertex3f(self.vertexes[0][0], self.vertexes[0][1], 0)
|
147
|
|
- pyglet.gl.glVertex3f(self.vertexes[1][0], self.vertexes[1][1], 0)
|
148
|
|
- pyglet.gl.glVertex3f(self.vertexes[2][0], self.vertexes[2][1], 0)
|
149
|
|
- pyglet.gl.glVertex3f(self.vertexes[3][0], self.vertexes[3][1], 0)
|
150
|
|
- pyglet.gl.glEnd()
|
|
97
|
+
|
|
98
|
+ draw_rectangle(
|
|
99
|
+ self.vertexes,
|
|
100
|
+ self.color3,
|
|
101
|
+ self.color3f,
|
|
102
|
+ )
|
151
|
103
|
|
152
|
104
|
def set_vertexes_from_minmax(self, minx, maxx, miny, maxy):
|
153
|
105
|
self.vertexes = [(minx, miny), (minx, maxy), (maxx, maxy), (maxx, miny)]
|
|
@@ -259,6 +211,15 @@ class EditLayer(cocos.layer.Layer):
|
259
|
211
|
actor.update_cshape()
|
260
|
212
|
self.collision_manager.add(actor)
|
261
|
213
|
|
|
214
|
+ for actor, cshape in self.selection.items():
|
|
215
|
+ grid_position = self.grid_manager.get_grid_position(actor.position)
|
|
216
|
+ rect_positions = self.grid_manager.get_rectangle_positions(grid_position)
|
|
217
|
+
|
|
218
|
+ draw_rectangle(
|
|
219
|
+ self.layer_manager.scrolling_manager.world_to_screen_positions(rect_positions),
|
|
220
|
+ (0, 81, 211),
|
|
221
|
+ )
|
|
222
|
+
|
262
|
223
|
def on_enter(self):
|
263
|
224
|
super(EditLayer, self).on_enter()
|
264
|
225
|
scene = self.get_ancestor(cocos.scene.Scene)
|
|
@@ -425,9 +386,14 @@ class EditLayer(cocos.layer.Layer):
|
425
|
386
|
self.autoscrolling = False
|
426
|
387
|
|
427
|
388
|
def on_mouse_press(self, x, y, buttons, modifiers):
|
428
|
|
- if self.logger.is_debug:
|
429
|
|
- rx, ry = self.layer_manager.scrolling_manager.screen_to_world(x, y)
|
430
|
|
- self.logger.debug('GUI click: x: {}, y: {}, rx: {}, ry: {}'.format(x, y, rx, ry))
|
|
389
|
+ rx, ry = self.layer_manager.scrolling_manager.screen_to_world(x, y)
|
|
390
|
+ self.logger.info(
|
|
391
|
+ 'GUI click: x: {}, y: {}, rx: {}, ry: {} ({}|{})'.format(x, y, rx, ry, buttons, modifiers)
|
|
392
|
+ )
|
|
393
|
+
|
|
394
|
+ actor = self.single_actor_from_mouse()
|
|
395
|
+ if actor:
|
|
396
|
+ self.selection_add(actor)
|
431
|
397
|
|
432
|
398
|
def on_mouse_release(self, sx, sy, button, modifiers):
|
433
|
399
|
# should we handle here mod_restricted_mov ?
|