The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
handlers.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2017 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project http://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 "formula/string_utils.hpp"
24 #include "game_data.hpp"
25 #include "log.hpp"
28 #include "sound.hpp"
29 #include "variable.hpp"
30 
31 #include <iostream>
32 
33 static lg::log_domain log_engine("engine");
34 #define DBG_NG LOG_STREAM(debug, log_engine)
35 #define LOG_NG LOG_STREAM(info, log_engine)
36 #define WRN_NG LOG_STREAM(warn, log_engine)
37 
38 static lg::log_domain log_event_handler("event_handler");
39 #define DBG_EH LOG_STREAM(debug, log_event_handler)
40 
41 // This file is in the game_events namespace.
42 namespace game_events
43 {
44 /* ** event_handler ** */
45 
47  : first_time_only_(cfg["first_time_only"].to_bool(true))
48  , is_menu_item_(imi)
49  , disabled_(false)
50  , cfg_(cfg)
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 
79 {
80  const std::string my_names = !gd
81  ? cfg_["name"].str()
83 
84  std::string::const_iterator
85  itor, it_begin = my_names.begin(),
86  it_end = my_names.end(),
87  match_it = name.begin(),
88  match_begin = name.begin(),
89  match_end = name.end();
90 
91  int skip_count = 0;
92  for(itor = it_begin; itor != it_end; ++itor) {
93  bool do_eat = false, do_skip = false;
94 
95  switch(*itor) {
96  case ',':
97  if(itor - it_begin - skip_count == match_it - match_begin && match_it == match_end) {
98  return true;
99  }
100  it_begin = itor + 1;
101  match_it = match_begin;
102  skip_count = 0;
103  continue;
104  case '\f':
105  case '\n':
106  case '\r':
107  case '\t':
108  case '\v':
109  do_skip = (match_it == match_begin || match_it == match_end);
110  break;
111  case ' ':
112  do_skip = (match_it == match_begin || match_it == match_end);
113  FALLTHROUGH;
114  case '_':
115  do_eat = (match_it != match_end && (*match_it == ' ' || *match_it == '_'));
116  break;
117  default:
118  do_eat = (match_it != match_end && *match_it == *itor);
119  break;
120  }
121 
122  if(do_eat) {
123  ++match_it;
124  } else if(do_skip) {
125  ++skip_count;
126  } else {
127  itor = std::find(itor, it_end, ',');
128  if(itor == it_end) {
129  return false;
130  }
131  it_begin = itor + 1;
132  match_it = match_begin;
133  skip_count = 0;
134  }
135  }
136 
137  if(itor - it_begin - skip_count == match_it - match_begin && match_it == match_end) {
138  return true;
139  }
140 
141  return false;
142 }
143 
144 } // end namespace game_events
event_handler(config &&cfg, bool is_menu_item)
Definition: handlers.cpp:46
std::vector< char_t > string
std::string interpolate_variables_into_string(const std::string &str, const string_map *const symbols)
Function which will interpolate variables, starting with '$' in the string 'str' with the equivalent ...
bool run_wml_action(const std::string &, vconfig const &, game_events::queued_event const &)
Runs a command from an event handler.
void handle_event(const queued_event &event_info, game_lua_kernel &)
Handles the queued event, according to our WML instructions.
Definition: handlers.cpp:60
bool matches_name(const std::string &name, const game_data *data) const
Definition: handlers.cpp:78
#define DBG_NG
Definition: handlers.cpp:34
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:54
static lg::log_domain log_event_handler("event_handler")
Define the handlers for the game's events mechanism.
bool find(E event, F functor)
Tests whether an event handler is available.
A variable-expanding proxy for the config class.
Definition: variable.hpp:42
Standard logging facilities (interface).
static const char * name(const std::vector< SDL_Joystick * > &joysticks, const size_t index)
Definition: joystick.cpp:48
void commit_music_changes()
Definition: sound.cpp:783
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93