The Battle for Wesnoth  1.19.0-dev
debug_clock.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2024
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/pane.hpp"
26 
27 
28 #include <ctime>
29 
30 namespace gui2::dialogs
31 {
32 
33 REGISTER_DIALOG(debug_clock)
34 
36  : modeless_dialog(window_id())
37  , signal_()
38  , time_()
39 {
40  hour_percentage_ = find_widget<progress_bar>(
41  this, "hour_percentage", false, false);
42  minute_percentage_ = find_widget<progress_bar>(
43  this, "minute_percentage", false, false);
44  second_percentage_ = find_widget<progress_bar>(
45  this, "second_percentage", false, false);
46 
47  hour_ = find_widget<integer_selector>(this, "hour", false, false);
48  if(styled_widget *hour = dynamic_cast<styled_widget*>(hour_)) { //Note that the standard specifies that a dynamic cast of a null pointer is null
49  hour->set_active(false);
50  }
51  minute_ = find_widget<integer_selector>(this, "minute", false, false);
52  if(styled_widget *minute = dynamic_cast<styled_widget*>(minute_)) {
53  minute->set_active(false);
54  }
55  second_ = find_widget<integer_selector>(this, "second", false, false);
56  if(styled_widget *second = dynamic_cast<styled_widget*>(second_)) {
57  second->set_active(false);
58  }
59 
60  pane_ = find_widget<pane>(this, "pane", false, false);
61 
62  clock_ = find_widget<styled_widget>(this, "clock", false, false);
63 
64  time_.set_current_time();
65  update_time(true);
66 }
67 
69 {
70  update_time(false);
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_->queue_redraw();
112  }
113 
114  const std::map<std::string, std::string> tags;
115  widget_data 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
A simple canvas which can be drawn upon.
Definition: canvas.hpp:44
void set_variable(const std::string &key, wfl::variant &&value)
Definition: canvas.hpp:153
Clock to test the draw events.
Definition: debug_clock.hpp:50
virtual void update() override
Update state and any parameters that may effect layout, or any of the later stages.
Definition: debug_clock.cpp:68
progress_bar * second_percentage_
Progress bar for displaying the seconds as a percentage.
Definition: debug_clock.hpp:62
progress_bar * minute_percentage_
Progress bar for displaying the minutes as a percentage.
Definition: debug_clock.hpp:59
integer_selector * minute_
An integer selector to display the total seconds this hour.
Definition: debug_clock.hpp:68
integer_selector * second_
An integer selector to display the seconds this minute.
Definition: debug_clock.hpp:71
void update_time(const bool force)
The callback for the drawing routine.
Definition: debug_clock.cpp:74
time time_
The ‘current’ time.
progress_bar * hour_percentage_
Progress bar for displaying the hours as a percentage.
Definition: debug_clock.hpp:56
integer_selector * hour_
An integer selector to display the total seconds.
Definition: debug_clock.hpp:65
styled_widget * clock_
A widget that can display the time.
Definition: debug_clock.hpp:76
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:116
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:455
This file contains the window object, this object is a top level container which has the event manage...
REGISTER_DIALOG(tod_new_schedule)
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:34
std::map< std::string, t_string > widget_item
Definition: widget.hpp:31
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:412
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.