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.

112 lines
3.6KB

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