From d6f98bb5cadaef50f5870ee5e68c03dd1217132c Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Mon, 27 Jun 2016 11:19:40 +0800 Subject: [PATCH 01/19] remove DOM node --- cocos2d/core/editbox/CCSGEditBox.js | 68 +-- cocos2d/core/editbox/CCdomNode.js | 659 ---------------------------- 2 files changed, 8 insertions(+), 719 deletions(-) delete mode 100644 cocos2d/core/editbox/CCdomNode.js diff --git a/cocos2d/core/editbox/CCSGEditBox.js b/cocos2d/core/editbox/CCSGEditBox.js index 21fba5b8e7..643f427111 100644 --- a/cocos2d/core/editbox/CCSGEditBox.js +++ b/cocos2d/core/editbox/CCSGEditBox.js @@ -1,9 +1,9 @@ /*global _ccsg */ /**************************************************************************** - Copyright (c) 2011-2012 cocos2d-x.org - Copyright (c) 2013-2014 Chukong Technologies Inc. + Copyright (c) 2013-2016 Chukong Technologies Inc. Copyright (c) 2012 James Chen + Copyright (c) 2011-2012 cocos2d-x.org http://www.cocos2d-x.org @@ -196,7 +196,6 @@ cc.EditBoxDelegate = cc._Class.extend({ }); var EditBoxImpl = function(editBox) { - this._domInputSprite = null; this._spriteDOM = null; this._edTxt = null; this._edFontSize = 14; @@ -210,25 +209,16 @@ EditBoxImpl.prototype = { constructor: EditBoxImpl, createNativeControl: function(size) { - var tmpDOMSprite = this._domInputSprite = new _ccsg.Sprite(); - tmpDOMSprite.draw = function () {}; //redefine draw function - this._editBox.addChild(tmpDOMSprite); - this._spriteDOM = tmpDOMSprite; - this._createDomTextArea(); - - cc.DOM.convert(tmpDOMSprite); - this._addDomInputControl(); - this._domInputSprite.dom.showTooltipDiv = false; - this._domInputSprite.dom.style.width = (size.width - 6) + 'px'; - this._domInputSprite.dom.style.height = (size.height - 6) + 'px'; + // this._domInputSprite.dom.showTooltipDiv = false; + // this._domInputSprite.dom.style.width = (size.width - 6) + 'px'; + // this._domInputSprite.dom.style.height = (size.height - 6) + 'px'; - tmpDOMSprite.canvas.remove(); - this._domInputSprite.x = 3; - this._domInputSprite.y = 3; + // this._domInputSprite.x = 3; + // this._domInputSprite.y = 3; this._createLabels(); }, @@ -618,15 +608,7 @@ EditBoxImpl.prototype = { }); return tmpEdTxt; - }, - - _removeDomInputControl: function() { - this._domInputSprite.dom.removeChild(this._edTxt); - }, - - _addDomInputControl: function () { - this._domInputSprite.dom.appendChild(this._edTxt); - }, + } }; /** @@ -661,7 +643,6 @@ _ccsg.EditBox = _ccsg.Node.extend({ _editBoxInputFlag: InputFlag.SENSITIVE, _keyboardReturnType: KeyboardReturnType.DEFAULT, _maxLength: 50, - _nativeControl: null, _text: '', _textColor: null, @@ -671,8 +652,6 @@ _ccsg.EditBox = _ccsg.Node.extend({ _placeholderFontSize: 14, _placeholderColor: null, - _adjustHeight: 18, - _tooltip: false, _className: 'EditBox', /** @@ -689,9 +668,6 @@ _ccsg.EditBox = _ccsg.Node.extend({ this._placeholderColor = cc.Color.GRAY; _ccsg.Node.prototype.setContentSize.call(this, size); - var editBoxImpl = this._nativeControl = new EditBoxImpl(this); - editBoxImpl.createNativeControl(size); - //because cc.DOM.convert will replace editbox's setContentSize method. //here is a hack to provide a better version of setContentSize. this._oldSetContentSize = this.setContentSize; @@ -976,22 +952,6 @@ _ccsg.EditBox = _ccsg.Node.extend({ this._keyboardReturnType = returnType; }, - keyboardWillShow: function (info) { - var rectTracked = _ccsg.EditBox.getRect(this); - // some adjustment for margin between the keyboard and the edit box. - rectTracked.y -= 4; - // if the keyboard area doesn't intersect with the tracking node area, nothing needs to be done. - if (!rectTracked.intersectsRect(info.end)) { - cc.log("needn't to adjust view layout."); - return; - } - - // assume keyboard at the bottom of screen, calculate the vertical adjustment. - this._adjustHeight = info.end.getMaxY() - rectTracked.getMinY(); - - //callback - }, - /** * @warning HTML5 Only * @param {cc.Size} size @@ -1057,18 +1017,6 @@ cc.defineGetterSetter(_p, 'returnType', null, _p.setReturnType); _p = null; -/** - * get the rect of a node in world coordinate frame - * @function - * @param {_ccsg.Node} node - * @return {cc.Rect} - */ -_ccsg.EditBox.getRect = function (node) { - var contentSize = node.getContentSize(); - var rect = cc.rect(0, 0, contentSize.width, contentSize.height); - return cc.rectApplyAffineTransform(rect, node.getNodeToWorldTransform()); -}; - _ccsg.EditBox.InputMode = InputMode; _ccsg.EditBox.InputFlag = InputFlag; _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; diff --git a/cocos2d/core/editbox/CCdomNode.js b/cocos2d/core/editbox/CCdomNode.js deleted file mode 100644 index a9e4ae6f12..0000000000 --- a/cocos2d/core/editbox/CCdomNode.js +++ /dev/null @@ -1,659 +0,0 @@ -/**************************************************************************** - Copyright (c) 2011-2012 cocos2d-x.org - Copyright (c) 2013-2014 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. - ****************************************************************************/ -/** - * the DOM object - * @namespace - * @name cc.DOM - */ -cc.DOM = {}; - -/** - * @function - * @private - * @param node - */ -cc.DOM._addMethods = function (node) { - for (var funcs in cc.DOM.methods) { - node[funcs] = cc.DOM.methods[funcs]; - } - - // Redefine getter setter - cc.defineGetterSetter(node, "x", node.getPositionX, node.setPositionX); - cc.defineGetterSetter(node, "y", node.getPositionY, node.setPositionY); - cc.defineGetterSetter(node, "width", node._getWidth, node._setWidth); - cc.defineGetterSetter(node, "height", node._getHeight, node._setHeight); - cc.defineGetterSetter(node, "anchorX", node._getAnchorX, node._setAnchorX); - cc.defineGetterSetter(node, "anchorY", node._getAnchorY, node._setAnchorY); - cc.defineGetterSetter(node, "scale", node.getScale, node.setScale); - cc.defineGetterSetter(node, "scaleX", node.getScaleX, node.setScaleX); - cc.defineGetterSetter(node, "scaleY", node.getScaleY, node.setScaleY); - cc.defineGetterSetter(node, "rotation", node.getRotation, node.setRotation); - cc.defineGetterSetter(node, "skewX", node.getSkewX, node.setSkewX); - cc.defineGetterSetter(node, "skewY", node.getSkewY, node.setSkewY); - cc.defineGetterSetter(node, "visible", node.isVisible, node.setVisible); - cc.defineGetterSetter(node, "parent", node.getParent, node.setParent); - cc.defineGetterSetter(node, "opacity", node.getOpacity, node.setOpacity); -}; -cc.DOM.methods = /** @lends cc.DOM# */{ - /** - * Replace the set position of ccNode - * @param {cc.Vec2|Number} x - * @param {Number} y - */ - setPosition:function (x, y) { - if (y === undefined) { - this._position.x = x.x; - this._position.y = x.y; - } else { - this._position.x = x; - this._position.y = y; - } - this.setNodeDirty(); - this.dom.translates(this._position.x, -this._position.y); - }, - /** - * replace set Position Y of ccNode - * @param {Number} y - */ - setPositionY:function (y) { - this._position.y = y; - this.setNodeDirty(); - this.dom.translates(this._position.x, -this._position.y); - }, - - /** - * replace set Position X of ccNode - * @param {Number} x - */ - setPositionX:function (x) { - this._position.x = x; - this.setNodeDirty(); - this.dom.translates(this._position.x, -this._position.y); - }, - - /** - * replace set Scale of ccNode - * @param {object|Number} scale - * @param {Number} scaleY - */ - setScale:function (scale, scaleY) { - //save dirty region when before change - //this._addDirtyRegionToDirector(this.getBoundingBoxToWorld()); - - this._scaleX = scale; - this._scaleY = scaleY || scale; - - //save dirty region when after changed - //this._addDirtyRegionToDirector(this.getBoundingBoxToWorld()); - this.setNodeDirty(); - this.dom.resize(this._scaleX, this._scaleY); - }, - - /** - * replace set Scale X of ccNode - * @param {Number} x - */ - setScaleX:function (x) { - this._scaleX = x; - this.setNodeDirty(); - this.dom.resize(this._scaleX, this._scaleY); - }, - - /** - * replace set Scale Y of ccNode - * @param {Number} y - */ - setScaleY:function (y) { - this._scaleY = y; - this.setNodeDirty(); - this.dom.resize(this._scaleX, this._scaleY); - }, - - /** - * replace set anchorpoint of ccNode - * @param {cc.Vec2|Number} point The anchor point of node or The anchor point.x of node. - * @param {Number} [y] The anchor point.y of node. - */ - setAnchorPoint:function (point, y) { - var cmd = this._renderCmd; - - var locAnchorPoint = this._anchorPoint; - if (y === undefined) { - locAnchorPoint.x = point.x; - locAnchorPoint.y = point.y; - } else { - locAnchorPoint.x = point; - locAnchorPoint.y = y; - } - var locAPP = cmd._anchorPointInPoints, locSize = this._contentSize; - locAPP.x = locSize.width * locAnchorPoint.x; - locAPP.y = locSize.height * locAnchorPoint.y; - - this.dom.style[cc.$.pfx + 'TransformOrigin'] = '' + locAPP.x + 'px ' + -locAPP.y + 'px'; - if (this.ignoreAnchor) { - this.dom.style.marginLeft = 0; - this.dom.style.marginBottom = 0; - } else { - this.dom.style.marginLeft = (this.isToggler) ? 0 : -locAPP.x + 'px'; - this.dom.style.marginBottom = -locAPP.y + 'px'; - } - this.setNodeDirty(); - }, - - /** - * replace set anchorpoint x of ccNode - * @param {Number} x The anchor x of node. - */ - _setAnchorX:function (x) { - var locAnchorPoint = this._anchorPoint; - var cmd = this._renderCmd; - - if (x === locAnchorPoint.x) - return; - locAnchorPoint.x = x; - - var locAPP = cmd._anchorPointInPoints, locSize = this._contentSize; - locAPP.x = locSize.width * locAnchorPoint.x; - - this.dom.style[cc.$.pfx + 'TransformOrigin'] = '' + locAPP.x + 'px ' + -locAPP.y + 'px'; - if (this.ignoreAnchor) { - this.dom.style.marginLeft = 0; - this.dom.style.marginBottom = 0; - } else { - this.dom.style.marginLeft = (this.isToggler) ? 0 : -locAPP.x + 'px'; - } - this.setNodeDirty(); - }, - - /** - * replace set anchorpoint y of ccNode - * @param {Number} y The anchor y of node. - */ - _setAnchorY:function (y) { - var locAnchorPoint = this._anchorPoint; - var cmd = this._renderCmd; - - if (y === locAnchorPoint.y) - return; - locAnchorPoint.y = y; - - var locAPP = cmd._anchorPointInPoints, locSize = this._contentSize; - locAPP.y = locSize.height * locAnchorPoint.y; - - this.dom.style[cc.$.pfx + 'TransformOrigin'] = '' + locAPP.x + 'px ' + -locAPP.y + 'px'; - if (this.ignoreAnchor) { - this.dom.style.marginLeft = 0; - this.dom.style.marginBottom = 0; - } else { - this.dom.style.marginBottom = -locAPP.y + 'px'; - } - this.setNodeDirty(); - }, - - /** - * replace set ContentSize of ccNode - * @param {cc.Size|Number} size The untransformed size of the node or The untransformed size's width of the node. - * @param {Number} [height] The untransformed size's height of the node. - */ - setContentSize:function (size, height) { - var cmd = this._renderCmd; - - var locContentSize = this._contentSize; - if (height === undefined) { - locContentSize.width = size.width; - locContentSize.height = size.height; - } else { - locContentSize.width = size; - locContentSize.height = height; - } - var locAPP = cmd._anchorPointInPoints, locAnchorPoint = this._anchorPoint; - locAPP.x = locContentSize.width * locAnchorPoint.x; - locAPP.y = locContentSize.height * locAnchorPoint.y; - this.dom.width = locContentSize.width; - this.dom.height = locContentSize.height; - this.setAnchorPoint(this.getAnchorPoint()); - if (this.canvas) { - this.canvas.width = locContentSize.width; - this.canvas.height = locContentSize.height; - } - this.setNodeDirty(); - // this.redraw(); - }, - - /** - * replace set width of ccNode - * @param {Number} width The untransformed size's width of the node. - */ - _setWidth:function (width) { - var locContentSize = this._contentSize; - var cmd = this._renderCmd; - if (width === locContentSize.width) - return; - locContentSize.width = width; - - var locAPP = cmd._anchorPointInPoints, locAnchorPoint = this._anchorPoint; - locAPP.x = locContentSize.width * locAnchorPoint.x; - this.dom.width = locContentSize.width; - this.anchorX = locAnchorPoint.x; - if (this.canvas) { - this.canvas.width = locContentSize.width; - } - this.setNodeDirty(); - // this.redraw(); - }, - - /** - * replace set height of ccNode - * @param {Number} height The untransformed size's height of the node. - */ - _setHeight:function (height) { - var locContentSize = this._contentSize; - var cmd = this._renderCmd; - if (height === locContentSize.height) - return; - locContentSize.height = height; - - var locAPP = cmd._anchorPointInPoints, locAnchorPoint = this._anchorPoint; - locAPP.y = locContentSize.height * locAnchorPoint.y; - this.dom.height = locContentSize.height; - this.anchorY = locAnchorPoint.y; - if (this.canvas) { - this.canvas.height = locContentSize.height; - } - this.setNodeDirty(); - // this.redraw(); - }, - - /** - * replace set Rotation of ccNode - * @param {Number} newRotation - */ - setRotation:function (newRotation) { - if (this._rotation === newRotation) - return; - - this._rotationX = this._rotationY = newRotation; - this.setNodeDirty(); - this.dom.rotate(newRotation); - }, - - /** - * replace set SkewX of ccNode - * @param {Number} x - */ - setSkewX:function (x) { - this._skewX = x; - this.setNodeDirty(); - this.dom.setSkew(this._skewX, this._skewY); - }, - - /** - * replace set SkewY of ccNode - * @param {Number} y - */ - setSkewY:function (y) { - this._skewY = y; - this.setNodeDirty(); - this.dom.setSkew(this._skewX, this._skewY); - }, - - /** - * replace set Visible of ccNode - * @param {Boolean} x - */ - setVisible:function (x) { - _ccsg.Node.prototype.setVisible.call(this, x); - this._visible = x; - this.setNodeDirty(); - if (this.dom) - this.dom.style.display = (x) ? 'block' : 'none'; - }, - _setLocalZOrder:function (z) { - this._localZOrder = z - this.setNodeDirty(); - if (this.dom) - this.dom.zIndex = z; - }, - - /** - * replace set Parent of ccNode - * @param {_ccsg.Node} p - */ - setParent:function (p) { - this._parent = p; - - if (p !== null) { - p.setAnchorPoint(p.getAnchorPoint()); - this.setNodeDirty(); - cc.DOM.parentDOM(this); - } - }, - - /** - * replace resume Schedule and actions of ccNode - */ - resume:function () { - this.getScheduler().resumeTarget(this); - this.getActionManager().resumeTarget(this); - cc.eventManager.resumeTarget(this); - //if dom does not have parent, but node has no parent and its running - if (this.dom && !this.dom.parentNode) { - if (!this.getParent()) { - if(this.dom.id === ""){ - cc.DOM._createEGLViewDiv(this); - }else{ - this.dom.appendTo(cc.container); - } - } else { - cc.DOM.parentDOM(this); - } - } - if (this.dom) - this.dom.style.visibility = "visible"; - }, - - /** - * replace pause Schedule and Actions of ccNode - */ - pause:function () { - this.getScheduler().pauseTarget(this); - this.getActionManager().pauseTarget(this); - cc.eventManager.pauseTarget(this); - if (this.dom) { - this.dom.style.visibility = 'hidden'; - } - }, - - /** - * replace clean up of ccNode - */ - cleanup:function () { - // actions - this.stopAllActions(); - this.unscheduleAllCallbacks(); - - // timers - this._arrayMakeObjectsPerformSelector(this._children, _ccsg.Node._stateCallbackType.cleanup); - if (this.dom) { - this.dom.remove(); - } - }, - /** - * replace remove from parent and clean up of ccNode - */ - removeFromParentAndCleanup:function () { - this.dom.remove(); - }, - setOpacity:function (o) { - _ccsg.Node.prototype.setOpacity.call(this, o); - this._opacity = o; - this.dom.style.opacity = o / 255; - }, - /** - * refresh/updates the DOM element - */ - redraw:function () { - if (this.isSprite) { - var tmp = this._children; - this._children = []; - _ccsg.Sprite.prototype.visit.call(this, this.ctx); - this._children = tmp; - } - else { - _ccsg.Sprite.prototype.visit.call(this, this.ctx); - } - } -}; - -cc.DOM._resetEGLViewDiv = function(){ - var div = cc.$("#EGLViewDiv"); - if(div){ - var view = cc.view; - var designSize = view.getDesignResolutionSize(); - var viewPortRect = view.getViewPortRect(); - var screenSize = view.getFrameSize(); - var pixelRatio = view.getDevicePixelRatio(); - var designSizeWidth = designSize.width, designSizeHeight = designSize.height; - if((designSize.width === 0) && (designSize.height === 0)){ - designSizeWidth = screenSize.width; - designSizeHeight = screenSize.height; - } - - var viewPortWidth = viewPortRect.width/pixelRatio; - if((viewPortRect.width === 0) && (viewPortRect.height === 0)){ - viewPortWidth = screenSize.width; - } - - div.style.position = 'absolute'; - //x.dom.style.display='block'; - div.style.width = designSizeWidth + "px"; - div.style.maxHeight = designSizeHeight + "px"; - div.style.margin = 0; - - div.resize(view.getScaleX()/pixelRatio, view.getScaleY()/pixelRatio); - if (view.getResolutionPolicy() === view._rpNoBorder) { - div.style.left = (view.getFrameSize().width - designSizeWidth)/2 + "px"; - div.style.bottom = (view.getFrameSize().height - designSizeHeight*view.getScaleY()/pixelRatio)/2 + "px"; - } - else { - div.style.left = (designSizeWidth*view.getScaleX()/pixelRatio - designSizeWidth) / 2 + "px"; - div.style.bottom = "0px"; - } - } -}; - -/** - * @function - * @private - * @param x - * @return {Boolean} - */ -cc.DOM.parentDOM = function (x) { - var p = x.getParent(); - //if has parent, parent need to have dom too - if (!p || !x.dom) - return false; - if (!p.dom) { - cc.DOM.placeHolder(p); - p.setParent = cc.DOM.methods.setParent; - } - //if parent have dom, attach self to parent - x.dom.appendTo(p.dom); - p.setAnchorPoint(p.getAnchorPoint()); - - if (p.getParent()) { - cc.DOM.parentDOM(p); - } else { - //parent has no more parent, if its running, then add it to the container - if (p.isRunning()) { - //find EGLView div - var eglViewDiv = cc.$("#EGLViewDiv"); - if (eglViewDiv) { - p.dom.appendTo(eglViewDiv); - } else { - cc.DOM._createEGLViewDiv(p); - } - } - } - return true; -}; - -cc.DOM._createEGLViewDiv = function(p){ - var div = cc.$("#EGLViewDiv"); - if(!div){ - div = cc.$new("div"); - div.id = "EGLViewDiv"; - } - - var view = cc.view; - var designSize = view.getDesignResolutionSize(); - var viewPortRect = view.getViewPortRect(); - var screenSize = view.getFrameSize(); - var pixelRatio = view.getDevicePixelRatio(); - var designSizeWidth = designSize.width, designSizeHeight = designSize.height; - if ((designSize.width === 0) && (designSize.height === 0)) { - designSizeWidth = screenSize.width; - designSizeHeight = screenSize.height; - } - - var viewPortWidth = viewPortRect.width/pixelRatio; - if ((viewPortRect.width === 0) && (viewPortRect.height === 0)) { - viewPortWidth = screenSize.width; - } - - div.style.position = 'absolute'; - //x.dom.style.display='block'; - div.style.width = designSizeWidth + "px"; - div.style.maxHeight = designSizeHeight + "px"; - div.style.margin = 0; - - div.resize(view.getScaleX()/pixelRatio, view.getScaleY()/pixelRatio); - if (view.getResolutionPolicy() === view._rpNoBorder) { - div.style.left = (screenSize.width - designSizeWidth)/2 + "px"; - div.style.bottom = (screenSize.height - designSizeHeight*view.getScaleY()/pixelRatio)/2 + "px"; - } - else { - div.style.left = (designSizeWidth*view.getScaleX()/pixelRatio - designSizeWidth) / 2 + "px"; - div.style.bottom = "0px"; - } - - p.dom.appendTo(div); - div.appendTo(cc.container); -}; - -/** - * @function - * @private - * @param x - */ -cc.DOM.setTransform = function (x) { - if (x.ctx) { - x.ctx.translate(x.getAnchorPointInPoints().x, x.getAnchorPointInPoints().y); - if (x.isSprite) { - var tmp = x._children; - x._children = []; - _ccsg.Sprite.prototype.visit.call(x); - x._children = tmp; - } - else { - _ccsg.Sprite.prototype.visit.call(x); - } - } - if (x.dom) { - x.dom.position.x = x.getPositionX(); - x.dom.position.y = -x.getPositionY(); - x.dom.rotation = x.getRotation(); - x.dom.scale = {x:x.getScaleX(), y:x.getScaleY()}; - x.dom.skew = {x:x.getSkewX(), y:x.getSkewY()}; - if (x.setAnchorPoint) - x.setAnchorPoint(x.getAnchorPoint()); - x.dom.transforms(); - } - -}; - -/** - * @function - * @private - * @param x - */ -cc.DOM.forSprite = function (x) { - x.dom = cc.$new('div'); - x.canvas = cc.$new('canvas'); - var locContentSize = x.getContentSize(); - x.canvas.width = locContentSize.width; - x.canvas.height = locContentSize.height; - x.dom.style.position = 'absolute'; - x.dom.style.bottom = 0; - x.ctx = x.canvas.getContext('2d'); - x.dom.appendChild(x.canvas); - if (x.getParent()) { - cc.DOM.parentDOM(x); - } - x.isSprite = true; -}; - -/** - * This creates divs for parent Nodes that are related to the current node - * @function - * @private - * @param x - */ -cc.DOM.placeHolder = function (x) { - //creating a placeholder dom to simulate other ccNode in the hierachy - x.dom = cc.$new('div'); - // x.placeholder = true; - x.dom.style.position = 'absolute'; - x.dom.style.bottom = 0; - //x.dom.style.display='block'; - x.dom.style.width = (x.getContentSize().width || cc.director.getWinSize().width) + "px"; - x.dom.style.maxHeight = (x.getContentSize().height || cc.director.getWinSize().height) + "px"; - x.dom.style.margin = 0; - cc.DOM.setTransform(x); - x.dom.transforms(); - cc.DOM._addMethods(x); - //x.dom.style.border = 'red 1px dotted'; -}; - -/** - * Converts _ccsg.Sprite or cc.MenuItem to DOM elements
- * It currently only supports _ccsg.Sprite and cc.MenuItem - * @function - * @param {_ccsg.Sprite|cc.MenuItem|Array} nodeObject - * @example - * // example - * cc.DOM.convert(Sprite1, Sprite2, Menuitem); - * - * var myDOMElements = [Sprite1, Sprite2, MenuItem]; - * cc.DOM.convert(myDOMElements); - */ -cc.DOM.convert = function (nodeObject) { - //if passing by list, make it an array - if (arguments.length > 1) { - cc.DOM.convert(arguments); - return; - } else if (arguments.length === 1 && !arguments[0].length) { - cc.DOM.convert([arguments[0]]); - return; - } - var args = arguments[0]; - for (var i = 0; i < args.length; i++) { - //first check if its sprite - if (args[i] instanceof _ccsg.Sprite) { - // create a canvas - if (!args[i].dom) - cc.DOM.forSprite(args[i]); - } else { - cc.log('DOM converter only supports sprite and menuitems yet'); - } - cc.DOM._addMethods(args[i]); - args[i].visit = function () { - }; - args[i].transform = function () { - }; - cc.DOM.setTransform(args[i]); - args[i].setVisible(args[i].isVisible()); - } -}; From 6ac8ba8420f130b5e76492b6288218402486e8b9 Mon Sep 17 00:00:00 2001 From: VisualSJ Date: Tue, 28 Jun 2016 10:47:40 +0800 Subject: [PATCH 02/19] Change IOS audio loader event --- cocos2d/core/platform/CCSys.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cocos2d/core/platform/CCSys.js b/cocos2d/core/platform/CCSys.js index d266377c71..323973807f 100644 --- a/cocos2d/core/platform/CCSys.js +++ b/cocos2d/core/platform/CCSys.js @@ -764,6 +764,13 @@ else { __audioSupport = { ONLY_ONE: false, WEB_AUDIO: supportWebAudio, DELAY_CREATE_CTX: false }; + if (sys.os === sys.OS_IOS) { + // IOS no event that used to parse completed callback + // this time is not complete, can not play + // + __audioSupport.USE_LOADER_EVENT = 'loadedmetadata'; + } + if (sys.browserType === sys.BROWSER_TYPE_FIREFOX) { __audioSupport.DELAY_CREATE_CTX = true; __audioSupport.USE_LOADER_EVENT = 'canplay'; From 2a7a66c65e630ea5064290fc4a16278c7f1e8e1b Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Thu, 23 Jun 2016 18:43:00 +0800 Subject: [PATCH 03/19] use __eventTargets to automate unregister Scene events (+1 squashed commit) Squashed commits: [f93e09b] fix unit test --- cocos2d/core/CCNode.js | 12 ------------ cocos2d/core/utils/base-node.js | 25 ++++++++++++++++++++++--- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index 2e2fec7d4b..67be786d82 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -463,9 +463,6 @@ var Node = cc.Class({ this._name = typeof name !== 'undefined' ? name : 'New Node'; this._activeInHierarchy = false; - // Support for ActionManager and EventManager - this.__instanceId = this._id || cc.ClassManager.getNewInstanceId(); - // cache component this._widget = null; @@ -475,15 +472,6 @@ var Node = cc.Class({ // Mouse event listener this._mouseListener = null; - /** - * Register all related EventTargets, - * all event callbacks will be removed in _onPreDestroy - * @property __eventTargets - * @type {EventTarget[]} - * @private - */ - this.__eventTargets = []; - // Retained actions for JSB if (CC_JSB) { this._retainedActions = []; diff --git a/cocos2d/core/utils/base-node.js b/cocos2d/core/utils/base-node.js index 154c33600f..40349e9480 100644 --- a/cocos2d/core/utils/base-node.js +++ b/cocos2d/core/utils/base-node.js @@ -769,11 +769,30 @@ var BaseNode = cc.Class(/** @lends cc.Node# */{ this._sizeProvider = null; this.__ignoreAnchor = false; + + // Support for ActionManager and EventManager + this.__instanceId = this._id || cc.ClassManager.getNewInstanceId(); + + /** + * Register all related EventTargets, + * all event callbacks will be removed in _onPreDestroy + * @property __eventTargets + * @type {EventTarget[]} + * @private + */ + this.__eventTargets = []; }, - _onPreDestroy: CC_JSB && function () { - this._sgNode.release(); - this._sgNode = null; + _onPreDestroy: function () { + if (CC_JSB) { + this._sgNode.release(); + this._sgNode = null; + } + cc.eventManager.removeListeners(this); + for (var i = 0, len = this.__eventTargets.length; i < len; ++i) { + var target = this.__eventTargets[i]; + target && target.targetOff(this); + } }, // ABSTRACT INTERFACES From 99d66f056b374c2305b74cfebada8d6b08ac8e7f Mon Sep 17 00:00:00 2001 From: Nan Wang Date: Tue, 28 Jun 2016 14:23:00 +0800 Subject: [PATCH 04/19] update NodePool api doc --- extensions/ccpool/CCNodePool.js | 41 ++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/extensions/ccpool/CCNodePool.js b/extensions/ccpool/CCNodePool.js index 8f6042b028..07258f7227 100644 --- a/extensions/ccpool/CCNodePool.js +++ b/extensions/ccpool/CCNodePool.js @@ -38,8 +38,9 @@ * !#zh * cc.NodePool 是用于管理节点对象的对象缓存池。
* 它可以帮助您提高游戏性能,适用于优化对象的反复创建和销毁
+ * 以前 cocos2d-x 中的 cc.pool 和新的节点事件注册系统不兼容,因此请使用 cc.NodePool 来代替。 * - * 建议为每种节点分别实例化一个缓冲池,这里的种类对应于游戏中的节点设计,一个 prefab 相当于一个种类的节点。
+ * 新的 NodePool 需要实例化之后才能使用,每种不同的节点对象池需要一个不同的对象池实例,这里的种类对应于游戏中的节点设计,一个 prefab 相当于一个种类的节点。
* 在创建缓冲池时,可以传入一个包含 unuse, reuse 函数的组件类型用于节点的回收和复用逻辑。
* * 一些常见的用例是:
@@ -47,8 +48,23 @@ * 2.糖果粉碎传奇中的木块(频繁创建)。 * 等等.... * @class NodePool - * @param {Function|String} poolHandlerComp The constructor or the class name of the component to control the unuse/reuse logic. - * @constructor + */ + +/** + * !#en + * Constructor for creating a pool for a specific node template (usually a prefab). You can pass a component (type or name) argument for handling event for reusing and recycling node. + * !#zh + * 使用构造函数来创建一个节点专用的对象池,您可以传递一个组件类型或名称,用于处理节点回收和复用时的事件逻辑。 + * @param {Function|String} poolHandlerComp !#en The constructor or the class name of the component to control the unuse/reuse logic. !#zh 处理节点回收和复用事件逻辑的组件类型或名称。 + * @method NodePool + * @example + * properties: { + * template: cc.Prefab + * }, + * onLoad () { + // MyTemplateHandler is a component with 'unuse' and 'reuse' to handle events when node is reused or recycled. + * this.myPool = new cc.NodePool('MyTemplateHandler'); + * } */ cc.NodePool = function (poolHandlerComp) { /** @@ -72,6 +88,19 @@ cc.NodePool.prototype = { return this._pool.length; }, + /** + * !#en Destroy all cached nodes in the pool + * !#zh 销毁对象池中缓存的所有节点 + * @method clear + */ + clear: function () { + var count = this._pool.length; + for (var i = 0; i < count; ++i) { + this._pool[i].destroy(); + } + this._pool.length = 0; + }, + /** * !#en Put a new Node into the pool. * It will automatically remove the node from its parent without cleanup. @@ -80,6 +109,9 @@ cc.NodePool.prototype = { * 这个函数会自动将目标节点从父节点上移除,但是不会进行 cleanup 操作。 * 这个函数会调用 poolHandlerComp 的 unuse 函数,如果组件和函数都存在的话。 * @method put + * @example + * let myNode = cc.instantiate(this.template); + * this.myPool.put(myNode); */ put: function (obj) { if (obj && this._pool.indexOf(obj) === -1) { @@ -102,7 +134,10 @@ cc.NodePool.prototype = { * !#zh 获取对象池中的对象,如果对象池没有可用对象,则返回空。 * 这个函数会调用 poolHandlerComp 的 reuse 函数,如果组件和函数都存在的话。 * @method get + * @param {any} params - !#en Params to pass to 'reuse' method in poolHandlerComp !#zh 向 poolHandlerComp 中的 'reuse' 函数传递的参数 * @return {Object|null} + * @example + * let newNode = this.myPool.get(); */ get: function () { var last = this._pool.length-1; From c7d589289ff730bc9ca6dc2fe31682bdd038c738 Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Mon, 27 Jun 2016 11:53:20 +0800 Subject: [PATCH 05/19] refactor EditBox, fix double click issue on mobile finish first version of refactoring remove dom node working now. remove previous comments add webgl render fix editbox running improve editbox fix PC editbox --- cocos2d/core/components/CCEditBox.js | 4 +- cocos2d/core/editbox/CCSGEditBox.js | 1214 ++++++++++++-------------- 2 files changed, 583 insertions(+), 635 deletions(-) diff --git a/cocos2d/core/components/CCEditBox.js b/cocos2d/core/components/CCEditBox.js index a060ce7a89..17c4baa9f8 100644 --- a/cocos2d/core/components/CCEditBox.js +++ b/cocos2d/core/components/CCEditBox.js @@ -490,11 +490,11 @@ var EditBox = cc.Class({ if(CC_JSB) { EditBox.prototype.editBoxEditingDidBegin = function (sender) { this.editBoxEditingDidBegan(sender); - } + }; EditBox.prototype.editBoxEditingDidEnd = function (sender) { this.editBoxEditingDidEnded(sender); - } + }; } cc.EditBox = module.exports = EditBox; diff --git a/cocos2d/core/editbox/CCSGEditBox.js b/cocos2d/core/editbox/CCSGEditBox.js index 643f427111..66639c6211 100644 --- a/cocos2d/core/editbox/CCSGEditBox.js +++ b/cocos2d/core/editbox/CCSGEditBox.js @@ -195,35 +195,480 @@ cc.EditBoxDelegate = cc._Class.extend({ } }); -var EditBoxImpl = function(editBox) { - this._spriteDOM = null; - this._edTxt = null; - this._edFontSize = 14; - this._edFontName = 'Arial'; - this._editBox = editBox; - this._textLabel = null; - this._placeholderLabel = null; -}; -EditBoxImpl.prototype = { - constructor: EditBoxImpl, +/** + *

cc.EditBox is a brief Class for edit box.
+ * You can use this widget to gather small amounts of text from the user.

+ * + */ +_ccsg.EditBox = _ccsg.Node.extend({ + _backgroundSprite: null, + _delegate: null, + _editBoxInputMode: InputMode.ANY, + _editBoxInputFlag: InputFlag.SENSITIVE, + _keyboardReturnType: KeyboardReturnType.DEFAULT, + _maxLength: 50, + _text: '', + _textColor: null, + _placeholderText: '', + _placeholderFontName: '', + _placeholderFontSize: 14, + _placeholderColor: null, + _className: 'EditBox', - createNativeControl: function(size) { - this._createDomTextArea(); - this._addDomInputControl(); + ctor: function (size, normal9SpriteBg) { + _ccsg.Node.prototype.ctor.call(this); - // this._domInputSprite.dom.showTooltipDiv = false; - // this._domInputSprite.dom.style.width = (size.width - 6) + 'px'; - // this._domInputSprite.dom.style.height = (size.height - 6) + 'px'; + this._textColor = cc.Color.WHITE; + this._placeholderColor = cc.Color.GRAY; - - // this._domInputSprite.x = 3; - // this._domInputSprite.y = 3; - - this._createLabels(); + this.initWithSizeAndBackgroundSprite(size, normal9SpriteBg); + this._renderCmd.createNativeControl(); }, - _createLabels: function () { + _createRenderCmd: function () { + if (cc._renderType === cc.game.RENDER_TYPE_CANVAS) { + return new _ccsg.EditBox.CanvasRenderCmd(this); + } else { + return new _ccsg.EditBox.WebGLRenderCmd(this); + } + }, + + setContentSize: function (width, height) { + if (width.width !== undefined && width.height !== undefined) { + height = width.height; + width = width.width; + } + _ccsg.Node.prototype.setContentSize.call(this, width, height); + this._updateEditBoxSize(width, height); + }, + + setVisible: function ( visible ) { + _ccsg.Node.prototype.setVisible.call(this, visible); + this._renderCmd.updateVisibility(); + }, + + cleanup: function () { + this._renderCmd._removeDomInputControl(); + this.stopAllActions(); + this.unscheduleAllCallbacks(); + }, + + _onTouchBegan: function (touch) { + var touchPoint = touch.getLocation(); + var bb = cc.rect(0,0, this._contentSize.width, this._contentSize.height); + var hitted = cc.rectContainsPoint(bb, this.convertToNodeSpace(touchPoint)); + if(hitted) { + return true; + } + else { + this._renderCmd.hidden(); + return false; + } + }, + + _onTouchEnded: function () { + this._renderCmd.show(); + }, + + _updateBackgroundSpriteSize: function (width, height) { + if(this._backgroundSprite) { + this._backgroundSprite.setContentSize(width, height); + } + }, + + _updateEditBoxSize: function(size, height) { + var newWidth = (typeof size.width === 'number') ? size.width : size; + var newHeight = (typeof size.height === 'number') ? size.height : height; + + this._updateBackgroundSpriteSize(newWidth, newHeight); + this._renderCmd.updateSize(newWidth, newHeight); + }, + + setLineHeight: function (lineHeight) { + this._renderCmd.setLineHeight(lineHeight); + }, + + setFont: function (fontName, fontSize) { + this._renderCmd.setFont(fontName, fontSize); + }, + + _setFont: function (fontStyle) { + this._renderCmd._setFont(fontStyle); + }, + + getBackgroundSprite: function() { + return this._backgroundSprite; + }, + + setFontName: function (fontName) { + this._renderCmd.setFontName(fontName); + }, + + setFontSize: function (fontSize) { + this._renderCmd.setFontSize(fontSize); + }, + + setString: function (text) { + if (text.length >= this._maxLength) { + text = text.slice(0, this._maxLength); + } + this._text = text; + this._renderCmd.setString(text); + }, + + setFontColor: function (color) { + this._textColor = color; + this._renderCmd.setFontColor(color); + }, + + setMaxLength: function (maxLength) { + if (!isNaN(maxLength) && maxLength > 0) { + this._maxLength = maxLength; + this._renderCmd.setMaxLength(maxLength); + } + }, + + getMaxLength: function () { + return this._maxLength; + }, + + setPlaceHolder: function (text) { + if (text !== null) { + this._renderCmd.setPlaceHolder(text); + this._placeholderText = text; + } + }, + + setPlaceholderFont: function (fontName, fontSize) { + this._placeholderFontName = fontName; + this._placeholderFontSize = fontSize; + this._renderCmd._updateDOMPlaceholderFontStyle(); + }, + + _setPlaceholderFont: function (fontStyle) { + var res = cc.LabelTTF._fontStyleRE.exec(fontStyle); + if (res) { + this._placeholderFontName = res[2]; + this._placeholderFontSize = parseInt(res[1]); + this._renderCmd._updateDOMPlaceholderFontStyle(); + } + }, + + setPlaceholderFontName: function (fontName) { + this._placeholderFontName = fontName; + this._renderCmd._updateDOMPlaceholderFontStyle(); + }, + + setPlaceholderFontSize: function (fontSize) { + this._placeholderFontSize = fontSize; + this._renderCmd._updateDOMPlaceholderFontStyle(); + }, + + setPlaceholderFontColor: function (color) { + this._placeholderColor = color; + this._renderCmd.setPlaceholderFontColor(color); + }, + + setInputFlag: function (inputFlag) { + this._editBoxInputFlag = inputFlag; + this._renderCmd.setInputFlag(inputFlag); + }, + + getText: function () { + cc.log('Please use the getString'); + return this.getString(); + }, + + getString: function () { + return this._text; + }, + + initWithSizeAndBackgroundSprite: function (size, normal9SpriteBg) { + if(this._backgroundSprite) { + this._backgroundSprite.removeFromParent(); + } + this._backgroundSprite = normal9SpriteBg; + _ccsg.Node.prototype.setContentSize.call(this, size); + + if(this._backgroundSprite && !this._backgroundSprite.parent) { + this._backgroundSprite.setAnchorPoint(cc.p(0, 0)); + this.addChild(this._backgroundSprite); + + this._updateBackgroundSpriteSize(size.width, size.height); + } + + + this.x = 0; + this.y = 0; + return true; + }, + + setDelegate: function (delegate) { + this._delegate = delegate; + }, + + getPlaceHolder: function () { + return this._placeholderText; + }, + + setInputMode: function (inputMode) { + if (this._editBoxInputMode === inputMode) return; + + var oldText = this.getString(); + + this._renderCmd.setInputMode(inputMode); + this._renderCmd.transform(); + + this.setString(oldText); + this._editBoxInputMode = inputMode; + this._renderCmd._updateLabelPosition(this.getContentSize()); + }, + + setReturnType: function (returnType) { + this._keyboardReturnType = returnType; + }, + + initWithBackgroundColor: function (size, bgColor) { + this._edWidth = size.width; + this.dom.style.width = this._edWidth.toString() + 'px'; + this._edHeight = size.height; + this.dom.style.height = this._edHeight.toString() + 'px'; + this.dom.style.backgroundColor = cc.colorToHex(bgColor); + } +}); + +var _p = _ccsg.EditBox.prototype; + +// Extended properties +_p.font; +cc.defineGetterSetter(_p, 'font', null, _p._setFont); +cc.defineGetterSetter(_p, 'fontName', null, _p.setFontName); +cc.defineGetterSetter(_p, 'fontSize', null, _p.setFontSize); +cc.defineGetterSetter(_p, 'fontColor', null, _p.setFontColor); +cc.defineGetterSetter(_p, 'string', _p.getString, _p.setString); +cc.defineGetterSetter(_p, 'maxLength', _p.getMaxLength, _p.setMaxLength); +cc.defineGetterSetter(_p, 'placeholder', _p.getPlaceHolder, _p.setPlaceHolder); +cc.defineGetterSetter(_p, 'placeholderFont', null, _p._setPlaceholderFont); +cc.defineGetterSetter(_p, 'placeholderFontName', null, _p.setPlaceholderFontName); +cc.defineGetterSetter(_p, 'placeholderFontSize', null, _p.setPlaceholderFontSize); +cc.defineGetterSetter(_p, 'placeholderFontColor', null, _p.setPlaceholderFontColor); +cc.defineGetterSetter(_p, 'inputFlag', null, _p.setInputFlag); +cc.defineGetterSetter(_p, 'delegate', null, _p.setDelegate); +cc.defineGetterSetter(_p, 'inputMode', null, _p.setInputMode); +cc.defineGetterSetter(_p, 'returnType', null, _p.setReturnType); + +_p = null; + +_ccsg.EditBox.InputMode = InputMode; +_ccsg.EditBox.InputFlag = InputFlag; +_ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; + +(function (editbox) { + editbox._polyfill = { + zoomInvalid: false + }; + + if (cc.sys.OS_ANDROID === cc.sys.os + && (cc.sys.browserType === cc.sys.BROWSER_TYPE_SOUGOU + || cc.sys.browserType === cc.sys.BROWSER_TYPE_360)) { + editbox._polyfill.zoomInvalid = true; + } +})(_ccsg.EditBox); + +(function (polyfill) { + var EditBoxImpl = function () { + + }; + + var proto = EditBoxImpl.prototype = Object.create(Object.prototype); + + + + proto.updateMatrix = function () { + if (!this._edTxt) return; + var node = this._node, scaleX = cc.view._scaleX, scaleY = cc.view._scaleY; + var dpr = cc.view._devicePixelRatio; + var t = node.getNodeToWorldTransform(); + if (!t) return; + + scaleX /= dpr; + scaleY /= dpr; + + var container = cc.container; + var a = t.a * scaleX, b = t.b, c = t.c, d = t.d * scaleY; + + var offsetX = container && container.style.paddingLeft && parseInt(container.style.paddingLeft); + var offsetY = container && container.style.paddingBottom && parseInt(container.style.paddingBottom); + var tx = t.tx * scaleX + offsetX, ty = t.ty * scaleY + offsetY; + + if (polyfill.zoomInvalid) { + this.updateSize(node._contentSize.width * a, node._contentSize.height * d); + a = 1; + d = 1; + } + + var matrix = "matrix(" + a + "," + -b + "," + -c + "," + d + "," + tx + "," + -ty + ")"; + this._edTxt.style['transform'] = matrix; + this._edTxt.style['-webkit-transform'] = matrix; + this._edTxt.style['transform-origin'] = '0px 100% 0px'; + this._edTxt.style['-webkit-transform-origin'] = '0px 100% 0px'; + }; + + proto.updateVisibility = function () { + var node = this._node; + if (!this._edTxt) return; + var editBox = this._edTxt; + if (node.visible) { + editBox.style.visibility = 'visible'; + } else { + editBox.style.visibility = 'hidden'; + } + }; + + proto._createDomInput = function () { + var thisPointer = this; + var tmpEdTxt = this._edTxt = document.createElement('input'); + tmpEdTxt.type = 'text'; + tmpEdTxt.style.fontSize = this._edFontSize + 'px'; + tmpEdTxt.style.color = '#000000'; + tmpEdTxt.style.border = 0; + tmpEdTxt.style.background = 'transparent'; + tmpEdTxt.style.width = '100%'; + tmpEdTxt.style.height = '100%'; + tmpEdTxt.style.active = 0; + tmpEdTxt.style.outline = 'medium'; + tmpEdTxt.style.padding = '0'; + tmpEdTxt.style.textTransform = 'uppercase'; + tmpEdTxt.style.display = 'none'; + tmpEdTxt.style.position = "absolute"; + tmpEdTxt.style.bottom = "0px"; + tmpEdTxt.style.left = "0px"; + tmpEdTxt.style.className = "cocosEditBox"; + + tmpEdTxt.addEventListener('input', function () { + var editBox = thisPointer._editBox; + if (editBox._delegate && editBox._delegate.editBoxTextChanged) { + editBox._text = this.value; + thisPointer._updateEditBoxContentStyle(); + editBox._delegate.editBoxTextChanged(editBox, editBox._text); + } + }); + tmpEdTxt.addEventListener('keypress', function (e) { + var editBox = thisPointer._editBox; + + if (e.keyCode === cc.KEY.enter) { + e.stopPropagation(); + e.preventDefault(); + if(this.value === '') { + this.style.fontSize = editBox._placeholderFontSize + 'px'; + this.style.color = cc.colorToHex(editBox._placeholderColor); + } + editBox._text = this.value; + thisPointer._updateEditBoxContentStyle(); + thisPointer.hidden(); + if (editBox._delegate && editBox._delegate.editBoxReturn) { + editBox._delegate.editBoxReturn(editBox); + } + cc._canvas.focus(); + } + }); + tmpEdTxt.addEventListener('focus', function () { + var editBox = thisPointer._editBox; + this.style.fontSize = thisPointer._edFontSize + 'px'; + this.style.color = cc.colorToHex(editBox._textColor); + + if (editBox._editBoxInputFlag === InputFlag.PASSWORD) { + thisPointer._edTxt.type = 'password'; + } else { + thisPointer._edTxt.type = 'text'; + } + + if (editBox._delegate && editBox._delegate.editBoxEditingDidBegan) { + editBox._delegate.editBoxEditingDidBegan(editBox); + } + }); + tmpEdTxt.addEventListener('blur', function () { + var editBox = thisPointer._editBox; + editBox._text = this.value; + thisPointer._updateEditBoxContentStyle(); + + if (editBox._delegate && editBox._delegate.editBoxEditingDidEnded) { + editBox._delegate.editBoxEditingDidEnded(editBox); + } + + if (this.value === '') { + this.style.fontSize = editBox._placeholderFontSize + 'px'; + this.style.color = cc.colorToHex(editBox._placeholderColor); + } + thisPointer.hidden(); + }); + return tmpEdTxt; + }; + + proto._createDomTextArea = function () { + var thisPointer = this; + var tmpEdTxt = this._edTxt = document.createElement('textarea'); + tmpEdTxt.type = 'text'; + tmpEdTxt.style.fontSize = this._edFontSize + 'px'; + tmpEdTxt.style.color = '#000000'; + tmpEdTxt.style.border = 0; + tmpEdTxt.style.background = 'transparent'; + tmpEdTxt.style.width = '100%'; + tmpEdTxt.style.height = '100%'; + tmpEdTxt.style.active = 0; + tmpEdTxt.style.outline = 'medium'; + tmpEdTxt.style.padding = '0'; + tmpEdTxt.style.resize = 'none'; + tmpEdTxt.style.textTransform = 'uppercase'; + tmpEdTxt.style.overflow_y = 'scroll'; + tmpEdTxt.style.display = 'none'; + tmpEdTxt.style.position = "absolute"; + tmpEdTxt.style.bottom = "0px"; + tmpEdTxt.style.left = "0px"; + tmpEdTxt.style.className = "cocosEditBox"; + + tmpEdTxt.addEventListener('input', function () { + var editBox = thisPointer._editBox; + if (editBox._delegate && editBox._delegate.editBoxTextChanged) { + editBox._text = this.value; + thisPointer._updateEditBoxContentStyle(); + editBox._delegate.editBoxTextChanged(editBox, editBox._text); + } + }); + + tmpEdTxt.addEventListener('focus', function () { + var editBox = thisPointer._editBox; + + this.style.fontSize = thisPointer._edFontSize + 'px'; + this.style.color = cc.colorToHex(editBox._textColor); + + if (editBox._delegate && editBox._delegate.editBoxEditingDidBegan) { + editBox._delegate.editBoxEditingDidBegan(editBox); + } + + }); + tmpEdTxt.addEventListener('blur', function () { + var editBox = thisPointer._editBox; + editBox._text = this.value; + thisPointer._updateEditBoxContentStyle(); + + if (editBox._delegate && editBox._delegate.editBoxEditingDidEnded) { + editBox._delegate.editBoxEditingDidEnded(editBox); + } + + if (this.value === '') { + this.style.fontSize = editBox._placeholderFontSize + 'px'; + this.style.color = cc.colorToHex(editBox._placeholderColor); + } + + thisPointer.hidden(); + }); + + return tmpEdTxt; + }; + + proto._createLabels = function () { var editBoxSize = this._editBox.getContentSize(); this._textLabel = new _ccsg.Label(); this._textLabel.setVisible(false); @@ -237,9 +682,9 @@ EditBoxImpl.prototype = { this._editBox.addChild(this._placeholderLabel, 100); this._updateLabelPosition(editBoxSize); - }, + }; - _updateLabelPosition: function (editBoxSize) { + proto._updateLabelPosition = function (editBoxSize) { this._textLabel.setContentSize(editBoxSize); this._placeholderLabel.setLineHeight(editBoxSize.height); var placeholderLabelSize = this._placeholderLabel.getContentSize(); @@ -259,18 +704,18 @@ EditBoxImpl.prototype = { this._textLabel.setVerticalAlign(cc.VerticalTextAlignment.CENTER); } - }, + }; - setLineHeight: function (lineHeight) { + proto.setLineHeight = function (lineHeight) { this._textLabel.setLineHeight(lineHeight); - }, + }; - _hiddenLabels: function () { + proto._hiddenLabels = function () { this._textLabel.setVisible(false); this._placeholderLabel.setVisible(false); - }, + }; - _updateEditBoxContentStyle: function() { + proto._updateEditBoxContentStyle = function() { var inputFlag = this._editBox._editBoxInputFlag; if (inputFlag === InputFlag.INITIAL_CAPS_ALL_CHARACTERS) { this._editBox._text = this._editBox._text.toUpperCase(); @@ -281,9 +726,9 @@ EditBoxImpl.prototype = { else if (inputFlag === InputFlag.INITIAL_CAPS_SENTENCE) { this._editBox._text = capitalizeFirstLetter(this._editBox._text); } - }, + }; - _updateLabelString: function() { + proto._updateLabelString = function() { this._textLabel.setVisible(true); this._textLabel.setString(this._editBox._text); if (this._edTxt.type === 'password') { @@ -297,9 +742,9 @@ EditBoxImpl.prototype = { this._updateEditBoxContentStyle(); this._textLabel.setString(this._editBox._text); } - }, + }; - _showLabels: function () { + proto._showLabels = function () { this._hiddenLabels(); if (this._edTxt.value === '') { this._placeholderLabel.setVisible(true); @@ -309,86 +754,71 @@ EditBoxImpl.prototype = { else { this._updateLabelString(); } - }, + }; - registerClickEvent: function() { - var self = this; - var div = self._edTxt.parentNode; - div.style.pointerEvents = 'none'; - div.addEventListener('click', function() { - if (self._edTxt.style.display === 'none') { - self._edTxt.style.display = ''; - self._edTxt.focus(); - self._hiddenLabels(); - } - }); - }, + proto.show = function() { + if (this._edTxt.style.display === 'none') { + this._edTxt.style.display = ''; + this._edTxt.focus(); + this._hiddenLabels(); + } + }; - show: function() { - var div = this._edTxt.parentNode; - div.style.pointerEvents = ''; - window.setTimeout(function() { - div.click(); - }, 10); - }, - - hidden: function() { + proto.hidden = function() { this._edTxt.style.display = 'none'; - var div = this._edTxt.parentNode; - div.style.pointerEvents = 'none'; this._showLabels(); - }, + }; - _setFont: function (fontStyle) { + proto._setFont = function (fontStyle) { var res = cc.LabelTTF._fontStyleRE.exec(fontStyle); var textFontName = res[2]; var textFontSize = parseInt(res[1]); if (res) { this.setFont(textFontName, textFontSize); } - }, + }; - setFont: function (fontName, fontSize) { + proto.setFont = function (fontName, fontSize) { this._edFontName = fontName || this._edFontName; this._edFontSize = fontSize || this._edFontSize; this._updateDOMFontStyle(); - }, + }; - setFontName: function (fontName) { + proto.setFontName = function (fontName) { this._edFontName = fontName || this._edFontName; this._updateDOMFontStyle(); - }, + }; - setFontSize: function (fontSize) { + proto.setFontSize = function (fontSize) { this._edFontSize = fontSize || this._edFontSize; this._updateDOMFontStyle(); - }, + }; - setFontColor: function (color) { + proto.setFontColor = function (color) { if (this._edTxt.value !== this._editBox._placeholderText) { this._edTxt.style.color = cc.colorToHex(color); } this._textLabel.setColor(color); - }, + }; - setPlaceHolder: function (text) { + proto.setPlaceHolder = function (text) { this._placeholderLabel.setString(text); - }, + }; - setMaxLength: function (maxLength) { + proto.setMaxLength = function (maxLength) { this._edTxt.maxLength = maxLength; - }, + }; - _updateDOMPlaceholderFontStyle: function () { + proto._updateDOMPlaceholderFontStyle = function () { this._placeholderLabel.setFontFileOrFamily(this._editBox._placeholderFontName); this._placeholderLabel.setFontSize(this._editBox._placeholderFontSize); - }, + }; - setPlaceholderFontColor: function (color) { + proto.setPlaceholderFontColor = function (color) { this._placeholderLabel.setColor(color); - }, + }; - setInputFlag: function (inputFlag) { + proto.setInputFlag = function (inputFlag) { if ((this._edTxt.value !== this._placeholderText) && (inputFlag === InputFlag.PASSWORD)) this._edTxt.type = 'password'; else @@ -403,9 +833,9 @@ EditBoxImpl.prototype = { this._edTxt.style.textTransform = 'capitalize'; } this._updateLabelString(); - }, + }; - setInputMode : function (inputMode) { + proto.setInputMode = function (inputMode) { if (inputMode === InputMode.ANY) { this._removeDomInputControl(); this._createDomTextArea(); @@ -416,13 +846,11 @@ EditBoxImpl.prototype = { this._createDomInput(); this._addDomInputControl(); } - }, + var contentSize = this._node.getContentSize(); + this.updateSize(contentSize.width, contentSize.height); + }; - /** - * Set the text entered in the edit box. - * @param {string} text The given text. - */ - setString: function (text) { + proto.setString = function (text) { if (text !== null) { this._edTxt.value = text; @@ -446,9 +874,9 @@ EditBoxImpl.prototype = { this._updateLabelString(); } } - }, + }; - _updateDOMFontStyle: function() { + proto._updateDOMFontStyle = function() { if (this._edTxt.value !== '') { this._edTxt.style.fontFamily = this._edFontName; this._edTxt.style.fontSize = this._edFontSize + 'px'; @@ -459,564 +887,84 @@ EditBoxImpl.prototype = { } this._textLabel.setFontSize(this._edFontSize); this._textLabel.setFontFileOrFamily(this._edFontName); - }, + }; - setSize: function(newWidth, newHeight) { - this._spriteDOM.dom.style.width = (newWidth - 6) + 'px'; - this._spriteDOM.dom.style.height = (newHeight - 6) + 'px'; + proto.updateSize = function(newWidth, newHeight) { + var editboxDomNode = this._edTxt; + if (!editboxDomNode) return; + + editboxDomNode.style['width'] = newWidth + 'px'; + editboxDomNode.style['height'] = newHeight + 'px'; + this._updateLabelPosition(cc.size(newWidth, newHeight)); - }, + }; - _createDomInput: function () { - var selfPointer = this; - var tmpEdTxt = this._edTxt = document.createElement('input'); - tmpEdTxt.type = 'text'; - tmpEdTxt.style.fontSize = this._edFontSize + 'px'; - tmpEdTxt.style.color = '#000000'; - tmpEdTxt.style.border = 0; - tmpEdTxt.style.background = 'transparent'; - //tmpEdTxt.style.paddingLeft = '2px'; - tmpEdTxt.style.width = '100%'; - tmpEdTxt.style.height = '100%'; - tmpEdTxt.style.active = 0; - tmpEdTxt.style.outline = 'medium'; - tmpEdTxt.style.padding = '0'; - tmpEdTxt.style.textTransform = 'uppercase'; - tmpEdTxt.style.display = 'none'; - var onCanvasClick = function() { tmpEdTxt.blur();}; + proto.createNativeControl = function() { + this._createDomTextArea(); + this._addDomInputControl(); + this._createLabels(); + }; - tmpEdTxt.addEventListener('input', function () { - var editBox = selfPointer._editBox; - if (editBox._delegate && editBox._delegate.editBoxTextChanged) { - editBox._text = this.value; - selfPointer._updateEditBoxContentStyle(); - editBox._delegate.editBoxTextChanged(editBox, editBox._text); + proto._addDomInputControl = function () { + cc.container.appendChild(this._edTxt); + }; + + proto._removeDomInputControl = function () { + var editBox = this._edTxt; + if(editBox){ + var hasChild = false; + if('contains' in cc.container) { + hasChild = cc.container.contains(editBox); + }else { + hasChild = cc.container.compareDocumentPosition(editBox) % 16; } - }); - tmpEdTxt.addEventListener('keypress', function (e) { - var editBox = selfPointer._editBox; - - if (e.keyCode === cc.KEY.enter) { - e.stopPropagation(); - e.preventDefault(); - if(this.value === '') { - this.style.fontSize = editBox._placeholderFontSize + 'px'; - this.style.color = cc.colorToHex(editBox._placeholderColor); - } - editBox._text = this.value; - selfPointer._updateEditBoxContentStyle(); - selfPointer.hidden(); - if (editBox._delegate && editBox._delegate.editBoxReturn) { - editBox._delegate.editBoxReturn(editBox); - } - cc._canvas.focus(); - } - }); - tmpEdTxt.addEventListener('focus', function () { - var editBox = selfPointer._editBox; - - this.style.fontSize = selfPointer._edFontSize + 'px'; - this.style.color = cc.colorToHex(editBox._textColor); - if (editBox._editBoxInputFlag === InputFlag.PASSWORD) { - selfPointer._edTxt.type = 'password'; - } else { - selfPointer._edTxt.type = 'text'; - } - - if (editBox._delegate && editBox._delegate.editBoxEditingDidBegan) { - editBox._delegate.editBoxEditingDidBegan(editBox); - } - cc._canvas.addEventListener('click', onCanvasClick); - }); - tmpEdTxt.addEventListener('blur', function () { - var editBox = selfPointer._editBox; - editBox._text = this.value; - selfPointer._updateEditBoxContentStyle(); - - if (editBox._delegate && editBox._delegate.editBoxEditingDidEnded) { - editBox._delegate.editBoxEditingDidEnded(editBox); - } - cc._canvas.removeEventListener('click', onCanvasClick); - - if (this.value === '') { - this.style.fontSize = editBox._placeholderFontSize + 'px'; - this.style.color = cc.colorToHex(editBox._placeholderColor); - } - selfPointer.hidden(); - }); - return tmpEdTxt; - }, - - _createDomTextArea: function () { - var selfPointer = this; - var tmpEdTxt = this._edTxt = document.createElement('textarea'); - tmpEdTxt.type = 'text'; - tmpEdTxt.style.fontSize = this._edFontSize + 'px'; - tmpEdTxt.style.color = '#000000'; - tmpEdTxt.style.border = 0; - tmpEdTxt.style.background = 'transparent'; - //tmpEdTxt.style.paddingLeft = '2px'; - tmpEdTxt.style.width = '100%'; - tmpEdTxt.style.height = '100%'; - tmpEdTxt.style.active = 0; - tmpEdTxt.style.outline = 'medium'; - tmpEdTxt.style.padding = '0'; - tmpEdTxt.style.resize = 'none'; - tmpEdTxt.style.textTransform = 'uppercase'; - tmpEdTxt.style.overflow_y = 'scroll'; - tmpEdTxt.style.display = 'none'; - var onCanvasClick = function() { tmpEdTxt.blur();}; - - tmpEdTxt.addEventListener('input', function () { - var editBox = selfPointer._editBox; - if (editBox._delegate && editBox._delegate.editBoxTextChanged) { - editBox._text = this.value; - selfPointer._updateEditBoxContentStyle(); - editBox._delegate.editBoxTextChanged(editBox, editBox._text); - } - }); - - tmpEdTxt.addEventListener('focus', function () { - var editBox = selfPointer._editBox; - - this.style.fontSize = selfPointer._edFontSize + 'px'; - this.style.color = cc.colorToHex(editBox._textColor); - - if (editBox._delegate && editBox._delegate.editBoxEditingDidBegan) { - editBox._delegate.editBoxEditingDidBegan(editBox); - } - - cc._canvas.addEventListener('click', onCanvasClick); - }); - tmpEdTxt.addEventListener('blur', function () { - var editBox = selfPointer._editBox; - editBox._text = this.value; - selfPointer._updateEditBoxContentStyle(); - - if (editBox._delegate && editBox._delegate.editBoxEditingDidEnded) { - editBox._delegate.editBoxEditingDidEnded(editBox); - } - - if (this.value === '') { - this.style.fontSize = editBox._placeholderFontSize + 'px'; - this.style.color = cc.colorToHex(editBox._placeholderColor); - } - - selfPointer.hidden(); - cc._canvas.removeEventListener('click', onCanvasClick); - }); - - return tmpEdTxt; - } -}; - -/** - *

cc.EditBox is a brief Class for edit box.
- * You can use this widget to gather small amounts of text from the user.

- * - * @class - * @extends cc.Node - * - * @property {String} string - Content string of edit box - * @property {String} maxLength - Max length of the content string - * @property {String} font - <@writeonly> Config font of edit box - * @property {String} fontName - <@writeonly> Config font name of edit box - * @property {Number} fontSize - <@writeonly> Config font size of edit box - * @property {cc.Color} fontColor - <@writeonly> Config font color of edit box - * @property {String} placeHolder - Place holder of edit box - * @property {String} placeHolderFont - <@writeonly> Config font of place holder - * @property {String} placeHolderFontName - <@writeonly> Config font name of place holder - * @property {Number} placeHolderFontSize - <@writeonly> Config font size of place holder - * @property {cc.Color} placeHolderFontColor - <@writeonly> Config font color of place holder - * @property {cc.EditBox.InputFlag} inputFlag - <@writeonly> Input flag of edit box, one of the cc.EditBox.InputFlag constants. e.g.cc.EditBox.InputFlag..PASSWORD - * @property {Object} delegate - <@writeonly> Delegate of edit box - * @property {cc.EditBox.InputMode} inputMode - <@writeonly> Input mode of the edit box. Value should be one of the cc.EditBox.InputMode constants. - * @property {Number} returnType - <@writeonly> Return type of edit box, value should be one of the KeyboardReturnType constants. - * - */ -_ccsg.EditBox = _ccsg.Node.extend({ - _backgroundSprite: null, - - _delegate: null, - _editBoxInputMode: InputMode.ANY, - _editBoxInputFlag: InputFlag.SENSITIVE, - _keyboardReturnType: KeyboardReturnType.DEFAULT, - _maxLength: 50, - - _text: '', - _textColor: null, - - _placeholderText: '', - _placeholderFontName: '', - _placeholderFontSize: 14, - _placeholderColor: null, - - _className: 'EditBox', - - /** - * constructor of cc.EditBox - * @param {cc.Size} size - * @param {cc.Scale9Sprite} normal9SpriteBg - * @param {cc.Scale9Sprite} press9SpriteBg - * @param {cc.Scale9Sprite} disabled9SpriteBg - */ - ctor: function (size, normal9SpriteBg, press9SpriteBg, disabled9SpriteBg) { - _ccsg.Node.prototype.ctor.call(this); - - this._textColor = cc.Color.WHITE; - this._placeholderColor = cc.Color.GRAY; - _ccsg.Node.prototype.setContentSize.call(this, size); - - //because cc.DOM.convert will replace editbox's setContentSize method. - //here is a hack to provide a better version of setContentSize. - this._oldSetContentSize = this.setContentSize; - this.setContentSize = this._updateEditBoxSize; - - this.initWithSizeAndBackgroundSprite(size, normal9SpriteBg); - - this.scheduleOnce(function () { - editBoxImpl.registerClickEvent(); - }, 0.01); - }, - - _onTouchBegan: function (touch, event) { - var touchPoint = touch.getLocation(); - var bb = cc.rect(0,0, this._contentSize.width, this._contentSize.height); - var hitted = cc.rectContainsPoint(bb, this.convertToNodeSpace(touchPoint)); - if(hitted) { - return true; + if(hasChild) + cc.container.removeChild(editBox); } - else { - this._nativeControl.hidden(); - return false; - } - }, + this._edTxt = null; + }; - _onTouchEnded: function (touch, event) { - this._nativeControl.show(); - }, + proto.initializeRenderCmd = function (node) { + this._editBox = node; - _updateBackgroundSpriteSize: function (width, height) { - if(this._backgroundSprite) { - this._backgroundSprite.setContentSize(width, height); - } - }, + //it's a dom node, may be assigned with Input or TextArea. + this._edTxt = null; + this._edFontSize = 14; + this._edFontName = 'Arial'; + this._textLabel = null; + this._placeholderLabel = null; + }; - _updateEditBoxSize: function(size, height) { - this._oldSetContentSize(size, height); + //define the canvas render command + _ccsg.EditBox.CanvasRenderCmd = function (node) { + _ccsg.Node.CanvasRenderCmd.call(this, node); + this.initializeRenderCmd(node); + }; - var newWidth = (typeof size.width === 'number') ? size.width : size; - var newHeight = (typeof size.height === 'number') ? size.height : height; + var canvasRenderCmdProto = _ccsg.EditBox.CanvasRenderCmd.prototype = Object.create(_ccsg.Node.CanvasRenderCmd.prototype); + cc.js.mixin(canvasRenderCmdProto, proto); + canvasRenderCmdProto.constructor = _ccsg.EditBox.CanvasRenderCmd; - this._updateBackgroundSpriteSize(newWidth, newHeight); - this._nativeControl.setSize(newWidth, newHeight); - }, - - setLineHeight: function (lineHeight) { - this._nativeControl.setLineHeight(lineHeight); - }, - - /** - * Set the font. - * @param {String} fontName The font name. - * @param {Number} fontSize The font size. - */ - setFont: function (fontName, fontSize) { - this._nativeControl.setFont(fontName, fontSize); - }, - - _setFont: function (fontStyle) { - this._nativeControl._setFont(fontStyle); - }, - - getBackgroundSprite: function() { - return this._backgroundSprite; - }, - - /** - * set fontName - * @param {String} fontName - */ - setFontName: function (fontName) { - this._nativeControl.setFontName(fontName); - }, - - /** - * set fontSize - * @param {Number} fontSize - */ - setFontSize: function (fontSize) { - this._nativeControl.setFontSize(fontSize); - }, - - /** - * Set the text entered in the edit box. - * @deprecated - * @param {string} text The given text. - */ - setText: function (text) { - cc.log('Please use the setString'); - this.setString(text); - }, - - setString: function (text) { - if (text.length >= this._maxLength) { - text = text.slice(0, this._maxLength); - } - this._text = text; - this._nativeControl.setString(text); - }, - - /** - * Set the font color of the widget's text. - * @param {cc.Color} color - */ - setFontColor: function (color) { - this._textColor = color; - this._nativeControl.setFontColor(color); - }, - - /** - *

- * Sets the maximum input length of the edit box.
- * Setting this value enables multiline input mode by default. - *

- * @param {Number} maxLength The maximum length. - */ - setMaxLength: function (maxLength) { - if (!isNaN(maxLength) && maxLength > 0) { - this._maxLength = maxLength; - this._nativeControl.setMaxLength(maxLength); - } - }, - - /** - * Gets the maximum input length of the edit box. - * @return {Number} Maximum input length. - */ - getMaxLength: function () { - return this._maxLength; - }, - - /** - * Set a text in the edit box that acts as a placeholder when an edit box is empty. - * @param {string} text The given text. - */ - setPlaceHolder: function (text) { - if (text !== null) { - //Note: the following calling order matters - this._nativeControl.setPlaceHolder(text); - this._placeholderText = text; - } - }, - - /** - * Set the placeholder's font. - * @param {String} fontName - * @param {Number} fontSize - */ - setPlaceholderFont: function (fontName, fontSize) { - this._placeholderFontName = fontName; - this._placeholderFontSize = fontSize; - this._nativeControl._updateDOMPlaceholderFontStyle(); - }, - - _setPlaceholderFont: function (fontStyle) { - var res = cc.LabelTTF._fontStyleRE.exec(fontStyle); - if (res) { - this._placeholderFontName = res[2]; - this._placeholderFontSize = parseInt(res[1]); - this._nativeControl._updateDOMPlaceholderFontStyle(); - } - }, - - /** - * Set the placeholder's fontName. - * @param {String} fontName - */ - setPlaceholderFontName: function (fontName) { - this._placeholderFontName = fontName; - this._nativeControl._updateDOMPlaceholderFontStyle(); - }, - - /** - * Set the placeholder's fontSize. - * @param {Number} fontSize - */ - setPlaceholderFontSize: function (fontSize) { - this._placeholderFontSize = fontSize; - this._nativeControl._updateDOMPlaceholderFontStyle(); - }, - - /** - * Set the font color of the placeholder text when the edit box is empty. - * @param {cc.Color} color - */ - setPlaceholderFontColor: function (color) { - this._placeholderColor = color; - this._nativeControl.setPlaceholderFontColor(color); - }, - - /** - * Set the input flags that are to be applied to the edit box. - * @param {cc.EditBox.InputFlag} inputFlag - One of the cc.EditBox.InputFlag constants. - * e.g.cc.EditBox.InputFlag..PASSWORD - */ - setInputFlag: function (inputFlag) { - this._editBoxInputFlag = inputFlag; - this._nativeControl.setInputFlag(inputFlag); - }, - - /** - * Gets the input string of the edit box. - * @deprecated - * @return {string} - */ - getText: function () { - cc.log('Please use the getString'); - return this.getString(); - }, - - /** - * Gets the input string of the edit box. - * @return {string} - */ - getString: function () { - return this._text; - }, - - /** - * Init edit box with specified size. - * @param {cc.Size} size - * @param {cc.Color | cc.Scale9Sprite} normal9SpriteBg - */ - initWithSizeAndBackgroundSprite: function (size, normal9SpriteBg) { - if(this._backgroundSprite) { - this._backgroundSprite.removeFromParent(); - } - this._backgroundSprite = normal9SpriteBg; - - if(this._backgroundSprite && !this._backgroundSprite.parent) { - this._backgroundSprite.setAnchorPoint(cc.p(0, 0)); - this.addChild(this._backgroundSprite); - - this._updateBackgroundSpriteSize(size.width, size.height); - } + canvasRenderCmdProto.transform = function (parentCmd, recursive) { + _ccsg.Node.CanvasRenderCmd.prototype.transform.call(this, parentCmd, recursive); + this.updateMatrix(); + }; - this.x = 0; - this.y = 0; - return true; - }, + //define the webgl render command + _ccsg.EditBox.WebGLRenderCmd = function (node) { + _ccsg.Node.WebGLRenderCmd.call(this, node); + this.initializeRenderCmd(node); + }; - /* override functions */ - /** - * Set the delegate for edit box. - * @param {cc.EditBoxDelegate} delegate - */ - setDelegate: function (delegate) { - this._delegate = delegate; - }, + var webGLRenderCmdProto = _ccsg.EditBox.WebGLRenderCmd.prototype = Object.create(_ccsg.Node.WebGLRenderCmd.prototype); + cc.js.mixin(webGLRenderCmdProto, proto); + webGLRenderCmdProto.constructor = _ccsg.EditBox.WebGLRenderCmd; - /** - * Get a text in the edit box that acts as a placeholder when an - * edit box is empty. - * @return {String} - */ - getPlaceHolder: function () { - return this._placeholderText; - }, + webGLRenderCmdProto.transform = function (parentCmd, recursive) { + _ccsg.Node.WebGLRenderCmd.prototype.transform.call(this, parentCmd, recursive); + this.updateMatrix(); + }; - /** - * Set the input mode of the edit box. - * @param {Number} inputMode One of the EditBoxInputMode constants. - */ - setInputMode: function (inputMode) { - if (this._editBoxInputMode === inputMode) return; - - var oldText = this.getString(); - - this._nativeControl.setInputMode(inputMode); - - this.setString(oldText); - this._editBoxInputMode = inputMode; - this._nativeControl._updateLabelPosition(this.getContentSize()); - }, - - /** - * Set the return type that are to be applied to the edit box. - * @param {Number} returnType One of the CCKeyboardReturnType constants. - */ - setReturnType: function (returnType) { - this._keyboardReturnType = returnType; - }, - - /** - * @warning HTML5 Only - * @param {cc.Size} size - * @param {cc.color} bgColor - */ - initWithBackgroundColor: function (size, bgColor) { - this._edWidth = size.width; - this.dom.style.width = this._edWidth.toString() + 'px'; - this._edHeight = size.height; - this.dom.style.height = this._edHeight.toString() + 'px'; - this.dom.style.backgroundColor = cc.colorToHex(bgColor); - } -}); - -var _p = _ccsg.EditBox.prototype; - -// Extended properties -/** @expose */ -_p.font; -cc.defineGetterSetter(_p, 'font', null, _p._setFont); -/** @expose */ -_p.fontName; -cc.defineGetterSetter(_p, 'fontName', null, _p.setFontName); -/** @expose */ -_p.fontSize; -cc.defineGetterSetter(_p, 'fontSize', null, _p.setFontSize); -/** @expose */ -_p.fontColor; -cc.defineGetterSetter(_p, 'fontColor', null, _p.setFontColor); -/** @expose */ -_p.string; -cc.defineGetterSetter(_p, 'string', _p.getString, _p.setString); -/** @expose */ -_p.maxLength; -cc.defineGetterSetter(_p, 'maxLength', _p.getMaxLength, _p.setMaxLength); -/** @expose */ -_p.placeHolder; -cc.defineGetterSetter(_p, 'placeholder', _p.getPlaceHolder, _p.setPlaceHolder); -/** @expose */ -_p.placeHolderFont; -cc.defineGetterSetter(_p, 'placeholderFont', null, _p._setPlaceholderFont); -/** @expose */ -_p.placeHolderFontName; -cc.defineGetterSetter(_p, 'placeholderFontName', null, _p.setPlaceholderFontName); -/** @expose */ -_p.placeHolderFontSize; -cc.defineGetterSetter(_p, 'placeholderFontSize', null, _p.setPlaceholderFontSize); -/** @expose */ -_p.placeHolderFontColor; -cc.defineGetterSetter(_p, 'placeholderFontColor', null, _p.setPlaceholderFontColor); -/** @expose */ -_p.inputFlag; -cc.defineGetterSetter(_p, 'inputFlag', null, _p.setInputFlag); -/** @expose */ -_p.delegate; -cc.defineGetterSetter(_p, 'delegate', null, _p.setDelegate); -/** @expose */ -_p.inputMode; -cc.defineGetterSetter(_p, 'inputMode', null, _p.setInputMode); -/** @expose */ -_p.returnType; -cc.defineGetterSetter(_p, 'returnType', null, _p.setReturnType); - -_p = null; - -_ccsg.EditBox.InputMode = InputMode; -_ccsg.EditBox.InputFlag = InputFlag; -_ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; +}(_ccsg.EditBox._polyfill)); From bf4d2a1a85a63c41085e7a0c7219d32263eed57a Mon Sep 17 00:00:00 2001 From: zilongshanren Date: Wed, 29 Jun 2016 10:23:59 +0800 Subject: [PATCH 06/19] clean up the CCSGEditBox --- cocos2d/core/editbox/CCSGEditBox.js | 8 -------- 1 file changed, 8 deletions(-) diff --git a/cocos2d/core/editbox/CCSGEditBox.js b/cocos2d/core/editbox/CCSGEditBox.js index 66639c6211..2c4792b8e4 100644 --- a/cocos2d/core/editbox/CCSGEditBox.js +++ b/cocos2d/core/editbox/CCSGEditBox.js @@ -375,11 +375,6 @@ _ccsg.EditBox = _ccsg.Node.extend({ this._renderCmd.setInputFlag(inputFlag); }, - getText: function () { - cc.log('Please use the getString'); - return this.getString(); - }, - getString: function () { return this._text; }, @@ -478,13 +473,10 @@ _ccsg.EditBox.KeyboardReturnType = KeyboardReturnType; (function (polyfill) { var EditBoxImpl = function () { - }; var proto = EditBoxImpl.prototype = Object.create(Object.prototype); - - proto.updateMatrix = function () { if (!this._edTxt) return; var node = this._node, scaleX = cc.view._scaleX, scaleY = cc.view._scaleY; From d7ac3f7fa610cbdeb70f24752ced7b713d3889eb Mon Sep 17 00:00:00 2001 From: knoxHuang <821686174@qq.com> Date: Wed, 29 Jun 2016 15:30:12 +0800 Subject: [PATCH 07/19] fix Follow error, for #fireball/issues/3409 --- cocos2d/actions/CCAction.js | 13 +++++++------ cocos2d/core/CCDirector.js | 6 ++++-- jsb/jsb-action.js | 7 +++++++ jsb/jsb-director.js | 2 +- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/cocos2d/actions/CCAction.js b/cocos2d/actions/CCAction.js index bbb268309c..2ce7ac7d71 100644 --- a/cocos2d/actions/CCAction.js +++ b/cocos2d/actions/CCAction.js @@ -528,19 +528,20 @@ cc.Follow = cc.Action.extend({ }, step:function (dt) { - var tempPosX = this._followedNode.x; - var tempPosY = this._followedNode.y; - tempPosX = this._halfScreenSize.x - tempPosX; - tempPosY = this._halfScreenSize.y - tempPosY; + var targetWorldPos = this.target.convertToWorldSpaceAR(cc.Vec2.ZERO); + var followedWorldPos = this._followedNode.convertToWorldSpaceAR(cc.Vec2.ZERO); + // compute the offset between followed and target node + var delta = cc.pSub(targetWorldPos, followedWorldPos); + var tempPos = this.target.parent.convertToNodeSpaceAR(cc.pAdd(delta, this._halfScreenSize)); if (this._boundarySet) { // whole map fits inside a single screen, no need to modify the position - unless map boundaries are increased if (this._boundaryFullyCovered) return; - this.target.setPosition(cc.clampf(tempPosX, this.leftBoundary, this.rightBoundary), cc.clampf(tempPosY, this.bottomBoundary, this.topBoundary)); + this.target.setPosition(cc.clampf(tempPos.x, this.leftBoundary, this.rightBoundary), cc.clampf(tempPos.y, this.bottomBoundary, this.topBoundary)); } else { - this.target.setPosition(tempPosX, tempPosY); + this.target.setPosition(tempPos.x, tempPos.y); } }, diff --git a/cocos2d/core/CCDirector.js b/cocos2d/core/CCDirector.js index 6d9362cf86..14f5ef5b7e 100644 --- a/cocos2d/core/CCDirector.js +++ b/cocos2d/core/CCDirector.js @@ -209,7 +209,9 @@ cc.Director = Class.extend(/** @lends cc.Director# */{ } // WidgetManager - cc._widgetManager.init(this); + if (cc._widgetManager) { + cc._widgetManager.init(this); + } }, /** @@ -645,7 +647,7 @@ cc.Director = Class.extend(/** @lends cc.Director# */{ } // Delay run / replace scene to the end of the frame - this.once(cc.Director.EVENT_AFTER_DRAW, function () { + this.once(cc.Director.EVENT_AFTER_UPDATE, function () { this.runSceneImmediate(scene, onBeforeLoadScene, onLaunched); }); }, diff --git a/jsb/jsb-action.js b/jsb/jsb-action.js index 496a0d9fa2..f2f0659f6f 100644 --- a/jsb/jsb-action.js +++ b/jsb/jsb-action.js @@ -116,6 +116,13 @@ cc.follow = function (followedNode, rect) { return new cc.Follow(followedNode._sgNode, rect); }; +cc.Follow.prototype.update = function(dt) { + var target = this.getTarget(); + if (target._owner) { + target._owner.setPosition(target.getPosition()); + } +}; + // Special call func cc.callFunc = function (selector, selectorTarget, data) { var callback = function (sender, data) { diff --git a/jsb/jsb-director.js b/jsb/jsb-director.js index 8c44bc2520..ff8e1db0c9 100644 --- a/jsb/jsb-director.js +++ b/jsb/jsb-director.js @@ -203,7 +203,7 @@ cc.js.mixin(cc.director, { } // Delay run / replace scene to the end of the frame - this.once(cc.Director.EVENT_AFTER_DRAW, function () { + this.once(cc.Director.EVENT_AFTER_UPDATE, function () { this.runSceneImmediate(scene, onBeforeLoadScene, onLaunched); }); }, From 1e8af79ece89bcfbb7fbf7018ad0813ad37eacc7 Mon Sep 17 00:00:00 2001 From: Nan Wang Date: Wed, 29 Jun 2016 15:56:13 +0800 Subject: [PATCH 08/19] update version --- CCBoot.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CCBoot.js b/CCBoot.js index 7bee5c5321..6cb886b6ce 100644 --- a/CCBoot.js +++ b/CCBoot.js @@ -58,7 +58,7 @@ _p = null; * @name cc.ENGINE_VERSION */ var engineVersion; -engineVersion = '1.1.2-beta.1'; +engineVersion = '1.1.2-rc.1'; window['CocosEngine'] = cc.ENGINE_VERSION = engineVersion; /** From 336171daacaf049afedee6ea8e6fde16c655f315 Mon Sep 17 00:00:00 2001 From: VisualSJ Date: Wed, 29 Jun 2016 16:02:14 +0800 Subject: [PATCH 09/19] Fixed audio source rewind --- cocos2d/core/components/CCAudioSource.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cocos2d/core/components/CCAudioSource.js b/cocos2d/core/components/CCAudioSource.js index 7dfa76761a..adb99c13a7 100644 --- a/cocos2d/core/components/CCAudioSource.js +++ b/cocos2d/core/components/CCAudioSource.js @@ -249,8 +249,8 @@ var AudioSource = cc.Class({ */ rewind: function(){ if ( this.audio ) { - cc.audioEngine.stopEffect(this.audio); - cc.audioEngine.playEffect(this.audio); + this.stop(); + this.play(); } }, From e6ae96d53eb11726807b1042d1bad41db554c66f Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Wed, 29 Jun 2016 17:15:11 +0800 Subject: [PATCH 10/19] clear debug draw when disable collision debug draw --- cocos2d/core/collider/CCCollisionManager.js | 1 + 1 file changed, 1 insertion(+) diff --git a/cocos2d/core/collider/CCCollisionManager.js b/cocos2d/core/collider/CCCollisionManager.js index 71121113b2..f1ad0298b8 100644 --- a/cocos2d/core/collider/CCCollisionManager.js +++ b/cocos2d/core/collider/CCCollisionManager.js @@ -385,6 +385,7 @@ cc.js.getset(CollisionManager.prototype, 'enabledDebugDraw', cc.director.on(cc.Director.EVENT_AFTER_SCENE_LAUNCH, this.onSceneLaunched, this); } else if (!value && this._enabledDebugDraw) { + this._debugDrawer.clear(); cc.director.getScene()._sgNode.removeChild(this._debugDrawer); cc.director.off(cc.Director.EVENT_AFTER_SCENE_LAUNCH, this.onSceneLaunched, this); } From 787eb2203931e88c44557ff50937a75cd8063100 Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Wed, 29 Jun 2016 18:41:02 +0800 Subject: [PATCH 11/19] try to fix scene error of example --- cocos2d/core/platform/deserialize.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/cocos2d/core/platform/deserialize.js b/cocos2d/core/platform/deserialize.js index 2ec34f902a..909c238f00 100644 --- a/cocos2d/core/platform/deserialize.js +++ b/cocos2d/core/platform/deserialize.js @@ -217,10 +217,13 @@ var _Deserializer = (function () { var _dereference = function (self) { // 这里不采用遍历反序列化结果的方式,因为反序列化的结果如果引用到复杂的外部库,很容易堆栈溢出。 var deserializedList = self.deserializedList; + var idPropList = self._idPropList; + var idList = self._idList; + var idObjList = self._idObjList; for (var i = 0, len = self._idList.length; i < len; i++) { - var propName = self._idPropList[i]; - var id = self._idList[i]; - self._idObjList[i][propName] = deserializedList[id]; + var propName = idPropList[i]; + var id = idList[i]; + idObjList[i][propName] = deserializedList[id]; } }; @@ -387,8 +390,8 @@ var _Deserializer = (function () { } } if (props[props.length - 1] === '_$erialized') { - // save original serialized data - obj._$erialized = serialized; + // deep copy original serialized data + obj._$erialized = JSON.parse(JSON.stringify(serialized)); // parse the serialized data as primitive javascript object, so its __id__ will be dereferenced _deserializePrimitiveObject(self, obj._$erialized, serialized); } @@ -523,6 +526,8 @@ cc.deserialize = function (data, result, options) { data = JSON.parse(data); } + //var oldJson = JSON.stringify(data, null, 2); + if (createAssetRefs && !result) { result = new Details(); } @@ -534,6 +539,11 @@ cc.deserialize = function (data, result, options) { result.assignAssetsBy(Editor.serialize.asAsset); } + //var afterJson = JSON.stringify(data, null, 2); + //if (oldJson !== afterJson) { + // throw new Error('JSON SHOULD not changed'); + //} + return deserializer.deserializedData; }; From ea25b55057b440ce2fd96fd6b74d610c1929d6b9 Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Thu, 30 Jun 2016 00:25:50 +0800 Subject: [PATCH 12/19] fix scene reloading --- cocos2d/core/load-pipeline/pack-downloader.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/cocos2d/core/load-pipeline/pack-downloader.js b/cocos2d/core/load-pipeline/pack-downloader.js index 66a9865c33..d33a0417c7 100644 --- a/cocos2d/core/load-pipeline/pack-downloader.js +++ b/cocos2d/core/load-pipeline/pack-downloader.js @@ -96,13 +96,16 @@ module.exports = { var unpacker = globalUnpackers[packUuid]; if (unpacker) { - var json = unpacker.retrieve(uuid); - if (json) { - callback(null, json); - } - else { - error(callback, uuid, packUuid); - } + // ensure async + setTimeout(function () { + var json = unpacker.retrieve(uuid); + if (json) { + callback(null, json); + } + else { + error(callback, uuid, packUuid); + } + }, 0); } else { this._loadNewPack(uuid, packUuid, callback); From f12f6c06ac10cea359f9250f47d6f91c865c89d8 Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Thu, 30 Jun 2016 00:52:52 +0800 Subject: [PATCH 13/19] try to fix glitter when scene loading --- cocos2d/core/CCDirector.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cocos2d/core/CCDirector.js b/cocos2d/core/CCDirector.js index 14f5ef5b7e..d321aacffa 100644 --- a/cocos2d/core/CCDirector.js +++ b/cocos2d/core/CCDirector.js @@ -625,7 +625,7 @@ cc.Director = Class.extend(/** @lends cc.Director# */{ if (onLaunched) { onLaunched(null, scene); } - cc.renderer.clear(); + //cc.renderer.clear(); this.emit(cc.Director.EVENT_AFTER_SCENE_LAUNCH, scene); }, From e586c7c00e411e5ff049dc37491fbe295a238ea5 Mon Sep 17 00:00:00 2001 From: Nan Wang Date: Thu, 30 Jun 2016 11:59:14 +0800 Subject: [PATCH 14/19] change widget.alignOnce to true as default --- CCBoot.js | 2 +- cocos2d/core/components/CCWidget.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CCBoot.js b/CCBoot.js index 6cb886b6ce..ebb0d6f42d 100644 --- a/CCBoot.js +++ b/CCBoot.js @@ -58,7 +58,7 @@ _p = null; * @name cc.ENGINE_VERSION */ var engineVersion; -engineVersion = '1.1.2-rc.1'; +engineVersion = '1.1.2'; window['CocosEngine'] = cc.ENGINE_VERSION = engineVersion; /** diff --git a/cocos2d/core/components/CCWidget.js b/cocos2d/core/components/CCWidget.js index 9adc7bcbbb..0b26508888 100644 --- a/cocos2d/core/components/CCWidget.js +++ b/cocos2d/core/components/CCWidget.js @@ -390,7 +390,7 @@ var Widget = cc.Class({ * @default false */ isAlignOnce: { - default: false, + default: true, tooltip: 'i18n:COMPONENT.widget.align_once', displayName: "AlignOnce" }, From b4060e396af888822aba539158a8d9abcec385ee Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Thu, 30 Jun 2016 15:00:45 +0800 Subject: [PATCH 15/19] simplify clipping Node, support nest in canvas --- .../CCClippingNodeCanvasRenderCmd.js | 113 +++++------------- 1 file changed, 28 insertions(+), 85 deletions(-) diff --git a/cocos2d/clipping-nodes/CCClippingNodeCanvasRenderCmd.js b/cocos2d/clipping-nodes/CCClippingNodeCanvasRenderCmd.js index c8751a2e4a..e111096d84 100644 --- a/cocos2d/clipping-nodes/CCClippingNodeCanvasRenderCmd.js +++ b/cocos2d/clipping-nodes/CCClippingNodeCanvasRenderCmd.js @@ -27,11 +27,9 @@ cc.ClippingNode.CanvasRenderCmd = function(renderable){ _ccsg.Node.CanvasRenderCmd.call(this, renderable); this._needDraw = false; - this._godhelpme = false; this._clipElemType = false; - this._rendererSaveCmd = new cc.CustomRenderCmd(this, this._saveCmdCallback); - this._rendererClipCmd = new cc.CustomRenderCmd(this, this._clipCmdCallback); + this._rendererClipCmd = new cc.CustomRenderCmd(this, this._drawStencilCommand); this._rendererRestoreCmd = new cc.CustomRenderCmd(this, this._restoreCmdCallback); }; var proto = cc.ClippingNode.CanvasRenderCmd.prototype = Object.create(_ccsg.Node.CanvasRenderCmd.prototype); @@ -75,34 +73,8 @@ proto.setStencil = function(stencil){ } }; }else{ - stencil._parent = this._node; - } -}; - -proto._saveCmdCallback = function(ctx, scaleX, scaleY) { - var wrapper = ctx || cc._renderContext, context = wrapper.getContext(); - - if (this._clipElemType) { - context.save(); - context.globalAlpha = this._displayedOpacity/ 255; - var locCache = cc.ClippingNode.CanvasRenderCmd._getSharedCache(); - var canvas = context.canvas; - if (locCache.width !== canvas.width) - locCache.width = canvas.width; - if (locCache.height !== canvas.height) - locCache.height = canvas.height; //note: on some browser, it can't clear the canvas, e.g. baidu - var locCacheCtx = locCache.getContext("2d"); - locCacheCtx.drawImage(canvas, 0, 0); //save the result to shareCache canvas - } else { - wrapper.save(); - context.beginPath(); //save for clip - //Because drawNode's content size is zero - wrapper.setTransform(this._worldTransform, scaleX, scaleY); - - if (this._node.inverted) { - context.rect(0, 0, context.canvas.width, -context.canvas.height); - context.clip(); - } + cc.error('do not support other type as stencil'); + //stencil._parent = this._node; } }; @@ -121,33 +93,10 @@ proto._setStencilCompositionOperation = function(stencil){ } }; -proto._clipCmdCallback = function(ctx) { - var node = this._node; - var wrapper = ctx || cc._renderContext, context = wrapper.getContext(); - - if (this._clipElemType) { - //hack - this._setStencilCompositionOperation(node._stencil); - } else { - context.clip(); - } -}; - proto._restoreCmdCallback = function (ctx) { var locCache = cc.ClippingNode.CanvasRenderCmd._getSharedCache(); var wrapper = ctx || cc._renderContext, context = wrapper.getContext(); - if (this._clipElemType) { - // Redraw the cached canvas, so that the clipped area shows the background etc. - context.save(); - context.setTransform(1, 0, 0, 1, 0, 0); - context.globalCompositeOperation = "destination-over"; - context.drawImage(locCache, 0, 0); - context.restore(); - this._dirtyFlag = 0; - context.restore(); - } else { - wrapper.restore(); //use for restore clip operation - } + wrapper.restore(); }; proto.transform = function(parentCmd, recursive){ @@ -157,10 +106,24 @@ proto.transform = function(parentCmd, recursive){ node._stencil._renderCmd.transform(this, recursive); }; -proto._cangodhelpme = function (godhelpme) { - if (godhelpme === true || godhelpme === false) - cc.ClippingNode.CanvasRenderCmd.prototype._godhelpme = godhelpme; - return cc.ClippingNode.CanvasRenderCmd.prototype._godhelpme; +proto._drawStencilCommand = function (ctx, scaleX, scaleY) { + var wrapper = ctx || cc._renderContext, context = wrapper.getContext(); + wrapper.save(); + context.beginPath(); //save for clip + wrapper.setTransform(this._worldTransform, scaleX, scaleY); + + //draw elements + var stencilBuffer = this._node._stencil._buffer; + for(var index = 0; index < stencilBuffer.length; ++index) { + var vertices = stencilBuffer[index].verts; + if(vertices.length < 3) continue; + context.moveTo(vertices[0].x * scaleX, -vertices[0].y * scaleY); + for(var vIndex = 1; vIndex < vertices.length; ++vIndex) { + context.lineTo(vertices[vIndex].x * scaleX, -vertices[vIndex].y * scaleY); + } + } + //end draw elements + context.clip(); }; proto.visit = function(parentCmd){ @@ -172,43 +135,23 @@ proto.visit = function(parentCmd){ parentCmd = parentCmd || this.getParentRenderCmd(); if( parentCmd) this._curLevel = parentCmd._curLevel + 1; - var transformRenderCmd = this; - - // Composition mode, costy but support texture stencil - this._clipElemType = !(!this._cangodhelpme() && node._stencil instanceof cc.DrawNode); - if (!node._stencil || !node._stencil.visible) { - if (this.inverted) - _ccsg.Node.CanvasRenderCmd.prototype.visit.call(this, parentCmd); // draw everything - return; - } this._syncStatus(parentCmd); - cc.renderer.pushRenderCommand(this._rendererSaveCmd); - if(this._clipElemType){ - // Draw everything first using node visit function - _ccsg.Node.CanvasRenderCmd.prototype.visit.call(this, parentCmd); - }else{ - node._stencil.visit(this); + if(this._node._stencil) { + cc.renderer.pushRenderCommand(this._rendererClipCmd); } - cc.renderer.pushRenderCommand(this._rendererClipCmd); - - if(this._clipElemType){ - node._stencil.visit(transformRenderCmd); - }else{ var i, children = node._children; - // Clip mode doesn't support recursive stencil, so once we used a clip stencil, - // so if it has ClippingNode as a child, the child must uses composition stencil. - this._cangodhelpme(true); + var len = children.length; if (len > 0) { node.sortAllChildren(); for (i = 0; i < len; i++) children[i]._renderCmd.visit(this); } - this._cangodhelpme(false); - } - cc.renderer.pushRenderCommand(this._rendererRestoreCmd); + if(this._node._stencil) { + cc.renderer.pushRenderCommand(this._rendererRestoreCmd); + } this._dirtyFlag = 0; }; From 857aa85c8a99c8a7961e05043314e1a7e2e647e6 Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Thu, 30 Jun 2016 17:17:08 +0800 Subject: [PATCH 16/19] fix unpacking on mobile --- .../core/load-pipeline/binary-downloader.js | 2 +- cocos2d/core/load-pipeline/downloader.js | 71 ++----------------- cocos2d/core/load-pipeline/pack-downloader.js | 3 +- cocos2d/core/load-pipeline/text-downloader.js | 43 +++++++++++ cocos2d/core/load-pipeline/utils.js | 26 +++++++ 5 files changed, 76 insertions(+), 69 deletions(-) create mode 100644 cocos2d/core/load-pipeline/text-downloader.js create mode 100644 cocos2d/core/load-pipeline/utils.js diff --git a/cocos2d/core/load-pipeline/binary-downloader.js b/cocos2d/core/load-pipeline/binary-downloader.js index d16ae535ee..dd737f9890 100644 --- a/cocos2d/core/load-pipeline/binary-downloader.js +++ b/cocos2d/core/load-pipeline/binary-downloader.js @@ -113,6 +113,6 @@ function downloadBinary (url, callback) { }; } xhr.send(null); -}; +} module.exports = downloadBinary; \ No newline at end of file diff --git a/cocos2d/core/load-pipeline/downloader.js b/cocos2d/core/load-pipeline/downloader.js index 4616d87550..91670920d4 100644 --- a/cocos2d/core/load-pipeline/downloader.js +++ b/cocos2d/core/load-pipeline/downloader.js @@ -26,6 +26,10 @@ var JS = require('../platform/js'); var Path = require('../utils/CCPath'); var Pipeline = require('./pipeline'); var PackDownloader = require('./pack-downloader'); +// var downloadBinary = require('./binary-downloader'); +var downloadText = require('./text-downloader'); + +var urlAppendTimestamp = require('./utils').urlAppendTimestamp; var downloadAudio; if (!CC_EDITOR || !Editor.isMainProcess) { @@ -34,32 +38,6 @@ if (!CC_EDITOR || !Editor.isMainProcess) { else { downloadAudio = null; } -// var downloadBinary = require('binary-downloader'); - -function isUrlCrossOrigin (url) { - if (!url) { - cc.log('invalid URL'); - return false; - } - var startIndex = url.indexOf('://'); - if (startIndex === -1) - return false; - - var endIndex = url.indexOf('/', startIndex + 3); - var urlOrigin = (endIndex === -1) ? url : url.substring(0, endIndex); - return urlOrigin !== location.origin; -} - -var _noCacheRex = /\?/; -function urlAppendTimestamp (url) { - if (cc.game.config['noCache'] && typeof url === 'string') { - if(_noCacheRex.test(url)) - url += '&_t=' + (new Date() - 0); - else - url += '?_t=' + (new Date() - 0); - } - return url; -} function downloadScript (item, callback, isAsync) { var url = item.url, @@ -84,47 +62,6 @@ function downloadScript (item, callback, isAsync) { d.body.appendChild(s); } -function downloadText (item, callback) { - var url = item.url, - xhr = Pipeline.getXMLHttpRequest(), - errInfo = 'Load ' + url + ' failed!', - navigator = window.navigator; - - url = urlAppendTimestamp(url); - - xhr.open('GET', url, true); - if (/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent)) { - // IE-specific logic here - xhr.setRequestHeader('Accept-Charset', 'utf-8'); - xhr.onreadystatechange = function () { - if(xhr.readyState === 4) { - if (xhr.status === 200 || xhr.status === 0) { - callback(null, xhr.responseText); - } - else { - callback({status:xhr.status, errorMessage:errInfo}); - } - } - }; - } else { - if (xhr.overrideMimeType) xhr.overrideMimeType('text\/plain; charset=utf-8'); - xhr.onload = function () { - if(xhr.readyState === 4) { - if (xhr.status === 200 || xhr.status === 0) { - callback(null, xhr.responseText); - } - else { - callback({status:xhr.status, errorMessage:errInfo}); - } - } - }; - xhr.onerror = function(){ - callback({status:xhr.status, errorMessage:errInfo}); - }; - } - xhr.send(null); -} - function downloadTextSync (item) { var url = item.url; var xhr = Pipeline.getXMLHttpRequest(); diff --git a/cocos2d/core/load-pipeline/pack-downloader.js b/cocos2d/core/load-pipeline/pack-downloader.js index d33a0417c7..d8a36a8698 100644 --- a/cocos2d/core/load-pipeline/pack-downloader.js +++ b/cocos2d/core/load-pipeline/pack-downloader.js @@ -24,6 +24,7 @@ ****************************************************************************/ var JsonUnpacker = require('./json-unpacker'); +var downloadText = require('./text-downloader'); // {assetUuid: packUuid} var uuidToPack = {}; @@ -55,7 +56,7 @@ module.exports = { _loadNewPack: function (uuid, packUuid, callback) { var packUrl = cc.AssetLibrary.getImportedDir(packUuid) + '/' + packUuid + '.json'; - cc.loader.load(packUrl, function (err, packJson) { + downloadText({ url: packUrl }, function (err, packJson) { if (err) { cc.error('Failed to download package for ' + uuid); return callback(err); diff --git a/cocos2d/core/load-pipeline/text-downloader.js b/cocos2d/core/load-pipeline/text-downloader.js new file mode 100644 index 0000000000..ee27dff13c --- /dev/null +++ b/cocos2d/core/load-pipeline/text-downloader.js @@ -0,0 +1,43 @@ +var Pipeline = require('./pipeline'); +var urlAppendTimestamp = require('./utils').urlAppendTimestamp; + +module.exports = function (item, callback) { + var url = item.url, + xhr = Pipeline.getXMLHttpRequest(), + errInfo = 'Load ' + url + ' failed!', + navigator = window.navigator; + + url = urlAppendTimestamp(url); + + xhr.open('GET', url, true); + if (/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent)) { + // IE-specific logic here + xhr.setRequestHeader('Accept-Charset', 'utf-8'); + xhr.onreadystatechange = function () { + if(xhr.readyState === 4) { + if (xhr.status === 200 || xhr.status === 0) { + callback(null, xhr.responseText); + } + else { + callback({status:xhr.status, errorMessage:errInfo}); + } + } + }; + } else { + if (xhr.overrideMimeType) xhr.overrideMimeType('text\/plain; charset=utf-8'); + xhr.onload = function () { + if(xhr.readyState === 4) { + if (xhr.status === 200 || xhr.status === 0) { + callback(null, xhr.responseText); + } + else { + callback({status:xhr.status, errorMessage:errInfo}); + } + } + }; + xhr.onerror = function(){ + callback({status:xhr.status, errorMessage:errInfo}); + }; + } + xhr.send(null); +}; diff --git a/cocos2d/core/load-pipeline/utils.js b/cocos2d/core/load-pipeline/utils.js new file mode 100644 index 0000000000..14a721b909 --- /dev/null +++ b/cocos2d/core/load-pipeline/utils.js @@ -0,0 +1,26 @@ +var _noCacheRex = /\?/; + +module.exports = { + //isUrlCrossOrigin: function (url) { + // if (!url) { + // cc.log('invalid URL'); + // return false; + // } + // var startIndex = url.indexOf('://'); + // if (startIndex === -1) + // return false; + // + // var endIndex = url.indexOf('/', startIndex + 3); + // var urlOrigin = (endIndex === -1) ? url : url.substring(0, endIndex); + // return urlOrigin !== location.origin; + //}, + urlAppendTimestamp: function (url) { + if (cc.game.config['noCache'] && typeof url === 'string') { + if(_noCacheRex.test(url)) + url += '&_t=' + (new Date() - 0); + else + url += '?_t=' + (new Date() - 0); + } + return url; + } +}; From f086ac14ce9e2235404b7769a49f0518268bc832 Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Thu, 30 Jun 2016 18:29:31 +0800 Subject: [PATCH 17/19] fix cc.clone on JSB --- cocos2d/core/platform/_CCClass.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cocos2d/core/platform/_CCClass.js b/cocos2d/core/platform/_CCClass.js index 489edf9486..55eba261ee 100644 --- a/cocos2d/core/platform/_CCClass.js +++ b/cocos2d/core/platform/_CCClass.js @@ -203,8 +203,10 @@ cc.clone = function (obj) { for (var key in obj) { var copy = obj[key]; // Beware that typeof null == "object" ! - if (((typeof copy) === "object") && copy && - !(copy instanceof _ccsg.Node) && !(copy instanceof HTMLElement)) { + if (typeof copy === "object" && + copy && + !(copy instanceof _ccsg.Node) && + (CC_JSB || !(copy instanceof HTMLElement))) { newObj[key] = cc.clone(copy); } else { newObj[key] = copy; From 5dcff33b98fe560eb50d8687c5664b2dfe2d632e Mon Sep 17 00:00:00 2001 From: Jare Guo Date: Thu, 30 Jun 2016 21:52:14 +0800 Subject: [PATCH 18/19] fix pack loader on JSB --- cocos2d/core/load-pipeline/downloader.js | 2 +- cocos2d/core/load-pipeline/text-downloader.js | 97 +++++++++++-------- jsb/jsb-loader.js | 30 ------ 3 files changed, 57 insertions(+), 72 deletions(-) diff --git a/cocos2d/core/load-pipeline/downloader.js b/cocos2d/core/load-pipeline/downloader.js index 91670920d4..c570eed262 100644 --- a/cocos2d/core/load-pipeline/downloader.js +++ b/cocos2d/core/load-pipeline/downloader.js @@ -324,7 +324,7 @@ JS.mixin(Downloader.prototype, { * @param {Object} extMap Custom supported types with corresponded handler */ addHandlers: function (extMap) { - this.extMap = JS.mixin(this.extMap, extMap); + JS.mixin(this.extMap, extMap); }, handle: function (item, callback) { diff --git a/cocos2d/core/load-pipeline/text-downloader.js b/cocos2d/core/load-pipeline/text-downloader.js index ee27dff13c..6090c9934b 100644 --- a/cocos2d/core/load-pipeline/text-downloader.js +++ b/cocos2d/core/load-pipeline/text-downloader.js @@ -1,43 +1,58 @@ -var Pipeline = require('./pipeline'); -var urlAppendTimestamp = require('./utils').urlAppendTimestamp; +if (CC_JSB) { + module.exports = function (item, callback) { + var url = item.url; -module.exports = function (item, callback) { - var url = item.url, - xhr = Pipeline.getXMLHttpRequest(), - errInfo = 'Load ' + url + ' failed!', - navigator = window.navigator; - - url = urlAppendTimestamp(url); - - xhr.open('GET', url, true); - if (/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent)) { - // IE-specific logic here - xhr.setRequestHeader('Accept-Charset', 'utf-8'); - xhr.onreadystatechange = function () { - if(xhr.readyState === 4) { - if (xhr.status === 200 || xhr.status === 0) { - callback(null, xhr.responseText); - } - else { - callback({status:xhr.status, errorMessage:errInfo}); - } - } - }; - } else { - if (xhr.overrideMimeType) xhr.overrideMimeType('text\/plain; charset=utf-8'); - xhr.onload = function () { - if(xhr.readyState === 4) { - if (xhr.status === 200 || xhr.status === 0) { - callback(null, xhr.responseText); - } - else { - callback({status:xhr.status, errorMessage:errInfo}); - } - } - }; - xhr.onerror = function(){ - callback({status:xhr.status, errorMessage:errInfo}); - }; + var result = jsb.fileUtils.getStringFromFile(url); + if (typeof result === 'string' && result) { + callback(null, result); + } + else { + callback(new Error('Download text failed: ' + url)); + } } - xhr.send(null); -}; +} +else { + var Pipeline = require('./pipeline'); + var urlAppendTimestamp = require('./utils').urlAppendTimestamp; + + module.exports = function (item, callback) { + var url = item.url, + xhr = Pipeline.getXMLHttpRequest(), + errInfo = 'Load ' + url + ' failed!', + navigator = window.navigator; + + url = urlAppendTimestamp(url); + + xhr.open('GET', url, true); + if (/msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent)) { + // IE-specific logic here + xhr.setRequestHeader('Accept-Charset', 'utf-8'); + xhr.onreadystatechange = function () { + if(xhr.readyState === 4) { + if (xhr.status === 200 || xhr.status === 0) { + callback(null, xhr.responseText); + } + else { + callback({status:xhr.status, errorMessage:errInfo}); + } + } + }; + } else { + if (xhr.overrideMimeType) xhr.overrideMimeType('text\/plain; charset=utf-8'); + xhr.onload = function () { + if(xhr.readyState === 4) { + if (xhr.status === 200 || xhr.status === 0) { + callback(null, xhr.responseText); + } + else { + callback({status:xhr.status, errorMessage:errInfo}); + } + } + }; + xhr.onerror = function(){ + callback({status:xhr.status, errorMessage:errInfo}); + }; + } + xhr.send(null); + }; +} diff --git a/jsb/jsb-loader.js b/jsb/jsb-loader.js index f68037f04a..48624cf54d 100644 --- a/jsb/jsb-loader.js +++ b/jsb/jsb-loader.js @@ -36,18 +36,6 @@ function downloadScript (item, callback) { callback(null, url); } -function downloadText (item, callback) { - var url = item.url; - - var result = jsb.fileUtils.getStringFromFile(url); - if (typeof result === 'string' && result) { - callback(null, result); - } - else { - callback(new Error('Download text failed: ' + url)); - } -} - function downloadAudio (item, callback) { callback(null, item.url); } @@ -75,22 +63,6 @@ cc.loader.addDownloadHandlers({ 'mp4' : downloadAudio, 'm4a' : downloadAudio, - // Txt - 'txt' : downloadText, - 'xml' : downloadText, - 'vsh' : downloadText, - 'fsh' : downloadText, - 'atlas' : downloadText, - - 'tmx' : downloadText, - 'tsx' : downloadText, - - 'json' : downloadText, - 'ExportJson' : downloadText, - 'plist' : downloadText, - - 'fnt' : downloadText, - // Font 'font' : empty, 'eot' : empty, @@ -98,8 +70,6 @@ cc.loader.addDownloadHandlers({ 'woff' : empty, 'svg' : empty, 'ttc' : empty, - - 'default' : downloadText }); From f0b5a2116590f5b022192ecea76f6188db1b123b Mon Sep 17 00:00:00 2001 From: 2youyouo2 <501251991@qq.com> Date: Fri, 1 Jul 2016 10:36:57 +0800 Subject: [PATCH 19/19] update AnimationClip doc --- docs/cocos2d/core/animation-clip/event-data.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/cocos2d/core/animation-clip/event-data.js b/docs/cocos2d/core/animation-clip/event-data.js index 9c921b8c86..5d0e54c30d 100644 --- a/docs/cocos2d/core/animation-clip/event-data.js +++ b/docs/cocos2d/core/animation-clip/event-data.js @@ -1,3 +1,6 @@ +// frame : The exactly time in second. +// func : Callback function name +// params : Callback parameters [ { frame: 0, func: 'onAnimationEvent1', params:['param-1', 'param-2'] }, { frame: 2, func: 'onAnimationEvent3', params:['param-1', 'param-2'] },