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.

160 lines
5.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. The code included in this file is provided under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  7. To use, copy, modify, and/or distribute this software for any purpose with or
  8. without fee is hereby granted provided that the above copyright notice and
  9. this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
  11. WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
  12. PURPOSE, ARE DISCLAIMED.
  13. ==============================================================================
  14. */
  15. /*******************************************************************************
  16. The block below describes the properties of this PIP. A PIP is a short snippet
  17. of code that can be read by the Projucer and used to generate a JUCE project.
  18. BEGIN_JUCE_PIP_METADATA
  19. name: ComponentTransformsDemo
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Applies transformations to components.
  24. dependencies: juce_core, juce_data_structures, juce_events, juce_graphics,
  25. juce_gui_basics, juce_gui_extra
  26. exporters: xcode_mac, vs2017, linux_make, androidstudio, xcode_iphone
  27. type: Component
  28. mainClass: ComponentTransformsDemo
  29. useLocalCopy: 1
  30. END_JUCE_PIP_METADATA
  31. *******************************************************************************/
  32. #pragma once
  33. #include "../Assets/DemoUtilities.h"
  34. #include "WidgetsDemo.h"
  35. //==============================================================================
  36. class ComponentTransformsDemo : public Component
  37. {
  38. public:
  39. ComponentTransformsDemo()
  40. {
  41. content.reset (new WidgetsDemo());
  42. addAndMakeVisible (content.get());
  43. content->setSize (750, 500);
  44. for (int i = 0; i < 3; ++i)
  45. {
  46. auto* d = new CornerDragger();
  47. addAndMakeVisible (draggers.add (d));
  48. }
  49. draggers.getUnchecked (0)->relativePos = { 0.10f, 0.15f };
  50. draggers.getUnchecked (1)->relativePos = { 0.95f, 0.05f };
  51. draggers.getUnchecked (2)->relativePos = { 0.05f, 0.85f };
  52. setSize (800, 600);
  53. }
  54. void paint (Graphics& g) override
  55. {
  56. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground));
  57. g.setColour (Colours::white);
  58. g.setFont (15.0f);
  59. g.drawFittedText ("Drag the corner-points around to show how complex components can have affine-transforms applied...",
  60. getLocalBounds().removeFromBottom (40).reduced (10, 0), Justification::centred, 3);
  61. }
  62. void resized() override
  63. {
  64. for (auto* d : draggers)
  65. d->setCentrePosition (proportionOfWidth (d->relativePos.x),
  66. proportionOfHeight (d->relativePos.y));
  67. }
  68. void childBoundsChanged (Component* child) override
  69. {
  70. if (dynamic_cast<CornerDragger*> (child) != nullptr)
  71. updateTransform();
  72. }
  73. private:
  74. std::unique_ptr<Component> content;
  75. struct CornerDragger : public Component
  76. {
  77. CornerDragger()
  78. {
  79. setSize (30, 30);
  80. setRepaintsOnMouseActivity (true);
  81. }
  82. void paint (Graphics& g) override
  83. {
  84. g.setColour (Colours::white.withAlpha (isMouseOverOrDragging() ? 0.9f : 0.5f));
  85. g.fillEllipse (getLocalBounds().reduced (3).toFloat());
  86. g.setColour (Colours::darkgreen);
  87. g.drawEllipse (getLocalBounds().reduced (3).toFloat(), 2.0f);
  88. }
  89. void resized() override
  90. {
  91. constrainer.setMinimumOnscreenAmounts (getHeight(), getWidth(), getHeight(), getWidth());
  92. }
  93. void moved() override
  94. {
  95. if (isMouseButtonDown())
  96. relativePos = getBounds().getCentre().toFloat() / Point<int> (getParentWidth(), getParentHeight()).toFloat();
  97. }
  98. void mouseDown (const MouseEvent& e) override { dragger.startDraggingComponent (this, e); }
  99. void mouseDrag (const MouseEvent& e) override { dragger.dragComponent (this, e, &constrainer); }
  100. Point<float> relativePos;
  101. private:
  102. ComponentBoundsConstrainer constrainer;
  103. ComponentDragger dragger;
  104. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CornerDragger)
  105. };
  106. OwnedArray<CornerDragger> draggers;
  107. Point<float> getDraggerPos (int index) const
  108. {
  109. return draggers.getUnchecked (index)->getBounds().getCentre().toFloat();
  110. }
  111. void updateTransform()
  112. {
  113. auto p0 = getDraggerPos (0);
  114. auto p1 = getDraggerPos (1);
  115. auto p2 = getDraggerPos (2);
  116. if (p0 != p1 && p1 != p2 && p0 != p2)
  117. content->setTransform (AffineTransform::fromTargetPoints (0, 0, p0.x, p0.y,
  118. (float) content->getWidth(), 0, p1.x, p1.y,
  119. 0, (float) content->getHeight(), p2.x, p2.y));
  120. }
  121. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComponentTransformsDemo)
  122. };