Strip newline whitespace

This commit is contained in:
Charles Dang 2015-09-20 04:46:34 +11:00
parent e96f1c1bc1
commit 660647f7d8
2 changed files with 45 additions and 46 deletions

View file

@ -12,32 +12,32 @@ end
local function get_image(cfg, speaker)
local image = cfg.image
if speaker and image == nil then
image = speaker.__cfg.profile
end
if image == "none" or image == nil then
return ""
end
return image
end
local function get_caption(cfg, speaker)
local caption = cfg.caption
if not caption and speaker ~= nil then
caption = speaker.name or speaker.type_name
end
return caption
end
local function get_speaker(cfg)
local speaker
local context = wesnoth.current.event_context
if cfg.speaker == "narrator" then
speaker = "narrator"
elseif cfg.speaker == "unit" then
@ -47,14 +47,14 @@ local function get_speaker(cfg)
else
speaker = wesnoth.get_units(cfg)[1]
end
return speaker
end
local function message_user_choice(cfg, speaker, options, text_input)
local image = get_image(cfg, speaker)
local caption = get_caption(cfg, speaker)
local left_side = true
-- If this doesn't work, might need tostring()
if image:find("~RIGHT()") then
@ -62,14 +62,14 @@ local function message_user_choice(cfg, speaker, options, text_input)
-- The percent signs escape the parentheses for a literal match
image = image:gsub("~RIGHT%(%)", "")
end
local msg_cfg = {
left_side = left_side,
title = caption,
message = cfg.message,
portrait = image,
}
-- Parse input text, if not available all fields are empty
if text_input then
local input_max_size = tonumber(text_input.max_length) or 256
@ -77,7 +77,7 @@ local function message_user_choice(cfg, speaker, options, text_input)
log("Invalid maximum size for input " .. input_max_size, "warning")
input_max_size = 256
end
-- This roundabout method is because text_input starts out
-- as an immutable userdata value
text_input = {
@ -86,24 +86,24 @@ local function message_user_choice(cfg, speaker, options, text_input)
max_length = input_max_size,
}
end
return function()
local option_chosen, ti_content = wesnoth.show_message_dialog(msg_cfg, options, text_input)
if option_chosen == -2 then -- Pressed Escape (only if no input)
wesnoth.skip_messages()
end
local result_cfg = {}
if #options > 0 then
result_cfg.value = option_chosen
end
if text_input ~= nil then
result_cfg.text = ti_content
end
return result_cfg
end
end
@ -114,7 +114,7 @@ function wesnoth.wml_actions.message(cfg)
log("[message] skipped because [show_if] did not pass", "debug")
return
end
-- Only the first text_input tag is considered
local text_input
for cfg in helper.child_range(cfg, "text_input") do
@ -124,34 +124,34 @@ function wesnoth.wml_actions.message(cfg)
end
text_input = cfg
end
local options, option_events = {}, {}
for option in helper.child_range(cfg, "option") do
local condition = helper.get_child(cfg, "show_if") or {}
if wesnoth.eval_conditional(condition) then
table.insert(options, option.message)
table.insert(option_events, {})
for cmd in helper.child_range(option, "command") do
table.insert(option_events[#option_events], cmd)
end
end
end
-- Check if there is any input to be made, if not the message may be skipped
local has_input = text_input ~= nil or #options > 0
if not has_input and wesnoth.is_skipping_messages() then
-- No input to get and the user is not interested either
log("Skipping [message] because user not interested", "debug")
return
end
local sides_for = cfg.side_for
if sides_for and not has_input then
local show_for_side = false
-- Sanity checks on side number and controller
for side in utils.split(sides_for) do
side = tonumber(side)
@ -160,14 +160,14 @@ function wesnoth.wml_actions.message(cfg)
break
end
end
if not show_for_side then
-- Player isn't controlling side which should see the message
log("Player isn't controlling side that should see [message]", "debug")
return
end
end
local speaker = get_speaker(cfg)
if not speaker then
-- No matching unit found, continue onto the next message
@ -183,39 +183,38 @@ function wesnoth.wml_actions.message(cfg)
if cfg.scroll ~= false then
wesnoth.scroll_to_tile(speaker.x, speaker.y)
end
wesnoth.select_hex(speaker.x, speaker.y, false)
end
if cfg.sound then wesnoth.play_sound(cfg.sound) end
local msg_dlg = message_user_choice(cfg, speaker, options, text_input)
local option_chosen
if not has_input then
-- Always show the dialog if it has no input, whether we are replaying or not
msg_dlg()
else
local choice = wesnoth.synchronize_choice(msg_dlg)
option_chosen = tonumber(choice.value)
if text_input ~= nil then
-- Implement the consequences of the choice
wesnoth.set_variable(text_input.variable or "input", choice.text)
end
end
if #options > 0 then
if option_chosen > #options then
log("invalid choice (" .. option_chosen .. ") was specified, choice 1 to " ..
#options .. " was expected", "debug")
return
end
for i, cmd in ipairs(option_events[option_chosen]) do
utils.handle_event_commands(cmd)
end
end
end

View file

@ -8,11 +8,11 @@ local used_items = {}
function wml_actions.object(cfg)
local context = wesnoth.current.event_context
-- If this item has already been used
local obj_id = utils.check_key(cfg.id, "id", "object", true)
if obj_id and used_items[obj_id] then return end
local unit
local filter = helper.get_child(cfg, "filter")
if filter then
@ -21,12 +21,12 @@ function wml_actions.object(cfg)
if not unit then
unit = wesnoth.get_unit(contxt.x, context.y)
end
local command_type, text
if unit then
text = tostring(cfg.description or "")
command_type = "then"
local dvs = cfg.delayed_variable_substitution
local add = cfg.no_write ~= true
if dvs then
@ -34,26 +34,26 @@ function wml_actions.object(cfg)
else
wesnoth.add_modification(unit, "object", helper.parsed(cfg), add)
end
wesnoth.select_hex(unit.x, unit.y)
-- Mark this item as used up
if obj_id then used_items[obj_id] = true end
else
text = tostring(cfg.cannot_use_message or "")
command_type = "else"
end
-- Default to silent if object has no description
local silent = cfg.silent
if silent == nil then silent = (text:len() == 0) end
if not silent then
wml_actions.redraw{}
local name = tostring(cfg.name or "")
wesnoth.show_popup_dialog(name, text, cfg.image)
end
for cmd in helper.child_range(cfg, command_type) do
utils.handle_event_commands(cmd)
end
@ -72,7 +72,7 @@ function wesnoth.game_events.on_load(cfg)
end
old_on_load(cfg)
end
local old_on_save = wesnoth.game_events.on_save
function wesnoth.game_events.on_save()
local cfg = old_on_save()