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.

152 lines
4.4KB

  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. class SendNoteExampleUI : public UI
  23. {
  24. public:
  25. SendNoteExampleUI()
  26. : UI(64*12+8, 64+8)
  27. {
  28. std::memset(fKeyState, 0, sizeof(fKeyState));
  29. }
  30. protected:
  31. /* --------------------------------------------------------------------------------------------------------
  32. * DSP/Plugin Callbacks */
  33. /**
  34. A parameter has changed on the plugin side.
  35. This is called by the host to inform the UI about parameter changes.
  36. */
  37. void parameterChanged(uint32_t index, float value) override
  38. {
  39. (void)index;
  40. (void)value;
  41. }
  42. /* --------------------------------------------------------------------------------------------------------
  43. * Widget Callbacks */
  44. /**
  45. The OpenGL drawing function.
  46. This UI will draw a row of 12 keys, with on/off states according to pressed status.
  47. */
  48. void onDisplay() override
  49. {
  50. for (int key = 0; key < 12; ++key)
  51. {
  52. bool pressed = fKeyState[key];
  53. DGL::Rectangle<int> bounds = getKeyBounds(key);
  54. if (pressed)
  55. glColor3f(0.8f, 0.5f, 0.3f);
  56. else
  57. glColor3f(0.3f, 0.5f, 0.8f);
  58. bounds.draw();
  59. }
  60. }
  61. /**
  62. Mouse press event.
  63. This UI will de/activate keys when you click them and reports it as MIDI note events to the plugin.
  64. */
  65. bool onMouse(const MouseEvent& ev) override
  66. {
  67. // Test for left-clicked + pressed first.
  68. if (ev.button != 1 || ! ev.press)
  69. return false;
  70. // Find the key which is pressed, if any
  71. int whichKey = -1;
  72. for (int key = 0; key < 12 && whichKey == -1; ++key)
  73. {
  74. DGL::Rectangle<int> bounds = getKeyBounds(key);
  75. if (bounds.contains(ev.pos))
  76. whichKey = key;
  77. }
  78. if (whichKey == -1)
  79. return false;
  80. // Send a note event. Velocity=0 means off
  81. bool pressed = !fKeyState[whichKey];
  82. sendNote(0, kNoteOctaveStart+whichKey, pressed ? kNoteVelocity : 0);
  83. // Invert the pressed state of this key, and update display
  84. fKeyState[whichKey] = pressed;
  85. repaint();
  86. return true;
  87. }
  88. /**
  89. Set our UI class as non-copyable and add a leak detector just in case.
  90. */
  91. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(SendNoteExampleUI)
  92. private:
  93. /**
  94. Get the bounds of a particular key of the virtual MIDI keyboard.
  95. */
  96. DGL::Rectangle<int> getKeyBounds(unsigned index) const
  97. {
  98. DGL::Rectangle<int> bounds;
  99. int padding = 8;
  100. bounds.setX(64 * index + padding);
  101. bounds.setY(padding);
  102. bounds.setWidth(64 - padding);
  103. bounds.setHeight(64 - padding);
  104. return bounds;
  105. }
  106. /**
  107. The pressed state of one octave of a virtual MIDI keyboard.
  108. */
  109. bool fKeyState[12];
  110. enum
  111. {
  112. kNoteVelocity = 100, // velocity of sent Note-On events
  113. kNoteOctaveStart = 60, // starting note of the virtual MIDI keyboard
  114. };
  115. };
  116. /* ------------------------------------------------------------------------------------------------------------
  117. * UI entry point, called by DPF to create a new UI instance. */
  118. UI* createUI()
  119. {
  120. return new SendNoteExampleUI();
  121. }
  122. // -----------------------------------------------------------------------------------------------------------
  123. END_NAMESPACE_DISTRHO