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.

184 lines
4.6KB

  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_DIAL_H
  22. #define AVTK_DIAL_H
  23. #include <FL/Fl_Dial.H>
  24. #include <FL/Fl_Slider.H>
  25. namespace Avtk
  26. {
  27. class Dial : public Fl_Slider
  28. {
  29. public:
  30. Dial(int _x, int _y, int _w, int _h, const char* _label=0):
  31. Fl_Slider(_x, _y, _w, _h, _label)
  32. {
  33. x = _x;
  34. y = _y;
  35. w = _w;
  36. h = _h;
  37. // * 0.9 for line width to remain inside redraw area
  38. if ( w > h )
  39. radius = (h / 2.f)*0.9;
  40. else
  41. radius = (w / 2.f)*0.9;
  42. lineWidth = 1.4 + radius / 12.f;
  43. mouseClickedY = 0;
  44. mouseClicked = false;
  45. highlight = false;
  46. label = _label;
  47. }
  48. bool highlight;
  49. int x, y, w, h;
  50. const char* label;
  51. bool drawLab;
  52. void drawLabel(bool b){drawLab = b; redraw();}
  53. float radius;
  54. float lineWidth;
  55. int mouseClickedY;
  56. bool mouseClicked;
  57. void draw()
  58. {
  59. if (damage() & FL_DAMAGE_ALL)
  60. {
  61. cairo_t *cr = Fl::cairo_cc();
  62. cairo_save( cr );
  63. // label behind value
  64. draw_label();
  65. cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
  66. cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
  67. cairo_set_line_width(cr, lineWidth-0.2);
  68. cairo_move_to( cr, x+w/2,y+h/2);
  69. cairo_line_to( cr, x+w/2,y+h/2);
  70. cairo_set_source_rgba(cr, 0.5, 0.5, 0.5, 0.4 );
  71. // draw dash guide
  72. double dashes[2];
  73. cairo_set_line_width(cr, 1.7);
  74. dashes[0] = 3.0;
  75. dashes[1] = 3.0;
  76. cairo_set_dash ( cr, dashes, 2, 0.0);
  77. cairo_stroke(cr);
  78. cairo_arc(cr, x+w/2,y+h/2, radius, 2.46, 0.75 );
  79. //cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.4 );
  80. cairo_stroke(cr);
  81. cairo_set_dash ( cr, dashes, 0, 0.0);
  82. float angle = 2.46 + ( 4.54 * value() );
  83. cairo_set_line_width(cr, lineWidth);
  84. cairo_arc(cr, x+w/2,y+h/2, radius, 2.46, angle );
  85. cairo_line_to(cr, x+w/2,y+h/2);
  86. cairo_set_source_rgba(cr, 1.0, 0.48, 0, 1.0);
  87. cairo_stroke(cr);
  88. cairo_restore( cr );
  89. if ( drawLab )
  90. {
  91. //draw_label();
  92. }
  93. }
  94. }
  95. void resize(int X, int Y, int W, int H)
  96. {
  97. Fl_Slider::resize(X,Y,W,H);
  98. x = X;
  99. y = Y;
  100. w = W;
  101. h = H;
  102. redraw();
  103. }
  104. int handle(int event)
  105. {
  106. //cout << "handle event type = " << event << " value = " << value() << endl;
  107. //Fl_Slider::handle( event );
  108. switch(event) {
  109. case FL_PUSH:
  110. highlight = 1;
  111. redraw();
  112. return 1;
  113. case FL_DRAG:
  114. {
  115. if ( Fl::event_state(FL_BUTTON1) )
  116. {
  117. if ( mouseClicked == false ) // catch the "click" event
  118. {
  119. mouseClickedY = Fl::event_y();
  120. mouseClicked = true;
  121. }
  122. float deltaY = mouseClickedY - Fl::event_y();
  123. float val = value();
  124. val += deltaY / 100.f;
  125. if ( val > 1.0 ) val = 1.0;
  126. if ( val < 0.0 ) val = 0.0;
  127. set_value( val );
  128. mouseClickedY = Fl::event_y();
  129. redraw();
  130. do_callback(); // makes FLTK call "extra" code entered in FLUID
  131. }
  132. }
  133. return 1;
  134. case FL_RELEASE:
  135. if (highlight) {
  136. highlight = 0;
  137. redraw();
  138. // never do anything after a callback, as the callback
  139. // may delete the widget!
  140. }
  141. mouseClicked = false;
  142. return 1;
  143. default:
  144. return Fl_Widget::handle(event);
  145. }
  146. }
  147. };
  148. } // Avtk
  149. #endif // AVTK_DIAL_H