DPF Plugin examples
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.

285 lines
7.2KB

  1. /*
  2. * Author: Harry van Haaren 2014
  3. * harryhaaren@gmail.com
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. * MA 02110-1301, USA.
  19. *
  20. */
  21. #ifndef AVTK_PARAMETERIC_H
  22. #define AVTK_PARAMETERIC_H
  23. #include <FL/Fl_Widget.H>
  24. #include <FL/Fl_Slider.H>
  25. #include <FL/Fl_Menu_Item.H>
  26. #include <valarray>
  27. #include <string>
  28. namespace Avtk
  29. {
  30. class Parameteric : public Fl_Slider
  31. {
  32. public:
  33. Parameteric(int _x, int _y, int _w, int _h, const char *_label = 0):
  34. Fl_Slider(_x, _y, _w, _h, _label)
  35. {
  36. x = _x;
  37. y = _y;
  38. w = _w;
  39. h = _h;
  40. label = _label;
  41. mouseClickedX = 0;
  42. mouseClickedY = 0;
  43. mouseClicked = false;
  44. active = true;
  45. highlight = false;
  46. gains[0] = 0.f;
  47. gains[1] = 0.f;
  48. gains[2] = 0.f;
  49. gains[3] = 0.f;
  50. gains[4] = 0.f;
  51. }
  52. bool active;
  53. bool highlight;
  54. int x, y, w, h;
  55. const char* label;
  56. int mouseClickedX;
  57. int mouseClickedY;
  58. bool mouseClicked;
  59. bool mouseRightClicked;
  60. float gains[5];
  61. void setGain( int s, float g)
  62. {
  63. gains[s] = g;
  64. redraw();
  65. }
  66. float getVolume()
  67. {
  68. return gains[0];
  69. }
  70. void setActive(bool a)
  71. {
  72. active = a;
  73. redraw();
  74. }
  75. bool getActive()
  76. {
  77. return active;
  78. }
  79. void draw()
  80. {
  81. if (damage() & FL_DAMAGE_ALL)
  82. {
  83. cairo_t *cr = Fl::cairo_cc();
  84. cairo_save( cr );
  85. cairo_set_line_width(cr, 1.5);
  86. // fill background
  87. cairo_rectangle( cr, x, y, w, h);
  88. cairo_set_source_rgb( cr, 28 / 255.f, 28 / 255.f , 28 / 255.f );
  89. cairo_fill_preserve( cr );
  90. cairo_clip( cr );
  91. // set up dashed lines, 1 px off, 1 px on
  92. double dashes[1];
  93. dashes[0] = 2.0;
  94. cairo_set_dash ( cr, dashes, 1, 0.0);
  95. cairo_set_line_width( cr, 1.0);
  96. // loop over each 2nd line, drawing dots
  97. cairo_set_line_width(cr, 1.0);
  98. cairo_set_source_rgb(cr, 0.4,0.4,0.4);
  99. for ( int i = 0; i < 4; i++ )
  100. {
  101. cairo_move_to( cr, x + ((w / 4.f)*i), y );
  102. cairo_line_to( cr, x + ((w / 4.f)*i), y + h );
  103. }
  104. for ( int i = 0; i < 4; i++ )
  105. {
  106. cairo_move_to( cr, x , y + ((h / 4.f)*i) );
  107. cairo_line_to( cr, x + w, y + ((h / 4.f)*i) );
  108. }
  109. cairo_set_source_rgba( cr, 66 / 255.f, 66 / 255.f , 66 / 255.f , 0.5 );
  110. cairo_stroke(cr);
  111. cairo_set_dash ( cr, dashes, 0, 0.0);
  112. // draw frequency/amplitude rectangles
  113. for(int i = 0; i < 4; i ++)
  114. {
  115. int startY = y + h/2;
  116. int heightY = h/4 * ( ( (1-gains[i+1])*2)-1 );
  117. cairo_rectangle( cr, x + (w/4)*i, startY, w/4, heightY );
  118. }
  119. cairo_set_line_cap( cr, CAIRO_LINE_CAP_ROUND );
  120. cairo_set_source_rgba( cr, 0 / 255.f, 153 / 255.f , 255 / 255.f , 0.21 );
  121. cairo_fill_preserve(cr);
  122. cairo_set_line_width(cr, 1.8);
  123. cairo_set_source_rgba( cr, 0 / 255.f, 153 / 255.f , 255 / 255.f , 1 );
  124. cairo_stroke(cr);
  125. // draw "master gain line
  126. cairo_move_to( cr, x , y + h/2 + (h/4 *-(gains[0]*2-1)) );
  127. cairo_line_to( cr, x+w, y + h/2 + (h/4 *-(gains[0]*2-1)) );
  128. cairo_set_line_width(cr, 2.1);
  129. cairo_set_source_rgba( cr, 255 / 255.f, 0 / 255.f , 0 / 255.f , 1 );
  130. cairo_stroke(cr);
  131. // stroke outline
  132. cairo_rectangle(cr, x+1, y+1, w-2, h-2);
  133. cairo_set_source_rgba( cr, 126 / 255.f, 126 / 255.f , 126 / 255.f , 0.8 );
  134. cairo_set_line_width(cr, 1.0);
  135. cairo_stroke( cr );
  136. if ( !active )
  137. {
  138. // big grey X
  139. cairo_set_line_width(cr, 20.0);
  140. cairo_set_source_rgba(cr, 0.4,0.4,0.4, 0.7);
  141. cairo_move_to( cr, x + (3 * w / 4.f), y + ( h / 4.f ) );
  142. cairo_line_to( cr, x + (w / 4.f), y + ( 3 *h / 4.f ) );
  143. cairo_move_to( cr, x + (w / 4.f), y + ( h / 4.f ) );
  144. cairo_line_to( cr, x + (3 * w / 4.f), y + ( 3 *h / 4.f ) );
  145. cairo_set_line_cap ( cr, CAIRO_LINE_CAP_BUTT);
  146. cairo_stroke( cr );
  147. }
  148. cairo_restore( cr );
  149. }
  150. }
  151. void resize(int X, int Y, int W, int H)
  152. {
  153. Fl_Widget::resize(X,Y,W,H);
  154. x = X;
  155. y = Y;
  156. w = W;
  157. h = H;
  158. redraw();
  159. }
  160. int handle(int event)
  161. {
  162. switch(event)
  163. {
  164. case FL_PUSH:
  165. highlight = 0;
  166. mouseRightClicked = false;
  167. mouseClickedX = Fl::event_x();
  168. mouseClickedY = Fl::event_y();
  169. if ( Fl::event_button() == FL_RIGHT_MOUSE )
  170. {
  171. active = !active;
  172. redraw();
  173. mouseRightClicked = true;
  174. do_callback();
  175. }
  176. return 1;
  177. case FL_DRAG:
  178. {
  179. if ( Fl::event_state(FL_BUTTON1) )
  180. {
  181. if ( mouseClicked == false ) // catch the "click" event
  182. {
  183. mouseClickedX = Fl::event_x();
  184. mouseClickedY = Fl::event_y();
  185. mouseClicked = true;
  186. }
  187. //float deltaX = mouseClickedX - Fl::event_x();
  188. float deltaY = mouseClickedY - Fl::event_y();
  189. //float valX = value() ;
  190. //valX -= deltaX / 100.f;
  191. float valY = gains[0];
  192. valY += deltaY / 100.f;
  193. //if ( valX > 1.0 ) valX = 1.0;
  194. //if ( valX < 0.0 ) valX = 0.0;
  195. if ( valY > 1.0 ) valY = 1.0;
  196. if ( valY < 0.0 ) valY = 0.0;
  197. //handle_drag( value + deltaY );
  198. //set_value( valX );
  199. gains[0] = valY;
  200. mouseClickedX = Fl::event_x();
  201. mouseClickedY = Fl::event_y();
  202. redraw();
  203. do_callback();
  204. }
  205. }
  206. return 1;
  207. case FL_RELEASE:
  208. mouseRightClicked = false;
  209. if (highlight) {
  210. highlight = 0;
  211. redraw();
  212. do_callback();
  213. }
  214. mouseClicked = false;
  215. return 1;
  216. case FL_SHORTCUT:
  217. if ( test_shortcut() )
  218. {
  219. do_callback();
  220. return 1;
  221. }
  222. return 0;
  223. default:
  224. return Fl_Widget::handle(event);
  225. }
  226. return 0;
  227. }
  228. private:
  229. };
  230. } // Avtk
  231. #endif // AVTK_WAVESHAPER_H