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.

200 lines
6.0KB

  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_BOOLGRIDCOMPONENT_HEADER_INCLUDED
  26. #define DISTRHO_VEX_BOOLGRIDCOMPONENT_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 BoolGridComponent : public Component,
  36. public ChangeBroadcaster
  37. {
  38. public:
  39. BoolGridComponent()
  40. : Component("Bool Grid Component")
  41. {
  42. grid = new bool[80];
  43. lastCell = -1;
  44. cellX = -1; cellY = -1;
  45. sizeX = 16; sizeY = 5;
  46. activeLength = 8;
  47. for(int x = 0; x < sizeX; x++)
  48. {
  49. for(int y = 0; y < sizeY; y++)
  50. {
  51. grid[(x * sizeY) + y] = false;
  52. }
  53. }
  54. }
  55. ~BoolGridComponent() override
  56. {
  57. delete[] grid;
  58. }
  59. void paint(Graphics& g) override
  60. {
  61. //background
  62. //g.setColour(Colour(200,200,210).withAlpha(0.4f));
  63. //g.fillRect(0,0,getWidth(),getHeight());
  64. //cell size -recalculate in case of resizing
  65. cellX = getWidth() / sizeX;
  66. cellY = getHeight() / sizeY;
  67. //Draw lines and checkmarks
  68. for(int x = 0; x < sizeX; x++){
  69. for(int y = 0; y < sizeY; y++){
  70. //lines
  71. g.setColour(Colour(100,100,130));
  72. g.drawLine((float) x*cellX, 0.0f,(float) x*cellX, (float) getHeight() );
  73. g.drawLine(0.0f, (float) y*cellY, (float)getWidth(), (float) y*cellY);
  74. //checkmarks
  75. g.setColour(Colours::black);
  76. if (grid[(x * sizeY) + sizeY - y -1]){
  77. g.fillEllipse((float) x*cellX+3, (float) y*cellY+3 ,(float) cellX - 5 ,(float) cellY - 5);
  78. }
  79. }
  80. }
  81. //Grey stuff out
  82. g.setColour(Colour(uint8(170),170,170,.7f));
  83. g.fillRect(cellX*activeLength,0,getWidth(),getHeight());
  84. //bevel outline for the entire draw area
  85. LookAndFeel_V2::drawBevel(g, 0, 0, getWidth(), getHeight(), 1, Colours::black, Colours::white, 0);
  86. }
  87. //Yay, someone clicked my component!
  88. void mouseDown(const MouseEvent& e) override
  89. {
  90. if (e.mouseWasClicked())
  91. {
  92. if ((e.y < getHeight()-1) && (e.x < getWidth()-1)){ //this avoids false triggers along the rims
  93. int cx = (e.x-1)/cellX; int cy = (e.y-1)/cellY; //cx,cy are the cell coords
  94. if (cx < activeLength){ //if the click was on the greyed out portion, we dont do jack
  95. lastCell = (cx * sizeY) + sizeY - cy -1;
  96. grid[lastCell]= !grid[lastCell]; //toggle the clicked cell
  97. repaint();
  98. sendChangeMessage();
  99. }
  100. }
  101. }
  102. }
  103. //get the state of a specific cell
  104. bool getCellState(int x, int y) const
  105. {
  106. return grid[(x * sizeY) + y];
  107. }
  108. bool getCellState(int x) const
  109. {
  110. return grid[x];
  111. }
  112. //set the state of a specific cell. the last param determines if we broadcast
  113. //Cell determined by grid coords i.e X * Y
  114. void setCellState(int x, int y, bool state, bool broadcast = false)
  115. {
  116. grid[(x * sizeY) + y] = state;
  117. if (broadcast){sendChangeMessage();}
  118. repaint();
  119. }
  120. //Overloaded - cell determined soley by array index
  121. void setCellState(int x, bool state, bool broadcast = false)
  122. {
  123. grid[x] = state;
  124. if (broadcast){sendChangeMessage();}
  125. repaint();
  126. }
  127. // the last changed cell, in terms of array index
  128. int getLastChanged()
  129. {
  130. return lastCell;
  131. lastCell = -1;
  132. }
  133. //Get the active length
  134. int getLength() const
  135. {
  136. return activeLength;
  137. }
  138. //Set the active length of the grid
  139. void setLength(int l)
  140. {
  141. activeLength = jmin(l, sizeX);
  142. activeLength = jmax(activeLength, 1);
  143. repaint();
  144. }
  145. //Clear the grid
  146. void reset()
  147. {
  148. for(int x = 0; x < sizeX; x++){
  149. for(int y = 0; y < sizeY; y++){
  150. grid[(x * sizeY) + y] = false;
  151. }
  152. }
  153. repaint();
  154. }
  155. private:
  156. int sizeX, sizeY; // grid size in number of cells
  157. int cellX, cellY; // cell size in pixels, used by paint();
  158. int lastCell; // last cell clicked, for outside interaction
  159. int activeLength; // How much of the grid should be usable vs greyed
  160. bool* grid; // SizeX * SizeY
  161. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(BoolGridComponent)
  162. };
  163. #endif // DISTRHO_VEX_BOOLGRIDCOMPONENT_HEADER_INCLUDED