-
Notifications
You must be signed in to change notification settings - Fork 9
/
CCImGuiLayer.cpp
61 lines (56 loc) · 1.46 KB
/
CCImGuiLayer.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
#include "CCImGuiLayer.h"
#include "imgui/imgui.h"
#include "imgui_impl_cocos2dx.h"
#include "CCIMGUI.h"
using namespace cocos2d;
ImGuiLayer* ImGuiLayer::create()
{
ImGuiLayer* pRet = new(std::nothrow) ImGuiLayer();
if (pRet && pRet->init())
{
pRet->autorelease();
return pRet;
}
delete pRet;
return nullptr;
}
bool ImGuiLayer::init()
{
if (!Layer::init() || !CCIMGUI::getInstance())
return false;
#ifdef CC_PLATFORM_PC
// note: when at the first click to focus the window, this will not take effect
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = [this](Touch* touch, Event*) -> bool {
if (!_visible)
return false;
return ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow);
};
getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);
#endif
// add an empty sprite to avoid render problem
const auto sp = Sprite::create();
sp->setGlobalZOrder(1);
sp->setOpacity(0);
addChild(sp, 1);
return true;
}
void ImGuiLayer::visit(Renderer* renderer, const Mat4 &parentTransform, uint32_t parentFlags)
{
if (!_visible)
return;
onDraw();
Layer::visit(renderer, parentTransform, parentFlags);
}
void ImGuiLayer::onDraw()
{
// create frame
ImGui_ImplCocos2dx_NewFrame();
// draw all gui
CCIMGUI::getInstance()->update();
// render
ImGui::Render();
ImGui_ImplCocos2dx_RenderDrawData(ImGui::GetDrawData());
ImGui_ImplCocos2dx_RenderPlatform();
}