1#include "editor_history.h"
2
3#include "editor.h"
4#include "editor_actions.h"
5
6#include <engine/shared/config.h>
7
8void CEditorHistory::RecordAction(const std::shared_ptr<IEditorAction> &pAction)
9{
10 RecordAction(pAction, pDisplay: nullptr);
11}
12
13void CEditorHistory::Execute(const std::shared_ptr<IEditorAction> &pAction, const char *pDisplay)
14{
15 pAction->Redo();
16 RecordAction(pAction, pDisplay);
17}
18
19void CEditorHistory::RecordAction(const std::shared_ptr<IEditorAction> &pAction, const char *pDisplay)
20{
21 if(m_IsBulk)
22 {
23 m_vpBulkActions.push_back(x: pAction);
24 return;
25 }
26
27 m_vpRedoActions.clear();
28
29 if((int)m_vpUndoActions.size() >= g_Config.m_ClEditorMaxHistory)
30 {
31 m_vpUndoActions.pop_front();
32 }
33
34 if(pDisplay == nullptr)
35 m_vpUndoActions.emplace_back(args: pAction);
36 else
37 m_vpUndoActions.emplace_back(args: std::make_shared<CEditorActionBulk>(args: Map(), args: std::vector<std::shared_ptr<IEditorAction>>{pAction}, args&: pDisplay));
38}
39
40bool CEditorHistory::Undo()
41{
42 if(m_vpUndoActions.empty())
43 return false;
44
45 auto pLastAction = m_vpUndoActions.back();
46 m_vpUndoActions.pop_back();
47
48 pLastAction->Undo();
49
50 m_vpRedoActions.emplace_back(args&: pLastAction);
51 return true;
52}
53
54bool CEditorHistory::Redo()
55{
56 if(m_vpRedoActions.empty())
57 return false;
58
59 auto pLastAction = m_vpRedoActions.back();
60 m_vpRedoActions.pop_back();
61
62 pLastAction->Redo();
63
64 m_vpUndoActions.emplace_back(args&: pLastAction);
65 return true;
66}
67
68void CEditorHistory::Clear()
69{
70 m_vpUndoActions.clear();
71 m_vpRedoActions.clear();
72}
73
74void CEditorHistory::BeginBulk()
75{
76 m_IsBulk = true;
77 m_vpBulkActions.clear();
78}
79
80void CEditorHistory::EndBulk(const char *pDisplay)
81{
82 if(!m_IsBulk)
83 return;
84 m_IsBulk = false;
85
86 // Record bulk action
87 if(!m_vpBulkActions.empty())
88 RecordAction(pAction: std::make_shared<CEditorActionBulk>(args: Map(), args&: m_vpBulkActions, args&: pDisplay, args: true));
89
90 m_vpBulkActions.clear();
91}
92
93void CEditorHistory::EndBulk(int DisplayToUse)
94{
95 EndBulk(pDisplay: (DisplayToUse < 0 || DisplayToUse >= (int)m_vpBulkActions.size()) ? nullptr : m_vpBulkActions[DisplayToUse]->DisplayText());
96}
97