istereo2
diff libs/goatkit/boolanm.cc @ 6:3bccfc7d10fe
goatkit is drawing
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 23 Sep 2015 05:44:58 +0300 |
parents | |
children | 018f997dc646 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/goatkit/boolanm.cc Wed Sep 23 05:44:58 2015 +0300 1.3 @@ -0,0 +1,151 @@ 1.4 +/* 1.5 +GoatKit - a themable/animated widget toolkit for games 1.6 +Copyright (C) 2014 John Tsiombikas <nuclear@member.fsf.org> 1.7 + 1.8 +This program is free software: you can redistribute it and/or modify 1.9 +it under the terms of the GNU Lesser General Public License as published by 1.10 +the Free Software Foundation, either version 3 of the License, or 1.11 +(at your option) any later version. 1.12 + 1.13 +This program is distributed in the hope that it will be useful, 1.14 +but WITHOUT ANY WARRANTY; without even the implied warranty of 1.15 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1.16 +GNU Lesser General Public License for more details. 1.17 + 1.18 +You should have received a copy of the GNU Lesser General Public License 1.19 +along with this program. If not, see <http://www.gnu.org/licenses/>. 1.20 +*/ 1.21 +#include "boolanm.h" 1.22 + 1.23 +static long default_get_msec(); 1.24 + 1.25 +BoolAnim::BoolAnim(bool st) 1.26 +{ 1.27 + set(st); 1.28 + trans_start = 0; 1.29 + trans_dur = 500; 1.30 + get_msec = default_get_msec; 1.31 +} 1.32 + 1.33 +void BoolAnim::update(long tm) const 1.34 +{ 1.35 + if(trans_dir == 0.0) return; 1.36 + 1.37 + float dt = (tm - trans_start) / 1000.0; 1.38 + float t = dt / (trans_dur / 1000.0); 1.39 + 1.40 + if(trans_dir > 0.0) { 1.41 + value = t; 1.42 + } else { 1.43 + value = 1.0 - t; 1.44 + } 1.45 + 1.46 + if(value < 0.0) { 1.47 + value = 0.0; 1.48 + trans_dir = 0.0; 1.49 + } else if(value > 1.0) { 1.50 + value = 1.0; 1.51 + trans_dir = 0.0; 1.52 + } 1.53 +} 1.54 + 1.55 +void BoolAnim::set_transition_duration(long dur) 1.56 +{ 1.57 + trans_dur = dur; 1.58 +} 1.59 + 1.60 +void BoolAnim::set_time_callback(long (*time_func)()) 1.61 +{ 1.62 + get_msec = time_func; 1.63 +} 1.64 + 1.65 +void BoolAnim::set(bool st) 1.66 +{ 1.67 + value = st ? 1.0 : 0.0; 1.68 + trans_dir = 0.0; 1.69 +} 1.70 + 1.71 +void BoolAnim::change(bool st) 1.72 +{ 1.73 + change(st, get_msec()); 1.74 +} 1.75 + 1.76 +void BoolAnim::change(bool st, long tm) 1.77 +{ 1.78 + trans_dir = st ? 1.0 : -1.0; 1.79 + trans_start = tm; 1.80 +} 1.81 + 1.82 +bool BoolAnim::get_state() const 1.83 +{ 1.84 + return get_state(get_msec()); 1.85 +} 1.86 + 1.87 +bool BoolAnim::get_state(long tm) const 1.88 +{ 1.89 + update(tm); 1.90 + 1.91 + // if we're not in transition use the value (should be 0 or 1) 1.92 + if(trans_dir == 0.0) { 1.93 + return value > 0.5; 1.94 + } 1.95 + 1.96 + // if we're in transition base it on the direction of the transition 1.97 + return trans_dir > 0.0; 1.98 +} 1.99 + 1.100 +float BoolAnim::get_value() const 1.101 +{ 1.102 + return get_value(get_msec()); 1.103 +} 1.104 + 1.105 +float BoolAnim::get_value(long tm) const 1.106 +{ 1.107 + update(tm); 1.108 + return value; 1.109 +} 1.110 + 1.111 +float BoolAnim::get_dir() const 1.112 +{ 1.113 + return get_dir(get_msec()); 1.114 +} 1.115 + 1.116 +float BoolAnim::get_dir(long tm) const 1.117 +{ 1.118 + update(tm); 1.119 + return trans_dir; 1.120 +} 1.121 + 1.122 +BoolAnim::operator bool() const 1.123 +{ 1.124 + return get_state(); 1.125 +} 1.126 + 1.127 +BoolAnim::operator float() const 1.128 +{ 1.129 + return get_value(); 1.130 +} 1.131 + 1.132 +#ifdef WIN32 1.133 +#include <windows.h> 1.134 + 1.135 +static long default_get_msec() 1.136 +{ 1.137 + return GetTickCount(); 1.138 +} 1.139 +#else 1.140 +#include <sys/time.h> 1.141 + 1.142 +static long default_get_msec() 1.143 +{ 1.144 + static struct timeval tv0; 1.145 + struct timeval tv; 1.146 + 1.147 + gettimeofday(&tv, 0); 1.148 + if(tv0.tv_sec == 0 && tv0.tv_usec == 0) { 1.149 + tv0 = tv; 1.150 + return 0; 1.151 + } 1.152 + return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000; 1.153 +} 1.154 +#endif