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.

370 lines
9.3KB

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