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.

119 lines
3.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. #include "../../Application/jucer_Headers.h"
  19. #include "jucer_SlidingPanelComponent.h"
  20. //==============================================================================
  21. struct SlidingPanelComponent::DotButton final : public Button
  22. {
  23. DotButton (SlidingPanelComponent& sp, int pageIndex)
  24. : Button (String()), owner (sp), index (pageIndex) {}
  25. void paintButton (Graphics& g, bool /*isMouseOverButton*/, bool /*isButtonDown*/) override
  26. {
  27. g.setColour (findColour (defaultButtonBackgroundColourId));
  28. const auto r = getLocalBounds().reduced (getWidth() / 4).toFloat();
  29. if (index == owner.getCurrentTabIndex())
  30. g.fillEllipse (r);
  31. else
  32. g.drawEllipse (r, 1.0f);
  33. }
  34. void clicked() override
  35. {
  36. owner.goToTab (index);
  37. }
  38. using Button::clicked;
  39. SlidingPanelComponent& owner;
  40. int index;
  41. };
  42. //==============================================================================
  43. SlidingPanelComponent::SlidingPanelComponent()
  44. : currentIndex (0), dotSize (20)
  45. {
  46. addAndMakeVisible (pageHolder);
  47. }
  48. SlidingPanelComponent::~SlidingPanelComponent()
  49. {
  50. }
  51. SlidingPanelComponent::PageInfo::~PageInfo()
  52. {
  53. if (shouldDelete)
  54. content.deleteAndZero();
  55. }
  56. void SlidingPanelComponent::addTab (const String& tabName,
  57. Component* const contentComponent,
  58. const bool deleteComponentWhenNotNeeded,
  59. const int insertIndex)
  60. {
  61. PageInfo* page = new PageInfo();
  62. pages.insert (insertIndex, page);
  63. page->content = contentComponent;
  64. page->dotButton.reset (new DotButton (*this, pages.indexOf (page)));
  65. addAndMakeVisible (page->dotButton.get());
  66. page->name = tabName;
  67. page->shouldDelete = deleteComponentWhenNotNeeded;
  68. pageHolder.addAndMakeVisible (contentComponent);
  69. resized();
  70. }
  71. void SlidingPanelComponent::goToTab (int targetTabIndex)
  72. {
  73. currentIndex = targetTabIndex;
  74. Desktop::getInstance().getAnimator()
  75. .animateComponent (&pageHolder, pageHolder.getBounds().withX (-targetTabIndex * getWidth()),
  76. 1.0f, 600, false, 0.0, 0.0);
  77. repaint();
  78. }
  79. void SlidingPanelComponent::resized()
  80. {
  81. pageHolder.setBounds (-currentIndex * getWidth(), pageHolder.getPosition().y,
  82. getNumTabs() * getWidth(), getHeight());
  83. Rectangle<int> content (getLocalBounds());
  84. Rectangle<int> dotHolder = content.removeFromBottom (20 + dotSize)
  85. .reduced ((content.getWidth() - dotSize * getNumTabs()) / 2, 10);
  86. for (int i = 0; i < getNumTabs(); ++i)
  87. pages.getUnchecked (i)->dotButton->setBounds (dotHolder.removeFromLeft (dotSize));
  88. for (int i = pages.size(); --i >= 0;)
  89. if (Component* c = pages.getUnchecked (i)->content)
  90. c->setBounds (content.translated (i * content.getWidth(), 0));
  91. }