imgui.cpp 46 KB

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