The Battle for Wesnoth  1.15.1+dev
debug_clock.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project https://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 
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 namespace dialogs
34 {
35 
36 /*WIKI
37  * @page = GUIWindowDefinitionWML
38  * @order = 2_clock
39  *
40  * == Clock ==
41  *
42  * This shows the dialog for keeping track of the drawing events related to the
43  * current time. (This window is used for debug purposes only.)
44  *
45  * @begin{table}{dialog_widgets}
46  * hour_percentage & & progress_bar & o &
47  * This shows the hours as a percentage, where 24 hours is 100%. $
48  * minute_percentage & & progress_bar & o &
49  * This shows the minutes as a percentage, where 60 minutes is 100%. $
50  * second_percentage & & progress_bar & o &
51  * This shows the seconds as a percentage, where 60 seconds is 100%. $
52  *
53  * hour & & integer_selector & o &
54  * This shows the seconds since the beginning of the day. The styled_widget
55  * should have a ''minimum_value'' of 0 and a ''maximum_value'' of 86399
56  * (24 * 60 * 60 - 1). $
57  *
58  * minute & & integer_selector & o &
59  * This shows the seconds since the beginning of the current hour. The
60  * styled_widget should have a ''minimum_value'' of 0 and a ''maximum_value''
61  * of 3599 (60 * 60 - 1). $
62  *
63  * minute & & integer_selector & o &
64  * This shows the seconds since the beginning of the current minute. The
65  * styled_widget should have a ''minimum_value'' of 0 and a ''maximum_value''
66  * of 59. $
67  *
68  * clock & & styled_widget & o &
69  * A styled_widget which will have set three variables in its canvas:
70  * @* hour, the same value as the hour integer_selector.
71  * @* minute, the same value as the minute integer_selector.
72  * @* second, the same value as the second integer_selector.
73  * @- the styled_widget can then should the time in its own preferred
74  * format(s). $
75  * @end{table}
76  */
77 
78 REGISTER_DIALOG(debug_clock)
79 
80 void debug_clock::pre_show(window& window)
81 {
82  hour_percentage_ = find_widget<progress_bar>(
83  &window, "hour_percentage", false, false);
84  minute_percentage_ = find_widget<progress_bar>(
85  &window, "minute_percentage", false, false);
86  second_percentage_ = find_widget<progress_bar>(
87  &window, "second_percentage", false, false);
88 
89  hour_ = find_widget<integer_selector>(&window, "hour", false, false);
90  if(styled_widget *hour = dynamic_cast<styled_widget*>(hour_)) { //Note that the standard specifies that a dynamic cast of a null pointer is null
91  hour->set_active(false);
92  }
93  minute_ = find_widget<integer_selector>(&window, "minute", false, false);
94  if(styled_widget *minute = dynamic_cast<styled_widget*>(minute_)) {
95  minute->set_active(false);
96  }
97  second_ = find_widget<integer_selector>(&window, "second", false, false);
98  if(styled_widget *second = dynamic_cast<styled_widget*>(second_)) {
99  second->set_active(false);
100  }
101 
102  pane_ = find_widget<pane>(&window, "pane", false, false);
103 
104  clock_ = find_widget<styled_widget>(&window, "clock", false, false);
105 
106  signal_ = std::bind(&debug_clock::update_time, this, false);
107  connect_signal_on_draw(window, signal_);
108 
109  time_.set_current_time();
110  update_time(true);
111 }
112 
114 {
116 }
117 
118 void debug_clock::update_time(const bool force)
119 {
120  if(!time_.step() && !force) {
121  return;
122  }
123 
124  if(hour_percentage_) {
126  }
127  if(minute_percentage_) {
129  }
130  if(second_percentage_) {
132  }
133 
134  const int hour_stamp = time_.hour * 3600 + time_.minute * 60 + time_.second;
135  const int minute_stamp = time_.minute * 60 + time_.second;
136  const int second_stamp = time_.second;
137 
138  if(hour_) {
139  hour_->set_value(hour_stamp);
140  }
141  if(minute_) {
142  minute_->set_value(minute_stamp);
143  }
144  if(second_) {
145  second_->set_value(second_stamp);
146  }
147 
148  if(clock_) {
149  for(auto & canvas : clock_->get_canvases())
150  {
151  canvas.set_variable("hour", wfl::variant(hour_stamp));
152  canvas.set_variable("minute", wfl::variant(minute_stamp));
153  canvas.set_variable("second", wfl::variant(second_stamp));
154  }
155  clock_->set_is_dirty(true);
156  }
157 
158  const std::map<std::string, std::string> tags;
159  std::map<std::string, string_map> item_data;
161 
162  item["label"] = std::to_string(second_stamp);
163  item_data.emplace("time", item);
164 
165  if(pane_) {
166  pane_->create_item(item_data, tags);
167  }
168 }
169 
170 debug_clock::time::time() : hour(0), minute(0), second(0), millisecond(0)
171 {
172 }
173 
175 {
176  std::time_t now = ::std::time(nullptr);
177  std::tm* stamp = std::localtime(&now);
178 
179  hour = stamp->tm_hour;
180  minute = stamp->tm_min;
181  second = stamp->tm_sec;
182  millisecond = 0;
183 }
184 
185 bool debug_clock::time::step(const unsigned milliseconds)
186 {
187  millisecond += milliseconds;
188 
189  if(millisecond < 1000)
190  return false;
191 
192  millisecond -= 1000;
193  ++second;
194 
195  if(second < 60)
196  return true;
197 
198  second -= 60;
199  ++minute;
200 
201  if(minute < 60)
202  return true;
203 
204  minute -= 60;
205  ++hour;
206 
207  if(hour < 24)
208  return true;
209 
210  hour -= 24;
211 
212  return true;
213 }
214 
215 } // namespace dialogs
216 } // namespace gui2
integer_selector * minute_
An integer selector to display the total seconds this hour.
Definition: debug_clock.hpp:65
progress_bar * second_percentage_
Progress bar for displaying the seconds as a percentage.
Definition: debug_clock.hpp:59
integer_selector * hour_
An integer selector to display the total seconds.
Definition: debug_clock.hpp:62
void set_percentage(unsigned percentage)
progress_bar * minute_percentage_
Progress bar for displaying the minutes as a percentage.
Definition: debug_clock.hpp:56
Definition: video.hpp:31
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:138
void set_variable(const std::string &key, const wfl::variant &value)
Definition: canvas.hpp:171
This file contains the window object, this object is a top level container which has the event manage...
integer_selector * second_
An integer selector to display the seconds this minute.
Definition: debug_clock.hpp:68
unsigned second
The number of seconds.
event::signal_function signal_
The signal patched in the drawing routine.
Definition: debug_clock.hpp:76
Generic file dialog.
Definition: field-fwd.hpp:22
void update_time(const bool force)
The callback for the drawing routine.
This file contains the settings handling of the widget library.
void set_is_dirty(const bool is_dirty)
Definition: widget.cpp:463
Periodic redraw request.
Definition: handler.hpp:57
std::enable_if_t< has_key< set_event, E >::value > disconnect_signal(const signal_function &signal, const queue_position position=back_child)
Disconnect a signal for callback in set_event.
Definition: dispatcher.hpp:411
std::vector< canvas > & get_canvases()
Various uncategorised dialogs.
progress_bar * hour_percentage_
Progress bar for displaying the hours as a percentage.
Definition: debug_clock.hpp:53
window * get_window() const
Returns a pointer to the dialog&#39;s window.
A simple canvas which can be drawn upon.
Definition: canvas.hpp:41
unsigned minute
The number of minutes.
time time_
The `current&#39; time.
void set_current_time()
Sets the fields to the current time.
Clock to test the draw events.
Definition: debug_clock.hpp:33
std::map< std::string, t_string > string_map
Definition: widget.hpp:24
Base class for all visible items.
virtual void post_show(CVideo &video)
Inherited from modal_dialog.
virtual void set_value(int value)=0
Sets the selected value.
unsigned hour
The number of hours.
unsigned millisecond
The number of milliseconds.
bool step(const unsigned milliseconds=30)
Moves the clock x milliseconds forward.
styled_widget * clock_
A widget that can display the time.
Definition: debug_clock.hpp:73
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:63
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:371
void connect_signal_on_draw(dispatcher &dispatcher, const signal_function &signal)
Connects a signal handler for a callback when the widget is drawn.
Definition: dispatcher.cpp:253