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.

WidgetPDial.cpp 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // generated by Fast Light User Interface Designer (fluid) version 1.0107f
  2. #include "WidgetPDial.h"
  3. #include <cstdio>
  4. #include <iostream>
  5. #include <cmath>
  6. #include <string>
  7. #include <FL/Fl_Tooltip.H>
  8. #include <FL/fl_draw.H>
  9. #include <FL/Fl_Group.H>
  10. #include <FL/Fl_Menu_Window.H>
  11. #include "../Misc/Util.h"
  12. //Copyright (c) 2003-2005 Nasca Octavian Paul
  13. //License: GNU GPL version 2 or later
  14. using namespace std;
  15. class TipWin:public Fl_Menu_Window
  16. {
  17. public:
  18. TipWin();
  19. void draw();
  20. void showValue(float f);
  21. void setText(const char *c);
  22. void showText();
  23. private:
  24. void redraw();
  25. const char *getStr() const;
  26. string tip;
  27. string text;
  28. bool textmode;
  29. };
  30. TipWin::TipWin():Fl_Menu_Window(1, 1)
  31. {
  32. set_override();
  33. end();
  34. }
  35. void TipWin::draw()
  36. {
  37. //setup window
  38. draw_box(FL_BORDER_BOX, 0, 0, w(), h(), Fl_Color(175));
  39. fl_color(FL_BLACK);
  40. fl_font(labelfont(), labelsize());
  41. //Draw the current string
  42. fl_draw(getStr(), 3, 3, w() - 6, h() - 6,
  43. Fl_Align(FL_ALIGN_LEFT | FL_ALIGN_WRAP));
  44. }
  45. void TipWin::showValue(float f)
  46. {
  47. //convert the value to a string
  48. char tmp[10];
  49. snprintf(tmp, 9, "%.2f", f);
  50. tip = tmp;
  51. textmode = false;
  52. redraw();
  53. show();
  54. }
  55. void TipWin::setText(const char *c)
  56. {
  57. text = c;
  58. textmode = true;
  59. redraw();
  60. }
  61. void TipWin::showText()
  62. {
  63. if(!text.empty()) {
  64. textmode = true;
  65. redraw();
  66. show();
  67. }
  68. }
  69. void TipWin::redraw()
  70. {
  71. // Recalc size of window
  72. fl_font(labelfont(), labelsize());
  73. int W = 0, H = 0;
  74. fl_measure(getStr(), W, H, 0);
  75. //provide a bit of extra space
  76. W += 8;
  77. H += 4;
  78. size(W, H);
  79. Fl_Menu_Window::redraw();
  80. }
  81. const char *TipWin::getStr() const
  82. {
  83. return (textmode ? text : tip).c_str();
  84. }
  85. //static int numobj = 0;
  86. WidgetPDial::WidgetPDial(int x, int y, int w, int h, const char *label)
  87. :Fl_Dial(x, y, w, h, label), oldvalue(0.0f), pos(false), textset(false)
  88. {
  89. //cout << "[" << label << "] There are now " << ++numobj << endl;
  90. Fl_Group *save = Fl_Group::current();
  91. tipwin = new TipWin();
  92. tipwin->hide();
  93. Fl_Group::current(save);
  94. }
  95. WidgetPDial::~WidgetPDial()
  96. {
  97. //cout << "There are now " << --numobj << endl;
  98. delete tipwin;
  99. }
  100. int WidgetPDial::handle(int event)
  101. {
  102. #ifdef NTK_GUI
  103. return Fl_Dial::handle( event );
  104. #else
  105. double dragsize, min = minimum(), max = maximum();
  106. int my;
  107. switch(event) {
  108. case FL_PUSH:
  109. oldvalue = value();
  110. case FL_DRAG:
  111. getPos();
  112. tipwin->showValue(value());
  113. my = -(Fl::event_y() - y() - h() / 2);
  114. dragsize = 200.0f;
  115. if(Fl::event_state(FL_BUTTON1) == 0)
  116. dragsize *= 10;
  117. value(limit(oldvalue + my / dragsize * (max - min), min, max));
  118. value_damage();
  119. if(this->when() != 0)
  120. do_callback();
  121. return 1;
  122. case FL_ENTER:
  123. getPos();
  124. tipwin->showText();
  125. return 1;
  126. case FL_HIDE:
  127. case FL_LEAVE:
  128. tipwin->hide();
  129. resetPos();
  130. break;
  131. case FL_RELEASE:
  132. tipwin->hide();
  133. resetPos();
  134. if(this->when() == 0)
  135. do_callback();
  136. return 1;
  137. break;
  138. }
  139. return 0;
  140. #endif
  141. }
  142. void WidgetPDial::drawgradient(int cx, int cy, int sx, double m1, double m2)
  143. {
  144. #ifdef NTK_GUI
  145. return;
  146. #else
  147. for(int i = (int)(m1 * sx); i < (int)(m2 * sx); i++) {
  148. double tmp = 1.0f - powf(i * 1.0f / sx, 2.0f);
  149. pdialcolor(140
  150. + (int) (tmp
  151. * 90), 140
  152. + (int)(tmp * 90), 140 + (int) (tmp * 100));
  153. fl_arc(cx + sx / 2 - i / 2, cy + sx / 2 - i / 2, i, i, 0, 360 );
  154. }
  155. #endif
  156. }
  157. void WidgetPDial::draw()
  158. {
  159. #ifdef NTK_GUI
  160. box( FL_NO_BOX );
  161. Fl_Dial::draw();
  162. return;
  163. #else
  164. int cx = x(), cy = y(), sx = w(), sy = h();
  165. //clears the button face
  166. pdialcolor(190, 190, 200);
  167. fl_pie(cx - 1, cy - 1, sx + 2, sy + 2, 0, 360);
  168. /* //Draws the button face (gradinet) */
  169. drawgradient(cx, cy, sx, 0.5f, 1.0f);
  170. double val = (value() - minimum()) / (maximum() - minimum());
  171. //draws the scale
  172. pdialcolor(220, 220, 250);
  173. double a1 = angle1(), a2 = angle2();
  174. for(int i = 0; i < 12; i++) {
  175. double a = -i / 12.0f * 360.0f - val * (a2 - a1) - a1;
  176. fl_pie(cx, cy, sx, sy, a + 270 - 3, a + 3 + 270);
  177. }
  178. drawgradient(cx, cy, sx, 0.0f, 0.75f);
  179. //draws the value
  180. double a = -(a2 - a1) * val - a1;
  181. //draws the max and min points
  182. pdialcolor(0, 100, 200);
  183. int xp =
  184. (int)(cx + sx / 2.0f + sx / 2.0f * sinf(angle1() / 180.0f * 3.141592f));
  185. int yp =
  186. (int)(cy + sy / 2.0f + sy / 2.0f * cosf(angle1() / 180.0f * 3.141592f));
  187. fl_pie(xp - 2, yp - 2, 4, 4, 0, 360);
  188. xp = (int)(cx + sx / 2.0f + sx / 2.0f * sinf(angle2() / 180.0f * 3.141592f));
  189. yp = (int)(cy + sy / 2.0f + sy / 2.0f * cosf(angle2() / 180.0f * 3.141592f));
  190. fl_pie(xp - 2, yp - 2, 4, 4, 0, 360);
  191. fl_push_matrix();
  192. fl_translate(cx + sx / 2, cy + sy / 2);
  193. fl_rotate(a - 90.0f);
  194. fl_translate(sx / 2, 0);
  195. fl_begin_polygon();
  196. pdialcolor(0, 0, 0);
  197. fl_vertex(-10, -4);
  198. fl_vertex(-10, 4);
  199. fl_vertex(0, 0);
  200. fl_end_polygon();
  201. fl_pop_matrix();
  202. #endif
  203. }
  204. void WidgetPDial::pdialcolor(int r, int g, int b)
  205. {
  206. if(active_r())
  207. fl_color(r, g, b);
  208. else
  209. fl_color(160 - (160 - r) / 3, 160 - (160 - b) / 3, 160 - (160 - b) / 3);
  210. }
  211. void WidgetPDial::tooltip(const char *c)
  212. {
  213. tipwin->setText(c);
  214. textset = true;
  215. }
  216. void WidgetPDial::getPos()
  217. {
  218. if(!pos) {
  219. tipwin->position(Fl::event_x_root(), Fl::event_y_root() + 20);
  220. pos = true;
  221. }
  222. }
  223. void WidgetPDial::resetPos()
  224. {
  225. pos = false;
  226. }