Fix #8316: Make sort industries by production and transported with a cargo filter...
[openttd-github.git] / src / guitimer_func.h
blobf37bd5dbbc6e40c27b03a86a4f196c779dae8623
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /** @file guitimer_func.h GUI Timers. */
10 #ifndef GUITIMER_FUNC_H
11 #define GUITIMER_FUNC_H
13 class GUITimer
15 protected:
16 uint timer;
17 uint interval;
19 public:
20 GUITimer() : timer(0), interval(0) { }
21 explicit GUITimer(uint interval) : timer(0), interval(interval) { }
23 inline bool HasElapsed() const
25 return this->interval == 0;
28 inline void SetInterval(uint interval)
30 this->timer = 0;
31 this->interval = interval;
34 /**
35 * Count how many times the interval has elapsed.
36 * Use to ensure a specific amount of events happen within a timeframe, e.g. for animation.
37 * @param delta Time since last test.
38 * @return Number of times the interval has elapsed.
40 inline uint CountElapsed(uint delta)
42 if (this->interval == 0) return 0;
43 uint count = delta / this->interval;
44 if (this->timer + (delta % this->interval) >= this->interval) count++;
45 this->timer = (this->timer + delta) % this->interval;
46 return count;
49 /**
50 * Test if a timer has elapsed.
51 * Use to ensure an event happens only once within a timeframe, e.g. for window updates.
52 * @param delta Time since last test.
53 * @return True iff the timer has elapsed.
55 inline bool Elapsed(uint delta)
57 if (this->CountElapsed(delta) == 0) return false;
58 this->SetInterval(0);
59 return true;
63 #endif /* GUITIMER_FUNC_H */