UITextAtlas.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 text atlas control of Cocos UI.
  23. * @class
  24. * @extends ccui.Widget
  25. *
  26. * @property {String} string - Content string of the label
  27. */
  28. ccui.TextAtlas = ccui.Widget.extend(/** @lends ccui.TextAtlas# */{
  29. _labelAtlasRenderer: null,
  30. _stringValue: "",
  31. _charMapFileName: "",
  32. _itemWidth: 0,
  33. _itemHeight: 0,
  34. _startCharMap: "",
  35. _className: "TextAtlas",
  36. _labelAtlasRendererAdaptDirty: null,
  37. /**
  38. * Allocates and initializes a UILabelAtlas. <br/>
  39. * Constructor of ccui.TextAtlas, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  40. * @param {String} stringValue
  41. * @param {String} charMapFile
  42. * @param {number} itemWidth
  43. * @param {number} itemHeight
  44. * @param {String} startCharMap
  45. * @example
  46. * // example
  47. * var uiLabelAtlas = new ccui.TextAtlas();
  48. */
  49. ctor: function (stringValue, charMapFile, itemWidth, itemHeight, startCharMap) {
  50. ccui.Widget.prototype.ctor.call(this);
  51. startCharMap && this.setProperty(stringValue, charMapFile, itemWidth, itemHeight, startCharMap);
  52. },
  53. _initRenderer: function () {
  54. this._labelAtlasRenderer = new cc.LabelAtlas();
  55. this._labelAtlasRenderer.setAnchorPoint(cc.p(0.5, 0.5));
  56. this.addProtectedChild(this._labelAtlasRenderer, ccui.TextAtlas.RENDERER_ZORDER, -1);
  57. },
  58. /**
  59. * initializes the UILabelAtlas with a string, a char map file(the atlas), the width and height of each element and the starting char of the atlas
  60. * @param {String} stringValue
  61. * @param {String} charMapFile
  62. * @param {number} itemWidth
  63. * @param {number} itemHeight
  64. * @param {String} startCharMap
  65. */
  66. setProperty: function (stringValue, charMapFile, itemWidth, itemHeight, startCharMap) {
  67. this._stringValue = stringValue;
  68. this._charMapFileName = charMapFile;
  69. this._itemWidth = itemWidth;
  70. this._itemHeight = itemHeight;
  71. this._startCharMap = startCharMap;
  72. this._labelAtlasRenderer.initWithString(
  73. stringValue,
  74. this._charMapFileName,
  75. this._itemWidth,
  76. this._itemHeight,
  77. this._startCharMap[0]
  78. );
  79. this._updateContentSizeWithTextureSize(this._labelAtlasRenderer.getContentSize());
  80. this._labelAtlasRendererAdaptDirty = true;
  81. },
  82. /**
  83. * Sets string value for ui text atlas.
  84. * @param {String} value
  85. */
  86. setString: function (value) {
  87. this._stringValue = value;
  88. this._labelAtlasRenderer.setString(value);
  89. this._updateContentSizeWithTextureSize(this._labelAtlasRenderer.getContentSize());
  90. this._labelAtlasRendererAdaptDirty = true;
  91. },
  92. /**
  93. * Sets string value for text atlas.
  94. * @deprecated since v3.0, please use setString instead.
  95. * @param {String} value
  96. */
  97. setStringValue: function (value) {
  98. cc.log("Please use the setString");
  99. this.setString(value);
  100. },
  101. /**
  102. * get string value for text atlas.
  103. * @deprecated since v3.0, please use getString instead.
  104. * @returns {String}
  105. */
  106. getStringValue: function () {
  107. cc.log("Please use the getString");
  108. return this.getString();
  109. },
  110. /**
  111. * get string value for ui text atlas.
  112. * @returns {String}
  113. */
  114. getString: function () {
  115. return this._labelAtlasRenderer.getString();
  116. },
  117. /**
  118. * Returns the length of string.
  119. * @returns {*|Number|long|int}
  120. */
  121. getStringLength: function(){
  122. return this._labelAtlasRenderer.getStringLength();
  123. },
  124. _onSizeChanged: function () {
  125. ccui.Widget.prototype._onSizeChanged.call(this);
  126. this._labelAtlasRendererAdaptDirty = true;
  127. },
  128. _adaptRenderers: function(){
  129. if (this._labelAtlasRendererAdaptDirty){
  130. this._labelAtlasScaleChangedWithSize();
  131. this._labelAtlasRendererAdaptDirty = false;
  132. }
  133. },
  134. /**
  135. * Returns the renderer's content size
  136. * @overrider
  137. * @returns {cc.Size}
  138. */
  139. getVirtualRendererSize: function(){
  140. return this._labelAtlasRenderer.getContentSize();
  141. },
  142. /**
  143. * Returns the renderer of ccui.TextAtlas.
  144. * @returns {cc.Node}
  145. */
  146. getVirtualRenderer: function () {
  147. return this._labelAtlasRenderer;
  148. },
  149. _labelAtlasScaleChangedWithSize: function () {
  150. var locRenderer = this._labelAtlasRenderer;
  151. if (this._ignoreSize) {
  152. locRenderer.setScale(1.0);
  153. } else {
  154. var textureSize = locRenderer.getContentSize();
  155. if (textureSize.width <= 0.0 || textureSize.height <= 0.0) {
  156. locRenderer.setScale(1.0);
  157. return;
  158. }
  159. locRenderer.setScaleX(this._contentSize.width / textureSize.width);
  160. locRenderer.setScaleY(this._contentSize.height / textureSize.height);
  161. }
  162. locRenderer.setPosition(this._contentSize.width / 2.0, this._contentSize.height / 2.0);
  163. },
  164. /**
  165. * Returns the "class name" of ccui.TextAtlas.
  166. * @returns {string}
  167. */
  168. getDescription: function () {
  169. return "LabelAtlas";
  170. },
  171. _copySpecialProperties: function (labelAtlas) {
  172. if (labelAtlas){
  173. this.setProperty(labelAtlas._stringValue, labelAtlas._charMapFileName, labelAtlas._itemWidth, labelAtlas._itemHeight, labelAtlas._startCharMap);
  174. }
  175. },
  176. _createCloneInstance: function () {
  177. return ccui.TextAtlas.create();
  178. }
  179. });
  180. var _p = ccui.TextAtlas.prototype;
  181. // Extended properties
  182. /** @expose */
  183. _p.string;
  184. cc.defineGetterSetter(_p, "string", _p.getString, _p.setString);
  185. _p = null;
  186. /**
  187. * allocates and initializes a UILabelAtlas.
  188. * @deprecated since v3.0, please use new ccui.TextAtlas() instead.
  189. * @return {ccui.TextAtlas}
  190. * @example
  191. * // example
  192. * var uiLabelAtlas = ccui.TextAtlas.create();
  193. */
  194. ccui.TextAtlas.create = function (stringValue, charMapFile, itemWidth, itemHeight, startCharMap) {
  195. return new ccui.TextAtlas(stringValue, charMapFile, itemWidth, itemHeight, startCharMap);
  196. };
  197. // Constants
  198. /**
  199. * The zOrder value of ccui.TextAtlas's renderer.
  200. * @type {number}
  201. */
  202. ccui.TextAtlas.RENDERER_ZORDER = -1;