The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
undo.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2003 - 2025
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/**
17 * @file
18 * Various functions that implement the undoing (and redoing) of in-game commands.
19 */
20
21#pragma once
22
23#include "vision.hpp"
24#include "map/location.hpp"
25#include "units/ptr.hpp"
26#include "undo_action.hpp"
27
28#include <vector>
29
30namespace actions {
31
32
33/** Class to store the actions that a player can undo and redo. */
34class undo_list {
35
36 typedef std::unique_ptr<undo_action_container> action_ptr_t;
37 typedef std::vector<action_ptr_t> action_list;
38 typedef std::vector<std::unique_ptr<config>> redos_list;
39
40public:
41 undo_list(const undo_list&) = delete;
42 undo_list& operator=(const undo_list&) = delete;
43
44 undo_list();
45 ~undo_list();
46
47 // Functions related to managing the undo stack:
48
49 /** Adds an auto-shroud toggle to the undo stack. */
50 void add_auto_shroud(bool turned_on);
51 /** Adds a dismissal to the undo stack. */
52 void add_dismissal(const unit_const_ptr& u);
53 /** Adds a move to the undo stack. */
54 void add_move(const unit_const_ptr& u,
55 const std::vector<map_location>::const_iterator & begin,
56 const std::vector<map_location>::const_iterator & end,
57 int start_moves,
59 /** Adds a recall to the undo stack. */
60 void add_recall(const unit_const_ptr& u, const map_location& loc,
61 const map_location& from);
62 /** Adds a recruit to the undo stack. */
63 void add_recruit(const unit_const_ptr& u, const map_location& loc,
64 const map_location& from);
65
66 template<class T, class... Args>
67 void add_custom(Args&&... args)
68 {
69 add(std::make_unique<T>(std::forward<Args>(args)...));
70 }
71private:
72public:
73 /** Clears the stack of undoable (and redoable) actions. */
74 void clear();
75 /**
76 * Updates fog/shroud based on the undo stack, then updates stack as needed.
77 * Returns true if somethign was cleared.
78 */
79 bool commit_vision();
80 /**
81 * Performs some initializations and error checks when starting a new
82 * side-turn.
83 */
84 void new_side_turn(int side);
85 /** Returns true if the player has performed any actions this turn. */
86 bool player_acted() const { return committed_actions_ || !undos_.empty(); }
87 /** Read the undo_list from the provided config. */
88 void read(const config & cfg, int current_side);
89 /** Write the undo_list into the provided config. */
90 void write(config & cfg) const;
91
92 // Functions related to using the undo stack:
93
94 /** True if there are actions that can be undone. */
95 bool can_undo() const { return !undos_.empty(); }
96 /** True if there are actions that can be redone. */
97 bool can_redo() const { return !redos_.empty(); }
98
99 /** called before a user action, starts collecting undo steps for the new action. */
100 void init_action();
101 /** called after a user action, pushes the collected undo steps on the undo stack. */
102 void finish_action(bool can_undo);
103 /** called after a user action, removes empty actions */
104 void cleanup_action();
105 /** Undoes the top action on the undo stack. */
106 void undo();
107 /** Redoes the top action on the redo stack. */
108 void redo();
109
111 {
112 return current_.get();
113 }
114
115private:
116 /** Adds an undo step to the current action. */
117 void add(std::unique_ptr<undo_action>&& action)
118 {
119 if(current_) {
120 current_->add(std::move(action));
121 }
122 }
123 /** Applies the pending fog/shroud changes from the undo stack. */
124 bool apply_shroud_changes() const;
125
126private: // data
130
131 /** Tracks the current side. */
132 int side_;
133 /** Tracks if actions have been cleared from the stack since the turn began. */
135};
136
137
138}//namespace actions
map_location loc
Definition move.cpp:172
Class to store the actions that a player can undo and redo.
Definition undo.hpp:34
void new_side_turn(int side)
Performs some initializations and error checks when starting a new side-turn.
Definition undo.cpp:173
bool can_undo() const
True if there are actions that can be undone.
Definition undo.hpp:95
undo_list & operator=(const undo_list &)=delete
void add_dismissal(const unit_const_ptr &u)
Adds a dismissal to the undo stack.
Definition undo.cpp:88
undo_list(const undo_list &)=delete
void add_recall(const unit_const_ptr &u, const map_location &loc, const map_location &from)
Adds a recall to the undo stack.
Definition undo.cpp:108
bool committed_actions_
Tracks if actions have been cleared from the stack since the turn began.
Definition undo.hpp:134
action_ptr_t current_
Definition undo.hpp:127
void add(std::unique_ptr< undo_action > &&action)
Adds an undo step to the current action.
Definition undo.hpp:117
undo_action_container * get_current()
Definition undo.hpp:110
void write(config &cfg) const
Write the undo_list into the provided config.
Definition undo.cpp:239
action_list undos_
Definition undo.hpp:128
void add_custom(Args &&... args)
Definition undo.hpp:67
std::vector< action_ptr_t > action_list
Definition undo.hpp:37
~undo_list()
Destructor.
Definition undo.cpp:69
bool commit_vision()
Updates fog/shroud based on the undo stack, then updates stack as needed.
Definition undo.cpp:153
void read(const config &cfg, int current_side)
Read the undo_list from the provided config.
Definition undo.cpp:199
void add_move(const unit_const_ptr &u, const std::vector< map_location >::const_iterator &begin, const std::vector< map_location >::const_iterator &end, int start_moves, const map_location::direction dir=map_location::direction::indeterminate)
Adds a move to the undo stack.
Definition undo.cpp:96
void redo()
Redoes the top action on the redo stack.
Definition undo.cpp:314
undo_list()
Constructor.
Definition undo.cpp:61
void cleanup_action()
called after a user action, removes empty actions
Definition undo.cpp:270
bool apply_shroud_changes() const
Applies the pending fog/shroud changes from the undo stack.
Definition undo.cpp:364
std::vector< std::unique_ptr< config > > redos_list
Definition undo.hpp:38
void clear()
Clears the stack of undoable (and redoable) actions.
Definition undo.cpp:131
int side_
Tracks the current side.
Definition undo.hpp:132
void init_action()
called before a user action, starts collecting undo steps for the new action.
Definition undo.cpp:251
void add_auto_shroud(bool turned_on)
Adds an auto-shroud toggle to the undo stack.
Definition undo.cpp:79
void add_recruit(const unit_const_ptr &u, const map_location &loc, const map_location &from)
Adds a recruit to the undo stack.
Definition undo.cpp:117
redos_list redos_
Definition undo.hpp:129
std::unique_ptr< undo_action_container > action_ptr_t
Definition undo.hpp:36
void finish_action(bool can_undo)
called after a user action, pushes the collected undo steps on the undo stack.
Definition undo.cpp:258
bool player_acted() const
Returns true if the player has performed any actions this turn.
Definition undo.hpp:86
bool can_redo() const
True if there are actions that can be redone.
Definition undo.hpp:97
void undo()
Undoes the top action on the undo stack.
Definition undo.cpp:281
A config object defines a single node in a WML file, with access to child nodes.
Definition config.hpp:157
const config * cfg
std::shared_ptr< const unit > unit_const_ptr
Definition ptr.hpp:27
Encapsulates the map of the game.
Definition location.hpp:46
direction
Valid directions which can be moved in our hexagonal world.
Definition location.hpp:48
Various functions implementing vision (through fog of war and shroud).