UiTextComponentOffsetsSelector.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 "UiTextComponent.h"
  10. #include <AzCore/std/containers/stack.h>
  11. ////////////////////////////////////////////////////////////////////////////////////////////////////
  12. //! \brief Helper class for calculating offsets for visualizing multi-line selection.
  13. struct UiTextComponentOffsetsSelector
  14. {
  15. UiTextComponentOffsetsSelector(
  16. const UiTextComponent::DrawBatchLines& drawBatchLines,
  17. const STextDrawContext& fontContext,
  18. float fontSize,
  19. int firstIndex,
  20. int lastIndex,
  21. int lastIndexLineNumber,
  22. int lineNumHint)
  23. : m_drawBatchLines(drawBatchLines)
  24. , m_fontContext(fontContext)
  25. , m_fontSize(fontSize)
  26. , m_maxLineHeight(0.0f)
  27. , m_firstIndex(firstIndex)
  28. , m_lastIndex(lastIndex)
  29. , m_lastIndexLineNumber(lastIndexLineNumber)
  30. , m_numLines(static_cast<int>(m_drawBatchLines.batchLines.size()))
  31. , m_indexIter(0)
  32. , m_numCharsSelected(0)
  33. , m_lineCounter(0)
  34. , m_lineNumHint(lineNumHint)
  35. {
  36. }
  37. //! \brief Parses all the DrawBatch string content of a DrawBatchLine for offsets calculation.
  38. void ParseBatchLine(const UiTextComponent::DrawBatchLine& batchLine, float& curLineWidth);
  39. //! \brief Handles top and middle offset section cases.
  40. void HandleTopAndMiddleOffsets();
  41. //! \brief Condition y-offset incrementing for whatever is on the top of the stack.
  42. void IncrementYOffsets();
  43. //! \brief Parses entirety of DrawBatchLines of text and assigns values to top, middle, and bottom offsets accordingly.
  44. void CalculateOffsets(UiTextComponent::LineOffsets& top, UiTextComponent::LineOffsets& middle, UiTextComponent::LineOffsets& bottom);
  45. AZStd::stack<UiTextComponent::LineOffsets*> lineOffsetsStack; //!< A multi-line selection can be divided into three offsets: the first
  46. //!< line (top), the last line (bottom), and a multi-line middle section
  47. //!< that is basically a rect.
  48. //!<
  49. //!< Each LineOffset contains a Vec2 for left and right offsets. The left
  50. //!< offset is "absolute" for the element rect whereas the right offset is
  51. //!< relative to the left offset.
  52. const UiTextComponent::DrawBatchLines& m_drawBatchLines;
  53. const STextDrawContext& m_fontContext;
  54. const float m_fontSize;
  55. float m_maxLineHeight;
  56. const int m_firstIndex;
  57. const int m_lastIndex;
  58. const int m_lastIndexLineNumber; //!< Used to determine location within "middle" section
  59. const int m_numLines;
  60. int m_indexIter; //!< Index for iterating over displaying string (m_locText)
  61. int m_numCharsSelected;
  62. int m_lineCounter;
  63. int m_lineNumHint;
  64. bool firstLine = true;
  65. bool firstIndexFound = false;
  66. bool lastIndexFound = false;
  67. bool firstAndLastIndexOccurOnDifferentLines = false;
  68. };