CCTextFieldTTF.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2008-2010 Ricardo Quesada
  4. Copyright (c) 2011 Zynga Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. /**
  23. * Text field delegate
  24. * @class
  25. * @extends cc.Class
  26. */
  27. cc.TextFieldDelegate = cc.Class.extend(/** @lends cc.TextFieldDelegate# */{
  28. /**
  29. * If the sender doesn't want to attach with IME, return true;
  30. * @param {cc.TextFieldTTF} sender
  31. * @return {Boolean}
  32. */
  33. onTextFieldAttachWithIME:function (sender) {
  34. return false;
  35. },
  36. /**
  37. * If the sender doesn't want to detach with IME, return true;
  38. * @param {cc.TextFieldTTF} sender
  39. * @return {Boolean}
  40. */
  41. onTextFieldDetachWithIME:function (sender) {
  42. return false;
  43. },
  44. /**
  45. * If the sender doesn't want to insert the text, return true;
  46. * @param {cc.TextFieldTTF} sender
  47. * @param {String} text
  48. * @param {Number} len
  49. * @return {Boolean}
  50. */
  51. onTextFieldInsertText:function (sender, text, len) {
  52. return false
  53. },
  54. /**
  55. * If the sender doesn't want to delete the delText, return true;
  56. * @param {cc.TextFieldTTF} sender
  57. * @param {String} delText
  58. * @param {Number} len
  59. * @return {Boolean}
  60. */
  61. onTextFieldDeleteBackward:function (sender, delText, len) {
  62. return false;
  63. },
  64. /**
  65. * If doesn't want draw sender as default, return true.
  66. * @param {cc.TextFieldTTF} sender
  67. * @return {Boolean}
  68. */
  69. onDraw:function (sender) {
  70. return false;
  71. }
  72. });
  73. /**
  74. * A simple text input field with TTF font.
  75. * @class
  76. * @extends cc.LabelTTF
  77. */
  78. cc.TextFieldTTF = cc.LabelTTF.extend(/** @lends cc.TextFieldTTF# */{
  79. _lens:null,
  80. _inputText:"",
  81. _placeHolder:"",
  82. _charCount:0,
  83. _delegate:null,
  84. _ColorSpaceHolder:null,
  85. /**
  86. * Constructor
  87. */
  88. ctor:function () {
  89. this._ColorSpaceHolder = new cc.Color3B(127, 127, 127);
  90. cc.IMEDispatcher.getInstance().addDelegate(this);
  91. cc.LabelTTF.prototype.ctor.call(this);
  92. },
  93. /**
  94. * @return {cc.Node}
  95. */
  96. getDelegate:function () {
  97. return this._delegate;
  98. },
  99. /**
  100. * @param {cc.Node} value
  101. */
  102. setDelegate:function (value) {
  103. this._delegate = value;
  104. },
  105. /**
  106. * @return {Number}
  107. */
  108. getCharCount:function () {
  109. return this._charCount;
  110. },
  111. /**
  112. * @return {cc.Color3B}
  113. */
  114. getColorSpaceHolder:function () {
  115. return this._ColorSpaceHolder;
  116. },
  117. /**
  118. * @param {cc.Color3B} value
  119. */
  120. setColorSpaceHolder:function (value) {
  121. this._ColorSpaceHolder = value;
  122. },
  123. /**
  124. * Initializes the cc.TextFieldTTF with a font name, alignment, dimension and font size
  125. * @param {String} placeholder
  126. * @param {cc.Size} dimensions
  127. * @param {Number} alignment
  128. * @param {String} fontName
  129. * @param {Number} fontSize
  130. * @return {Boolean}
  131. * @example
  132. * //example
  133. * var textField = new cc.TextFieldTTF();
  134. * // When five parameters
  135. * textField.initWithPlaceHolder("<click here for input>", cc.size(100,50), cc.TEXT_ALIGNMENT_LEFT,"Arial", 32);
  136. * // When three parameters
  137. * textField.initWithPlaceHolder("<click here for input>", "Arial", 32);
  138. */
  139. initWithPlaceHolder:function (placeholder, dimensions, alignment, fontName, fontSize) {
  140. switch (arguments.length) {
  141. case 5:
  142. if (placeholder) {
  143. this._placeHolder = placeholder;
  144. }
  145. return this.initWithString(this._placeHolder,fontName, fontSize, dimensions, alignment);
  146. break;
  147. case 3:
  148. if (placeholder) {
  149. this._placeHolder = placeholder;
  150. }
  151. fontName = arguments[1];
  152. fontSize = arguments[2];
  153. return this.initWithString(this._placeHolder, fontName, fontSize);
  154. break;
  155. default:
  156. throw "Argument must be non-nil ";
  157. break;
  158. }
  159. },
  160. /**
  161. * Input text property
  162. * @param {String} text
  163. */
  164. setString:function (text) {
  165. text = String(text);
  166. this._inputText = text || "";
  167. // if there is no input text, display placeholder instead
  168. if (!this._inputText.length)
  169. cc.LabelTTF.prototype.setString.call(this, this._placeHolder);
  170. else
  171. cc.LabelTTF.prototype.setString.call(this,this._inputText);
  172. this._charCount = this._inputText.length;
  173. },
  174. /**
  175. * @return {String}
  176. */
  177. getString:function () {
  178. return this._inputText;
  179. },
  180. /**
  181. * @param {String} text
  182. */
  183. setPlaceHolder:function (text) {
  184. this._placeHolder = text || "";
  185. if (!this._inputText.length) {
  186. cc.LabelTTF.prototype.setString.call(this,this._placeHolder);
  187. }
  188. },
  189. /**
  190. * @return {String}
  191. */
  192. getPlaceHolder:function () {
  193. return this._placeHolder;
  194. },
  195. /**
  196. * @param {CanvasContext} ctx
  197. */
  198. draw:function (ctx) {
  199. //console.log("size",this._contentSize);
  200. var context = ctx || cc.renderContext;
  201. if (this._delegate && this._delegate.onDraw(this))
  202. return;
  203. if (this._inputText && this._inputText.length > 0) {
  204. cc.LabelTTF.prototype.draw.call(this, context);
  205. return;
  206. }
  207. // draw placeholder
  208. var color = this.getColor();
  209. this.setColor(this._ColorSpaceHolder);
  210. if(cc.renderContextType === cc.CANVAS)
  211. this._updateTexture();
  212. cc.LabelTTF.prototype.draw.call(this, context);
  213. this.setColor(color);
  214. },
  215. //////////////////////////////////////////////////////////////////////////
  216. // CCIMEDelegate interface
  217. //////////////////////////////////////////////////////////////////////////
  218. /**
  219. * Open keyboard and receive input text.
  220. * @return {Boolean}
  221. */
  222. attachWithIME:function () {
  223. return cc.IMEDispatcher.getInstance().attachDelegateWithIME(this);
  224. },
  225. /**
  226. * End text input and close keyboard.
  227. * @return {Boolean}
  228. */
  229. detachWithIME:function () {
  230. return cc.IMEDispatcher.getInstance().detachDelegateWithIME(this);
  231. },
  232. /**
  233. * @return {Boolean}
  234. */
  235. canAttachWithIME:function () {
  236. return (this._delegate) ? (!this._delegate.onTextFieldAttachWithIME(this)) : true;
  237. },
  238. /**
  239. * When the delegate detach with IME, this method call by CCIMEDispatcher.
  240. */
  241. didAttachWithIME:function () {
  242. },
  243. /**
  244. * @return {Boolean}
  245. */
  246. canDetachWithIME:function () {
  247. return (this._delegate) ? (!this._delegate.onTextFieldDetachWithIME(this)) : true;
  248. },
  249. /**
  250. * When the delegate detach with IME, this method call by CCIMEDispatcher.
  251. */
  252. didDetachWithIME:function () {
  253. },
  254. /**
  255. * Delete backward
  256. */
  257. deleteBackward:function () {
  258. var strLen = this._inputText.length;
  259. if (strLen == 0)
  260. return;
  261. // get the delete byte number
  262. var deleteLen = 1; // default, erase 1 byte
  263. if (this._delegate && this._delegate.onTextFieldDeleteBackward(this, this._inputText[strLen - deleteLen], deleteLen)) {
  264. // delegate don't want delete backward
  265. return;
  266. }
  267. // if delete all text, show space holder string
  268. if (strLen <= deleteLen) {
  269. this._inputText = "";
  270. this._charCount = 0;
  271. cc.LabelTTF.prototype.setString.call(this,this._placeHolder);
  272. return;
  273. }
  274. // set new input text
  275. var sText = this._inputText.substring(0, strLen - deleteLen);
  276. this.setString(sText);
  277. },
  278. /**
  279. * Remove delegate
  280. */
  281. removeDelegate:function () {
  282. cc.IMEDispatcher.getInstance().removeDelegate(this);
  283. },
  284. /**
  285. * @param {String} text
  286. * @param {Number} len
  287. */
  288. insertText:function (text, len) {
  289. var sInsert = text;
  290. // insert \n means input end
  291. var pos = sInsert.indexOf('\n');
  292. if (pos > -1) {
  293. sInsert = sInsert.substring(0, pos);
  294. }
  295. if (sInsert.length > 0) {
  296. if (this._delegate && this._delegate.onTextFieldInsertText(this, sInsert, sInsert.length)) {
  297. // delegate doesn't want insert text
  298. return;
  299. }
  300. var sText = this._inputText + sInsert;
  301. this._charCount = sText.length;
  302. this.setString(sText);
  303. }
  304. if (pos == -1)
  305. return;
  306. // '\n' has inserted, let delegate process first
  307. if (this._delegate && this._delegate.onTextFieldInsertText(this, "\n", 1))
  308. return;
  309. // if delegate hasn't process, detach with ime as default
  310. this.detachWithIME();
  311. },
  312. /**
  313. * @return {String}
  314. */
  315. getContentText:function () {
  316. return this._inputText;
  317. },
  318. //////////////////////////////////////////////////////////////////////////
  319. // keyboard show/hide notification
  320. //////////////////////////////////////////////////////////////////////////
  321. keyboardWillShow:function (info) {
  322. },
  323. keyboardDidShow:function (info) {
  324. },
  325. keyboardWillHide:function (info) {
  326. },
  327. keyboardDidHide:function (info) {
  328. }
  329. });
  330. /**
  331. * creates a cc.TextFieldTTF from a fontName, alignment, dimension and font size
  332. * @param {String} placeholder
  333. * @param {cc.Size} dimensions
  334. * @param {Number} alignment
  335. * @param {String} fontName
  336. * @param {Number} fontSize
  337. * @return {cc.TextFieldTTF|Null}
  338. * @example
  339. * //example
  340. * // When five parameters
  341. * var textField = cc.TextFieldTTF.create("<click here for input>", cc.size(100,50), cc.TEXT_ALIGNMENT_LEFT,"Arial", 32);
  342. * // When three parameters
  343. * var textField = cc.TextFieldTTF.create("<click here for input>", "Arial", 32);
  344. */
  345. cc.TextFieldTTF.create = function (placeholder, dimensions, alignment, fontName, fontSize) {
  346. var ret;
  347. switch (arguments.length) {
  348. case 5:
  349. ret = new cc.TextFieldTTF();
  350. if (ret && ret.initWithPlaceHolder("", dimensions, alignment, fontName, fontSize)) {
  351. if (placeholder)
  352. ret.setPlaceHolder(placeholder);
  353. return ret;
  354. }
  355. return null;
  356. break;
  357. case 3:
  358. ret = new cc.TextFieldTTF();
  359. if (ret && ret.initWithString("", arguments[1], arguments[2])) {
  360. if (placeholder)
  361. ret.setPlaceHolder(placeholder);
  362. return ret;
  363. }
  364. return null;
  365. break;
  366. default:
  367. throw "Argument must be non-nil ";
  368. break;
  369. }
  370. };