gameui
view 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 source
1 #include <string>
2 #include <stringstream>
3 #include "widget.h"
5 using namespace gameui;
7 class WidgetImpl {
8 public:
9 std::string name_prefix;
10 std::string name;
11 BBox box;
12 VisState vis_st;
13 ActiveState act_st;
15 float vis, act;
17 long vis_start_time, act_start_time;
18 };
21 Widget::Widget()
22 : name_prefix("widget")
23 {
24 static int widget_count;
26 std::stringstream sstr;
27 sstr << name_prefix << widget_count++;
28 name = sstr.str();
30 box.bmin = Vec2(0, 0);
31 box.bmax = Vec2(1, 1);
33 vis_st = VST_VISIBLE;
34 act_st = AST_ACTIVE;
36 vis = act = 1.0;
37 }
39 Widget::~Widget()
40 {
41 }
43 void Widget::show()
44 {
45 if(vis_st == VST_EASEVISIBLE || vis_st == VST_EASEIN) {
46 return;
47 }
49 vis_st = VST_EASEIN;
50 vis_start_time = get_cur_time();
51 }
53 void Widget::hide()
54 {
55 if(vis_st == VST_EASEHIDDEN || vis_st == VST_EASEOUT) {
56 return;
57 }
59 vis_st = VST_EASEOUT;
60 vis_start_time = get_cur_time();
61 }
63 float Widget::get_visibility() const
64 {
65 switch(vis_st) {
66 case VST_EASEIN:
67 vis = (get_cur_time() - vis_start_time) / gameui::ease_time;
68 if(vis < 0.0) vis = 0.0;
69 if(vis > 1.0) vis = 1.0;
70 break;
72 case VST_EASEOUT:
73 vis = 1.0 - (get_cur_time() - vis_start_time) / gameui::ease_time;
74 if(vis < 0.0) vis = 0.0;
75 if(vis > 1.0) vis = 1.0;
76 break;
78 case VST_HIDDEN:
79 vis = 0.0;
80 break;
82 case VST_VISIBLE:
83 vis = 1.0;
84 break;
85 }
87 return vis;
88 }
90 #ifdef WIN32
91 long gameui::get_cur_time()
92 {
93 return GetTickCount();
94 }
95 #endif
97 #if defined(__unix__) || defined(__APPLE__)
98 long gameui::get_cur_time()
99 {
100 struct timeval tv;
101 static struct timeval tv0;
103 gettimeofday(&tv, 0);
104 if(tv0.tv_sec == 0 && tv0.tv_msec == 0) {
105 tv0 = tv;
106 return 0;
107 }
108 return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000;
109 }
110 #endif