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.

375 lines
9.6KB

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