istereo2
annotate libs/goatkit/boolanm.cc @ 20:2b85d05df3f2
animation controls for easier screenshot grabbing
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 02 Oct 2015 04:54:55 +0300 |
parents | 3bccfc7d10fe |
children |
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@14 | 57 long BoolAnim::get_transition_duration() const |
nuclear@14 | 58 { |
nuclear@14 | 59 return trans_dur; |
nuclear@14 | 60 } |
nuclear@14 | 61 |
nuclear@6 | 62 void BoolAnim::set_time_callback(long (*time_func)()) |
nuclear@6 | 63 { |
nuclear@6 | 64 get_msec = time_func; |
nuclear@6 | 65 } |
nuclear@6 | 66 |
nuclear@6 | 67 void BoolAnim::set(bool st) |
nuclear@6 | 68 { |
nuclear@6 | 69 value = st ? 1.0 : 0.0; |
nuclear@6 | 70 trans_dir = 0.0; |
nuclear@6 | 71 } |
nuclear@6 | 72 |
nuclear@6 | 73 void BoolAnim::change(bool st) |
nuclear@6 | 74 { |
nuclear@6 | 75 change(st, get_msec()); |
nuclear@6 | 76 } |
nuclear@6 | 77 |
nuclear@6 | 78 void BoolAnim::change(bool st, long tm) |
nuclear@6 | 79 { |
nuclear@6 | 80 trans_dir = st ? 1.0 : -1.0; |
nuclear@6 | 81 trans_start = tm; |
nuclear@6 | 82 } |
nuclear@6 | 83 |
nuclear@6 | 84 bool BoolAnim::get_state() const |
nuclear@6 | 85 { |
nuclear@6 | 86 return get_state(get_msec()); |
nuclear@6 | 87 } |
nuclear@6 | 88 |
nuclear@6 | 89 bool BoolAnim::get_state(long tm) const |
nuclear@6 | 90 { |
nuclear@6 | 91 update(tm); |
nuclear@6 | 92 |
nuclear@6 | 93 // if we're not in transition use the value (should be 0 or 1) |
nuclear@6 | 94 if(trans_dir == 0.0) { |
nuclear@6 | 95 return value > 0.5; |
nuclear@6 | 96 } |
nuclear@6 | 97 |
nuclear@6 | 98 // if we're in transition base it on the direction of the transition |
nuclear@6 | 99 return trans_dir > 0.0; |
nuclear@6 | 100 } |
nuclear@6 | 101 |
nuclear@6 | 102 float BoolAnim::get_value() const |
nuclear@6 | 103 { |
nuclear@6 | 104 return get_value(get_msec()); |
nuclear@6 | 105 } |
nuclear@6 | 106 |
nuclear@6 | 107 float BoolAnim::get_value(long tm) const |
nuclear@6 | 108 { |
nuclear@6 | 109 update(tm); |
nuclear@6 | 110 return value; |
nuclear@6 | 111 } |
nuclear@6 | 112 |
nuclear@6 | 113 float BoolAnim::get_dir() const |
nuclear@6 | 114 { |
nuclear@6 | 115 return get_dir(get_msec()); |
nuclear@6 | 116 } |
nuclear@6 | 117 |
nuclear@6 | 118 float BoolAnim::get_dir(long tm) const |
nuclear@6 | 119 { |
nuclear@6 | 120 update(tm); |
nuclear@6 | 121 return trans_dir; |
nuclear@6 | 122 } |
nuclear@6 | 123 |
nuclear@6 | 124 BoolAnim::operator bool() const |
nuclear@6 | 125 { |
nuclear@6 | 126 return get_state(); |
nuclear@6 | 127 } |
nuclear@6 | 128 |
nuclear@6 | 129 BoolAnim::operator float() const |
nuclear@6 | 130 { |
nuclear@6 | 131 return get_value(); |
nuclear@6 | 132 } |
nuclear@6 | 133 |
nuclear@6 | 134 #ifdef WIN32 |
nuclear@6 | 135 #include <windows.h> |
nuclear@6 | 136 |
nuclear@6 | 137 static long default_get_msec() |
nuclear@6 | 138 { |
nuclear@6 | 139 return GetTickCount(); |
nuclear@6 | 140 } |
nuclear@6 | 141 #else |
nuclear@6 | 142 #include <sys/time.h> |
nuclear@6 | 143 |
nuclear@6 | 144 static long default_get_msec() |
nuclear@6 | 145 { |
nuclear@6 | 146 static struct timeval tv0; |
nuclear@6 | 147 struct timeval tv; |
nuclear@6 | 148 |
nuclear@6 | 149 gettimeofday(&tv, 0); |
nuclear@6 | 150 if(tv0.tv_sec == 0 && tv0.tv_usec == 0) { |
nuclear@6 | 151 tv0 = tv; |
nuclear@6 | 152 return 0; |
nuclear@6 | 153 } |
nuclear@6 | 154 return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000; |
nuclear@6 | 155 } |
nuclear@6 | 156 #endif |