3
0

TextMarkup.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include "LyShine.h"
  10. #include <AzCore/std/containers/list.h>
  11. struct IConsoleCmdArgs;
  12. namespace TextMarkup
  13. {
  14. //! Default color value for font color attribute (represents unassigned state)
  15. static const AZ::Vector3 ColorInvalid(-1.0f, -1.0f, -1.0f);
  16. ////////////////////////////////////////////////////////////////////////////////////////////////////
  17. //! Different tag types supported by TextMarkup
  18. enum class TagType
  19. {
  20. Root,
  21. Text,
  22. Bold,
  23. Italic,
  24. Anchor,
  25. Font,
  26. Image
  27. };
  28. ////////////////////////////////////////////////////////////////////////////////////////////////////
  29. //! Tag base class. All tags can have child tags.
  30. struct Tag
  31. {
  32. Tag() { }
  33. virtual ~Tag()
  34. {
  35. for (auto child : children)
  36. {
  37. delete child;
  38. }
  39. }
  40. // Copying and assignment disallowed
  41. Tag(const Tag&) = delete;
  42. Tag(const Tag&&) = delete;
  43. Tag& operator=(const Tag&) = delete;
  44. Tag& operator=(const Tag&&) = delete;
  45. //! There should only ever be one root tag in an TextMarkup tree (a root tag should never be a child of another tag).
  46. virtual TagType GetType() const { return TagType::Root; };
  47. AZStd::list<Tag*> children; //!< List of child tags
  48. };
  49. ////////////////////////////////////////////////////////////////////////////////////////////////////
  50. //! Contains text data
  51. struct TextTag
  52. : public Tag
  53. {
  54. TagType GetType() const override { return TagType::Text; }
  55. AZStd::string text;
  56. };
  57. ////////////////////////////////////////////////////////////////////////////////////////////////////
  58. //! Indicates that child elements should be bolded
  59. struct BoldTag
  60. : public Tag
  61. {
  62. TagType GetType() const override { return TagType::Bold; }
  63. };
  64. ////////////////////////////////////////////////////////////////////////////////////////////////////
  65. //! Indicates that child elements should be italicized
  66. struct ItalicTag
  67. : public Tag
  68. {
  69. TagType GetType() const override { return TagType::Italic; }
  70. };
  71. ////////////////////////////////////////////////////////////////////////////////////////////////////
  72. //! Defines clickable regions of text (links)
  73. struct AnchorTag
  74. : public Tag
  75. {
  76. TagType GetType() const override { return TagType::Anchor; }
  77. AZStd::string action;
  78. AZStd::string data;
  79. };
  80. ////////////////////////////////////////////////////////////////////////////////////////////////////
  81. //! Allows modifying font display properties, such as face and color
  82. struct FontTag
  83. : public Tag
  84. {
  85. TagType GetType() const override { return TagType::Font; }
  86. AZStd::string face;
  87. AZ::Vector3 color;
  88. };
  89. ////////////////////////////////////////////////////////////////////////////////////////////////////
  90. //! Contains data to display an image
  91. struct ImageTag
  92. : public Tag
  93. {
  94. TagType GetType() const override { return TagType::Image; }
  95. AZStd::string m_imagePathname;
  96. AZStd::string m_height; // an absolute value or a string identifying how to calculate the height
  97. float m_scale;
  98. AZStd::string m_vAlign;
  99. float m_yOffset;
  100. float m_leftPadding;
  101. float m_rightPadding;
  102. };
  103. //! Takes markup text and populates an markup tag tree from it.
  104. bool ParseMarkupBuffer(const AZStd::string& sourceBuffer, TextMarkup::Tag& markupTag, bool suppressWarnings = false);
  105. //! Takes a source markup buffer and dumps only the character data to the target buffer.
  106. void CopyCharData(const AZStd::string& sourceBuffer, AZStd::string& targetBuffer);
  107. #if defined(LYSHINE_INTERNAL_UNIT_TEST)
  108. void UnitTest(IConsoleCmdArgs* cmdArgs);
  109. #endif
  110. }