Files
cocos4/cocos2d/core/editbox/CCSGEditBox.js
T
zilongshanren 0a6293bad0 Improve EditBox
Squashed commit of the following:

commit ad31d2beadceea31732970145238a68a97995b76
Merge: 8e15189 061e6ca
Author: zilongshanren <guanghui8827@126.com>
Date:   Tue Feb 23 16:51:09 2016 +0800

    Merge remote-tracking branch 'me/master'

commit 8e15189d4a37d7215ffb66d6c08ec0b79cc57a19
Merge: 7c233f0 968480e
Author: zilongshanren <guanghui8827@126.com>
Date:   Tue Feb 23 16:50:19 2016 +0800

    Merge remote-tracking branch 'fireball/master'

commit 7c233f03db2a78b1ab2c5866520efef04a6c4920
Author: zilongshanren <guanghui8827@126.com>
Date:   Tue Feb 23 16:47:25 2016 +0800

    minor fix

commit fc89b4f920f979497e5961e48c1c796e18c3aa8e
Author: zilongshanren <guanghui8827@126.com>
Date:   Tue Feb 23 14:24:43 2016 +0800

    add editbox reorder functionality

commit 6fd1b48199ce351716c43250497e5b192194b1d5
Author: zilongshanren <guanghui8827@126.com>
Date:   Mon Feb 22 18:21:48 2016 +0800

    fix label rendering issue on editor

commit 2a335cd701ed62c6bc45f8849b4322413cf9acca
Author: zilongshanren <guanghui8827@126.com>
Date:   Mon Feb 22 16:10:43 2016 +0800

    add label

commit 51180fae14b43bfe2a2e11d618dda7a91b7522f1
Author: zilongshanren <guanghui8827@126.com>
Date:   Mon Feb 22 14:02:47 2016 +0800

    refactoring, add EditBoxImpl

commit d40cf8839963a98446a3b85f033f352b3325f7ed
Author: zilongshanren <guanghui8827@126.com>
Date:   Mon Feb 22 11:30:35 2016 +0800

    add toggle native editbox logic

commit 6114905f7222e8a21a8bc32be71f0bef1c4c658e
Author: zilongshanren <guanghui8827@126.com>
Date:   Fri Feb 19 13:37:34 2016 +0800

    remove resize and add vertical scrollbar

commit 061e6cadf259a2c6ae3b178fb45a747427af8589
Merge: c2a142c 0314e4c
Author: zilongshanren <guanghui8827@126.com>
Date:   Tue Feb 23 14:25:41 2016 +0800

    Merge remote-tracking branch 'fireball/master'

commit c2a142ce003f2d85d5f161be69f79dd6c0eaa68d
Author: zilongshanren <guanghui8827@126.com>
Date:   Tue Feb 23 14:24:43 2016 +0800

    add editbox reorder functionality

commit 48f02e97f27847b431c510d60fdf9380b14b650f
Author: zilongshanren <guanghui8827@126.com>
Date:   Mon Feb 22 18:21:48 2016 +0800

    fix label rendering issue on editor

commit 3b40ce5bd6a9ef8aa6c4f4b7874408e446ecc655
Author: zilongshanren <guanghui8827@126.com>
Date:   Mon Feb 22 16:10:43 2016 +0800

    add label

commit c304e8f40f9097905023d5bb5206faa6cf20be1a
Author: zilongshanren <guanghui8827@126.com>
Date:   Mon Feb 22 14:02:47 2016 +0800

    refactoring, add EditBoxImpl

commit 913b52536e9085ec2baaf3c71cd8300d7a988962
Author: zilongshanren <guanghui8827@126.com>
Date:   Mon Feb 22 11:30:35 2016 +0800

    add toggle native editbox logic

commit 2058f73ab82c1aa28c2ccf36046a727477a4c519
Merge: 744d8ad 595d744
Author: zilongshanren <guanghui8827@126.com>
Date:   Mon Feb 22 10:48:43 2016 +0800

    Merge remote-tracking branch 'fireball/master'

commit 744d8ad93d6b8bc322173c828be954b9f2834bbb
Author: zilongshanren <guanghui8827@126.com>
Date:   Fri Feb 19 13:37:34 2016 +0800

    remove resize and add vertical scrollbar
2016-02-23 16:52:22 +08:00

1041 lines
33 KiB
JavaScript

