LCOV - code coverage report
Current view: top level - source/gui/ObjectTypes - CText.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 104 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 9 0.0 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       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             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      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
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "CText.h"
      21             : 
      22             : #include "gui/CGUI.h"
      23             : #include "gui/CGUIScrollBarVertical.h"
      24             : #include "gui/CGUIText.h"
      25             : #include "gui/IGUIScrollBar.h"
      26             : #include "gui/SGUIMessage.h"
      27             : #include "maths/Rect.h"
      28             : #include "maths/Size2D.h"
      29             : 
      30             : #include <limits>
      31             : #include <memory>
      32             : #include <string>
      33             : #include <utility>
      34             : #include <vector>
      35             : 
      36           0 : CText::CText(CGUI& pGUI)
      37             :     : IGUIObject(pGUI),
      38             :       IGUIScrollBarOwner(*static_cast<IGUIObject*>(this)),
      39             :       IGUITextOwner(*static_cast<IGUIObject*>(this)),
      40           0 :       m_BufferZone(this, "buffer_zone"),
      41           0 :       m_Caption(this, "caption"),
      42           0 :       m_Clip(this, "clip", true),
      43           0 :       m_Font(this, "font"),
      44           0 :       m_ScrollBar(this, "scrollbar", false),
      45           0 :       m_ScrollBarStyle(this, "scrollbar_style"),
      46           0 :       m_ScrollBottom(this, "scroll_bottom"),
      47           0 :       m_ScrollTop(this, "scroll_top"),
      48           0 :       m_Sprite(this, "sprite"),
      49           0 :       m_SpriteOverlay(this, "sprite_overlay"),
      50           0 :       m_TextColor(this, "textcolor"),
      51           0 :       m_TextColorDisabled(this, "textcolor_disabled")
      52             : {
      53             :     // Add scroll-bar
      54           0 :     auto bar = std::make_unique<CGUIScrollBarVertical>(pGUI);
      55           0 :     bar->SetRightAligned(true);
      56           0 :     AddScrollBar(std::move(bar));
      57             : 
      58             :     // Add text
      59           0 :     AddText();
      60           0 : }
      61             : 
      62           0 : void CText::SetupText()
      63             : {
      64           0 :     if (m_GeneratedTexts.empty())
      65             :         return;
      66             : 
      67           0 :     float width = GetActualSize().GetWidth();
      68             :     // remove scrollbar if applicable
      69           0 :     if (m_ScrollBar && GetScrollBar(0).GetStyle())
      70           0 :         width -= GetScrollBar(0).GetStyle()->m_Width;
      71             : 
      72           0 :     m_GeneratedTexts[0] = CGUIText(m_pGUI, m_Caption, m_Font, width, m_BufferZone, m_TextAlign, this);
      73             : 
      74             :     // Setup scrollbar
      75           0 :     if (m_ScrollBar)
      76             :     {
      77             :         // If we are currently scrolled to the bottom of the text,
      78             :         // then add more lines of text, update the scrollbar so we
      79             :         // stick to the bottom.
      80             :         // (Use 1.5px delta so this triggers the first time caption is set)
      81           0 :         bool bottom = false;
      82           0 :         if (m_ScrollBottom && GetScrollBar(0).GetPos() > GetScrollBar(0).GetMaxPos() - 1.5f)
      83             :             bottom = true;
      84             : 
      85           0 :         GetScrollBar(0).SetScrollRange(m_GeneratedTexts[0].GetSize().Height);
      86           0 :         GetScrollBar(0).SetScrollSpace(GetActualSize().GetHeight());
      87             : 
      88           0 :         GetScrollBar(0).SetX(GetActualSize().right);
      89           0 :         GetScrollBar(0).SetY(GetActualSize().top);
      90           0 :         GetScrollBar(0).SetZ(GetBufferedZ());
      91           0 :         GetScrollBar(0).SetLength(GetActualSize().bottom - GetActualSize().top);
      92             : 
      93           0 :         if (bottom)
      94           0 :             GetScrollBar(0).SetPos(GetScrollBar(0).GetMaxPos());
      95             : 
      96           0 :         if (m_ScrollTop)
      97           0 :             GetScrollBar(0).SetPos(0.0f);
      98             :     }
      99             : 
     100           0 :     if (!m_ScrollBar || !GetScrollBar(0).IsVisible())
     101           0 :         CalculateTextPosition(GetActualSize(), m_TextPos, m_GeneratedTexts[0]);
     102             : }
     103             : 
     104           0 : void CText::ResetStates()
     105             : {
     106           0 :     IGUIObject::ResetStates();
     107           0 :     IGUIScrollBarOwner::ResetStates();
     108           0 : }
     109             : 
     110           0 : void CText::HandleSizeChanged()
     111             : {
     112           0 :     IGUIObject::HandleSizeChanged();
     113           0 :     IGUITextOwner::HandleSizeChanged();
     114           0 : }
     115             : 
     116           0 : CSize2D CText::GetTextSize()
     117             : {
     118           0 :     UpdateText();
     119           0 :     return m_GeneratedTexts[0].GetSize();
     120             : }
     121             : 
     122           0 : CSize2D CText::GetPreferredTextSize()
     123             : {
     124           0 :     return CGUIText{m_pGUI, m_Caption, m_Font, std::numeric_limits<float>::max(), m_BufferZone, m_TextAlign, this}.GetSize();
     125             : }
     126             : 
     127           0 : const CStrW& CText::GetTooltipText() const
     128             : {
     129           0 :     for (const CGUIText& text : m_GeneratedTexts)
     130           0 :         for (const CGUIText::STextCall& textChunk : text.GetTextCalls())
     131             :         {
     132           0 :             if (textChunk.m_Tooltip.empty())
     133           0 :                 continue;
     134           0 :             CRect area(textChunk.m_Pos - CVector2D(0.f, textChunk.m_Size.Height), textChunk.m_Size);
     135           0 :             if (area.PointInside(m_pGUI.GetMousePos() - GetActualSize().TopLeft()))
     136           0 :                 return textChunk.m_Tooltip;
     137             :         }
     138           0 :     return m_Tooltip;
     139             : }
     140             : 
     141           0 : void CText::HandleMessage(SGUIMessage& Message)
     142             : {
     143           0 :     IGUIObject::HandleMessage(Message);
     144           0 :     if (m_ScrollBar)
     145           0 :         IGUIScrollBarOwner::HandleMessage(Message);
     146             :     //IGUITextOwner::HandleMessage(Message); <== placed it after the switch instead!
     147             : 
     148           0 :     switch (Message.type)
     149             :     {
     150           0 :     case GUIM_SETTINGS_UPDATED:
     151           0 :         if (Message.value == "scrollbar")
     152           0 :             SetupText();
     153             : 
     154             :         // Update scrollbar
     155           0 :         if (Message.value == "scrollbar_style")
     156             :         {
     157           0 :             GetScrollBar(0).SetScrollBarStyle(m_ScrollBarStyle);
     158           0 :             SetupText();
     159             :         }
     160             : 
     161             :         break;
     162             : 
     163           0 :     case GUIM_LOAD:
     164           0 :     {
     165           0 :         GetScrollBar(0).SetX(GetActualSize().right);
     166           0 :         GetScrollBar(0).SetY(GetActualSize().top);
     167           0 :         GetScrollBar(0).SetZ(GetBufferedZ());
     168           0 :         GetScrollBar(0).SetLength(GetActualSize().bottom - GetActualSize().top);
     169           0 :         GetScrollBar(0).SetScrollBarStyle(m_ScrollBarStyle);
     170           0 :         break;
     171             :     }
     172             : 
     173             :     default:
     174             :         break;
     175             :     }
     176             : 
     177           0 :     IGUITextOwner::HandleMessage(Message);
     178           0 : }
     179             : 
     180           0 : void CText::Draw(CCanvas2D& canvas)
     181             : {
     182           0 :     m_pGUI.DrawSprite(m_Sprite, canvas, GetActualSize(), m_VisibleArea);
     183             : 
     184             :     // Ensure the scrollbars are up-to-date.
     185           0 :     UpdateText();
     186             : 
     187           0 :     float scroll = 0.f;
     188           0 :     if (m_ScrollBar)
     189           0 :         scroll = GetScrollBar(0).GetPos();
     190             : 
     191             :     // Clipping area (we'll have to subtract the scrollbar)
     192           0 :     CRect cliparea = m_VisibleArea ? m_VisibleArea : GetActualSize();
     193           0 :     if (m_Clip)
     194             :     {
     195           0 :         if (m_ScrollBar)
     196             :         {
     197             :             // subtract scrollbar from cliparea
     198           0 :             if (cliparea.right > GetScrollBar(0).GetOuterRect().left &&
     199           0 :                 cliparea.right <= GetScrollBar(0).GetOuterRect().right)
     200           0 :                 cliparea.right = GetScrollBar(0).GetOuterRect().left;
     201             : 
     202           0 :             if (cliparea.left >= GetScrollBar(0).GetOuterRect().left &&
     203           0 :                 cliparea.left < GetScrollBar(0).GetOuterRect().right)
     204           0 :                 cliparea.left = GetScrollBar(0).GetOuterRect().right;
     205             :         }
     206             :     }
     207             : 
     208           0 :     const CGUIColor& color = m_Enabled ? m_TextColor : m_TextColorDisabled;
     209             : 
     210           0 :     if (m_ScrollBar && GetScrollBar(0).IsVisible())
     211             :     {
     212           0 :         DrawText(canvas, 0, color, GetActualSize().TopLeft() - CVector2D(0.f, scroll), cliparea);
     213             :         // Draw scrollbars on top of the content
     214           0 :         IGUIScrollBarOwner::Draw(canvas);
     215             :     }
     216             :     else
     217           0 :         DrawText(canvas, 0, color, m_TextPos, cliparea);
     218             : 
     219             :     // Draw the overlays last
     220           0 :     m_pGUI.DrawSprite(m_SpriteOverlay, canvas, GetActualSize(), m_VisibleArea);
     221           0 : }

Generated by: LCOV version 1.16