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.

74 lines
2.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. #pragma once
  14. #include "../jucer_JucerDocument.h"
  15. #include "../jucer_PaintRoutine.h"
  16. //==============================================================================
  17. class SnapGridPainter
  18. {
  19. public:
  20. SnapGridPainter()
  21. : snapGridSize (-1), snapShown (false)
  22. {
  23. }
  24. bool updateFromDesign (JucerDocument& design)
  25. {
  26. if (snapGridSize != design.getSnappingGridSize()
  27. || snapShown != (design.isSnapShown() && design.isSnapActive (false)))
  28. {
  29. snapGridSize = design.getSnappingGridSize();
  30. snapShown = design.isSnapShown() && design.isSnapActive (false);
  31. return true;
  32. }
  33. return false;
  34. }
  35. void draw (Graphics& g, PaintRoutine* backgroundGraphics)
  36. {
  37. if (snapShown && snapGridSize > 2)
  38. {
  39. Colour col (Colours::black);
  40. if (backgroundGraphics != nullptr)
  41. col = backgroundGraphics->getBackgroundColour().contrasting();
  42. const Rectangle<int> clip (g.getClipBounds());
  43. RectangleList<float> gridLines;
  44. for (int x = clip.getX() - (clip.getX() % snapGridSize); x < clip.getRight(); x += snapGridSize)
  45. gridLines.addWithoutMerging (Rectangle<float> ((float) x, 0.0f, 1.0f, (float) clip.getBottom()));
  46. for (int y = clip.getY() - (clip.getY() % snapGridSize); y < clip.getBottom(); y += snapGridSize)
  47. gridLines.addWithoutMerging (Rectangle<float> (0.0f, (float) y, (float) clip.getRight(), 1.0f));
  48. g.setColour (col.withAlpha (0.1f));
  49. g.fillRectList (gridLines);
  50. }
  51. }
  52. private:
  53. int snapGridSize;
  54. bool snapShown;
  55. };