-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathlayer.cpp
385 lines (326 loc) · 10.8 KB
/
layer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#include "rubymotion.h"
#include "motion-game.h"
#include <dlfcn.h>
/// @class Scene < Node
/// This class represents a scene, an independent screen or stage of the
/// application workflow. A scene is responsible for handling events from the
/// device, providing a physics world for the sprites, and also starting the
/// game loop.
/// An application must have at least one scene, and the +Scene+ class is
/// designed to be subclassed.
VALUE rb_cScene = Qnil;
#if CC_TARGET_OS_IPHONE && TARGET_IPHONE_SIMULATOR
extern "C" {
void rb_repl_new(VALUE);
}
#endif
enum mc_Scene_EventType {
ON_BEGIN,
ON_MOVE,
ON_END,
ON_CANCEL
};
class mc_Scene : public cocos2d::LayerColor {
public:
cocos2d::Scene *scene;
VALUE obj;
SEL update_sel;
cocos2d::EventListenerTouchOneByOne *touch_listener;
mc_Scene() {
obj = Qnil;
touch_listener = NULL;
#if CC_TARGET_OS_IPHONE || CC_TARGET_OS_APPLETV
update_sel = rb_selector("update:");
#else
update_sel = rb_selector("update");
#endif
}
static mc_Scene *create(void) {
auto scene = new mc_Scene();
scene->initWithColor(cocos2d::Color4B::BLACK);
return scene;
}
virtual void update(float delta) {
LayerColor::update(delta);
VALUE arg = DBL2NUM(delta);
rb_send(obj, update_sel, 1, &arg);
}
void setBackgroundColor(cocos2d::Color3B color) {
setColor(color);
updateColor();
}
#if CC_TARGET_OS_IPHONE && TARGET_IPHONE_SIMULATOR
virtual void onEnter() {
cocos2d::LayerColor::onEnter();
rb_repl_new(this->obj);
}
#endif
};
#define SCENE(obj) _COCOS_WRAP_GET(obj, mc_Scene)
extern "C"
cocos2d::Scene *
rb_any_to_scene(VALUE obj)
{
if (rb_obj_is_kind_of(obj, rb_cScene)) {
return SCENE(obj)->scene;
}
rb_raise(rb_eArgError, "expected Scene object");
}
static VALUE
scene_alloc(VALUE rcv, SEL sel)
{
auto layer = mc_Scene::create();
auto scene = cocos2d::Scene::createWithPhysics();
scene->addChild(layer);
layer->scene = scene;
VALUE obj = rb_cocos2d_object_new(layer, rcv);
layer->obj = rb_retain(obj);
return obj;
}
/// @group Constructors
/// @method #initialize
/// The default initializer. Subclasses can construct the scene interface in
/// this method, as well as providing an implementation for {#update}, then
/// run the update loop by calling {#start_update}.
/// @return [Scene] the receiver.
static VALUE
scene_initialize(VALUE rcv, SEL sel)
{
return rcv;
}
/// @group Update Loop
/// @method #start_update
/// Starts the update loop. The +#update+ method will be called on this object
/// for every frame.
/// @return [self] the receiver.
static VALUE
scene_start_update(VALUE rcv, SEL sel)
{
SCENE(rcv)->scheduleUpdate();
return rcv;
}
/// @method #stop_update
/// Stops the update loop. The +#update+ method will no longer be called on
/// this object.
/// @return [self] the receiver.
static VALUE
scene_stop_update(VALUE rcv, SEL sel)
{
SCENE(rcv)->unscheduleUpdate();
return rcv;
}
/// @method #update(delta)
/// The update loop method. Subclasses can provide a custom implementation of
/// this method. The default implementation is empty.
/// @param delta [Float] a value representing the amount of time, in seconds,
/// since the last time this method was called.
/// @return [self] the receiver.
static VALUE
scene_update(VALUE rcv, SEL sel, VALUE delta)
{
// Do nothing.
return rcv;
}
/// @group Events
static VALUE
scene_add_listener(VALUE rcv, cocos2d::EventListener *listener)
{
auto scene = SCENE(rcv);
scene->getEventDispatcher()->addEventListenerWithSceneGraphPriority(
listener, scene);
return rcv;
}
static bool
scene_dummy_onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event) {
return true;
}
static VALUE
scene_on_touch_event(VALUE rcv, SEL sel, mc_Scene_EventType type)
{
VALUE block = rb_current_block();
if (block == Qnil) {
rb_raise(rb_eArgError, "block not given");
}
block = rb_retain(block); // FIXME need release...
auto scene = SCENE(rcv);
if (scene->touch_listener == NULL) {
scene->touch_listener = cocos2d::EventListenerTouchOneByOne::create();
}
else {
scene->getEventDispatcher()->removeEventListener(scene->touch_listener);
}
auto lambda = [block](cocos2d::Touch *touch, cocos2d::Event *event) -> bool {
VALUE touch_obj = rb_cocos2d_object_new(touch, rb_cTouch);
return RTEST(rb_block_call(block, 1, &touch_obj));
};
switch (type) {
case ON_BEGIN:
scene->touch_listener->onTouchBegan = lambda;
break;
case ON_MOVE:
scene->touch_listener->onTouchMoved = lambda;
break;
case ON_END:
scene->touch_listener->onTouchEnded = lambda;
break;
case ON_CANCEL:
scene->touch_listener->onTouchCancelled = lambda;
break;
}
if (scene->touch_listener->onTouchBegan == NULL) {
// EventDispatcher will use the return value of 'onTouchBegan' to determine whether to pass following 'move', 'end'
// message to 'EventListenerTouchOneByOne' or not. So 'onTouchBegan' needs to be set.
scene->touch_listener->onTouchBegan = scene_dummy_onTouchBegan;
}
return scene_add_listener(rcv, scene->touch_listener);
}
/// @method #on_touch_begin
/// Starts listening for touch begin events on the receiver.
/// @yield [Events::Touch] the given block will be yield when a touch begin
/// event is received.
/// @return [self] the receiver.
static VALUE
scene_on_touch_begin(VALUE rcv, SEL sel)
{
return scene_on_touch_event(rcv, sel, mc_Scene_EventType::ON_BEGIN);
}
/// @method #on_touch_end
/// Starts listening for touch end events on the receiver.
/// @yield [Events::Touch] the given block will be yield when a touch end
/// event is received.
/// @return [self] the receiver.
static VALUE
scene_on_touch_end(VALUE rcv, SEL sel)
{
return scene_on_touch_event(rcv, sel, mc_Scene_EventType::ON_END);
}
/// @method #on_touch_move
/// Starts listening for touch move events on the receiver.
/// @yield [Events::Touch] the given block will be yield when a touch move
/// event is received.
/// @return [self] the receiver.
static VALUE
scene_on_touch_move(VALUE rcv, SEL sel)
{
return scene_on_touch_event(rcv, sel, mc_Scene_EventType::ON_MOVE);
}
/// @method #on_touch_cancel
/// Starts listening for touch cancel events on the receiver.
/// @yield [Events::Touch] the given block will be yield when a touch cancel
/// event is received.
/// @return [self] the receiver.
static VALUE
scene_on_touch_cancel(VALUE rcv, SEL sel)
{
return scene_on_touch_event(rcv, sel, mc_Scene_EventType::ON_CANCEL);
}
/// @method #on_accelerate
/// Starts listening for accelerometer events on the receiver.
/// @yield [Events::Acceleration] the given block will be yield when an
/// accelerometer event is received from the device.
/// @return [self] the receiver.
static VALUE
scene_on_accelerate(VALUE rcv, SEL sel)
{
#if CC_TARGET_OS_APPLETV
rb_raise(rb_eRuntimeError, "Not supported in tvOS");
#else
VALUE block = rb_current_block();
if (block == Qnil) {
rb_raise(rb_eArgError, "block not given");
}
block = rb_retain(block); // FIXME need release...
cocos2d::Device::setAccelerometerEnabled(true);
auto listener = cocos2d::EventListenerAcceleration::create(
[block](cocos2d::Acceleration *acc, cocos2d::Event *event) {
VALUE acc_obj = rb_cocos2d_object_new(acc, rb_cAcceleration);
rb_block_call(block, 1, &acc_obj);
});
return scene_add_listener(rcv, listener);
#endif
}
/// @method #on_contact_begin
/// Starts listening for contact begin events from the physics engine.
/// @yield [Events::PhysicsContact] the given block will be yield when a
/// contact event is received from the physics engine.
/// @return [self] the receiver.
static VALUE
scene_on_contact_begin(VALUE rcv, SEL sel)
{
VALUE block = rb_current_block();
if (block == Qnil) {
rb_raise(rb_eArgError, "block not given");
}
block = rb_retain(block); // FIXME need release...
auto listener = cocos2d::EventListenerPhysicsContact::create();
listener->onContactBegin = [block](cocos2d::PhysicsContact &contact) -> bool {
return RTEST(rb_block_call(block, 0, NULL));
};
return scene_add_listener(rcv, listener);
}
/// @endgroup
/// @property #gravity
/// @return [Point] the gravity of the scene's physics world.
static VALUE
scene_gravity(VALUE rcv, SEL sel)
{
return rb_ccvec2_to_obj(SCENE(rcv)->scene->getPhysicsWorld()->getGravity());
}
static VALUE
scene_gravity_set(VALUE rcv, SEL sel, VALUE arg)
{
SCENE(rcv)->scene->getPhysicsWorld()->setGravity(rb_any_to_ccvec2(arg));
return rcv;
}
/// @method #debug_physics?
/// @return [Boolean] whether the physics engine should draw debug lines.
static VALUE
scene_debug_physics(VALUE rcv, SEL sel)
{
return SCENE(rcv)->scene->getPhysicsWorld()->getDebugDrawMask()
== cocos2d::PhysicsWorld::DEBUGDRAW_NONE ? Qfalse : Qtrue;
}
/// @method #debug_physics=(value)
/// Set to draw the debug line.
/// @param value [Boolean] true if draw debug lines.
static VALUE
scene_debug_physics_set(VALUE rcv, SEL sel, VALUE arg)
{
SCENE(rcv)->scene->getPhysicsWorld()->setDebugDrawMask(RTEST(arg)
? cocos2d::PhysicsWorld::DEBUGDRAW_ALL
: cocos2d::PhysicsWorld::DEBUGDRAW_NONE);
return arg;
}
/// @method #background_color=(color)
/// Set background color for scene.
/// @param color [Color] background color for scene.
static VALUE
scene_background_color_set(VALUE rcv, SEL sel, VALUE val)
{
SCENE(rcv)->setBackgroundColor(rb_any_to_cccolor3(val));
return val;
}
extern "C"
void
Init_Layer(void)
{
rb_cScene = rb_define_class_under(rb_mMC, "Scene", rb_cNode);
// rb_register_cocos2d_object_finalizer(rb_cScene); removed because rb_cScene inherits rb_cNode and it already has finalizer.
rb_define_singleton_method(rb_cScene, "alloc", scene_alloc, 0);
rb_define_method(rb_cScene, "initialize", scene_initialize, 0);
rb_define_method(rb_cScene, "start_update", scene_start_update, 0);
rb_define_method(rb_cScene, "stop_update", scene_stop_update, 0);
rb_define_method(rb_cScene, "update", scene_update, 1);
rb_define_method(rb_cScene, "on_touch_begin", scene_on_touch_begin, 0);
rb_define_method(rb_cScene, "on_touch_end", scene_on_touch_end, 0);
rb_define_method(rb_cScene, "on_touch_move", scene_on_touch_move, 0);
rb_define_method(rb_cScene, "on_touch_cancel", scene_on_touch_cancel, 0);
rb_define_method(rb_cScene, "on_accelerate", scene_on_accelerate, 0);
rb_define_method(rb_cScene, "on_contact_begin", scene_on_contact_begin, 0);
rb_define_method(rb_cScene, "gravity", scene_gravity, 0);
rb_define_method(rb_cScene, "gravity=", scene_gravity_set, 1);
rb_define_method(rb_cScene, "debug_physics?", scene_debug_physics, 0);
rb_define_method(rb_cScene, "debug_physics=", scene_debug_physics_set, 1);
rb_define_method(rb_cScene, "background_color=", scene_background_color_set, 1);
rb_define_method(rb_cScene, "color=", scene_background_color_set, 1); // depricated
}