The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
hotkey_handler_sp.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2016 by Chris Beck <[email protected]>
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 
16 
17 #include "formula/string_utils.hpp"
19 #include "hotkey/hotkey_item.hpp"
20 #include "map/label.hpp"
21 #include "play_controller.hpp"
23 #include "whiteboard/manager.hpp"
26 #include "save_index.hpp"
27 #include "gui/dialogs/message.hpp"
28 #include "resources.hpp"
29 #include "replay.hpp"
30 
31 #include "units/unit.hpp"
32 
35  , playsingle_controller_(pc)
36  , whiteboard_manager_(pc.get_whiteboard())
37 {}
38 
40 
41 bool playsingle_controller::hotkey_handler::is_observer() const { return playsingle_controller_.is_observer(); }
42 
44  if (!browse())
45  menu_handler_.recruit(play_controller_.current_side(), mouse_handler_.get_last_hex());
46  else if (whiteboard_manager_->is_active())
47  menu_handler_.recruit(gui()->viewing_side(), mouse_handler_.get_last_hex());
48 }
49 
51  if (!browse())
52  menu_handler_.repeat_recruit(play_controller_.current_side(), mouse_handler_.get_last_hex());
53  else if (whiteboard_manager_->is_active())
55 }
56 
58  if (!browse())
59  menu_handler_.recall(play_controller_.current_side(), mouse_handler_.get_last_hex());
60  else if (whiteboard_manager_->is_active())
61  menu_handler_.recall(gui()->viewing_side(), mouse_handler_.get_last_hex());
62 }
63 
65  menu_handler_.toggle_shroud_updates(gui()->viewing_team()+1);
66 }
67 
69  menu_handler_.update_shroud_now(gui()->viewing_team()+1);
70 }
71 
73  playsingle_controller_.end_turn();
74 }
75 
78 }
79 
82 }
83 
86 }
87 
90 }
91 
94 }
95 
98 }
99 
101  menu_handler_.continue_move(mouse_handler_, play_controller_.current_side());
102 }
103 
105  if (!browse())
106  menu_handler_.unit_hold_position(mouse_handler_, play_controller_.current_side());
107 }
108 
110  if (!browse())
111  menu_handler_.end_unit_turn(mouse_handler_, play_controller_.current_side());
112 }
113 
116 }
117 
120 }
121 
124 }
125 
128 }
129 
132 }
133 
135  whiteboard_manager_->set_active(!whiteboard_manager_->is_active());
136 
137  if (whiteboard_manager_->is_active()) {
139  utils::string_map symbols;
140  symbols["hotkey"] = hk;
141 
142  gui()->announce(_("Planning mode activated!") + std::string("\n") + vgettext("(press $hotkey to deactivate)", symbols), font::NORMAL_COLOR);
143  } else {
144  gui()->announce(_("Planning mode deactivated!"), font::NORMAL_COLOR);
145  }
146  //@todo Stop printing whiteboard help in the chat once we have better documentation/help
147  whiteboard_manager_->print_help_once();
148 }
149 
151  whiteboard_manager_->contextual_execute();
152 }
153 
155  whiteboard_manager_->execute_all_actions();
156 }
157 
159  whiteboard_manager_->contextual_delete();
160 }
161 
163 {
164  whiteboard_manager_->contextual_bump_up_action();
165 }
166 
168 {
169  whiteboard_manager_->contextual_bump_down_action();
170 }
171 
173 {
174  unit* curr_unit;
175  map_location loc;
176  { wb::future_map future; //start planned unit map scope
177  curr_unit = &*menu_handler_.current_unit();
178  loc = curr_unit->get_location();
179  } // end planned unit map scope
180  whiteboard_manager_->save_suppose_dead(*curr_unit,loc);
181 }
182 
184 {
185  switch(command) {
188  default:
190  }
191 }
192 
194 {
195  hotkey::HOTKEY_COMMAND command = cmd.id;
196  bool res = true;
197  switch (command){
198 
199  case hotkey::HOTKEY_WML:
200  {
201  int prefixlen = wml_menu_hotkey_prefix.length();
202  if(cmd.command.compare(0, prefixlen, wml_menu_hotkey_prefix) != 0) {
203  return false;
204  }
206  if(it == gamestate().get_wml_menu_items().end()) {
207  return false;
208  }
209 
210  return !(**it).is_synced() || play_controller_.can_use_synced_wml_menu();
211  }
214  return !browse() && !linger() && !events::commands_disabled;
218  return (!browse() || whiteboard_manager_->is_active()) && !linger() && !events::commands_disabled;
220  return (!browse() || linger()) && !events::commands_disabled;
221 
223  return !linger()
224  && (viewing_team().uses_fog() || viewing_team().uses_shroud())
225  && viewing_team_is_playing()
226  && viewing_team().is_local_human()
229  return !linger()
230  && viewing_team_is_playing()
231  && viewing_team().is_local_human()
233  && viewing_team().auto_shroud_updates() == false;
234 
235  // Commands we can only do if in debug mode
239  return !events::commands_disabled && game_config::debug && play_controller_.get_map_const().on_board(mouse_handler_.get_last_hex());
240 
242  res = !is_observer();
243  break;
246  const terrain_label *label = gui()->labels().get_label(mouse_handler_.get_last_hex());
247  res = !events::commands_disabled && play_controller_.get_map_const().on_board(mouse_handler_.get_last_hex())
248  && !gui()->shrouded(mouse_handler_.get_last_hex())
249  && !is_observer()
250  && (!label || !label->immutable());
251  break;
252  }
254  if(browse() || events::commands_disabled)
255  return false;
256 
258  && (menu_handler_.current_unit()->move_interrupted()))
259  return true;
260  const unit_map::const_iterator i = play_controller_.get_units_const().find(mouse_handler_.get_selected_hex());
261  if (!i.valid()) return false;
262  return i->move_interrupted();
263  }
265  return !is_observer();
268  return whiteboard_manager_->can_enable_execution_hotkeys();
270  return whiteboard_manager_->can_enable_modifier_hotkeys();
273  return whiteboard_manager_->can_enable_reorder_hotkeys();
275  {
276  //@todo re-enable this once we figure out a decent UI for suppose_dead
277  return false;
278  }
279 
290  return playsingle_controller_.get_replay_controller() && playsingle_controller_.get_replay_controller()->can_execute_command(cmd, index);
292  return playsingle_controller_.is_replay();
293  default: return play_controller::hotkey_handler::can_execute_command(cmd, index);
294  }
295  return res;
296 }
297 
299 {
300  if(playsingle_controller_.is_networked_mp())
301  {
303  std::string error_log;
304  savegame::read_save_file(filename, savegame, &error_log);
305 
306  if(!error_log.empty() || savegame.child_or_empty("snapshot")["replay_pos"].to_int(-1) < 0 ) {
307  gui2::show_error_message(play_controller_.get_display().video(),
308  _("The file you have tried to load is corrupt: '") +
309  error_log);
310  return;
311  }
312  boost::shared_ptr<config> res(new config(savegame.child_or_empty("snapshot")));
313  throw reset_gamestate_exception(res, true);
314  }
315  else
316  {
318  }
319 }
320 
322 {
323  if(!playsingle_controller_.is_networked_mp()) {
325  }
326  playsingle_controller_.set_player_type_changed();
327 }
void label_terrain(mouse_handler &mousehandler, bool team_only)
void delete_upcoming_commands()
Definition: replay.cpp:187
void show_error_message(CVideo &video, const std::string &message, bool message_use_markup)
Shows an error message to the user.
Definition: message.cpp:198
const map_location & get_location() const
Definition: unit.hpp:286
void read_save_file(const std::string &name, config &cfg, std::string *error_log)
Read the complete config information out of a savefile.
Definition: save_index.cpp:250
Definition: unit.hpp:95
events::mouse_handler mouse_handler_
virtual hotkey::ACTION_STATE get_action_state(hotkey::HOTKEY_COMMAND command, int index) const
virtual void load_autosave(const std::string &filename)
General purpose widgets.
Stores all information related to functions that can be bound to hotkeys.
void update_shroud_now(int side_num)
Replay control code.
const map_location & get_last_hex() const
const config & child_or_empty(const std::string &key) const
Returns the first child with the given key, or an empty config if there is none.
Definition: config.cpp:722
void create_unit(mouse_handler &mousehandler)
Creates a unit (in debug mode via hotkey or context menu).
const SDL_Color NORMAL_COLOR
Definition: font.cpp:564
iterator find(const std::string &id)
Returns an iterator to a menu item with the given id, if one exists.
map_location get_selected_hex() const
An extension of play_controller::hotkey_handler, which has support for SP wesnoth features like white...
events::menu_handler menu_handler_
bool immutable() const
Definition: label.cpp:454
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:82
void toggle_shroud_updates(int side_num)
std::map< std::string, t_string > string_map
Applies the planned unit map for the duration of the struct's life.
Definition: manager.hpp:249
void recall(int side_num, const map_location &last_hex)
void end_unit_turn(mouse_handler &mousehandler, int side_num)
void recruit(int side_num, const map_location &last_hex)
replay * recorder
Definition: resources.cpp:30
bool is_observer() const
virtual bool can_execute_command(const hotkey::hotkey_command &command, int index=-1) const
Check if a command can be executed.
Encapsulates the map of the game.
Definition: location.hpp:38
Declarations for a container for wml_menu_item.
GLuint res
Definition: glew.h:9258
virtual void load_autosave(const std::string &filename)
void continue_move(mouse_handler &mousehandler, int side_num)
const std::string command
The command is unique.
virtual hotkey::ACTION_STATE get_action_state(hotkey::HOTKEY_COMMAND command, int index) const
virtual bool can_execute_command(const hotkey::hotkey_command &command, int index=-1) const
Check if a command can be executed.
GLuint index
Definition: glew.h:1782
size_t i
Definition: function.cpp:1057
void unit_hold_position(mouse_handler &mousehandler, int side_num)
void change_side(mouse_handler &mousehandler)
std::string get_names(std::string id)
Returns a comma-separated string of hotkey names.
void kill_unit(mouse_handler &mousehandler)
std::string vgettext(const char *msgid, const utils::string_map &symbols)
hotkey_handler(playsingle_controller &, saved_game &)
To store label data Class implements logic for rendering.
Definition: label.hpp:103
unit_map::iterator current_unit()
game_state & gamestate()
const hotkey::HOTKEY_COMMAND id
the names are strange: the "hotkey::HOTKEY_COMMAND" is named id, and the string to identify the objec...
boost::shared_ptr< wb::manager > whiteboard_manager_
This is a const_iterator class that extends an existing const_iterator by overriding dereference...
Definition: iterator.hpp:27
bool valid() const
Definition: map.hpp:229
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
void repeat_recruit(int side_num, const map_location &last_hex)
GLsizei const GLcharARB ** string
Definition: glew.h:4503
static const hotkey_command & get_command_by_command(HOTKEY_COMMAND command)
the execute_command argument was changed from HOTKEY_COMMAND to hotkey_command, to be able to call it...
game_events::wmi_container & get_wml_menu_items()
Definition: game_state.cpp:385