New syntax for [message][option]message= similar to new [campaign][difficulty]

This commit is contained in:
Celtic Minstrel 2015-09-30 01:39:48 -04:00
parent 7f24dd1907
commit 114b31c90c
6 changed files with 97 additions and 31 deletions

View file

@ -131,7 +131,25 @@ function wesnoth.wml_actions.message(cfg)
local condition = helper.get_child(option, "show_if") or {}
if wesnoth.eval_conditional(condition) then
table.insert(options, option.message)
if option.message and not option.image and not option.label then
local message = tostring(option.message)
if message:find("&") or message:find("=") or message:find("*") == 1 then
wesnoth.wml_actions.deprecated_message{message = '[option]message="&image=col1=col2" is deprecated, use new DescriptionWML instead (default, image, label, description)'}
end
-- Legacy format
table.insert(options, option.message)
else
local opt = helper.parsed(option)
if opt.message then
if not opt.label then
-- Support either message or description
opt.label = opt.message
else
log("[option] has both label= and message=, ignoring the latter", "warning")
end
end
table.insert(options, opt)
end
table.insert(option_events, {})
for cmd in helper.child_range(option, "command") do

View file

@ -37,7 +37,6 @@
#include "game_preferences.hpp"
#include "gettext.hpp"
#include "gui/dialogs/transient_message.hpp"
#include "gui/dialogs/wml_message.hpp"
#include "gui/widgets/window.hpp"
#include "log.hpp"
#include "map.hpp"

View file

