Moved [switch] implementation to Lua.

This commit is contained in:
Guillaume Melquiond 2010-07-19 13:19:25 +00:00
parent 9b82345628
commit 4857a03e02
2 changed files with 22 additions and 22 deletions

View file

@ -312,3 +312,25 @@ end
wml_actions["if"] = if_handler
wml_actions["while"] = while_handler
function wml_actions.switch(cfg)
local value = wesnoth.get_variable(cfg.variable)
local found = false
-- Execute all the [case]s where the value matches.
for v in helper.child_range(cfg, "case") do
local match = false
for w in string.gmatch(v.value, "[^%s,][^,]*") do
if w == value then match = true end
end
if match then
handle_event_commands(v)
found = true
end
end
-- Otherwise execute [else] statements.
if not found then
for v in helper.child_range(cfg, "else") do
handle_event_commands(v)
end
end
end

View file

@ -2609,28 +2609,6 @@ WML_HANDLER_FUNCTION(allow_undo,/*event_info*/,/*cfg*/)
current_context->mutated = false;
}
WML_HANDLER_FUNCTION(switch, event_info, cfg)
{
const std::string var_name = cfg["variable"];
std::string var = resources::state_of_game->get_variable_const(var_name);
bool not_found = true;
// execute all cases where the value matches
foreach (const vconfig &c, cfg.get_children("case")) {
std::vector<std::string> vals = utils::split(c["value"]);
if (std::find(vals.begin(), vals.end(), var) != vals.end()) {
not_found = false;
handle_event_commands(event_info, c);
}
}
if (not_found) {
// otherwise execute 'else' statements
foreach (const vconfig &e, cfg.get_children("else")) {
handle_event_commands(event_info, e);
}
}
}
WML_HANDLER_FUNCTION(open_help, /*event_info*/, cfg)
{
game_display &screen = *resources::screen;