imgui.cpp 35 KB

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