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.

260 lines
8.0KB

  1. #pragma once
  2. #ifdef LAUNCHPAD
  3. #include "communicator.hpp"
  4. #include "launchpad.hpp"
  5. // off on switch, single key
  6. struct LaunchpadSwitch : launchpadControl
  7. {
  8. public:
  9. LaunchpadSwitch(int page, LaunchpadKey key, LaunchpadLed offColor, LaunchpadLed onColor, bool shifted = false)
  10. : LaunchpadSwitch(0, page, key, offColor, onColor, shifted) {}
  11. LaunchpadSwitch(int lp, int page, LaunchpadKey key, LaunchpadLed offColor, LaunchpadLed onColor, bool shifted = false) : launchpadControl(lp, page, key, shifted)
  12. {
  13. m_offColor = offColor;
  14. m_onColor = onColor;
  15. }
  16. protected:
  17. virtual void draw(launchpadDriver *drv) override { drv->drive_led(m_lpNumber, m_key, getValue() > 0.0 ? m_onColor : m_offColor); }
  18. virtual bool intersect(LaunchpadKey key) override { return key == m_key; }
  19. virtual void onLaunchpadKey(Module *pModule, LaunchpadMessage msg) override { if(msg.status == LaunchpadKeyStatus::keyDown) setValue(pModule, getValue() < 1); }
  20. private:
  21. LaunchpadLed m_offColor;
  22. LaunchpadLed m_onColor;
  23. };
  24. // 3-state switch, single key
  25. struct LaunchpadThree : launchpadControl
  26. {
  27. public:
  28. LaunchpadThree(int page, LaunchpadKey key, LaunchpadLed offColor, LaunchpadLed onColor1, LaunchpadLed onColor2, bool shifted = false)
  29. : LaunchpadThree(0, page, key, offColor, onColor1, onColor2, shifted) {}
  30. LaunchpadThree(int lp, int page, LaunchpadKey key, LaunchpadLed offColor, LaunchpadLed onColor1, LaunchpadLed onColor2, bool shifted = false) : launchpadControl(lp, page, key, shifted)
  31. {
  32. m_colors[0] = offColor;
  33. m_colors[1] = onColor1;
  34. m_colors[2] = onColor2;
  35. }
  36. protected:
  37. virtual void draw(launchpadDriver *drv) override
  38. {
  39. int n = (int)roundf(getValue());
  40. if(n > 2)
  41. n = 2;
  42. else if(n < 0)
  43. n = 0;
  44. drv->drive_led(m_lpNumber, m_key, m_colors[n]);
  45. }
  46. virtual bool intersect(LaunchpadKey key) override { return key == m_key; }
  47. virtual void onLaunchpadKey(Module *pModule, LaunchpadMessage msg) override
  48. {
  49. if(msg.status == LaunchpadKeyStatus::keyDown)
  50. {
  51. int n = (int)roundf(getValue()) + 1;
  52. if(n > 2)
  53. n = 0;
  54. setValue(pModule, n);
  55. }
  56. }
  57. private:
  58. LaunchpadLed m_colors[3];
  59. };
  60. struct LaunchpadMomentary : launchpadControl
  61. {
  62. public:
  63. LaunchpadMomentary(int page, LaunchpadKey key, LaunchpadLed offColor, LaunchpadLed onColor, bool shifted = false)
  64. : LaunchpadMomentary(0, page, key, offColor, onColor, shifted) {}
  65. LaunchpadMomentary(int lp, int page, LaunchpadKey key, LaunchpadLed offColor, LaunchpadLed onColor, bool shifted = false)
  66. : launchpadControl(lp, page, key, shifted)
  67. {
  68. m_offColor = offColor;
  69. m_onColor = onColor;
  70. }
  71. protected:
  72. virtual void draw(launchpadDriver *drv) override { drv->drive_led(m_lpNumber, m_key, getValue() > 0.0 ? m_onColor : m_offColor); }
  73. virtual bool intersect(LaunchpadKey key) override
  74. {
  75. return key == m_key;
  76. }
  77. virtual void onLaunchpadKey(Module *pModule, LaunchpadMessage msg) override
  78. {
  79. setValue(pModule, msg.status == LaunchpadKeyStatus::keyDown ? 1.0 : 0.0);
  80. }
  81. private:
  82. LaunchpadLed m_offColor;
  83. LaunchpadLed m_onColor;
  84. };
  85. struct LaunchpadKnob : launchpadControl
  86. {
  87. public:
  88. LaunchpadKnob(int page, LaunchpadKey key, LaunchpadLed rgbMin, LaunchpadLed rgbMax, bool horizontal = false, bool shifted = false) :
  89. LaunchpadKnob(0, page, key, rgbMin, rgbMax, horizontal, shifted) {}
  90. LaunchpadKnob(int lp, int page, LaunchpadKey key, LaunchpadLed rgbMin, LaunchpadLed rgbMax, bool horizontal = false, bool shifted = false) : launchpadControl(lp, page, key, shifted)
  91. {
  92. m_offColor = rgbMin;
  93. m_onColor = rgbMax;
  94. int r, c;
  95. ILaunchpadPro::Key2RC(m_key, &r, &c);
  96. if(horizontal)
  97. m_secondKey = ILaunchpadPro::RC2Key(r, c + 1);
  98. else
  99. m_secondKey = ILaunchpadPro::RC2Key(r + 1, c);
  100. }
  101. protected:
  102. virtual void draw(launchpadDriver *drv) override
  103. {
  104. float v = getValue();
  105. float inv_v = pBindedParam->maxValue - v + pBindedParam->minValue;
  106. LaunchpadLed led;
  107. led.status = ButtonColorType::RGB;
  108. led.r_color = (int)roundf(rescale(v, pBindedParam->minValue, pBindedParam->maxValue, m_offColor.r_color, m_onColor.r_color));
  109. led.g = (int)roundf(rescale(v, pBindedParam->minValue, pBindedParam->maxValue, m_offColor.g, m_onColor.g));
  110. led.b = (int)roundf(rescale(v, pBindedParam->minValue, pBindedParam->maxValue, m_offColor.b, m_onColor.b));
  111. drv->drive_led(m_lpNumber, m_key, led);
  112. led.r_color = (int)roundf(rescale(inv_v, pBindedParam->minValue, pBindedParam->maxValue, m_offColor.r_color, m_onColor.r_color));
  113. led.g = (int)roundf(rescale(inv_v, pBindedParam->minValue, pBindedParam->maxValue, m_offColor.g, m_onColor.g));
  114. led.b = (int)roundf(rescale(inv_v, pBindedParam->minValue, pBindedParam->maxValue, m_offColor.b, m_onColor.b));
  115. drv->drive_led(m_lpNumber, m_secondKey, led);
  116. }
  117. virtual bool intersect(LaunchpadKey key) override { return key == m_key || key == m_secondKey; }
  118. virtual void onLaunchpadKey(Module *pModule, LaunchpadMessage msg) override
  119. {
  120. switch(msg.status)
  121. {
  122. default:
  123. break;
  124. case LaunchpadKeyStatus::keyChannelPressure:
  125. case LaunchpadKeyStatus::keyPressure:
  126. float delta = sensitivity * (pBindedParam->maxValue - pBindedParam->minValue);
  127. if(msg.param0 < 100)
  128. delta /= 16.0;
  129. float v = pBindedParam->value;
  130. if(msg.key == m_key)
  131. v += delta;
  132. else
  133. v -= delta;
  134. setValue(pModule, v);
  135. break;
  136. }
  137. }
  138. private:
  139. const float sensitivity = 0.015;
  140. LaunchpadKey m_secondKey;
  141. LaunchpadLed m_offColor;
  142. LaunchpadLed m_onColor;
  143. };
  144. struct LaunchpadLight : launchpadControl
  145. {
  146. public:
  147. LaunchpadLight(int page, LaunchpadKey key, LaunchpadLed offColor, LaunchpadLed onColor)
  148. : LaunchpadLight(0, page, key, offColor, onColor) {}
  149. LaunchpadLight(int lp, int page, LaunchpadKey key, LaunchpadLed offColor, LaunchpadLed onColor) : launchpadControl(lp, page, key, false)
  150. {
  151. m_offColor = offColor;
  152. m_onColor = onColor;
  153. }
  154. protected:
  155. virtual void draw(launchpadDriver *drv) override {
  156. float newValue = getValue();
  157. drv->drive_led(m_lpNumber, m_key, newValue > 0.0 ? m_onColor : m_offColor);
  158. }
  159. virtual void onLaunchpadKey(Module *pModule, LaunchpadMessage msg) override {}
  160. private:
  161. LaunchpadLed m_offColor;
  162. LaunchpadLed m_onColor;
  163. };
  164. // Radiobutton switch, multiple keys, horizontal or vertical
  165. struct LaunchpadRadio : launchpadControl
  166. {
  167. public:
  168. LaunchpadRadio(int page, LaunchpadKey firstKey, int numKeys, LaunchpadLed unselectedColor, LaunchpadLed selectedColor, bool horizontal = false, bool shifted = false)
  169. : LaunchpadRadio(0, page, firstKey, numKeys, unselectedColor, selectedColor, horizontal, shifted) {}
  170. LaunchpadRadio(int lp, int page, LaunchpadKey firstKey, int numKeys, LaunchpadLed unselectedColor, LaunchpadLed selectedColor, bool horizontal = false, bool shifted = false)
  171. : launchpadControl(lp, page, firstKey, shifted)
  172. {
  173. m_numKeys = numKeys;
  174. m_unselectedColor = unselectedColor;
  175. m_selectedColor = selectedColor;
  176. m_horizontal = horizontal;
  177. }
  178. virtual void onLaunchpadKey(Module *pModule, LaunchpadMessage msg) override
  179. {
  180. if(msg.status == LaunchpadKeyStatus::keyDown)
  181. {
  182. int r, c, ir, ic;
  183. if(ILaunchpadPro::Key2RC(m_key, &r, &c) && ILaunchpadPro::Key2RC(msg.key, &ir, &ic))
  184. {
  185. int v = m_horizontal ? ic - c : (m_numKeys - 1) - (ir - r);
  186. setValue(pModule, v);
  187. }
  188. }
  189. }
  190. protected:
  191. virtual bool intersect(LaunchpadKey key) override
  192. {
  193. int r, c, ir, ic;
  194. if(ILaunchpadPro::Key2RC(m_key, &r, &c) && ILaunchpadPro::Key2RC(key, &ir, &ic))
  195. {
  196. if(m_horizontal)
  197. return ir == r && ic >= c && ic <= c + m_numKeys;
  198. else
  199. return ic == c && ir >= r && ir <= r + m_numKeys;
  200. }
  201. return false;
  202. }
  203. virtual void draw(launchpadDriver *drv) override
  204. {
  205. int r, c;
  206. if(ILaunchpadPro::Key2RC(m_key, &r, &c))
  207. {
  208. int n = (int)roundf(getValue());
  209. for(int k = 0; k < m_numKeys; k++)
  210. {
  211. if(m_horizontal)
  212. {
  213. drv->drive_led(m_lpNumber, ILaunchpadPro::RC2Key(r, c), k == n ? m_selectedColor : m_unselectedColor);
  214. c++;
  215. } else
  216. {
  217. drv->drive_led(m_lpNumber, ILaunchpadPro::RC2Key(r, c), m_numKeys - 1 - k == n ? m_selectedColor : m_unselectedColor);
  218. r++;
  219. }
  220. }
  221. }
  222. }
  223. private:
  224. LaunchpadLed m_unselectedColor;
  225. LaunchpadLed m_selectedColor;
  226. int m_numKeys;
  227. bool m_horizontal;
  228. };
  229. #endif