Browse Source

Made the dots work in the sliding page comp

tags/2021-05-28
jules 11 years ago
parent
commit
19cd4103be
2 changed files with 65 additions and 58 deletions
  1. +47
    -50
      extras/Introjucer/Source/Utility/jucer_SlidingPanelComponent.cpp
  2. +18
    -8
      extras/Introjucer/Source/Utility/jucer_SlidingPanelComponent.h

+ 47
- 50
extras/Introjucer/Source/Utility/jucer_SlidingPanelComponent.cpp View File

@@ -25,34 +25,47 @@
#include "jucer_SlidingPanelComponent.h"
//==============================================================================
// TEMPORARY COPY OF TAB HELPERS FOR SLIDER PROTOTYPE, REMOVE BEFORE RELEASE ///
//==============================================================================
namespace TabbedComponentHelpers
struct SlidingPanelComponent::DotButton : public Button
{
const Identifier deleteComponentId ("deleteByTabComp_");
DotButton (SlidingPanelComponent& sp, int pageIndex)
: Button (String()), owner (sp), index (pageIndex) {}
static void deleteIfNecessary (Component* const comp)
void paintButton (Graphics& g, bool /*isMouseOverButton*/, bool /*isButtonDown*/) override
{
if (comp != nullptr && (bool) comp->getProperties() [deleteComponentId])
delete comp;
g.setColour (Colours::white);
const Rectangle<float> r (getLocalBounds().reduced (getWidth() / 4).toFloat());
if (index == owner.getCurrentTabIndex())
g.fillEllipse (r);
else
g.drawEllipse (r, 1.0f);
}
void clicked() override
{
owner.goToTab (index);
}
}
SlidingPanelComponent& owner;
int index;
};
//==============================================================================
SlidingPanelComponent::SlidingPanelComponent()
: currentTabIndex (0), dotSize (20)
: currentIndex (0), dotSize (20)
{
addAndMakeVisible (slide);
addAndMakeVisible (pageHolder);
}
SlidingPanelComponent::~SlidingPanelComponent()
{
for (int i = contentComponents.size(); --i >= 0;)
if (Component* c = contentComponents.getReference(i))
TabbedComponentHelpers::deleteIfNecessary (c);
}
SlidingPanelComponent::PageInfo::~PageInfo()
{
if (shouldDelete)
content.deleteAndZero();
}
void SlidingPanelComponent::addTab (const String& tabName,
@@ -60,60 +73,44 @@ void SlidingPanelComponent::addTab (const String& tabName,
const bool deleteComponentWhenNotNeeded,
const int insertIndex)
{
contentComponents.insert (insertIndex, WeakReference<Component> (contentComponent));
tabNames.insert (insertIndex, tabName);
if (deleteComponentWhenNotNeeded && contentComponent != nullptr)
contentComponent->getProperties().set (TabbedComponentHelpers::deleteComponentId, true);
slide.addAndMakeVisible (contentComponent);
PageInfo* page = new PageInfo();
pages.insert (insertIndex, page);
page->content = contentComponent;
addAndMakeVisible (page->dotButton = new DotButton (*this, pages.indexOf (page)));
page->name = tabName;
page->shouldDelete = deleteComponentWhenNotNeeded;
pageHolder.addAndMakeVisible (contentComponent);
resized();
}
void SlidingPanelComponent::goToTab (int targetTabIndex)
{
const int xTranslation = (currentTabIndex - targetTabIndex) * getWidth();
const int xTranslation = (currentIndex - targetTabIndex) * getWidth();
currentTabIndex = targetTabIndex;
currentIndex = targetTabIndex;
Desktop::getInstance().getAnimator()
.animateComponent (&slide, slide.getBounds().translated (xTranslation, 0),
.animateComponent (&pageHolder, pageHolder.getBounds().translated (xTranslation, 0),
1.0f, 600, false, 0.0, 0.0);
repaint();
}
void SlidingPanelComponent::paint (Graphics& g)
{
Rectangle<int> dotHolder = getLocalBounds();
dotHolder.reduce ((getWidth() - dotSize * getNumTabs()) / 2, 20);
dotHolder = dotHolder.removeFromBottom (dotSize);
g.setColour (Colours::white);
for (int i = 0; i < getNumTabs(); ++i)
{
const Rectangle<float> r (dotHolder.removeFromLeft (dotSize).reduced (5, 5).toFloat());
if (i == currentTabIndex)
g.fillEllipse (r);
else
g.drawEllipse (r, 1.0f);
}
}
void SlidingPanelComponent::resized()
{
slide.setBounds (-currentTabIndex * getWidth(), slide.getPosition().y,
getNumTabs() * getWidth(), getHeight());
pageHolder.setBounds (-currentIndex * getWidth(), pageHolder.getPosition().y,
getNumTabs() * getWidth(), getHeight());
Rectangle<int> content (getLocalBounds());
content.removeFromBottom (20 + 2 * dotSize);
Rectangle<int> dotHolder = content.removeFromBottom (20 + dotSize)
.reduced ((content.getWidth() - dotSize * getNumTabs()) / 2, 10);
for (int i = 0; i < getNumTabs(); ++i)
pages.getUnchecked(i)->dotButton->setBounds (dotHolder.removeFromLeft (dotSize));
for (int i = contentComponents.size(); --i >= 0;)
if (Component* c = contentComponents.getReference(i))
for (int i = pages.size(); --i >= 0;)
if (Component* c = pages.getUnchecked(i)->content)
c->setBounds (content.translated (i * content.getWidth(), 0));
}

+ 18
- 8
extras/Introjucer/Source/Utility/jucer_SlidingPanelComponent.h View File

@@ -46,10 +46,10 @@ public:
void removeTab (int tabIndex);
/** Gets index of current tab. */
int getCurrentTabIndex() const noexcept { return currentTabIndex; }
int getCurrentTabIndex() const noexcept { return currentIndex; }
/** Returns the number of tabs. */
int getNumTabs() const noexcept { return contentComponents.size(); }
int getNumTabs() const noexcept { return pages.size(); }
/** Animates the window to the desired tab. */
void goToTab (int targetTabIndex);
@@ -57,16 +57,26 @@ public:
//==============================================================================
/** @internal */
void paint (Graphics&) override;
/** @internal */
void resized() override;
private:
Array<WeakReference<Component> > contentComponents;
StringArray tabNames;
struct DotButton;
friend struct DotButton;
struct PageInfo
{
~PageInfo();
Component::SafePointer<Component> content;
ScopedPointer<DotButton> dotButton;
String name;
bool shouldDelete;
};
OwnedArray<PageInfo> pages;
Component slide;
int currentTabIndex, dotSize;
Component pageHolder;
int currentIndex, dotSize;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SlidingPanelComponent);
};


Loading…
Cancel
Save