The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

242 lines
7.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #pragma once
  20. #include "../JuceLibraryCode/JuceHeader.h"
  21. #include "LightpadComponent.h"
  22. //==============================================================================
  23. /**
  24. A struct that handles the setup and layout of the DrumPadGridProgram
  25. */
  26. struct ColourGrid
  27. {
  28. ColourGrid (int cols, int rows)
  29. : numColumns (cols),
  30. numRows (rows)
  31. {
  32. constructGridFillArray();
  33. }
  34. /** Creates a GridFill object for each pad in the grid and sets its colour
  35. and fill before adding it to an array of GridFill objects
  36. */
  37. void constructGridFillArray()
  38. {
  39. gridFillArray.clear();
  40. auto counter = 0;
  41. for (auto i = 0; i < numColumns; ++i)
  42. {
  43. for (auto j = 0; j < numRows; ++j)
  44. {
  45. DrumPadGridProgram::GridFill fill;
  46. Colour colourToUse = colourArray.getUnchecked (counter);
  47. fill.colour = colourToUse.withBrightness (colourToUse == currentColour ? 1.0f : 0.1f);
  48. if (colourToUse == Colours::black)
  49. fill.fillType = DrumPadGridProgram::GridFill::FillType::hollow;
  50. else
  51. fill.fillType = DrumPadGridProgram::GridFill::FillType::filled;
  52. gridFillArray.add (fill);
  53. if (++counter == colourArray.size())
  54. counter = 0;
  55. }
  56. }
  57. }
  58. /** Sets which colour should be active for a given touch co-ordinate. Returns
  59. true if the colour has changed
  60. */
  61. bool setActiveColourForTouch (int x, int y)
  62. {
  63. auto colourHasChanged = false;
  64. auto xindex = x / 5;
  65. auto yindex = y / 5;
  66. auto newColour = colourArray.getUnchecked ((yindex * 3) + xindex);
  67. if (currentColour != newColour)
  68. {
  69. currentColour = newColour;
  70. constructGridFillArray();
  71. colourHasChanged = true;
  72. }
  73. return colourHasChanged;
  74. }
  75. //==============================================================================
  76. int numColumns, numRows;
  77. Array<DrumPadGridProgram::GridFill> gridFillArray;
  78. Array<Colour> colourArray = { Colours::white, Colours::red, Colours::green,
  79. Colours::blue, Colours::hotpink, Colours::orange,
  80. Colours::magenta, Colours::cyan, Colours::black };
  81. Colour currentColour = Colours::hotpink;
  82. //==============================================================================
  83. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ColourGrid)
  84. };
  85. //==============================================================================
  86. /**
  87. The main component
  88. */
  89. class MainComponent : public Component,
  90. public TopologySource::Listener,
  91. private TouchSurface::Listener,
  92. private ControlButton::Listener,
  93. private LightpadComponent::Listener,
  94. private Button::Listener,
  95. private Slider::Listener,
  96. private Timer
  97. {
  98. public:
  99. MainComponent();
  100. ~MainComponent();
  101. void resized() override;
  102. /** Overridden from TopologySource::Listener. Called when the topology changes */
  103. void topologyChanged() override;
  104. private:
  105. /** Overridden from TouchSurface::Listener. Called when a Touch is received on the Lightpad */
  106. void touchChanged (TouchSurface&, const TouchSurface::Touch&) override;
  107. /** Overridden from ControlButton::Listener. Called when a button on the Lightpad is pressed */
  108. void buttonPressed (ControlButton&, Block::Timestamp) override { }
  109. /** Overridden from ControlButton::Listener. Called when a button on the Lightpad is released */
  110. void buttonReleased (ControlButton&, Block::Timestamp) override;
  111. void ledClicked (int x, int y, float z) override;
  112. void buttonClicked (Button*) override;
  113. void sliderValueChanged (Slider*) override;
  114. void timerCallback() override;
  115. /** Removes TouchSurface and ControlButton listeners and sets activeBlock to nullptr */
  116. void detachActiveBlock();
  117. /** Sets the LEDGrid Program for the selected mode */
  118. void setLEDProgram (Block&);
  119. void clearLEDs();
  120. /** Sets an LED on the Lightpad for a given touch co-ordinate and pressure */
  121. void drawLED (uint32 x0, uint32 y0, float z, Colour drawColour);
  122. /** Redraws the LEDs on the Lightpad from the activeLeds array */
  123. void redrawLEDs();
  124. //==============================================================================
  125. BitmapLEDProgram* getCanvasProgram()
  126. {
  127. if (activeBlock != nullptr)
  128. return dynamic_cast<BitmapLEDProgram*> (activeBlock->getProgram());
  129. return nullptr;
  130. }
  131. DrumPadGridProgram* getPaletteProgram()
  132. {
  133. if (activeBlock != nullptr)
  134. return dynamic_cast<DrumPadGridProgram*> (activeBlock->getProgram());
  135. return nullptr;
  136. }
  137. //==============================================================================
  138. /**
  139. A struct that represents an active LED on the Lightpad.
  140. Has a position, colour and brightness.
  141. */
  142. struct ActiveLED
  143. {
  144. uint32 x, y;
  145. Colour colour;
  146. float brightness;
  147. /** Returns true if this LED occupies the given co-ordinates */
  148. bool occupies (uint32 xPos, uint32 yPos) const
  149. {
  150. return xPos == x && yPos == y;
  151. }
  152. };
  153. Array<ActiveLED> activeLeds;
  154. int getLEDAt (uint32 x, uint32 y) const
  155. {
  156. for (int i = 0; i < activeLeds.size(); ++i)
  157. if (activeLeds.getReference(i).occupies (x, y))
  158. return i;
  159. return -1;
  160. }
  161. //==============================================================================
  162. enum DisplayMode
  163. {
  164. colourPalette = 0,
  165. canvas
  166. };
  167. DisplayMode currentMode = colourPalette;
  168. //==============================================================================
  169. ColourGrid layout { 3, 3 };
  170. PhysicalTopologySource topologySource;
  171. Block::Ptr activeBlock;
  172. float scaleX = 0.0;
  173. float scaleY = 0.0;
  174. bool doublePress = false;
  175. Label infoLabel;
  176. LightpadComponent lightpadComponent;
  177. TextButton clearButton;
  178. LEDComponent brightnessLED;
  179. Slider brightnessSlider;
  180. #if JUCE_IOS
  181. TextButton connectButton;
  182. #endif
  183. //==============================================================================
  184. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
  185. };