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