The Battle for Wesnoth  1.15.1+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 "font/standard_colors.hpp"
18 #include "formula/string_utils.hpp"
20 #include "hotkey/hotkey_item.hpp"
21 #include "map/label.hpp"
22 #include "play_controller.hpp"
24 #include "whiteboard/manager.hpp"
27 #include "map/map.hpp"
28 #include "save_index.hpp"
29 #include "gui/dialogs/message.hpp"
30 #include "resources.hpp"
31 #include "replay.hpp"
32 
33 #include "units/unit.hpp"
34 
37  , playsingle_controller_(pc)
38  , whiteboard_manager_(pc.get_whiteboard())
39 {}
40 
42 
44 
46  if (!browse())
48  else if (whiteboard_manager_->is_active())
49  menu_handler_.recruit(gui()->viewing_side(), mouse_handler_.get_last_hex());
50 }
51 
53  if (!browse())
55  else if (whiteboard_manager_->is_active())
57 }
58 
60  if (!browse())
62  else if (whiteboard_manager_->is_active())
63  menu_handler_.recall(gui()->viewing_side(), mouse_handler_.get_last_hex());
64 }
65 
68 }
69 
72 }
73 
76 }
77 
80 }
81 
84 }
85 
88 }
89 
92 }
93 
96 }
97 
100 }
101 
104 }
105 
107  if (!browse())
109 }
110 
112  if (!browse())
114 }
115 
118 }
119 
122 }
123 
126 }
127 
130 }
131 
134 }
135 
137  whiteboard_manager_->set_active(!whiteboard_manager_->is_active());
138 
139  if (whiteboard_manager_->is_active()) {
141  utils::string_map symbols;
142  symbols["hotkey"] = hk;
143 
144  gui()->announce(_("Planning mode activated!") + std::string("\n") + VGETTEXT("(press $hotkey to deactivate)", symbols), font::NORMAL_COLOR);
145  } else {
146  gui()->announce(_("Planning mode deactivated!"), font::NORMAL_COLOR);
147  }
148  //@todo Stop printing whiteboard help in the chat once we have better documentation/help
149  whiteboard_manager_->print_help_once();
150 }
151 
153  whiteboard_manager_->contextual_execute();
154 }
155 
157  whiteboard_manager_->execute_all_actions();
158 }
159 
161  whiteboard_manager_->contextual_delete();
162 }
163 
165 {
166  whiteboard_manager_->contextual_bump_up_action();
167 }
168 
170 {
171  whiteboard_manager_->contextual_bump_down_action();
172 }
173 
175 {
176  unit* curr_unit;
177  map_location loc;
178  { wb::future_map future; //start planned unit map scope
179  curr_unit = &*menu_handler_.current_unit();
180  loc = curr_unit->get_location();
181  } // end planned unit map scope
182  whiteboard_manager_->save_suppose_dead(*curr_unit,loc);
183 }
184 
186 {
187  switch(command) {
190  default:
192  }
193 }
194 
196 {
197  hotkey::HOTKEY_COMMAND command = cmd.id;
198  bool res = true;
199  switch (command){
200 
201  case hotkey::HOTKEY_WML:
202  {
203  int prefixlen = wml_menu_hotkey_prefix.length();
204  if(cmd.command.compare(0, prefixlen, wml_menu_hotkey_prefix) != 0) {
205  return false;
206  }
207 
209  if(!item) {
210  return false;
211  }
212 
213  return !item->is_synced() || play_controller_.can_use_synced_wml_menu();
214  }
219  return !browse() && !linger() && !events::commands_disabled;
223  return (!browse() || whiteboard_manager_->is_active()) && !linger() && !events::commands_disabled;
225  return (!browse() || linger()) && !events::commands_disabled;
226 
228  return !linger()
234  return !linger()
238  && viewing_team().auto_shroud_updates() == false;
239 
240  // Commands we can only do if in debug mode
245 
247  res = !is_observer();
248  break;
254  && !is_observer()
255  && (!label || !label->immutable());
256  break;
257  }
260  return false;
261 
263  && (menu_handler_.current_unit()->move_interrupted()))
264  return true;
266  if (!i.valid()) return false;
267  return i->move_interrupted();
268  }
270  return !is_observer();
273  return whiteboard_manager_->can_enable_execution_hotkeys();
275  return whiteboard_manager_->can_enable_modifier_hotkeys();
278  return whiteboard_manager_->can_enable_reorder_hotkeys();
280  {
281  //@todo re-enable this once we figure out a decent UI for suppose_dead
282  return false;
283  }
284 
298  default: return play_controller::hotkey_handler::can_execute_command(cmd, index);
299  }
300  return res;
301 }
302 
304 {
306  {
308  std::string error_log;
309  savegame::read_save_file(filename, savegame, &error_log);
310 
311  if(!error_log.empty() || savegame.child_or_empty("snapshot")["replay_pos"].to_int(-1) < 0 ) {
312  gui2::show_error_message(_("The file you have tried to load is corrupt: '") + error_log);
313  return;
314  }
315  std::shared_ptr<config> res(new config(savegame.child_or_empty("snapshot")));
316  std::shared_ptr<config> stats(new config(savegame.child_or_empty("statistics")));
317  throw reset_gamestate_exception(res, stats, true);
318  }
319  else
320  {
322  }
323 }
324 
326 {
329  }
331 }
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:183
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_
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:270
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:626
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).
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:317
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:316
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:336
bool shrouded(const map_location &loc) const
Returns true if location (x,y) is covered in shroud.
Definition: display.cpp:711
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:1748
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)
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:260
bool is_replay() const
hotkey_handler(playsingle_controller &, saved_game &)
bool is_local_human() const
Definition: team.hpp:266
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:1258
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:2494
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:92
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...