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.

415 lines
16KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_MIDIKEYBOARDCOMPONENT_JUCEHEADER__
  19. #define __JUCE_MIDIKEYBOARDCOMPONENT_JUCEHEADER__
  20. //==============================================================================
  21. /**
  22. A component that displays a piano keyboard, whose notes can be clicked on.
  23. This component will mimic a physical midi keyboard, showing the current state of
  24. a MidiKeyboardState object. When the on-screen keys are clicked on, it will play these
  25. notes by calling the noteOn() and noteOff() methods of its MidiKeyboardState object.
  26. Another feature is that the computer keyboard can also be used to play notes. By
  27. default it maps the top two rows of a standard querty keyboard to the notes, but
  28. these can be remapped if needed. It will only respond to keypresses when it has
  29. the keyboard focus, so to disable this feature you can call setWantsKeyboardFocus (false).
  30. The component is also a ChangeBroadcaster, so if you want to be informed when the
  31. keyboard is scrolled, you can register a ChangeListener for callbacks.
  32. @see MidiKeyboardState
  33. */
  34. class JUCE_API MidiKeyboardComponent : public Component,
  35. public MidiKeyboardStateListener,
  36. public ChangeBroadcaster,
  37. private Timer
  38. {
  39. public:
  40. //==============================================================================
  41. /** The direction of the keyboard.
  42. @see setOrientation
  43. */
  44. enum Orientation
  45. {
  46. horizontalKeyboard,
  47. verticalKeyboardFacingLeft,
  48. verticalKeyboardFacingRight,
  49. };
  50. /** Creates a MidiKeyboardComponent.
  51. @param state the midi keyboard model that this component will represent
  52. @param orientation whether the keyboard is horizonal or vertical
  53. */
  54. MidiKeyboardComponent (MidiKeyboardState& state,
  55. Orientation orientation);
  56. /** Destructor. */
  57. ~MidiKeyboardComponent();
  58. //==============================================================================
  59. /** Changes the velocity used in midi note-on messages that are triggered by clicking
  60. on the component.
  61. Values are 0 to 1.0, where 1.0 is the heaviest.
  62. @see setMidiChannel
  63. */
  64. void setVelocity (float velocity, bool useMousePositionForVelocity);
  65. /** Changes the midi channel number that will be used for events triggered by clicking
  66. on the component.
  67. The channel must be between 1 and 16 (inclusive). This is the channel that will be
  68. passed on to the MidiKeyboardState::noteOn() method when the user clicks the component.
  69. Although this is the channel used for outgoing events, the component can display
  70. incoming events from more than one channel - see setMidiChannelsToDisplay()
  71. @see setVelocity
  72. */
  73. void setMidiChannel (int midiChannelNumber);
  74. /** Returns the midi channel that the keyboard is using for midi messages.
  75. @see setMidiChannel
  76. */
  77. int getMidiChannel() const noexcept { return midiChannel; }
  78. /** Sets a mask to indicate which incoming midi channels should be represented by
  79. key movements.
  80. The mask is a set of bits, where bit 0 = midi channel 1, bit 1 = midi channel 2, etc.
  81. If the MidiKeyboardState has a key down for any of the channels whose bits are set
  82. in this mask, the on-screen keys will also go down.
  83. By default, this mask is set to 0xffff (all channels displayed).
  84. @see setMidiChannel
  85. */
  86. void setMidiChannelsToDisplay (int midiChannelMask);
  87. /** Returns the current set of midi channels represented by the component.
  88. This is the value that was set with setMidiChannelsToDisplay().
  89. */
  90. int getMidiChannelsToDisplay() const noexcept { return midiInChannelMask; }
  91. //==============================================================================
  92. /** Changes the width used to draw the white keys. */
  93. void setKeyWidth (float widthInPixels);
  94. /** Returns the width that was set by setKeyWidth(). */
  95. float getKeyWidth() const noexcept { return keyWidth; }
  96. /** Changes the keyboard's current direction. */
  97. void setOrientation (Orientation newOrientation);
  98. /** Returns the keyboard's current direction. */
  99. const Orientation getOrientation() const noexcept { return orientation; }
  100. /** Sets the range of midi notes that the keyboard will be limited to.
  101. By default the range is 0 to 127 (inclusive), but you can limit this if you
  102. only want a restricted set of the keys to be shown.
  103. Note that the values here are inclusive and must be between 0 and 127.
  104. */
  105. void setAvailableRange (int lowestNote,
  106. int highestNote);
  107. /** Returns the first note in the available range.
  108. @see setAvailableRange
  109. */
  110. int getRangeStart() const noexcept { return rangeStart; }
  111. /** Returns the last note in the available range.
  112. @see setAvailableRange
  113. */
  114. int getRangeEnd() const noexcept { return rangeEnd; }
  115. /** If the keyboard extends beyond the size of the component, this will scroll
  116. it to show the given key at the start.
  117. Whenever the keyboard's position is changed, this will use the ChangeBroadcaster
  118. base class to send a callback to any ChangeListeners that have been registered.
  119. */
  120. void setLowestVisibleKey (int noteNumber);
  121. /** Returns the number of the first key shown in the component.
  122. @see setLowestVisibleKey
  123. */
  124. int getLowestVisibleKey() const noexcept { return (int) firstKey; }
  125. /** Returns the length of the black notes.
  126. This will be their vertical or horizontal length, depending on the keyboard's orientation.
  127. */
  128. int getBlackNoteLength() const noexcept { return blackNoteLength; }
  129. /** If set to true, then scroll buttons will appear at either end of the keyboard
  130. if there are too many notes to fit them all in the component at once.
  131. */
  132. void setScrollButtonsVisible (bool canScroll);
  133. //==============================================================================
  134. /** A set of colour IDs to use to change the colour of various aspects of the keyboard.
  135. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  136. methods.
  137. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  138. */
  139. enum ColourIds
  140. {
  141. whiteNoteColourId = 0x1005000,
  142. blackNoteColourId = 0x1005001,
  143. keySeparatorLineColourId = 0x1005002,
  144. mouseOverKeyOverlayColourId = 0x1005003, /**< This colour will be overlaid on the normal note colour. */
  145. keyDownOverlayColourId = 0x1005004, /**< This colour will be overlaid on the normal note colour. */
  146. textLabelColourId = 0x1005005,
  147. upDownButtonBackgroundColourId = 0x1005006,
  148. upDownButtonArrowColourId = 0x1005007
  149. };
  150. /** Returns the position within the component of the left-hand edge of a key.
  151. Depending on the keyboard's orientation, this may be a horizontal or vertical
  152. distance, in either direction.
  153. */
  154. int getKeyStartPosition (const int midiNoteNumber) const;
  155. //==============================================================================
  156. /** Deletes all key-mappings.
  157. @see setKeyPressForNote
  158. */
  159. void clearKeyMappings();
  160. /** Maps a key-press to a given note.
  161. @param key the key that should trigger the note
  162. @param midiNoteOffsetFromC how many semitones above C the triggered note should
  163. be. The actual midi note that gets played will be
  164. this value + (12 * the current base octave). To change
  165. the base octave, see setKeyPressBaseOctave()
  166. */
  167. void setKeyPressForNote (const KeyPress& key,
  168. int midiNoteOffsetFromC);
  169. /** Removes any key-mappings for a given note.
  170. For a description of what the note number means, see setKeyPressForNote().
  171. */
  172. void removeKeyPressForNote (int midiNoteOffsetFromC);
  173. /** Changes the base note above which key-press-triggered notes are played.
  174. The set of key-mappings that trigger notes can be moved up and down to cover
  175. the entire scale using this method.
  176. The value passed in is an octave number between 0 and 10 (inclusive), and
  177. indicates which C is the base note to which the key-mapped notes are
  178. relative.
  179. */
  180. void setKeyPressBaseOctave (int newOctaveNumber);
  181. /** This sets the octave number which is shown as the octave number for middle C.
  182. This affects only the default implementation of getWhiteNoteText(), which
  183. passes this octave number to MidiMessage::getMidiNoteName() in order to
  184. get the note text. See MidiMessage::getMidiNoteName() for more info about
  185. the parameter.
  186. By default this value is set to 3.
  187. @see getOctaveForMiddleC
  188. */
  189. void setOctaveForMiddleC (int octaveNumForMiddleC);
  190. /** This returns the value set by setOctaveForMiddleC().
  191. @see setOctaveForMiddleC
  192. */
  193. int getOctaveForMiddleC() const noexcept { return octaveNumForMiddleC; }
  194. //==============================================================================
  195. /** @internal */
  196. void paint (Graphics&);
  197. /** @internal */
  198. void resized();
  199. /** @internal */
  200. void mouseMove (const MouseEvent&);
  201. /** @internal */
  202. void mouseDrag (const MouseEvent&);
  203. /** @internal */
  204. void mouseDown (const MouseEvent&);
  205. /** @internal */
  206. void mouseUp (const MouseEvent&);
  207. /** @internal */
  208. void mouseEnter (const MouseEvent&);
  209. /** @internal */
  210. void mouseExit (const MouseEvent&);
  211. /** @internal */
  212. void mouseWheelMove (const MouseEvent&, const MouseWheelDetails&);
  213. /** @internal */
  214. void timerCallback();
  215. /** @internal */
  216. bool keyStateChanged (bool isKeyDown);
  217. /** @internal */
  218. void focusLost (FocusChangeType);
  219. /** @internal */
  220. void handleNoteOn (MidiKeyboardState*, int midiChannel, int midiNoteNumber, float velocity);
  221. /** @internal */
  222. void handleNoteOff (MidiKeyboardState*, int midiChannel, int midiNoteNumber);
  223. /** @internal */
  224. void colourChanged();
  225. protected:
  226. //==============================================================================
  227. /** Draws a white note in the given rectangle.
  228. isOver indicates whether the mouse is over the key, isDown indicates whether the key is
  229. currently pressed down.
  230. When doing this, be sure to note the keyboard's orientation.
  231. */
  232. virtual void drawWhiteNote (int midiNoteNumber,
  233. Graphics& g,
  234. int x, int y, int w, int h,
  235. bool isDown, bool isOver,
  236. const Colour& lineColour,
  237. const Colour& textColour);
  238. /** Draws a black note in the given rectangle.
  239. isOver indicates whether the mouse is over the key, isDown indicates whether the key is
  240. currently pressed down.
  241. When doing this, be sure to note the keyboard's orientation.
  242. */
  243. virtual void drawBlackNote (int midiNoteNumber,
  244. Graphics& g,
  245. int x, int y, int w, int h,
  246. bool isDown, bool isOver,
  247. const Colour& noteFillColour);
  248. /** Allows text to be drawn on the white notes.
  249. By default this is used to label the C in each octave, but could be used for other things.
  250. @see setOctaveForMiddleC
  251. */
  252. virtual String getWhiteNoteText (const int midiNoteNumber);
  253. /** Draws the up and down buttons that change the base note. */
  254. virtual void drawUpDownButton (Graphics& g, int w, int h,
  255. const bool isMouseOver,
  256. const bool isButtonPressed,
  257. const bool movesOctavesUp);
  258. /** Callback when the mouse is clicked on a key.
  259. You could use this to do things like handle right-clicks on keys, etc.
  260. Return true if you want the click to trigger the note, or false if you
  261. want to handle it yourself and not have the note played.
  262. @see mouseDraggedToKey
  263. */
  264. virtual bool mouseDownOnKey (int midiNoteNumber, const MouseEvent& e);
  265. /** Callback when the mouse is dragged from one key onto another.
  266. @see mouseDownOnKey
  267. */
  268. virtual void mouseDraggedToKey (int midiNoteNumber, const MouseEvent& e);
  269. /** Calculates the positon of a given midi-note.
  270. This can be overridden to create layouts with custom key-widths.
  271. @param midiNoteNumber the note to find
  272. @param keyWidth the desired width in pixels of one key - see setKeyWidth()
  273. @param x the x position of the left-hand edge of the key (this method
  274. always works in terms of a horizontal keyboard)
  275. @param w the width of the key
  276. */
  277. virtual void getKeyPosition (int midiNoteNumber, float keyWidth,
  278. int& x, int& w) const;
  279. private:
  280. //==============================================================================
  281. friend class MidiKeyboardUpDownButton;
  282. MidiKeyboardState& state;
  283. int xOffset, blackNoteLength;
  284. float keyWidth;
  285. Orientation orientation;
  286. int midiChannel, midiInChannelMask;
  287. float velocity;
  288. Array<int> mouseOverNotes, mouseDownNotes;
  289. BigInteger keysPressed, keysCurrentlyDrawnDown;
  290. bool shouldCheckState;
  291. int rangeStart, rangeEnd;
  292. float firstKey;
  293. bool canScroll, useMousePositionForVelocity, shouldCheckMousePos;
  294. ScopedPointer<Button> scrollDown, scrollUp;
  295. Array <KeyPress> keyPresses;
  296. Array <int> keyPressNotes;
  297. int keyMappingOctave;
  298. int octaveNumForMiddleC;
  299. static const uint8 whiteNotes[];
  300. static const uint8 blackNotes[];
  301. void getKeyPos (int midiNoteNumber, int& x, int& w) const;
  302. int xyToNote (const Point<int>&, float& mousePositionVelocity);
  303. int remappedXYToNote (const Point<int>&, float& mousePositionVelocity) const;
  304. void resetAnyKeysInUse();
  305. void updateNoteUnderMouse (const Point<int>&, bool isDown, int fingerNum);
  306. void updateNoteUnderMouse (const MouseEvent&, bool isDown);
  307. void repaintNote (const int midiNoteNumber);
  308. void setLowestVisibleKeyFloat (float noteNumber);
  309. Rectangle<int> getWhiteNotePos (int noteNumber) const;
  310. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MidiKeyboardComponent);
  311. };
  312. #endif // __JUCE_MIDIKEYBOARDCOMPONENT_JUCEHEADER__