imgui.cpp 19 KB

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