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.

383 lines
12KB

  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. #include "MainComponent.h"
  20. MainComponent::MainComponent()
  21. {
  22. activeLeds.clear();
  23. // Register MainContentComponent as a listener to the PhysicalTopologySource object
  24. topologySource.addListener (this);
  25. infoLabel.setText ("Connect a Lightpad Block to draw.", dontSendNotification);
  26. infoLabel.setJustificationType (Justification::centred);
  27. addAndMakeVisible (infoLabel);
  28. addAndMakeVisible (lightpadComponent);
  29. lightpadComponent.setVisible (false);
  30. lightpadComponent.addListener (this);
  31. clearButton.setButtonText ("Clear");
  32. clearButton.addListener (this);
  33. clearButton.setAlwaysOnTop (true);
  34. addAndMakeVisible (clearButton);
  35. brightnessSlider.setRange (0.0, 1.0);
  36. brightnessSlider.setValue (1.0);
  37. brightnessSlider.setAlwaysOnTop (true);
  38. brightnessSlider.setTextBoxStyle (Slider::TextEntryBoxPosition::NoTextBox, false, 0, 0);
  39. brightnessSlider.addListener (this);
  40. addAndMakeVisible (brightnessSlider);
  41. brightnessLED.setAlwaysOnTop (true);
  42. brightnessLED.setColour (layout.currentColour.withBrightness (static_cast<float> (brightnessSlider.getValue())));
  43. addAndMakeVisible (brightnessLED);
  44. #if JUCE_IOS
  45. connectButton.setButtonText ("Connect");
  46. connectButton.addListener (this);
  47. connectButton.setAlwaysOnTop (true);
  48. addAndMakeVisible (connectButton);
  49. #endif
  50. setSize (600, 600);
  51. }
  52. MainComponent::~MainComponent()
  53. {
  54. if (activeBlock != nullptr)
  55. detachActiveBlock();
  56. lightpadComponent.removeListener (this);
  57. }
  58. void MainComponent::resized()
  59. {
  60. infoLabel.centreWithSize (getWidth(), 100);
  61. Rectangle<int> bounds = getLocalBounds().reduced (20);
  62. // top buttons
  63. Rectangle<int> topButtonArea = bounds.removeFromTop (getHeight() / 20);
  64. topButtonArea.removeFromLeft (20);
  65. clearButton.setBounds (topButtonArea.removeFromLeft (80));
  66. #if JUCE_IOS
  67. topButtonArea.removeFromRight (20);
  68. connectButton.setBounds (topButtonArea.removeFromRight (80));
  69. #endif
  70. bounds.removeFromTop (20);
  71. // brightness controls
  72. Rectangle<int> brightnessControlBounds;
  73. Desktop::DisplayOrientation orientation = Desktop::getInstance().getCurrentOrientation();
  74. if (orientation == Desktop::DisplayOrientation::upright
  75. || orientation == Desktop::DisplayOrientation::upsideDown)
  76. {
  77. brightnessControlBounds = bounds.removeFromBottom (getHeight() / 10);
  78. brightnessSlider.setSliderStyle (Slider::SliderStyle::LinearHorizontal);
  79. brightnessLED.setBounds (brightnessControlBounds.removeFromLeft (getHeight() / 10));
  80. brightnessSlider.setBounds (brightnessControlBounds);
  81. }
  82. else
  83. {
  84. brightnessControlBounds = bounds.removeFromRight (getWidth() / 10);
  85. brightnessSlider.setSliderStyle (Slider::SliderStyle::LinearVertical);
  86. brightnessLED.setBounds (brightnessControlBounds.removeFromTop (getWidth() / 10));
  87. brightnessSlider.setBounds (brightnessControlBounds);
  88. }
  89. // lightpad component
  90. int sideLength = jmin (bounds.getWidth() - 40, bounds.getHeight() - 40);
  91. lightpadComponent.centreWithSize (sideLength, sideLength);
  92. }
  93. void MainComponent::topologyChanged()
  94. {
  95. lightpadComponent.setVisible (false);
  96. infoLabel.setVisible (true);
  97. // Reset the activeBlock object
  98. if (activeBlock != nullptr)
  99. detachActiveBlock();
  100. // Get the array of currently connected Block objects from the PhysicalTopologySource
  101. Block::Array blocks = topologySource.getCurrentTopology().blocks;
  102. // Iterate over the array of Block objects
  103. for (auto b : blocks)
  104. {
  105. // Find the first Lightpad
  106. if (b->getType() == Block::Type::lightPadBlock)
  107. {
  108. activeBlock = b;
  109. // Register MainContentComponent as a listener to the touch surface
  110. if (auto surface = activeBlock->getTouchSurface())
  111. surface->addListener (this);
  112. // Register MainContentComponent as a listener to any buttons
  113. for (auto button : activeBlock->getButtons())
  114. button->addListener (this);
  115. // Get the LEDGrid object from the Lightpad and set its program to the program for the current mode
  116. if (auto grid = activeBlock->getLEDGrid())
  117. {
  118. // Work out scale factors to translate X and Y touches to LED indexes
  119. scaleX = (float) (grid->getNumColumns()) / activeBlock->getWidth();
  120. scaleY = (float) (grid->getNumRows()) / activeBlock->getHeight();
  121. setLEDProgram (*activeBlock);
  122. }
  123. // Make the on screen Lighpad component visible
  124. lightpadComponent.setVisible (true);
  125. infoLabel.setVisible (false);
  126. break;
  127. }
  128. }
  129. }
  130. //==============================================================================
  131. void MainComponent::touchChanged (TouchSurface&, const TouchSurface::Touch& touch)
  132. {
  133. // Translate X and Y touch events to LED indexes
  134. int xLed = roundToInt (touch.x * scaleX);
  135. int yLed = roundToInt (touch.y * scaleY);
  136. if (currentMode == colourPalette)
  137. {
  138. if (layout.setActiveColourForTouch (xLed, yLed))
  139. {
  140. if (auto* colourPaletteProgram = getPaletteProgram())
  141. {
  142. colourPaletteProgram->setGridFills (layout.numColumns, layout.numRows, layout.gridFillArray);
  143. brightnessLED.setColour (layout.currentColour.withBrightness (layout.currentColour == Colours::black ? 0.0f
  144. : static_cast<float> (brightnessSlider.getValue())));
  145. }
  146. }
  147. }
  148. else if (currentMode == canvas)
  149. {
  150. drawLED ((uint32) xLed, (uint32) yLed, touch.z, layout.currentColour);
  151. }
  152. }
  153. void MainComponent::buttonReleased (ControlButton&, Block::Timestamp)
  154. {
  155. if (currentMode == canvas)
  156. {
  157. // Wait 500ms to see if there is a second press
  158. if (! isTimerRunning())
  159. startTimer (500);
  160. else
  161. doublePress = true;
  162. }
  163. else if (currentMode == colourPalette)
  164. {
  165. // Switch to canvas mode and set the LEDGrid program
  166. currentMode = canvas;
  167. setLEDProgram (*activeBlock);
  168. }
  169. }
  170. void MainComponent::buttonClicked (Button* b)
  171. {
  172. #if JUCE_IOS
  173. if (b == &connectButton)
  174. {
  175. BluetoothMidiDevicePairingDialogue::open();
  176. return;
  177. }
  178. #else
  179. ignoreUnused (b);
  180. #endif
  181. clearLEDs();
  182. }
  183. void MainComponent::sliderValueChanged (Slider* s)
  184. {
  185. if (s == &brightnessSlider)
  186. brightnessLED.setColour (layout.currentColour.withBrightness (layout.currentColour == Colours::black ? 0.0f
  187. : static_cast<float> (brightnessSlider.getValue())));
  188. }
  189. void MainComponent::timerCallback()
  190. {
  191. if (doublePress)
  192. {
  193. clearLEDs();
  194. // Reset the doublePress flag
  195. doublePress = false;
  196. }
  197. else
  198. {
  199. // Switch to colour palette mode and set the LEDGrid program
  200. currentMode = colourPalette;
  201. setLEDProgram (*activeBlock);
  202. }
  203. stopTimer();
  204. }
  205. void MainComponent::ledClicked (int x, int y, float z)
  206. {
  207. drawLED ((uint32) x, (uint32) y,
  208. z == 0.0f ? static_cast<float> (brightnessSlider.getValue())
  209. : z * static_cast<float> (brightnessSlider.getValue()), layout.currentColour);
  210. }
  211. void MainComponent::detachActiveBlock()
  212. {
  213. if (auto surface = activeBlock->getTouchSurface())
  214. surface->removeListener (this);
  215. for (auto button : activeBlock->getButtons())
  216. button->removeListener (this);
  217. activeBlock = nullptr;
  218. }
  219. void MainComponent::setLEDProgram (Block& block)
  220. {
  221. if (currentMode == canvas)
  222. {
  223. block.setProgram (new BitmapLEDProgram (block));
  224. // Redraw any previously drawn LEDs
  225. redrawLEDs();
  226. }
  227. else if (currentMode == colourPalette)
  228. {
  229. block.setProgram (new DrumPadGridProgram (block));
  230. // Setup the grid layout
  231. if (auto* program = getPaletteProgram())
  232. program->setGridFills (layout.numColumns, layout.numRows, layout.gridFillArray);
  233. }
  234. }
  235. void MainComponent::clearLEDs()
  236. {
  237. if (auto* canvasProgram = getCanvasProgram())
  238. {
  239. // Clear the LED grid
  240. for (uint32 x = 0; x < 15; ++x)
  241. {
  242. for (uint32 y = 0; y < 15; ++ y)
  243. {
  244. canvasProgram->setLED (x, y, Colours::black);
  245. lightpadComponent.setLEDColour (x, y, Colours::black);
  246. }
  247. }
  248. // Clear the ActiveLED array
  249. activeLeds.clear();
  250. }
  251. }
  252. void MainComponent::drawLED (uint32 x0, uint32 y0, float z, Colour drawColour)
  253. {
  254. if (auto* canvasProgram = getCanvasProgram())
  255. {
  256. // Check if the activeLeds array already contains an ActiveLED object for this LED
  257. auto index = getLEDAt (x0, y0);
  258. // If the colour is black then just set the LED to black and return
  259. if (drawColour == Colours::black)
  260. {
  261. if (index >= 0)
  262. {
  263. canvasProgram->setLED (x0, y0, Colours::black);
  264. lightpadComponent.setLEDColour (x0, y0, Colours::black);
  265. activeLeds.remove (index);
  266. }
  267. return;
  268. }
  269. // If there is no ActiveLED obejct for this LED then create one,
  270. // add it to the array, set the LED on the Block and return
  271. if (index < 0)
  272. {
  273. ActiveLED led;
  274. led.x = x0;
  275. led.y = y0;
  276. led.colour = drawColour;
  277. led.brightness = z;
  278. activeLeds.add (led);
  279. canvasProgram->setLED (led.x, led.y, led.colour.withBrightness (led.brightness));
  280. lightpadComponent.setLEDColour (led.x, led.y, led.colour.withBrightness (led.brightness));
  281. return;
  282. }
  283. // Get the ActiveLED object for this LED
  284. auto currentLed = activeLeds.getReference (index);
  285. // If the LED colour is the same as the draw colour, add the brightnesses together.
  286. // If it is different, blend the colours
  287. if (currentLed.colour == drawColour)
  288. currentLed.brightness = jmin (currentLed.brightness + z, 1.0f);
  289. else
  290. currentLed.colour = currentLed.colour.interpolatedWith (drawColour, z);
  291. // Set the LED on the Block and change the ActiveLED object in the activeLeds array
  292. if (canvasProgram != nullptr)
  293. canvasProgram->setLED (currentLed.x, currentLed.y, currentLed.colour.withBrightness (currentLed.brightness));
  294. lightpadComponent.setLEDColour (currentLed.x, currentLed.y, currentLed.colour.withBrightness (currentLed.brightness));
  295. activeLeds.set (index, currentLed);
  296. }
  297. }
  298. void MainComponent::redrawLEDs()
  299. {
  300. if (auto* canvasProgram = getCanvasProgram())
  301. {
  302. // Iterate over the activeLeds array and set the LEDs on the Block
  303. for (auto led : activeLeds)
  304. {
  305. canvasProgram->setLED (led.x, led.y, led.colour.withBrightness (led.brightness));
  306. lightpadComponent.setLEDColour (led.x, led.y, led.colour.withBrightness (led.brightness));
  307. }
  308. }
  309. }