Undo manager - Improved interface
* Adding returns reference to the action * Checks for whether undo or redo are possible
This commit is contained in:
parent
e11aff344f
commit
2e01842c10
2 changed files with 15 additions and 7 deletions
8
undo.cc
8
undo.cc
|
@ -26,7 +26,7 @@ void T_UndoManager::redo( ) noexcept
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void T_UndoManager::addAction(
|
A_UndoAction& T_UndoManager::addAction(
|
||||||
P_UndoAction action ) noexcept
|
P_UndoAction action ) noexcept
|
||||||
{
|
{
|
||||||
assert( action );
|
assert( action );
|
||||||
|
@ -34,12 +34,14 @@ void T_UndoManager::addAction(
|
||||||
count_ --;
|
count_ --;
|
||||||
actions_[ ( start_ + count_ ) % MaxUndo ].clear( );
|
actions_[ ( start_ + count_ ) % MaxUndo ].clear( );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const auto ai{ count_ == MaxUndo ? start_ : ( ( start_ + pos_ ) % MaxUndo ) };
|
||||||
if ( count_ == MaxUndo ) {
|
if ( count_ == MaxUndo ) {
|
||||||
actions_[ start_ ] = std::move( action );
|
|
||||||
start_ = ( start_ + 1 ) % MaxUndo;
|
start_ = ( start_ + 1 ) % MaxUndo;
|
||||||
} else {
|
} else {
|
||||||
actions_[ ( start_ + pos_ ) % MaxUndo ] = std::move( action );
|
|
||||||
count_ ++;
|
count_ ++;
|
||||||
pos_ ++;
|
pos_ ++;
|
||||||
}
|
}
|
||||||
|
actions_[ ai ] = std::move( action );
|
||||||
|
return *actions_[ ai ];
|
||||||
}
|
}
|
||||||
|
|
14
undo.hh
14
undo.hh
|
@ -36,22 +36,28 @@ class T_UndoManager
|
||||||
// Redo last undone action
|
// Redo last undone action
|
||||||
void redo( ) noexcept;
|
void redo( ) noexcept;
|
||||||
|
|
||||||
|
// Check if it is possible to undo or redo
|
||||||
|
bool canUndo( ) const noexcept
|
||||||
|
{ return pos_ > 0; }
|
||||||
|
bool canRedo( ) const noexcept
|
||||||
|
{ return pos_ < count_; }
|
||||||
|
|
||||||
// Add an action using a pointer
|
// Add an action using a pointer
|
||||||
void addAction( P_UndoAction action ) noexcept;
|
A_UndoAction& addAction( P_UndoAction action ) noexcept;
|
||||||
|
|
||||||
// Construct and add an action
|
// Construct and add an action
|
||||||
template<
|
template<
|
||||||
typename T ,
|
typename T ,
|
||||||
typename... Args
|
typename... Args
|
||||||
> void add( Args&&... args ) noexcept;
|
> A_UndoAction& add( Args&&... args ) noexcept;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
template<
|
template<
|
||||||
typename T ,
|
typename T ,
|
||||||
typename... Args
|
typename... Args
|
||||||
> inline void T_UndoManager::add(
|
> inline A_UndoAction& T_UndoManager::add(
|
||||||
Args&&... args ) noexcept
|
Args&&... args ) noexcept
|
||||||
{
|
{
|
||||||
addAction( NewOwned< T >( std::forward< Args >( args ) ... ) );
|
return addAction( NewOwned< T >( std::forward< Args >( args ) ... ) );
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue