-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui_element_dialog.cpp
More file actions
332 lines (292 loc) · 9.41 KB
/
ui_element_dialog.cpp
File metadata and controls
332 lines (292 loc) · 9.41 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
#include "stdafx.h"
#include "resource.h"
#include <helpers/BumpableElem.h>
#include "PdFoo.hpp"
#include <fstream>
#include <regex>
using namespace std;
using namespace pd;
vector<string> mixt;
extern PdFoo lpd;
extern cfg_bool cfg_refresh;
namespace {
// Anonymous namespace : standard practice in fb2k components
// Nothing outside should have any reason to see these symbols,
// and we don't want funny results if another cpp has similarly named classes.
// service_factory at the bottom takes care of publishing our class.
// 5741202f - dcb1 - 4249 - 9723 - 977906bfc7aa
static const GUID guid_myelem =
{ 0x5741202f ,0xdcb1 ,0x4249 ,{ 0x97 ,0x23 ,0x97 ,0x79 ,0x6 ,0xbf ,0xc7 ,0xaa } };
static struct {
int id;
int lbl;
int lblOut;
string dest;
BOOL isGradual;
BOOL isReverse;
float min ,max;
} hsl[] = {
{ IDC_SLIDER1 ,IDC_LBL_SLIDER1 ,IDC_LBL_SL1OUT ,"" ,0 ,0 ,0 ,1000 }
,{ IDC_SLIDER2 ,IDC_LBL_SLIDER2 ,IDC_LBL_SL2OUT ,"" ,0 ,0 ,0 ,1000 }
,{ IDC_SLIDER3 ,IDC_LBL_SLIDER3 ,IDC_LBL_SL3OUT ,"" ,0 ,0 ,0 ,1000 }
,{ IDC_SLIDER4 ,IDC_LBL_SLIDER4 ,IDC_LBL_SL4OUT ,"" ,0 ,0 ,0 ,1000 }
,{ IDC_SLIDER5 ,IDC_LBL_SLIDER5 ,IDC_LBL_SL5OUT ,"" ,0 ,0 ,0 ,1000 }
,{ IDC_SLIDER6 ,IDC_LBL_SLIDER6 ,IDC_LBL_SL6OUT ,"" ,0 ,0 ,0 ,1000 }
,{ IDC_SLIDER7 ,IDC_LBL_SLIDER7 ,IDC_LBL_SL7OUT ,"" ,0 ,0 ,0 ,1000 }
};
static struct {
int id;
string dest;
BOOL state;
} tgl[] = {
{ IDC_TOGGLE1 ,"" ,false }
,{ IDC_TOGGLE2 ,"" ,false }
,{ IDC_TOGGLE3 ,"" ,false }
};
static struct {
int id;
string dest;
} bng[] = {
{ IDC_BTN_BANG1 ,"" }
,{ IDC_BTN_BANG2 ,"" }
,{ IDC_BTN_BANG3 ,"" }
};
static struct {
int id;
int ed;
string dest;
} msg[] = {
{ IDC_BTN_MSG1 ,IDC_EDT_MSG1 ,"" }
,{ IDC_BTN_MSG2 ,IDC_EDT_MSG2 ,"" }
,{ IDC_BTN_ANY ,IDC_EDT_ANY_MSG ,"" }
};
bool is_number(const string &s) {
return !s.empty() && s.find_first_not_of("0123456789.-") == std::string::npos;
}
template <typename T>
string to_stringp(const T a_value ,const int n = 5) {
ostringstream out;
out.precision(n);
out << a_value;
return out.str();
}
class CDialogUIElem : public CDialogImpl<CDialogUIElem>
,public ui_element_instance ,private play_callback_impl_base {
public:
CDialogUIElem( ui_element_config::ptr cfg ,ui_element_instance_callback::ptr cb )
: m_callback(cb) ,m_flags( parseConfig(cfg) ) {}
enum { IDD = IDD_UI_ELEMENT };
BEGIN_MSG_MAP_EX( CDialogUIElem )
MSG_WM_INITDIALOG(OnInitDialog)
COMMAND_CODE_HANDLER_EX(BN_CLICKED ,OnButtonClicked)
MSG_WM_HSCROLL(OnHScroll)
END_MSG_MAP()
void initialize_window(HWND parent) {
WIN32_OP(Create(parent) != NULL);
}
HWND get_wnd() {
return m_hWnd;
}
void set_configuration(ui_element_config::ptr config) {
m_flags = parseConfig(config);
if (m_hWnd != NULL)
configToUI();
}
ui_element_config::ptr get_configuration() {
return makeConfig(m_flags);
}
static GUID g_get_guid() {
return guid_myelem;
}
static void g_get_name(pfc::string_base &out) {
out = "Pd Mixer";
}
static ui_element_config::ptr g_get_default_configuration() {
return makeConfig( );
}
static const char *g_get_description() {
return "Interact with patch settings";
}
static GUID g_get_subclass() {
return ui_element_subclass_utility;
}
ui_element_min_max_info get_min_max_info() {
ui_element_min_max_info ret;
CSize DPI = QueryScreenDPIEx(*this);
if (DPI.cx <= 0 || DPI.cy <= 0) // sanity
DPI = CSize(96 ,96);
ret.m_min_width = MulDiv(210 ,DPI.cx ,96);
ret.adjustForWindow(*this);
return ret;
}
private:
void on_playback_new_track(metadb_handle_ptr p_track) {
string path = p_track->get_path();
path.erase(0 ,7);
mixt.clear();
string line;
ifstream in(path);
while (getline(in ,line))
mixt.push_back(line);
if (!cfg_refresh) pd_mix();
}
static uint32_t parseConfig( ui_element_config::ptr cfg ) {
try
{ ::ui_element_config_parser in ( cfg );
uint32_t flags; in >> flags;
return flags; }
catch (exception_io_data)
{ return 0; }
}
static ui_element_config::ptr makeConfig(uint32_t flags = 0) {
ui_element_config_builder out;
out << flags;
return out.finish( g_get_guid() );
}
void OnButtonClicked(UINT uNotifyCode ,int nID ,CWindow wndCtl) {
int i;
for (i=0; i < PFC_TABSIZE(tgl); ++i)
if (tgl[i].id == nID)
{ tgl[i].state = !tgl[i].state;
lpd << Float(tgl[i].dest ,tgl[i].state);
return; }
for (i=0; i < PFC_TABSIZE(bng); ++i)
if (bng[i].id == nID)
{ lpd << Bang(bng[i].dest);
return; }
for (i=0; i < PFC_TABSIZE(msg); ++i)
if (msg[i].id == nID)
{ if (nID == IDC_BTN_ANY)
msg[i].dest = uGetDlgItemText(*this ,IDC_EDT_ANY_DEST).c_str();
string result = uGetDlgItemText(*this ,msg[i].ed).c_str();
result = ReplaceAll(result ,", " ,",");
vector<string> cmds = split(result ,',');
for (string cmd : cmds)
{ vector<string> list = split(cmd ,' ');
List send;
for (int j=0; j < list.size(); ++j)
{ if (is_number(list[j]))
send.addFloat(stof(list[j]));
else if (j>0) send.addSymbol(list[j]); }
if (list.size() > 0)
{ if (is_number(list[0]))
lpd.sendList(msg[i].dest ,send);
else lpd.sendMessage(msg[i].dest ,list[0] ,send); } }
return; }
if (nID == IDC_BTN_REFRESH) pd_mix();
}
void OnHScroll(UINT nSBCode ,UINT nPos ,CScrollBar pScrollBar) {
for (int i=0; i < PFC_TABSIZE(hsl); ++i)
if (pScrollBar.m_hWnd == m_slider[i].m_hWnd)
{ float val = m_slider[i].GetPos();
float min=hsl[i].min ,max=hsl[i].max;
if (hsl[i].isGradual)
val = val * ((max - min) / 1000.f) + min;
else if (hsl[i].isReverse)
val = (min - max) - val + max;
uSetDlgItemText(*this ,hsl[i].lblOut ,to_stringp(val).c_str());
lpd << Float(hsl[i].dest ,val); }
}
void pd_mix() {
int s=0 ,t=0 ,b=0 ,m=0;
isAny = false;
smatch match;
regex canvas("^#N canvas \\d+ \\d+ \\d+ \\d+ (mix|fb2k) \\d+;$");
for (int i=0; i < PFC_TABSIZE(hsl); ++i)
m_slider[i].SetPos(0);
// begin mixt for-loop
for (int i=0; i < mixt.size(); ++i)
{ if (regex_search(mixt[i] ,match ,canvas))
{ start = i+1;
regex slider("^#X obj \\d+ \\d+ (?:h|v)sl \\d+ \\d+ ([\\d\\.-]+) ([\\d\\.-]+)"
" \\d+ \\d+ ([\\w\\d\\.-]+) ([\\w\\d\\.-]+) ((?:(?:\\\\ )*[\\w\\d\\.-])+)");
regex obj("#X obj \\d+ \\d+ (bng|tgl) \\d+ (?:\\d+ \\d+ )?\\d+ ([\\w\\d\\.-]+)"
" [\\w\\d\\.-]+ ((?:(?:\\\\ )*['\\w\\d\\.:-])+)");
regex restore("^#X restore \\d+ \\d+ pd "+match[1].str()+";$");
regex spaces("(\\\\ |_)");
for (; i < mixt.size(); ++i)
{ if (regex_search(mixt[i] ,match ,slider) && s < PFC_TABSIZE(hsl))
{ float min=stof(match[1]) ,max=stof(match[2]);
hsl[s].min = min ,hsl[s].max = max;
hsl[s].isReverse = (min>max);
hsl[s].isGradual = (match[4] == "gradual");
if (!hsl[s].isGradual)
{ if (hsl[s].isReverse)
m_slider[s].SetRange(max ,min);
else m_slider[s].SetRange(min ,max); }
else m_slider[s].SetRange(0 ,1000);
hsl[s].dest = match[3];
string label = regex_replace(match[5].str() ,spaces ," ");
uSetDlgItemText(*this ,hsl[s].lbl ,label.c_str());
uSetDlgItemText(*this ,hsl[s].lblOut ,to_stringp(min).c_str());
++s; }
else if (regex_search(mixt[i] ,match ,obj))
{ if (match[1] == "bng")
{ if (match[3] == "empty" && b < PFC_TABSIZE(bng))
{ bng[b].dest = match[2];
uSetDlgItemText(*this ,bng[b].id ,bng[b].dest.c_str());
++b; }
else if (m < PFC_TABSIZE(msg))
{ string edit = regex_replace(match[3].str() ,spaces ," ");
edit = ReplaceAll(edit ,"'" ,",");
vector<string> any = split(edit ,':');
if (any.size() > 1)
{ uSetDlgItemText(*this ,IDC_EDT_ANY_DEST ,any[0].c_str());
uSetDlgItemText(*this ,IDC_EDT_ANY_MSG ,any[1].c_str());
isAny = true; }
else
{ msg[m].dest = match[2];
if (msg[m].id == IDC_BTN_ANY)
{ uSetDlgItemText(*this ,IDC_EDT_ANY_DEST ,msg[m].dest.c_str());
isAny = true; }
else uSetDlgItemText(*this ,msg[m].id ,msg[m].dest.c_str());
uSetDlgItemText(*this ,msg[m].ed ,edit.c_str());
++m; } } }
else if (t < PFC_TABSIZE(tgl))
{ tgl[t].dest = match[2];
string label = regex_replace(match[3].str() ,spaces ," ");
uSetDlgItemText(*this ,tgl[t].id ,label.c_str());
++t; } }
else if (regex_search(mixt[i] ,match ,restore))
{ end=i;
Trim(s ,t ,b ,m);
break; } }
break; } }
// end mixt for-loop
Trim(s ,t ,b ,m);
}
void Trim(int s ,int t ,int b ,int m) {
for (; s < PFC_TABSIZE(hsl); ++s)
{ uSetDlgItemText(*this ,hsl[s].lbl ,"--");
uSetDlgItemText(*this ,hsl[s].lblOut ,"-"); }
for (; t < PFC_TABSIZE(tgl); ++t)
uSetDlgItemText(*this ,tgl[t].id ,"--");
for (; b < PFC_TABSIZE(bng); ++b)
uSetDlgItemText(*this ,bng[b].id ,"--");
for (; m < PFC_TABSIZE(msg)-1; ++m)
{ uSetDlgItemText(*this ,msg[m].id ,"--");
uSetDlgItemText(*this ,msg[m].ed ,""); }
if (!isAny)
{ uSetDlgItemText(*this ,IDC_EDT_ANY_DEST ,"");
uSetDlgItemText(*this ,IDC_EDT_ANY_MSG ,""); }
}
void configToUI() {
for (int i=0; i < PFC_TABSIZE(hsl); ++i)
{ m_slider[i] = GetDlgItem(hsl[i].id);
m_slider[i].SetRange(0 ,1000); }
}
BOOL OnInitDialog(CWindow ,LPARAM) {
lpd.init();
configToUI();
return FALSE;
}
uint32_t m_flags;
CTrackBarCtrl m_slider[PFC_TABSIZE(hsl)];
int start=0 ,end=0;
BOOL isAny;
protected:
const ui_element_instance_callback::ptr m_callback;
};
class ui_element_myimpl : public ui_element_impl_withpopup<CDialogUIElem> {};
static service_factory_single_t<ui_element_myimpl> g_ui_element_myimpl_factory;
} // end namespace