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.

382 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. auto bounds = getLocalBounds().reduced (20);
  62. // top buttons
  63. auto 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. auto orientation = Desktop::getInstance().getCurrentOrientation();
  72. if (orientation == Desktop::DisplayOrientation::upright
  73. || orientation == Desktop::DisplayOrientation::upsideDown)
  74. {
  75. auto brightnessControlBounds = bounds.removeFromBottom (getHeight() / 10);
  76. brightnessSlider.setSliderStyle (Slider::SliderStyle::LinearHorizontal);
  77. brightnessLED.setBounds (brightnessControlBounds.removeFromLeft (getHeight() / 10));
  78. brightnessSlider.setBounds (brightnessControlBounds);
  79. }
  80. else
  81. {
  82. auto brightnessControlBounds = bounds.removeFromRight (getWidth() / 10);
  83. brightnessSlider.setSliderStyle (Slider::SliderStyle::LinearVertical);
  84. brightnessLED.setBounds (brightnessControlBounds.removeFromTop (getWidth() / 10));
  85. brightnessSlider.setBounds (brightnessControlBounds);
  86. }
  87. // lightpad component
  88. auto sideLength = jmin (bounds.getWidth() - 40, bounds.getHeight() - 40);
  89. lightpadComponent.centreWithSize (sideLength, sideLength);
  90. }
  91. void MainComponent::topologyChanged()
  92. {
  93. lightpadComponent.setVisible (false);
  94. infoLabel.setVisible (true);
  95. // Reset the activeBlock object
  96. if (activeBlock != nullptr)
  97. detachActiveBlock();
  98. // Get the array of currently connected Block objects from the PhysicalTopologySource
  99. auto blocks = topologySource.getCurrentTopology().blocks;
  100. // Iterate over the array of Block objects
  101. for (auto b : blocks)
  102. {
  103. // Find the first Lightpad
  104. if (b->getType() == Block::Type::lightPadBlock)
  105. {
  106. activeBlock = b;
  107. // Register MainContentComponent as a listener to the touch surface
  108. if (auto surface = activeBlock->getTouchSurface())
  109. surface->addListener (this);
  110. // Register MainContentComponent as a listener to any buttons
  111. for (auto button : activeBlock->getButtons())
  112. button->addListener (this);
  113. // Get the LEDGrid object from the Lightpad and set its program to the program for the current mode
  114. if (auto grid = activeBlock->getLEDGrid())
  115. {
  116. // Work out scale factors to translate X and Y touches to LED indexes
  117. scaleX = (float) (grid->getNumColumns() - 1) / activeBlock->getWidth();
  118. scaleY = (float) (grid->getNumRows() - 1) / activeBlock->getHeight();
  119. setLEDProgram (*activeBlock);
  120. }
  121. // Make the on screen Lighpad component visible
  122. lightpadComponent.setVisible (true);
  123. infoLabel.setVisible (false);
  124. break;
  125. }
  126. }
  127. }
  128. //==============================================================================
  129. void MainComponent::touchChanged (TouchSurface&, const TouchSurface::Touch& touch)
  130. {
  131. // Translate X and Y touch events to LED indexes
  132. auto xLed = roundToInt (touch.x * scaleX);
  133. auto yLed = roundToInt (touch.y * scaleY);
  134. if (currentMode == colourPalette)
  135. {
  136. if (layout.setActiveColourForTouch (xLed, yLed))
  137. {
  138. if (auto* colourPaletteProgram = getPaletteProgram())
  139. {
  140. colourPaletteProgram->setGridFills (layout.numColumns, layout.numRows, layout.gridFillArray);
  141. brightnessLED.setColour (layout.currentColour
  142. .withBrightness (layout.currentColour == Colours::black ? 0.0f
  143. : static_cast<float> (brightnessSlider.getValue())));
  144. }
  145. }
  146. }
  147. else if (currentMode == canvas)
  148. {
  149. drawLED ((uint32) xLed, (uint32) yLed, touch.z, layout.currentColour);
  150. }
  151. }
  152. void MainComponent::buttonReleased (ControlButton&, Block::Timestamp)
  153. {
  154. if (currentMode == canvas)
  155. {
  156. // Wait 500ms to see if there is a second press
  157. if (! isTimerRunning())
  158. startTimer (500);
  159. else
  160. doublePress = true;
  161. }
  162. else if (currentMode == colourPalette)
  163. {
  164. // Switch to canvas mode and set the LEDGrid program
  165. currentMode = canvas;
  166. setLEDProgram (*activeBlock);
  167. }
  168. }
  169. void MainComponent::buttonClicked (Button* b)
  170. {
  171. #if JUCE_IOS
  172. if (b == &connectButton)
  173. {
  174. BluetoothMidiDevicePairingDialogue::open();
  175. return;
  176. }
  177. #else
  178. ignoreUnused (b);
  179. #endif
  180. clearLEDs();
  181. }
  182. void MainComponent::sliderValueChanged (Slider* s)
  183. {
  184. if (s == &brightnessSlider)
  185. brightnessLED.setColour (layout.currentColour
  186. .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. }