CCLabelAtlas.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2017 Chukong Technologies Inc.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. #ifndef __CCLABEL_ATLAS_H__
  24. #define __CCLABEL_ATLAS_H__
  25. #include "2d/CCAtlasNode.h"
  26. #if CC_LABELATLAS_DEBUG_DRAW
  27. #include "renderer/CCCustomCommand.h"
  28. #include "2d/CCDrawNode.h"
  29. #endif
  30. NS_CC_BEGIN
  31. /**
  32. * @addtogroup _2d
  33. * @{
  34. */
  35. /**
  36. * @class LabelAtlas
  37. * @brief LabelAtlas is a subclass of AtlasNode.
  38. *
  39. * It can be as a replacement of Label since it is MUCH faster.
  40. *
  41. * LabelAtlas versus Label:
  42. * - LabelAtlas is MUCH faster than Label.
  43. * - LabelAtlas "characters" have a fixed height and width.
  44. * - LabelAtlas "characters" can be anything you want since they are taken from an image file.
  45. *
  46. * A more flexible class is LabelBMFont. It supports variable width characters and it also has a nice editor.
  47. */
  48. class CC_DLL LabelAtlas : public AtlasNode, public LabelProtocol
  49. {
  50. public:
  51. /**
  52. * Creates an empty LabelAtlas.
  53. * User need to call initWithString(...) later to make this object work properly.
  54. */
  55. static LabelAtlas* create();
  56. /** Creates the LabelAtlas with a string, a char map file(the atlas), the width and height of each element and the starting char of the atlas. */
  57. static LabelAtlas* create(const std::string& string, const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap);
  58. /**
  59. * Creates the LabelAtlas with a string and a configuration file.
  60. * @since v2.0
  61. */
  62. static LabelAtlas* create(const std::string& string, const std::string& fntFile);
  63. /** Initializes the LabelAtlas with a string, a char map file(the atlas), the width and height of each element and the starting char of the atlas. */
  64. bool initWithString(const std::string& string, const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap);
  65. /**
  66. * Initializes the LabelAtlas with a string and a configuration file.
  67. * @since v2.0
  68. */
  69. bool initWithString(const std::string& string, const std::string& fntFile);
  70. /** Initializes the LabelAtlas with a string, a texture, the width and height in points of each element and the starting char of the atlas */
  71. bool initWithString(const std::string& string, Texture2D* texture, int itemWidth, int itemHeight, int startCharMap);
  72. virtual void setString(const std::string &label) override;
  73. virtual const std::string& getString(void) const override;
  74. virtual void updateAtlasValues() override;
  75. /**
  76. * @js NA
  77. */
  78. virtual std::string getDescription() const override;
  79. #if CC_LABELATLAS_DEBUG_DRAW
  80. virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override;
  81. #endif
  82. CC_CONSTRUCTOR_ACCESS:
  83. LabelAtlas()
  84. :_string("")
  85. {
  86. #if CC_LABELATLAS_DEBUG_DRAW
  87. _debugDrawNode = DrawNode::create();
  88. addChild(_debugDrawNode);
  89. #endif
  90. }
  91. virtual ~LabelAtlas()
  92. {
  93. _string.clear();
  94. }
  95. protected:
  96. virtual void updateColor() override;
  97. #if CC_LABELATLAS_DEBUG_DRAW
  98. DrawNode *_debugDrawNode;
  99. #endif
  100. // string to render
  101. std::string _string;
  102. // the first char in the char map
  103. int _mapStartChar;
  104. };
  105. // end group
  106. /// @}
  107. NS_CC_END
  108. #endif //__CCLABEL_ATLAS_H__