-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCommands.cpp
More file actions
149 lines (130 loc) · 4.14 KB
/
Commands.cpp
File metadata and controls
149 lines (130 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "pch.h"
#include "Command.h"
#include "CShapeManager.h"
#include <utility>
namespace {
const COLORREF kDefaultCadColor = RGB(255, 255, 255);
}
// 构造“添加线条”命令对象
CAddLineCommand::CAddLineCommand(CShapeManager* mgr, std::shared_ptr<CLine> line)
: m_pManager(mgr), m_pLine(std::move(line)) {
}
// 执行添加线条命令
void CAddLineCommand::Execute() { m_pManager->AddShape(m_pLine); }
// 撤销添加线条命令
void CAddLineCommand::Undo() { m_pManager->RemoveShape(m_pLine); }
// 构造“删除多条线”命令对象
CDeleteLinesCommand::CDeleteLinesCommand(CShapeManager* mgr, std::vector<std::shared_ptr<CLine>> lines)
: m_pManager(mgr), m_lines(std::move(lines)) {
}
// 执行删除线条命令
void CDeleteLinesCommand::Execute() {
for (const auto& line : m_lines) {
m_pManager->RemoveShape(line);
}
}
// 撤销删除线条命令
void CDeleteLinesCommand::Undo() {
for (const auto& line : m_lines) {
m_pManager->AddShape(line);
}
}
// 构造“替换线条”命令对象
CReplaceLineCommand::CReplaceLineCommand(CShapeManager* mgr, std::shared_ptr<CLine> original, std::vector<std::shared_ptr<CLine>> replacements)
: m_pManager(mgr), m_original(std::move(original)), m_replacements(std::move(replacements)) {
}
// 执行替换线条命令
void CReplaceLineCommand::Execute() {
m_pManager->RemoveShape(m_original);
for (const auto& line : m_replacements) {
m_pManager->AddShape(line);
}
}
// 撤销替换线条命令
void CReplaceLineCommand::Undo() {
for (const auto& line : m_replacements) {
m_pManager->RemoveShape(line);
}
m_pManager->AddShape(m_original);
}
// 构造“修改线条颜色”命令对象
CChangeLineColorCommand::CChangeLineColorCommand(CShapeManager* mgr, std::vector<std::shared_ptr<CLine>> lines, COLORREF newColor)
: m_pManager(mgr), m_lines(std::move(lines)), m_newColor(newColor) {
m_oldColors.reserve(m_lines.size());
for (const auto& line : m_lines) {
m_oldColors.push_back(line ? line->GetColor() : kDefaultCadColor);
}
}
// 执行颜色修改命令
void CChangeLineColorCommand::Execute() {
UNREFERENCED_PARAMETER(m_pManager);
for (const auto& line : m_lines) {
if (line) {
line->SetColor(m_newColor);
}
}
}
// 撤销颜色修改命令
void CChangeLineColorCommand::Undo() {
for (size_t i = 0; i < m_lines.size() && i < m_oldColors.size(); ++i) {
if (m_lines[i]) {
m_lines[i]->SetColor(m_oldColors[i]);
}
}
}
// 构造“修改填充状态/颜色”命令对象
CChangeLineFillCommand::CChangeLineFillCommand(CShapeManager* mgr, std::shared_ptr<CLine> line, bool newHasFill, COLORREF newFillColor)
: m_pManager(mgr),
m_line(std::move(line)),
m_oldHasFill(false),
m_oldFillColor(kDefaultCadColor),
m_newHasFill(newHasFill),
m_newFillColor(newFillColor) {
if (m_line) {
m_oldHasFill = m_line->HasFill();
m_oldFillColor = m_line->GetFillColor();
}
}
// 执行填充修改命令
void CChangeLineFillCommand::Execute() {
UNREFERENCED_PARAMETER(m_pManager);
if (m_line) {
m_line->SetFill(m_newHasFill, m_newFillColor);
}
}
// 撤销填充修改命令
void CChangeLineFillCommand::Undo() {
if (m_line) {
m_line->SetFill(m_oldHasFill, m_oldFillColor);
}
}
// 构造“平移多条线”命令对象
CMoveLinesCommand::CMoveLinesCommand(CShapeManager* mgr, std::vector<std::shared_ptr<CLine>> lines, double dx, double dy, bool alreadyApplied)
: m_pManager(mgr),
m_lines(std::move(lines)),
m_dx(dx),
m_dy(dy),
m_hasExecuted(alreadyApplied) {
}
// 执行平移命令
void CMoveLinesCommand::Execute() {
UNREFERENCED_PARAMETER(m_pManager);
if (!m_hasExecuted) {
for (const auto& line : m_lines) {
if (line) {
line->Move(m_dx, m_dy);
}
}
}
m_hasExecuted = true;
}
// 撤销平移命令
void CMoveLinesCommand::Undo() {
if (!m_hasExecuted) return;
for (const auto& line : m_lines) {
if (line) {
line->Move(-m_dx, -m_dy);
}
}
m_hasExecuted = false;
}