forked from cocos/cocos-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsb-action.js
441 lines (369 loc) · 12.8 KB
/
jsb-action.js
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
var ENABLE_GC_FOR_NATIVE_OBJECTS = cc.macro.ENABLE_GC_FOR_NATIVE_OBJECTS;
cc.Action.prototype._getSgTarget = cc.Action.prototype.getTarget;
cc.Action.prototype.getTarget = function () {
var sgNode = this._getSgTarget();
return sgNode._owner || sgNode;
};
cc.targetedAction = function (target, action) {
return new cc.TargetedAction(target, action);
};
cc.TargetedAction.prototype._ctor = function(target, action) {
var node = target._sgNode || target;
node._owner = target;
action && this.initWithTarget(node, action);
};
cc.follow = function (followedNode, rect) {
return new cc.Follow(followedNode, rect);
};
cc.Follow = cc.BaseJSAction.extend({
_followedNode:null,
_boundarySet:false,
_boundaryFullyCovered:false,
_halfScreenSize:null,
_fullScreenSize:null,
_worldRect:null,
leftBoundary:0.0,
rightBoundary:0.0,
topBoundary:0.0,
bottomBoundary:0.0,
ctor:function (followedNode, rect) {
cc.BaseJSAction.prototype.ctor.call(this);
this._followedNode = null;
this._boundarySet = false;
this._boundaryFullyCovered = false;
this._halfScreenSize = null;
this._fullScreenSize = null;
this.leftBoundary = 0.0;
this.rightBoundary = 0.0;
this.topBoundary = 0.0;
this.bottomBoundary = 0.0;
this._worldRect = cc.rect(0, 0, 0, 0);
if(followedNode)
rect ? this.initWithTarget(followedNode, rect)
: this.initWithTarget(followedNode);
},
clone:function () {
var action = new cc.Follow();
var locRect = this._worldRect;
var rect = new cc.Rect(locRect.x, locRect.y, locRect.width, locRect.height);
action.initWithTarget(this._followedNode, rect);
return action;
},
isBoundarySet:function () {
return this._boundarySet;
},
setBoudarySet:function (value) {
this._boundarySet = value;
},
initWithTarget:function (followedNode, rect) {
if(!followedNode)
throw new Error("cc.Follow.initWithAction(): followedNode must be non nil");
var _this = this;
rect = rect || cc.rect(0, 0, 0, 0);
_this._followedNode = followedNode;
_this._worldRect = rect;
_this._boundarySet = !cc._rectEqualToZero(rect);
_this._boundaryFullyCovered = false;
var winSize = cc.director.getWinSize();
_this._fullScreenSize = cc.p(winSize.width, winSize.height);
_this._halfScreenSize = cc.pMult(_this._fullScreenSize, 0.5);
if (_this._boundarySet) {
_this.leftBoundary = -((rect.x + rect.width) - _this._fullScreenSize.x);
_this.rightBoundary = -rect.x;
_this.topBoundary = -rect.y;
_this.bottomBoundary = -((rect.y + rect.height) - _this._fullScreenSize.y);
if (_this.rightBoundary < _this.leftBoundary) {
_this.rightBoundary = _this.leftBoundary = (_this.leftBoundary + _this.rightBoundary) / 2;
}
if (_this.topBoundary < _this.bottomBoundary) {
_this.topBoundary = _this.bottomBoundary = (_this.topBoundary + _this.bottomBoundary) / 2;
}
if ((_this.topBoundary === _this.bottomBoundary) && (_this.leftBoundary === _this.rightBoundary))
_this._boundaryFullyCovered = true;
}
return true;
},
step:function (dt) {
var target = this.getTarget();
var targetWorldPos = target.convertToWorldSpaceAR(cc.Vec2.ZERO);
var followedWorldPos = this._followedNode.convertToWorldSpaceAR(cc.Vec2.ZERO);
var delta = cc.pSub(targetWorldPos, followedWorldPos);
var tempPos = target.parent.convertToNodeSpaceAR(cc.pAdd(delta, this._halfScreenSize));
if (this._boundarySet) {
if (this._boundaryFullyCovered)
return;
target.setPosition(cc.clampf(tempPos.x, this.leftBoundary, this.rightBoundary), cc.clampf(tempPos.y, this.bottomBoundary, this.topBoundary));
} else {
target.setPosition(tempPos.x, tempPos.y);
}
},
isDone:function () {
return ( !this._followedNode.isRunning() );
},
stop:function () {
this.setTarget(null);
cc.Action.prototype.stop.call(this);
}
});
var _FlipX = cc.FlipX;
cc.FlipX = _FlipX.extend({
_flippedX:false,
ctor:function(flip){
_FlipX.prototype.ctor.call(this);
this.initWithFlipX(flip);
},
initWithFlipX:function (flip) {
this._flippedX = !!flip;
return true;
},
update:function (dt) {
var target = this._getSgTarget();
target.scaleX = Math.abs(target.scaleX) * (this._flippedX ? -1 : 1);
},
reverse:function () {
return new cc.FlipX(!this._flippedX);
},
clone:function(){
return new cc.FlipX(this._flippedX);
}
});
cc.flipX = function (flip) {
return new cc.FlipX(flip);
};
var _FlipY = cc.FlipY;
cc.FlipY = _FlipY.extend({
_flippedY:false,
ctor: function(flip){
_FlipY.prototype.ctor.call(this);
this.initWithFlipY(flip);
},
initWithFlipY:function (flip) {
this._flippedY = !!flip;
return true;
},
update:function (dt) {
var target = this._getSgTarget();
target.scaleY = Math.abs(target.scaleY) * (this._flippedY ? -1 : 1);
},
reverse:function () {
return new cc.FlipY(!this._flippedY);
},
clone:function(){
return new cc.FlipY(this._flippedY);
}
});
cc.flipY = function (flip) {
return new cc.FlipY(flip);
};
function setRendererVisibility (sgNode, toggleVisible, visible) {
if (!sgNode) { return; }
var _renderComps = sgNode._owner.getComponentsInChildren(cc._SGComponent);
for (var i = 0; i < _renderComps.length; ++i) {
var render = _renderComps[i];
render.enabled = toggleVisible ? !render.enabled : visible;
}
}
cc.Show.prototype.update = function (dt) {
setRendererVisibility(this._getSgTarget(), false, true);
};
cc.Hide.prototype.update = function (dt) {
setRendererVisibility(this._getSgTarget(), false, false);
};
cc.ToggleVisibility.prototype.update = function (dt) {
setRendererVisibility(this._getSgTarget(), true);
};
// Special call func
cc.callFunc = function (selector, selectorTarget, data) {
var callback = function (sender) {
if (sender) {
sender = sender._owner || sender;
}
selector.call(this, sender, data);
};
var action = selectorTarget ? cc.CallFunc.create(callback, selectorTarget) : cc.CallFunc.create(callback);
return action;
};
cc.CallFunc.prototype._ctor = function (selector, selectorTarget, data) {
if(selector !== undefined){
var callback = function (sender) {
if (sender) {
sender = sender._owner || sender;
}
selector.call(this, sender, data);
};
if (selectorTarget === undefined) {
this.initWithFunction(callback);
}
else {
this.initWithFunction(callback, selectorTarget);
}
}
};
function getSGTarget (target) {
if (target instanceof cc.Component) {
target = target.node._sgNode;
}
else if (target instanceof cc.Node) {
target = target._sgNode;
}
else if (!(target instanceof _ccsg.Node)) {
target = null;
}
return target;
}
var jsbAddAction = cc.ActionManager.prototype.addAction;
cc.ActionManager.prototype.addAction = function (action, target, paused) {
target = getSGTarget(target);
if (target) {
jsbAddAction.call(this, action, target, paused);
}
};
function actionMgrFuncReplacer (funcName, targetPos) {
var proto = cc.ActionManager.prototype;
var oldFunc = proto[funcName];
proto[funcName] = function () {
var args = [];
for (var i = 0; i < arguments.length; i++) {
if (i === targetPos)
args[i] = getSGTarget(arguments[i]);
else
args[i] = arguments[i];
}
if (!args[targetPos]) {
return;
}
else {
return oldFunc.apply(this, args);
}
};
}
var targetRelatedFuncs = [
['removeAllActionsFromTarget', 0],
['removeActionByTag', 1],
['getActionByTag', 1],
['getNumberOfRunningActionsInTarget', 0],
['pauseTarget', 0],
['resumeTarget', 0]
];
for (var i = 0; i < targetRelatedFuncs.length; ++i) {
actionMgrFuncReplacer.apply(null, targetRelatedFuncs[i]);
}
cc.ActionManager.prototype.resumeTargets = function (targetsToResume) {
if (!targetsToResume)
return;
for (var i = 0; i< targetsToResume.length; i++) {
if (targetsToResume[i])
this.resumeTarget(targetsToResume[i]);
}
};
cc.ActionManager.prototype.pauseTargets = function (targetsToPause) {
if (!targetsToPause)
return;
for (var i = 0; i< targetsToPause.length; i++) {
if (targetsToPause[i])
this.pauseTarget(targetsToPause[i]);
}
};
function syncPositionUpdate (dt) {
var target = this._getSgTarget();
if (target._owner) {
target._owner.x = target.getPositionX();
target._owner.y = target.getPositionY();
}
}
function syncRotationUpdate (dt) {
var target = this._getSgTarget();
if (target._owner) {
target._owner.rotationX = target.getRotationX();
target._owner.rotationY = target.getRotationY();
}
}
function syncScaleUpdate (dt) {
var target = this._getSgTarget();
if (target._owner) {
target._owner.scaleX = target.getScaleX();
target._owner.scaleY = target.getScaleY();
}
}
function syncRemoveSelfUpdate (dt) {
var target = this._getSgTarget();
if (target._owner) {
target._owner.removeFromParent();
}
}
function syncSkewUpdate (dt) {
var target = this._getSgTarget();
if (target._owner) {
target._owner.skewX = target.getSkewX();
target._owner.skewY = target.getSkewY();
}
}
function syncOpacityUpdate (dt) {
var target = this._getSgTarget();
if (target._owner) {
target._owner.opacity = target.getOpacity();
}
}
function syncColorUpdate (dt) {
var target = this._getSgTarget();
if (target._owner) {
var color = target.getColor();
target._owner.color = color;
}
}
// Sub classes must be registered before their super class.
// Otherwise, JSB there will be internal Error: "too much recursion".
var actionUpdate = {
'MoveBy': syncPositionUpdate,
'JumpBy': syncPositionUpdate,
'Place': syncPositionUpdate,
'CardinalSplineTo': syncPositionUpdate,
'RotateTo': syncRotationUpdate,
'RotateBy': syncRotationUpdate,
'ScaleTo': syncScaleUpdate,
'RemoveSelf': syncRemoveSelfUpdate,
'SkewTo': syncSkewUpdate,
'Blink': syncOpacityUpdate,
'FadeIn': syncOpacityUpdate,
'FadeOut': syncOpacityUpdate,
'FadeTo': syncOpacityUpdate,
'TintTo': syncColorUpdate,
'TintBy': syncColorUpdate,
'BezierBy': syncPositionUpdate
};
for (var key in actionUpdate) {
var action = cc[key];
var prototype = action.prototype;
prototype.update = actionUpdate[key];
// polyfill to preserve chainable binding object (no need to polyfill easing)
prototype.speed = function (speed) {
return new cc.Speed(this, speed);
};
prototype.repeat = function (times) {
return new cc.Repeat(this, times);
};
prototype.repeatForever = function () {
return new cc.RepeatForever(this);
};
}