The Battle for Wesnoth  1.17.0-dev
hotkey_handler_sp.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2021
3  by Chris Beck <render787@gmail.com>
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 
17 
18 #include "filesystem.hpp" // for get_saves_dir()
19 #include "font/standard_colors.hpp"
20 #include "formula/string_utils.hpp"
22 #include "hotkey/hotkey_item.hpp"
23 #include "map/label.hpp"
24 #include "play_controller.hpp"
26 #include "whiteboard/manager.hpp"
29 #include "map/map.hpp"
30 #include "save_index.hpp"
31 #include "gui/dialogs/message.hpp"
32 #include "resources.hpp"
33 #include "replay.hpp"
34 
35 #include "units/unit.hpp"
36 
39  , playsingle_controller_(pc)
40  , whiteboard_manager_(pc.get_whiteboard())
41 {}
42 
44 
46 
48  if (!browse())
50  else if (whiteboard_manager_->is_active())
51  menu_handler_.recruit(gui()->viewing_side(), mouse_handler_.get_last_hex());
52 }
53 
55  if (!browse())
57  else if (whiteboard_manager_->is_active())
59 }
60 
62  if (!browse())
64  else if (whiteboard_manager_->is_active())
65  menu_handler_.recall(gui()->viewing_side(), mouse_handler_.get_last_hex());
66 }
67 
70 }
71 
74 }
75 
78 }
79 
82 }
83 
86 }
87 
90 }
91 
94 }
95 
98 }
99 
102 }
103 
106 }
107 
109  if (!browse())
111 }
112 
114  if (!browse())
116 }
117 
120 }
121 
124 }
125 
128 }
129 
132 }
133 
136 }
137 
139  whiteboard_manager_->set_active(!whiteboard_manager_->is_active());
140 
141  if (whiteboard_manager_->is_active()) {
143  utils::string_map symbols;
144  symbols["hotkey"] = hk;
145 
146  gui()->announce(_("Planning mode activated!") + std::string("\n") + VGETTEXT("(press $hotkey to deactivate)", symbols), font::NORMAL_COLOR);
147  } else {
148  gui()->announce(_("Planning mode deactivated!"), font::NORMAL_COLOR);
149  }
150  //@todo Stop printing whiteboard help in the chat once we have better documentation/help
151  whiteboard_manager_->print_help_once();
152 }
153 
155  whiteboard_manager_->contextual_execute();
156 }
157 
159  whiteboard_manager_->execute_all_actions();
160 }
161 
163  whiteboard_manager_->contextual_delete();
164 }
165 
167 {
168  whiteboard_manager_->contextual_bump_up_action();
169 }
170 
172 {
173  whiteboard_manager_->contextual_bump_down_action();
174 }
175 
177 {
178  unit* curr_unit;
179  map_location loc;
180  { wb::future_map future; //start planned unit map scope
181  curr_unit = &*menu_handler_.current_unit();
182  loc = curr_unit->get_location();
183  } // end planned unit map scope
184  whiteboard_manager_->save_suppose_dead(*curr_unit,loc);
185 }
186 
188 {
189  switch(command) {
192  default:
194  }
195 }
196 
198 {
199  hotkey::HOTKEY_COMMAND command = cmd.id;
200  bool res = true;
201  switch (command){
202 
203  case hotkey::HOTKEY_WML:
204  {
205  int prefixlen = wml_menu_hotkey_prefix.length();
206  if(cmd.command.compare(0, prefixlen, wml_menu_hotkey_prefix) != 0) {
207  return false;
208  }
209 
211  if(!item) {
212  return false;
213  }
214 
215  return !item->is_synced() || play_controller_.can_use_synced_wml_menu();
216  }
221  return !browse() && !linger() && !events::commands_disabled;
225  return (!browse() || whiteboard_manager_->is_active()) && !linger() && !events::commands_disabled;
227  return (!browse() || linger()) && !events::commands_disabled;
228 
230  return !linger()
236  return !linger()
240  && viewing_team().auto_shroud_updates() == false;
241 
242  // Commands we can only do if in debug mode
247 
249  res = !is_observer();
250  break;
256  && !is_observer()
257  && (!label || !label->immutable());
258  break;
259  }
262  return false;
263 
265  && (menu_handler_.current_unit()->move_interrupted()))
266  return true;
268  if (!i.valid()) return false;
269  return i->move_interrupted();
270  }
272  return !is_observer();
275  return whiteboard_manager_->can_enable_execution_hotkeys();
277  return whiteboard_manager_->can_enable_modifier_hotkeys();
280  return whiteboard_manager_->can_enable_reorder_hotkeys();
282  {
283  //@todo re-enable this once we figure out a decent UI for suppose_dead
284  //@todo when re-enabling this, change 'true' to 'false' in master_hotkey_list for this hotkey
285  return false;
286  }
287 
301  default:
303  }
304  return res;
305 }
306 
308 {
310  {
312  std::string error_log;
313  savegame::read_save_file(filesystem::get_saves_dir(), filename, savegame, &error_log);
314 
315  if(!error_log.empty() || savegame.child_or_empty("snapshot")["replay_pos"].to_int(-1) < 0 ) {
316  gui2::show_error_message(_("The file you have tried to load is corrupt: '") + error_log);
317  return;
318  }
319  std::shared_ptr<config> res(new config(savegame.child_or_empty("snapshot")));
320  std::shared_ptr<config> stats(new config(savegame.child_or_empty("statistics")));
321  throw reset_gamestate_exception(res, stats, true);
322  }
323  else
324  {
326  }
327 }
328 
330 {
333  }
335 }
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:185
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:121
const unit_map & get_units() const
game_events::wmi_manager & get_wml_menu_items()
Definition: game_state.cpp:415
virtual void load_autosave(const std::string &filename) override
bool at_end() const
Definition: replay.cpp:633
General purpose widgets.
Stores all information related to functions that can be bound to hotkeys.
void update_shroud_now(int side_num)
const gamemap & get_map() const
Replay control code.
virtual void whiteboard_suppose_dead() override
static std::string _(const char *str)
Definition: gettext.hpp:93
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:318
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:331
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
void toggle_shroud_updates(int side_num)
std::string label
What to show in the filter&#39;s drop-down list.
Definition: manager.cpp:217
bool uses_shroud() const
Definition: team.hpp:330
Applies the planned unit map for the duration of the struct&#39;s life.
Definition: manager.hpp:252
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:43
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:29
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:48
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:38
bool auto_shroud_updates() const
Definition: team.hpp:351
bool shrouded(const map_location &loc) const
Returns true if location (x,y) is covered in shroud.
Definition: display.cpp:740
unit_iterator find(std::size_t id)
Definition: map.cpp:310
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:941
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:1780
std::string command
The command is unique.
void unit_hold_position(mouse_handler &mousehandler, int side_num)
bool can_use_synced_wml_menu() const
bool on_board(const map_location &loc) const
Tell if a location is on the map.
Definition: map.cpp:385
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:72
#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:273
bool is_replay() const
hotkey_handler(playsingle_controller &, saved_game &)
bool is_local_human() const
Definition: team.hpp:279
To store label data Class implements logic for rendering.
Definition: label.hpp:110
unit_map::iterator current_unit()
const map_location & get_location() const
The current map location this unit is at.
Definition: unit.hpp:1346
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.
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:206
map_labels & labels()
Definition: display.cpp:2536
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:482
bool immutable() const
Definition: label.hpp:174
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:60
bool valid() const
Definition: map.hpp:274
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:410
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...