Lua API: Add wesnoth.show_message_dialog()

This commit is contained in:
Celtic Minstrel 2015-09-16 19:29:28 -04:00
parent af2e16f842
commit 91ac90db11
5 changed files with 73 additions and 0 deletions

View file

@ -30,6 +30,7 @@ Version 1.13.1+dev:
* Added support for current.event_context.unit_x/y fields (bug #23507)
* Added wesnoth.set_dialog_focus function
* Added wesnoth.set_dialog_visible function
* Added wesnoth.show_message_dialog function
* Added wesnoth.random function
* helper.shuffle is now synced
* Music and sound effects:

View file

@ -18,6 +18,7 @@
#include "gui/auxiliary/window_builder.hpp" // for twindow_builder, etc
#include "gui/dialogs/gamestate_inspector.hpp"
#include "gui/dialogs/lua_interpreter.hpp"
#include "gui/dialogs/wml_message.hpp"
#include "gui/widgets/clickable.hpp" // for tclickable_
#include "gui/widgets/control.hpp" // for tcontrol
#include "gui/widgets/multi_page.hpp" // for tmulti_page
@ -239,6 +240,61 @@ int show_dialog(lua_State *L, CVideo & video)
return 1;
}
/**
* Displays a message window
* - Arg 1: Table describing the window
* - Arg 2: List of options (nil or empty table - no options)
* - Arg 3: Text input specifications (nil or empty table - no text input)
* - Ret 1: option chosen (if no options: 0 if there's text input, -2 if escape pressed, else -1)
* - Ret 2: string entered (empty if none, nil if no text input)
*/
int show_message_dialog(lua_State *L, CVideo & video)
{
config txt_cfg;
const bool has_input = !lua_isnoneornil(L, 3) && luaW_toconfig(L, 3, txt_cfg) && !txt_cfg.empty();
const std::string& input_caption = txt_cfg["label"];
std::string input_text = txt_cfg["text"].str();
unsigned int input_max_len = txt_cfg["max_length"].to_int(256);
std::vector<std::string> options;
int chosen_option = -1;
if (!lua_isnoneornil(L, 2)) {
luaL_checktype(L, 2, LUA_TTABLE);
size_t n = lua_rawlen(L, 2);
for(size_t i = 1; i <= n; i++) {
lua_rawgeti(L, 2, i);
options.push_back(luaL_checkstring(L, -1));
}
}
const config& def_cfg = luaW_checkconfig(L, 1);
const std::string& title = def_cfg["title"];
const std::string& message = def_cfg["message"];
const std::string& portrait = def_cfg["portrait"];
const bool left_side = def_cfg["left_side"].to_bool(true);
const bool mirror = def_cfg["mirror"].to_bool(false);
int dlg_result = gui2::show_wml_message(
left_side, video, title, message, portrait, mirror,
has_input, input_caption, &input_text, input_max_len,
options, &chosen_option
);
if (!has_input and options.empty()) {
lua_pushinteger(L, dlg_result);
} else {
lua_pushinteger(L, chosen_option + 1);
}
if (has_input) {
lua_pushlstring(L, input_text.c_str(), input_text.length());
} else {
lua_pushnil(L);
}
return 2;
}
/**
* Sets the value of a widget on the current dialog.
* - Arg 1: scalar.

View file

@ -32,6 +32,7 @@ int intf_set_dialog_active(lua_State *L);
int intf_set_dialog_visible(lua_State *L);
int intf_add_dialog_tree_node(lua_State *L);
int show_dialog(lua_State *L, CVideo & video);
int show_message_dialog(lua_State *L, CVideo & video);
int show_lua_console(lua_State*L, CVideo & video, lua_kernel_base * lk);
int show_gamestate_inspector(CVideo & video, const vconfig & cfg);
int intf_remove_dialog_item(lua_State *L);

View file

@ -119,6 +119,17 @@ int lua_kernel_base::intf_show_dialog(lua_State *L)
return lua_gui2::show_dialog(L, *video_);
}
int lua_kernel_base::intf_show_message_dialog(lua_State *L)
{
if (!video_) {
ERR_LUA << "Cannot show dialog, no video object is available to this lua kernel.";
lua_error(L);
return 0;
}
return lua_gui2::show_message_dialog(L, *video_);
}
// The show lua console callback is similarly a method of lua kernel
int lua_kernel_base::intf_show_lua_console(lua_State *L)
{
@ -257,6 +268,7 @@ lua_kernel_base::lua_kernel_base(CVideo * video)
{ "dofile", &dispatch<&lua_kernel_base::intf_dofile> },
{ "require", &dispatch<&lua_kernel_base::intf_require> },
{ "show_dialog", &dispatch<&lua_kernel_base::intf_show_dialog> },
{ "show_message_dialog", &dispatch<&lua_kernel_base::intf_show_message_dialog> },
{ "show_lua_console", &dispatch<&lua_kernel_base::intf_show_lua_console> },
{ NULL, NULL }
};

View file

@ -103,6 +103,9 @@ protected:
// Show a dialog to the currently connected video object (if available)
int intf_show_dialog(lua_State * L);
// Show a message dialog, possibly with options
int intf_show_message_dialog(lua_State * L);
// Show the interactive lua console (for debugging purposes)
int intf_show_lua_console(lua_State * L);