CCLabelAtlas.h 4.5 KB

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