mirror of
https://github.com/genodelabs/genode.git
synced 2025-01-18 10:46:25 +00:00
demo: Utility for animating values
This commit is contained in:
parent
0be6817226
commit
d5f57992ac
85
repos/demo/include/util/lazy_value.h
Normal file
85
repos/demo/include/util/lazy_value.h
Normal file
@ -0,0 +1,85 @@
|
||||
/*
|
||||
* \brief Animated value
|
||||
* \author Norman Feske
|
||||
* \date 2010-10-29
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2010-2013 Genode Labs GmbH
|
||||
*
|
||||
* This file is part of the Genode OS framework, which is distributed
|
||||
* under the terms of the GNU General Public License version 2.
|
||||
*/
|
||||
|
||||
#ifndef _INCLUDE__UTIL__LAZY_VALUE_H_
|
||||
#define _INCLUDE__UTIL__LAZY_VALUE_H_
|
||||
|
||||
template <typename T>
|
||||
class Lazy_value
|
||||
{
|
||||
private:
|
||||
|
||||
T _speed;
|
||||
T _curr;
|
||||
T _dst;
|
||||
T _accel;
|
||||
|
||||
public:
|
||||
|
||||
Lazy_value()
|
||||
: _speed(0), _curr(0), _dst(0), _accel(1)
|
||||
{ }
|
||||
|
||||
Lazy_value(T value)
|
||||
: _speed(0), _curr(value), _dst(value), _accel(1)
|
||||
{ }
|
||||
|
||||
void dst(T dst, unsigned steps)
|
||||
{
|
||||
_dst = dst;
|
||||
T delta = (_curr > _dst) ? _curr - _dst : _dst - _curr;
|
||||
if (steps == 0)
|
||||
steps = 1;
|
||||
_accel = (4*delta) / (steps*steps);
|
||||
_speed = 0;
|
||||
if (_accel < 1)
|
||||
_accel = 1;
|
||||
}
|
||||
|
||||
T dst() { return _dst; }
|
||||
|
||||
void assign(T value) { _curr = value; }
|
||||
|
||||
void animate()
|
||||
{
|
||||
/* destination value reached? */
|
||||
if (_curr == _dst) {
|
||||
_speed = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
/* change value with current speed */
|
||||
if (_curr > _dst) {
|
||||
_curr -= _speed;
|
||||
if (_curr < _dst) _curr = _dst;
|
||||
} else {
|
||||
_curr += _speed;
|
||||
if (_curr > _dst) _curr = _dst;
|
||||
}
|
||||
|
||||
/* adapt speed */
|
||||
T delta = _curr - _dst;
|
||||
if (delta < 0) delta = -delta;
|
||||
|
||||
if (_speed*_speed < delta*_accel)
|
||||
_speed += _accel;
|
||||
else
|
||||
_speed -= _accel;
|
||||
|
||||
if (_speed < 1) _speed = 1;
|
||||
}
|
||||
|
||||
operator T () const { return _curr; }
|
||||
};
|
||||
|
||||
#endif /* _INCLUDE__UTIL__LAZY_VALUE_H_ */
|
Loading…
Reference in New Issue
Block a user