2 GoatKit - a themable/animated widget toolkit for games
3 Copyright (C) 2014-2015 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 static long default_get_msec();
22 BoolAnim::BoolAnim(bool st)
27 get_msec = default_get_msec;
30 void BoolAnim::update(long tm) const
32 if(trans_dir == 0.0) return;
34 float dt = (tm - trans_start) / 1000.0;
35 float t = dt / (trans_dur / 1000.0);
46 } else if(value > 1.0) {
52 void BoolAnim::set_transition_duration(long dur)
57 long BoolAnim::get_transition_duration() const
62 void BoolAnim::set_time_callback(long (*time_func)())
67 void BoolAnim::set(bool st)
69 value = st ? 1.0 : 0.0;
73 void BoolAnim::change(bool st)
75 change(st, get_msec());
78 void BoolAnim::change(bool st, long tm)
80 trans_dir = st ? 1.0 : -1.0;
84 bool BoolAnim::get_state() const
86 return get_state(get_msec());
89 bool BoolAnim::get_state(long tm) const
93 // if we're not in transition use the value (should be 0 or 1)
94 if(trans_dir == 0.0) {
98 // if we're in transition base it on the direction of the transition
99 return trans_dir > 0.0;
102 float BoolAnim::get_value() const
104 return get_value(get_msec());
107 float BoolAnim::get_value(long tm) const
113 float BoolAnim::get_dir() const
115 return get_dir(get_msec());
118 float BoolAnim::get_dir(long tm) const
124 BoolAnim::operator bool() const
129 BoolAnim::operator float() const
137 static long default_get_msec()
139 return GetTickCount();
142 #include <sys/time.h>
144 static long default_get_msec()
146 static struct timeval tv0;
149 gettimeofday(&tv, 0);
150 if(tv0.tv_sec == 0 && tv0.tv_usec == 0) {
154 return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000;