2014-08-10 18:12:07 +01:00
|
|
|
/*****************************************************************************
|
2024-01-01 05:52:28 -06:00
|
|
|
* Copyright (c) 2014-2024 OpenRCT2 developers
|
2014-08-10 18:12:07 +01:00
|
|
|
*
|
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
|
2014-08-10 18:12:07 +01:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
2014-08-10 18:12:07 +01:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
2024-05-10 23:24:46 +02:00
|
|
|
#include "../UiStringIds.h"
|
|
|
|
|
|
2018-06-22 23:20:47 +02:00
|
|
|
#include <openrct2-ui/interface/Graph.h>
|
2023-04-02 10:25:41 +02:00
|
|
|
#include <openrct2/Date.h>
|
2021-12-11 18:06:06 -05:00
|
|
|
#include <openrct2/localisation/Formatter.h>
|
2024-07-12 22:18:43 +02:00
|
|
|
#include <openrct2/localisation/Localisation.Date.h>
|
2024-05-10 23:24:46 +02:00
|
|
|
|
|
|
|
|
using namespace OpenRCT2;
|
2014-08-10 18:12:07 +01:00
|
|
|
|
2024-07-26 09:59:58 +02:00
|
|
|
namespace OpenRCT2::Graph
|
2014-08-10 18:12:07 +01:00
|
|
|
{
|
2023-04-03 21:41:56 +02:00
|
|
|
static void DrawMonths(DrawPixelInfo& dpi, const uint8_t* history, int32_t count, const ScreenCoordsXY& origCoords)
|
2018-05-08 09:20:13 +02:00
|
|
|
{
|
2023-04-02 10:25:41 +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();
|
2023-01-17 00:12:35 +01:00
|
|
|
ft.Add<uint32_t>(DateGameShortMonthNames[DateGetMonth((yearOver32 / 4) + MONTH_COUNT)]);
|
2021-03-27 07:40:17 -06:00
|
|
|
DrawTextBasic(
|
2023-04-03 21:41:56 +02:00
|
|
|
dpi, screenCoords - ScreenCoordsXY{ 0, 10 }, STR_GRAPH_LABEL, ft,
|
2022-10-12 22:40:59 +02:00
|
|
|
{ FontStyle::Small, TextAlignment::CENTRE });
|
2020-11-03 01:21:20 -07:00
|
|
|
|
|
|
|
|
// Draw month mark
|
2023-04-03 23:16:34 +02:00
|
|
|
GfxFillRect(dpi, { screenCoords, screenCoords + ScreenCoordsXY{ 0, 3 } }, PALETTE_INDEX_10);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2014-08-10 18:12:07 +01:00
|
|
|
|
2020-11-03 01:21:20 -07:00
|
|
|
yearOver32 = (yearOver32 + 1) % 32;
|
|
|
|
|
screenCoords.x += 6;
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
2014-08-10 18:12:07 +01:00
|
|
|
|
2023-04-03 21:41:56 +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)
|
2020-07-12 18:50:52 -03: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 leftTop1 = lastCoords + ScreenCoordsXY{ 1, 1 };
|
|
|
|
|
auto rightBottom1 = coords + ScreenCoordsXY{ 1, 1 };
|
|
|
|
|
auto leftTop2 = lastCoords + ScreenCoordsXY{ 0, 1 };
|
|
|
|
|
auto rightBottom2 = coords + ScreenCoordsXY{ 0, 1 };
|
2023-04-03 23:16:34 +02:00
|
|
|
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)
|
2023-04-03 23:16:34 +02:00
|
|
|
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;
|
2020-07-12 18:50:52 -03:00
|
|
|
}
|
2021-03-27 00:39:11 -03:00
|
|
|
coords.x += 6;
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
2014-08-10 18:12:07 +01:00
|
|
|
|
2023-04-03 21:41:56 +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;
|
2023-04-03 23:16:34 +02:00
|
|
|
GfxDrawLine(dpi, { leftTop, rightBottom }, PALETTE_INDEX_21);
|
2020-11-03 01:21:20 -07:00
|
|
|
}
|
|
|
|
|
if (i == 0)
|
2023-04-03 23:16:34 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
2014-08-10 18:12:07 +01:00
|
|
|
|
2023-04-03 21:41:56 +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
|
|
|
}
|
2024-07-26 09:59:58 +02:00
|
|
|
} // namespace OpenRCT2::Graph
|
2014-08-10 18:12:07 +01:00
|
|
|
|
2024-07-26 09:59:58 +02:00
|
|
|
namespace OpenRCT2::Graph
|
2020-03-28 05:03:10 -05:00
|
|
|
{
|
2024-07-30 04:41:58 +10:00
|
|
|
constexpr int32_t kDashLength = 2;
|
|
|
|
|
|
|
|
|
|
template<typename T, T TkNoValue>
|
2024-07-30 21:07:49 +10:00
|
|
|
static void DrawMonths(DrawPixelInfo& dpi, const T* series, int32_t count, const ScreenRect& bounds, const int32_t xStep)
|
2020-03-28 05:03:10 -05:00
|
|
|
{
|
2023-04-02 10:25:41 +02:00
|
|
|
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;
|
2023-04-02 10:25:41 +02:00
|
|
|
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
|
2021-07-27 20:32:27 +02:00
|
|
|
auto ft = Formatter();
|
2023-01-17 00:12:35 +01:00
|
|
|
ft.Add<StringId>(DateGameShortMonthNames[DateGetMonth((yearOver32 / 4) + MONTH_COUNT)]);
|
2021-02-27 15:23:50 +01:00
|
|
|
DrawTextBasic(
|
2024-07-30 04:41:58 +10:00
|
|
|
dpi, screenCoords - ScreenCoordsXY{ 0, 13 }, STR_GRAPH_LABEL, ft,
|
2022-10-12 22:40:59 +02:00
|
|
|
{ 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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-30 21:07:49 +10:00
|
|
|
template<typename T>
|
2020-11-03 01:21:20 -07:00
|
|
|
static void DrawHoveredValue(
|
2024-07-30 21:07:49 +10:00
|
|
|
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;
|
2024-07-30 21:07:49 +10:00
|
|
|
|
|
|
|
|
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
|
|
|
|
2024-07-30 21:07:49 +10: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);
|
|
|
|
|
|
2021-07-27 20:32:27 +02:00
|
|
|
auto ft = Formatter();
|
2024-07-30 04:41:58 +10:00
|
|
|
ft.Add<money64>(value);
|
2021-02-27 15:23:50 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-30 21:07:49 +10:00
|
|
|
void DrawFinanceGraph(DrawPixelInfo& dpi, const GraphProperties<money64>& p)
|
2024-07-30 04:41:58 +10:00
|
|
|
{
|
2024-07-30 21:07:49 +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(
|
2024-07-30 21:07:49 +10:00
|
|
|
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(
|
2024-07-30 21:07:49 +10:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-30 21:07:49 +10:00
|
|
|
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.
|
2024-07-30 21:07:49 +10:00
|
|
|
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
|
|
|
}
|
2024-07-26 09:59:58 +02:00
|
|
|
} // namespace OpenRCT2::Graph
|