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