2014-04-25 18:25:34 +01:00
|
|
|
/*****************************************************************************
|
2024-01-01 05:52:28 -06:00
|
|
|
* Copyright (c) 2014-2024 OpenRCT2 developers
|
2014-04-25 18:25:34 +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-04-25 18:25:34 +01:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
2014-04-25 18:25:34 +01:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
2024-05-09 13:54:40 +02:00
|
|
|
#include <algorithm>
|
2018-06-22 23:21:44 +02:00
|
|
|
#include <openrct2-ui/interface/Widget.h>
|
|
|
|
|
#include <openrct2-ui/windows/Window.h>
|
2017-09-06 16:09:35 +02:00
|
|
|
#include <openrct2/Context.h>
|
2024-07-09 16:23:31 +02:00
|
|
|
#include <openrct2/Diagnostic.h>
|
2017-09-06 16:09:35 +02:00
|
|
|
#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>
|
2024-05-10 15:04:12 +02:00
|
|
|
#include <openrct2/interface/Screenshot.h>
|
2024-07-13 13:48:49 +02:00
|
|
|
#include <openrct2/localisation/Formatting.h>
|
2016-09-03 14:22:03 +01:00
|
|
|
|
2024-03-10 13:52:35 +00:00
|
|
|
namespace OpenRCT2::Ui::Windows
|
|
|
|
|
{
|
|
|
|
|
// 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
|
|
|
};
|
|
|
|
|
|
2022-12-24 16:50:29 +01:00
|
|
|
static Widget window_error_widgets[] = {
|
2020-11-26 22:14:20 -05:00
|
|
|
MakeWidget({0, 0}, {200, 42}, WindowWidgetType::ImgBtn, WindowColour::Primary),
|
2024-03-09 14:15:45 +00:00
|
|
|
kWidgetsEnd,
|
2014-10-05 02:51:17 +01:00
|
|
|
};
|
2024-03-10 13:52:35 +00:00
|
|
|
// clang-format on
|
2014-10-05 02:51:17 +01:00
|
|
|
|
2024-03-10 13:52:35 +00:00
|
|
|
class ErrorWindow final : public Window
|
2022-10-29 05:25:20 -07:00
|
|
|
{
|
2024-03-10 13:52:35 +00:00
|
|
|
private:
|
|
|
|
|
std::string _text;
|
|
|
|
|
uint16_t _numLines;
|
|
|
|
|
uint8_t _staleCount;
|
2024-05-10 15:04:12 +02:00
|
|
|
bool _autoClose;
|
2022-10-29 05:25:20 -07:00
|
|
|
|
2024-03-10 13:52:35 +00:00
|
|
|
public:
|
2024-05-10 15:04:12 +02:00
|
|
|
ErrorWindow(std::string text, uint16_t numLines, bool autoClose)
|
2024-03-10 13:52:35 +00:00
|
|
|
: _text(std::move(text))
|
|
|
|
|
, _numLines(numLines)
|
2024-05-10 15:04:12 +02:00
|
|
|
, _autoClose(autoClose)
|
2022-10-29 05:25:20 -07:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-10 13:52:35 +00:00
|
|
|
void OnOpen() override
|
2022-10-29 05:25:20 -07:00
|
|
|
{
|
2024-03-10 13:52:35 +00:00
|
|
|
window_error_widgets[WIDX_BACKGROUND].right = width;
|
|
|
|
|
window_error_widgets[WIDX_BACKGROUND].bottom = height;
|
|
|
|
|
|
|
|
|
|
widgets = window_error_widgets;
|
|
|
|
|
_staleCount = 0;
|
|
|
|
|
if (!gDisableErrorWindowSound)
|
|
|
|
|
{
|
|
|
|
|
OpenRCT2::Audio::Play(OpenRCT2::Audio::SoundId::Error, 0, windowPos.x + (width / 2));
|
|
|
|
|
}
|
2022-10-29 05:25:20 -07:00
|
|
|
}
|
2020-03-03 21:11:26 +00:00
|
|
|
|
2024-03-10 13:52:35 +00:00
|
|
|
void OnDraw(DrawPixelInfo& dpi) override
|
|
|
|
|
{
|
|
|
|
|
ScreenCoordsXY leftTop{ windowPos };
|
|
|
|
|
ScreenCoordsXY rightBottom{ windowPos + ScreenCoordsXY{ width - 1, height - 1 } };
|
|
|
|
|
ScreenCoordsXY leftBottom{ leftTop.x, rightBottom.y };
|
|
|
|
|
ScreenCoordsXY rightTop{ rightBottom.x, leftTop.y };
|
2014-10-05 02:51:17 +01:00
|
|
|
|
2024-03-10 13:52:35 +00:00
|
|
|
GfxFilterRect(
|
|
|
|
|
dpi, ScreenRect{ leftTop + ScreenCoordsXY{ 1, 1 }, rightBottom - ScreenCoordsXY{ 1, 1 } },
|
|
|
|
|
FilterPaletteID::Palette45);
|
|
|
|
|
GfxFilterRect(dpi, ScreenRect{ leftTop, rightBottom }, FilterPaletteID::PaletteGlassSaturatedRed);
|
|
|
|
|
|
|
|
|
|
GfxFilterRect(
|
|
|
|
|
dpi, ScreenRect{ leftTop + ScreenCoordsXY{ 0, 2 }, leftBottom - ScreenCoordsXY{ 0, 2 } },
|
|
|
|
|
FilterPaletteID::PaletteDarken3);
|
|
|
|
|
GfxFilterRect(
|
|
|
|
|
dpi, ScreenRect{ rightTop + ScreenCoordsXY{ 0, 2 }, rightBottom - ScreenCoordsXY{ 0, 2 } },
|
|
|
|
|
FilterPaletteID::PaletteDarken3);
|
|
|
|
|
GfxFilterRect(
|
|
|
|
|
dpi, ScreenRect{ leftBottom + ScreenCoordsXY{ 2, 0 }, rightBottom - ScreenCoordsXY{ 2, 0 } },
|
|
|
|
|
FilterPaletteID::PaletteDarken3);
|
|
|
|
|
GfxFilterRect(
|
|
|
|
|
dpi, ScreenRect{ leftTop + ScreenCoordsXY{ 2, 0 }, rightTop - ScreenCoordsXY{ 2, 0 } },
|
|
|
|
|
FilterPaletteID::PaletteDarken3);
|
|
|
|
|
|
|
|
|
|
GfxFilterRect(
|
|
|
|
|
dpi, ScreenRect{ rightTop + ScreenCoordsXY{ 1, 1 }, rightTop + ScreenCoordsXY{ 1, 1 } },
|
|
|
|
|
FilterPaletteID::PaletteDarken3);
|
|
|
|
|
GfxFilterRect(
|
|
|
|
|
dpi, ScreenRect{ rightTop + ScreenCoordsXY{ -1, 1 }, rightTop + ScreenCoordsXY{ -1, 1 } },
|
|
|
|
|
FilterPaletteID::PaletteDarken3);
|
|
|
|
|
GfxFilterRect(
|
|
|
|
|
dpi, ScreenRect{ leftBottom + ScreenCoordsXY{ 1, -1 }, leftBottom + ScreenCoordsXY{ 1, -1 } },
|
|
|
|
|
FilterPaletteID::PaletteDarken3);
|
|
|
|
|
GfxFilterRect(
|
|
|
|
|
dpi, ScreenRect{ rightBottom - ScreenCoordsXY{ 1, 1 }, rightBottom - ScreenCoordsXY{ 1, 1 } },
|
|
|
|
|
FilterPaletteID::PaletteDarken3);
|
|
|
|
|
|
|
|
|
|
DrawStringCentredRaw(
|
|
|
|
|
dpi, { leftTop + ScreenCoordsXY{ (width + 1) / 2 - 1, 1 } }, _numLines, _text.data(), FontStyle::Medium);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OnPeriodicUpdate() override
|
|
|
|
|
{
|
|
|
|
|
// Close the window after 8 seconds of showing
|
|
|
|
|
_staleCount++;
|
|
|
|
|
if (_staleCount >= 8)
|
|
|
|
|
{
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-10 15:04:12 +02:00
|
|
|
|
|
|
|
|
void OnUpdate() override
|
|
|
|
|
{
|
|
|
|
|
// Automatically close previous screenshot messages before new screenshot is taken
|
|
|
|
|
if (_autoClose && gScreenshotCountdown > 0)
|
|
|
|
|
{
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-10 13:52:35 +00:00
|
|
|
};
|
|
|
|
|
|
2024-05-10 15:04:12 +02:00
|
|
|
WindowBase* ErrorOpen(std::string_view title, std::string_view message, bool autoClose)
|
2018-06-22 23:21:44 +02:00
|
|
|
{
|
2024-03-10 13:52:35 +00:00
|
|
|
std::string buffer = "{BLACK}";
|
|
|
|
|
buffer.append(title);
|
|
|
|
|
|
|
|
|
|
// Format the message
|
|
|
|
|
if (!message.empty())
|
|
|
|
|
{
|
|
|
|
|
buffer.push_back('\n');
|
|
|
|
|
buffer.append(message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LOG_VERBOSE("show error, %s", buffer.c_str() + 1);
|
|
|
|
|
|
|
|
|
|
// Don't do unnecessary work in headless. Also saves checking if cursor state is null.
|
|
|
|
|
if (gOpenRCT2Headless)
|
|
|
|
|
{
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if there is any text to display
|
|
|
|
|
if (buffer.size() <= 1)
|
|
|
|
|
{
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close any existing error windows if they exist.
|
|
|
|
|
WindowCloseByClass(WindowClass::Error);
|
|
|
|
|
|
|
|
|
|
int32_t width = GfxGetStringWidthNewLined(buffer.data(), FontStyle::Medium);
|
|
|
|
|
width = std::clamp(width, 64, 196);
|
|
|
|
|
|
|
|
|
|
int32_t numLines{};
|
|
|
|
|
GfxWrapString(buffer, width + 1, FontStyle::Medium, &buffer, &numLines);
|
|
|
|
|
|
|
|
|
|
width = width + 3;
|
|
|
|
|
int32_t height = (numLines + 1) * FontGetLineHeight(FontStyle::Medium) + 4;
|
|
|
|
|
int32_t screenWidth = ContextGetWidth();
|
|
|
|
|
int32_t screenHeight = ContextGetHeight();
|
|
|
|
|
const CursorState* state = ContextGetCursorState();
|
|
|
|
|
ScreenCoordsXY windowPosition = state->position - ScreenCoordsXY(width / 2, -26);
|
|
|
|
|
windowPosition.x = std::clamp(windowPosition.x, 0, screenWidth);
|
|
|
|
|
windowPosition.y = std::max(22, windowPosition.y);
|
|
|
|
|
int32_t maxY = screenHeight - height;
|
|
|
|
|
if (windowPosition.y > maxY)
|
|
|
|
|
{
|
|
|
|
|
windowPosition.y = std::min(windowPosition.y - height - 40, maxY);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-10 15:04:12 +02:00
|
|
|
auto errorWindow = std::make_unique<ErrorWindow>(std::move(buffer), numLines, autoClose);
|
2024-03-10 13:52:35 +00:00
|
|
|
return WindowCreate(
|
|
|
|
|
std::move(errorWindow), WindowClass::Error, windowPosition, width, height,
|
|
|
|
|
WF_STICK_TO_FRONT | WF_TRANSPARENT | WF_RESIZABLE);
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2014-10-05 02:51:17 +01:00
|
|
|
|
2024-05-10 15:04:12 +02:00
|
|
|
WindowBase* ErrorOpen(StringId title, StringId message, const Formatter& args, bool autoClose)
|
2018-06-22 23:21:44 +02:00
|
|
|
{
|
2024-03-10 18:15:49 +00:00
|
|
|
auto titlez = FormatStringIDLegacy(title, args.Data());
|
|
|
|
|
auto messagez = FormatStringIDLegacy(message, args.Data());
|
2024-05-10 15:04:12 +02:00
|
|
|
return ErrorOpen(titlez, messagez, autoClose);
|
2017-06-12 11:52:04 +02:00
|
|
|
}
|
2024-03-10 13:52:35 +00:00
|
|
|
} // namespace OpenRCT2::Ui::Windows
|