2017-06-01 21:55:10 +02:00
|
|
|
#pragma region Copyright (c) 2014-2017 OpenRCT2 Developers
|
2016-05-04 19:24:41 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
|
|
|
|
*
|
|
|
|
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
|
|
|
|
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
|
|
|
|
*
|
|
|
|
* OpenRCT2 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 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* A full copy of the GNU General Public License can be found in licence.txt
|
|
|
|
*****************************************************************************/
|
|
|
|
#pragma endregion
|
|
|
|
|
2018-01-19 13:41:14 +01:00
|
|
|
#include <fstream>
|
|
|
|
#include <vector>
|
2017-08-06 05:22:00 +02:00
|
|
|
#include <openrct2/Context.h>
|
|
|
|
#include <openrct2/core/Math.hpp>
|
2018-01-19 13:41:14 +01:00
|
|
|
#include <openrct2/core/String.hpp>
|
2018-01-06 18:32:25 +01:00
|
|
|
#include <openrct2/localisation/Localisation.h>
|
2018-01-21 03:13:32 +01:00
|
|
|
#include <openrct2/OpenRCT2.h>
|
2017-09-18 17:05:28 +02:00
|
|
|
#include <openrct2/platform/platform.h>
|
2018-01-21 03:13:32 +01:00
|
|
|
#include <openrct2/PlatformEnvironment.h>
|
2017-12-13 13:02:24 +01:00
|
|
|
#include <openrct2/util/Util.h>
|
2018-01-19 13:41:14 +01:00
|
|
|
#include <openrct2-ui/windows/Window.h>
|
|
|
|
#include <openrct2-ui/interface/Widget.h>
|
2018-03-19 23:28:40 +01:00
|
|
|
#include <openrct2/drawing/Drawing.h>
|
2015-06-13 14:30:50 +02:00
|
|
|
|
2018-01-21 03:13:32 +01:00
|
|
|
using namespace OpenRCT2;
|
|
|
|
|
2018-05-16 20:41:29 +02:00
|
|
|
// clang-format off
|
2015-06-13 14:30:50 +02:00
|
|
|
enum {
|
2017-06-06 23:24:18 +02:00
|
|
|
WIDX_BACKGROUND,
|
|
|
|
WIDX_TITLE,
|
|
|
|
WIDX_CLOSE,
|
|
|
|
WIDX_CONTENT_PANEL,
|
|
|
|
WIDX_SCROLL
|
2015-06-13 14:30:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#define WW 500
|
|
|
|
#define WH 400
|
|
|
|
#define MIN_WW 300
|
|
|
|
#define MIN_WH 200
|
|
|
|
|
2017-10-09 17:13:14 +02:00
|
|
|
static rct_widget window_changelog_widgets[] = {
|
2017-06-06 23:24:18 +02:00
|
|
|
{ WWT_FRAME, 0, 0, WW - 1, 0, WH - 1, 0xFFFFFFFF, STR_NONE }, // panel / background
|
|
|
|
{ WWT_CAPTION, 0, 1, WW - 2, 1, 14, STR_CHANGELOG_TITLE, STR_WINDOW_TITLE_TIP }, // title bar
|
|
|
|
{ WWT_CLOSEBOX, 0, WW - 13, WW - 3, 2, 13, STR_CLOSE_X, STR_CLOSE_WINDOW_TIP }, // close x button
|
|
|
|
{ WWT_RESIZE, 1, 0, WW - 1, 14, WH - 1, 0xFFFFFFFF, STR_NONE }, // content panel
|
|
|
|
{ WWT_SCROLL, 1, 3, WW - 3, 16, WH - 15, SCROLL_BOTH, STR_NONE }, // scroll area
|
|
|
|
{ WIDGETS_END },
|
2015-06-13 14:30:50 +02:00
|
|
|
};
|
|
|
|
|
2015-07-10 02:39:16 +02:00
|
|
|
static void window_changelog_close(rct_window *w);
|
2017-05-01 15:41:45 +02:00
|
|
|
static void window_changelog_mouseup(rct_window *w, rct_widgetindex widgetIndex);
|
2015-07-10 02:39:16 +02:00
|
|
|
static void window_changelog_resize(rct_window *w);
|
2017-01-04 22:17:08 +01:00
|
|
|
static void window_changelog_scrollgetsize(rct_window *w, sint32 scrollIndex, sint32 *width, sint32 *height);
|
2015-07-10 02:39:16 +02:00
|
|
|
static void window_changelog_invalidate(rct_window *w);
|
|
|
|
static void window_changelog_paint(rct_window *w, rct_drawpixelinfo *dpi);
|
2017-01-04 22:17:08 +01:00
|
|
|
static void window_changelog_scrollpaint(rct_window *w, rct_drawpixelinfo *dpi, sint32 scrollIndex);
|
2015-07-10 02:39:16 +02:00
|
|
|
|
|
|
|
static rct_window_event_list window_changelog_events = {
|
2017-06-06 23:24:18 +02:00
|
|
|
window_changelog_close,
|
|
|
|
window_changelog_mouseup,
|
|
|
|
window_changelog_resize,
|
2017-08-15 10:07:44 +02:00
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2017-06-06 23:24:18 +02:00
|
|
|
window_changelog_scrollgetsize,
|
2017-08-15 10:07:44 +02:00
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2017-06-06 23:24:18 +02:00
|
|
|
window_changelog_invalidate,
|
|
|
|
window_changelog_paint,
|
|
|
|
window_changelog_scrollpaint
|
2015-06-13 14:30:50 +02:00
|
|
|
};
|
2018-05-16 20:41:29 +02:00
|
|
|
// clang-format on
|
2015-06-13 14:30:50 +02:00
|
|
|
|
|
|
|
static bool window_changelog_read_file();
|
|
|
|
static void window_changelog_dispose_file();
|
|
|
|
|
2018-01-19 13:41:14 +01:00
|
|
|
static std::string _changelogText;
|
|
|
|
static std::vector<const char *> _changelogLines;
|
2017-01-04 22:17:08 +01:00
|
|
|
static sint32 _changelogLongestLineWidth = 0;
|
2015-06-13 14:30:50 +02:00
|
|
|
|
2017-08-06 05:22:00 +02:00
|
|
|
rct_window * window_changelog_open()
|
2015-06-13 14:30:50 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
rct_window* window;
|
2015-06-13 14:30:50 +02:00
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
window = window_bring_to_front_by_class(WC_CHANGELOG);
|
2017-08-15 10:07:44 +02:00
|
|
|
if (window != nullptr)
|
2017-06-06 23:24:18 +02:00
|
|
|
return window;
|
2015-06-13 14:30:50 +02:00
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
if (!window_changelog_read_file())
|
2017-08-15 10:07:44 +02:00
|
|
|
return nullptr;
|
2015-06-13 14:30:50 +02:00
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
sint32 screenWidth = context_get_width();
|
|
|
|
sint32 screenHeight = context_get_height();
|
2015-06-13 14:30:50 +02:00
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
window = window_create_centred(
|
|
|
|
screenWidth * 4 / 5,
|
|
|
|
screenHeight * 4 / 5,
|
|
|
|
&window_changelog_events,
|
|
|
|
WC_CHANGELOG,
|
|
|
|
WF_RESIZABLE
|
|
|
|
);
|
|
|
|
window->widgets = window_changelog_widgets;
|
|
|
|
window->enabled_widgets = (1 << WIDX_CLOSE);
|
2015-06-13 14:30:50 +02:00
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
window_init_scroll_widgets(window);
|
|
|
|
window->min_width = MIN_WW;
|
|
|
|
window->min_height = MIN_WH;
|
|
|
|
window->max_width = MIN_WW;
|
|
|
|
window->max_height = MIN_WH;
|
|
|
|
return window;
|
2015-06-13 14:30:50 +02:00
|
|
|
}
|
|
|
|
|
2018-04-20 13:56:37 +02:00
|
|
|
static void window_changelog_close([[maybe_unused]] rct_window * w)
|
2015-06-13 14:30:50 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
window_changelog_dispose_file();
|
2015-06-13 14:30:50 +02:00
|
|
|
}
|
|
|
|
|
2017-05-01 15:41:45 +02:00
|
|
|
static void window_changelog_mouseup(rct_window *w, rct_widgetindex widgetIndex)
|
2015-06-13 14:30:50 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
switch (widgetIndex) {
|
|
|
|
case WIDX_CLOSE:
|
|
|
|
window_close(w);
|
|
|
|
break;
|
|
|
|
}
|
2015-06-13 14:30:50 +02:00
|
|
|
}
|
|
|
|
|
2015-07-10 02:39:16 +02:00
|
|
|
static void window_changelog_resize(rct_window *w)
|
2015-06-13 14:30:50 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
sint32 screenWidth = context_get_width();
|
|
|
|
sint32 screenHeight = context_get_height();
|
2015-06-13 14:30:50 +02:00
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
w->max_width = (screenWidth * 4) / 5;
|
|
|
|
w->max_height = (screenHeight * 4) / 5;
|
2015-06-13 14:30:50 +02:00
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
w->min_width = MIN_WW;
|
|
|
|
w->min_height = MIN_WH;
|
|
|
|
if (w->width < w->min_width) {
|
|
|
|
window_invalidate(w);
|
|
|
|
w->width = w->min_width;
|
|
|
|
}
|
|
|
|
if (w->height < w->min_height) {
|
|
|
|
window_invalidate(w);
|
|
|
|
w->height = w->min_height;
|
|
|
|
}
|
2015-06-13 14:30:50 +02:00
|
|
|
}
|
|
|
|
|
2018-04-20 13:56:37 +02:00
|
|
|
static void window_changelog_scrollgetsize(
|
|
|
|
[[maybe_unused]] rct_window * w, [[maybe_unused]] sint32 scrollIndex, sint32 * width, sint32 * height)
|
2015-06-13 14:30:50 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
*width = _changelogLongestLineWidth + 4;
|
2018-06-08 09:30:20 +02:00
|
|
|
|
|
|
|
const sint32 lineHeight = font_get_line_height(gCurrentFontSpriteBase);
|
|
|
|
*height = (sint32)(_changelogLines.size() * lineHeight);
|
2015-06-13 14:30:50 +02:00
|
|
|
}
|
|
|
|
|
2015-07-10 02:39:16 +02:00
|
|
|
static void window_changelog_invalidate(rct_window *w)
|
2015-06-13 14:30:50 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
window_changelog_widgets[WIDX_BACKGROUND].right = w->width - 1;
|
|
|
|
window_changelog_widgets[WIDX_BACKGROUND].bottom = w->height - 1;
|
|
|
|
window_changelog_widgets[WIDX_TITLE].right = w->width - 2;
|
|
|
|
window_changelog_widgets[WIDX_CLOSE].left = w->width - 13;
|
|
|
|
window_changelog_widgets[WIDX_CLOSE].right = w->width - 3;
|
|
|
|
window_changelog_widgets[WIDX_CONTENT_PANEL].right = w->width - 1;
|
|
|
|
window_changelog_widgets[WIDX_CONTENT_PANEL].bottom = w->height - 1;
|
|
|
|
window_changelog_widgets[WIDX_SCROLL].right = w->width - 3;
|
|
|
|
window_changelog_widgets[WIDX_SCROLL].bottom = w->height - 15;
|
2015-06-13 14:30:50 +02:00
|
|
|
}
|
|
|
|
|
2015-07-10 02:39:16 +02:00
|
|
|
static void window_changelog_paint(rct_window *w, rct_drawpixelinfo *dpi)
|
2015-06-13 14:30:50 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
window_draw_widgets(w, dpi);
|
2015-06-13 14:30:50 +02:00
|
|
|
}
|
|
|
|
|
2018-04-20 13:56:37 +02:00
|
|
|
static void window_changelog_scrollpaint(rct_window * w, rct_drawpixelinfo * dpi, [[maybe_unused]] sint32 scrollIndex)
|
2015-06-13 14:30:50 +02:00
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
gCurrentFontFlags = 0;
|
|
|
|
gCurrentFontSpriteBase = FONT_SPRITE_BASE_MEDIUM;
|
2015-06-13 14:30:50 +02:00
|
|
|
|
2018-06-08 09:30:20 +02:00
|
|
|
const sint32 lineHeight = font_get_line_height(gCurrentFontSpriteBase);
|
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
sint32 x = 3;
|
2018-06-08 09:30:20 +02:00
|
|
|
sint32 y = 3 - lineHeight;
|
2018-01-19 13:41:14 +01:00
|
|
|
for (auto line : _changelogLines)
|
|
|
|
{
|
2018-06-08 09:30:20 +02:00
|
|
|
y += lineHeight;
|
|
|
|
if (y + lineHeight < dpi->y || y >= dpi->y + dpi->height)
|
|
|
|
continue;
|
|
|
|
|
2018-01-19 13:41:14 +01:00
|
|
|
gfx_draw_string(dpi, (char *)line, w->colours[0], x, y);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2015-06-13 14:30:50 +02:00
|
|
|
}
|
|
|
|
|
2018-01-21 03:13:32 +01:00
|
|
|
static std::string GetChangelogPath()
|
2015-06-13 14:30:50 +02:00
|
|
|
{
|
2018-01-21 03:13:32 +01:00
|
|
|
auto env = GetContext()->GetPlatformEnvironment();
|
|
|
|
return env->GetFilePath(PATHID::CHANGELOG);
|
|
|
|
}
|
2018-01-19 13:41:14 +01:00
|
|
|
|
2018-01-21 03:13:32 +01:00
|
|
|
static std::string GetChangelogText()
|
|
|
|
{
|
|
|
|
auto path = GetChangelogPath();
|
2018-01-19 13:41:14 +01:00
|
|
|
#if defined(_WIN32) && !defined(__MINGW32__)
|
|
|
|
auto pathW = String::ToUtf16(path);
|
2018-01-21 03:13:32 +01:00
|
|
|
auto fs = std::ifstream(pathW, std::ios::in);
|
2018-01-19 13:41:14 +01:00
|
|
|
#else
|
2018-01-21 03:13:32 +01:00
|
|
|
auto fs = std::ifstream(path, std::ios::in);
|
2018-01-19 13:41:14 +01:00
|
|
|
#endif
|
2018-01-21 03:13:32 +01:00
|
|
|
if (!fs.is_open())
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Unable to open " + path);
|
|
|
|
}
|
2018-01-19 13:41:14 +01:00
|
|
|
return std::string((std::istreambuf_iterator<char>(fs)), std::istreambuf_iterator<char>());
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool window_changelog_read_file()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
_changelogText = GetChangelogText();
|
|
|
|
}
|
|
|
|
catch (const std::bad_alloc &)
|
|
|
|
{
|
|
|
|
log_error("Unable to allocate memory for changelog.txt");
|
2017-06-06 23:24:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2018-01-19 13:41:14 +01:00
|
|
|
catch (const std::exception &)
|
|
|
|
{
|
|
|
|
log_error("Unable to read changelog.txt");
|
2017-06-06 23:24:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2015-06-13 14:30:50 +02:00
|
|
|
|
2018-01-21 13:41:42 +01:00
|
|
|
// Non-const cast required until C++17 is enabled
|
|
|
|
auto * start = (char *)_changelogText.data();
|
2018-01-19 13:41:14 +01:00
|
|
|
if (_changelogText.size() >= 3 && utf8_is_bom(start))
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
start += 3;
|
2018-01-19 13:41:14 +01:00
|
|
|
}
|
2015-06-13 14:30:50 +02:00
|
|
|
|
2018-01-19 13:41:14 +01:00
|
|
|
_changelogLines.clear();
|
|
|
|
_changelogLines.push_back(start);
|
|
|
|
auto ch = start;
|
|
|
|
while (*ch != '\0')
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
uint8 c = *ch;
|
2018-01-19 13:41:14 +01:00
|
|
|
if (c == '\n')
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
*ch++ = 0;
|
2018-01-19 13:41:14 +01:00
|
|
|
_changelogLines.push_back(ch);
|
|
|
|
}
|
|
|
|
else if (utf8_is_format_code(c))
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
*ch++ = FORMAT_OUTLINE_OFF;
|
2018-01-19 13:41:14 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-06 23:24:18 +02:00
|
|
|
ch++;
|
|
|
|
}
|
|
|
|
}
|
2015-06-13 14:30:50 +02:00
|
|
|
|
2017-06-06 23:24:18 +02:00
|
|
|
gCurrentFontSpriteBase = FONT_SPRITE_BASE_MEDIUM;
|
|
|
|
_changelogLongestLineWidth = 0;
|
2018-01-19 13:41:14 +01:00
|
|
|
for (auto line : _changelogLines)
|
|
|
|
{
|
|
|
|
auto width = gfx_get_string_width(line);
|
2017-08-02 00:20:32 +02:00
|
|
|
_changelogLongestLineWidth = Math::Max(width, _changelogLongestLineWidth);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
|
|
|
return true;
|
2015-06-13 14:30:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void window_changelog_dispose_file()
|
|
|
|
{
|
2018-01-19 13:41:14 +01:00
|
|
|
_changelogText = std::string();
|
|
|
|
_changelogLines.clear();
|
|
|
|
_changelogLines.shrink_to_fit();
|
2015-09-07 14:57:39 +02:00
|
|
|
}
|