istereo2
annotate libs/goatkit/boolanm.cc @ 9:64e15874f3bd
foo
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 26 Sep 2015 02:56:07 +0300 |
parents | |
children | 018f997dc646 |
rev | line source |
---|---|
nuclear@6 | 1 /* |
nuclear@6 | 2 GoatKit - a themable/animated widget toolkit for games |
nuclear@6 | 3 Copyright (C) 2014 John Tsiombikas <nuclear@member.fsf.org> |
nuclear@6 | 4 |
nuclear@6 | 5 This program is free software: you can redistribute it and/or modify |
nuclear@6 | 6 it under the terms of the GNU Lesser General Public License as published by |
nuclear@6 | 7 the Free Software Foundation, either version 3 of the License, or |
nuclear@6 | 8 (at your option) any later version. |
nuclear@6 | 9 |
nuclear@6 | 10 This program is distributed in the hope that it will be useful, |
nuclear@6 | 11 but WITHOUT ANY WARRANTY; without even the implied warranty of |
nuclear@6 | 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
nuclear@6 | 13 GNU Lesser General Public License for more details. |
nuclear@6 | 14 |
nuclear@6 | 15 You should have received a copy of the GNU Lesser General Public License |
nuclear@6 | 16 along with this program. If not, see <http://www.gnu.org/licenses/>. |
nuclear@6 | 17 */ |
nuclear@6 | 18 #include "boolanm.h" |
nuclear@6 | 19 |
nuclear@6 | 20 static long default_get_msec(); |
nuclear@6 | 21 |
nuclear@6 | 22 BoolAnim::BoolAnim(bool st) |
nuclear@6 | 23 { |
nuclear@6 | 24 set(st); |
nuclear@6 | 25 trans_start = 0; |
nuclear@6 | 26 trans_dur = 500; |
nuclear@6 | 27 get_msec = default_get_msec; |
nuclear@6 | 28 } |
nuclear@6 | 29 |
nuclear@6 | 30 void BoolAnim::update(long tm) const |
nuclear@6 | 31 { |
nuclear@6 | 32 if(trans_dir == 0.0) return; |
nuclear@6 | 33 |
nuclear@6 | 34 float dt = (tm - trans_start) / 1000.0; |
nuclear@6 | 35 float t = dt / (trans_dur / 1000.0); |
nuclear@6 | 36 |
nuclear@6 | 37 if(trans_dir > 0.0) { |
nuclear@6 | 38 value = t; |
nuclear@6 | 39 } else { |
nuclear@6 | 40 value = 1.0 - t; |
nuclear@6 | 41 } |
nuclear@6 | 42 |
nuclear@6 | 43 if(value < 0.0) { |
nuclear@6 | 44 value = 0.0; |
nuclear@6 | 45 trans_dir = 0.0; |
nuclear@6 | 46 } else if(value > 1.0) { |
nuclear@6 | 47 value = 1.0; |
nuclear@6 | 48 trans_dir = 0.0; |
nuclear@6 | 49 } |
nuclear@6 | 50 } |
nuclear@6 | 51 |
nuclear@6 | 52 void BoolAnim::set_transition_duration(long dur) |
nuclear@6 | 53 { |
nuclear@6 | 54 trans_dur = dur; |
nuclear@6 | 55 } |
nuclear@6 | 56 |
nuclear@6 | 57 void BoolAnim::set_time_callback(long (*time_func)()) |
nuclear@6 | 58 { |
nuclear@6 | 59 get_msec = time_func; |
nuclear@6 | 60 } |
nuclear@6 | 61 |
nuclear@6 | 62 void BoolAnim::set(bool st) |
nuclear@6 | 63 { |
nuclear@6 | 64 value = st ? 1.0 : 0.0; |
nuclear@6 | 65 trans_dir = 0.0; |
nuclear@6 | 66 } |
nuclear@6 | 67 |
nuclear@6 | 68 void BoolAnim::change(bool st) |
nuclear@6 | 69 { |
nuclear@6 | 70 change(st, get_msec()); |
nuclear@6 | 71 } |
nuclear@6 | 72 |
nuclear@6 | 73 void BoolAnim::change(bool st, long tm) |
nuclear@6 | 74 { |
nuclear@6 | 75 trans_dir = st ? 1.0 : -1.0; |
nuclear@6 | 76 trans_start = tm; |
nuclear@6 | 77 } |
nuclear@6 | 78 |
nuclear@6 | 79 bool BoolAnim::get_state() const |
nuclear@6 | 80 { |
nuclear@6 | 81 return get_state(get_msec()); |
nuclear@6 | 82 } |
nuclear@6 | 83 |
nuclear@6 | 84 bool BoolAnim::get_state(long tm) const |
nuclear@6 | 85 { |
nuclear@6 | 86 update(tm); |
nuclear@6 | 87 |
nuclear@6 | 88 // if we're not in transition use the value (should be 0 or 1) |
nuclear@6 | 89 if(trans_dir == 0.0) { |
nuclear@6 | 90 return value > 0.5; |
nuclear@6 | 91 } |
nuclear@6 | 92 |
nuclear@6 | 93 // if we're in transition base it on the direction of the transition |
nuclear@6 | 94 return trans_dir > 0.0; |
nuclear@6 | 95 } |
nuclear@6 | 96 |
nuclear@6 | 97 float BoolAnim::get_value() const |
nuclear@6 | 98 { |
nuclear@6 | 99 return get_value(get_msec()); |
nuclear@6 | 100 } |
nuclear@6 | 101 |
nuclear@6 | 102 float BoolAnim::get_value(long tm) const |
nuclear@6 | 103 { |
nuclear@6 | 104 update(tm); |
nuclear@6 | 105 return value; |
nuclear@6 | 106 } |
nuclear@6 | 107 |
nuclear@6 | 108 float BoolAnim::get_dir() const |
nuclear@6 | 109 { |
nuclear@6 | 110 return get_dir(get_msec()); |
nuclear@6 | 111 } |
nuclear@6 | 112 |
nuclear@6 | 113 float BoolAnim::get_dir(long tm) const |
nuclear@6 | 114 { |
nuclear@6 | 115 update(tm); |
nuclear@6 | 116 return trans_dir; |
nuclear@6 | 117 } |
nuclear@6 | 118 |
nuclear@6 | 119 BoolAnim::operator bool() const |
nuclear@6 | 120 { |
nuclear@6 | 121 return get_state(); |
nuclear@6 | 122 } |
nuclear@6 | 123 |
nuclear@6 | 124 BoolAnim::operator float() const |
nuclear@6 | 125 { |
nuclear@6 | 126 return get_value(); |
nuclear@6 | 127 } |
nuclear@6 | 128 |
nuclear@6 | 129 #ifdef WIN32 |
nuclear@6 | 130 #include <windows.h> |
nuclear@6 | 131 |
nuclear@6 | 132 static long default_get_msec() |
nuclear@6 | 133 { |
nuclear@6 | 134 return GetTickCount(); |
nuclear@6 | 135 } |
nuclear@6 | 136 #else |
nuclear@6 | 137 #include <sys/time.h> |
nuclear@6 | 138 |
nuclear@6 | 139 static long default_get_msec() |
nuclear@6 | 140 { |
nuclear@6 | 141 static struct timeval tv0; |
nuclear@6 | 142 struct timeval tv; |
nuclear@6 | 143 |
nuclear@6 | 144 gettimeofday(&tv, 0); |
nuclear@6 | 145 if(tv0.tv_sec == 0 && tv0.tv_usec == 0) { |
nuclear@6 | 146 tv0 = tv; |
nuclear@6 | 147 return 0; |
nuclear@6 | 148 } |
nuclear@6 | 149 return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000; |
nuclear@6 | 150 } |
nuclear@6 | 151 #endif |