-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicDrawPane.cpp
executable file
·196 lines (172 loc) · 7.1 KB
/
BasicDrawPane.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
//
// 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 = ((CGTeaFrame*)this->m_parent)->currentGraph;
Ver vv = boost::num_vertices(g);
boost::add_vertex(vv, g);
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 = ((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) {
Ver src = boost::source(e,g);
Ver tgt = boost::target(e,g);
cgtea_geometry::Point src_pos = boost::get(boost::vertex_distance, g, src);
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->DrawCircle(wxPoint(pos.x, pos.y), 20 /* radius */ );
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);
});
}