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.

371 lines
9.7KB

  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. using namespace std;
  23. ////////////////////////////////////////////
  24. ControllerPluginGUI::CVGUI::CVGUI(int n, ControllerPluginGUI *p, Fl_Color SelColour)
  25. {
  26. m_SliderGroup = new Fl_Group(0,0,60,153,"");
  27. //m_SliderGroup->box (FL_PLASTIC_UP_BOX);
  28. m_SliderGroup->user_data((void *)p);
  29. m_Title = new Fl_Input(5,2,50,15,"");
  30. m_Title->value("Name");
  31. m_Title->textsize(10);
  32. m_Title->callback((Fl_Callback*)ControllerPluginGUI::cb_Title,
  33. (void*)&Numbers[n]);
  34. m_SliderGroup->add(m_Title);
  35. m_Max = new Fl_Input(5,18,50,15,"");
  36. char t[64];
  37. sprintf(t,"%.6f",1.0f);
  38. m_Max->value(t);
  39. m_Max->textsize(10);
  40. m_Max->callback((Fl_Callback*)ControllerPluginGUI::cb_Max,
  41. (void*)&Numbers[n]);
  42. m_SliderGroup->add(m_Max);
  43. m_Chan = new Fl_Slider(20, 34, 20, 100, "");
  44. m_Chan->type (FL_VERT_NICE_SLIDER);
  45. m_Chan->box (FL_PLASTIC_DOWN_BOX);
  46. m_Chan->selection_color (SelColour);
  47. m_Chan->maximum(1);
  48. m_Chan->step(0.01);
  49. m_Chan->value(0.5);
  50. m_Chan->callback((Fl_Callback*)ControllerPluginGUI::cb_Chan,
  51. (void*)&Numbers[n]);
  52. m_SliderGroup->add(m_Chan);
  53. m_Min = new Fl_Input(5,136,50,15,"");
  54. char t2[64];
  55. sprintf(t2,"%.6f",-1.0f);
  56. m_Min->value(t2);
  57. m_Min->textsize(10);
  58. m_Min->callback((Fl_Callback*)ControllerPluginGUI::cb_Min,
  59. (void*)&Numbers[n]);
  60. m_SliderGroup->add(m_Min);
  61. }
  62. ////////////////////////////////////////////
  63. ControllerPluginGUI::ControllerPluginGUI(int w, int h,ControllerPlugin *o,ChannelHandler *ch,const HostInfo *Info) :
  64. SpiralPluginGUI(w,h,o,ch),
  65. m_CVCount(0)
  66. {
  67. for (int n=0; n<MAX_CHANNELS; n++)
  68. {
  69. Numbers[n]=n;
  70. }
  71. m_GUIColour = (Fl_Color)Info->GUI_COLOUR;
  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->box (FL_PLASTIC_UP_BOX);
  85. m_Delete->color (m_GUIColour);
  86. m_Delete->callback ((Fl_Callback*)cb_Delete);
  87. m_Buttons->add (m_Delete);
  88. m_Add = new Fl_Button (24, 0, 20, 20, "+");
  89. m_Add->box (FL_PLASTIC_UP_BOX);
  90. m_Add->color (m_GUIColour);
  91. m_Add->callback ((Fl_Callback*)cb_Add);
  92. m_Buttons->add (m_Add);
  93. }
  94. void ControllerPluginGUI::AddCV()
  95. {
  96. CVGUI *NewCV = new CVGUI (m_CVCount, this, m_GUIColour);
  97. m_GUIVec.push_back(NewCV);
  98. m_MainPack->add(NewCV->m_SliderGroup);
  99. m_CVCount++;
  100. }
  101. void ControllerPluginGUI::DeleteCV()
  102. {
  103. vector<CVGUI*>::iterator i=m_GUIVec.end();
  104. i--;
  105. m_MainPack->remove((*i)->m_SliderGroup);
  106. delete *i;
  107. m_GUIVec.erase(i);
  108. m_CVCount--;
  109. }
  110. void ControllerPluginGUI::Clear()
  111. {
  112. for (vector<ControllerPluginGUI::CVGUI*>::iterator i=m_GUIVec.begin();
  113. i!=m_GUIVec.end(); i++)
  114. {
  115. m_MainPack->remove((*i)->m_SliderGroup);
  116. delete *i;
  117. }
  118. m_GUIVec.clear();
  119. m_CVCount=0;
  120. }
  121. void ControllerPluginGUI::UpdateValues(SpiralPlugin *o)
  122. {
  123. ControllerPlugin *Plugin = (ControllerPlugin *)o;
  124. int c;
  125. float min, max, val;
  126. string Title,Min,Max;
  127. char temp[64];
  128. Clear();
  129. c=Plugin->GetNum();
  130. for (int n=0; n<c; n++) {
  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. if (m_CVCount<MAX_CHANNELS) {
  216. AddCV();
  217. int num = (int)m_GUIVec.size();
  218. float min = atof(m_GUIVec[num - 1]->m_Min->value());
  219. float max = atof(m_GUIVec[num - 1]->m_Max->value());
  220. float val = (1.0f-o->value())*(max-min)+min;
  221. char temp[256];
  222. sprintf(temp,"%s",m_GUIVec[num - 1]->m_Title->value());
  223. m_GUICH->Set("Number", num);
  224. m_GUICH->SetCommand(ControllerPlugin::SETNUM);
  225. m_GUICH->Wait();
  226. m_GUICH->Set("Number", num);
  227. m_GUICH->SetData("Name",(void*)temp);
  228. m_GUICH->Set("Max",max);
  229. m_GUICH->Set("Value",val);
  230. m_GUICH->Set("Min",min);
  231. m_GUICH->SetCommand(ControllerPlugin::SETALL);
  232. m_GUICH->Wait();
  233. resize(x(),y(),w()+60,h());
  234. }
  235. }
  236. void ControllerPluginGUI::cb_Add(Fl_Button* o, void* v)
  237. { ((ControllerPluginGUI*)(o->parent()->parent()))->cb_Add_i(o,v);}
  238. inline void ControllerPluginGUI::cb_Delete_i(Fl_Button* o, void* v) {
  239. if (m_GUIVec.size()>1) {
  240. DeleteCV();
  241. m_GUICH->Set("Number",(int)m_GUIVec.size());
  242. m_GUICH->SetCommand(ControllerPlugin::SETNUM);
  243. m_GUICH->Wait();
  244. resize(x(),y(),w()-60,h());
  245. }
  246. }
  247. void ControllerPluginGUI::cb_Delete(Fl_Button* o, void* v)
  248. { ((ControllerPluginGUI*)(o->parent()->parent()))->cb_Delete_i(o,v);}
  249. // call for version <3
  250. istream &operator>>(istream &s, ControllerPluginGUI &o)
  251. {
  252. int c;
  253. string Title,Min,Max;
  254. float Val;
  255. o.Clear();
  256. s>>c;
  257. for (int n=0; n<c; n++)
  258. {
  259. s>>Title>>Min>>Max>>Val;
  260. o.AddCV();
  261. o.m_GUIVec[n]->m_Title->value(Title.c_str());
  262. o.m_GUIVec[n]->m_Min->value(Min.c_str());
  263. o.m_GUIVec[n]->m_Max->value(Max.c_str());
  264. o.m_GUIVec[n]->m_Chan->value(Val);
  265. }
  266. o.resize(o.x(),o.y(),c*60,o.h());
  267. return s;
  268. }
  269. // call for version >=3
  270. // another version of the stream to fix the old style string streaming problem
  271. void ControllerPluginGUI::StreamIn(istream &s)
  272. {
  273. int c,version,size;
  274. string Title,Min,Max;
  275. float Val;
  276. char Buf[4096];
  277. Clear();
  278. s>>version;
  279. s>>c;
  280. for (int n=0; n<c; n++)
  281. {
  282. AddCV();
  283. s>>size;
  284. s.ignore(1);
  285. s.get(Buf,size+1);
  286. m_GUIVec[n]->m_Title->value(Buf);
  287. s>>Min>>Max>>Val;
  288. m_GUIVec[n]->m_Min->value(Min.c_str());
  289. m_GUIVec[n]->m_Max->value(Max.c_str());
  290. m_GUIVec[n]->m_Chan->value(Val);
  291. }
  292. resize(x(),y(),c*60,h());
  293. }
  294. void ControllerPluginGUI::StreamOut(ostream &s)
  295. {
  296. // version
  297. s<<1<<endl;
  298. s<<m_GUIVec.size()<<endl;
  299. for (vector<ControllerPluginGUI::CVGUI*>::iterator i=m_GUIVec.begin();
  300. i!=m_GUIVec.end(); i++)
  301. {
  302. s<<strlen((*i)->m_Title->value())<<endl;
  303. s<<(*i)->m_Title->value()<<" ";
  304. s<<(*i)->m_Min->value()<<" ";
  305. s<<(*i)->m_Max->value()<<" ";
  306. s<<(*i)->m_Chan->value()<<endl;
  307. }
  308. }
  309. const string ControllerPluginGUI::GetHelpText(const string &loc){
  310. return string("")
  311. + "This is a simple plugin to allow you to generate CV values\n"
  312. + "interatively with sliders in the plugin window. Useful if you\n"
  313. + "can't use Midi, or for controlling LADSPA plugins. The slider\n"
  314. + "ranges can be set, and titles can be given to each slider.\n"
  315. + "You can add or delete sliders from the plugin using the\n"
  316. + "+ or - buttons.\n";
  317. }