Audio plugin host https://kx.studio/carla
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.

229 lines
5.6KB

  1. // generated by Fast Light User Interface Designer (fluid) version 1.0107f
  2. #include <cmath>
  3. #include <FL/fl_draw.H>
  4. #include "../Misc/Util.h"
  5. #include "WidgetPDial.h"
  6. //Copyright (c) 2003-2005 Nasca Octavian Paul
  7. //License: GNU GPL version 2 or later
  8. //static int numobj = 0;
  9. WidgetPDial::WidgetPDial(int x, int y, int w, int h, const char *label)
  10. :Fl_Dial(x, y, w, h, label), reset_value(0), integer_step(true),
  11. oldvalue(0.0f), pos(false), textset(false), value_offset(0.0),
  12. value_scale(1.0)
  13. {
  14. //cout << "[" << label << "] There are now " << ++numobj << endl;
  15. Fl_Group *save = Fl_Group::current();
  16. tipwin = new TipWin();
  17. tipwin->hide();
  18. Fl_Group::current(save);
  19. }
  20. WidgetPDial::~WidgetPDial()
  21. {
  22. //cout << "There are now " << --numobj << endl;
  23. delete tipwin;
  24. }
  25. void WidgetPDial::setRounding(unsigned int digits)
  26. {
  27. tipwin->setRounding(digits);
  28. }
  29. int WidgetPDial::handle(int event)
  30. {
  31. double dragsize, min = minimum(), max = maximum(), result;
  32. int dy;
  33. if (event == FL_RELEASE && Fl::event_clicks() == 1) {
  34. Fl::event_clicks(0);
  35. value(reset_value);
  36. tipwin->hide();
  37. value_damage();
  38. if (this->when() != 0)
  39. do_callback();
  40. return 1;
  41. }
  42. switch(event) {
  43. case FL_PUSH:
  44. if (integer_step)
  45. setRounding(0);
  46. else if (Fl::event_shift())
  47. setRounding(4);
  48. else
  49. setRounding(Fl::event_button1() ? 2 : 3);
  50. oldvalue = value();
  51. old_y = Fl::event_y();
  52. case FL_DRAG:
  53. getPos();
  54. dy = old_y - Fl::event_y();
  55. if (Fl::event_shift())
  56. dragsize = 20000.0f;
  57. else
  58. dragsize = Fl::event_button1() ? 200.0f : 2000.0f;
  59. value(clamp(oldvalue + dy / dragsize * (max - min)));
  60. tipwin->showValue(transform(value()));
  61. value_damage();
  62. if(this->when() != 0)
  63. do_callback();
  64. return 1;
  65. case FL_MOUSEWHEEL:
  66. if (Fl::belowmouse() != this)
  67. return 1;
  68. dy = - Fl::event_dy();
  69. if (integer_step) {
  70. setRounding(0);
  71. result = (int)(value() + dy * (Fl::event_ctrl() ? 1 : 8));
  72. } else {
  73. float dragsize;
  74. if (Fl::event_shift()) {
  75. dragsize = 10000.0;
  76. setRounding(4);
  77. } else if (Fl::event_ctrl()) {
  78. dragsize = 1000.0;
  79. setRounding(3);
  80. } else {
  81. dragsize = 100.0;
  82. setRounding(2);
  83. }
  84. result = value() + dy / dragsize * (max - min);
  85. }
  86. value(clamp(result));
  87. tipwin->showValue(transform(value()));
  88. value_damage();
  89. if(this->when() != 0)
  90. do_callback();
  91. return 1;
  92. case FL_ENTER:
  93. getPos();
  94. tipwin->showText();
  95. return 1;
  96. case FL_HIDE:
  97. case FL_LEAVE:
  98. tipwin->hide();
  99. resetPos();
  100. break;
  101. case FL_RELEASE:
  102. if (integer_step) {
  103. float rounded = floorf(value() + 0.5);
  104. value(clamp(rounded));
  105. }
  106. tipwin->hide();
  107. resetPos();
  108. if(this->when() == 0)
  109. do_callback();
  110. return 1;
  111. break;
  112. }
  113. return 0;
  114. //#endif
  115. }
  116. void WidgetPDial::draw()
  117. {
  118. #ifdef NTK_GUI
  119. box( FL_NO_BOX );
  120. Fl_Dial::draw();
  121. return;
  122. #else
  123. const int cx = x(), cy = y(), sx = w(), sy = h();
  124. const double a1 = angle1(), a2 = angle2();
  125. const double val = (value() - minimum()) / (maximum() - minimum());
  126. // even radius produces less artifacts if no antialiasing is avail
  127. const int rad = (sx > sy ? sy : sx) &~1;
  128. /* clears the button background */
  129. pdialcolor(160, 160, 160);
  130. fl_pie(cx - 2, cy - 2, rad + 4, rad + 4, 0, 360);
  131. /* dark outline */
  132. fl_color(60, 60, 60);
  133. fl_pie(cx - 1, cy - 1, rad + 2, rad + 2, 0, 360);
  134. /* Draws the button faceplate, min/max */
  135. pdialcolor(110, 110, 115);
  136. fl_pie(cx, cy, rad, rad, 270 - a2, 270 - a1);
  137. /* knob center */
  138. if (rad > 8) {
  139. pdialcolor(140, 140, 145);
  140. fl_pie(cx + 4, cy + 4, rad - 8, rad - 8, 0, 360);
  141. }
  142. /* value circle */
  143. double a = -(a2 - a1) * val - a1;
  144. fl_line_style(0, 2, 0);
  145. pdialcolor(0, 200, 0);
  146. fl_arc(cx + 1, cy + 1, rad - 2, rad - 2, a - 90, a1 - 180);
  147. fl_line_style(0);
  148. /* draw value line */
  149. int ll = rad/4;
  150. if (ll < 2) ll = 2;
  151. fl_push_matrix();
  152. fl_translate(cx + rad / 2, cy + rad / 2);
  153. fl_rotate(a - 90.0f);
  154. fl_translate(rad / 2, 0);
  155. fl_begin_polygon();
  156. pdialcolor(0, 0, 0);
  157. fl_vertex(-ll, 0);
  158. fl_vertex(0, 0);
  159. fl_end_polygon();
  160. fl_pop_matrix();
  161. #endif
  162. }
  163. void WidgetPDial::pdialcolor(int r, int g, int b)
  164. {
  165. if(active_r())
  166. fl_color(r, g, b);
  167. else
  168. fl_color(160 - (160 - r) / 3, 160 - (160 - b) / 3, 160 - (160 - b) / 3);
  169. }
  170. void WidgetPDial::tooltip(const char *c)
  171. {
  172. tipwin->setText(c);
  173. textset = true;
  174. }
  175. void WidgetPDial::getPos()
  176. {
  177. if(!pos) {
  178. tipwin->position(Fl::event_x_root(), Fl::event_y_root() + 20);
  179. pos = true;
  180. }
  181. }
  182. void WidgetPDial::resetPos()
  183. {
  184. pos = false;
  185. }
  186. void WidgetPDial::set_transform(float scale, float offset)
  187. {
  188. value_offset = offset;
  189. value_scale = scale;
  190. }
  191. float WidgetPDial::transform(float x)
  192. {
  193. return value_scale * x + value_offset;
  194. }