1
0

CCLabelTTF.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2013-2017 Chukong Technologies 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. #ifndef __CCLABELTTF_H__
  23. #define __CCLABELTTF_H__
  24. /// @cond DO_NOT_SHOW
  25. #include "2d/CCNode.h"
  26. NS_CC_BEGIN
  27. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  28. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  29. #elif _MSC_VER >= 1400 //vs 2005 or higher
  30. #pragma warning (push)
  31. #pragma warning (disable: 4996)
  32. #endif
  33. /// @cond
  34. class Label;
  35. /**
  36. * @addtogroup _2d
  37. * @{
  38. */
  39. /** @brief LabelTTF is a subclass of TextureNode that knows how to render text labels
  40. *
  41. * All features from TextureNode are valid in LabelTTF
  42. *
  43. * LabelTTF objects are slow. Consider using LabelAtlas or LabelBMFont instead.
  44. *
  45. * Custom ttf file can be put in assets/ or external storage that the Application can access.
  46. * @code
  47. * LabelTTF *label1 = LabelTTF::create("alignment left", "A Damn Mess", fontSize, blockSize,
  48. * TextHAlignment::LEFT, TextVAlignment::CENTER);
  49. * LabelTTF *label2 = LabelTTF::create("alignment right", "/mnt/sdcard/Scissor Cuts.ttf", fontSize, blockSize,
  50. * TextHAlignment::LEFT, TextVAlignment::CENTER);
  51. * @endcode
  52. *
  53. */
  54. class CC_DLL CC_DEPRECATED_ATTRIBUTE LabelTTF : public Node, public LabelProtocol, public BlendProtocol
  55. {
  56. public:
  57. /**
  58. * @js ctor
  59. */
  60. LabelTTF();
  61. /**
  62. * @js NA
  63. * @lua NA
  64. */
  65. virtual ~LabelTTF();
  66. /** creates a Label from a fontname, alignment, dimension in points and font size in points
  67. @since v2.0.1
  68. */
  69. static LabelTTF * create(const std::string& string, const std::string& fontName, float fontSize,
  70. const Size& dimensions = Size::ZERO, TextHAlignment hAlignment = TextHAlignment::CENTER,
  71. TextVAlignment vAlignment = TextVAlignment::TOP);
  72. /** Create a label with string and a font definition*/
  73. static LabelTTF * createWithFontDefinition(const std::string& string, FontDefinition &textDefinition);
  74. /** initializes the LabelTTF with a font name, alignment, dimension and font size */
  75. bool initWithString(const std::string& string, const std::string& fontName, float fontSize,
  76. const Size& dimensions = Size::ZERO, TextHAlignment hAlignment = TextHAlignment::LEFT,
  77. TextVAlignment vAlignment = TextVAlignment::TOP);
  78. /** initializes the LabelTTF with a font name, alignment, dimension and font size */
  79. bool initWithStringAndTextDefinition(const std::string& string, FontDefinition &textDefinition);
  80. /** set the text definition used by this label */
  81. void setTextDefinition(const FontDefinition& theDefinition);
  82. /** get the text definition used by this label */
  83. const FontDefinition& getTextDefinition();
  84. /** enable or disable shadow for the label */
  85. void enableShadow(const Size &shadowOffset, float shadowOpacity, float shadowBlur, bool mustUpdateTexture = true);
  86. /** disable shadow rendering */
  87. void disableShadow(bool mustUpdateTexture = true);
  88. /** enable or disable stroke */
  89. void enableStroke(const Color3B &strokeColor, float strokeSize, bool mustUpdateTexture = true);
  90. /** disable stroke */
  91. void disableStroke(bool mustUpdateTexture = true);
  92. /** set text tinting */
  93. void setFontFillColor(const Color3B &tintColor, bool mustUpdateTexture = true);
  94. /** Creates an label.
  95. */
  96. static LabelTTF * create();
  97. /** changes the string to render
  98. * @warning Changing the string is as expensive as creating a new LabelTTF. To obtain better performance use LabelAtlas
  99. */
  100. virtual void setString(const std::string &label) override;
  101. virtual const std::string& getString(void) const override ;
  102. TextHAlignment getHorizontalAlignment() const;
  103. void setHorizontalAlignment(TextHAlignment alignment);
  104. TextVAlignment getVerticalAlignment() const;
  105. void setVerticalAlignment(TextVAlignment verticalAlignment);
  106. const Size& getDimensions() const;
  107. void setDimensions(const Size &dim);
  108. float getFontSize() const;
  109. void setFontSize(float fontSize);
  110. const std::string& getFontName() const;
  111. void setFontName(const std::string& fontName);
  112. virtual void setBlendFunc(const BlendFunc &blendFunc) override;
  113. virtual const BlendFunc &getBlendFunc() const override;
  114. virtual void setFlippedX(bool flippedX);
  115. virtual void setFlippedY(bool flippedY);
  116. virtual Rect getBoundingBox() const override;
  117. /**
  118. * @js NA
  119. * @lua NA
  120. */
  121. virtual std::string getDescription() const override;
  122. virtual void visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags) override;
  123. virtual const Size& getContentSize() const override;
  124. protected:
  125. Label* _renderLabel;
  126. bool _contentDirty;
  127. FontDefinition _fontDef;
  128. };
  129. // end of group
  130. /// @}
  131. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  132. #pragma GCC diagnostic warning "-Wdeprecated-declarations"
  133. #elif _MSC_VER >= 1400 //vs 2005 or higher
  134. #pragma warning (pop)
  135. #endif
  136. NS_CC_END
  137. /// @endcond
  138. #endif //__CCLABEL_H__