Super Simple ProgressWheel Component


#1

Add one of these to your window or another parent when your waiting for stuff. This is a super simple Mac looking progress wheel that makes use of Component::fadeOutComponent to get that cool look. It looks fantastic at about 30px by 30px.
Enjoy.

.h

[code]
#ifndef PROGRESS_WHEEL
#define PROGRESS_WHEEL

#include “juce.h”

enum ProgressWheelStyle
{
MacStyle,
CustomStyle
};

class ProgressWheel : public Component,
public Timer
{
public:
ProgressWheel(ProgressWheelStyle wheelStyle);
~ProgressWheel();

/** Overriden from Component.
*/
void paint(Graphics& g);

/** Overriden from Component.
*/
void resized();

/** Overriden from Component.
*/
void visibilityChanged();

/** Overriden from Timer
*/
void timerCallback();

const AffineTransform getTransform(int index) const;

void createPetal(int index);

private:

class ProgressPetal : public Component
{
public:
	ProgressPetal(const AffineTransform& trnsfrm, const Path& path)
	{
		transform = trnsfrm;
		p = path;
	}
	~ProgressPetal()
	{
		fadeOutComponent(500);
	}

	/** Overriden from Component.
	*/
	void paint(Graphics& g)
	{
		g.setColour(Colours::grey.withAlpha(0.8f));
		g.fillPath(p,transform);
	}
private:
	AffineTransform transform;
	Path p;
};

ProgressWheelStyle _wheelStyle;
float petalWidth, petalHeight, x, y;
Path _petalPath;
int petalIndex;

Array<float> petalPlacementList;

};

#endif // PROGRESS_WHEEL[/code]

.cpp

[code]
#include “ProgressWheel.h”

ProgressWheel::ProgressWheel(ProgressWheelStyle wheelStyle)
: petalIndex(0),
_wheelStyle(wheelStyle)
{
for(float i = 0.0f; i <= 1.000f; i += 0.0833f)
petalPlacementList.add(i);

setBufferedToImage(true);

}
ProgressWheel::~ProgressWheel()
{
deleteAllChildren();
}
void ProgressWheel::paint(Graphics& g)
{
petalWidth = (float)getWidth() / 11.0f;
petalHeight = (float)getHeight() / 3.8f;
x = ((float)getWidth() / 2) - (petalWidth / 2.0f);
y = (float)getHeight() - petalHeight;
const float cornerSize = (float)getHeight() / 40.0f;

_petalPath.clear();

if(_wheelStyle == MacStyle)
	_petalPath.addRoundedRectangle(x,y,petalWidth,petalHeight,cornerSize);
else
	_petalPath.addEllipse(x,y + petalHeight / 2,
	petalWidth + (0.02f * getWidth()),
	petalWidth + (0.02f * getWidth()));


g.setColour(Colours::grey.withAlpha(0.3f));

for(int i = 0; i < petalPlacementList.size() - 1; i++)
{
	g.fillPath(_petalPath,getTransform(i));
}

}
void ProgressWheel::resized()
{
}
void ProgressWheel::visibilityChanged()
{
if (isVisible())
startTimer(75);
else
stopTimer();
}
void ProgressWheel::timerCallback()
{
if(petalIndex >= petalPlacementList.size() - 1)
petalIndex = 1;
else
petalIndex++;

createPetal(petalIndex);

}
const AffineTransform ProgressWheel::getTransform(int index) const
{
return AffineTransform::rotation(float_Pi * 2.0f * petalPlacementList.getUnchecked(index),
x + petalWidth / 2.0f,
y - petalHeight / 1.1f);
}
void ProgressWheel::createPetal(int index)
{
ProgressPetal* progressPetal;
addAndMakeVisible(progressPetal = new ProgressPetal(getTransform(petalIndex),_petalPath));
progressPetal->setBounds(0,0,getWidth(),getHeight());

delete progressPetal;

}[/code]