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

260 lines
11 KiB
C++
Raw Normal View History

/*****************************************************************************
* Copyright (c) 2014-2024 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>
2021-12-11 18:06:06 -05:00
#include <openrct2/localisation/Formatter.h>
#include <openrct2/localisation/Localisation.Date.h>
using namespace OpenRCT2;
namespace OpenRCT2::Graph
{
static void DrawMonths(DrawPixelInfo& dpi, const uint8_t* history, int32_t count, const ScreenCoordsXY& origCoords)
2018-05-08 09:20:13 +02:00
{
auto& date = GetDate();
int32_t currentMonth = date.GetMonth();
int32_t currentDay = date.GetMonthTicks();
2021-03-27 00:39:11 -03:00
int32_t yearOver32 = (currentMonth * 4) + (currentDay >> 14) - 31;
auto screenCoords = origCoords;
for (int32_t i = count - 1; i >= 0; i--)
2018-05-08 09:20:13 +02:00
{
2020-11-03 01:21:20 -07:00
if (history[i] != 255 && yearOver32 % 4 == 0)
2018-05-08 09:20:13 +02:00
{
2020-11-03 01:21:20 -07:00
// Draw month text
auto ft = Formatter();
ft.Add<uint32_t>(DateGameShortMonthNames[DateGetMonth((yearOver32 / 4) + MONTH_COUNT)]);
DrawTextBasic(
dpi, screenCoords - ScreenCoordsXY{ 0, 10 }, STR_GRAPH_LABEL, ft,
{ FontStyle::Small, TextAlignment::CENTRE });
2020-11-03 01:21:20 -07:00
// Draw month mark
GfxFillRect(dpi, { screenCoords, screenCoords + ScreenCoordsXY{ 0, 3 } }, PALETTE_INDEX_10);
2017-06-06 23:24:18 +02:00
}
2020-11-03 01:21:20 -07:00
yearOver32 = (yearOver32 + 1) % 32;
screenCoords.x += 6;
2017-06-06 23:24:18 +02:00
}
}
static void DrawLineA(DrawPixelInfo& dpi, const uint8_t* history, int32_t count, const ScreenCoordsXY& origCoords)
2018-05-08 09:20:13 +02:00
{
2021-03-27 00:39:11 -03:00
auto lastCoords = ScreenCoordsXY{ -1, -1 };
auto coords = origCoords;
for (int32_t i = count - 1; i >= 0; i--)
2018-05-08 09:20:13 +02:00
{
2020-11-03 01:21:20 -07:00
if (history[i] != 255)
{
2021-03-27 00:39:11 -03:00
coords.y = origCoords.y + ((255 - history[i]) * 100) / 256;
2020-11-03 01:21:20 -07:00
2021-03-27 00:39:11 -03:00
if (lastCoords.x != -1)
2020-11-03 01:21:20 -07:00
{
2021-03-27 00:39:11 -03:00
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(dpi, { leftTop1, rightBottom1 }, PALETTE_INDEX_10);
GfxDrawLine(dpi, { leftTop2, rightBottom2 }, PALETTE_INDEX_10);
2020-11-03 01:21:20 -07:00
}
if (i == 0)
GfxFillRect(dpi, { coords, coords + ScreenCoordsXY{ 2, 2 } }, PALETTE_INDEX_10);
2020-11-03 01:21:20 -07:00
2021-03-27 00:39:11 -03:00
lastCoords = coords;
}
2021-03-27 00:39:11 -03:00
coords.x += 6;
2017-06-06 23:24:18 +02:00
}
}
static void DrawLineB(DrawPixelInfo& dpi, const uint8_t* history, int32_t count, const ScreenCoordsXY& origCoords)
2018-06-22 23:20:47 +02:00
{
2021-03-27 00:39:11 -03:00
auto lastCoords = ScreenCoordsXY{ -1, -1 };
auto coords = origCoords;
for (int32_t i = count - 1; i >= 0; i--)
2018-06-22 23:20:47 +02:00
{
2020-11-03 01:21:20 -07:00
if (history[i] != 255)
2018-06-22 23:20:47 +02:00
{
2021-03-27 00:39:11 -03:00
coords.y = origCoords.y + ((255 - history[i]) * 100) / 256;
2020-11-03 01:21:20 -07:00
2021-03-27 00:39:11 -03:00
if (lastCoords.x != -1)
2020-11-03 01:21:20 -07:00
{
2021-03-27 00:39:11 -03:00
auto leftTop = lastCoords;
auto rightBottom = coords;
GfxDrawLine(dpi, { leftTop, rightBottom }, PALETTE_INDEX_21);
2020-11-03 01:21:20 -07:00
}
if (i == 0)
GfxFillRect(dpi, { coords - ScreenCoordsXY{ 1, 1 }, coords + ScreenCoordsXY{ 1, 1 } }, PALETTE_INDEX_21);
2020-11-03 01:21:20 -07:00
2021-03-27 00:39:11 -03:00
lastCoords = coords;
2017-06-06 23:24:18 +02:00
}
2021-03-27 00:39:11 -03:00
coords.x += 6;
2017-06-06 23:24:18 +02:00
}
}
void Draw(DrawPixelInfo& dpi, uint8_t* history, int32_t count, const ScreenCoordsXY& screenPos)
2018-06-22 23:20:47 +02:00
{
2021-03-27 00:39:11 -03:00
DrawMonths(dpi, history, count, screenPos);
DrawLineA(dpi, history, count, screenPos);
DrawLineB(dpi, history, count, screenPos);
2017-06-06 23:24:18 +02:00
}
} // namespace OpenRCT2::Graph
namespace OpenRCT2::Graph
{
2024-07-30 04:41:58 +10:00
constexpr int32_t kDashLength = 2;
template<typename T, T TkNoValue>
static void DrawMonths(DrawPixelInfo& dpi, 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(
2024-07-30 04:41:58 +10:00
dpi, screenCoords - ScreenCoordsXY{ 0, 13 }, STR_GRAPH_LABEL, ft,
{ FontStyle::Small, TextAlignment::CENTRE });
2020-11-03 01:21:20 -07:00
// Draw month mark
2024-07-30 04:41:58 +10:00
GfxFillRect(dpi, { screenCoords - ScreenCoordsXY{ 0, 3 }, screenCoords }, PALETTE_INDEX_10);
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(
DrawPixelInfo& dpi, const T value, const int32_t hoverIdx, const ScreenRect& bounds, const int32_t xStep,
2024-07-30 04:41:58 +10:00
const T minValue, const T maxValue)
2020-11-03 01:21:20 -07:00
{
2024-07-30 04:41:58 +10:00
const int32_t screenRange = bounds.GetHeight();
const int32_t valueRange = maxValue - minValue;
const int32_t yPosition = bounds.GetBottom() - ((value - minValue) * screenRange) / valueRange;
ScreenCoordsXY coords = { bounds.GetRight() - hoverIdx * xStep, yPosition };
// Line needs to be shifted over 1 pixel to match with the month ticks.
2024-07-30 04:41:58 +10:00
ScreenCoordsXY lineCoords = { coords.x + 1, coords.y };
2020-11-03 01:21:20 -07:00
GfxDrawDashedLine(
dpi, { { lineCoords.x, bounds.GetTop() }, { lineCoords.x, bounds.GetBottom() } }, kDashLength, PALETTE_INDEX_10);
2024-07-30 04:41:58 +10:00
GfxDrawDashedLine(dpi, { { bounds.GetLeft(), lineCoords.y }, lineCoords }, kDashLength, PALETTE_INDEX_10);
auto ft = Formatter();
2024-07-30 04:41:58 +10:00
ft.Add<money64>(value);
DrawTextBasic(
2024-07-30 04:41:58 +10:00
dpi, coords - ScreenCoordsXY{ 0, 16 }, STR_FINANCES_SUMMARY_EXPENDITURE_VALUE, ft, { TextAlignment::CENTRE });
2020-11-03 01:21:20 -07:00
2024-07-30 04:41:58 +10:00
GfxFillRect(dpi, { { coords - ScreenCoordsXY{ 2, 2 } }, coords + ScreenCoordsXY{ 2, 2 } }, PALETTE_INDEX_10);
GfxFillRect(dpi, { { coords - ScreenCoordsXY{ 1, 1 } }, { coords + ScreenCoordsXY{ 1, 1 } } }, PALETTE_INDEX_21);
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(
DrawPixelInfo& dpi, 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
{
2024-07-30 04:41:58 +10:00
const int32_t screenRange = bounds.GetHeight();
const int32_t valueRange = maxValue - minValue;
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(dpi, { leftTop1, rightBottom1 }, PALETTE_INDEX_10);
GfxDrawLine(dpi, { leftTop2, rightBottom2 }, PALETTE_INDEX_10);
}
if (i == 0)
{
GfxFillRect(dpi, { coords, coords + ScreenCoordsXY{ 2, 2 } }, PALETTE_INDEX_10);
}
}
else
{
if (lastCoordsValid)
{
auto leftTop = lastCoords;
auto rightBottom = coords;
GfxDrawLine(dpi, { leftTop, rightBottom }, PALETTE_INDEX_21);
}
if (i == 0)
{
GfxFillRect(
dpi, { coords - ScreenCoordsXY{ 1, 1 }, coords + ScreenCoordsXY{ 1, 1 } }, PALETTE_INDEX_21);
}
}
lastCoords = coords;
lastCoordsValid = true;
}
coords.x += xStep;
}
}
void DrawFinanceGraph(DrawPixelInfo& dpi, const GraphProperties<money64>& p)
2024-07-30 04:41:58 +10:00
{
money64 curLabel = p.max;
int32_t curScreenPos = p.internalBounds.GetTop() - 5;
const money64 yLabelStep = (p.max - p.min) / (p.numYLabels - 1);
for (int32_t i = 0; i < p.numYLabels; i++)
2024-07-30 04:41:58 +10:00
{
Formatter ft;
ft.Add<money64>(curLabel);
DrawTextBasic(
dpi, { p.internalBounds.GetLeft(), curScreenPos }, STR_FINANCES_FINANCIAL_GRAPH_CASH_VALUE, ft,
2024-07-30 04:41:58 +10:00
{ FontStyle::Small, TextAlignment::RIGHT });
GfxFillRectInset(
dpi, { { p.internalBounds.GetLeft(), curScreenPos + 5 }, { p.internalBounds.GetRight(), curScreenPos + 5 } },
p.lineCol, INSET_RECT_FLAG_BORDER_INSET);
curScreenPos += p.yLabelStepPx;
2024-07-30 04:41:58 +10:00
curLabel -= yLabelStep;
}
DrawMonths<money64, kMoney64Undefined>(dpi, p.series, p.numPoints, p.internalBounds, p.xStepPx);
DrawLine<money64, kMoney64Undefined, true>(dpi, p.series, p.numPoints, p.internalBounds, p.xStepPx, p.min, p.max);
DrawLine<money64, kMoney64Undefined, false>(dpi, p.series, p.numPoints, p.internalBounds, p.xStepPx, p.min, p.max);
if (p.hoverIdx >= 0 && p.hoverIdx < p.numPoints)
{
money64 value = p.series[p.hoverIdx];
if (value != kMoney64Undefined)
DrawHoveredValue<money64>(dpi, value, p.hoverIdx, p.internalBounds, p.xStepPx, p.min, p.max);
}
2024-07-30 04:41:58 +10:00
// todo debug code.
ScreenCoordsXY bottomLeft{ p.internalBounds.Point1.x, p.internalBounds.Point2.y };
ScreenCoordsXY topRight{ p.internalBounds.Point2.x, p.internalBounds.Point1.y };
GfxDrawLine(dpi, { p.internalBounds.Point1, topRight }, 33);
GfxDrawLine(dpi, { p.internalBounds.Point1, bottomLeft }, 33);
GfxDrawLine(dpi, { bottomLeft, p.internalBounds.Point2 }, 33);
GfxDrawLine(dpi, { topRight, p.internalBounds.Point2 }, 33);
2020-11-03 01:21:20 -07:00
}
} // namespace OpenRCT2::Graph