imgui.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548
  1. /*
  2. * Copyright 2011-2013 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. // This code is based on:
  6. //
  7. // Copyright (c) 2009-2010 Mikko Mononen [email protected]
  8. //
  9. // This software is provided 'as-is', without any express or implied
  10. // warranty. In no event will the authors be held liable for any damages
  11. // arising from the use of this software.
  12. // Permission is granted to anyone to use this software for any purpose,
  13. // including commercial applications, and to alter it and redistribute it
  14. // freely, subject to the following restrictions:
  15. // 1. The origin of this software must not be misrepresented; you must not
  16. // claim that you wrote the original software. If you use this software
  17. // in a product, an acknowledgment in the product documentation would be
  18. // appreciated but is not required.
  19. // 2. Altered source versions must be plainly marked as such, and must not be
  20. // misrepresented as being the original software.
  21. // 3. This notice may not be removed or altered from any source distribution.
  22. //
  23. // Source altered and distributed from https://github.com/AdrienHerubel/imgui
  24. #include <stdio.h>
  25. #include <bx/string.h>
  26. #include <bx/uint32_t.h>
  27. #include <bgfx.h>
  28. #include "imgui.h"
  29. #include "../fpumath.h"
  30. #include "vs_imgui_color.bin.h"
  31. #include "fs_imgui_color.bin.h"
  32. #include "vs_imgui_texture.bin.h"
  33. #include "fs_imgui_texture.bin.h"
  34. #define MAX_TEMP_COORDS 100
  35. #define NUM_CIRCLE_VERTS (8 * 4)
  36. static const int32_t BUTTON_HEIGHT = 20;
  37. static const int32_t SLIDER_HEIGHT = 20;
  38. static const int32_t SLIDER_MARKER_WIDTH = 10;
  39. static const int32_t CHECK_SIZE = 8;
  40. static const int32_t DEFAULT_SPACING = 4;
  41. static const int32_t TEXT_HEIGHT = 8;
  42. static const int32_t SCROLL_AREA_PADDING = 6;
  43. static const int32_t INDENT_SIZE = 16;
  44. static const int32_t AREA_HEADER = 28;
  45. static const float s_tabStops[4] = {150, 210, 270, 330};
  46. static void* imguiMalloc(size_t size, void* /*_userptr*/)
  47. {
  48. return malloc(size);
  49. }
  50. static void imguiFree(void* _ptr, void* /*_userptr*/)
  51. {
  52. free(_ptr);
  53. }
  54. #define STBTT_malloc(_x, _y) imguiMalloc(_x, _y)
  55. #define STBTT_free(_x, _y) imguiFree(_x, _y)
  56. #define STB_TRUETYPE_IMPLEMENTATION
  57. #include <stb_truetype/stb_truetype.h>
  58. struct PosColorVertex
  59. {
  60. float m_x;
  61. float m_y;
  62. uint32_t m_abgr;
  63. static void init()
  64. {
  65. ms_decl.begin();
  66. ms_decl.add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float);
  67. ms_decl.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true);
  68. ms_decl.end();
  69. }
  70. static bgfx::VertexDecl ms_decl;
  71. };
  72. bgfx::VertexDecl PosColorVertex::ms_decl;
  73. struct PosColorUvVertex
  74. {
  75. float m_x;
  76. float m_y;
  77. float m_u;
  78. float m_v;
  79. uint32_t m_abgr;
  80. static void init()
  81. {
  82. ms_decl.begin();
  83. ms_decl.add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float);
  84. ms_decl.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float);
  85. ms_decl.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true);
  86. ms_decl.end();
  87. }
  88. static bgfx::VertexDecl ms_decl;
  89. };
  90. bgfx::VertexDecl PosColorUvVertex::ms_decl;
  91. struct Imgui
  92. {
  93. Imgui()
  94. : m_mx(-1)
  95. , m_my(-1)
  96. , m_scroll(0)
  97. , m_active(0)
  98. , m_hot(0)
  99. , m_hotToBe(0)
  100. , m_dragX(0)
  101. , m_dragY(0)
  102. , m_dragOrig(0)
  103. , m_widgetX(0)
  104. , m_widgetY(0)
  105. , m_widgetW(100)
  106. , m_left(false)
  107. , m_leftPressed(false)
  108. , m_leftReleased(false)
  109. , m_isHot(false)
  110. , m_isActive(false)
  111. , m_wentActive(false)
  112. , m_insideCurrentScroll(false)
  113. , m_areaId(0)
  114. , m_widgetId(0)
  115. , m_scissor(UINT16_MAX)
  116. , m_scrollTop(0)
  117. , m_scrollBottom(0)
  118. , m_scrollRight(0)
  119. , m_scrollAreaTop(0)
  120. , m_scrollVal(NULL)
  121. , m_focusTop(0)
  122. , m_focusBottom(0)
  123. , m_scrollId(0)
  124. , m_insideScrollArea(false)
  125. , m_textureWidth(512)
  126. , m_textureHeight(512)
  127. , m_halfTexel(0.0f)
  128. , m_view(31)
  129. {
  130. m_invTextureWidth = 1.0f/m_textureWidth;
  131. m_invTextureHeight = 1.0f/m_textureHeight;
  132. u_texColor.idx = bgfx::invalidHandle;
  133. m_fontTexture.idx = bgfx::invalidHandle;
  134. m_colorProgram.idx = bgfx::invalidHandle;
  135. m_textureProgram.idx = bgfx::invalidHandle;
  136. }
  137. bool create(void* _data, uint32_t /*_size*/)
  138. {
  139. for (int32_t ii = 0; ii < NUM_CIRCLE_VERTS; ++ii)
  140. {
  141. float a = (float)ii / (float)NUM_CIRCLE_VERTS * (float)(M_PI * 2.0);
  142. m_circleVerts[ii * 2 + 0] = cosf(a);
  143. m_circleVerts[ii * 2 + 1] = sinf(a);
  144. }
  145. PosColorVertex::init();
  146. PosColorUvVertex::init();
  147. u_texColor = bgfx::createUniform("u_texColor", bgfx::UniformType::Uniform1i);
  148. const bgfx::Memory* vs_imgui_color;
  149. const bgfx::Memory* fs_imgui_color;
  150. const bgfx::Memory* vs_imgui_texture;
  151. const bgfx::Memory* fs_imgui_texture;
  152. switch (bgfx::getRendererType() )
  153. {
  154. case bgfx::RendererType::Direct3D9:
  155. vs_imgui_color = bgfx::makeRef(vs_imgui_color_dx9, sizeof(vs_imgui_color_dx9) );
  156. fs_imgui_color = bgfx::makeRef(fs_imgui_color_dx9, sizeof(fs_imgui_color_dx9) );
  157. vs_imgui_texture = bgfx::makeRef(vs_imgui_texture_dx9, sizeof(vs_imgui_texture_dx9) );
  158. fs_imgui_texture = bgfx::makeRef(fs_imgui_texture_dx9, sizeof(fs_imgui_texture_dx9) );
  159. m_halfTexel = 0.5f;
  160. break;
  161. case bgfx::RendererType::Direct3D11:
  162. vs_imgui_color = bgfx::makeRef(vs_imgui_color_dx11, sizeof(vs_imgui_color_dx11) );
  163. fs_imgui_color = bgfx::makeRef(fs_imgui_color_dx11, sizeof(fs_imgui_color_dx11) );
  164. vs_imgui_texture = bgfx::makeRef(vs_imgui_texture_dx11, sizeof(vs_imgui_texture_dx11) );
  165. fs_imgui_texture = bgfx::makeRef(fs_imgui_texture_dx11, sizeof(fs_imgui_texture_dx11) );
  166. break;
  167. default:
  168. vs_imgui_color = bgfx::makeRef(vs_imgui_color_glsl, sizeof(vs_imgui_color_glsl) );
  169. fs_imgui_color = bgfx::makeRef(fs_imgui_color_glsl, sizeof(fs_imgui_color_glsl) );
  170. vs_imgui_texture = bgfx::makeRef(vs_imgui_texture_glsl, sizeof(vs_imgui_texture_glsl) );
  171. fs_imgui_texture = bgfx::makeRef(fs_imgui_texture_glsl, sizeof(fs_imgui_texture_glsl) );
  172. break;
  173. }
  174. bgfx::VertexShaderHandle vsh;
  175. bgfx::FragmentShaderHandle fsh;
  176. vsh = bgfx::createVertexShader(vs_imgui_color);
  177. fsh = bgfx::createFragmentShader(fs_imgui_color);
  178. m_colorProgram = bgfx::createProgram(vsh, fsh);
  179. bgfx::destroyVertexShader(vsh);
  180. bgfx::destroyFragmentShader(fsh);
  181. vsh = bgfx::createVertexShader(vs_imgui_texture);
  182. fsh = bgfx::createFragmentShader(fs_imgui_texture);
  183. m_textureProgram = bgfx::createProgram(vsh, fsh);
  184. bgfx::destroyVertexShader(vsh);
  185. bgfx::destroyFragmentShader(fsh);
  186. const bgfx::Memory* mem = bgfx::alloc(m_textureWidth * m_textureHeight);
  187. stbtt_BakeFontBitmap( (uint8_t*)_data, 0, 15.0f, mem->data, m_textureWidth, m_textureHeight, 32, 96, m_cdata);
  188. m_fontTexture = bgfx::createTexture2D(m_textureWidth, m_textureHeight, 1, bgfx::TextureFormat::L8, BGFX_TEXTURE_NONE, mem);
  189. return true;
  190. }
  191. void destroy()
  192. {
  193. bgfx::destroyUniform(u_texColor);
  194. bgfx::destroyTexture(m_fontTexture);
  195. bgfx::destroyProgram(m_colorProgram);
  196. bgfx::destroyProgram(m_textureProgram);
  197. }
  198. bool anyActive() const
  199. {
  200. return m_active != 0;
  201. }
  202. bool isActive(uint32_t _id) const
  203. {
  204. return m_active == _id;
  205. }
  206. bool isHot(uint32_t _id) const
  207. {
  208. return m_hot == _id;
  209. }
  210. bool inRect(int32_t _x, int32_t _y, int32_t _width, int32_t _height, bool _checkScroll = true) const
  211. {
  212. return (!_checkScroll || m_insideCurrentScroll)
  213. && m_mx >= _x
  214. && m_mx <= _x + _width
  215. && m_my >= _y
  216. && m_my <= _y + _height;
  217. }
  218. void clearInput()
  219. {
  220. m_leftPressed = false;
  221. m_leftReleased = false;
  222. m_scroll = 0;
  223. }
  224. void clearActive()
  225. {
  226. m_active = 0;
  227. // mark all UI for this frame as processed
  228. clearInput();
  229. }
  230. void setActive(uint32_t _id)
  231. {
  232. m_active = _id;
  233. m_wentActive = true;
  234. }
  235. void setHot(uint32_t _id)
  236. {
  237. m_hotToBe = _id;
  238. }
  239. bool buttonLogic(uint32_t _id, bool _over)
  240. {
  241. bool res = false;
  242. // process down
  243. if (!anyActive() )
  244. {
  245. if (_over)
  246. {
  247. setHot(_id);
  248. }
  249. if (isHot(_id)
  250. && m_leftPressed)
  251. {
  252. setActive(_id);
  253. }
  254. }
  255. // if button is active, then react on left up
  256. if (isActive(_id) )
  257. {
  258. m_isActive = true;
  259. if (_over)
  260. {
  261. setHot(_id);
  262. }
  263. if (m_leftReleased)
  264. {
  265. if (isHot(_id) )
  266. {
  267. res = true;
  268. }
  269. clearActive();
  270. }
  271. }
  272. if (isHot(_id) )
  273. {
  274. m_isHot = true;
  275. }
  276. return res;
  277. }
  278. void updateInput(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll)
  279. {
  280. bool left = (_button & IMGUI_MBUT_LEFT) != 0;
  281. m_mx = _mx;
  282. m_my = _my;
  283. m_leftPressed = !m_left && left;
  284. m_leftReleased = m_left && !left;
  285. m_left = left;
  286. m_scroll = _scroll;
  287. }
  288. void beginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, uint16_t _width, uint16_t _height, uint8_t _view)
  289. {
  290. m_view = _view;
  291. bgfx::setViewSeq(_view, true);
  292. bgfx::setViewRect(_view, 0, 0, _width, _height);
  293. float proj[16];
  294. mtxOrtho(proj, 0.0f, (float)_width, (float)_height, 0.0f, 0.0f, 1000.0f);
  295. bgfx::setViewTransform(_view, NULL, proj);
  296. updateInput(_mx, _my, _button, _scroll);
  297. m_hot = m_hotToBe;
  298. m_hotToBe = 0;
  299. m_wentActive = false;
  300. m_isActive = false;
  301. m_isHot = false;
  302. m_widgetX = 0;
  303. m_widgetY = 0;
  304. m_widgetW = 0;
  305. m_areaId = 1;
  306. m_widgetId = 1;
  307. }
  308. void endFrame()
  309. {
  310. clearInput();
  311. }
  312. bool beginScrollArea(const char* _name, int32_t _x, int32_t _y, int32_t _width, int32_t _height, int32_t* _scroll)
  313. {
  314. m_areaId++;
  315. m_widgetId = 0;
  316. m_scrollId = (m_areaId << 16) | m_widgetId;
  317. m_widgetX = _x + SCROLL_AREA_PADDING;
  318. m_widgetY = _y + + AREA_HEADER + (*_scroll);
  319. m_widgetW = _width - SCROLL_AREA_PADDING * 4;
  320. m_scrollTop = _y + SCROLL_AREA_PADDING;
  321. m_scrollBottom = _y - AREA_HEADER + _height;
  322. m_scrollRight = _x + _width - SCROLL_AREA_PADDING * 3;
  323. m_scrollVal = _scroll;
  324. m_scrollAreaTop = m_widgetY;
  325. m_focusTop = _y - AREA_HEADER;
  326. m_focusBottom = _y - AREA_HEADER + _height;
  327. m_insideScrollArea = inRect(_x, _y, _width, _height, false);
  328. m_insideCurrentScroll = m_insideScrollArea;
  329. drawRoundedRect( (float)_x
  330. , (float)_y
  331. , (float)_width
  332. , (float)_height
  333. , 6
  334. , imguiRGBA(0, 0, 0, 192)
  335. );
  336. drawText(_x + AREA_HEADER / 2
  337. , _y + AREA_HEADER / 2
  338. , ImguiTextAlign::Left
  339. , _name
  340. , imguiRGBA(255, 255, 255, 128)
  341. );
  342. m_scissor = bgfx::setScissor(_x + SCROLL_AREA_PADDING, _y + SCROLL_AREA_PADDING, _width - SCROLL_AREA_PADDING * 4, _height - AREA_HEADER - SCROLL_AREA_PADDING);
  343. return m_insideScrollArea;
  344. }
  345. void endScrollArea()
  346. {
  347. // Disable scissoring.
  348. m_scissor = UINT16_MAX;
  349. // Draw scroll bar
  350. int32_t xx = m_scrollRight + SCROLL_AREA_PADDING / 2;
  351. int32_t yy = m_scrollTop;
  352. int32_t width = SCROLL_AREA_PADDING * 2;
  353. int32_t height = m_scrollBottom - m_scrollTop;
  354. int32_t stop = m_scrollAreaTop;
  355. int32_t sbot = m_widgetY;
  356. int32_t sh = sbot - stop; // The scrollable area height.
  357. float barHeight = (float)height / (float)sh;
  358. if (barHeight < 1)
  359. {
  360. float barY = (float)(yy - sbot) / (float)sh;
  361. if (barY < 0)
  362. {
  363. barY = 0;
  364. }
  365. if (barY > 1)
  366. {
  367. barY = 1;
  368. }
  369. // Handle scroll bar logic.
  370. uint32_t hid = m_scrollId;
  371. int32_t hx = xx;
  372. int32_t hy = yy + (int)(barY * height);
  373. int32_t hw = width;
  374. int32_t hh = (int)(barHeight * height);
  375. const int32_t range = height - (hh - 1);
  376. bool over = inRect(hx, hy, hw, hh);
  377. buttonLogic(hid, over);
  378. if (isActive(hid) )
  379. {
  380. float u = (float)(hy - yy) / (float)range;
  381. if (m_wentActive)
  382. {
  383. m_dragY = m_my;
  384. m_dragOrig = u;
  385. }
  386. if (m_dragY != m_my)
  387. {
  388. u = m_dragOrig + (m_my - m_dragY) / (float)range;
  389. if (u < 0)
  390. {
  391. u = 0;
  392. }
  393. if (u > 1)
  394. {
  395. u = 1;
  396. }
  397. *m_scrollVal = (int)( (1 - u) * (sh - height) );
  398. }
  399. }
  400. // BG
  401. drawRoundedRect( (float)xx
  402. , (float)yy
  403. , (float)width
  404. , (float)height
  405. , (float)width / 2 - 1
  406. , imguiRGBA(0, 0, 0, 196)
  407. );
  408. // Bar
  409. if (isActive(hid) )
  410. {
  411. drawRoundedRect( (float)hx
  412. , (float)hy
  413. , (float)hw
  414. , (float)hh
  415. , (float)width / 2 - 1
  416. , imguiRGBA(255, 196, 0, 196)
  417. );
  418. }
  419. else
  420. {
  421. drawRoundedRect( (float)hx
  422. , (float)hy
  423. , (float)hw
  424. , (float)hh
  425. , (float)width / 2 - 1
  426. , isHot(hid) ? imguiRGBA(255, 196, 0, 96) : imguiRGBA(255, 255, 255, 64)
  427. );
  428. }
  429. // Handle mouse scrolling.
  430. if (m_insideScrollArea) // && !anyActive())
  431. {
  432. if (m_scroll)
  433. {
  434. *m_scrollVal += 20 * m_scroll;
  435. if (*m_scrollVal < 0)
  436. {
  437. *m_scrollVal = 0;
  438. }
  439. if (*m_scrollVal > (sh - height) )
  440. {
  441. *m_scrollVal = (sh - height);
  442. }
  443. }
  444. }
  445. }
  446. m_insideCurrentScroll = false;
  447. }
  448. bool button(const char* _text, bool _enabled)
  449. {
  450. m_widgetId++;
  451. uint32_t id = (m_areaId << 16) | m_widgetId;
  452. int32_t xx = m_widgetX;
  453. int32_t yy = m_widgetY;
  454. int32_t width = m_widgetW;
  455. int32_t height = BUTTON_HEIGHT;
  456. m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  457. bool over = _enabled && inRect(xx, yy, width, height);
  458. bool res = buttonLogic(id, over);
  459. drawRoundedRect( (float)xx
  460. , (float)yy
  461. , (float)width
  462. , (float)height
  463. , (float)BUTTON_HEIGHT / 2 - 1
  464. , imguiRGBA(128, 128, 128, isActive(id) ? 196 : 96)
  465. );
  466. if (_enabled)
  467. {
  468. drawText(xx + BUTTON_HEIGHT / 2
  469. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  470. , ImguiTextAlign::Left
  471. , _text
  472. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  473. );
  474. }
  475. else
  476. {
  477. drawText(xx + BUTTON_HEIGHT / 2
  478. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  479. , ImguiTextAlign::Left
  480. , _text
  481. , imguiRGBA(128, 128, 128, 200)
  482. );
  483. }
  484. return res;
  485. }
  486. bool item(const char* _text, bool _enabled)
  487. {
  488. m_widgetId++;
  489. uint32_t id = (m_areaId << 16) | m_widgetId;
  490. int32_t xx = m_widgetX;
  491. int32_t yy = m_widgetY;
  492. int32_t width = m_widgetW;
  493. int32_t height = BUTTON_HEIGHT;
  494. m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  495. bool over = _enabled && inRect(xx, yy, width, height);
  496. bool res = buttonLogic(id, over);
  497. if (isHot(id) )
  498. {
  499. drawRoundedRect( (float)xx
  500. , (float)yy
  501. , (float)width
  502. , (float)height
  503. , 2.0f
  504. , imguiRGBA(255, 196, 0, isActive(id) ? 196 : 96)
  505. );
  506. }
  507. if (_enabled)
  508. {
  509. drawText(xx + BUTTON_HEIGHT / 2
  510. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  511. , ImguiTextAlign::Left
  512. , _text
  513. , imguiRGBA(255, 255, 255, 200)
  514. );
  515. }
  516. else
  517. {
  518. drawText(xx + BUTTON_HEIGHT / 2
  519. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  520. , ImguiTextAlign::Left
  521. , _text
  522. , imguiRGBA(128, 128, 128, 200)
  523. );
  524. }
  525. return res;
  526. }
  527. bool check(const char* _text, bool _checked, bool _enabled)
  528. {
  529. m_widgetId++;
  530. uint32_t id = (m_areaId << 16) | m_widgetId;
  531. int32_t xx = m_widgetX;
  532. int32_t yy = m_widgetY;
  533. int32_t width = m_widgetW;
  534. int32_t height = BUTTON_HEIGHT;
  535. m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  536. bool over = _enabled && inRect(xx, yy, width, height);
  537. bool res = buttonLogic(id, over);
  538. const int32_t cx = xx + BUTTON_HEIGHT / 2 - CHECK_SIZE / 2;
  539. const int32_t cy = yy + BUTTON_HEIGHT / 2 - CHECK_SIZE / 2;
  540. drawRoundedRect( (float)cx - 3
  541. , (float)cy - 3
  542. , (float)CHECK_SIZE + 6
  543. , (float)CHECK_SIZE + 6
  544. , 4
  545. , imguiRGBA(128, 128, 128, isActive(id) ? 196 : 96)
  546. );
  547. if (_checked)
  548. {
  549. if (_enabled)
  550. {
  551. drawRoundedRect( (float)cx
  552. , (float)cy
  553. , (float)CHECK_SIZE
  554. , (float)CHECK_SIZE
  555. , (float)CHECK_SIZE / 2 - 1
  556. , imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200)
  557. );
  558. }
  559. else
  560. {
  561. drawRoundedRect( (float)cx
  562. , (float)cy
  563. , (float)CHECK_SIZE
  564. , (float)CHECK_SIZE
  565. , (float)CHECK_SIZE / 2 - 1
  566. , imguiRGBA(128, 128, 128, 200)
  567. );
  568. }
  569. }
  570. if (_enabled)
  571. {
  572. drawText(xx + BUTTON_HEIGHT
  573. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  574. , ImguiTextAlign::Left
  575. , _text
  576. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  577. );
  578. }
  579. else
  580. {
  581. drawText(xx + BUTTON_HEIGHT
  582. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  583. , ImguiTextAlign::Left
  584. , _text
  585. , imguiRGBA(128, 128, 128, 200)
  586. );
  587. }
  588. return res;
  589. }
  590. bool collapse(const char* _text, const char* _subtext, bool _checked, bool _enabled)
  591. {
  592. m_widgetId++;
  593. uint32_t id = (m_areaId << 16) | m_widgetId;
  594. int32_t xx = m_widgetX;
  595. int32_t yy = m_widgetY;
  596. int32_t width = m_widgetW;
  597. int32_t height = BUTTON_HEIGHT;
  598. m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  599. const int32_t cx = xx + BUTTON_HEIGHT / 2 - CHECK_SIZE / 2;
  600. const int32_t cy = yy + BUTTON_HEIGHT / 2 - CHECK_SIZE / 2;
  601. bool over = _enabled && inRect(xx, yy, width, height);
  602. bool res = buttonLogic(id, over);
  603. if (_checked)
  604. {
  605. drawTriangle(cx
  606. , cy
  607. , CHECK_SIZE
  608. , CHECK_SIZE
  609. , 2
  610. , imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200)
  611. );
  612. }
  613. else
  614. {
  615. drawTriangle(cx
  616. , cy
  617. , CHECK_SIZE
  618. , CHECK_SIZE
  619. , 1
  620. , imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200)
  621. );
  622. }
  623. if (_enabled)
  624. {
  625. drawText(xx + BUTTON_HEIGHT
  626. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  627. , ImguiTextAlign::Left
  628. , _text
  629. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  630. );
  631. }
  632. else
  633. {
  634. drawText(xx + BUTTON_HEIGHT
  635. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  636. , ImguiTextAlign::Left
  637. , _text
  638. , imguiRGBA(128, 128, 128, 200)
  639. );
  640. }
  641. if (_subtext)
  642. {
  643. drawText(xx + width - BUTTON_HEIGHT / 2
  644. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  645. , ImguiTextAlign::Right
  646. , _subtext
  647. , imguiRGBA(255, 255, 255, 128)
  648. );
  649. }
  650. return res;
  651. }
  652. void labelVargs(const char* _format, va_list _argList)
  653. {
  654. char temp[8192];
  655. char* out = temp;
  656. int32_t len = bx::vsnprintf(out, sizeof(temp), _format, _argList);
  657. if ( (int32_t)sizeof(temp) < len)
  658. {
  659. out = (char*)alloca(len+1);
  660. len = bx::vsnprintf(out, len, _format, _argList);
  661. }
  662. out[len] = '\0';
  663. int32_t xx = m_widgetX;
  664. int32_t yy = m_widgetY;
  665. m_widgetY += BUTTON_HEIGHT;
  666. drawText(xx
  667. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  668. , ImguiTextAlign::Left
  669. , out
  670. , imguiRGBA(255, 255, 255, 255)
  671. );
  672. }
  673. void value(const char* _text)
  674. {
  675. const int32_t xx = m_widgetX;
  676. const int32_t yy = m_widgetY;
  677. const int32_t ww = m_widgetW;
  678. m_widgetY += BUTTON_HEIGHT;
  679. drawText(xx + ww - BUTTON_HEIGHT / 2
  680. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  681. , ImguiTextAlign::Right
  682. , _text
  683. , imguiRGBA(255, 255, 255, 200)
  684. );
  685. }
  686. bool slider(const char* _text, float* _val, float _vmin, float _vmax, float _vinc, bool _enabled)
  687. {
  688. m_widgetId++;
  689. uint32_t id = (m_areaId << 16) | m_widgetId;
  690. int32_t xx = m_widgetX;
  691. int32_t yy = m_widgetY;
  692. int32_t width = m_widgetW;
  693. int32_t height = SLIDER_HEIGHT;
  694. m_widgetY += SLIDER_HEIGHT + DEFAULT_SPACING;
  695. drawRoundedRect( (float)xx, (float)yy, (float)width, (float)height, 4.0f, imguiRGBA(0, 0, 0, 128) );
  696. const int32_t range = width - SLIDER_MARKER_WIDTH;
  697. float uu = (*_val - _vmin) / (_vmax - _vmin);
  698. if (uu < 0)
  699. {
  700. uu = 0;
  701. }
  702. if (uu > 1)
  703. {
  704. uu = 1;
  705. }
  706. int32_t m = (int)(uu * range);
  707. bool over = _enabled && inRect(xx + m, yy, SLIDER_MARKER_WIDTH, SLIDER_HEIGHT);
  708. bool res = buttonLogic(id, over);
  709. bool valChanged = false;
  710. if (isActive(id) )
  711. {
  712. if (m_wentActive)
  713. {
  714. m_dragX = m_mx;
  715. m_dragOrig = uu;
  716. }
  717. if (m_dragX != m_mx)
  718. {
  719. uu = m_dragOrig + (float)(m_mx - m_dragX) / (float)range;
  720. if (uu < 0)
  721. {
  722. uu = 0;
  723. }
  724. if (uu > 1)
  725. {
  726. uu = 1;
  727. }
  728. *_val = _vmin + uu * (_vmax - _vmin);
  729. *_val = floorf(*_val / _vinc + 0.5f) * _vinc; // Snap to vinc
  730. m = (int)(uu * range);
  731. valChanged = true;
  732. }
  733. }
  734. if (isActive(id) )
  735. {
  736. drawRoundedRect( (float)(xx + m)
  737. , (float)yy
  738. , (float)SLIDER_MARKER_WIDTH
  739. , (float)SLIDER_HEIGHT
  740. , 4.0f
  741. , imguiRGBA(255, 255, 255, 255)
  742. );
  743. }
  744. else
  745. {
  746. drawRoundedRect( (float)(xx + m)
  747. , (float)yy
  748. , (float)SLIDER_MARKER_WIDTH
  749. , (float)SLIDER_HEIGHT
  750. , 4.0f
  751. , isHot(id) ? imguiRGBA(255, 196, 0, 128) : imguiRGBA(255, 255, 255, 64)
  752. );
  753. }
  754. // TODO: fix this, take a look at 'nicenum'.
  755. int32_t digits = (int)(ceilf(log10f(_vinc) ) );
  756. char fmt[16];
  757. bx::snprintf(fmt, 16, "%%.%df", digits >= 0 ? 0 : -digits);
  758. char msg[128];
  759. bx::snprintf(msg, 128, fmt, *_val);
  760. if (_enabled)
  761. {
  762. drawText(xx + SLIDER_HEIGHT / 2
  763. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  764. , ImguiTextAlign::Left
  765. , _text
  766. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  767. );
  768. drawText(xx + width - SLIDER_HEIGHT / 2
  769. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  770. , ImguiTextAlign::Right
  771. , msg
  772. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  773. );
  774. }
  775. else
  776. {
  777. drawText(xx + SLIDER_HEIGHT / 2
  778. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  779. , ImguiTextAlign::Left
  780. , _text
  781. , imguiRGBA(128, 128, 128, 200)
  782. );
  783. drawText(xx + width - SLIDER_HEIGHT / 2
  784. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  785. , ImguiTextAlign::Right
  786. , msg
  787. , imguiRGBA(128, 128, 128, 200)
  788. );
  789. }
  790. return res || valChanged;
  791. }
  792. void indent()
  793. {
  794. m_widgetX += INDENT_SIZE;
  795. m_widgetW -= INDENT_SIZE;
  796. }
  797. void unindent()
  798. {
  799. m_widgetX -= INDENT_SIZE;
  800. m_widgetW += INDENT_SIZE;
  801. }
  802. void separator()
  803. {
  804. m_widgetY += DEFAULT_SPACING * 3;
  805. }
  806. void separatorLine()
  807. {
  808. int32_t xx = m_widgetX;
  809. int32_t yy = m_widgetY;
  810. int32_t width = m_widgetW;
  811. int32_t height = 1;
  812. m_widgetY += DEFAULT_SPACING * 4;
  813. drawRect( (float)xx
  814. , (float)yy
  815. , (float)width
  816. , (float)height
  817. , imguiRGBA(255, 255, 255, 32)
  818. );
  819. }
  820. void drawPolygon(const float* _coords, uint32_t _numCoords, float _r, uint32_t _abgr)
  821. {
  822. _numCoords = bx::uint32_min(_numCoords, MAX_TEMP_COORDS);
  823. for (uint32_t ii = 0, jj = _numCoords - 1; ii < _numCoords; jj = ii++)
  824. {
  825. const float* v0 = &_coords[jj * 2];
  826. const float* v1 = &_coords[ii * 2];
  827. float dx = v1[0] - v0[0];
  828. float dy = v1[1] - v0[1];
  829. float d = sqrtf(dx * dx + dy * dy);
  830. if (d > 0)
  831. {
  832. d = 1.0f / d;
  833. dx *= d;
  834. dy *= d;
  835. }
  836. m_tempNormals[jj * 2 + 0] = dy;
  837. m_tempNormals[jj * 2 + 1] = -dx;
  838. }
  839. for (uint32_t ii = 0, jj = _numCoords - 1; ii < _numCoords; jj = ii++)
  840. {
  841. float dlx0 = m_tempNormals[jj * 2 + 0];
  842. float dly0 = m_tempNormals[jj * 2 + 1];
  843. float dlx1 = m_tempNormals[ii * 2 + 0];
  844. float dly1 = m_tempNormals[ii * 2 + 1];
  845. float dmx = (dlx0 + dlx1) * 0.5f;
  846. float dmy = (dly0 + dly1) * 0.5f;
  847. float dmr2 = dmx * dmx + dmy * dmy;
  848. if (dmr2 > 0.000001f)
  849. {
  850. float scale = 1.0f / dmr2;
  851. if (scale > 10.0f)
  852. {
  853. scale = 10.0f;
  854. }
  855. dmx *= scale;
  856. dmy *= scale;
  857. }
  858. m_tempCoords[ii * 2 + 0] = _coords[ii * 2 + 0] + dmx * _r;
  859. m_tempCoords[ii * 2 + 1] = _coords[ii * 2 + 1] + dmy * _r;
  860. }
  861. uint32_t numVertices = _numCoords*6 + (_numCoords-2)*3;
  862. if (bgfx::checkAvailTransientVertexBuffer(numVertices, PosColorVertex::ms_decl) )
  863. {
  864. bgfx::TransientVertexBuffer tvb;
  865. bgfx::allocTransientVertexBuffer(&tvb, numVertices, PosColorVertex::ms_decl);
  866. uint32_t trans = _abgr&0xffffff;
  867. PosColorVertex* vertex = (PosColorVertex*)tvb.data;
  868. for (uint32_t ii = 0, jj = _numCoords-1; ii < _numCoords; jj = ii++)
  869. {
  870. vertex->m_x = _coords[ii*2+0];
  871. vertex->m_y = _coords[ii*2+1];
  872. vertex->m_abgr = _abgr;
  873. ++vertex;
  874. vertex->m_x = _coords[jj*2+0];
  875. vertex->m_y = _coords[jj*2+1];
  876. vertex->m_abgr = _abgr;
  877. ++vertex;
  878. vertex->m_x = m_tempCoords[jj*2+0];
  879. vertex->m_y = m_tempCoords[jj*2+1];
  880. vertex->m_abgr = trans;
  881. ++vertex;
  882. vertex->m_x = m_tempCoords[jj*2+0];
  883. vertex->m_y = m_tempCoords[jj*2+1];
  884. vertex->m_abgr = trans;
  885. ++vertex;
  886. vertex->m_x = m_tempCoords[ii*2+0];
  887. vertex->m_y = m_tempCoords[ii*2+1];
  888. vertex->m_abgr = trans;
  889. ++vertex;
  890. vertex->m_x = _coords[ii*2+0];
  891. vertex->m_y = _coords[ii*2+1];
  892. vertex->m_abgr = _abgr;
  893. ++vertex;
  894. }
  895. for (uint32_t ii = 2; ii < _numCoords; ++ii)
  896. {
  897. vertex->m_x = _coords[0];
  898. vertex->m_y = _coords[1];
  899. vertex->m_abgr = _abgr;
  900. ++vertex;
  901. vertex->m_x = _coords[(ii-1)*2+0];
  902. vertex->m_y = _coords[(ii-1)*2+1];
  903. vertex->m_abgr = _abgr;
  904. ++vertex;
  905. vertex->m_x = _coords[ii*2+0];
  906. vertex->m_y = _coords[ii*2+1];
  907. vertex->m_abgr = _abgr;
  908. ++vertex;
  909. }
  910. bgfx::setVertexBuffer(&tvb);
  911. bgfx::setState(0
  912. | BGFX_STATE_RGB_WRITE
  913. | BGFX_STATE_ALPHA_WRITE
  914. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  915. );
  916. bgfx::setProgram(m_colorProgram);
  917. bgfx::setScissor(m_scissor);
  918. bgfx::submit(m_view);
  919. }
  920. }
  921. void drawRect(float _x, float _y, float w, float h, uint32_t _argb, float _fth = 1.0f)
  922. {
  923. float verts[4 * 2] =
  924. {
  925. _x + 0.5f, _y + 0.5f,
  926. _x + w - 0.5f, _y + 0.5f,
  927. _x + w - 0.5f, _y + h - 0.5f,
  928. _x + 0.5f, _y + h - 0.5f,
  929. };
  930. drawPolygon(verts, 4, _fth, _argb);
  931. }
  932. void drawRoundedRect(float _x, float _y, float w, float h, float r, uint32_t _argb, float _fth = 1.0f)
  933. {
  934. const uint32_t num = NUM_CIRCLE_VERTS / 4;
  935. const float* cverts = m_circleVerts;
  936. float verts[(num + 1) * 4 * 2];
  937. float* vv = verts;
  938. for (uint32_t ii = 0; ii <= num; ++ii)
  939. {
  940. *vv++ = _x + w - r + cverts[ii * 2] * r;
  941. *vv++ = _y + h - r + cverts[ii * 2 + 1] * r;
  942. }
  943. for (uint32_t ii = num; ii <= num * 2; ++ii)
  944. {
  945. *vv++ = _x + r + cverts[ii * 2] * r;
  946. *vv++ = _y + h - r + cverts[ii * 2 + 1] * r;
  947. }
  948. for (uint32_t ii = num * 2; ii <= num * 3; ++ii)
  949. {
  950. *vv++ = _x + r + cverts[ii * 2] * r;
  951. *vv++ = _y + r + cverts[ii * 2 + 1] * r;
  952. }
  953. for (uint32_t ii = num * 3; ii < num * 4; ++ii)
  954. {
  955. *vv++ = _x + w - r + cverts[ii * 2] * r;
  956. *vv++ = _y + r + cverts[ii * 2 + 1] * r;
  957. }
  958. *vv++ = _x + w - r + cverts[0] * r;
  959. *vv++ = _y + r + cverts[1] * r;
  960. drawPolygon(verts, (num + 1) * 4, _fth, _argb);
  961. }
  962. void drawLine(float _x0, float _y0, float _x1, float _y1, float _r, uint32_t _abgr, float _fth = 1.0f)
  963. {
  964. float dx = _x1 - _x0;
  965. float dy = _y1 - _y0;
  966. float d = sqrtf(dx * dx + dy * dy);
  967. if (d > 0.0001f)
  968. {
  969. d = 1.0f / d;
  970. dx *= d;
  971. dy *= d;
  972. }
  973. float nx = dy;
  974. float ny = -dx;
  975. float verts[4 * 2];
  976. _r -= _fth;
  977. _r *= 0.5f;
  978. if (_r < 0.01f)
  979. {
  980. _r = 0.01f;
  981. }
  982. dx *= _r;
  983. dy *= _r;
  984. nx *= _r;
  985. ny *= _r;
  986. verts[0] = _x0 - dx - nx;
  987. verts[1] = _y0 - dy - ny;
  988. verts[2] = _x0 - dx + nx;
  989. verts[3] = _y0 - dy + ny;
  990. verts[4] = _x1 + dx + nx;
  991. verts[5] = _y1 + dy + ny;
  992. verts[6] = _x1 + dx - nx;
  993. verts[7] = _y1 + dy - ny;
  994. drawPolygon(verts, 4, _fth, _abgr);
  995. }
  996. void drawTriangle(int32_t _x, int32_t _y, int32_t _width, int32_t _height, int32_t _flags, uint32_t _abgr)
  997. {
  998. if (1 == _flags)
  999. {
  1000. const float verts[3 * 2] =
  1001. {
  1002. (float)_x + 0.5f, (float)_y + 0.5f,
  1003. (float)_x + 0.5f + (float)_width * 1.0f, (float)_y + 0.5f + (float)_height / 2.0f - 0.5f,
  1004. (float)_x + 0.5f, (float)_y + 0.5f + (float)_height - 1.0f,
  1005. };
  1006. drawPolygon(verts, 3, 1.0f, _abgr);
  1007. }
  1008. else
  1009. {
  1010. const float verts[3 * 2] =
  1011. {
  1012. (float)_x + 0.5f, (float)_y + 0.5f + (float)_height - 1.0f,
  1013. (float)_x + 0.5f + (float)_width / 2.0f - 0.5f, (float)_y + 0.5f,
  1014. (float)_x + 0.5f + (float)_width - 1.0f, (float)_y + 0.5f + (float)_height - 1.0f,
  1015. };
  1016. drawPolygon(verts, 3, 1.0f, _abgr);
  1017. }
  1018. }
  1019. void getBakedQuad(stbtt_bakedchar* _chardata, int32_t char_index, float* _xpos, float* _ypos, stbtt_aligned_quad* _quad)
  1020. {
  1021. stbtt_bakedchar* b = _chardata + char_index;
  1022. int32_t round_x = STBTT_ifloor(*_xpos + b->xoff);
  1023. int32_t round_y = STBTT_ifloor(*_ypos + b->yoff);
  1024. _quad->x0 = (float)round_x;
  1025. _quad->y0 = (float)round_y;
  1026. _quad->x1 = (float)round_x + b->x1 - b->x0;
  1027. _quad->y1 = (float)round_y + b->y1 - b->y0;
  1028. _quad->s0 = (b->x0 + m_halfTexel) * m_invTextureWidth;
  1029. _quad->t0 = (b->y0 + m_halfTexel) * m_invTextureWidth;
  1030. _quad->s1 = (b->x1 + m_halfTexel) * m_invTextureHeight;
  1031. _quad->t1 = (b->y1 + m_halfTexel) * m_invTextureHeight;
  1032. *_xpos += b->xadvance;
  1033. }
  1034. float getTextLength(stbtt_bakedchar* _chardata, const char* _text, uint32_t& _numVertices)
  1035. {
  1036. float xpos = 0;
  1037. float len = 0;
  1038. uint32_t numVertices = 0;
  1039. while (*_text)
  1040. {
  1041. int32_t ch = (uint8_t)*_text;
  1042. if (ch == '\t')
  1043. {
  1044. for (int32_t ii = 0; ii < 4; ++ii)
  1045. {
  1046. if (xpos < s_tabStops[ii])
  1047. {
  1048. xpos = s_tabStops[ii];
  1049. break;
  1050. }
  1051. }
  1052. }
  1053. else if (ch >= ' '
  1054. && ch < 128)
  1055. {
  1056. stbtt_bakedchar* b = _chardata + ch - ' ';
  1057. int32_t round_x = STBTT_ifloor( (xpos + b->xoff) + 0.5);
  1058. len = round_x + b->x1 - b->x0 + 0.5f;
  1059. xpos += b->xadvance;
  1060. numVertices += 6;
  1061. }
  1062. ++_text;
  1063. }
  1064. _numVertices = numVertices;
  1065. return len;
  1066. }
  1067. void drawText(int32_t _x, int32_t _y, ImguiTextAlign::Enum _align, const char* _text, uint32_t _abgr)
  1068. {
  1069. drawText( (float)_x, (float)_y, _text, _align, _abgr);
  1070. }
  1071. void drawText(float _x, float _y, const char* _text, ImguiTextAlign::Enum _align, uint32_t _abgr)
  1072. {
  1073. if (!_text)
  1074. {
  1075. return;
  1076. }
  1077. uint32_t numVertices = 0;
  1078. if (_align == ImguiTextAlign::Center)
  1079. {
  1080. _x -= getTextLength(m_cdata, _text, numVertices) / 2;
  1081. }
  1082. else if (_align == ImguiTextAlign::Right)
  1083. {
  1084. _x -= getTextLength(m_cdata, _text, numVertices);
  1085. }
  1086. else // just count vertices
  1087. {
  1088. getTextLength(m_cdata, _text, numVertices);
  1089. }
  1090. if (bgfx::checkAvailTransientVertexBuffer(numVertices, PosColorUvVertex::ms_decl) )
  1091. {
  1092. bgfx::TransientVertexBuffer tvb;
  1093. bgfx::allocTransientVertexBuffer(&tvb, numVertices, PosColorUvVertex::ms_decl);
  1094. PosColorUvVertex* vertex = (PosColorUvVertex*)tvb.data;
  1095. const float ox = _x;
  1096. while (*_text)
  1097. {
  1098. int32_t ch = (uint8_t)*_text;
  1099. if (ch == '\t')
  1100. {
  1101. for (int32_t i = 0; i < 4; ++i)
  1102. {
  1103. if (_x < s_tabStops[i] + ox)
  1104. {
  1105. _x = s_tabStops[i] + ox;
  1106. break;
  1107. }
  1108. }
  1109. }
  1110. else if (ch >= ' '
  1111. && ch < 128)
  1112. {
  1113. stbtt_aligned_quad quad;
  1114. getBakedQuad(m_cdata, ch - 32, &_x, &_y, &quad);
  1115. vertex->m_x = quad.x0;
  1116. vertex->m_y = quad.y0;
  1117. vertex->m_u = quad.s0;
  1118. vertex->m_v = quad.t0;
  1119. vertex->m_abgr = _abgr;
  1120. ++vertex;
  1121. vertex->m_x = quad.x1;
  1122. vertex->m_y = quad.y1;
  1123. vertex->m_u = quad.s1;
  1124. vertex->m_v = quad.t1;
  1125. vertex->m_abgr = _abgr;
  1126. ++vertex;
  1127. vertex->m_x = quad.x1;
  1128. vertex->m_y = quad.y0;
  1129. vertex->m_u = quad.s1;
  1130. vertex->m_v = quad.t0;
  1131. vertex->m_abgr = _abgr;
  1132. ++vertex;
  1133. vertex->m_x = quad.x0;
  1134. vertex->m_y = quad.y0;
  1135. vertex->m_u = quad.s0;
  1136. vertex->m_v = quad.t0;
  1137. vertex->m_abgr = _abgr;
  1138. ++vertex;
  1139. vertex->m_x = quad.x0;
  1140. vertex->m_y = quad.y1;
  1141. vertex->m_u = quad.s0;
  1142. vertex->m_v = quad.t1;
  1143. vertex->m_abgr = _abgr;
  1144. ++vertex;
  1145. vertex->m_x = quad.x1;
  1146. vertex->m_y = quad.y1;
  1147. vertex->m_u = quad.s1;
  1148. vertex->m_v = quad.t1;
  1149. vertex->m_abgr = _abgr;
  1150. ++vertex;
  1151. }
  1152. ++_text;
  1153. }
  1154. bgfx::setTexture(0, u_texColor, m_fontTexture);
  1155. bgfx::setVertexBuffer(&tvb);
  1156. bgfx::setState(0
  1157. | BGFX_STATE_RGB_WRITE
  1158. | BGFX_STATE_ALPHA_WRITE
  1159. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  1160. );
  1161. bgfx::setProgram(m_textureProgram);
  1162. bgfx::setScissor(m_scissor);
  1163. bgfx::submit(m_view);
  1164. }
  1165. }
  1166. int32_t m_mx;
  1167. int32_t m_my;
  1168. int32_t m_scroll;
  1169. uint32_t m_active;
  1170. uint32_t m_hot;
  1171. uint32_t m_hotToBe;
  1172. int32_t m_dragX;
  1173. int32_t m_dragY;
  1174. float m_dragOrig;
  1175. int32_t m_widgetX;
  1176. int32_t m_widgetY;
  1177. int32_t m_widgetW;
  1178. bool m_left;
  1179. bool m_leftPressed;
  1180. bool m_leftReleased;
  1181. bool m_isHot;
  1182. bool m_isActive;
  1183. bool m_wentActive;
  1184. bool m_insideCurrentScroll;
  1185. uint32_t m_areaId;
  1186. uint32_t m_widgetId;
  1187. uint16_t m_scissor;
  1188. float m_tempCoords[MAX_TEMP_COORDS * 2];
  1189. float m_tempNormals[MAX_TEMP_COORDS * 2];
  1190. float m_circleVerts[NUM_CIRCLE_VERTS * 2];
  1191. int32_t m_scrollTop;
  1192. int32_t m_scrollBottom;
  1193. int32_t m_scrollRight;
  1194. int32_t m_scrollAreaTop;
  1195. int32_t* m_scrollVal;
  1196. int32_t m_focusTop;
  1197. int32_t m_focusBottom;
  1198. uint32_t m_scrollId;
  1199. bool m_insideScrollArea;
  1200. stbtt_bakedchar m_cdata[96]; // ASCII 32..126 is 95 glyphs
  1201. uint16_t m_textureWidth;
  1202. uint16_t m_textureHeight;
  1203. float m_invTextureWidth;
  1204. float m_invTextureHeight;
  1205. float m_halfTexel;
  1206. uint8_t m_view;
  1207. bgfx::UniformHandle u_texColor;
  1208. bgfx::TextureHandle m_fontTexture;
  1209. bgfx::ProgramHandle m_colorProgram;
  1210. bgfx::ProgramHandle m_textureProgram;
  1211. };
  1212. static Imgui s_imgui;
  1213. bool imguiCreate(void* _data, uint32_t _size)
  1214. {
  1215. return s_imgui.create(_data, _size);
  1216. }
  1217. void imguiDestroy()
  1218. {
  1219. s_imgui.destroy();
  1220. }
  1221. void imguiBeginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, uint16_t _width, uint16_t _height, uint8_t _view)
  1222. {
  1223. s_imgui.beginFrame(_mx, _my, _button, _scroll, _width, _height, _view);
  1224. }
  1225. void imguiEndFrame()
  1226. {
  1227. s_imgui.endFrame();
  1228. }
  1229. bool imguiBeginScrollArea(const char* _name, int32_t _x, int32_t _y, int32_t _width, int32_t _height, int32_t* _scroll)
  1230. {
  1231. return s_imgui.beginScrollArea(_name, _x, _y, _width, _height, _scroll);
  1232. }
  1233. void imguiEndScrollArea()
  1234. {
  1235. return s_imgui.endScrollArea();
  1236. }
  1237. void imguiIndent()
  1238. {
  1239. s_imgui.indent();
  1240. }
  1241. void imguiUnindent()
  1242. {
  1243. s_imgui.unindent();
  1244. }
  1245. void imguiSeparator()
  1246. {
  1247. s_imgui.separator();
  1248. }
  1249. void imguiSeparatorLine()
  1250. {
  1251. s_imgui.separatorLine();
  1252. }
  1253. bool imguiButton(const char* _text, bool _enabled)
  1254. {
  1255. return s_imgui.button(_text, _enabled);
  1256. }
  1257. bool imguiItem(const char* _text, bool _enabled)
  1258. {
  1259. return s_imgui.item(_text, _enabled);
  1260. }
  1261. bool imguiCheck(const char* _text, bool _checked, bool _enabled)
  1262. {
  1263. return s_imgui.check(_text, _checked, _enabled);
  1264. }
  1265. bool imguiCollapse(const char* _text, const char* _subtext, bool _checked, bool _enabled)
  1266. {
  1267. return s_imgui.collapse(_text, _subtext, _checked, _enabled);
  1268. }
  1269. void imguiLabel(const char* _format, ...)
  1270. {
  1271. va_list argList;
  1272. va_start(argList, _format);
  1273. s_imgui.labelVargs(_format, argList);
  1274. va_end(argList);
  1275. }
  1276. void imguiValue(const char* _text)
  1277. {
  1278. s_imgui.value(_text);
  1279. }
  1280. bool imguiSlider(const char* _text, float* _val, float _vmin, float _vmax, float _vinc, bool _enabled)
  1281. {
  1282. return s_imgui.slider(_text, _val, _vmin, _vmax, _vinc, _enabled);
  1283. }
  1284. bool imguiSlider(const char* _text, int32_t* _val, int32_t _vmin, int32_t _vmax, bool _enabled)
  1285. {
  1286. float val = (float)*_val;
  1287. bool result = s_imgui.slider(_text, &val, (float)_vmin, (float)_vmax, 1.0f, _enabled);
  1288. *_val = (int32_t)val;
  1289. return result;
  1290. }
  1291. uint32_t imguiChooseUseMacroInstead(uint32_t _selected, const char* _first, ...)
  1292. {
  1293. uint32_t ii = 0;
  1294. for (const char** str = &_first; *str != NULL; ++str, ++ii)
  1295. {
  1296. if (imguiCheck(*str, ii == _selected) )
  1297. {
  1298. _selected = ii;
  1299. }
  1300. }
  1301. return _selected;
  1302. }
  1303. void imguiDrawText(int32_t _x, int32_t _y, ImguiTextAlign::Enum _align, const char* _text, uint32_t _argb)
  1304. {
  1305. s_imgui.drawText(_x, _y, _align, _text, _argb);
  1306. }
  1307. void imguiDrawLine(float _x0, float _y0, float _x1, float _y1, float _r, uint32_t _argb)
  1308. {
  1309. s_imgui.drawLine(_x0, _y0, _x1, _y1, _r, _argb);
  1310. }
  1311. void imguiDrawRoundedRect(float _x, float _y, float _width, float _height, float _r, uint32_t _argb)
  1312. {
  1313. s_imgui.drawRoundedRect(_x, _y, _width, _height, _r, _argb);
  1314. }
  1315. void imguiDrawRect(float _x, float _y, float _width, float _height, uint32_t _argb)
  1316. {
  1317. s_imgui.drawRect(_x, _y, _width, _height, _argb);
  1318. }