The Battle for Wesnoth  1.15.2+dev
hotkey_handler_sp.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2018 by Chris Beck <render787@gmail.com>
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 
16 
17 #include "filesystem.hpp" // for get_saves_dir()
18 #include "font/standard_colors.hpp"
19 #include "formula/string_utils.hpp"
21 #include "hotkey/hotkey_item.hpp"
22 #include "map/label.hpp"
23 #include "play_controller.hpp"
25 #include "whiteboard/manager.hpp"
28 #include "map/map.hpp"
29 #include "save_index.hpp"
30 #include "gui/dialogs/message.hpp"
31 #include "resources.hpp"
32 #include "replay.hpp"
33 
34 #include "units/unit.hpp"
35 
38  , playsingle_controller_(pc)
39  , whiteboard_manager_(pc.get_whiteboard())
40 {}
41 
43 
45 
47  if (!browse())
49  else if (whiteboard_manager_->is_active())
50  menu_handler_.recruit(gui()->viewing_side(), mouse_handler_.get_last_hex());
51 }
52 
54  if (!browse())
56  else if (whiteboard_manager_->is_active())
58 }
59 
61  if (!browse())
63  else if (whiteboard_manager_->is_active())
64  menu_handler_.recall(gui()->viewing_side(), mouse_handler_.get_last_hex());
65 }
66 
69 }
70 
73 }
74 
77 }
78 
81 }
82 
85 }
86 
89 }
90 
93 }
94 
97 }
98 
101 }
102 
105 }
106 
108  if (!browse())
110 }
111 
113  if (!browse())
115 }
116 
119 }
120 
123 }
124 
127 }
128 
131 }
132 
135 }
136 
138  whiteboard_manager_->set_active(!whiteboard_manager_->is_active());
139 
140  if (whiteboard_manager_->is_active()) {
142  utils::string_map symbols;
143  symbols["hotkey"] = hk;
144 
145  gui()->announce(_("Planning mode activated!") + std::string("\n") + VGETTEXT("(press $hotkey to deactivate)", symbols), font::NORMAL_COLOR);
146  } else {
147  gui()->announce(_("Planning mode deactivated!"), font::NORMAL_COLOR);
148  }
149  //@todo Stop printing whiteboard help in the chat once we have better documentation/help
150  whiteboard_manager_->print_help_once();
151 }
152 
154  whiteboard_manager_->contextual_execute();
155 }
156 
158  whiteboard_manager_->execute_all_actions();
159 }
160 
162  whiteboard_manager_->contextual_delete();
163 }
164 
166 {
167  whiteboard_manager_->contextual_bump_up_action();
168 }
169 
171 {
172  whiteboard_manager_->contextual_bump_down_action();
173 }
174 
176 {
177  unit* curr_unit;
178  map_location loc;
179  { wb::future_map future; //start planned unit map scope
180  curr_unit = &*menu_handler_.current_unit();
181  loc = curr_unit->get_location();
182  } // end planned unit map scope
183  whiteboard_manager_->save_suppose_dead(*curr_unit,loc);
184 }
185 
187 {
188  switch(command) {
191  default:
193  }
194 }
195 
197 {
198  hotkey::HOTKEY_COMMAND command = cmd.id;
199  bool res = true;
200  switch (command){
201 
202  case hotkey::HOTKEY_WML:
203  {
204  int prefixlen = wml_menu_hotkey_prefix.length();
205  if(cmd.command.compare(0, prefixlen, wml_menu_hotkey_prefix) != 0) {
206  return false;
207  }
208 
210  if(!item) {
211  return false;
212  }
213 
214  return !item->is_synced() || play_controller_.can_use_synced_wml_menu();
215  }
220  return !browse() && !linger() && !events::commands_disabled;
224  return (!browse() || whiteboard_manager_->is_active()) && !linger() && !events::commands_disabled;
226  return (!browse() || linger()) && !events::commands_disabled;
227 
229  return !linger()
235  return !linger()
239  && viewing_team().auto_shroud_updates() == false;
240 
241  // Commands we can only do if in debug mode
246 
248  res = !is_observer();
249  break;
255  && !is_observer()
256  && (!label || !label->immutable());
257  break;
258  }
261  return false;
262 
264  && (menu_handler_.current_unit()->move_interrupted()))
265  return true;
267  if (!i.valid()) return false;
268  return i->move_interrupted();
269  }
271  return !is_observer();
274  return whiteboard_manager_->can_enable_execution_hotkeys();
276  return whiteboard_manager_->can_enable_modifier_hotkeys();
279  return whiteboard_manager_->can_enable_reorder_hotkeys();
281  {
282  //@todo re-enable this once we figure out a decent UI for suppose_dead
283  //@todo when re-enabling this, change 'true' to 'false' in master_hotkey_list for this hotkey
284  return false;
285  }
286 
300  default:
302  }
303  return res;
304 }
305 
307 {
309  {
311  std::string error_log;
312  savegame::read_save_file(filesystem::get_saves_dir(), filename, savegame, &error_log);
313 
314  if(!error_log.empty() || savegame.child_or_empty("snapshot")["replay_pos"].to_int(-1) < 0 ) {
315  gui2::show_error_message(_("The file you have tried to load is corrupt: '") + error_log);
316  return;
317  }
318  std::shared_ptr<config> res(new config(savegame.child_or_empty("snapshot")));
319  std::shared_ptr<config> stats(new config(savegame.child_or_empty("statistics")));
320  throw reset_gamestate_exception(res, stats, true);
321  }
322  else
323  {
325  }
326 }
327 
329 {
332  }
334 }
virtual bool can_execute_command(const hotkey::hotkey_command &command, int index=-1) const override
Check if a command can be executed.
void label_terrain(mouse_handler &mousehandler, bool team_only)
const team & viewing_team() const
void delete_upcoming_commands()
Definition: replay.cpp:184
HOTKEY_COMMAND id
the names are strange: the "hotkey::HOTKEY_COMMAND" is named id, and the string to identify the objec...
virtual void toggle_shroud_updates() override
std::shared_ptr< wb::manager > whiteboard_manager_
std::map< std::string, t_string > string_map
This class represents a single unit of a specific type.
Definition: unit.hpp:99
game_events::wmi_manager & get_wml_menu_items()
Definition: game_state.cpp:403
virtual void load_autosave(const std::string &filename) override
bool at_end() const
Definition: replay.cpp:627
const unit_map & get_units_const() const
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.
bool on_board(const map_location &loc) const
Tell if a location is on the map.
Definition: map.cpp:377
virtual void whiteboard_suppose_dead() override
void create_unit(mouse_handler &mousehandler)
Creates a unit (in debug mode via hotkey or context menu).
std::string get_saves_dir()
void read_save_file(const std::string &dir, const std::string &name, config &cfg, std::string *error_log)
Read the complete config information out of a savefile.
Definition: save_index.cpp:296
virtual bool can_execute_command(const hotkey::hotkey_command &command, int index=-1) const override
Check if a command can be executed.
item_ptr get_item(const std::string &id) const
Gets the menu item with the specified ID.
play_controller & play_controller_
References to parent object / constituents.
An extension of play_controller::hotkey_handler, which has support for SP wesnoth features like white...
replay_controller * get_replay_controller() const override
bool uses_fog() const
Definition: team.hpp:319
map_location get_selected_hex() const
std::string get_names(const std::string &id)
Returns a comma-separated string of hotkey names.
virtual hotkey::ACTION_STATE get_action_state(hotkey::HOTKEY_COMMAND command, int index) const override
const map_location & get_last_hex() const
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:91
void toggle_shroud_updates(int side_num)
bool uses_shroud() const
Definition: team.hpp:318
Applies the planned unit map for the duration of the struct&#39;s life.
Definition: manager.hpp:251
virtual void whiteboard_bump_down_action() override
void recall(int side_num, const map_location &last_hex)
events::menu_handler & menu_handler_
std::shared_ptr< wml_menu_item > item_ptr
wml_menu_item pointers
Definition: wmi_manager.hpp:42
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:28
Declarations for a container for wml_menu_item.
const terrain_label * get_label(const map_location &loc, const std::string &team_name) const
Definition: label.hpp:47
static const std::string wml_menu_hotkey_prefix
const color_t NORMAL_COLOR
bool is_observer() const
Encapsulates the map of the game.
Definition: location.hpp:42
bool auto_shroud_updates() const
Definition: team.hpp:338
bool shrouded(const map_location &loc) const
Returns true if location (x,y) is covered in shroud.
Definition: display.cpp:734
unit_iterator find(std::size_t id)
Definition: map.cpp:311
virtual bool is_networked_mp() const
virtual void load_autosave(const std::string &filename)
void continue_move(mouse_handler &mousehandler, int side_num)
std::size_t i
Definition: function.cpp:933
bool can_execute_command(const hotkey::hotkey_command &cmd, int index) const
void announce(const std::string &msg, const color_t &color=font::GOOD_COLOR, const announce_options &options=announce_options())
Announce a message prominently.
Definition: display.cpp:1771
std::string command
The command is unique.
void unit_hold_position(mouse_handler &mousehandler, int side_num)
bool can_use_synced_wml_menu() const
void change_side(mouse_handler &mousehandler)
Declarations for File-IO.
events::mouse_handler & mouse_handler_
const bool & debug
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:71
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
void kill_unit(mouse_handler &mousehandler)
bool is_local() const
Definition: team.hpp:261
bool is_replay() const
hotkey_handler(playsingle_controller &, saved_game &)
bool is_local_human() const
Definition: team.hpp:267
To store label data Class implements logic for rendering.
Definition: label.hpp:107
unit_map::iterator current_unit()
const map_location & get_location() const
The current map location this unit is at.
Definition: unit.hpp:1301
virtual hotkey::ACTION_STATE get_action_state(hotkey::HOTKEY_COMMAND command, int index) const override
virtual void whiteboard_bump_up_action() override
int current_side() const
Returns the number of the side whose turn it is.
const gamemap & get_map_const() const
virtual void whiteboard_execute_all_actions() override
game_display * gui() const
virtual void whiteboard_delete_action() override
void show_error_message(const std::string &msg, bool message_use_markup)
Shows an error message to the user.
Definition: message.cpp:205
map_labels & labels()
Definition: display.cpp:2517
const config & child_or_empty(config_key_type key) const
Returns the first child with the given key, or an empty config if there is none.
Definition: config.cpp:453
bool immutable() const
Definition: label.hpp:171
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
bool valid() const
Definition: map.hpp:276
virtual void label_terrain(bool) override
void repeat_recruit(int side_num, const map_location &last_hex)
playsingle_controller & playsingle_controller_
virtual void whiteboard_execute_action() override
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:371
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...