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.

162 lines
5.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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, vs2022, linux_make, androidstudio, xcode_iphone
  27. moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1
  28. type: Component
  29. mainClass: ComponentTransformsDemo
  30. useLocalCopy: 1
  31. END_JUCE_PIP_METADATA
  32. *******************************************************************************/
  33. #pragma once
  34. #include "../Assets/DemoUtilities.h"
  35. #include "WidgetsDemo.h"
  36. //==============================================================================
  37. class ComponentTransformsDemo final : public Component
  38. {
  39. public:
  40. ComponentTransformsDemo()
  41. {
  42. content.reset (new WidgetsDemo (true));
  43. addAndMakeVisible (content.get());
  44. content->setSize (750, 500);
  45. for (int i = 0; i < 3; ++i)
  46. {
  47. auto* d = new CornerDragger();
  48. addAndMakeVisible (draggers.add (d));
  49. }
  50. draggers.getUnchecked (0)->relativePos = { 0.10f, 0.15f };
  51. draggers.getUnchecked (1)->relativePos = { 0.95f, 0.05f };
  52. draggers.getUnchecked (2)->relativePos = { 0.05f, 0.85f };
  53. setSize (800, 600);
  54. }
  55. void paint (Graphics& g) override
  56. {
  57. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground));
  58. g.setColour (Colours::white);
  59. g.setFont (15.0f);
  60. g.drawFittedText ("Drag the corner-points around to show how complex components can have affine-transforms applied...",
  61. getLocalBounds().removeFromBottom (40).reduced (10, 0), Justification::centred, 3);
  62. }
  63. void resized() override
  64. {
  65. for (auto* d : draggers)
  66. d->setCentrePosition (proportionOfWidth (d->relativePos.x),
  67. proportionOfHeight (d->relativePos.y));
  68. }
  69. void childBoundsChanged (Component* child) override
  70. {
  71. if (dynamic_cast<CornerDragger*> (child) != nullptr)
  72. updateTransform();
  73. }
  74. private:
  75. std::unique_ptr<Component> content;
  76. struct CornerDragger final : public Component
  77. {
  78. CornerDragger()
  79. {
  80. setSize (30, 30);
  81. setRepaintsOnMouseActivity (true);
  82. }
  83. void paint (Graphics& g) override
  84. {
  85. g.setColour (Colours::white.withAlpha (isMouseOverOrDragging() ? 0.9f : 0.5f));
  86. g.fillEllipse (getLocalBounds().reduced (3).toFloat());
  87. g.setColour (Colours::darkgreen);
  88. g.drawEllipse (getLocalBounds().reduced (3).toFloat(), 2.0f);
  89. }
  90. void resized() override
  91. {
  92. constrainer.setMinimumOnscreenAmounts (getHeight(), getWidth(), getHeight(), getWidth());
  93. }
  94. void moved() override
  95. {
  96. if (isMouseButtonDown())
  97. relativePos = getBounds().getCentre().toFloat() / Point<int> (getParentWidth(), getParentHeight()).toFloat();
  98. }
  99. void mouseDown (const MouseEvent& e) override { dragger.startDraggingComponent (this, e); }
  100. void mouseDrag (const MouseEvent& e) override { dragger.dragComponent (this, e, &constrainer); }
  101. Point<float> relativePos;
  102. private:
  103. ComponentBoundsConstrainer constrainer;
  104. ComponentDragger dragger;
  105. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CornerDragger)
  106. };
  107. OwnedArray<CornerDragger> draggers;
  108. Point<float> getDraggerPos (int index) const
  109. {
  110. return draggers.getUnchecked (index)->getBounds().getCentre().toFloat();
  111. }
  112. void updateTransform()
  113. {
  114. auto p0 = getDraggerPos (0);
  115. auto p1 = getDraggerPos (1);
  116. auto p2 = getDraggerPos (2);
  117. if (p0 != p1 && p1 != p2 && p0 != p2)
  118. content->setTransform (AffineTransform::fromTargetPoints (0, 0, p0.x, p0.y,
  119. (float) content->getWidth(), 0, p1.x, p1.y,
  120. 0, (float) content->getHeight(), p2.x, p2.y));
  121. }
  122. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComponentTransformsDemo)
  123. };