
#include <WFX/kernel/Timer.h>

#include <WFX/kernel/TimeTask.h>

using namespace WFX;
using namespace WFX::kernel;

Timer::Timer(TimerMode mode, ULong time, TimerListener* l, Int id) {
    Id = id;
    Mode = mode;
    Interval = time;
    Listener = l;
}

void Timer::start() {
    TimeTask* t = TimeTask::getInstancePtr();
    t->addTimer(this);
    Start = t->ThisFrame; 
}

void Timer::stop() {
    TimeTask::getInstance().removeTimer(this);
}

void Timer::update() {
    TimeTask* t = TimeTask::getInstancePtr();
    if ((t->ThisFrame - Start) >= Interval) {
        Start = t->ThisFrame;
        Listener->timerEvent(Id);
        if (Mode == TM_ONCE) {
            stop();
        }
    }
}


