The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
debug_clock.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2016 by Mark de Wever <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
18 
20 #include "gui/dialogs/dialog.hpp"
22 #include "gui/widgets/window.hpp"
23 #include "gui/widgets/settings.hpp"
24 #include "gui/widgets/pane.hpp"
26 
27 #include "utils/functional.hpp"
28 
29 #include <ctime>
30 
31 namespace gui2
32 {
33 
34 /*WIKI
35  * @page = GUIWindowDefinitionWML
36  * @order = 2_clock
37  *
38  * == Clock ==
39  *
40  * This shows the dialog for keeping track of the drawing events related to the
41  * current time. (This window is used for debug purposes only.)
42  *
43  * @begin{table}{dialog_widgets}
44  * hour_percentage & & progress_bar & o &
45  * This shows the hours as a percentage, where 24 hours is 100%. $
46  * minute_percentage & & progress_bar & o &
47  * This shows the minutes as a percentage, where 60 minutes is 100%. $
48  * second_percentage & & progress_bar & o &
49  * This shows the seconds as a percentage, where 60 seconds is 100%. $
50  *
51  * hour & & integer_selector & o &
52  * This shows the seconds since the beginning of the day. The control
53  * should have a ''minimum_value'' of 0 and a ''maximum_value'' of 86399
54  * (24 * 60 * 60 - 1). $
55  *
56  * minute & & integer_selector & o &
57  * This shows the seconds since the beginning of the current hour. The
58  * control should have a ''minimum_value'' of 0 and a ''maximum_value''
59  * of 3599 (60 * 60 - 1). $
60  *
61  * minute & & integer_selector & o &
62  * This shows the seconds since the beginning of the current minute. The
63  * control should have a ''minimum_value'' of 0 and a ''maximum_value''
64  * of 59. $
65  *
66  * clock & & control & o &
67  * A control which will have set three variables in its canvas:
68  * @* hour, the same value as the hour integer_selector.
69  * @* minute, the same value as the minute integer_selector.
70  * @* second, the same value as the second integer_selector.
71  * @- the control can then should the time in its own preferred
72  * format(s). $
73  * @end{table}
74  */
75 
76 REGISTER_DIALOG(debug_clock)
77 
78 void tdebug_clock::pre_show(twindow& window)
79 {
80  hour_percentage_ = find_widget<tprogress_bar>(
81  &window, "hour_percentage", false, false);
82  minute_percentage_ = find_widget<tprogress_bar>(
83  &window, "minute_percentage", false, false);
84  second_percentage_ = find_widget<tprogress_bar>(
85  &window, "second_percentage", false, false);
86 
87  hour_ = find_widget<tinteger_selector_>(&window, "hour", false, false);
88  if(tcontrol *hour = dynamic_cast<tcontrol*>(hour_)) { //Note that the standard specifies that a dynamic cast of a null pointer is null
89  hour->set_active(false);
90  }
91  minute_ = find_widget<tinteger_selector_>(&window, "minute", false, false);
92  if(tcontrol *minute = dynamic_cast<tcontrol*>(minute_)) {
93  minute->set_active(false);
94  }
95  second_ = find_widget<tinteger_selector_>(&window, "second", false, false);
96  if(tcontrol *second = dynamic_cast<tcontrol*>(second_)) {
97  second->set_active(false);
98  }
99 
100  pane_ = find_widget<tpane>(&window, "pane", false, false);
101 
102  clock_ = find_widget<tcontrol>(&window, "clock", false, false);
103 
104  window_ = &window;
105 
106  signal_ = std::bind(&tdebug_clock::update_time, this, false);
107  window.connect_signal<event::DRAW>(signal_,
109 
110  time_.set_current_time();
111  update_time(true);
112 }
113 
115 {
117 }
118 
119 void tdebug_clock::update_time(const bool force)
120 {
121  if(!time_.step() && !force) {
122  return;
123  }
124 
125  if(hour_percentage_) {
127  }
128  if(minute_percentage_) {
130  }
131  if(second_percentage_) {
133  }
134 
135  const int hour_stamp = time_.hour * 3600 + time_.minute * 60 + time_.second;
136  const int minute_stamp = time_.minute * 60 + time_.second;
137  const int second_stamp = time_.second;
138 
139  if(hour_) {
140  hour_->set_value(hour_stamp);
141  }
142  if(minute_) {
143  minute_->set_value(minute_stamp);
144  }
145  if(second_) {
146  second_->set_value(second_stamp);
147  }
148 
149  if(clock_) {
150  for(auto & canvas : clock_->canvas())
151  {
152  canvas.set_variable("hour", variant(hour_stamp));
153  canvas.set_variable("minute", variant(minute_stamp));
154  canvas.set_variable("second", variant(second_stamp));
155  }
156  clock_->set_is_dirty(true);
157  }
158 
159  const std::map<std::string, std::string> tags;
160  std::map<std::string, string_map> item_data;
161  string_map item;
162 
163  item["label"] = std::to_string(second_stamp);
164  item_data.insert(std::make_pair("time", item));
165 
166  if(pane_) {
167  pane_->create_item(item_data, tags);
168  }
169 }
170 
171 tdebug_clock::ttime::ttime() : hour(0), minute(0), second(0), millisecond(0)
172 {
173 }
174 
176 {
177  time_t now = time(nullptr);
178  tm* stamp = localtime(&now);
179 
180  hour = stamp->tm_hour;
181  minute = stamp->tm_min;
182  second = stamp->tm_sec;
183  millisecond = 0;
184 }
185 
186 bool tdebug_clock::ttime::step(const unsigned milliseconds)
187 {
188  millisecond += milliseconds;
189 
190  if(millisecond < 1000)
191  return false;
192 
193  millisecond -= 1000;
194  ++second;
195 
196  if(second < 60)
197  return true;
198 
199  second -= 60;
200  ++minute;
201 
202  if(minute < 60)
203  return true;
204 
205  minute -= 60;
206  ++hour;
207 
208  if(hour < 24)
209  return true;
210 
211  hour -= 24;
212 
213  return true;
214 }
215 
216 } // namespace gui2
tcontrol * clock_
A widget that can display the time.
Definition: debug_clock.hpp:72
void post_show(CVideo &video)
Inherited from tdialog.
tprogress_bar * second_percentage_
Progress bar for displaying the seconds as a percentage.
Definition: debug_clock.hpp:58
GLvoid **typedef void(GLAPIENTRY *PFNGLGETVERTEXATTRIBDVPROC)(GLuint
Definition: glew.h:1806
void set_percentage(unsigned percentage)
Definition: video.hpp:58
boost::enable_if< boost::mpl::has_key< tset_event, boost::mpl::int_< E > > >::type disconnect_signal(const tsignal_function &signal, const tposition position=back_child)
Disconnect a signal for callback in tset_event.
Definition: dispatcher.hpp:297
This file contains the window object, this object is a top level container which has the event manage...
REGISTER_DIALOG(label_settings)
Clock to test the draw events.
Definition: debug_clock.hpp:31
void set_is_dirty(const bool is_dirty)
Definition: widget.cpp:435
unsigned create_item(const std::map< std::string, string_map > &item_data, const std::map< std::string, std::string > &tags)
Creates a new item.
Definition: pane.cpp:142
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
unsigned minute
The number of minutes.
This file contains the settings handling of the widget library.
twindow * window_
The window being shown.
Definition: debug_clock.hpp:75
Periodic redraw request.
Definition: handler.hpp:55
void update_time(const bool force)
The callback for the drawing routine.
void set_current_time()
Sets the fields to the current time.
bool step(const unsigned milliseconds=30)
Moves the clock x milliseconds forward.
virtual void set_value(const int value)=0
Sets the selected value.
tinteger_selector_ * hour_
An integer selector to display the total seconds.
Definition: debug_clock.hpp:61
tinteger_selector_ * second_
An integer selector to display the seconds this minute.
Definition: debug_clock.hpp:67
tinteger_selector_ * minute_
An integer selector to display the total seconds this hour.
Definition: debug_clock.hpp:64
std::map< std::string, t_string > string_map
Definition: generator.hpp:23
tprogress_bar * minute_percentage_
Progress bar for displaying the minutes as a percentage.
Definition: debug_clock.hpp:55
unsigned hour
The number of hours.
unsigned second
The number of seconds.
ttime time_
The `current' time.
std::vector< tcanvas > & canvas()
Definition: control.hpp:282
Base class for all visible items.
Definition: control.hpp:34
tprogress_bar * hour_percentage_
Progress bar for displaying the hours as a percentage.
Definition: debug_clock.hpp:52
event::tsignal_function signal_
The signal patched in the drawing routine.
Definition: debug_clock.hpp:78