瀏覽代碼

draw selection rectangles

Bastien Sevajol 7 年之前
父節點
當前提交
bdf81d6bd6
共有 3 個文件被更改,包括 124 次插入84 次删除
  1. 40 0
      synergine2_cocos2d/gl.py
  2. 48 82
      synergine2_cocos2d/gui.py
  3. 36 2
      synergine2_cocos2d/layer.py

+ 40 - 0
synergine2_cocos2d/gl.py 查看文件

@@ -0,0 +1,40 @@
1
+# coding: utf-8
2
+import typing
3
+
4
+import pyglet
5
+
6
+rectangle_positions_type = typing.Union[
7
+    typing.Tuple[
8
+        typing.Tuple[int, int], typing.Tuple[int, int], typing.Tuple[int, int], typing.Tuple[int, int]
9
+    ],
10
+    typing.List[typing.Tuple[int, int]],
11
+]
12
+rgb_type = typing.Tuple[int, int, int]
13
+rgb_alpha_type = typing.Tuple[int, int, int, float]
14
+
15
+
16
+def draw_rectangle(
17
+    positions: rectangle_positions_type,
18
+    around_color: rgb_type,
19
+    fill_color: typing.Optional[rgb_alpha_type]=None,
20
+):
21
+    """
22
+    A<---D
23
+    |    |
24
+    B--->C
25
+    """
26
+    pyglet.gl.glColor3ub(*around_color)
27
+    pyglet.gl.glBegin(pyglet.gl.GL_LINE_STRIP)
28
+    for v in positions:
29
+        pyglet.gl.glVertex2f(*v)
30
+    pyglet.gl.glVertex2f(*positions[0])
31
+    pyglet.gl.glEnd()
32
+
33
+    if fill_color:
34
+        pyglet.gl.glColor4f(*fill_color)
35
+        pyglet.gl.glBegin(pyglet.gl.GL_QUADS)
36
+        pyglet.gl.glVertex3f(positions[0][0], positions[0][1], 0)
37
+        pyglet.gl.glVertex3f(positions[1][0], positions[1][1], 0)
38
+        pyglet.gl.glVertex3f(positions[2][0], positions[2][1], 0)
39
+        pyglet.gl.glVertex3f(positions[3][0], positions[3][1], 0)
40
+        pyglet.gl.glEnd()

+ 48 - 82
synergine2_cocos2d/gui.py 查看文件

@@ -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 ?

+ 36 - 2
synergine2_cocos2d/layer.py 查看文件

@@ -1,4 +1,6 @@
1 1
 # coding: utf-8
2
+import typing
3
+
2 4
 from pyglet.window import key
3 5
 
4 6
 import cocos
@@ -12,6 +14,38 @@ if False:
12 14
     from synergine2_cocos2d.gui import GridManager
13 15
 
14 16
 
17
+class ScrollingManager(cocos.layer.ScrollingManager):
18
+    def world_to_screen_positions(
19
+        self,
20
+        positions: typing.List[typing.Tuple[int, int]],
21
+    ) -> typing.List[typing.Tuple[int, int]]:
22
+        screen_positions = []
23
+
24
+        for position in positions:
25
+            scx, scy = self.world_to_screen(
26
+                position[0],
27
+                position[1],
28
+            )
29
+            screen_positions.append((scx, scy))
30
+
31
+        return screen_positions
32
+
33
+    def screen_to_world_positions(
34
+        self,
35
+        positions: typing.List[typing.Tuple[int, int]],
36
+    ) -> typing.List[typing.Tuple[int, int]]:
37
+        world_positions = []
38
+
39
+        for position in positions:
40
+            wx, wy = self.screen_to_world(
41
+                position[0],
42
+                position[1],
43
+            )
44
+            world_positions.append((wx, wy))
45
+
46
+        return world_positions
47
+
48
+
15 49
 class LayerManager(object):
16 50
     def __init__(
17 51
         self,
@@ -24,7 +58,7 @@ class LayerManager(object):
24 58
         self.middleware = middleware
25 59
 
26 60
         self.grid_manager = None  # type: GridManager
27
-        self.scrolling_manager = None  # type: cocos.layer.ScrollingManager
61
+        self.scrolling_manager = None  # type: ScrollingManager
28 62
         self.main_scene = None  # type: cocos.scene.Scene
29 63
         self.main_layer = None  # type: cocos.layer.Layer
30 64
         self.edit_layer = None  # TODO type
@@ -50,7 +84,7 @@ class LayerManager(object):
50 84
         )
51 85
 
52 86
         self.main_scene = cocos.scene.Scene()
53
-        self.scrolling_manager = cocos.layer.ScrollingManager()
87
+        self.scrolling_manager = ScrollingManager()
54 88
 
55 89
         self.main_layer = MainLayer(
56 90
             self,