/*global _ccsg */
/****************************************************************************
Copyright (c) 2011-2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
Copyright (c) 2012 James Chen
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.
****************************************************************************/
/**
* Enum for keyboard return types
* @readonly
* @enum cc.EditBox.KeyboardReturnType
*/
var KeyboardReturnType = cc.Enum({
/**
* @property {Number} DEFAULT
*/
DEFAULT: 0,
/**
* @property {Number} DONE
*/
DONE: 1,
/**
* @property {Number} SEND
*/
SEND: 2,
/**
* @property {Number} SEARCH
*/
SEARCH: 3,
/**
* @property {Number} GO
*/
GO: 4
});
/**
* The EditBox's InputMode defines the type of text that the user is allowed to enter
* @readonly
* @enum {number}
* @memberof cc.EditBox.InputMode
*/
var InputMode = cc.Enum({
/**
* @property {Number} ANY
*/
ANY: 0,
/**
* The user is allowed to enter an e-mail address.
* @property {Number} EMAILADDR
*/
EMAILADDR: 1,
/**
* The user is allowed to enter an integer value.
* @property {Number} NUMERIC
*/
NUMERIC: 2,
/**
* The user is allowed to enter a phone number.
* @property {Number} PHONENUMBER
*/
PHONENUMBER: 3,
/**
* The user is allowed to enter a URL.
* @property {Number} URL
*/
URL: 4,
/**
* The user is allowed to enter a real number value.
* This extends kEditBoxInputModeNumeric by allowing a decimal point.
* @property {Number} DECIMAL
*/
DECIMAL: 5,
/**
* The user is allowed to enter any text, except for line breaks.
* @property {Number} SINGLELINE
*/
SINGLELINE: 6
});
/**
* Enum for the EditBox's input flags
* @readonly
* @enum cc.EditBox.InputFlag
*/
var InputFlag = cc.Enum({
/**
* Indicates that the text entered is confidential data that should be
* obscured whenever possible. This implies EDIT_BOX_INPUT_FLAG_SENSITIVE.
*
* @property {Number} PASSWORD
*/
PASSWORD: 0,
/**
* Indicates that the text entered is sensitive data that the
* implementation must never store into a dictionary or table for use
* in predictive, auto-completing, or other accelerated input schemes.
* A credit card number is an example of sensitive data.
*
* @property {Number} SENSITIVE
*/
SENSITIVE: 1,
/**
* This flag is a hint to the implementation that during text editing,
* the initial letter of each word should be capitalized.
*
* @property {Number} INITIAL_CAPS_WORD
*/
INITIAL_CAPS_WORD: 2,
/**
* This flag is a hint to the implementation that during text editing,
* the initial letter of each sentence should be capitalized.
*
* @property {Number} INITIAL_CAPS_SENTENCE
*/
INITIAL_CAPS_SENTENCE: 3,
/**
* Capitalize all characters automatically.
*
* @property {Number} INITIAL_CAPS_ALL_CHARACTERS
*/
INITIAL_CAPS_ALL_CHARACTERS: 4
});
/**
* @class
* @extends cc._Class
*/
cc.EditBoxDelegate = cc._Class.extend({
/**
* This method is called when an edit box gains focus after keyboard is shown.
* @param {cc.EditBox} sender
*/
editBoxEditingDidBegin: function (sender) {
},
/**
* This method is called when an edit box loses focus after keyboard is hidden.
* @param {cc.EditBox} sender
*/
editBoxEditingDidEnd: function (sender) {
},
/**
* This method is called when the edit box text was changed.
* @param {cc.EditBox} sender
* @param {String} text
*/
editBoxTextChanged: function (sender, text) {
},
/**
* This method is called when the return button was pressed.
* @param {cc.EditBox} sender
*/
editBoxReturn: function (sender) {
}
});
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,
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';
tmpDOMSprite.canvas.remove();
this._domInputSprite.x = 3;
this._domInputSprite.y = 3;
this._createLabels();
},
_createLabels: function () {
var editBoxSize = this._editBox.getContentSize();
this._textLabel = new _ccsg.Label()
this._textLabel.setVisible(false);
this._textLabel.setAnchorPoint(cc.p(0, 1));
this._editBox.addChild(this._textLabel, 100);
this._placeholderLabel = new _ccsg.Label();
this._placeholderLabel.setAnchorPoint(cc.p(0, 1));
this._placeholderLabel.setColor(cc.Color.GRAY);
this._editBox.addChild(this._placeholderLabel, 100);
this._updateLabelSize(editBoxSize);
},
_updateLabelSize: function (editBoxSize) {
this._textLabel.setPosition(cc.p(0, editBoxSize.height));
this._textLabel.setFontSize(editBoxSize.height * 2.0 /3.0);
this._textLabel.setLineHeight(this._textLabel.getContentSize().height);
this._placeholderLabel.setFontSize(editBoxSize.height * 2 / 3.0);
this._placeholderLabel.setLineHeight(this._placeholderLabel.getContentSize().height);
this._placeholderLabel.setPosition(cc.p(0, editBoxSize.height));
},
_hiddenLabels: function () {
this._textLabel.setVisible(false);
this._placeholderLabel.setVisible(false);
},
_updateLabelString: function() {
this._textLabel.setVisible(true);
this._textLabel.setString(this._editBox._text);
if (this._edTxt.type === 'password') {
var passwordString = '';
var len = this._editBox._text.length;
for (var i = 0; i < len; ++i) {
passwordString += '\u25CF';
}
this._textLabel.setString(passwordString);
}
},
_showLabels: function () {
this._hiddenLabels();
if (this._edTxt.value === '') {
this._placeholderLabel.setVisible(true);
this._placeholderLabel.setString(this._editBox._placeholderText);
}
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();
}
});
},
show: function() {
var div = this._edTxt.parentNode;
div.style.pointerEvents = '';
window.setTimeout(function() {
div.click();
}, 10);
},
hidden: function() {
this._edTxt.style.display = 'none';
var div = this._edTxt.parentNode;
div.style.pointerEvents = 'none';
this._showLabels();
},
_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) {
this._edFontName = fontName || this._edFontName;
this._edFontSize = fontSize || this._edFontSize;
this._updateDOMFontStyle();
},
setFontName: function (fontName) {
this._edFontName = fontName || this._edFontName;
this._updateDOMFontStyle();
},
setFontSize: function (fontSize) {
this._edFontSize = fontSize || this._edFontSize;
this._updateDOMFontStyle();
},
setFontColor: function (color) {
if (this._edTxt.value !== this._editBox._placeholderText) {
this._edTxt.style.color = cc.colorToHex(color);
}
this._textLabel.setColor(color);
},
setPlaceHolder: function (text) {
this._placeholderLabel.setString(text);
},
setMaxLength: function (maxLength) {
this._edTxt.maxLength = maxLength;
},
_updateDOMPlaceholderFontStyle: function () {
this._placeholderLabel.setFontFileOrFamily(this._editBox._placeholderFontName);
this._placeholderLabel.setFontSize(this._editBox._placeholderFontSize);
},
setPlaceholderFontColor: function (color) {
this._placeholderLabel.setColor(color);
},
setInputFlag: function (inputFlag) {
if ((this._edTxt.value !== this._placeholderText)
&& (inputFlag === InputFlag.PASSWORD))
this._edTxt.type = 'password';
else
this._edTxt.type = 'text';
if (inputFlag === InputFlag.SENSITIVE || inputFlag === InputFlag.PASSWORD) {
this._edTxt.style.textTransform = 'none';
}
else if (inputFlag === InputFlag.INITIAL_CAPS_ALL_CHARACTERS) {
this._edTxt.style.textTransform = 'uppercase';
}
else if (inputFlag === InputFlag.INITIAL_CAPS_SENTENCE
|| inputFlag === InputFlag.INITIAL_CAPS_WORD) {
this._edTxt.style.textTransform = 'capitalize';
}
this._updateLabelString();
},
setInputMode : function (inputMode) {
if (inputMode === InputMode.ANY) {
this._removeDomInputControl();
this._createDomTextArea();
this._addDomInputControl();
}
else if(this._editBox._editBoxInputMode === InputMode.ANY) {
this._removeDomInputControl();
this._createDomInput();
this._addDomInputControl();
}
},
/**
* Set the text entered in the edit box.
* @param {string} text The given text.
*/
setString: function (text) {
if (text !== null) {
this._edTxt.value = text;
if (text === '') {
this._edTxt.type = 'text';
this._placeholderLabel.setString(this._editBox._placeholderText);
this._placeholderLabel.setColor(this._editBox._placeholderColor);
this._placeholderLabel.setVisible(true);
this._textLabel.setVisible(false);
}
else {
this._edTxt.style.color = cc.colorToHex(this._editBox._textColor);
this._textLabel.setColor(this._editBox._textColor);
this._placeholderLabel.setVisible(false);
if (this._editBox._editBoxInputFlag === InputFlag.PASSWORD) {
this._edTxt.type = 'password';
}
else {
this._edTxt.type = 'text';
}
this._updateLabelString();
}
}
},
_updateDOMFontStyle: function() {
if (this._edTxt.value !== '') {
this._edTxt.style.fontFamily = this._edFontName;
this._edTxt.style.fontSize = this._edFontSize + 'px';
if (this._editBox._editBoxInputFlag === InputFlag.PASSWORD)
this._edTxt.type = 'password';
else
this._edTxt.type = 'text';
}
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';
this._updateLabelSize(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();};
tmpEdTxt.addEventListener('input', function () {
var editBox = selfPointer._editBox;
if (editBox._delegate && editBox._delegate.editBoxTextChanged)
editBox._delegate.editBoxTextChanged(editBox, this.value);
});
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.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.editBoxEditingDidBegin)
editBox._delegate.editBoxEditingDidBegin(editBox);
cc._canvas.addEventListener('click', onCanvasClick);
});
tmpEdTxt.addEventListener('blur', function () {
var editBox = selfPointer._editBox;
editBox._text = this.value;
if (editBox._delegate && editBox._delegate.editBoxEditingDidEnd)
editBox._delegate.editBoxEditingDidEnd(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._delegate.editBoxTextChanged(editBox, this.value);
});
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.editBoxEditingDidBegin)
editBox._delegate.editBoxEditingDidBegin(editBox);
cc._canvas.addEventListener('click', onCanvasClick);
});
tmpEdTxt.addEventListener('blur', function () {
var editBox = selfPointer._editBox;
editBox._text = this.value;
if (editBox._delegate && editBox._delegate.editBoxEditingDidEnd)
editBox._delegate.editBoxEditingDidEnd(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);
},
};
/**
* <p>cc.EditBox is a brief Class for edit box.<br/>
* You can use this widget to gather small amounts of text from the user.</p>
*
* @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._registerTouchEvent();
var self = this;
this.scheduleOnce(function () {
editBoxImpl.registerClickEvent();
}, 0.01);
},
_registerTouchEvent: function() {
var touchListener = cc.EventListener.create({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches: true,
onTouchBegan: this._onTouchBegan.bind(this),
onTouchEnded: this._onTouchEnded.bind(this),
});
cc.eventManager.addListener(touchListener, this);
},
_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;
}
else {
this._nativeControl.hidden();
return false;
}
},
_onTouchEnded: function (touch, event) {
this._nativeControl.show();
},
_updateBackgroundSpriteSizeAndPosition: function (width, height) {
if(this._backgroundSprite) {
this._backgroundSprite.setContentSize(width, height);
this._backgroundSprite.setPosition(cc.p(width/2, height/2));
}
},
_updateEditBoxSize: function(size, height) {
this._oldSetContentSize(size, height);
var newWidth = size.width || size;
var newHeight = size.height || height;
this._updateBackgroundSpriteSizeAndPosition(newWidth, newHeight);
this._nativeControl.setSize(newWidth, newHeight);
this.setFontSize(height * 2 / 3);
},
/**
* 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);
},
/**
* <p>
* Sets the maximum input length of the edit box. <br/>
* Setting this value enables multiline input mode by default.
* </p>
* @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) {
this._backgroundSprite = normal9SpriteBg;
if(this._backgroundSprite) {
this.addChild(this._backgroundSprite);
this._updateBackgroundSpriteSizeAndPosition(size.width, size.height);
}
this.x = 0;
this.y = 0;
return true;
},
/* override functions */
/**
* Set the delegate for edit box.
* @param {cc.EditBoxDelegate} delegate
*/
setDelegate: function (delegate) {
this._delegate = delegate;
},
/**
* 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;
},
/**
* 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;
},
/**
* 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());
};
/**
* create a edit box with size and background-color or
* @deprecated since v3.0, please use new cc.EditBox(size, normal9SpriteBg, press9SpriteBg, disabled9SpriteBg) instead
* @param {cc.Size} size
* @param {cc.Scale9Sprite } normal9SpriteBg
* @param {cc.Scale9Sprite } [press9SpriteBg]
* @param {cc.Scale9Sprite } [disabled9SpriteBg]
* @return {cc.EditBox}
*/
_ccsg.EditBox.create = function (size, normal9SpriteBg, press9SpriteBg, disabled9SpriteBg) {
return new _ccsg.EditBox(size, normal9SpriteBg, press9SpriteBg, disabled9SpriteBg);
};
_ccsg.EditBox.InputMode = InputMode;
_ccsg.EditBox.InputFlag = InputFlag;
_ccsg.EditBox.KeyboardReturnType = KeyboardReturnType;