The Battle for Wesnoth  1.19.5+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 
22 #include "gui/widgets/window.hpp"
23 #include "gui/widgets/pane.hpp"
25 
26 
27 #include <ctime>
28 
29 namespace gui2::dialogs
30 {
31 
32 REGISTER_DIALOG(debug_clock)
33 
35  : modeless_dialog(window_id())
36  , signal_()
37  , time_()
38 {
39  hour_percentage_ = find_widget<progress_bar>("hour_percentage", false, false);
40  minute_percentage_ = find_widget<progress_bar>("minute_percentage", false, false);
41  second_percentage_ = find_widget<progress_bar>("second_percentage", false, false);
42 
43  hour_ = find_widget<integer_selector>("hour", false, false);
44  if(styled_widget *hour = dynamic_cast<styled_widget*>(hour_)) { //Note that the standard specifies that a dynamic cast of a null pointer is null
45  hour->set_active(false);
46  }
47  minute_ = find_widget<integer_selector>("minute", false, false);
48  if(styled_widget *minute = dynamic_cast<styled_widget*>(minute_)) {
49  minute->set_active(false);
50  }
51  second_ = find_widget<integer_selector>("second", false, false);
52  if(styled_widget *second = dynamic_cast<styled_widget*>(second_)) {
53  second->set_active(false);
54  }
55 
56  pane_ = find_widget<pane>("pane", false, false);
57 
58  clock_ = find_widget<styled_widget>("clock", false, false);
59 
60  time_.set_current_time();
61  update_time(true);
62 }
63 
65 {
66  update_time(false);
68 }
69 
70 void debug_clock::update_time(const bool force)
71 {
72  if(!time_.step() && !force) {
73  return;
74  }
75 
76  if(hour_percentage_) {
78  }
79  if(minute_percentage_) {
81  }
82  if(second_percentage_) {
84  }
85 
86  const int hour_stamp = time_.hour * 3600 + time_.minute * 60 + time_.second;
87  const int minute_stamp = time_.minute * 60 + time_.second;
88  const int second_stamp = time_.second;
89 
90  if(hour_) {
91  hour_->set_value(hour_stamp);
92  }
93  if(minute_) {
94  minute_->set_value(minute_stamp);
95  }
96  if(second_) {
97  second_->set_value(second_stamp);
98  }
99 
100  if(clock_) {
101  for(auto & canvas : clock_->get_canvases())
102  {
103  canvas.set_variable("hour", wfl::variant(hour_stamp));
104  canvas.set_variable("minute", wfl::variant(minute_stamp));
105  canvas.set_variable("second", wfl::variant(second_stamp));
106  }
107  clock_->queue_redraw();
108  }
109 
110  const std::map<std::string, std::string> tags;
111  widget_data item_data;
112  widget_item item;
113 
114  item["label"] = std::to_string(second_stamp);
115  item_data.emplace("time", item);
116 
117  if(pane_) {
118  pane_->create_item(item_data, tags);
119  }
120 }
121 
122 debug_clock::time::time() : hour(0), minute(0), second(0), millisecond(0)
123 {
124 }
125 
127 {
128  std::time_t now = ::std::time(nullptr);
129  std::tm* stamp = std::localtime(&now);
130 
131  hour = stamp->tm_hour;
132  minute = stamp->tm_min;
133  second = stamp->tm_sec;
134  millisecond = 0;
135 }
136 
137 bool debug_clock::time::step(const unsigned milliseconds)
138 {
139  millisecond += milliseconds;
140 
141  if(millisecond < 1000)
142  return false;
143 
144  millisecond -= 1000;
145  ++second;
146 
147  if(second < 60)
148  return true;
149 
150  second -= 60;
151  ++minute;
152 
153  if(minute < 60)
154  return true;
155 
156  minute -= 60;
157  ++hour;
158 
159  if(hour < 24)
160  return true;
161 
162  hour -= 24;
163 
164  return true;
165 }
166 
167 } // 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
virtual void update() override
Update state and any parameters that may effect layout, or any of the later stages.
Definition: debug_clock.cpp:64
progress_bar * second_percentage_
Progress bar for displaying the seconds as a percentage.
Definition: debug_clock.hpp:46
progress_bar * minute_percentage_
Progress bar for displaying the minutes as a percentage.
Definition: debug_clock.hpp:43
integer_selector * minute_
An integer selector to display the total seconds this hour.
Definition: debug_clock.hpp:52
integer_selector * second_
An integer selector to display the seconds this minute.
Definition: debug_clock.hpp:55
void update_time(const bool force)
The callback for the drawing routine.
Definition: debug_clock.cpp:70
time time_
The ‘current’ time.
progress_bar * hour_percentage_
Progress bar for displaying the hours as a percentage.
Definition: debug_clock.hpp:40
integer_selector * hour_
An integer selector to display the total seconds.
Definition: debug_clock.hpp:49
styled_widget * clock_
A widget that can display the time.
Definition: debug_clock.hpp:60
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:117
void set_percentage(unsigned percentage)
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:464
This file contains the window object, this object is a top level container which has the event manage...
REGISTER_DIALOG(editor_edit_unit)
std::map< std::string, widget_item > widget_data
Definition: widget.hpp:36
std::map< std::string, t_string > widget_item
Definition: widget.hpp:33
unsigned minute
The number of minutes.
Definition: debug_clock.hpp:95
unsigned second
The number of seconds.
Definition: debug_clock.hpp:98
unsigned hour
The number of hours.
Definition: debug_clock.hpp:92
void set_current_time()
Sets the fields to the current time.
bool step(const unsigned milliseconds=30)
Moves the clock x milliseconds forward.