| [25140] | 1 | /* Copyright (C) 2021 Wildfire Games.
|
|---|
| [27965] | 2 | * This file is part of 0 A.D.
|
|---|
| [6830] | 3 | *
|
|---|
| [27965] | 4 | * 0 A.D. is free software: you can redistribute it and/or modify
|
|---|
| [6830] | 5 | * it under the terms of the GNU General Public License as published by
|
|---|
| 6 | * the Free Software Foundation, either version 2 of the License, or
|
|---|
| 7 | * (at your option) any later version.
|
|---|
| 8 | *
|
|---|
| [27965] | 9 | * 0 A.D. is distributed in the hope that it will be useful,
|
|---|
| [6830] | 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 12 | * GNU General Public License for more details.
|
|---|
| 13 | *
|
|---|
| 14 | * You should have received a copy of the GNU General Public License
|
|---|
| [27965] | 15 | * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| [6830] | 16 | */
|
|---|
| 17 |
|
|---|
| [16931] | 18 | #include "precompiled.h"
|
|---|
| [141] | 19 |
|
|---|
| [290] | 20 | #include "CText.h"
|
|---|
| [16931] | 21 |
|
|---|
| [22931] | 22 | #include "gui/CGUI.h"
|
|---|
| [22558] | 23 | #include "gui/CGUIScrollBarVertical.h"
|
|---|
| [22976] | 24 | #include "gui/CGUIText.h"
|
|---|
| [141] | 25 |
|
|---|
| [22741] | 26 | CText::CText(CGUI& pGUI)
|
|---|
| [23005] | 27 | : IGUIObject(pGUI),
|
|---|
| [23020] | 28 | IGUIScrollBarOwner(*static_cast<IGUIObject*>(this)),
|
|---|
| 29 | IGUITextOwner(*static_cast<IGUIObject*>(this)),
|
|---|
| [25392] | 30 | m_BufferZone(this, "buffer_zone"),
|
|---|
| 31 | m_Caption(this, "caption"),
|
|---|
| 32 | m_Clip(this, "clip", true),
|
|---|
| 33 | m_Font(this, "font"),
|
|---|
| 34 | m_ScrollBar(this, "scrollbar", false),
|
|---|
| 35 | m_ScrollBarStyle(this, "scrollbar_style"),
|
|---|
| 36 | m_ScrollBottom(this, "scroll_bottom"),
|
|---|
| 37 | m_ScrollTop(this, "scroll_top"),
|
|---|
| 38 | m_Sprite(this, "sprite"),
|
|---|
| [25587] | 39 | m_SpriteOverlay(this, "sprite_overlay"),
|
|---|
| [25392] | 40 | m_TextColor(this, "textcolor"),
|
|---|
| 41 | m_TextColorDisabled(this, "textcolor_disabled")
|
|---|
| [141] | 42 | {
|
|---|
| 43 | // Add scroll-bar
|
|---|
| [25693] | 44 | auto bar = std::make_unique<CGUIScrollBarVertical>(pGUI);
|
|---|
| [141] | 45 | bar->SetRightAligned(true);
|
|---|
| [25693] | 46 | AddScrollBar(std::move(bar));
|
|---|
| [290] | 47 |
|
|---|
| 48 | // Add text
|
|---|
| [22679] | 49 | AddText();
|
|---|
| [141] | 50 | }
|
|---|
| 51 |
|
|---|
| 52 | CText::~CText()
|
|---|
| 53 | {
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| [290] | 56 | void CText::SetupText()
|
|---|
| 57 | {
|
|---|
| [22587] | 58 | if (m_GeneratedTexts.empty())
|
|---|
| [290] | 59 | return;
|
|---|
| 60 |
|
|---|
| [1107] | 61 | float width = m_CachedActualSize.GetWidth();
|
|---|
| [290] | 62 | // remove scrollbar if applicable
|
|---|
| [23005] | 63 | if (m_ScrollBar && GetScrollBar(0).GetStyle())
|
|---|
| [290] | 64 | width -= GetScrollBar(0).GetStyle()->m_Width;
|
|---|
| 65 |
|
|---|
| [25392] | 66 | m_GeneratedTexts[0] = CGUIText(m_pGUI, m_Caption, m_Font, width, m_BufferZone, m_TextAlign, this);
|
|---|
| [290] | 67 |
|
|---|
| [23005] | 68 | if (!m_ScrollBar)
|
|---|
| [22679] | 69 | CalculateTextPosition(m_CachedActualSize, m_TextPos, m_GeneratedTexts[0]);
|
|---|
| [1584] | 70 |
|
|---|
| [290] | 71 | // Setup scrollbar
|
|---|
| [23005] | 72 | if (m_ScrollBar)
|
|---|
| [290] | 73 | {
|
|---|
| [7664] | 74 | // If we are currently scrolled to the bottom of the text,
|
|---|
| 75 | // then add more lines of text, update the scrollbar so we
|
|---|
| 76 | // stick to the bottom.
|
|---|
| 77 | // (Use 1.5px delta so this triggers the first time caption is set)
|
|---|
| 78 | bool bottom = false;
|
|---|
| [23005] | 79 | if (m_ScrollBottom && GetScrollBar(0).GetPos() > GetScrollBar(0).GetMaxPos() - 1.5f)
|
|---|
| [7664] | 80 | bottom = true;
|
|---|
| 81 |
|
|---|
| [25143] | 82 | GetScrollBar(0).SetScrollRange(m_GeneratedTexts[0].GetSize().Height);
|
|---|
| [7664] | 83 | GetScrollBar(0).SetScrollSpace(m_CachedActualSize.GetHeight());
|
|---|
| 84 |
|
|---|
| [7670] | 85 | GetScrollBar(0).SetX(m_CachedActualSize.right);
|
|---|
| 86 | GetScrollBar(0).SetY(m_CachedActualSize.top);
|
|---|
| 87 | GetScrollBar(0).SetZ(GetBufferedZ());
|
|---|
| 88 | GetScrollBar(0).SetLength(m_CachedActualSize.bottom - m_CachedActualSize.top);
|
|---|
| 89 |
|
|---|
| [7664] | 90 | if (bottom)
|
|---|
| 91 | GetScrollBar(0).SetPos(GetScrollBar(0).GetMaxPos());
|
|---|
| [23005] | 92 |
|
|---|
| 93 | if (m_ScrollTop)
|
|---|
| [19036] | 94 | GetScrollBar(0).SetPos(0.0f);
|
|---|
| [290] | 95 | }
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| [23020] | 98 | void CText::ResetStates()
|
|---|
| 99 | {
|
|---|
| 100 | IGUIObject::ResetStates();
|
|---|
| 101 | IGUIScrollBarOwner::ResetStates();
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | void CText::UpdateCachedSize()
|
|---|
| 105 | {
|
|---|
| 106 | IGUIObject::UpdateCachedSize();
|
|---|
| 107 | IGUITextOwner::UpdateCachedSize();
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| [25225] | 110 | CSize2D CText::GetTextSize()
|
|---|
| 111 | {
|
|---|
| 112 | UpdateText();
|
|---|
| 113 | return m_GeneratedTexts[0].GetSize();
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| [25353] | 116 | const CStrW& CText::GetTooltipText() const
|
|---|
| 117 | {
|
|---|
| 118 | for (const CGUIText& text : m_GeneratedTexts)
|
|---|
| 119 | for (const CGUIText::STextCall& textChunk : text.GetTextCalls())
|
|---|
| 120 | {
|
|---|
| 121 | if (textChunk.m_Tooltip.empty())
|
|---|
| 122 | continue;
|
|---|
| 123 | CRect area(textChunk.m_Pos - CVector2D(0.f, textChunk.m_Size.Height), textChunk.m_Size);
|
|---|
| 124 | if (area.PointInside(m_pGUI.GetMousePos() - m_CachedActualSize.TopLeft()))
|
|---|
| 125 | return textChunk.m_Tooltip;
|
|---|
| 126 | }
|
|---|
| 127 | return m_Tooltip;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| [16931] | 130 | void CText::HandleMessage(SGUIMessage& Message)
|
|---|
| [141] | 131 | {
|
|---|
| [23020] | 132 | IGUIObject::HandleMessage(Message);
|
|---|
| [141] | 133 | IGUIScrollBarOwner::HandleMessage(Message);
|
|---|
| [1904] | 134 | //IGUITextOwner::HandleMessage(Message); <== placed it after the switch instead!
|
|---|
| [141] | 135 |
|
|---|
| 136 | switch (Message.type)
|
|---|
| 137 | {
|
|---|
| 138 | case GUIM_SETTINGS_UPDATED:
|
|---|
| [7670] | 139 | if (Message.value == "scrollbar")
|
|---|
| [1904] | 140 | SetupText();
|
|---|
| 141 |
|
|---|
| [290] | 142 | // Update scrollbar
|
|---|
| [7670] | 143 | if (Message.value == "scrollbar_style")
|
|---|
| [141] | 144 | {
|
|---|
| [23005] | 145 | GetScrollBar(0).SetScrollBarStyle(m_ScrollBarStyle);
|
|---|
| [2115] | 146 | SetupText();
|
|---|
| [141] | 147 | }
|
|---|
| [290] | 148 |
|
|---|
| [141] | 149 | break;
|
|---|
| 150 |
|
|---|
| 151 | case GUIM_MOUSE_WHEEL_DOWN:
|
|---|
| [16931] | 152 | {
|
|---|
| [2979] | 153 | GetScrollBar(0).ScrollPlus();
|
|---|
| [290] | 154 | // Since the scroll was changed, let's simulate a mouse movement
|
|---|
| 155 | // to check if scrollbar now is hovered
|
|---|
| [9340] | 156 | SGUIMessage msg(GUIM_MOUSE_MOTION);
|
|---|
| 157 | HandleMessage(msg);
|
|---|
| [141] | 158 | break;
|
|---|
| [16931] | 159 | }
|
|---|
| [141] | 160 | case GUIM_MOUSE_WHEEL_UP:
|
|---|
| [16931] | 161 | {
|
|---|
| [2979] | 162 | GetScrollBar(0).ScrollMinus();
|
|---|
| [290] | 163 | // Since the scroll was changed, let's simulate a mouse movement
|
|---|
| 164 | // to check if scrollbar now is hovered
|
|---|
| [9340] | 165 | SGUIMessage msg(GUIM_MOUSE_MOTION);
|
|---|
| 166 | HandleMessage(msg);
|
|---|
| [141] | 167 | break;
|
|---|
| [16931] | 168 | }
|
|---|
| [1904] | 169 | case GUIM_LOAD:
|
|---|
| [16931] | 170 | {
|
|---|
| 171 | GetScrollBar(0).SetX(m_CachedActualSize.right);
|
|---|
| 172 | GetScrollBar(0).SetY(m_CachedActualSize.top);
|
|---|
| 173 | GetScrollBar(0).SetZ(GetBufferedZ());
|
|---|
| 174 | GetScrollBar(0).SetLength(m_CachedActualSize.bottom - m_CachedActualSize.top);
|
|---|
| [23005] | 175 | GetScrollBar(0).SetScrollBarStyle(m_ScrollBarStyle);
|
|---|
| [1904] | 176 | break;
|
|---|
| [16931] | 177 | }
|
|---|
| [1904] | 178 |
|
|---|
| [141] | 179 | default:
|
|---|
| 180 | break;
|
|---|
| 181 | }
|
|---|
| [1904] | 182 |
|
|---|
| 183 | IGUITextOwner::HandleMessage(Message);
|
|---|
| [141] | 184 | }
|
|---|
| 185 |
|
|---|
| [25591] | 186 | void CText::Draw(CCanvas2D& canvas)
|
|---|
| [141] | 187 | {
|
|---|
| [25591] | 188 | m_pGUI.DrawSprite(m_Sprite, canvas, m_CachedActualSize);
|
|---|
| [22693] | 189 |
|
|---|
| [16931] | 190 | float scroll = 0.f;
|
|---|
| [23005] | 191 | if (m_ScrollBar)
|
|---|
| [16931] | 192 | scroll = GetScrollBar(0).GetPos();
|
|---|
| 193 |
|
|---|
| 194 | // Clipping area (we'll have to subtract the scrollbar)
|
|---|
| 195 | CRect cliparea;
|
|---|
| [23005] | 196 | if (m_Clip)
|
|---|
| [141] | 197 | {
|
|---|
| [16931] | 198 | cliparea = m_CachedActualSize;
|
|---|
| [290] | 199 |
|
|---|
| [23005] | 200 | if (m_ScrollBar)
|
|---|
| [290] | 201 | {
|
|---|
| [16931] | 202 | // subtract scrollbar from cliparea
|
|---|
| 203 | if (cliparea.right > GetScrollBar(0).GetOuterRect().left &&
|
|---|
| 204 | cliparea.right <= GetScrollBar(0).GetOuterRect().right)
|
|---|
| 205 | cliparea.right = GetScrollBar(0).GetOuterRect().left;
|
|---|
| [2532] | 206 |
|
|---|
| [16931] | 207 | if (cliparea.left >= GetScrollBar(0).GetOuterRect().left &&
|
|---|
| 208 | cliparea.left < GetScrollBar(0).GetOuterRect().right)
|
|---|
| 209 | cliparea.left = GetScrollBar(0).GetOuterRect().right;
|
|---|
| [290] | 210 | }
|
|---|
| [16931] | 211 | }
|
|---|
| [290] | 212 |
|
|---|
| [23005] | 213 | const CGUIColor& color = m_Enabled ? m_TextColor : m_TextColorDisabled;
|
|---|
| [290] | 214 |
|
|---|
| [23005] | 215 | if (m_ScrollBar)
|
|---|
| [25591] | 216 | DrawText(canvas, 0, color, m_CachedActualSize.TopLeft() - CVector2D(0.f, scroll), cliparea);
|
|---|
| [16931] | 217 | else
|
|---|
| [25591] | 218 | DrawText(canvas, 0, color, m_TextPos, cliparea);
|
|---|
| [25587] | 219 |
|
|---|
| 220 | // Draw scrollbars on top of the content
|
|---|
| 221 | if (m_ScrollBar)
|
|---|
| [25591] | 222 | IGUIScrollBarOwner::Draw(canvas);
|
|---|
| [25587] | 223 |
|
|---|
| 224 | // Draw the overlays last
|
|---|
| [25591] | 225 | m_pGUI.DrawSprite(m_SpriteOverlay, canvas, m_CachedActualSize);
|
|---|
| [141] | 226 | }
|
|---|