1 /*
2    Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3    Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY.
11 
12    See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * The structure that tracks WML event handlers.
18  * (Typically, handlers are defined by [event] tags.)
19  */
20 
21 #include "game_events/handlers.hpp"
22 
23 #include "game_data.hpp"
24 #include "log.hpp"
25 #include "scripting/game_lua_kernel.hpp"
26 #include "sound.hpp"
27 #include "variable.hpp"
28 
29 #include <iostream>
30 
31 static lg::log_domain log_engine("engine");
32 #define DBG_NG LOG_STREAM(debug, log_engine)
33 #define LOG_NG LOG_STREAM(info, log_engine)
34 #define WRN_NG LOG_STREAM(warn, log_engine)
35 
36 static lg::log_domain log_event_handler("event_handler");
37 #define DBG_EH LOG_STREAM(debug, log_event_handler)
38 
39 // This file is in the game_events namespace.
40 namespace game_events
41 {
42 /* ** event_handler ** */
43 
event_handler(config && cfg,bool imi,const std::vector<std::string> & types)44 event_handler::event_handler(config&& cfg, bool imi, const std::vector<std::string>& types)
45 	: first_time_only_(cfg["first_time_only"].to_bool(true))
46 	, is_menu_item_(imi)
47 	, disabled_(false)
48 	, cfg_(cfg)
49 	, types_(types)
50 {
51 }
52 
disable()53 void event_handler::disable()
54 {
55 	assert(!disabled_ && "Trying to disable a disabled event. Shouldn't happen!");
56 	disabled_ = true;
57 }
58 
handle_event(const queued_event & event_info,game_lua_kernel & lk)59 void event_handler::handle_event(const queued_event& event_info, game_lua_kernel& lk)
60 {
61 	if(disabled_) {
62 		return;
63 	}
64 
65 	if(is_menu_item_) {
66 		DBG_NG << cfg_["name"] << " will now invoke the following command(s):\n" << cfg_;
67 	}
68 
69 	if(first_time_only_) {
70 		disable();
71 	}
72 
73 	lk.run_wml_action("command", vconfig(cfg_, false), event_info);
74 	sound::commit_music_changes();
75 }
76 
77 } // end namespace game_events
78