-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLine.cpp
203 lines (147 loc) · 3.61 KB
/
Line.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
#include "Line.h"
#include "App.h"
bool Line::onMouseEvent(const MouseEvent& ev) {
static Point _lastMousePos;
if (ev.isUp() && ev.isLButton()) {
if (dragPoint >= 0 && dragPoint < 2) {
setDragPoint(-1);
App::Instance()->clearCaptureObject();
return true;
}
if (isDragLine) {
App::Instance()->clearCaptureObject();
setDragLine(false);
return true;
}
}
else if (ev.isDown() && ev.isLButton()) {
if (updateHoverPoint(ev, pt, 2)) {
App::Instance()->setCaptureObject(this);
setDragPoint(hoverPoint);
return true;
}
if (updateHoverLine(ev, 10)) {
App::Instance()->setCaptureObject(this);
setDragLine(true);
_lastMousePos = ev.pos;
return true;
}
}
else if (ev.isMove()) {
if (dragPoint >= 0 && dragPoint < 2) {
pt[dragPoint] = ev.pos;
return true;
}
if (isDragLine) {
Point v = Point(ev.pos) - _lastMousePos;
for (auto& p : pt) {
p += v;
}
_lastMousePos = ev.pos;
return true;
}
bool isUpdated = false;
isUpdated = updateHoverLine(ev, 10);
if (isUpdated) return true;
isUpdated = updateHoverPoint(ev, pt, 2);
if (isUpdated) return true;
return false;
}
return false;
}
bool Line::updateHoverLine(const MouseEvent& ev, int distance = 10) {
ishoverLine = false;
if (hoverPoint != -1) {
return false;
}
if (pt[0].inRange(ev.pos, 3) || pt[1].inRange(ev.pos, 3)) {
return false;
}
Point mousePos = ev.pos;
Vector2D v = pt[1] - pt[0];
Vector2D m = mousePos - pt[0];
double dp = m.dotProduct(v);
if (dp < 0 || dp / v.length() > v.length()) {
return false;
}
Vector2D p = pt[0].asVector2D() + m.project(v);
double d = p.distance(mousePos);
if (d > distance) {
return false;
}
ishoverLine = true;
return true;
}
void Line::onCreate(const MouseEvent& ev) {
App::Instance()->setCaptureObject(this);
pt[0] = ev.pos;
pt[1] = ev.pos;
setDragPoint(1);
}
void Line::draw(HDC hdc_) const {
if (ishoverLine) {
Line::drawLine(hdc_, pt[0], pt[1], App::Instance()->solidRedPen);
}
else {
Line::drawLine(hdc_, pt[0], pt[1], GetStockPen(DC_PEN));
}
//MoveToEx(hdc_, pt[0].x, pt[0].y, nullptr);
//LineTo(hdc_, pt[1].x, pt[1].y);
assert(hoverPoint < 2);
if (hoverPoint >= 0 && hoverPoint < 2) {
pt[hoverPoint].draw(hdc_, 6, true);
}
}
void Line::drawLine(HDC hdc_, const Point& pt0, const Point& pt1, HPEN hPen) {
auto oldPen = SelectObject(hdc_, hPen);
MoveToEx(hdc_, pt0.x, pt0.y, nullptr);
LineTo(hdc_, pt1.x, pt1.y);
SelectObject(hdc_, oldPen);
}
void Line::save(std::ofstream& f) {
writeString(f, typeAsString());
f.put(':');
for (auto& p : pt) {
p.save(f);
}
writeInt(f, hoverPoint);
writeInt(f, dragPoint);
f.put('\n');
}
void Line::load(std::ifstream& f)
{
auto p = std::make_unique<Line>();
Line& line = *p;
for (auto& c : line.pt) {
Point::load(f, c);
}
readInt(f, line.hoverPoint);
readInt(f, line.dragPoint);
App::Instance()->objList.emplace_back(std::move(p));
printf("[Line] p0(% d, % d), p1(% d, % d), h:% d, d : % d\n",
line.pt[0].x, line.pt[0].y,
line.pt[1].x, line.pt[1].y,
line.hoverPoint, line.dragPoint
);
}
bool Line::updateHoverPoint(const MouseEvent ev, const Point* points, int nPoints) {
assert(points != nullptr);
clearHoverPoint();
for (int i = 0; i < nPoints; i++) {
if (points[i].inRange(ev.pos, 3)) {
hoverPoint = i;
return true;
}
}
return false;
}
Point Line::lerp(const Point& p0, const Point& p1, float t)
{
assert(t >= 0.0f && t <= 1.0f);
return p0 * (1.0f - t) + p1 * t;
}
//Point Line::lerp(const Vector2D& p0, const Vector2D& p1, float t)
//{
// assert(t >= 0.0f && t <= 1.0f);
// return p0 * (1.0f - t) + p1 * t;
//}