imgui.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951
  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 <bx/fpumath.h>
  28. #include <bgfx.h>
  29. #include "../entry/dbg.h"
  30. #include "imgui.h"
  31. #include "../nanovg/nanovg.h"
  32. #include "vs_imgui_color.bin.h"
  33. #include "fs_imgui_color.bin.h"
  34. #include "vs_imgui_texture.bin.h"
  35. #include "fs_imgui_texture.bin.h"
  36. #define USE_NANOVG_FONT 0
  37. #define MAX_TEMP_COORDS 100
  38. #define NUM_CIRCLE_VERTS (8 * 4)
  39. static const int32_t BUTTON_HEIGHT = 20;
  40. static const int32_t SLIDER_HEIGHT = 20;
  41. static const int32_t SLIDER_MARKER_WIDTH = 10;
  42. static const int32_t CHECK_SIZE = 8;
  43. static const int32_t DEFAULT_SPACING = 4;
  44. static const int32_t TEXT_HEIGHT = 8;
  45. static const int32_t SCROLL_AREA_PADDING = 6;
  46. static const int32_t INDENT_SIZE = 16;
  47. static const int32_t AREA_HEADER = 28;
  48. static const int32_t COLOR_WHEEL_PADDING = 60;
  49. static const float s_tabStops[4] = {150, 210, 270, 330};
  50. static void* imguiMalloc(size_t size, void* /*_userptr*/)
  51. {
  52. return malloc(size);
  53. }
  54. static void imguiFree(void* _ptr, void* /*_userptr*/)
  55. {
  56. free(_ptr);
  57. }
  58. #define STBTT_malloc(_x, _y) imguiMalloc(_x, _y)
  59. #define STBTT_free(_x, _y) imguiFree(_x, _y)
  60. #define STB_TRUETYPE_IMPLEMENTATION
  61. #include <stb_truetype/stb_truetype.h>
  62. namespace
  63. {
  64. float sign(float px, float py, float ax, float ay, float bx, float by)
  65. {
  66. return (px - bx) * (ay - by) - (ax - bx) * (py - by);
  67. }
  68. bool pointInTriangle(float px, float py, float ax, float ay, float bx, float by, float cx, float cy)
  69. {
  70. const bool b1 = sign(px, py, ax, ay, bx, by) < 0.0f;
  71. const bool b2 = sign(px, py, bx, by, cx, cy) < 0.0f;
  72. const bool b3 = sign(px, py, cx, cy, ax, ay) < 0.0f;
  73. return ( (b1 == b2) && (b2 == b3) );
  74. }
  75. void closestPointOnLine(float& ox, float &oy, float px, float py, float ax, float ay, float bx, float by)
  76. {
  77. float dx = px - ax;
  78. float dy = py - ay;
  79. float lx = bx - ax;
  80. float ly = by - ay;
  81. float len = sqrtf(lx*lx+ly*ly);
  82. // Normalize.
  83. float invLen = 1.0f/len;
  84. lx*=invLen;
  85. ly*=invLen;
  86. float dot = (dx*lx + dy*ly);
  87. if (dot < 0.0f)
  88. {
  89. ox = ax;
  90. oy = ay;
  91. }
  92. else if (dot > len)
  93. {
  94. ox = bx;
  95. oy = by;
  96. }
  97. else
  98. {
  99. ox = ax + lx*dot;
  100. oy = ay + ly*dot;
  101. }
  102. }
  103. void closestPointOnTriangle(float& ox, float &oy, float px, float py, float ax, float ay, float bx, float by, float cx, float cy)
  104. {
  105. float abx, aby;
  106. float bcx, bcy;
  107. float cax, cay;
  108. closestPointOnLine(abx, aby, px, py, ax, ay, bx, by);
  109. closestPointOnLine(bcx, bcy, px, py, bx, by, cx, cy);
  110. closestPointOnLine(cax, cay, px, py, cx, cy, ax, ay);
  111. const float pabx = px - abx;
  112. const float paby = py - aby;
  113. const float pbcx = px - bcx;
  114. const float pbcy = py - bcy;
  115. const float pcax = px - cax;
  116. const float pcay = py - cay;
  117. const float lab = sqrtf(pabx*pabx+paby*paby);
  118. const float lbc = sqrtf(pbcx*pbcx+pbcy*pbcy);
  119. const float lca = sqrtf(pcax*pcax+pcay*pcay);
  120. const float m = bx::fmin3(lab, lbc, lca);
  121. if (m == lab)
  122. {
  123. ox = abx;
  124. oy = aby;
  125. }
  126. else if (m == lbc)
  127. {
  128. ox = bcx;
  129. oy = bcy;
  130. }
  131. else// if (m == lca).
  132. {
  133. ox = cax;
  134. oy = cay;
  135. }
  136. }
  137. inline float vec2Dot(const float* __restrict _a, const float* __restrict _b)
  138. {
  139. return _a[0]*_b[0] + _a[1]*_b[1];
  140. }
  141. void barycentric(float& _u, float& _v, float& _w
  142. , float _ax, float _ay
  143. , float _bx, float _by
  144. , float _cx, float _cy
  145. , float _px, float _py
  146. )
  147. {
  148. const float v0[2] = { _bx - _ax, _by - _ay };
  149. const float v1[2] = { _cx - _ax, _cy - _ay };
  150. const float v2[2] = { _px - _ax, _py - _ay };
  151. const float d00 = vec2Dot(v0, v0);
  152. const float d01 = vec2Dot(v0, v1);
  153. const float d11 = vec2Dot(v1, v1);
  154. const float d20 = vec2Dot(v2, v0);
  155. const float d21 = vec2Dot(v2, v1);
  156. const float denom = d00 * d11 - d01 * d01;
  157. _v = (d11 * d20 - d01 * d21) / denom;
  158. _w = (d00 * d21 - d01 * d20) / denom;
  159. _u = 1.0f - _v - _w;
  160. }
  161. struct PosColorVertex
  162. {
  163. float m_x;
  164. float m_y;
  165. uint32_t m_abgr;
  166. static void init()
  167. {
  168. ms_decl
  169. .begin()
  170. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  171. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  172. .end();
  173. }
  174. static bgfx::VertexDecl ms_decl;
  175. };
  176. bgfx::VertexDecl PosColorVertex::ms_decl;
  177. struct PosColorUvVertex
  178. {
  179. float m_x;
  180. float m_y;
  181. float m_u;
  182. float m_v;
  183. uint32_t m_abgr;
  184. static void init()
  185. {
  186. ms_decl
  187. .begin()
  188. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  189. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  190. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  191. .end();
  192. }
  193. static bgfx::VertexDecl ms_decl;
  194. };
  195. bgfx::VertexDecl PosColorUvVertex::ms_decl;
  196. } // namespace
  197. struct Imgui
  198. {
  199. Imgui()
  200. : m_mx(-1)
  201. , m_my(-1)
  202. , m_scroll(0)
  203. , m_active(0)
  204. , m_hot(0)
  205. , m_hotToBe(0)
  206. , m_dragX(0)
  207. , m_dragY(0)
  208. , m_dragOrig(0)
  209. , m_widgetX(0)
  210. , m_widgetY(0)
  211. , m_widgetW(100)
  212. , m_left(false)
  213. , m_leftPressed(false)
  214. , m_leftReleased(false)
  215. , m_isHot(false)
  216. , m_isActive(false)
  217. , m_wentActive(false)
  218. , m_insideCurrentScroll(false)
  219. , m_areaId(0)
  220. , m_widgetId(0)
  221. , m_scissor(UINT16_MAX)
  222. , m_scrollTop(0)
  223. , m_scrollBottom(0)
  224. , m_scrollRight(0)
  225. , m_scrollAreaTop(0)
  226. , m_scrollAreaWidth(0)
  227. , m_scrollAreaX(0)
  228. , m_scrollVal(NULL)
  229. , m_focusTop(0)
  230. , m_focusBottom(0)
  231. , m_scrollId(0)
  232. , m_insideScrollArea(false)
  233. , m_textureWidth(512)
  234. , m_textureHeight(512)
  235. , m_halfTexel(0.0f)
  236. , m_nvg(NULL)
  237. , m_view(31)
  238. {
  239. m_invTextureWidth = 1.0f/m_textureWidth;
  240. m_invTextureHeight = 1.0f/m_textureHeight;
  241. u_texColor.idx = bgfx::invalidHandle;
  242. #if !USE_NANOVG_FONT
  243. m_fontTexture.idx = bgfx::invalidHandle;
  244. #endif // !USE_NANOVG_FONT
  245. m_colorProgram.idx = bgfx::invalidHandle;
  246. m_textureProgram.idx = bgfx::invalidHandle;
  247. }
  248. bool create(const void* _data)
  249. {
  250. m_nvg = nvgCreate(512, 512, 1, m_view);
  251. nvgCreateFontMem(m_nvg, "default", (unsigned char*)_data, INT32_MAX, 0);
  252. nvgFontSize(m_nvg, 15.0f);
  253. nvgFontFace(m_nvg, "default");
  254. for (int32_t ii = 0; ii < NUM_CIRCLE_VERTS; ++ii)
  255. {
  256. float a = (float)ii / (float)NUM_CIRCLE_VERTS * (float)(M_PI * 2.0);
  257. m_circleVerts[ii * 2 + 0] = cosf(a);
  258. m_circleVerts[ii * 2 + 1] = sinf(a);
  259. }
  260. PosColorVertex::init();
  261. PosColorUvVertex::init();
  262. u_texColor = bgfx::createUniform("u_texColor", bgfx::UniformType::Uniform1i);
  263. const bgfx::Memory* vs_imgui_color;
  264. const bgfx::Memory* fs_imgui_color;
  265. const bgfx::Memory* vs_imgui_texture;
  266. const bgfx::Memory* fs_imgui_texture;
  267. switch (bgfx::getRendererType() )
  268. {
  269. case bgfx::RendererType::Direct3D9:
  270. vs_imgui_color = bgfx::makeRef(vs_imgui_color_dx9, sizeof(vs_imgui_color_dx9) );
  271. fs_imgui_color = bgfx::makeRef(fs_imgui_color_dx9, sizeof(fs_imgui_color_dx9) );
  272. vs_imgui_texture = bgfx::makeRef(vs_imgui_texture_dx9, sizeof(vs_imgui_texture_dx9) );
  273. fs_imgui_texture = bgfx::makeRef(fs_imgui_texture_dx9, sizeof(fs_imgui_texture_dx9) );
  274. m_halfTexel = 0.5f;
  275. break;
  276. case bgfx::RendererType::Direct3D11:
  277. vs_imgui_color = bgfx::makeRef(vs_imgui_color_dx11, sizeof(vs_imgui_color_dx11) );
  278. fs_imgui_color = bgfx::makeRef(fs_imgui_color_dx11, sizeof(fs_imgui_color_dx11) );
  279. vs_imgui_texture = bgfx::makeRef(vs_imgui_texture_dx11, sizeof(vs_imgui_texture_dx11) );
  280. fs_imgui_texture = bgfx::makeRef(fs_imgui_texture_dx11, sizeof(fs_imgui_texture_dx11) );
  281. break;
  282. default:
  283. vs_imgui_color = bgfx::makeRef(vs_imgui_color_glsl, sizeof(vs_imgui_color_glsl) );
  284. fs_imgui_color = bgfx::makeRef(fs_imgui_color_glsl, sizeof(fs_imgui_color_glsl) );
  285. vs_imgui_texture = bgfx::makeRef(vs_imgui_texture_glsl, sizeof(vs_imgui_texture_glsl) );
  286. fs_imgui_texture = bgfx::makeRef(fs_imgui_texture_glsl, sizeof(fs_imgui_texture_glsl) );
  287. break;
  288. }
  289. bgfx::ShaderHandle vsh;
  290. bgfx::ShaderHandle fsh;
  291. vsh = bgfx::createShader(vs_imgui_color);
  292. fsh = bgfx::createShader(fs_imgui_color);
  293. m_colorProgram = bgfx::createProgram(vsh, fsh);
  294. bgfx::destroyShader(vsh);
  295. bgfx::destroyShader(fsh);
  296. vsh = bgfx::createShader(vs_imgui_texture);
  297. fsh = bgfx::createShader(fs_imgui_texture);
  298. m_textureProgram = bgfx::createProgram(vsh, fsh);
  299. bgfx::destroyShader(vsh);
  300. bgfx::destroyShader(fsh);
  301. #if !USE_NANOVG_FONT
  302. const bgfx::Memory* mem = bgfx::alloc(m_textureWidth * m_textureHeight);
  303. stbtt_BakeFontBitmap( (uint8_t*)_data, 0, 15.0f, mem->data, m_textureWidth, m_textureHeight, 32, 96, m_cdata);
  304. m_fontTexture = bgfx::createTexture2D(m_textureWidth, m_textureHeight, 1, bgfx::TextureFormat::R8, BGFX_TEXTURE_NONE, mem);
  305. #endif // !USE_NANOVG_FONT
  306. return true;
  307. }
  308. void destroy()
  309. {
  310. bgfx::destroyUniform(u_texColor);
  311. #if !USE_NANOVG_FONT
  312. bgfx::destroyTexture(m_fontTexture);
  313. #endif // !USE_NANOVG_FONT
  314. bgfx::destroyProgram(m_colorProgram);
  315. bgfx::destroyProgram(m_textureProgram);
  316. nvgDelete(m_nvg);
  317. }
  318. bool anyActive() const
  319. {
  320. return m_active != 0;
  321. }
  322. bool isActive(uint32_t _id) const
  323. {
  324. return m_active == _id;
  325. }
  326. bool isHot(uint32_t _id) const
  327. {
  328. return m_hot == _id;
  329. }
  330. bool inRect(int32_t _x, int32_t _y, int32_t _width, int32_t _height, bool _checkScroll = true) const
  331. {
  332. return (!_checkScroll || m_insideCurrentScroll)
  333. && m_mx >= _x
  334. && m_mx <= _x + _width
  335. && m_my >= _y
  336. && m_my <= _y + _height;
  337. }
  338. void clearInput()
  339. {
  340. m_leftPressed = false;
  341. m_leftReleased = false;
  342. m_scroll = 0;
  343. }
  344. void clearActive()
  345. {
  346. m_active = 0;
  347. // mark all UI for this frame as processed
  348. clearInput();
  349. }
  350. void setActive(uint32_t _id)
  351. {
  352. m_active = _id;
  353. m_wentActive = true;
  354. }
  355. void setHot(uint32_t _id)
  356. {
  357. m_hotToBe = _id;
  358. }
  359. bool buttonLogic(uint32_t _id, bool _over)
  360. {
  361. bool res = false;
  362. // process down
  363. if (!anyActive() )
  364. {
  365. if (_over)
  366. {
  367. setHot(_id);
  368. }
  369. if (isHot(_id)
  370. && m_leftPressed)
  371. {
  372. setActive(_id);
  373. }
  374. }
  375. // if button is active, then react on left up
  376. if (isActive(_id) )
  377. {
  378. m_isActive = true;
  379. if (_over)
  380. {
  381. setHot(_id);
  382. }
  383. if (m_leftReleased)
  384. {
  385. if (isHot(_id) )
  386. {
  387. res = true;
  388. }
  389. clearActive();
  390. }
  391. }
  392. if (isHot(_id) )
  393. {
  394. m_isHot = true;
  395. }
  396. return res;
  397. }
  398. void updateInput(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll)
  399. {
  400. bool left = (_button & IMGUI_MBUT_LEFT) != 0;
  401. m_mx = _mx;
  402. m_my = _my;
  403. m_leftPressed = !m_left && left;
  404. m_leftReleased = m_left && !left;
  405. m_left = left;
  406. m_scroll = _scroll;
  407. }
  408. void beginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, uint16_t _width, uint16_t _height, uint8_t _view)
  409. {
  410. nvgBeginFrame(m_nvg, _width, _height, 1.0f, NVG_STRAIGHT_ALPHA);
  411. m_view = _view;
  412. bgfx::setViewSeq(_view, true);
  413. bgfx::setViewRect(_view, 0, 0, _width, _height);
  414. float proj[16];
  415. bx::mtxOrtho(proj, 0.0f, (float)_width, (float)_height, 0.0f, 0.0f, 1000.0f);
  416. bgfx::setViewTransform(_view, NULL, proj);
  417. updateInput(_mx, _my, _button, _scroll);
  418. m_hot = m_hotToBe;
  419. m_hotToBe = 0;
  420. m_wentActive = false;
  421. m_isActive = false;
  422. m_isHot = false;
  423. m_widgetX = 0;
  424. m_widgetY = 0;
  425. m_widgetW = 0;
  426. m_areaId = 1;
  427. m_widgetId = 1;
  428. }
  429. void endFrame()
  430. {
  431. clearInput();
  432. nvgEndFrame(m_nvg);
  433. }
  434. bool beginScrollArea(const char* _name, int32_t _x, int32_t _y, int32_t _width, int32_t _height, int32_t* _scroll)
  435. {
  436. m_areaId++;
  437. m_widgetId = 0;
  438. m_scrollId = (m_areaId << 16) | m_widgetId;
  439. m_widgetX = _x + SCROLL_AREA_PADDING;
  440. m_widgetY = _y + AREA_HEADER + (*_scroll);
  441. m_widgetW = _width - SCROLL_AREA_PADDING * 4;
  442. m_scrollTop = _y + AREA_HEADER;
  443. m_scrollBottom = _y + _height;
  444. m_scrollRight = _x + _width - SCROLL_AREA_PADDING * 3;
  445. m_scrollVal = _scroll;
  446. m_scrollAreaX = _x;
  447. m_scrollAreaWidth = _width;
  448. m_scrollAreaTop = m_widgetY;
  449. m_focusTop = _y - AREA_HEADER;
  450. m_focusBottom = _y - AREA_HEADER + _height;
  451. m_insideScrollArea = inRect(_x, _y, _width, _height, false);
  452. m_insideCurrentScroll = m_insideScrollArea;
  453. drawRoundedRect( (float)_x
  454. , (float)_y
  455. , (float)_width
  456. , (float)_height
  457. , 6
  458. , imguiRGBA(0, 0, 0, 192)
  459. );
  460. drawText(_x + AREA_HEADER / 2
  461. , _y + AREA_HEADER / 2
  462. , ImguiTextAlign::Left
  463. , _name
  464. , imguiRGBA(255, 255, 255, 128)
  465. );
  466. nvgScissor(m_nvg
  467. , float(_x + SCROLL_AREA_PADDING)
  468. , float(_y + AREA_HEADER)
  469. , float(_width - SCROLL_AREA_PADDING * 4)
  470. , float(_height - AREA_HEADER - SCROLL_AREA_PADDING)
  471. );
  472. m_scissor = bgfx::setScissor(uint16_t(_x + SCROLL_AREA_PADDING)
  473. , uint16_t(_y + AREA_HEADER)
  474. , uint16_t(_width - SCROLL_AREA_PADDING * 4)
  475. , uint16_t(_height - AREA_HEADER - SCROLL_AREA_PADDING)
  476. );
  477. return m_insideScrollArea;
  478. }
  479. void endScrollArea()
  480. {
  481. // Disable scissoring.
  482. m_scissor = UINT16_MAX;
  483. nvgResetScissor(m_nvg);
  484. // Draw scroll bar
  485. int32_t xx = m_scrollRight + SCROLL_AREA_PADDING / 2;
  486. int32_t yy = m_scrollTop;
  487. int32_t width = SCROLL_AREA_PADDING * 2;
  488. int32_t height = m_scrollBottom - m_scrollTop;
  489. int32_t stop = m_scrollAreaTop;
  490. int32_t sbot = m_widgetY + SCROLL_AREA_PADDING;
  491. int32_t sh = sbot - stop; // The scrollable area height.
  492. float barHeight = (float)height / (float)sh;
  493. if (barHeight < 1.0f)
  494. {
  495. float barY = bx::fsaturate( (float)(yy - stop) / (float)sh);
  496. // Handle scroll bar logic.
  497. uint32_t hid = m_scrollId;
  498. int32_t hx = xx;
  499. int32_t hy = yy + (int)(barY * height);
  500. int32_t hw = width;
  501. int32_t hh = (int)(barHeight * height);
  502. const int32_t range = height - (hh - 1);
  503. bool over = inRect(hx, hy, hw, hh);
  504. buttonLogic(hid, over);
  505. if (isActive(hid) )
  506. {
  507. float uu = (float)(hy - yy) / (float)range;
  508. if (m_wentActive)
  509. {
  510. m_dragY = m_my;
  511. m_dragOrig = uu;
  512. }
  513. if (m_dragY != m_my)
  514. {
  515. uu = bx::fsaturate(m_dragOrig + (m_my - m_dragY) / (float)range);
  516. *m_scrollVal = (int)(uu * (height - sh) );
  517. }
  518. }
  519. // BG
  520. drawRoundedRect( (float)xx
  521. , (float)yy
  522. , (float)width
  523. , (float)height
  524. , (float)width / 2 - 1
  525. , imguiRGBA(0, 0, 0, 196)
  526. );
  527. // Bar
  528. if (isActive(hid) )
  529. {
  530. drawRoundedRect( (float)hx
  531. , (float)hy
  532. , (float)hw
  533. , (float)hh
  534. , (float)width / 2 - 1
  535. , imguiRGBA(255, 196, 0, 196)
  536. );
  537. }
  538. else
  539. {
  540. drawRoundedRect( (float)hx
  541. , (float)hy
  542. , (float)hw
  543. , (float)hh
  544. , (float)width / 2 - 1
  545. , isHot(hid) ? imguiRGBA(255, 196, 0, 96) : imguiRGBA(255, 255, 255, 64)
  546. );
  547. }
  548. // Handle mouse scrolling.
  549. if (m_insideScrollArea) // && !anyActive() )
  550. {
  551. if (m_scroll)
  552. {
  553. *m_scrollVal += bx::uint32_clamp(20 * m_scroll, 0, sh - height);
  554. }
  555. }
  556. }
  557. m_insideCurrentScroll = false;
  558. }
  559. bool button(const char* _text, bool _enabled)
  560. {
  561. m_widgetId++;
  562. uint32_t id = (m_areaId << 16) | m_widgetId;
  563. int32_t xx = m_widgetX;
  564. int32_t yy = m_widgetY;
  565. int32_t width = m_widgetW;
  566. int32_t height = BUTTON_HEIGHT;
  567. m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  568. bool over = _enabled && inRect(xx, yy, width, height);
  569. bool res = buttonLogic(id, over);
  570. drawRoundedRect( (float)xx
  571. , (float)yy
  572. , (float)width
  573. , (float)height
  574. , (float)BUTTON_HEIGHT / 2 - 1
  575. , imguiRGBA(128, 128, 128, isActive(id) ? 196 : 96)
  576. );
  577. if (_enabled)
  578. {
  579. drawText(xx + BUTTON_HEIGHT / 2
  580. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  581. , ImguiTextAlign::Left
  582. , _text
  583. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  584. );
  585. }
  586. else
  587. {
  588. drawText(xx + BUTTON_HEIGHT / 2
  589. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  590. , ImguiTextAlign::Left
  591. , _text
  592. , imguiRGBA(128, 128, 128, 200)
  593. );
  594. }
  595. return res;
  596. }
  597. bool item(const char* _text, bool _enabled)
  598. {
  599. m_widgetId++;
  600. uint32_t id = (m_areaId << 16) | m_widgetId;
  601. int32_t xx = m_widgetX;
  602. int32_t yy = m_widgetY;
  603. int32_t width = m_widgetW;
  604. int32_t height = BUTTON_HEIGHT;
  605. m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  606. bool over = _enabled && inRect(xx, yy, width, height);
  607. bool res = buttonLogic(id, over);
  608. if (isHot(id) )
  609. {
  610. drawRoundedRect( (float)xx
  611. , (float)yy
  612. , (float)width
  613. , (float)height
  614. , 2.0f
  615. , imguiRGBA(255, 196, 0, isActive(id) ? 196 : 96)
  616. );
  617. }
  618. if (_enabled)
  619. {
  620. drawText(xx + BUTTON_HEIGHT / 2
  621. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  622. , ImguiTextAlign::Left
  623. , _text
  624. , imguiRGBA(255, 255, 255, 200)
  625. );
  626. }
  627. else
  628. {
  629. drawText(xx + BUTTON_HEIGHT / 2
  630. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  631. , ImguiTextAlign::Left
  632. , _text
  633. , imguiRGBA(128, 128, 128, 200)
  634. );
  635. }
  636. return res;
  637. }
  638. bool check(const char* _text, bool _checked, bool _enabled)
  639. {
  640. m_widgetId++;
  641. uint32_t id = (m_areaId << 16) | m_widgetId;
  642. int32_t xx = m_widgetX;
  643. int32_t yy = m_widgetY;
  644. int32_t width = m_widgetW;
  645. int32_t height = BUTTON_HEIGHT;
  646. m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  647. bool over = _enabled && inRect(xx, yy, width, height);
  648. bool res = buttonLogic(id, over);
  649. const int32_t cx = xx + BUTTON_HEIGHT / 2 - CHECK_SIZE / 2;
  650. const int32_t cy = yy + BUTTON_HEIGHT / 2 - CHECK_SIZE / 2;
  651. drawRoundedRect( (float)cx - 3
  652. , (float)cy - 3
  653. , (float)CHECK_SIZE + 6
  654. , (float)CHECK_SIZE + 6
  655. , 4
  656. , imguiRGBA(128, 128, 128, isActive(id) ? 196 : 96)
  657. );
  658. if (_checked)
  659. {
  660. if (_enabled)
  661. {
  662. drawRoundedRect( (float)cx
  663. , (float)cy
  664. , (float)CHECK_SIZE
  665. , (float)CHECK_SIZE
  666. , (float)CHECK_SIZE / 2 - 1
  667. , imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200)
  668. );
  669. }
  670. else
  671. {
  672. drawRoundedRect( (float)cx
  673. , (float)cy
  674. , (float)CHECK_SIZE
  675. , (float)CHECK_SIZE
  676. , (float)CHECK_SIZE / 2 - 1
  677. , imguiRGBA(128, 128, 128, 200)
  678. );
  679. }
  680. }
  681. if (_enabled)
  682. {
  683. drawText(xx + BUTTON_HEIGHT
  684. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  685. , ImguiTextAlign::Left
  686. , _text
  687. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  688. );
  689. }
  690. else
  691. {
  692. drawText(xx + BUTTON_HEIGHT
  693. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  694. , ImguiTextAlign::Left
  695. , _text
  696. , imguiRGBA(128, 128, 128, 200)
  697. );
  698. }
  699. return res;
  700. }
  701. bool collapse(const char* _text, const char* _subtext, bool _checked, bool _enabled)
  702. {
  703. m_widgetId++;
  704. uint32_t id = (m_areaId << 16) | m_widgetId;
  705. int32_t xx = m_widgetX;
  706. int32_t yy = m_widgetY;
  707. int32_t width = m_widgetW;
  708. int32_t height = BUTTON_HEIGHT;
  709. m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  710. const int32_t cx = xx + BUTTON_HEIGHT / 2 - CHECK_SIZE / 2;
  711. const int32_t cy = yy + BUTTON_HEIGHT / 2 - CHECK_SIZE / 2;
  712. bool over = _enabled && inRect(xx, yy, width, height);
  713. bool res = buttonLogic(id, over);
  714. if (_checked)
  715. {
  716. drawTriangle(cx
  717. , cy
  718. , CHECK_SIZE
  719. , CHECK_SIZE
  720. , 2
  721. , imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200)
  722. );
  723. }
  724. else
  725. {
  726. drawTriangle(cx
  727. , cy
  728. , CHECK_SIZE
  729. , CHECK_SIZE
  730. , 1
  731. , imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200)
  732. );
  733. }
  734. if (_enabled)
  735. {
  736. drawText(xx + BUTTON_HEIGHT
  737. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  738. , ImguiTextAlign::Left
  739. , _text
  740. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  741. );
  742. }
  743. else
  744. {
  745. drawText(xx + BUTTON_HEIGHT
  746. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  747. , ImguiTextAlign::Left
  748. , _text
  749. , imguiRGBA(128, 128, 128, 200)
  750. );
  751. }
  752. if (_subtext)
  753. {
  754. drawText(xx + width - BUTTON_HEIGHT / 2
  755. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  756. , ImguiTextAlign::Right
  757. , _subtext
  758. , imguiRGBA(255, 255, 255, 128)
  759. );
  760. }
  761. return res;
  762. }
  763. void labelVargs(const char* _format, va_list _argList)
  764. {
  765. char temp[8192];
  766. char* out = temp;
  767. int32_t len = bx::vsnprintf(out, sizeof(temp), _format, _argList);
  768. if ( (int32_t)sizeof(temp) < len)
  769. {
  770. out = (char*)alloca(len+1);
  771. len = bx::vsnprintf(out, len, _format, _argList);
  772. }
  773. out[len] = '\0';
  774. int32_t xx = m_widgetX;
  775. int32_t yy = m_widgetY;
  776. m_widgetY += BUTTON_HEIGHT;
  777. drawText(xx
  778. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  779. , ImguiTextAlign::Left
  780. , out
  781. , imguiRGBA(255, 255, 255, 255)
  782. );
  783. }
  784. void value(const char* _text)
  785. {
  786. const int32_t xx = m_widgetX;
  787. const int32_t yy = m_widgetY;
  788. const int32_t ww = m_widgetW;
  789. m_widgetY += BUTTON_HEIGHT;
  790. drawText(xx + ww - BUTTON_HEIGHT / 2
  791. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  792. , ImguiTextAlign::Right
  793. , _text
  794. , imguiRGBA(255, 255, 255, 200)
  795. );
  796. }
  797. bool slider(const char* _text, float& _val, float _vmin, float _vmax, float _vinc, bool _enabled)
  798. {
  799. m_widgetId++;
  800. uint32_t id = (m_areaId << 16) | m_widgetId;
  801. int32_t xx = m_widgetX;
  802. int32_t yy = m_widgetY;
  803. int32_t width = m_widgetW;
  804. int32_t height = SLIDER_HEIGHT;
  805. m_widgetY += SLIDER_HEIGHT + DEFAULT_SPACING;
  806. drawRoundedRect( (float)xx, (float)yy, (float)width, (float)height, 4.0f, imguiRGBA(0, 0, 0, 128) );
  807. const int32_t range = width - SLIDER_MARKER_WIDTH;
  808. float uu = bx::fsaturate( (_val - _vmin) / (_vmax - _vmin) );
  809. int32_t m = (int)(uu * range);
  810. bool over = _enabled && inRect(xx + m, yy, SLIDER_MARKER_WIDTH, SLIDER_HEIGHT);
  811. bool res = buttonLogic(id, over);
  812. bool valChanged = false;
  813. if (isActive(id) )
  814. {
  815. if (m_wentActive)
  816. {
  817. m_dragX = m_mx;
  818. m_dragOrig = uu;
  819. }
  820. if (m_dragX != m_mx)
  821. {
  822. uu = bx::fsaturate(m_dragOrig + (float)(m_mx - m_dragX) / (float)range);
  823. _val = _vmin + uu * (_vmax - _vmin);
  824. _val = floorf(_val / _vinc + 0.5f) * _vinc; // Snap to vinc
  825. m = (int)(uu * range);
  826. valChanged = true;
  827. }
  828. }
  829. if (isActive(id) )
  830. {
  831. drawRoundedRect( (float)(xx + m)
  832. , (float)yy
  833. , (float)SLIDER_MARKER_WIDTH
  834. , (float)SLIDER_HEIGHT
  835. , 4.0f
  836. , imguiRGBA(255, 255, 255, 255)
  837. );
  838. }
  839. else
  840. {
  841. drawRoundedRect( (float)(xx + m)
  842. , (float)yy
  843. , (float)SLIDER_MARKER_WIDTH
  844. , (float)SLIDER_HEIGHT
  845. , 4.0f
  846. , isHot(id) ? imguiRGBA(255, 196, 0, 128) : imguiRGBA(255, 255, 255, 64)
  847. );
  848. }
  849. // TODO: fix this, take a look at 'nicenum'.
  850. int32_t digits = (int)(ceilf(log10f(_vinc) ) );
  851. char fmt[16];
  852. bx::snprintf(fmt, 16, "%%.%df", digits >= 0 ? 0 : -digits);
  853. char msg[128];
  854. bx::snprintf(msg, 128, fmt, _val);
  855. if (_enabled)
  856. {
  857. drawText(xx + SLIDER_HEIGHT / 2
  858. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  859. , ImguiTextAlign::Left
  860. , _text
  861. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  862. );
  863. drawText(xx + width - SLIDER_HEIGHT / 2
  864. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  865. , ImguiTextAlign::Right
  866. , msg
  867. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  868. );
  869. }
  870. else
  871. {
  872. drawText(xx + SLIDER_HEIGHT / 2
  873. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  874. , ImguiTextAlign::Left
  875. , _text
  876. , imguiRGBA(128, 128, 128, 200)
  877. );
  878. drawText(xx + width - SLIDER_HEIGHT / 2
  879. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  880. , ImguiTextAlign::Right
  881. , msg
  882. , imguiRGBA(128, 128, 128, 200)
  883. );
  884. }
  885. return res || valChanged;
  886. }
  887. void indent()
  888. {
  889. m_widgetX += INDENT_SIZE;
  890. m_widgetW -= INDENT_SIZE;
  891. }
  892. void unindent()
  893. {
  894. m_widgetX -= INDENT_SIZE;
  895. m_widgetW += INDENT_SIZE;
  896. }
  897. void separator()
  898. {
  899. m_widgetY += DEFAULT_SPACING * 3;
  900. }
  901. void separatorLine()
  902. {
  903. int32_t xx = m_widgetX;
  904. int32_t yy = m_widgetY;
  905. int32_t width = m_widgetW;
  906. int32_t height = 1;
  907. m_widgetY += DEFAULT_SPACING * 4;
  908. drawRect( (float)xx
  909. , (float)yy
  910. , (float)width
  911. , (float)height
  912. , imguiRGBA(255, 255, 255, 32)
  913. );
  914. }
  915. void drawPolygon(const float* _coords, uint32_t _numCoords, float _r, uint32_t _abgr)
  916. {
  917. _numCoords = bx::uint32_min(_numCoords, MAX_TEMP_COORDS);
  918. for (uint32_t ii = 0, jj = _numCoords - 1; ii < _numCoords; jj = ii++)
  919. {
  920. const float* v0 = &_coords[jj * 2];
  921. const float* v1 = &_coords[ii * 2];
  922. float dx = v1[0] - v0[0];
  923. float dy = v1[1] - v0[1];
  924. float d = sqrtf(dx * dx + dy * dy);
  925. if (d > 0)
  926. {
  927. d = 1.0f / d;
  928. dx *= d;
  929. dy *= d;
  930. }
  931. m_tempNormals[jj * 2 + 0] = dy;
  932. m_tempNormals[jj * 2 + 1] = -dx;
  933. }
  934. for (uint32_t ii = 0, jj = _numCoords - 1; ii < _numCoords; jj = ii++)
  935. {
  936. float dlx0 = m_tempNormals[jj * 2 + 0];
  937. float dly0 = m_tempNormals[jj * 2 + 1];
  938. float dlx1 = m_tempNormals[ii * 2 + 0];
  939. float dly1 = m_tempNormals[ii * 2 + 1];
  940. float dmx = (dlx0 + dlx1) * 0.5f;
  941. float dmy = (dly0 + dly1) * 0.5f;
  942. float dmr2 = dmx * dmx + dmy * dmy;
  943. if (dmr2 > 0.000001f)
  944. {
  945. float scale = 1.0f / dmr2;
  946. if (scale > 10.0f)
  947. {
  948. scale = 10.0f;
  949. }
  950. dmx *= scale;
  951. dmy *= scale;
  952. }
  953. m_tempCoords[ii * 2 + 0] = _coords[ii * 2 + 0] + dmx * _r;
  954. m_tempCoords[ii * 2 + 1] = _coords[ii * 2 + 1] + dmy * _r;
  955. }
  956. uint32_t numVertices = _numCoords*6 + (_numCoords-2)*3;
  957. if (bgfx::checkAvailTransientVertexBuffer(numVertices, PosColorVertex::ms_decl) )
  958. {
  959. bgfx::TransientVertexBuffer tvb;
  960. bgfx::allocTransientVertexBuffer(&tvb, numVertices, PosColorVertex::ms_decl);
  961. uint32_t trans = _abgr&0xffffff;
  962. PosColorVertex* vertex = (PosColorVertex*)tvb.data;
  963. for (uint32_t ii = 0, jj = _numCoords-1; ii < _numCoords; jj = ii++)
  964. {
  965. vertex->m_x = _coords[ii*2+0];
  966. vertex->m_y = _coords[ii*2+1];
  967. vertex->m_abgr = _abgr;
  968. ++vertex;
  969. vertex->m_x = _coords[jj*2+0];
  970. vertex->m_y = _coords[jj*2+1];
  971. vertex->m_abgr = _abgr;
  972. ++vertex;
  973. vertex->m_x = m_tempCoords[jj*2+0];
  974. vertex->m_y = m_tempCoords[jj*2+1];
  975. vertex->m_abgr = trans;
  976. ++vertex;
  977. vertex->m_x = m_tempCoords[jj*2+0];
  978. vertex->m_y = m_tempCoords[jj*2+1];
  979. vertex->m_abgr = trans;
  980. ++vertex;
  981. vertex->m_x = m_tempCoords[ii*2+0];
  982. vertex->m_y = m_tempCoords[ii*2+1];
  983. vertex->m_abgr = trans;
  984. ++vertex;
  985. vertex->m_x = _coords[ii*2+0];
  986. vertex->m_y = _coords[ii*2+1];
  987. vertex->m_abgr = _abgr;
  988. ++vertex;
  989. }
  990. for (uint32_t ii = 2; ii < _numCoords; ++ii)
  991. {
  992. vertex->m_x = _coords[0];
  993. vertex->m_y = _coords[1];
  994. vertex->m_abgr = _abgr;
  995. ++vertex;
  996. vertex->m_x = _coords[(ii-1)*2+0];
  997. vertex->m_y = _coords[(ii-1)*2+1];
  998. vertex->m_abgr = _abgr;
  999. ++vertex;
  1000. vertex->m_x = _coords[ii*2+0];
  1001. vertex->m_y = _coords[ii*2+1];
  1002. vertex->m_abgr = _abgr;
  1003. ++vertex;
  1004. }
  1005. bgfx::setVertexBuffer(&tvb);
  1006. bgfx::setState(0
  1007. | BGFX_STATE_RGB_WRITE
  1008. | BGFX_STATE_ALPHA_WRITE
  1009. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  1010. );
  1011. bgfx::setProgram(m_colorProgram);
  1012. bgfx::setScissor(m_scissor);
  1013. bgfx::submit(m_view);
  1014. }
  1015. }
  1016. void drawRect(float _x, float _y, float w, float h, uint32_t _argb, float _fth = 1.0f)
  1017. {
  1018. float verts[4 * 2] =
  1019. {
  1020. _x + 0.5f, _y + 0.5f,
  1021. _x + w - 0.5f, _y + 0.5f,
  1022. _x + w - 0.5f, _y + h - 0.5f,
  1023. _x + 0.5f, _y + h - 0.5f,
  1024. };
  1025. drawPolygon(verts, 4, _fth, _argb);
  1026. }
  1027. void drawRoundedRect(float _x, float _y, float w, float h, float r, uint32_t _argb, float _fth = 1.0f)
  1028. {
  1029. const uint32_t num = NUM_CIRCLE_VERTS / 4;
  1030. const float* cverts = m_circleVerts;
  1031. float verts[(num + 1) * 4 * 2];
  1032. float* vv = verts;
  1033. for (uint32_t ii = 0; ii <= num; ++ii)
  1034. {
  1035. *vv++ = _x + w - r + cverts[ii * 2] * r;
  1036. *vv++ = _y + h - r + cverts[ii * 2 + 1] * r;
  1037. }
  1038. for (uint32_t ii = num; ii <= num * 2; ++ii)
  1039. {
  1040. *vv++ = _x + r + cverts[ii * 2] * r;
  1041. *vv++ = _y + h - r + cverts[ii * 2 + 1] * r;
  1042. }
  1043. for (uint32_t ii = num * 2; ii <= num * 3; ++ii)
  1044. {
  1045. *vv++ = _x + r + cverts[ii * 2] * r;
  1046. *vv++ = _y + r + cverts[ii * 2 + 1] * r;
  1047. }
  1048. for (uint32_t ii = num * 3; ii < num * 4; ++ii)
  1049. {
  1050. *vv++ = _x + w - r + cverts[ii * 2] * r;
  1051. *vv++ = _y + r + cverts[ii * 2 + 1] * r;
  1052. }
  1053. *vv++ = _x + w - r + cverts[0] * r;
  1054. *vv++ = _y + r + cverts[1] * r;
  1055. drawPolygon(verts, (num + 1) * 4, _fth, _argb);
  1056. }
  1057. void drawLine(float _x0, float _y0, float _x1, float _y1, float _r, uint32_t _abgr, float _fth = 1.0f)
  1058. {
  1059. float dx = _x1 - _x0;
  1060. float dy = _y1 - _y0;
  1061. float d = sqrtf(dx * dx + dy * dy);
  1062. if (d > 0.0001f)
  1063. {
  1064. d = 1.0f / d;
  1065. dx *= d;
  1066. dy *= d;
  1067. }
  1068. float nx = dy;
  1069. float ny = -dx;
  1070. float verts[4 * 2];
  1071. _r -= _fth;
  1072. _r *= 0.5f;
  1073. if (_r < 0.01f)
  1074. {
  1075. _r = 0.01f;
  1076. }
  1077. dx *= _r;
  1078. dy *= _r;
  1079. nx *= _r;
  1080. ny *= _r;
  1081. verts[0] = _x0 - dx - nx;
  1082. verts[1] = _y0 - dy - ny;
  1083. verts[2] = _x0 - dx + nx;
  1084. verts[3] = _y0 - dy + ny;
  1085. verts[4] = _x1 + dx + nx;
  1086. verts[5] = _y1 + dy + ny;
  1087. verts[6] = _x1 + dx - nx;
  1088. verts[7] = _y1 + dy - ny;
  1089. drawPolygon(verts, 4, _fth, _abgr);
  1090. }
  1091. void drawTriangle(int32_t _x, int32_t _y, int32_t _width, int32_t _height, int32_t _flags, uint32_t _abgr)
  1092. {
  1093. if (1 == _flags)
  1094. {
  1095. const float verts[3 * 2] =
  1096. {
  1097. (float)_x + 0.5f, (float)_y + 0.5f,
  1098. (float)_x + 0.5f + (float)_width * 1.0f, (float)_y + 0.5f + (float)_height / 2.0f - 0.5f,
  1099. (float)_x + 0.5f, (float)_y + 0.5f + (float)_height - 1.0f,
  1100. };
  1101. drawPolygon(verts, 3, 1.0f, _abgr);
  1102. }
  1103. else
  1104. {
  1105. const float verts[3 * 2] =
  1106. {
  1107. (float)_x + 0.5f, (float)_y + 0.5f + (float)_height - 1.0f,
  1108. (float)_x + 0.5f + (float)_width / 2.0f - 0.5f, (float)_y + 0.5f,
  1109. (float)_x + 0.5f + (float)_width - 1.0f, (float)_y + 0.5f + (float)_height - 1.0f,
  1110. };
  1111. drawPolygon(verts, 3, 1.0f, _abgr);
  1112. }
  1113. }
  1114. #if !USE_NANOVG_FONT
  1115. void getBakedQuad(stbtt_bakedchar* _chardata, int32_t char_index, float* _xpos, float* _ypos, stbtt_aligned_quad* _quad)
  1116. {
  1117. stbtt_bakedchar* b = _chardata + char_index;
  1118. int32_t round_x = STBTT_ifloor(*_xpos + b->xoff);
  1119. int32_t round_y = STBTT_ifloor(*_ypos + b->yoff);
  1120. _quad->x0 = (float)round_x;
  1121. _quad->y0 = (float)round_y;
  1122. _quad->x1 = (float)round_x + b->x1 - b->x0;
  1123. _quad->y1 = (float)round_y + b->y1 - b->y0;
  1124. _quad->s0 = (b->x0 + m_halfTexel) * m_invTextureWidth;
  1125. _quad->t0 = (b->y0 + m_halfTexel) * m_invTextureWidth;
  1126. _quad->s1 = (b->x1 + m_halfTexel) * m_invTextureHeight;
  1127. _quad->t1 = (b->y1 + m_halfTexel) * m_invTextureHeight;
  1128. *_xpos += b->xadvance;
  1129. }
  1130. float getTextLength(stbtt_bakedchar* _chardata, const char* _text, uint32_t& _numVertices)
  1131. {
  1132. float xpos = 0;
  1133. float len = 0;
  1134. uint32_t numVertices = 0;
  1135. while (*_text)
  1136. {
  1137. int32_t ch = (uint8_t)*_text;
  1138. if (ch == '\t')
  1139. {
  1140. for (int32_t ii = 0; ii < 4; ++ii)
  1141. {
  1142. if (xpos < s_tabStops[ii])
  1143. {
  1144. xpos = s_tabStops[ii];
  1145. break;
  1146. }
  1147. }
  1148. }
  1149. else if (ch >= ' '
  1150. && ch < 128)
  1151. {
  1152. stbtt_bakedchar* b = _chardata + ch - ' ';
  1153. int32_t round_x = STBTT_ifloor( (xpos + b->xoff) + 0.5);
  1154. len = round_x + b->x1 - b->x0 + 0.5f;
  1155. xpos += b->xadvance;
  1156. numVertices += 6;
  1157. }
  1158. ++_text;
  1159. }
  1160. _numVertices = numVertices;
  1161. return len;
  1162. }
  1163. #endif // !USE_NANOVG_FONT
  1164. void drawText(int32_t _x, int32_t _y, ImguiTextAlign::Enum _align, const char* _text, uint32_t _abgr)
  1165. {
  1166. drawText( (float)_x, (float)_y, _text, _align, _abgr);
  1167. }
  1168. void drawText(float _x, float _y, const char* _text, ImguiTextAlign::Enum _align, uint32_t _abgr)
  1169. {
  1170. #if USE_NANOVG_FONT
  1171. static uint32_t textAlign[ImguiTextAlign::Count] =
  1172. {
  1173. NVG_ALIGN_LEFT,
  1174. NVG_ALIGN_CENTER,
  1175. NVG_ALIGN_RIGHT,
  1176. };
  1177. nvgTextAlign(m_nvg, textAlign[_align]);
  1178. nvgFontBlur(m_nvg, 0.0f);
  1179. nvgFillColor(m_nvg, nvgRGBAu(_abgr) );
  1180. nvgText(m_nvg, _x, _y, _text, NULL);
  1181. #else
  1182. uint32_t numVertices = 0;
  1183. if (_align == ImguiTextAlign::Center)
  1184. {
  1185. _x -= getTextLength(m_cdata, _text, numVertices) / 2;
  1186. }
  1187. else if (_align == ImguiTextAlign::Right)
  1188. {
  1189. _x -= getTextLength(m_cdata, _text, numVertices);
  1190. }
  1191. else // just count vertices
  1192. {
  1193. getTextLength(m_cdata, _text, numVertices);
  1194. }
  1195. if (bgfx::checkAvailTransientVertexBuffer(numVertices, PosColorUvVertex::ms_decl) )
  1196. {
  1197. bgfx::TransientVertexBuffer tvb;
  1198. bgfx::allocTransientVertexBuffer(&tvb, numVertices, PosColorUvVertex::ms_decl);
  1199. PosColorUvVertex* vertex = (PosColorUvVertex*)tvb.data;
  1200. const float ox = _x;
  1201. while (*_text)
  1202. {
  1203. int32_t ch = (uint8_t)*_text;
  1204. if (ch == '\t')
  1205. {
  1206. for (int32_t i = 0; i < 4; ++i)
  1207. {
  1208. if (_x < s_tabStops[i] + ox)
  1209. {
  1210. _x = s_tabStops[i] + ox;
  1211. break;
  1212. }
  1213. }
  1214. }
  1215. else if (ch >= ' '
  1216. && ch < 128)
  1217. {
  1218. stbtt_aligned_quad quad;
  1219. getBakedQuad(m_cdata, ch - 32, &_x, &_y, &quad);
  1220. vertex->m_x = quad.x0;
  1221. vertex->m_y = quad.y0;
  1222. vertex->m_u = quad.s0;
  1223. vertex->m_v = quad.t0;
  1224. vertex->m_abgr = _abgr;
  1225. ++vertex;
  1226. vertex->m_x = quad.x1;
  1227. vertex->m_y = quad.y1;
  1228. vertex->m_u = quad.s1;
  1229. vertex->m_v = quad.t1;
  1230. vertex->m_abgr = _abgr;
  1231. ++vertex;
  1232. vertex->m_x = quad.x1;
  1233. vertex->m_y = quad.y0;
  1234. vertex->m_u = quad.s1;
  1235. vertex->m_v = quad.t0;
  1236. vertex->m_abgr = _abgr;
  1237. ++vertex;
  1238. vertex->m_x = quad.x0;
  1239. vertex->m_y = quad.y0;
  1240. vertex->m_u = quad.s0;
  1241. vertex->m_v = quad.t0;
  1242. vertex->m_abgr = _abgr;
  1243. ++vertex;
  1244. vertex->m_x = quad.x0;
  1245. vertex->m_y = quad.y1;
  1246. vertex->m_u = quad.s0;
  1247. vertex->m_v = quad.t1;
  1248. vertex->m_abgr = _abgr;
  1249. ++vertex;
  1250. vertex->m_x = quad.x1;
  1251. vertex->m_y = quad.y1;
  1252. vertex->m_u = quad.s1;
  1253. vertex->m_v = quad.t1;
  1254. vertex->m_abgr = _abgr;
  1255. ++vertex;
  1256. }
  1257. ++_text;
  1258. }
  1259. bgfx::setTexture(0, u_texColor, m_fontTexture);
  1260. bgfx::setVertexBuffer(&tvb);
  1261. bgfx::setState(0
  1262. | BGFX_STATE_RGB_WRITE
  1263. | BGFX_STATE_ALPHA_WRITE
  1264. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  1265. );
  1266. bgfx::setProgram(m_textureProgram);
  1267. bgfx::setScissor(m_scissor);
  1268. bgfx::submit(m_view);
  1269. }
  1270. #endif // USE_NANOVG_FONT
  1271. }
  1272. void colorWheelWidget(float _rgb[3], bool _respectIndentation, bool _enabled)
  1273. {
  1274. m_widgetId++;
  1275. const uint32_t wheelId = (m_areaId << 16) | m_widgetId;
  1276. m_widgetId++;
  1277. const uint32_t triangleId = (m_areaId << 16) | m_widgetId;
  1278. const int32_t height = m_scrollAreaWidth - COLOR_WHEEL_PADDING;
  1279. const float heightf = float(height);
  1280. const float widthf = float(m_scrollAreaWidth - COLOR_WHEEL_PADDING);
  1281. const float xx = float( (_respectIndentation ? m_widgetX-SCROLL_AREA_PADDING : m_scrollAreaX) + COLOR_WHEEL_PADDING/2);
  1282. const float yy = float(m_widgetY);
  1283. m_widgetY += height + DEFAULT_SPACING;
  1284. const float ro = (widthf < heightf ? widthf : heightf) * 0.5f - 5.0f; // radiusOuter.
  1285. const float rd = 20.0f; // radiusDelta.
  1286. const float ri = ro - rd; // radiusInner.
  1287. const float aeps = 0.5f / ro; // Half a pixel arc length in radians (2pi cancels out).
  1288. const float center[2] = { xx + widthf*0.5f, yy + heightf*0.5f };
  1289. const float cmx = float(m_mx) - center[0];
  1290. const float cmy = float(m_my) - center[1];
  1291. const float aa[2] = { ri - 6.0f, 0.0f }; // Hue point.
  1292. const float bb[2] = { cosf(-120.0f/180.0f*NVG_PI) * aa[0], sinf(-120.0f/180.0f*NVG_PI) * aa[0] }; // Black point.
  1293. const float cc[2] = { cosf( 120.0f/180.0f*NVG_PI) * aa[0], sinf( 120.0f/180.0f*NVG_PI) * aa[0] }; // White point.
  1294. const float ca[2] = { aa[0] - cc[0], aa[1] - cc[1] };
  1295. const float lenCa = sqrtf(ca[0]*ca[0]+ca[1]*ca[1]);
  1296. const float invLenCa = 1.0f/lenCa;
  1297. const float dirCa[2] = { ca[0]*invLenCa, ca[1]*invLenCa };
  1298. float sel[2];
  1299. float hsv[3];
  1300. bx::rgbToHsv(hsv, _rgb);
  1301. if (_enabled)
  1302. {
  1303. if (m_leftPressed)
  1304. {
  1305. const float len = sqrtf(cmx*cmx + cmy*cmy);
  1306. if (len > ri)
  1307. {
  1308. if (len < ro)
  1309. {
  1310. setActive(wheelId);
  1311. }
  1312. }
  1313. else
  1314. {
  1315. setActive(triangleId);
  1316. }
  1317. }
  1318. if (m_leftReleased
  1319. && (isActive(wheelId) || isActive(triangleId) ) )
  1320. {
  1321. clearActive();
  1322. }
  1323. // Set hue.
  1324. if (m_left
  1325. && isActive(wheelId) )
  1326. {
  1327. hsv[0] = atan2f(cmy, cmx)/NVG_PI*0.5f;
  1328. if (hsv[0] < 0.0f)
  1329. {
  1330. hsv[0]+=1.0f;
  1331. }
  1332. }
  1333. }
  1334. if (_enabled
  1335. && m_left
  1336. && isActive(triangleId) )
  1337. {
  1338. float an = -hsv[0]*NVG_PI*2.0f;
  1339. float tmx = (cmx*cosf(an)-cmy*sinf(an) );
  1340. float tmy = (cmx*sinf(an)+cmy*cosf(an) );
  1341. if (pointInTriangle(tmx, tmy, aa[0], aa[1], bb[0], bb[1], cc[0], cc[1]) )
  1342. {
  1343. sel[0] = tmx;
  1344. sel[1] = tmy;
  1345. }
  1346. else
  1347. {
  1348. closestPointOnTriangle(sel[0], sel[1], tmx, tmy, aa[0], aa[1], bb[0], bb[1], cc[0], cc[1]);
  1349. }
  1350. }
  1351. else
  1352. {
  1353. ///
  1354. /// bb (black)
  1355. /// /\
  1356. /// / \
  1357. /// / \
  1358. /// / \
  1359. /// / \
  1360. /// / .sel \
  1361. /// / \
  1362. /// cc(white)/____.ss_______\aa (hue)
  1363. ///
  1364. const float ss[2] =
  1365. {
  1366. cc[0] + dirCa[0]*lenCa*hsv[1],
  1367. cc[1] + dirCa[1]*lenCa*hsv[1],
  1368. };
  1369. const float sb[2] = { bb[0]-ss[0], bb[1]-ss[1] };
  1370. const float lenSb = sqrtf(sb[0]*sb[0]+sb[1]*sb[1]);
  1371. const float invLenSb = 1.0f/lenSb;
  1372. const float dirSb[2] = { sb[0]*invLenSb, sb[1]*invLenSb };
  1373. sel[0] = cc[0] + dirCa[0]*lenCa*hsv[1] + dirSb[0]*lenSb*(1.0f - hsv[2]);
  1374. sel[1] = cc[1] + dirCa[1]*lenCa*hsv[1] + dirSb[1]*lenSb*(1.0f - hsv[2]);
  1375. }
  1376. float uu, vv, ww;
  1377. barycentric(uu, vv, ww
  1378. , aa[0], aa[1]
  1379. , bb[0], bb[1]
  1380. , cc[0], cc[1]
  1381. , sel[0], sel[1]
  1382. );
  1383. const float val = bx::fclamp(1.0f-vv, 0.0001f, 1.0f);
  1384. const float sat = bx::fclamp(uu/val, 0.0001f, 1.0f);
  1385. const float out[3] = { hsv[0], sat, val };
  1386. bx::hsvToRgb(_rgb, out);
  1387. // Draw widget.
  1388. nvgSave(m_nvg);
  1389. {
  1390. const float drawSaturation = _enabled ? 1.0f : 0.0f;
  1391. // Circle.
  1392. for (uint8_t ii = 0; ii < 6; ii++)
  1393. {
  1394. const float a0 = float(ii)/6.0f * 2.0f*NVG_PI - aeps;
  1395. const float a1 = float(ii+1.0f)/6.0f * 2.0f*NVG_PI + aeps;
  1396. nvgBeginPath(m_nvg);
  1397. nvgArc(m_nvg, center[0], center[1], ri, a0, a1, NVG_CW);
  1398. nvgArc(m_nvg, center[0], center[1], ro, a1, a0, NVG_CCW);
  1399. nvgClosePath(m_nvg);
  1400. const float ax = center[0] + cosf(a0) * (ri+ro)*0.5f;
  1401. const float ay = center[1] + sinf(a0) * (ri+ro)*0.5f;
  1402. const float bx = center[0] + cosf(a1) * (ri+ro)*0.5f;
  1403. const float by = center[1] + sinf(a1) * (ri+ro)*0.5f;
  1404. NVGpaint paint = nvgLinearGradient(m_nvg
  1405. , ax, ay
  1406. , bx, by
  1407. , nvgHSLA(a0/NVG_PI*0.5f,drawSaturation,0.55f,255)
  1408. , nvgHSLA(a1/NVG_PI*0.5f,drawSaturation,0.55f,255)
  1409. );
  1410. nvgFillPaint(m_nvg, paint);
  1411. nvgFill(m_nvg);
  1412. }
  1413. // Circle stroke.
  1414. nvgBeginPath(m_nvg);
  1415. nvgCircle(m_nvg, center[0], center[1], ri-0.5f);
  1416. nvgCircle(m_nvg, center[0], center[1], ro+0.5f);
  1417. nvgStrokeColor(m_nvg, nvgRGBA(0,0,0,64) );
  1418. nvgStrokeWidth(m_nvg, 1.0f);
  1419. nvgStroke(m_nvg);
  1420. nvgSave(m_nvg);
  1421. {
  1422. // Hue selector.
  1423. nvgTranslate(m_nvg, center[0], center[1]);
  1424. nvgRotate(m_nvg, hsv[0]*NVG_PI*2.0f);
  1425. nvgStrokeWidth(m_nvg, 2.0f);
  1426. nvgBeginPath(m_nvg);
  1427. nvgRect(m_nvg, ri-1.0f,-3.0f,rd+2.0f,6.0f);
  1428. nvgStrokeColor(m_nvg, nvgRGBA(255,255,255,192) );
  1429. nvgStroke(m_nvg);
  1430. // Hue selector drop shadow.
  1431. NVGpaint paint = nvgBoxGradient(m_nvg, ri-3.0f,-5.0f,ro-ri+6.0f,10.0f, 2.0f,4.0f, nvgRGBA(0,0,0,128), nvgRGBA(0,0,0,0) );
  1432. nvgBeginPath(m_nvg);
  1433. nvgRect(m_nvg, ri-2.0f-10.0f,-4.0f-10.0f,ro-ri+4.0f+20.0f,8.0f+20.0f);
  1434. nvgRect(m_nvg, ri-2.0f,-4.0f,ro-ri+4.0f,8.0f);
  1435. nvgPathWinding(m_nvg, NVG_HOLE);
  1436. nvgFillPaint(m_nvg, paint);
  1437. nvgFill(m_nvg);
  1438. // Center triangle stroke.
  1439. nvgBeginPath(m_nvg);
  1440. nvgMoveTo(m_nvg, aa[0], aa[1]);
  1441. nvgLineTo(m_nvg, bb[0], bb[1]);
  1442. nvgLineTo(m_nvg, cc[0], cc[1]);
  1443. nvgClosePath(m_nvg);
  1444. nvgStrokeColor(m_nvg, nvgRGBA(0,0,0,64) );
  1445. nvgStroke(m_nvg);
  1446. // Center triangle fill.
  1447. paint = nvgLinearGradient(m_nvg, aa[0], aa[1], bb[0], bb[1], nvgHSL(hsv[0],drawSaturation,0.5f), nvgRGBA(0,0,0,255) );
  1448. nvgFillPaint(m_nvg, paint);
  1449. nvgFill(m_nvg);
  1450. paint = nvgLinearGradient(m_nvg, (aa[0]+bb[0])*0.5f, (aa[1]+bb[1])*0.5f, cc[0], cc[1], nvgRGBA(0,0,0,0), nvgRGBA(255,255,255,255) );
  1451. nvgFillPaint(m_nvg, paint);
  1452. nvgFill(m_nvg);
  1453. // Color selector.
  1454. nvgStrokeWidth(m_nvg, 2.0f);
  1455. nvgBeginPath(m_nvg);
  1456. nvgCircle(m_nvg, sel[0], sel[1], 5);
  1457. nvgStrokeColor(m_nvg, nvgRGBA(255,255,255,192) );
  1458. nvgStroke(m_nvg);
  1459. // Color selector stroke.
  1460. paint = nvgRadialGradient(m_nvg, sel[0], sel[1], 7.0f, 9.0f, nvgRGBA(0,0,0,64), nvgRGBA(0,0,0,0) );
  1461. nvgBeginPath(m_nvg);
  1462. nvgRect(m_nvg, sel[0]-20.0f, sel[1]-20.0f, 40.0f, 40.0f);
  1463. nvgCircle(m_nvg, sel[0], sel[1], 7.0f);
  1464. nvgPathWinding(m_nvg, NVG_HOLE);
  1465. nvgFillPaint(m_nvg, paint);
  1466. nvgFill(m_nvg);
  1467. }
  1468. nvgRestore(m_nvg);
  1469. }
  1470. nvgRestore(m_nvg);
  1471. }
  1472. int32_t m_mx;
  1473. int32_t m_my;
  1474. int32_t m_scroll;
  1475. uint32_t m_active;
  1476. uint32_t m_hot;
  1477. uint32_t m_hotToBe;
  1478. int32_t m_dragX;
  1479. int32_t m_dragY;
  1480. float m_dragOrig;
  1481. int32_t m_widgetX;
  1482. int32_t m_widgetY;
  1483. int32_t m_widgetW;
  1484. bool m_left;
  1485. bool m_leftPressed;
  1486. bool m_leftReleased;
  1487. bool m_isHot;
  1488. bool m_isActive;
  1489. bool m_wentActive;
  1490. bool m_insideCurrentScroll;
  1491. uint32_t m_areaId;
  1492. uint32_t m_widgetId;
  1493. uint16_t m_scissor;
  1494. float m_tempCoords[MAX_TEMP_COORDS * 2];
  1495. float m_tempNormals[MAX_TEMP_COORDS * 2];
  1496. float m_circleVerts[NUM_CIRCLE_VERTS * 2];
  1497. int32_t m_scrollTop;
  1498. int32_t m_scrollBottom;
  1499. int32_t m_scrollRight;
  1500. int32_t m_scrollAreaTop;
  1501. int32_t m_scrollAreaWidth;
  1502. int32_t m_scrollAreaX;
  1503. int32_t* m_scrollVal;
  1504. int32_t m_focusTop;
  1505. int32_t m_focusBottom;
  1506. uint32_t m_scrollId;
  1507. bool m_insideScrollArea;
  1508. uint16_t m_textureWidth;
  1509. uint16_t m_textureHeight;
  1510. float m_invTextureWidth;
  1511. float m_invTextureHeight;
  1512. float m_halfTexel;
  1513. NVGcontext* m_nvg;
  1514. uint8_t m_view;
  1515. bgfx::UniformHandle u_texColor;
  1516. bgfx::ProgramHandle m_colorProgram;
  1517. bgfx::ProgramHandle m_textureProgram;
  1518. #if !USE_NANOVG_FONT
  1519. stbtt_bakedchar m_cdata[96]; // ASCII 32..126 is 95 glyphs
  1520. bgfx::TextureHandle m_fontTexture;
  1521. #endif // !USE_NANOVG_FONT
  1522. };
  1523. static Imgui s_imgui;
  1524. bool imguiCreate(const void* _data)
  1525. {
  1526. return s_imgui.create(_data);
  1527. }
  1528. void imguiDestroy()
  1529. {
  1530. s_imgui.destroy();
  1531. }
  1532. void imguiBeginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, uint16_t _width, uint16_t _height, uint8_t _view)
  1533. {
  1534. s_imgui.beginFrame(_mx, _my, _button, _scroll, _width, _height, _view);
  1535. }
  1536. void imguiEndFrame()
  1537. {
  1538. s_imgui.endFrame();
  1539. }
  1540. bool imguiBeginScrollArea(const char* _name, int32_t _x, int32_t _y, int32_t _width, int32_t _height, int32_t* _scroll)
  1541. {
  1542. return s_imgui.beginScrollArea(_name, _x, _y, _width, _height, _scroll);
  1543. }
  1544. void imguiEndScrollArea()
  1545. {
  1546. return s_imgui.endScrollArea();
  1547. }
  1548. void imguiIndent()
  1549. {
  1550. s_imgui.indent();
  1551. }
  1552. void imguiUnindent()
  1553. {
  1554. s_imgui.unindent();
  1555. }
  1556. void imguiSeparator()
  1557. {
  1558. s_imgui.separator();
  1559. }
  1560. void imguiSeparatorLine()
  1561. {
  1562. s_imgui.separatorLine();
  1563. }
  1564. bool imguiButton(const char* _text, bool _enabled)
  1565. {
  1566. return s_imgui.button(_text, _enabled);
  1567. }
  1568. bool imguiItem(const char* _text, bool _enabled)
  1569. {
  1570. return s_imgui.item(_text, _enabled);
  1571. }
  1572. bool imguiCheck(const char* _text, bool _checked, bool _enabled)
  1573. {
  1574. return s_imgui.check(_text, _checked, _enabled);
  1575. }
  1576. bool imguiCollapse(const char* _text, const char* _subtext, bool _checked, bool _enabled)
  1577. {
  1578. return s_imgui.collapse(_text, _subtext, _checked, _enabled);
  1579. }
  1580. void imguiLabel(const char* _format, ...)
  1581. {
  1582. va_list argList;
  1583. va_start(argList, _format);
  1584. s_imgui.labelVargs(_format, argList);
  1585. va_end(argList);
  1586. }
  1587. void imguiValue(const char* _text)
  1588. {
  1589. s_imgui.value(_text);
  1590. }
  1591. bool imguiSlider(const char* _text, float& _val, float _vmin, float _vmax, float _vinc, bool _enabled)
  1592. {
  1593. return s_imgui.slider(_text, _val, _vmin, _vmax, _vinc, _enabled);
  1594. }
  1595. bool imguiSlider(const char* _text, int32_t& _val, int32_t _vmin, int32_t _vmax, bool _enabled)
  1596. {
  1597. float val = (float)_val;
  1598. bool result = s_imgui.slider(_text, val, (float)_vmin, (float)_vmax, 1.0f, _enabled);
  1599. _val = (int32_t)val;
  1600. return result;
  1601. }
  1602. uint32_t imguiChooseUseMacroInstead(uint32_t _selected, ...)
  1603. {
  1604. va_list argList;
  1605. va_start(argList, _selected);
  1606. const char* str = va_arg(argList, const char*);
  1607. for (uint32_t ii = 0; str != NULL; ++ii, str = va_arg(argList, const char*) )
  1608. {
  1609. if (imguiCheck(str, ii == _selected) )
  1610. {
  1611. _selected = ii;
  1612. }
  1613. }
  1614. va_end(argList);
  1615. return _selected;
  1616. }
  1617. void imguiDrawText(int32_t _x, int32_t _y, ImguiTextAlign::Enum _align, const char* _text, uint32_t _argb)
  1618. {
  1619. s_imgui.drawText(_x, _y, _align, _text, _argb);
  1620. }
  1621. void imguiDrawLine(float _x0, float _y0, float _x1, float _y1, float _r, uint32_t _argb)
  1622. {
  1623. s_imgui.drawLine(_x0, _y0, _x1, _y1, _r, _argb);
  1624. }
  1625. void imguiDrawRoundedRect(float _x, float _y, float _width, float _height, float _r, uint32_t _argb)
  1626. {
  1627. s_imgui.drawRoundedRect(_x, _y, _width, _height, _r, _argb);
  1628. }
  1629. void imguiDrawRect(float _x, float _y, float _width, float _height, uint32_t _argb)
  1630. {
  1631. s_imgui.drawRect(_x, _y, _width, _height, _argb);
  1632. }
  1633. int imguiReserve(int _y)
  1634. {
  1635. const int yy = s_imgui.m_widgetY;
  1636. s_imgui.m_widgetY += _y;
  1637. return yy;
  1638. }
  1639. void imguiBool(const char* _text, bool& _flag, bool _enabled)
  1640. {
  1641. if (imguiCheck(_text, _flag, _enabled) )
  1642. {
  1643. _flag = !_flag;
  1644. }
  1645. }
  1646. void imguiColorWheel(float _rgb[3], bool _respectIndentation, bool _enabled)
  1647. {
  1648. s_imgui.colorWheelWidget(_rgb, _respectIndentation, _enabled);
  1649. }
  1650. void imguiColorWheel(const char* _text, float _rgb[3], bool& _activated, bool _enabled)
  1651. {
  1652. char buf[128];
  1653. bx::snprintf(buf, sizeof(buf), "%s [RGB %-2.2f %-2.2f %-2.2f]"
  1654. , _text
  1655. , _rgb[0]
  1656. , _rgb[1]
  1657. , _rgb[2]
  1658. );
  1659. if (imguiButton(buf, true) )
  1660. {
  1661. _activated = !_activated;
  1662. }
  1663. if (_activated)
  1664. {
  1665. imguiColorWheel(_rgb, false, _enabled);
  1666. }
  1667. }