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

295 lines
11 KiB
C++
Raw Normal View History

/*****************************************************************************
2020-07-21 15:04:34 +02:00
* Copyright (c) 2014-2020 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.
*****************************************************************************/
2018-06-22 23:20:47 +02:00
#include <openrct2-ui/interface/Graph.h>
#include <openrct2/Context.h>
2018-01-06 18:32:25 +01:00
#include <openrct2/localisation/Date.h>
#include <openrct2/localisation/Localisation.h>
2020-11-03 01:21:20 -07:00
namespace Graph
{
2020-11-03 01:21:20 -07:00
static void DrawMonths(rct_drawpixelinfo* dpi, const uint8_t* history, int32_t count, int32_t baseX, int32_t baseY)
2018-05-08 09:20:13 +02:00
{
2020-11-03 01:21:20 -07:00
int32_t i, yearOver32, currentMonth, currentDay;
currentMonth = date_get_month(gDateMonthsElapsed);
currentDay = gDateMonthTicks;
yearOver32 = (currentMonth * 4) + (currentDay >> 14) - 31;
auto screenCoords = ScreenCoordsXY{ baseX, baseY };
for (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[date_get_month((yearOver32 / 4) + MONTH_COUNT)]);
DrawTextBasic(dpi, screenCoords - ScreenCoordsXY{ 0, 10 }, STR_GRAPH_LABEL, ft, { TextAlignment::CENTRE });
2020-11-03 01:21:20 -07:00
// Draw month mark
gfx_fill_rect(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
}
}
2020-11-03 01:21:20 -07:00
static void DrawLineA(rct_drawpixelinfo* dpi, const uint8_t* history, int32_t count, int32_t baseX, int32_t baseY)
2018-05-08 09:20:13 +02:00
{
2020-11-03 01:21:20 -07:00
int32_t i, x, y, lastX, lastY;
lastX = -1;
lastY = -1;
x = baseX;
for (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-11-03 01:21:20 -07:00
y = baseY + ((255 - history[i]) * 100) / 256;
if (lastX != -1)
{
auto leftTop1 = ScreenCoordsXY{ lastX + 1, lastY + 1 };
auto rightBottom1 = ScreenCoordsXY{ x + 1, y + 1 };
auto leftTop2 = ScreenCoordsXY{ lastX, lastY + 1 };
auto rightBottom2 = ScreenCoordsXY{ x, y + 1 };
gfx_draw_line(dpi, { leftTop1, rightBottom1 }, PALETTE_INDEX_10);
gfx_draw_line(dpi, { leftTop2, rightBottom2 }, PALETTE_INDEX_10);
}
if (i == 0)
gfx_fill_rect(dpi, { { x, y }, { x + 2, y + 2 } }, PALETTE_INDEX_10);
lastX = x;
lastY = y;
}
2020-11-03 01:21:20 -07:00
x += 6;
2017-06-06 23:24:18 +02:00
}
}
2020-11-03 01:21:20 -07:00
static void DrawLineB(rct_drawpixelinfo* dpi, const uint8_t* history, int32_t count, int32_t baseX, int32_t baseY)
2018-06-22 23:20:47 +02:00
{
2020-11-03 01:21:20 -07:00
int32_t i, x, y, lastX, lastY;
lastX = -1;
lastY = -1;
x = baseX;
for (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
{
2020-11-03 01:21:20 -07:00
y = baseY + ((255 - history[i]) * 100) / 256;
if (lastX != -1)
{
auto leftTop = ScreenCoordsXY{ lastX, lastY };
auto rightBottom = ScreenCoordsXY{ x, y };
gfx_draw_line(dpi, { leftTop, rightBottom }, PALETTE_INDEX_21);
}
if (i == 0)
gfx_fill_rect(dpi, { { x - 1, y - 1 }, { x + 1, y + 1 } }, PALETTE_INDEX_21);
lastX = x;
lastY = y;
2017-06-06 23:24:18 +02:00
}
2020-11-03 01:21:20 -07:00
x += 6;
2017-06-06 23:24:18 +02:00
}
}
2020-11-03 01:21:20 -07:00
void Draw(rct_drawpixelinfo* dpi, uint8_t* history, int32_t count, int32_t baseX, int32_t baseY)
2018-06-22 23:20:47 +02:00
{
2020-11-03 01:21:20 -07:00
DrawMonths(dpi, history, count, baseX, baseY);
DrawLineA(dpi, history, count, baseX, baseY);
DrawLineB(dpi, history, count, baseX, baseY);
2017-06-06 23:24:18 +02:00
}
2020-11-03 01:21:20 -07:00
} // namespace Graph
struct FinancialTooltipInfo
{
const ScreenCoordsXY coords;
const money32 money{};
};
2020-11-03 01:21:20 -07:00
static constexpr auto ChartMaxDataCount = 64;
static constexpr auto ChartMaxIndex = ChartMaxDataCount - 1;
static constexpr auto ChartDataWidth = 6;
static constexpr auto ChartMaxWidth = ChartMaxIndex * ChartDataWidth;
static constexpr auto ChartMaxHeight = 164;
static constexpr auto CursorXOffset = 3;
static constexpr auto DefaultDashedLength = 2;
static int32_t IndexForCursorAndHistory(const int32_t historyCount, const int32_t cursorX, const int32_t chartX)
{
2020-11-03 01:21:20 -07:00
const auto offsettedCursorX = cursorX + CursorXOffset;
return (historyCount - 1) - (offsettedCursorX - chartX) / ChartDataWidth;
}
static const ScreenCoordsXY ScreenCoordsForHistoryIndex(
const int32_t index, const money32* history, const int32_t chartX, const int32_t chartY, const int32_t modifier,
const int32_t offset)
{
ScreenCoordsXY coords;
2020-11-03 01:21:20 -07:00
coords.x = chartX + ChartDataWidth * (ChartMaxIndex - index);
coords.y = chartY + ChartMaxHeight - ((((history[index] >> modifier) + offset) * 170) / 256);
return coords;
}
static const FinancialTooltipInfo finance_tooltip_info_from_money(
const money32* history, const int32_t historyCount, const int32_t modifier, const int32_t offset,
const ScreenRect& chartFrame, const ScreenCoordsXY& cursorPosition)
{
if (!chartFrame.Contains(cursorPosition))
{
return { {}, MONEY32_UNDEFINED };
}
const auto historyIndex = IndexForCursorAndHistory(historyCount, cursorPosition.x, chartFrame.GetLeft());
const auto coords = ScreenCoordsForHistoryIndex(
historyIndex, history, chartFrame.GetLeft(), chartFrame.GetTop(), modifier, offset);
return { { coords.x, coords.y }, history[historyIndex] };
}
2020-11-03 01:21:20 -07:00
namespace Graph
{
2020-11-03 01:21:20 -07:00
static void DrawMonths(rct_drawpixelinfo* dpi, const money32* history, int32_t count, int32_t baseX, int32_t baseY)
{
2020-11-03 01:21:20 -07:00
int32_t i, yearOver32, currentMonth, currentDay;
currentMonth = date_get_month(gDateMonthsElapsed);
currentDay = gDateMonthTicks;
yearOver32 = (currentMonth * 4) + (currentDay >> 14) - 31;
auto screenCoords = ScreenCoordsXY{ baseX, baseY };
for (i = count - 1; i >= 0; i--)
{
if (history[i] != MONEY32_UNDEFINED && yearOver32 % 4 == 0)
{
// Draw month text
int32_t monthFormat = DateGameShortMonthNames[date_get_month((yearOver32 / 4) + MONTH_COUNT)];
DrawTextBasic(
dpi, screenCoords - ScreenCoordsXY{ 0, 10 }, STR_GRAPH_LABEL, &monthFormat, { TextAlignment::CENTRE });
2020-11-03 01:21:20 -07:00
// Draw month mark
gfx_fill_rect(dpi, { screenCoords, screenCoords + ScreenCoordsXY{ 0, 3 } }, PALETTE_INDEX_10);
}
yearOver32 = (yearOver32 + 1) % 32;
screenCoords.x += 6;
}
}
2020-11-03 01:21:20 -07:00
static void DrawLineA(
rct_drawpixelinfo* dpi, const money32* history, int32_t count, int32_t baseX, int32_t baseY, int32_t modifier,
int32_t offset)
{
2020-11-03 01:21:20 -07:00
int32_t i, x, y, lastX, lastY;
lastX = -1;
lastY = -1;
x = baseX;
for (i = count - 1; i >= 0; i--)
{
if (history[i] != MONEY32_UNDEFINED)
{
y = baseY + 170 - 6 - ((((history[i] >> modifier) + offset) * 170) / 256);
2020-11-03 01:21:20 -07:00
if (lastX != -1)
{
auto leftTop1 = ScreenCoordsXY{ lastX + 1, lastY + 1 };
auto rightBottom1 = ScreenCoordsXY{ x + 1, y + 1 };
auto leftTop2 = ScreenCoordsXY{ lastX, lastY + 1 };
auto rightBottom2 = ScreenCoordsXY{ x, y + 1 };
gfx_draw_line(dpi, { leftTop1, rightBottom1 }, PALETTE_INDEX_10);
gfx_draw_line(dpi, { leftTop2, rightBottom2 }, PALETTE_INDEX_10);
}
if (i == 0)
gfx_fill_rect(dpi, { { x, y }, { x + 2, y + 2 } }, PALETTE_INDEX_10);
lastX = x;
lastY = y;
}
x += 6;
}
}
2020-11-03 01:21:20 -07:00
static void DrawLineB(
rct_drawpixelinfo* dpi, const money32* history, int32_t count, int32_t baseX, int32_t baseY, int32_t modifier,
int32_t offset)
{
int32_t i, x, y, lastX, lastY;
2020-11-03 01:21:20 -07:00
lastX = -1;
lastY = -1;
x = baseX;
for (i = count - 1; i >= 0; i--)
{
if (history[i] != MONEY32_UNDEFINED)
{
y = baseY + 170 - 6 - ((((history[i] >> modifier) + offset) * 170) / 256);
2020-11-03 01:21:20 -07:00
if (lastX != -1)
{
auto leftTop = ScreenCoordsXY{ lastX, lastY };
auto rightBottom = ScreenCoordsXY{ x, y };
gfx_draw_line(dpi, { leftTop, rightBottom }, PALETTE_INDEX_21);
}
if (i == 0)
gfx_fill_rect(dpi, { { x - 1, y - 1 }, { x + 1, y + 1 } }, PALETTE_INDEX_21);
lastX = x;
lastY = y;
}
x += 6;
}
}
static void DrawHoveredValue(
rct_drawpixelinfo* dpi, const money32* history, const int32_t historyCount, const int32_t baseX, const int32_t baseY,
const int32_t modifier, const int32_t offset)
{
const auto cursorPosition = context_get_cursor_position_scaled();
const ScreenRect chartFrame{ { baseX, baseY }, { baseX + ChartMaxWidth, baseY + ChartMaxHeight } };
if (!chartFrame.Contains(cursorPosition))
{
return;
}
const auto info = finance_tooltip_info_from_money(
history, ChartMaxDataCount, modifier, offset, chartFrame, cursorPosition);
if (info.money == MONEY32_UNDEFINED)
{
return;
}
gfx_draw_dashed_line(dpi, { { info.coords.x, chartFrame.GetTop() }, info.coords }, DefaultDashedLength, 0);
gfx_draw_dashed_line(dpi, { { chartFrame.GetLeft() - 10, info.coords.y }, info.coords }, DefaultDashedLength, 0);
if (cursorPosition.y > info.coords.y)
{
gfx_draw_dashed_line(dpi, { info.coords, { info.coords.x, cursorPosition.y } }, DefaultDashedLength, 0);
}
DrawTextBasic(
dpi, info.coords - ScreenCoordsXY{ 0, 16 }, STR_FINANCES_SUMMARY_EXPENDITURE_VALUE, &info.money,
{ TextAlignment::CENTRE });
2020-11-03 01:21:20 -07:00
gfx_fill_rect(
dpi, { { info.coords - ScreenCoordsXY{ 2, 2 } }, info.coords + ScreenCoordsXY{ 2, 2 } }, PALETTE_INDEX_10);
gfx_fill_rect(
dpi, { { info.coords - ScreenCoordsXY{ 1, 1 } }, { info.coords + ScreenCoordsXY{ 1, 1 } } }, PALETTE_INDEX_21);
}
void Draw(
rct_drawpixelinfo* dpi, const money32* history, const int32_t count, const int32_t baseX, const int32_t baseY,
const int32_t modifier, const int32_t offset)
{
DrawMonths(dpi, history, count, baseX, baseY);
DrawLineA(dpi, history, count, baseX, baseY, modifier, offset);
DrawLineB(dpi, history, count, baseX, baseY, modifier, offset);
DrawHoveredValue(dpi, history, count, baseX, baseY, modifier, offset);
}
} // namespace Graph