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.

108 lines
3.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. #pragma once
  19. #include "jucer_ElementSiblingComponent.h"
  20. #include "../UI/jucer_PaintRoutineEditor.h"
  21. //==============================================================================
  22. class PointComponent : public ElementSiblingComponent
  23. {
  24. public:
  25. PointComponent (PaintElement* const e)
  26. : ElementSiblingComponent (e)
  27. {
  28. setSize (11, 11);
  29. setMouseCursor (MouseCursor::UpDownLeftRightResizeCursor);
  30. }
  31. virtual RelativePositionedRectangle getPosition() = 0;
  32. virtual void setPosition (const RelativePositionedRectangle& newPos) = 0;
  33. void updatePosition() override
  34. {
  35. if (dynamic_cast<PaintRoutineEditor*> (getParentComponent()) != nullptr)
  36. {
  37. const Rectangle<int> area (((PaintRoutineEditor*) getParentComponent())->getComponentArea());
  38. const Rectangle<int> r (getPosition().getRectangle (area, owner->getDocument()->getComponentLayout()));
  39. setCentrePosition (r.getX(), r.getY());
  40. }
  41. }
  42. //==============================================================================
  43. void paint (Graphics& g) override
  44. {
  45. g.setColour (Colours::white);
  46. g.drawEllipse (2.0f, 2.0f, (float) getWidth() - 4.0f, (float) getHeight() - 4.0f, 2.0f);
  47. g.setColour (Colours::black);
  48. g.drawEllipse (1.0f, 1.0f, (float) getWidth() - 2.0f, (float) getHeight() - 2.0f, 2.0f);
  49. }
  50. //==============================================================================
  51. void mouseDown (const MouseEvent&) override
  52. {
  53. const Rectangle<int> area (((PaintRoutineEditor*) getParentComponent())->getComponentArea());
  54. dragX = getX() + getWidth() / 2 - area.getX();
  55. dragY = getY() + getHeight() / 2 - area.getY();
  56. }
  57. void mouseDrag (const MouseEvent& e) override
  58. {
  59. const Rectangle<int> area (((PaintRoutineEditor*) getParentComponent())->getComponentArea());
  60. int x = dragX + e.getDistanceFromDragStartX();
  61. int y = dragY + e.getDistanceFromDragStartY();
  62. if (JucerDocument* const document = owner->getDocument())
  63. {
  64. x = document->snapPosition (x);
  65. y = document->snapPosition (y);
  66. const RelativePositionedRectangle original (getPosition());
  67. RelativePositionedRectangle pr (original);
  68. Rectangle<int> r (pr.getRectangle (Rectangle<int> (0, 0, area.getWidth(), area.getHeight()),
  69. document->getComponentLayout()));
  70. r.setPosition (x, y);
  71. pr.updateFrom (r.getX(), r.getY(), r.getWidth(), r.getHeight(),
  72. Rectangle<int> (0, 0, area.getWidth(), area.getHeight()),
  73. document->getComponentLayout());
  74. if (pr != original)
  75. setPosition (pr);
  76. }
  77. }
  78. void mouseUp (const MouseEvent&) override
  79. {
  80. }
  81. private:
  82. int dragX, dragY;
  83. };