The Battle for Wesnoth  1.19.5+dev
undo_action.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2024
3  by David White <dave@whitevine.net>
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 
16 #pragma once
17 
18 #include "utils/optional_fwd.hpp"
19 #include "map/location.hpp"
20 #include "synced_context.hpp"
21 #include "config.hpp"
22 
23 namespace actions {
24 
25  struct undo_event {
26  utils::optional<int> lua_idx;
29  std::size_t uid1, uid2;
30  std::string id1, id2;
31  undo_event(int fcn_idx, const config& args, const game_events::queued_event& ctx);
32  undo_event(const config& cmds, const game_events::queued_event& ctx);
33  undo_event(const config& first, const config& second, const config& weapons, const config& cmds);
34  };
35 
36  /**
37  * Records information to be able to undo an action.
38  * Each type of action gets its own derived type.
39  * Base class for all entries in the undo stack, also contains non undoable actions like update_shroud or auto_shroud.
40  */
42  {
45 
46  /**
47  * Default constructor.
48  * This is the only way to get nullptr view_info.
49  */
51  { }
52  // Virtual destructor to support derived classes.
53  virtual ~undo_action_base() {}
54 
55  /** Writes this into the provided config. */
56  virtual void write(config & cfg) const
57  {
58  cfg["type"] = this->get_type();
59  }
60 
61  virtual const char* get_type() const = 0;
62  };
63 
64  /** actions that are undoable (this does not include update_shroud and auto_shroud) */
66  {
67  /**
68  * Default constructor.
69  * It is assumed that undo actions are constructed after the action is performed
70  * so that the unit id diff does not change after this constructor
71  */
72  undo_action();
73  undo_action(const config& cfg);
74  // Virtual destructor to support derived classes.
75  virtual ~undo_action() {}
76 
77  /** Writes this into the provided config. */
78  virtual void write(config & cfg) const;
79 
80  /**
81  * Undoes this action.
82  * @return true on success; false on an error.
83  */
84  virtual bool undo(int side) = 0;
85  /**
86  * the difference in the unit ids
87  * TODO: does it really make sense to allow undoing if the unit id counter has changed?
88  */
90  /** actions wml (specified by wml) that should be executed when undoing this command. */
91  typedef std::vector<undo_event> event_vector;
93  void execute_undo_umc_wml();
94 
95  static void read_event_vector(event_vector& vec, const config& cfg, const std::string& tag);
96  static void write_event_vector(const event_vector& vec, config& cfg, const std::string& tag);
97  };
98 
99  /** entry for player actions that do not need any special code to be performed when undoing such as right-click menu items. */
101  {
103  : undo_action()
104  {
105  }
106  explicit undo_dummy_action (const config & cfg)
107  : undo_action(cfg)
108  {
109  }
110  virtual const char* get_type() const { return "dummy"; }
111  virtual ~undo_dummy_action () {}
112  /** Undoes this action. */
113  virtual bool undo(int)
114  {
116  return true;
117  }
118  };
119 
120 }
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
Definitions for the interface to Wesnoth Markup Language (WML).
std::string tag(const std::string &tag_name, Args &&... contents)
Definition: markup.hpp:45
Records information to be able to undo an action.
Definition: undo_action.hpp:42
virtual const char * get_type() const =0
undo_action_base(const undo_action_base &)=delete
undo_action_base()
Default constructor.
Definition: undo_action.hpp:50
virtual void write(config &cfg) const
Writes this into the provided config.
Definition: undo_action.hpp:56
undo_action_base & operator=(const undo_action_base &)=delete
actions that are undoable (this does not include update_shroud and auto_shroud)
Definition: undo_action.hpp:66
event_vector umc_commands_undo
Definition: undo_action.hpp:92
virtual void write(config &cfg) const
Writes this into the provided config.
std::vector< undo_event > event_vector
actions wml (specified by wml) that should be executed when undoing this command.
Definition: undo_action.hpp:91
undo_action()
Default constructor.
Definition: undo_action.cpp:87
static void read_event_vector(event_vector &vec, const config &cfg, const std::string &tag)
virtual bool undo(int side)=0
Undoes this action.
static void write_event_vector(const event_vector &vec, config &cfg, const std::string &tag)
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:89
entry for player actions that do not need any special code to be performed when undoing such as right...
virtual bool undo(int)
Undoes this action.
undo_dummy_action(const config &cfg)
virtual const char * get_type() const
map_location loc1
Definition: undo_action.hpp:28
map_location loc2
Definition: undo_action.hpp:28
map_location filter_loc2
Definition: undo_action.hpp:28
undo_event(int fcn_idx, const config &args, const game_events::queued_event &ctx)
Definition: undo_action.cpp:32
utils::optional< int > lua_idx
Definition: undo_action.hpp:26
map_location filter_loc1
Definition: undo_action.hpp:28
Encapsulates the map of the game.
Definition: location.hpp:45