You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

372 lines
9.4KB

  1. /* SpiralPlugin
  2. * Copyleft (C) 2000 David Griffiths <dave@pawfal.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include <stdio.h>
  19. #include "ControllerPluginGUI.h"
  20. #include <FL/fl_draw.h>
  21. #include <FL/fl_draw.H>
  22. static const int GUI_COLOUR = 179;
  23. static const int GUIBG_COLOUR = 144;
  24. static const int GUIBG2_COLOUR = 145;
  25. ////////////////////////////////////////////
  26. ControllerPluginGUI::CVGUI::CVGUI(int n, ControllerPluginGUI *p)
  27. {
  28. m_SliderGroup = new Fl_Group(0,0,60,153,"");
  29. m_SliderGroup->box(FL_UP_BOX);
  30. m_SliderGroup->user_data((void *)p);
  31. m_Title = new Fl_Input(5,2,50,15,"");
  32. m_Title->value("Name");
  33. m_Title->textsize(10);
  34. m_Title->callback((Fl_Callback*)ControllerPluginGUI::cb_Title,
  35. (void*)&Numbers[n]);
  36. m_SliderGroup->add(m_Title);
  37. m_Max = new Fl_Input(5,18,50,15,"");
  38. char t[64];
  39. sprintf(t,"%.6f",1.0f);
  40. m_Max->value(t);
  41. m_Max->textsize(10);
  42. m_Max->callback((Fl_Callback*)ControllerPluginGUI::cb_Max,
  43. (void*)&Numbers[n]);
  44. m_SliderGroup->add(m_Max);
  45. m_Chan = new Fl_Slider(20, 34, 20, 100, "");
  46. m_Chan->type(4);
  47. m_Chan->selection_color(GUI_COLOUR);
  48. m_Chan->maximum(1);
  49. m_Chan->step(0.01);
  50. m_Chan->value(0.5);
  51. m_Chan->callback((Fl_Callback*)ControllerPluginGUI::cb_Chan,
  52. (void*)&Numbers[n]);
  53. m_SliderGroup->add(m_Chan);
  54. m_Min = new Fl_Input(5,136,50,15,"");
  55. char t2[64];
  56. sprintf(t2,"%.6f",-1.0f);
  57. m_Min->value(t2);
  58. m_Min->textsize(10);
  59. m_Min->callback((Fl_Callback*)ControllerPluginGUI::cb_Min,
  60. (void*)&Numbers[n]);
  61. m_SliderGroup->add(m_Min);
  62. }
  63. ////////////////////////////////////////////
  64. ControllerPluginGUI::ControllerPluginGUI(int w, int h,ControllerPlugin *o,ChannelHandler *ch,const HostInfo *Info) :
  65. SpiralPluginGUI(w,h,o,ch),
  66. m_CVCount(0)
  67. {
  68. for (int n=0; n<MAX_CHANNELS; n++)
  69. {
  70. Numbers[n]=n;
  71. }
  72. m_MainPack = new Fl_Pack(0,20,w,h-44);
  73. m_MainPack->type(FL_HORIZONTAL);
  74. add (m_MainPack);
  75. // start with four...
  76. AddCV();
  77. AddCV();
  78. AddCV();
  79. AddCV();
  80. m_Buttons = new Fl_Pack (0, h-22, 45, 20);
  81. m_Buttons->type(FL_HORIZONTAL);
  82. add (m_Buttons);
  83. m_Delete = new Fl_Button(2,0,20,20,"-");
  84. m_Delete->callback((Fl_Callback*)cb_Delete);
  85. m_Buttons->add(m_Delete);
  86. m_Add = new Fl_Button(24,0,20,20,"+");
  87. m_Add->callback((Fl_Callback*)cb_Add);
  88. m_Buttons->add(m_Add);
  89. }
  90. void ControllerPluginGUI::AddCV()
  91. {
  92. CVGUI *NewCV = new CVGUI(m_CVCount,this);
  93. m_GUIVec.push_back(NewCV);
  94. m_MainPack->add(NewCV->m_SliderGroup);
  95. m_CVCount++;
  96. }
  97. void ControllerPluginGUI::DeleteCV()
  98. {
  99. vector<CVGUI*>::iterator i=m_GUIVec.end();
  100. i--;
  101. m_MainPack->remove((*i)->m_SliderGroup);
  102. delete *i;
  103. m_GUIVec.erase(i);
  104. m_CVCount--;
  105. }
  106. void ControllerPluginGUI::Clear()
  107. {
  108. for (vector<ControllerPluginGUI::CVGUI*>::iterator i=m_GUIVec.begin();
  109. i!=m_GUIVec.end(); i++)
  110. {
  111. m_MainPack->remove((*i)->m_SliderGroup);
  112. delete *i;
  113. }
  114. m_GUIVec.clear();
  115. m_CVCount=0;
  116. }
  117. void ControllerPluginGUI::UpdateValues(SpiralPlugin *o)
  118. {
  119. ControllerPlugin *Plugin = (ControllerPlugin *)o;
  120. int c;
  121. float min, max, val;
  122. string Title,Min,Max;
  123. char temp[64];
  124. Clear();
  125. c=Plugin->GetNum();
  126. for (int n=0; n<c; n++)
  127. {
  128. AddCV();
  129. m_GUIVec[n]->m_Title->value(Plugin->GetName(n).c_str());
  130. min = Plugin->GetMin(n);
  131. max = Plugin->GetMax(n);
  132. sprintf(temp,"%.6f",min);
  133. m_GUIVec[n]->m_Min->value(temp);
  134. sprintf(temp,"%.6f",max);
  135. m_GUIVec[n]->m_Max->value(temp);
  136. // Scale and invert value to match slider range (0->1)
  137. float val = 1.0f - (Plugin->GetVal(n) - min) / (max - min);
  138. m_GUIVec[n]->m_Chan->value(val);
  139. }
  140. resize(x(),y(),c*60,h());
  141. }
  142. inline void ControllerPluginGUI::cb_Title_i(Fl_Input* o, void* v)
  143. {
  144. int num=*(int*)(v);
  145. char temp[256];
  146. sprintf(temp,"%s",m_GUIVec[num]->m_Title->value());
  147. m_GUICH->Set("Number",num);
  148. m_GUICH->SetData("Name",(void*)temp);
  149. m_GUICH->SetCommand(ControllerPlugin::SETNAME);
  150. }
  151. void ControllerPluginGUI::cb_Title(Fl_Input* o, void* v)
  152. { ((ControllerPluginGUI*)(o->parent()->user_data()))->cb_Title_i(o,v);}
  153. inline void ControllerPluginGUI::cb_Max_i(Fl_Input* o, void* v)
  154. {
  155. int num=*(int*)(v);
  156. float min = atof(m_GUIVec[num]->m_Min->value());
  157. float max = atof(m_GUIVec[num]->m_Max->value());
  158. if (min > max) {
  159. // Swap values if arse over tit...
  160. float temp = min;
  161. char t[64];
  162. min = max;
  163. max = temp;
  164. sprintf(t,"%.6f",min);
  165. m_GUIVec[num]->m_Min->value(t);
  166. sprintf(t,"%.6f",max);
  167. m_GUIVec[num]->m_Max->value(t);
  168. }
  169. m_GUICH->Set("Number",num);
  170. m_GUICH->Set("Max",max);
  171. m_GUICH->SetCommand(ControllerPlugin::SETMAX);
  172. }
  173. void ControllerPluginGUI::cb_Max(Fl_Input* o, void* v)
  174. { ((ControllerPluginGUI*)(o->parent()->user_data()))->cb_Max_i(o,v);}
  175. inline void ControllerPluginGUI::cb_Chan_i(Fl_Slider* o, void* v)
  176. {
  177. int num=*(int*)(v);
  178. // swap em over, cos it's the easiqest way to reverse
  179. // the fltk slider, which is upside down imho
  180. float min = atof(m_GUIVec[num]->m_Min->value());
  181. float max = atof(m_GUIVec[num]->m_Max->value());
  182. float val = (1.0f-o->value())*(max-min)+min;
  183. m_GUICH->Set("Number",num);
  184. m_GUICH->Set("Value",val);
  185. m_GUICH->SetCommand(ControllerPlugin::SETCHANNEL);
  186. }
  187. void ControllerPluginGUI::cb_Chan(Fl_Slider* o, void* v)
  188. { ((ControllerPluginGUI*)(o->parent()->user_data()))->cb_Chan_i(o,v);}
  189. inline void ControllerPluginGUI::cb_Min_i(Fl_Input* o, void* v)
  190. {
  191. int num=*(int*)(v);
  192. float min = atof(m_GUIVec[num]->m_Min->value());
  193. float max = atof(m_GUIVec[num]->m_Max->value());
  194. if (min > max) {
  195. // Swap values if arse over tit...
  196. float temp = min;
  197. char t[64];
  198. min = max;
  199. max = temp;
  200. sprintf(t,"%.6f",min);
  201. m_GUIVec[num]->m_Min->value(t);
  202. sprintf(t,"%.6f",max);
  203. m_GUIVec[num]->m_Max->value(t);
  204. }
  205. m_GUICH->Set("Number",num);
  206. m_GUICH->Set("Min",min);
  207. m_GUICH->SetCommand(ControllerPlugin::SETMIN);
  208. }
  209. void ControllerPluginGUI::cb_Min(Fl_Input* o, void* v)
  210. { ((ControllerPluginGUI*)(o->parent()->user_data()))->cb_Min_i(o,v);}
  211. inline void ControllerPluginGUI::cb_Add_i(Fl_Button* o, void* v)
  212. {
  213. if (m_CVCount<MAX_CHANNELS)
  214. {
  215. AddCV();
  216. resize(x(),y(),w()+60,h());
  217. redraw();
  218. int num = (int)m_GUIVec.size();
  219. float min = atof(m_GUIVec[num - 1]->m_Min->value());
  220. float max = atof(m_GUIVec[num - 1]->m_Max->value());
  221. float val = (1.0f-o->value())*(max-min)+min;
  222. char temp[256];
  223. sprintf(temp,"%s",m_GUIVec[num - 1]->m_Title->value());
  224. m_GUICH->Set("Number", num);
  225. m_GUICH->SetCommand(ControllerPlugin::SETNUM);
  226. m_GUICH->Wait();
  227. m_GUICH->Set("Number", num);
  228. m_GUICH->SetData("Name",(void*)temp);
  229. m_GUICH->Set("Max",max);
  230. m_GUICH->Set("Value",val);
  231. m_GUICH->Set("Min",min);
  232. m_GUICH->SetCommand(ControllerPlugin::SETALL);
  233. }
  234. }
  235. void ControllerPluginGUI::cb_Add(Fl_Button* o, void* v)
  236. { ((ControllerPluginGUI*)(o->parent()->parent()))->cb_Add_i(o,v);}
  237. inline void ControllerPluginGUI::cb_Delete_i(Fl_Button* o, void* v)
  238. {
  239. if (m_GUIVec.size()>1)
  240. {
  241. DeleteCV();
  242. resize(x(),y(),w()-60,h());
  243. redraw();
  244. m_GUICH->Set("Number",(int)m_GUIVec.size());
  245. m_GUICH->SetCommand(ControllerPlugin::SETNUM);
  246. }
  247. }
  248. void ControllerPluginGUI::cb_Delete(Fl_Button* o, void* v)
  249. { ((ControllerPluginGUI*)(o->parent()->parent()))->cb_Delete_i(o,v);}
  250. // call for version <3
  251. istream &operator>>(istream &s, ControllerPluginGUI &o)
  252. {
  253. int c;
  254. string Title,Min,Max;
  255. float Val;
  256. o.Clear();
  257. s>>c;
  258. for (int n=0; n<c; n++)
  259. {
  260. s>>Title>>Min>>Max>>Val;
  261. o.AddCV();
  262. o.m_GUIVec[n]->m_Title->value(Title.c_str());
  263. o.m_GUIVec[n]->m_Min->value(Min.c_str());
  264. o.m_GUIVec[n]->m_Max->value(Max.c_str());
  265. o.m_GUIVec[n]->m_Chan->value(Val);
  266. }
  267. o.resize(o.x(),o.y(),c*60,o.h());
  268. return s;
  269. }
  270. // call for version >=3
  271. // another version of the stream to fix the old style string streaming problem
  272. void ControllerPluginGUI::StreamIn(istream &s)
  273. {
  274. int c,version,size;
  275. string Title,Min,Max;
  276. float Val;
  277. char Buf[4096];
  278. Clear();
  279. s>>version;
  280. s>>c;
  281. for (int n=0; n<c; n++)
  282. {
  283. AddCV();
  284. s>>size;
  285. s.ignore(1);
  286. s.get(Buf,size+1);
  287. m_GUIVec[n]->m_Title->value(Buf);
  288. s>>Min>>Max>>Val;
  289. m_GUIVec[n]->m_Min->value(Min.c_str());
  290. m_GUIVec[n]->m_Max->value(Max.c_str());
  291. m_GUIVec[n]->m_Chan->value(Val);
  292. }
  293. resize(x(),y(),c*60,h());
  294. }
  295. void ControllerPluginGUI::StreamOut(ostream &s)
  296. {
  297. // version
  298. s<<1<<endl;
  299. s<<m_GUIVec.size()<<endl;
  300. for (vector<ControllerPluginGUI::CVGUI*>::iterator i=m_GUIVec.begin();
  301. i!=m_GUIVec.end(); i++)
  302. {
  303. s<<strlen((*i)->m_Title->value())<<endl;
  304. s<<(*i)->m_Title->value()<<" ";
  305. s<<(*i)->m_Min->value()<<" ";
  306. s<<(*i)->m_Max->value()<<" ";
  307. s<<(*i)->m_Chan->value()<<endl;
  308. }
  309. }
  310. const string ControllerPluginGUI::GetHelpText(const string &loc){
  311. return string("")
  312. + "This is a simple plugin to allow you to generate CV values\n"
  313. + "interatively with sliders in the plugin window. Useful if you\n"
  314. + "can't use Midi, or for controlling LADSPA plugins. The slider\n"
  315. + "ranges can be set, and titles can be given to each slider.\n"
  316. + "You can add or delete sliders from the plugin using the\n"
  317. + "+ or - buttons.\n";
  318. }