The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

456 lines
14KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For the technical preview this file cannot be licensed commercially.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. constexpr uint8 whiteNotes[] = { 0, 2, 4, 5, 7, 9, 11 };
  16. constexpr uint8 blackNotes[] = { 1, 3, 6, 8, 10 };
  17. //==============================================================================
  18. struct KeyboardComponentBase::UpDownButton : public Button
  19. {
  20. UpDownButton (KeyboardComponentBase& c, int d)
  21. : Button ({}), owner (c), delta (d)
  22. {
  23. }
  24. void clicked() override
  25. {
  26. auto note = owner.getLowestVisibleKey();
  27. note = delta < 0 ? (note - 1) / 12 : note / 12 + 1;
  28. owner.setLowestVisibleKey (note * 12);
  29. }
  30. using Button::clicked;
  31. void paintButton (Graphics& g, bool shouldDrawButtonAsHighlighted, bool shouldDrawButtonAsDown) override
  32. {
  33. owner.drawUpDownButton (g, getWidth(), getHeight(),
  34. shouldDrawButtonAsHighlighted, shouldDrawButtonAsDown,
  35. delta > 0);
  36. }
  37. private:
  38. KeyboardComponentBase& owner;
  39. int delta;
  40. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (UpDownButton)
  41. };
  42. //==============================================================================
  43. KeyboardComponentBase::KeyboardComponentBase (Orientation o) : orientation (o)
  44. {
  45. scrollDown = std::make_unique<UpDownButton> (*this, -1);
  46. scrollUp = std::make_unique<UpDownButton> (*this, 1);
  47. addChildComponent (*scrollDown);
  48. addChildComponent (*scrollUp);
  49. colourChanged();
  50. }
  51. //==============================================================================
  52. void KeyboardComponentBase::setKeyWidth (float widthInPixels)
  53. {
  54. jassert (widthInPixels > 0);
  55. if (keyWidth != widthInPixels) // Prevent infinite recursion if the width is being computed in a 'resized()' callback
  56. {
  57. keyWidth = widthInPixels;
  58. resized();
  59. }
  60. }
  61. void KeyboardComponentBase::setScrollButtonWidth (int widthInPixels)
  62. {
  63. jassert (widthInPixels > 0);
  64. if (scrollButtonWidth != widthInPixels)
  65. {
  66. scrollButtonWidth = widthInPixels;
  67. resized();
  68. }
  69. }
  70. void KeyboardComponentBase::setOrientation (Orientation newOrientation)
  71. {
  72. if (orientation != newOrientation)
  73. {
  74. orientation = newOrientation;
  75. resized();
  76. }
  77. }
  78. void KeyboardComponentBase::setAvailableRange (int lowestNote, int highestNote)
  79. {
  80. jassert (lowestNote >= 0 && lowestNote <= 127);
  81. jassert (highestNote >= 0 && highestNote <= 127);
  82. jassert (lowestNote <= highestNote);
  83. if (rangeStart != lowestNote || rangeEnd != highestNote)
  84. {
  85. rangeStart = jlimit (0, 127, lowestNote);
  86. rangeEnd = jlimit (0, 127, highestNote);
  87. firstKey = jlimit ((float) rangeStart, (float) rangeEnd, firstKey);
  88. resized();
  89. }
  90. }
  91. void KeyboardComponentBase::setLowestVisibleKey (int noteNumber)
  92. {
  93. setLowestVisibleKeyFloat ((float) noteNumber);
  94. }
  95. void KeyboardComponentBase::setLowestVisibleKeyFloat (float noteNumber)
  96. {
  97. noteNumber = jlimit ((float) rangeStart, (float) rangeEnd, noteNumber);
  98. if (noteNumber != firstKey)
  99. {
  100. bool hasMoved = (((int) firstKey) != (int) noteNumber);
  101. firstKey = noteNumber;
  102. if (hasMoved)
  103. sendChangeMessage();
  104. resized();
  105. }
  106. }
  107. float KeyboardComponentBase::getWhiteNoteLength() const noexcept
  108. {
  109. return (orientation == horizontalKeyboard) ? (float) getHeight() : (float) getWidth();
  110. }
  111. void KeyboardComponentBase::setBlackNoteLengthProportion (float ratio) noexcept
  112. {
  113. jassert (ratio >= 0.0f && ratio <= 1.0f);
  114. if (blackNoteLengthRatio != ratio)
  115. {
  116. blackNoteLengthRatio = ratio;
  117. resized();
  118. }
  119. }
  120. float KeyboardComponentBase::getBlackNoteLength() const noexcept
  121. {
  122. auto whiteNoteLength = orientation == horizontalKeyboard ? getHeight() : getWidth();
  123. return (float) whiteNoteLength * blackNoteLengthRatio;
  124. }
  125. void KeyboardComponentBase::setBlackNoteWidthProportion (float ratio) noexcept
  126. {
  127. jassert (ratio >= 0.0f && ratio <= 1.0f);
  128. if (blackNoteWidthRatio != ratio)
  129. {
  130. blackNoteWidthRatio = ratio;
  131. resized();
  132. }
  133. }
  134. void KeyboardComponentBase::setScrollButtonsVisible (bool newCanScroll)
  135. {
  136. if (canScroll != newCanScroll)
  137. {
  138. canScroll = newCanScroll;
  139. resized();
  140. }
  141. }
  142. //==============================================================================
  143. Range<float> KeyboardComponentBase::getKeyPos (int midiNoteNumber) const
  144. {
  145. return getKeyPosition (midiNoteNumber, keyWidth)
  146. - xOffset
  147. - getKeyPosition (rangeStart, keyWidth).getStart();
  148. }
  149. float KeyboardComponentBase::getKeyStartPosition (int midiNoteNumber) const
  150. {
  151. return getKeyPos (midiNoteNumber).getStart();
  152. }
  153. float KeyboardComponentBase::getTotalKeyboardWidth() const noexcept
  154. {
  155. return getKeyPos (rangeEnd).getEnd();
  156. }
  157. KeyboardComponentBase::NoteAndVelocity KeyboardComponentBase::getNoteAndVelocityAtPosition (Point<float> pos, bool children)
  158. {
  159. if (! reallyContains (pos, children))
  160. return { -1, 0.0f };
  161. auto p = pos;
  162. if (orientation != horizontalKeyboard)
  163. {
  164. p = { p.y, p.x };
  165. if (orientation == verticalKeyboardFacingLeft)
  166. p = { p.x, (float) getWidth() - p.y };
  167. else
  168. p = { (float) getHeight() - p.x, p.y };
  169. }
  170. return remappedXYToNote (p + Point<float> (xOffset, 0));
  171. }
  172. KeyboardComponentBase::NoteAndVelocity KeyboardComponentBase::remappedXYToNote (Point<float> pos) const
  173. {
  174. auto blackNoteLength = getBlackNoteLength();
  175. if (pos.getY() < blackNoteLength)
  176. {
  177. for (int octaveStart = 12 * (rangeStart / 12); octaveStart <= rangeEnd; octaveStart += 12)
  178. {
  179. for (int i = 0; i < 5; ++i)
  180. {
  181. auto note = octaveStart + blackNotes[i];
  182. if (rangeStart <= note && note <= rangeEnd)
  183. {
  184. if (getKeyPos (note).contains (pos.x - xOffset))
  185. {
  186. return { note, jmax (0.0f, pos.y / blackNoteLength) };
  187. }
  188. }
  189. }
  190. }
  191. }
  192. for (int octaveStart = 12 * (rangeStart / 12); octaveStart <= rangeEnd; octaveStart += 12)
  193. {
  194. for (int i = 0; i < 7; ++i)
  195. {
  196. auto note = octaveStart + whiteNotes[i];
  197. if (note >= rangeStart && note <= rangeEnd)
  198. {
  199. if (getKeyPos (note).contains (pos.x - xOffset))
  200. {
  201. auto whiteNoteLength = (orientation == horizontalKeyboard) ? getHeight() : getWidth();
  202. return { note, jmax (0.0f, pos.y / (float) whiteNoteLength) };
  203. }
  204. }
  205. }
  206. }
  207. return { -1, 0 };
  208. }
  209. Rectangle<float> KeyboardComponentBase::getRectangleForKey (int note) const
  210. {
  211. jassert (note >= rangeStart && note <= rangeEnd);
  212. auto pos = getKeyPos (note);
  213. auto x = pos.getStart();
  214. auto w = pos.getLength();
  215. if (MidiMessage::isMidiNoteBlack (note))
  216. {
  217. auto blackNoteLength = getBlackNoteLength();
  218. switch (orientation)
  219. {
  220. case horizontalKeyboard: return { x, 0, w, blackNoteLength };
  221. case verticalKeyboardFacingLeft: return { (float) getWidth() - blackNoteLength, x, blackNoteLength, w };
  222. case verticalKeyboardFacingRight: return { 0, (float) getHeight() - x - w, blackNoteLength, w };
  223. default: jassertfalse; break;
  224. }
  225. }
  226. else
  227. {
  228. switch (orientation)
  229. {
  230. case horizontalKeyboard: return { x, 0, w, (float) getHeight() };
  231. case verticalKeyboardFacingLeft: return { 0, x, (float) getWidth(), w };
  232. case verticalKeyboardFacingRight: return { 0, (float) getHeight() - x - w, (float) getWidth(), w };
  233. default: jassertfalse; break;
  234. }
  235. }
  236. return {};
  237. }
  238. //==============================================================================
  239. void KeyboardComponentBase::setOctaveForMiddleC (int octaveNum)
  240. {
  241. octaveNumForMiddleC = octaveNum;
  242. repaint();
  243. }
  244. //==============================================================================
  245. void KeyboardComponentBase::drawUpDownButton (Graphics& g, int w, int h, bool mouseOver, bool buttonDown, bool movesOctavesUp)
  246. {
  247. g.fillAll (findColour (upDownButtonBackgroundColourId));
  248. float angle = 0;
  249. switch (getOrientation())
  250. {
  251. case horizontalKeyboard: angle = movesOctavesUp ? 0.0f : 0.5f; break;
  252. case verticalKeyboardFacingLeft: angle = movesOctavesUp ? 0.25f : 0.75f; break;
  253. case verticalKeyboardFacingRight: angle = movesOctavesUp ? 0.75f : 0.25f; break;
  254. default: jassertfalse; break;
  255. }
  256. Path path;
  257. path.addTriangle (0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.5f);
  258. path.applyTransform (AffineTransform::rotation (MathConstants<float>::twoPi * angle, 0.5f, 0.5f));
  259. g.setColour (findColour (upDownButtonArrowColourId)
  260. .withAlpha (buttonDown ? 1.0f : (mouseOver ? 0.6f : 0.4f)));
  261. g.fillPath (path, path.getTransformToScaleToFit (1.0f, 1.0f, (float) w - 2.0f, (float) h - 2.0f, true));
  262. }
  263. Range<float> KeyboardComponentBase::getKeyPosition (int midiNoteNumber, float targetKeyWidth) const
  264. {
  265. auto ratio = getBlackNoteWidthProportion();
  266. static const float notePos[] = { 0.0f, 1 - ratio * 0.6f,
  267. 1.0f, 2 - ratio * 0.4f,
  268. 2.0f,
  269. 3.0f, 4 - ratio * 0.7f,
  270. 4.0f, 5 - ratio * 0.5f,
  271. 5.0f, 6 - ratio * 0.3f,
  272. 6.0f };
  273. auto octave = midiNoteNumber / 12;
  274. auto note = midiNoteNumber % 12;
  275. auto start = (float) octave * 7.0f * targetKeyWidth + notePos[note] * targetKeyWidth;
  276. auto width = MidiMessage::isMidiNoteBlack (note) ? blackNoteWidthRatio * targetKeyWidth : targetKeyWidth;
  277. return { start, start + width };
  278. }
  279. //==============================================================================
  280. void KeyboardComponentBase::paint (Graphics& g)
  281. {
  282. drawKeyboardBackground (g, getLocalBounds().toFloat());
  283. for (int octaveBase = 0; octaveBase < 128; octaveBase += 12)
  284. {
  285. for (auto noteNum : whiteNotes)
  286. {
  287. const auto key = octaveBase + noteNum;
  288. if (rangeStart <= key && key <= rangeEnd)
  289. drawWhiteKey (key, g, getRectangleForKey (key));
  290. }
  291. for (auto noteNum : blackNotes)
  292. {
  293. const auto key = octaveBase + noteNum;
  294. if (rangeStart <= key && key <= rangeEnd)
  295. drawBlackKey (key, g, getRectangleForKey (key));
  296. }
  297. }
  298. }
  299. void KeyboardComponentBase::resized()
  300. {
  301. auto w = getWidth();
  302. auto h = getHeight();
  303. if (w > 0 && h > 0)
  304. {
  305. if (orientation != horizontalKeyboard)
  306. std::swap (w, h);
  307. auto kx2 = getKeyPos (rangeEnd).getEnd();
  308. if ((int) firstKey != rangeStart)
  309. {
  310. auto kx1 = getKeyPos (rangeStart).getStart();
  311. if (kx2 - kx1 <= (float) w)
  312. {
  313. firstKey = (float) rangeStart;
  314. sendChangeMessage();
  315. repaint();
  316. }
  317. }
  318. scrollDown->setVisible (canScroll && firstKey > (float) rangeStart);
  319. xOffset = 0;
  320. if (canScroll)
  321. {
  322. auto scrollButtonW = jmin (scrollButtonWidth, w / 2);
  323. auto r = getLocalBounds();
  324. if (orientation == horizontalKeyboard)
  325. {
  326. scrollDown->setBounds (r.removeFromLeft (scrollButtonW));
  327. scrollUp ->setBounds (r.removeFromRight (scrollButtonW));
  328. }
  329. else if (orientation == verticalKeyboardFacingLeft)
  330. {
  331. scrollDown->setBounds (r.removeFromTop (scrollButtonW));
  332. scrollUp ->setBounds (r.removeFromBottom (scrollButtonW));
  333. }
  334. else
  335. {
  336. scrollDown->setBounds (r.removeFromBottom (scrollButtonW));
  337. scrollUp ->setBounds (r.removeFromTop (scrollButtonW));
  338. }
  339. auto endOfLastKey = getKeyPos (rangeEnd).getEnd();
  340. auto spaceAvailable = w;
  341. auto lastStartKey = remappedXYToNote ({ endOfLastKey - (float) spaceAvailable, 0 }).note + 1;
  342. if (lastStartKey >= 0 && ((int) firstKey) > lastStartKey)
  343. {
  344. firstKey = (float) jlimit (rangeStart, rangeEnd, lastStartKey);
  345. sendChangeMessage();
  346. }
  347. xOffset = getKeyPos ((int) firstKey).getStart();
  348. }
  349. else
  350. {
  351. firstKey = (float) rangeStart;
  352. }
  353. scrollUp->setVisible (canScroll && getKeyPos (rangeEnd).getStart() > (float) w);
  354. repaint();
  355. }
  356. }
  357. //==============================================================================
  358. void KeyboardComponentBase::mouseWheelMove (const MouseEvent&, const MouseWheelDetails& wheel)
  359. {
  360. auto amount = (orientation == horizontalKeyboard && wheel.deltaX != 0)
  361. ? wheel.deltaX : (orientation == verticalKeyboardFacingLeft ? wheel.deltaY
  362. : -wheel.deltaY);
  363. setLowestVisibleKeyFloat (firstKey - amount * keyWidth);
  364. }
  365. } // namespace juce