-
Notifications
You must be signed in to change notification settings - Fork 193
/
MovingHeadDimmerPanel.cpp
executable file
·346 lines (315 loc) · 12.5 KB
/
MovingHeadDimmerPanel.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
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
/***************************************************************
* This source files comes from the xLights project
* https://www.xlights.org
* https://github.com/xLightsSequencer/xLights
* See the github commit history for a record of contributing
* developers.
* Copyright claimed based on commit dates recorded in Github
* License: https://github.com/xLightsSequencer/xLights/blob/master/License.txt
**************************************************************/
#include "MovingHeadDimmerPanel.h"
#include <wx/dcbuffer.h>
#include <wx/graphics.h>
namespace
{
const int BorderWidth = 5;
const int HandleWidth = 6;
const int HalfHandleWidth = HandleWidth / 2;
const float box_size = 0.02f;
const float snap_zone = 0.015f;
const float num_hlines = 10.0f;
const float num_vlines = 8.0f;
const float hline_gap = 1.0f / num_hlines;
const float vline_gap = 1.0f / num_vlines;
const float marker_length = 0.04f;
const float pt_tol = 0.001f; // can't let points be too close or Value Curve deletes them
}
BEGIN_EVENT_TABLE(MovingHeadDimmerPanel, wxPanel)
EVT_PAINT(MovingHeadDimmerPanel::OnMovingHeadPaint)
EVT_KEY_DOWN(MovingHeadDimmerPanel::OnKeyDown)
EVT_LEFT_DOWN(MovingHeadDimmerPanel::OnMovingHeadLeftDown)
EVT_LEFT_UP(MovingHeadDimmerPanel::OnMovingHeadLeftUp)
EVT_MOTION(MovingHeadDimmerPanel::OnMovingHeadMouseMove)
EVT_ENTER_WINDOW(MovingHeadDimmerPanel::OnMovingHeadEntered)
EVT_SIZE(MovingHeadDimmerPanel::OnSize)
END_EVENT_TABLE()
MovingHeadDimmerPanel::MovingHeadDimmerPanel(IMovingHeadDimmerParent* MovingHeadDimmerParent, wxWindow* parent, wxWindowID id /*=wxID_ANY*/, const wxPoint& pos /*= wxDefaultPosition*/, const wxSize& size /*=wxDefaultSize*/) :
wxPanel(parent, id, pos, size, wxNO_BORDER | wxWANTS_CHARS),
m_MovingHeadDimmerParent(MovingHeadDimmerParent)
{
SetBackgroundStyle(wxBG_STYLE_PAINT);
wxPoint2DDouble pt1(NormalizedToUI2(wxPoint2DDouble(0.0f,0.0f)));
wxPoint2DDouble pt2(NormalizedToUI2(wxPoint2DDouble(1.0f,0.0f)));
m_handles.push_back(UItoNormalized(pt1));
m_handles.push_back(UItoNormalized(pt2));
}
void MovingHeadDimmerPanel::OnSize(wxSizeEvent& event){
wxSize old_sz = GetSize();
if( old_sz.GetHeight() != old_sz.GetWidth()/2 ) {
old_sz.SetHeight(old_sz.GetWidth()/2);
SetMinSize(old_sz);
SetSize(old_sz);
}
Refresh();
//skip the event.
event.Skip();
}
void MovingHeadDimmerPanel::OnMovingHeadPaint(wxPaintEvent& /*event*/)
{
wxAutoBufferedPaintDC pdc(this);
wxSize sz(GetSize());
pdc.SetPen(*wxWHITE_PEN);
pdc.SetBrush(*wxGREY_BRUSH);
pdc.DrawRectangle(wxPoint(0, 0), sz);
pdc.SetPen(*wxLIGHT_GREY_PEN);
for( int i = 0; i <= num_vlines; ++i ) {
wxPoint2DDouble start_x1(vline_gap * (float)i, 0);
wxPoint2DDouble end_x1(vline_gap * (float)i, marker_length);
wxPoint2DDouble start_x2(vline_gap * (float)i, 1 - marker_length);
wxPoint2DDouble end_x2(vline_gap * (float)i, 1);
pdc.DrawLine(NormalizedToUI2(start_x1), NormalizedToUI2(end_x1));
pdc.DrawLine(NormalizedToUI2(start_x2), NormalizedToUI2(end_x2));
}
for( int i = 0; i <= num_hlines + 2; ++i ) {
wxPoint2DDouble start_y1(0, hline_gap * (float)i);
wxPoint2DDouble end_y1(marker_length, hline_gap * (float)i);
wxPoint2DDouble start_y2(1 - marker_length, hline_gap * (float)i);
wxPoint2DDouble end_y2(1, hline_gap * (float)i);
pdc.DrawLine(NormalizedToUI2(start_y1), NormalizedToUI2(end_y1));
pdc.DrawLine(NormalizedToUI2(start_y2), NormalizedToUI2(end_y2));
}
// draw outline
pdc.DrawLine(NormalizedToUI2(wxPoint2DDouble(0.0f,0.0f)), NormalizedToUI2(wxPoint2DDouble(0.0f,1.0f)));
pdc.DrawLine(NormalizedToUI2(wxPoint2DDouble(0.0f,1.0f)), NormalizedToUI2(wxPoint2DDouble(1.0f,1.0f)));
pdc.DrawLine(NormalizedToUI2(wxPoint2DDouble(1.0f,1.0f)), NormalizedToUI2(wxPoint2DDouble(1.0f,0.0f)));
pdc.DrawLine(NormalizedToUI2(wxPoint2DDouble(1.0f,0.0f)), NormalizedToUI2(wxPoint2DDouble(0.0f,0.0f)));
// draw curve
wxPoint old_pt(-1,-1);
pdc.SetPen(*wxGREEN_PEN);
for (auto it = m_handles.begin(); it != m_handles.end(); ++it) {
wxPoint ptUI {NormalizedToUI2(*it)};
if (old_pt.x != -1) {
pdc.DrawLine(old_pt.x, old_pt.y, ptUI.x, ptUI.y);
}
old_pt = ptUI;
}
// draw handles
int handle = 0;
for (auto it = m_handles.begin(); it != m_handles.end(); ++it) {
if (handle == selected_handle) {
pdc.SetPen(*wxBLUE_PEN);
pdc.SetBrush(*wxBLUE_BRUSH);
} else {
pdc.SetPen(*wxRED_PEN);
pdc.SetBrush(*wxRED_BRUSH);
}
wxPoint ptUI {NormalizedToUI2(*it)};
pdc.DrawRectangle(ptUI.x - HalfHandleWidth, ptUI.y - HalfHandleWidth, HandleWidth, HandleWidth);
handle++;
}
if (active_handle >= 0) {
wxPoint2DDouble pos = m_handles[active_handle];
pdc.SetPen(*wxRED_PEN);
wxPoint2DDouble lstart_x(0, pos.m_y);
wxPoint2DDouble lend_x(pos.m_x-box_size, pos.m_y);
wxPoint2DDouble rstart_x(pos.m_x+box_size, pos.m_y);
wxPoint2DDouble rend_x(1.0f, pos.m_y);
pdc.DrawLine(NormalizedToUI2(lstart_x), NormalizedToUI2(lend_x));
pdc.DrawLine(NormalizedToUI2(rstart_x), NormalizedToUI2(rend_x));
wxPoint2DDouble tstart_y(pos.m_x, 0);
wxPoint2DDouble tend_y(pos.m_x, pos.m_y-box_size);
wxPoint2DDouble bstart_y(pos.m_x, pos.m_y+box_size);
wxPoint2DDouble bend_y(pos.m_x, 1.0f);
pdc.DrawLine(NormalizedToUI2(tstart_y), NormalizedToUI2(tend_y));
pdc.DrawLine(NormalizedToUI2(bstart_y), NormalizedToUI2(bend_y));
wxPoint2DDouble top_left_corner(pos.m_x-box_size/2.0f, pos.m_y-box_size);
wxPoint2DDouble top_right_corner(pos.m_x+box_size/2.0f, pos.m_y-box_size);
wxPoint2DDouble bot_left_corner(pos.m_x-box_size/2.0f, pos.m_y+box_size);
wxPoint2DDouble bot_right_corner(pos.m_x+box_size/2.0f, pos.m_y+box_size);
pdc.DrawLine(NormalizedToUI2(top_left_corner), NormalizedToUI2(top_right_corner));
pdc.DrawLine(NormalizedToUI2(top_right_corner), NormalizedToUI2(bot_right_corner));
pdc.DrawLine(NormalizedToUI2(bot_right_corner), NormalizedToUI2(bot_left_corner));
pdc.DrawLine(NormalizedToUI2(bot_left_corner), NormalizedToUI2(top_left_corner));
}
}
void MovingHeadDimmerPanel::OnKeyDown(wxKeyEvent& event)
{
int keycode = event.GetKeyCode();
if (keycode == WXK_DELETE) {
if( selected_handle >= 0 ) {
m_handles.erase(m_handles.begin()+selected_handle);
selected_handle = -1;
active_handle = -1;
m_MovingHeadDimmerParent->NotifyDimmerUpdated();
Refresh();
}
}
}
std::string MovingHeadDimmerPanel::GetDimmerCommands()
{
if( m_handles.size() >= 0 ) {
std::string text{"Dimmer: "};
bool add_comma = false;
for (auto it = m_handles.begin(); it != m_handles.end(); ++it) {
wxString dimmer_pt = wxString::Format("%f,%f", (*it).m_x, (*it).m_y);
if( add_comma ) {
text += ",";
}
text += dimmer_pt;
add_comma = true;
}
return text;
} else {
return "Dimmer: 0,1,0,1";
}
}
void MovingHeadDimmerPanel::SetDimmerCommands( const std::string& _dimmerCmds )
{
m_handles.clear();
selected_handle = -1;
active_handle = -1;
wxArrayString points = wxSplit(_dimmerCmds, ',');
unsigned long num_pts = points.size() / 2;
for( int i = 0; i < num_pts; ++i ) {
double x { wxAtof(points[i*2]) };
double y { wxAtof(points[i*2+1]) };
wxPoint2DDouble pt(x, y);
pt.m_y = pt.m_y;
m_handles.push_back(pt);
}
Refresh();
}
void MovingHeadDimmerPanel::SnapToLines(wxPoint2DDouble& pos)
{
float snap_x = round(pos.m_x / hline_gap) * hline_gap;
float snap_y = round(pos.m_y / vline_gap) * vline_gap;
if( abs(pos.m_x - snap_x) < snap_zone ) {
pos.m_x = snap_x;
}
if( abs(pos.m_y - snap_y) < snap_zone ) {
pos.m_y = snap_y;
}
}
void MovingHeadDimmerPanel::OnMovingHeadLeftDown(wxMouseEvent& event)
{
wxAffineMatrix2D m;
wxPoint2DDouble ptUI(m.TransformPoint(event.GetPosition()));
m_mousePos = UItoNormalized(ptUI);
//SnapToLines(m_mousePos);
m_mouseDown = true;
if( m_handles.size() == 0 ) {
m_handles.push_back(m_mousePos);
active_handle = 0;
m_MovingHeadDimmerParent->NotifyDimmerUpdated();
Refresh();
} else {
int selected_point = HitTest(ptUI);
if( selected_point >= 0 ) {
active_handle = selected_point;
} else {
active_handle = m_handles.size();
int new_pos = 0;
for (auto it = m_handles.begin(); it != m_handles.end(); ++it) {
if( (*it).m_x < m_mousePos.m_x) {
new_pos++;
} else {
active_handle = new_pos;
m_handles.insert(m_handles.begin() + active_handle, m_mousePos);
break;
}
}
m_MovingHeadDimmerParent->NotifyDimmerUpdated();
}
if (active_handle != 0 &&
active_handle != m_handles.size()-1) {
selected_handle = active_handle;
} else {
selected_handle = -1;
}
Refresh();
}
}
void MovingHeadDimmerPanel::OnMovingHeadLeftUp(wxMouseEvent& event)
{
active_handle = -1;
m_mouseDown = false;
Refresh();
}
void MovingHeadDimmerPanel::OnMovingHeadMouseMove(wxMouseEvent& event)
{
wxAffineMatrix2D m;
wxPoint2DDouble ptUI(m.TransformPoint(event.GetPosition()));
if( m_mouseDown ) {
SetCursor(wxCURSOR_HAND);
m_mousePos = UItoNormalized(ptUI);
//SnapToLines(m_mousePos);
if( active_handle != -1 ) {
m_mousePos = UItoNormalized(ptUI);
if (m_mousePos.m_x >= 0.0f &&
m_mousePos.m_x <= 1.0f &&
m_mousePos.m_y >= 0.0f &&
m_mousePos.m_y <= 1.0f) {
// check if inside surrounding handles
if (active_handle != 0 &&
active_handle != m_handles.size()-1) {
if (m_mousePos.m_x < m_handles[active_handle-1].m_x + pt_tol) {
m_handles[active_handle].m_x = m_handles[active_handle-1].m_x + pt_tol;
}
else if (m_mousePos.m_x > m_handles[active_handle+1].m_x - pt_tol) {
m_handles[active_handle].m_x = m_handles[active_handle+1].m_x - pt_tol;
} else {
m_handles[active_handle].m_x = m_mousePos.m_x;
}
}
m_handles[active_handle].m_y = m_mousePos.m_y;
m_MovingHeadDimmerParent->NotifyDimmerUpdated();
}
}
Refresh();
} else {
if ( HitTest(ptUI) >= 0 ) {
SetCursor(wxCURSOR_HAND);
} else {
SetCursor(wxCURSOR_ARROW);
}
}
}
void MovingHeadDimmerPanel::OnMovingHeadEntered(wxMouseEvent& /*event*/)
{
SetFocus();
}
int MovingHeadDimmerPanel::HitTest(wxPoint2DDouble& ptUI)
{
for (int i = 0; i < m_handles.size(); ++i ) {
wxPoint2DDouble pt{NormalizedToUI(m_handles[i])};
if( (ptUI.m_x > pt.m_x - HalfHandleWidth) &&
(ptUI.m_x < pt.m_x + HalfHandleWidth) &&
(ptUI.m_y > pt.m_y - HalfHandleWidth) &&
(ptUI.m_y < pt.m_y + HalfHandleWidth) ) {
return i;
}
}
return -1;
}
// Usable area rect: BorderWidth+1, BorderWidth+1, sz.GetWidth()-2*BorderWidth-2, sz.GetHeight()-2*BorderWidth-2;
wxPoint2DDouble MovingHeadDimmerPanel::UItoNormalized(const wxPoint2DDouble& pt) const
{
wxPoint o(BorderWidth + 1, BorderWidth + 1);
wxSize sz(GetSize() - wxSize(2 * BorderWidth + 2, 2 * BorderWidth + 2));
double x = double(pt.m_x - o.x) / sz.GetWidth();
double y = double(pt.m_y - o.y) / sz.GetHeight();
return wxPoint2DDouble(x, 1. - y);
}
wxPoint2DDouble MovingHeadDimmerPanel::NormalizedToUI(const wxPoint2DDouble& pt) const
{
wxPoint o(BorderWidth + 1, BorderWidth + 1);
wxSize sz(GetSize() - wxSize(2 * BorderWidth + 2, 2 * BorderWidth + 2));
double x = pt.m_x * sz.GetWidth();
double y = pt.m_y * sz.GetHeight();
return wxPoint2DDouble(o.x + x, o.y + (sz.GetHeight() - y));
}
wxPoint MovingHeadDimmerPanel::NormalizedToUI2(const wxPoint2DDouble& pt) const
{
wxPoint2DDouble pt1 = NormalizedToUI(pt);
return wxPoint((int)pt1.m_x, (int)pt1.m_y);
}