2019-05-13 14:23:26 -07:00
|
|
|
/* Copyright (C) 2019 Wildfire Games.
|
2016-12-07 11:03:54 -08:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
|
*
|
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
2019-07-26 11:57:28 -07:00
|
|
|
|
2016-12-07 11:03:54 -08:00
|
|
|
#include "CChart.h"
|
|
|
|
|
|
2019-07-26 11:57:28 -07:00
|
|
|
#include "gui/CGUIColor.h"
|
2019-08-10 12:16:52 -07:00
|
|
|
#include "gui/CGUIString.h"
|
2019-08-03 20:52:00 -07:00
|
|
|
#include "gui/GUIMatrix.h"
|
2016-12-07 11:03:54 -08:00
|
|
|
#include "graphics/ShaderManager.h"
|
2018-03-04 04:42:35 -08:00
|
|
|
#include "i18n/L10n.h"
|
2016-12-07 11:03:54 -08:00
|
|
|
#include "lib/ogl.h"
|
|
|
|
|
#include "ps/CLogger.h"
|
|
|
|
|
#include "renderer/Renderer.h"
|
2018-03-04 04:42:35 -08:00
|
|
|
#include "third_party/cppformat/format.h"
|
2016-12-07 11:03:54 -08:00
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
2019-08-01 13:20:24 -07:00
|
|
|
CChart::CChart(CGUI* pGUI)
|
2019-08-01 16:55:10 -07:00
|
|
|
: IGUIObject(pGUI), IGUITextOwner(pGUI)
|
2016-12-07 11:03:54 -08:00
|
|
|
{
|
2019-08-03 19:20:08 -07:00
|
|
|
AddSetting<CGUIColor>("axis_color");
|
|
|
|
|
AddSetting<float>("axis_width");
|
|
|
|
|
AddSetting<float>("buffer_zone");
|
|
|
|
|
AddSetting<CStrW>("font");
|
|
|
|
|
AddSetting<CStrW>("format_x");
|
|
|
|
|
AddSetting<CStrW>("format_y");
|
|
|
|
|
AddSetting<CGUIList>("series_color");
|
|
|
|
|
AddSetting<CGUISeries>("series");
|
|
|
|
|
AddSetting<EAlign>("text_align");
|
2018-03-04 04:42:35 -08:00
|
|
|
|
|
|
|
|
GUI<float>::GetSetting(this, "axis_width", m_AxisWidth);
|
|
|
|
|
GUI<CStrW>::GetSetting(this, "format_x", m_FormatX);
|
|
|
|
|
GUI<CStrW>::GetSetting(this, "format_y", m_FormatY);
|
2016-12-07 11:03:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CChart::~CChart()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CChart::HandleMessage(SGUIMessage& Message)
|
|
|
|
|
{
|
|
|
|
|
// TODO: implement zoom
|
2017-01-06 03:14:03 -08:00
|
|
|
switch (Message.type)
|
|
|
|
|
{
|
|
|
|
|
case GUIM_SETTINGS_UPDATED:
|
|
|
|
|
{
|
2018-03-04 04:42:35 -08:00
|
|
|
GUI<float>::GetSetting(this, "axis_width", m_AxisWidth);
|
|
|
|
|
GUI<CStrW>::GetSetting(this, "format_x", m_FormatX);
|
|
|
|
|
GUI<CStrW>::GetSetting(this, "format_y", m_FormatY);
|
|
|
|
|
|
2017-01-06 03:14:03 -08:00
|
|
|
UpdateSeries();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-07 11:03:54 -08:00
|
|
|
}
|
|
|
|
|
|
2019-07-26 11:57:28 -07:00
|
|
|
void CChart::DrawLine(const CShaderProgramPtr& shader, const CGUIColor& color, const std::vector<float>& vertices) const
|
2017-04-11 05:27:21 -07:00
|
|
|
{
|
|
|
|
|
shader->Uniform(str_color, color);
|
|
|
|
|
shader->VertexPointer(3, GL_FLOAT, 0, &vertices[0]);
|
|
|
|
|
shader->AssertPointersBound();
|
|
|
|
|
|
|
|
|
|
glEnable(GL_LINE_SMOOTH);
|
|
|
|
|
glLineWidth(1.1f);
|
|
|
|
|
if (!g_Renderer.m_SkipSubmit)
|
|
|
|
|
glDrawArrays(GL_LINE_STRIP, 0, vertices.size() / 3);
|
|
|
|
|
glLineWidth(1.0f);
|
|
|
|
|
glDisable(GL_LINE_SMOOTH);
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-26 11:57:28 -07:00
|
|
|
void CChart::DrawTriangleStrip(const CShaderProgramPtr& shader, const CGUIColor& color, const std::vector<float>& vertices) const
|
2018-03-04 04:42:35 -08:00
|
|
|
{
|
|
|
|
|
shader->Uniform(str_color, color);
|
|
|
|
|
shader->VertexPointer(3, GL_FLOAT, 0, &vertices[0]);
|
|
|
|
|
shader->AssertPointersBound();
|
|
|
|
|
|
|
|
|
|
if (!g_Renderer.m_SkipSubmit)
|
|
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, vertices.size() / 3);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CChart::DrawAxes(const CShaderProgramPtr& shader) const
|
|
|
|
|
{
|
|
|
|
|
const float bz = GetBufferedZ();
|
|
|
|
|
CRect rect = GetChartRect();
|
|
|
|
|
std::vector<float> vertices;
|
|
|
|
|
vertices.reserve(30);
|
|
|
|
|
#define ADD(x, y) vertices.push_back(x); vertices.push_back(y); vertices.push_back(bz + 0.5f);
|
|
|
|
|
ADD(m_CachedActualSize.right, m_CachedActualSize.bottom);
|
|
|
|
|
ADD(rect.right + m_AxisWidth, rect.bottom);
|
|
|
|
|
ADD(m_CachedActualSize.left, m_CachedActualSize.bottom);
|
|
|
|
|
ADD(rect.left, rect.bottom);
|
|
|
|
|
ADD(m_CachedActualSize.left, m_CachedActualSize.top);
|
|
|
|
|
ADD(rect.left, rect.top - m_AxisWidth);
|
|
|
|
|
#undef ADD
|
2019-07-26 11:57:28 -07:00
|
|
|
CGUIColor axis_color(0.5f, 0.5f, 0.5f, 1.f);
|
|
|
|
|
GUI<CGUIColor>::GetSetting(this, "axis_color", axis_color);
|
2018-03-04 04:42:35 -08:00
|
|
|
DrawTriangleStrip(shader, axis_color, vertices);
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-07 11:03:54 -08:00
|
|
|
void CChart::Draw()
|
|
|
|
|
{
|
|
|
|
|
PROFILE3("render chart");
|
|
|
|
|
|
2017-01-06 03:14:03 -08:00
|
|
|
if (m_Series.empty())
|
|
|
|
|
return;
|
2016-12-07 11:03:54 -08:00
|
|
|
|
|
|
|
|
const float bz = GetBufferedZ();
|
|
|
|
|
CRect rect = GetChartRect();
|
|
|
|
|
const float width = rect.GetWidth();
|
|
|
|
|
const float height = rect.GetHeight();
|
|
|
|
|
|
|
|
|
|
// Disable depth updates to prevent apparent z-fighting-related issues
|
|
|
|
|
// with some drivers causing units to get drawn behind the texture.
|
|
|
|
|
glDepthMask(0);
|
|
|
|
|
|
|
|
|
|
// Setup the render state
|
|
|
|
|
CMatrix3D transform = GetDefaultGuiMatrix();
|
|
|
|
|
CShaderDefines lineDefines;
|
|
|
|
|
CShaderTechniquePtr tech = g_Renderer.GetShaderManager().LoadEffect(str_gui_solid, g_Renderer.GetSystemShaderDefines(), lineDefines);
|
|
|
|
|
tech->BeginPass();
|
|
|
|
|
CShaderProgramPtr shader = tech->GetShader();
|
|
|
|
|
shader->Uniform(str_transform, transform);
|
|
|
|
|
|
2018-03-04 04:42:35 -08:00
|
|
|
CVector2D scale(width / (m_RightTop.X - m_LeftBottom.X), height / (m_RightTop.Y - m_LeftBottom.Y));
|
2016-12-07 11:03:54 -08:00
|
|
|
for (const CChartData& data : m_Series)
|
|
|
|
|
{
|
|
|
|
|
if (data.m_Points.empty())
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
std::vector<float> vertices;
|
|
|
|
|
for (const CVector2D& point : data.m_Points)
|
|
|
|
|
{
|
2017-11-27 11:51:21 -08:00
|
|
|
if (fabs(point.X) != std::numeric_limits<float>::infinity() && fabs(point.Y) != std::numeric_limits<float>::infinity())
|
2017-04-11 05:27:21 -07:00
|
|
|
{
|
2018-03-04 04:42:35 -08:00
|
|
|
vertices.push_back(rect.left + (point.X - m_LeftBottom.X) * scale.X);
|
|
|
|
|
vertices.push_back(rect.bottom - (point.Y - m_LeftBottom.Y) * scale.Y);
|
2017-04-11 05:27:21 -07:00
|
|
|
vertices.push_back(bz + 0.5f);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
DrawLine(shader, data.m_Color, vertices);
|
|
|
|
|
vertices.clear();
|
|
|
|
|
}
|
2016-12-07 11:03:54 -08:00
|
|
|
}
|
2017-04-11 05:27:21 -07:00
|
|
|
if (!vertices.empty())
|
|
|
|
|
DrawLine(shader, data.m_Color, vertices);
|
2016-12-07 11:03:54 -08:00
|
|
|
}
|
|
|
|
|
|
2018-03-04 04:42:35 -08:00
|
|
|
if (m_AxisWidth > 0)
|
|
|
|
|
DrawAxes(shader);
|
|
|
|
|
|
2016-12-07 11:03:54 -08:00
|
|
|
tech->EndPass();
|
|
|
|
|
|
|
|
|
|
// Reset depth mask
|
|
|
|
|
glDepthMask(1);
|
2018-03-04 04:42:35 -08:00
|
|
|
|
|
|
|
|
for (size_t i = 0; i < m_TextPositions.size(); ++i)
|
2019-07-26 11:57:28 -07:00
|
|
|
DrawText(i, CGUIColor(1.f, 1.f, 1.f, 1.f), m_TextPositions[i], bz + 0.5f);
|
2016-12-07 11:03:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CRect CChart::GetChartRect() const
|
|
|
|
|
{
|
2018-03-04 04:42:35 -08:00
|
|
|
return CRect(
|
|
|
|
|
m_CachedActualSize.TopLeft() + CPos(m_AxisWidth, m_AxisWidth),
|
|
|
|
|
m_CachedActualSize.BottomRight() - CPos(m_AxisWidth, m_AxisWidth)
|
|
|
|
|
);
|
2016-12-07 11:03:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CChart::UpdateSeries()
|
|
|
|
|
{
|
2019-08-19 03:32:29 -07:00
|
|
|
const CGUISeries& pSeries = GUI<CGUISeries>::GetSetting(this, "series");
|
|
|
|
|
const CGUIList& pSeriesColor = GUI<CGUIList>::GetSetting(this, "series_color");
|
2016-12-07 11:03:54 -08:00
|
|
|
|
|
|
|
|
m_Series.clear();
|
2019-08-19 03:32:29 -07:00
|
|
|
m_Series.resize(pSeries.m_Series.size());
|
|
|
|
|
for (size_t i = 0; i < pSeries.m_Series.size(); ++i)
|
2016-12-07 11:03:54 -08:00
|
|
|
{
|
2017-01-06 03:14:03 -08:00
|
|
|
CChartData& data = m_Series[i];
|
2016-12-07 11:03:54 -08:00
|
|
|
|
2019-08-19 03:32:29 -07:00
|
|
|
if (i < pSeriesColor.m_Items.size() && !data.m_Color.ParseString(m_pGUI, pSeriesColor.m_Items[i].GetOriginalString().ToUTF8(), 0))
|
|
|
|
|
LOGWARNING("GUI: Error parsing 'series_color' (\"%s\")", utf8_from_wstring(pSeriesColor.m_Items[i].GetOriginalString()));
|
2016-12-07 11:03:54 -08:00
|
|
|
|
2019-08-19 03:32:29 -07:00
|
|
|
data.m_Points = pSeries.m_Series[i];
|
2016-12-07 11:03:54 -08:00
|
|
|
}
|
2018-03-04 04:42:35 -08:00
|
|
|
UpdateBounds();
|
|
|
|
|
|
|
|
|
|
SetupText();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CChart::SetupText()
|
|
|
|
|
{
|
|
|
|
|
m_GeneratedTexts.clear();
|
|
|
|
|
m_TextPositions.clear();
|
|
|
|
|
|
|
|
|
|
if (m_Series.empty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
CStrW font;
|
|
|
|
|
if (GUI<CStrW>::GetSetting(this, "font", font) != PSRETURN_OK || font.empty())
|
|
|
|
|
font = L"default";
|
|
|
|
|
|
|
|
|
|
float buffer_zone = 0.f;
|
|
|
|
|
GUI<float>::GetSetting(this, "buffer_zone", buffer_zone);
|
|
|
|
|
|
|
|
|
|
// Add Y-axis
|
|
|
|
|
GUI<CStrW>::GetSetting(this, "format_y", m_FormatY);
|
|
|
|
|
const float height = GetChartRect().GetHeight();
|
|
|
|
|
// TODO: split values depend on the format;
|
|
|
|
|
if (m_EqualY)
|
|
|
|
|
{
|
|
|
|
|
// We don't need to generate many items for equal values
|
|
|
|
|
AddFormattedValue(m_FormatY, m_RightTop.Y, font, buffer_zone);
|
|
|
|
|
m_TextPositions.emplace_back(GetChartRect().TopLeft());
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
for (int i = 0; i < 3; ++i)
|
|
|
|
|
{
|
|
|
|
|
AddFormattedValue(m_FormatY, m_RightTop.Y - (m_RightTop.Y - m_LeftBottom.Y) / 3.f * i, font, buffer_zone);
|
|
|
|
|
m_TextPositions.emplace_back(GetChartRect().TopLeft() + CPos(0.f, height / 3.f * i));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add X-axis
|
|
|
|
|
GUI<CStrW>::GetSetting(this, "format_x", m_FormatX);
|
|
|
|
|
const float width = GetChartRect().GetWidth();
|
|
|
|
|
if (m_EqualX)
|
|
|
|
|
{
|
|
|
|
|
CSize text_size = AddFormattedValue(m_FormatX, m_RightTop.X, font, buffer_zone);
|
|
|
|
|
m_TextPositions.emplace_back(GetChartRect().BottomRight() - text_size);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
for (int i = 0; i < 3; ++i)
|
|
|
|
|
{
|
|
|
|
|
CSize text_size = AddFormattedValue(m_FormatX, m_RightTop.X - (m_RightTop.X - m_LeftBottom.X) / 3 * i, font, buffer_zone);
|
|
|
|
|
m_TextPositions.emplace_back(GetChartRect().BottomRight() - text_size - CPos(width / 3 * i, 0.f));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CSize CChart::AddFormattedValue(const CStrW& format, const float value, const CStrW& font, const float buffer_zone)
|
|
|
|
|
{
|
|
|
|
|
// TODO: we need to catch cases with equal formatted values.
|
|
|
|
|
CGUIString gui_str;
|
|
|
|
|
if (format == L"DECIMAL2")
|
|
|
|
|
{
|
|
|
|
|
wchar_t buffer[64];
|
|
|
|
|
swprintf(buffer, 64, L"%.2f", value);
|
|
|
|
|
gui_str.SetValue(buffer);
|
|
|
|
|
}
|
|
|
|
|
else if (format == L"INTEGER")
|
|
|
|
|
{
|
|
|
|
|
wchar_t buffer[64];
|
2019-05-13 14:23:26 -07:00
|
|
|
swprintf(buffer, 64, L"%d", std::lround(value));
|
2018-03-04 04:42:35 -08:00
|
|
|
gui_str.SetValue(buffer);
|
|
|
|
|
}
|
|
|
|
|
else if (format == L"DURATION_SHORT")
|
|
|
|
|
{
|
|
|
|
|
const int seconds = value;
|
|
|
|
|
wchar_t buffer[64];
|
|
|
|
|
swprintf(buffer, 64, L"%d:%02d", seconds / 60, seconds % 60);
|
|
|
|
|
gui_str.SetValue(buffer);
|
|
|
|
|
}
|
2019-05-13 14:23:26 -07:00
|
|
|
else if (format == L"PERCENTAGE")
|
|
|
|
|
{
|
|
|
|
|
wchar_t buffer[64];
|
|
|
|
|
swprintf(buffer, 64, L"%d%%", std::lround(value));
|
|
|
|
|
gui_str.SetValue(buffer);
|
|
|
|
|
}
|
2018-03-04 04:42:35 -08:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
LOGERROR("Unsupported chart format: " + format.EscapeToPrintableASCII());
|
|
|
|
|
return CSize();
|
|
|
|
|
}
|
Move CGUI::GenerateText to CGUIText constructor, CGUI::DrawText to CGUIText::Draw, SGenerateTextImage from CGUI to CGUIText.
Makes GUI text construction 30x faster for empty strings, otherwise less
than 1% faster.
Split the constructor into smaller helper functions to reduce nesting
and improve readability.
Change m_GeneratedTexts from pointer to reference, so that one doesn't
have to keep track to delete it correctly in several places, without
having to resort to copy or move assignments but constructing in place.
Mark CGUIText as NONCOPYABLE and MOVABLE which is already implicitly the
case due to the CGUISpriteInstance members, refs 0a7d0ecdde/D2133,
D2163/3028551b91.
Differential Revision: https://code.wildfiregames.com/D2168
Prepared by the GUIText.h file split in 838889ab12 / D2167.
Comments By: Vladislav
Tested on: gcc 9, clang 8, VS2015, Jenkins
Inlining tested using: clang -Rpass=inline and gcc -Winline
This was SVN commit r22679.
2019-08-16 18:32:11 -07:00
|
|
|
|
|
|
|
|
return AddText(gui_str, font, 0, buffer_zone, this).GetSize();
|
2018-03-04 04:42:35 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CChart::UpdateBounds()
|
|
|
|
|
{
|
|
|
|
|
if (m_Series.empty() || m_Series[0].m_Points.empty())
|
|
|
|
|
{
|
|
|
|
|
m_LeftBottom = m_RightTop = CVector2D(0.f, 0.f);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_LeftBottom = m_RightTop = m_Series[0].m_Points[0];
|
|
|
|
|
for (const CChartData& data : m_Series)
|
|
|
|
|
for (const CVector2D& point : data.m_Points)
|
|
|
|
|
{
|
|
|
|
|
if (fabs(point.X) != std::numeric_limits<float>::infinity() && point.X < m_LeftBottom.X)
|
|
|
|
|
m_LeftBottom.X = point.X;
|
|
|
|
|
if (fabs(point.Y) != std::numeric_limits<float>::infinity() && point.Y < m_LeftBottom.Y)
|
|
|
|
|
m_LeftBottom.Y = point.Y;
|
|
|
|
|
|
|
|
|
|
if (fabs(point.X) != std::numeric_limits<float>::infinity() && point.X > m_RightTop.X)
|
|
|
|
|
m_RightTop.X = point.X;
|
|
|
|
|
if (fabs(point.Y) != std::numeric_limits<float>::infinity() && point.Y > m_RightTop.Y)
|
|
|
|
|
m_RightTop.Y = point.Y;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_EqualY = m_RightTop.Y == m_LeftBottom.Y;
|
|
|
|
|
if (m_EqualY)
|
|
|
|
|
m_RightTop.Y += 1;
|
|
|
|
|
m_EqualX = m_RightTop.X == m_LeftBottom.X;
|
|
|
|
|
if (m_EqualX)
|
|
|
|
|
m_RightTop.X += 1;
|
2016-12-07 11:03:54 -08:00
|
|
|
}
|