istereo2
view 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 source
1 /*
2 GoatKit - a themable/animated widget toolkit for games
3 Copyright (C) 2014 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/>.
17 */
18 #include "boolanm.h"
20 static long default_get_msec();
22 BoolAnim::BoolAnim(bool st)
23 {
24 set(st);
25 trans_start = 0;
26 trans_dur = 500;
27 get_msec = default_get_msec;
28 }
30 void BoolAnim::update(long tm) const
31 {
32 if(trans_dir == 0.0) return;
34 float dt = (tm - trans_start) / 1000.0;
35 float t = dt / (trans_dur / 1000.0);
37 if(trans_dir > 0.0) {
38 value = t;
39 } else {
40 value = 1.0 - t;
41 }
43 if(value < 0.0) {
44 value = 0.0;
45 trans_dir = 0.0;
46 } else if(value > 1.0) {
47 value = 1.0;
48 trans_dir = 0.0;
49 }
50 }
52 void BoolAnim::set_transition_duration(long dur)
53 {
54 trans_dur = dur;
55 }
57 void BoolAnim::set_time_callback(long (*time_func)())
58 {
59 get_msec = time_func;
60 }
62 void BoolAnim::set(bool st)
63 {
64 value = st ? 1.0 : 0.0;
65 trans_dir = 0.0;
66 }
68 void BoolAnim::change(bool st)
69 {
70 change(st, get_msec());
71 }
73 void BoolAnim::change(bool st, long tm)
74 {
75 trans_dir = st ? 1.0 : -1.0;
76 trans_start = tm;
77 }
79 bool BoolAnim::get_state() const
80 {
81 return get_state(get_msec());
82 }
84 bool BoolAnim::get_state(long tm) const
85 {
86 update(tm);
88 // if we're not in transition use the value (should be 0 or 1)
89 if(trans_dir == 0.0) {
90 return value > 0.5;
91 }
93 // if we're in transition base it on the direction of the transition
94 return trans_dir > 0.0;
95 }
97 float BoolAnim::get_value() const
98 {
99 return get_value(get_msec());
100 }
102 float BoolAnim::get_value(long tm) const
103 {
104 update(tm);
105 return value;
106 }
108 float BoolAnim::get_dir() const
109 {
110 return get_dir(get_msec());
111 }
113 float BoolAnim::get_dir(long tm) const
114 {
115 update(tm);
116 return trans_dir;
117 }
119 BoolAnim::operator bool() const
120 {
121 return get_state();
122 }
124 BoolAnim::operator float() const
125 {
126 return get_value();
127 }
129 #ifdef WIN32
130 #include <windows.h>
132 static long default_get_msec()
133 {
134 return GetTickCount();
135 }
136 #else
137 #include <sys/time.h>
139 static long default_get_msec()
140 {
141 static struct timeval tv0;
142 struct timeval tv;
144 gettimeofday(&tv, 0);
145 if(tv0.tv_sec == 0 && tv0.tv_usec == 0) {
146 tv0 = tv;
147 return 0;
148 }
149 return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000;
150 }
151 #endif