2014-10-12 17:15:08 +01:00
|
|
|
/*****************************************************************************
|
2024-12-31 20:30:04 +01:00
|
|
|
* Copyright (c) 2014-2025 OpenRCT2 developers
|
2016-05-04 19:24:41 +02: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
|
2016-05-04 19:24:41 +02:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
2016-05-04 19:24:41 +02:00
|
|
|
*****************************************************************************/
|
2014-10-12 17:15:08 +01:00
|
|
|
|
2024-01-30 22:44:58 +00:00
|
|
|
#include <SDL.h>
|
2022-09-26 01:33:02 +07:00
|
|
|
#include <SDL_keycode.h>
|
2018-11-21 23:16:04 +01:00
|
|
|
#include <iterator>
|
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-10-23 00:36:59 +02:00
|
|
|
#include <openrct2/Context.h>
|
2025-09-12 14:03:44 +02:00
|
|
|
#include <openrct2/Input.h>
|
2017-12-26 15:36:22 +01:00
|
|
|
#include <openrct2/core/String.hpp>
|
2024-07-12 18:21:15 +02:00
|
|
|
#include <openrct2/core/UTF8.h>
|
2018-06-22 23:21:44 +02:00
|
|
|
#include <openrct2/drawing/Drawing.h>
|
2024-07-13 13:48:49 +02:00
|
|
|
#include <openrct2/localisation/Formatting.h>
|
2024-07-12 14:59:00 +02:00
|
|
|
#include <openrct2/localisation/StringIds.h>
|
2025-01-08 21:12:11 +01:00
|
|
|
#include <openrct2/ui/WindowManager.h>
|
2014-10-12 17:15:08 +01:00
|
|
|
|
2024-03-10 13:52:35 +00:00
|
|
|
namespace OpenRCT2::Ui::Windows
|
2021-02-07 21:36:31 +00:00
|
|
|
{
|
2025-06-16 23:10:47 +02:00
|
|
|
static constexpr ScreenSize kWindowSize = { 250, 90 };
|
2014-10-12 17:15:08 +01:00
|
|
|
|
2024-03-10 13:52:35 +00:00
|
|
|
enum WindowTextInputWidgetIdx
|
2021-02-07 21:36:31 +00:00
|
|
|
{
|
2024-03-10 13:52:35 +00:00
|
|
|
WIDX_BACKGROUND,
|
|
|
|
|
WIDX_TITLE,
|
|
|
|
|
WIDX_CLOSE,
|
|
|
|
|
WIDX_CANCEL,
|
|
|
|
|
WIDX_OKAY
|
|
|
|
|
};
|
2021-02-07 21:36:31 +00:00
|
|
|
|
2025-06-07 23:02:14 +02:00
|
|
|
static constexpr auto _textInputWidgets = makeWidgets(
|
2025-06-16 23:10:47 +02:00
|
|
|
makeWindowShim(kStringIdNone, kWindowSize),
|
2025-06-10 19:55:12 +02:00
|
|
|
makeWidget({ 170, 68 }, { 71, 14 }, WidgetType::button, WindowColour::secondary, STR_CANCEL),
|
|
|
|
|
makeWidget({ 10, 68 }, { 71, 14 }, WidgetType::button, WindowColour::secondary, STR_OK));
|
2024-03-10 13:52:35 +00:00
|
|
|
|
|
|
|
|
class TextInputWindow final : public Window
|
2021-02-07 21:36:31 +00:00
|
|
|
{
|
2024-03-10 13:52:35 +00:00
|
|
|
private:
|
|
|
|
|
WidgetIdentifier _parentWidget{};
|
|
|
|
|
|
|
|
|
|
std::string _title;
|
2025-01-19 16:59:45 +01:00
|
|
|
StringId _titleStringId = kStringIdNone;
|
2024-03-10 13:52:35 +00:00
|
|
|
|
|
|
|
|
std::string _description;
|
2025-01-19 16:59:45 +01:00
|
|
|
StringId _descriptionStringId = kStringIdNone;
|
2024-03-10 13:52:35 +00:00
|
|
|
Formatter _descriptionArgs;
|
|
|
|
|
|
|
|
|
|
std::function<void(std::string_view)> _callback;
|
|
|
|
|
std::function<void()> _cancelCallback;
|
|
|
|
|
|
|
|
|
|
int32_t _cursorBlink{};
|
|
|
|
|
size_t _maxInputLength{};
|
|
|
|
|
u8string _buffer;
|
|
|
|
|
|
|
|
|
|
public:
|
2025-09-17 20:10:06 +02:00
|
|
|
void onOpen() override
|
2021-02-07 21:36:31 +00:00
|
|
|
{
|
2025-09-17 20:10:06 +02:00
|
|
|
setWidgets(_textInputWidgets);
|
2024-03-10 13:52:35 +00:00
|
|
|
WindowInitScrollWidgets(*this);
|
2025-09-17 20:10:06 +02:00
|
|
|
setParentWindow(nullptr, 0);
|
2021-02-07 21:36:31 +00:00
|
|
|
}
|
2024-03-10 13:52:35 +00:00
|
|
|
|
2025-09-17 20:10:06 +02:00
|
|
|
void setParentWindow(WindowBase* parentWindow, WidgetIndex widgetIndex)
|
2021-02-07 21:36:31 +00:00
|
|
|
{
|
2024-03-10 13:52:35 +00:00
|
|
|
// Save calling window details so that the information can be passed back to the correct window & widget
|
2021-02-07 21:36:31 +00:00
|
|
|
if (parentWindow == nullptr)
|
|
|
|
|
{
|
2024-03-10 13:52:35 +00:00
|
|
|
_parentWidget.window.classification = WindowClass::Null;
|
|
|
|
|
_parentWidget.window.number = 0;
|
2025-09-17 18:30:48 +02:00
|
|
|
_parentWidget.widgetIndex = 0;
|
2024-03-10 13:52:35 +00:00
|
|
|
|
|
|
|
|
colours[0] = COLOUR_GREY;
|
|
|
|
|
colours[1] = COLOUR_GREY;
|
|
|
|
|
colours[2] = COLOUR_GREY;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_parentWidget.window.classification = parentWindow->classification;
|
|
|
|
|
_parentWidget.window.number = parentWindow->number;
|
2025-09-17 18:30:48 +02:00
|
|
|
_parentWidget.widgetIndex = widgetIndex;
|
2024-03-10 13:52:35 +00:00
|
|
|
|
|
|
|
|
colours[0] = parentWindow->colours[1];
|
|
|
|
|
colours[1] = parentWindow->colours[1];
|
|
|
|
|
colours[2] = parentWindow->colours[1];
|
2021-02-07 21:36:31 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-17 20:10:06 +02:00
|
|
|
void setTitle(StringId title, StringId description, const Formatter& decriptionArgs)
|
2021-02-07 21:36:31 +00:00
|
|
|
{
|
2024-03-10 13:52:35 +00:00
|
|
|
_titleStringId = title;
|
|
|
|
|
_descriptionStringId = description;
|
|
|
|
|
_descriptionArgs = decriptionArgs;
|
2021-02-07 21:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-17 20:10:06 +02:00
|
|
|
void setTitle(std::string_view title, std::string_view description)
|
2021-02-07 21:36:31 +00:00
|
|
|
{
|
2025-01-19 16:59:45 +01:00
|
|
|
_titleStringId = kStringIdNone;
|
2024-03-10 13:52:35 +00:00
|
|
|
_title = title;
|
2025-01-19 16:59:45 +01:00
|
|
|
_descriptionStringId = kStringIdNone;
|
2024-03-10 13:52:35 +00:00
|
|
|
_description = description;
|
2021-02-07 21:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-17 20:10:06 +02:00
|
|
|
void setText(std::string_view text, size_t maxLength)
|
2021-02-07 21:36:31 +00:00
|
|
|
{
|
2024-12-11 22:28:31 +01:00
|
|
|
text = String::utf8TruncateCodePoints(text, maxLength);
|
2024-03-10 13:52:35 +00:00
|
|
|
_buffer = u8string{ text };
|
|
|
|
|
_maxInputLength = maxLength;
|
2024-03-22 08:43:42 +00:00
|
|
|
SetTexboxSession(ContextStartTextInput(_buffer, maxLength));
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
2021-02-07 21:36:31 +00:00
|
|
|
|
2025-09-17 20:10:06 +02:00
|
|
|
void setCallback(std::function<void(std::string_view)> callback, std::function<void()> cancelCallback)
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
|
|
|
|
_callback = callback;
|
|
|
|
|
_cancelCallback = cancelCallback;
|
|
|
|
|
}
|
2021-02-07 21:36:31 +00:00
|
|
|
|
2025-09-17 20:10:06 +02:00
|
|
|
void onClose() override
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
|
|
|
|
// Make sure that we take it out of the text input
|
|
|
|
|
// mode otherwise problems may occur.
|
|
|
|
|
ContextStopTextInput();
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-17 20:10:06 +02:00
|
|
|
void onUpdate() override
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
|
|
|
|
if (HasParentWindow())
|
2021-02-07 21:36:31 +00:00
|
|
|
{
|
2024-03-10 13:52:35 +00:00
|
|
|
// If the calling window is closed then close the text input window
|
|
|
|
|
auto parentWindow = GetParentWindow();
|
|
|
|
|
if (parentWindow == nullptr)
|
2021-02-07 21:36:31 +00:00
|
|
|
{
|
2025-09-17 20:10:06 +02:00
|
|
|
close();
|
2024-03-10 13:52:35 +00:00
|
|
|
return;
|
2024-03-03 18:04:09 +01:00
|
|
|
}
|
2021-02-07 21:36:31 +00:00
|
|
|
}
|
2024-03-10 13:52:35 +00:00
|
|
|
|
|
|
|
|
// Used to blink the cursor.
|
|
|
|
|
_cursorBlink++;
|
|
|
|
|
if (_cursorBlink > 30)
|
|
|
|
|
_cursorBlink = 0;
|
|
|
|
|
|
2025-09-17 20:10:06 +02:00
|
|
|
invalidate();
|
2021-02-07 21:36:31 +00:00
|
|
|
}
|
2024-03-10 13:52:35 +00:00
|
|
|
|
2025-09-17 20:10:06 +02:00
|
|
|
void onMouseUp(WidgetIndex widgetIndex) override
|
2021-02-07 21:36:31 +00:00
|
|
|
{
|
2024-03-10 13:52:35 +00:00
|
|
|
switch (widgetIndex)
|
2021-02-07 21:36:31 +00:00
|
|
|
{
|
2024-03-10 13:52:35 +00:00
|
|
|
case WIDX_CANCEL:
|
|
|
|
|
case WIDX_CLOSE:
|
|
|
|
|
ContextStopTextInput();
|
|
|
|
|
ExecuteCallback(false);
|
2025-09-17 20:10:06 +02:00
|
|
|
close();
|
2024-03-10 13:52:35 +00:00
|
|
|
break;
|
|
|
|
|
case WIDX_OKAY:
|
|
|
|
|
ContextStopTextInput();
|
|
|
|
|
ExecuteCallback(true);
|
2025-09-17 20:10:06 +02:00
|
|
|
close();
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-17 20:10:06 +02:00
|
|
|
void onPrepareDraw() override
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
|
|
|
|
// Change window size if required.
|
|
|
|
|
int32_t newHeight = CalculateWindowHeight(_buffer.data());
|
|
|
|
|
if (newHeight != height)
|
|
|
|
|
{
|
2025-06-16 23:10:47 +02:00
|
|
|
WindowSetResize(*this, { kWindowSize.width, newHeight }, { kWindowSize.width, newHeight });
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
widgets[WIDX_OKAY].top = newHeight - 22;
|
|
|
|
|
widgets[WIDX_OKAY].bottom = newHeight - 9;
|
|
|
|
|
widgets[WIDX_CANCEL].top = newHeight - 22;
|
|
|
|
|
widgets[WIDX_CANCEL].bottom = newHeight - 9;
|
|
|
|
|
widgets[WIDX_BACKGROUND].bottom = newHeight - 1;
|
|
|
|
|
|
|
|
|
|
// Set window title argument
|
2025-01-19 16:59:45 +01:00
|
|
|
if (_titleStringId == kStringIdNone)
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
|
|
|
|
auto ft = Formatter::Common();
|
|
|
|
|
ft.Add<const char*>(_title.c_str());
|
|
|
|
|
widgets[WIDX_TITLE].text = STR_STRING;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
widgets[WIDX_TITLE].text = _titleStringId;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-17 20:10:06 +02:00
|
|
|
void onDraw(RenderTarget& rt) override
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
2025-09-17 20:10:06 +02:00
|
|
|
drawWidgets(rt);
|
2024-03-10 13:52:35 +00:00
|
|
|
|
2025-06-16 23:10:47 +02:00
|
|
|
auto screenCoords = windowPos + ScreenCoordsXY{ kWindowSize.width / 2, widgets[WIDX_TITLE].bottom + 13 };
|
2024-03-10 13:52:35 +00:00
|
|
|
|
|
|
|
|
int32_t no_lines = 0;
|
|
|
|
|
|
2025-01-19 16:59:45 +01:00
|
|
|
if (_descriptionStringId == kStringIdNone)
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
|
|
|
|
auto ft = Formatter();
|
|
|
|
|
ft.Add<const char*>(_description.c_str());
|
2025-06-16 23:10:47 +02:00
|
|
|
DrawTextWrapped(rt, screenCoords, kWindowSize.width, STR_STRING, ft, { colours[1], TextAlignment::CENTRE });
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
DrawTextWrapped(
|
2025-06-16 23:10:47 +02:00
|
|
|
rt, screenCoords, kWindowSize.width, _descriptionStringId, _descriptionArgs,
|
|
|
|
|
{ colours[1], TextAlignment::CENTRE });
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
screenCoords.y += 25;
|
|
|
|
|
|
|
|
|
|
// String length needs to add 12 either side of box
|
|
|
|
|
// +13 for cursor when max length.
|
|
|
|
|
u8string wrappedString;
|
|
|
|
|
GfxWrapString(
|
2025-06-16 23:10:47 +02:00
|
|
|
u8string_view{ _buffer.data(), _buffer.size() }, kWindowSize.width - (24 + 13), FontStyle::Medium,
|
|
|
|
|
&wrappedString, &no_lines);
|
2024-03-10 13:52:35 +00:00
|
|
|
|
|
|
|
|
GfxFillRectInset(
|
2025-05-05 01:06:46 +03:00
|
|
|
rt,
|
2025-06-16 23:10:47 +02:00
|
|
|
{ { windowPos.x + 10, screenCoords.y },
|
|
|
|
|
{ windowPos.x + kWindowSize.width - 10, screenCoords.y + 10 * (no_lines + 1) + 3 } },
|
2024-03-10 13:52:35 +00:00
|
|
|
colours[1], INSET_RECT_F_60);
|
|
|
|
|
|
|
|
|
|
screenCoords.y += 1;
|
|
|
|
|
|
|
|
|
|
const utf8* wrapPointer = wrappedString.data();
|
|
|
|
|
size_t char_count = 0;
|
|
|
|
|
uint8_t cur_drawn = 0;
|
|
|
|
|
|
2024-03-22 08:43:42 +00:00
|
|
|
auto* textInput = GetTextboxSession();
|
2024-03-10 13:52:35 +00:00
|
|
|
int32_t cursorX = 0;
|
|
|
|
|
int32_t cursorY = 0;
|
|
|
|
|
for (int32_t line = 0; line <= no_lines; line++)
|
|
|
|
|
{
|
|
|
|
|
screenCoords.x = windowPos.x + 12;
|
2025-05-05 01:06:46 +03:00
|
|
|
DrawText(rt, screenCoords, { colours[1], FontStyle::Medium, TextAlignment::LEFT }, wrapPointer, true);
|
2024-03-10 13:52:35 +00:00
|
|
|
|
|
|
|
|
size_t string_length = GetStringSize(wrapPointer) - 1;
|
2024-03-22 08:43:42 +00:00
|
|
|
if (!cur_drawn && (textInput->SelectionStart <= char_count + string_length))
|
2021-02-07 21:36:31 +00:00
|
|
|
{
|
2024-03-10 13:52:35 +00:00
|
|
|
// Make a view of the string for measuring the width.
|
|
|
|
|
cursorX = windowPos.x + 13
|
|
|
|
|
+ GfxGetStringWidthNoFormatting(
|
2024-03-22 08:58:43 +00:00
|
|
|
u8string_view{ wrapPointer, textInput->SelectionStart - char_count }, FontStyle::Medium);
|
2024-03-10 13:52:35 +00:00
|
|
|
cursorY = screenCoords.y;
|
|
|
|
|
|
|
|
|
|
int32_t textWidth = 6;
|
2024-03-22 08:43:42 +00:00
|
|
|
if (textInput->SelectionStart < strlen(_buffer.data()))
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
|
|
|
|
// Make a 1 utf8-character wide string for measuring the width
|
|
|
|
|
// of the currently selected character.
|
|
|
|
|
utf8 tmp[5] = {}; // This is easier than setting temp_string[0..5]
|
2024-03-22 08:43:42 +00:00
|
|
|
uint32_t codepoint = UTF8GetNext(_buffer.data() + textInput->SelectionStart, nullptr);
|
2024-03-10 13:52:35 +00:00
|
|
|
UTF8WriteCodepoint(tmp, codepoint);
|
|
|
|
|
textWidth = std::max(GfxGetStringWidthNoFormatting(tmp, FontStyle::Medium) - 2, 4);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_cursorBlink > 15)
|
|
|
|
|
{
|
2024-05-09 02:45:07 +02:00
|
|
|
uint8_t colour = ColourMapA[colours[1].colour].mid_light;
|
2024-03-10 13:52:35 +00:00
|
|
|
// TODO: palette index addition
|
|
|
|
|
GfxFillRect(
|
2025-05-05 01:06:46 +03:00
|
|
|
rt, { { cursorX, screenCoords.y + 9 }, { cursorX + textWidth, screenCoords.y + 9 } }, colour + 5);
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cur_drawn++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wrapPointer += string_length + 1;
|
|
|
|
|
|
|
|
|
|
if (_buffer[char_count + string_length] == ' ')
|
|
|
|
|
char_count++;
|
|
|
|
|
char_count += string_length;
|
|
|
|
|
|
|
|
|
|
screenCoords.y += 10;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!cur_drawn)
|
|
|
|
|
{
|
2025-05-05 01:06:46 +03:00
|
|
|
cursorX = rt.lastStringPos.x;
|
2024-03-10 13:52:35 +00:00
|
|
|
cursorY = screenCoords.y - 10;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IME composition
|
2024-12-11 22:28:31 +01:00
|
|
|
if (!String::isNullOrEmpty(textInput->ImeBuffer))
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
|
|
|
|
IMEComposition(cursorX, cursorY);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-17 20:10:06 +02:00
|
|
|
void onReturnPressed()
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
|
|
|
|
ContextStopTextInput();
|
|
|
|
|
ExecuteCallback(true);
|
2025-09-17 20:10:06 +02:00
|
|
|
close();
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
2025-04-11 23:26:34 +02:00
|
|
|
int32_t CalculateWindowHeight(std::string_view text)
|
2024-03-10 13:52:35 +00:00
|
|
|
{
|
|
|
|
|
// String length needs to add 12 either side of box +13 for cursor when max length.
|
|
|
|
|
int32_t numLines{};
|
2025-06-16 23:10:47 +02:00
|
|
|
GfxWrapString(text, kWindowSize.width - (24 + 13), FontStyle::Medium, nullptr, &numLines);
|
2024-03-10 13:52:35 +00:00
|
|
|
|
2025-04-11 20:37:54 +02:00
|
|
|
const auto textHeight = numLines * 10;
|
2025-06-16 23:10:47 +02:00
|
|
|
return kWindowSize.height + textHeight + getTitleBarDiffNormal();
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
static void IMEComposition(int32_t cursorX, int32_t cursorY)
|
|
|
|
|
{
|
|
|
|
|
SDL_Rect rect = { cursorX, cursorY, 100, 100 };
|
|
|
|
|
SDL_SetTextInputRect(&rect);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ExecuteCallback(bool hasValue)
|
|
|
|
|
{
|
|
|
|
|
if (HasParentWindow())
|
|
|
|
|
{
|
|
|
|
|
if (hasValue)
|
|
|
|
|
{
|
|
|
|
|
auto w = GetParentWindow();
|
|
|
|
|
if (w != nullptr)
|
|
|
|
|
{
|
2025-09-17 20:10:06 +02:00
|
|
|
w->onTextInput(_parentWidget.widgetIndex, _buffer);
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
2021-02-07 21:36:31 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-03-10 13:52:35 +00:00
|
|
|
if (hasValue)
|
2021-02-07 21:36:31 +00:00
|
|
|
{
|
2024-03-10 13:52:35 +00:00
|
|
|
if (_callback)
|
|
|
|
|
{
|
|
|
|
|
_callback(_buffer.data());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (_cancelCallback)
|
|
|
|
|
{
|
|
|
|
|
_cancelCallback();
|
|
|
|
|
}
|
2021-02-07 21:36:31 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-10 13:52:35 +00:00
|
|
|
bool HasParentWindow() const
|
2020-05-09 14:18:50 +01:00
|
|
|
{
|
2024-03-10 13:52:35 +00:00
|
|
|
return _parentWidget.window.classification != WindowClass::Null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WindowBase* GetParentWindow() const
|
|
|
|
|
{
|
2025-01-19 10:58:28 +01:00
|
|
|
auto* windowMgr = GetWindowManager();
|
2025-01-08 21:12:11 +01:00
|
|
|
return HasParentWindow() ? windowMgr->FindByNumber(_parentWidget.window.classification, _parentWidget.window.number)
|
2024-03-10 13:52:35 +00:00
|
|
|
: nullptr;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void WindowTextInputRawOpen(
|
|
|
|
|
WindowBase* call_w, WidgetIndex call_widget, StringId title, StringId description, const Formatter& descriptionArgs,
|
|
|
|
|
const_utf8string existing_text, int32_t maxLength)
|
|
|
|
|
{
|
2025-01-19 10:58:28 +01:00
|
|
|
auto* windowMgr = GetWindowManager();
|
2025-01-19 18:49:18 +01:00
|
|
|
windowMgr->CloseByClass(WindowClass::Textinput);
|
2024-03-10 13:52:35 +00:00
|
|
|
|
2025-06-10 22:20:27 +02:00
|
|
|
auto w = windowMgr->Create<TextInputWindow>(
|
2025-06-16 23:10:47 +02:00
|
|
|
WindowClass::Textinput, { kWindowSize.width, kWindowSize.height + 10 }, WF_CENTRE_SCREEN | WF_STICK_TO_FRONT);
|
2024-03-10 13:52:35 +00:00
|
|
|
if (w != nullptr)
|
|
|
|
|
{
|
2025-09-17 20:10:06 +02:00
|
|
|
w->setParentWindow(call_w, call_widget);
|
|
|
|
|
w->setTitle(title, description, descriptionArgs);
|
|
|
|
|
w->setText(existing_text, maxLength);
|
2020-05-09 14:18:50 +01:00
|
|
|
}
|
2017-06-06 23:24:18 +02:00
|
|
|
}
|
2021-05-24 01:11:04 +03:00
|
|
|
|
2024-03-10 13:52:35 +00:00
|
|
|
void WindowTextInputOpen(
|
|
|
|
|
std::string_view title, std::string_view description, std::string_view initialValue, size_t maxLength,
|
|
|
|
|
std::function<void(std::string_view)> callback, std::function<void()> cancelCallback)
|
|
|
|
|
{
|
2025-01-19 10:58:28 +01:00
|
|
|
auto* windowMgr = GetWindowManager();
|
2025-06-10 22:20:27 +02:00
|
|
|
auto w = windowMgr->Create<TextInputWindow>(
|
2025-06-16 23:10:47 +02:00
|
|
|
WindowClass::Textinput, { kWindowSize.width, kWindowSize.height + 10 }, WF_CENTRE_SCREEN | WF_STICK_TO_FRONT);
|
2024-03-10 13:52:35 +00:00
|
|
|
if (w != nullptr)
|
|
|
|
|
{
|
2025-09-17 20:10:06 +02:00
|
|
|
w->setTitle(title, description);
|
|
|
|
|
w->setText(initialValue, maxLength);
|
|
|
|
|
w->setCallback(callback, cancelCallback);
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WindowTextInputOpen(
|
|
|
|
|
WindowBase* call_w, WidgetIndex call_widget, StringId title, StringId description, const Formatter& descriptionArgs,
|
|
|
|
|
StringId existing_text, uintptr_t existing_args, int32_t maxLength)
|
|
|
|
|
{
|
2024-03-10 18:15:49 +00:00
|
|
|
auto existingText = FormatStringIDLegacy(existing_text, &existing_args);
|
2024-03-10 13:52:35 +00:00
|
|
|
WindowTextInputRawOpen(call_w, call_widget, title, description, descriptionArgs, existingText.c_str(), maxLength);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WindowTextInputKey(WindowBase* w, uint32_t keycode)
|
|
|
|
|
{
|
|
|
|
|
const auto wndNumber = w->number;
|
|
|
|
|
const auto wndClass = w->classification;
|
|
|
|
|
|
|
|
|
|
// If the return button is pressed stop text input
|
|
|
|
|
if (keycode == SDLK_RETURN || keycode == SDLK_KP_ENTER)
|
|
|
|
|
{
|
|
|
|
|
if (w->classification == WindowClass::Textinput)
|
|
|
|
|
{
|
|
|
|
|
auto textInputWindow = static_cast<TextInputWindow*>(w);
|
2025-09-17 20:10:06 +02:00
|
|
|
textInputWindow->onReturnPressed();
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The window can be potentially closed within a callback, we need to check if its still alive.
|
2025-01-19 10:58:28 +01:00
|
|
|
auto* windowMgr = GetWindowManager();
|
2025-01-08 21:12:11 +01:00
|
|
|
w = windowMgr->FindByNumber(wndClass, wndNumber);
|
2024-03-10 13:52:35 +00:00
|
|
|
if (w != nullptr)
|
2025-09-17 20:10:06 +02:00
|
|
|
w->invalidate();
|
2024-03-10 13:52:35 +00:00
|
|
|
}
|
|
|
|
|
} // namespace OpenRCT2::Ui::Windows
|