Files
OpenRCT2-Web/src/openrct2-ui/windows/Error.cpp
T

179 lines
5.9 KiB
C++
Raw Normal View History

/*****************************************************************************
2022-10-01 03:42:14 -04:00
* Copyright (c) 2014-2022 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-08-12 12:50:40 +01:00
#include <algorithm>
2018-06-22 23:21:44 +02:00
#include <openrct2-ui/interface/Widget.h>
#include <openrct2-ui/windows/Window.h>
#include <openrct2/Context.h>
#include <openrct2/OpenRCT2.h>
#include <openrct2/audio/audio.h>
2018-03-19 23:28:40 +01:00
#include <openrct2/drawing/Drawing.h>
2018-06-22 23:21:44 +02:00
#include <openrct2/drawing/Font.h>
2021-12-11 18:06:06 -05:00
#include <openrct2/localisation/Formatter.h>
2018-06-22 23:21:44 +02:00
#include <openrct2/localisation/Localisation.h>
2016-09-03 14:22:03 +01:00
// clang-format off
2014-10-05 02:51:17 +01:00
enum {
2017-06-06 23:24:18 +02:00
WIDX_BACKGROUND
2014-10-05 02:51:17 +01:00
};
static rct_widget window_error_widgets[] = {
MakeWidget({0, 0}, {200, 42}, WindowWidgetType::ImgBtn, WindowColour::Primary),
2021-09-26 11:11:42 +02:00
WIDGETS_END,
2014-10-05 02:51:17 +01:00
};
static void WindowErrorUnknown5(rct_window *w);
static void WindowErrorPaint(rct_window *w, rct_drawpixelinfo *dpi);
2014-10-05 02:51:17 +01:00
static WindowEventList window_error_events([](auto& events)
{
events.unknown_05 = &WindowErrorUnknown5;
events.paint = &WindowErrorPaint;
});
// clang-format on
2014-10-05 02:51:17 +01:00
2020-03-03 21:11:26 +00:00
static std::string _window_error_text;
static uint16_t _window_error_num_lines;
2014-10-05 02:51:17 +01:00
/**
2015-10-20 20:16:30 +02:00
*
* rct2: 0x0066792F
2014-10-05 02:51:17 +01:00
*
* bx: title
* dx: message
*/
2022-07-31 14:22:58 +02:00
rct_window* WindowErrorOpen(StringId title, StringId message, const Formatter& args)
{
auto titlez = format_string(title, args.Data());
auto messagez = format_string(message, args.Data());
return WindowErrorOpen(titlez, messagez);
2020-03-03 21:11:26 +00:00
}
rct_window* WindowErrorOpen(std::string_view title, std::string_view message)
2020-03-03 21:11:26 +00:00
{
2021-02-27 12:40:20 +01:00
int32_t numLines, width, height, maxY;
2018-06-22 23:21:44 +02:00
rct_window* w;
2014-10-05 02:51:17 +01:00
window_close_by_class(WindowClass::Error);
2020-03-03 21:11:26 +00:00
auto& buffer = _window_error_text;
2020-10-13 23:34:18 +01:00
buffer.assign("{BLACK}");
2020-03-03 21:11:26 +00:00
buffer.append(title);
2014-10-05 02:51:17 +01:00
2017-06-06 23:24:18 +02:00
// Format the message
2020-03-03 21:11:26 +00:00
if (!message.empty())
2018-06-22 23:21:44 +02:00
{
2020-10-13 23:34:18 +01:00
buffer.push_back('\n');
2020-03-03 21:11:26 +00:00
buffer.append(message);
2017-06-06 23:24:18 +02:00
}
2014-10-05 02:51:17 +01:00
2020-03-03 21:11:26 +00:00
log_verbose("show error, %s", buffer.c_str() + 1);
// Don't do unnecessary work in headless. Also saves checking if cursor state is null.
2018-06-22 23:21:44 +02:00
if (gOpenRCT2Headless)
{
return nullptr;
}
2017-06-06 23:24:18 +02:00
// Check if there is any text to display
2020-03-03 21:11:26 +00:00
if (buffer.size() <= 1)
return nullptr;
2014-10-05 02:51:17 +01:00
width = gfx_get_string_width_new_lined(buffer.data(), FontStyle::Medium);
2020-03-02 23:55:18 +00:00
width = std::clamp(width, 64, 196);
2014-10-05 02:51:17 +01:00
gfx_wrap_string(buffer.data(), width + 1, FontStyle::Medium, &numLines);
2014-10-05 02:51:17 +01:00
2017-06-06 23:24:18 +02:00
_window_error_num_lines = numLines;
width = width + 3;
height = (numLines + 1) * font_get_line_height(FontStyle::Medium) + 4;
2014-10-05 02:51:17 +01:00
2017-06-06 23:24:18 +02:00
window_error_widgets[WIDX_BACKGROUND].right = width;
window_error_widgets[WIDX_BACKGROUND].bottom = height;
2014-10-05 02:51:17 +01:00
int32_t screenWidth = context_get_width();
int32_t screenHeight = context_get_height();
2018-06-22 23:21:44 +02:00
const CursorState* state = context_get_cursor_state();
ScreenCoordsXY windowPosition = state->position - ScreenCoordsXY(width / 2, -26);
windowPosition.x = std::clamp(windowPosition.x, 0, screenWidth);
windowPosition.y = std::max(22, windowPosition.y);
2017-06-06 23:24:18 +02:00
maxY = screenHeight - height;
if (windowPosition.y > maxY)
2018-06-22 23:21:44 +02:00
{
windowPosition.y = std::min(windowPosition.y - height - 40, maxY);
2017-06-06 23:24:18 +02:00
}
2015-10-20 20:16:30 +02:00
w = WindowCreate(
windowPosition, width, height, &window_error_events, WindowClass::Error,
WF_STICK_TO_FRONT | WF_TRANSPARENT | WF_RESIZABLE);
2017-06-06 23:24:18 +02:00
w->widgets = window_error_widgets;
w->error.var_480 = 0;
2018-06-22 23:21:44 +02:00
if (!gDisableErrorWindowSound)
{
2020-10-07 00:34:42 +03:00
OpenRCT2::Audio::Play(OpenRCT2::Audio::SoundId::Error, 0, w->windowPos.x + (w->width / 2));
2017-06-06 23:24:18 +02:00
}
return w;
2014-10-05 02:51:17 +01:00
}
/**
2015-10-20 20:16:30 +02:00
*
2014-10-05 02:51:17 +01:00
* rct2: 0x00667BFE
*/
static void WindowErrorUnknown5(rct_window* w)
2014-10-05 02:51:17 +01:00
{
2017-06-06 23:24:18 +02:00
w->error.var_480++;
if (w->error.var_480 >= 8)
window_close(*w);
2014-10-05 02:51:17 +01:00
}
/**
2015-10-20 20:16:30 +02:00
*
2014-10-05 02:51:17 +01:00
* rct2: 0x00667AA3
*/
static void WindowErrorPaint(rct_window* w, rct_drawpixelinfo* dpi)
2014-10-05 02:51:17 +01:00
{
ScreenCoordsXY leftTop{ w->windowPos };
ScreenCoordsXY rightBottom{ w->windowPos + ScreenCoordsXY{ w->width - 1, w->height - 1 } };
ScreenCoordsXY leftBottom{ leftTop.x, rightBottom.y };
ScreenCoordsXY rightTop{ rightBottom.x, leftTop.y };
2014-10-05 02:51:17 +01:00
gfx_filter_rect(
dpi, ScreenRect{ leftTop + ScreenCoordsXY{ 1, 1 }, rightBottom - ScreenCoordsXY{ 1, 1 } }, FilterPaletteID::Palette45);
gfx_filter_rect(dpi, ScreenRect{ leftTop, rightBottom }, FilterPaletteID::PaletteGlassSaturatedRed);
2014-10-05 02:51:17 +01:00
gfx_filter_rect(
dpi, ScreenRect{ leftTop + ScreenCoordsXY{ 0, 2 }, leftBottom - ScreenCoordsXY{ 0, 2 } },
FilterPaletteID::PaletteDarken3);
gfx_filter_rect(
dpi, ScreenRect{ rightTop + ScreenCoordsXY{ 0, 2 }, rightBottom - ScreenCoordsXY{ 0, 2 } },
FilterPaletteID::PaletteDarken3);
gfx_filter_rect(
dpi, ScreenRect{ leftBottom + ScreenCoordsXY{ 2, 0 }, rightBottom - ScreenCoordsXY{ 2, 0 } },
FilterPaletteID::PaletteDarken3);
gfx_filter_rect(
dpi, ScreenRect{ leftTop + ScreenCoordsXY{ 2, 0 }, rightTop - ScreenCoordsXY{ 2, 0 } },
FilterPaletteID::PaletteDarken3);
2014-10-05 02:51:17 +01:00
gfx_filter_rect(
dpi, ScreenRect{ rightTop + ScreenCoordsXY{ 1, 1 }, rightTop + ScreenCoordsXY{ 1, 1 } },
FilterPaletteID::PaletteDarken3);
gfx_filter_rect(
dpi, ScreenRect{ rightTop + ScreenCoordsXY{ -1, 1 }, rightTop + ScreenCoordsXY{ -1, 1 } },
FilterPaletteID::PaletteDarken3);
gfx_filter_rect(
dpi, ScreenRect{ leftBottom + ScreenCoordsXY{ 1, -1 }, leftBottom + ScreenCoordsXY{ 1, -1 } },
FilterPaletteID::PaletteDarken3);
gfx_filter_rect(
dpi, ScreenRect{ rightBottom - ScreenCoordsXY{ 1, 1 }, rightBottom - ScreenCoordsXY{ 1, 1 } },
FilterPaletteID::PaletteDarken3);
2014-10-05 02:51:17 +01:00
draw_string_centred_raw(
dpi, { leftTop + ScreenCoordsXY{ (w->width + 1) / 2 - 1, 1 } }, _window_error_num_lines, _window_error_text.data(),
FontStyle::Medium);
2015-12-20 22:03:37 -06:00
}