generated from Sigmarik/cpp-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
window_manager.h
162 lines (133 loc) · 4.26 KB
/
window_manager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#pragma once
#include <memory>
#include <string>
#include <vector>
#include "graphics/libs.h"
struct WindowManager {
/**
* @brief Construct the main program window and initialize graphics
* libraries.
*
* @param[in] width window width
* @param[in] height window height
* @param[in] title window title ("Unnamed" by default)
* @param[in] fullscreen weather the window should be in full screen mode
*
* @note In case of an error the window pointer remains `nullptr`, which can
* be checked using `WindowManager::valid()` call.
*/
static void init(size_t width, size_t height, const char* title = "Unnamed",
bool fullscreen = false);
/**
* @brief Destroy the main window
*
*/
static void terminate();
/**
* @brief Get the currently active window
*
* @return GLFWwindow*
*/
static GLFWwindow* get_active_window();
/**
* @brief Check weather the window has been initialized
*
* @return true
* @return false
*/
static bool valid() { return window_ != nullptr; }
struct SubtitleEntry;
friend SubtitleEntry;
/**
* @brief Set window title.
*
* @warning Should be preferred over `glfwSetWindowTitle`, as this function
* respects window subtitles
*
* @param[in] halt_update weather the title update should be halted
*/
static void set_title(const char* title, bool halt_update = false);
/**
* @brief Force-update the title
*
*/
static void update_title();
/**
* @brief Swap display buffers and update window title if needed.
*
*/
static void refresh();
struct SubtitleDataBlock {
std::string key = "UNDEF_KEY";
std::string value = "UNDEFINED";
double refresh_dt = 0.1;
};
/**
* @brief Add an entry to the window subtitle `window_title (subtitle_key:
* value, key: value)`.
*
* @param[in] key entry key
* @param[in] expiration_time time requirement between title updates for a
* parameter (0.1 by default)
* @return SubtitleEntry
*/
static SubtitleEntry add_subtitle_entry(const std::string& key,
double expiration_time = 0.1);
private:
static void notify_subtitle_stat_change();
static void request_title_update() { requires_title_update_ = true; }
static GLFWwindow* window_;
static std::vector<std::weak_ptr<SubtitleDataBlock>> subtitle_info_;
static std::string window_title_;
static double last_title_update_;
static double subtitle_update_dt_;
static bool requires_title_update_;
};
struct WindowManager::SubtitleEntry {
/**
* @brief Set displayed value of the entry
*
* @param[in] value
*
* @note Call after `hide` will have no effect on the title.
*/
void set_value(const std::string& value) {
if (!*this) return;
data_->value = value;
WindowManager::request_title_update();
}
/**
* @brief Set displayed value of the entry
*
* @tparam T argument type (should be convertible to string using
* std::to_string)
* @param[in] value
*
* @note Call after `hide` will have no effect on the title.
*/
template <class T>
requires requires(T&& t) { std::to_string(t); }
void set_value(T&& value) {
if (!*this) return;
data_->value = std::to_string(value);
WindowManager::request_title_update();
}
friend WindowManager;
/**
* @brief Remove entry from the window subtitle.
*
* @note Can be called however many times over.
* @note Does not affect correctness of `set_value` calls.
*/
void hide();
operator bool() const { return static_cast<bool>(data_); }
~SubtitleEntry();
private:
SubtitleEntry(const std::string& key, double refresh_dt);
SubtitleEntry(const SubtitleEntry& entry) = default;
SubtitleEntry(SubtitleEntry&& entry) = default;
SubtitleEntry& operator=(const SubtitleEntry& entry) = default;
SubtitleEntry& operator=(SubtitleEntry&& entry) = default;
std::weak_ptr<WindowManager::SubtitleDataBlock> get_data() { return data_; }
std::shared_ptr<WindowManager::SubtitleDataBlock> data_;
};