Hey Guys
I put together a pulsating button class that takes a TextButton and gives it a “pulsating light” effect. I’m using it as a subtle way of getting the user’s attention. I thought you all might find it useful so here’s the code. And if anyone has any ideas on how to make it better I’m all ears!
Pulsator.h
class Pulsator;
class PulsatingButton : public TextButton,
public AsyncUpdater
{
public:
PulsatingButton();
~PulsatingButton();
void handleAsyncUpdate();
void pulsate(bool doPulsate);
bool isPulsating();
private:
int green;
bool up, reset;
Pulsator* pulsator;
};
class Pulsator : public Thread
{
public:
Pulsator();
~Pulsator();
void setTarget(PulsatingButton* button_);
void run();
private:
PulsatingButton* button;
};
Pulsator.cpp
[code]PulsatingButton::PulsatingButton()
: green (187),
up (true)
{
pulsator = new Pulsator();
pulsator->setTarget(this);
}
PulsatingButton::~PulsatingButton()
{
pulsator->threadShouldExit();
deleteAndZero(pulsator);
}
void PulsatingButton::handleAsyncUpdate()
{
if (!reset)
{
if (up)
{
green += 10;
if (green > 247)
{
green -= 20;
up = false;
}
}
else
{
green -= 10;
if (green < 187)
{
green += 20;
up = true;
}
}
setColour(TextButton::buttonColourId, Colour(187, green, 255));
}
else
{
setColour(TextButton::buttonColourId, Colour(187, 187, 255));
}
}
void PulsatingButton::pulsate(bool doPulsate)
{
if (doPulsate) {
reset = false;
pulsator->startThread();
}
else
{
reset = true;
pulsator->stopThread(100);
}
}
bool PulsatingButton::isPulsating()
{
return pulsator->isThreadRunning();
}
Pulsator::Pulsator()
: Thread (“Pulsator”)
{}
Pulsator::~Pulsator()
{
stopThread(100);
waitForThreadToExit(100);
}
void Pulsator::setTarget(PulsatingButton* button_)
{
button = button_;
}
void Pulsator::run()
{
while (!threadShouldExit())
{
wait(150);
button->triggerAsyncUpdate();
}
}
[/code]
Just create a PulsatingButton object the same way you would a TextButton, and to start and stop the pulsating:
myPulsatingButton->pulsate(true); // to start pulsating
myPulsatingButton->pulsate(false); // to stop pulsating
