|
@@ -3,11 +3,13 @@ import weakref
|
3
|
3
|
|
4
|
4
|
import cocos
|
5
|
5
|
import pyglet
|
6
|
|
-from pyglet.window import key as wkey, mouse
|
|
6
|
+from pyglet.window import mouse
|
7
|
7
|
|
8
|
|
-from cocos import collision_model, euclid
|
|
8
|
+from cocos import collision_model
|
|
9
|
+from cocos import euclid
|
9
|
10
|
from cocos.director import director
|
10
|
|
-from cocos.layer import ScrollableLayer, Layer
|
|
11
|
+from cocos.layer import ScrollableLayer
|
|
12
|
+from cocos.layer import Layer
|
11
|
13
|
from cocos.sprite import Sprite
|
12
|
14
|
|
13
|
15
|
from synergine2.config import Config
|
|
@@ -18,6 +20,39 @@ from synergine2_cocos2d.layer import LayerManager
|
18
|
20
|
from synergine2_cocos2d.middleware import TMXMiddleware
|
19
|
21
|
|
20
|
22
|
|
|
23
|
+class Actor(cocos.sprite.Sprite):
|
|
24
|
+ def __init__(
|
|
25
|
+ self,
|
|
26
|
+ image,
|
|
27
|
+ position=(0, 0),
|
|
28
|
+ rotation=0,
|
|
29
|
+ scale=1,
|
|
30
|
+ opacity=255,
|
|
31
|
+ color=(255, 255, 255),
|
|
32
|
+ anchor=None,
|
|
33
|
+ **kwargs
|
|
34
|
+ ):
|
|
35
|
+ super().__init__(
|
|
36
|
+ image,
|
|
37
|
+ position,
|
|
38
|
+ rotation,
|
|
39
|
+ scale,
|
|
40
|
+ opacity,
|
|
41
|
+ color,
|
|
42
|
+ anchor,
|
|
43
|
+ **kwargs
|
|
44
|
+ )
|
|
45
|
+ self.cshape = collision_model.AARectShape(
|
|
46
|
+ euclid.Vector2(0.0, 0.0),
|
|
47
|
+ self.width,
|
|
48
|
+ self.height,
|
|
49
|
+ )
|
|
50
|
+
|
|
51
|
+ def update_position(self, new_position: euclid.Vector2) -> None:
|
|
52
|
+ self.position = new_position
|
|
53
|
+ self.cshape.center = new_position
|
|
54
|
+
|
|
55
|
+
|
21
|
56
|
class GridManager(object):
|
22
|
57
|
def __init__(
|
23
|
58
|
self,
|
|
@@ -75,29 +110,11 @@ class GridLayerMixin(object):
|
75
|
110
|
super().__init__(*args, **kwargs)
|
76
|
111
|
|
77
|
112
|
|
78
|
|
-class ProbeQuad(cocos.cocosnode.CocosNode):
|
79
|
|
-
|
80
|
|
- def __init__(self, r, color4):
|
81
|
|
- super(ProbeQuad, self).__init__()
|
82
|
|
- self.color4 = color4
|
83
|
|
- self.vertexes = [(r, 0, 0), (0, r, 0), (-r, 0, 0), (0, -r, 0)]
|
84
|
|
-
|
85
|
|
- def draw(self):
|
86
|
|
- pyglet.gl.glPushMatrix()
|
87
|
|
- self.transform()
|
88
|
|
- pyglet.gl.glBegin(pyglet.gl.GL_QUADS)
|
89
|
|
- pyglet.gl.glColor4ub(*self.color4)
|
90
|
|
- for v in self.vertexes:
|
91
|
|
- pyglet.gl.glVertex3i(*v)
|
92
|
|
- pyglet.gl.glEnd()
|
93
|
|
- pyglet.gl.glPopMatrix()
|
94
|
|
-
|
95
|
|
-
|
96
|
113
|
class MinMaxRect(cocos.cocosnode.CocosNode):
|
97
|
114
|
def __init__(self, layer_manager: LayerManager):
|
98
|
115
|
super(MinMaxRect, self).__init__()
|
99
|
116
|
self.layer_manager = layer_manager
|
100
|
|
- self.color3 = (0, 0, 255)
|
|
117
|
+ self.color3 = (20, 20, 20)
|
101
|
118
|
self.vertexes = [(0.0, 0.0), (0.0, 0.0), (0.0, 0.0), (0.0, 0.0)]
|
102
|
119
|
self.visible = False
|
103
|
120
|
|
|
@@ -107,6 +124,29 @@ class MinMaxRect(cocos.cocosnode.CocosNode):
|
107
|
124
|
smaxx, smaxy = self.layer_manager.scrolling_manager.world_to_screen(wmaxx, wmaxy)
|
108
|
125
|
self.vertexes = [(sminx, sminy), (sminx, smaxy), (smaxx, smaxy), (smaxx, sminy)]
|
109
|
126
|
|
|
127
|
+ def draw(self):
|
|
128
|
+ if not self.visible:
|
|
129
|
+ return
|
|
130
|
+ pyglet.gl.glLineWidth(1) # deprecated
|
|
131
|
+ pyglet.gl.glColor3ub(*self.color3)
|
|
132
|
+ pyglet.gl.glBegin(pyglet.gl.GL_LINE_STRIP)
|
|
133
|
+ for v in self.vertexes:
|
|
134
|
+ pyglet.gl.glVertex2f(*v)
|
|
135
|
+ pyglet.gl.glVertex2f(*self.vertexes[0])
|
|
136
|
+ pyglet.gl.glEnd()
|
|
137
|
+
|
|
138
|
+ # rectangle
|
|
139
|
+ pyglet.gl.glColor4f(0, 0, 0, 0.5)
|
|
140
|
+ pyglet.gl.glBegin(pyglet.gl.GL_QUADS)
|
|
141
|
+ pyglet.gl.glVertex3f(self.vertexes[0][0], self.vertexes[0][1], 0)
|
|
142
|
+ pyglet.gl.glVertex3f(self.vertexes[1][0], self.vertexes[1][1], 0)
|
|
143
|
+ pyglet.gl.glVertex3f(self.vertexes[2][0], self.vertexes[2][1], 0)
|
|
144
|
+ pyglet.gl.glVertex3f(self.vertexes[3][0], self.vertexes[3][1], 0)
|
|
145
|
+ pyglet.gl.glEnd()
|
|
146
|
+
|
|
147
|
+ def set_vertexes_from_minmax(self, minx, maxx, miny, maxy):
|
|
148
|
+ self.vertexes = [(minx, miny), (minx, maxy), (maxx, maxy), (maxx, miny)]
|
|
149
|
+
|
110
|
150
|
|
111
|
151
|
class EditLayer(cocos.layer.Layer):
|
112
|
152
|
is_event_handler = True
|
|
@@ -164,7 +204,6 @@ class EditLayer(cocos.layer.Layer):
|
164
|
204
|
self.wdrag_start_point = (0, 0)
|
165
|
205
|
self.elastic_box = None # type: MinMaxRect
|
166
|
206
|
self.elastic_box_wminmax = 0, 0, 0, 0
|
167
|
|
- self.mouse_mark = None # type: ProbeQuad
|
168
|
207
|
self.selection = {}
|
169
|
208
|
self.screen_mouse = (0, 0)
|
170
|
209
|
self.world_mouse = (0, 0)
|
|
@@ -187,8 +226,6 @@ class EditLayer(cocos.layer.Layer):
|
187
|
226
|
gsize,
|
188
|
227
|
gsize,
|
189
|
228
|
)
|
190
|
|
- for actor in worldview.actors:
|
191
|
|
- self.collman.add(actor)
|
192
|
229
|
|
193
|
230
|
self.schedule(self.update)
|
194
|
231
|
|
|
@@ -198,9 +235,6 @@ class EditLayer(cocos.layer.Layer):
|
198
|
235
|
if self.elastic_box is None:
|
199
|
236
|
self.elastic_box = MinMaxRect(self.layer_manager)
|
200
|
237
|
scene.add(self.elastic_box, z=10)
|
201
|
|
- if self.mouse_mark is None:
|
202
|
|
- self.mouse_mark = ProbeQuad(4, (0, 0, 0, 255))
|
203
|
|
- scene.add(self.mouse_mark, z=11)
|
204
|
238
|
|
205
|
239
|
def update(self, dt):
|
206
|
240
|
mx = self.buttons['right'] - self.buttons['left']
|
|
@@ -296,7 +330,6 @@ class EditLayer(cocos.layer.Layer):
|
296
|
330
|
self.autoscrolling = sdx != 0.0 or sdy != 0.0
|
297
|
331
|
if self.autoscrolling:
|
298
|
332
|
self.autoscrolling_sdelta = (sdx / border, sdy / border)
|
299
|
|
- self.mouse_mark.position = self.layer_manager.scrolling_manager.world_to_screen(*self.world_mouse)
|
300
|
333
|
|
301
|
334
|
def update_autoscroll(self, dt):
|
302
|
335
|
fraction_sdx, fraction_sdy = self.autoscrolling_sdelta
|
|
@@ -525,37 +558,11 @@ class MainLayer(ScrollableLayer):
|
525
|
558
|
self.scroll_step = scroll_step
|
526
|
559
|
self.grid_manager = GridManager(self, 32, border=2)
|
527
|
560
|
|
528
|
|
- self.actors = [] # TODO type list of ? Sprite ?
|
529
|
561
|
self.width = width
|
530
|
562
|
self.height = height
|
531
|
563
|
self.px_width = width
|
532
|
564
|
self.px_height = height
|
533
|
565
|
|
534
|
|
- # Set scene center on center of screen
|
535
|
|
- window_size = director.get_window_size()
|
536
|
|
- self.position = window_size[0] // 2, window_size[1] // 2
|
537
|
|
-
|
538
|
|
- def on_key_press(self, key, modifiers):
|
539
|
|
- if key == wkey.LEFT:
|
540
|
|
- self.position = (self.position[0] + self.scroll_step, self.position[1])
|
541
|
|
-
|
542
|
|
- if key == wkey.RIGHT:
|
543
|
|
- self.position = (self.position[0] - self.scroll_step, self.position[1])
|
544
|
|
-
|
545
|
|
- if key == wkey.UP:
|
546
|
|
- self.position = (self.position[0], self.position[1] - self.scroll_step)
|
547
|
|
-
|
548
|
|
- if key == wkey.DOWN:
|
549
|
|
- self.position = (self.position[0], self.position[1] + self.scroll_step)
|
550
|
|
-
|
551
|
|
- if key == wkey.A:
|
552
|
|
- if self.scale >= 0.3:
|
553
|
|
- self.scale -= 0.2
|
554
|
|
-
|
555
|
|
- if key == wkey.Z:
|
556
|
|
- if self.scale <= 4:
|
557
|
|
- self.scale += 0.2
|
558
|
|
-
|
559
|
566
|
|
560
|
567
|
class Gui(object):
|
561
|
568
|
def __init__(
|
|
@@ -570,6 +577,7 @@ class Gui(object):
|
570
|
577
|
self._read_queue_interval = read_queue_interval
|
571
|
578
|
self.terminal = terminal
|
572
|
579
|
self.cycle_duration = self.config.core.cycle_duration
|
|
580
|
+
|
573
|
581
|
cocos.director.director.init(
|
574
|
582
|
width=640,
|
575
|
583
|
height=480,
|
|
@@ -577,6 +585,14 @@ class Gui(object):
|
577
|
585
|
resizable=True,
|
578
|
586
|
)
|
579
|
587
|
|
|
588
|
+ # Enable blending
|
|
589
|
+ pyglet.gl.glEnable(pyglet.gl.GL_BLEND)
|
|
590
|
+ pyglet.gl.glBlendFunc(pyglet.gl.GL_SRC_ALPHA, pyglet.gl.GL_ONE_MINUS_SRC_ALPHA)
|
|
591
|
+
|
|
592
|
+ # Enable transparency
|
|
593
|
+ pyglet.gl.glEnable(pyglet.gl.GL_ALPHA_TEST)
|
|
594
|
+ pyglet.gl.glAlphaFunc(pyglet.gl.GL_GREATER, .1)
|
|
595
|
+
|
580
|
596
|
def run(self):
|
581
|
597
|
self.before_run()
|
582
|
598
|
pyglet.clock.schedule_interval(
|
|
@@ -628,4 +644,4 @@ class TMXGui(Gui):
|
628
|
644
|
self.layer_manager.center()
|
629
|
645
|
|
630
|
646
|
def get_main_scene(self) -> cocos.cocosnode.CocosNode:
|
631
|
|
- return self.layer_manager.main_scene
|
|
647
|
+ return self.layer_manager.main_scene
|