The Battle for Wesnoth  1.13.10+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 /*
2  Copyright (C) 2017 the Battle for Wesnoth Project http://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  size_t uid1, uid2;
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  /// Records information to be able to undo an action.
36  /// Each type of action gets its own derived type.
37  /// Base class for all entries in the undo stack, also contains non undoable actions like update_shroud or auto_shroud.
39  {
40  undo_action_base(const undo_action_base&) = delete;
42 
43  /// Default constructor.
44  /// This is the only way to get nullptr view_info.
46  { }
47  // Virtual destructor to support derived classes.
48  virtual ~undo_action_base() {}
49 
50  /// Writes this into the provided config.
51  virtual void write(config & cfg) const
52  {
53  cfg["type"] = this->get_type();
54  }
55 
56  virtual const char* get_type() const = 0;
57  };
58 
59  /// actions that are undoable (this does not include update_shroud and auto_shroud)
61  {
62  /// Default constructor.
63  /// It is assumed that undo actions are contructed after the action is performed
64  /// so that the unit id diff does not change after this contructor.
65  undo_action();
66  undo_action(const config& cfg);
67  // Virtual destructor to support derived classes.
68  virtual ~undo_action() {}
69 
70  /// Writes this into the provided config.
71  virtual void write(config & cfg) const;
72 
73  /// Undoes this action.
74  /// @return true on success; false on an error.
75  virtual bool undo(int side) = 0;
76  /// the difference in the unit ids
77  /// TODO: does it really make sense to allow undoing if the unit id counter has changed?
79  /// actions wml (specified by wml) that should be executed when undoing this command.
80  typedef std::vector<undo_event> event_vector;
81  event_vector umc_commands_undo;
82  void execute_undo_umc_wml();
83 
84  static void read_event_vector(event_vector& vec, const config& cfg, const std::string& tag);
85  static void write_event_vector(const event_vector& vec, config& cfg, const std::string& tag);
86  };
87 
88  /// entry for player actions that do not need any special code to be performed when undoing such as right-click menu items.
90  {
92  : undo_action()
93  {
94  }
95  explicit undo_dummy_action (const config & cfg)
96  : undo_action(cfg)
97  {
98  }
99  virtual const char* get_type() const { return "dummy"; }
100  virtual ~undo_dummy_action () {}
101  /// Undoes this action.
102  virtual bool undo(int)
103  {
104  return true;
105  }
106  };
107 
108 }
virtual void write(config &cfg) const
Writes this into the provided config.
Definition: undo_action.hpp:51
std::vector< char_t > string
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.
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:38
event_vector umc_commands_undo
Definition: undo_action.hpp:81
std::vector< undo_event > event_vector
actions wml (specified by wml) that should be executed when undoing this command. ...
Definition: undo_action.hpp:80
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:99
undo_dummy_action(const config &cfg)
Definition: undo_action.hpp:95
Encapsulates the map of the game.
Definition: location.hpp:40
virtual void write(config &cfg) const
Writes this into the provided config.
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:89
static void write_event_vector(const event_vector &vec, config &cfg, const std::string &tag)
Define the game's event mechanism.
undo_action_base & operator=(const undo_action_base &)=delete
undo_action()
Default constructor.
Definition: undo_action.cpp:64
undo_action_base()
Default constructor.
Definition: undo_action.hpp:45
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:93
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:78
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:60