The Battle for Wesnoth  1.15.0-dev
handlers.cpp
Go to the documentation of this file.
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"
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 
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 
54 {
55  assert(!disabled_ && "Trying to disable a disabled event. Shouldn't happen!");
56  disabled_ = true;
57 }
58 
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);
75 }
76 
77 } // end namespace game_events
void handle_event(const queued_event &event_info, game_lua_kernel &)
Handles the queued event, according to our WML instructions.
Definition: handlers.cpp:59
event_handler(config &&cfg, bool is_menu_item, const std::vector< std::string > &types)
Definition: handlers.cpp:44
#define DBG_NG
Definition: handlers.cpp:32
static lg::log_domain log_engine("engine")
Domain specific events.
Definition: action_wml.cpp:88
void disable()
Flag this handler as disabled.
Definition: handlers.cpp:53
static lg::log_domain log_event_handler("event_handler")
Define the handlers for the game&#39;s events mechanism.
A variable-expanding proxy for the config class.
Definition: variable.hpp:42
Standard logging facilities (interface).
void commit_music_changes()
Definition: sound.cpp:784
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
bool run_wml_action(const std::string &, const vconfig &, const game_events::queued_event &)
Runs a command from an event handler.