LFOPluginEditor.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * LFOPluginEditor.cpp
  3. * -------------------
  4. * Purpose: Editor interface for the LFO plugin.
  5. * Notes : (currently none)
  6. * Authors: Johannes Schultz (OpenMPT Devs)
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #include "stdafx.h"
  10. #include "LFOPluginEditor.h"
  11. #include "../Mptrack.h"
  12. #include "../UpdateHints.h"
  13. #include "../../soundlib/Sndfile.h"
  14. #include "../../soundlib/MIDIEvents.h"
  15. #include "../../mptrack/resource.h"
  16. OPENMPT_NAMESPACE_BEGIN
  17. BEGIN_MESSAGE_MAP(LFOPluginEditor, CAbstractVstEditor)
  18. //{{AFX_MSG_MAP(LFOPluginEditor)
  19. ON_WM_HSCROLL()
  20. ON_COMMAND(IDC_BUTTON1, &LFOPluginEditor::OnPluginEditor)
  21. ON_COMMAND(IDC_CHECK1, &LFOPluginEditor::OnPolarityChanged)
  22. ON_COMMAND(IDC_CHECK2, &LFOPluginEditor::OnTempoSyncChanged)
  23. ON_COMMAND(IDC_CHECK3, &LFOPluginEditor::OnBypassChanged)
  24. ON_COMMAND(IDC_CHECK4, &LFOPluginEditor::OnLoopModeChanged)
  25. ON_COMMAND_RANGE(IDC_RADIO1, IDC_RADIO1 + LFOPlugin::kNumWaveforms - 1, &LFOPluginEditor::OnWaveformChanged)
  26. ON_CBN_SELCHANGE(IDC_COMBO1, &LFOPluginEditor::OnPlugParameterChanged)
  27. ON_CBN_SELCHANGE(IDC_COMBO2, &LFOPluginEditor::OnOutputPlugChanged)
  28. ON_CBN_SELCHANGE(IDC_COMBO3, &LFOPluginEditor::OnMidiCCChanged)
  29. ON_COMMAND(IDC_RADIO7, &LFOPluginEditor::OnParameterChanged)
  30. ON_COMMAND(IDC_RADIO8, &LFOPluginEditor::OnParameterChanged)
  31. ON_EN_UPDATE(IDC_EDIT1, &LFOPluginEditor::OnParameterChanged)
  32. ON_MESSAGE(LFOPlugin::WM_PARAM_UDPATE, &LFOPluginEditor::OnUpdateParam)
  33. //}}AFX_MSG_MAP
  34. END_MESSAGE_MAP()
  35. void LFOPluginEditor::DoDataExchange(CDataExchange* pDX)
  36. {
  37. CDialog::DoDataExchange(pDX);
  38. //{{AFX_DATA_MAP(LFOPluginEditor)
  39. DDX_Control(pDX, IDC_COMBO1, m_plugParam);
  40. DDX_Control(pDX, IDC_COMBO2, m_outPlug);
  41. DDX_Control(pDX, IDC_COMBO3, m_midiCC);
  42. DDX_Control(pDX, IDC_SLIDER1, m_amplitudeSlider);
  43. DDX_Control(pDX, IDC_SLIDER2, m_offsetSlider);
  44. DDX_Control(pDX, IDC_SLIDER3, m_frequencySlider);
  45. DDX_Control(pDX, IDC_EDIT1, m_midiChnEdit);
  46. DDX_Control(pDX, IDC_SPIN1, m_midiChnSpin);
  47. //}}AFX_DATA_MAP
  48. }
  49. LFOPluginEditor::LFOPluginEditor(LFOPlugin &plugin)
  50. : CAbstractVstEditor(plugin)
  51. , m_lfoPlugin(plugin)
  52. , m_locked(true)
  53. {
  54. }
  55. bool LFOPluginEditor::OpenEditor(CWnd *parent)
  56. {
  57. m_locked = true;
  58. Create(IDD_LFOPLUGIN, parent);
  59. m_midiChnSpin.SetRange32(1, 16);
  60. m_midiCC.SetRedraw(FALSE);
  61. CString s;
  62. for(unsigned int i = 0; i < 128; i++)
  63. {
  64. s.Format(_T("%3u: "), i);
  65. s += mpt::ToCString(mpt::Charset::UTF8, MIDIEvents::MidiCCNames[i]);
  66. m_midiCC.AddString(s);
  67. }
  68. if(m_lfoPlugin.m_outputToCC && m_lfoPlugin.m_outputParam != LFOPlugin::INVALID_OUTPUT_PARAM)
  69. {
  70. m_midiCC.SetCurSel(m_lfoPlugin.m_outputParam & 0x7F);
  71. SetDlgItemInt(IDC_EDIT1, 1 + ((m_lfoPlugin.m_outputParam & 0xF00) >> 8));
  72. } else
  73. {
  74. SetDlgItemInt(IDC_EDIT1, 1);
  75. }
  76. m_midiCC.SetRedraw(TRUE);
  77. UpdateView(PluginHint().Info().Names());
  78. for(int32 i = 0; i < LFOPlugin::kLFONumParameters; i++)
  79. {
  80. UpdateParam(i);
  81. }
  82. UpdateParamDisplays();
  83. m_locked = false;
  84. // Avoid weird WM_COMMAND message (following a WM_ACTIVATE message) activating a wrong waveform when closing the plugin editor while the pattern editor is open
  85. GetDlgItem(IDC_RADIO1 + m_lfoPlugin.m_waveForm)->SetFocus();
  86. return CAbstractVstEditor::OpenEditor(parent);
  87. }
  88. void LFOPluginEditor::UpdateParamDisplays()
  89. {
  90. CAbstractVstEditor::UpdateParamDisplays();
  91. m_locked = true;
  92. CheckRadioButton(IDC_RADIO7, IDC_RADIO8, m_lfoPlugin.m_outputToCC ? IDC_RADIO8 : IDC_RADIO7);
  93. m_plugParam.SetRedraw(FALSE);
  94. IMixPlugin *outPlug = m_lfoPlugin.GetOutputPlugin();
  95. if(outPlug != nullptr)
  96. {
  97. if(LONG_PTR(outPlug) != GetWindowLongPtr(m_plugParam, GWLP_USERDATA))
  98. {
  99. m_plugParam.ResetContent();
  100. AddPluginParameternamesToCombobox(m_plugParam, *outPlug);
  101. if(!m_lfoPlugin.m_outputToCC)
  102. m_plugParam.SetCurSel(m_lfoPlugin.m_outputParam);
  103. SetWindowLongPtr(m_plugParam, GWLP_USERDATA, LONG_PTR(outPlug));
  104. }
  105. GetDlgItem(IDC_BUTTON1)->EnableWindow(outPlug ? TRUE :FALSE);
  106. } else
  107. {
  108. m_plugParam.ResetContent();
  109. SetWindowLongPtr(m_plugParam, GWLP_USERDATA, 0);
  110. }
  111. m_plugParam.SetRedraw(TRUE);
  112. m_locked = false;
  113. }
  114. void LFOPluginEditor::UpdateParam(int32 p)
  115. {
  116. LFOPlugin::Parameters param = static_cast<LFOPlugin::Parameters>(p);
  117. CAbstractVstEditor::UpdateParam(p);
  118. m_locked = true;
  119. switch(param)
  120. {
  121. case LFOPlugin::kAmplitude:
  122. InitSlider(m_amplitudeSlider, LFOPlugin::kAmplitude);
  123. break;
  124. case LFOPlugin::kOffset:
  125. InitSlider(m_offsetSlider, LFOPlugin::kOffset);
  126. break;
  127. case LFOPlugin::kFrequency:
  128. InitSlider(m_frequencySlider, LFOPlugin::kFrequency);
  129. break;
  130. case LFOPlugin::kTempoSync:
  131. CheckDlgButton(IDC_CHECK2, m_lfoPlugin.m_tempoSync ? BST_CHECKED : BST_UNCHECKED);
  132. InitSlider(m_frequencySlider, LFOPlugin::kFrequency);
  133. break;
  134. case LFOPlugin::kWaveform:
  135. CheckRadioButton(IDC_RADIO1, IDC_RADIO1 + LFOPlugin::kNumWaveforms - 1, IDC_RADIO1 + m_lfoPlugin.m_waveForm);
  136. break;
  137. case LFOPlugin::kPolarity:
  138. CheckDlgButton(IDC_CHECK1, m_lfoPlugin.m_polarity ? BST_CHECKED : BST_UNCHECKED);
  139. break;
  140. case LFOPlugin::kBypassed:
  141. CheckDlgButton(IDC_CHECK3, m_lfoPlugin.m_bypassed ? BST_CHECKED : BST_UNCHECKED);
  142. break;
  143. case LFOPlugin::kLoopMode:
  144. CheckDlgButton(IDC_CHECK4, m_lfoPlugin.m_oneshot ? BST_CHECKED : BST_UNCHECKED);
  145. break;
  146. default:
  147. break;
  148. }
  149. m_locked = false;
  150. }
  151. void LFOPluginEditor::UpdateView(UpdateHint hint)
  152. {
  153. CAbstractVstEditor::UpdateView(hint);
  154. if(hint.GetType()[HINT_PLUGINNAMES | HINT_MIXPLUGINS])
  155. {
  156. PLUGINDEX hintPlug = hint.ToType<PluginHint>().GetPlugin();
  157. if(hintPlug > 0 && hintPlug <= m_lfoPlugin.GetSlot())
  158. {
  159. return;
  160. }
  161. CString s;
  162. IMixPlugin *outPlugin = m_lfoPlugin.GetOutputPlugin();
  163. m_outPlug.SetRedraw(FALSE);
  164. m_outPlug.ResetContent();
  165. for(PLUGINDEX out = m_lfoPlugin.GetSlot() + 1; out < MAX_MIXPLUGINS; out++)
  166. {
  167. const SNDMIXPLUGIN &outPlug = m_lfoPlugin.GetSoundFile().m_MixPlugins[out];
  168. if(outPlug.IsValidPlugin())
  169. {
  170. mpt::ustring libName = outPlug.GetLibraryName();
  171. s.Format(_T("FX%d: "), out + 1);
  172. s += mpt::ToCString(libName);
  173. if(outPlug.GetName() != U_("") && libName != outPlug.GetName())
  174. {
  175. s += _T(" (");
  176. s += mpt::ToCString(outPlug.GetName());
  177. s += _T(")");
  178. }
  179. int n = m_outPlug.AddString(s);
  180. m_outPlug.SetItemData(n, out);
  181. if(outPlugin == outPlug.pMixPlugin)
  182. {
  183. m_outPlug.SetCurSel(n);
  184. }
  185. }
  186. }
  187. m_outPlug.SetRedraw(TRUE);
  188. m_outPlug.Invalidate(FALSE);
  189. m_plugParam.Invalidate(FALSE);
  190. }
  191. }
  192. void LFOPluginEditor::InitSlider(CSliderCtrl &slider, LFOPlugin::Parameters param)
  193. {
  194. slider.SetRange(0, SLIDER_GRANULARITY);
  195. slider.SetTicFreq(SLIDER_GRANULARITY / 10);
  196. SetSliderValue(slider, m_lfoPlugin.GetParameter(param));
  197. SetSliderText(param);
  198. }
  199. void LFOPluginEditor::SetSliderText(LFOPlugin::Parameters param)
  200. {
  201. CString s = m_lfoPlugin.GetParamName(param) + _T(": ") + m_lfoPlugin.GetFormattedParamValue(param);
  202. SetDlgItemText(IDC_STATIC1 + param, s);
  203. }
  204. void LFOPluginEditor::SetSliderValue(CSliderCtrl &slider, float value)
  205. {
  206. slider.SetPos(mpt::saturate_round<int>(value * SLIDER_GRANULARITY));
  207. }
  208. float LFOPluginEditor::GetSliderValue(CSliderCtrl &slider)
  209. {
  210. float value = slider.GetPos() / static_cast<float>(SLIDER_GRANULARITY);
  211. return value;
  212. }
  213. void LFOPluginEditor::OnHScroll(UINT nCode, UINT nPos, CScrollBar *pSB)
  214. {
  215. CAbstractVstEditor::OnHScroll(nCode, nPos, pSB);
  216. if(!m_locked && nCode != SB_ENDSCROLL && pSB != nullptr)
  217. {
  218. auto slider = reinterpret_cast<CSliderCtrl *>(pSB);
  219. LFOPlugin::Parameters param;
  220. if(slider == &m_amplitudeSlider)
  221. param = LFOPlugin::kAmplitude;
  222. else if(slider == &m_offsetSlider)
  223. param = LFOPlugin::kOffset;
  224. else if(slider == &m_frequencySlider)
  225. param = LFOPlugin::kFrequency;
  226. else
  227. return;
  228. float value = GetSliderValue(*slider);
  229. m_lfoPlugin.SetParameter(param, value);
  230. m_lfoPlugin.AutomateParameter(param);
  231. SetSliderText(param);
  232. }
  233. }
  234. void LFOPluginEditor::OnPolarityChanged()
  235. {
  236. if(!m_locked)
  237. {
  238. m_lfoPlugin.m_polarity = IsDlgButtonChecked(IDC_CHECK1) != BST_UNCHECKED;
  239. m_lfoPlugin.AutomateParameter(LFOPlugin::kPolarity);
  240. }
  241. }
  242. void LFOPluginEditor::OnTempoSyncChanged()
  243. {
  244. if(!m_locked)
  245. {
  246. m_lfoPlugin.m_tempoSync = IsDlgButtonChecked(IDC_CHECK2) != BST_UNCHECKED;
  247. m_lfoPlugin.RecalculateFrequency();
  248. m_lfoPlugin.AutomateParameter(LFOPlugin::kTempoSync);
  249. InitSlider(m_frequencySlider, LFOPlugin::kFrequency);
  250. }
  251. }
  252. void LFOPluginEditor::OnBypassChanged()
  253. {
  254. if(!m_locked)
  255. {
  256. m_lfoPlugin.m_bypassed = IsDlgButtonChecked(IDC_CHECK3) != BST_UNCHECKED;
  257. m_lfoPlugin.AutomateParameter(LFOPlugin::kBypassed);
  258. }
  259. }
  260. void LFOPluginEditor::OnLoopModeChanged()
  261. {
  262. if(!m_locked)
  263. {
  264. m_lfoPlugin.m_oneshot = IsDlgButtonChecked(IDC_CHECK4) != BST_UNCHECKED;
  265. m_lfoPlugin.AutomateParameter(LFOPlugin::kLoopMode);
  266. }
  267. }
  268. void LFOPluginEditor::OnWaveformChanged(UINT nID)
  269. {
  270. if(!m_locked)
  271. {
  272. m_lfoPlugin.m_waveForm = static_cast<LFOPlugin::LFOWaveform>(nID - IDC_RADIO1);
  273. m_lfoPlugin.AutomateParameter(LFOPlugin::kWaveform);
  274. }
  275. }
  276. void LFOPluginEditor::OnPlugParameterChanged()
  277. {
  278. if(!m_locked)
  279. {
  280. CheckRadioButton(IDC_RADIO7, IDC_RADIO8, IDC_RADIO7);
  281. OnParameterChanged();
  282. }
  283. }
  284. void LFOPluginEditor::OnMidiCCChanged()
  285. {
  286. if(!m_locked)
  287. {
  288. CheckRadioButton(IDC_RADIO7, IDC_RADIO8, IDC_RADIO8);
  289. OnParameterChanged();
  290. }
  291. }
  292. void LFOPluginEditor::OnParameterChanged()
  293. {
  294. if(!m_locked)
  295. {
  296. m_locked = true;
  297. PlugParamIndex param = 0;
  298. bool outputToCC = IsDlgButtonChecked(IDC_RADIO8) != BST_UNCHECKED;
  299. if(outputToCC)
  300. {
  301. param = ((Clamp(GetDlgItemInt(IDC_EDIT1), 1u, 16u) - 1) << 8);
  302. if(m_lfoPlugin.m_outputToCC || m_midiCC.GetCurSel() >= 0)
  303. param |= (m_midiCC.GetCurSel() & 0x7F);
  304. } else
  305. {
  306. if(!m_lfoPlugin.m_outputToCC || m_plugParam.GetCurSel() >= 0)
  307. param = static_cast<PlugParamIndex>(m_plugParam.GetItemData(m_plugParam.GetCurSel()));
  308. }
  309. m_lfoPlugin.m_outputToCC = outputToCC;
  310. m_lfoPlugin.m_outputParam = param;
  311. m_lfoPlugin.SetModified();
  312. m_locked = false;
  313. }
  314. }
  315. void LFOPluginEditor::OnOutputPlugChanged()
  316. {
  317. if(!m_locked)
  318. {
  319. PLUGINDEX plug = static_cast<PLUGINDEX>(m_outPlug.GetItemData(m_outPlug.GetCurSel()));
  320. if(plug > m_lfoPlugin.GetSlot())
  321. {
  322. m_lfoPlugin.GetSoundFile().m_MixPlugins[m_lfoPlugin.GetSlot()].SetOutputPlugin(plug);
  323. m_lfoPlugin.SetModified();
  324. UpdateParamDisplays();
  325. }
  326. }
  327. }
  328. void LFOPluginEditor::OnPluginEditor()
  329. {
  330. std::vector<IMixPlugin *> plug;
  331. if(m_lfoPlugin.GetOutputPlugList(plug) && plug.front() != nullptr)
  332. {
  333. plug.front()->ToggleEditor();
  334. }
  335. }
  336. LRESULT LFOPluginEditor::OnUpdateParam(WPARAM wParam, LPARAM lParam)
  337. {
  338. if(wParam == m_lfoPlugin.GetSlot())
  339. {
  340. UpdateParam(static_cast<int32>(lParam));
  341. }
  342. return 0;
  343. }
  344. OPENMPT_NAMESPACE_END