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.

363 lines
10.0KB

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