The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
debug_clock.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2010 - 2025
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/pane.hpp"
25#include "utils/general.hpp"
26
27#include <ctime>
28
29namespace gui2::dialogs
30{
31
32REGISTER_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
70void debug_clock::update_time(const bool force)
71{
72 if(!time_.step() && !force) {
73 return;
74 }
75
78 }
81 }
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 }
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
122debug_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{};
130 utils::localtime_r(&now, &stamp);
131
132 hour = stamp.tm_hour;
133 minute = stamp.tm_min;
134 second = stamp.tm_sec;
135 millisecond = 0;
136}
137
138bool debug_clock::time::step(const unsigned milliseconds)
139{
140 millisecond += milliseconds;
141
142 if(millisecond < 1000)
143 return false;
144
145 millisecond -= 1000;
146 ++second;
147
148 if(second < 60)
149 return true;
150
151 second -= 60;
152 ++minute;
153
154 if(minute < 60)
155 return true;
156
157 minute -= 60;
158 ++hour;
159
160 if(hour < 24)
161 return true;
162
163 hour -= 24;
164
165 return true;
166}
167
168} // 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:165
virtual void update() override
Update state and any parameters that may effect layout, or any of the later stages.
progress_bar * second_percentage_
Progress bar for displaying the seconds as a percentage.
progress_bar * minute_percentage_
Progress bar for displaying the minutes as a percentage.
integer_selector * minute_
An integer selector to display the total seconds this hour.
integer_selector * second_
An integer selector to display the seconds this minute.
void update_time(const bool force)
The callback for the drawing routine.
time time_
The ‘current’ time.
progress_bar * hour_percentage_
Progress bar for displaying the hours as a percentage.
integer_selector * hour_
An integer selector to display the total seconds.
styled_widget * clock_
A widget that can display the time.
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...
#define REGISTER_DIALOG(window_id)
Wrapper for REGISTER_DIALOG2.
std::map< std::string, widget_item > widget_data
Definition widget.hpp:36
std::map< std::string, t_string > widget_item
Definition widget.hpp:33
std::tm * localtime_r(const std::time_t *time, std::tm *result)
Definition general.hpp:175
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.