From ac7271c10badd4ed85d420a3d74db2b4f1adce5a Mon Sep 17 00:00:00 2001 From: jules Date: Sat, 16 Nov 2013 22:11:48 +0000 Subject: [PATCH] Added some methods to TabbedButtonBar for animating tab movements. --- .../layout/juce_TabbedButtonBar.cpp | 35 ++++++++++++++++--- .../layout/juce_TabbedButtonBar.h | 6 +++- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/modules/juce_gui_basics/layout/juce_TabbedButtonBar.cpp b/modules/juce_gui_basics/layout/juce_TabbedButtonBar.cpp index 35cd9d5543..67f46bc9a4 100644 --- a/modules/juce_gui_basics/layout/juce_TabbedButtonBar.cpp +++ b/modules/juce_gui_basics/layout/juce_TabbedButtonBar.cpp @@ -299,12 +299,12 @@ void TabbedButtonBar::removeTab (const int tabIndex) resized(); } -void TabbedButtonBar::moveTab (const int currentIndex, const int newIndex) +void TabbedButtonBar::moveTab (const int currentIndex, const int newIndex, const bool animate) { TabInfo* const currentTab = tabs [currentTabIndex]; tabs.move (currentIndex, newIndex); currentTabIndex = tabs.indexOf (currentTab); - resized(); + updateTabPositions (animate); } int TabbedButtonBar::getNumTabs() const @@ -369,6 +369,16 @@ int TabbedButtonBar::indexOfTabButton (const TabBarButton* button) const return -1; } +Rectangle TabbedButtonBar::getTargetBounds (TabBarButton* button) const +{ + if (button == nullptr || indexOfTabButton (button) == -1) + return Rectangle(); + + ComponentAnimator& animator = Desktop::getInstance().getAnimator(); + + return animator.isAnimating (button) ? animator.getComponentDestination (button) : button->getBounds(); +} + void TabbedButtonBar::lookAndFeelChanged() { extraTabsButton = nullptr; @@ -381,6 +391,12 @@ void TabbedButtonBar::paint (Graphics& g) } void TabbedButtonBar::resized() +{ + updateTabPositions (false); +} + +//============================================================================== +void TabbedButtonBar::updateTabPositions (bool animate) { LookAndFeel& lf = getLookAndFeel(); @@ -462,6 +478,7 @@ void TabbedButtonBar::resized() int pos = 0; TabBarButton* frontTab = nullptr; + ComponentAnimator& animator = Desktop::getInstance().getAnimator(); for (int i = 0; i < tabs.size(); ++i) { @@ -471,10 +488,18 @@ void TabbedButtonBar::resized() if (i < numVisibleButtons) { - if (isVertical()) - tb->setBounds (0, pos, getWidth(), bestLength); + const Rectangle newBounds (isVertical() ? Rectangle (0, pos, getWidth(), bestLength) + : Rectangle (pos, 0, bestLength, getHeight())); + + if (animate) + { + animator.animateComponent (tb, newBounds, 1.0f, 200, false, 3.0, 0.0); + } else - tb->setBounds (pos, 0, bestLength, getHeight()); + { + animator.cancelAnimation (tb, false); + tb->setBounds (newBounds); + } tb->toBack(); diff --git a/modules/juce_gui_basics/layout/juce_TabbedButtonBar.h b/modules/juce_gui_basics/layout/juce_TabbedButtonBar.h index 042b02ecfb..a9b2561071 100644 --- a/modules/juce_gui_basics/layout/juce_TabbedButtonBar.h +++ b/modules/juce_gui_basics/layout/juce_TabbedButtonBar.h @@ -217,7 +217,7 @@ public: /** Moves a tab to a new index in the list. Pass -1 as the index to move it to the end of the list. */ - void moveTab (int currentIndex, int newIndex); + void moveTab (int currentIndex, int newIndex, bool animate = false); /** Returns the number of tabs in the bar. */ int getNumTabs() const; @@ -254,6 +254,9 @@ public: /** Returns the index of a TabBarButton if it belongs to this bar. */ int indexOfTabButton (const TabBarButton* button) const; + /** Returns the final bounds of this button if it is currently being animated. */ + Rectangle getTargetBounds (TabBarButton* button) const; + //============================================================================== /** Callback method to indicate the selected tab has been changed. @see setCurrentTabIndex @@ -357,6 +360,7 @@ private: void showExtraItemsMenu(); static void extraItemsMenuCallback (int, TabbedButtonBar*); + void updateTabPositions (bool animate); JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TabbedButtonBar) };