# HG changeset patch # User John Tsiombikas # Date 1395291838 -7200 # Node ID e5b1525084f73ab8cb4cf1d77b99f2ab4eb08c6b # Parent 54ffb1765d3927cc3cf2e0cc1d62a2a781bc787d boolanim diff -r 54ffb1765d39 -r e5b1525084f7 include/widget.h --- a/include/widget.h Tue Mar 11 23:04:11 2014 +0200 +++ b/include/widget.h Thu Mar 20 07:03:58 2014 +0200 @@ -16,7 +16,7 @@ Vec2 bmin, bmax; }; -class WidgetImpl; +struct WidgetImpl; class Widget { private: diff -r 54ffb1765d39 -r e5b1525084f7 src/boolanm.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/boolanm.cc Thu Mar 20 07:03:58 2014 +0200 @@ -0,0 +1,129 @@ +#include "boolanm.h" + +static long default_get_msec(); + +BoolAnim::BoolAnim(bool st) +{ + value = st ? 1.0 : 0.0; + trans_dir = 0.0; + trans_start = 0; + trans_dur = 1000; + get_msec = default_get_msec; +} + +void BoolAnim::update(long tm) const +{ + if(trans_dir == 0.0) return; + + float dt = (tm - trans_start) / 1000.0; + float t = dt / (trans_dur / 1000.0); + + if(trans_dir > 0.0) { + value = t; + } else { + value = 1.0 - t; + } + + if(value < 0.0) { + value = 0.0; + trans_dir = 0.0; + } else if(value > 1.0) { + value = 1.0; + trans_dir = 0.0; + } +} + +void BoolAnim::set_transition_duration(long dur) +{ + trans_dur = dur; +} + +void BoolAnim::set_time_callback(long (*time_func)()) +{ + get_msec = time_func; +} + +void BoolAnim::change(bool st) +{ + change(st, get_msec()); +} + +void BoolAnim::change(bool st, long tm) +{ + trans_dir = st ? 1.0 : -1.0; + trans_start = tm; +} + +bool BoolAnim::get_state() const +{ + return get_state(get_msec()); +} + +bool BoolAnim::get_state(long tm) const +{ + update(tm); + + // if we're not in transition use the value (should be 0 or 1) + if(trans_dir == 0.0) { + return value > 0.5; + } + + // if we're in transition base it on the direction of the transition + return trans_dir > 0.0; +} + +float BoolAnim::get_value() const +{ + return get_value(get_msec()); +} + +float BoolAnim::get_value(long tm) const +{ + update(tm); + return value; +} + +float BoolAnim::get_dir() const +{ + return get_dir(get_msec()); +} + +float BoolAnim::get_dir(long tm) const +{ + update(tm); + return trans_dir; +} + +BoolAnim::operator bool() const +{ + return get_state(); +} + +BoolAnim::operator float() const +{ + return get_value(); +} + +#ifdef WIN32 +#include + +static long default_get_msec() +{ + return GetTickCount(); +} +#else +#include + +static long default_get_msec() +{ + static struct timeval tv0; + struct timeval tv; + + gettimeofday(&tv, 0); + if(tv0.tv_sec == 0 && tv0.tv_usec == 0) { + tv0 = tv; + return 0; + } + return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000; +} +#endif diff -r 54ffb1765d39 -r e5b1525084f7 src/boolanm.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/boolanm.h Thu Mar 20 07:03:58 2014 +0200 @@ -0,0 +1,38 @@ +#ifndef BOOLANIM_H_ +#define BOOLANIM_H_ + +class BoolAnim { +private: + mutable float value; + mutable float trans_dir; // transition direction (sign) + long trans_start; // transition start time + long trans_dur; + + long (*get_msec)(); + + void update(long tm) const; + +public: + BoolAnim(bool st = false); + + void set_transition_duration(long dur); + void set_time_callback(long (*time_func)()); + + void change(bool st); + void change(bool st, long trans_start); + + bool get_state() const; + bool get_state(long tm) const; + + float get_value() const; + float get_value(long tm) const; + + // transition direction (-1, 0, 1) + float get_dir() const; + float get_dir(long tm) const; + + operator bool() const; // equivalent to get_state + operator float() const; // equivalent to get_value +}; + +#endif // BOOLANIM_H_ diff -r 54ffb1765d39 -r e5b1525084f7 src/widget.cc --- a/src/widget.cc Tue Mar 11 23:04:11 2014 +0200 +++ b/src/widget.cc Thu Mar 20 07:03:58 2014 +0200 @@ -1,20 +1,16 @@ #include -#include +#include #include "widget.h" +#include "boolanm.h" using namespace gameui; -class WidgetImpl { -public: +struct WidgetImpl { std::string name_prefix; std::string name; BBox box; - VisState vis_st; - ActiveState act_st; - float vis, act; - - long vis_start_time, act_start_time; + BoolAnim visible, active, press, hover; };