ocornut_imgui.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. /*
  2. * Copyright 2014-2015 Daniel Collin. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include <bgfx/bgfx.h>
  6. #include <bx/allocator.h>
  7. #include <bx/fpumath.h>
  8. #include <bx/timer.h>
  9. #include <ocornut-imgui/imgui.h>
  10. #include <ocornut-imgui/imgui_wm.h>
  11. #include "imgui.h"
  12. #include "ocornut_imgui.h"
  13. #include <stb/stb_image.c>
  14. #ifndef USE_ENTRY
  15. # if defined(SCI_NAMESPACE)
  16. # define USE_ENTRY 1
  17. # else
  18. # define USE_ENTRY 0
  19. # endif // defined(SCI_NAMESPACE)
  20. #endif // USE_ENTRY
  21. #if USE_ENTRY
  22. # include "../entry/entry.h"
  23. #endif // USE_ENTRY
  24. #if defined(SCI_NAMESPACE)
  25. # include "../entry/input.h"
  26. # include "scintilla.h"
  27. #endif // defined(SCI_NAMESPACE)
  28. #include "vs_ocornut_imgui.bin.h"
  29. #include "fs_ocornut_imgui.bin.h"
  30. class PlatformWindow : public ImGuiWM::PlatformWindow
  31. {
  32. typedef ImGuiWM::PlatformWindow Super;
  33. public:
  34. PlatformWindow(bool _mainWindow, bool _isDragWindow)
  35. : ImGuiWM::PlatformWindow(_mainWindow, _isDragWindow)
  36. , m_pos(0.0f, 0.0f)
  37. , m_size(0.0f, 0.0f)
  38. , m_drag(false)
  39. {
  40. #if USE_ENTRY
  41. if (!_mainWindow
  42. && !_isDragWindow)
  43. {
  44. m_window = entry::createWindow(0, 0, 640, 380);
  45. extern void pwToWindow(entry::WindowHandle _handle, class PlatformWindow* _pw);
  46. pwToWindow(m_window, this);
  47. }
  48. else
  49. {
  50. m_window.idx = 0;
  51. }
  52. #endif // USE_ENTRY
  53. }
  54. virtual ~PlatformWindow()
  55. {
  56. #if USE_ENTRY
  57. if (0 != m_window.idx)
  58. {
  59. entry::destroyWindow(m_window);
  60. }
  61. #endif // USE_ENTRY
  62. }
  63. virtual bool Init(ImGuiWM::PlatformWindow* /*_parent*/) BX_OVERRIDE
  64. {
  65. return true;
  66. }
  67. virtual const ImVec2& GetPosition() const BX_OVERRIDE
  68. {
  69. return m_pos;
  70. }
  71. virtual const ImVec2& GetSize() const BX_OVERRIDE
  72. {
  73. return m_size;
  74. }
  75. virtual void Show() BX_OVERRIDE
  76. {
  77. }
  78. virtual void Hide() BX_OVERRIDE
  79. {
  80. }
  81. virtual void SetSize(const ImVec2& _size) BX_OVERRIDE
  82. {
  83. #if USE_ENTRY
  84. if (0 != m_window.idx
  85. && m_size.x != _size.x
  86. && m_size.y != _size.y)
  87. {
  88. entry::setWindowSize(m_window, int32_t(_size.x), int32_t(_size.y) );
  89. }
  90. #endif // USE_ENTRY
  91. m_size = _size;
  92. }
  93. virtual void SetPosition(const ImVec2& _pos) BX_OVERRIDE
  94. {
  95. #if USE_ENTRY
  96. if (0 != m_window.idx
  97. && m_pos.x != _pos.x
  98. && m_pos.y != _pos.y)
  99. {
  100. entry::setWindowPos(m_window, int32_t(_pos.x), int32_t(_pos.y) );
  101. }
  102. #endif // USE_ENTRY
  103. m_pos = _pos;
  104. }
  105. virtual void SetTitle(const char* _title) BX_OVERRIDE
  106. {
  107. #if USE_ENTRY
  108. entry::setWindowTitle(m_window, _title);
  109. #else
  110. BX_UNUSED(_title);
  111. #endif // USE_ENTRY
  112. }
  113. virtual void PreUpdate() BX_OVERRIDE
  114. {
  115. }
  116. virtual void PaintBegin() BX_OVERRIDE;
  117. virtual void Paint() BX_OVERRIDE;
  118. virtual void PaintEnd() BX_OVERRIDE;
  119. virtual void Destroy() BX_OVERRIDE
  120. {
  121. }
  122. virtual void StartDrag() BX_OVERRIDE
  123. {
  124. m_drag = true;
  125. }
  126. virtual void StopDrag() BX_OVERRIDE
  127. {
  128. m_drag = false;
  129. }
  130. virtual bool IsDraging() BX_OVERRIDE
  131. {
  132. return m_drag;
  133. }
  134. private:
  135. ImVec2 m_pos;
  136. ImVec2 m_size;
  137. bool m_drag;
  138. #if USE_ENTRY
  139. entry::WindowHandle m_window;
  140. #endif // USE_ENTRY
  141. };
  142. class WindowManager : public ImGuiWM::WindowManager
  143. {
  144. typedef ImGuiWM::WindowManager Super;
  145. public:
  146. WindowManager()
  147. {
  148. }
  149. virtual ~WindowManager()
  150. {
  151. }
  152. protected:
  153. virtual ImGuiWM::PlatformWindow* CreatePlatformWindow(bool _main, ImGuiWM::PlatformWindow* _parent, bool _isDragWindow) BX_OVERRIDE
  154. {
  155. #if USE_ENTRY
  156. #else
  157. if (!_main
  158. && !_isDragWindow)
  159. {
  160. return NULL;
  161. }
  162. #endif // USE_ENTRY
  163. PlatformWindow* window = new (ImGui::MemAlloc(sizeof(PlatformWindow) ) ) PlatformWindow(_main, _isDragWindow);
  164. window->Init(_parent);
  165. return static_cast<ImGuiWM::PlatformWindow*>(window);
  166. }
  167. virtual void LogFormatted(const char* _str) BX_OVERRIDE
  168. {
  169. BX_TRACE("%s", _str); BX_UNUSED(_str);
  170. }
  171. virtual void InternalRun() BX_OVERRIDE
  172. {
  173. PreUpdate();
  174. Update();
  175. }
  176. };
  177. struct OcornutImguiContext
  178. {
  179. static void* memAlloc(size_t _size);
  180. static void memFree(void* _ptr);
  181. static void renderDrawLists(ImDrawData* _drawData);
  182. void render(ImDrawData* _drawData)
  183. {
  184. const ImGuiIO& io = ImGui::GetIO();
  185. const float width = io.DisplaySize.x;
  186. const float height = io.DisplaySize.y;
  187. {
  188. float ortho[16];
  189. bx::mtxOrtho(ortho, 0.0f, width, height, 0.0f, -1.0f, 1.0f);
  190. bgfx::setViewTransform(m_viewId, NULL, ortho);
  191. }
  192. #if USE_ENTRY
  193. for (uint32_t ii = 1; ii < BX_COUNTOF(m_window); ++ii)
  194. {
  195. Window& window = m_window[ii];
  196. if (bgfx::isValid(window.m_fbh) )
  197. {
  198. const uint8_t viewId = window.m_viewId;
  199. bgfx::setViewClear(viewId
  200. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  201. , 0x303030ff
  202. , 1.0f
  203. , 0
  204. );
  205. bgfx::setViewFrameBuffer(viewId, window.m_fbh);
  206. bgfx::setViewRect(viewId
  207. , 0
  208. , 0
  209. , window.m_state.m_width
  210. , window.m_state.m_height
  211. );
  212. float ortho[16];
  213. bx::mtxOrtho(ortho
  214. , 0.0f
  215. , float(window.m_state.m_width)
  216. , float(window.m_state.m_height)
  217. , 0.0f
  218. , -1.0f
  219. , 1.0f
  220. );
  221. bgfx::setViewTransform(viewId
  222. , NULL
  223. , ortho
  224. );
  225. }
  226. }
  227. #endif // USE_ENTRY
  228. // Render command lists
  229. for (int32_t ii = 0, num = _drawData->CmdListsCount; ii < num; ++ii)
  230. {
  231. bgfx::TransientVertexBuffer tvb;
  232. bgfx::TransientIndexBuffer tib;
  233. const ImDrawList* drawList = _drawData->CmdLists[ii];
  234. uint32_t numVertices = (uint32_t)drawList->VtxBuffer.size();
  235. uint32_t numIndices = (uint32_t)drawList->IdxBuffer.size();
  236. if (!bgfx::checkAvailTransientVertexBuffer(numVertices, m_decl)
  237. || !bgfx::checkAvailTransientIndexBuffer(numIndices) )
  238. {
  239. // not enough space in transient buffer just quit drawing the rest...
  240. break;
  241. }
  242. bgfx::allocTransientVertexBuffer(&tvb, numVertices, m_decl);
  243. bgfx::allocTransientIndexBuffer(&tib, numIndices);
  244. ImDrawVert* verts = (ImDrawVert*)tvb.data;
  245. memcpy(verts, drawList->VtxBuffer.begin(), numVertices * sizeof(ImDrawVert) );
  246. ImDrawIdx* indices = (ImDrawIdx*)tib.data;
  247. memcpy(indices, drawList->IdxBuffer.begin(), numIndices * sizeof(ImDrawIdx) );
  248. uint32_t offset = 0;
  249. for (const ImDrawCmd* cmd = drawList->CmdBuffer.begin(), *cmdEnd = drawList->CmdBuffer.end(); cmd != cmdEnd; ++cmd)
  250. {
  251. if (cmd->UserCallback)
  252. {
  253. cmd->UserCallback(drawList, cmd);
  254. }
  255. else if (0 != cmd->ElemCount)
  256. {
  257. uint64_t state = 0
  258. | BGFX_STATE_RGB_WRITE
  259. | BGFX_STATE_ALPHA_WRITE
  260. | BGFX_STATE_MSAA
  261. ;
  262. bgfx::TextureHandle th = m_texture;
  263. bgfx::ProgramHandle program = m_program;
  264. if (NULL != cmd->TextureId)
  265. {
  266. union { ImTextureID ptr; struct { bgfx::TextureHandle handle; uint8_t flags; uint8_t mip; } s; } texture = { cmd->TextureId };
  267. state |= 0 != (IMGUI_FLAGS_ALPHA_BLEND & texture.s.flags)
  268. ? BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  269. : BGFX_STATE_NONE
  270. ;
  271. th = texture.s.handle;
  272. if (0 != texture.s.mip)
  273. {
  274. extern bgfx::ProgramHandle imguiGetImageProgram(uint8_t _mip);
  275. program = imguiGetImageProgram(texture.s.mip);
  276. }
  277. }
  278. else
  279. {
  280. state |= BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA);
  281. }
  282. const uint16_t xx = uint16_t(bx::fmax(cmd->ClipRect.x, 0.0f) );
  283. const uint16_t yy = uint16_t(bx::fmax(cmd->ClipRect.y, 0.0f) );
  284. bgfx::setScissor(xx, yy
  285. , uint16_t(bx::fmin(cmd->ClipRect.z, 65535.0f)-xx)
  286. , uint16_t(bx::fmin(cmd->ClipRect.w, 65535.0f)-yy)
  287. );
  288. bgfx::setState(state);
  289. bgfx::setTexture(0, s_tex, th);
  290. bgfx::setVertexBuffer(&tvb, 0, numVertices);
  291. bgfx::setIndexBuffer(&tib, offset, cmd->ElemCount);
  292. bgfx::submit(cmd->ViewId, program);
  293. }
  294. offset += cmd->ElemCount;
  295. }
  296. }
  297. }
  298. void create(const void* _data, uint32_t _size, float _fontSize, bx::AllocatorI* _allocator)
  299. {
  300. m_viewId = 255;
  301. m_allocator = _allocator;
  302. m_lastScroll = 0;
  303. m_last = bx::getHPCounter();
  304. ImGuiIO& io = ImGui::GetIO();
  305. io.RenderDrawListsFn = renderDrawLists;
  306. if (NULL != m_allocator)
  307. {
  308. io.MemAllocFn = memAlloc;
  309. io.MemFreeFn = memFree;
  310. }
  311. io.DisplaySize = ImVec2(1280.0f, 720.0f);
  312. io.DeltaTime = 1.0f / 60.0f;
  313. io.IniFilename = NULL;
  314. #if defined(SCI_NAMESPACE)
  315. io.KeyMap[ImGuiKey_Tab] = (int)entry::Key::Tab;
  316. io.KeyMap[ImGuiKey_LeftArrow] = (int)entry::Key::Left;
  317. io.KeyMap[ImGuiKey_RightArrow] = (int)entry::Key::Right;
  318. io.KeyMap[ImGuiKey_UpArrow] = (int)entry::Key::Up;
  319. io.KeyMap[ImGuiKey_DownArrow] = (int)entry::Key::Down;
  320. io.KeyMap[ImGuiKey_Home] = (int)entry::Key::Home;
  321. io.KeyMap[ImGuiKey_End] = (int)entry::Key::End;
  322. io.KeyMap[ImGuiKey_Delete] = (int)entry::Key::Delete;
  323. io.KeyMap[ImGuiKey_Backspace] = (int)entry::Key::Backspace;
  324. io.KeyMap[ImGuiKey_Enter] = (int)entry::Key::Return;
  325. io.KeyMap[ImGuiKey_Escape] = (int)entry::Key::Esc;
  326. io.KeyMap[ImGuiKey_A] = (int)entry::Key::KeyA;
  327. io.KeyMap[ImGuiKey_C] = (int)entry::Key::KeyC;
  328. io.KeyMap[ImGuiKey_V] = (int)entry::Key::KeyV;
  329. io.KeyMap[ImGuiKey_X] = (int)entry::Key::KeyX;
  330. io.KeyMap[ImGuiKey_Y] = (int)entry::Key::KeyY;
  331. io.KeyMap[ImGuiKey_Z] = (int)entry::Key::KeyZ;
  332. #endif // defined(SCI_NAMESPACE)
  333. const bgfx::Memory* vsmem;
  334. const bgfx::Memory* fsmem;
  335. switch (bgfx::getRendererType() )
  336. {
  337. case bgfx::RendererType::Direct3D9:
  338. vsmem = bgfx::makeRef(vs_ocornut_imgui_dx9, sizeof(vs_ocornut_imgui_dx9) );
  339. fsmem = bgfx::makeRef(fs_ocornut_imgui_dx9, sizeof(fs_ocornut_imgui_dx9) );
  340. break;
  341. case bgfx::RendererType::Direct3D11:
  342. case bgfx::RendererType::Direct3D12:
  343. vsmem = bgfx::makeRef(vs_ocornut_imgui_dx11, sizeof(vs_ocornut_imgui_dx11) );
  344. fsmem = bgfx::makeRef(fs_ocornut_imgui_dx11, sizeof(fs_ocornut_imgui_dx11) );
  345. break;
  346. case bgfx::RendererType::Metal:
  347. vsmem = bgfx::makeRef(vs_ocornut_imgui_mtl, sizeof(vs_ocornut_imgui_mtl) );
  348. fsmem = bgfx::makeRef(fs_ocornut_imgui_mtl, sizeof(fs_ocornut_imgui_mtl) );
  349. break;
  350. default:
  351. vsmem = bgfx::makeRef(vs_ocornut_imgui_glsl, sizeof(vs_ocornut_imgui_glsl) );
  352. fsmem = bgfx::makeRef(fs_ocornut_imgui_glsl, sizeof(fs_ocornut_imgui_glsl) );
  353. break;
  354. }
  355. bgfx::ShaderHandle vsh = bgfx::createShader(vsmem);
  356. bgfx::ShaderHandle fsh = bgfx::createShader(fsmem);
  357. m_program = bgfx::createProgram(vsh, fsh, true);
  358. m_decl
  359. .begin()
  360. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  361. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  362. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  363. .end();
  364. s_tex = bgfx::createUniform("s_tex", bgfx::UniformType::Int1);
  365. uint8_t* data;
  366. int32_t width;
  367. int32_t height;
  368. {
  369. void* font = ImGui::MemAlloc(_size);
  370. memcpy(font, _data, _size);
  371. io.Fonts->AddFontFromMemoryTTF(font, _size, _fontSize);
  372. }
  373. io.Fonts->GetTexDataAsRGBA32(&data, &width, &height);
  374. m_texture = bgfx::createTexture2D( (uint16_t)width
  375. , (uint16_t)height
  376. , 1
  377. , bgfx::TextureFormat::BGRA8
  378. , 0
  379. , bgfx::copy(data, width*height*4)
  380. );
  381. ImGuiStyle& style = ImGui::GetStyle();
  382. style.FrameRounding = 4.0f;
  383. m_wm = BX_NEW(m_allocator, WindowManager);
  384. m_wm->Init();
  385. #if 0
  386. {
  387. class Window : public ImGuiWM::Window
  388. {
  389. public:
  390. Window(const char* _title)
  391. : ImGuiWM::Window()
  392. {
  393. SetTitle(_title);
  394. }
  395. virtual void OnGui() BX_OVERRIDE
  396. {
  397. }
  398. };
  399. class WindowX : public ImGuiWM::Window
  400. {
  401. public:
  402. WindowX(const char* _title)
  403. : ImGuiWM::Window()
  404. {
  405. SetTitle(_title);
  406. }
  407. virtual void OnGui() BX_OVERRIDE
  408. {
  409. #if defined(SCI_NAMESPACE) && 0
  410. bool opened = true;
  411. ImGuiScintilla("Scintilla Editor", &opened, ImVec2(640.0f, 480.0f) );
  412. #endif // 0
  413. }
  414. };
  415. Window* w0 = new Window("test");
  416. WindowX* w1 = new WindowX("Scintilla");
  417. Window* w2 = new Window("xyzw");
  418. Window* w3 = new Window("0123");
  419. m_wm->Dock(w0);
  420. m_wm->DockWith(w1, w0, ImGuiWM::E_DOCK_ORIENTATION_RIGHT);
  421. m_wm->DockWith(w2, w1, ImGuiWM::E_DOCK_ORIENTATION_BOTTOM);
  422. m_wm->DockWith(w3, w0, ImGuiWM::E_DOCK_ORIENTATION_BOTTOM);
  423. }
  424. #endif // 0
  425. }
  426. void destroy()
  427. {
  428. m_wm->Exit();
  429. BX_DELETE(m_allocator, m_wm);
  430. ImGui::Shutdown();
  431. bgfx::destroyUniform(s_tex);
  432. bgfx::destroyTexture(m_texture);
  433. bgfx::destroyProgram(m_program);
  434. m_allocator = NULL;
  435. }
  436. void beginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, int _width, int _height, char _inputChar, uint8_t _viewId)
  437. {
  438. m_viewId = _viewId;
  439. ImGuiIO& io = ImGui::GetIO();
  440. if (_inputChar < 0x7f)
  441. {
  442. io.AddInputCharacter(_inputChar); // ASCII or GTFO! :(
  443. }
  444. io.DisplaySize = ImVec2( (float)_width, (float)_height);
  445. const int64_t now = bx::getHPCounter();
  446. const int64_t frameTime = now - m_last;
  447. m_last = now;
  448. const double freq = double(bx::getHPFrequency() );
  449. io.DeltaTime = float(frameTime/freq);
  450. io.MousePos = ImVec2( (float)_mx, (float)_my);
  451. io.MouseDown[0] = 0 != (_button & IMGUI_MBUT_LEFT);
  452. io.MouseDown[1] = 0 != (_button & IMGUI_MBUT_RIGHT);
  453. io.MouseDown[2] = 0 != (_button & IMGUI_MBUT_MIDDLE);
  454. io.MouseWheel = (float)(_scroll - m_lastScroll);
  455. m_lastScroll = _scroll;
  456. #if defined(SCI_NAMESPACE)
  457. uint8_t modifiers = inputGetModifiersState();
  458. io.KeyShift = 0 != (modifiers & (entry::Modifier::LeftShift | entry::Modifier::RightShift) );
  459. io.KeyCtrl = 0 != (modifiers & (entry::Modifier::LeftCtrl | entry::Modifier::RightCtrl ) );
  460. io.KeyAlt = 0 != (modifiers & (entry::Modifier::LeftAlt | entry::Modifier::RightAlt ) );
  461. for (int32_t ii = 0; ii < (int32_t)entry::Key::Count; ++ii)
  462. {
  463. io.KeysDown[ii] = inputGetKeyState(entry::Key::Enum(ii) );
  464. }
  465. #endif // defined(SCI_NAMESPACE)
  466. ImGui::NewFrame();
  467. ImGui::PushStyleVar(ImGuiStyleVar_ViewId, (float)_viewId);
  468. #if 0
  469. ImGui::ShowTestWindow(); //Debug only.
  470. #endif // 0
  471. #if 0
  472. extern void ShowExampleAppCustomNodeGraph(bool* opened);
  473. bool opened = true;
  474. ShowExampleAppCustomNodeGraph(&opened);
  475. #endif // 0
  476. }
  477. void endFrame()
  478. {
  479. m_wm->Run();
  480. ImGui::PopStyleVar(1);
  481. ImGui::Render();
  482. }
  483. bx::AllocatorI* m_allocator;
  484. bgfx::VertexDecl m_decl;
  485. bgfx::ProgramHandle m_program;
  486. bgfx::TextureHandle m_texture;
  487. bgfx::UniformHandle s_tex;
  488. WindowManager* m_wm;
  489. int64_t m_last;
  490. int32_t m_lastScroll;
  491. uint8_t m_viewId;
  492. #if USE_ENTRY
  493. struct Window
  494. {
  495. Window()
  496. {
  497. m_fbh.idx = bgfx::invalidHandle;
  498. }
  499. entry::WindowState m_state;
  500. PlatformWindow* m_pw;
  501. bgfx::FrameBufferHandle m_fbh;
  502. uint8_t m_viewId;
  503. };
  504. Window m_window[16];
  505. #endif // USE_ENTRY
  506. };
  507. static OcornutImguiContext s_ctx;
  508. void PlatformWindow::PaintBegin()
  509. {
  510. #if USE_ENTRY
  511. if (!m_bIsDragWindow)
  512. {
  513. OcornutImguiContext::Window& win = s_ctx.m_window[m_window.idx];
  514. entry::WindowState& state = win.m_state;
  515. ImGuiIO& io = ImGui::GetIO();
  516. io.MousePos = ImVec2((float)state.m_mouse.m_mx, (float)state.m_mouse.m_my);
  517. io.MouseDown[0] = !!state.m_mouse.m_buttons[entry::MouseButton::Left];
  518. io.MouseDown[1] = !!state.m_mouse.m_buttons[entry::MouseButton::Right];
  519. io.MouseDown[2] = !!state.m_mouse.m_buttons[entry::MouseButton::Middle];
  520. io.MouseWheel = float(state.m_mouse.m_mz);
  521. ImGui::PushStyleVar(ImGuiStyleVar_ViewId, (float)win.m_viewId);
  522. }
  523. #endif // USE_ENTRY
  524. }
  525. void PlatformWindow::Paint()
  526. {
  527. if (!m_bIsDragWindow)
  528. {
  529. Super::Paint();
  530. }
  531. }
  532. void PlatformWindow::PaintEnd()
  533. {
  534. #if USE_ENTRY
  535. if (!m_bIsDragWindow)
  536. {
  537. ImGui::PopStyleVar(1);
  538. entry::WindowState& state = s_ctx.m_window[0].m_state;
  539. ImGuiIO& io = ImGui::GetIO();
  540. io.MousePos = ImVec2((float)state.m_mouse.m_mx, (float)state.m_mouse.m_my);
  541. io.MouseDown[0] = !!state.m_mouse.m_buttons[entry::MouseButton::Left];
  542. io.MouseDown[1] = !!state.m_mouse.m_buttons[entry::MouseButton::Right];
  543. io.MouseDown[2] = !!state.m_mouse.m_buttons[entry::MouseButton::Middle];
  544. io.MouseWheel = float(state.m_mouse.m_mz);
  545. }
  546. #endif // USE_ENTRY
  547. }
  548. #if USE_ENTRY
  549. void pwToWindow(entry::WindowHandle _handle, PlatformWindow* _pw)
  550. {
  551. s_ctx.m_window[_handle.idx].m_pw = _pw;
  552. }
  553. void imguiUpdateWindow(const entry::WindowState& _state)
  554. {
  555. OcornutImguiContext::Window& window = s_ctx.m_window[_state.m_handle.idx];
  556. if (window.m_state.m_nwh != _state.m_nwh
  557. || (window.m_state.m_width != _state.m_width
  558. || window.m_state.m_height != _state.m_height) )
  559. {
  560. // When window changes size or native window handle changed
  561. // frame buffer must be recreated.
  562. if (bgfx::isValid(window.m_fbh) )
  563. {
  564. bgfx::destroyFrameBuffer(window.m_fbh);
  565. window.m_fbh.idx = bgfx::invalidHandle;
  566. }
  567. if (NULL != _state.m_nwh)
  568. {
  569. window.m_fbh = bgfx::createFrameBuffer(_state.m_nwh, _state.m_width, _state.m_height);
  570. window.m_viewId = 200 + _state.m_handle.idx;
  571. }
  572. else
  573. {
  574. window.m_viewId = s_ctx.m_viewId;
  575. }
  576. }
  577. memcpy(&window.m_state, &_state, sizeof(entry::WindowState) );
  578. }
  579. #endif // USE_ENTRY
  580. void* OcornutImguiContext::memAlloc(size_t _size)
  581. {
  582. return BX_ALLOC(s_ctx.m_allocator, _size);
  583. }
  584. void OcornutImguiContext::memFree(void* _ptr)
  585. {
  586. BX_FREE(s_ctx.m_allocator, _ptr);
  587. }
  588. void OcornutImguiContext::renderDrawLists(ImDrawData* _drawData)
  589. {
  590. s_ctx.render(_drawData);
  591. }
  592. void IMGUI_create(const void* _data, uint32_t _size, float _fontSize, bx::AllocatorI* _allocator)
  593. {
  594. s_ctx.create(_data, _size, _fontSize, _allocator);
  595. }
  596. void IMGUI_destroy()
  597. {
  598. s_ctx.destroy();
  599. }
  600. void IMGUI_beginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, int _width, int _height, char _inputChar, uint8_t _viewId)
  601. {
  602. s_ctx.beginFrame(_mx, _my, _button, _scroll, _width, _height, _inputChar, _viewId);
  603. }
  604. void IMGUI_endFrame()
  605. {
  606. s_ctx.endFrame();
  607. }