LCOV - code coverage report
Current view: top level - source/gui/ObjectTypes - CChart.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 135 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 10 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 "CChart.h"
      21             : 
      22             : #include "graphics/Canvas2D.h"
      23             : #include "gui/CGUIText.h"
      24             : #include "gui/SGUIMessage.h"
      25             : #include "gui/SettingTypes/CGUIList.h"
      26             : #include "gui/SettingTypes/CGUISeries.h"
      27             : #include "gui/SettingTypes/CGUIString.h"
      28             : #include "lib/utf8.h"
      29             : #include "maths/Rect.h"
      30             : #include "maths/Size2D.h"
      31             : #include "ps/CLogger.h"
      32             : #include "ps/Profile.h"
      33             : 
      34             : #include <cmath>
      35             : #include <cwchar>
      36             : #include <limits>
      37             : #include <optional>
      38             : #include <string>
      39             : 
      40             : class CGUI;
      41             : 
      42           0 : CChart::CChart(CGUI& pGUI)
      43             :     : IGUIObject(pGUI),
      44             :       IGUITextOwner(*static_cast<IGUIObject*>(this)),
      45           0 :       m_AxisColor(this, "axis_color"),
      46           0 :       m_AxisWidth(this, "axis_width"),
      47           0 :       m_BufferZone(this, "buffer_zone"),
      48           0 :       m_Font(this, "font"),
      49           0 :       m_FormatX(this, "format_x"),
      50           0 :       m_FormatY(this, "format_y"),
      51           0 :       m_SeriesColor(this, "series_color"),
      52           0 :       m_SeriesSetting(this, "series")
      53             : {
      54           0 : }
      55             : 
      56           0 : void CChart::HandleSizeChanged()
      57             : {
      58           0 :     IGUIObject::HandleSizeChanged();
      59           0 :     IGUITextOwner::HandleSizeChanged();
      60           0 : }
      61             : 
      62           0 : void CChart::HandleMessage(SGUIMessage& Message)
      63             : {
      64           0 :     IGUIObject::HandleMessage(Message);
      65             :     // IGUITextOwner::HandleMessage(Message); performed in UpdateSeries
      66             : 
      67             :     // TODO: implement zoom
      68           0 :     if(Message.type == GUIM_SETTINGS_UPDATED)
      69           0 :         UpdateSeries();
      70           0 : }
      71             : 
      72           0 : void CChart::DrawAxes(CCanvas2D& canvas) const
      73             : {
      74           0 :     canvas.DrawRect(CRect(
      75           0 :         GetActualSize().TopLeft(),
      76           0 :         GetActualSize().BottomLeft() + CVector2D(m_AxisWidth, 0.0f)), m_AxisColor);
      77           0 :     canvas.DrawRect(CRect(
      78           0 :         GetActualSize().BottomLeft() - CVector2D(0.0f, m_AxisWidth),
      79           0 :         GetActualSize().BottomRight()), m_AxisColor);
      80           0 : }
      81             : 
      82           0 : void CChart::Draw(CCanvas2D& canvas)
      83             : {
      84           0 :     PROFILE3("render chart");
      85             : 
      86           0 :     if (m_Series.empty())
      87           0 :         return;
      88             : 
      89           0 :     std::optional<CCanvas2D::ScopedScissor> scissor;
      90           0 :     if (m_VisibleArea)
      91           0 :         scissor.emplace(canvas, m_VisibleArea);
      92             : 
      93           0 :     CRect rect = GetChartRect();
      94           0 :     const float width = rect.GetWidth();
      95           0 :     const float height = rect.GetHeight();
      96             : 
      97           0 :     CVector2D scale(width / (m_RightTop.X - m_LeftBottom.X), height / (m_RightTop.Y - m_LeftBottom.Y));
      98           0 :     std::vector<CVector2D> linePoints;
      99           0 :     for (const CChartData& data : m_Series)
     100             :     {
     101           0 :         if (data.m_Points.empty())
     102           0 :             continue;
     103             : 
     104           0 :         linePoints.clear();
     105           0 :         for (const CVector2D& point : data.m_Points)
     106             :         {
     107           0 :             if (fabs(point.X) != std::numeric_limits<float>::infinity() && fabs(point.Y) != std::numeric_limits<float>::infinity())
     108             :             {
     109           0 :                 linePoints.emplace_back(
     110           0 :                     rect.left + (point.X - m_LeftBottom.X) * scale.X,
     111           0 :                     rect.bottom - (point.Y - m_LeftBottom.Y) * scale.Y);
     112             :             }
     113             :             else
     114             :             {
     115           0 :                 canvas.DrawLine(linePoints, 2.0f, data.m_Color);
     116           0 :                 linePoints.clear();
     117             :             }
     118             :         }
     119           0 :         if (!linePoints.empty())
     120           0 :             canvas.DrawLine(linePoints, 2.0f, data.m_Color);
     121             :     }
     122             : 
     123           0 :     if (m_AxisWidth > 0)
     124           0 :         DrawAxes(canvas);
     125             : 
     126           0 :     for (size_t i = 0; i < m_TextPositions.size(); ++i)
     127           0 :         DrawText(canvas, i, CGUIColor(1.f, 1.f, 1.f, 1.f), m_TextPositions[i], m_VisibleArea);
     128           0 : }
     129             : 
     130           0 : CRect CChart::GetChartRect() const
     131             : {
     132           0 :     return CRect(
     133           0 :         GetActualSize().TopLeft() + CVector2D(m_AxisWidth, m_AxisWidth),
     134           0 :         GetActualSize().BottomRight() - CVector2D(m_AxisWidth, m_AxisWidth)
     135           0 :     );
     136             : }
     137             : 
     138           0 : void CChart::UpdateSeries()
     139             : {
     140           0 :     m_Series.clear();
     141           0 :     m_Series.resize(m_SeriesSetting->m_Series.size());
     142             : 
     143           0 :     for (size_t i = 0; i < m_SeriesSetting->m_Series.size(); ++i)
     144             :     {
     145           0 :         CChartData& data = m_Series[i];
     146             : 
     147           0 :         if (i < m_SeriesColor->m_Items.size() && !data.m_Color.ParseString(m_pGUI, m_SeriesColor->m_Items[i].GetOriginalString().ToUTF8(), 0))
     148             :         {
     149           0 :             LOGWARNING("GUI: Error parsing 'series_color' (\"{}\")",
     150             :                 utf8_from_wstring(m_SeriesColor->m_Items[i].GetOriginalString()));
     151             :         }
     152             : 
     153           0 :         data.m_Points = m_SeriesSetting->m_Series[i];
     154             :     }
     155           0 :     UpdateBounds();
     156             : 
     157           0 :     SetupText();
     158           0 : }
     159             : 
     160           0 : void CChart::SetupText()
     161             : {
     162           0 :     m_GeneratedTexts.clear();
     163           0 :     m_TextPositions.clear();
     164             : 
     165           0 :     if (m_Series.empty())
     166             :         return;
     167             : 
     168             :     // Add Y-axis
     169           0 :     const float height = GetChartRect().GetHeight();
     170             :     // TODO: split values depend on the format;
     171           0 :     if (m_EqualY)
     172             :     {
     173             :         // We don't need to generate many items for equal values
     174           0 :         AddFormattedValue(m_FormatY, m_RightTop.Y, m_Font, m_BufferZone);
     175           0 :         m_TextPositions.emplace_back(GetChartRect().TopLeft());
     176             :     }
     177             :     else
     178           0 :         for (int i = 0; i < 3; ++i)
     179             :         {
     180           0 :             AddFormattedValue(m_FormatY, m_RightTop.Y - (m_RightTop.Y - m_LeftBottom.Y) / 3.f * i, m_Font, m_BufferZone);
     181           0 :             m_TextPositions.emplace_back(GetChartRect().TopLeft() + CVector2D(0.f, height / 3.f * i));
     182             :         }
     183             : 
     184             :     // Add X-axis
     185           0 :     const float width = GetChartRect().GetWidth();
     186           0 :     if (m_EqualX)
     187             :     {
     188           0 :         CSize2D text_size = AddFormattedValue(m_FormatX, m_RightTop.X, m_Font, m_BufferZone);
     189           0 :         m_TextPositions.emplace_back(GetChartRect().BottomRight() - text_size);
     190             :     }
     191             :     else
     192           0 :         for (int i = 0; i < 3; ++i)
     193             :         {
     194           0 :             CSize2D text_size = AddFormattedValue(m_FormatX, m_RightTop.X - (m_RightTop.X - m_LeftBottom.X) / 3 * i, m_Font, m_BufferZone);
     195           0 :             m_TextPositions.emplace_back(GetChartRect().BottomRight() - text_size - CVector2D(width / 3 * i, 0.f));
     196             :         }
     197             : }
     198             : 
     199           0 : CSize2D CChart::AddFormattedValue(const CStrW& format, const float value, const CStrW& font, const float buffer_zone)
     200             : {
     201             :     // TODO: we need to catch cases with equal formatted values.
     202           0 :     CGUIString gui_str;
     203           0 :     if (format == L"DECIMAL2")
     204             :     {
     205           0 :         wchar_t buffer[64];
     206           0 :         swprintf(buffer, 64, L"%.2f", value);
     207           0 :         gui_str.SetValue(buffer);
     208             :     }
     209           0 :     else if (format == L"INTEGER")
     210             :     {
     211           0 :         wchar_t buffer[64];
     212           0 :         swprintf(buffer, 64, L"%d", std::lround(value));
     213           0 :         gui_str.SetValue(buffer);
     214             :     }
     215           0 :     else if (format == L"DURATION_SHORT")
     216             :     {
     217           0 :         const int seconds = value;
     218           0 :         wchar_t buffer[64];
     219           0 :         swprintf(buffer, 64, L"%d:%02d", seconds / 60, seconds % 60);
     220           0 :         gui_str.SetValue(buffer);
     221             :     }
     222           0 :     else if (format == L"PERCENTAGE")
     223             :     {
     224           0 :         wchar_t buffer[64];
     225           0 :         swprintf(buffer, 64, L"%d%%", std::lround(value));
     226           0 :         gui_str.SetValue(buffer);
     227             :     }
     228             :     else
     229             :     {
     230           0 :         LOGERROR("Unsupported chart format: {}", format.EscapeToPrintableASCII());
     231           0 :         return CSize2D();
     232             :     }
     233             : 
     234           0 :     return AddText(gui_str, font, 0, buffer_zone).GetSize();
     235           0 : }
     236             : 
     237           0 : void CChart::UpdateBounds()
     238             : {
     239           0 :     if (m_Series.empty() || m_Series[0].m_Points.empty())
     240             :     {
     241           0 :         m_LeftBottom = m_RightTop = CVector2D(0.f, 0.f);
     242           0 :         return;
     243             :     }
     244             : 
     245           0 :     m_LeftBottom = m_RightTop = m_Series[0].m_Points[0];
     246           0 :     for (const CChartData& data : m_Series)
     247           0 :         for (const CVector2D& point : data.m_Points)
     248             :         {
     249           0 :             if (fabs(point.X) != std::numeric_limits<float>::infinity() && point.X < m_LeftBottom.X)
     250           0 :                 m_LeftBottom.X = point.X;
     251           0 :             if (fabs(point.Y) != std::numeric_limits<float>::infinity() && point.Y < m_LeftBottom.Y)
     252           0 :                 m_LeftBottom.Y = point.Y;
     253             : 
     254           0 :             if (fabs(point.X) != std::numeric_limits<float>::infinity() && point.X > m_RightTop.X)
     255           0 :                 m_RightTop.X = point.X;
     256           0 :             if (fabs(point.Y) != std::numeric_limits<float>::infinity() && point.Y > m_RightTop.Y)
     257           0 :                 m_RightTop.Y = point.Y;
     258             :         }
     259             : 
     260           0 :     m_EqualY = m_RightTop.Y == m_LeftBottom.Y;
     261           0 :     if (m_EqualY)
     262           0 :         m_RightTop.Y += 1;
     263           0 :     m_EqualX = m_RightTop.X == m_LeftBottom.X;
     264           0 :     if (m_EqualX)
     265           0 :         m_RightTop.X += 1;
     266             : }

Generated by: LCOV version 1.16