imgui.cpp 46 KB

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