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.

Fl_Osc_Dial.cpp 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include <FL/Fl.H>
  2. #include "Fl_Osc_Dial.H"
  3. #include "Fl_Osc_Interface.h"
  4. #include "Fl_Osc_Pane.H"
  5. #include <cstdlib>
  6. #include <cstring>
  7. #include <cmath>
  8. #include <cassert>
  9. #include <sstream>
  10. static void callback_fn_dial(Fl_Widget *w, void *)
  11. {
  12. ((Fl_Osc_Dial*)w)->cb();
  13. }
  14. Fl_Osc_Pane *fetch_osc_pane(Fl_Widget *w)
  15. {
  16. if(!w)
  17. return NULL;
  18. Fl_Osc_Pane *pane = dynamic_cast<Fl_Osc_Pane*>(w->parent());
  19. if(pane)
  20. return pane;
  21. return fetch_osc_pane(w->parent());
  22. }
  23. Fl_Osc_Dial::Fl_Osc_Dial(int X, int Y, int W, int H, const char *label)
  24. :WidgetPDial(X,Y,W,H, label), Fl_Osc_Widget(this), alt_style(false), dead(false)
  25. {
  26. bounds(0.0, 127.0f);
  27. WidgetPDial::callback(callback_fn_dial);
  28. }
  29. void Fl_Osc_Dial::init(std::string path_)
  30. {
  31. assert(osc);
  32. ext = path_;
  33. oscRegister(path_.c_str());
  34. };
  35. void Fl_Osc_Dial::alt_init(std::string base, std::string path_)
  36. {
  37. Fl_Osc_Pane *pane = fetch_osc_pane(this);
  38. assert(pane);
  39. osc = pane->osc;
  40. assert(osc);
  41. loc = base;
  42. oscRegister(path_.c_str());
  43. ext = path_;
  44. alt_style = true;
  45. }
  46. Fl_Osc_Dial::~Fl_Osc_Dial(void)
  47. {}
  48. void Fl_Osc_Dial::callback(Fl_Callback *cb, void *p)
  49. {
  50. cb_data.first = cb;
  51. cb_data.second = p;
  52. }
  53. int Fl_Osc_Dial::handle(int ev)
  54. {
  55. bool middle_mouse = (ev == FL_PUSH && Fl::event_state(FL_BUTTON2) && !Fl::event_shift());
  56. bool ctl_click = (ev == FL_PUSH && Fl::event_state(FL_BUTTON1) && Fl::event_ctrl());
  57. bool shift_middle = (ev == FL_PUSH && Fl::event_state(FL_BUTTON2) && Fl::event_shift());
  58. if(middle_mouse || ctl_click) {
  59. printf("Trying to learn...\n");
  60. osc->write("/learn", "s", (loc+ext).c_str());
  61. return 1;
  62. } else if(shift_middle) {
  63. osc->write("/unlearn", "s", (loc+ext).c_str());
  64. return 1;
  65. }
  66. return WidgetPDial::handle(ev);
  67. }
  68. void Fl_Osc_Dial::OSC_value(int v)
  69. {
  70. value(v+minimum()+fmodf(value(), 1));
  71. }
  72. void Fl_Osc_Dial::OSC_value(char v)
  73. {
  74. value(v+minimum()+fmodf(value(), 1));
  75. }
  76. void Fl_Osc_Dial::update(void)
  77. {
  78. osc->requestValue(loc+ext);
  79. }
  80. void Fl_Osc_Dial::cb(void)
  81. {
  82. assert(osc);
  83. /* if((maximum()-minimum()) == 127 || (maximum()-minimum()) == 255) {
  84. oscWrite(ext, "i", (int)(value()-minimum()));
  85. }
  86. else*/
  87. oscWrite(ext, "i", (int)(value()-minimum()));
  88. if(cb_data.first)
  89. cb_data.first(this, cb_data.second);
  90. }
  91. void Fl_Osc_Dial::mark_dead(void)
  92. {
  93. dead = true;
  94. }
  95. void Fl_Osc_Dial::rebase(std::string new_base)
  96. {
  97. if(dead || loc == "/")
  98. return;
  99. if(!alt_style) {
  100. Fl_Osc_Widget::rebase(new_base);
  101. return;
  102. }
  103. //ok, for a simple hack here lets just assume that there is one branch
  104. //missing
  105. int depth = 0;
  106. for(int i=0; i<(int)loc.size(); ++i)
  107. depth += loc[i] == '/';
  108. int match_depth = 0;
  109. int match_pos = 0;
  110. for(int i=0; i<(int)new_base.size(); ++i) {
  111. match_depth += new_base[i] == '/';
  112. if(match_depth == depth) {
  113. match_pos = i;
  114. break;
  115. }
  116. }
  117. if(match_pos == 0) {
  118. //well, that didn't work
  119. assert(!"good enough hack");
  120. }
  121. std::string new_loc = new_base.substr(0, match_pos+1);
  122. printf("Moving '%s' to\n", (loc+ext).c_str());
  123. printf(" '%s'\n", (new_loc+ext).c_str());
  124. oscMove(loc+ext, new_loc+ext);
  125. loc = new_loc;
  126. }