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.

562 lines
19KB

  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. int counter = 0;
  41. for (int i = 0; i < numColumns; ++i)
  42. {
  43. for (int 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. bool colourHasChanged = false;
  64. int xindex = x / 5;
  65. int yindex = y / 5;
  66. Colour 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. {
  101. activeLeds.clear();
  102. // Register MainContentComponent as a listener to the PhysicalTopologySource object
  103. topologySource.addListener (this);
  104. infoLabel.setText ("Connect a Lightpad Block to draw.", dontSendNotification);
  105. infoLabel.setJustificationType (Justification::centred);
  106. addAndMakeVisible (infoLabel);
  107. addAndMakeVisible (lightpadComponent);
  108. lightpadComponent.setVisible (false);
  109. lightpadComponent.addListener (this);
  110. clearButton.setButtonText ("Clear");
  111. clearButton.addListener (this);
  112. clearButton.setAlwaysOnTop (true);
  113. addAndMakeVisible (clearButton);
  114. brightnessSlider.setRange (0.0, 1.0);
  115. brightnessSlider.setValue (1.0);
  116. brightnessSlider.setAlwaysOnTop (true);
  117. brightnessSlider.setTextBoxStyle (Slider::TextEntryBoxPosition::NoTextBox, false, 0, 0);
  118. brightnessSlider.addListener (this);
  119. addAndMakeVisible (brightnessSlider);
  120. brightnessLED.setAlwaysOnTop (true);
  121. brightnessLED.setColour (layout.currentColour.withBrightness (static_cast<float> (brightnessSlider.getValue())));
  122. addAndMakeVisible (brightnessLED);
  123. #if JUCE_IOS
  124. connectButton.setButtonText ("Connect");
  125. connectButton.addListener (this);
  126. connectButton.setAlwaysOnTop (true);
  127. addAndMakeVisible (connectButton);
  128. #endif
  129. setSize (600, 600);
  130. }
  131. ~MainComponent()
  132. {
  133. if (activeBlock != nullptr)
  134. detachActiveBlock();
  135. lightpadComponent.removeListener (this);
  136. }
  137. void paint (Graphics& g) override
  138. {
  139. }
  140. void resized() override
  141. {
  142. infoLabel.centreWithSize (getWidth(), 100);
  143. Rectangle<int> bounds = getLocalBounds().reduced (20);
  144. // top buttons
  145. Rectangle<int> topButtonArea = bounds.removeFromTop (getHeight() / 20);
  146. topButtonArea.removeFromLeft (20);
  147. clearButton.setBounds (topButtonArea.removeFromLeft (80));
  148. #if JUCE_IOS
  149. topButtonArea.removeFromRight (20);
  150. connectButton.setBounds (topButtonArea.removeFromRight (80));
  151. #endif
  152. bounds.removeFromTop (20);
  153. // brightness controls
  154. Rectangle<int> brightnessControlBounds;
  155. Desktop::DisplayOrientation orientation = Desktop::getInstance().getCurrentOrientation();
  156. if (orientation == Desktop::DisplayOrientation::upright || orientation == Desktop::DisplayOrientation::upsideDown)
  157. {
  158. brightnessControlBounds = bounds.removeFromBottom (getHeight() / 10);
  159. brightnessSlider.setSliderStyle (Slider::SliderStyle::LinearHorizontal);
  160. brightnessLED.setBounds (brightnessControlBounds.removeFromLeft (getHeight() / 10));
  161. brightnessSlider.setBounds (brightnessControlBounds);
  162. }
  163. else
  164. {
  165. brightnessControlBounds = bounds.removeFromRight (getWidth() / 10);
  166. brightnessSlider.setSliderStyle (Slider::SliderStyle::LinearVertical);
  167. brightnessLED.setBounds (brightnessControlBounds.removeFromTop (getWidth() / 10));
  168. brightnessSlider.setBounds (brightnessControlBounds);
  169. }
  170. // lightpad component
  171. int sideLength = jmin (bounds.getWidth() - 40, bounds.getHeight() - 40);
  172. lightpadComponent.centreWithSize (sideLength, sideLength);
  173. }
  174. /** Overridden from TopologySource::Listener. Called when the topology changes */
  175. void topologyChanged() override
  176. {
  177. lightpadComponent.setVisible (false);
  178. infoLabel.setVisible (true);
  179. // Reset the activeBlock object
  180. if (activeBlock != nullptr)
  181. detachActiveBlock();
  182. // Get the array of currently connected Block objects from the PhysicalTopologySource
  183. Block::Array blocks = topologySource.getCurrentTopology().blocks;
  184. // Iterate over the array of Block objects
  185. for (auto b : blocks)
  186. {
  187. // Find the first Lightpad
  188. if (b->getType() == Block::Type::lightPadBlock)
  189. {
  190. activeBlock = b;
  191. // Register MainContentComponent as a listener to the touch surface
  192. if (auto surface = activeBlock->getTouchSurface())
  193. surface->addListener (this);
  194. // Register MainContentComponent as a listener to any buttons
  195. for (auto button : activeBlock->getButtons())
  196. button->addListener (this);
  197. // Get the LEDGrid object from the Lightpad and set its program to the program for the current mode
  198. if (auto grid = activeBlock->getLEDGrid())
  199. {
  200. // Work out scale factors to translate X and Y touches to LED indexes
  201. scaleX = (float) (grid->getNumColumns()) / activeBlock->getWidth();
  202. scaleY = (float) (grid->getNumRows()) / activeBlock->getHeight();
  203. setLEDProgram (*activeBlock);
  204. }
  205. // Make the on screen Lighpad component visible
  206. lightpadComponent.setVisible (true);
  207. infoLabel.setVisible (false);
  208. break;
  209. }
  210. }
  211. }
  212. private:
  213. /** Overridden from TouchSurface::Listener. Called when a Touch is received on the Lightpad */
  214. void touchChanged (TouchSurface&, const TouchSurface::Touch& touch) override
  215. {
  216. // Translate X and Y touch events to LED indexes
  217. int xLed = roundToInt (touch.x * scaleX);
  218. int yLed = roundToInt (touch.y * scaleY);
  219. if (currentMode == colourPalette)
  220. {
  221. if (layout.setActiveColourForTouch (xLed, yLed))
  222. {
  223. colourPaletteProgram->setGridFills (layout.numColumns, layout.numRows, layout.gridFillArray);
  224. brightnessLED.setColour (layout.currentColour.withBrightness (layout.currentColour == Colours::black ? 0.0f
  225. : static_cast<float> (brightnessSlider.getValue())));
  226. }
  227. }
  228. else if (currentMode == canvas)
  229. {
  230. drawLED ((uint32) xLed, (uint32) yLed, touch.z, layout.currentColour);
  231. }
  232. }
  233. /** Overridden from ControlButton::Listener. Called when a button on the Lightpad is pressed */
  234. void buttonPressed (ControlButton&, Block::Timestamp) override { }
  235. /** Overridden from ControlButton::Listener. Called when a button on the Lightpad is released */
  236. void buttonReleased (ControlButton&, Block::Timestamp) override
  237. {
  238. if (currentMode == canvas)
  239. {
  240. // Wait 500ms to see if there is a second press
  241. if (! isTimerRunning())
  242. startTimer (500);
  243. else
  244. doublePress = true;
  245. }
  246. else if (currentMode == colourPalette)
  247. {
  248. // Switch to canvas mode and set the LEDGrid program
  249. currentMode = canvas;
  250. setLEDProgram (*activeBlock);
  251. }
  252. }
  253. void buttonClicked (Button* b) override
  254. {
  255. #if JUCE_IOS
  256. if (b == &connectButton)
  257. {
  258. BluetoothMidiDevicePairingDialogue::open();
  259. return;
  260. }
  261. #else
  262. ignoreUnused (b);
  263. #endif
  264. clearLEDs();
  265. }
  266. void sliderValueChanged (Slider* s) override
  267. {
  268. if (s == &brightnessSlider)
  269. brightnessLED.setColour (layout.currentColour.withBrightness (layout.currentColour == Colours::black ? 0.0f
  270. : static_cast<float> (brightnessSlider.getValue())));
  271. }
  272. void timerCallback() override
  273. {
  274. if (doublePress)
  275. {
  276. clearLEDs();
  277. // Reset the doublePress flag
  278. doublePress = false;
  279. }
  280. else
  281. {
  282. // Switch to colour palette mode and set the LEDGrid program
  283. currentMode = colourPalette;
  284. setLEDProgram (*activeBlock);
  285. }
  286. stopTimer();
  287. }
  288. void ledClicked (int x, int y, float z) override
  289. {
  290. drawLED ((uint32) x, (uint32) y, z == 0.0f ? static_cast<float> (brightnessSlider.getValue())
  291. : z * static_cast<float> (brightnessSlider.getValue()), layout.currentColour);
  292. }
  293. /** Removes TouchSurface and ControlButton listeners and sets activeBlock to nullptr */
  294. void detachActiveBlock()
  295. {
  296. if (auto surface = activeBlock->getTouchSurface())
  297. surface->removeListener (this);
  298. for (auto button : activeBlock->getButtons())
  299. button->removeListener (this);
  300. activeBlock = nullptr;
  301. }
  302. /** Sets the LEDGrid Program for the selected mode */
  303. void setLEDProgram (Block& block)
  304. {
  305. canvasProgram = nullptr;
  306. colourPaletteProgram = nullptr;
  307. if (currentMode == canvas)
  308. {
  309. // Create a new BitmapLEDProgram for the LEDGrid
  310. canvasProgram = new BitmapLEDProgram (block);
  311. // Set the LEDGrid program
  312. block.setProgram (canvasProgram);
  313. // Redraw any previously drawn LEDs
  314. redrawLEDs();
  315. }
  316. else if (currentMode == colourPalette)
  317. {
  318. // Create a new DrumPadGridProgram for the LEDGrid
  319. colourPaletteProgram = new DrumPadGridProgram (block);
  320. // Set the LEDGrid program
  321. block.setProgram (colourPaletteProgram);
  322. // Setup the grid layout
  323. colourPaletteProgram->setGridFills (layout.numColumns,
  324. layout.numRows,
  325. layout.gridFillArray);
  326. }
  327. }
  328. void clearLEDs()
  329. {
  330. // Clear the LED grid
  331. for (uint32 x = 0; x < 15; ++x)
  332. {
  333. for (uint32 y = 0; y < 15; ++ y)
  334. {
  335. if (canvasProgram != nullptr)
  336. canvasProgram->setLED (x, y, Colours::black);
  337. lightpadComponent.setLEDColour (x, y, Colours::black);
  338. }
  339. }
  340. // Clear the ActiveLED array
  341. activeLeds.clear();
  342. }
  343. /** Sets an LED on the Lightpad for a given touch co-ordinate and pressure */
  344. void drawLED (uint32 x0, uint32 y0, float z, Colour drawColour)
  345. {
  346. // Check if the activeLeds array already contains an ActiveLED object for this LED
  347. auto index = getLEDAt (x0, y0);
  348. // If the colour is black then just set the LED to black and return
  349. if (drawColour == Colours::black)
  350. {
  351. if (index >= 0)
  352. {
  353. if (canvasProgram != nullptr)
  354. canvasProgram->setLED (x0, y0, Colours::black);
  355. lightpadComponent.setLEDColour (x0, y0, Colours::black);
  356. activeLeds.remove (index);
  357. }
  358. return;
  359. }
  360. // If there is no ActiveLED obejct for this LED then create one,
  361. // add it to the array, set the LED on the Block and return
  362. if (index < 0)
  363. {
  364. ActiveLED led;
  365. led.x = x0;
  366. led.y = y0;
  367. led.colour = drawColour;
  368. led.brightness = z;
  369. activeLeds.add (led);
  370. if (canvasProgram != nullptr)
  371. canvasProgram->setLED (led.x, led.y, led.colour.withBrightness (led.brightness));
  372. lightpadComponent.setLEDColour (led.x, led.y, led.colour.withBrightness (led.brightness));
  373. return;
  374. }
  375. // Get the ActiveLED object for this LED
  376. ActiveLED currentLed = activeLeds.getReference (index);
  377. // If the LED colour is the same as the draw colour, add the brightnesses together.
  378. // If it is different, blend the colours
  379. if (currentLed.colour == drawColour)
  380. currentLed.brightness = jmin (currentLed.brightness + z, 1.0f);
  381. else
  382. currentLed.colour = currentLed.colour.interpolatedWith (drawColour, z);
  383. // Set the LED on the Block and change the ActiveLED object in the activeLeds array
  384. if (canvasProgram != nullptr)
  385. canvasProgram->setLED (currentLed.x, currentLed.y, currentLed.colour.withBrightness (currentLed.brightness));
  386. lightpadComponent.setLEDColour (currentLed.x, currentLed.y, currentLed.colour.withBrightness (currentLed.brightness));
  387. activeLeds.set (index, currentLed);
  388. }
  389. /** Redraws the LEDs on the Lightpad from the activeLeds array */
  390. void redrawLEDs()
  391. {
  392. // Iterate over the activeLeds array and set the LEDs on the Block
  393. for (auto led : activeLeds)
  394. {
  395. canvasProgram->setLED (led.x, led.y, led.colour.withBrightness (led.brightness));
  396. lightpadComponent.setLEDColour (led.x, led.y, led.colour.withBrightness (led.brightness));
  397. }
  398. }
  399. /**
  400. A struct that represents an active LED on the Lightpad.
  401. Has a position, colour and brightness.
  402. */
  403. struct ActiveLED
  404. {
  405. uint32 x, y;
  406. Colour colour;
  407. float brightness;
  408. /** Returns true if this LED occupies the given co-ordinates */
  409. bool occupies (uint32 xPos, uint32 yPos) const
  410. {
  411. return xPos == x && yPos == y;
  412. }
  413. };
  414. Array<ActiveLED> activeLeds;
  415. int getLEDAt (uint32 x, uint32 y) const
  416. {
  417. for (int i = 0; i < activeLeds.size(); ++i)
  418. if (activeLeds.getReference(i).occupies (x, y))
  419. return i;
  420. return -1;
  421. }
  422. //==============================================================================
  423. enum DisplayMode
  424. {
  425. colourPalette = 0,
  426. canvas
  427. };
  428. DisplayMode currentMode = colourPalette;
  429. //==============================================================================
  430. BitmapLEDProgram* canvasProgram = nullptr;
  431. DrumPadGridProgram* colourPaletteProgram = nullptr;
  432. ColourGrid layout { 3, 3 };
  433. PhysicalTopologySource topologySource;
  434. Block::Ptr activeBlock;
  435. float scaleX = 0.0;
  436. float scaleY = 0.0;
  437. bool doublePress = false;
  438. //==============================================================================
  439. Label infoLabel;
  440. LightpadComponent lightpadComponent;
  441. TextButton clearButton;
  442. LEDComponent brightnessLED;
  443. Slider brightnessSlider;
  444. #if JUCE_IOS
  445. TextButton connectButton;
  446. #endif
  447. //==============================================================================
  448. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainComponent)
  449. };