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.

157 lines
5.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-12 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "../JuceDemoHeader.h"
  19. //==============================================================================
  20. class ComponentTransformsDemo : public Component
  21. {
  22. public:
  23. ComponentTransformsDemo()
  24. {
  25. addAndMakeVisible (content = createContentComp());
  26. content->setSize (800, 600);
  27. for (int i = 0; i < 3; ++i)
  28. {
  29. CornerDragger* d = new CornerDragger();
  30. draggers.add (d);
  31. addAndMakeVisible (d);
  32. }
  33. draggers.getUnchecked(0)->relativePos = Point<float> (0.10f, 0.15f);
  34. draggers.getUnchecked(1)->relativePos = Point<float> (0.95f, 0.05f);
  35. draggers.getUnchecked(2)->relativePos = Point<float> (0.05f, 0.85f);
  36. }
  37. void paint (Graphics& g)
  38. {
  39. fillBrushedAluminiumBackground (g);
  40. g.setColour (Colours::white);
  41. g.setFont (15.0f);
  42. g.drawFittedText ("Drag the corner-points around to show how complex components can have affine-transforms applied...",
  43. getLocalBounds().removeFromBottom (40).reduced (10, 0), Justification::centred, 3);
  44. }
  45. void resized() override
  46. {
  47. for (int i = 0; i < 3; ++i)
  48. {
  49. CornerDragger* d = draggers.getUnchecked(i);
  50. d->setCentrePosition (proportionOfWidth (d->relativePos.x),
  51. proportionOfHeight (d->relativePos.y));
  52. }
  53. }
  54. void childBoundsChanged (Component* child) override
  55. {
  56. if (dynamic_cast<CornerDragger*> (child) != nullptr)
  57. updateTransform();
  58. }
  59. private:
  60. ScopedPointer<Component> content;
  61. static Component* createContentComp()
  62. {
  63. Array<JuceDemoTypeBase*>& demos (JuceDemoTypeBase::getDemoTypeList());
  64. for (int i = 0; i < demos.size(); ++i)
  65. if (demos.getUnchecked(i)->name.containsIgnoreCase ("Widgets"))
  66. return demos.getUnchecked (i)->createComponent();
  67. jassertfalse;
  68. return nullptr;
  69. }
  70. struct CornerDragger : public Component
  71. {
  72. CornerDragger()
  73. {
  74. setSize (30, 30);
  75. setRepaintsOnMouseActivity (true);
  76. }
  77. void paint (Graphics& g)
  78. {
  79. g.setColour (Colours::white.withAlpha (isMouseOverOrDragging() ? 0.9f : 0.5f));
  80. g.fillEllipse (getLocalBounds().reduced (3).toFloat());
  81. g.setColour (Colours::darkgreen);
  82. g.drawEllipse (getLocalBounds().reduced (3).toFloat(), 2.0f);
  83. }
  84. void resized() override
  85. {
  86. constrainer.setMinimumOnscreenAmounts (getHeight(), getWidth(), getHeight(), getWidth());
  87. }
  88. void moved() override
  89. {
  90. if (isMouseButtonDown())
  91. relativePos = getBounds().getCentre().toFloat() / Point<int> (getParentWidth(), getParentHeight()).toFloat();
  92. }
  93. void mouseDown (const MouseEvent& e) override { dragger.startDraggingComponent (this, e); }
  94. void mouseDrag (const MouseEvent& e) override { dragger.dragComponent (this, e, &constrainer); }
  95. Point<float> relativePos;
  96. private:
  97. ComponentBoundsConstrainer constrainer;
  98. ComponentDragger dragger;
  99. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CornerDragger);
  100. };
  101. OwnedArray<CornerDragger> draggers;
  102. Point<float> getDraggerPos (int index) const
  103. {
  104. return draggers.getUnchecked(index)->getBounds().getCentre().toFloat();
  105. }
  106. void updateTransform()
  107. {
  108. const Point<float> p0 (getDraggerPos(0));
  109. const Point<float> p1 (getDraggerPos(1));
  110. const Point<float> p2 (getDraggerPos(2));
  111. if (p0 != p1 && p1 != p2 && p0 != p2)
  112. content->setTransform (AffineTransform::fromTargetPoints (0, 0, p0.x, p0.y,
  113. (float) content->getWidth(), 0, p1.x, p1.y,
  114. 0, (float) content->getHeight(), p2.x, p2.y));
  115. }
  116. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComponentTransformsDemo);
  117. };
  118. // This static object will register this demo type in a global list of demos..
  119. static JuceDemoType<ComponentTransformsDemo> demo ("10 Components: Transforms");