gameui

view src/widget.cc @ 2:e5b1525084f7

boolanim
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 20 Mar 2014 07:03:58 +0200
parents 54ffb1765d39
children f1014234dece
line source
1 #include <string>
2 #include <sstream>
3 #include "widget.h"
4 #include "boolanm.h"
6 using namespace gameui;
8 struct WidgetImpl {
9 std::string name_prefix;
10 std::string name;
11 BBox box;
13 BoolAnim visible, active, press, hover;
14 };
17 Widget::Widget()
18 : name_prefix("widget")
19 {
20 static int widget_count;
22 std::stringstream sstr;
23 sstr << name_prefix << widget_count++;
24 name = sstr.str();
26 box.bmin = Vec2(0, 0);
27 box.bmax = Vec2(1, 1);
29 vis_st = VST_VISIBLE;
30 act_st = AST_ACTIVE;
32 vis = act = 1.0;
33 }
35 Widget::~Widget()
36 {
37 }
39 void Widget::show()
40 {
41 if(vis_st == VST_EASEVISIBLE || vis_st == VST_EASEIN) {
42 return;
43 }
45 vis_st = VST_EASEIN;
46 vis_start_time = get_cur_time();
47 }
49 void Widget::hide()
50 {
51 if(vis_st == VST_EASEHIDDEN || vis_st == VST_EASEOUT) {
52 return;
53 }
55 vis_st = VST_EASEOUT;
56 vis_start_time = get_cur_time();
57 }
59 float Widget::get_visibility() const
60 {
61 switch(vis_st) {
62 case VST_EASEIN:
63 vis = (get_cur_time() - vis_start_time) / gameui::ease_time;
64 if(vis < 0.0) vis = 0.0;
65 if(vis > 1.0) vis = 1.0;
66 break;
68 case VST_EASEOUT:
69 vis = 1.0 - (get_cur_time() - vis_start_time) / gameui::ease_time;
70 if(vis < 0.0) vis = 0.0;
71 if(vis > 1.0) vis = 1.0;
72 break;
74 case VST_HIDDEN:
75 vis = 0.0;
76 break;
78 case VST_VISIBLE:
79 vis = 1.0;
80 break;
81 }
83 return vis;
84 }
86 void Widget::activate()
87 {
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