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.

297 lines
7.7KB

  1. /*
  2. * Author: Harry van Haaren 2013
  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_WAVESHAPER_H
  22. #define AVTK_WAVESHAPER_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 Waveshaper : public Fl_Slider
  31. {
  32. public:
  33. Waveshaper(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. envelope = 0.5;
  47. waveshapeType = 0.f;
  48. }
  49. /// holds the preset: used from callback() to write value
  50. float waveshapeType;
  51. void type(float t) {waveshapeType = t; redraw();}
  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 _threshold;
  61. float _sidechainAmp;
  62. float envelope;
  63. void setEnvelope(float e)
  64. {
  65. envelope = e;
  66. redraw();
  67. }
  68. float getEnvelope()
  69. {
  70. return envelope;
  71. }
  72. void setActive(bool a)
  73. {
  74. active = a;
  75. redraw();
  76. }
  77. bool getActive()
  78. {
  79. return active;
  80. }
  81. void draw()
  82. {
  83. if (damage() & FL_DAMAGE_ALL)
  84. {
  85. cairo_t *cr = Fl::cairo_cc();
  86. cairo_save( cr );
  87. cairo_set_line_width(cr, 1.5);
  88. // fill background
  89. cairo_rectangle( cr, x, y, w, h);
  90. cairo_set_source_rgb( cr, 28 / 255.f, 28 / 255.f , 28 / 255.f );
  91. cairo_fill_preserve( cr );
  92. cairo_clip( cr );
  93. // set up dashed lines, 1 px off, 1 px on
  94. double dashes[1];
  95. dashes[0] = 2.0;
  96. cairo_set_dash ( cr, dashes, 1, 0.0);
  97. cairo_set_line_width( cr, 1.0);
  98. // loop over each 2nd line, drawing dots
  99. cairo_set_line_width(cr, 1.0);
  100. cairo_set_source_rgb(cr, 0.4,0.4,0.4);
  101. for ( int i = 0; i < 4; i++ )
  102. {
  103. cairo_move_to( cr, x + ((w / 4.f)*i), y );
  104. cairo_line_to( cr, x + ((w / 4.f)*i), y + h );
  105. }
  106. for ( int i = 0; i < 4; i++ )
  107. {
  108. cairo_move_to( cr, x , y + ((h / 4.f)*i) );
  109. cairo_line_to( cr, x + w, y + ((h / 4.f)*i) );
  110. }
  111. cairo_set_source_rgba( cr, 66 / 255.f, 66 / 255.f , 66 / 255.f , 0.5 );
  112. cairo_stroke(cr);
  113. cairo_set_dash ( cr, dashes, 0, 0.0);
  114. // curved waveshape
  115. float distort = value();
  116. cairo_move_to( cr, x , y + h );
  117. cairo_curve_to( cr, x + w * distort, y+h, // control point 1
  118. x + w - (w * distort), y, // control point 2
  119. x + w, y ); // end of curve 1, start curve 2
  120. cairo_line_to ( cr, x + w, y + h );
  121. cairo_close_path(cr);
  122. cairo_set_source_rgba( cr, 0 / 255.f, 153 / 255.f , 255 / 255.f , 0.21 );
  123. cairo_fill_preserve(cr);
  124. cairo_set_line_width(cr, 2.0);
  125. cairo_set_source_rgba( cr, 0 / 255.f, 153 / 255.f , 255 / 255.f , 1 );
  126. cairo_stroke(cr);
  127. // Tone bars
  128. cairo_move_to( cr, x + w/4, y + 2 );
  129. cairo_line_to( cr, x + w/4, y + h / 2 * envelope );
  130. cairo_move_to( cr, x + w - w/4, y + h - 2 );
  131. cairo_line_to( cr, x + w - w/4, y + h - (h / 2 * envelope) );
  132. //cairo_set_source_rgba (cr, 0.0, 0.48, 1.0, 0.6 );
  133. cairo_set_source_rgba (cr, 0,0,0, 0.7 );
  134. cairo_set_line_width(cr, 21.5);
  135. cairo_set_line_cap( cr, CAIRO_LINE_CAP_ROUND );
  136. cairo_stroke( cr );
  137. // stroke outline
  138. cairo_rectangle(cr, x+1, y+1, w-2, h-2);
  139. cairo_set_source_rgba( cr, 126 / 255.f, 126 / 255.f , 126 / 255.f , 0.8 );
  140. cairo_set_line_width(cr, 1.0);
  141. cairo_stroke( cr );
  142. if ( !active )
  143. {
  144. // big grey X
  145. cairo_set_line_width(cr, 20.0);
  146. cairo_set_source_rgba(cr, 0.4,0.4,0.4, 0.7);
  147. cairo_move_to( cr, x + (3 * w / 4.f), y + ( h / 4.f ) );
  148. cairo_line_to( cr, x + (w / 4.f), y + ( 3 *h / 4.f ) );
  149. cairo_move_to( cr, x + (w / 4.f), y + ( h / 4.f ) );
  150. cairo_line_to( cr, x + (3 * w / 4.f), y + ( 3 *h / 4.f ) );
  151. cairo_set_line_cap ( cr, CAIRO_LINE_CAP_BUTT);
  152. cairo_stroke( cr );
  153. }
  154. cairo_restore( cr );
  155. }
  156. }
  157. void resize(int X, int Y, int W, int H)
  158. {
  159. Fl_Widget::resize(X,Y,W,H);
  160. x = X;
  161. y = Y;
  162. w = W;
  163. h = H;
  164. redraw();
  165. }
  166. int handle(int event)
  167. {
  168. switch(event)
  169. {
  170. case FL_PUSH:
  171. highlight = 0;
  172. mouseRightClicked = false;
  173. mouseClickedX = Fl::event_x();
  174. mouseClickedY = Fl::event_y();
  175. if ( Fl::event_button() == FL_RIGHT_MOUSE )
  176. {
  177. active = !active;
  178. redraw();
  179. mouseRightClicked = true;
  180. do_callback();
  181. }
  182. return 1;
  183. case FL_DRAG:
  184. {
  185. if ( Fl::event_state(FL_BUTTON1) )
  186. {
  187. if ( mouseClicked == false ) // catch the "click" event
  188. {
  189. mouseClickedX = Fl::event_x();
  190. mouseClickedY = Fl::event_y();
  191. mouseClicked = true;
  192. }
  193. float deltaX = mouseClickedX - Fl::event_x();
  194. float deltaY = mouseClickedY - Fl::event_y();
  195. float valX = envelope;
  196. valX -= deltaX / 100.f;
  197. float valY = value();
  198. valY += deltaY / 100.f;
  199. if ( valX > 1.0 ) valX = 1.0;
  200. if ( valX < 0.0 ) valX = 0.0;
  201. if ( valY > 1.0 ) valY = 1.0;
  202. if ( valY < 0.0 ) valY = 0.0;
  203. //handle_drag( value + deltaY );
  204. set_value( valY );
  205. envelope = valX;
  206. mouseClickedX = Fl::event_x();
  207. mouseClickedY = Fl::event_y();
  208. redraw();
  209. do_callback();
  210. }
  211. }
  212. return 1;
  213. case FL_RELEASE:
  214. mouseRightClicked = false;
  215. if (highlight) {
  216. highlight = 0;
  217. redraw();
  218. do_callback();
  219. }
  220. mouseClicked = false;
  221. return 1;
  222. case FL_SHORTCUT:
  223. if ( test_shortcut() )
  224. {
  225. do_callback();
  226. return 1;
  227. }
  228. return 0;
  229. default:
  230. return Fl_Widget::handle(event);
  231. }
  232. return 0;
  233. }
  234. private:
  235. };
  236. } // Avtk
  237. #endif // AVTK_WAVESHAPER_H