-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicDrawPane.cpp
More file actions
executable file
·381 lines (336 loc) · 13.9 KB
/
BasicDrawPane.cpp
File metadata and controls
executable file
·381 lines (336 loc) · 13.9 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
//
// Created by rostam on 15.10.19.
//
#include "BasicDrawPane.h"
#include "CGTeaFrame.h"
#include <wx/dcclient.h>
BEGIN_EVENT_TABLE(BasicDrawPane, wxPanel)
// some useful events
EVT_MOTION(BasicDrawPane::mouseMoved)
EVT_LEFT_DOWN(BasicDrawPane::mouseDown)
EVT_LEFT_UP(BasicDrawPane::mouseReleased)
EVT_RIGHT_DOWN(BasicDrawPane::rightClick)
EVT_LEAVE_WINDOW(BasicDrawPane::mouseLeftWindow)
EVT_KEY_DOWN(BasicDrawPane::keyPressed)
EVT_KEY_UP(BasicDrawPane::keyReleased)
EVT_MOUSEWHEEL(BasicDrawPane::mouseWheelMoved)
// catch paint events
EVT_PAINT(BasicDrawPane::paintEvent)
END_EVENT_TABLE()
// some useful events
void BasicDrawPane::mouseMoved(wxMouseEvent& event) {}
void BasicDrawPane::mouseDown(wxMouseEvent& event) {
// boost::put(boost::vertex_distance, g, v, pos[i]);
Graph& g = static_cast<CGTeaFrame*>(this->m_parent)->currentGraph;
const Ver vv = boost::num_vertices(g);
boost::add_vertex(vv, g);
const cgtea_geometry::Point p(event.GetPosition().x,event.GetPosition().y);
boost::put(boost::vertex_distance, g, vv, p);
Refresh();
// int radius = 20;
// for_each_v_const(g, [&](Ver v) {
// cgtea_geometry::Point pos = boost::get(boost::vertex_distance, g, v);
// if (event.GetPosition().GetDistance(pos) < radius) {
// raggedVertexIndex = i;
// }
// });
// for (size_t i = 0; i < vertices.size(); ++i) {
// if (event.GetPosition().GetDistance(vertices[i].position) < radius) {
// vertices[i].isBeingDragged = true;
// draggedVertexIndex = i;
// return;
// }
// }
// // If no vertex was clicked, create a new one
// vertices.push_back({event.GetPosition()});
// Refresh();
}
void BasicDrawPane::mouseWheelMoved(wxMouseEvent& event) {}
void BasicDrawPane::mouseReleased(wxMouseEvent& event) {}
void BasicDrawPane::rightClick(wxMouseEvent& event) {}
void BasicDrawPane::mouseLeftWindow(wxMouseEvent& event) {}
void BasicDrawPane::keyPressed(wxKeyEvent& event) {}
void BasicDrawPane::keyReleased(wxKeyEvent& event) {}
BasicDrawPane::BasicDrawPane(wxWindow* parent) : wxPanel(parent, wxID_ANY) {
distinctColors[0] = wxColour(255, 255, 255, 255);//white with alpha = 1
distinctColors[1] = wxColour(230, 25, 75, 150);//red
distinctColors[2] = wxColour(60, 180, 75, 150);//green
distinctColors[3] = wxColour(255, 225, 25, 150);//yellow
distinctColors[4] = wxColour(0, 130, 200, 150);//blue
distinctColors[5] = wxColour(245, 130, 49, 150);//orange
distinctColors[6] = wxColour(145, 30, 180, 150);//purple
distinctColors[7] = wxColour(70, 240, 240, 150);//cyan
distinctColors[8] = wxColour(240, 50, 230, 150);//magenta
distinctColors[9] = wxColour(240, 50, 230, 150);//lime
distinctColors[10] = wxColour(250, 190, 190, 150);//pink
distinctColors[11] = wxColour(0, 128, 128, 150);//teal
distinctColors[12] = wxColour(230, 190, 255, 150);//lavender
distinctColors[13] = wxColour(170, 110, 40, 150);//brown
distinctColors[14] = wxColour(255, 250, 200, 150);//beige
distinctColors[15] = wxColour(128, 0, 0, 150);//maroon
distinctColors[16] = wxColour(170, 255, 195, 150);//Mint
distinctColors[17] = wxColour(128, 128, 0, 150);//olive
distinctColors[18] = wxColour(255, 216, 177, 150);//Coral
distinctColors[19] = wxColour(0, 0, 128, 150);//Navy
distinctColors[20] = wxColour(128, 128, 128, 150);//Grey
for(int i=21;i<1000;i++) {
auto col = distinctColors[i%20];
auto r = col.Red();
auto g = col.Green();
auto b = col.Blue();
r = r < 200 ? r + 40 : r - 50;
g = g < 200 ? g + 40 : g - 50;
b = b < 200 ? b + 40 : b - 50;
distinctColors[i] = wxColor(r,g,b,150);
}
}
/*
* Called by the system of by wxWidgets when the panel needs
* to be redrawn. You can also trigger this call by
* calling Refresh()/Update().
*/
void BasicDrawPane::paintEvent(wxPaintEvent & evt)
{
wxPaintDC dc(this);
render(dc);
}
/*
* Alternatively, you can use a clientDC to paint on the panel
* at any time. Using this generally does not free you from
* catching paint events, since it is possible that e.g. the window
* manager throws away your drawing when the window comes to the
* background and expects you will redraw it when the window comes
* back (by sending a paint event).
*
* In most cases, this will not be needed at all; simply handling
* paint events and calling Refresh() when a refresh is needed
* will do the job.
*/
void BasicDrawPane::paintNow()
{
wxPaintDC dc(this);
render(dc);
}
/*
* Here we do the actual rendering. I put it in a separate
* method so that it can work no matter what type of DC
* (e.g., wxPaintDC or wxClientDC) is used.
*/
void BasicDrawPane::render(wxPaintDC& dc) {
wxGraphicsContext* gc = wxGraphicsContext::Create(dc);
if (gc)
{
wxFont font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
font.SetPointSize(16);
gc->SetFont(font,*wxBLACK);
gc->SetPen(wxPen(wxColor(0, 0, 0), 1)); // black line, 3 pixels thick
gc->DrawRectangle(0,0,dc.GetSize().GetWidth(),dc.GetSize().GetHeight());
try {
const Graph& g = static_cast<CGTeaFrame*>(this->m_parent)->currentGraph;
drawEdges(g, gc);
drawVertices(g, gc);
} catch (std::exception &e) {
std::cerr << e.what() << std::endl;
}
delete gc;
}
}
// void BasicDrawPane::drawEdges(const Graph &g, wxGraphicsContext* gc) {
// for_each_e_const(g, [&](Edge e) {
// const Ver src = boost::source(e,g);
// const Ver tgt = boost::target(e,g);
// const cgtea_geometry::Point src_pos = boost::get(boost::vertex_distance, g, src);
// const cgtea_geometry::Point tgt_pos = boost::get(boost::vertex_distance, g, tgt);
// gc->SetPen(wxPen(wxColor(0, 0, 0), 2)); // black line, 3 pixels thick
// wxGraphicsPath path = gc->CreatePath();
// path.MoveToPoint(src_pos.x, src_pos.y);
// path.AddLineToPoint(tgt_pos.x, tgt_pos.y);
// gc->StrokePath(path);
// });
// }
// void BasicDrawPane::drawVertices(const Graph &g, wxGraphicsContext* gc) {
// for_each_v_const(g, [&](Ver v) {
// int color = boost::get(vertex_color, g,v);
// gc->SetPen(wxPen(wxColor(255, 0, 0), 1)); // 5-pixels-thick red outline
// cgtea_geometry::Point pos = boost::get(boost::vertex_distance, g, v);
// gc->SetBrush(wxBrush( wxColour(255, 255, 255, 255))); // green filling
// wxGraphicsPath pathBackground = gc->CreatePath();
// pathBackground.AddCircle(pos.x, pos.y, 20 );
// gc->FillPath(pathBackground);
//
// gc->SetBrush(wxBrush( distinctColors[color + 1]));
// wxGraphicsPath path = gc->CreatePath();
// path.AddCircle(pos.x, pos.y, 20 );
// gc->FillPath(path);
//
// gc->SetBrush(wxBrush( wxColour(0, 0, 0, 255)));
// int tmp = boost::get(boost::vertex_index, g, v) + 1;
// wxString mystring = wxString::Format(wxT("%i"),tmp);
// wxDouble w, h;
// gc->GetTextExtent(mystring, &w, &h, nullptr, nullptr);
// gc->DrawText(mystring, pos.x-w/2, pos.y-h/2);
// });
// }
void BasicDrawPane::drawShape(wxGraphicsContext* gc, const VertexShape shape,
const cgtea_geometry::Point& pos, double size) {
switch(shape) {
case VertexShape::Square:
drawSquare(gc, pos, size);
break;
case VertexShape::Triangle:
drawTriangle(gc, pos, size);
break;
case VertexShape::Diamond:
drawDiamond(gc, pos, size);
break;
case VertexShape::Circle:
default:
drawCircle(gc, pos, size);
break;
}
}
void BasicDrawPane::drawCircle(wxGraphicsContext* gc, const cgtea_geometry::Point& pos, double size) {
wxGraphicsPath path = gc->CreatePath();
path.AddCircle(pos.x, pos.y, size);
gc->FillPath(path);
}
void BasicDrawPane::drawSquare(wxGraphicsContext* gc, const cgtea_geometry::Point& pos, double size) {
wxGraphicsPath path = gc->CreatePath();
path.AddRectangle(pos.x - size, pos.y - size, size * 2, size * 2);
gc->FillPath(path);
}
void BasicDrawPane::drawTriangle(wxGraphicsContext* gc, const cgtea_geometry::Point& pos, double size) {
wxGraphicsPath path = gc->CreatePath();
path.MoveToPoint(pos.x, pos.y - size);
path.AddLineToPoint(pos.x - size, pos.y + size);
path.AddLineToPoint(pos.x + size, pos.y + size);
path.CloseSubpath();
gc->FillPath(path);
}
void BasicDrawPane::drawDiamond(wxGraphicsContext* gc, const cgtea_geometry::Point& pos, double size) {
wxGraphicsPath path = gc->CreatePath();
path.MoveToPoint(pos.x, pos.y - size);
path.AddLineToPoint(pos.x + size, pos.y);
path.AddLineToPoint(pos.x, pos.y + size);
path.AddLineToPoint(pos.x - size, pos.y);
path.CloseSubpath();
gc->FillPath(path);
}
void BasicDrawPane::drawVertices(const Graph &g, wxGraphicsContext* gc) {
for_each_v_const(g, [&](Ver v) {
const int color = boost::get(vertex_color, g, v);
const cgtea_geometry::Point pos = boost::get(boost::vertex_distance, g, v);
//auto shape = VertexShape::Diamond;
const auto frame = static_cast<CGTeaFrame*>(this->m_parent);
const auto shape = frame ? frame->getCurrentVertexShape() : VertexShape::Circle;
// Draw a white background
gc->SetPen(wxPen(wxColor(255, 0, 0), 1));
gc->SetBrush(wxBrush(wxColour(255, 255, 255, 255)));
drawShape(gc, shape, pos, 20);
// Draw a colored shape
gc->SetBrush(wxBrush(distinctColors[color + 1]));
drawShape(gc, shape, pos, 20);
// Draw a vertex number
gc->SetBrush(wxBrush(wxColour(0, 0, 0, 255)));
int tmp = boost::get(boost::vertex_index, g, v) + 1;
wxString mystring = wxString::Format(wxT("%i"), tmp);
wxDouble w, h;
gc->GetTextExtent(mystring, &w, &h, nullptr, nullptr);
gc->DrawText(mystring, pos.x-w/2, pos.y-h/2);
});
}
void BasicDrawPane::drawEdgeShape(wxGraphicsContext* gc, EdgeShape shape,
const cgtea_geometry::Point& src,
const cgtea_geometry::Point& tgt) {
switch(shape) {
case EdgeShape::Curve:
drawCurve(gc, src, tgt);
break;
case EdgeShape::DoubleArrow:
drawDoubleArrow(gc, src, tgt);
break;
case EdgeShape::Dashed:
drawDashed(gc, src, tgt);
break;
case EdgeShape::Line:
default:
drawLine(gc, src, tgt);
break;
}
}
void BasicDrawPane::drawLine(wxGraphicsContext* gc,
const cgtea_geometry::Point& src,
const cgtea_geometry::Point& tgt) {
wxGraphicsPath path = gc->CreatePath();
path.MoveToPoint(src.x, src.y);
path.AddLineToPoint(tgt.x, tgt.y);
gc->StrokePath(path);
}
void BasicDrawPane::drawCurve(wxGraphicsContext* gc,
const cgtea_geometry::Point& src,
const cgtea_geometry::Point& tgt) {
wxGraphicsPath path = gc->CreatePath();
path.MoveToPoint(src.x, src.y);
// Calculate control point for quadratic curve
double midX = (src.x + tgt.x) / 2;
double midY = (src.y + tgt.y) / 2;
double offsetY = 30; // Curve height
path.AddQuadCurveToPoint(midX, midY - offsetY, tgt.x, tgt.y);
gc->StrokePath(path);
}
void BasicDrawPane::drawDoubleArrow(wxGraphicsContext* gc,
const cgtea_geometry::Point& src,
const cgtea_geometry::Point& tgt) {
// Draw main line
drawLine(gc, src, tgt);
// Calculate arrow parameters
double angle = atan2(tgt.y - src.y, tgt.x - src.x);
double arrowLength = 15;
double arrowAngle = M_PI / 6; // 30 degrees
// Draw arrows at both ends
for(const auto& point : {src, tgt}) {
double baseAngle = (point.x == src.x && point.y == src.y) ?
angle + M_PI : angle;
wxGraphicsPath arrowPath = gc->CreatePath();
arrowPath.MoveToPoint(point.x, point.y);
arrowPath.AddLineToPoint(
point.x + arrowLength * cos(baseAngle + arrowAngle),
point.y + arrowLength * sin(baseAngle + arrowAngle)
);
arrowPath.MoveToPoint(point.x, point.y);
arrowPath.AddLineToPoint(
point.x + arrowLength * cos(baseAngle - arrowAngle),
point.y + arrowLength * sin(baseAngle - arrowAngle)
);
gc->StrokePath(arrowPath);
}
}
void BasicDrawPane::drawDashed(wxGraphicsContext* gc,
const cgtea_geometry::Point& src,
const cgtea_geometry::Point& tgt) {
// Create a new dashed pen
wxPen dashedPen(wxColor(0, 0, 0), 2); // Match the regular pen style
wxDash dashes[] = {5, 5}; // 5 pixel dash, 5 pixel gap
dashedPen.SetDashes(2, dashes);
dashedPen.SetStyle(wxPENSTYLE_USER_DASH);
gc->SetPen(dashedPen);
// Draw the dashed line
wxGraphicsPath path = gc->CreatePath();
path.MoveToPoint(src.x, src.y);
path.AddLineToPoint(tgt.x, tgt.y);
gc->StrokePath(path);
// Reset to solid pen
gc->SetPen(wxPen(wxColor(0, 0, 0), 2));
}
void BasicDrawPane::drawEdges(const Graph &g, wxGraphicsContext* gc) {
for_each_e_const(g, [&](Edge e) {
const Ver src = boost::source(e,g);
const Ver tgt = boost::target(e,g);
const cgtea_geometry::Point src_pos = boost::get(boost::vertex_distance, g, src);
const cgtea_geometry::Point tgt_pos = boost::get(boost::vertex_distance, g, tgt);
gc->SetPen(wxPen(wxColor(0, 0, 0), 2));
const auto frame = static_cast<CGTeaFrame*>(this->m_parent);
const auto edgeShape = frame ? frame->getCurrentEdgeShape() : EdgeShape::Line;
drawEdgeShape(gc, edgeShape, src_pos, tgt_pos);
});
}