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.

369 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. ////////////////////////////////////////////
  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. AddCV();
  131. m_GUIVec[n]->m_Title->value(Plugin->GetName(n).c_str());
  132. min = Plugin->GetMin(n);
  133. max = Plugin->GetMax(n);
  134. sprintf(temp,"%.6f",min);
  135. m_GUIVec[n]->m_Min->value(temp);
  136. sprintf(temp,"%.6f",max);
  137. m_GUIVec[n]->m_Max->value(temp);
  138. // Scale and invert value to match slider range (0->1)
  139. float val = 1.0f - (Plugin->GetVal(n) - min) / (max - min);
  140. m_GUIVec[n]->m_Chan->value(val);
  141. }
  142. resize(x(), y(), c*60, h());
  143. }
  144. inline void ControllerPluginGUI::cb_Title_i(Fl_Input* o, void* v)
  145. {
  146. int num=*(int*)(v);
  147. char temp[256];
  148. sprintf(temp,"%s",m_GUIVec[num]->m_Title->value());
  149. m_GUICH->Set("Number",num);
  150. m_GUICH->SetData("Name",(void*)temp);
  151. m_GUICH->SetCommand(ControllerPlugin::SETNAME);
  152. }
  153. void ControllerPluginGUI::cb_Title(Fl_Input* o, void* v)
  154. { ((ControllerPluginGUI*)(o->parent()->user_data()))->cb_Title_i(o,v);}
  155. inline void ControllerPluginGUI::cb_Max_i(Fl_Input* o, void* v)
  156. {
  157. int num=*(int*)(v);
  158. float min = atof(m_GUIVec[num]->m_Min->value());
  159. float max = atof(m_GUIVec[num]->m_Max->value());
  160. if (min > max) {
  161. // Swap values if arse over tit...
  162. float temp = min;
  163. char t[64];
  164. min = max;
  165. max = temp;
  166. sprintf(t,"%.6f",min);
  167. m_GUIVec[num]->m_Min->value(t);
  168. sprintf(t,"%.6f",max);
  169. m_GUIVec[num]->m_Max->value(t);
  170. }
  171. m_GUICH->Set("Number",num);
  172. m_GUICH->Set("Max",max);
  173. m_GUICH->SetCommand(ControllerPlugin::SETMAX);
  174. }
  175. void ControllerPluginGUI::cb_Max(Fl_Input* o, void* v)
  176. { ((ControllerPluginGUI*)(o->parent()->user_data()))->cb_Max_i(o,v);}
  177. inline void ControllerPluginGUI::cb_Chan_i(Fl_Slider* o, void* v)
  178. {
  179. int num=*(int*)(v);
  180. // swap em over, cos it's the easiqest way to reverse
  181. // the fltk slider, which is upside down imho
  182. float min = atof(m_GUIVec[num]->m_Min->value());
  183. float max = atof(m_GUIVec[num]->m_Max->value());
  184. float val = (1.0f-o->value())*(max-min)+min;
  185. m_GUICH->Set("Number",num);
  186. m_GUICH->Set("Value",val);
  187. m_GUICH->SetCommand(ControllerPlugin::SETCHANNEL);
  188. }
  189. void ControllerPluginGUI::cb_Chan(Fl_Slider* o, void* v)
  190. { ((ControllerPluginGUI*)(o->parent()->user_data()))->cb_Chan_i(o,v);}
  191. inline void ControllerPluginGUI::cb_Min_i(Fl_Input* o, void* v)
  192. {
  193. int num=*(int*)(v);
  194. float min = atof(m_GUIVec[num]->m_Min->value());
  195. float max = atof(m_GUIVec[num]->m_Max->value());
  196. if (min > max) {
  197. // Swap values if arse over tit...
  198. float temp = min;
  199. char t[64];
  200. min = max;
  201. max = temp;
  202. sprintf(t,"%.6f",min);
  203. m_GUIVec[num]->m_Min->value(t);
  204. sprintf(t,"%.6f",max);
  205. m_GUIVec[num]->m_Max->value(t);
  206. }
  207. m_GUICH->Set("Number",num);
  208. m_GUICH->Set("Min",min);
  209. m_GUICH->SetCommand(ControllerPlugin::SETMIN);
  210. }
  211. void ControllerPluginGUI::cb_Min(Fl_Input* o, void* v)
  212. { ((ControllerPluginGUI*)(o->parent()->user_data()))->cb_Min_i(o,v);}
  213. inline void ControllerPluginGUI::cb_Add_i(Fl_Button* o, void* v) {
  214. if (m_CVCount<MAX_CHANNELS) {
  215. AddCV();
  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. m_GUICH->Wait();
  232. resize(x(),y(),w()+60,h());
  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. if (m_GUIVec.size()>1) {
  239. DeleteCV();
  240. m_GUICH->Set("Number",(int)m_GUIVec.size());
  241. m_GUICH->SetCommand(ControllerPlugin::SETNUM);
  242. m_GUICH->Wait();
  243. resize(x(),y(),w()-60,h());
  244. }
  245. }
  246. void ControllerPluginGUI::cb_Delete(Fl_Button* o, void* v)
  247. { ((ControllerPluginGUI*)(o->parent()->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. }