gameui
diff src/boolanm.cc @ 2:e5b1525084f7
boolanim
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 20 Mar 2014 07:03:58 +0200 |
parents | |
children | f1014234dece |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/boolanm.cc Thu Mar 20 07:03:58 2014 +0200 1.3 @@ -0,0 +1,129 @@ 1.4 +#include "boolanm.h" 1.5 + 1.6 +static long default_get_msec(); 1.7 + 1.8 +BoolAnim::BoolAnim(bool st) 1.9 +{ 1.10 + value = st ? 1.0 : 0.0; 1.11 + trans_dir = 0.0; 1.12 + trans_start = 0; 1.13 + trans_dur = 1000; 1.14 + get_msec = default_get_msec; 1.15 +} 1.16 + 1.17 +void BoolAnim::update(long tm) const 1.18 +{ 1.19 + if(trans_dir == 0.0) return; 1.20 + 1.21 + float dt = (tm - trans_start) / 1000.0; 1.22 + float t = dt / (trans_dur / 1000.0); 1.23 + 1.24 + if(trans_dir > 0.0) { 1.25 + value = t; 1.26 + } else { 1.27 + value = 1.0 - t; 1.28 + } 1.29 + 1.30 + if(value < 0.0) { 1.31 + value = 0.0; 1.32 + trans_dir = 0.0; 1.33 + } else if(value > 1.0) { 1.34 + value = 1.0; 1.35 + trans_dir = 0.0; 1.36 + } 1.37 +} 1.38 + 1.39 +void BoolAnim::set_transition_duration(long dur) 1.40 +{ 1.41 + trans_dur = dur; 1.42 +} 1.43 + 1.44 +void BoolAnim::set_time_callback(long (*time_func)()) 1.45 +{ 1.46 + get_msec = time_func; 1.47 +} 1.48 + 1.49 +void BoolAnim::change(bool st) 1.50 +{ 1.51 + change(st, get_msec()); 1.52 +} 1.53 + 1.54 +void BoolAnim::change(bool st, long tm) 1.55 +{ 1.56 + trans_dir = st ? 1.0 : -1.0; 1.57 + trans_start = tm; 1.58 +} 1.59 + 1.60 +bool BoolAnim::get_state() const 1.61 +{ 1.62 + return get_state(get_msec()); 1.63 +} 1.64 + 1.65 +bool BoolAnim::get_state(long tm) const 1.66 +{ 1.67 + update(tm); 1.68 + 1.69 + // if we're not in transition use the value (should be 0 or 1) 1.70 + if(trans_dir == 0.0) { 1.71 + return value > 0.5; 1.72 + } 1.73 + 1.74 + // if we're in transition base it on the direction of the transition 1.75 + return trans_dir > 0.0; 1.76 +} 1.77 + 1.78 +float BoolAnim::get_value() const 1.79 +{ 1.80 + return get_value(get_msec()); 1.81 +} 1.82 + 1.83 +float BoolAnim::get_value(long tm) const 1.84 +{ 1.85 + update(tm); 1.86 + return value; 1.87 +} 1.88 + 1.89 +float BoolAnim::get_dir() const 1.90 +{ 1.91 + return get_dir(get_msec()); 1.92 +} 1.93 + 1.94 +float BoolAnim::get_dir(long tm) const 1.95 +{ 1.96 + update(tm); 1.97 + return trans_dir; 1.98 +} 1.99 + 1.100 +BoolAnim::operator bool() const 1.101 +{ 1.102 + return get_state(); 1.103 +} 1.104 + 1.105 +BoolAnim::operator float() const 1.106 +{ 1.107 + return get_value(); 1.108 +} 1.109 + 1.110 +#ifdef WIN32 1.111 +#include <windows.h> 1.112 + 1.113 +static long default_get_msec() 1.114 +{ 1.115 + return GetTickCount(); 1.116 +} 1.117 +#else 1.118 +#include <sys/time.h> 1.119 + 1.120 +static long default_get_msec() 1.121 +{ 1.122 + static struct timeval tv0; 1.123 + struct timeval tv; 1.124 + 1.125 + gettimeofday(&tv, 0); 1.126 + if(tv0.tv_sec == 0 && tv0.tv_usec == 0) { 1.127 + tv0 = tv; 1.128 + return 0; 1.129 + } 1.130 + return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000; 1.131 +} 1.132 +#endif