Audio plugin host https://kx.studio/carla
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.

SliderFieldComponent.h 5.4KB

11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCETICE project - Copyright 2008 by Lucio Asnaghi.
  4. JUCETICE is based around the JUCE library - "Jules' Utility Class Extensions"
  5. Copyright 2008 by Julian Storer.
  6. ------------------------------------------------------------------------------
  7. JUCE and JUCETICE can be redistributed and/or modified under the terms of
  8. the GNU Lesser General Public License, as published by the Free Software
  9. Foundation; either version 2 of the License, or (at your option) any later
  10. version.
  11. JUCE and JUCETICE are distributed in the hope that they will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public License
  16. along with JUCE and JUCETICE; if not, visit www.gnu.org/licenses or write to
  17. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  18. Boston, MA 02111-1307 USA
  19. ==============================================================================
  20. @author rockhardbuns
  21. @tweaker Lucio Asnaghi
  22. @tweaker falkTX
  23. ==============================================================================
  24. */
  25. #ifndef DISTRHO_VEX_SLIDERFIELDCOMPONENT_HEADER_INCLUDED
  26. #define DISTRHO_VEX_SLIDERFIELDCOMPONENT_HEADER_INCLUDED
  27. #ifndef CARLA_EXPORT
  28. #define CARLA_EXPORT
  29. #endif
  30. #ifdef CARLA_EXPORT
  31. #include "juce_gui_basics.h"
  32. #else
  33. #include "../StandardHeader.h"
  34. #endif
  35. class SliderFieldComponent : public Component,
  36. public ChangeBroadcaster
  37. {
  38. public:
  39. SliderFieldComponent()
  40. : Component("Slider Field Component")
  41. {
  42. array = new float[16];
  43. lastSlider = -1; // -1, not to test against really
  44. sliderWidth = -1; // but it makes fuckups more visible
  45. numSliders = 16;
  46. activeLength = 8;
  47. //empty out the array.. to make sure
  48. for(int i = 0; i < numSliders; i++)
  49. array[i] = 0.5f;
  50. }
  51. ~SliderFieldComponent() override
  52. {
  53. delete[] array;
  54. deleteAllChildren();
  55. }
  56. void paint(Graphics& g) override
  57. {
  58. //cell size -recalculate in case of resizing
  59. sliderWidth = getWidth() / numSliders;
  60. //int middle = int(getHeight() * 0.5f);
  61. //Draw bars
  62. g.setColour(Colour(50,50,50));
  63. for(int i = 0; i < numSliders; i++)
  64. {
  65. if(array[i] > 0.0f){
  66. g.fillRect(i * sliderWidth + 2, getHeight() - int(array[i] * getHeight()), sliderWidth - 4, int(getHeight() * array[i])) ;
  67. }
  68. }
  69. g.setColour(Colour(100,100,130));
  70. //Grey stuff out
  71. g.setColour(Colour(uint8(170),170,170,.7f));
  72. g.fillRect(sliderWidth*activeLength,0,getWidth(),getHeight());
  73. //bevel outline for the entire draw area
  74. LookAndFeel_V2::drawBevel(g, 0, 0, getWidth(), getHeight(), 1, Colours::black, Colours::white, 0);
  75. }
  76. void mouseDrag(const MouseEvent& e) override
  77. {
  78. if ((e.y < getHeight()-1) && (e.x < getWidth()-1))
  79. {
  80. //this avoids false triggers along the rims
  81. float height = (float) getHeight();
  82. int index = (e.x-1)/sliderWidth;
  83. float value = ((height - e.y -1.0f) / height);
  84. //if the click was on the greyed out portion, we dont do jack
  85. if (index < activeLength)
  86. {
  87. lastSlider = index;
  88. array[index]= value;
  89. repaint();
  90. sendChangeMessage();
  91. }
  92. }
  93. }
  94. void mouseDown(const MouseEvent& e) override
  95. {
  96. if ((e.y < getHeight()-1) && (e.x < getWidth()-1))
  97. {
  98. //this avoids false triggers along the rims
  99. float height = (float) getHeight();
  100. int index = (e.x-1)/sliderWidth;
  101. float value = ((height - e.y ) / height);
  102. //if the click was on the greyed out portion, we dont do jack
  103. if (index < activeLength)
  104. {
  105. lastSlider = index;
  106. array[index]= value;
  107. repaint();
  108. sendChangeMessage();
  109. }
  110. }
  111. }
  112. int getLastSlider() const
  113. {
  114. return lastSlider;
  115. }
  116. float getValue(int i) const
  117. {
  118. return array[i];
  119. }
  120. void setValue(int i, float v) const
  121. {
  122. array[i] = v;
  123. }
  124. int getLength() const
  125. {
  126. return activeLength;
  127. }
  128. void setLength(const int l)
  129. {
  130. activeLength = jmin(l, numSliders);
  131. activeLength = jmax(activeLength, 1);
  132. repaint();
  133. }
  134. void reset()
  135. {
  136. //empty out the array.. to make sure
  137. for(int i = 0; i < numSliders; i++)
  138. array[i] = 0.0f;
  139. repaint();
  140. }
  141. private:
  142. int numSliders;
  143. int sliderWidth;
  144. int lastSlider; // last cell clicked, for outside interaction
  145. int activeLength; // How much of the grid should be usable vs greyed
  146. float* array; // SizeX
  147. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(SliderFieldComponent)
  148. };
  149. #endif // DISTRHO_VEX_SLIDERFIELDCOMPONENT_HEADER_INCLUDED