forked from cinder/Cinder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCinderImGui.cpp
More file actions
791 lines (703 loc) · 28.6 KB
/
Copy pathCinderImGui.cpp
File metadata and controls
791 lines (703 loc) · 28.6 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
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
#include "cinder/CinderImGui.h"
#include "imgui_impl_opengl3.h"
#include "cinder/app/App.h"
#include "cinder/Log.h"
#include "cinder/app/Window.h"
#include "cinder/app/MouseEvent.h"
#include "cinder/app/KeyEvent.h"
#include "cinder/gl/Texture.h"
#include "cinder/gl/Context.h"
#include "cinder/Clipboard.h"
#if defined( CINDER_MSW )
#include "cinder/app/msw/PlatformMsw.h"
#endif
#include <unordered_map>
static bool sInitialized = false;
static bool sTriggerNewFrame = false;
static ci::signals::ConnectionList sAppConnections;
static std::unordered_map<ci::app::WindowRef, ci::signals::ConnectionList> sWindowConnections;
namespace ImGui {
Options::Options()
: mWindow( ci::app::getWindow() )
, mAutoRender( true )
, mIniPath()
, mSignalPriority( 1 )
, mKeyboardEnabled( true )
, mGamepadEnabled( true )
{
//! Default Cinder styling
mStyle.Alpha = 1.0f; // Global alpha applies to everything in ImGui
mStyle.WindowPadding = ImVec2( 8, 8 ); // Padding within a window
mStyle.WindowRounding = 7.0f; // Radius of window corners rounding. Set to 0.0f to have rectangular windows
mStyle.WindowBorderSize = 1.0f; // Thickness of border around windows. Generally set to 0.0f or 1.0f. Other values not well tested.
mStyle.WindowMinSize = ImVec2( 32, 32 ); // Minimum window size
mStyle.WindowTitleAlign = ImVec2( 0.0f, 0.5f ); // Alignment for title bar text
mStyle.ChildRounding = 0.0f; // Radius of child window corners rounding. Set to 0.0f to have rectangular child windows
mStyle.ChildBorderSize = 1.0f; // Thickness of border around child windows. Generally set to 0.0f or 1.0f. Other values not well tested.
mStyle.PopupRounding = 0.0f; // Radius of popup window corners rounding. Set to 0.0f to have rectangular child windows
mStyle.PopupBorderSize = 1.0f; // Thickness of border around popup or tooltip windows. Generally set to 0.0f or 1.0f. Other values not well tested.
mStyle.FramePadding = ImVec2( 4, 3 ); // Padding within a framed rectangle (used by most widgets)
mStyle.FrameRounding = 0.0f; // Radius of frame corners rounding. Set to 0.0f to have rectangular frames (used by most widgets).
mStyle.FrameBorderSize = 1.0f; // Thickness of border around frames. Generally set to 0.0f or 1.0f. Other values not well tested.
mStyle.ItemSpacing = ImVec2( 8, 4 ); // Horizontal and vertical spacing between widgets/lines
mStyle.ItemInnerSpacing = ImVec2( 4, 4 ); // Horizontal and vertical spacing between within elements of a composed widget (e.g. a slider and its label)
mStyle.TouchExtraPadding = ImVec2( 0, 0 ); // Expand reactive bounding box for touch-based system where touch position is not accurate enough. Unfortunately we don't sort widgets so priority on overlap will always be given to the first
// widget. So don't grow this too much!
mStyle.IndentSpacing = 21.0f; // Horizontal spacing when e.g. entering a tree node. Generally == (FontSize + FramePadding.x*2).
mStyle.ColumnsMinSpacing = 6.0f; // Minimum horizontal spacing between two columns
mStyle.ScrollbarSize = 16.0f; // Width of the vertical scrollbar, Height of the horizontal scrollbar
mStyle.ScrollbarRounding = 9.0f; // Radius of grab corners rounding for scrollbar
mStyle.GrabMinSize = 10.0f; // Minimum width/height of a grab box for slider/scrollbar
mStyle.GrabRounding = 0.0f; // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs.
mStyle.TabRounding = 4.0f; // Radius of upper corners of a tab. Set to 0.0f to have rectangular tabs.
mStyle.TabBorderSize = 1.0f; // Thickness of border around tabs.
mStyle.ButtonTextAlign = ImVec2( 0.5f, 0.5f ); // Alignment of button text when button is larger than text.
mStyle.DisplayWindowPadding = ImVec2( 20, 20 ); // Window position are clamped to be visible within the display area or monitors by at least this amount. Only applies to regular windows.
mStyle.DisplaySafeAreaPadding = ImVec2( 3, 3 ); // If you cannot see the edge of your screen (e.g. on a TV) increase the safe area padding. Covers popups/tooltips as well regular windows.
mStyle.MouseCursorScale = 1.0f; // Scale software rendered mouse cursor (when io.MouseDrawCursor is enabled). May be removed later.
mStyle.AntiAliasedLines = true; // Enable anti-aliasing on lines/borders. Disable if you are really short on CPU/GPU.
mStyle.AntiAliasedFill = true; // Enable anti-aliasing on filled shapes (rounded rectangles, circles, etc.)
mStyle.CurveTessellationTol = 1.25f; // Tessellation tolerance when using PathBezierCurveTo() without a specific number of segments. Decrease for highly tessellated curves (higher quality, more polygons), increase to reduce quality.
ImVec4 grayDarkBg{ 0.07f, 0.09f, 0.08f, 0.96f };
ImVec4 grayBg{ 0.13f, 0.15f, 0.14f, 0.96f };
ImVec4 grayLight{ 0.29f, 0.31f, 0.30f, 0.93f };
ImVec4 grayInactive{ 0.22f, 0.25f, 0.24f, 0.95f };
ImVec4 orangeDimmed = grayLight;
ImVec4 orangeActive{ 0.663f, 0.235f, 0.082f, 0.96f };
mStyle.Colors[ImGuiCol_Text] = ImVec4( 0.93f, 0.96f, 0.95f, 0.88f );
mStyle.Colors[ImGuiCol_TextDisabled] = ImVec4( 0.93f, 0.96f, 0.95f, 0.28f );
mStyle.Colors[ImGuiCol_WindowBg] = grayDarkBg;
mStyle.Colors[ImGuiCol_ChildBg] = grayDarkBg;
mStyle.Colors[ImGuiCol_PopupBg] = ImVec4( 0.16f, 0.18f, 0.17f, 0.96f );
mStyle.Colors[ImGuiCol_Border] = ImVec4( grayLight.x, grayLight.y, grayLight.z, 0.6f );
mStyle.Colors[ImGuiCol_BorderShadow] = ImVec4( 0.00f, 0.00f, 0.00f, 0.00f );
mStyle.Colors[ImGuiCol_FrameBg] = grayBg;
mStyle.Colors[ImGuiCol_FrameBgHovered] = grayInactive;
mStyle.Colors[ImGuiCol_FrameBgActive] = orangeActive;
mStyle.Colors[ImGuiCol_TitleBg] = grayInactive;
mStyle.Colors[ImGuiCol_TitleBgActive] = orangeActive;
mStyle.Colors[ImGuiCol_TitleBgCollapsed] = orangeActive;
mStyle.Colors[ImGuiCol_MenuBarBg] = grayDarkBg;
mStyle.Colors[ImGuiCol_ScrollbarBg] = grayBg;
mStyle.Colors[ImGuiCol_ScrollbarGrab] = grayLight;
mStyle.Colors[ImGuiCol_ScrollbarGrabHovered] = grayInactive;
mStyle.Colors[ImGuiCol_ScrollbarGrabActive] = orangeActive;
mStyle.Colors[ImGuiCol_CheckMark] = ImVec4( 0.93f, 0.96f, 0.95f, 0.88f );
mStyle.Colors[ImGuiCol_SliderGrab] = grayLight;
mStyle.Colors[ImGuiCol_SliderGrabActive] = grayBg;
mStyle.Colors[ImGuiCol_Button] = grayBg;
mStyle.Colors[ImGuiCol_ButtonHovered] = grayInactive;
mStyle.Colors[ImGuiCol_ButtonActive] = orangeActive;
mStyle.Colors[ImGuiCol_Header] = grayInactive;
mStyle.Colors[ImGuiCol_HeaderHovered] = orangeDimmed; // orangeActive;
mStyle.Colors[ImGuiCol_HeaderActive] = orangeActive; // orangeBright;
mStyle.Colors[ImGuiCol_Separator] = ImVec4( 0.14f, 0.16f, 0.19f, 1.00f );
mStyle.Colors[ImGuiCol_SeparatorHovered] = grayInactive;
mStyle.Colors[ImGuiCol_SeparatorActive] = orangeActive;
mStyle.Colors[ImGuiCol_ResizeGrip] = grayBg;
mStyle.Colors[ImGuiCol_ResizeGripHovered] = grayInactive;
mStyle.Colors[ImGuiCol_ResizeGripActive] = orangeActive;
mStyle.Colors[ImGuiCol_Tab] = grayInactive;
mStyle.Colors[ImGuiCol_TabHovered] = orangeDimmed;
mStyle.Colors[ImGuiCol_TabActive] = orangeActive; // orangeBright;
mStyle.Colors[ImGuiCol_TabUnfocused] = orangeDimmed;
mStyle.Colors[ImGuiCol_TabUnfocusedActive] = orangeActive;
// mStyle.Colors[ImGuiCol_DockingPreview] = grayInactive;
// mStyle.Colors[ImGuiCol_DockingEmptyBg] = ImVec4( 0.20f, 0.20f, 0.20f, 1.00f );
mStyle.Colors[ImGuiCol_PlotLines] = ImVec4( 0.93f, 0.96f, 0.95f, 0.80f );
mStyle.Colors[ImGuiCol_PlotLinesHovered] = grayInactive;
mStyle.Colors[ImGuiCol_PlotHistogram] = ImVec4( 0.93f, 0.96f, 0.95f, 0.80f );
mStyle.Colors[ImGuiCol_PlotHistogramHovered] = grayInactive;
mStyle.Colors[ImGuiCol_TextSelectedBg] = grayInactive;
mStyle.Colors[ImGuiCol_DragDropTarget] = grayInactive;
mStyle.Colors[ImGuiCol_NavHighlight] = grayInactive;
mStyle.Colors[ImGuiCol_NavWindowingHighlight] = ImVec4( 1.00f, 1.00f, 1.00f, 0.70f );
mStyle.Colors[ImGuiCol_NavWindowingDimBg] = ImVec4( 0.80f, 0.80f, 0.80f, 0.20f );
mStyle.Colors[ImGuiCol_ModalWindowDimBg] = ImVec4( 0.20f, 0.22f, 0.27f, 0.73f );
}
Options& Options::window( const ci::app::WindowRef& window, int signalPriority )
{
mWindow = window;
mSignalPriority = signalPriority;
return *this;
}
Options& Options::autoRender( bool autoRender )
{
mAutoRender = autoRender;
return *this;
}
Options& Options::iniPath( const ci::fs::path& path )
{
mIniPath = path;
return *this;
}
Options& Options::enableKeyboard( bool enable )
{
mKeyboardEnabled = enable;
return *this;
}
Options& Options::enableGamepad( bool enable )
{
mGamepadEnabled = enable;
return *this;
}
Options& Options::signalPriority( int signalPriority )
{
mSignalPriority = signalPriority;
return *this;
}
Options& Options::style( const ImGuiStyle& style )
{
mStyle = style;
return *this;
}
ScopedId::ScopedId( int int_id )
{
ImGui::PushID( int_id );
}
ScopedId::ScopedId( const char* label )
{
ImGui::PushID( label );
}
ScopedId::ScopedId( const void* ptrId )
{
ImGui::PushID( ptrId );
}
ScopedId::~ScopedId()
{
ImGui::PopID();
}
ScopedWindow::ScopedWindow( const char* label )
{
ImGui::Begin( label );
}
ScopedWindow::~ScopedWindow()
{
ImGui::End();
}
ScopedMainMenuBar::ScopedMainMenuBar()
: mOpened{ ImGui::BeginMainMenuBar() }
{
}
ScopedMainMenuBar::~ScopedMainMenuBar()
{
if( mOpened )
ImGui::EndMainMenuBar();
}
ScopedMenuBar::ScopedMenuBar()
: mOpened{ ImGui::BeginMenuBar() }
{
}
ScopedMenuBar::~ScopedMenuBar()
{
if( mOpened )
ImGui::EndMenuBar();
}
ScopedGroup::ScopedGroup()
{
ImGui::BeginGroup();
}
ScopedGroup::~ScopedGroup()
{
ImGui::EndGroup();
}
ScopedColumns::ScopedColumns( int count, const char* id, bool border )
{
ImGui::Columns( count, id, border );
}
ScopedColumns::~ScopedColumns()
{
ImGui::NextColumn();
}
ScopedTreeNode::ScopedTreeNode( const std::string& name )
: mOpened( ImGui::TreeNode( name.c_str() ) )
{
}
ScopedTreeNode::~ScopedTreeNode()
{
if( mOpened )
ImGui::TreePop();
}
bool DragFloat2( const char* label, glm::vec2* v2, float v_speed, float v_min, float v_max, const char* format, ImGuiSliderFlags flags )
{
return DragFloat2( label, glm::value_ptr( *v2 ), v_speed, v_min, v_max, format, flags );
}
bool DragFloat3( const char* label, glm::vec3* v3, float v_speed, float v_min, float v_max, const char* format, ImGuiSliderFlags flags )
{
return DragFloat3( label, glm::value_ptr( *v3 ), v_speed, v_min, v_max, format, flags );
}
bool DragFloat4( const char* label, glm::vec4* v4, float v_speed, float v_min, float v_max, const char* format, ImGuiSliderFlags flags )
{
return DragFloat4( label, glm::value_ptr( *v4 ), v_speed, v_min, v_max, format, flags );
}
bool DragInt2( const char* label, glm::ivec2* v2, float v_speed, int v_min, int v_max, const char* format )
{
return DragInt2( label, glm::value_ptr( *v2 ), v_speed, v_min, v_max, format );
}
bool DragInt3( const char* label, glm::ivec3* v3, float v_speed, int v_min, int v_max, const char* format )
{
return DragInt3( label, glm::value_ptr( *v3 ), v_speed, v_min, v_max, format );
}
bool DragInt4( const char* label, glm::ivec4* v4, float v_speed, int v_min, int v_max, const char* format )
{
return DragInt4( label, glm::value_ptr( *v4 ), v_speed, v_min, v_max, format );
}
bool SliderFloat2( const char* label, glm::vec2* v2, float v_min, float v_max, const char* format, ImGuiSliderFlags flags )
{
return SliderFloat2( label, glm::value_ptr( *v2 ), v_min, v_max, format, flags );
}
bool SliderFloat3( const char* label, glm::vec3* v3, float v_min, float v_max, const char* format, ImGuiSliderFlags flags )
{
return SliderFloat3( label, glm::value_ptr( *v3 ), v_min, v_max, format, flags );
}
bool SliderFloat4( const char* label, glm::vec4* v4, float v_min, float v_max, const char* format, ImGuiSliderFlags flags )
{
return SliderFloat4( label, glm::value_ptr( *v4 ), v_min, v_max, format, flags );
}
bool SliderInt2( const char* label, glm::ivec2* v2, int v_min, int v_max, const char* format )
{
return SliderInt2( label, glm::value_ptr( *v2 ), v_min, v_max, format );
}
bool SliderInt3( const char* label, glm::ivec3* v3, int v_min, int v_max, const char* format )
{
return SliderInt3( label, glm::value_ptr( *v3 ), v_min, v_max, format );
}
bool SliderInt4( const char* label, glm::ivec4* v4, int v_min, int v_max, const char* format )
{
return SliderInt4( label, glm::value_ptr( *v4 ), v_min, v_max, format );
}
bool InputInt2( const char* label, glm::ivec2* v2, ImGuiInputTextFlags flags )
{
return InputInt2( label, glm::value_ptr( *v2 ), flags );
}
bool InputInt3( const char* label, glm::ivec3* v3, ImGuiInputTextFlags flags )
{
return InputInt3( label, glm::value_ptr( *v3 ), flags );
}
bool InputInt4( const char* label, glm::ivec4* v4, ImGuiInputTextFlags flags )
{
return InputInt4( label, glm::value_ptr( *v4 ), flags );
}
bool ColorEdit3( const char* label, ci::Colorf* color, ImGuiColorEditFlags flags )
{
return ColorEdit3( label, color->ptr(), flags );
}
bool ColorEdit4( const char* label, ci::ColorAf* color, ImGuiColorEditFlags flags )
{
return ColorEdit4( label, color->ptr(), flags );
}
bool ColorPicker3( const char* label, ci::Colorf* color, ImGuiColorEditFlags flags )
{
return ColorPicker3( label, color->ptr(), flags );
}
bool ColorPicker4( const char* label, ci::ColorAf* color, ImGuiColorEditFlags flags )
{
return ColorPicker4( label, color->ptr(), flags );
}
bool Combo( const char* label, int* currIndex, const std::vector<std::string>& values, ImGuiComboFlags flags )
{
if( values.empty() )
return false;
bool changed = false;
int itemsCount = (int)values.size();
const char* previewItem = NULL;
if( *currIndex >= 0 && *currIndex < itemsCount ) {
previewItem = values.at( *currIndex ).c_str();
}
if( ImGui::BeginCombo( label, previewItem, flags ) ) {
for( int i = 0; i < itemsCount; ++i ) {
ImGui::PushID( (void*)(intptr_t)i );
bool selected = ( *currIndex == i );
if( ImGui::Selectable( values.at( i ).c_str(), selected ) ) {
*currIndex = i;
changed = true;
}
if( selected )
ImGui::SetItemDefaultFocus();
ImGui::PopID();
}
ImGui::EndCombo();
}
return changed;
}
bool ListBox( const char* label, int* currIndex, const std::vector<std::string>& values, int height_in_items )
{
if( values.empty() )
return false;
bool changed = false;
if( ImGui::BeginListBox( label, ImVec2( 0, height_in_items * ImGui::GetTextLineHeightWithSpacing() ) ) ) {
for( int i = 0; i < (int)values.size(); ++i ) {
ImGui::PushID( (void*)(intptr_t)i );
bool selected = ( *currIndex == i );
if( ImGui::Selectable( values.at( i ).c_str(), selected ) ) {
*currIndex = i;
changed = true;
}
if( selected )
ImGui::SetItemDefaultFocus();
ImGui::PopID();
}
ImGui::EndListBox();
}
return changed;
}
void Image( const ci::gl::Texture2dRef& texture, const ci::vec2& size, const ci::vec2& uv0, const ci::vec2& uv1, const ci::vec4& tint_col, const ci::vec4& border_col )
{
Image( (ImTextureID)(intptr_t)texture->getId(), size, uv0, uv1, tint_col, border_col );
}
} // namespace ImGui
static void ImGui_ImplCinder_MouseDown( ci::app::MouseEvent& event )
{
ImGuiIO& io = ImGui::GetIO();
io.MousePos = event.getWindow()->toPixels( event.getPos() );
if( event.isLeft() )
io.AddMouseButtonEvent( 0, true );
if( event.isRight() )
io.AddMouseButtonEvent( 1, true );
if( event.isMiddle() )
io.AddMouseButtonEvent( 2, true );
io.AddKeyEvent( ImGuiMod_Ctrl, event.isControlDown() );
io.AddKeyEvent( ImGuiMod_Shift, event.isShiftDown() );
io.AddKeyEvent( ImGuiMod_Alt, event.isAltDown() );
io.AddKeyEvent( ImGuiMod_Super, event.isMetaDown() );
event.setHandled( io.WantCaptureMouse );
}
static void ImGui_ImplCinder_MouseUp( ci::app::MouseEvent& event )
{
ImGuiIO& io = ImGui::GetIO();
if( event.isLeft() )
io.AddMouseButtonEvent( 0, false );
if( event.isRight() )
io.AddMouseButtonEvent( 1, false );
if( event.isMiddle() )
io.AddMouseButtonEvent( 2, false );
io.AddKeyEvent( ImGuiMod_Ctrl, event.isControlDown() );
io.AddKeyEvent( ImGuiMod_Shift, event.isShiftDown() );
io.AddKeyEvent( ImGuiMod_Alt, event.isAltDown() );
io.AddKeyEvent( ImGuiMod_Super, event.isMetaDown() );
event.setHandled( io.WantCaptureMouse );
}
static void ImGui_ImplCinder_MouseWheel( ci::app::MouseEvent& event )
{
ImGuiIO& io = ImGui::GetIO();
io.AddMouseWheelEvent( 0.0f, event.getWheelIncrement() );
io.AddKeyEvent( ImGuiMod_Ctrl, event.isControlDown() );
io.AddKeyEvent( ImGuiMod_Shift, event.isShiftDown() );
io.AddKeyEvent( ImGuiMod_Alt, event.isAltDown() );
io.AddKeyEvent( ImGuiMod_Super, event.isMetaDown() );
event.setHandled( io.WantCaptureMouse );
}
static void ImGui_ImplCinder_MouseMove( ci::app::MouseEvent& event )
{
ImGuiIO& io = ImGui::GetIO();
io.AddMousePosEvent( event.getWindow()->toPixels( event.getPos() ).x, event.getWindow()->toPixels( event.getPos() ).y );
io.AddKeyEvent( ImGuiMod_Ctrl, event.isControlDown() );
io.AddKeyEvent( ImGuiMod_Shift, event.isShiftDown() );
io.AddKeyEvent( ImGuiMod_Alt, event.isAltDown() );
io.AddKeyEvent( ImGuiMod_Super, event.isMetaDown() );
event.setHandled( io.WantCaptureMouse );
}
//! sets the right mouseDrag IO values in imgui
static void ImGui_ImplCinder_MouseDrag( ci::app::MouseEvent& event )
{
ImGuiIO& io = ImGui::GetIO();
io.AddMousePosEvent( event.getWindow()->toPixels( event.getPos() ).x, event.getWindow()->toPixels( event.getPos() ).y );
io.AddKeyEvent( ImGuiMod_Ctrl, event.isControlDown() );
io.AddKeyEvent( ImGuiMod_Shift, event.isShiftDown() );
io.AddKeyEvent( ImGuiMod_Alt, event.isAltDown() );
io.AddKeyEvent( ImGuiMod_Super, event.isMetaDown() );
event.setHandled( io.WantCaptureMouse );
}
static ImGuiKey CinderKeyToImGuiKey( int cinderKey )
{
// Handle letter keys with range check
if( cinderKey >= ci::app::KeyEvent::KEY_a && cinderKey <= ci::app::KeyEvent::KEY_z ) {
return (ImGuiKey)( ImGuiKey_A + ( cinderKey - ci::app::KeyEvent::KEY_a ) );
}
// Handle number keys with range check
else if( cinderKey >= ci::app::KeyEvent::KEY_0 && cinderKey <= ci::app::KeyEvent::KEY_9 ) {
return (ImGuiKey)( ImGuiKey_0 + ( cinderKey - ci::app::KeyEvent::KEY_0 ) );
}
// Handle function keys with range check
else if( cinderKey >= ci::app::KeyEvent::KEY_F1 && cinderKey <= ci::app::KeyEvent::KEY_F15 ) {
return (ImGuiKey)( ImGuiKey_F1 + ( cinderKey - ci::app::KeyEvent::KEY_F1 ) );
}
// Handle numpad digits with range check
else if( cinderKey >= ci::app::KeyEvent::KEY_KP0 && cinderKey <= ci::app::KeyEvent::KEY_KP9 ) {
return (ImGuiKey)( ImGuiKey_Keypad0 + ( cinderKey - ci::app::KeyEvent::KEY_KP0 ) );
}
// Handle remaining keys with switch
switch( cinderKey ) {
case ci::app::KeyEvent::KEY_TAB:
return ImGuiKey_Tab;
case ci::app::KeyEvent::KEY_LEFT:
return ImGuiKey_LeftArrow;
case ci::app::KeyEvent::KEY_RIGHT:
return ImGuiKey_RightArrow;
case ci::app::KeyEvent::KEY_UP:
return ImGuiKey_UpArrow;
case ci::app::KeyEvent::KEY_DOWN:
return ImGuiKey_DownArrow;
case ci::app::KeyEvent::KEY_PAGEUP:
return ImGuiKey_PageUp;
case ci::app::KeyEvent::KEY_PAGEDOWN:
return ImGuiKey_PageDown;
case ci::app::KeyEvent::KEY_HOME:
return ImGuiKey_Home;
case ci::app::KeyEvent::KEY_END:
return ImGuiKey_End;
case ci::app::KeyEvent::KEY_INSERT:
return ImGuiKey_Insert;
case ci::app::KeyEvent::KEY_DELETE:
return ImGuiKey_Delete;
case ci::app::KeyEvent::KEY_BACKSPACE:
return ImGuiKey_Backspace;
case ci::app::KeyEvent::KEY_SPACE:
return ImGuiKey_Space;
case ci::app::KeyEvent::KEY_RETURN:
return ImGuiKey_Enter;
case ci::app::KeyEvent::KEY_ESCAPE:
return ImGuiKey_Escape;
case ci::app::KeyEvent::KEY_QUOTE:
return ImGuiKey_Apostrophe;
case ci::app::KeyEvent::KEY_COMMA:
return ImGuiKey_Comma;
case ci::app::KeyEvent::KEY_MINUS:
return ImGuiKey_Minus;
case ci::app::KeyEvent::KEY_PERIOD:
return ImGuiKey_Period;
case ci::app::KeyEvent::KEY_SLASH:
return ImGuiKey_Slash;
case ci::app::KeyEvent::KEY_SEMICOLON:
return ImGuiKey_Semicolon;
case ci::app::KeyEvent::KEY_EQUALS:
return ImGuiKey_Equal;
case ci::app::KeyEvent::KEY_LEFTBRACKET:
return ImGuiKey_LeftBracket;
case ci::app::KeyEvent::KEY_BACKSLASH:
return ImGuiKey_Backslash;
case ci::app::KeyEvent::KEY_RIGHTBRACKET:
return ImGuiKey_RightBracket;
case ci::app::KeyEvent::KEY_BACKQUOTE:
return ImGuiKey_GraveAccent;
case ci::app::KeyEvent::KEY_KP_PERIOD:
return ImGuiKey_KeypadDecimal;
case ci::app::KeyEvent::KEY_KP_DIVIDE:
return ImGuiKey_KeypadDivide;
case ci::app::KeyEvent::KEY_KP_MULTIPLY:
return ImGuiKey_KeypadMultiply;
case ci::app::KeyEvent::KEY_KP_MINUS:
return ImGuiKey_KeypadSubtract;
case ci::app::KeyEvent::KEY_KP_PLUS:
return ImGuiKey_KeypadAdd;
case ci::app::KeyEvent::KEY_KP_ENTER:
return ImGuiKey_KeypadEnter;
case ci::app::KeyEvent::KEY_KP_EQUALS:
return ImGuiKey_KeypadEqual;
case ci::app::KeyEvent::KEY_LSHIFT:
return ImGuiKey_LeftShift;
case ci::app::KeyEvent::KEY_LCTRL:
return ImGuiKey_LeftCtrl;
case ci::app::KeyEvent::KEY_LALT:
return ImGuiKey_LeftAlt;
case ci::app::KeyEvent::KEY_LSUPER:
return ImGuiKey_LeftSuper;
case ci::app::KeyEvent::KEY_RSHIFT:
return ImGuiKey_RightShift;
case ci::app::KeyEvent::KEY_RCTRL:
return ImGuiKey_RightCtrl;
case ci::app::KeyEvent::KEY_RALT:
return ImGuiKey_RightAlt;
case ci::app::KeyEvent::KEY_RSUPER:
return ImGuiKey_RightSuper;
case ci::app::KeyEvent::KEY_LMETA:
return ImGuiKey_LeftSuper;
case ci::app::KeyEvent::KEY_RMETA:
return ImGuiKey_RightSuper;
case ci::app::KeyEvent::KEY_MENU:
return ImGuiKey_Menu;
case ci::app::KeyEvent::KEY_CAPSLOCK:
return ImGuiKey_CapsLock;
case ci::app::KeyEvent::KEY_SCROLLOCK:
return ImGuiKey_ScrollLock; // Note: Cinder has typo SCROLLOCK
case ci::app::KeyEvent::KEY_NUMLOCK:
return ImGuiKey_NumLock;
case ci::app::KeyEvent::KEY_PRINT:
return ImGuiKey_PrintScreen;
case ci::app::KeyEvent::KEY_PAUSE:
return ImGuiKey_Pause;
default:
return ImGuiKey_None;
}
}
static void ImGui_ImplCinder_KeyDown( ci::app::KeyEvent& event )
{
ImGuiIO& io = ImGui::GetIO();
ImGuiKey key = CinderKeyToImGuiKey( event.getCode() );
if( key != ImGuiKey_None )
io.AddKeyEvent( key, true );
io.AddKeyEvent( ImGuiMod_Ctrl, event.isControlDown() );
io.AddKeyEvent( ImGuiMod_Shift, event.isShiftDown() );
io.AddKeyEvent( ImGuiMod_Alt, event.isAltDown() );
io.AddKeyEvent( ImGuiMod_Super, event.isMetaDown() );
// Only mark handled if ImGui wants full keyboard capture (not just text input)
// This allows global hotkeys to work even when ImGui text field has focus
event.setHandled( io.WantCaptureKeyboard );
}
static void ImGui_ImplCinder_KeyUp( ci::app::KeyEvent& event )
{
ImGuiIO& io = ImGui::GetIO();
ImGuiKey key = CinderKeyToImGuiKey( event.getCode() );
if( key != ImGuiKey_None )
io.AddKeyEvent( key, false );
io.AddKeyEvent( ImGuiMod_Ctrl, event.isControlDown() );
io.AddKeyEvent( ImGuiMod_Shift, event.isShiftDown() );
io.AddKeyEvent( ImGuiMod_Alt, event.isAltDown() );
io.AddKeyEvent( ImGuiMod_Super, event.isMetaDown() );
event.setHandled( io.WantCaptureKeyboard );
}
static void ImGui_ImplCinder_KeyChar( ci::app::KeyEvent& event )
{
ImGuiIO& io = ImGui::GetIO();
uint32_t character = event.getCharUtf32();
if( character > 0 ) {
// Handle Unicode characters beyond the Basic Multilingual Plane (BMP)
// by converting to UTF-16 surrogate pairs
if( character > 0xFFFF ) {
character -= 0x10000;
io.AddInputCharacterUTF16( static_cast<ImWchar16>( 0xD800 + ( character >> 10 ) ) );
io.AddInputCharacterUTF16( static_cast<ImWchar16>( 0xDC00 + ( character & 0x3FF ) ) );
}
else {
io.AddInputCharacterUTF16( static_cast<ImWchar16>( character ) );
}
}
// Mark handled if ImGui wants text input or full keyboard capture
event.setHandled( io.WantTextInput || io.WantCaptureKeyboard );
}
static void ImGui_ImplCinder_NewFrameGuard( const ci::app::WindowRef& window );
static void ImGui_ImplCinder_Resize( const ci::app::WindowRef& window )
{
ImGuiIO& io = ImGui::GetIO();
io.DisplaySize = ci::vec2( window->toPixels( window->getSize() ) );
ImGui_ImplCinder_NewFrameGuard( window );
}
static void ImGui_ImplCinder_NewFrameGuard( const ci::app::WindowRef& window )
{
if( ! sTriggerNewFrame )
return;
ImGui_ImplOpenGL3_NewFrame();
ImGuiIO& io = ImGui::GetIO();
// Setup display size
io.DisplaySize = window->toPixels( window->getSize() );
// Setup time step
static double g_Time = 0.0f;
double current_time = ci::app::getElapsedSeconds();
io.DeltaTime = g_Time > 0.0 ? (float)( current_time - g_Time ) : (float)( 1.0f / 60.0f );
g_Time = current_time;
ImGui::NewFrame();
sTriggerNewFrame = false;
}
static void ImGui_ImplCinder_PostDraw()
{
#if defined( CINDER_MSW )
// Skip ImGui rendering when inside modal dialog loop, but reset trigger flag
if( static_cast<ci::app::PlatformMsw*>( ci::app::Platform::get() )->isInsideModalLoop() ) {
sTriggerNewFrame = true;
return;
}
#endif
ImGui::Render();
ImDrawData* drawData = ImGui::GetDrawData();
// Only call OpenGL render if there's actually something to draw
if( drawData && drawData->CmdListsCount > 0 ) {
ImGui_ImplOpenGL3_RenderDrawData( drawData );
}
sTriggerNewFrame = true;
}
static bool ImGui_ImplCinder_Init( const ci::app::WindowRef& window, const ImGui::Options& options )
{
// Setup back-end capabilities flags
ImGuiIO& io = ImGui::GetIO();
io.BackendPlatformName = "imgui_impl_cinder";
ImGuiStyle& imGuiStyle = ImGui::GetStyle();
imGuiStyle = options.getStyle();
#ifndef CINDER_LINUX
// clipboard callbacks
io.SetClipboardTextFn = []( void* user_data, const char* text ) { ci::Clipboard::setString( std::string( text ) ); };
io.GetClipboardTextFn = []( void* user_data ) {
std::string str = ci::Clipboard::getString();
static std::vector<char> strCopy;
strCopy = std::vector<char>( str.begin(), str.end() );
strCopy.push_back( '\0' );
return (const char*)&strCopy[0];
};
#endif
int signalPriority = options.getSignalPriority();
sWindowConnections[window] += window->getSignalMouseDown().connect( signalPriority, ImGui_ImplCinder_MouseDown );
sWindowConnections[window] += window->getSignalMouseUp().connect( signalPriority, ImGui_ImplCinder_MouseUp );
sWindowConnections[window] += window->getSignalMouseMove().connect( signalPriority, ImGui_ImplCinder_MouseMove );
sWindowConnections[window] += window->getSignalMouseDrag().connect( signalPriority, ImGui_ImplCinder_MouseDrag );
sWindowConnections[window] += window->getSignalMouseWheel().connect( signalPriority, ImGui_ImplCinder_MouseWheel );
sWindowConnections[window] += window->getSignalKeyDown().connect( signalPriority, ImGui_ImplCinder_KeyDown );
sWindowConnections[window] += window->getSignalKeyUp().connect( signalPriority, ImGui_ImplCinder_KeyUp );
sWindowConnections[window] += window->getSignalKeyChar().connect( signalPriority, ImGui_ImplCinder_KeyChar );
sWindowConnections[window] += window->getSignalResize().connect( signalPriority, std::bind( ImGui_ImplCinder_Resize, window ) );
if( options.isAutoRenderEnabled() ) {
sWindowConnections[window] += ci::app::App::get()->getSignalUpdate().connect( std::bind( ImGui_ImplCinder_NewFrameGuard, window ) );
sWindowConnections[window] += window->getSignalDraw().connect( std::bind( ImGui_ImplCinder_NewFrameGuard, window ) );
sWindowConnections[window] += window->getSignalPostDraw().connect( ImGui_ImplCinder_PostDraw );
}
sWindowConnections[window] += window->getSignalClose().connect( [=] {
sWindowConnections.erase( window );
sTriggerNewFrame = false;
} );
return true;
}
static void ImGui_ImplCinder_Shutdown()
{
sWindowConnections.clear();
}
bool ImGui::Initialize( const ImGui::Options& options )
{
if( sInitialized )
return false;
IMGUI_CHECKVERSION();
auto context = ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
if( options.isKeyboardEnabled() ) {
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavNoCaptureKeyboard; // Don't capture keyboard when navigation is active
}
if( options.isGamepadEnabled() )
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
ci::app::WindowRef window = options.getWindow();
io.DisplaySize = ci::vec2( window->toPixels( window->getSize() ) );
io.DeltaTime = 1.0f / 60.0f;
io.WantCaptureMouse = true;
static std::string path;
if( options.getIniPath().empty() ) {
path = ( ci::app::getAssetPath( "" ) / "imgui.ini" ).string();
}
else {
path = options.getIniPath().string().c_str();
}
io.IniFilename = path.c_str();
#if ! defined( CINDER_GL_ES )
ImGui_ImplOpenGL3_Init( "#version 150" );
#else
ImGui_ImplOpenGL3_Init();
#endif
ImGui_ImplCinder_Init( window, options );
if( options.isAutoRenderEnabled() ) {
ImGui_ImplCinder_NewFrameGuard( window );
sTriggerNewFrame = true;
}
sAppConnections += ci::app::App::get()->getSignalCleanup().connect( [context]() {
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplCinder_Shutdown();
ImGui::DestroyContext( context );
} );
sInitialized = true;
return sInitialized;
}