Repository navigation
Expand file tree
/
Copy pathimgui.hpp
More file actions
423 lines (341 loc) · 9.17 KB
/
Copy pathimgui.hpp
File metadata and controls
423 lines (341 loc) · 9.17 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/**
* @file liblava/app/imgui.hpp
* @brief ImGui integration
* @authors Lava Block OÜ and contributors
* @copyright Copyright (c) 2018-present, MIT License
*/
#pragma once
#include "liblava/block/render_pipeline.hpp"
#include "liblava/file.hpp"
#include "liblava/frame/input.hpp"
#include "liblava/resource/texture.hpp"
#include "liblava/util/layer.hpp"
// fwd
struct GLFWwindow;
struct GLFWcursor;
struct ImDrawData;
struct ImGuiStyle;
namespace lava {
/// Default ImGui font size
constexpr const r32 default_imgui_font_size = 18.f;
/**
* @brief ImGui integration
*/
struct imgui {
/// Pointer to imgui
using ptr = imgui*;
/**
* @brief Construct a new ImGui
*/
explicit imgui() = default;
/**
* @brief Construct a new ImGui
* @param window Window for ImGui
*/
explicit imgui(GLFWwindow* window) {
setup(window);
}
/**
* @brief Destroy the ImGui
*/
~imgui() {
destroy();
}
/**
* @brief ImGui icon font settings
*/
struct icon_font {
/// Icon font data
data font_data;
/// Icon range begin
ui16 range_begin = 0;
/// Icon range end
ui16 range_end = 0;
/// Default icon font size
r32 size = default_imgui_font_size;
};
/**
* @brief ImGui font settings
*/
struct font {
/// Const font reference
using ref = font const&;
/// Font file
string file;
/// Font size
r32 size = 21.f;
/// Font icon file
string icon_file;
/// Font icon size
r32 icon_size = 21.f;
/// Font range begin
ui16 icon_range_begin = 0;
/// Font range end
ui16 icon_range_end = 0;
};
/**
* @brief ImGui configuration
*/
struct config {
/// Font data
data font_data;
/// Font size
r32 font_size = default_imgui_font_size;
/// Font style
std::shared_ptr<ImGuiStyle> style;
/// Font icon settings
icon_font icon;
/// ImGui state file path
std::filesystem::path ini_file_dir;
/// ImGuiConfigFlags
i32 flags = 0;
};
/**
* @brief Set up ImGui with configuration
* @param window Target window
* @param config Configuration
*/
void setup(GLFWwindow* window, config config);
/**
* @brief Set up default ImGui
* @param win Target window
*/
void setup(GLFWwindow* win) {
setup(win, config());
}
/**
* @brief Create pipeline for ImGui
* @param pipeline Render pipeline
* @param max_frames Number of frames
* @return Create was successful or failed
*/
bool create(render_pipeline::s_ptr pipeline, index max_frames);
/**
* @brief Create pipeline for ImGui with device
* @param dev Vulkan device
* @param frames Number of frames
* @param pipeline_cache Pipeline cache
* @return Create was successful or failed
*/
bool create(device::ptr dev,
index frames,
VkPipelineCache pipeline_cache) {
return create(render_pipeline::make(dev, pipeline_cache),
frames);
}
/**
* @brief Create pipeline for ImGui with device and render pass
* @param dev Vulkan device
* @param frames Number of frames
* @param pass Render pass
* @param pipeline_cache Pipeline cache
* @return Create was successful or failed
*/
bool create(device::ptr dev,
index frames,
VkRenderPass pass,
VkPipelineCache pipeline_cache = 0) {
if (!create(dev, frames, pipeline_cache))
return false;
return m_pipeline->create(pass);
}
/**
* @brief Upload font texture
* @param texture Texture to upload
* @return Upload was successful or failed
*/
bool upload_fonts(texture::s_ptr texture);
/**
* @brief Destroy ImGui
*/
void destroy();
/**
* @brief Check if ImGui is ready
* @return ImGui is ready or not
*/
bool ready() const {
return m_initialized;
}
/**
* @brief Get the pipeline
* @return render_pipeline::s_ptr Render pipeline
*/
render_pipeline::s_ptr get_pipeline() {
return m_pipeline;
}
/// Draw function
using draw_func = std::function<void()>;
/// Function called on ImGui draw
draw_func on_draw;
/// Layer list
layer_list layers;
/**
* @brief Check if mouse capture is active
* @return Capture is active or not
*/
bool capture_mouse() const;
/**
* @brief Check if keyboard capture is active
* @return Capture is active or not
*/
bool capture_keyboard() const;
/**
* @brief Set ImGui active
* @param value Active state
*/
void set_active(bool value = true) {
m_active = value;
}
/**
* @brief Check if ImGui is activated
* @return ImGui is active or not
*/
bool activated() const {
return m_active;
}
/**
* @brief Togge active state
*/
void toggle() {
m_active = !m_active;
}
/**
* @brief Set the ini file
* @param dir Path for file
*/
void set_ini_file(std::filesystem::path dir);
/**
* @brief Get the ini file
* @return fs::path Path of file
*/
std::filesystem::path get_ini_file() const {
return std::filesystem::path(m_ini_file);
}
/**
* @brief Convert style to sRGB
*/
void convert_style_to_srgb();
/**
* @brief Get the input callback
* @return input_callback const& Input callback
*/
input_callback const& get_input_callback() const {
return m_callback;
}
private:
/**
* @brief Handle key event
* @param key Key
* @param scancode Scan code
* @param action Action
* @param mods Mods
*/
void handle_key_event(i32 key, i32 scancode, i32 action, i32 mods);
/**
* @brief Handle mouse button event
* @param button Button
* @param action Action
* @param mods Mods
*/
void handle_mouse_button_event(i32 button, i32 action, i32 mods);
/**
* @brief Handle scroll event
* @param x_offset X offset
* @param y_offset Y offset
*/
void handle_scroll_event(r64 x_offset, r64 y_offset);
/**
* @brief Prepare draw lists
* @param draw_data Draw data
*/
void prepare_draw_lists(ImDrawData* draw_data);
/**
* @brief Render draw lists
* @param cmd_buf Vulkan command buffer
*/
void render_draw_lists(VkCommandBuffer cmd_buf);
/**
* @brief Invalidate device objects
*/
void invalidate_device_objects();
/**
* @brief Update mouse position and buttons
*/
void update_mouse_pos_and_buttons();
/**
* @brief Update mouse cursor
*/
void update_mouse_cursor();
/**
* @brief Start new frame
*/
void new_frame();
/**
* @brief Render ImGui
* @param cmd_buf Vulkan command buffer
*/
void render(VkCommandBuffer cmd_buf);
/// Vulkan device
device::ptr m_device = nullptr;
// Initialized state
bool m_initialized = false;
/// Render pipeline
render_pipeline::s_ptr m_pipeline;
/// Pipeline layout
pipeline_layout::s_ptr m_layout;
/// Buffer memory alignment
size_t m_buffer_memory_alignment = 256;
/// Current frame index
index m_frame = 0;
/// Number of frames
index m_max_frames = 4;
/// Vertex buffer list
buffer::s_list m_vertex_buffers;
/// Index buffer list
buffer::s_list m_index_buffers;
/// Vulkan descriptor
descriptor::s_ptr m_descriptor;
/// Vulkan descriptor pool
descriptor::pool::s_ptr m_descriptor_pool;
/// Vulkan descriptor set
VkDescriptorSet m_descriptor_set = VK_NULL_HANDLE;
/// Target window
GLFWwindow* m_window = nullptr;
/// Mouse just pressed state
bool m_mouse_just_pressed[5] = {false, false, false, false, false};
/// Current time
r64 m_current_time = 0.0;
/// Mouse cursors
std::vector<GLFWcursor*> m_mouse_cursors;
/// File for state (path)
string m_ini_file;
/// Active state
bool m_active = true;
/// Input callback
input_callback m_callback;
/// Font icons range
std::array<ui16, 3> m_icons_range;
};
/**
* @brief Set up ImGui font
* @param config ImGui configuration
* @param font ImGui font
*/
void setup_imgui_font(imgui::config& config,
imgui::font::ref font);
/**
* @brief Set up imgui font icons
* @param font Imgui font
* @param filename Font icon file name
* @param min Min range
* @param max Max range
*/
void setup_imgui_font_icons(imgui::font& font,
string filename,
ui16 min, ui16 max);
/**
* @brief ImGui left spacing with top offset
* @param top Top offset
*/
void imgui_left_spacing(ui32 top = 1);
} // namespace lava