gameui

changeset 2:e5b1525084f7

boolanim
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 20 Mar 2014 07:03:58 +0200
parents 54ffb1765d39
children f1014234dece
files include/widget.h src/boolanm.cc src/boolanm.h src/widget.cc
diffstat 4 files changed, 172 insertions(+), 9 deletions(-) [+]
line diff
     1.1 --- a/include/widget.h	Tue Mar 11 23:04:11 2014 +0200
     1.2 +++ b/include/widget.h	Thu Mar 20 07:03:58 2014 +0200
     1.3 @@ -16,7 +16,7 @@
     1.4  	Vec2 bmin, bmax;
     1.5  };
     1.6  
     1.7 -class WidgetImpl;
     1.8 +struct WidgetImpl;
     1.9  
    1.10  class Widget {
    1.11  private:
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/boolanm.cc	Thu Mar 20 07:03:58 2014 +0200
     2.3 @@ -0,0 +1,129 @@
     2.4 +#include "boolanm.h"
     2.5 +
     2.6 +static long default_get_msec();
     2.7 +
     2.8 +BoolAnim::BoolAnim(bool st)
     2.9 +{
    2.10 +	value = st ? 1.0 : 0.0;
    2.11 +	trans_dir = 0.0;
    2.12 +	trans_start = 0;
    2.13 +	trans_dur = 1000;
    2.14 +	get_msec = default_get_msec;
    2.15 +}
    2.16 +
    2.17 +void BoolAnim::update(long tm) const
    2.18 +{
    2.19 +	if(trans_dir == 0.0) return;
    2.20 +
    2.21 +	float dt = (tm - trans_start) / 1000.0;
    2.22 +	float t = dt / (trans_dur / 1000.0);
    2.23 +
    2.24 +	if(trans_dir > 0.0) {
    2.25 +		value = t;
    2.26 +	} else {
    2.27 +		value = 1.0 - t;
    2.28 +	}
    2.29 +
    2.30 +	if(value < 0.0) {
    2.31 +		value = 0.0;
    2.32 +		trans_dir = 0.0;
    2.33 +	} else if(value > 1.0) {
    2.34 +		value = 1.0;
    2.35 +		trans_dir = 0.0;
    2.36 +	}
    2.37 +}
    2.38 +
    2.39 +void BoolAnim::set_transition_duration(long dur)
    2.40 +{
    2.41 +	trans_dur = dur;
    2.42 +}
    2.43 +
    2.44 +void BoolAnim::set_time_callback(long (*time_func)())
    2.45 +{
    2.46 +	get_msec = time_func;
    2.47 +}
    2.48 +
    2.49 +void BoolAnim::change(bool st)
    2.50 +{
    2.51 +	change(st, get_msec());
    2.52 +}
    2.53 +
    2.54 +void BoolAnim::change(bool st, long tm)
    2.55 +{
    2.56 +	trans_dir = st ? 1.0 : -1.0;
    2.57 +	trans_start = tm;
    2.58 +}
    2.59 +
    2.60 +bool BoolAnim::get_state() const
    2.61 +{
    2.62 +	return get_state(get_msec());
    2.63 +}
    2.64 +
    2.65 +bool BoolAnim::get_state(long tm) const
    2.66 +{
    2.67 +	update(tm);
    2.68 +
    2.69 +	// if we're not in transition use the value (should be 0 or 1)
    2.70 +	if(trans_dir == 0.0) {
    2.71 +		return value > 0.5;
    2.72 +	}
    2.73 +
    2.74 +	// if we're in transition base it on the direction of the transition
    2.75 +	return trans_dir > 0.0;
    2.76 +}
    2.77 +
    2.78 +float BoolAnim::get_value() const
    2.79 +{
    2.80 +	return get_value(get_msec());
    2.81 +}
    2.82 +
    2.83 +float BoolAnim::get_value(long tm) const
    2.84 +{
    2.85 +	update(tm);
    2.86 +	return value;
    2.87 +}
    2.88 +
    2.89 +float BoolAnim::get_dir() const
    2.90 +{
    2.91 +	return get_dir(get_msec());
    2.92 +}
    2.93 +
    2.94 +float BoolAnim::get_dir(long tm) const
    2.95 +{
    2.96 +	update(tm);
    2.97 +	return trans_dir;
    2.98 +}
    2.99 +
   2.100 +BoolAnim::operator bool() const
   2.101 +{
   2.102 +	return get_state();
   2.103 +}
   2.104 +
   2.105 +BoolAnim::operator float() const
   2.106 +{
   2.107 +	return get_value();
   2.108 +}
   2.109 +
   2.110 +#ifdef WIN32
   2.111 +#include <windows.h>
   2.112 +
   2.113 +static long default_get_msec()
   2.114 +{
   2.115 +	return GetTickCount();
   2.116 +}
   2.117 +#else
   2.118 +#include <sys/time.h>
   2.119 +
   2.120 +static long default_get_msec()
   2.121 +{
   2.122 +	static struct timeval tv0;
   2.123 +	struct timeval tv;
   2.124 +
   2.125 +	gettimeofday(&tv, 0);
   2.126 +	if(tv0.tv_sec == 0 && tv0.tv_usec == 0) {
   2.127 +		tv0 = tv;
   2.128 +		return 0;
   2.129 +	}
   2.130 +	return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000;
   2.131 +}
   2.132 +#endif
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/boolanm.h	Thu Mar 20 07:03:58 2014 +0200
     3.3 @@ -0,0 +1,38 @@
     3.4 +#ifndef BOOLANIM_H_
     3.5 +#define BOOLANIM_H_
     3.6 +
     3.7 +class BoolAnim {
     3.8 +private:
     3.9 +	mutable float value;
    3.10 +	mutable float trans_dir;	// transition direction (sign)
    3.11 +	long trans_start;	// transition start time
    3.12 +	long trans_dur;
    3.13 +
    3.14 +	long (*get_msec)();
    3.15 +
    3.16 +	void update(long tm) const;
    3.17 +
    3.18 +public:
    3.19 +	BoolAnim(bool st = false);
    3.20 +
    3.21 +	void set_transition_duration(long dur);
    3.22 +	void set_time_callback(long (*time_func)());
    3.23 +
    3.24 +	void change(bool st);
    3.25 +	void change(bool st, long trans_start);
    3.26 +
    3.27 +	bool get_state() const;
    3.28 +	bool get_state(long tm) const;
    3.29 +
    3.30 +	float get_value() const;
    3.31 +	float get_value(long tm) const;
    3.32 +
    3.33 +	// transition direction (-1, 0, 1)
    3.34 +	float get_dir() const;
    3.35 +	float get_dir(long tm) const;
    3.36 +
    3.37 +	operator bool() const;	// equivalent to get_state
    3.38 +	operator float() const;	// equivalent to get_value
    3.39 +};
    3.40 +
    3.41 +#endif	// BOOLANIM_H_
     4.1 --- a/src/widget.cc	Tue Mar 11 23:04:11 2014 +0200
     4.2 +++ b/src/widget.cc	Thu Mar 20 07:03:58 2014 +0200
     4.3 @@ -1,20 +1,16 @@
     4.4  #include <string>
     4.5 -#include <stringstream>
     4.6 +#include <sstream>
     4.7  #include "widget.h"
     4.8 +#include "boolanm.h"
     4.9  
    4.10  using namespace gameui;
    4.11  
    4.12 -class WidgetImpl {
    4.13 -public:
    4.14 +struct WidgetImpl {
    4.15  	std::string name_prefix;
    4.16  	std::string name;
    4.17  	BBox box;
    4.18 -	VisState vis_st;
    4.19 -	ActiveState act_st;
    4.20  
    4.21 -	float vis, act;
    4.22 -
    4.23 -	long vis_start_time, act_start_time;
    4.24 +	BoolAnim visible, active, press, hover;
    4.25  };
    4.26  
    4.27