DISTRHO Plugin Framework
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.

173 lines
4.9KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2021 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "DistrhoUI.hpp"
  17. #include "Color.hpp"
  18. #include <cstring>
  19. START_NAMESPACE_DISTRHO
  20. /**
  21. We need the rectangle class from DGL.
  22. */
  23. using DGL_NAMESPACE::Rectangle;
  24. // -----------------------------------------------------------------------------------------------------------
  25. class SendNoteExampleUI : public UI
  26. {
  27. public:
  28. SendNoteExampleUI()
  29. : UI(64*12+8, 64+8),
  30. fLastKey(-1)
  31. {
  32. std::memset(fKeyState, 0, sizeof(fKeyState));
  33. }
  34. protected:
  35. /* --------------------------------------------------------------------------------------------------------
  36. * DSP/Plugin Callbacks */
  37. /**
  38. A parameter has changed on the plugin side.
  39. This is called by the host to inform the UI about parameter changes.
  40. */
  41. void parameterChanged(uint32_t index, float value) override
  42. {
  43. (void)index;
  44. (void)value;
  45. }
  46. /* --------------------------------------------------------------------------------------------------------
  47. * Widget Callbacks */
  48. /**
  49. The OpenGL drawing function.
  50. This UI will draw a row of 12 keys, with on/off states according to pressed status.
  51. */
  52. void onDisplay() override
  53. {
  54. const GraphicsContext& context(getGraphicsContext());
  55. for (int key = 0; key < 12; ++key)
  56. {
  57. bool pressed = fKeyState[key];
  58. Rectangle<int> bounds = getKeyBounds(key);
  59. if (pressed)
  60. Color(0.8f, 0.5f, 0.3f).setFor(context);
  61. else
  62. Color(0.3f, 0.5f, 0.8f).setFor(context);
  63. bounds.draw(context);
  64. }
  65. }
  66. /**
  67. Mouse press event.
  68. This UI will de/activate keys when you click them and reports it as MIDI note events to the plugin.
  69. */
  70. bool onMouse(const MouseEvent& ev) override
  71. {
  72. // Test for last key release first
  73. if (fLastKey != -1 && ! ev.press)
  74. {
  75. // Send a note event. Velocity=0 means off
  76. sendNote(0, kNoteOctaveStart+fLastKey, 0);
  77. // Unset key state
  78. fKeyState[fLastKey] = false;
  79. fLastKey = -1;
  80. repaint();
  81. return true;
  82. }
  83. // Test for left-clicked first.
  84. if (ev.button != 1)
  85. return false;
  86. // Find the key which is pressed, if any
  87. int whichKey = -1;
  88. for (int key = 0; key < 12 && whichKey == -1; ++key)
  89. {
  90. Rectangle<int> bounds = getKeyBounds(key);
  91. if (bounds.contains(ev.pos))
  92. whichKey = key;
  93. }
  94. if (whichKey == -1)
  95. return false;
  96. if (fKeyState[whichKey] == ev.press)
  97. return false;
  98. // Send a note event. Velocity=0 means off
  99. sendNote(0, kNoteOctaveStart+whichKey, ev.press ? kNoteVelocity : 0);
  100. // Set pressed state of this key, and update display
  101. fLastKey = whichKey;
  102. fKeyState[whichKey] = ev.press;
  103. repaint();
  104. return true;
  105. }
  106. /**
  107. Set our UI class as non-copyable and add a leak detector just in case.
  108. */
  109. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(SendNoteExampleUI)
  110. private:
  111. /**
  112. Get the bounds of a particular key of the virtual MIDI keyboard.
  113. */
  114. Rectangle<int> getKeyBounds(unsigned index) const
  115. {
  116. Rectangle<int> bounds;
  117. int padding = 8;
  118. bounds.setX(64 * index + padding);
  119. bounds.setY(padding);
  120. bounds.setWidth(64 - padding);
  121. bounds.setHeight(64 - padding);
  122. return bounds;
  123. }
  124. /**
  125. The pressed state of one octave of a virtual MIDI keyboard.
  126. */
  127. bool fKeyState[12];
  128. int8_t fLastKey;
  129. enum
  130. {
  131. kNoteVelocity = 100, // velocity of sent Note-On events
  132. kNoteOctaveStart = 60, // starting note of the virtual MIDI keyboard
  133. };
  134. };
  135. /* ------------------------------------------------------------------------------------------------------------
  136. * UI entry point, called by DPF to create a new UI instance. */
  137. UI* createUI()
  138. {
  139. return new SendNoteExampleUI();
  140. }
  141. // -----------------------------------------------------------------------------------------------------------
  142. END_NAMESPACE_DISTRHO