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