gameui

diff src/widget.cc @ 0:3aa12cdb9925

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 24 Feb 2014 22:25:49 +0200
parents
children 54ffb1765d39
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/widget.cc	Mon Feb 24 22:25:49 2014 +0200
     1.3 @@ -0,0 +1,110 @@
     1.4 +#include <string>
     1.5 +#include <stringstream>
     1.6 +#include "widget.h"
     1.7 +
     1.8 +using namespace gameui;
     1.9 +
    1.10 +class WidgetImpl {
    1.11 +public:
    1.12 +	std::string name_prefix;
    1.13 +	std::string name;
    1.14 +	BBox box;
    1.15 +	VisState vis_st;
    1.16 +	ActiveState act_st;
    1.17 +
    1.18 +	float vis, act;
    1.19 +
    1.20 +	long vis_start_time, act_start_time;
    1.21 +};
    1.22 +
    1.23 +
    1.24 +Widget::Widget()
    1.25 +	: name_prefix("widget")
    1.26 +{
    1.27 +	static int widget_count;
    1.28 +
    1.29 +	std::stringstream sstr;
    1.30 +	sstr << name_prefix << widget_count++;
    1.31 +	name = sstr.str();
    1.32 +
    1.33 +	box.bmin = Vec2(0, 0);
    1.34 +	box.bmax = Vec2(1, 1);
    1.35 +
    1.36 +	vis_st = VST_VISIBLE;
    1.37 +	act_st = AST_ACTIVE;
    1.38 +
    1.39 +	vis = act = 1.0;
    1.40 +}
    1.41 +
    1.42 +Widget::~Widget()
    1.43 +{
    1.44 +}
    1.45 +
    1.46 +void Widget::show()
    1.47 +{
    1.48 +	if(vis_st == VST_EASEVISIBLE || vis_st == VST_EASEIN) {
    1.49 +		return;
    1.50 +	}
    1.51 +
    1.52 +	vis_st = VST_EASEIN;
    1.53 +	vis_start_time = get_cur_time();
    1.54 +}
    1.55 +
    1.56 +void Widget::hide()
    1.57 +{
    1.58 +	if(vis_st == VST_EASEHIDDEN || vis_st == VST_EASEOUT) {
    1.59 +		return;
    1.60 +	}
    1.61 +
    1.62 +	vis_st = VST_EASEOUT;
    1.63 +	vis_start_time = get_cur_time();
    1.64 +}
    1.65 +
    1.66 +float Widget::get_visibility() const
    1.67 +{
    1.68 +	switch(vis_st) {
    1.69 +	case VST_EASEIN:
    1.70 +		vis = (get_cur_time() - vis_start_time) / gameui::ease_time;
    1.71 +		if(vis < 0.0) vis = 0.0;
    1.72 +		if(vis > 1.0) vis = 1.0;
    1.73 +		break;
    1.74 +
    1.75 +	case VST_EASEOUT:
    1.76 +		vis = 1.0 - (get_cur_time() - vis_start_time) / gameui::ease_time;
    1.77 +		if(vis < 0.0) vis = 0.0;
    1.78 +		if(vis > 1.0) vis = 1.0;
    1.79 +		break;
    1.80 +
    1.81 +	case VST_HIDDEN:
    1.82 +		vis = 0.0;
    1.83 +		break;
    1.84 +
    1.85 +	case VST_VISIBLE:
    1.86 +		vis = 1.0;
    1.87 +		break;
    1.88 +	}
    1.89 +
    1.90 +	return vis;
    1.91 +}
    1.92 +
    1.93 +#ifdef WIN32
    1.94 +long gameui::get_cur_time()
    1.95 +{
    1.96 +	return GetTickCount();
    1.97 +}
    1.98 +#endif
    1.99 +
   1.100 +#if defined(__unix__) || defined(__APPLE__)
   1.101 +long gameui::get_cur_time()
   1.102 +{
   1.103 +	struct timeval tv;
   1.104 +	static struct timeval tv0;
   1.105 +
   1.106 +	gettimeofday(&tv, 0);
   1.107 +	if(tv0.tv_sec == 0 && tv0.tv_msec == 0) {
   1.108 +		tv0 = tv;
   1.109 +		return 0;
   1.110 +	}
   1.111 +	return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000;
   1.112 +}
   1.113 +#endif