imgui.cpp 46 KB

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