ocornut_imgui.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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 <bgfx/embedded_shader.h>
  7. #include <bx/allocator.h>
  8. #include <bx/fpumath.h>
  9. #include <bx/timer.h>
  10. #include <ocornut-imgui/imgui.h>
  11. #include "imgui.h"
  12. #include "ocornut_imgui.h"
  13. #include "../bgfx_utils.h"
  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. #include "roboto_regular.ttf.h"
  31. #include "robotomono_regular.ttf.h"
  32. #include "icons_kenney.ttf.h"
  33. #include "icons_font_awesome.ttf.h"
  34. static const bgfx::EmbeddedShader s_embeddedShaders[] =
  35. {
  36. BGFX_EMBEDDED_SHADER(vs_ocornut_imgui),
  37. BGFX_EMBEDDED_SHADER(fs_ocornut_imgui),
  38. BGFX_EMBEDDED_SHADER_END()
  39. };
  40. struct FontRangeMerge
  41. {
  42. const void* data;
  43. size_t size;
  44. ImWchar ranges[3];
  45. };
  46. static FontRangeMerge s_fontRangeMerge[] =
  47. {
  48. { s_iconsKenneyTtf, sizeof(s_iconsKenneyTtf), { ICON_MIN_KI, ICON_MAX_KI, 0 } },
  49. { s_iconsFontAwesomeTtf, sizeof(s_iconsFontAwesomeTtf), { ICON_MIN_FA, ICON_MAX_FA, 0 } },
  50. };
  51. struct OcornutImguiContext
  52. {
  53. static void* memAlloc(size_t _size);
  54. static void memFree(void* _ptr);
  55. static void renderDrawLists(ImDrawData* _drawData);
  56. void render(ImDrawData* _drawData)
  57. {
  58. const ImGuiIO& io = ImGui::GetIO();
  59. const float width = io.DisplaySize.x;
  60. const float height = io.DisplaySize.y;
  61. {
  62. float ortho[16];
  63. bx::mtxOrtho(ortho, 0.0f, width, height, 0.0f, -1.0f, 1.0f);
  64. bgfx::setViewTransform(m_viewId, NULL, ortho);
  65. }
  66. // Render command lists
  67. for (int32_t ii = 0, num = _drawData->CmdListsCount; ii < num; ++ii)
  68. {
  69. bgfx::TransientVertexBuffer tvb;
  70. bgfx::TransientIndexBuffer tib;
  71. const ImDrawList* drawList = _drawData->CmdLists[ii];
  72. uint32_t numVertices = (uint32_t)drawList->VtxBuffer.size();
  73. uint32_t numIndices = (uint32_t)drawList->IdxBuffer.size();
  74. if (!checkAvailTransientBuffers(numVertices, m_decl, numIndices) )
  75. {
  76. // not enough space in transient buffer just quit drawing the rest...
  77. break;
  78. }
  79. bgfx::allocTransientVertexBuffer(&tvb, numVertices, m_decl);
  80. bgfx::allocTransientIndexBuffer(&tib, numIndices);
  81. ImDrawVert* verts = (ImDrawVert*)tvb.data;
  82. memcpy(verts, drawList->VtxBuffer.begin(), numVertices * sizeof(ImDrawVert) );
  83. ImDrawIdx* indices = (ImDrawIdx*)tib.data;
  84. memcpy(indices, drawList->IdxBuffer.begin(), numIndices * sizeof(ImDrawIdx) );
  85. uint32_t offset = 0;
  86. for (const ImDrawCmd* cmd = drawList->CmdBuffer.begin(), *cmdEnd = drawList->CmdBuffer.end(); cmd != cmdEnd; ++cmd)
  87. {
  88. if (cmd->UserCallback)
  89. {
  90. cmd->UserCallback(drawList, cmd);
  91. }
  92. else if (0 != cmd->ElemCount)
  93. {
  94. uint64_t state = 0
  95. | BGFX_STATE_RGB_WRITE
  96. | BGFX_STATE_ALPHA_WRITE
  97. | BGFX_STATE_MSAA
  98. ;
  99. bgfx::TextureHandle th = m_texture;
  100. bgfx::ProgramHandle program = m_program;
  101. if (NULL != cmd->TextureId)
  102. {
  103. union { ImTextureID ptr; struct { bgfx::TextureHandle handle; uint8_t flags; uint8_t mip; } s; } texture = { cmd->TextureId };
  104. state |= 0 != (IMGUI_FLAGS_ALPHA_BLEND & texture.s.flags)
  105. ? BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  106. : BGFX_STATE_NONE
  107. ;
  108. th = texture.s.handle;
  109. if (0 != texture.s.mip)
  110. {
  111. extern bgfx::ProgramHandle imguiGetImageProgram(uint8_t _mip);
  112. program = imguiGetImageProgram(texture.s.mip);
  113. }
  114. }
  115. else
  116. {
  117. state |= BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA);
  118. }
  119. const uint16_t xx = uint16_t(bx::fmax(cmd->ClipRect.x, 0.0f) );
  120. const uint16_t yy = uint16_t(bx::fmax(cmd->ClipRect.y, 0.0f) );
  121. bgfx::setScissor(xx, yy
  122. , uint16_t(bx::fmin(cmd->ClipRect.z, 65535.0f)-xx)
  123. , uint16_t(bx::fmin(cmd->ClipRect.w, 65535.0f)-yy)
  124. );
  125. bgfx::setState(state);
  126. bgfx::setTexture(0, s_tex, th);
  127. bgfx::setVertexBuffer(&tvb, 0, numVertices);
  128. bgfx::setIndexBuffer(&tib, offset, cmd->ElemCount);
  129. bgfx::submit(cmd->ViewId, program);
  130. }
  131. offset += cmd->ElemCount;
  132. }
  133. }
  134. }
  135. void create(float _fontSize, bx::AllocatorI* _allocator)
  136. {
  137. m_viewId = 255;
  138. m_allocator = _allocator;
  139. m_lastScroll = 0;
  140. m_last = bx::getHPCounter();
  141. ImGuiIO& io = ImGui::GetIO();
  142. io.RenderDrawListsFn = renderDrawLists;
  143. if (NULL != m_allocator)
  144. {
  145. io.MemAllocFn = memAlloc;
  146. io.MemFreeFn = memFree;
  147. }
  148. io.DisplaySize = ImVec2(1280.0f, 720.0f);
  149. io.DeltaTime = 1.0f / 60.0f;
  150. io.IniFilename = NULL;
  151. setupStyle(true);
  152. #if defined(SCI_NAMESPACE)
  153. io.KeyMap[ImGuiKey_Tab] = (int)entry::Key::Tab;
  154. io.KeyMap[ImGuiKey_LeftArrow] = (int)entry::Key::Left;
  155. io.KeyMap[ImGuiKey_RightArrow] = (int)entry::Key::Right;
  156. io.KeyMap[ImGuiKey_UpArrow] = (int)entry::Key::Up;
  157. io.KeyMap[ImGuiKey_DownArrow] = (int)entry::Key::Down;
  158. io.KeyMap[ImGuiKey_Home] = (int)entry::Key::Home;
  159. io.KeyMap[ImGuiKey_End] = (int)entry::Key::End;
  160. io.KeyMap[ImGuiKey_Delete] = (int)entry::Key::Delete;
  161. io.KeyMap[ImGuiKey_Backspace] = (int)entry::Key::Backspace;
  162. io.KeyMap[ImGuiKey_Enter] = (int)entry::Key::Return;
  163. io.KeyMap[ImGuiKey_Escape] = (int)entry::Key::Esc;
  164. io.KeyMap[ImGuiKey_A] = (int)entry::Key::KeyA;
  165. io.KeyMap[ImGuiKey_C] = (int)entry::Key::KeyC;
  166. io.KeyMap[ImGuiKey_V] = (int)entry::Key::KeyV;
  167. io.KeyMap[ImGuiKey_X] = (int)entry::Key::KeyX;
  168. io.KeyMap[ImGuiKey_Y] = (int)entry::Key::KeyY;
  169. io.KeyMap[ImGuiKey_Z] = (int)entry::Key::KeyZ;
  170. #endif // defined(SCI_NAMESPACE)
  171. bgfx::RendererType::Enum type = bgfx::getRendererType();
  172. m_program = bgfx::createProgram(
  173. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_ocornut_imgui")
  174. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_ocornut_imgui")
  175. , true
  176. );
  177. m_decl
  178. .begin()
  179. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  180. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  181. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  182. .end();
  183. s_tex = bgfx::createUniform("s_tex", bgfx::UniformType::Int1);
  184. uint8_t* data;
  185. int32_t width;
  186. int32_t height;
  187. {
  188. ImFontConfig config;
  189. config.FontDataOwnedByAtlas = false;
  190. config.MergeMode = false;
  191. // config.MergeGlyphCenterV = true;
  192. m_font[ImGui::Font::Regular] = io.Fonts->AddFontFromMemoryTTF( (void*)s_robotoRegularTtf, sizeof(s_robotoRegularTtf), _fontSize, &config);
  193. m_font[ImGui::Font::Mono ] = io.Fonts->AddFontFromMemoryTTF( (void*)s_robotoMonoRegularTtf, sizeof(s_robotoMonoRegularTtf), _fontSize-3.0f, &config);
  194. config.MergeMode = true;
  195. config.DstFont = m_font[ImGui::Font::Regular];
  196. for (uint32_t ii = 0; ii < BX_COUNTOF(s_fontRangeMerge); ++ii)
  197. {
  198. const FontRangeMerge& frm = s_fontRangeMerge[ii];
  199. io.Fonts->AddFontFromMemoryTTF( (void*)frm.data
  200. , (int)frm.size
  201. , _fontSize-3.0f
  202. , &config
  203. , frm.ranges
  204. );
  205. }
  206. }
  207. io.Fonts->GetTexDataAsRGBA32(&data, &width, &height);
  208. m_texture = bgfx::createTexture2D(
  209. (uint16_t)width
  210. , (uint16_t)height
  211. , false
  212. , 1
  213. , bgfx::TextureFormat::BGRA8
  214. , 0
  215. , bgfx::copy(data, width*height*4)
  216. );
  217. ImGui::InitDockContext();
  218. }
  219. void destroy()
  220. {
  221. ImGui::ShutdownDockContext();
  222. ImGui::Shutdown();
  223. bgfx::destroyUniform(s_tex);
  224. bgfx::destroyTexture(m_texture);
  225. bgfx::destroyProgram(m_program);
  226. m_allocator = NULL;
  227. }
  228. void setupStyle(bool _dark)
  229. {
  230. // Doug Binks' darl color scheme
  231. // https://gist.github.com/dougbinks/8089b4bbaccaaf6fa204236978d165a9
  232. ImGuiStyle& style = ImGui::GetStyle();
  233. style.FrameRounding = 4.0f;
  234. // light style from Pacome Danhiez (user itamago)
  235. // https://github.com/ocornut/imgui/pull/511#issuecomment-175719267
  236. style.Colors[ImGuiCol_Text] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
  237. style.Colors[ImGuiCol_TextDisabled] = ImVec4(0.60f, 0.60f, 0.60f, 1.00f);
  238. style.Colors[ImGuiCol_WindowBg] = ImVec4(0.94f, 0.94f, 0.94f, 1.00f);
  239. style.Colors[ImGuiCol_ChildWindowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
  240. style.Colors[ImGuiCol_Border] = ImVec4(0.00f, 0.00f, 0.00f, 0.39f);
  241. style.Colors[ImGuiCol_BorderShadow] = ImVec4(1.00f, 1.00f, 1.00f, 0.10f);
  242. style.Colors[ImGuiCol_FrameBg] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
  243. style.Colors[ImGuiCol_FrameBgHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.40f);
  244. style.Colors[ImGuiCol_FrameBgActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f);
  245. style.Colors[ImGuiCol_TitleBg] = ImVec4(0.96f, 0.96f, 0.96f, 1.00f);
  246. style.Colors[ImGuiCol_TitleBgCollapsed] = ImVec4(1.00f, 1.00f, 1.00f, 0.51f);
  247. style.Colors[ImGuiCol_TitleBgActive] = ImVec4(0.82f, 0.82f, 0.82f, 1.00f);
  248. style.Colors[ImGuiCol_MenuBarBg] = ImVec4(0.86f, 0.86f, 0.86f, 1.00f);
  249. style.Colors[ImGuiCol_ScrollbarBg] = ImVec4(0.98f, 0.98f, 0.98f, 0.53f);
  250. style.Colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.69f, 0.69f, 0.69f, 0.80f);
  251. style.Colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.49f, 0.49f, 0.49f, 0.80f);
  252. style.Colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.49f, 0.49f, 0.49f, 1.00f);
  253. style.Colors[ImGuiCol_ComboBg] = ImVec4(0.86f, 0.86f, 0.86f, 0.99f);
  254. style.Colors[ImGuiCol_CheckMark] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
  255. style.Colors[ImGuiCol_SliderGrab] = ImVec4(0.26f, 0.59f, 0.98f, 0.78f);
  256. style.Colors[ImGuiCol_SliderGrabActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
  257. style.Colors[ImGuiCol_Button] = ImVec4(0.26f, 0.59f, 0.98f, 0.40f);
  258. style.Colors[ImGuiCol_ButtonHovered] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
  259. style.Colors[ImGuiCol_ButtonActive] = ImVec4(0.06f, 0.53f, 0.98f, 1.00f);
  260. style.Colors[ImGuiCol_Header] = ImVec4(0.26f, 0.59f, 0.98f, 0.31f);
  261. style.Colors[ImGuiCol_HeaderHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.80f);
  262. style.Colors[ImGuiCol_HeaderActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
  263. style.Colors[ImGuiCol_Column] = ImVec4(0.39f, 0.39f, 0.39f, 1.00f);
  264. style.Colors[ImGuiCol_ColumnHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.78f);
  265. style.Colors[ImGuiCol_ColumnActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
  266. style.Colors[ImGuiCol_ResizeGrip] = ImVec4(1.00f, 1.00f, 1.00f, 0.50f);
  267. style.Colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f);
  268. style.Colors[ImGuiCol_ResizeGripActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f);
  269. style.Colors[ImGuiCol_CloseButton] = ImVec4(0.59f, 0.59f, 0.59f, 0.50f);
  270. style.Colors[ImGuiCol_CloseButtonHovered] = ImVec4(0.98f, 0.39f, 0.36f, 1.00f);
  271. style.Colors[ImGuiCol_CloseButtonActive] = ImVec4(0.98f, 0.39f, 0.36f, 1.00f);
  272. style.Colors[ImGuiCol_PlotLines] = ImVec4(0.39f, 0.39f, 0.39f, 1.00f);
  273. style.Colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.43f, 0.35f, 1.00f);
  274. style.Colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f);
  275. style.Colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f);
  276. style.Colors[ImGuiCol_TextSelectedBg] = ImVec4(0.26f, 0.59f, 0.98f, 0.35f);
  277. style.Colors[ImGuiCol_PopupBg] = ImVec4(1.00f, 1.00f, 1.00f, 0.94f);
  278. style.Colors[ImGuiCol_ModalWindowDarkening] = ImVec4(0.20f, 0.20f, 0.20f, 0.35f);
  279. if (_dark)
  280. {
  281. for (int i = 0; i <= ImGuiCol_COUNT; i++)
  282. {
  283. ImVec4& col = style.Colors[i];
  284. float H, S, V;
  285. ImGui::ColorConvertRGBtoHSV( col.x, col.y, col.z, H, S, V );
  286. if( S < 0.1f )
  287. {
  288. V = 1.0f - V;
  289. }
  290. ImGui::ColorConvertHSVtoRGB( H, S, V, col.x, col.y, col.z );
  291. }
  292. }
  293. }
  294. void beginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, int _width, int _height, char _inputChar, uint8_t _viewId)
  295. {
  296. m_viewId = _viewId;
  297. ImGuiIO& io = ImGui::GetIO();
  298. if (_inputChar < 0x7f)
  299. {
  300. io.AddInputCharacter(_inputChar); // ASCII or GTFO! :(
  301. }
  302. io.DisplaySize = ImVec2( (float)_width, (float)_height);
  303. const int64_t now = bx::getHPCounter();
  304. const int64_t frameTime = now - m_last;
  305. m_last = now;
  306. const double freq = double(bx::getHPFrequency() );
  307. io.DeltaTime = float(frameTime/freq);
  308. io.MousePos = ImVec2( (float)_mx, (float)_my);
  309. io.MouseDown[0] = 0 != (_button & IMGUI_MBUT_LEFT);
  310. io.MouseDown[1] = 0 != (_button & IMGUI_MBUT_RIGHT);
  311. io.MouseDown[2] = 0 != (_button & IMGUI_MBUT_MIDDLE);
  312. io.MouseWheel = (float)(_scroll - m_lastScroll);
  313. m_lastScroll = _scroll;
  314. #if defined(SCI_NAMESPACE)
  315. uint8_t modifiers = inputGetModifiersState();
  316. io.KeyShift = 0 != (modifiers & (entry::Modifier::LeftShift | entry::Modifier::RightShift) );
  317. io.KeyCtrl = 0 != (modifiers & (entry::Modifier::LeftCtrl | entry::Modifier::RightCtrl ) );
  318. io.KeyAlt = 0 != (modifiers & (entry::Modifier::LeftAlt | entry::Modifier::RightAlt ) );
  319. for (int32_t ii = 0; ii < (int32_t)entry::Key::Count; ++ii)
  320. {
  321. io.KeysDown[ii] = inputGetKeyState(entry::Key::Enum(ii) );
  322. }
  323. #endif // defined(SCI_NAMESPACE)
  324. ImGui::NewFrame();
  325. ImGuizmo::BeginFrame();
  326. ImGui::PushStyleVar(ImGuiStyleVar_ViewId, (float)_viewId);
  327. }
  328. void endFrame()
  329. {
  330. ImGui::PopStyleVar(1);
  331. ImGui::Render();
  332. }
  333. bx::AllocatorI* m_allocator;
  334. bgfx::VertexDecl m_decl;
  335. bgfx::ProgramHandle m_program;
  336. bgfx::TextureHandle m_texture;
  337. bgfx::UniformHandle s_tex;
  338. ImFont* m_font[ImGui::Font::Count];
  339. int64_t m_last;
  340. int32_t m_lastScroll;
  341. uint8_t m_viewId;
  342. };
  343. static OcornutImguiContext s_ctx;
  344. void* OcornutImguiContext::memAlloc(size_t _size)
  345. {
  346. return BX_ALLOC(s_ctx.m_allocator, _size);
  347. }
  348. void OcornutImguiContext::memFree(void* _ptr)
  349. {
  350. BX_FREE(s_ctx.m_allocator, _ptr);
  351. }
  352. void OcornutImguiContext::renderDrawLists(ImDrawData* _drawData)
  353. {
  354. s_ctx.render(_drawData);
  355. }
  356. void IMGUI_create(float _fontSize, bx::AllocatorI* _allocator)
  357. {
  358. s_ctx.create(_fontSize, _allocator);
  359. }
  360. void IMGUI_destroy()
  361. {
  362. s_ctx.destroy();
  363. }
  364. void IMGUI_beginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, int _width, int _height, char _inputChar, uint8_t _viewId)
  365. {
  366. s_ctx.beginFrame(_mx, _my, _button, _scroll, _width, _height, _inputChar, _viewId);
  367. }
  368. void IMGUI_endFrame()
  369. {
  370. s_ctx.endFrame();
  371. }
  372. namespace ImGui
  373. {
  374. void PushFont(Font::Enum _font)
  375. {
  376. PushFont(s_ctx.m_font[_font]);
  377. }
  378. } // namespace ImGui