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.

81 lines
2.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #pragma once
  18. #include "../jucer_JucerDocument.h"
  19. #include "../jucer_PaintRoutine.h"
  20. //==============================================================================
  21. class SnapGridPainter
  22. {
  23. public:
  24. SnapGridPainter()
  25. : snapGridSize (-1), snapShown (false)
  26. {
  27. }
  28. bool updateFromDesign (JucerDocument& design)
  29. {
  30. if (snapGridSize != design.getSnappingGridSize()
  31. || snapShown != (design.isSnapShown() && design.isSnapActive (false)))
  32. {
  33. snapGridSize = design.getSnappingGridSize();
  34. snapShown = design.isSnapShown() && design.isSnapActive (false);
  35. return true;
  36. }
  37. return false;
  38. }
  39. void draw (Graphics& g, PaintRoutine* backgroundGraphics)
  40. {
  41. if (snapShown && snapGridSize > 2)
  42. {
  43. Colour col (Colours::black);
  44. if (backgroundGraphics != nullptr)
  45. col = backgroundGraphics->getBackgroundColour().contrasting();
  46. const Rectangle<int> clip (g.getClipBounds());
  47. RectangleList<float> gridLines;
  48. for (int x = clip.getX() - (clip.getX() % snapGridSize); x < clip.getRight(); x += snapGridSize)
  49. gridLines.addWithoutMerging (Rectangle<float> ((float) x, 0.0f, 1.0f, (float) clip.getBottom()));
  50. for (int y = clip.getY() - (clip.getY() % snapGridSize); y < clip.getBottom(); y += snapGridSize)
  51. gridLines.addWithoutMerging (Rectangle<float> (0.0f, (float) y, (float) clip.getRight(), 1.0f));
  52. g.setColour (col.withAlpha (0.1f));
  53. g.fillRectList (gridLines);
  54. }
  55. }
  56. private:
  57. int snapGridSize;
  58. bool snapShown;
  59. };