From 7fce735ca1fd5b4b25f150d22db1c5dbb0ebd44a Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 2 Jan 2025 16:53:28 +0300 Subject: [PATCH] add gui.confirm(...) --- doc/en/scripting/builtins/libgui.md | 18 +++++++++++++++ doc/ru/scripting/builtins/libgui.md | 18 +++++++++++++++ src/graphics/ui/gui_util.cpp | 14 +++++++----- src/graphics/ui/gui_util.hpp | 1 + src/logic/EngineController.cpp | 1 + src/logic/scripting/lua/libs/libgui.cpp | 29 +++++++++++++++++++++++++ 6 files changed, 76 insertions(+), 5 deletions(-) diff --git a/doc/en/scripting/builtins/libgui.md b/doc/en/scripting/builtins/libgui.md index 3132bce3..c49ff478 100644 --- a/doc/en/scripting/builtins/libgui.md +++ b/doc/en/scripting/builtins/libgui.md @@ -61,3 +61,21 @@ gui.escape_markup( ``` Escapes markup in text. + +```lua +gui.confirm( + -- message (does not translate automatically, use gui.str(...)) + message: str, + -- function called upon confirmation + on_confirm: function() -> nil, + -- function called upon denial/cancellation + [optional] on_deny: function() -> nil, + -- text for the confirmation button (default: "Yes") + -- use an empty string for the default value if you want to specify no_text. + [optional] yes_text: str, + -- text for the denial button (default: "No") + [optional] no_text: str, +) +``` + +Requests confirmation from the user for an action. **Does not** stop code execution. diff --git a/doc/ru/scripting/builtins/libgui.md b/doc/ru/scripting/builtins/libgui.md index 11dfde03..22b22981 100644 --- a/doc/ru/scripting/builtins/libgui.md +++ b/doc/ru/scripting/builtins/libgui.md @@ -58,3 +58,21 @@ gui.escape_markup( ``` Экранирует разметку в тексте. + +```lua +gui.confirm( + -- сообщение (не переводится автоматически, используйте gui.str(...)) + message: str, + -- функция, вызываемая при подтвержении + on_confirm: function() -> nil, + -- функция, вызываемая при отказе/отмене + [опционально] on_deny: function() -> nil, + -- текст кнопки подтвержения (по-умолчанию: "Да") + -- используйте пустую строку для значения по-умолчанию, если нужно указать no_text. + [опционально] yes_text: str, + -- текст кнопки отказа (по-умолчанию: "Нет") + [опционально] no_text: str, +) +``` + +Запрашивает у пользователя подтверждение действия. **Не** останавливает выполнение кода. diff --git a/src/graphics/ui/gui_util.cpp b/src/graphics/ui/gui_util.cpp index 0d913395..94ff01d7 100644 --- a/src/graphics/ui/gui_util.cpp +++ b/src/graphics/ui/gui_util.cpp @@ -50,11 +50,13 @@ void guiutil::alert( } void guiutil::confirm( - const std::shared_ptr& menu, - const std::wstring& text, - const runnable& on_confirm, - std::wstring yestext, - std::wstring notext) { + const std::shared_ptr& menu, + const std::wstring& text, + const runnable& on_confirm, + const runnable& on_deny, + std::wstring yestext, + std::wstring notext +) { if (yestext.empty()) yestext = langs::get(L"Yes"); if (notext.empty()) notext = langs::get(L"No"); @@ -71,6 +73,8 @@ void guiutil::confirm( })); subpanel->add(std::make_shared