forked from simongeilfus/Cinder-ImGui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCinderImGui.cpp
2520 lines (2212 loc) · 69.6 KB
/
CinderImGui.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
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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Cinder-ImGui
This code is intended for use with Cinder
and Omar Cornut ImGui C++ libraries.
http://libcinder.org
https://github.com/ocornut
Copyright (c) 2013-2015, Simon Geilfus - All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that
the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
// Linux fixes thanks to the help of @petros, see this thread:
// https://forum.libcinder.org/#Topic/23286000002634083
#include "CinderImGui.h"
#define IMGUI_DEFINE_MATH_OPERATORS
#define IMGUI_DEFINE_PLACEMENT_NEW
#include "imgui_internal.h"
#include "cinder/app/App.h"
#include "cinder/gl/scoped.h"
#include "cinder/gl/draw.h"
#include "cinder/gl/Context.h"
#include "cinder/gl/Vao.h"
#include "cinder/gl/Vbo.h"
#include "cinder/gl/Texture.h"
#include "cinder/gl/GlslProg.h"
#include "cinder/Clipboard.h"
#include "cinder/CinderAssert.h"
#include "cinder/Log.h"
using namespace std;
using namespace ci;
using namespace ci::app;
namespace ImGui {
// static variables
static bool sInitialized = false;
ImGui::Options::Options()
: mWindow( ci::app::getWindow() ),
mAutoRender( true ), mMergeFonts( true )
{
}
ImGui::Options& ImGui::Options::window( const ci::app::WindowRef &window )
{
mWindow = window;
return *this;
}
ImGui::Options& ImGui::Options::autoRender( bool autoRender )
{
mAutoRender = autoRender;
return *this;
}
ImGui::Options& ImGui::Options::font( const ci::fs::path &fontPath, float size )
{
mFonts = { { fontPath, size } };
return *this;
}
ImGui::Options& ImGui::Options::fonts( const std::vector<std::pair<ci::fs::path,float>> &fonts, bool merge )
{
mFonts = fonts;
mMergeFonts = merge;
return *this;
}
ImGui::Options& ImGui::Options::fontGlyphRanges( const std::string &name, const vector<ImWchar> &glyphRanges )
{
mFontsGlyphRanges[name] = glyphRanges;
return *this;
}
ImGui::Options& ImGui::Options::fontGlobalScale(float scale)
{
ImGui::GetIO().FontGlobalScale = scale;
return *this;
}
ImGui::Options& ImGui::Options::alpha( float a )
{
mStyle.Alpha = a;
return *this;
}
ImGui::Options& ImGui::Options::windowPadding( const glm::vec2 &padding )
{
mStyle.WindowPadding = padding;
return *this;
}
ImGui::Options& ImGui::Options::windowMinSize( const glm::vec2 &minSize )
{
mStyle.WindowMinSize = minSize;
return *this;
}
ImGui::Options& ImGui::Options::windowRounding( float rounding )
{
mStyle.WindowRounding = rounding;
return *this;
}
ImGui::Options& ImGui::Options::windowTitleAlign( const glm::vec2 &align )
{
mStyle.WindowTitleAlign = align;
return *this;
}
ImGui::Options& ImGui::Options::childRounding( float rounding )
{
mStyle.ChildRounding = rounding;
return *this;
}
ImGui::Options& ImGui::Options::framePadding( const glm::vec2 &padding )
{
mStyle.FramePadding = padding;
return *this;
}
ImGui::Options& ImGui::Options::frameRounding( float rounding )
{
mStyle.FrameRounding = rounding;
return *this;
}
ImGui::Options& ImGui::Options::itemSpacing( const glm::vec2 &spacing )
{
mStyle.ItemSpacing = spacing;
return *this;
}
ImGui::Options& ImGui::Options::itemInnerSpacing( const glm::vec2 &spacing )
{
mStyle.ItemInnerSpacing = spacing;
return *this;
}
ImGui::Options& ImGui::Options::touchExtraPadding( const glm::vec2 &padding )
{
mStyle.TouchExtraPadding = padding;
return *this;
}
ImGui::Options& ImGui::Options::indentSpacing( float spacing )
{
mStyle.IndentSpacing = spacing;
return *this;
}
ImGui::Options& ImGui::Options::columnsMinSpacing( float minSpacing )
{
mStyle.ColumnsMinSpacing = minSpacing;
return *this;
}
ImGui::Options& ImGui::Options::scrollBarSize( float size )
{
mStyle.ScrollbarSize = size;
return *this;
}
ImGui::Options& ImGui::Options::scrollbarRounding( float rounding )
{
mStyle.ScrollbarRounding = rounding;
return *this;
}
ImGui::Options& ImGui::Options::grabMinSize( float minSize )
{
mStyle.GrabMinSize = minSize;
return *this;
}
ImGui::Options& ImGui::Options::grabRounding( float rounding )
{
mStyle.GrabRounding = rounding;
return *this;
}
ImGui::Options& ImGui::Options::buttonTextAlign( const ci::vec2 &textAlign )
{
mStyle.ButtonTextAlign = textAlign;
return *this;
}
ImGui::Options& ImGui::Options::displayWindowPadding( const glm::vec2 &padding )
{
mStyle.DisplayWindowPadding = padding;
return *this;
}
ImGui::Options& ImGui::Options::displaySafeAreaPadding( const glm::vec2 &padding )
{
mStyle.DisplaySafeAreaPadding = padding;
return *this;
}
ImGui::Options& ImGui::Options::antiAliasedLines( bool antiAliasing )
{
mStyle.AntiAliasedLines = antiAliasing;
return *this;
}
ImGui::Options& ImGui::Options::antiAliasedFill( bool antiAliasing )
{
mStyle.AntiAliasedFill = antiAliasing;
return *this;
}
ImGui::Options& ImGui::Options::curveTessellationTol( float tessTolerance )
{
mStyle.CurveTessellationTol = tessTolerance;
return *this;
}
ImGui::Options& ImGui::Options::iniPath( const ci::fs::path &path )
{
mIniPath = path;
return *this;
}
const ImWchar* ImGui::Options::getFontGlyphRanges( const std::string &name ) const
{
if( mFontsGlyphRanges.count( name ) ) {
return &mFontsGlyphRanges.find(name)->second[0];
}
else return NULL;
}
ImGui::Options& ImGui::Options::defaultTheme()
{
mStyle = ImGuiStyle();
return *this;
}
ImGui::Options& ImGui::Options::darkTheme()
{
mStyle.WindowMinSize = ImVec2( 160, 20 );
mStyle.FramePadding = ImVec2( 4, 2 );
mStyle.ItemSpacing = ImVec2( 6, 2 );
mStyle.ItemInnerSpacing = ImVec2( 6, 4 );
mStyle.Alpha = 0.95f;
mStyle.WindowRounding = 4.0f;
mStyle.FrameRounding = 2.0f;
mStyle.IndentSpacing = 6.0f;
mStyle.ItemInnerSpacing = ImVec2( 2, 4 );
mStyle.ColumnsMinSpacing = 50.0f;
mStyle.GrabMinSize = 14.0f;
mStyle.GrabRounding = 16.0f;
mStyle.ScrollbarSize = 12.0f;
mStyle.ScrollbarRounding = 16.0f;
ImGuiStyle& style = mStyle;
style.Colors[ImGuiCol_Text] = ImVec4(0.86f, 0.93f, 0.89f, 0.78f);
style.Colors[ImGuiCol_TextDisabled] = ImVec4(0.86f, 0.93f, 0.89f, 0.28f);
style.Colors[ImGuiCol_WindowBg] = ImVec4(0.13f, 0.14f, 0.17f, 1.00f);
style.Colors[ImGuiCol_Border] = ImVec4(0.31f, 0.31f, 1.00f, 0.00f);
style.Colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
style.Colors[ImGuiCol_FrameBg] = ImVec4(0.20f, 0.22f, 0.27f, 1.00f);
style.Colors[ImGuiCol_FrameBgHovered] = ImVec4(0.92f, 0.18f, 0.29f, 0.78f);
style.Colors[ImGuiCol_FrameBgActive] = ImVec4(0.92f, 0.18f, 0.29f, 1.00f);
style.Colors[ImGuiCol_TitleBg] = ImVec4(0.20f, 0.22f, 0.27f, 1.00f);
style.Colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.20f, 0.22f, 0.27f, 0.75f);
style.Colors[ImGuiCol_TitleBgActive] = ImVec4(0.92f, 0.18f, 0.29f, 1.00f);
style.Colors[ImGuiCol_MenuBarBg] = ImVec4(0.20f, 0.22f, 0.27f, 0.47f);
style.Colors[ImGuiCol_ScrollbarBg] = ImVec4(0.20f, 0.22f, 0.27f, 1.00f);
style.Colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.09f, 0.15f, 0.16f, 1.00f);
style.Colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.92f, 0.18f, 0.29f, 0.78f);
style.Colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.92f, 0.18f, 0.29f, 1.00f);
style.Colors[ImGuiCol_CheckMark] = ImVec4(0.71f, 0.22f, 0.27f, 1.00f);
style.Colors[ImGuiCol_SliderGrab] = ImVec4(0.47f, 0.77f, 0.83f, 0.14f);
style.Colors[ImGuiCol_SliderGrabActive] = ImVec4(0.92f, 0.18f, 0.29f, 1.00f);
style.Colors[ImGuiCol_Button] = ImVec4(0.47f, 0.77f, 0.83f, 0.14f);
style.Colors[ImGuiCol_ButtonHovered] = ImVec4(0.92f, 0.18f, 0.29f, 0.86f);
style.Colors[ImGuiCol_ButtonActive] = ImVec4(0.92f, 0.18f, 0.29f, 1.00f);
style.Colors[ImGuiCol_Header] = ImVec4(0.92f, 0.18f, 0.29f, 0.76f);
style.Colors[ImGuiCol_HeaderHovered] = ImVec4(0.92f, 0.18f, 0.29f, 0.86f);
style.Colors[ImGuiCol_HeaderActive] = ImVec4(0.92f, 0.18f, 0.29f, 1.00f);
style.Colors[ImGuiCol_Separator] = ImVec4(0.14f, 0.16f, 0.19f, 1.00f);
style.Colors[ImGuiCol_SeparatorHovered] = ImVec4(0.92f, 0.18f, 0.29f, 0.78f);
style.Colors[ImGuiCol_SeparatorActive] = ImVec4(0.92f, 0.18f, 0.29f, 1.00f);
style.Colors[ImGuiCol_ResizeGrip] = ImVec4(0.47f, 0.77f, 0.83f, 0.04f);
style.Colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.92f, 0.18f, 0.29f, 0.78f);
style.Colors[ImGuiCol_ResizeGripActive] = ImVec4(0.92f, 0.18f, 0.29f, 1.00f);
style.Colors[ImGuiCol_PlotLines] = ImVec4(0.86f, 0.93f, 0.89f, 0.63f);
style.Colors[ImGuiCol_PlotLinesHovered] = ImVec4(0.92f, 0.18f, 0.29f, 1.00f);
style.Colors[ImGuiCol_PlotHistogram] = ImVec4(0.86f, 0.93f, 0.89f, 0.63f);
style.Colors[ImGuiCol_PlotHistogramHovered] = ImVec4(0.92f, 0.18f, 0.29f, 1.00f);
style.Colors[ImGuiCol_TextSelectedBg] = ImVec4(0.92f, 0.18f, 0.29f, 0.43f);
style.Colors[ImGuiCol_PopupBg] = ImVec4(0.20f, 0.22f, 0.27f, 0.9f);
style.Colors[ImGuiCol_ModalWindowDarkening] = ImVec4(0.20f, 0.22f, 0.27f, 0.73f);
return *this;
}
ImGui::Options& ImGui::Options::color( ImGuiCol option, const ci::ColorA &color )
{
mStyle.Colors[ option ] = color;
return *this;
}
//! cinder renderer
class Renderer {
public:
Renderer();
//! renders imgui drawlist
void render( ImDrawData* draw_data );
//! sets the font
ImFont* addFont( const ci::fs::path &font, float size, const ImWchar* glyph_ranges = NULL, bool merge = true );
//! initializes and returns the font texture
ci::gl::Texture2dRef getFontTextureRef();
//! initializes and returns the vao
ci::gl::VaoRef getVao();
//! initializes and returns the vbo
ci::gl::VboRef getVbo();
//! initializes and returns the shader
ci::gl::GlslProgRef getGlslProg();
//! initializes the font texture
void initFontTexture();
//! initializes the vbo mesh
void initBuffers( size_t size = 1000 );
//! initializes the shader
void initGlslProg();
ImFont* getFont( const std::string &name );
void clearFonts();
protected:
ci::gl::Texture2dRef mFontTexture;
ci::gl::VaoRef mVao;
ci::gl::VboRef mVbo;
ci::gl::VboRef mIbo;
ci::gl::GlslProgRef mShader;
map<string,ImFont*> mFonts;
vector<vector<ImWchar>> mFontsGlyphRanges;
};
Renderer::Renderer()
{
initGlslProg();
initBuffers();
}
#define USE_MAP_BUFFER
//! renders imgui drawlist
void Renderer::render( ImDrawData* draw_data )
{
const float width = ImGui::GetIO().DisplaySize.x;
const float height = ImGui::GetIO().DisplaySize.y;
const auto &vao = getVao();
const auto &vbo = getVbo();
const auto &shader = getGlslProg();
auto ctx = gl::context();
const mat4 mat =
{
{ 2.0f/width, 0.0f, 0.0f, 0.0f },
{ 0.0f, 2.0f/-height, 0.0f, 0.0f },
{ 0.0f, 0.0f, -1.0f, 0.0f },
{ -1.0f, 1.0f, 0.0f, 1.0f },
};
gl::ScopedVao scopedVao( vao.get() );
gl::ScopedBuffer scopedVbo( GL_ARRAY_BUFFER, vbo->getId() );
gl::ScopedBuffer scopedIbo( GL_ELEMENT_ARRAY_BUFFER, mIbo->getId() );
gl::ScopedGlslProg scopedShader( shader );
gl::ScopedDepth scopedDepth( false );
gl::ScopedBlendAlpha scopedBlend;
gl::ScopedFaceCulling scopedFaceCulling( false );
shader->uniform( "uModelViewProjection", mat );
GLuint currentTextureId = 0;
ctx->pushTextureBinding( GL_TEXTURE_2D, CINDER_IMGUI_TEXTURE_UNIT );
ctx->pushBoolState( GL_SCISSOR_TEST, GL_TRUE );
ctx->pushScissor();
for (int n = 0; n < draw_data->CmdListsCount; n++) {
const ImDrawList* cmd_list = draw_data->CmdLists[n];
const ImDrawIdx* idx_buffer_offset = 0;
#if defined( USE_MAP_BUFFER )
// grow vertex buffer if needed
int needed_vtx_size = cmd_list->VtxBuffer.size() * sizeof(ImDrawVert);
if ( vbo->getSize() < needed_vtx_size) {
GLsizeiptr size = needed_vtx_size + 2000 * sizeof(ImDrawVert);
#ifndef CINDER_LINUX_EGL_RPI2
mVbo->bufferData( size, nullptr, GL_STREAM_DRAW );
#else
mVbo->bufferData( size, nullptr, GL_DYNAMIC_DRAW );
#endif
}
// update vertex data
{
ImDrawVert *vtx_data = static_cast<ImDrawVert*>( vbo->mapReplace() );
if (!vtx_data)
continue;
memcpy( vtx_data, &cmd_list->VtxBuffer[0], cmd_list->VtxBuffer.size() * sizeof(ImDrawVert) );
vbo->unmap();
}
// grow index buffer if needed
int needed_idx_size = cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx);
if( mIbo->getSize() < needed_idx_size ) {
GLsizeiptr size = needed_idx_size + 2000 * sizeof(ImDrawIdx);
#if ! defined( CINDER_LINUX_EGL_RPI2 )
mIbo->bufferData( size, nullptr, GL_STREAM_DRAW );
#else
mIbo->bufferData( size, nullptr, GL_DYNAMIC_DRAW );
#endif
}
// update index data
{
ImDrawIdx *idx_data = static_cast<ImDrawIdx*>( mIbo->mapReplace() );
if( ! idx_data )
continue;
memcpy( idx_data, &cmd_list->IdxBuffer[0], cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx) );
mIbo->unmap();
}
#else
#if ! defined( CINDER_LINUX_EGL_RPI2 )
mVbo->bufferData( (GLsizeiptr)cmd_list->VtxBuffer.Size * sizeof(ImDrawVert), (const GLvoid*)cmd_list->VtxBuffer.Data, GL_STREAM_DRAW );
mIbo->bufferData( (GLsizeiptr)cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx), (const GLvoid*)cmd_list->IdxBuffer.Data, GL_STREAM_DRAW );
#else
mVbo->bufferData( (GLsizeiptr)cmd_list->VtxBuffer.Size * sizeof(ImDrawVert), (const GLvoid*)cmd_list->VtxBuffer.Data, GL_DYNAMIC_DRAW );
mIbo->bufferData( (GLsizeiptr)cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx), (const GLvoid*)cmd_list->IdxBuffer.Data, GL_DYNAMIC_DRAW );
#endif
#endif
// issue draw commands
for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
{
const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
if (pcmd->UserCallback) {
pcmd->UserCallback(cmd_list, pcmd);
}
else {
bool pushTexture = currentTextureId != (GLuint)(intptr_t) pcmd->TextureId;
if( pushTexture ) {
currentTextureId = (GLuint)(intptr_t) pcmd->TextureId;
ctx->bindTexture( GL_TEXTURE_2D, currentTextureId, CINDER_IMGUI_TEXTURE_UNIT );
}
ctx->setScissor( { ivec2( (int)pcmd->ClipRect.x, (int)(height - pcmd->ClipRect.w) ), ivec2( (int)(pcmd->ClipRect.z - pcmd->ClipRect.x), (int)(pcmd->ClipRect.w - pcmd->ClipRect.y) ) } );
gl::drawElements( GL_TRIANGLES, (GLsizei)pcmd->ElemCount, sizeof(ImDrawIdx) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT, idx_buffer_offset );
}
idx_buffer_offset += pcmd->ElemCount;
}
}
ctx->popScissor();
ctx->popBoolState( GL_SCISSOR_TEST );
ctx->popTextureBinding( GL_TEXTURE_2D, CINDER_IMGUI_TEXTURE_UNIT );
}
//! initializes and returns the font texture
gl::TextureRef Renderer::getFontTextureRef()
{
if( !mFontTexture ){
initFontTexture();
}
return mFontTexture;
}
//! initializes and returns the vbo mesh
gl::VaoRef Renderer::getVao()
{
if( !mVao ){
initBuffers();
}
return mVao;
}
//! initializes and returns the vbo
gl::VboRef Renderer::getVbo()
{
if( !mVbo ){
initBuffers();
}
return mVbo;
}
//! initializes the vbo mesh
void Renderer::initBuffers( size_t size )
{
#if ! defined( CINDER_LINUX_EGL_RPI2 )
mVbo = gl::Vbo::create( GL_ARRAY_BUFFER, size, nullptr, GL_STREAM_DRAW );
mIbo = gl::Vbo::create( GL_ELEMENT_ARRAY_BUFFER, 10, nullptr, GL_STREAM_DRAW );
#else
mVbo = gl::Vbo::create( GL_ARRAY_BUFFER, size, nullptr, GL_DYNAMIC_DRAW );
mIbo = gl::Vbo::create( GL_ELEMENT_ARRAY_BUFFER, 10, nullptr, GL_DYNAMIC_DRAW );
#endif
mVao = gl::Vao::create();
gl::ScopedVao mVaoScope( mVao );
gl::ScopedBuffer mVboScope( mVbo );
gl::enableVertexAttribArray( 0 );
gl::enableVertexAttribArray( 1 );
gl::enableVertexAttribArray( 2 );
gl::vertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (const GLvoid*)offsetof(ImDrawVert, pos) );
gl::vertexAttribPointer( 1, 2, GL_FLOAT, GL_FALSE, sizeof(ImDrawVert), (const GLvoid*)offsetof(ImDrawVert, uv) );
gl::vertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(ImDrawVert), (const GLvoid*)offsetof(ImDrawVert, col) );
}
//! initalizes the font texture
void Renderer::initFontTexture()
{
unsigned char* pixels;
int width, height;
ImGui::GetIO().Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
mFontTexture = gl::Texture::create( pixels, GL_RGBA, width, height, gl::Texture::Format().magFilter(GL_LINEAR).minFilter(GL_LINEAR) );
ImGui::GetIO().Fonts->ClearTexData();
ImGui::GetIO().Fonts->TexID = (void *)(intptr_t) mFontTexture->getId();
}
/*ImFont* addFont( ci::DataSourceRef font, float size, int fontId )
{
ImFont* newFont = getRenderer()->addFont( font, size, fontId );
return newFont;
}
*/
ImFont* Renderer::addFont( const ci::fs::path &font, float size, const ImWchar* glyph_ranges, bool merge )
{
ImFontAtlas* fontAtlas = ImGui::GetIO().Fonts;
auto fontSource = loadFile( font );
Font ciFont( fontSource, size );
ImWchar* glyphRanges = NULL;
// if we have glyph ranges copy them
if( glyph_ranges && glyph_ranges[0] && glyph_ranges[1] ){
mFontsGlyphRanges.push_back( vector<ImWchar>() );
auto &ranges = mFontsGlyphRanges.back();
for( const ImWchar* range = glyph_ranges; range[0] && range[1]; range += 2 ) {
ranges.push_back( range[0] );
ranges.push_back( range[1] );
}
ranges.push_back( 0 );
glyphRanges = ranges.data();
}
// otherwise get them from the font
else if( ciFont.getNumGlyphs() ) {
int numGlyphs = ciFont.getNumGlyphs();
// copy glyphs so we can sort them
vector<ImWchar> glyphs;
for( size_t i = 0; i < numGlyphs; ++i ) {
glyphs.push_back( ciFont.getGlyphIndex( i ) );
}
sort( glyphs.begin(), glyphs.end() );
// find glyph ranges
mFontsGlyphRanges.push_back( vector<ImWchar>() );
auto &ranges = mFontsGlyphRanges.back();
Font::Glyph start = glyphs[0] == 0 ? '0' : glyphs[0];
for( size_t i = 1; i < numGlyphs; ++i ) {
if( glyphs[i] != glyphs[i-1] + 1 ) {
ranges.push_back( start );
ranges.push_back( glyphs[i-1] );
start = glyphs[i];
}
}
if( ranges.empty() || ranges.back() != glyphs[numGlyphs-1] ) {
ranges.push_back( start );
ranges.push_back( glyphs[numGlyphs-1] );
}
ranges.push_back( '\0' );
glyphRanges = ranges.data();
}
BufferRef buffer = fontSource->getBuffer();
void* bufferCopy = (void*) malloc( buffer->getSize() );
memcpy( bufferCopy, buffer->getData(), buffer->getSize() );
ImFontConfig config;
if( merge && ! mFonts.empty() ) {
config.MergeMode = true;
config.PixelSnapH = true;
//config.OversampleV = 4;
}
/*else if( merge ) {
fontAtlas->AddFontDefault();
config.MergeMode = true;
config.PixelSnapH = true;
}*/
ImFont* newFont = fontAtlas->AddFontFromMemoryTTF( bufferCopy, buffer->getSize(), size, &config, glyphRanges );
//ImFont* newFont = fontAtlas->AddFontFromFileTTF( font.string().c_str(), size, &config, glyphRanges );
mFonts.insert( make_pair( font.stem().string(), newFont ) );
return newFont;
}
void Renderer::clearFonts()
{
mFonts.clear();
}
//! initalizes and returns the shader
gl::GlslProgRef Renderer::getGlslProg()
{
if( !mShader ){
initGlslProg();
}
return mShader;
}
//! initalizes the shader
void Renderer::initGlslProg()
{
try {
mShader = gl::GlslProg::create( gl::GlslProg::Format()
.vertex(
#if defined(CINDER_GL_ES_2)
R"(
precision highp float;
uniform mat4 uModelViewProjection;
attribute vec2 iPosition;
attribute vec2 iUv;
attribute vec4 iColor;
varying vec2 vUv;
varying vec4 vColor;
void main() {
vColor = iColor;
vUv = iUv;
gl_Position = uModelViewProjection * vec4( iPosition, 0.0, 1.0 );
} )"
#elif defined(CINDER_GL_ES_3)
R"(
#version 300 es
precision highp float;
uniform mat4 uModelViewProjection;
in vec2 iPosition;
in vec2 iUv;
in vec4 iColor;
out vec2 vUv;
out vec4 vColor;
void main() {
vColor = iColor;
vUv = iUv;
gl_Position = uModelViewProjection * vec4( iPosition, 0.0, 1.0 );
} )"
#else
R"(
#version 150
uniform mat4 uModelViewProjection;
in vec2 iPosition;
in vec2 iUv;
in vec4 iColor;
out vec2 vUv;
out vec4 vColor;
void main() {
vColor = iColor;
vUv = iUv;
gl_Position = uModelViewProjection * vec4( iPosition, 0.0, 1.0 );
} )"
#endif
)
.fragment(
#if defined(CINDER_GL_ES_2)
R"(
precision highp float;
varying highp vec2 vUv;
varying highp vec4 vColor;
uniform sampler2D uTex;
void main() {
vec4 color = texture2D( uTex, vUv ) * vColor;
gl_FragColor = color;
} )"
#elif defined(CINDER_GL_ES_3)
R"(
#version 300 es
precision highp float;
in highp vec2 vUv;
in highp vec4 vColor;
out highp vec4 oColor;
uniform sampler2D uTex;
void main() {
vec4 color = texture( uTex, vUv ) * vColor;
oColor = color;
} )"
#else
R"(
#version 150
in vec2 vUv;
in vec4 vColor;
out vec4 oColor;
uniform sampler2D uTex;
void main() {
vec4 color = texture( uTex, vUv ) * vColor;
oColor = color;
} )"
#endif
)
.attribLocation( "iPosition", 0 )
.attribLocation( "iUv", 1 )
.attribLocation( "iColor", 2 )
);
mShader->uniform( "uTex", CINDER_IMGUI_TEXTURE_UNIT );
}
catch( gl::GlslProgCompileExc exc ){
CI_LOG_E( "Problem Compiling ImGui::Renderer shader " << exc.what() );
}
}
ImFont* Renderer::getFont( const std::string &name )
{
if( !mFonts.count( name ) ){
return nullptr;
}
else {
return mFonts[name];
}
}
typedef std::shared_ptr<Renderer> RendererRef;
RendererRef getRenderer()
{
static RendererRef renderer = RendererRef( new Renderer() );
return renderer;
}
// Cinder Helpers
void Image( const ci::gl::Texture2dRef &texture, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, const ImVec4& tint_col, const ImVec4& border_col )
{
Image( (void*)(intptr_t) texture->getId(), size, uv0, uv1, tint_col, border_col );
}
bool ImageButton( const ci::gl::Texture2dRef &texture, const ImVec2& size, const ImVec2& uv0, const ImVec2& uv1, int frame_padding, const ImVec4& bg_col, const ImVec4& tint_col)
{
return ImageButton( (void*)(intptr_t) texture->getId(), size, uv0, uv1, frame_padding, bg_col, tint_col );
}
void PushFont( const std::string& name )
{
auto renderer = getRenderer();
ImFont* font = renderer->getFont( name );
CI_ASSERT( font != nullptr );
PushFont( font );
}
// Std Helpers
bool ListBox( const char* label, int* current_item, const std::vector<std::string>& items, int height_in_items )
{
// copy names to a vector
vector<const char*> names;
for( auto item : items ) {
char *cname = new char[ item.size() + 1 ];
std::strcpy( cname, item.c_str() );
names.push_back( cname );
}
bool result = ListBox( label, current_item, names.data(), names.size(), height_in_items );
// cleanup
for( auto &name : names ) delete [] name;
return result;
}
bool InputText( const char* label, std::string* buf, ImGuiInputTextFlags flags, ImGuiTextEditCallback callback, void* user_data )
{
// conversion
char *buffer = new char[buf->size()+128];
std::strcpy( buffer, buf->c_str() );
bool result = InputText( label, buffer, buf->size()+128, flags, callback, user_data );
if( result ){
*buf = string( buffer );
}
// cleanup
delete [] buffer;
return result;
}
bool InputTextMultiline( const char* label, std::string* buf, const ImVec2& size, ImGuiInputTextFlags flags, ImGuiTextEditCallback callback, void* user_data )
{
// conversion
constexpr size_t extraSpace = 16384;
char *buffer = new char[buf->size()+extraSpace];
std::strcpy( buffer, buf->c_str() );
bool result = InputTextMultiline( label, buffer, buf->size()+extraSpace, size, flags, callback, user_data );
if( result ){
*buf = string( buffer );
}
// cleanup
delete [] buffer;
return result;
}
namespace {
//! sets the right mouseDown IO values in imgui
void mouseDown( ci::app::MouseEvent& event )
{
ImGuiIO& io = ImGui::GetIO();
io.MousePos = toPixels( event.getPos() );
if( event.isLeftDown() ){
io.MouseDown[0] = true;
io.MouseDown[1] = false;
}
else if( event.isRightDown() ){
io.MouseDown[0] = false;
io.MouseDown[1] = true;
}
event.setHandled( io.WantCaptureMouse );
}
//! sets the right mouseMove IO values in imgui
void mouseMove( ci::app::MouseEvent& event )
{
ImGuiIO& io = ImGui::GetIO();
io.MousePos = toPixels( event.getPos() );
event.setHandled( io.WantCaptureMouse );
}
//! sets the right mouseDrag IO values in imgui
void mouseDrag( ci::app::MouseEvent& event )
{
ImGuiIO& io = ImGui::GetIO();
io.MousePos = toPixels( event.getPos() );
event.setHandled( io.WantCaptureMouse );
}
//! sets the right mouseDrag IO values in imgui
void mouseUp( ci::app::MouseEvent& event )
{
ImGuiIO& io = ImGui::GetIO();
io.MouseDown[0] = false;
io.MouseDown[1] = false;
event.setHandled( io.WantCaptureMouse );
}
//! sets the right mouseWheel IO values in imgui
void mouseWheel( ci::app::MouseEvent& event )
{
ImGuiIO& io = ImGui::GetIO();
io.MouseWheel += event.getWheelIncrement();
event.setHandled( io.WantCaptureMouse );
}
vector<int> sAccelKeys;
//! sets the right keyDown IO values in imgui
void keyDown( ci::app::KeyEvent& event )
{
ImGuiIO& io = ImGui::GetIO();
#if defined CINDER_LINUX
auto character = event.getChar();
#else
uint32_t character = event.getCharUtf32();
#endif
io.KeysDown[event.getCode()] = true;
if ( !event.isAccelDown() && character > 0 && character <= 255 ) {
io.AddInputCharacter( ( char ) character );
}
else if( event.getCode() != KeyEvent::KEY_LMETA
&& event.getCode() != KeyEvent::KEY_RMETA
&& event.isAccelDown()
&& find( sAccelKeys.begin(), sAccelKeys.end(), event.getCode() ) == sAccelKeys.end() ){
sAccelKeys.push_back( event.getCode() );
}
io.KeyCtrl = io.KeysDown[KeyEvent::KEY_LCTRL] || io.KeysDown[KeyEvent::KEY_RCTRL];
io.KeyShift = io.KeysDown[KeyEvent::KEY_LSHIFT] || io.KeysDown[KeyEvent::KEY_RSHIFT];
io.KeyAlt = io.KeysDown[KeyEvent::KEY_LALT] || io.KeysDown[KeyEvent::KEY_RALT];
io.KeySuper = io.KeysDown[KeyEvent::KEY_LMETA] || io.KeysDown[KeyEvent::KEY_RMETA] || io.KeysDown[KeyEvent::KEY_LSUPER] || io.KeysDown[KeyEvent::KEY_RSUPER];
event.setHandled( io.WantCaptureKeyboard );
}
//! sets the right keyUp IO values in imgui
void keyUp( ci::app::KeyEvent& event )
{
ImGuiIO& io = ImGui::GetIO();
io.KeysDown[event.getCode()] = false;
for( auto key : sAccelKeys ){
io.KeysDown[key] = false;
}
sAccelKeys.clear();
io.KeyCtrl = io.KeysDown[KeyEvent::KEY_LCTRL] || io.KeysDown[KeyEvent::KEY_RCTRL];
io.KeyShift = io.KeysDown[KeyEvent::KEY_LSHIFT] || io.KeysDown[KeyEvent::KEY_RSHIFT];
io.KeyAlt = io.KeysDown[KeyEvent::KEY_LALT] || io.KeysDown[KeyEvent::KEY_RALT];
io.KeySuper = io.KeysDown[KeyEvent::KEY_LMETA] || io.KeysDown[KeyEvent::KEY_RMETA] || io.KeysDown[KeyEvent::KEY_LSUPER] || io.KeysDown[KeyEvent::KEY_RSUPER];
event.setHandled( io.WantCaptureKeyboard );
}
static bool sNewFrame = false;
void newFrameGuard();
void render()
{
static auto timer = ci::Timer(true);
ImGuiIO& io = ImGui::GetIO();
io.DeltaTime = timer.getSeconds();
if( io.DeltaTime < 0.0f ) {
CI_LOG_W("WARNING: overriding imgui deltatime because it is " << io.DeltaTime);
io.DeltaTime = 1.0f/60.0f;
}
timer.start();
ImGui::Render();
auto renderer = getRenderer();
renderer->render( ImGui::GetDrawData() );
sNewFrame = false;
App::get()->dispatchAsync( []() {
newFrameGuard();
} );
}
void newFrameGuard()
{
if( ! sNewFrame ){
ImGui::NewFrame();
sNewFrame = true;
}
}
void resize()
{
ImGuiIO& io = ImGui::GetIO();
io.DisplaySize = toPixels( getWindowSize() );
newFrameGuard();
}
void resetKeys()
{
for( int i = 0; i < IM_ARRAYSIZE( ImGui::GetIO().KeysDown ); i++ ) {
ImGui::GetIO().KeysDown[i] = false;
}
for( int i = 0; i < IM_ARRAYSIZE( ImGui::GetIO().KeysDownDuration ); i++ ) {
ImGui::GetIO().KeysDownDuration[i] = 0.0f;
}
for( int i = 0; i < IM_ARRAYSIZE( ImGui::GetIO().KeysDownDurationPrev ); i++ ) {
ImGui::GetIO().KeysDownDurationPrev[i] = 0.0f;
}
ImGui::GetIO().KeyCtrl = false;
ImGui::GetIO().KeyShift = false;
ImGui::GetIO().KeyAlt = false;
}
} // Anonymous namespace
// wrong... and would not work in a multi-windows scenario
static signals::ConnectionList sWindowConnections;
// also wrong... but fixes a crash on cleanup
static signals::ConnectionList sAppConnections;
void initialize( const Options &options )
{
// create one context for now. will update with multiple context / shared fontatlas soon!
ImGuiContext* context = ImGui::CreateContext();
// get the window and switch to its context before initializing the renderer
auto window = options.getWindow();
auto currentContext = gl::context();
window->getRenderer()->makeCurrentContext();
auto renderer = getRenderer();
// set style
const ImGuiStyle& style = options.getStyle();
ImGuiStyle& imGuiStyle = ImGui::GetStyle();
imGuiStyle.Alpha = style.Alpha;
imGuiStyle.WindowPadding = style.WindowPadding;
imGuiStyle.WindowMinSize = style.WindowMinSize;
imGuiStyle.WindowRounding = style.WindowRounding;
imGuiStyle.WindowTitleAlign = style.WindowTitleAlign;
imGuiStyle.ChildRounding = style.ChildRounding;
imGuiStyle.FramePadding = style.FramePadding;
imGuiStyle.FrameRounding = style.FrameRounding;
imGuiStyle.ItemSpacing = style.ItemSpacing;
imGuiStyle.ItemInnerSpacing = style.ItemInnerSpacing;
imGuiStyle.TouchExtraPadding = style.TouchExtraPadding;
imGuiStyle.IndentSpacing = style.IndentSpacing;