gameui

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