Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #1624 support all input for TextInputTest

  • Loading branch information...
commit cca28c118b9d5b53d3e51414cc03005e700b91aa 1 parent 2620df6
@dingpinglv dingpinglv authored
View
1  cocos2d/keyboard_dispatcher/CCKeyboardDispatcher.js
@@ -299,7 +299,6 @@ cc.KeyboardDispatcher.getInstance = function () {
cc.canvas.style.cursor = 'default';
cc.canvas.addEventListener("keydown", function (e) {
cc.keyboardDispatcher.dispatchKeyboardMSG(e, true);
- cc.IMEDispatcher.getInstance().processKeycode(e.keyCode);
});
cc.canvas.addEventListener("keyup", function (e) {
cc.keyboardDispatcher.dispatchKeyboardMSG(e, false);
View
97 cocos2d/text_input_node/CCIMEDispatcher.js
@@ -131,20 +131,70 @@ cc.IMEDelegate = cc.Class.extend(/** @lends cc.IMEDelegate# */{
}
});
-
/**
* Input Method Edit Message Dispatcher.
* @class
* @extends cc.Class
*/
cc.IMEDispatcher = cc.Class.extend(/** @lends cc.IMEDispatcher# */{
+ _domInputControl:null,
impl:null,
+ _currentInputString:"",
/**
* Constructor
*/
ctor:function () {
this.impl = new cc.IMEDispatcher.Impl();
},
+
+ init:function () {
+ this._domInputControl = document.getElementById("imeDispatcherInput");
+ if (!this._domInputControl) {
+ this._domInputControl = document.createElement("input");
+ this._domInputControl.setAttribute("type", "text");
+ this._domInputControl.setAttribute("id", "imeDispatcherInput");
+ this._domInputControl.style.position = "absolute";
+ this._domInputControl.style.top = "-200px";
+ this._domInputControl.style.left = "-200px";
+ this._domInputControl.setAttribute('tabindex', 2);
+ document.body.appendChild(this._domInputControl);
+ }
+ var selfPointer = this;
+ //add event listener
+ this._domInputControl.addEventListener("input", function () {
+ selfPointer._processDomInputString(selfPointer._domInputControl.value);
+ }, false);
+ this._domInputControl.addEventListener("keydown", function (e) {
+ // ignore tab key
+ if (e.keyCode === cc.KEY.tab) {
+ e.stopPropagation();
+ e.preventDefault();
+ } else if (e.keyCode == cc.KEY.enter) {
+ selfPointer.dispatchInsertText("\n", 1);
+ e.stopPropagation();
+ e.preventDefault();
+ }
+ }, false);
+ },
+
+ _processDomInputString:function (text) {
+ var i, startPos;
+ var len = this._currentInputString.length < text.length ? this._currentInputString.length : text.length;
+ for (startPos = 0; startPos < len; startPos++) {
+ if (text[startPos] !== this._currentInputString[startPos])
+ break;
+ }
+ var delTimes = this._currentInputString.length - startPos;
+ var insTimes = text.length - startPos;
+ for (i = 0; i < delTimes; i++)
+ this.dispatchDeleteBackward();
+
+ for (i = 0; i < insTimes; i++)
+ this.dispatchInsertText(text[startPos + i], 1);
+
+ this._currentInputString = text;
+ },
+
/**
* Dispatch the input text from ime
* @param {String} text
@@ -202,6 +252,7 @@ cc.IMEDispatcher = cc.Class.extend(/** @lends cc.IMEDispatcher# */{
}
}
},
+
/**
* Dispatch keyboard notification
* @param {cc.IMEKeyboardNotificationInfo} info
@@ -210,12 +261,12 @@ cc.IMEDispatcher = cc.Class.extend(/** @lends cc.IMEDispatcher# */{
if (this.impl) {
for (var i = 0; i < this.impl._delegateList.length; i++) {
var delegate = this.impl._delegateList[i];
- if (delegate) {
+ if (delegate)
delegate.keyboardDidShow(info);
- }
}
}
},
+
/**
* Dispatch keyboard notification
* @param {cc.IMEKeyboardNotificationInfo} info
@@ -230,6 +281,7 @@ cc.IMEDispatcher = cc.Class.extend(/** @lends cc.IMEDispatcher# */{
}
}
},
+
/**
* Dispatch keyboard notification
* @param {cc.IMEKeyboardNotificationInfo} info
@@ -253,9 +305,9 @@ cc.IMEDispatcher = cc.Class.extend(/** @lends cc.IMEDispatcher# */{
* cc.IMEDispatcher.getInstance().addDelegate(this);
*/
addDelegate:function (delegate) {
- if (!delegate || !this.impl) {
+ if (!delegate || !this.impl)
return;
- }
+
if (this.impl._delegateList.indexOf(delegate) > -1) {
// delegate already in list
return;
@@ -272,14 +324,12 @@ cc.IMEDispatcher = cc.Class.extend(/** @lends cc.IMEDispatcher# */{
* var ret = cc.IMEDispatcher.getInstance().attachDelegateWithIME(this);
*/
attachDelegateWithIME:function (delegate) {
- if (!this.impl || !delegate) {
+ if (!this.impl || !delegate)
return false;
- }
// if delegate is not in delegate list, return
- if (this.impl._delegateList.indexOf(delegate) == -1) {
+ if (this.impl._delegateList.indexOf(delegate) == -1)
return false;
- }
if (this.impl._delegateWithIme) {
// if old delegate canDetachWithIME return false
@@ -295,7 +345,10 @@ cc.IMEDispatcher = cc.Class.extend(/** @lends cc.IMEDispatcher# */{
pOldDelegate.didDetachWithIME();
this.impl._delegateWithIme = delegate;
+ this._currentInputString = delegate.getString ? delegate.getString() : "";
delegate.didAttachWithIME();
+ this._domInputControl.focus();
+ this._domInputControl.value = this._currentInputString;
return true;
}
@@ -304,7 +357,10 @@ cc.IMEDispatcher = cc.Class.extend(/** @lends cc.IMEDispatcher# */{
return false;
this.impl._delegateWithIme = delegate;
+ this._currentInputString = delegate.getString ? delegate.getString() : "";
delegate.didAttachWithIME();
+ this._domInputControl.focus();
+ this._domInputControl.value = this._currentInputString;
return true;
},
/**
@@ -316,21 +372,19 @@ cc.IMEDispatcher = cc.Class.extend(/** @lends cc.IMEDispatcher# */{
* var ret = cc.IMEDispatcher.getInstance().detachDelegateWithIME(this);
*/
detachDelegateWithIME:function (delegate) {
- if (!this.impl || !delegate) {
+ if (!this.impl || !delegate)
return false;
- }
// if delegate is not the current delegate attached with ime, return
- if (this.impl._delegateWithIme != delegate) {
+ if (this.impl._delegateWithIme != delegate)
return false;
- }
- if (!delegate.canDetachWithIME()) {
+ if (!delegate.canDetachWithIME())
return false;
- }
- this.impl._delegateWithIme = 0;
+ this.impl._delegateWithIme = null;
delegate.didDetachWithIME();
+ cc.canvas.focus();
return true;
},
@@ -342,14 +396,12 @@ cc.IMEDispatcher = cc.Class.extend(/** @lends cc.IMEDispatcher# */{
* cc.IMEDispatcher.getInstance().removeDelegate(this);
*/
removeDelegate:function (delegate) {
- if (!this.impl || !delegate) {
+ if (!this.impl || !delegate)
return;
- }
// if delegate is not in delegate list, return
- if (this.impl._delegateList.indexOf(delegate) == -1) {
+ if (this.impl._delegateList.indexOf(delegate) == -1)
return;
- }
if (this.impl._delegateWithIme) {
if (delegate == this.impl._delegateWithIme) {
@@ -407,9 +459,8 @@ cc.IMEDispatcher.Impl = cc.Class.extend(/** @lends cc.IMEDispatcher.Impl# */{
*/
findDelegate:function (delegate) {
for (var i = 0; i < this._delegateList.length; i++) {
- if (this._delegateList[i] == delegate) {
+ if (this._delegateList[i] == delegate)
return i;
- }
}
return null;
}
@@ -422,7 +473,7 @@ cc.IMEDispatcher.Impl = cc.Class.extend(/** @lends cc.IMEDispatcher.Impl# */{
cc.IMEDispatcher.getInstance = function () {
if (!cc.IMEDispatcher.instance) {
cc.IMEDispatcher.instance = new cc.IMEDispatcher();
- cc.KeyboardDispatcher.getInstance();
+ cc.IMEDispatcher.instance.init();
}
return cc.IMEDispatcher.instance;
};
View
79 cocos2d/text_input_node/CCTextFieldTTF.js
@@ -100,30 +100,35 @@ cc.TextFieldTTF = cc.LabelTTF.extend(/** @lends cc.TextFieldTTF# */{
cc.IMEDispatcher.getInstance().addDelegate(this);
this._super();
},
+
/**
* @return {cc.Node}
*/
getDelegate:function () {
return this._delegate;
},
+
/**
* @param {cc.Node} value
*/
setDelegate:function (value) {
this._delegate = value;
},
+
/**
* @return {Number}
*/
getCharCount:function () {
return this._charCount;
},
+
/**
* @return {cc.Color3B}
*/
getColorSpaceHolder:function () {
return this._ColorSpaceHolder;
},
+
/**
* @param {cc.Color3B} value
*/
@@ -167,6 +172,7 @@ cc.TextFieldTTF = cc.LabelTTF.extend(/** @lends cc.TextFieldTTF# */{
break;
}
},
+
/**
* Input text property
* @param {String} text
@@ -177,26 +183,24 @@ cc.TextFieldTTF = cc.LabelTTF.extend(/** @lends cc.TextFieldTTF# */{
this._super(text);
return;
}
- if (text) {
- this._inputText = text;
- } else {
- this._inputText = "";
- }
+
+ this._inputText = text || "";
// if there is no input text, display placeholder instead
- if (!this._inputText.length) {
+ if (!this._inputText.length)
this._super(this._placeHolder);
- } else {
+ else
this._super(this._inputText);
- }
this._charCount = this._inputText.length;
},
+
/**
* @return {String}
*/
getString:function () {
return this._inputText;
},
+
/**
* @param {String} text
*/
@@ -206,21 +210,23 @@ cc.TextFieldTTF = cc.LabelTTF.extend(/** @lends cc.TextFieldTTF# */{
this.setString(this._placeHolder, true);
}
},
+
/**
* @return {String}
*/
getPlaceHolder:function () {
return this._placeHolder;
},
+
/**
* @param {CanvasContext} ctx
*/
draw:function (ctx) {
var context = ctx || cc.renderContext;
- if (this._delegate && this._delegate.onDraw(this)) {
+ if (this._delegate && this._delegate.onDraw(this))
return;
- }
- if (this._inputText) {
+
+ if (this._inputText && this._inputText.length > 0) {
this._super(context);
return;
}
@@ -240,17 +246,17 @@ cc.TextFieldTTF = cc.LabelTTF.extend(/** @lends cc.TextFieldTTF# */{
* @return {Boolean}
*/
attachWithIME:function () {
- var ret = cc.IMEDispatcher.getInstance().attachDelegateWithIME(this);
- return ret;
+ return cc.IMEDispatcher.getInstance().attachDelegateWithIME(this);
},
+
/**
* End text input and close keyboard.
* @return {Boolean}
*/
detachWithIME:function () {
- var ret = cc.IMEDispatcher.getInstance().detachDelegateWithIME(this);
- return ret;
+ return cc.IMEDispatcher.getInstance().detachDelegateWithIME(this);
},
+
/**
* @return {Boolean}
*/
@@ -263,6 +269,7 @@ cc.TextFieldTTF = cc.LabelTTF.extend(/** @lends cc.TextFieldTTF# */{
*/
didAttachWithIME:function () {
},
+
/**
* @return {Boolean}
*/
@@ -275,26 +282,25 @@ cc.TextFieldTTF = cc.LabelTTF.extend(/** @lends cc.TextFieldTTF# */{
*/
didDetachWithIME:function () {
},
+
/**
* Delete backward
*/
deleteBackward:function () {
- var nStrLen = this._inputText.length;
- if (nStrLen == 0) {
- // there is no string
+ var strLen = this._inputText.length;
+ if (strLen == 0)
return;
- }
// get the delete byte number
- var nDeleteLen = 1; // default, erase 1 byte
+ var deleteLen = 1; // default, erase 1 byte
- if (this._delegate && this._delegate.onTextFieldDeleteBackward(this, this._inputText[nStrLen - nDeleteLen], nDeleteLen)) {
+ if (this._delegate && this._delegate.onTextFieldDeleteBackward(this, this._inputText[strLen - deleteLen], deleteLen)) {
// delegate don't want delete backward
return;
}
// if delete all text, show space holder string
- if (nStrLen <= nDeleteLen) {
+ if (strLen <= deleteLen) {
this._inputText = "";
this._charCount = 0;
this.setString(this._placeHolder, true);
@@ -302,15 +308,17 @@ cc.TextFieldTTF = cc.LabelTTF.extend(/** @lends cc.TextFieldTTF# */{
}
// set new input text
- var sText = this._inputText.substring(0, nStrLen - nDeleteLen);
- this.setString(sText);
+ var sText = this._inputText.substring(0, strLen - deleteLen);
+ this.setString(sText, false);
},
+
/**
* Remove delegate
*/
removeDelegate:function () {
cc.IMEDispatcher.getInstance().removeDelegate(this);
},
+
/**
* @param {String} text
* @param {Number} len
@@ -319,9 +327,9 @@ cc.TextFieldTTF = cc.LabelTTF.extend(/** @lends cc.TextFieldTTF# */{
var sInsert = text;
// insert \n means input end
- var nPos = sInsert.indexOf('\n');
- if (nPos > -1) {
- sInsert = sInsert.substring(0, nPos);
+ var pos = sInsert.indexOf('\n');
+ if (pos > -1) {
+ sInsert = sInsert.substring(0, pos);
}
if (sInsert.length > 0) {
@@ -335,14 +343,12 @@ cc.TextFieldTTF = cc.LabelTTF.extend(/** @lends cc.TextFieldTTF# */{
this.setString(sText);
}
- if (nPos == -1) {
+ if (pos == -1)
return;
- }
// '\n' has inserted, let delegate process first
- if (this._delegate && this._delegate.onTextFieldInsertText(this, "\n", 1)) {
+ if (this._delegate && this._delegate.onTextFieldInsertText(this, "\n", 1))
return;
- }
// if delegate hasn't process, detach with ime as default
this.detachWithIME();
@@ -383,25 +389,24 @@ cc.TextFieldTTF = cc.LabelTTF.extend(/** @lends cc.TextFieldTTF# */{
* var textField = cc.TextFieldTTF.create("<click here for input>", "Arial", 32);
*/
cc.TextFieldTTF.create = function (placeholder, dimensions, alignment, fontName, fontSize) {
+ var ret;
switch (arguments.length) {
case 5:
- var ret = new cc.TextFieldTTF();
+ ret = new cc.TextFieldTTF();
if (ret && ret.initWithPlaceHolder("", dimensions, alignment, fontName, fontSize)) {
- if (placeholder) {
+ if (placeholder)
ret.setPlaceHolder(placeholder);
- }
return ret;
}
return null;
break;
case 3:
- var ret = new cc.TextFieldTTF();
+ ret = new cc.TextFieldTTF();
fontName = arguments[1];
fontSize = arguments[2];
if (ret && ret.initWithString(["", fontName, fontSize])) {
- if (placeholder) {
+ if (placeholder)
ret.setPlaceHolder(placeholder);
- }
return ret;
}
return null;
2  samples
@@ -1 +1 @@
-Subproject commit 85f5b7b7e7b0cc427db15885775a187048c6161a
+Subproject commit e71bd657a23e01b9f8288c5531d23610434ecffb
Please sign in to comment.
Something went wrong with that request. Please try again.