Plasma Engine  2.0
Loading...
Searching...
No Matches
CommandHistory.h
1#pragma once
2
3#include <Foundation/Types/RefCounted.h>
4#include <Foundation/Types/SharedPtr.h>
5#include <ToolsFoundation/Command/Command.h>
6#include <ToolsFoundation/ToolsFoundationDLL.h>
7
9
10class PL_TOOLSFOUNDATION_DLL plCommandTransaction : public plCommand
11{
12 PL_ADD_DYNAMIC_REFLECTION(plCommandTransaction, plCommand);
13
14public:
17
18 plString m_sDisplayString;
19
20private:
21 virtual plStatus DoInternal(bool bRedo) override;
22 virtual plStatus UndoInternal(bool bFireEvents) override;
23 virtual void CleanupInternal(CommandState state) override;
24 plStatus AddCommandTransaction(plCommand* command);
25
26private:
27 friend class plCommandHistory;
28};
29
31{
32 enum class Type
33 {
34 UndoStarted,
35 UndoEnded,
36 RedoStarted,
37 RedoEnded,
43 HistoryChanged,
44 };
45
46 Type m_Type;
47 const plDocument* m_pDocument;
48};
49
51class PL_TOOLSFOUNDATION_DLL plCommandHistory
52{
53public:
55
56 // \brief Storage for the command history so it can be swapped when using multiple sub documents.
57 class Storage : public plRefCounted
58 {
59 public:
61 plHybridArray<plCommand*, 4> m_ActiveCommandStack;
64 plDocument* m_pDocument = nullptr;
66 };
67
68public:
69 plCommandHistory(plDocument* pDocument);
71
72 const plDocument* GetDocument() const { return m_pHistoryStorage->m_pDocument; }
73
74 plStatus Undo(plUInt32 uiNumEntries = 1);
75 plStatus Redo(plUInt32 uiNumEntries = 1);
76
77 bool CanUndo() const;
78 bool CanRedo() const;
79
80 plStringView GetUndoDisplayString() const;
81 plStringView GetRedoDisplayString() const;
82
83 void StartTransaction(const plFormatString& displayString);
84 void CancelTransaction() { EndTransaction(true); }
85 void FinishTransaction() { EndTransaction(false); }
86
88 bool IsInTransaction() const { return !m_pHistoryStorage->m_TransactionStack.IsEmpty(); }
89 bool IsInUndoRedo() const { return m_bIsInUndoRedo; }
90
94 void BeginTemporaryCommands(plStringView sDisplayString, bool bFireEventsWhenUndoingTempCommands = false);
95 void CancelTemporaryCommands();
96 void FinishTemporaryCommands();
97
98 bool InTemporaryTransaction() const;
99 void SuspendTemporaryTransaction();
100 void ResumeTemporaryTransaction();
101
102 plStatus AddCommand(plCommand& ref_command);
103
104 void ClearUndoHistory();
105 void ClearRedoHistory();
106
107 void MergeLastTwoTransactions();
108
109 plUInt32 GetUndoStackSize() const;
110 plUInt32 GetRedoStackSize() const;
111 const plCommandTransaction* GetUndoStackEntry(plUInt32 uiIndex) const;
112 const plCommandTransaction* GetRedoStackEntry(plUInt32 uiIndex) const;
113
115 plSharedPtr<plCommandHistory::Storage> GetStorage() { return m_pHistoryStorage; }
116
117private:
118 friend class plCommand;
119
120 plStatus UndoInternal();
121 plStatus RedoInternal();
122
123 void EndTransaction(bool bCancel);
124 void EndTemporaryCommands(bool bCancel);
125
127
128 plEvent<const plCommandHistoryEvent&, plMutex>::Unsubscriber m_EventsUnsubscriber;
129
130 bool m_bFireEventsWhenUndoingTempCommands = false;
131 bool m_bTemporaryMode = false;
132 plInt32 m_iTemporaryDepth = -1;
133 plInt32 m_iPreSuspendTemporaryDepth = -1;
134 bool m_bIsInUndoRedo = false;
135};
Definition CommandHistory.h:58
Stores the undo / redo stacks of transactions done on a document.
Definition CommandHistory.h:52
bool IsInTransaction() const
Returns true, if between StartTransaction / EndTransaction. False during Undo/Redo.
Definition CommandHistory.h:88
Interface for a command.
Definition Command.h:15
Definition CommandHistory.h:11
Definition Deque.h:270
Definition Document.h:57
Definition Event.h:177
Implements formating of strings with placeholders and formatting options.
Definition FormatString.h:59
A hybrid array uses in-place storage to handle the first few elements without any allocation....
Definition HybridArray.h:12
Base class for reference counted objects.
Definition RefCounted.h:52
A Shared ptr manages a shared object and destroys that object when no one references it anymore....
Definition SharedPtr.h:10
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
Definition CommandHistory.h:31
Type
Definition CommandHistory.h:33
@ TransactionCanceled
Emit after initial transaction canceled.
@ BeforeTransactionCanceled
Emit before initial transaction ended.
@ TransactionStarted
Emit after initial transaction started.
@ BeforeTransactionEnded
Emit before initial transaction ended.
@ TransactionEnded
Emit after initial transaction ended.
An plResult with an additional message for the reason of failure.
Definition Status.h:12