]> git.localhorst.tv Git - l2e.git/commitdiff
added repetition awareness in timers
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 12 Aug 2012 14:06:23 +0000 (16:06 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Sun, 12 Aug 2012 14:06:23 +0000 (16:06 +0200)
src/app/Timer.h

index 70b49b051c124b9d0dd1105ca30eb9b54a9e9e17..750f3ad40b4cc545283688cdeb2831aed2e20541 100644 (file)
@@ -17,13 +17,14 @@ namespace app {
 template<class Time>
 struct TimerData {
 
-       TimerData() : time(0), target(0), refCount(0), justHit(false) { }
-       TimerData(Time target) : time(0), target(target), refCount(0), justHit(false) { }
+       TimerData() : time(0), target(0), refCount(0), justHit(false), repeat(false) { }
+       TimerData(Time target, bool repeat) : time(0), target(target), refCount(0), justHit(false), repeat(repeat) { }
 
        Time time;
        Time target;
        int refCount;
        bool justHit;
+       bool repeat;
 
 };
 
@@ -47,11 +48,11 @@ public:
        }
 
 public:
-       bool Running() const {
-               return data;
-       }
        bool Finished() const {
-               return data ? data->time >= data->target : false;
+               return data && data->target != Time() && !data->repeat && data->time >= data->target;
+       }
+       bool Running() const {
+               return data && !Finished();
        }
        Time Elapsed() const {
                return data ? data->time : Time();
@@ -60,7 +61,7 @@ public:
                return data ? (data->target - data->time) : Time();
        }
        int Iteration() const {
-               return (data && data->target > 0) ? std::floor(data->time / data->target) : 0;
+               return (data && data->target > Time()) ? std::floor(data->time / data->target) : Time();
        }
        bool JustHit() const {
                return data && data->justHit;
@@ -73,14 +74,14 @@ public:
                }
        }
        void Reset() {
-               if (data) data->time = 0;
+               if (data) data->time = Time();
        }
        void Restart() {
                if (data) {
-                       if (data->target > 0 && data->justHit) {
+                       if (data->target > Time() && data->justHit) {
                                data->time -= data->target;
                        } else {
-                               data->time = 0;
+                               data->time = Time();
                        }
                }
        }
@@ -102,7 +103,7 @@ public:
                for (typename std::list<TimerData<Time> >::iterator i(data.begin()), end(data.end()); i != end;) {
                        if (i->target > 0) {
                                Time intervalTime(i->time);
-                               while (intervalTime > i->target) intervalTime -= i->target;
+                               if (i->repeat) while (intervalTime > i->target) intervalTime -= i->target;
                                i->justHit = intervalTime < i->target && intervalTime + delta >= i->target;
                        }
                        i->time += delta;
@@ -118,11 +119,11 @@ public:
                return Timer<Time>(&data.back());
        }
        Timer<Time> StartCountdown(Time duration) {
-               data.push_back(TimerData<Time>(duration));
+               data.push_back(TimerData<Time>(duration, false));
                return Timer<Time>(&data.back());
        }
        Timer<Time> StartInterval(Time duration) {
-               data.push_back(TimerData<Time>(duration));
+               data.push_back(TimerData<Time>(duration, true));
                return Timer<Time>(&data.back());
        }