| 1 | /* |
| 2 | * Copyright (C) 2005-2018 Team Kodi |
| 3 | * This file is part of Kodi - https://kodi.tv |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 6 | * See LICENSES/README.md for more information. |
| 7 | */ |
| 8 | |
| 9 | #pragma once |
| 10 | |
| 11 | /*! |
| 12 | \file GUILabel.h |
| 13 | \brief |
| 14 | */ |
| 15 | |
| 16 | #include "GUIFont.h" |
| 17 | #include "GUITextLayout.h" |
| 18 | #include "guiinfo/GUIInfoColor.h" |
| 19 | #include "utils/ColorUtils.h" |
| 20 | #include "utils/Geometry.h" |
| 21 | |
| 22 | class CLabelInfo |
| 23 | { |
| 24 | public: |
| 25 | CLabelInfo(): |
| 26 | scrollSuffix(" | ") |
| 27 | { |
| 28 | font = NULL; |
| 29 | align = XBFONT_LEFT; |
| 30 | offsetX = offsetY = 0; |
| 31 | width = 0; |
| 32 | angle = 0; |
| 33 | scrollSpeed = CScrollInfo::defaultSpeed; |
| 34 | }; |
| 35 | bool UpdateColors() |
| 36 | { |
| 37 | bool changed = false; |
| 38 | |
| 39 | changed |= textColor.Update(); |
| 40 | changed |= shadowColor.Update(); |
| 41 | changed |= selectedColor.Update(); |
| 42 | changed |= disabledColor.Update(); |
| 43 | changed |= focusedColor.Update(); |
| 44 | changed |= invalidColor.Update(); |
| 45 | |
| 46 | return changed; |
| 47 | }; |
| 48 | |
| 49 | KODI::GUILIB::GUIINFO::CGUIInfoColor textColor; |
| 50 | KODI::GUILIB::GUIINFO::CGUIInfoColor shadowColor; |
| 51 | KODI::GUILIB::GUIINFO::CGUIInfoColor selectedColor; |
| 52 | KODI::GUILIB::GUIINFO::CGUIInfoColor disabledColor; |
| 53 | KODI::GUILIB::GUIINFO::CGUIInfoColor focusedColor; |
| 54 | KODI::GUILIB::GUIINFO::CGUIInfoColor invalidColor; |
| 55 | uint32_t align; |
| 56 | float offsetX; |
| 57 | float offsetY; |
| 58 | float width; |
| 59 | float angle; |
| 60 | CGUIFont *font; |
| 61 | int scrollSpeed; |
| 62 | std::string scrollSuffix; |
| 63 | }; |
| 64 | |
| 65 | /*! |
| 66 | \ingroup controls, labels |
| 67 | \brief Class for rendering text labels. Handles alignment and rendering of text within a control. |
| 68 | */ |
| 69 | class CGUILabel |
| 70 | { |
| 71 | public: |
| 72 | /*! \brief allowed color categories for labels, as defined by the skin |
| 73 | */ |
| 74 | enum COLOR { COLOR_TEXT = 0, |
| 75 | COLOR_SELECTED, |
| 76 | COLOR_FOCUSED, |
| 77 | COLOR_DISABLED, |
| 78 | COLOR_INVALID }; |
| 79 | |
| 80 | /*! \brief allowed overflow handling techniques for labels, as defined by the skin |
| 81 | */ |
| 82 | enum OVER_FLOW |
| 83 | { |
| 84 | OVER_FLOW_TRUNCATE = 0, // Truncated text from right (text end with ellipses) |
| 85 | OVER_FLOW_SCROLL, |
| 86 | OVER_FLOW_WRAP, |
| 87 | OVER_FLOW_CLIP, |
| 88 | OVER_FLOW_TRUNCATE_LEFT // Truncated text from left (text start with ellipses) |
| 89 | }; |
| 90 | |
| 91 | CGUILabel(float posX, float posY, float width, float height, const CLabelInfo& labelInfo, OVER_FLOW overflow = OVER_FLOW_TRUNCATE); |
| 92 | CGUILabel(const CGUILabel& label); |
| 93 | |
| 94 | virtual ~CGUILabel() = default; |
| 95 | |
| 96 | /*! \brief Process the label |
| 97 | \return bool stating if process caused control to change |
| 98 | */ |
| 99 | bool Process(unsigned int currentTime); |
| 100 | |
| 101 | /*! \brief Render the label on screen |
| 102 | */ |
| 103 | void Render(); |
| 104 | |
| 105 | /*! \brief Set the maximal extent of the label |
| 106 | Sets the maximal size and positioning that the label may render in. Note that `textwidth>` can override |
| 107 | this, and `<textoffsetx>` and `<textoffsety>` may also allow the label to be moved outside this rectangle. |
| 108 | */ |
| 109 | bool SetMaxRect(float x, float y, float w, float h); |
| 110 | |
| 111 | bool SetAlign(uint32_t align); |
| 112 | |
| 113 | /*! \brief Set the text to be displayed in the label |
| 114 | Updates the label control and recomputes final position and size |
| 115 | \param text std::string to set as this labels text |
| 116 | \sa SetTextW, SetStyledText |
| 117 | */ |
| 118 | bool SetText(const std::string &label); |
| 119 | |
| 120 | /*! \brief Set the text to be displayed in the label |
| 121 | Updates the label control and recomputes final position and size |
| 122 | \param text std::wstring to set as this labels text |
| 123 | \sa SetText, SetStyledText |
| 124 | */ |
| 125 | bool SetTextW(const std::wstring &label); |
| 126 | |
| 127 | /*! \brief Set styled text to be displayed in the label |
| 128 | Updates the label control and recomputes final position and size |
| 129 | \param text styled text to set. |
| 130 | \param colors colors referenced in the styled text. |
| 131 | \sa SetText, SetTextW |
| 132 | */ |
| 133 | bool SetStyledText(const vecText& text, const std::vector<UTILS::COLOR::Color>& colors); |
| 134 | |
| 135 | /*! \brief Set the color to use for the label |
| 136 | Sets the color to be used for this label. Takes effect at the next render |
| 137 | \param color color to be used for the label |
| 138 | */ |
| 139 | bool SetColor(COLOR color); |
| 140 | |
| 141 | /*! \brief Set the final layout of the current text |
| 142 | Overrides the calculated layout of the current text, forcing a particular size and position |
| 143 | \param rect CRect containing the extents of the current text |
| 144 | \sa GetRenderRect, UpdateRenderRect |
| 145 | */ |
| 146 | void SetRenderRect(const CRect& rect) { m_renderRect = rect; } |
| 147 | |
| 148 | /*! \brief Set whether or not this label control should scroll |
| 149 | \param scrolling true if this label should scroll. |
| 150 | */ |
| 151 | bool SetScrolling(bool scrolling); |
| 152 | |
| 153 | /*! \brief Set max. text scroll count |
| 154 | */ |
| 155 | void SetScrollLoopCount(unsigned int loopCount) { m_maxScrollLoops = loopCount; } |
| 156 | |
| 157 | /*! \brief Set how this label should handle overflowing text. |
| 158 | \param overflow the overflow type |
| 159 | \sa OVER_FLOW |
| 160 | */ |
| 161 | bool SetOverflow(OVER_FLOW overflow); |
| 162 | |
| 163 | /*! \brief Set this label invalid. Forces an update of the control |
| 164 | */ |
| 165 | void SetInvalid(); |
| 166 | |
| 167 | /*! \brief Update this labels colors |
| 168 | */ |
| 169 | bool UpdateColors(); |
| 170 | |
| 171 | /*! \brief Returns the precalculated final layout of the current text |
| 172 | \return CRect containing the extents of the current text |
| 173 | \sa SetRenderRect, UpdateRenderRect |
| 174 | */ |
| 175 | const CRect& GetRenderRect() const { return m_renderRect; } |
| 176 | |
| 177 | /*! \brief Returns the precalculated full width of the current text, regardless of layout |
| 178 | \return full width of the current text |
| 179 | \sa CalcTextWidth |
| 180 | */ |
| 181 | float GetTextWidth() const { return m_textLayout.GetTextWidth(); } |
| 182 | |
| 183 | /*! \brief Returns the maximal width that this label can render into |
| 184 | \return Maximal width that this label can render into. Note that this may differ from the |
| 185 | amount given in SetMaxRect as offsets and text width overrides have been taken into account. |
| 186 | \sa SetMaxRect |
| 187 | */ |
| 188 | float GetMaxWidth() const; |
| 189 | |
| 190 | /*! \brief Calculates the width of some text |
| 191 | \param text std::wstring of text whose width we want |
| 192 | \return width of the given text |
| 193 | \sa GetTextWidth |
| 194 | */ |
| 195 | float CalcTextWidth(const std::wstring& text) const { return m_textLayout.GetTextWidth(text); } |
| 196 | |
| 197 | const CLabelInfo& GetLabelInfo() const { return m_label; } |
| 198 | CLabelInfo& GetLabelInfo() { return m_label; } |
| 199 | |
| 200 | /*! \brief Check a left aligned and right aligned label for overlap and cut the labels off so that no overlap occurs |
| 201 | |
| 202 | If a left-aligned label occupies some of the same space on screen as a right-aligned label, then we may be able to |
| 203 | correct for this by restricting the width of one or both of them. This routine checks two labels to see whether they |
| 204 | satisfy this assumption and, if so, adjusts the render rect of both labels so that they no longer do so. The order |
| 205 | of the two labels is not important, but we do assume that the left-aligned label is also the left-most on screen, and |
| 206 | that the right-aligned label is the right most on-screen, so that they overlap due to the fact that one or both of |
| 207 | the labels are longer than anticipated. In the following diagram, [R...[R R] refers to the maximal allowed and |
| 208 | actual space occupied by the right label. Similarly, [L L]...L] refers to the maximal and actual space occupied |
| 209 | by the left label. | refers to the central cutting point, i.e. the point that would divide the maximal allowed |
| 210 | overlap perfectly in two. There are 3 scenarios to consider: |
| 211 | |
| 212 | cut |
| 213 | [L [R...[R L].|..........L] R] left label ends to the left of the cut -> just crop the left label. |
| 214 | [L [R.....[R | L]..L] R] both left and right labels occupy more than the cut allows, so crop both. |
| 215 | [L [R..........|.[R L]..L] R] right label ends to the right of the cut -> just crop the right label. |
| 216 | |
| 217 | \param label1 First label to check |
| 218 | \param label2 Second label to check |
| 219 | */ |
| 220 | static bool CheckAndCorrectOverlap(CGUILabel &label1, CGUILabel &label2); |
| 221 | |
| 222 | protected: |
| 223 | UTILS::COLOR::Color GetColor() const; |
| 224 | |
| 225 | /*! \brief Computes the final layout of the text |
| 226 | Uses the maximal position and width of the text, as well as the text length |
| 227 | and alignment to compute the final render rect of the text. |
| 228 | \sa GetRenderRect, SetRenderRect |
| 229 | */ |
| 230 | void UpdateRenderRect(); |
| 231 | |
| 232 | private: |
| 233 | CLabelInfo m_label; |
| 234 | CGUITextLayout m_textLayout; |
| 235 | |
| 236 | bool m_scrolling; |
| 237 | OVER_FLOW m_overflowType; |
| 238 | CScrollInfo m_scrollInfo; |
| 239 | CRect m_renderRect; ///< actual sizing of text |
| 240 | CRect m_maxRect; ///< maximum sizing of text |
| 241 | bool m_invalid = true; ///< if true, the label needs recomputing |
| 242 | COLOR m_color = COLOR_TEXT; ///< color to render text \sa SetColor, GetColor |
| 243 | unsigned int m_maxScrollLoops = ~0U; |
| 244 | }; |