imgui.cpp 34 KB

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