-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransform.cpp
128 lines (101 loc) · 3.11 KB
/
Transform.cpp
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
#include "Transform.h"
//Constructor
Transform::Transform() : translateX(0), translateY(0), scaleX(1), scaleY(1), rotationAngle(0) {}
Transform::Transform(float translateX, float translateY, float scaleX, float scaleY, float rotationAngle)
: translateX(translateX), translateY(translateY), scaleX(scaleX), scaleY(scaleY), rotationAngle(rotationAngle) {}
void Transform::apply(Gdiplus::Matrix& parentMatrix) const {
Gdiplus::Matrix matrix;
matrix.Translate(translateX, translateY);
matrix.Rotate(rotationAngle);
matrix.Scale(scaleX, scaleY);
// Áp dụng phép biến đổi cho ma trận cha
parentMatrix.Multiply(&matrix);
}
Transform Transform::loadTransform(rapidxml::xml_node<>* node) {
Transform transform;
rapidxml::xml_attribute<>* transformAttr = node->first_attribute("transform");
if (transformAttr) {
std::string transformStr = transformAttr->value();
std::regex regex("(\\w+)\\s*\\(([^)]+)\\)");
std::smatch match;
while (std::regex_search(transformStr, match, regex)) {
std::string command = match[1].str();
std::string values = match[2].str();
parseCommand(command, values, transform);
transformStr = match.suffix().str();
}
}
return transform;
}
void Transform::parseCommand(const std::string& command, const std::string& values, Transform& transform) {
if (command == "translate") {
float translateX = 0.0f, translateY = 0.0f;
std::stringstream ss(values);
ss >> translateX;
ss.ignore();
if (!(ss >> translateY)) translateY = 0.0f;
transform.setTranslateX(translateX);
transform.setTranslateY(translateY);
}
else if (command == "scale") {
float scaleX = 1.0f, scaleY = 1.0f;
std::stringstream ss(values);
ss >> scaleX;
ss.ignore();
if (!(ss >> scaleY)) scaleY = scaleX;
transform.setScaleX(scaleX);
transform.setScaleY(scaleY);
}
else if (command == "rotate") {
float angle = 0.0f, cx = 0.0f, cy = 0.0f;
std::stringstream ss(values);
ss >> angle;
if (ss >> cx >> cy) {
// Có thể lưu cx, cy nếu cần
}
transform.setRotationAngle(angle);
}
}
//Getter
float Transform::getTranslateX() const {
return translateX;
}
float Transform::getTranslateY() const {
return translateY;
}
float Transform::getScaleX() const {
return scaleX;
}
float Transform::getScaleY() const {
return scaleY;
}
float Transform::getRotationAngle() const {
return rotationAngle;
}
//Setter
void Transform::setTranslateX(float translateX) {
this->translateX = translateX;
}
void Transform::setTranslateY(float translateY) {
this->translateY = translateY;
}
void Transform::setScaleX(float scaleX) {
this->scaleX = scaleX;
}
void Transform::setScaleY(float scaleY) {
this->scaleY = scaleY;
}
void Transform::setRotationAngle(float rotationAngle) {
this->rotationAngle = rotationAngle;
}
//Operator
bool Transform::operator==(const Transform& other) const {
return (translateX == other.translateX && translateY == other.translateY &&
scaleX == other.scaleX && scaleY == other.scaleY &&
rotationAngle == other.rotationAngle);
}
bool Transform::isDefault() const {
return translateX == 0 && translateY == 0 &&
rotationAngle == 0 &&
scaleX == 1 && scaleY == 1;
}