-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathimgui_impl_cocos2dx.cpp
1563 lines (1426 loc) · 58 KB
/
imgui_impl_cocos2dx.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
#include "imgui_impl_cocos2dx.h"
#include "cocos2d.h"
#include "renderer/backend/Backend.h"
#ifdef CC_USE_GFX
#include "renderer/backend/gfx/DeviceGFX.h"
#endif
#ifdef CC_PLATFORM_PC
// GLFW
#ifdef _WIN32
#include "glfw3.h"
#undef APIENTRY
#ifndef GLFW_EXPOSE_NATIVE_WIN32
#define GLFW_EXPOSE_NATIVE_WIN32
#endif
#include "glfw3native.h"
#else
#include <glfw3.h>
#endif // _WIN32
static_assert(
GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300,
"glfw version should be 3.3+");
#define GLFW_HAS_WINDOW_TOPMOST (1) // 3.2+ GLFW_FLOATING
#define GLFW_HAS_WINDOW_HOVERED (1) // 3.3+ GLFW_HOVERED
#define GLFW_HAS_WINDOW_ALPHA (1) // 3.3+ glfwSetWindowOpacity
#define GLFW_HAS_PER_MONITOR_DPI (1) // 3.3+ glfwGetMonitorContentScale
#define GLFW_HAS_VULKAN (1) // 3.2+ glfwCreateWindowSurface
#define GLFW_HAS_FOCUS_WINDOW (1) // 3.2+ glfwFocusWindow
#define GLFW_HAS_FOCUS_ON_SHOW (1) // 3.3+ GLFW_FOCUS_ON_SHOW
#define GLFW_HAS_MONITOR_WORK_AREA (1) // 3.3+ glfwGetMonitorWorkarea
// 3.3.1+ Fixed: Resizing window repositions it on MacOS #1553
#define GLFW_HAS_OSX_WINDOW_POS_FIX (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 + GLFW_VERSION_REVISION * 10 >= 3310)
// Let's be nice to people who pulled GLFW between 2019-04-16 (3.4 define) and 2019-11-29 (cursors defines) // FIXME: Remove when GLFW 3.4 is released?
#ifdef GLFW_RESIZE_NESW_CURSOR
// 3.4+ GLFW_RESIZE_ALL_CURSOR, GLFW_RESIZE_NESW_CURSOR, GLFW_RESIZE_NWSE_CURSOR, GLFW_NOT_ALLOWED_CURSOR
#define GLFW_HAS_NEW_CURSORS (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3400)
#else
#define GLFW_HAS_NEW_CURSORS (0)
#endif
// Let's be nice to people who pulled GLFW between 2019-04-16 (3.4 define) and 2020-07-17 (passthrough)
#ifdef GLFW_MOUSE_PASSTHROUGH
// 3.4+ GLFW_MOUSE_PASSTHROUGH
#define GLFW_HAS_MOUSE_PASSTHROUGH (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3400)
#else
#define GLFW_HAS_MOUSE_PASSTHROUGH (0)
#endif
#endif // CC_PLATFORM_PC
#if defined(CC_USE_GL) && defined(CC_PLATFORM_PC) && !defined(CC_USE_GFX)
#define IMPL_MULTI_WINDOW
#endif
using namespace cocos2d;
using namespace backend;
// GLFW data
static double g_Time = 0.0;
static bool g_MouseJustPressed[5] = { false, false, false, false, false };
static ImVec2 g_CursorPos = ImVec2(-FLT_MAX, -FLT_MAX);
#ifdef CC_PLATFORM_PC
static GLFWcursor* g_MouseCursors[ImGuiMouseCursor_COUNT] = { nullptr };
static GLFWwindow* g_KeyOwnerWindows[512] = { nullptr };
// Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any.
static GLFWmousebuttonfun g_PrevUserCallbackMousebutton = nullptr;
static GLFWscrollfun g_PrevUserCallbackScroll = nullptr;
static GLFWkeyfun g_PrevUserCallbackKey = nullptr;
static GLFWcharfun g_PrevUserCallbackChar = nullptr;
static GLFWmonitorfun g_PrevUserCallbackMonitor = nullptr;
static bool g_WantUpdateMonitors = true;
static bool g_InstalledCallbacks = false;
// Forward Declarations
static void ImGui_ImplGlfw_InitPlatformInterface();
static void ImGui_ImplGlfw_ShutdownPlatformInterface();
static void ImGui_ImplGlfw_UpdateMonitors();
#endif // CC_PLATFORM_PC
struct ProgramInfo
{
cocos2d::backend::Program* program = nullptr;
// Uniforms location
UniformLocation texture;
UniformLocation projection;
// Vertex attributes location
int position = 0;
int uv = 0;
int color = 0;
cocos2d::backend::VertexLayout layout;
};
static ProgramInfo g_ProgramInfo;
static ProgramInfo g_ProgramFontInfo;
static Texture2D* g_FontTexture = nullptr;
static Mat4 g_Projection;
constexpr IndexFormat g_IndexFormat = sizeof(ImDrawIdx) == 2 ? IndexFormat::U_SHORT : IndexFormat::U_INT;
static std::vector<std::shared_ptr<CallbackCommand>> g_CallbackCommands;
static std::vector<std::shared_ptr<CustomCommand>> g_CustomCommands;
static Vector<ProgramState*> g_ProgramStates;
static void AddRendererCommand(const std::function<void()>& f)
{
const auto renderer = Director::getInstance()->getRenderer();
#ifdef CC_VERSION
auto cmd = renderer->nextCallbackCommand();
cmd->func = f;
renderer->addCommand(cmd);
#else
auto cmd = std::make_shared<CallbackCommand>();
g_CallbackCommands.push_back(cmd);
cmd->init(0.f);
cmd->func = f;
renderer->addCommand(cmd.get());
#endif // CC_VERSION
}
#ifdef CC_PLATFORM_PC
static GLFWwindow* ImGui_ImplCocos2dx_GetWindow()
{
const auto glv = (GLViewImpl*)Director::getInstance()->getOpenGLView();
return glv->getWindow();
}
#endif // CC_PLATFORM_PC
static void ImGui_ImplCocos2dx_SetupRenderState(ImDrawData* draw_data, int fb_width, int fb_height)
{
const auto renderer = Director::getInstance()->getRenderer();
// setup
AddRendererCommand([=]()
{
renderer->setCullMode(backend::CullMode::NONE);
renderer->setScissorTest(true);
renderer->setDepthTest(false);
renderer->setStencilTest(false);
renderer->setViewPort(0, 0, fb_width, fb_height);
});
const auto L = draw_data->DisplayPos.x;
const auto R = draw_data->DisplayPos.x + draw_data->DisplaySize.x;
const auto T = draw_data->DisplayPos.y;
const auto B = draw_data->DisplayPos.y + draw_data->DisplaySize.y;
Mat4::createOrthographicOffCenter(L, R, B, T, -1.f, 1.f, &g_Projection);
}
struct SavedRenderState
{
backend::CullMode cull;
Viewport vp;
ScissorRect scissorRect;
bool scissorTest;
bool depthTest;
bool stencilTest;
};
static SavedRenderState g_SavedRenderState;
void ImGui_ImplCocos2dx_RenderDrawData(ImDrawData* draw_data)
{
// Avoid rendering when minimized, scale coordinates for retina displays
// (screen coordinates != framebuffer coordinates)
int fb_width = (int)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x);
int fb_height = (int)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y);
if (fb_width <= 0 || fb_height <= 0)
return;
const auto renderer = Director::getInstance()->getRenderer();
// store
AddRendererCommand([renderer]()
{
g_SavedRenderState.cull = renderer->getCullMode();
g_SavedRenderState.vp = renderer->getViewport();
g_SavedRenderState.scissorTest = renderer->getScissorTest();
g_SavedRenderState.scissorRect = renderer->getScissorRect();
g_SavedRenderState.depthTest = renderer->getDepthTest();
g_SavedRenderState.stencilTest = renderer->getStencilTest();
});
ImGui_ImplCocos2dx_SetupRenderState(draw_data, fb_width, fb_height);
// Will project scissor/clipping rectangles into framebuffer space
ImVec2 clip_off = draw_data->DisplayPos; // (0,0) unless using multi-viewports
ImVec2 clip_scale = draw_data->FramebufferScale; // (1,1) unless using retina display which are often (2,2)
// Render command lists
for (int n = 0; n < draw_data->CmdListsCount; n++)
{
const ImDrawList* cmd_list = draw_data->CmdLists[n];
size_t ibuffer_offset = 0;
// Upload vertex/index buffers
#ifdef CC_USE_GFX
const auto device = static_cast<backend::DeviceGFX*>(backend::Device::getInstance());
const auto vsize = cmd_list->VtxBuffer.Size * sizeof(ImDrawVert);
IM_ASSERT(vsize > 0);
auto vbuffer = device->newBuffer(
vsize, sizeof(ImDrawVert), BufferType::VERTEX, BufferUsage::STATIC);
const auto isize = cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx);
IM_ASSERT(isize > 0);
auto ibuffer = device->newBuffer(
isize, sizeof(ImDrawIdx), BufferType::INDEX, BufferUsage::STATIC);
#else
const auto vsize = cmd_list->VtxBuffer.Size * sizeof(ImDrawVert);
IM_ASSERT(vsize > 0);
auto vbuffer = backend::Device::getInstance()->newBuffer(
vsize, BufferType::VERTEX, BufferUsage::STATIC);
const auto isize = cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx);
IM_ASSERT(isize > 0);
auto ibuffer = backend::Device::getInstance()->newBuffer(
isize, BufferType::INDEX, BufferUsage::STATIC);
#endif
vbuffer->autorelease();
vbuffer->updateData(cmd_list->VtxBuffer.Data, vsize);
ibuffer->autorelease();
ibuffer->updateData(cmd_list->IdxBuffer.Data, isize);
for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++)
{
const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
if (pcmd->UserCallback != nullptr)
{
// User callback, registered via ImDrawList::AddCallback()
// (ImDrawCallback_ResetRenderState is a special callback value used by the user
// to request the renderer to reset render state.)
if (pcmd->UserCallback == ImDrawCallback_ResetRenderState)
ImGui_ImplCocos2dx_SetupRenderState(draw_data, fb_width, fb_height);
else
{
AddRendererCommand([=]()
{
pcmd->UserCallback(cmd_list, pcmd);
});
}
}
else
{
// Project scissor/clipping rectangles into framebuffer space
ImVec4 clip_rect;
clip_rect.x = (pcmd->ClipRect.x - clip_off.x) * clip_scale.x;
clip_rect.y = (pcmd->ClipRect.y - clip_off.y) * clip_scale.y;
clip_rect.z = (pcmd->ClipRect.z - clip_off.x) * clip_scale.x;
clip_rect.w = (pcmd->ClipRect.w - clip_off.y) * clip_scale.y;
if (clip_rect.x < fb_width && clip_rect.y < fb_height &&
clip_rect.z >= 0.0f && clip_rect.w >= 0.0f)
{
// Apply scissor/clipping rectangle
AddRendererCommand([=]()
{
renderer->setScissorRect(
clip_rect.x,
fb_height - clip_rect.w,
clip_rect.z - clip_rect.x,
clip_rect.w - clip_rect.y);
});
if (typeid(*((Ref*)pcmd->TextureId)) == typeid(Texture2D))
{
auto tex = (Texture2D*)pcmd->TextureId;
auto cmd = std::make_shared<CustomCommand>();
g_CustomCommands.push_back(cmd);
cmd->init(0.f, BlendFunc::ALPHA_NON_PREMULTIPLIED);
const auto pinfo = tex == g_FontTexture ? &g_ProgramFontInfo : &g_ProgramInfo;
// create new ProgramState
auto state = new ProgramState(pinfo->program);
state->autorelease();
g_ProgramStates.pushBack(state);
auto& desc = cmd->getPipelineDescriptor();
desc.programState = state;
#ifndef CC_USE_GFX
// setup attributes for ImDrawVert
*desc.programState->getVertexLayout() = pinfo->layout;
#endif
desc.programState->setUniform(pinfo->projection, &g_Projection, sizeof(Mat4));
desc.programState->setTexture(pinfo->texture, 0, tex->getBackendTexture());
// In order to composite our output buffer we need to preserve alpha
desc.blendDescriptor.sourceAlphaBlendFactor = BlendFactor::ONE;
// set vertex/index buffer
cmd->setIndexBuffer(ibuffer, g_IndexFormat);
cmd->setVertexBuffer(vbuffer);
cmd->setDrawType(CustomCommand::DrawType::ELEMENT);
cmd->setPrimitiveType(PrimitiveType::TRIANGLE);
cmd->setIndexDrawInfo(ibuffer_offset + pcmd->IdxOffset, pcmd->ElemCount);
renderer->addCommand(cmd.get());
}
else
{
auto node = (Node*)pcmd->TextureId;
const auto tr = node->getNodeToParentTransform();
node->setVisible(true);
node->setNodeToParentTransform(tr);
const auto& proj = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
node->visit(Director::getInstance()->getRenderer(), proj.getInversed() * g_Projection, 0);
node->setVisible(false);
}
}
}
}
}
// restore
AddRendererCommand([renderer]()
{
renderer->setCullMode(g_SavedRenderState.cull);
auto& vp = g_SavedRenderState.vp;
renderer->setViewPort(vp.x, vp.y, vp.w, vp.h);
renderer->setScissorTest(g_SavedRenderState.scissorTest);
auto& sc = g_SavedRenderState.scissorRect;
renderer->setScissorRect(sc.x, sc.y, sc.width, sc.height);
renderer->setDepthTest(g_SavedRenderState.depthTest);
renderer->setStencilTest(g_SavedRenderState.stencilTest);
});
}
void ImGui_ImplCocos2dx_RenderPlatform()
{
#ifdef CC_PLATFORM_PC
if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
GLFWwindow* backup_current_context = glfwGetCurrentContext();
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
glfwMakeContextCurrent(backup_current_context);
AddRendererCommand([=]()
{
glfwMakeContextCurrent(backup_current_context);
});
}
#endif
}
static const char* ImGui_ImplCocos2dx_GetClipboardText(void* user_data)
{
#ifdef CC_PLATFORM_PC
return glfwGetClipboardString((GLFWwindow*)user_data);
#else
return "";
#endif
}
static void ImGui_ImplCocos2dx_SetClipboardText(void* user_data, const char* text)
{
#ifdef CC_PLATFORM_PC
glfwSetClipboardString((GLFWwindow*)user_data, text);
#else
#endif
}
#ifdef CC_PLATFORM_PC
void ImGui_ImplCocos2dx_MouseButtonCallback(GLFWwindow* window, int button, int action, int mods)
{
if (g_PrevUserCallbackMousebutton != nullptr && window == ImGui_ImplCocos2dx_GetWindow())
g_PrevUserCallbackMousebutton(window, button, action, mods);
if (action == GLFW_PRESS && button >= 0 && button < IM_ARRAYSIZE(g_MouseJustPressed))
g_MouseJustPressed[button] = true;
}
void ImGui_ImplCocos2dx_ScrollCallback(GLFWwindow* window, double xoffset, double yoffset)
{
if (g_PrevUserCallbackScroll != nullptr && window == ImGui_ImplCocos2dx_GetWindow())
g_PrevUserCallbackScroll(window, xoffset, yoffset);
ImGuiIO& io = ImGui::GetIO();
io.MouseWheelH += (float)xoffset;
io.MouseWheel += (float)yoffset;
}
void ImGui_ImplCocos2dx_KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (g_PrevUserCallbackKey != nullptr && window == ImGui_ImplCocos2dx_GetWindow())
g_PrevUserCallbackKey(window, key, scancode, action, mods);
if (key < 0)
return;
ImGuiIO& io = ImGui::GetIO();
if (action == GLFW_PRESS) {
io.KeysDown[key] = true;
g_KeyOwnerWindows[key] = window;
}
if (action == GLFW_RELEASE) {
io.KeysDown[key] = false;
g_KeyOwnerWindows[key] = nullptr;
}
// Modifiers are not reliable across systems
io.KeyCtrl = io.KeysDown[GLFW_KEY_LEFT_CONTROL] || io.KeysDown[GLFW_KEY_RIGHT_CONTROL];
io.KeyShift = io.KeysDown[GLFW_KEY_LEFT_SHIFT] || io.KeysDown[GLFW_KEY_RIGHT_SHIFT];
io.KeyAlt = io.KeysDown[GLFW_KEY_LEFT_ALT] || io.KeysDown[GLFW_KEY_RIGHT_ALT];
#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
io.KeySuper = false;
#else
io.KeySuper = io.KeysDown[GLFW_KEY_LEFT_SUPER] || io.KeysDown[GLFW_KEY_RIGHT_SUPER];
#endif
}
void ImGui_ImplCocos2dx_CharCallback(GLFWwindow* window, unsigned int c)
{
if (g_PrevUserCallbackChar != nullptr && window == ImGui_ImplCocos2dx_GetWindow())
g_PrevUserCallbackChar(window, c);
ImGuiIO& io = ImGui::GetIO();
io.AddInputCharacter(c);
}
void ImGui_ImplGlfw_MonitorCallback(GLFWmonitor*, int)
{
g_WantUpdateMonitors = true;
}
#endif
bool ImGui_ImplCocos2dx_CreateFontsTexture()
{
// Build texture atlas
ImGuiIO& io = ImGui::GetIO();
unsigned char* pixels;
int width, height;
// Load as RGBA 32-bits (75% of the memory is wasted, but default font is so small)
// because it is more likely to be compatible with user's existing shaders.
// If your ImTextureId represent a higher-level concept than just a GL texture id,
// consider calling GetTexDataAsAlpha8() instead to save on GPU memory.
io.Fonts->GetTexDataAsAlpha8(&pixels, &width, &height);
CC_SAFE_RELEASE(g_FontTexture);
g_FontTexture = new Texture2D();
g_FontTexture->setAntiAliasTexParameters();
#ifdef CC_USE_GFX
g_FontTexture->initWithData(pixels, width * height,
backend::PixelFormat::A8, width, height);
#else
g_FontTexture->initWithData(pixels, width*height,
backend::PixelFormat::A8, width, height, cocos2d::Size(width, height));
#endif
io.Fonts->SetTexID((ImTextureID)g_FontTexture);
return true;
}
void ImGui_ImplCocos2dx_DestroyFontsTexture()
{
if (g_FontTexture)
{
ImGui::GetIO().Fonts->SetTexID(nullptr);
CC_SAFE_DELETE(g_FontTexture);
}
}
bool ImGui_ImplCocos2dx_CreateDeviceObjects()
{
static auto vertex_shader = R"(
#ifndef GL_ES
#define lowp
#define mediump
#endif
#if __VERSION__ >= 300
layout(location=0) in vec2 a_position;
layout(location=1) in vec2 a_texCoord;
layout(location=2) in vec4 a_color;
layout(std140, binding=0) uniform VSBlock
{
mat4 u_MVPMatrix;
};
layout(location=0) out lowp vec4 v_fragmentColor;
layout(location=1) out mediump vec2 v_texCoord;
#else
attribute vec2 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord;
uniform mat4 u_MVPMatrix;
varying lowp vec4 v_fragmentColor;
varying mediump vec2 v_texCoord;
#endif
void main()
{
gl_Position = u_MVPMatrix * vec4(a_position.xy, 0.0, 1.0);
v_fragmentColor = a_color;
v_texCoord = a_texCoord;
}
)";
static auto fragment_shader = R"(
#ifdef GL_ES
precision lowp float;
#endif
#if __VERSION__ >= 300
layout(location=0) in vec4 v_fragmentColor;
layout(location=1) in vec2 v_texCoord;
layout(binding=2) uniform sampler2D u_texture;
layout(location=0) out vec4 cc_FragColor;
#else
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
uniform sampler2D u_texture;
#endif
void main()
{
#if __VERSION__ >= 300
cc_FragColor = v_fragmentColor * texture(u_texture, v_texCoord);
#else
gl_FragColor = v_fragmentColor * texture2D(u_texture, v_texCoord);
#endif
}
)";
#ifdef CC_USE_GFX
static auto fragment_shader_font = R"(
#ifdef GL_ES
precision lowp float;
#endif
#if __VERSION__ >= 300
layout(location=0) in vec4 v_fragmentColor;
layout(location=1) in vec2 v_texCoord;
layout(binding=2) uniform sampler2D u_texture;
layout(location=0) out vec4 cc_FragColor;
#else
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
uniform sampler2D u_texture;
#endif
void main()
{
#if __VERSION__ >= 300
float a = texture(u_texture, v_texCoord.st).r;
cc_FragColor = vec4(v_fragmentColor.rgb, v_fragmentColor.a * a);
#else
float a = texture2D(u_texture, v_texCoord.st).r;
gl_FragColor = vec4(v_fragmentColor.rgb, v_fragmentColor.a * a);
#endif
}
)";
#else
static auto fragment_shader_font = R"(
#ifdef GL_ES
precision lowp float;
#endif
#if __VERSION__ >= 300
layout(location=0) in vec4 v_fragmentColor;
layout(location=1) in vec2 v_texCoord;
layout(binding=2) uniform sampler2D u_texture;
layout(location=0) out vec4 cc_FragColor;
#else
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
uniform sampler2D u_texture;
#endif
void main()
{
#if __VERSION__ >= 300
float a = texture(u_texture, v_texCoord.st).a;
cc_FragColor = vec4(v_fragmentColor.rgb, v_fragmentColor.a * a);
#else
float a = texture2D(u_texture, v_texCoord.st).a;
gl_FragColor = vec4(v_fragmentColor.rgb, v_fragmentColor.a * a);
#endif
}
)";
#endif
CC_SAFE_RELEASE(g_ProgramInfo.program);
CC_SAFE_RELEASE(g_ProgramFontInfo.program);
g_ProgramInfo.program = backend::Device::getInstance()->newProgram(
vertex_shader, fragment_shader);
g_ProgramFontInfo.program = backend::Device::getInstance()->newProgram(
vertex_shader, fragment_shader_font);
IM_ASSERT(g_ProgramInfo.program);
IM_ASSERT(g_ProgramFontInfo.program);
if (!g_ProgramInfo.program || !g_ProgramFontInfo.program)
return false;
for (auto& p : { &g_ProgramInfo,&g_ProgramFontInfo })
{
p->texture = p->program->getUniformLocation(TEXTURE);
p->projection = p->program->getUniformLocation(MVP_MATRIX);
IM_ASSERT(bool(p->texture));
IM_ASSERT(bool(p->projection));
#ifdef CC_USE_GFX
auto layout = p->program->getVertexLayout();
layout->setAttribute("a_position", 0,
VertexFormat::FLOAT2, 0, false);
layout->setAttribute("a_texCoord", 1,
VertexFormat::FLOAT2, offsetof(ImDrawVert, uv), false);
layout->setAttribute("a_color", 2,
VertexFormat::UBYTE4, offsetof(ImDrawVert, col), true);
layout->setStride(sizeof(ImDrawVert));
#else
p->position = p->program->getAttributeLocation(POSITION);
p->uv = p->program->getAttributeLocation(TEXCOORD);
p->color = p->program->getAttributeLocation(COLOR);
IM_ASSERT(p->position >= 0);
IM_ASSERT(p->uv >= 0);
IM_ASSERT(p->color >= 0);
auto& layout = p->layout;
layout.setAttribute("a_position", p->position,
VertexFormat::FLOAT2, 0, false);
layout.setAttribute("a_texCoord", p->uv,
VertexFormat::FLOAT2, offsetof(ImDrawVert, uv), false);
layout.setAttribute("a_color", p->color,
VertexFormat::UBYTE4, offsetof(ImDrawVert, col), true);
layout.setLayout(sizeof(ImDrawVert));
#endif
}
ImGui_ImplCocos2dx_CreateFontsTexture();
return true;
}
void ImGui_ImplCocos2dx_DestroyDeviceObjects()
{
CC_SAFE_RELEASE_NULL(g_ProgramInfo.program);
CC_SAFE_RELEASE_NULL(g_ProgramFontInfo.program);
ImGui_ImplCocos2dx_DestroyFontsTexture();
}
//--------------------------------------------------------------------------------------------------------
// MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT
// This is an _advanced_ and _optional_ feature, allowing the back-end to create and handle multiple viewports simultaneously.
// If you are new to dear imgui or creating a new binding for dear imgui, it is recommended that you completely ignore this section first..
//--------------------------------------------------------------------------------------------------------
static void ImGui_ImplOpenGL2_RenderWindow(ImGuiViewport* viewport, void*)
{
if (!(viewport->Flags & ImGuiViewportFlags_NoRendererClear))
{
const auto renderer = Director::getInstance()->getRenderer();
renderer->clear(ClearFlag::COLOR, { 0,0,0,1 }, 1, 0, 0);
}
ImGui_ImplCocos2dx_RenderDrawData(viewport->DrawData);
}
bool ImGui_ImplCocos2dx_Init(bool install_callbacks)
{
g_Time = 0.0;
ImGui::CreateContext();
// Setup backend capabilities flags
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
//io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
#ifdef CC_PLATFORM_PC
// Enable Multi-Viewport / Platform Windows
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;
//io.ConfigViewportsNoAutoMerge = true;
//io.ConfigViewportsNoTaskBarIcon = true;
const auto window = ImGui_ImplCocos2dx_GetWindow();
// We can honor GetMouseCursor() values (optional)
io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors;
// We can honor io.WantSetMousePos requests (optional, rarely used)
io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos;
// We can create multi-viewports on the Platform side (optional)
io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports;
#if GLFW_HAS_MOUSE_PASSTHROUGH || (GLFW_HAS_WINDOW_HOVERED && CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
// We can set io.MouseHoveredViewport correctly (optional, not easy)
io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport;
#endif
// metal renderer is not supported
#ifdef IMPL_MULTI_WINDOW
io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports;
#endif
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
platform_io.Renderer_RenderWindow = ImGui_ImplOpenGL2_RenderWindow;
#endif // CC_PLATFORM_PC
io.BackendPlatformName = "imgui_impl_cocos";
io.BackendRendererName = "imgui_impl_cocos";
// disable auto load and save
io.IniFilename = nullptr;
#ifdef CC_PLATFORM_PC
// Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array.
io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB;
io.KeyMap[ImGuiKey_LeftArrow] = GLFW_KEY_LEFT;
io.KeyMap[ImGuiKey_RightArrow] = GLFW_KEY_RIGHT;
io.KeyMap[ImGuiKey_UpArrow] = GLFW_KEY_UP;
io.KeyMap[ImGuiKey_DownArrow] = GLFW_KEY_DOWN;
io.KeyMap[ImGuiKey_PageUp] = GLFW_KEY_PAGE_UP;
io.KeyMap[ImGuiKey_PageDown] = GLFW_KEY_PAGE_DOWN;
io.KeyMap[ImGuiKey_Home] = GLFW_KEY_HOME;
io.KeyMap[ImGuiKey_End] = GLFW_KEY_END;
io.KeyMap[ImGuiKey_Insert] = GLFW_KEY_INSERT;
io.KeyMap[ImGuiKey_Delete] = GLFW_KEY_DELETE;
io.KeyMap[ImGuiKey_Backspace] = GLFW_KEY_BACKSPACE;
io.KeyMap[ImGuiKey_Space] = GLFW_KEY_SPACE;
io.KeyMap[ImGuiKey_Enter] = GLFW_KEY_ENTER;
io.KeyMap[ImGuiKey_Escape] = GLFW_KEY_ESCAPE;
io.KeyMap[ImGuiKey_KeyPadEnter] = GLFW_KEY_KP_ENTER;
io.KeyMap[ImGuiKey_A] = GLFW_KEY_A;
io.KeyMap[ImGuiKey_C] = GLFW_KEY_C;
io.KeyMap[ImGuiKey_V] = GLFW_KEY_V;
io.KeyMap[ImGuiKey_X] = GLFW_KEY_X;
io.KeyMap[ImGuiKey_Y] = GLFW_KEY_Y;
io.KeyMap[ImGuiKey_Z] = GLFW_KEY_Z;
io.SetClipboardTextFn = ImGui_ImplCocos2dx_SetClipboardText;
io.GetClipboardTextFn = ImGui_ImplCocos2dx_GetClipboardText;
io.ClipboardUserData = window;
// Create mouse cursors
// (By design, on X11 cursors are user configurable and some cursors may be missing. When a cursor doesn't exist,
// GLFW will emit an error which will often be printed by the app, so we temporarily disable error reporting.
// Missing cursors will return NULL and our _UpdateMouseCursor() function will use the Arrow cursor instead.)
GLFWerrorfun prev_error_callback = glfwSetErrorCallback(nullptr);
g_MouseCursors[ImGuiMouseCursor_Arrow] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
g_MouseCursors[ImGuiMouseCursor_TextInput] = glfwCreateStandardCursor(GLFW_IBEAM_CURSOR);
g_MouseCursors[ImGuiMouseCursor_ResizeNS] = glfwCreateStandardCursor(GLFW_VRESIZE_CURSOR);
g_MouseCursors[ImGuiMouseCursor_ResizeEW] = glfwCreateStandardCursor(GLFW_HRESIZE_CURSOR);
g_MouseCursors[ImGuiMouseCursor_Hand] = glfwCreateStandardCursor(GLFW_HAND_CURSOR);
#if GLFW_HAS_NEW_CURSORS
g_MouseCursors[ImGuiMouseCursor_ResizeAll] = glfwCreateStandardCursor(GLFW_RESIZE_ALL_CURSOR);
g_MouseCursors[ImGuiMouseCursor_ResizeNESW] = glfwCreateStandardCursor(GLFW_RESIZE_NESW_CURSOR);
g_MouseCursors[ImGuiMouseCursor_ResizeNWSE] = glfwCreateStandardCursor(GLFW_RESIZE_NWSE_CURSOR);
g_MouseCursors[ImGuiMouseCursor_NotAllowed] = glfwCreateStandardCursor(GLFW_NOT_ALLOWED_CURSOR);
#else
g_MouseCursors[ImGuiMouseCursor_ResizeAll] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
g_MouseCursors[ImGuiMouseCursor_ResizeNESW] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
g_MouseCursors[ImGuiMouseCursor_ResizeNWSE] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
g_MouseCursors[ImGuiMouseCursor_NotAllowed] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
#endif
glfwSetErrorCallback(prev_error_callback);
// Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any.
g_PrevUserCallbackMousebutton = nullptr;
g_PrevUserCallbackScroll = nullptr;
g_PrevUserCallbackKey = nullptr;
g_PrevUserCallbackChar = nullptr;
g_PrevUserCallbackMonitor = nullptr; // not used
if (install_callbacks)
{
g_InstalledCallbacks = true;
g_PrevUserCallbackMousebutton = glfwSetMouseButtonCallback(window, ImGui_ImplCocos2dx_MouseButtonCallback);
g_PrevUserCallbackScroll = glfwSetScrollCallback(window, ImGui_ImplCocos2dx_ScrollCallback);
g_PrevUserCallbackKey = glfwSetKeyCallback(window, ImGui_ImplCocos2dx_KeyCallback);
g_PrevUserCallbackChar = glfwSetCharCallback(window, ImGui_ImplCocos2dx_CharCallback);
g_PrevUserCallbackMonitor = glfwSetMonitorCallback(ImGui_ImplGlfw_MonitorCallback);
}
// Update monitors the first time (note: monitor callback are broken in GLFW 3.2 and earlier, see github.com/glfw/glfw/issues/784)
ImGui_ImplGlfw_UpdateMonitors();
glfwSetMonitorCallback(ImGui_ImplGlfw_MonitorCallback);
// Our mouse update function expect PlatformHandle to be filled for the main viewport
ImGuiViewport* main_viewport = ImGui::GetMainViewport();
main_viewport->PlatformHandle = (void*)window;
#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
main_viewport->PlatformHandleRaw = glfwGetWin32Window(window);
#endif
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
ImGui_ImplGlfw_InitPlatformInterface();
#else
/*
auto e = cocos2d::EventListenerMouse::create();
e->onMouseDown = [](cocos2d::EventMouse* ev)
{
const auto b = (int)ev->getMouseButton();
if (0 <= b && b < 5)
g_MouseJustPressed[b] = true;
};
e->onMouseUp = [](cocos2d::EventMouse* ev)
{
const auto b = (int)ev->getMouseButton();
if (0 <= b && b < 5)
g_MouseJustPressed[b] = false;
};
e->onMouseMove = [](cocos2d::EventMouse* ev)
{
g_CursorPos.x = ev->getCursorX();
g_CursorPos.y = ev->getCursorY();
};
e->onMouseScroll = [](cocos2d::EventMouse* ev)
{
auto& _io = ImGui::GetIO();
_io.MouseWheelH += (float)ev->getScrollX();
_io.MouseWheel += (float)ev->getScrollY();
};
cocos2d::Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(e, 1);
*/
auto e1 = cocos2d::EventListenerTouchOneByOne::create();
//e1->setSwallowTouches(true);
e1->onTouchBegan = [](Touch* touch, Event*)
{
const auto loc = touch->getLocationInView();
g_CursorPos.x = loc.x;
g_CursorPos.y = loc.y;
g_MouseJustPressed[0] = true;
return true;
};
e1->onTouchMoved = [](Touch* touch, Event*)
{
const auto loc = touch->getLocationInView();
g_CursorPos.x = loc.x;
g_CursorPos.y = loc.y;
g_MouseJustPressed[0] = true;
};
e1->onTouchEnded = [](Touch* touch, Event*)
{
const auto loc = touch->getLocationInView();
g_CursorPos.x = loc.x;
g_CursorPos.y = loc.y;
g_MouseJustPressed[0] = false;
};
e1->onTouchCancelled = [&](Touch* touch, Event*)
{
g_CursorPos = ImVec2(-FLT_MAX, -FLT_MAX);
g_MouseJustPressed[0] = false;
};
cocos2d::Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(e1, -1);
using KeyCode = cocos2d::EventKeyboard::KeyCode;
io.KeyMap[ImGuiKey_Tab] = (int)KeyCode::KEY_TAB;
io.KeyMap[ImGuiKey_LeftArrow] = (int)KeyCode::KEY_LEFT_ARROW;
io.KeyMap[ImGuiKey_RightArrow] = (int)KeyCode::KEY_RIGHT_ARROW;
io.KeyMap[ImGuiKey_UpArrow] = (int)KeyCode::KEY_UP_ARROW;
io.KeyMap[ImGuiKey_DownArrow] = (int)KeyCode::KEY_DOWN_ARROW;
io.KeyMap[ImGuiKey_PageUp] = (int)KeyCode::KEY_PG_UP;
io.KeyMap[ImGuiKey_PageDown] = (int)KeyCode::KEY_PG_DOWN;
io.KeyMap[ImGuiKey_Home] = (int)KeyCode::KEY_HOME;
io.KeyMap[ImGuiKey_End] = (int)KeyCode::KEY_END;
io.KeyMap[ImGuiKey_Insert] = (int)KeyCode::KEY_INSERT;
io.KeyMap[ImGuiKey_Delete] = (int)KeyCode::KEY_DELETE;
io.KeyMap[ImGuiKey_Backspace] = (int)KeyCode::KEY_BACKSPACE;
io.KeyMap[ImGuiKey_Space] = (int)KeyCode::KEY_SPACE;
io.KeyMap[ImGuiKey_Enter] = (int)KeyCode::KEY_ENTER;
io.KeyMap[ImGuiKey_Escape] = (int)KeyCode::KEY_ESCAPE;
io.KeyMap[ImGuiKey_KeyPadEnter] = (int)KeyCode::KEY_KP_ENTER;
io.KeyMap[ImGuiKey_A] = (int)KeyCode::KEY_A;
io.KeyMap[ImGuiKey_C] = (int)KeyCode::KEY_C;
io.KeyMap[ImGuiKey_V] = (int)KeyCode::KEY_V;
io.KeyMap[ImGuiKey_X] = (int)KeyCode::KEY_X;
io.KeyMap[ImGuiKey_Y] = (int)KeyCode::KEY_Y;
io.KeyMap[ImGuiKey_Z] = (int)KeyCode::KEY_Z;
auto e2 = cocos2d::EventListenerKeyboard::create();
e2->onKeyPressed = [](KeyCode k, cocos2d::Event* ev)
{
auto& _io = ImGui::GetIO();
_io.KeysDown[(int)k] = true;
// Modifiers are not reliable across systems
_io.KeyCtrl = _io.KeysDown[(int)KeyCode::KEY_LEFT_CTRL] || _io.KeysDown[(int)KeyCode::KEY_RIGHT_CTRL];
_io.KeyShift = _io.KeysDown[(int)KeyCode::KEY_LEFT_SHIFT] || _io.KeysDown[(int)KeyCode::KEY_RIGHT_SHIFT];
_io.KeyAlt = _io.KeysDown[(int)KeyCode::KEY_LEFT_ALT] || _io.KeysDown[(int)KeyCode::KEY_RIGHT_ALT];
_io.KeySuper = _io.KeysDown[(int)KeyCode::KEY_HYPER];
};
e2->onKeyReleased = [](KeyCode k, cocos2d::Event* ev)
{
auto& _io = ImGui::GetIO();
_io.KeysDown[(int)k] = false;
// Modifiers are not reliable across systems
_io.KeyCtrl = _io.KeysDown[(int)KeyCode::KEY_LEFT_CTRL] || _io.KeysDown[(int)KeyCode::KEY_RIGHT_CTRL];
_io.KeyShift = _io.KeysDown[(int)KeyCode::KEY_LEFT_SHIFT] || _io.KeysDown[(int)KeyCode::KEY_RIGHT_SHIFT];
_io.KeyAlt = _io.KeysDown[(int)KeyCode::KEY_LEFT_ALT] || _io.KeysDown[(int)KeyCode::KEY_RIGHT_ALT];
_io.KeySuper = _io.KeysDown[(int)KeyCode::KEY_HYPER];
};
cocos2d::Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(e2, 1);
#endif // CC_PLATFORM_PC
return true;
}
void ImGui_ImplCocos2dx_Shutdown()
{
#ifdef CC_PLATFORM_PC
ImGui_ImplGlfw_ShutdownPlatformInterface();
const auto g_Window = ImGui_ImplCocos2dx_GetWindow();
if (g_InstalledCallbacks)
{
glfwSetMouseButtonCallback(g_Window, g_PrevUserCallbackMousebutton);
glfwSetScrollCallback(g_Window, g_PrevUserCallbackScroll);
glfwSetKeyCallback(g_Window, g_PrevUserCallbackKey);
glfwSetCharCallback(g_Window, g_PrevUserCallbackChar);
g_InstalledCallbacks = false;
}
for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++)
{
glfwDestroyCursor(g_MouseCursors[cursor_n]);
g_MouseCursors[cursor_n] = nullptr;
}
#endif // CC_PLATFORM_PC
ImGui::DestroyPlatformWindows();
ImGui_ImplCocos2dx_DestroyDeviceObjects();
ImGui::DestroyContext();
}
static void ImGui_ImplCocos2dx_UpdateMousePosAndButtons()
{
#ifdef CC_PLATFORM_PC
const auto g_Window = ImGui_ImplCocos2dx_GetWindow();
// Update buttons
ImGuiIO& io = ImGui::GetIO();
for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++)
{
// If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame.
io.MouseDown[i] = g_MouseJustPressed[i] || glfwGetMouseButton(g_Window, i) != 0;
g_MouseJustPressed[i] = false;
}
// Update mouse position
const ImVec2 mouse_pos_backup = io.MousePos;
io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX);
io.MouseHoveredViewport = 0;
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
for (int n = 0; n < platform_io.Viewports.Size; n++)
{
ImGuiViewport* viewport = platform_io.Viewports[n];
GLFWwindow* window = (GLFWwindow*)viewport->PlatformHandle;
IM_ASSERT(window != NULL);
#ifdef __EMSCRIPTEN__
const bool focused = true;
IM_ASSERT(platform_io.Viewports.Size == 1);
#else
const bool focused = glfwGetWindowAttrib(window, GLFW_FOCUSED) != 0;
#endif
if (focused)
{
if (io.WantSetMousePos)
{
glfwSetCursorPos(window, (double)(mouse_pos_backup.x - viewport->Pos.x), (double)(mouse_pos_backup.y - viewport->Pos.y));
}
else
{
double mouse_x, mouse_y;
glfwGetCursorPos(window, &mouse_x, &mouse_y);
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
// Multi-viewport mode: mouse position in OS absolute coordinates (io.MousePos is (0,0) when the mouse is on the upper-left of the primary monitor)
int window_x, window_y;
glfwGetWindowPos(window, &window_x, &window_y);
io.MousePos = ImVec2((float)mouse_x + window_x, (float)mouse_y + window_y);
}
else
{
// Single viewport mode: mouse position in client window coordinates (io.MousePos is (0,0) when the mouse is on the upper-left corner of the app window)
io.MousePos = ImVec2((float)mouse_x, (float)mouse_y);
}
}
for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++)
io.MouseDown[i] |= glfwGetMouseButton(window, i) != 0;
}
// (Optional) When using multiple viewports: set io.MouseHoveredViewport to the viewport the OS mouse cursor is hovering.
// Important: this information is not easy to provide and many high-level windowing library won't be able to provide it correctly, because
// - This is _ignoring_ viewports with the ImGuiViewportFlags_NoInputs flag (pass-through windows).
// - This is _regardless_ of whether another viewport is focused or being dragged from.
// If ImGuiBackendFlags_HasMouseHoveredViewport is not set by the backend, imgui will ignore this field and infer the information by relying on the
// rectangles and last focused time of every viewports it knows about. It will be unaware of other windows that may be sitting between or over your windows.
// [GLFW] FIXME: This is currently only correct on Win32. See what we do below with the WM_NCHITTEST, missing an equivalent for other systems.
// See https://github.com/glfw/glfw/issues/1236 if you want to help in making this a GLFW feature.
#if GLFW_HAS_MOUSE_PASSTHROUGH || (GLFW_HAS_WINDOW_HOVERED && CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
const bool window_no_input = (viewport->Flags & ImGuiViewportFlags_NoInputs) != 0;
#if GLFW_HAS_MOUSE_PASSTHROUGH
glfwSetWindowAttrib(window, GLFW_MOUSE_PASSTHROUGH, window_no_input);
#endif
if (glfwGetWindowAttrib(window, GLFW_HOVERED) && !window_no_input)
io.MouseHoveredViewport = viewport->ID;
#endif
}
#else
// Update buttons
ImGuiIO& io = ImGui::GetIO();
for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++)
{
// g_MouseJustPressed represents touch state on mobile platforms
io.MouseDown[i] = g_MouseJustPressed[i];
}
// Update mouse position
const ImVec2 mouse_pos_backup = io.MousePos;
io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX);
if (io.WantSetMousePos)
{
io.MousePos = mouse_pos_backup;
}
else
{
if (g_CursorPos.x != -FLT_MAX && g_CursorPos.y != -FLT_MAX)
{
// convert g_CursorPos
const auto glv = cocos2d::Director::getInstance()->getOpenGLView();
const auto rect = glv->getViewPortRect();
io.MousePos.x = g_CursorPos.x * glv->getScaleX() + rect.origin.x;
io.MousePos.y = g_CursorPos.y * glv->getScaleY() + rect.origin.y;
}
}
#endif // CC_PLATFORM_PC
}
static void ImGui_ImplCocos2dx_UpdateMouseCursor()
{