mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 07:42:38 +08:00
Merge branch 'master' of github.com:fireball-x/engine
This commit is contained in:
+249
-3
@@ -24,12 +24,125 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
var EventTarget = require('./event/event-target');
|
||||
|
||||
var JS = cc.js;
|
||||
var Flags = cc.Object.Flags;
|
||||
var Destroying = Flags.Destroying;
|
||||
var DontDestroy = Flags.DontDestroy;
|
||||
//var RegisteredInEditor = Flags.RegisteredInEditor;
|
||||
|
||||
/**
|
||||
* The event type supported by Node
|
||||
* @class Node.EventType
|
||||
* @static
|
||||
* @namespace Node
|
||||
*/
|
||||
var EventType = cc.Enum({
|
||||
/**
|
||||
* The event type for touch begin event
|
||||
* @property TOUCH_BEGAN
|
||||
* @type {String}
|
||||
* @readonly
|
||||
*/
|
||||
TOUCH_BEGAN: 'touch_began',
|
||||
/**
|
||||
* The event type for touch move event
|
||||
* @property TOUCH_MOVED
|
||||
* @type {String}
|
||||
* @value 1
|
||||
* @readonly
|
||||
*/
|
||||
TOUCH_MOVED: 'touch_moved',
|
||||
/**
|
||||
* The event type for touch end event
|
||||
* @property TOUCH_ENDED
|
||||
* @type {String}
|
||||
* @readonly
|
||||
*/
|
||||
TOUCH_ENDED: 'touch_ended',
|
||||
|
||||
/**
|
||||
* The event type for mouse down events
|
||||
* @property MOUSE_DOWN
|
||||
* @type {String}
|
||||
* @readonly
|
||||
*/
|
||||
MOUSE_DOWN: 'mouse_down',
|
||||
/**
|
||||
* The event type for mouse move events
|
||||
* @property MOUSE_MOVED
|
||||
* @type {String}
|
||||
* @readonly
|
||||
*/
|
||||
MOUSE_MOVED: 'mouse_moved',
|
||||
/**
|
||||
* The event type for mouse up events
|
||||
* @property MOUSE_UP
|
||||
* @type {String}
|
||||
* @readonly
|
||||
*/
|
||||
MOUSE_UP: 'mouse_up',
|
||||
});
|
||||
|
||||
var _touchBeganHandler = function (touch, event) {
|
||||
var pos = touch.getLocation();
|
||||
var node = this.owner;
|
||||
|
||||
if (node._hitTest(pos)) {
|
||||
event.type = EventType.TOUCH_BEGAN;
|
||||
event.touch = touch;
|
||||
event.bubbles = true;
|
||||
node.dispatchEvent(event);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
var _touchMovedHandler = function (touch, event) {
|
||||
var node = this.owner;
|
||||
event.type = EventType.TOUCH_MOVED;
|
||||
event.touch = touch;
|
||||
event.bubbles = true;
|
||||
node.dispatchEvent(event);
|
||||
};
|
||||
var _touchEndedHandler = function (touch, event) {
|
||||
var node = this.owner;
|
||||
event.type = EventType.TOUCH_ENDED;
|
||||
event.touch = touch;
|
||||
event.bubbles = true;
|
||||
node.dispatchEvent(event);
|
||||
};
|
||||
|
||||
var _mouseDownHandler = function (event) {
|
||||
var pos = event.getLocation();
|
||||
var node = this.owner;
|
||||
|
||||
if (node._hitTest(pos)) {
|
||||
event.type = EventType.MOUSE_DOWN;
|
||||
node.dispatchEvent(event);
|
||||
event.stopPropagation();
|
||||
}
|
||||
};
|
||||
var _mouseMovedHandler = function (event) {
|
||||
var pos = event.getLocation();
|
||||
var node = this.owner;
|
||||
|
||||
if (node._hitTest(pos)) {
|
||||
event.type = EventType.MOUSE_MOVED;
|
||||
node.dispatchEvent(event);
|
||||
event.stopPropagation();
|
||||
}
|
||||
};
|
||||
var _mouseUpHandler = function (event) {
|
||||
var pos = event.getLocation();
|
||||
var node = this.owner;
|
||||
|
||||
if (node._hitTest(pos)) {
|
||||
event.type = EventType.MOUSE_UP;
|
||||
node.dispatchEvent(event);
|
||||
event.stopPropagation();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
@@ -41,6 +154,7 @@ var DontDestroy = Flags.DontDestroy;
|
||||
var Node = cc.Class({
|
||||
name: 'cc.Node',
|
||||
extends: require('./utils/base-node'),
|
||||
mixins: [EventTarget],
|
||||
|
||||
properties: {
|
||||
/**
|
||||
@@ -131,6 +245,12 @@ var Node = cc.Class({
|
||||
// cache component
|
||||
this._widget = null;
|
||||
|
||||
// Touch event listener
|
||||
this._touchListener = null;
|
||||
|
||||
// Mouse event listener
|
||||
this._mouseListener = null;
|
||||
|
||||
/**
|
||||
* Register all related EventTargets,
|
||||
* all event callbacks will be removed in _onPreDestroy
|
||||
@@ -192,6 +312,13 @@ var Node = cc.Class({
|
||||
}
|
||||
this.__eventTargets.length = 0;
|
||||
|
||||
if (this._touchListener) {
|
||||
cc.eventManager.removeListener(this._touchListener);
|
||||
}
|
||||
if (this._mouseListener) {
|
||||
cc.eventManager.removeListener(this._mouseListener);
|
||||
}
|
||||
|
||||
// remove from persist
|
||||
if (this._persistNode) {
|
||||
cc.game.removePersistRootNode(this);
|
||||
@@ -250,11 +377,11 @@ var Node = cc.Class({
|
||||
var err, existing = this.getComponent(ctor._disallowMultiple);
|
||||
if (existing) {
|
||||
if (existing.constructor === ctor) {
|
||||
err = 'Can\'t add component \'%s\' because %s already contains the same component.';
|
||||
err = 'Can\'t add component "%s" because %s already contains the same component.';
|
||||
cc.error(err, JS.getClassName(ctor), this._name);
|
||||
}
|
||||
else {
|
||||
err = 'Can\'t add component \'%s\' to %s because it conflicts with the existing \'%s\' derived component.';
|
||||
err = 'Can\'t add component "%s" to %s because it conflicts with the existing "%s" derived component.';
|
||||
cc.error(err, JS.getClassName(ctor), this._name, JS.getClassName(existing));
|
||||
}
|
||||
return false;
|
||||
@@ -589,10 +716,129 @@ var Node = cc.Class({
|
||||
comp._sgNode.setOpacityModifyRGB(this._opacityModifyRGB);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
// EVENTS
|
||||
/**
|
||||
* Register an callback of a specific event type on Node.
|
||||
* Use this method to register touch or mouse event permit propagation based on scene graph,
|
||||
* you can propagate the event to the parents or swallow it by calling stopPropagation on the event.
|
||||
* It's the recommended way to register touch/mouse event for Node,
|
||||
* please do not use cc.eventManager directly for Node.
|
||||
*
|
||||
* @method on
|
||||
* @param {String} type - A string representing the event type to listen for.
|
||||
* @param {Function} callback - The callback that will be invoked when the event is dispatched.
|
||||
* The callback is ignored if it is a duplicate (the callbacks are unique).
|
||||
* @param {Event} callback.param event
|
||||
* @param {Object} [target] - The target to invoke the callback, can be null
|
||||
* @return {Function} - Just returns the incoming callback so you can save the anonymous function easier.
|
||||
*/
|
||||
on: function (type, callback, target) {
|
||||
if (type === EventType.TOUCH_BEGAN || type === EventType.TOUCH_MOVED || type === EventType.TOUCH_ENDED) {
|
||||
if (!this._touchListener) {
|
||||
this._touchListener = cc.EventListener.create({
|
||||
event: cc.EventListener.TOUCH_ONE_BY_ONE,
|
||||
swallowTouches: true,
|
||||
owner: this,
|
||||
onTouchBegan: _touchBeganHandler,
|
||||
onTouchMoved: _touchMovedHandler,
|
||||
onTouchEnded: _touchEndedHandler
|
||||
});
|
||||
cc.eventManager.addListener(this._touchListener, this);
|
||||
}
|
||||
}
|
||||
else if (type === EventType.MOUSE_DOWN || type === EventType.MOUSE_MOVED || type === EventType.MOUSE_UP) {
|
||||
if (!this._mouseListener) {
|
||||
this._mouseListener = cc.EventListener.create({
|
||||
event: cc.EventListener.MOUSE,
|
||||
owner: this,
|
||||
onMouseDown: _mouseDownHandler,
|
||||
onMouseMove: _mouseMovedHandler,
|
||||
onMouseUp: _mouseUpHandler
|
||||
});
|
||||
cc.eventManager.addListener(this._mouseListener, this);
|
||||
}
|
||||
}
|
||||
EventTarget.prototype.on.call(this, type, callback, target);
|
||||
},
|
||||
|
||||
off: function (type, callback, target) {
|
||||
EventTarget.prototype.off.call(this, type, callback, target);
|
||||
|
||||
if (type === EventType.TOUCH_BEGAN || type === EventType.TOUCH_MOVED || type === EventType.TOUCH_ENDED) {
|
||||
this._checkTouchListeners();
|
||||
}
|
||||
else if (type === EventType.MOUSE_DOWN || type === EventType.MOUSE_MOVED || type === EventType.MOUSE_UP) {
|
||||
this._checkMouseListeners();
|
||||
}
|
||||
},
|
||||
|
||||
targetOff: function (target) {
|
||||
EventTarget.prototype.targetOff.call(this, target);
|
||||
|
||||
this._checkTouchListeners();
|
||||
this._checkMouseListeners();
|
||||
},
|
||||
|
||||
_checkTouchListeners: function () {
|
||||
if (this._bubblingListeners &&
|
||||
!this._bubblingListeners.has(EventType.TOUCH_BEGAN) &&
|
||||
!this._bubblingListeners.has(EventType.TOUCH_MOVED) &&
|
||||
!this._bubblingListeners.has(EventType.TOUCH_UP) &&
|
||||
this._touchListener)
|
||||
{
|
||||
cc.eventManager.removeListener(this._touchListener);
|
||||
this._touchListener = null;
|
||||
}
|
||||
},
|
||||
_checkMouseListeners: function () {
|
||||
if (this._bubblingListeners &&
|
||||
!this._bubblingListeners.has(EventType.MOUSE_DOWN) &&
|
||||
!this._bubblingListeners.has(EventType.MOUSE_MOVED) &&
|
||||
!this._bubblingListeners.has(EventType.MOUSE_UP) &&
|
||||
this._mouseListener)
|
||||
{
|
||||
cc.eventManager.removeListener(this._mouseListener);
|
||||
this._mouseListener = null;
|
||||
}
|
||||
},
|
||||
|
||||
_hitTest: function (point) {
|
||||
var apx = this._anchorPoint.x,
|
||||
apy = this._anchorPoint.y,
|
||||
w = this.width,
|
||||
h = this.height;
|
||||
var rect = cc.rect(-apx * w, -apy * h, w, h);
|
||||
var trans = this.getNodeToWorldTransform();
|
||||
cc._rectApplyAffineTransformIn(rect, trans);
|
||||
var left = point.x - rect.x,
|
||||
right = rect.x + rect.width - point.x,
|
||||
bottom = point.y - rect.y,
|
||||
top = rect.y + rect.height - point.y;
|
||||
if (left >= 0 && right >= 0 && top >= 0 && bottom >= 0) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
// Store all bubbling parents that are listening to the same event in the array
|
||||
_getBubblingTargets: function (type, array) {
|
||||
var parent = this.parent;
|
||||
while (parent) {
|
||||
if (parent.hasEventListener(type)) {
|
||||
array.push(parent);
|
||||
}
|
||||
parent = parent.parent;
|
||||
}
|
||||
},
|
||||
|
||||
});
|
||||
|
||||
Node.EventType = EventType;
|
||||
|
||||
/**
|
||||
* @event position-changed
|
||||
*/
|
||||
|
||||
@@ -1782,9 +1782,16 @@ _ccsg.Node = cc.Class({
|
||||
*/
|
||||
getNodeToWorldTransform: function () {
|
||||
//TODO renderCmd has a WorldTransform
|
||||
var t = this.getNodeToParentTransform();
|
||||
for (var p = this._parent; p !== null; p = p.parent)
|
||||
t = cc.affineTransformConcat(t, p.getNodeToParentTransform());
|
||||
var t;
|
||||
if (this._renderCmd && cc._renderType === cc.game.RENDER_TYPE_CANVAS) {
|
||||
t = this._renderCmd._worldTransform;
|
||||
}
|
||||
else {
|
||||
t = this.getNodeToParentTransform();
|
||||
for (var p = this._parent; p !== null; p = p.parent) {
|
||||
t = cc.affineTransformConcat(t, p.getNodeToParentTransform());
|
||||
}
|
||||
}
|
||||
return t;
|
||||
},
|
||||
|
||||
@@ -2035,7 +2042,7 @@ _ccsg.Node = cc.Class({
|
||||
getBoundingBoxToWorld: function () {
|
||||
var rect = cc.rect(0, 0, this._contentSize.width, this._contentSize.height);
|
||||
var trans = this.getNodeToWorldTransform();
|
||||
rect = cc.rectApplyAffineTransform(rect, trans);
|
||||
cc._rectApplyAffineTransformIn(rect, trans);
|
||||
|
||||
//query child's BoundingBox
|
||||
if (!this._children)
|
||||
@@ -2056,7 +2063,7 @@ _ccsg.Node = cc.Class({
|
||||
_getBoundingBoxToCurrentNode: function (parentTransform) {
|
||||
var rect = cc.rect(0, 0, this._contentSize.width, this._contentSize.height);
|
||||
var trans = (parentTransform === undefined) ? this.getNodeToParentTransform() : cc.affineTransformConcat(this.getNodeToParentTransform(), parentTransform);
|
||||
rect = cc.rectApplyAffineTransform(rect, trans);
|
||||
cc._rectApplyAffineTransformIn(rect, trans);
|
||||
|
||||
//query child's BoundingBox
|
||||
if (!this._children)
|
||||
|
||||
@@ -270,31 +270,24 @@ cc.EventListener.KEYBOARD = 3;
|
||||
* @type {Number}
|
||||
*/
|
||||
cc.EventListener.MOUSE = 4;
|
||||
/**
|
||||
* The type code of acceleration event listener.
|
||||
* @constant
|
||||
* @type {Number}
|
||||
*/
|
||||
cc.EventListener.ACCELERATION = 5;
|
||||
/**
|
||||
* The type code of focus event listener.
|
||||
* @constant
|
||||
* @type {Number}
|
||||
*/
|
||||
cc.EventListener.ACCELERATION = 6;
|
||||
/**
|
||||
* The type code of custom event listener.
|
||||
* @constant
|
||||
* @type {Number}
|
||||
*/
|
||||
cc.EventListener.CUSTOM = 8;
|
||||
|
||||
/**
|
||||
* The type code of Focus change event listener.
|
||||
* @constant
|
||||
* @type {Number}
|
||||
*/
|
||||
cc.EventListener.FOCUS = 7;
|
||||
/**
|
||||
* The type code of custom event listener.
|
||||
* @constant
|
||||
* @type {Number}
|
||||
*/
|
||||
cc.EventListener.CUSTOM = 8;
|
||||
|
||||
cc._EventListenerCustom = cc.EventListener.extend({
|
||||
_onCustomEvent: null,
|
||||
|
||||
@@ -122,7 +122,11 @@ JS.mixin(EventTarget.prototype, {
|
||||
* @param {Boolean} A value of true if a callback of the specified type is registered; false otherwise.
|
||||
*/
|
||||
hasEventListener: function (type) {
|
||||
return this._bubblingListeners.has(type) || this._capturingListeners.has(type);
|
||||
if (this._bubblingListeners && this._bubblingListeners.has(type))
|
||||
return true;
|
||||
if (this._capturingListeners && this._capturingListeners.has(type))
|
||||
return true;
|
||||
return false;
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
@@ -429,7 +429,7 @@
|
||||
if ((locTexture &&!locTexture._textureLoaded) || this._displayedOpacity === 0)
|
||||
return;
|
||||
|
||||
var gl = ctx || cc._renderContext ;
|
||||
var gl = ctx || cc._renderContext;
|
||||
//cc.assert(!_t._batchNode, "If _ccsg.Sprite is being rendered by cc.SpriteBatchNode, _ccsg.Sprite#draw SHOULD NOT be called");
|
||||
|
||||
if (locTexture) {
|
||||
|
||||
@@ -167,7 +167,9 @@ cc.game.run({
|
||||
"src/NativeTest/NativeTest.js",
|
||||
"src/NativeTest/FileUtils/FileUtilsTest.js",
|
||||
"src/NativeTest/JSBExtendTest.js",
|
||||
"src/NativeTest/AudioEngineTest.js"
|
||||
"src/NativeTest/AudioEngineTest.js",
|
||||
|
||||
"src/NodeEventTest/NodeEventTest.js"
|
||||
]
|
||||
}, function(){
|
||||
cc.view.enableRetina(false);
|
||||
|
||||
@@ -32,11 +32,11 @@ var BASE_TEST_SUBTITLE_TAG = 12;
|
||||
|
||||
|
||||
var autoTestEnabled = autoTestEnabled || false;
|
||||
var autoTestCurrentTestName = autoTestCurrentTestName || "N/A";
|
||||
var autoTestCurrentTestName = autoTestCurrentTestName || 'N/A';
|
||||
|
||||
var BaseTestLayerProps = {
|
||||
|
||||
ctor:function(colorA, colorB ) {
|
||||
ctor:function(colorA, colorB, backHandler, flowControler) {
|
||||
|
||||
cc.sys.garbageCollect();
|
||||
|
||||
@@ -46,7 +46,7 @@ var BaseTestLayerProps = {
|
||||
|
||||
if( arguments.length >= 1 )
|
||||
a = colorA;
|
||||
if( arguments.length == 2 )
|
||||
if( arguments.length >= 2 )
|
||||
b = colorB;
|
||||
|
||||
// for automation, no gradient. helps for grabbing the screen if needed
|
||||
@@ -57,15 +57,25 @@ var BaseTestLayerProps = {
|
||||
|
||||
this._super( a, b );
|
||||
|
||||
// Update winsize in case it was resized
|
||||
winSize = director.getWinSize();
|
||||
|
||||
if( autoTestEnabled ) {
|
||||
this.totalNumberOfTests = this.numberOfPendingTests();
|
||||
this.scheduleOnce( this.endTest, this.testDuration );
|
||||
|
||||
this.setupAutomation();
|
||||
}
|
||||
|
||||
if (backHandler) {
|
||||
var label = new cc.LabelTTF('Back', 'Arial', 20);
|
||||
var menuItem = new cc.MenuItemLabel(label, backHandler, this);
|
||||
var menu = new cc.Menu(menuItem);
|
||||
menu.x = 0;
|
||||
menu.y = 0;
|
||||
menuItem.x = cc.winSize.width - 50;
|
||||
menuItem.y = 25;
|
||||
this.addChild(menu, 1);
|
||||
}
|
||||
|
||||
this._flowControler = flowControler || null;
|
||||
},
|
||||
|
||||
setupAutomation:function() {
|
||||
@@ -74,7 +84,7 @@ var BaseTestLayerProps = {
|
||||
},
|
||||
|
||||
getTitle:function() {
|
||||
var t = "";
|
||||
var t = '';
|
||||
|
||||
// some tests use "this.title()" and others use "this._title";
|
||||
if( 'title' in this )
|
||||
@@ -84,7 +94,7 @@ var BaseTestLayerProps = {
|
||||
return t;
|
||||
},
|
||||
getSubtitle:function() {
|
||||
var st = "";
|
||||
var st = '';
|
||||
// some tests use "this.subtitle()" and others use "this._subtitle";
|
||||
if(this.subtitle)
|
||||
st = this.subtitle();
|
||||
@@ -104,17 +114,17 @@ var BaseTestLayerProps = {
|
||||
this._super();
|
||||
|
||||
var t = this.getTitle();
|
||||
var label = new cc.LabelTTF(t, "Arial", 28);
|
||||
var label = new cc.LabelTTF(t, 'Arial', 28);
|
||||
this.addChild(label, 100, BASE_TEST_TITLE_TAG);
|
||||
label.x = winSize.width / 2;
|
||||
label.y = winSize.height - 50;
|
||||
label.x = cc.winSize.width / 2;
|
||||
label.y = cc.winSize.height - 50;
|
||||
|
||||
var st = this.getSubtitle();
|
||||
if (st) {
|
||||
var l = new cc.LabelTTF(st.toString(), "Thonburi", 16);
|
||||
var l = new cc.LabelTTF(st.toString(), 'Thonburi', 16);
|
||||
this.addChild(l, 101, BASE_TEST_SUBTITLE_TAG);
|
||||
l.x = winSize.width / 2;
|
||||
l.y = winSize.height - 80;
|
||||
l.x = cc.winSize.width / 2;
|
||||
l.y = cc.winSize.height - 80;
|
||||
}
|
||||
|
||||
var item1 = new cc.MenuItemImage(s_pathB1, s_pathB2, this.onBackCallback, this);
|
||||
@@ -130,23 +140,35 @@ var BaseTestLayerProps = {
|
||||
menu.x = 0;
|
||||
menu.y = 0;
|
||||
var width = item2.width, height = item2.height;
|
||||
item1.x = winSize.width/2 - width*2;
|
||||
item1.x = cc.winSize.width/2 - width*2;
|
||||
item1.y = height/2 ;
|
||||
item2.x = winSize.width/2;
|
||||
item2.x = cc.winSize.width/2;
|
||||
item2.y = height/2 ;
|
||||
item3.x = winSize.width/2 + width*2;
|
||||
item3.x = cc.winSize.width/2 + width*2;
|
||||
item3.y = height/2 ;
|
||||
|
||||
this.addChild(menu, 102, BASE_TEST_MENU_TAG);
|
||||
},
|
||||
onRestartCallback:function (sender) {
|
||||
// override me
|
||||
if (this._flowControler) {
|
||||
var s = new _ccsg.Scene();
|
||||
s.addChild(this._flowControler.current());
|
||||
cc.director.runScene(s);
|
||||
}
|
||||
},
|
||||
onNextCallback:function (sender) {
|
||||
// override me
|
||||
if (this._flowControler) {
|
||||
var s = new _ccsg.Scene();
|
||||
s.addChild(this._flowControler.next());
|
||||
cc.director.runScene(s);
|
||||
}
|
||||
},
|
||||
onBackCallback:function (sender) {
|
||||
// override me
|
||||
if (this._flowControler) {
|
||||
var s = new _ccsg.Scene();
|
||||
s.addChild(this._flowControler.previous());
|
||||
cc.director.runScene(s);
|
||||
}
|
||||
},
|
||||
//------------------------------------------
|
||||
//
|
||||
@@ -160,17 +182,17 @@ var BaseTestLayerProps = {
|
||||
// Automated test
|
||||
getExpectedResult:function() {
|
||||
// Override me
|
||||
throw "Not Implemented";
|
||||
throw 'Not Implemented';
|
||||
},
|
||||
|
||||
// Automated test
|
||||
getCurrentResult:function() {
|
||||
// Override me
|
||||
throw "Not Implemented";
|
||||
throw 'Not Implemented';
|
||||
},
|
||||
|
||||
compareResults:function(current, expected) {
|
||||
return (current == expected);
|
||||
return (current === expected);
|
||||
},
|
||||
|
||||
tearDown:function(dt) {
|
||||
@@ -181,38 +203,44 @@ var BaseTestLayerProps = {
|
||||
|
||||
var ret = this.compareResults(current, expected);
|
||||
if( ! ret )
|
||||
this.errorDescription = "Expected value: '" + expected + "'. Current value'" + current + "'.";
|
||||
this.errorDescription = 'Expected value: \'' + expected + '\'. Current value\'' + current + '\'.';
|
||||
|
||||
return ret;
|
||||
},
|
||||
|
||||
endTest:function(dt) {
|
||||
|
||||
this.errorDescription = "";
|
||||
this.errorDescription = '';
|
||||
var title = this.getTitle();
|
||||
|
||||
try {
|
||||
if( this.tearDown(dt) ) {
|
||||
// Test OK
|
||||
cc.log( autoTestCurrentTestName + " - " + this.getTestNumber() + ": Test '" + title + "':' OK");
|
||||
cc.log( autoTestCurrentTestName + ' - ' + this.getTestNumber() + ': Test \'' + title + '\':\' OK');
|
||||
} else {
|
||||
// Test failed
|
||||
cc.log( autoTestCurrentTestName + " - " +this.getTestNumber() + ": Test '" + title + "': Error: " + this.errorDescription );
|
||||
cc.log( autoTestCurrentTestName + ' - ' +this.getTestNumber() + ': Test \'' + title + '\': Error: ' + this.errorDescription );
|
||||
}
|
||||
} catch(err) {
|
||||
cc.log( autoTestCurrentTestName + " - " +this.getTestNumber() + ": Test '" + title + "':'" + err);
|
||||
cc.log( autoTestCurrentTestName + ' - ' +this.getTestNumber() + ': Test \'' + title + '\':\'' + err);
|
||||
}
|
||||
|
||||
this.runNextTest();
|
||||
},
|
||||
|
||||
// automation
|
||||
numberOfPendingTests:function() {
|
||||
// override me. Should return true if the last test was executed
|
||||
throw "Override me: numberOfPendingTests";
|
||||
if (this._flowControler)
|
||||
return ( (this._flowControler.length-1) - this._flowControler.getId() );
|
||||
else
|
||||
throw 'Override me: numberOfPendingTests';
|
||||
},
|
||||
|
||||
getTestNumber:function() {
|
||||
throw "Override me: getTestNumber";
|
||||
if (this._flowControler)
|
||||
return this._flowControler.getId();
|
||||
else
|
||||
throw 'Override me: getTestNumber';
|
||||
|
||||
},
|
||||
|
||||
runNextTest:function() {
|
||||
@@ -220,21 +248,20 @@ var BaseTestLayerProps = {
|
||||
var scene = new _ccsg.Scene();
|
||||
var layer = new TestController();
|
||||
scene.addChild(layer);
|
||||
director.runScene(scene);
|
||||
cc.director.runScene(scene);
|
||||
} else
|
||||
try {
|
||||
this.onNextCallback(this);
|
||||
} catch (err) {
|
||||
cc.log( autoTestCurrentTestName + " - " +this.getTestNumber() + ": Test '" + this.getTitle() + "':'" + err);
|
||||
cc.log( autoTestCurrentTestName + ' - ' +this.getTestNumber() + ': Test \'' + this.getTitle() + '\':\'' + err);
|
||||
this.runNextTest();
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
containsPixel: function(arr, pix, approx, range) {
|
||||
|
||||
range = range || 50.0;
|
||||
approx = approx || false;
|
||||
range = range || 50.0;
|
||||
approx = approx || false;
|
||||
|
||||
var abs = function(a,b) {
|
||||
return ((a-b) > 0) ? (a-b) : (b-a);
|
||||
@@ -242,7 +269,7 @@ var BaseTestLayerProps = {
|
||||
|
||||
var pixelEqual = function(pix1, pix2) {
|
||||
if(approx && abs(pix1, pix2) < range) return true;
|
||||
else if(!approx && pix1 == pix2) return true;
|
||||
else if(!approx && pix1 === pix2) return true;
|
||||
return false;
|
||||
};
|
||||
|
||||
@@ -257,14 +284,14 @@ var BaseTestLayerProps = {
|
||||
},
|
||||
|
||||
readPixels:function(x,y,w,h) {
|
||||
if( 'opengl' in cc.sys.capabilities) {
|
||||
if ( 'opengl' in cc.sys.capabilities) {
|
||||
var size = 4 * w * h;
|
||||
var array = new Uint8Array(size);
|
||||
gl.readPixels(x, y, w, h, gl.RGBA, gl.UNSIGNED_BYTE, array);
|
||||
return array;
|
||||
} else {
|
||||
// implement a canvas-html5 readpixels
|
||||
return cc._renderContext.getImageData(x, winSize.height-y-h, w, h).data;
|
||||
return cc._renderContext.getImageData(x, cc.winSize.height-y-h, w, h).data;
|
||||
}
|
||||
},
|
||||
|
||||
@@ -293,7 +320,33 @@ var BaseTestLayerProps = {
|
||||
|
||||
var BaseTestLayer = cc.LayerGradient.extend(BaseTestLayerProps);
|
||||
|
||||
var FlowControl = function (testArray) {
|
||||
cc.js.mixin(BaseTestLayerProps, {
|
||||
onRestartCallback:function () {
|
||||
if (this._flowControler) {
|
||||
var s = new cc.Scene();
|
||||
s._sgNode.addChild(this._flowControler.current());
|
||||
cc.director.runScene(s);
|
||||
}
|
||||
},
|
||||
onNextCallback:function () {
|
||||
if (this._flowControler) {
|
||||
var s = new cc.Scene();
|
||||
s._sgNode.addChild(this._flowControler.next());
|
||||
cc.director.runScene(s);
|
||||
}
|
||||
},
|
||||
onBackCallback:function () {
|
||||
if (this._flowControler) {
|
||||
var s = new cc.Scene();
|
||||
s._sgNode.addChild(this._flowControler.previous());
|
||||
cc.director.runScene(s);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
var ECSTestLayer = cc.LayerGradient.extend(BaseTestLayerProps);
|
||||
|
||||
var FlowControl = function (testArray, forECS) {
|
||||
|
||||
var sceneIdx = 0;
|
||||
|
||||
@@ -320,9 +373,16 @@ var FlowControl = function (testArray) {
|
||||
},
|
||||
start: function() {
|
||||
sceneIdx = 0;
|
||||
var s = new _ccsg.Scene();
|
||||
s.addChild(this.current());
|
||||
cc.director.runScene(s);
|
||||
if (forECS) {
|
||||
var s = new cc.Scene();
|
||||
s._sgNode.addChild(this.current());
|
||||
cc.director.runScene(s);
|
||||
}
|
||||
else {
|
||||
var s = new _ccsg.Scene();
|
||||
s.addChild(this.current());
|
||||
cc.director.runScene(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,238 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2015 Chukong Technologies Inc.
|
||||
|
||||
http://www.cocos2d-x.org
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
'use strict';
|
||||
|
||||
(function (window, BaseTestLayer) {
|
||||
|
||||
var backHandler = function () {
|
||||
var scene = new _ccsg.Scene();
|
||||
var layer = new TestController();
|
||||
scene.addChild(layer);
|
||||
cc.director.runScene(scene);
|
||||
};
|
||||
|
||||
var createEntity = function (file, x, y) {
|
||||
var node = new cc.Node(file);
|
||||
if (file) {
|
||||
var tex = cc.textureCache.getTextureForKey(file);
|
||||
var spriteFrame = new cc.SpriteFrame(tex, cc.rect(0, 0, tex.getPixelWidth(), tex.getPixelHeight()));
|
||||
var spriteRenderer = node.addComponent(cc.Sprite);
|
||||
spriteRenderer.spriteFrame = spriteFrame;
|
||||
spriteRenderer._createSgNode();
|
||||
}
|
||||
node.x = x;
|
||||
node.y = y;
|
||||
return node;
|
||||
};
|
||||
|
||||
var TouchEventTest = ECSTestLayer.extend({
|
||||
_title: 'Node Event Test',
|
||||
_subtitle: 'Grossini can be touched',
|
||||
|
||||
ctor:function() {
|
||||
this._super(cc.color(0,0,0,255), cc.color(98,99,117,255), backHandler, NodeEventTestFlow);
|
||||
|
||||
this._label = new cc.LabelTTF('', 'Arial', 20);
|
||||
this._label.x = cc.winSize.width/2;
|
||||
this._label.y = 150;
|
||||
this.addChild(this._label);
|
||||
|
||||
this._node = null;
|
||||
},
|
||||
|
||||
onEnter: function () {
|
||||
this._super();
|
||||
|
||||
cc.eventManager.addListener({
|
||||
event: cc.EventListener.TOUCH_ONE_BY_ONE,
|
||||
owner: this,
|
||||
onTouchBegan: function () {
|
||||
var layer = this.owner;
|
||||
layer._label.string = 'Touch outside';
|
||||
return true;
|
||||
}
|
||||
}, this);
|
||||
|
||||
this._node = createEntity(s_pathGrossini, cc.winSize.width/2, cc.winSize.height/2);
|
||||
cc.director.getScene().addChild(this._node);
|
||||
|
||||
this._node.on(cc.Node.EventType.TOUCH_BEGAN, function () {
|
||||
this._label.string = 'Touch began - Grossini';
|
||||
}, this);
|
||||
this._node.on(cc.Node.EventType.TOUCH_MOVED, function () {
|
||||
this._label.string = 'Touch moved - Grossini';
|
||||
}, this);
|
||||
this._node.on(cc.Node.EventType.TOUCH_ENDED, function () {
|
||||
this._label.string = 'Touch ended - Grossini';
|
||||
}, this);
|
||||
}
|
||||
});
|
||||
|
||||
var MouseEventTest = ECSTestLayer.extend({
|
||||
_title: 'Node Event Test',
|
||||
_subtitle: 'Grossini can interact with mouse',
|
||||
|
||||
ctor: function() {
|
||||
this._super(cc.color(0,0,0,255), cc.color(98,99,117,255), backHandler, NodeEventTestFlow);
|
||||
this._label = new cc.LabelTTF('', 'Arial', 20);
|
||||
this._label.x = cc.winSize.width/2;
|
||||
this._label.y = 150;
|
||||
this.addChild(this._label);
|
||||
},
|
||||
onEnter: function () {
|
||||
this._super();
|
||||
|
||||
cc.eventManager.addListener({
|
||||
event: cc.EventListener.MOUSE,
|
||||
owner: this,
|
||||
onMouseDown: function () {
|
||||
var layer = this.owner;
|
||||
layer._label.string = 'Mouse down - outside';
|
||||
},
|
||||
onMouseMove: function () {
|
||||
var layer = this.owner;
|
||||
layer._label.string = 'Mouse moved - outside';
|
||||
},
|
||||
onMouseUp: function () {
|
||||
var layer = this.owner;
|
||||
layer._label.string = 'Mouse up - outside';
|
||||
}
|
||||
}, this);
|
||||
|
||||
var node = createEntity(s_pathGrossini, cc.winSize.width/2, cc.winSize.height/2);
|
||||
cc.director.getScene().addChild(node);
|
||||
|
||||
node.on(cc.Node.EventType.MOUSE_DOWN, function () {
|
||||
this._label.string = 'Mouse down - Grossini';
|
||||
}, this);
|
||||
node.on(cc.Node.EventType.MOUSE_MOVED, function () {
|
||||
this._label.string = 'Mouse moved - Grossini';
|
||||
}, this);
|
||||
node.on(cc.Node.EventType.MOUSE_UP, function () {
|
||||
this._label.string = 'Mouse up - Grossini';
|
||||
}, this);
|
||||
}
|
||||
});
|
||||
|
||||
var PropagationTest = ECSTestLayer.extend({
|
||||
_title: 'Node Event Test',
|
||||
_subtitle: 'Enable or disable propagation to see difference',
|
||||
|
||||
ctor: function () {
|
||||
this._super(cc.color(0,0,0,255), cc.color(98,99,117,255), backHandler, NodeEventTestFlow);
|
||||
this.propagate = true;
|
||||
},
|
||||
onEnter: function () {
|
||||
this._super();
|
||||
|
||||
var node = createEntity('Images/CyanSquare.png', cc.winSize.width/2, cc.winSize.height/2);
|
||||
cc.director.getScene().addChild(node);
|
||||
|
||||
var self = this;
|
||||
node.on(cc.Node.EventType.TOUCH_BEGAN, function (event) {
|
||||
this.opacity = 180;
|
||||
if (self.propagate)
|
||||
event.stopPropagation();
|
||||
}, node);
|
||||
node.on(cc.Node.EventType.TOUCH_MOVED, function (event) {
|
||||
var delta = event.touch.getDelta();
|
||||
this.x += delta.x;
|
||||
this.y += delta.y;
|
||||
if (self.propagate)
|
||||
event.stopPropagation();
|
||||
}, node);
|
||||
node.on(cc.Node.EventType.TOUCH_ENDED, function (event) {
|
||||
this.opacity = 255;
|
||||
if (self.propagate)
|
||||
event.stopPropagation();
|
||||
}, node);
|
||||
|
||||
var magenta = createEntity('Images/MagentaSquare.png', 50, -50);
|
||||
node.addChild(magenta);
|
||||
|
||||
magenta.on(cc.Node.EventType.TOUCH_BEGAN, function (event) {
|
||||
this.opacity = 180;
|
||||
if (self.propagate)
|
||||
event.stopPropagation();
|
||||
}, magenta);
|
||||
magenta.on(cc.Node.EventType.TOUCH_MOVED, function (event) {
|
||||
var delta = event.touch.getDelta();
|
||||
this.x += delta.x;
|
||||
this.y += delta.y;
|
||||
if (self.propagate)
|
||||
event.stopPropagation();
|
||||
}, magenta);
|
||||
magenta.on(cc.Node.EventType.TOUCH_ENDED, function (event) {
|
||||
this.opacity = 255;
|
||||
if (self.propagate)
|
||||
event.stopPropagation();
|
||||
}, magenta);
|
||||
|
||||
var yellow = createEntity('Images/YellowSquare.png', 50, 80);
|
||||
magenta.addChild(yellow);
|
||||
|
||||
yellow.on(cc.Node.EventType.TOUCH_BEGAN, function (event) {
|
||||
this.opacity = 180;
|
||||
if (self.propagate)
|
||||
event.stopPropagation();
|
||||
}, yellow);
|
||||
yellow.on(cc.Node.EventType.TOUCH_MOVED, function (event) {
|
||||
var delta = event.touch.getDelta();
|
||||
this.x += delta.x;
|
||||
this.y += delta.y;
|
||||
if (self.propagate)
|
||||
event.stopPropagation();
|
||||
}, yellow);
|
||||
yellow.on(cc.Node.EventType.TOUCH_ENDED, function (event) {
|
||||
this.opacity = 255;
|
||||
if (self.propagate)
|
||||
event.stopPropagation();
|
||||
}, yellow);
|
||||
|
||||
var propagateItem = new cc.MenuItemFont('Propagation OFF', function() {
|
||||
self.propagate = !self.propagate;
|
||||
propagateItem.string = self.propagate ? 'Propagation OFF' : 'Propagation ON';
|
||||
});
|
||||
|
||||
propagateItem.fontSize = 16;
|
||||
propagateItem.x = cc.visibleRect.right.x - propagateItem.width/2-20;
|
||||
propagateItem.y = cc.visibleRect.right.y;
|
||||
|
||||
var menu = new cc.Menu(propagateItem);
|
||||
menu.setPosition(0, 0);
|
||||
menu.setAnchorPoint(0, 0);
|
||||
this.addChild(menu);
|
||||
}
|
||||
});
|
||||
|
||||
var NodeEventTestFlow = new FlowControl([
|
||||
TouchEventTest,
|
||||
MouseEventTest,
|
||||
PropagationTest,
|
||||
], true);
|
||||
|
||||
window.NodeEventTestFlow = NodeEventTestFlow;
|
||||
|
||||
})(window, BaseTestLayer);
|
||||
@@ -299,6 +299,15 @@ var testNames = [
|
||||
return new EventDispatcherTestScene();
|
||||
}
|
||||
},
|
||||
{
|
||||
title:"Event Node Test",
|
||||
resource:g_eventDispatcher,
|
||||
platforms: PLATFORM_ALL,
|
||||
linksrc:"src/NodeEventTest/NodeEventTest.js",
|
||||
testScene:function () {
|
||||
NodeEventTestFlow.start();
|
||||
}
|
||||
},
|
||||
{
|
||||
title:"Extensions Test",
|
||||
resource:g_extensions,
|
||||
|
||||
Reference in New Issue
Block a user