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