The Battle for Wesnoth  1.17.23+dev
debug_clock.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2023
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 
38  : modeless_dialog(window_id())
39  , signal_()
40  , time_()
41 {
42  hour_percentage_ = find_widget<progress_bar>(
43  this, "hour_percentage", false, false);
44  minute_percentage_ = find_widget<progress_bar>(
45  this, "minute_percentage", false, false);
46  second_percentage_ = find_widget<progress_bar>(
47  this, "second_percentage", false, false);
48 
49  hour_ = find_widget<integer_selector>(this, "hour", false, false);
50  if(styled_widget *hour = dynamic_cast<styled_widget*>(hour_)) { //Note that the standard specifies that a dynamic cast of a null pointer is null
51  hour->set_active(false);
52  }
53  minute_ = find_widget<integer_selector>(this, "minute", false, false);
54  if(styled_widget *minute = dynamic_cast<styled_widget*>(minute_)) {
55  minute->set_active(false);
56  }
57  second_ = find_widget<integer_selector>(this, "second", false, false);
58  if(styled_widget *second = dynamic_cast<styled_widget*>(second_)) {
59  second->set_active(false);
60  }
61 
62  pane_ = find_widget<pane>(this, "pane", false, false);
63 
64  clock_ = find_widget<styled_widget>(this, "clock", false, false);
65 
66  time_.set_current_time();
67  update_time(true);
68 }
69 
71 {
72  update_time(false);
74 }
75 
76 void debug_clock::update_time(const bool force)
77 {
78  if(!time_.step() && !force) {
79  return;
80  }
81 
82  if(hour_percentage_) {
84  }
85  if(minute_percentage_) {
87  }
88  if(second_percentage_) {
90  }
91 
92  const int hour_stamp = time_.hour * 3600 + time_.minute * 60 + time_.second;
93  const int minute_stamp = time_.minute * 60 + time_.second;
94  const int second_stamp = time_.second;
95 
96  if(hour_) {
97  hour_->set_value(hour_stamp);
98  }
99  if(minute_) {
100  minute_->set_value(minute_stamp);
101  }
102  if(second_) {
103  second_->set_value(second_stamp);
104  }
105 
106  if(clock_) {
107  for(auto & canvas : clock_->get_canvases())
108  {
109  canvas.set_variable("hour", wfl::variant(hour_stamp));
110  canvas.set_variable("minute", wfl::variant(minute_stamp));
111  canvas.set_variable("second", wfl::variant(second_stamp));
112  }
113  clock_->queue_redraw();
114  }
115 
116  const std::map<std::string, std::string> tags;
117  widget_data item_data;
119 
120  item["label"] = std::to_string(second_stamp);
121  item_data.emplace("time", item);
122 
123  if(pane_) {
124  pane_->create_item(item_data, tags);
125  }
126 }
127 
128 debug_clock::time::time() : hour(0), minute(0), second(0), millisecond(0)
129 {
130 }
131 
133 {
134  std::time_t now = ::std::time(nullptr);
135  std::tm* stamp = std::localtime(&now);
136 
137  hour = stamp->tm_hour;
138  minute = stamp->tm_min;
139  second = stamp->tm_sec;
140  millisecond = 0;
141 }
142 
143 bool debug_clock::time::step(const unsigned milliseconds)
144 {
145  millisecond += milliseconds;
146 
147  if(millisecond < 1000)
148  return false;
149 
150  millisecond -= 1000;
151  ++second;
152 
153  if(second < 60)
154  return true;
155 
156  second -= 60;
157  ++minute;
158 
159  if(minute < 60)
160  return true;
161 
162  minute -= 60;
163  ++hour;
164 
165  if(hour < 24)
166  return true;
167 
168  hour -= 24;
169 
170  return true;
171 }
172 
173 } // namespace dialogs
A simple canvas which can be drawn upon.
Definition: canvas.hpp:45
void set_variable(const std::string &key, wfl::variant &&value)
Definition: canvas.hpp:154
Clock to test the draw events.
Definition: debug_clock.hpp:51
virtual void update() override
Update state and any parameters that may effect layout, or any of the later stages.
Definition: debug_clock.cpp:70
progress_bar * second_percentage_
Progress bar for displaying the seconds as a percentage.
Definition: debug_clock.hpp:63
progress_bar * minute_percentage_
Progress bar for displaying the minutes as a percentage.
Definition: debug_clock.hpp:60
integer_selector * minute_
An integer selector to display the total seconds this hour.
Definition: debug_clock.hpp:69
integer_selector * second_
An integer selector to display the seconds this minute.
Definition: debug_clock.hpp:72
void update_time(const bool force)
The callback for the drawing routine.
Definition: debug_clock.cpp:76
time time_
The ‘current’ time.
progress_bar * hour_percentage_
Progress bar for displaying the hours as a percentage.
Definition: debug_clock.hpp:57
integer_selector * hour_
An integer selector to display the total seconds.
Definition: debug_clock.hpp:66
styled_widget * clock_
A widget that can display the time.
Definition: debug_clock.hpp:77
The popup class shows windows that are shown non-modal.
virtual void set_value(int value)=0
Sets the selected value.
unsigned create_item(const widget_data &item_data, const std::map< std::string, std::string > &tags)
Creates a new item.
Definition: pane.cpp:118
void set_percentage(unsigned percentage)
Base class for all visible items.
std::vector< canvas > & get_canvases()
virtual void update()
Update state and any parameters that may effect layout, or any of the later stages.
void queue_redraw()
Indicates that this widget should be redrawn.
Definition: widget.cpp:456
This file contains the window object, this object is a top level container which has the event manage...
#define REGISTER_DIALOG(window_id)
Wrapper for REGISTER_DIALOG2.
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:35
std::map< std::string, t_string > widget_item
Definition: widget.hpp:32
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:414
This file contains the settings handling of the widget library.
unsigned minute
The number of minutes.
unsigned second
The number of seconds.
unsigned hour
The number of hours.
void set_current_time()
Sets the fields to the current time.
bool step(const unsigned milliseconds=30)
Moves the clock x milliseconds forward.