Count down button

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
RdR
Competition winner
Posts: 273
Joined: Tue Mar 29, 2011 2:58 pm
Contact:

Count down button

Post by RdR »

A simple class to create a count down button.
It disables the button on press and counts down till 0 and enables the button again.

Image

Code: Select all

 
//Create button
IGUIButton * button = new CGUICountDownButton(5, Environment, this, 1, core::rect<s32>(x, y, w, h));
button->setText(L"Some text");
button->drop();
 
CGUICountDownButton.h

Code: Select all

/**
 * CGUICountDownButton.h
 *
 * Tank @ War Project
 * February 2011
 */
 
#ifndef CGUICOUNTDOWNBUTTON_H_
#define CGUICOUNTDOWNBUTTON_H_
 
#include <irrlicht.h>
#include "CGUIButton.h"
 
namespace irr {
namespace gui {
 
class CGUICountDownButton: public CGUIButton {
protected:
        irr::u32 timeDisabled;
        irr::u32 countDownTime;
        core::stringw originalText;
public:
        CGUICountDownButton(u32 countDownTime, IGUIEnvironment* environment, IGUIElement* parent, s32 id, core::rect<s32> rectangle, bool noclip = false);
        virtual ~CGUICountDownButton();
 
        //! called if an event happened.
        virtual bool OnEvent(const SEvent& event);
 
        void setText(const wchar_t* text);
 
        //! draws the element and its children
        virtual void draw();
};
 
}
}
 
#endif /* CGUICOUNTDOWNBUTTON_H_ */
 
CGUICountDownButton.cpp

Code: Select all

// Tank @ War Client
 
#include "CGUICountDownButton.h"
#include "CGUIButton.cpp"
#include <stdio.h>
#include <time.h>
 
namespace irr {
namespace gui {
 
CGUICountDownButton::CGUICountDownButton(u32 countDownTime, IGUIEnvironment* environment, IGUIElement* parent, s32 id, core::rect<s32> rectangle, bool noclip) :
                CGUIButton(environment, parent, id, rectangle, noclip) {
        this->countDownTime = countDownTime;
        setIsPushButton(true);
}
 
CGUICountDownButton::~CGUICountDownButton() {
}
 
bool CGUICountDownButton::OnEvent(const SEvent& event) {
        if (CGUIButton::OnEvent(event) && isPressed() && isEnabled()) {
                time_t now = time(NULL);
                timeDisabled = (irr::u32) now;
                setEnabled(false);
 
                return true;
        }
        return false;
}
 
void CGUICountDownButton::setText(const wchar_t* text) {
        originalText = text;
        IGUIElement::setText(text);
}
 
void CGUICountDownButton::draw() {
        time_t now = time(NULL);
        u32 timeDiff = now - timeDisabled;
        if (timeDiff < countDownTime && isPressed() && !isEnabled()) {
                core::stringw text = originalText;
                text += " (";
                text += countDownTime - timeDiff;
                text += ")";
                IGUIElement::setText(text.c_str());
        } else {
                IGUIElement::setText(originalText.c_str());
                setPressed(false);
                setEnabled(true);
        }
 
        CGUIButton::draw();
}
 
}
}
 
 
Post Reply