The Battle for Wesnoth  1.19.5+dev
hotkey_handler_sp.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2024
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"
21 #include "gui/dialogs/message.hpp"
23 #include "hotkey/hotkey_item.hpp"
24 #include "map/label.hpp"
25 #include "play_controller.hpp"
27 #include "whiteboard/manager.hpp"
30 #include "map/map.hpp"
31 #include "save_index.hpp"
32 #include "saved_game.hpp"
33 #include "resources.hpp"
34 #include "replay.hpp"
35 
36 #include "units/unit.hpp"
37 
38 #include <boost/algorithm/string/predicate.hpp>
39 
40 namespace balg = boost::algorithm;
41 
44  , playsingle_controller_(pc)
45  , whiteboard_manager_(pc.get_whiteboard())
46 {}
47 
49 
50 bool playsingle_controller::hotkey_handler::is_observer() const { return playsingle_controller_.is_observer(); }
51 
53  if (!browse())
54  menu_handler_.recruit(play_controller_.current_side(), mouse_handler_.get_last_hex());
55  else if (whiteboard_manager_->is_active())
56  menu_handler_.recruit(gui()->viewing_team().side(), mouse_handler_.get_last_hex());
57 }
58 
60  if (!browse())
61  menu_handler_.repeat_recruit(play_controller_.current_side(), mouse_handler_.get_last_hex());
62  else if (whiteboard_manager_->is_active())
63  menu_handler_.repeat_recruit(gui()->viewing_team().side(), mouse_handler_.get_last_hex());
64 }
65 
67  if (!browse())
68  menu_handler_.recall(play_controller_.current_side(), mouse_handler_.get_last_hex());
69  else if (whiteboard_manager_->is_active())
70  menu_handler_.recall(gui()->viewing_team().side(), mouse_handler_.get_last_hex());
71 }
72 
74  menu_handler_.toggle_shroud_updates(gui()->viewing_team().side());
75 }
76 
78  menu_handler_.update_shroud_now(gui()->viewing_team().side());
79 }
80 
82  playsingle_controller_.end_turn();
83 }
84 
87 }
88 
91 }
92 
95 }
96 
99 }
100 
103 }
104 
107 }
108 
111 }
112 
114  menu_handler_.continue_move(mouse_handler_, play_controller_.current_side());
115 }
116 
118  if (!browse())
119  menu_handler_.unit_hold_position(mouse_handler_, play_controller_.current_side());
120 }
121 
123  if (!browse())
124  menu_handler_.end_unit_turn(mouse_handler_, play_controller_.current_side());
125 }
126 
129 }
130 
133 }
134 
137 }
138 
141 }
142 
145 }
146 
148  whiteboard_manager_->set_active(!whiteboard_manager_->is_active());
149 
150  if (whiteboard_manager_->is_active()) {
152  utils::string_map symbols;
153  symbols["hotkey"] = hk;
154 
155  gui()->announce(_("Planning mode activated!") + std::string("\n") + VGETTEXT("(press $hotkey to deactivate)", symbols), font::NORMAL_COLOR);
156  } else {
157  gui()->announce(_("Planning mode deactivated!"), font::NORMAL_COLOR);
158  }
159  //@todo Stop printing whiteboard help in the chat once we have better documentation/help
160  whiteboard_manager_->print_help_once();
161 }
162 
164  whiteboard_manager_->contextual_execute();
165 }
166 
168  whiteboard_manager_->execute_all_actions();
169 }
170 
172  whiteboard_manager_->contextual_delete();
173 }
174 
176 {
177  whiteboard_manager_->contextual_bump_up_action();
178 }
179 
181 {
182  whiteboard_manager_->contextual_bump_down_action();
183 }
184 
186 {
187  unit* curr_unit;
188  map_location loc;
189  { wb::future_map future; //start planned unit map scope
190  curr_unit = &*menu_handler_.current_unit();
191  loc = curr_unit->get_location();
192  } // end planned unit map scope
193  whiteboard_manager_->save_suppose_dead(*curr_unit,loc);
194 }
195 
197 {
198  switch(cmd.hotkey_command) {
201  default:
203  }
204 }
205 
207 {
209  bool res = true;
210  int prefixlen = wml_menu_hotkey_prefix.length();
211  switch (command){
212  case hotkey::HOTKEY_NULL:
213  case hotkey::HOTKEY_WML:
214  {
215  if(cmd.id.compare(0, prefixlen, wml_menu_hotkey_prefix) == 0) {
216  game_events::wmi_manager::item_ptr item = gamestate().get_wml_menu_items().get_item(std::string(cmd.id.substr(prefixlen)));
217  if(!item) {
218  return false;
219  }
220  return !item->is_synced() || play_controller_.can_use_synced_wml_menu();
221  }
223 
224  }
226  return !events::commands_disabled || (playsingle_controller_.is_replay() && events::commands_disabled < 2);
229  return !browse() && !linger() && !events::commands_disabled;
233  return (!browse() || whiteboard_manager_->is_active()) && !linger() && !events::commands_disabled;
235  //playmp_controller::hotkey_handler checks whether we are the host.
236  return (!browse() || linger()) && !events::commands_disabled;
237 
239  return !linger()
240  && (gui()->viewing_team().uses_fog() || gui()->viewing_team().uses_shroud())
241  && gui()->viewing_team_is_playing()
242  && gui()->viewing_team().is_local_human()
245  return !linger()
246  && gui()->viewing_team_is_playing()
247  && gui()->viewing_team().is_local_human()
249  && gui()->viewing_team().auto_shroud_updates() == false;
250 
251  // Commands we can only do if in debug mode
256  return !events::commands_disabled && game_config::debug && play_controller_.get_map().on_board(mouse_handler_.get_last_hex()) && play_controller_.current_team().is_local();
257 
259  res = !is_observer();
260  break;
263  const terrain_label *label = gui()->labels().get_label(mouse_handler_.get_last_hex());
264  res = !events::commands_disabled && play_controller_.get_map().on_board(mouse_handler_.get_last_hex())
265  && !gui()->shrouded(mouse_handler_.get_last_hex())
266  && !is_observer()
267  && (!label || !label->immutable());
268  break;
269  }
271  if(browse() || events::commands_disabled)
272  return false;
273 
275  && (menu_handler_.current_unit()->move_interrupted()))
276  return true;
277  const unit_map::const_iterator i = play_controller_.get_units().find(mouse_handler_.get_selected_hex());
278  if (!i.valid()) return false;
279  return i->move_interrupted();
280  }
282  return !is_observer();
285  return whiteboard_manager_->can_enable_execution_hotkeys() && !events::commands_disabled && !browse();
287  return whiteboard_manager_->can_enable_modifier_hotkeys();
290  return whiteboard_manager_->can_enable_reorder_hotkeys();
292  {
293  //@todo re-enable this once we figure out a decent UI for suppose_dead
294  //@todo when re-enabling this, change 'true' to 'false' in master_hotkey_list for this hotkey
295  return false;
296  }
297 
308  return playsingle_controller_.get_replay_controller() && playsingle_controller_.get_replay_controller()->can_execute_command(cmd);
310  return playsingle_controller_.is_replay() && (!playsingle_controller_.is_networked_mp() || resources::recorder->at_end());
311  default:
313  }
314  return res;
315 }
316 
317 void playsingle_controller::hotkey_handler::load_autosave(const std::string& filename, bool start_replay)
318 {
319  if(!start_replay) {
321  }
322  auto invalid_save_file = [this, filename](std::string msg){
323  if(playsingle_controller_.is_networked_mp()) {
325  } else {
326  const int res = gui2::show_message("", msg + _("Do you want to load it anyway?"), gui2::dialogs::message::yes_no_buttons);
327  if(res != gui2::retval::CANCEL) {
329  }
330  }
331  };
332 
334  std::string error_log;
336 
337  if(!error_log.empty()) {
338  invalid_save_file(_("The file you have tried to load is corrupt: '") + error_log);
339  return;
340  }
341  if(savegame.child_or_empty("snapshot")["replay_pos"].to_int(-1) < 0 ) {
342  invalid_save_file(_("The file you have tried to load has no replay information. "));
343  return;
344  }
345  if(!playsingle_controller_.get_saved_game().get_replay().is_ancestor(savegame.child_or_empty("replay"))) {
346  invalid_save_file(_("The file you have tried to load is not from the current session."));
347  return;
348  }
349 
350  auto res = std::make_shared<config>(savegame.child_or_empty("snapshot"));
351  auto stats = std::make_shared<config>(savegame.child_or_empty("statistics"));
352  throw reset_gamestate_exception(res, stats, false);
353 }
354 
356 {
357  if(!playsingle_controller_.is_networked_mp()) {
359  }
360  playsingle_controller_.set_player_type_changed();
361 }
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
void label_terrain(mouse_handler &mousehandler, bool team_only)
void update_shroud_now(int side_num)
virtual void clear_messages() override
void recall(int side_num, const map_location &last_hex)
void create_unit(mouse_handler &mousehandler)
Creates a unit (in debug mode via hotkey or context menu).
void unit_hold_position(mouse_handler &mousehandler, int side_num)
void end_unit_turn(mouse_handler &mousehandler, int side_num)
void change_side(mouse_handler &mousehandler)
void toggle_shroud_updates(int side_num)
void recruit(int side_num, const map_location &last_hex)
void continue_move(mouse_handler &mousehandler, int side_num)
void repeat_recruit(int side_num, const map_location &last_hex)
void kill_unit(mouse_handler &mousehandler)
unit_map::iterator current_unit()
map_location get_selected_hex() const
const map_location & get_last_hex() const
std::shared_ptr< wml_menu_item > item_ptr
wml_menu_item pointers
Definition: wmi_manager.hpp:44
item_ptr get_item(const std::string &id) const
Gets the menu item with the specified ID.
game_events::wmi_manager & get_wml_menu_items()
Definition: game_state.cpp:383
@ yes_no_buttons
Shows a yes and no button.
Definition: message.hpp:81
virtual void load_autosave(const std::string &filename, bool start_replay=false)
virtual hotkey::ACTION_STATE get_action_state(const hotkey::ui_command &) const override
virtual bool can_execute_command(const hotkey::ui_command &command) const override
Check if a command can be executed.
events::menu_handler menu_handler_
bool is_observer() const
game_state & gamestate()
events::mouse_handler mouse_handler_
std::shared_ptr< wb::manager > whiteboard_manager_
virtual void toggle_shroud_updates() override
virtual void whiteboard_suppose_dead() override
hotkey_handler(playsingle_controller &, saved_game &)
virtual bool can_execute_command(const hotkey::ui_command &cmd) const override
Check if a command can be executed.
virtual void whiteboard_delete_action() override
virtual void whiteboard_execute_action() override
virtual void label_terrain(bool) override
virtual void load_autosave(const std::string &filename, bool start_replay=false) override
virtual void whiteboard_bump_up_action() override
virtual hotkey::ACTION_STATE get_action_state(const hotkey::ui_command &) const override
virtual void whiteboard_bump_down_action() override
virtual void whiteboard_execute_all_actions() override
bool at_end() const
Definition: replay.cpp:646
void delete_upcoming_commands()
Definition: replay.cpp:189
To store label data Class implements logic for rendering.
Definition: label.hpp:111
This class represents a single unit of a specific type.
Definition: unit.hpp:133
Declarations for File-IO.
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
std::size_t i
Definition: function.cpp:1028
static std::string _(const char *str)
Definition: gettext.hpp:93
const map_location & get_location() const
The current map location this unit is at.
Definition: unit.hpp:1404
std::string label
What to show in the filter's drop-down list.
Definition: manager.cpp:200
An extension of play_controller::hotkey_handler, which has support for SP wesnoth features like white...
Declarations for a class that implements WML-defined (right-click) menu items.
std::string get_saves_dir()
const color_t NORMAL_COLOR
const bool & debug
Definition: game_config.cpp:94
void show_error_message(const std::string &msg, bool message_use_markup)
Shows an error message to the user.
Definition: message.cpp:201
void show_message(const std::string &title, const std::string &msg, const std::string &button_caption, const bool auto_close, const bool message_use_markup, const bool title_use_markup)
Shows a message to the user.
Definition: message.cpp:148
@ CANCEL
Dialog was closed with the CANCEL button.
Definition: retval.hpp:38
General purpose widgets.
std::string get_names(const std::string &id)
Returns a comma-separated string of hotkey names.
@ HOTKEY_TELEPORT_UNIT
@ HOTKEY_END_UNIT_TURN
@ HOTKEY_WB_EXECUTE_ALL_ACTIONS
@ HOTKEY_WB_SUPPOSE_DEAD
@ HOTKEY_REPLAY_PLAY
@ HOTKEY_SAVE_GAME
@ HOTKEY_UPDATE_SHROUD
@ HOTKEY_REPEAT_RECRUIT
@ HOTKEY_REPLAY_STOP
@ HOTKEY_KILL_UNIT
@ HOTKEY_LABEL_TEAM_TERRAIN
@ HOTKEY_UNIT_HOLD_POSITION
@ HOTKEY_REPLAY_NEXT_TURN
@ HOTKEY_REPLAY_SHOW_EVERYTHING
@ HOTKEY_REPLAY_SHOW_TEAM1
@ HOTKEY_LABEL_TERRAIN
@ HOTKEY_CLEAR_LABELS
@ HOTKEY_CONTINUE_MOVE
@ HOTKEY_WB_EXECUTE_ACTION
@ HOTKEY_CHANGE_SIDE
@ HOTKEY_DELAY_SHROUD
@ HOTKEY_WB_BUMP_UP_ACTION
@ HOTKEY_CREATE_UNIT
@ HOTKEY_REPLAY_EXIT
@ HOTKEY_REPLAY_NEXT_SIDE
@ HOTKEY_REPLAY_NEXT_MOVE
@ HOTKEY_REPLAY_RESET
@ HOTKEY_WB_TOGGLE
@ HOTKEY_REPLAY_SKIP_ANIMATION
@ HOTKEY_WB_BUMP_DOWN_ACTION
@ HOTKEY_REPLAY_SHOW_EACH
@ HOTKEY_WB_DELETE_ACTION
replay * recorder
Definition: resources.cpp:28
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:311
std::map< std::string, t_string > string_map
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
Replay control code.
std::string filename
Filename.
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...
Used as the main paramneter for can_execute_command/do_execute_command These functions are used to ex...
hotkey::HOTKEY_COMMAND hotkey_command
The hotkey::HOTKEY_COMMAND associated with this action, HOTKEY_NULL for actions that don't allow hotk...
std::string id
The string command, never empty, describes the action uniquely. when the action is the result of a me...
Encapsulates the map of the game.
Definition: location.hpp:45
bool valid() const
Definition: map.hpp:273
Applies the planned unit map for the duration of the struct's life.
Definition: manager.hpp:253
Declarations for a container for wml_menu_item.