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

417 lines
13 KiB
C++
Raw Normal View History

2014-10-12 17:15:08 +01:00
/*****************************************************************************
2020-05-09 14:18:50 +01:00
* Copyright (c) 2014-2020 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.
*****************************************************************************/
2014-10-12 17:15:08 +01:00
/**
* Text Input Window
*
2015-10-20 20:16:30 +02:00
* This is a new window created to replace the windows dialog box
* that is used for inputing new text for ride names and peep names.
*/
2018-08-12 12:50:40 +01:00
#include <algorithm>
2018-11-21 23:16:04 +01:00
#include <iterator>
2018-06-22 23:21:44 +02:00
#include <openrct2-ui/interface/Widget.h>
2017-10-23 00:36:59 +02:00
#include <openrct2-ui/windows/Window.h>
#include <openrct2/Context.h>
2018-06-22 23:21:44 +02:00
#include <openrct2/config/Config.h>
2017-12-26 15:36:22 +01:00
#include <openrct2/core/String.hpp>
2018-06-22 23:21:44 +02:00
#include <openrct2/drawing/Drawing.h>
2018-01-06 18:32:25 +01:00
#include <openrct2/localisation/Localisation.h>
2017-12-13 13:02:24 +01:00
#include <openrct2/util/Util.h>
2014-10-12 17:15:08 +01:00
2020-05-05 22:26:14 +02:00
static constexpr const rct_string_id WINDOW_TITLE = STR_OPTIONS;
2020-05-09 17:05:01 +02:00
static constexpr const int32_t WW = 250;
static constexpr const int32_t WH = 90;
2014-10-12 17:15:08 +01:00
// clang-format off
2014-10-12 17:15:08 +01:00
enum WINDOW_TEXT_INPUT_WIDGET_IDX {
2017-06-06 23:24:18 +02:00
WIDX_BACKGROUND,
WIDX_TITLE,
WIDX_CLOSE,
WIDX_CANCEL,
WIDX_OKAY
2014-10-12 17:15:08 +01:00
};
// 0x9DE4E0
static rct_widget window_text_input_widgets[] = {
2020-05-09 16:44:21 +02:00
WINDOW_SHIM(WINDOW_TITLE, WW, WH),
MakeWidget({170, 68}, {71, 14}, WWT_BUTTON, WindowColour::Secondary, STR_CANCEL),
MakeWidget({ 10, 68}, {71, 14}, WWT_BUTTON, WindowColour::Secondary, STR_OK ),
{ WIDGETS_END }
2014-10-12 17:15:08 +01:00
};
static void window_text_input_close(rct_window *w);
2017-05-01 15:41:45 +02:00
static void window_text_input_mouseup(rct_window *w, rct_widgetindex widgetIndex);
2019-02-07 17:46:52 +01:00
static void window_text_input_periodic_update(rct_window *w);
static void window_text_input_invalidate(rct_window *w);
static void window_text_input_paint(rct_window *w, rct_drawpixelinfo *dpi);
2017-01-12 18:20:22 +00:00
static void draw_ime_composition(rct_drawpixelinfo * dpi, int cursorX, int cursorY);
2014-10-12 17:15:08 +01:00
//0x9A3F7C
static rct_window_event_list window_text_input_events([](auto& events)
{
events.close = &window_text_input_close;
events.mouse_up = &window_text_input_mouseup;
events.periodic_update = &window_text_input_periodic_update;
events.invalidate = &window_text_input_invalidate;
events.paint = &window_text_input_paint;
});
// clang-format on
2014-10-12 17:15:08 +01:00
2017-09-06 20:53:30 +02:00
static rct_string_id input_text_description;
2017-12-26 15:36:22 +01:00
static utf8 text_input[TEXT_INPUT_SIZE] = { 0 };
2017-09-06 20:53:30 +02:00
static rct_windowclass calling_class = 0;
static rct_windownumber calling_number = 0;
static int32_t calling_widget = 0;
static int32_t _maxInputLength;
2014-12-10 23:07:11 +00:00
2020-05-09 14:18:50 +01:00
static std::string _title;
static std::string _description;
static std::function<void(const std::string_view&)> _callback;
static std::function<void()> _cancelCallback;
2018-06-22 23:21:44 +02:00
void window_text_input_open(
2018-07-21 13:51:54 +02:00
rct_window* call_w, rct_widgetindex call_widget, rct_string_id title, rct_string_id description,
rct_string_id existing_text, uintptr_t existing_args, int32_t maxLength)
2017-12-26 15:36:22 +01:00
{
// Get the raw string
2018-11-21 23:16:04 +01:00
utf8 buffer[std::size(text_input)]{};
2017-12-26 15:36:22 +01:00
if (existing_text != STR_NONE)
format_string(buffer, maxLength, existing_text, &existing_args);
utf8_remove_format_codes(buffer, false);
window_text_input_raw_open(call_w, call_widget, title, description, buffer, maxLength);
}
2018-06-22 23:21:44 +02:00
void window_text_input_raw_open(
2018-07-21 13:51:54 +02:00
rct_window* call_w, rct_widgetindex call_widget, rct_string_id title, rct_string_id description,
const_utf8string existing_text, int32_t maxLength)
2014-12-10 23:07:11 +00:00
{
2017-06-06 23:24:18 +02:00
_maxInputLength = maxLength;
2014-10-13 21:14:03 +01:00
2017-06-06 23:24:18 +02:00
window_close_by_class(WC_TEXTINPUT);
2014-10-12 17:15:08 +01:00
2017-12-26 15:36:22 +01:00
// Set the input text
if (existing_text != nullptr)
String::Set(text_input, sizeof(text_input), existing_text);
else
String::Set(text_input, sizeof(text_input), "");
2015-07-29 14:36:21 +01:00
2017-06-06 23:24:18 +02:00
// This is the text displayed above the input box
input_text_description = description;
2017-06-06 23:24:18 +02:00
// Work out the existing size of the window
char wrapped_string[TEXT_INPUT_SIZE];
safe_strcpy(wrapped_string, text_input, TEXT_INPUT_SIZE);
int32_t no_lines = 0, font_height = 0;
2017-12-26 15:36:22 +01:00
// String length needs to add 12 either side of box +13 for cursor when max length.
2017-06-06 23:24:18 +02:00
gfx_wrap_string(wrapped_string, WW - (24 + 13), &no_lines, &font_height);
int32_t height = no_lines * 10 + WH;
2017-07-01 00:11:28 +02:00
// Window will be in the centre of the screen
rct_window* w = WindowCreateCentred(WW, height, &window_text_input_events, WC_TEXTINPUT, WF_STICK_TO_FRONT);
2014-10-15 22:53:12 +01:00
2017-06-06 23:24:18 +02:00
w->widgets = window_text_input_widgets;
2017-12-26 15:36:22 +01:00
w->enabled_widgets = (1ULL << WIDX_CLOSE) | (1ULL << WIDX_CANCEL) | (1ULL << WIDX_OKAY);
2017-06-06 23:24:18 +02:00
window_text_input_widgets[WIDX_TITLE].text = title;
2017-12-26 15:36:22 +01:00
// Save calling window details so that the information can be passed back to the correct window & widget
2020-05-09 14:18:50 +01:00
if (call_w == nullptr)
{
calling_class = WC_NULL;
calling_number = 0;
calling_widget = 0;
}
else
{
calling_class = call_w->classification;
calling_number = call_w->number;
calling_widget = call_widget;
}
2017-06-06 23:24:18 +02:00
gTextInput = context_start_text_input(text_input, maxLength);
WindowInitScrollWidgets(w);
2020-05-09 14:18:50 +01:00
if (call_w == nullptr)
{
w->colours[0] = COLOUR_GREY;
w->colours[1] = COLOUR_GREY;
w->colours[2] = COLOUR_GREY;
}
else
{
2020-05-13 00:31:24 +02:00
w->colours[0] = call_w->colours[1];
2020-05-09 14:18:50 +01:00
w->colours[1] = call_w->colours[1];
2020-05-13 00:31:24 +02:00
w->colours[2] = call_w->colours[1];
2020-05-09 14:18:50 +01:00
}
}
void window_text_input_open(
const std::string_view& title, const std::string_view& description, const std::string_view& initialValue, size_t maxLength,
std::function<void(const std::string_view&)> callback, std::function<void()> cancelCallback)
{
_title = title;
_description = description;
_callback = callback;
_cancelCallback = cancelCallback;
std::string szInitialValue(initialValue);
auto szDescription = _description.c_str();
std::memcpy(TextInputDescriptionArgs, &szDescription, sizeof(const char*));
maxLength = std::min(sizeof(text_input) - 1, maxLength);
window_text_input_raw_open(nullptr, 0, STR_STRING, STR_STRING, szInitialValue.c_str(), static_cast<int32_t>(maxLength));
}
static void window_text_input_execute_callback(bool hasValue)
{
if (calling_class == WC_NULL)
{
if (hasValue)
{
if (_callback)
{
_callback(text_input);
}
}
else
{
if (_cancelCallback)
{
_cancelCallback();
}
}
}
else
{
auto calling_w = window_find_by_number(calling_class, calling_number);
if (calling_w != nullptr)
{
auto value = hasValue ? text_input : nullptr;
window_event_textinput_call(calling_w, calling_widget, value);
}
}
2014-10-12 17:15:08 +01:00
}
/**
2018-06-22 23:21:44 +02:00
*
*/
static void window_text_input_mouseup(rct_window* w, rct_widgetindex widgetIndex)
{
2018-06-22 23:21:44 +02:00
switch (widgetIndex)
{
case WIDX_CANCEL:
case WIDX_CLOSE:
context_stop_text_input();
2020-05-09 14:18:50 +01:00
window_text_input_execute_callback(false);
2018-06-22 23:21:44 +02:00
window_close(w);
break;
case WIDX_OKAY:
context_stop_text_input();
2020-05-09 14:18:50 +01:00
window_text_input_execute_callback(true);
2018-06-22 23:21:44 +02:00
window_close(w);
2017-06-06 23:24:18 +02:00
}
2014-10-12 17:15:08 +01:00
}
2014-10-13 21:14:03 +01:00
2014-10-12 17:15:08 +01:00
/**
2018-06-22 23:21:44 +02:00
*
*/
static void window_text_input_paint(rct_window* w, rct_drawpixelinfo* dpi)
{
WindowDrawWidgets(w, dpi);
2014-10-12 17:15:08 +01:00
ScreenCoordsXY screenCoords;
screenCoords.y = w->windowPos.y + 25;
2015-10-20 20:16:30 +02:00
int32_t no_lines = 0;
int32_t font_height = 0;
2015-10-20 20:16:30 +02:00
gfx_draw_string_centred_wrapped(
dpi, &TextInputDescriptionArgs, { w->windowPos.x + WW / 2, screenCoords.y }, WW, input_text_description, w->colours[1]);
screenCoords.y += 25;
2017-06-06 23:24:18 +02:00
gCurrentFontSpriteBase = FONT_SPRITE_BASE_MEDIUM;
gCurrentFontFlags = 0;
2015-06-06 21:18:20 -04:00
2017-06-06 23:24:18 +02:00
char wrapped_string[TEXT_INPUT_SIZE];
safe_strcpy(wrapped_string, text_input, TEXT_INPUT_SIZE);
2014-10-15 22:53:12 +01:00
2017-06-06 23:24:18 +02:00
// String length needs to add 12 either side of box
// +13 for cursor when max length.
gfx_wrap_string(wrapped_string, WW - (24 + 13), &no_lines, &font_height);
2020-03-01 16:32:35 -03:00
gfx_fill_rect_inset(
dpi,
{ { w->windowPos.x + 10, screenCoords.y }, { w->windowPos.x + WW - 10, screenCoords.y + 10 * (no_lines + 1) + 3 } },
w->colours[1], INSET_RECT_F_60);
screenCoords.y += 1;
2017-06-06 23:24:18 +02:00
char* wrap_pointer = wrapped_string;
size_t char_count = 0;
uint8_t cur_drawn = 0;
2014-12-22 10:40:00 +00:00
int32_t cursorX = 0;
int32_t cursorY = 0;
2018-06-22 23:21:44 +02:00
for (int32_t line = 0; line <= no_lines; line++)
{
screenCoords.x = w->windowPos.x + 12;
gfx_draw_string(dpi, wrap_pointer, w->colours[1], screenCoords);
2014-12-22 10:40:00 +00:00
2017-06-06 23:24:18 +02:00
size_t string_length = get_string_size(wrap_pointer) - 1;
2014-12-22 10:40:00 +00:00
2018-06-22 23:21:44 +02:00
if (!cur_drawn && (gTextInput->SelectionStart <= char_count + string_length))
{
2017-06-06 23:24:18 +02:00
// Make a copy of the string for measuring the width.
char temp_string[TEXT_INPUT_SIZE] = { 0 };
std::memcpy(temp_string, wrap_pointer, gTextInput->SelectionStart - char_count);
2020-03-01 16:32:35 -03:00
cursorX = w->windowPos.x + 13 + gfx_get_string_width(temp_string);
cursorY = screenCoords.y;
2014-12-22 10:40:00 +00:00
int32_t width = 6;
2018-06-22 23:21:44 +02:00
if (gTextInput->SelectionStart < strlen(text_input))
{
2017-06-06 23:24:18 +02:00
// Make a 1 utf8-character wide string for measuring the width
// of the currently selected character.
utf8 tmp[5] = { 0 }; // This is easier than setting temp_string[0..5]
uint32_t codepoint = utf8_get_next(text_input + gTextInput->SelectionStart, nullptr);
2017-06-06 23:24:18 +02:00
utf8_write_codepoint(tmp, codepoint);
2018-06-20 17:11:35 +02:00
width = std::max(gfx_get_string_width(tmp) - 2, 4);
2017-06-06 23:24:18 +02:00
}
2014-12-22 10:40:00 +00:00
2018-06-22 23:21:44 +02:00
if (w->frame_no > 15)
{
uint8_t colour = ColourMapA[w->colours[1]].mid_light;
2017-06-06 23:24:18 +02:00
// TODO: palette index addition
gfx_fill_rect(dpi, { { cursorX, screenCoords.y + 9 }, { cursorX + width, screenCoords.y + 9 } }, colour + 5);
2017-06-06 23:24:18 +02:00
}
2014-12-22 10:40:00 +00:00
2017-06-06 23:24:18 +02:00
cur_drawn++;
}
2015-10-20 20:16:30 +02:00
2017-06-06 23:24:18 +02:00
wrap_pointer += string_length + 1;
2014-12-22 10:40:00 +00:00
2018-06-22 23:21:44 +02:00
if (text_input[char_count + string_length] == ' ')
char_count++;
2017-06-06 23:24:18 +02:00
char_count += string_length;
2014-12-22 10:40:00 +00:00
screenCoords.y += 10;
2017-06-06 23:24:18 +02:00
}
2015-07-29 00:15:15 +01:00
2018-06-22 23:21:44 +02:00
if (!cur_drawn)
{
2017-06-06 23:24:18 +02:00
cursorX = gLastDrawStringX;
cursorY = screenCoords.y - 10;
2017-06-06 23:24:18 +02:00
}
2015-07-29 00:15:15 +01:00
2017-06-06 23:24:18 +02:00
// IME composition
2018-06-22 23:21:44 +02:00
if (!str_is_null_or_empty(gTextInput->ImeBuffer))
{
2017-06-06 23:24:18 +02:00
draw_ime_composition(dpi, cursorX, cursorY);
}
2014-10-12 17:15:08 +01:00
}
2017-06-11 23:22:32 +01:00
void window_text_input_key(rct_window* w, char keychar)
{
2017-06-06 23:24:18 +02:00
// If the return button is pressed stop text input
2018-06-22 23:21:44 +02:00
if (keychar == '\r')
{
2017-06-06 23:24:18 +02:00
context_stop_text_input();
2020-05-09 14:18:50 +01:00
window_text_input_execute_callback(true);
2017-06-06 23:24:18 +02:00
window_close(w);
}
2020-05-09 14:18:50 +01:00
w->Invalidate();
2014-10-15 22:53:12 +01:00
}
2019-02-07 17:46:52 +01:00
void window_text_input_periodic_update(rct_window* w)
2014-10-15 22:53:12 +01:00
{
2020-05-09 14:18:50 +01:00
if (calling_class != WC_NULL)
2018-06-22 23:21:44 +02:00
{
2020-05-09 14:18:50 +01:00
auto calling_w = window_find_by_number(calling_class, calling_number);
// If the calling window is closed then close the text
// input window.
if (!calling_w)
{
window_close(w);
return;
}
2017-06-06 23:24:18 +02:00
}
2017-06-06 23:24:18 +02:00
// Used to blink the cursor.
w->frame_no++;
2018-06-22 23:21:44 +02:00
if (w->frame_no > 30)
w->frame_no = 0;
2019-02-07 17:46:52 +01:00
w->Invalidate();
}
2018-06-22 23:21:44 +02:00
static void window_text_input_close(rct_window* w)
2015-02-12 20:51:40 +00:00
{
2017-06-06 23:24:18 +02:00
// Make sure that we take it out of the text input
// mode otherwise problems may occur.
context_stop_text_input();
2020-05-09 14:18:50 +01:00
_title = {};
_description = {};
_callback = {};
_cancelCallback = {};
2014-12-22 10:40:00 +00:00
}
2018-06-22 23:21:44 +02:00
static void window_text_input_invalidate(rct_window* w)
{
2017-06-06 23:24:18 +02:00
// Work out the existing size of the window
char wrapped_string[TEXT_INPUT_SIZE];
safe_strcpy(wrapped_string, text_input, TEXT_INPUT_SIZE);
int32_t no_lines = 0, font_height = 0;
2017-06-06 23:24:18 +02:00
// String length needs to add 12 either side of box
// +13 for cursor when max length.
gfx_wrap_string(wrapped_string, WW - (24 + 13), &no_lines, &font_height);
int32_t height = no_lines * 10 + WH;
2017-06-06 23:24:18 +02:00
// Change window size if required.
2018-06-22 23:21:44 +02:00
if (height != w->height)
{
w->Invalidate();
2017-06-06 23:24:18 +02:00
window_set_resize(w, WW, height, WW, height);
}
2014-12-22 10:40:00 +00:00
window_text_input_widgets[WIDX_OKAY].top = height - 22;
window_text_input_widgets[WIDX_OKAY].bottom = height - 9;
2014-12-22 10:40:00 +00:00
window_text_input_widgets[WIDX_CANCEL].top = height - 22;
window_text_input_widgets[WIDX_CANCEL].bottom = height - 9;
2014-12-22 10:40:00 +00:00
2017-06-06 23:24:18 +02:00
window_text_input_widgets[WIDX_BACKGROUND].bottom = height - 1;
2020-05-09 14:18:50 +01:00
// Set window title argument
auto ft = Formatter::Common();
ft.Add<const char*>(_title.c_str());
2015-10-30 15:18:29 +01:00
}
2017-01-12 18:20:22 +00:00
2018-06-22 23:21:44 +02:00
static void draw_ime_composition(rct_drawpixelinfo* dpi, int cursorX, int cursorY)
2017-01-12 18:20:22 +00:00
{
2017-06-06 23:24:18 +02:00
int compositionWidth = gfx_get_string_width(gTextInput->ImeBuffer);
ScreenCoordsXY screenCoords(cursorX - (compositionWidth / 2), cursorY + 13);
2017-06-06 23:24:18 +02:00
int width = compositionWidth;
int height = 10;
2017-01-12 18:20:22 +00:00
gfx_fill_rect(
dpi, { screenCoords - ScreenCoordsXY{ 1, 1 }, screenCoords + ScreenCoordsXY{ width + 1, height + 1 } },
PALETTE_INDEX_12);
gfx_fill_rect(dpi, { screenCoords, screenCoords + ScreenCoordsXY{ width, height } }, PALETTE_INDEX_0);
gfx_draw_string(dpi, static_cast<const char*>(gTextInput->ImeBuffer), COLOUR_DARK_GREEN, screenCoords);
2017-01-12 18:20:22 +00:00
}