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