The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
undo_action.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "vision.hpp"
4 #include "map/location.hpp"
5 #include "units/ptr.hpp"
6 #include "synced_context.hpp"
7 #include "game_events/pump.hpp" // for queued_event
8 #include "config.hpp"
9 
10 #include <boost/noncopyable.hpp>
11 #include <boost/optional.hpp>
12 
13 namespace actions {
14  class undo_list;
15 
16  struct undo_event {
19  size_t uid1, uid2;
21  undo_event(const config& cmds, const game_events::queued_event& ctx);
22  undo_event(const config& first, const config& second, const config& weapons, const config& cmds);
23  };
24 
25  /// Records information to be able to undo an action.
26  /// Each type of action gets its own derived type.
27  /// Base class for all entries in the undo stack, also contains non undoable actions like update_shroud or auto_shroud.
28  struct undo_action_base : boost::noncopyable
29  {
30  /// Default constructor.
31  /// This is the only way to get nullptr view_info.
33  { }
34  // Virtual destructor to support derived classes.
35  virtual ~undo_action_base() {}
36 
37  /// Writes this into the provided config.
38  virtual void write(config & cfg) const
39  {
40  cfg["type"] = this->get_type();
41  }
42 
43  virtual const char* get_type() const = 0;
44  };
45 
46  /// actions that are undoable (this does not include update_shroud and auto_shroud)
48  {
49  /// Default constructor.
50  /// It is assumed that undo actions are contructed after the action is performed
51  /// so that the unit id diff does not change after this contructor.
52  undo_action();
53  undo_action(const config& cfg);
54  // Virtual destructor to support derived classes.
55  virtual ~undo_action() {}
56 
57  /// Writes this into the provided config.
58  virtual void write(config & cfg) const;
59 
60  /// Undoes this action.
61  /// @return true on success; false on an error.
62  virtual bool undo(int side) = 0;
63  /// Redoes this action.
64  /// @return true on success; false on an error.
65  virtual bool redo(int side) = 0;
66  /// the replay data to do this action, this is only !empty() when this action is on the redo stack
67  /// we need this because we don't recalculate the redos like they would be in real game,
68  /// but even undoable commands can have "dependent" (= user_input) commands, which we save here.
70  /// the difference in the unit ids
71  /// TODO: does it really make sense to allow undoing if the unit id counter has changed?
73  /// actions wml (specified by wml) that should be executed when undoing this command.
74  typedef std::vector<undo_event> event_vector;
75  event_vector umc_commands_undo;
76  event_vector umc_commands_redo;
77  void execute_undo_umc_wml();
78  void execute_redo_umc_wml();
79 
80  static void read_event_vector(event_vector& vec, const config& cfg, const std::string& tag);
81  static void write_event_vector(const event_vector& vec, config& cfg, const std::string& tag);
82  };
83 
84  /// entry for player actions that do not need any special code to be performed when undoing such as right-click menu items.
86  {
88  : undo_action()
89  {
90  }
91  explicit undo_dummy_action (const config & cfg)
92  : undo_action(cfg)
93  {
94  }
95  virtual const char* get_type() const { return "dummy"; }
96  virtual ~undo_dummy_action () {};
97  /// Undoes this action.
98  virtual bool undo(int)
99  {
100  return true;
101  }
102  /// Redoes this action.
103  virtual bool redo(int)
104  {
105  return true;
106  }
107  };
108 
109 }
virtual void write(config &cfg) const
Writes this into the provided config.
Definition: undo_action.hpp:38
map_location filter_loc1
Definition: undo_action.hpp:18
map_location loc1
Definition: undo_action.hpp:18
Various functions implementing vision (through fog of war and shroud).
undo_event(const config &cmds, const game_events::queued_event &ctx)
Definition: undo_action.cpp:15
virtual bool undo(int)
Undoes this action.
Definition: undo_action.hpp:98
virtual const char * get_type() const =0
Definitions for the interface to Wesnoth Markup Language (WML).
Records information to be able to undo an action.
Definition: undo_action.hpp:28
event_vector umc_commands_undo
Definition: undo_action.hpp:75
std::vector< undo_event > event_vector
actions wml (specified by wml) that should be executed when undoing this command. ...
Definition: undo_action.hpp:74
static void read_event_vector(event_vector &vec, const config &cfg, const std::string &tag)
virtual const char * get_type() const
Definition: undo_action.hpp:95
event_vector umc_commands_redo
Definition: undo_action.hpp:76
undo_dummy_action(const config &cfg)
Definition: undo_action.hpp:91
virtual bool redo(int)
Redoes this action.
Encapsulates the map of the game.
Definition: location.hpp:38
virtual void write(config &cfg) const
Writes this into the provided config.
map_location loc2
Definition: undo_action.hpp:18
entry for player actions that do not need any special code to be performed when undoing such as right...
Definition: undo_action.hpp:85
static void write_event_vector(const event_vector &vec, config &cfg, const std::string &tag)
virtual bool redo(int side)=0
Redoes this action.
Define the game's event mechanism.
undo_action()
Default constructor.
Definition: undo_action.cpp:49
undo_action_base()
Default constructor.
Definition: undo_action.hpp:32
GLint * first
Definition: glew.h:1496
map_location filter_loc2
Definition: undo_action.hpp:18
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
int unit_id_diff
the difference in the unit ids TODO: does it really make sense to allow undoing if the unit id counte...
Definition: undo_action.hpp:72
config replay_data
the replay data to do this action, this is only !empty() when this action is on the redo stack we nee...
Definition: undo_action.hpp:69
GLsizei const GLcharARB ** string
Definition: glew.h:4503
virtual bool undo(int side)=0
Undoes this action.
actions that are undoable (this does not include update_shroud and auto_shroud)
Definition: undo_action.hpp:47