fadeInComponent


#1

i’ve hacked a bit the fadeOutProxyComponent to make a component fadein…

[code]//==============================================================================
class FadeInProxyComponent : public Component,
public Timer
{
public:
FadeInProxyComponent (Component* comp,
const int fadeLengthMs,
const int deltaXToMove,
const int deltaYToMove,
const float scaleFactorAtStart)
: lastTime (0),
alpha (0.0f),
scale (scaleFactorAtStart),
component(comp)
{
image = comp->createComponentSnapshot (Rectangle (0, 0, comp->getWidth(), comp->getHeight()));

    Rectangle initialBounds = comp->getBounds();
    initialBounds.translate(deltaXToMove,deltaYToMove);
    setBounds(initialBounds);

    comp->getParentComponent()->addAndMakeVisible (this);
    comp->toBehind (this);

    alphaChangePerMs = 1.0f / (float)fadeLengthMs;

    centreX = comp->getX() + comp->getWidth() * 0.5f + deltaXToMove;
    xChangePerMs = - deltaXToMove / (float)fadeLengthMs;

    centreY = comp->getY() + comp->getHeight() * 0.5f + deltaYToMove;
    yChangePerMs = - deltaYToMove / (float)fadeLengthMs;

    scaleChangePerMs = (1.0f - scaleFactorAtStart) / (float)fadeLengthMs;

    setInterceptsMouseClicks (false, false);

    // 30 fps is enough for a fade, but we need a higher rate if it's moving as well..
    startTimer (1000 / ((deltaXToMove == 0 && deltaYToMove == 0) ? 30 : 50));
}

~FadeInProxyComponent()
{
    delete image;
}

void paint (Graphics& g)
{
    g.setOpacity (alpha);

    g.drawImage (image,
                 0, 0, getWidth(), getHeight(),
                 0, 0, image->getWidth(), image->getHeight());
}

void timerCallback()
{
    const uint32 now = Time::getMillisecondCounter();

    if (lastTime == 0)
        lastTime = now;

    const int msPassed = now - lastTime;
    lastTime = now;

    alpha += alphaChangePerMs * msPassed;

    if (alpha < 1)
    {
        if (xChangePerMs != 0.0f || yChangePerMs != 0.0f || scaleChangePerMs != 0.0f)
        {
            centreX += xChangePerMs * msPassed;
            centreY += yChangePerMs * msPassed;
            scale += scaleChangePerMs * msPassed;

            const int w = roundFloatToInt (image->getWidth() * scale);
            const int h = roundFloatToInt (image->getHeight() * scale);

            setBounds (roundFloatToInt (centreX) - w / 2,
                       roundFloatToInt (centreY) - h / 2,
                       w, h);
        }

        repaint();
    }
    else
    {
        component->toFront(false);
        component->setVisible(true);
        delete this;
    }
}

juce_UseDebuggingNewOperator

private:
Component* component;
Image* image;
uint32 lastTime;
float alpha, alphaChangePerMs;
float centreX, xChangePerMs;
float centreY, yChangePerMs;
float scale, scaleChangePerMs;

FadeInProxyComponent (const FadeInProxyComponent&);
const FadeInProxyComponent& operator= (const FadeInProxyComponent&);

};

void Component::fadeInComponent (const int millisecondsToFade,
const int deltaXToMove,
const int deltaYToMove,
const float scaleFactorAtStart)
{
//xxx won’t work for comps without parents
if (!isShowing() && millisecondsToFade > 0)
new FadeInProxyComponent (this, millisecondsToFade,
deltaXToMove, deltaYToMove, scaleFactorAtStart);
}
[/code]

i hope it will be added in the next juce release :wink:


#2

seems working good for me…
why Julian haven’t been including it to JUCE so far?


#3

+1


#4

I’ve had the code lying around somewhere for ages, I know… Sorry, no particular reason why I’ve not done it yet!


#5

Up!


#6

Thing is, I don’t want to use this as the method for fading. With my new graphics stuff I’ll be able to give components an alpha level, so you can fade them without needing a proxy component. Need to get some other code in place before I can do that though.


#7

Ok, so what about adding the method to Component anyway, so that when you switch code it’s transparent for us, poor mortal ?


#8

I’d just like to think about it all properly, rather than hack that particular method in there…


#9

Has there been progress in this regard?


#10

Sorry, keeps getting pushed down my to-do-list!


#11