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.

157 lines
4.5KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2019 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 "DistrhoPluginInfo.h"
  17. #include "DistrhoUI.hpp"
  18. #include "Window.hpp"
  19. #include <cstring>
  20. START_NAMESPACE_DISTRHO
  21. /**
  22. We need the rectangle class from DGL.
  23. */
  24. using DGL_NAMESPACE::Rectangle;
  25. // -----------------------------------------------------------------------------------------------------------
  26. class SendNoteExampleUI : public UI
  27. {
  28. public:
  29. SendNoteExampleUI()
  30. : UI(64*12+8, 64+8)
  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. for (int key = 0; key < 12; ++key)
  55. {
  56. bool pressed = fKeyState[key];
  57. Rectangle<int> bounds = getKeyBounds(key);
  58. if (pressed)
  59. glColor3f(0.8f, 0.5f, 0.3f);
  60. else
  61. glColor3f(0.3f, 0.5f, 0.8f);
  62. bounds.draw();
  63. }
  64. }
  65. /**
  66. Mouse press event.
  67. This UI will de/activate keys when you click them and reports it as MIDI note events to the plugin.
  68. */
  69. bool onMouse(const MouseEvent& ev) override
  70. {
  71. // Test for left-clicked + pressed first.
  72. if (ev.button != 1 || ! ev.press)
  73. return false;
  74. // Find the key which is pressed, if any
  75. int whichKey = -1;
  76. for (int key = 0; key < 12 && whichKey == -1; ++key)
  77. {
  78. Rectangle<int> bounds = getKeyBounds(key);
  79. if (bounds.contains(ev.pos))
  80. whichKey = key;
  81. }
  82. if (whichKey == -1)
  83. return false;
  84. // Send a note event. Velocity=0 means off
  85. bool pressed = !fKeyState[whichKey];
  86. sendNote(0, kNoteOctaveStart+whichKey, pressed ? kNoteVelocity : 0);
  87. // Invert the pressed state of this key, and update display
  88. fKeyState[whichKey] = pressed;
  89. repaint();
  90. return true;
  91. }
  92. /**
  93. Set our UI class as non-copyable and add a leak detector just in case.
  94. */
  95. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(SendNoteExampleUI)
  96. private:
  97. /**
  98. Get the bounds of a particular key of the virtual MIDI keyboard.
  99. */
  100. Rectangle<int> getKeyBounds(unsigned index) const
  101. {
  102. Rectangle<int> bounds;
  103. int padding = 8;
  104. bounds.setX(64 * index + padding);
  105. bounds.setY(padding);
  106. bounds.setWidth(64 - padding);
  107. bounds.setHeight(64 - padding);
  108. return bounds;
  109. }
  110. /**
  111. The pressed state of one octave of a virtual MIDI keyboard.
  112. */
  113. bool fKeyState[12];
  114. enum
  115. {
  116. kNoteVelocity = 100, // velocity of sent Note-On events
  117. kNoteOctaveStart = 60, // starting note of the virtual MIDI keyboard
  118. };
  119. };
  120. /* ------------------------------------------------------------------------------------------------------------
  121. * UI entry point, called by DPF to create a new UI instance. */
  122. UI* createUI()
  123. {
  124. return new SendNoteExampleUI();
  125. }
  126. // -----------------------------------------------------------------------------------------------------------
  127. END_NAMESPACE_DISTRHO