2014-04-25 18:25:34 +01:00
|
|
|
/*****************************************************************************
|
2024-12-31 20:30:04 +01:00
|
|
|
* Copyright (c) 2014-2025 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>
|
2025-01-19 15:50:54 +01:00
|
|
|
#include <openrct2-ui/windows/Windows.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>
|
2025-09-12 14:03:44 +02:00
|
|
|
#include <openrct2/Input.h>
|
2017-09-06 16:09:35 +02:00
|
|
|
#include <openrct2/OpenRCT2.h>
|
2025-02-17 07:24:03 -05:00
|
|
|
#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>
|
2025-01-19 10:58:28 +01:00
|
|
|
#include <openrct2/ui/WindowManager.h>
|
2016-09-03 14:22:03 +01:00
|
|
|
|
2024-03-10 13:52:35 +00:00
|
|
|
namespace OpenRCT2::Ui::Windows
|
|
|
|
|
{
|
2024-07-28 11:19:59 +02:00
|
|
|
enum
|
|
|
|
|
{
|
|
|
|
|
WIDX_BACKGROUND,
|
|
|
|
|
};
|
2014-10-05 02:51:17 +01:00
|
|
|
|
2024-07-28 11:19:59 +02:00
|
|
|
static constexpr auto kMinWidth = 70;
|
|
|
|
|
static constexpr auto kMaxWidth = 250;
|
|
|
|
|
static constexpr auto kPadding = 4;
|
|
|
|
|
|
2025-01-18 14:45:17 +02:00
|
|
|
static constexpr Widget window_error_widgets[] = {
|
2025-06-10 19:55:12 +02:00
|
|
|
makeWidget({ 0, 0 }, { 200, 42 }, WidgetType::frame, WindowColour::primary),
|
2024-07-28 11:19:59 +02:00
|
|
|
};
|
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
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-17 20:10:06 +02:00
|
|
|
void onOpen() override
|
2022-10-29 05:25:20 -07:00
|
|
|
{
|
2025-09-17 20:10:06 +02:00
|
|
|
setWidgets(window_error_widgets);
|
2025-01-18 14:45:17 +02:00
|
|
|
|
|
|
|
|
widgets[WIDX_BACKGROUND].right = width - 1;
|
|
|
|
|
widgets[WIDX_BACKGROUND].bottom = height - 1;
|
2024-03-10 13:52:35 +00:00
|
|
|
|
|
|
|
|
_staleCount = 0;
|
2024-07-28 11:19:59 +02:00
|
|
|
|
2024-03-10 13:52:35 +00:00
|
|
|
if (!gDisableErrorWindowSound)
|
|
|
|
|
{
|
2025-09-30 22:13:25 +02:00
|
|
|
Audio::Play(Audio::SoundId::error, 0, windowPos.x + (width / 2));
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
2022-10-29 05:25:20 -07:00
|
|
|
}
|
2020-03-03 21:11:26 +00:00
|
|
|
|
2025-09-17 20:10:06 +02:00
|
|
|
void onDraw(RenderTarget& rt) override
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
2025-05-05 01:06:46 +03:00
|
|
|
WindowDrawWidgets(*this, rt);
|
2014-10-05 02:51:17 +01:00
|
|
|
|
2024-07-28 11:19:59 +02:00
|
|
|
auto screenCoords = windowPos + ScreenCoordsXY{ (width + 1) / 2 - 1, kPadding - 1 };
|
2025-10-16 20:20:46 +02:00
|
|
|
DrawStringCentredRaw(rt, screenCoords, _numLines, _text.data(), FontStyle::medium);
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-17 20:10:06 +02:00
|
|
|
void onPeriodicUpdate() override
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
|
|
|
|
// Close the window after 8 seconds of showing
|
|
|
|
|
_staleCount++;
|
|
|
|
|
if (_staleCount >= 8)
|
|
|
|
|
{
|
2025-09-17 20:10:06 +02:00
|
|
|
close();
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-05-10 15:04:12 +02:00
|
|
|
|
2025-09-17 20:10:06 +02:00
|
|
|
void onUpdate() override
|
2024-05-10 15:04:12 +02:00
|
|
|
{
|
|
|
|
|
// Automatically close previous screenshot messages before new screenshot is taken
|
|
|
|
|
if (_autoClose && gScreenshotCountdown > 0)
|
|
|
|
|
{
|
2025-09-17 20:10:06 +02:00
|
|
|
close();
|
2024-05-10 15:04:12 +02:00
|
|
|
}
|
|
|
|
|
}
|
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-07-28 11:19:59 +02:00
|
|
|
std::string buffer = "{WINDOW_COLOUR_1}";
|
2024-03-10 13:52:35 +00:00
|
|
|
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.
|
2025-01-19 18:49:18 +01:00
|
|
|
auto* windowMgr = Ui::GetWindowManager();
|
2025-09-17 23:18:52 +02:00
|
|
|
windowMgr->CloseByClass(WindowClass::error);
|
2024-03-10 13:52:35 +00:00
|
|
|
|
2024-07-28 11:19:59 +02:00
|
|
|
// How wide is the error string?
|
2025-10-16 20:20:46 +02:00
|
|
|
int32_t width = GfxGetStringWidthNewLined(buffer.data(), FontStyle::medium);
|
2024-07-28 11:19:59 +02:00
|
|
|
width = std::clamp(width + 2 * kPadding, kMinWidth, kMaxWidth);
|
2024-03-10 13:52:35 +00:00
|
|
|
|
2024-07-28 11:19:59 +02:00
|
|
|
// How high is the error string?
|
2024-03-10 13:52:35 +00:00
|
|
|
int32_t numLines{};
|
2025-10-16 20:20:46 +02:00
|
|
|
GfxWrapString(buffer, width + 1, FontStyle::medium, &buffer, &numLines);
|
|
|
|
|
int32_t height = (numLines + 1) * FontGetLineHeight(FontStyle::medium) + (2 * kPadding);
|
2024-03-10 13:52:35 +00:00
|
|
|
|
2024-07-28 11:19:59 +02:00
|
|
|
// Position error message around the cursor
|
2024-03-10 13:52:35 +00:00
|
|
|
const CursorState* state = ContextGetCursorState();
|
|
|
|
|
ScreenCoordsXY windowPosition = state->position - ScreenCoordsXY(width / 2, -26);
|
2024-07-28 11:19:59 +02:00
|
|
|
windowPosition.x = std::clamp(windowPosition.x, 0, ContextGetWidth() - width - 40);
|
|
|
|
|
windowPosition.y = std::clamp(windowPosition.y, 22, ContextGetHeight() - height - 40);
|
2024-03-10 13:52:35 +00:00
|
|
|
|
2024-05-10 15:04:12 +02:00
|
|
|
auto errorWindow = std::make_unique<ErrorWindow>(std::move(buffer), numLines, autoClose);
|
2025-01-19 10:58:28 +01:00
|
|
|
|
|
|
|
|
return windowMgr->Create(
|
2025-09-17 23:18:52 +02:00
|
|
|
std::move(errorWindow), WindowClass::error, windowPosition, { width, height },
|
2025-09-20 21:46:42 +02:00
|
|
|
{ WindowFlag::stickToFront, WindowFlag::transparent, WindowFlag::noTitleBar });
|
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
|