The Battle for Wesnoth  1.17.0-dev
debug_clock.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2021
3  by Mark de Wever <koraq@xs4all.nl>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
19 
23 #include "gui/widgets/window.hpp"
24 #include "gui/widgets/settings.hpp"
25 #include "gui/widgets/pane.hpp"
27 
28 #include <functional>
29 
30 #include <ctime>
31 
32 namespace gui2::dialogs
33 {
34 
35 REGISTER_DIALOG(debug_clock)
36 
37 void debug_clock::pre_show(window& window)
38 {
39  hour_percentage_ = find_widget<progress_bar>(
40  &window, "hour_percentage", false, false);
41  minute_percentage_ = find_widget<progress_bar>(
42  &window, "minute_percentage", false, false);
43  second_percentage_ = find_widget<progress_bar>(
44  &window, "second_percentage", false, false);
45 
46  hour_ = find_widget<integer_selector>(&window, "hour", false, false);
47  if(styled_widget *hour = dynamic_cast<styled_widget*>(hour_)) { //Note that the standard specifies that a dynamic cast of a null pointer is null
48  hour->set_active(false);
49  }
50  minute_ = find_widget<integer_selector>(&window, "minute", false, false);
51  if(styled_widget *minute = dynamic_cast<styled_widget*>(minute_)) {
52  minute->set_active(false);
53  }
54  second_ = find_widget<integer_selector>(&window, "second", false, false);
55  if(styled_widget *second = dynamic_cast<styled_widget*>(second_)) {
56  second->set_active(false);
57  }
58 
59  pane_ = find_widget<pane>(&window, "pane", false, false);
60 
61  clock_ = find_widget<styled_widget>(&window, "clock", false, false);
62 
63  signal_ = std::bind(&debug_clock::update_time, this, false);
64  connect_signal_on_draw(window, signal_);
65 
66  time_.set_current_time();
67  update_time(true);
68 }
69 
71 {
73 }
74 
75 void debug_clock::update_time(const bool force)
76 {
77  if(!time_.step() && !force) {
78  return;
79  }
80 
81  if(hour_percentage_) {
83  }
84  if(minute_percentage_) {
86  }
87  if(second_percentage_) {
89  }
90 
91  const int hour_stamp = time_.hour * 3600 + time_.minute * 60 + time_.second;
92  const int minute_stamp = time_.minute * 60 + time_.second;
93  const int second_stamp = time_.second;
94 
95  if(hour_) {
96  hour_->set_value(hour_stamp);
97  }
98  if(minute_) {
99  minute_->set_value(minute_stamp);
100  }
101  if(second_) {
102  second_->set_value(second_stamp);
103  }
104 
105  if(clock_) {
106  for(auto & canvas : clock_->get_canvases())
107  {
108  canvas.set_variable("hour", wfl::variant(hour_stamp));
109  canvas.set_variable("minute", wfl::variant(minute_stamp));
110  canvas.set_variable("second", wfl::variant(second_stamp));
111  }
112  clock_->set_is_dirty(true);
113  }
114 
115  const std::map<std::string, std::string> tags;
116  std::map<std::string, string_map> item_data;
118 
119  item["label"] = std::to_string(second_stamp);
120  item_data.emplace("time", item);
121 
122  if(pane_) {
123  pane_->create_item(item_data, tags);
124  }
125 }
126 
127 debug_clock::time::time() : hour(0), minute(0), second(0), millisecond(0)
128 {
129 }
130 
132 {
133  std::time_t now = ::std::time(nullptr);
134  std::tm* stamp = std::localtime(&now);
135 
136  hour = stamp->tm_hour;
137  minute = stamp->tm_min;
138  second = stamp->tm_sec;
139  millisecond = 0;
140 }
141 
142 bool debug_clock::time::step(const unsigned milliseconds)
143 {
144  millisecond += milliseconds;
145 
146  if(millisecond < 1000)
147  return false;
148 
149  millisecond -= 1000;
150  ++second;
151 
152  if(second < 60)
153  return true;
154 
155  second -= 60;
156  ++minute;
157 
158  if(minute < 60)
159  return true;
160 
161  minute -= 60;
162  ++hour;
163 
164  if(hour < 24)
165  return true;
166 
167  hour -= 24;
168 
169  return true;
170 }
171 
172 } // namespace dialogs
integer_selector * minute_
An integer selector to display the total seconds this hour.
Definition: debug_clock.hpp:81
progress_bar * second_percentage_
Progress bar for displaying the seconds as a percentage.
Definition: debug_clock.hpp:75
integer_selector * hour_
An integer selector to display the total seconds.
Definition: debug_clock.hpp:78
void set_variable(const std::string &key, wfl::variant &&value)
Definition: canvas.hpp:168
void set_percentage(unsigned percentage)
progress_bar * minute_percentage_
Progress bar for displaying the minutes as a percentage.
Definition: debug_clock.hpp:72
Definition: video.hpp:32
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:126
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:84
unsigned second
The number of seconds.
std::enable_if_t< is_general_event(E)> disconnect_signal(const signal_function &signal, const queue_position position=back_child)
Disconnect a signal for callback in set_event.
Definition: dispatcher.hpp:523
event::signal_function signal_
The signal patched in the drawing routine.
Definition: debug_clock.hpp:92
void update_time(const bool force)
The callback for the drawing routine.
Definition: debug_clock.cpp:75
This file contains the settings handling of the widget library.
void set_is_dirty(const bool is_dirty)
Definition: widget.cpp:466
Periodic redraw request.
Definition: handler.hpp:50
std::vector< canvas > & get_canvases()
progress_bar * hour_percentage_
Progress bar for displaying the hours as a percentage.
Definition: debug_clock.hpp:69
window * get_window() const
Returns a pointer to the dialog&#39;s window.
A simple canvas which can be drawn upon.
Definition: canvas.hpp:42
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:49
std::map< std::string, t_string > string_map
Definition: widget.hpp:26
Base class for all visible items.
virtual void post_show(CVideo &video)
Definition: debug_clock.cpp:70
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:89
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:65
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:410
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:192