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.

158 lines
5.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #include "../JuceDemoHeader.h"
  20. //==============================================================================
  21. class ComponentTransformsDemo : public Component
  22. {
  23. public:
  24. ComponentTransformsDemo()
  25. {
  26. addAndMakeVisible (content = createContentComp());
  27. content->setSize (800, 600);
  28. for (int i = 0; i < 3; ++i)
  29. {
  30. CornerDragger* d = new CornerDragger();
  31. draggers.add (d);
  32. addAndMakeVisible (d);
  33. }
  34. draggers.getUnchecked(0)->relativePos = Point<float> (0.10f, 0.15f);
  35. draggers.getUnchecked(1)->relativePos = Point<float> (0.95f, 0.05f);
  36. draggers.getUnchecked(2)->relativePos = Point<float> (0.05f, 0.85f);
  37. }
  38. void paint (Graphics& g) override
  39. {
  40. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground));
  41. g.setColour (Colours::white);
  42. g.setFont (15.0f);
  43. g.drawFittedText ("Drag the corner-points around to show how complex components can have affine-transforms applied...",
  44. getLocalBounds().removeFromBottom (40).reduced (10, 0), Justification::centred, 3);
  45. }
  46. void resized() override
  47. {
  48. for (int i = 0; i < 3; ++i)
  49. {
  50. CornerDragger* d = draggers.getUnchecked(i);
  51. d->setCentrePosition (proportionOfWidth (d->relativePos.x),
  52. proportionOfHeight (d->relativePos.y));
  53. }
  54. }
  55. void childBoundsChanged (Component* child) override
  56. {
  57. if (dynamic_cast<CornerDragger*> (child) != nullptr)
  58. updateTransform();
  59. }
  60. private:
  61. ScopedPointer<Component> content;
  62. static Component* createContentComp()
  63. {
  64. Array<JuceDemoTypeBase*>& demos (JuceDemoTypeBase::getDemoTypeList());
  65. for (int i = 0; i < demos.size(); ++i)
  66. if (demos.getUnchecked(i)->name.containsIgnoreCase ("Widgets"))
  67. return demos.getUnchecked (i)->createComponent();
  68. jassertfalse;
  69. return nullptr;
  70. }
  71. struct CornerDragger : public Component
  72. {
  73. CornerDragger()
  74. {
  75. setSize (30, 30);
  76. setRepaintsOnMouseActivity (true);
  77. }
  78. void paint (Graphics& g) override
  79. {
  80. g.setColour (Colours::white.withAlpha (isMouseOverOrDragging() ? 0.9f : 0.5f));
  81. g.fillEllipse (getLocalBounds().reduced (3).toFloat());
  82. g.setColour (Colours::darkgreen);
  83. g.drawEllipse (getLocalBounds().reduced (3).toFloat(), 2.0f);
  84. }
  85. void resized() override
  86. {
  87. constrainer.setMinimumOnscreenAmounts (getHeight(), getWidth(), getHeight(), getWidth());
  88. }
  89. void moved() override
  90. {
  91. if (isMouseButtonDown())
  92. relativePos = getBounds().getCentre().toFloat() / Point<int> (getParentWidth(), getParentHeight()).toFloat();
  93. }
  94. void mouseDown (const MouseEvent& e) override { dragger.startDraggingComponent (this, e); }
  95. void mouseDrag (const MouseEvent& e) override { dragger.dragComponent (this, e, &constrainer); }
  96. Point<float> relativePos;
  97. private:
  98. ComponentBoundsConstrainer constrainer;
  99. ComponentDragger dragger;
  100. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CornerDragger)
  101. };
  102. OwnedArray<CornerDragger> draggers;
  103. Point<float> getDraggerPos (int index) const
  104. {
  105. return draggers.getUnchecked(index)->getBounds().getCentre().toFloat();
  106. }
  107. void updateTransform()
  108. {
  109. const Point<float> p0 (getDraggerPos(0));
  110. const Point<float> p1 (getDraggerPos(1));
  111. const Point<float> p2 (getDraggerPos(2));
  112. if (p0 != p1 && p1 != p2 && p0 != p2)
  113. content->setTransform (AffineTransform::fromTargetPoints (0, 0, p0.x, p0.y,
  114. (float) content->getWidth(), 0, p1.x, p1.y,
  115. 0, (float) content->getHeight(), p2.x, p2.y));
  116. }
  117. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComponentTransformsDemo)
  118. };
  119. // This static object will register this demo type in a global list of demos..
  120. static JuceDemoType<ComponentTransformsDemo> demo ("10 Components: Transforms");