gameui

view src/boolanm.cc @ 5:5a84873185ff

rudimentary theme plugin system and other minor fixes
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 22 Mar 2014 01:50:01 +0200
parents e5b1525084f7
children
line source
1 #include "boolanm.h"
3 static long default_get_msec();
5 BoolAnim::BoolAnim(bool st)
6 {
7 set(st);
8 trans_start = 0;
9 trans_dur = 500;
10 get_msec = default_get_msec;
11 }
13 void BoolAnim::update(long tm) const
14 {
15 if(trans_dir == 0.0) return;
17 float dt = (tm - trans_start) / 1000.0;
18 float t = dt / (trans_dur / 1000.0);
20 if(trans_dir > 0.0) {
21 value = t;
22 } else {
23 value = 1.0 - t;
24 }
26 if(value < 0.0) {
27 value = 0.0;
28 trans_dir = 0.0;
29 } else if(value > 1.0) {
30 value = 1.0;
31 trans_dir = 0.0;
32 }
33 }
35 void BoolAnim::set_transition_duration(long dur)
36 {
37 trans_dur = dur;
38 }
40 void BoolAnim::set_time_callback(long (*time_func)())
41 {
42 get_msec = time_func;
43 }
45 void BoolAnim::set(bool st)
46 {
47 value = st ? 1.0 : 0.0;
48 trans_dir = 0.0;
49 }
51 void BoolAnim::change(bool st)
52 {
53 change(st, get_msec());
54 }
56 void BoolAnim::change(bool st, long tm)
57 {
58 trans_dir = st ? 1.0 : -1.0;
59 trans_start = tm;
60 }
62 bool BoolAnim::get_state() const
63 {
64 return get_state(get_msec());
65 }
67 bool BoolAnim::get_state(long tm) const
68 {
69 update(tm);
71 // if we're not in transition use the value (should be 0 or 1)
72 if(trans_dir == 0.0) {
73 return value > 0.5;
74 }
76 // if we're in transition base it on the direction of the transition
77 return trans_dir > 0.0;
78 }
80 float BoolAnim::get_value() const
81 {
82 return get_value(get_msec());
83 }
85 float BoolAnim::get_value(long tm) const
86 {
87 update(tm);
88 return value;
89 }
91 float BoolAnim::get_dir() const
92 {
93 return get_dir(get_msec());
94 }
96 float BoolAnim::get_dir(long tm) const
97 {
98 update(tm);
99 return trans_dir;
100 }
102 BoolAnim::operator bool() const
103 {
104 return get_state();
105 }
107 BoolAnim::operator float() const
108 {
109 return get_value();
110 }
112 #ifdef WIN32
113 #include <windows.h>
115 static long default_get_msec()
116 {
117 return GetTickCount();
118 }
119 #else
120 #include <sys/time.h>
122 static long default_get_msec()
123 {
124 static struct timeval tv0;
125 struct timeval tv;
127 gettimeofday(&tv, 0);
128 if(tv0.tv_sec == 0 && tv0.tv_usec == 0) {
129 tv0 = tv;
130 return 0;
131 }
132 return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000;
133 }
134 #endif