Files
OpenRCT2-Web/src/openrct2-ui/interface/Graph.cpp
T

198 lines
8.4 KiB
C++
Raw Normal View History

/*****************************************************************************
2024-12-31 20:30:04 +01:00
* Copyright (c) 2014-2025 OpenRCT2 developers
*
2018-06-15 14:07:34 +02:00
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include "../UiStringIds.h"
2018-06-22 23:20:47 +02:00
#include <openrct2-ui/interface/Graph.h>
#include <openrct2/Date.h>
2025-10-16 13:13:02 +02:00
#include <openrct2/drawing/Rect.h>
2021-12-11 18:06:06 -05:00
#include <openrct2/localisation/Formatter.h>
2024-08-02 05:31:22 +10:00
#include <openrct2/localisation/Formatting.h>
#include <openrct2/localisation/Localisation.Date.h>
2025-08-13 19:55:58 +02:00
#include <openrct2/world/ParkData.h>
namespace OpenRCT2::Graph
{
2024-07-30 04:41:58 +10:00
constexpr int32_t kDashLength = 2;
2024-07-31 01:45:06 +10:00
template<typename T>
static void DrawYLabels(
RenderTarget& rt, const ScreenRect& internalBounds, const T min, const T max, const int32_t numYLabels,
2024-08-02 05:31:22 +10:00
const int32_t yLabelStepPx, const ColourWithFlags lineCol, const FmtString& fmt)
2024-07-31 01:45:06 +10:00
{
T curLabel = max;
int32_t curScreenPos = internalBounds.GetTop() - 5;
const T yLabelStep = (max - min) / (numYLabels - 1);
for (int32_t i = 0; i < numYLabels; i++)
{
// Draw Y label text
2024-08-02 05:31:22 +10:00
char buffer[64]{};
FormatStringToBuffer(buffer, sizeof(buffer), fmt, curLabel);
DrawText(
rt, { internalBounds.GetLeft() - kYTickMarkPadding, curScreenPos }, { FontStyle::Small, TextAlignment::RIGHT },
buffer);
// Draw Y label tick mark
GfxFillRect(
rt, { { internalBounds.GetLeft() - 5, curScreenPos + 5 }, { internalBounds.GetLeft(), curScreenPos + 5 } },
2025-04-09 16:47:22 +02:00
PaletteIndex::pi10);
// Draw horizontal gridline
2024-07-31 01:45:06 +10:00
GfxFillRectInset(
rt, { { internalBounds.GetLeft(), curScreenPos + 5 }, { internalBounds.GetRight(), curScreenPos + 5 } },
lineCol, RectBorderStyle::inset);
2024-07-31 01:45:06 +10:00
curScreenPos += yLabelStepPx;
curLabel -= yLabelStep;
}
}
2024-07-30 04:41:58 +10:00
template<typename T, T TkNoValue>
static void DrawMonths(RenderTarget& rt, const T* series, int32_t count, const ScreenRect& bounds, const int32_t xStep)
{
auto& date = GetDate();
int32_t currentMonth = date.GetMonth();
int32_t currentDay = date.GetMonthTicks();
int32_t yearOver32 = (currentMonth * 4) + (currentDay >> 14) - 31;
2024-07-30 04:41:58 +10:00
auto screenCoords = bounds.Point1;
for (int32_t i = count - 1; i >= 0; i--)
2020-11-03 01:21:20 -07:00
{
2024-07-30 04:41:58 +10:00
if (series[i] != TkNoValue && yearOver32 % 4 == 0)
2020-11-03 01:21:20 -07:00
{
// Draw month text
auto ft = Formatter();
ft.Add<StringId>(DateGameShortMonthNames[DateGetMonth((yearOver32 / 4) + MONTH_COUNT)]);
DrawTextBasic(
rt, screenCoords - ScreenCoordsXY{ 0, 14 }, STR_GRAPH_LABEL, ft,
{ FontStyle::Small, TextAlignment::CENTRE });
// Draw month tick mark
2024-07-31 01:45:06 +10:00
GfxFillRect(
rt, { screenCoords - ScreenCoordsXY{ 0, 4 }, screenCoords - ScreenCoordsXY{ 0, 1 } }, PaletteIndex::pi10);
2020-11-03 01:21:20 -07:00
}
yearOver32 = (yearOver32 + 1) % 32;
2024-07-30 04:41:58 +10:00
screenCoords.x += xStep;
2020-11-03 01:21:20 -07:00
}
}
template<typename T>
2020-11-03 01:21:20 -07:00
static void DrawHoveredValue(
RenderTarget& rt, const T value, const int32_t hoverIdx, const ScreenRect& bounds, const int32_t xStep,
2024-08-02 05:31:22 +10:00
const T minValue, const T maxValue, const_utf8string text, ColourWithFlags textCol)
2020-11-03 01:21:20 -07:00
{
const T screenRange = bounds.GetHeight();
const T valueRange = maxValue - minValue;
const int32_t yPosition = bounds.GetBottom() - ((value - minValue) * screenRange) / valueRange;
ScreenCoordsXY coords = { bounds.GetRight() - hoverIdx * xStep, yPosition };
2020-11-03 01:21:20 -07:00
GfxDrawDashedLine(
rt,
2024-07-31 01:45:06 +10:00
{
{ coords.x, bounds.GetTop() },
{ coords.x, bounds.GetBottom() },
},
2025-04-09 16:47:22 +02:00
kDashLength, PaletteIndex::pi10);
GfxDrawDashedLine(rt, { { bounds.GetLeft(), coords.y }, coords }, kDashLength, PaletteIndex::pi10);
2024-07-30 04:41:58 +10:00
DrawText(rt, coords - ScreenCoordsXY{ 0, 16 }, { textCol, TextAlignment::CENTRE }, text);
2020-11-03 01:21:20 -07:00
GfxFillRect(rt, { { coords - ScreenCoordsXY{ 2, 2 } }, coords + ScreenCoordsXY{ 2, 2 } }, PaletteIndex::pi10);
GfxFillRect(rt, { { coords - ScreenCoordsXY{ 1, 1 } }, { coords + ScreenCoordsXY{ 1, 1 } } }, PaletteIndex::pi21);
2020-11-03 01:21:20 -07:00
}
2024-07-30 04:41:58 +10:00
template<typename T, T TkNoValue, bool TbackgroundLine>
static void DrawLine(
RenderTarget& rt, const T* series, const int32_t count, const ScreenRect& bounds, const int32_t xStep, const T minValue,
const T maxValue)
2020-11-03 01:21:20 -07:00
{
const T screenRange = bounds.GetHeight();
const T valueRange = maxValue - minValue;
2024-07-30 04:41:58 +10:00
ScreenCoordsXY lastCoords;
bool lastCoordsValid = false;
ScreenCoordsXY coords = bounds.Point1;
for (int32_t i = count - 1; i >= 0; i--)
{
auto value = series[i];
if (value != TkNoValue)
{
coords.y = bounds.GetBottom() - ((value - minValue) * screenRange) / valueRange;
if constexpr (TbackgroundLine)
{
if (lastCoordsValid)
{
auto leftTop1 = lastCoords + ScreenCoordsXY{ 1, 1 };
auto rightBottom1 = coords + ScreenCoordsXY{ 1, 1 };
auto leftTop2 = lastCoords + ScreenCoordsXY{ 0, 1 };
auto rightBottom2 = coords + ScreenCoordsXY{ 0, 1 };
GfxDrawLine(rt, { leftTop1, rightBottom1 }, PaletteIndex::pi10);
GfxDrawLine(rt, { leftTop2, rightBottom2 }, PaletteIndex::pi10);
2024-07-30 04:41:58 +10:00
}
if (i == 0)
{
GfxFillRect(rt, { coords, coords + ScreenCoordsXY{ 2, 2 } }, PaletteIndex::pi10);
2024-07-30 04:41:58 +10:00
}
}
else
{
if (lastCoordsValid)
{
auto leftTop = lastCoords;
auto rightBottom = coords;
GfxDrawLine(rt, { leftTop, rightBottom }, PaletteIndex::pi21);
2024-07-30 04:41:58 +10:00
}
if (i == 0)
{
GfxFillRect(
rt, { coords - ScreenCoordsXY{ 1, 1 }, coords + ScreenCoordsXY{ 1, 1 } }, PaletteIndex::pi21);
2024-07-30 04:41:58 +10:00
}
}
lastCoords = coords;
lastCoordsValid = true;
}
coords.x += xStep;
}
}
2024-09-04 12:40:35 +10:00
template<typename T, T TkNoValue>
static void DrawGraph(RenderTarget& rt, const GraphProperties<T>& p, const FmtString& labelFmt, const FmtString& tooltipFmt)
2024-07-30 04:41:58 +10:00
{
DrawYLabels<T>(rt, p.internalBounds, p.min, p.max, p.numYLabels, p.yLabelStepPx, p.lineCol, labelFmt);
DrawMonths<T, TkNoValue>(rt, p.series, p.numPoints, p.internalBounds, p.xStepPx);
DrawLine<T, TkNoValue, true>(rt, p.series, p.numPoints, p.internalBounds, p.xStepPx, p.min, p.max);
DrawLine<T, TkNoValue, false>(rt, p.series, p.numPoints, p.internalBounds, p.xStepPx, p.min, p.max);
if (p.hoverIdx >= 0 && p.hoverIdx < p.numPoints)
{
2024-09-04 12:40:35 +10:00
const T value = p.series[p.hoverIdx];
if (value != TkNoValue)
2024-08-02 05:31:22 +10:00
{
char buffer[64]{};
2024-09-04 12:40:35 +10:00
FormatStringToBuffer(buffer, sizeof(buffer), tooltipFmt, value);
DrawHoveredValue<T>(
rt, value, p.hoverIdx, p.internalBounds, p.xStepPx, p.min, p.max, buffer,
2024-08-02 05:31:22 +10:00
p.lineCol.withFlag(ColourFlag::withOutline, true));
}
}
2024-07-31 01:45:06 +10:00
}
2024-07-30 04:41:58 +10:00
void DrawFinanceGraph(RenderTarget& rt, const GraphProperties<money64>& p)
2024-09-04 12:40:35 +10:00
{
DrawGraph<money64, kMoney64Undefined>(rt, p, "{BLACK}{CURRENCY2DP}", "{CURRENCY2DP}");
2024-09-04 12:40:35 +10:00
}
void DrawRatingGraph(RenderTarget& rt, const GraphProperties<uint16_t>& p)
2024-07-31 01:45:06 +10:00
{
DrawGraph<uint16_t, kParkRatingHistoryUndefined>(rt, p, "{BLACK}{COMMA32}", "{COMMA32}");
2024-07-31 01:45:06 +10:00
}
void DrawGuestGraph(RenderTarget& rt, const GraphProperties<uint32_t>& p)
2024-07-31 01:45:06 +10:00
{
DrawGraph<uint32_t, kGuestsInParkHistoryUndefined>(rt, p, "{BLACK}{COMMA32}", "{COMMA32}");
2020-11-03 01:21:20 -07:00
}
} // namespace OpenRCT2::Graph