gui/dialogs/debug_clock.cpp

Go to the documentation of this file.
00001 /* $Id: debug_clock.cpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2010 - 2012 by Mark de Wever <koraq@xs4all.nl>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 #define GETTEXT_DOMAIN "wesnoth-lib"
00017 
00018 #include "gui/dialogs/debug_clock.hpp"
00019 
00020 #include "gui/dialogs/dialog.hpp"
00021 #include "gui/widgets/integer_selector.hpp"
00022 #include "gui/widgets/window.hpp"
00023 #include "gui/widgets/settings.hpp"
00024 #include "gui/widgets/progress_bar.hpp"
00025 
00026 #include <boost/bind.hpp>
00027 
00028 #include <ctime>
00029 
00030 namespace gui2 {
00031 
00032 /*WIKI
00033  * @page = GUIWindowDefinitionWML
00034  * @order = 2_clock
00035  *
00036  * == Clock ==
00037  *
00038  * This shows the dialog for keeping track of the drawing events related to the
00039  * current time. (This window is used for debug purposes only.)
00040  *
00041  * @begin{table}{dialog_widgets}
00042  * hour_percentage   & & progress_bar     & o &
00043  *         This shows the hours as a percentage, where 24 hours is 100%. $
00044  * minute_percentage & & progress_bar     & o &
00045  *         This shows the minutes as a percentage, where 60 minutes is 100%. $
00046  * second_percentage & & progress_bar     & o &
00047  *         This shows the seconds as a percentage, where 60 seconds is 100%. $
00048  *
00049  * hour              & & integer_selector & o &
00050  *         This shows the seconds since the beginning of the day. The control
00051  *         should have a ''minimum_value'' of 0 and a ''maximum_value'' of 86399
00052  *         (24 * 60 * 60 - 1). $
00053  *
00054  * minute            & & integer_selector & o &
00055  *         This shows the seconds since the beginning of the current hour. The
00056  *         control should have a ''minimum_value'' of 0 and a ''maximum_value''
00057  *         of 3599 (60 * 60 - 1). $
00058  *
00059  * minute            & & integer_selector & o &
00060  *         This shows the seconds since the beginning of the current minute. The
00061  *         control should have a ''minimum_value'' of 0 and a ''maximum_value''
00062  *         of 59. $
00063  *
00064  * clock             & & control          & o &
00065  *         A control which will have set three variables in its canvas:
00066  *         @* hour, the same value as the hour integer_selector.
00067  *         @* minute, the same value as the minute integer_selector.
00068  *         @* second, the same value as the second integer_selector.
00069  *         @- the control can then should the time in its own preferred
00070  *         format(s). $
00071  * @end{table}
00072  */
00073 
00074 REGISTER_DIALOG(debug_clock)
00075 
00076 void tdebug_clock::pre_show(CVideo& /*video*/, twindow& window)
00077 {
00078     hour_percentage_ = find_widget<tprogress_bar>(
00079               &window
00080             , "hour_percentage"
00081             , false
00082             , false);
00083     minute_percentage_ = find_widget<tprogress_bar>(
00084               &window
00085             , "minute_percentage"
00086             , false
00087             , false);
00088     second_percentage_ = find_widget<tprogress_bar>(
00089               &window
00090             , "second_percentage"
00091             , false
00092             , false);
00093 
00094     hour_ = find_widget<tinteger_selector_>(&window, "hour", false, false);
00095     if(hour_) {
00096         dynamic_cast<tcontrol*>(hour_)->set_active(false);
00097     }
00098     minute_ = find_widget<tinteger_selector_>(&window, "minute", false, false);
00099     if(minute_) {
00100         dynamic_cast<tcontrol*>(minute_)->set_active(false);
00101     }
00102     second_ = find_widget<tinteger_selector_>(&window, "second", false, false);
00103     if(second_) {
00104         dynamic_cast<tcontrol*>(second_)->set_active(false);
00105     }
00106 
00107     clock_ = find_widget<tcontrol>(&window, "clock", false, false);
00108 
00109     window_ = &window;
00110 
00111     signal_ = boost::bind(&tdebug_clock::update_time, this, false);
00112     window.connect_signal<event::DRAW>(
00113               signal_
00114             , event::tdispatcher::front_child);
00115 
00116     time_.set_current_time();
00117     update_time(true);
00118 }
00119 
00120 void tdebug_clock::post_show(CVideo& /*video*/)
00121 {
00122     window_->disconnect_signal<event::DRAW>(signal_);
00123 }
00124 
00125 void tdebug_clock::update_time(const bool force)
00126 {
00127     if(!time_.step() && !force) {
00128         return;
00129     }
00130 
00131     if(hour_percentage_) {
00132         hour_percentage_->set_percentage(time_.hour / 0.24);
00133     }
00134     if(minute_percentage_) {
00135         minute_percentage_->set_percentage(time_.minute / 0.60);
00136     }
00137     if(second_percentage_) {
00138         second_percentage_->set_percentage(time_.second / 0.60);
00139     }
00140 
00141     const int hour_stamp = time_.hour * 3600 + time_.minute * 60 + time_.second;
00142     const int minute_stamp = time_.minute * 60 + time_.second;
00143     const int second_stamp = time_.second;
00144 
00145     if(hour_) {
00146         hour_->set_value(hour_stamp);
00147     }
00148     if(minute_) {
00149         minute_->set_value(minute_stamp);
00150     }
00151     if(second_) {
00152         second_->set_value(second_stamp);
00153     }
00154 
00155     if(clock_) {
00156         foreach(tcanvas& canvas, clock_->canvas()) {
00157             canvas.set_variable("hour", variant(hour_stamp));
00158             canvas.set_variable("minute", variant(minute_stamp));
00159             canvas.set_variable("second", variant(second_stamp));
00160         }
00161         clock_->set_dirty();
00162     }
00163 }
00164 
00165 tdebug_clock::ttime::ttime()
00166     : hour(0)
00167     , minute(0)
00168     , second(0)
00169     , millisecond(0)
00170 {
00171 }
00172 
00173 void tdebug_clock::ttime::set_current_time()
00174 {
00175     time_t now = time(NULL);
00176     tm *stamp = localtime(&now);
00177 
00178     hour = stamp->tm_hour;
00179     minute = stamp->tm_min;
00180     second = stamp->tm_sec;
00181     millisecond = 0;
00182 }
00183 
00184 bool tdebug_clock::ttime::step(const unsigned milliseconds)
00185 {
00186     millisecond += milliseconds;
00187 
00188     if(millisecond < 1000) return false;
00189 
00190     millisecond -= 1000;
00191     ++second;
00192 
00193     if(second < 60) return true;
00194 
00195     second -= 60;
00196     ++minute;
00197 
00198     if(minute < 60) return true;
00199 
00200     minute -= 60;
00201     ++hour;
00202 
00203     if(hour < 24) return true;
00204 
00205     hour -= 24;
00206 
00207     return true;
00208 }
00209 
00210 } // namespace gui2
00211 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Thu May 24 2012 01:02:42 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs