imgui.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /*
  2. * Copyright 2014-2015 Daniel Collin. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. #include <bgfx/bgfx.h>
  6. #include <bgfx/embedded_shader.h>
  7. #include <bx/allocator.h>
  8. #include <bx/math.h>
  9. #include <bx/timer.h>
  10. #include <dear-imgui/imgui.h>
  11. #include <dear-imgui/imgui_internal.h>
  12. #include "imgui.h"
  13. #include "../bgfx_utils.h"
  14. //#define USE_ENTRY 1
  15. #ifndef USE_ENTRY
  16. # define USE_ENTRY 0
  17. #endif // USE_ENTRY
  18. #if USE_ENTRY
  19. # include "../entry/entry.h"
  20. # include "../entry/input.h"
  21. #endif // USE_ENTRY
  22. #include "vs_ocornut_imgui.bin.h"
  23. #include "fs_ocornut_imgui.bin.h"
  24. #include "vs_imgui_image.bin.h"
  25. #include "fs_imgui_image.bin.h"
  26. #include "roboto_regular.ttf.h"
  27. #include "robotomono_regular.ttf.h"
  28. #include "icons_kenney.ttf.h"
  29. #include "icons_font_awesome.ttf.h"
  30. static const bgfx::EmbeddedShader s_embeddedShaders[] =
  31. {
  32. BGFX_EMBEDDED_SHADER(vs_ocornut_imgui),
  33. BGFX_EMBEDDED_SHADER(fs_ocornut_imgui),
  34. BGFX_EMBEDDED_SHADER(vs_imgui_image),
  35. BGFX_EMBEDDED_SHADER(fs_imgui_image),
  36. BGFX_EMBEDDED_SHADER_END()
  37. };
  38. struct FontRangeMerge
  39. {
  40. const void* data;
  41. size_t size;
  42. ImWchar ranges[3];
  43. };
  44. static FontRangeMerge s_fontRangeMerge[] =
  45. {
  46. { s_iconsKenneyTtf, sizeof(s_iconsKenneyTtf), { ICON_MIN_KI, ICON_MAX_KI, 0 } },
  47. { s_iconsFontAwesomeTtf, sizeof(s_iconsFontAwesomeTtf), { ICON_MIN_FA, ICON_MAX_FA, 0 } },
  48. };
  49. static void* memAlloc(size_t _size, void* _userData);
  50. static void memFree(void* _ptr, void* _userData);
  51. struct OcornutImguiContext
  52. {
  53. void render(ImDrawData* _drawData)
  54. {
  55. // Avoid rendering when minimized, scale coordinates for retina displays (screen coordinates != framebuffer coordinates)
  56. int fb_width = (int)(_drawData->DisplaySize.x * _drawData->FramebufferScale.x);
  57. int fb_height = (int)(_drawData->DisplaySize.y * _drawData->FramebufferScale.y);
  58. if (fb_width <= 0 || fb_height <= 0)
  59. return;
  60. bgfx::setViewName(m_viewId, "ImGui");
  61. bgfx::setViewMode(m_viewId, bgfx::ViewMode::Sequential);
  62. const bgfx::Caps* caps = bgfx::getCaps();
  63. {
  64. float ortho[16];
  65. float x = _drawData->DisplayPos.x;
  66. float y = _drawData->DisplayPos.y;
  67. float width = _drawData->DisplaySize.x;
  68. float height = _drawData->DisplaySize.y;
  69. bx::mtxOrtho(ortho, x, x + width, y + height, y, 0.0f, 1000.0f, 0.0f, caps->homogeneousDepth);
  70. bgfx::setViewTransform(m_viewId, NULL, ortho);
  71. bgfx::setViewRect(m_viewId, 0, 0, uint16_t(width), uint16_t(height) );
  72. }
  73. const ImVec2 clipPos = _drawData->DisplayPos; // (0,0) unless using multi-viewports
  74. const ImVec2 clipScale = _drawData->FramebufferScale; // (1,1) unless using retina display which are often (2,2)
  75. // Render command lists
  76. for (int32_t ii = 0, num = _drawData->CmdListsCount; ii < num; ++ii)
  77. {
  78. bgfx::TransientVertexBuffer tvb;
  79. bgfx::TransientIndexBuffer tib;
  80. const ImDrawList* drawList = _drawData->CmdLists[ii];
  81. uint32_t numVertices = (uint32_t)drawList->VtxBuffer.size();
  82. uint32_t numIndices = (uint32_t)drawList->IdxBuffer.size();
  83. if (!checkAvailTransientBuffers(numVertices, m_layout, numIndices) )
  84. {
  85. // not enough space in transient buffer just quit drawing the rest...
  86. break;
  87. }
  88. bgfx::allocTransientVertexBuffer(&tvb, numVertices, m_layout);
  89. bgfx::allocTransientIndexBuffer(&tib, numIndices, sizeof(ImDrawIdx) == 4);
  90. ImDrawVert* verts = (ImDrawVert*)tvb.data;
  91. bx::memCopy(verts, drawList->VtxBuffer.begin(), numVertices * sizeof(ImDrawVert) );
  92. ImDrawIdx* indices = (ImDrawIdx*)tib.data;
  93. bx::memCopy(indices, drawList->IdxBuffer.begin(), numIndices * sizeof(ImDrawIdx) );
  94. bgfx::Encoder* encoder = bgfx::begin();
  95. for (const ImDrawCmd* cmd = drawList->CmdBuffer.begin(), *cmdEnd = drawList->CmdBuffer.end(); cmd != cmdEnd; ++cmd)
  96. {
  97. if (cmd->UserCallback)
  98. {
  99. cmd->UserCallback(drawList, cmd);
  100. }
  101. else if (0 != cmd->ElemCount)
  102. {
  103. uint64_t state = 0
  104. | BGFX_STATE_WRITE_RGB
  105. | BGFX_STATE_WRITE_A
  106. | BGFX_STATE_MSAA
  107. ;
  108. bgfx::TextureHandle th = m_texture;
  109. bgfx::ProgramHandle program = m_program;
  110. if (NULL != cmd->TextureId)
  111. {
  112. union { ImTextureID ptr; struct { bgfx::TextureHandle handle; uint8_t flags; uint8_t mip; } s; } texture = { cmd->TextureId };
  113. state |= 0 != (IMGUI_FLAGS_ALPHA_BLEND & texture.s.flags)
  114. ? BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  115. : BGFX_STATE_NONE
  116. ;
  117. th = texture.s.handle;
  118. if (0 != texture.s.mip)
  119. {
  120. const float lodEnabled[4] = { float(texture.s.mip), 1.0f, 0.0f, 0.0f };
  121. bgfx::setUniform(u_imageLodEnabled, lodEnabled);
  122. program = m_imageProgram;
  123. }
  124. }
  125. else
  126. {
  127. state |= BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA);
  128. }
  129. // Project scissor/clipping rectangles into framebuffer space
  130. ImVec4 clipRect;
  131. clipRect.x = (cmd->ClipRect.x - clipPos.x) * clipScale.x;
  132. clipRect.y = (cmd->ClipRect.y - clipPos.y) * clipScale.y;
  133. clipRect.z = (cmd->ClipRect.z - clipPos.x) * clipScale.x;
  134. clipRect.w = (cmd->ClipRect.w - clipPos.y) * clipScale.y;
  135. if (clipRect.x < fb_width
  136. && clipRect.y < fb_height
  137. && clipRect.z >= 0.0f
  138. && clipRect.w >= 0.0f)
  139. {
  140. const uint16_t xx = uint16_t(bx::max(clipRect.x, 0.0f) );
  141. const uint16_t yy = uint16_t(bx::max(clipRect.y, 0.0f) );
  142. encoder->setScissor(xx, yy
  143. , uint16_t(bx::min(clipRect.z, 65535.0f)-xx)
  144. , uint16_t(bx::min(clipRect.w, 65535.0f)-yy)
  145. );
  146. encoder->setState(state);
  147. encoder->setTexture(0, s_tex, th);
  148. encoder->setVertexBuffer(0, &tvb, cmd->VtxOffset, numVertices);
  149. encoder->setIndexBuffer(&tib, cmd->IdxOffset, cmd->ElemCount);
  150. encoder->submit(m_viewId, program);
  151. }
  152. }
  153. }
  154. bgfx::end(encoder);
  155. }
  156. }
  157. void create(float _fontSize, bx::AllocatorI* _allocator)
  158. {
  159. m_allocator = _allocator;
  160. if (NULL == _allocator)
  161. {
  162. static bx::DefaultAllocator allocator;
  163. m_allocator = &allocator;
  164. }
  165. m_viewId = 255;
  166. m_lastScroll = 0;
  167. m_last = bx::getHPCounter();
  168. ImGui::SetAllocatorFunctions(memAlloc, memFree, NULL);
  169. m_imgui = ImGui::CreateContext();
  170. ImGuiIO& io = ImGui::GetIO();
  171. io.DisplaySize = ImVec2(1280.0f, 720.0f);
  172. io.DeltaTime = 1.0f / 60.0f;
  173. io.IniFilename = NULL;
  174. setupStyle(true);
  175. io.BackendFlags |= ImGuiBackendFlags_RendererHasVtxOffset;
  176. #if USE_ENTRY
  177. for (int32_t ii = 0; ii < (int32_t)entry::Key::Count; ++ii)
  178. {
  179. m_keyMap[ii] = ImGuiKey_None;
  180. }
  181. m_keyMap[entry::Key::Esc] = ImGuiKey_Escape;
  182. m_keyMap[entry::Key::Return] = ImGuiKey_Enter;
  183. m_keyMap[entry::Key::Tab] = ImGuiKey_Tab;
  184. m_keyMap[entry::Key::Space] = ImGuiKey_Space;
  185. m_keyMap[entry::Key::Backspace] = ImGuiKey_Backspace;
  186. m_keyMap[entry::Key::Up] = ImGuiKey_UpArrow;
  187. m_keyMap[entry::Key::Down] = ImGuiKey_DownArrow;
  188. m_keyMap[entry::Key::Left] = ImGuiKey_LeftArrow;
  189. m_keyMap[entry::Key::Right] = ImGuiKey_RightArrow;
  190. m_keyMap[entry::Key::Insert] = ImGuiKey_Insert;
  191. m_keyMap[entry::Key::Delete] = ImGuiKey_Delete;
  192. m_keyMap[entry::Key::Home] = ImGuiKey_Home;
  193. m_keyMap[entry::Key::End] = ImGuiKey_End;
  194. m_keyMap[entry::Key::PageUp] = ImGuiKey_PageUp;
  195. m_keyMap[entry::Key::PageDown] = ImGuiKey_PageDown;
  196. m_keyMap[entry::Key::Print] = ImGuiKey_PrintScreen;
  197. m_keyMap[entry::Key::Plus] = ImGuiKey_Equal;
  198. m_keyMap[entry::Key::Minus] = ImGuiKey_Minus;
  199. m_keyMap[entry::Key::LeftBracket] = ImGuiKey_LeftBracket;
  200. m_keyMap[entry::Key::RightBracket] = ImGuiKey_RightBracket;
  201. m_keyMap[entry::Key::Semicolon] = ImGuiKey_Semicolon;
  202. m_keyMap[entry::Key::Quote] = ImGuiKey_Apostrophe;
  203. m_keyMap[entry::Key::Comma] = ImGuiKey_Comma;
  204. m_keyMap[entry::Key::Period] = ImGuiKey_Period;
  205. m_keyMap[entry::Key::Slash] = ImGuiKey_Slash;
  206. m_keyMap[entry::Key::Backslash] = ImGuiKey_Backslash;
  207. m_keyMap[entry::Key::Tilde] = ImGuiKey_GraveAccent;
  208. m_keyMap[entry::Key::F1] = ImGuiKey_F1;
  209. m_keyMap[entry::Key::F2] = ImGuiKey_F2;
  210. m_keyMap[entry::Key::F3] = ImGuiKey_F3;
  211. m_keyMap[entry::Key::F4] = ImGuiKey_F4;
  212. m_keyMap[entry::Key::F5] = ImGuiKey_F5;
  213. m_keyMap[entry::Key::F6] = ImGuiKey_F6;
  214. m_keyMap[entry::Key::F7] = ImGuiKey_F7;
  215. m_keyMap[entry::Key::F8] = ImGuiKey_F8;
  216. m_keyMap[entry::Key::F9] = ImGuiKey_F9;
  217. m_keyMap[entry::Key::F10] = ImGuiKey_F10;
  218. m_keyMap[entry::Key::F11] = ImGuiKey_F11;
  219. m_keyMap[entry::Key::F12] = ImGuiKey_F12;
  220. m_keyMap[entry::Key::NumPad0] = ImGuiKey_Keypad0;
  221. m_keyMap[entry::Key::NumPad1] = ImGuiKey_Keypad1;
  222. m_keyMap[entry::Key::NumPad2] = ImGuiKey_Keypad2;
  223. m_keyMap[entry::Key::NumPad3] = ImGuiKey_Keypad3;
  224. m_keyMap[entry::Key::NumPad4] = ImGuiKey_Keypad4;
  225. m_keyMap[entry::Key::NumPad5] = ImGuiKey_Keypad5;
  226. m_keyMap[entry::Key::NumPad6] = ImGuiKey_Keypad6;
  227. m_keyMap[entry::Key::NumPad7] = ImGuiKey_Keypad7;
  228. m_keyMap[entry::Key::NumPad8] = ImGuiKey_Keypad8;
  229. m_keyMap[entry::Key::NumPad9] = ImGuiKey_Keypad9;
  230. m_keyMap[entry::Key::Key0] = ImGuiKey_0;
  231. m_keyMap[entry::Key::Key1] = ImGuiKey_1;
  232. m_keyMap[entry::Key::Key2] = ImGuiKey_2;
  233. m_keyMap[entry::Key::Key3] = ImGuiKey_3;
  234. m_keyMap[entry::Key::Key4] = ImGuiKey_4;
  235. m_keyMap[entry::Key::Key5] = ImGuiKey_5;
  236. m_keyMap[entry::Key::Key6] = ImGuiKey_6;
  237. m_keyMap[entry::Key::Key7] = ImGuiKey_7;
  238. m_keyMap[entry::Key::Key8] = ImGuiKey_8;
  239. m_keyMap[entry::Key::Key9] = ImGuiKey_9;
  240. m_keyMap[entry::Key::KeyA] = ImGuiKey_A;
  241. m_keyMap[entry::Key::KeyB] = ImGuiKey_B;
  242. m_keyMap[entry::Key::KeyC] = ImGuiKey_C;
  243. m_keyMap[entry::Key::KeyD] = ImGuiKey_D;
  244. m_keyMap[entry::Key::KeyE] = ImGuiKey_E;
  245. m_keyMap[entry::Key::KeyF] = ImGuiKey_F;
  246. m_keyMap[entry::Key::KeyG] = ImGuiKey_G;
  247. m_keyMap[entry::Key::KeyH] = ImGuiKey_H;
  248. m_keyMap[entry::Key::KeyI] = ImGuiKey_I;
  249. m_keyMap[entry::Key::KeyJ] = ImGuiKey_J;
  250. m_keyMap[entry::Key::KeyK] = ImGuiKey_K;
  251. m_keyMap[entry::Key::KeyL] = ImGuiKey_L;
  252. m_keyMap[entry::Key::KeyM] = ImGuiKey_M;
  253. m_keyMap[entry::Key::KeyN] = ImGuiKey_N;
  254. m_keyMap[entry::Key::KeyO] = ImGuiKey_O;
  255. m_keyMap[entry::Key::KeyP] = ImGuiKey_P;
  256. m_keyMap[entry::Key::KeyQ] = ImGuiKey_Q;
  257. m_keyMap[entry::Key::KeyR] = ImGuiKey_R;
  258. m_keyMap[entry::Key::KeyS] = ImGuiKey_S;
  259. m_keyMap[entry::Key::KeyT] = ImGuiKey_T;
  260. m_keyMap[entry::Key::KeyU] = ImGuiKey_U;
  261. m_keyMap[entry::Key::KeyV] = ImGuiKey_V;
  262. m_keyMap[entry::Key::KeyW] = ImGuiKey_W;
  263. m_keyMap[entry::Key::KeyX] = ImGuiKey_X;
  264. m_keyMap[entry::Key::KeyY] = ImGuiKey_Y;
  265. m_keyMap[entry::Key::KeyZ] = ImGuiKey_Z;
  266. io.ConfigFlags |= 0
  267. | ImGuiConfigFlags_NavEnableGamepad
  268. | ImGuiConfigFlags_NavEnableKeyboard
  269. ;
  270. m_keyMap[entry::Key::GamepadStart] = ImGuiKey_GamepadStart;
  271. m_keyMap[entry::Key::GamepadBack] = ImGuiKey_GamepadBack;
  272. m_keyMap[entry::Key::GamepadY] = ImGuiKey_GamepadFaceUp;
  273. m_keyMap[entry::Key::GamepadA] = ImGuiKey_GamepadFaceDown;
  274. m_keyMap[entry::Key::GamepadX] = ImGuiKey_GamepadFaceLeft;
  275. m_keyMap[entry::Key::GamepadB] = ImGuiKey_GamepadFaceRight;
  276. m_keyMap[entry::Key::GamepadUp] = ImGuiKey_GamepadDpadUp;
  277. m_keyMap[entry::Key::GamepadDown] = ImGuiKey_GamepadDpadDown;
  278. m_keyMap[entry::Key::GamepadLeft] = ImGuiKey_GamepadDpadLeft;
  279. m_keyMap[entry::Key::GamepadRight] = ImGuiKey_GamepadDpadRight;
  280. m_keyMap[entry::Key::GamepadShoulderL] = ImGuiKey_GamepadL1;
  281. m_keyMap[entry::Key::GamepadShoulderR] = ImGuiKey_GamepadR1;
  282. m_keyMap[entry::Key::GamepadThumbL] = ImGuiKey_GamepadL3;
  283. m_keyMap[entry::Key::GamepadThumbR] = ImGuiKey_GamepadR3;
  284. #endif // USE_ENTRY
  285. bgfx::RendererType::Enum type = bgfx::getRendererType();
  286. m_program = bgfx::createProgram(
  287. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_ocornut_imgui")
  288. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_ocornut_imgui")
  289. , true
  290. );
  291. u_imageLodEnabled = bgfx::createUniform("u_imageLodEnabled", bgfx::UniformType::Vec4);
  292. m_imageProgram = bgfx::createProgram(
  293. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_imgui_image")
  294. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_imgui_image")
  295. , true
  296. );
  297. m_layout
  298. .begin()
  299. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  300. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  301. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  302. .end();
  303. s_tex = bgfx::createUniform("s_tex", bgfx::UniformType::Sampler);
  304. uint8_t* data;
  305. int32_t width;
  306. int32_t height;
  307. {
  308. ImFontConfig config;
  309. config.FontDataOwnedByAtlas = false;
  310. config.MergeMode = false;
  311. // config.MergeGlyphCenterV = true;
  312. const ImWchar* ranges = io.Fonts->GetGlyphRangesCyrillic();
  313. m_font[ImGui::Font::Regular] = io.Fonts->AddFontFromMemoryTTF( (void*)s_robotoRegularTtf, sizeof(s_robotoRegularTtf), _fontSize, &config, ranges);
  314. m_font[ImGui::Font::Mono ] = io.Fonts->AddFontFromMemoryTTF( (void*)s_robotoMonoRegularTtf, sizeof(s_robotoMonoRegularTtf), _fontSize-3.0f, &config, ranges);
  315. config.MergeMode = true;
  316. config.DstFont = m_font[ImGui::Font::Regular];
  317. for (uint32_t ii = 0; ii < BX_COUNTOF(s_fontRangeMerge); ++ii)
  318. {
  319. const FontRangeMerge& frm = s_fontRangeMerge[ii];
  320. io.Fonts->AddFontFromMemoryTTF( (void*)frm.data
  321. , (int)frm.size
  322. , _fontSize-3.0f
  323. , &config
  324. , frm.ranges
  325. );
  326. }
  327. }
  328. io.Fonts->GetTexDataAsRGBA32(&data, &width, &height);
  329. m_texture = bgfx::createTexture2D(
  330. (uint16_t)width
  331. , (uint16_t)height
  332. , false
  333. , 1
  334. , bgfx::TextureFormat::BGRA8
  335. , 0
  336. , bgfx::copy(data, width*height*4)
  337. );
  338. ImGui::InitDockContext();
  339. }
  340. void destroy()
  341. {
  342. ImGui::ShutdownDockContext();
  343. ImGui::DestroyContext(m_imgui);
  344. bgfx::destroy(s_tex);
  345. bgfx::destroy(m_texture);
  346. bgfx::destroy(u_imageLodEnabled);
  347. bgfx::destroy(m_imageProgram);
  348. bgfx::destroy(m_program);
  349. m_allocator = NULL;
  350. }
  351. void setupStyle(bool _dark)
  352. {
  353. // Doug Binks' darl color scheme
  354. // https://gist.github.com/dougbinks/8089b4bbaccaaf6fa204236978d165a9
  355. ImGuiStyle& style = ImGui::GetStyle();
  356. if (_dark)
  357. {
  358. ImGui::StyleColorsDark(&style);
  359. }
  360. else
  361. {
  362. ImGui::StyleColorsLight(&style);
  363. }
  364. style.FrameRounding = 4.0f;
  365. style.WindowBorderSize = 0.0f;
  366. }
  367. void beginFrame(
  368. int32_t _mx
  369. , int32_t _my
  370. , uint8_t _button
  371. , int32_t _scroll
  372. , int _width
  373. , int _height
  374. , int _inputChar
  375. , bgfx::ViewId _viewId
  376. )
  377. {
  378. m_viewId = _viewId;
  379. ImGuiIO& io = ImGui::GetIO();
  380. if (_inputChar >= 0)
  381. {
  382. io.AddInputCharacter(_inputChar);
  383. }
  384. io.DisplaySize = ImVec2( (float)_width, (float)_height);
  385. const int64_t now = bx::getHPCounter();
  386. const int64_t frameTime = now - m_last;
  387. m_last = now;
  388. const double freq = double(bx::getHPFrequency() );
  389. io.DeltaTime = float(frameTime/freq);
  390. io.AddMousePosEvent( (float)_mx, (float)_my);
  391. io.AddMouseButtonEvent(ImGuiMouseButton_Left, 0 != (_button & IMGUI_MBUT_LEFT ) );
  392. io.AddMouseButtonEvent(ImGuiMouseButton_Right, 0 != (_button & IMGUI_MBUT_RIGHT ) );
  393. io.AddMouseButtonEvent(ImGuiMouseButton_Middle, 0 != (_button & IMGUI_MBUT_MIDDLE) );
  394. io.AddMouseWheelEvent(0.0f, (float)(_scroll - m_lastScroll) );
  395. m_lastScroll = _scroll;
  396. #if USE_ENTRY
  397. uint8_t modifiers = inputGetModifiersState();
  398. io.AddKeyEvent(ImGuiKey_ModShift, 0 != (modifiers & (entry::Modifier::LeftShift | entry::Modifier::RightShift) ) );
  399. io.AddKeyEvent(ImGuiKey_ModCtrl, 0 != (modifiers & (entry::Modifier::LeftCtrl | entry::Modifier::RightCtrl ) ) );
  400. io.AddKeyEvent(ImGuiKey_ModAlt, 0 != (modifiers & (entry::Modifier::LeftAlt | entry::Modifier::RightAlt ) ) );
  401. io.AddKeyEvent(ImGuiKey_ModSuper, 0 != (modifiers & (entry::Modifier::LeftMeta | entry::Modifier::RightMeta ) ) );
  402. for (int32_t ii = 0; ii < (int32_t)entry::Key::Count; ++ii)
  403. {
  404. io.AddKeyEvent(m_keyMap[ii], inputGetKeyState(entry::Key::Enum(ii) ) );
  405. io.SetKeyEventNativeData(m_keyMap[ii], 0, 0, ii);
  406. }
  407. #endif // USE_ENTRY
  408. ImGui::NewFrame();
  409. ImGuizmo::BeginFrame();
  410. }
  411. void endFrame()
  412. {
  413. ImGui::Render();
  414. render(ImGui::GetDrawData() );
  415. }
  416. ImGuiContext* m_imgui;
  417. bx::AllocatorI* m_allocator;
  418. bgfx::VertexLayout m_layout;
  419. bgfx::ProgramHandle m_program;
  420. bgfx::ProgramHandle m_imageProgram;
  421. bgfx::TextureHandle m_texture;
  422. bgfx::UniformHandle s_tex;
  423. bgfx::UniformHandle u_imageLodEnabled;
  424. ImFont* m_font[ImGui::Font::Count];
  425. int64_t m_last;
  426. int32_t m_lastScroll;
  427. bgfx::ViewId m_viewId;
  428. #if USE_ENTRY
  429. ImGuiKey m_keyMap[(int)entry::Key::Count];
  430. #endif // USE_ENTRY
  431. };
  432. static OcornutImguiContext s_ctx;
  433. static void* memAlloc(size_t _size, void* _userData)
  434. {
  435. BX_UNUSED(_userData);
  436. return bx::alloc(s_ctx.m_allocator, _size);
  437. }
  438. static void memFree(void* _ptr, void* _userData)
  439. {
  440. BX_UNUSED(_userData);
  441. bx::free(s_ctx.m_allocator, _ptr);
  442. }
  443. void imguiCreate(float _fontSize, bx::AllocatorI* _allocator)
  444. {
  445. s_ctx.create(_fontSize, _allocator);
  446. }
  447. void imguiDestroy()
  448. {
  449. s_ctx.destroy();
  450. }
  451. void imguiBeginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, uint16_t _width, uint16_t _height, int _inputChar, bgfx::ViewId _viewId)
  452. {
  453. s_ctx.beginFrame(_mx, _my, _button, _scroll, _width, _height, _inputChar, _viewId);
  454. }
  455. void imguiEndFrame()
  456. {
  457. s_ctx.endFrame();
  458. }
  459. namespace ImGui
  460. {
  461. void PushFont(Font::Enum _font)
  462. {
  463. PushFont(s_ctx.m_font[_font]);
  464. }
  465. void PushEnabled(bool _enabled)
  466. {
  467. extern void PushItemFlag(int option, bool enabled);
  468. PushItemFlag(ImGuiItemFlags_Disabled, !_enabled);
  469. PushStyleVar(ImGuiStyleVar_Alpha, ImGui::GetStyle().Alpha * (_enabled ? 1.0f : 0.5f) );
  470. }
  471. void PopEnabled()
  472. {
  473. extern void PopItemFlag();
  474. PopItemFlag();
  475. PopStyleVar();
  476. }
  477. } // namespace ImGui
  478. BX_PRAGMA_DIAGNOSTIC_IGNORED_MSVC(4505); // error C4505: '' : unreferenced local function has been removed
  479. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wunused-function"); // warning: 'int rect_width_compare(const void*, const void*)' defined but not used
  480. BX_PRAGMA_DIAGNOSTIC_PUSH();
  481. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG("-Wunknown-pragmas")
  482. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wtype-limits"); // warning: comparison is always true due to limited range of data type
  483. #define STBTT_malloc(_size, _userData) memAlloc(_size, _userData)
  484. #define STBTT_free(_ptr, _userData) memFree(_ptr, _userData)
  485. #define STB_RECT_PACK_IMPLEMENTATION
  486. #include <stb/stb_rect_pack.h>
  487. #define STB_TRUETYPE_IMPLEMENTATION
  488. #include <stb/stb_truetype.h>
  489. BX_PRAGMA_DIAGNOSTIC_POP();