-
Notifications
You must be signed in to change notification settings - Fork 20
/
ImGuiPlatform.cpp
521 lines (390 loc) · 18.2 KB
/
ImGuiPlatform.cpp
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*******************************
Copyright (c) 2016-2024 Grégoire Angerand
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
**********************************/
#include "ImGuiPlatform.h"
#include "Settings.h"
#include <editor/utils/ui.h>
#include <yave/graphics/commands/CmdBufferRecorder.h>
#include <yave/graphics/framebuffer/Framebuffer.h>
#include <yave/window/Monitor.h>
#include <yave/utils/color.h>
#include <y/io2/File.h>
#include <y/utils/log.h>
#include <y/utils/format.h>
#include <deque>
namespace editor {
ImGuiPlatform* imgui_platform() {
return ImGuiPlatform::instance();
}
// ---------------------------------------------- SETUP HELPERS ----------------------------------------------
static void setup_style() {
auto& style = ImGui::GetStyle();
ImVec4* colors = style.Colors;
const math::Vec4 none = {};
auto rgb = [=](u8 r, u8 g, u8 b, float alpha = 1.0f) {
const math::Vec3 linear = (math::Vec3(r, g, b) / 255.0f).saturated();
return math::Vec4(sRGB_to_linear(linear), alpha);
};
auto grey = [=](u8 g, float alpha = 1.0f) {
return rgb(g, g, g, alpha);
};
for(usize i = 0; i != ImGuiCol_COUNT; ++i) {
colors[i] = rgb(255, 0, 0);
}
auto stl = [&](u8 base, float alpha = 1.0f) {
return rgb(base, base + 3, base + 7, alpha);
};
const math::Vec4 bg = stl(33); // rgb(33, 36, 40);
const math::Vec4 child = stl(44); // rgb(44, 47, 52);
const math::Vec4 highlight = rgb(0, 112, 224);
colors[ImGuiCol_BorderShadow] = none;
colors[ImGuiCol_FrameBgActive] = none;
colors[ImGuiCol_Tab] = none;
colors[ImGuiCol_TabUnfocused] = none;
colors[ImGuiCol_DockingEmptyBg] = none;
colors[ImGuiCol_ModalWindowDimBg] = none;
colors[ImGuiCol_TableRowBg] = none;
colors[ImGuiCol_FrameBgHovered] = none;
colors[ImGuiCol_TableRowBgAlt] = none;
colors[ImGuiCol_Border] = none;
colors[ImGuiCol_Text] = rgb(177, 183, 190);
colors[ImGuiCol_TextDisabled] = rgb(107, 123, 139);
colors[ImGuiCol_SliderGrabActive] = stl(143); // rgb(143, 146, 150);
colors[ImGuiCol_ScrollbarGrabActive] = stl(143); // rgb(143, 146, 150);
colors[ImGuiCol_ScrollbarGrabHovered] = stl(143); // rgb(143, 146, 150);
colors[ImGuiCol_ResizeGripActive] = stl(143); // rgb(143, 146, 150);
colors[ImGuiCol_ResizeGripHovered] = stl(143); // rgb(143, 146, 150);
colors[ImGuiCol_SliderGrab] = stl(57); // rgb(57, 60, 65);
colors[ImGuiCol_ScrollbarGrab] = stl(57); // rgb(57, 60, 65);
colors[ImGuiCol_ResizeGrip] = stl(57); // rgb(57, 60, 65);
colors[ImGuiCol_Button] = stl(63); // rgb(63, 68, 76);
colors[ImGuiCol_ButtonHovered] = stl(56); // rgb(56, 60, 68);
colors[ImGuiCol_Header] = stl(56); // rgb(56, 60, 68);
colors[ImGuiCol_ButtonActive] = stl(60); // rgb(60, 65, 72);
colors[ImGuiCol_TableBorderLight] = stl(60);
colors[ImGuiCol_TableBorderStrong] = stl(60);
colors[ImGuiCol_PopupBg] = stl(30, 0.9f);
colors[ImGuiCol_TabActive] = child;
colors[ImGuiCol_TabUnfocusedActive] = child;
colors[ImGuiCol_ChildBg] = child;
colors[ImGuiCol_WindowBg] = child;
colors[ImGuiCol_TabHovered] = child;
colors[ImGuiCol_ScrollbarBg] = bg;
colors[ImGuiCol_Separator] = bg;
colors[ImGuiCol_TitleBg] = bg;
colors[ImGuiCol_TitleBgActive] = bg;
colors[ImGuiCol_TitleBgCollapsed] = bg;
colors[ImGuiCol_MenuBarBg] = bg;
colors[ImGuiCol_FrameBg] = bg;
colors[ImGuiCol_TableHeaderBg] = bg;
colors[ImGuiCol_DockingPreview] = bg;
colors[ImGuiCol_CheckMark] = highlight;
colors[ImGuiCol_DragDropTarget] = highlight;
colors[ImGuiCol_NavWindowingHighlight] = highlight;
colors[ImGuiCol_PlotHistogram] = highlight;
colors[ImGuiCol_PlotHistogramHovered] = highlight;
colors[ImGuiCol_PlotLines] = highlight;
colors[ImGuiCol_PlotLinesHovered] = highlight;
colors[ImGuiCol_HeaderActive] = highlight;
colors[ImGuiCol_SeparatorActive] = highlight;
colors[ImGuiCol_HeaderHovered] = math::lerp(child, highlight, 0.25f);
colors[ImGuiCol_SeparatorHovered] = math::lerp(child, highlight, 0.25f);
colors[ImGuiCol_TextSelectedBg] = math::lerp(child, highlight, 0.25f);
// colors[ImGuiCol_NavWindowingHighlight] = rgb(128, 168, 224);
// colors[ImGuiCol_NavWindowingDimBg] = grey(128, 0.75f);
style.WindowPadding = ImVec2(4, 4);
style.FramePadding = ImVec2(6, 6);
style.ItemSpacing = ImVec2(4, 2);
style.CellPadding = ImVec2(4, 3);
style.ScrollbarSize = 12;
style.ScrollbarRounding = 12;
style.IndentSpacing = 12;
style.FrameBorderSize = 1;
style.WindowBorderSize = 0;
style.ChildBorderSize = 0;
style.PopupBorderSize = 0;
style.PopupRounding = 0;
style.FrameRounding = 3;
style.GrabRounding = 0;
style.WindowRounding = 0;
style.ChildRounding = 0;
style.TabBorderSize = 0;
style.TabRounding = 0;
style.DisabledAlpha = 0.25f;
style.WindowMenuButtonPosition = ImGuiDir_Right;
if(ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable) {
style.WindowRounding = 0.0f;
style.Colors[ImGuiCol_WindowBg].w = 1.0f;
}
}
static void setup_imgui_dockspace() {
ImGuiViewport* viewport = ImGui::GetMainViewport();
ImGui::DockSpaceOverViewport(viewport);
}
static void setup_config_files(ImGuiIO& io) {
io.IniFilename = "editor.ini";
io.LogFilename = "editor_logs.txt";
if(io2::File::open("../editor.ini").is_ok()) {
io.IniFilename = "../editor.ini";
}
}
static void setup_backend_flags(ImGuiIO& io, bool multi_viewport) {
io.BackendPlatformName = "Yave ImGuiPlatform";
io.BackendRendererName = "Yave";
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
io.ConfigDockingWithShift = false;
io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports | ImGuiBackendFlags_RendererHasViewports;
io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos;
if(multi_viewport) {
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;
}
}
static CursorShape to_cursor_shape(ImGuiMouseCursor cursor) {
switch (cursor){
case ImGuiMouseCursor_None:
return CursorShape::None;
case ImGuiMouseCursor_Arrow:
return CursorShape::Arrow;
case ImGuiMouseCursor_TextInput:
return CursorShape::TextInput;
case ImGuiMouseCursor_ResizeAll:
return CursorShape::ResizeAll;
case ImGuiMouseCursor_ResizeEW:
return CursorShape::ResizeEW;
case ImGuiMouseCursor_ResizeNS:
return CursorShape::ResizeNS;
case ImGuiMouseCursor_ResizeNESW:
return CursorShape::ResizeNESW;
case ImGuiMouseCursor_ResizeNWSE:
return CursorShape::ResizeNWSE;
case ImGuiMouseCursor_Hand:
return CursorShape::Hand;
case ImGuiMouseCursor_NotAllowed:
return CursorShape::NotAllowed;
default:
return CursorShape::Arrow;
}
}
static void discover_monitors(ImGuiPlatformIO& platform) {
y_profile();
platform.Monitors.clear();
auto monitors = Monitor::monitors();
std::sort(monitors.begin(), monitors.end(), [](const Monitor& a, const Monitor& b) { return b.is_primary < a.is_primary; });
std::transform(monitors.begin(), monitors.end(), std::back_inserter(platform.Monitors), [](const Monitor& monitor) {
ImGuiPlatformMonitor imgui_mon;
imgui_mon.MainPos = monitor.position;
imgui_mon.MainSize = monitor.size;
imgui_mon.WorkPos = monitor.work_position;
imgui_mon.WorkSize = monitor.work_size;
return imgui_mon;
});
}
// ---------------------------------------------- EVENT HANDLER ----------------------------------------------
class ImGuiEventHandler : public EventHandler {
public:
ImGuiEventHandler(Window* window) : _window(window) {
}
void mouse_moved(const math::Vec2i& pos) override {
const math::Vec2 win_pos = to_window(pos);
ImGui::GetIO().AddMousePosEvent(win_pos.x(), win_pos.y());
}
void mouse_pressed(const math::Vec2i& pos, MouseButton button) override {
mouse_moved(pos);
ImGui::GetIO().AddMouseButtonEvent(to_imgui_button(button), true);
}
void mouse_released(const math::Vec2i& pos, MouseButton button) override {
mouse_moved(pos);
ImGui::GetIO().AddMouseButtonEvent(to_imgui_button(button), false);
}
void mouse_wheel(i32 vdelta, i32 hdelta) override {
ImGui::GetIO().AddMouseWheelEvent(float(hdelta), float(vdelta));
}
void char_input(u32 character) override {
ImGui::GetIO().AddInputCharacter(character);
}
void key_pressed(Key key) override {
ImGui::GetIO().AddKeyEvent(to_imgui_key(key), true);
}
void key_released(Key key) override {
ImGui::GetIO().AddKeyEvent(to_imgui_key(key), false);
}
private:
math::Vec2i to_window(const math::Vec2i& pos) const {
return ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable
? pos + _window->position() : pos;
}
Window* _window = nullptr;
};
// ---------------------------------------------- PLATFORM WINDOW ----------------------------------------------
ImGuiPlatform::PlatformWindow::PlatformWindow(ImGuiPlatform* parent, Window::Flags flags) :
platform(parent),
window({1280, 768}, "Yave Editor", flags),
swapchain(&window),
event_handler(std::make_unique<ImGuiEventHandler>(&window)) {
window.set_event_handler(event_handler.get());
window.show();
}
bool ImGuiPlatform::PlatformWindow::render(ImGuiViewport* viewport) {
y_profile();
if(!window.update()) {
return false;
}
if(const auto r = swapchain.next_frame()) {
const FrameToken& token = r.unwrap();
CmdBufferRecorder recorder = create_disposable_cmd_buffer();
{
Framebuffer framebuffer(token.image_view);
RenderPassRecorder pass = recorder.bind_framebuffer(framebuffer);
platform->_renderer->render(viewport->DrawData, pass);
}
swapchain.present(token, std::move(recorder), command_queue());
}
return true;
}
// ---------------------------------------------- PLATFORM ----------------------------------------------
ImGuiPlatform* ImGuiPlatform::_instance = nullptr;
ImGuiPlatform* ImGuiPlatform::instance() {
y_debug_assert(_instance);
return _instance;
}
ImGuiPlatform::ImGuiPlatform(bool multi_viewport) {
y_profile();
y_always_assert(_instance == nullptr, "ImGuiPlatform instance already exists");
_instance = this;
ImGui::CreateContext();
setup_style();
auto& io = ImGui::GetIO();
y_always_assert(io.BackendPlatformUserData == nullptr, "ImGui already has a platform backend");
io.BackendPlatformUserData = this;
io.ConfigWindowsMoveFromTitleBarOnly = true;
setup_config_files(io);
setup_backend_flags(io, multi_viewport);
if(multi_viewport) {
auto& platform = ImGui::GetPlatformIO();
Y_TODO(do every frame)
discover_monitors(platform);
platform.Platform_CreateWindow = [](ImGuiViewport* vp) {
ImGuiPlatform* self = get_platform();
vp->PlatformHandle = self->_windows.emplace_back(std::make_unique<PlatformWindow>(self, Window::NoDecoration)).get();
};
platform.Platform_DestroyWindow = [](ImGuiViewport* vp) { get_platform()->close_window(get_platform_window(vp)); };
platform.Platform_ShowWindow = [](ImGuiViewport* vp) { get_window(vp)->show(); };
platform.Platform_SetWindowPos = [](ImGuiViewport* vp, ImVec2 pos) { get_window(vp)->set_position(pos); };
platform.Platform_SetWindowSize = [](ImGuiViewport* vp, ImVec2 size) { get_window(vp)->set_size(size); };
platform.Platform_GetWindowPos = [](ImGuiViewport* vp) { return ImVec2(get_window(vp)->position()); };
platform.Platform_GetWindowSize = [](ImGuiViewport* vp) { return ImVec2(get_window(vp)->size()); };
platform.Platform_SetWindowTitle = [](ImGuiViewport* vp, const char* title) { get_window(vp)->set_title(title); };
platform.Platform_SetWindowFocus = [](ImGuiViewport* vp) { get_window(vp)->focus(); };
platform.Platform_GetWindowFocus = [](ImGuiViewport* vp) { return get_window(vp)->has_focus(); };
platform.Platform_GetWindowMinimized = [](ImGuiViewport* vp) {return get_window(vp)->is_minimized(); };
platform.Renderer_RenderWindow = [](ImGuiViewport* vp, void*) { get_platform_window(vp)->render(vp); };
}
_renderer = std::make_unique<ImGuiRenderer>();
_main_window = std::make_unique<PlatformWindow>(this, Window::Resizable);
ImGuiViewport* viewport = ImGui::GetMainViewport();
viewport->PlatformHandle = _main_window.get();
}
ImGuiPlatform::~ImGuiPlatform() {
y_always_assert(_instance == this, "ImGuiPlatform instance has already been deleted");
ImGui::DestroyContext();
_instance = nullptr;
}
const ImGuiRenderer* ImGuiPlatform::renderer() const {
return _renderer.get();
}
Window* ImGuiPlatform::main_window() {
return &_main_window->window;
}
void ImGuiPlatform::exec(OnGuiFunc func) {
for(;;) {
{
y_profile_zone("frame rate cap");
const float max_fps = app_settings().editor.max_fps;
do {
if(!_main_window->window.update()) {
return;
}
} while(max_fps > 0.0f && _frame_timer.elapsed().to_secs() < 1.0f / max_fps);
}
y_profile_zone("exec once");
ImGui::GetIO().DeltaTime = std::max(math::epsilon<float>, float(_frame_timer.reset().to_secs()));
ImGui::GetIO().DisplaySize = _main_window->window.size();
Window::set_cursor_shape(ImGui::GetIO().MouseDrawCursor ? CursorShape::None : to_cursor_shape(ImGui::GetMouseCursor()));
if(const auto r = _main_window->swapchain.next_frame()) {
const FrameToken& token = r.unwrap();
{
y_profile_zone("imgui");
ImGui::NewFrame();
setup_imgui_dockspace();
if(_demo_window) {
ImGui::PushStyleColor(ImGuiCol_MenuBarBg, 0);
ImGui::ShowDemoWindow(&_demo_window);
ImGui::PopStyleColor();
}
if(func) {
func();
}
ImGui::Render();
}
CmdBufferRecorder recorder = create_disposable_cmd_buffer();
{
y_profile_zone("main window");
Framebuffer framebuffer(token.image_view);
RenderPassRecorder pass = recorder.bind_framebuffer(framebuffer);
_renderer->render(ImGui::GetDrawData(), pass);
}
if(ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable) {
y_profile_zone("secondary windows");
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
}
_main_window->swapchain.present(token, std::move(recorder), command_queue());
UiTexture::clear_all();
}
}
}
void ImGuiPlatform::close_window(PlatformWindow* window) {
window->window.close();
while(window->window.update()) {
// nothing
}
for(auto it = _windows.begin(); it != _windows.end(); ++it) {
if(it->get() == window) {
_windows.erase_unordered(it);
return;
}
}
if(window != _main_window.get()) {
y_fatal("Window not found");
}
}
ImGuiPlatform* ImGuiPlatform::get_platform() {
y_debug_assert(ImGui::GetIO().BackendPlatformUserData);
return static_cast<ImGuiPlatform*>(ImGui::GetIO().BackendPlatformUserData);
}
Window* ImGuiPlatform::get_window(ImGuiViewport* vp) {
return &get_platform_window(vp)->window;
}
ImGuiPlatform::PlatformWindow* ImGuiPlatform::get_platform_window(ImGuiViewport* vp) {
y_debug_assert(vp->PlatformHandle);
return static_cast<PlatformWindow*>(vp->PlatformHandle);
}
void ImGuiPlatform::show_demo() {
_demo_window = true;
}
}