BsTextSprite.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsPrerequisites.h"
  5. #include "BsSprite.h"
  6. #include "BsTextData.h"
  7. #include "BsColor.h"
  8. #include "BsVector2.h"
  9. #include "BsStaticAlloc.h"
  10. namespace bs
  11. {
  12. /** @addtogroup 2D
  13. * @{
  14. */
  15. /** Specifies how is text horizontally aligned within its bounds. */
  16. enum TextHorzAlign
  17. {
  18. THA_Left, THA_Center, THA_Right
  19. };
  20. /** Specifies how is text vertically aligned within its bounds. */
  21. enum TextVertAlign
  22. {
  23. TVA_Top, TVA_Center, TVA_Bottom
  24. };
  25. /** Text sprite description structure used for initializing or updating a text sprite. */
  26. struct TEXT_SPRITE_DESC
  27. {
  28. TEXT_SPRITE_DESC()
  29. :width(0), height(0), anchor(SA_TopLeft), fontSize(0),
  30. horzAlign(THA_Left), vertAlign(TVA_Top), wordWrap(false), wordBreak(true)
  31. { }
  32. UINT32 width; /**< Width of the bounds to render the text within, in pixels. */
  33. UINT32 height; /**< Height of the bounds to render the text within, in pixels. */
  34. SpriteAnchor anchor; /**< Determines how to anchor the text within the bounds. */
  35. WString text; /**< Text to generate geometry for. */
  36. HFont font; /**< Font containing the data about character glyphs. */
  37. UINT32 fontSize; /**< Size of the font to use when displaying the text. */
  38. Color color; /**< Color tint of the text. */
  39. TextHorzAlign horzAlign; /**< Specifies how is text horizontally aligned within its bounds. */
  40. TextVertAlign vertAlign; /**< Specifies how is text vertically aligned within its bounds. */
  41. bool wordWrap; /**< If true the text will word wrap when it doesn't fit, otherwise it will overflow. */
  42. bool wordBreak; /**< If enabled together with word wrap it will allow words to be broken if they don't fit. */
  43. };
  44. /** A sprite consisting of a quads representing a text string. */
  45. class BS_EXPORT TextSprite : public Sprite
  46. {
  47. public:
  48. TextSprite();
  49. ~TextSprite();
  50. /**
  51. * Recreates internal sprite data according the specified description structure.
  52. *
  53. * @param[in] desc Describes the geometry and material of the sprite.
  54. * @param[in] groupId Group identifier that forces different materials to be used for different groups (for
  55. * example you don't want the sprites to share the same group if they use different world
  56. * transform matrices).
  57. */
  58. void update(const TEXT_SPRITE_DESC& desc, UINT64 groupId);
  59. /**
  60. * Calculates and returns offset for each individual text line. The offsets provide information on how much to
  61. * offset the lines within provided bounds.
  62. *
  63. * @param[in] textData Text data to generate offsets for.
  64. * @param[in] width Width of the text bounds into which to constrain the text, in pixels.
  65. * @param[in] height Height of the text bounds into which to constrain the text, in pixels.
  66. * @param[in] horzAlign Specifies how is text horizontally aligned within its bounds.
  67. * @param[in] vertAlign Specifies how is text vertically aligned within its bounds.
  68. * @param[out] output Pre-allocated buffer to output the results in. Buffer must have an element
  69. * for every line in @p textData.
  70. */
  71. static void getAlignmentOffsets(const TextDataBase& textData,
  72. UINT32 width, UINT32 height, TextHorzAlign horzAlign, TextVertAlign vertAlign, Vector2I* output);
  73. /**
  74. * Calculates text quads you may use for text rendering, based on the specified text data. Only generates quads for
  75. * the specified page.
  76. *
  77. * @param[in] page Font page to generate the data for.
  78. * @param[in] textData Text data to generate offsets for.
  79. * @param[in] width Width of the text bounds into which to constrain the text, in pixels.
  80. * @param[in] height Height of the text bounds into which to constrain the text, in pixels.
  81. * @param[in] horzAlign Specifies how is text horizontally aligned within its bounds.
  82. * @param[in] vertAlign Specifies how is text vertically aligned within its bounds.
  83. * @param[in] anchor Determines how to anchor the text within the bounds.
  84. * @param[out] vertices Output buffer containing quad positions. Must be allocated and of adequate size.
  85. * @param[out] uv Output buffer containing quad UV coordinates. Must be allocated and of adequate
  86. * size. Can be null.
  87. * @param[out] indices Output buffer containing quad indices. Must be allocated and of adequate size. Can
  88. * be null.
  89. * @param[in] bufferSizeQuads Size of the output buffers, in number of quads.
  90. * @return Number of generated quads.
  91. */
  92. static UINT32 genTextQuads(UINT32 page, const TextDataBase& textData, UINT32 width, UINT32 height,
  93. TextHorzAlign horzAlign, TextVertAlign vertAlign, SpriteAnchor anchor, Vector2* vertices, Vector2* uv, UINT32* indices,
  94. UINT32 bufferSizeQuads);
  95. /**
  96. * Calculates text quads you may use for text rendering, based on the specified text data. Generates quads for all
  97. * pages.
  98. *
  99. * @param[in] textData Text data to generate offsets for.
  100. * @param[in] width Width of the text bounds into which to constrain the text, in pixels.
  101. * @param[in] height Height of the text bounds into which to constrain the text, in pixels.
  102. * @param[in] horzAlign Specifies how is text horizontally aligned within its bounds.
  103. * @param[in] vertAlign Specifies how is text vertically aligned within its bounds.
  104. * @param[in] anchor Determines how to anchor the text within the bounds.
  105. * @param[out] vertices Output buffer containing quad positions. Must be allocated and of adequate size.
  106. * @param[out] uv Output buffer containing quad UV coordinates. Must be allocated and of adequate
  107. * size. Can be null.
  108. * @param[out] indices Output buffer containing quad indices. Must be allocated and of adequate size. Can
  109. * be null.
  110. * @param[in] bufferSizeQuads Size of the output buffers, in number of quads.
  111. * @return Number of generated quads.
  112. */
  113. static UINT32 genTextQuads(const TextDataBase& textData, UINT32 width, UINT32 height,
  114. TextHorzAlign horzAlign, TextVertAlign vertAlign, SpriteAnchor anchor, Vector2* vertices, Vector2* uv, UINT32* indices,
  115. UINT32 bufferSizeQuads);
  116. private:
  117. static const int STATIC_CHARS_TO_BUFFER = 25;
  118. static const int STATIC_BUFFER_SIZE = STATIC_CHARS_TO_BUFFER * (4 * (2 * sizeof(Vector2)) + (6 * sizeof(UINT32)));
  119. /** Clears internal geometry buffers. */
  120. void clearMesh();
  121. mutable StaticAlloc<STATIC_BUFFER_SIZE, STATIC_BUFFER_SIZE> mAlloc;
  122. };
  123. /** @} */
  124. }