demotool/c-undo.cc

55 lines
1 KiB
C++
Raw Normal View History

2017-11-22 10:01:37 +01:00
#include "externals.hh"
#include "c-undo.hh"
2017-11-22 10:01:37 +01:00
/*= A_UndoAction ===============================================================*/
A_UndoAction::~A_UndoAction( )
{}
/*= T_UndoManager ==============================================================*/
2017-11-22 14:13:43 +01:00
T_UndoManager::T_UndoManager( ) noexcept
{
while ( actions_.size( ) < MaxUndo ) {
actions_.addNew( );
}
}
2017-11-22 10:01:37 +01:00
void T_UndoManager::undo( ) noexcept
{
if ( pos_ > 0 ) {
pos_ --;
actions_[ ( start_ + pos_ ) % MaxUndo ]->undo( );
}
}
void T_UndoManager::redo( ) noexcept
{
if ( pos_ < count_ ) {
actions_[ ( start_ + pos_ ) % MaxUndo ]->redo( );
2017-11-22 14:13:43 +01:00
pos_ ++;
2017-11-22 10:01:37 +01:00
}
}
A_UndoAction& T_UndoManager::addAction(
2017-11-22 10:01:37 +01:00
P_UndoAction action ) noexcept
{
assert( action );
while ( pos_ < count_ ) {
count_ --;
actions_[ ( start_ + count_ ) % MaxUndo ].clear( );
}
const auto ai{ count_ == MaxUndo ? start_ : ( ( start_ + pos_ ) % MaxUndo ) };
2017-11-22 10:01:37 +01:00
if ( count_ == MaxUndo ) {
start_ = ( start_ + 1 ) % MaxUndo;
} else {
count_ ++;
pos_ ++;
}
actions_[ ai ] = std::move( action );
return *actions_[ ai ];
2017-11-22 10:01:37 +01:00
}