The Battle for Wesnoth  1.15.12+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 <functional>
28 
29 #include <ctime>
30 
31 namespace gui2::dialogs
32 {
33 
34 REGISTER_DIALOG(debug_clock)
35 
36 void debug_clock::pre_show(window& window)
37 {
38  hour_percentage_ = find_widget<progress_bar>(
39  &window, "hour_percentage", false, false);
40  minute_percentage_ = find_widget<progress_bar>(
41  &window, "minute_percentage", false, false);
42  second_percentage_ = find_widget<progress_bar>(
43  &window, "second_percentage", false, false);
44 
45  hour_ = find_widget<integer_selector>(&window, "hour", false, false);
46  if(styled_widget *hour = dynamic_cast<styled_widget*>(hour_)) { //Note that the standard specifies that a dynamic cast of a null pointer is null
47  hour->set_active(false);
48  }
49  minute_ = find_widget<integer_selector>(&window, "minute", false, false);
50  if(styled_widget *minute = dynamic_cast<styled_widget*>(minute_)) {
51  minute->set_active(false);
52  }
53  second_ = find_widget<integer_selector>(&window, "second", false, false);
54  if(styled_widget *second = dynamic_cast<styled_widget*>(second_)) {
55  second->set_active(false);
56  }
57 
58  pane_ = find_widget<pane>(&window, "pane", false, false);
59 
60  clock_ = find_widget<styled_widget>(&window, "clock", false, false);
61 
62  signal_ = std::bind(&debug_clock::update_time, this, false);
63  connect_signal_on_draw(window, signal_);
64 
65  time_.set_current_time();
66  update_time(true);
67 }
68 
70 {
72 }
73 
74 void debug_clock::update_time(const bool force)
75 {
76  if(!time_.step() && !force) {
77  return;
78  }
79 
80  if(hour_percentage_) {
82  }
83  if(minute_percentage_) {
85  }
86  if(second_percentage_) {
88  }
89 
90  const int hour_stamp = time_.hour * 3600 + time_.minute * 60 + time_.second;
91  const int minute_stamp = time_.minute * 60 + time_.second;
92  const int second_stamp = time_.second;
93 
94  if(hour_) {
95  hour_->set_value(hour_stamp);
96  }
97  if(minute_) {
98  minute_->set_value(minute_stamp);
99  }
100  if(second_) {
101  second_->set_value(second_stamp);
102  }
103 
104  if(clock_) {
105  for(auto & canvas : clock_->get_canvases())
106  {
107  canvas.set_variable("hour", wfl::variant(hour_stamp));
108  canvas.set_variable("minute", wfl::variant(minute_stamp));
109  canvas.set_variable("second", wfl::variant(second_stamp));
110  }
111  clock_->set_is_dirty(true);
112  }
113 
114  const std::map<std::string, std::string> tags;
115  std::map<std::string, string_map> item_data;
117 
118  item["label"] = std::to_string(second_stamp);
119  item_data.emplace("time", item);
120 
121  if(pane_) {
122  pane_->create_item(item_data, tags);
123  }
124 }
125 
126 debug_clock::time::time() : hour(0), minute(0), second(0), millisecond(0)
127 {
128 }
129 
131 {
132  std::time_t now = ::std::time(nullptr);
133  std::tm* stamp = std::localtime(&now);
134 
135  hour = stamp->tm_hour;
136  minute = stamp->tm_min;
137  second = stamp->tm_sec;
138  millisecond = 0;
139 }
140 
141 bool debug_clock::time::step(const unsigned milliseconds)
142 {
143  millisecond += milliseconds;
144 
145  if(millisecond < 1000)
146  return false;
147 
148  millisecond -= 1000;
149  ++second;
150 
151  if(second < 60)
152  return true;
153 
154  second -= 60;
155  ++minute;
156 
157  if(minute < 60)
158  return true;
159 
160  minute -= 60;
161  ++hour;
162 
163  if(hour < 24)
164  return true;
165 
166  hour -= 24;
167 
168  return true;
169 }
170 
171 } // namespace dialogs
integer_selector * minute_
An integer selector to display the total seconds this hour.
Definition: debug_clock.hpp:80
progress_bar * second_percentage_
Progress bar for displaying the seconds as a percentage.
Definition: debug_clock.hpp:74
integer_selector * hour_
An integer selector to display the total seconds.
Definition: debug_clock.hpp:77
void set_percentage(unsigned percentage)
progress_bar * minute_percentage_
Progress bar for displaying the minutes as a percentage.
Definition: debug_clock.hpp:71
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:125
void set_variable(const std::string &key, const wfl::variant &value)
Definition: canvas.hpp:167
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:83
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:522
event::signal_function signal_
The signal patched in the drawing routine.
Definition: debug_clock.hpp:91
void update_time(const bool force)
The callback for the drawing routine.
Definition: debug_clock.cpp:74
This file contains the settings handling of the widget library.
void set_is_dirty(const bool is_dirty)
Definition: widget.cpp:465
Periodic redraw request.
Definition: handler.hpp:49
std::vector< canvas > & get_canvases()
progress_bar * hour_percentage_
Progress bar for displaying the hours as a percentage.
Definition: debug_clock.hpp:68
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:48
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:69
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:88
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:64
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:409
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:191