-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRect.cpp
204 lines (157 loc) · 4.08 KB
/
Rect.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "Rect.h"
#include "App.h"
void Rect::onCreate(const MouseEvent& ev) {
for (auto& pt : corners) {
pt = ev.pos;
}
App::Instance()->setCaptureObject(this);
setDragAndHoverPoint(Corner::RIGHT_BOTTOM);
}
void Rect::updateCorner(Corner c, const POINT* pos) {
if (pos) {
corners[static_cast<int>(c)] = *pos;
return;
}
switch (c)
{
case Rect::Corner::LEFT_TOP: {
leftTop().x = leftBottom().x;
leftTop().y = rightTop().y;
return;
}
case Rect::Corner::LEFT_BOTTOM: {
leftBottom().x = leftTop().x;
leftBottom().y = rightBottom().y;
return;
}
case Rect::Corner::RIGHT_TOP: {
rightTop().x = rightBottom().x;
rightTop().y = leftTop().y;
return;
}
case Rect::Corner::RIGHT_BOTTOM: {
rightBottom().x = rightTop().x;
rightBottom().y = leftBottom().y;
return;
}
default: {
assert("corner update failed.");
return;
}
}
}
bool Rect::onMouseEvent(const MouseEvent& ev) {
if (ev.isLButton()) {
if (ev.isUp()) {
setDragAndHoverPoint(Corner::NONE);
App::Instance()->clearCaptureObject();
//printf("rect id : %d, b::LeftIsUp: dragPoint %d hoverPoint %d\n", id, dragPoint, hoverPoint);
return true;
}
if (ev.isDown()) {
updateHoverPoint(ev);
if (hoverPoint != Corner::NONE) {
App::Instance()->setCaptureObject(this);
dragPoint = hoverPoint;
//printf("rect id: %d, b::LeftIsDown: dragPoint %d hoverPoint %d\n", id, dragPoint, hoverPoint);
return true;
}
return false;
}
}
else if (ev.isMove()) {
if (App::Instance()->mouseButtonState() == MouseButton::Left) {
//printf("rect id:%d: b::MoveAndLeftIsDown: dragPoint %d hoverPoint %d\n", id, dragPoint, hoverPoint); //bug here, create 2 rect and drag to reproduce.
switch (dragPoint) {
default: {
return false;
}
case Corner::NONE: {
return false;
}
case Corner::LEFT_TOP: {
updateCorner(Corner::LEFT_TOP, &ev.pos);
updateCorner(Corner::RIGHT_TOP);
updateCorner(Corner::LEFT_BOTTOM);
return true;
}
case Corner::LEFT_BOTTOM: {
updateCorner(Corner::LEFT_BOTTOM, &ev.pos);
updateCorner(Corner::LEFT_TOP);
updateCorner(Corner::RIGHT_BOTTOM);
return true;
}
case Corner::RIGHT_TOP: {
updateCorner(Corner::RIGHT_TOP, &ev.pos);
updateCorner(Corner::LEFT_TOP);
updateCorner(Corner::RIGHT_BOTTOM);
return true;
}
case Corner::RIGHT_BOTTOM: {
updateCorner(Corner::RIGHT_BOTTOM, &ev.pos);
updateCorner(Corner::LEFT_BOTTOM);
updateCorner(Corner::RIGHT_TOP);
return true;
}
}
}
else {
return updateHoverPoint(ev);
}
}
return false;
}
void Rect::setDragAndHoverPoint(Corner c) {
dragPoint = c;
hoverPoint = c;
}
void Rect::draw(HDC hdc) const {
Point lt = leftTop();
Point rb = rightBottom();
SelectBrush(hdc, GetStockBrush(NULL_BRUSH)); // color does not work.
::Rectangle(hdc, lt.x, lt.y, rb.x, rb.y);
if (hoverPoint != Corner::NONE) {
corners[(int)hoverPoint].draw(hdc, 6, true);
}
}
bool Rect::updateHoverPoint(const MouseEvent& ev) {
hoverPoint = Corner::NONE;
const Corner checkCorners[] = { Corner::LEFT_TOP , Corner::LEFT_BOTTOM, Corner::RIGHT_TOP, Corner::RIGHT_BOTTOM };
for (Corner c : checkCorners) {
if (corners[static_cast<int>(c)].inRange(ev.pos, 3)) {
hoverPoint = c;
return true;
}
}
return false;
}
void Rect::save(std::ofstream& f) {
writeString(f, typeAsString());
f.put(':');
for (auto& c : corners) {
c.save(f);
}
writeInt(f, static_cast<int>(hoverPoint));
writeInt(f, static_cast<int>(dragPoint));
f.put('\n');
}
void Rect::load(std::ifstream& f) {
auto p = std::make_unique<Rect>();
Rect& rect = *p;
for (auto& c : rect.corners) {
Point::load(f, c);
}
int h;
int d;
readInt(f, h);
readInt(f, d);
rect.hoverPoint = static_cast<Corner>(h);
rect.dragPoint = static_cast<Corner>(d);
App::Instance()->objList.emplace_back(std::move(p));
printf("[Rect]: (%d, %d), (%d, %d), (%d, %d), (%d, %d); h: %d, d: %d\n",
rect.corners[0].x, rect.corners[0].y,
rect.corners[1].x, rect.corners[1].y,
rect.corners[2].x, rect.corners[2].y,
rect.corners[3].x, rect.corners[3].y,
rect.hoverPoint, rect.dragPoint);
}