UITextBMFont.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /****************************************************************************
  2. Copyright (c) 2011-2012 cocos2d-x.org
  3. Copyright (c) 2013-2014 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. /**
  22. * The TextBMFont control of Cocos UI, it rendered by LabelBMFont.
  23. * @class
  24. * @extends ccui.Widget
  25. *
  26. * @property {String} string - Content string of the label
  27. */
  28. ccui.LabelBMFont = ccui.TextBMFont = ccui.Widget.extend(/** @lends ccui.TextBMFont# */{
  29. _labelBMFontRenderer: null,
  30. _fntFileHasInit: false,
  31. _fntFileName: "",
  32. _stringValue: "",
  33. _className: "TextBMFont",
  34. _labelBMFontRendererAdaptDirty: true,
  35. /**
  36. * Allocates and initializes a TextBMFont. <br/>
  37. * Constructor of ccui.TextBMFont. override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  38. * @param {String} text
  39. * @param {String} filename
  40. * @example
  41. * // example
  42. * var uiLabelBMFont = new ccui.TextBMFont();
  43. */
  44. ctor: function (text, filename) {
  45. ccui.Widget.prototype.ctor.call(this);
  46. if(filename != undefined){
  47. this.setFntFile(filename);
  48. this.setString(text);
  49. }
  50. },
  51. _initRenderer: function () {
  52. this._labelBMFontRenderer = new cc.LabelBMFont();
  53. this.addProtectedChild(this._labelBMFontRenderer, ccui.TextBMFont.RENDERER_ZORDER, -1);
  54. },
  55. /**
  56. * Initializes a bitmap font atlas with an initial string and the FNT file
  57. * @param {String} fileName
  58. */
  59. setFntFile: function (fileName) {
  60. if (!fileName)
  61. return;
  62. var _self = this;
  63. _self._fntFileName = fileName;
  64. _self._fntFileHasInit = true;
  65. _self._labelBMFontRenderer.initWithString(this._stringValue, fileName);
  66. var locRenderer = _self._labelBMFontRenderer;
  67. if(!locRenderer._textureLoaded){
  68. locRenderer.addLoadedEventListener(function(){
  69. _self.updateSizeAndPosition();
  70. });
  71. }
  72. },
  73. /**
  74. * Sets string value for TextBMFont
  75. * @deprecated since v3.0, please use setString instead.
  76. * @param {String} value
  77. */
  78. setText: function (value) {
  79. cc.log("Please use the setString");
  80. this.setString(value);
  81. },
  82. /**
  83. * Sets string value for TextBMFont
  84. * @param {String} value
  85. */
  86. setString: function (value) {
  87. this._stringValue = value;
  88. if (!this._fntFileHasInit)
  89. return;
  90. this._labelBMFontRenderer.setString(value);
  91. this._updateContentSizeWithTextureSize(this._labelBMFontRenderer.getContentSize());
  92. this._labelBMFontRendererAdaptDirty = true;
  93. },
  94. /**
  95. * Returns string value for TextBMFont.
  96. * @returns {String}
  97. */
  98. getString: function () {
  99. return this._stringValue;
  100. },
  101. /**
  102. * Returns the length of TextBMFont's string.
  103. * @returns {Number}
  104. */
  105. getStringLength: function(){
  106. return this._labelBMFontRenderer.getStringLength();
  107. },
  108. _onSizeChanged: function () {
  109. ccui.Widget.prototype._onSizeChanged.call(this);
  110. this._labelBMFontRendererAdaptDirty = true;
  111. },
  112. _adaptRenderers: function(){
  113. if (this._labelBMFontRendererAdaptDirty){
  114. this._labelBMFontScaleChangedWithSize();
  115. this._labelBMFontRendererAdaptDirty = false;
  116. }
  117. },
  118. /**
  119. * Returns TextBMFont's content size
  120. * @override
  121. * @returns {cc.Size}
  122. */
  123. getVirtualRendererSize: function(){
  124. return this._labelBMFontRenderer.getContentSize();
  125. },
  126. /**
  127. * Returns the renderer of TextBMFont
  128. * @override
  129. * @returns {cc.Node}
  130. */
  131. getVirtualRenderer: function () {
  132. return this._labelBMFontRenderer;
  133. },
  134. _labelBMFontScaleChangedWithSize: function () {
  135. var locRenderer = this._labelBMFontRenderer;
  136. if (this._ignoreSize)
  137. locRenderer.setScale(1.0);
  138. else {
  139. var textureSize = locRenderer.getContentSize();
  140. if (textureSize.width <= 0.0 || textureSize.height <= 0.0) {
  141. locRenderer.setScale(1.0);
  142. return;
  143. }
  144. locRenderer.setScaleX(this._contentSize.width / textureSize.width);
  145. locRenderer.setScaleY(this._contentSize.height / textureSize.height);
  146. }
  147. locRenderer.setPosition(this._contentSize.width / 2.0, this._contentSize.height / 2.0);
  148. },
  149. /**
  150. * Returns the "class name" of ccui.TextBMFont.
  151. * @returns {string}
  152. */
  153. getDescription: function () {
  154. return "TextBMFont";
  155. },
  156. _createCloneInstance: function () {
  157. return new ccui.TextBMFont();
  158. },
  159. _copySpecialProperties: function (labelBMFont) {
  160. this.setFntFile(labelBMFont._fntFileName);
  161. this.setString(labelBMFont._stringValue);
  162. }
  163. });
  164. var _p = ccui.TextBMFont.prototype;
  165. // Extended properties
  166. /** @expose */
  167. _p.string;
  168. cc.defineGetterSetter(_p, "string", _p.getString, _p.setStringValue);
  169. _p = null;
  170. /**
  171. * allocates and initializes a UILabelBMFont.
  172. * @deprecated since v3.0, please use new ccui.TextBMFont() instead.
  173. * @return {ccui.TextBMFont}
  174. * @example
  175. * // example
  176. * var uiLabelBMFont = ccui.TextBMFont.create();
  177. */
  178. ccui.TextBMFont.create = function (text, filename) {
  179. return new ccui.TextBMFont(text, filename);
  180. };
  181. // Constants
  182. /**
  183. * The zOrder value of TextBMFont's renderer.
  184. * @constant
  185. * @type {number}
  186. */
  187. ccui.TextBMFont.RENDERER_ZORDER = -1;