From 319ca33637829cf3a3c469c00eb70758d442eabe Mon Sep 17 00:00:00 2001 From: ed Date: Tue, 13 Jun 2017 12:48:21 +0100 Subject: [PATCH] BLOCKS: Some code cleanup and modernisation in BlocksDrawing --- .../BlocksDrawing/Source/LightpadComponent.h | 24 ++++++------- .../BlocksDrawing/Source/MainComponent.cpp | 35 +++++++++---------- .../BlocksDrawing/Source/MainComponent.h | 14 ++++---- 3 files changed, 36 insertions(+), 37 deletions(-) diff --git a/examples/BLOCKS/BlocksDrawing/Source/LightpadComponent.h b/examples/BLOCKS/BlocksDrawing/Source/LightpadComponent.h index 148b6901b8..557fc17c1d 100644 --- a/examples/BLOCKS/BlocksDrawing/Source/LightpadComponent.h +++ b/examples/BLOCKS/BlocksDrawing/Source/LightpadComponent.h @@ -58,8 +58,8 @@ class LightpadComponent : public Component public: LightpadComponent () { - for (int x = 0; x < 15; ++x) - for (int y = 0; y < 15; ++y) + for (auto x = 0; x < 15; ++x) + for (auto y = 0; y < 15; ++y) addAndMakeVisible (leds.add (new LEDComponent())); } @@ -81,13 +81,13 @@ public: void resized() override { - Rectangle r = getLocalBounds().reduced (10); + auto r = getLocalBounds().reduced (10); - int circleWidth = r.getWidth() / 15; - int circleHeight = r.getHeight() / 15; + auto circleWidth = r.getWidth() / 15; + auto circleHeight = r.getHeight() / 15; - for (int x = 0; x < 15; ++x) - for (int y = 0; y < 15; ++y) + for (auto x = 0; x < 15; ++x) + for (auto y = 0; y < 15; ++y) leds.getUnchecked ((x * 15) + y)->setBounds (r.getX() + (x * circleWidth), r.getY() + (y * circleHeight), circleWidth, circleHeight); @@ -95,9 +95,9 @@ public: void mouseDown (const MouseEvent& e) override { - for (int x = 0; x < 15; ++x) + for (auto x = 0; x < 15; ++x) { - for (int y = 0; y < 15; ++y) + for (auto y = 0; y < 15; ++y) { if (leds.getUnchecked ((x * 15) + y)->getBounds().contains (e.position.toInt())) { @@ -109,13 +109,13 @@ public: void mouseDrag (const MouseEvent& e) override { - for (int x = 0; x < 15; ++x) + for (auto x = 0; x < 15; ++x) { - for (int y = 0; y < 15; ++y) + for (auto y = 0; y < 15; ++y) { if (leds.getUnchecked ((x * 15) + y)->getBounds().contains (e.position.toInt())) { - const Time t = e.eventTime; + const auto t = e.eventTime; if (lastLED == Point (x, y) && t.toMilliseconds() - lastMouseEventTime.toMilliseconds() < 50) return; diff --git a/examples/BLOCKS/BlocksDrawing/Source/MainComponent.cpp b/examples/BLOCKS/BlocksDrawing/Source/MainComponent.cpp index 214a45c173..614dae888d 100644 --- a/examples/BLOCKS/BlocksDrawing/Source/MainComponent.cpp +++ b/examples/BLOCKS/BlocksDrawing/Source/MainComponent.cpp @@ -79,10 +79,10 @@ void MainComponent::resized() { infoLabel.centreWithSize (getWidth(), 100); - Rectangle bounds = getLocalBounds().reduced (20); + auto bounds = getLocalBounds().reduced (20); // top buttons - Rectangle topButtonArea = bounds.removeFromTop (getHeight() / 20); + auto topButtonArea = bounds.removeFromTop (getHeight() / 20); topButtonArea.removeFromLeft (20); clearButton.setBounds (topButtonArea.removeFromLeft (80)); @@ -94,15 +94,12 @@ void MainComponent::resized() bounds.removeFromTop (20); - // brightness controls - Rectangle brightnessControlBounds; - - Desktop::DisplayOrientation orientation = Desktop::getInstance().getCurrentOrientation(); + auto orientation = Desktop::getInstance().getCurrentOrientation(); if (orientation == Desktop::DisplayOrientation::upright || orientation == Desktop::DisplayOrientation::upsideDown) { - brightnessControlBounds = bounds.removeFromBottom (getHeight() / 10); + auto brightnessControlBounds = bounds.removeFromBottom (getHeight() / 10); brightnessSlider.setSliderStyle (Slider::SliderStyle::LinearHorizontal); brightnessLED.setBounds (brightnessControlBounds.removeFromLeft (getHeight() / 10)); @@ -110,7 +107,7 @@ void MainComponent::resized() } else { - brightnessControlBounds = bounds.removeFromRight (getWidth() / 10); + auto brightnessControlBounds = bounds.removeFromRight (getWidth() / 10); brightnessSlider.setSliderStyle (Slider::SliderStyle::LinearVertical); brightnessLED.setBounds (brightnessControlBounds.removeFromTop (getWidth() / 10)); @@ -118,7 +115,7 @@ void MainComponent::resized() } // lightpad component - int sideLength = jmin (bounds.getWidth() - 40, bounds.getHeight() - 40); + auto sideLength = jmin (bounds.getWidth() - 40, bounds.getHeight() - 40); lightpadComponent.centreWithSize (sideLength, sideLength); } @@ -132,7 +129,7 @@ void MainComponent::topologyChanged() detachActiveBlock(); // Get the array of currently connected Block objects from the PhysicalTopologySource - Block::Array blocks = topologySource.getCurrentTopology().blocks; + auto blocks = topologySource.getCurrentTopology().blocks; // Iterate over the array of Block objects for (auto b : blocks) @@ -154,8 +151,8 @@ void MainComponent::topologyChanged() if (auto grid = activeBlock->getLEDGrid()) { // Work out scale factors to translate X and Y touches to LED indexes - scaleX = (float) (grid->getNumColumns()) / activeBlock->getWidth(); - scaleY = (float) (grid->getNumRows()) / activeBlock->getHeight(); + scaleX = (float) (grid->getNumColumns() - 1) / activeBlock->getWidth(); + scaleY = (float) (grid->getNumRows() - 1) / activeBlock->getHeight(); setLEDProgram (*activeBlock); } @@ -173,8 +170,8 @@ void MainComponent::topologyChanged() void MainComponent::touchChanged (TouchSurface&, const TouchSurface::Touch& touch) { // Translate X and Y touch events to LED indexes - int xLed = roundToInt (touch.x * scaleX); - int yLed = roundToInt (touch.y * scaleY); + auto xLed = roundToInt (touch.x * scaleX); + auto yLed = roundToInt (touch.y * scaleY); if (currentMode == colourPalette) { @@ -183,8 +180,9 @@ void MainComponent::touchChanged (TouchSurface&, const TouchSurface::Touch& touc if (auto* colourPaletteProgram = getPaletteProgram()) { colourPaletteProgram->setGridFills (layout.numColumns, layout.numRows, layout.gridFillArray); - brightnessLED.setColour (layout.currentColour.withBrightness (layout.currentColour == Colours::black ? 0.0f - : static_cast (brightnessSlider.getValue()))); + brightnessLED.setColour (layout.currentColour + .withBrightness (layout.currentColour == Colours::black ? 0.0f + : static_cast (brightnessSlider.getValue()))); } } } @@ -230,8 +228,9 @@ void MainComponent::buttonClicked (Button* b) void MainComponent::sliderValueChanged (Slider* s) { if (s == &brightnessSlider) - brightnessLED.setColour (layout.currentColour.withBrightness (layout.currentColour == Colours::black ? 0.0f - : static_cast (brightnessSlider.getValue()))); + brightnessLED.setColour (layout.currentColour + .withBrightness (layout.currentColour == Colours::black ? 0.0f + : static_cast (brightnessSlider.getValue()))); } void MainComponent::timerCallback() diff --git a/examples/BLOCKS/BlocksDrawing/Source/MainComponent.h b/examples/BLOCKS/BlocksDrawing/Source/MainComponent.h index 4bde1d0f0d..3ce58afcac 100644 --- a/examples/BLOCKS/BlocksDrawing/Source/MainComponent.h +++ b/examples/BLOCKS/BlocksDrawing/Source/MainComponent.h @@ -49,11 +49,11 @@ struct ColourGrid { gridFillArray.clear(); - int counter = 0; + auto counter = 0; - for (int i = 0; i < numColumns; ++i) + for (auto i = 0; i < numColumns; ++i) { - for (int j = 0; j < numRows; ++j) + for (auto j = 0; j < numRows; ++j) { DrumPadGridProgram::GridFill fill; Colour colourToUse = colourArray.getUnchecked (counter); @@ -78,12 +78,12 @@ struct ColourGrid */ bool setActiveColourForTouch (int x, int y) { - bool colourHasChanged = false; + auto colourHasChanged = false; - int xindex = x / 5; - int yindex = y / 5; + auto xindex = x / 5; + auto yindex = y / 5; - Colour newColour = colourArray.getUnchecked ((yindex * 3) + xindex); + auto newColour = colourArray.getUnchecked ((yindex * 3) + xindex); if (currentColour != newColour) { currentColour = newColour;