diff --git a/CCBoot.js b/CCBoot.js index 7bee5c5321..ebb0d6f42d 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'; window['CocosEngine'] = cc.ENGINE_VERSION = engineVersion; /** 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 c69217cdc9..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); }, @@ -647,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/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); } 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(); } }, 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/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" }, diff --git a/cocos2d/core/editbox/CCSGEditBox.js b/cocos2d/core/editbox/CCSGEditBox.js index 21fba5b8e7..2c4792b8e4 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 @@ -195,45 +195,472 @@ cc.EditBoxDelegate = cc._Class.extend({ } }); -var EditBoxImpl = function(editBox) { - this._domInputSprite = null; - 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) { - var tmpDOMSprite = this._domInputSprite = new _ccsg.Sprite(); - tmpDOMSprite.draw = function () {}; //redefine draw function - this._editBox.addChild(tmpDOMSprite); - this._spriteDOM = tmpDOMSprite; + ctor: function (size, normal9SpriteBg) { + _ccsg.Node.prototype.ctor.call(this); - this._createDomTextArea(); + this._textColor = cc.Color.WHITE; + this._placeholderColor = cc.Color.GRAY; - 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'; - - tmpDOMSprite.canvas.remove(); - - 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); + }, + + 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); @@ -247,9 +674,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(); @@ -269,18 +696,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(); @@ -291,9 +718,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') { @@ -307,9 +734,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); @@ -319,86 +746,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 @@ -413,9 +825,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(); @@ -426,13 +838,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; @@ -456,9 +866,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'; @@ -469,606 +879,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; - }, - - _removeDomInputControl: function() { - this._domInputSprite.dom.removeChild(this._edTxt); - }, - - _addDomInputControl: function () { - this._domInputSprite.dom.appendChild(this._edTxt); - }, -}; - -/** - *

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, - _nativeControl: null, - - _text: '', - _textColor: null, - - _placeholderText: '', - _placeholderFontName: '', - _placeholderFontSize: 14, - _placeholderColor: null, - - _adjustHeight: 18, - _tooltip: false, - _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); - - 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; - 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; - }, - - 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 - * @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; - -/** - * 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; +}(_ccsg.EditBox._polyfill)); 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()); - } -}; 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..c570eed262 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(); @@ -387,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/pack-downloader.js b/cocos2d/core/load-pipeline/pack-downloader.js index 66a9865c33..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); @@ -96,13 +97,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); diff --git a/cocos2d/core/load-pipeline/text-downloader.js b/cocos2d/core/load-pipeline/text-downloader.js new file mode 100644 index 0000000000..6090c9934b --- /dev/null +++ b/cocos2d/core/load-pipeline/text-downloader.js @@ -0,0 +1,58 @@ +if (CC_JSB) { + module.exports = function (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)); + } + } +} +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/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; + } +}; diff --git a/cocos2d/core/platform/CCSys.js b/cocos2d/core/platform/CCSys.js index f57e7239a1..e2ae00b442 100644 --- a/cocos2d/core/platform/CCSys.js +++ b/cocos2d/core/platform/CCSys.js @@ -772,6 +772,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'; diff --git a/cocos2d/core/platform/_CCClass.js b/cocos2d/core/platform/_CCClass.js index e63979fac9..941312f56c 100644 --- a/cocos2d/core/platform/_CCClass.js +++ b/cocos2d/core/platform/_CCClass.js @@ -202,8 +202,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; 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; }; 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'] }, 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; 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); }); }, 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 });