@ -17,7 +17,6 @@
#include "gui/dialogs/wml_message.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/auxiliary/old_markup.hpp"
#include "gui/widgets/button.hpp"
#include "gui/widgets/label.hpp"
#ifdef GUI2_EXPERIMENTAL_LISTBOX
@ -44,7 +43,7 @@ void twml_message_::set_input(const std::string& caption,
input_maximum_length_ = maximum_length;
}
void twml_message_::set_option_list(const std::vector<std::string>& option_list,
void twml_message_::set_option_list(const std::vector<twml_message_option>& option_list,
int* chosen_option)
{
assert(!option_list.empty());
@ -100,21 +99,9 @@ void twml_message_::pre_show(CVideo& /*video*/, twindow& window)
if(!option_list_.empty()) {
std::map<std::string, string_map> data;
for(size_t i = 0; i < option_list_.size(); ++i) {
/**
* @todo This syntax looks like a bad hack, it would be nice to
* write a new syntax which doesn't use those hacks (also avoids
* the problem with special meanings for certain characters.
*/
tlegacy_menu_item item(option_list_[i]);
if(item.is_default()) {
// Number of items hasn't been increased yet so i is ok.
*chosen_option_ = i;
}
BOOST_FOREACH(const twml_message_option& item, option_list_) {
// Add the data.
data["icon"]["label"] = item.icon();
data["icon"]["label"] = item.image();
data["label"]["label"] = item.label();
data["label"]["use_markup"] = "true";
data["description"]["label"] = item.description();
@ -170,10 +157,10 @@ int show_wml_message(const bool left_side,
const std::string& input_caption,
std::string* input_text,
const unsigned maximum_length,
const std::vector<std::string>& option_list,
const std::vector<twml_message_option>& option_list,
int* chosen_option)
{
std::auto_ptr<twml_message_> dlg;
boost::shared_ptr<twml_message_> dlg;
if(left_side) {
dlg.reset(new twml_message_left(title, message, portrait, mirror));
} else {

View file

@ -20,6 +20,23 @@
namespace gui2
{
/**
* Helper class for message options
*/
class twml_message_option {
public:
explicit twml_message_option(std::string label, std::string description = "", std::string image = "")
: label_(label)
, description_(description)
, image_(image)
{}
std::string label() const {return label_;};
std::string description() const {return description_;};
std::string image() const {return image_;};
private:
std::string label_, description_, image_;
};
/**
* Base class for the wml generated messages.
*
@ -50,15 +67,22 @@ public:
* Sets the input text variables.
*
* @param caption The caption for the label.
* @param text The initial text, after showing the final
* @param [in,out] text The initial text, after showing the final
* text.
* @param maximum_length The maximum length of the text.
*/
void set_input(const std::string& caption,
std::string* text,
const unsigned maximum_length);
void set_option_list(const std::vector<std::string>& option_list,
/**
* Sets the option list
*
* @param option_list The list of options to display.
* @param [in,out] chosen_option Pointer to the index of the initially
* selected option; after showing, the
* chosen option.
*/
void set_option_list(const std::vector<twml_message_option>& option_list,
int* chosen_option);
private:
@ -93,7 +117,7 @@ private:
unsigned input_maximum_length_;
/** The list of options the user can choose. */
std::vector<std::string> option_list_;
std::vector<twml_message_option> option_list_;
/** The chosen option. */
int* chosen_option_;
@ -161,7 +185,7 @@ private:
*
* @param option_list A list of options to select in the dialog.
* @param chosen_option Pointer to the initially chosen option.
* Will be set to the chosen_option when the
* Will be set to the chosen option when the
* dialog closes.
*/
int show_wml_message(const bool left_side,
@ -174,7 +198,7 @@ int show_wml_message(const bool left_side,
const std::string& input_caption,
std::string* input_text,
const unsigned maximum_length,
const std::vector<std::string>& option_list,
const std::vector<twml_message_option>& option_list,
int* chosen_option);

View file

@ -45,7 +45,6 @@
#include "gui/dialogs/label_settings.hpp"
#include "gui/dialogs/message.hpp"
#include "gui/dialogs/transient_message.hpp"
#include "gui/dialogs/wml_message.hpp"
#include "gui/dialogs/gamestate_inspector.hpp"
#include "gui/dialogs/mp_change_control.hpp"
#include "gui/dialogs/data_manage.hpp"

View file

@ -16,6 +16,7 @@
#include "gui/auxiliary/canvas.hpp" // for tcanvas
#include "gui/auxiliary/window_builder.hpp" // for twindow_builder, etc
#include "gui/auxiliary/old_markup.hpp"
#include "gui/dialogs/gamestate_inspector.hpp"
#include "gui/dialogs/lua_interpreter.hpp"
#include "gui/dialogs/wml_message.hpp"
@ -43,7 +44,6 @@
#include "scripting/lua_api.hpp" // for luaW_toboolean, etc
#include "scripting/lua_common.hpp"
#include "scripting/lua_types.hpp" // for getunitKey, dlgclbkKey, etc
#include "scripting/push_check.hpp"
#include "serialization/string_utils.hpp"
#include "tstring.hpp"
#include "video.hpp"
@ -258,11 +258,50 @@ int show_message_dialog(lua_State *L, CVideo & video)
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;
std::vector<gui2::twml_message_option> options;
int chosen_option = -1;
if (!lua_isnoneornil(L, 2)) {
std::vector<t_string> t_options = lua_check<std::vector<t_string> >(L, 2);
std::copy(t_options.begin(), t_options.end(), std::back_inserter(options));
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);
t_string short_opt;
config opt;
if(luaW_totstring(L, -1, short_opt)) {
// Note: Although this currently uses the tlegacy_menu_item class
// for the deprecated syntax, this branch should still be retained
// when the deprecated syntax is removed, as a simpler method
// of specifying options when only a single string is needed.
gui2::tlegacy_menu_item item(short_opt);
opt["image"] = item.icon();
opt["label"] = item.label();
opt["description"] = item.description();
opt["default"] = item.is_default();
if(!opt["image"].blank() || !opt["description"].blank() || !opt["default"].blank()) {
ERR_LUA << "The &image=col1=col2 syntax is deprecated, use new DescriptionWML instead.\n";
}
} else if(!luaW_toconfig(L, -1, opt)) {
std::ostringstream error;
error << "expected array of config and/or translatable strings, but index ";
error << i << " was a " << lua_typename(L, lua_type(L, -1));
return luaL_argerror(L, 2, error.str().c_str());
}
gui2::twml_message_option option(opt["label"], opt["description"], opt["image"]);
if(opt["default"].to_bool(false)) {
chosen_option = i - 1;
}
options.push_back(option);
}
lua_getfield(L, 2, "default");
if(lua_isnumber(L, -1)) {
int i = lua_tointeger(L, -1);
if(i < 1 || size_t(i) > n) {
std::ostringstream error;
error << "default= key in options list is not a valid option index (1-" << n << ")";
return luaL_argerror(L, 2, error.str().c_str());
}
chosen_option = i - 1;
}
}
const config& def_cfg = luaW_checkconfig(L, 1);