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.

101 lines
3.6KB

  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_ElementSiblingComponent.h"
  15. #include "../UI/jucer_PaintRoutineEditor.h"
  16. //==============================================================================
  17. class PointComponent : public ElementSiblingComponent
  18. {
  19. public:
  20. PointComponent (PaintElement* const e)
  21. : ElementSiblingComponent (e)
  22. {
  23. setSize (11, 11);
  24. setMouseCursor (MouseCursor::UpDownLeftRightResizeCursor);
  25. }
  26. virtual RelativePositionedRectangle getPosition() = 0;
  27. virtual void setPosition (const RelativePositionedRectangle& newPos) = 0;
  28. void updatePosition() override
  29. {
  30. if (dynamic_cast<PaintRoutineEditor*> (getParentComponent()) != nullptr)
  31. {
  32. const Rectangle<int> area (((PaintRoutineEditor*) getParentComponent())->getComponentArea());
  33. const Rectangle<int> r (getPosition().getRectangle (area, owner->getDocument()->getComponentLayout()));
  34. setCentrePosition (r.getX(), r.getY());
  35. }
  36. }
  37. //==============================================================================
  38. void paint (Graphics& g) override
  39. {
  40. g.setColour (Colours::white);
  41. g.drawEllipse (2.0f, 2.0f, getWidth() - 4.0f, getHeight() - 4.0f, 2.0f);
  42. g.setColour (Colours::black);
  43. g.drawEllipse (1.0f, 1.0f, getWidth() - 2.0f, getHeight() - 2.0f, 2.0f);
  44. }
  45. //==============================================================================
  46. void mouseDown (const MouseEvent&) override
  47. {
  48. const Rectangle<int> area (((PaintRoutineEditor*) getParentComponent())->getComponentArea());
  49. dragX = getX() + getWidth() / 2 - area.getX();
  50. dragY = getY() + getHeight() / 2 - area.getY();
  51. }
  52. void mouseDrag (const MouseEvent& e) override
  53. {
  54. const Rectangle<int> area (((PaintRoutineEditor*) getParentComponent())->getComponentArea());
  55. int x = dragX + e.getDistanceFromDragStartX();
  56. int y = dragY + e.getDistanceFromDragStartY();
  57. if (JucerDocument* const document = owner->getDocument())
  58. {
  59. x = document->snapPosition (x);
  60. y = document->snapPosition (y);
  61. const RelativePositionedRectangle original (getPosition());
  62. RelativePositionedRectangle pr (original);
  63. Rectangle<int> r (pr.getRectangle (Rectangle<int> (0, 0, area.getWidth(), area.getHeight()),
  64. document->getComponentLayout()));
  65. r.setPosition (x, y);
  66. pr.updateFrom (r.getX(), r.getY(), r.getWidth(), r.getHeight(),
  67. Rectangle<int> (0, 0, area.getWidth(), area.getHeight()),
  68. document->getComponentLayout());
  69. if (pr != original)
  70. setPosition (pr);
  71. }
  72. }
  73. void mouseUp (const MouseEvent&) override
  74. {
  75. }
  76. private:
  77. int dragX, dragY;
  78. };