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.

156 lines
5.2KB

  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. #include "../JuceDemoHeader.h"
  18. //==============================================================================
  19. class ComponentTransformsDemo : public Component
  20. {
  21. public:
  22. ComponentTransformsDemo()
  23. {
  24. addAndMakeVisible (content = createContentComp());
  25. content->setSize (800, 600);
  26. for (int i = 0; i < 3; ++i)
  27. {
  28. CornerDragger* d = new CornerDragger();
  29. draggers.add (d);
  30. addAndMakeVisible (d);
  31. }
  32. draggers.getUnchecked(0)->relativePos = Point<float> (0.10f, 0.15f);
  33. draggers.getUnchecked(1)->relativePos = Point<float> (0.95f, 0.05f);
  34. draggers.getUnchecked(2)->relativePos = Point<float> (0.05f, 0.85f);
  35. }
  36. void paint (Graphics& g) override
  37. {
  38. fillStandardDemoBackground (g);
  39. g.setColour (Colours::white);
  40. g.setFont (15.0f);
  41. g.drawFittedText ("Drag the corner-points around to show how complex components can have affine-transforms applied...",
  42. getLocalBounds().removeFromBottom (40).reduced (10, 0), Justification::centred, 3);
  43. }
  44. void resized() override
  45. {
  46. for (int i = 0; i < 3; ++i)
  47. {
  48. CornerDragger* d = draggers.getUnchecked(i);
  49. d->setCentrePosition (proportionOfWidth (d->relativePos.x),
  50. proportionOfHeight (d->relativePos.y));
  51. }
  52. }
  53. void childBoundsChanged (Component* child) override
  54. {
  55. if (dynamic_cast<CornerDragger*> (child) != nullptr)
  56. updateTransform();
  57. }
  58. private:
  59. ScopedPointer<Component> content;
  60. static Component* createContentComp()
  61. {
  62. Array<JuceDemoTypeBase*>& demos (JuceDemoTypeBase::getDemoTypeList());
  63. for (int i = 0; i < demos.size(); ++i)
  64. if (demos.getUnchecked(i)->name.containsIgnoreCase ("Widgets"))
  65. return demos.getUnchecked (i)->createComponent();
  66. jassertfalse;
  67. return nullptr;
  68. }
  69. struct CornerDragger : public Component
  70. {
  71. CornerDragger()
  72. {
  73. setSize (30, 30);
  74. setRepaintsOnMouseActivity (true);
  75. }
  76. void paint (Graphics& g) override
  77. {
  78. g.setColour (Colours::white.withAlpha (isMouseOverOrDragging() ? 0.9f : 0.5f));
  79. g.fillEllipse (getLocalBounds().reduced (3).toFloat());
  80. g.setColour (Colours::darkgreen);
  81. g.drawEllipse (getLocalBounds().reduced (3).toFloat(), 2.0f);
  82. }
  83. void resized() override
  84. {
  85. constrainer.setMinimumOnscreenAmounts (getHeight(), getWidth(), getHeight(), getWidth());
  86. }
  87. void moved() override
  88. {
  89. if (isMouseButtonDown())
  90. relativePos = getBounds().getCentre().toFloat() / Point<int> (getParentWidth(), getParentHeight()).toFloat();
  91. }
  92. void mouseDown (const MouseEvent& e) override { dragger.startDraggingComponent (this, e); }
  93. void mouseDrag (const MouseEvent& e) override { dragger.dragComponent (this, e, &constrainer); }
  94. Point<float> relativePos;
  95. private:
  96. ComponentBoundsConstrainer constrainer;
  97. ComponentDragger dragger;
  98. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CornerDragger)
  99. };
  100. OwnedArray<CornerDragger> draggers;
  101. Point<float> getDraggerPos (int index) const
  102. {
  103. return draggers.getUnchecked(index)->getBounds().getCentre().toFloat();
  104. }
  105. void updateTransform()
  106. {
  107. const Point<float> p0 (getDraggerPos(0));
  108. const Point<float> p1 (getDraggerPos(1));
  109. const Point<float> p2 (getDraggerPos(2));
  110. if (p0 != p1 && p1 != p2 && p0 != p2)
  111. content->setTransform (AffineTransform::fromTargetPoints (0, 0, p0.x, p0.y,
  112. (float) content->getWidth(), 0, p1.x, p1.y,
  113. 0, (float) content->getHeight(), p2.x, p2.y));
  114. }
  115. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComponentTransformsDemo)
  116. };
  117. // This static object will register this demo type in a global list of demos..
  118. static JuceDemoType<ComponentTransformsDemo> demo ("10 Components: Transforms");