imgui.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009
  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. if (_enabled)
  1385. {
  1386. if (m_leftPressed)
  1387. {
  1388. const float len = sqrtf(cmx*cmx+cmy*cmy);
  1389. if (len > ri)
  1390. {
  1391. if (len < ro)
  1392. {
  1393. setActive(wheelId);
  1394. }
  1395. }
  1396. else
  1397. {
  1398. setActive(triangleId);
  1399. }
  1400. }
  1401. if (m_leftReleased
  1402. && (isActive(wheelId) || isActive(triangleId) ) )
  1403. {
  1404. clearActive();
  1405. }
  1406. // Set hue.
  1407. if (m_left && isActive(wheelId))
  1408. {
  1409. hsv[0] = atan2f(cmy, cmx)/NVG_PI*0.5f;
  1410. if (hsv[0] < 0.0f)
  1411. {
  1412. hsv[0]+=1.0f;
  1413. }
  1414. }
  1415. }
  1416. if (_enabled && m_left && isActive(triangleId))
  1417. {
  1418. float an = -hsv[0]*NVG_PI*2.0f;
  1419. float tmx = (cmx*cosf(an)-cmy*sinf(an));
  1420. float tmy = (cmx*sinf(an)+cmy*cosf(an));
  1421. if (pointInTriangle(tmx, tmy, aa[0], aa[1], bb[0], bb[1], cc[0], cc[1]))
  1422. {
  1423. sel[0] = tmx;
  1424. sel[1] = tmy;
  1425. }
  1426. else
  1427. {
  1428. closestPointOnTriangle(sel[0], sel[1], tmx, tmy, aa[0], aa[1], bb[0], bb[1], cc[0], cc[1]);
  1429. }
  1430. }
  1431. else
  1432. {
  1433. ///
  1434. /// bb (black)
  1435. /// /\
  1436. /// / \
  1437. /// / \
  1438. /// / \
  1439. /// / \
  1440. /// / .sel \
  1441. /// / \
  1442. /// cc(white)/____.ss_______\aa (hue)
  1443. ///
  1444. const float ss[2] =
  1445. {
  1446. cc[0] + dirCa[0]*lenCa*hsv[1],
  1447. cc[1] + dirCa[1]*lenCa*hsv[1],
  1448. };
  1449. const float sb[2] = { bb[0]-ss[0], bb[1]-ss[1] };
  1450. const float lenSb = sqrtf(sb[0]*sb[0]+sb[1]*sb[1]);
  1451. const float invLenSb = 1.0f/lenSb;
  1452. const float dirSb[2] = { sb[0]*invLenSb, sb[1]*invLenSb };
  1453. sel[0] = cc[0] + dirCa[0]*lenCa*hsv[1] + dirSb[0]*lenSb*(1.0f - hsv[2]);
  1454. sel[1] = cc[1] + dirCa[1]*lenCa*hsv[1] + dirSb[1]*lenSb*(1.0f - hsv[2]);
  1455. }
  1456. float uu, vv, ww;
  1457. barycentric(uu, vv, ww
  1458. , aa[0], aa[1]
  1459. , bb[0], bb[1]
  1460. , cc[0], cc[1]
  1461. , sel[0], sel[1]
  1462. );
  1463. const float val = clampf(1.0f-vv, 0.0001f, 1.0f);
  1464. const float sat = clampf(uu/val, 0.0001f, 1.0f);
  1465. const float out[3] = { hsv[0], sat, val };
  1466. hsvToRgb(_color, out);
  1467. // Draw widget.
  1468. nvgSave(m_nvg);
  1469. const float drawSaturation = _enabled ? 1.0f : 0.0f;
  1470. // Circle.
  1471. for (uint8_t ii = 0; ii < 6; ii++)
  1472. {
  1473. const float a0 = float(ii)/6.0f * 2.0f*NVG_PI - aeps;
  1474. const float a1 = float(ii+1.0f)/6.0f * 2.0f*NVG_PI + aeps;
  1475. nvgBeginPath(m_nvg);
  1476. nvgArc(m_nvg, center[0], center[1], ri, a0, a1, NVG_CW);
  1477. nvgArc(m_nvg, center[0], center[1], ro, a1, a0, NVG_CCW);
  1478. nvgClosePath(m_nvg);
  1479. const float ax = center[0] + cosf(a0) * (ri+ro)*0.5f;
  1480. const float ay = center[1] + sinf(a0) * (ri+ro)*0.5f;
  1481. const float bx = center[0] + cosf(a1) * (ri+ro)*0.5f;
  1482. const float by = center[1] + sinf(a1) * (ri+ro)*0.5f;
  1483. NVGpaint paint = nvgLinearGradient(m_nvg
  1484. , ax, ay
  1485. , bx, by
  1486. , nvgHSLA(a0/NVG_PI*0.5f,drawSaturation,0.55f,255)
  1487. , nvgHSLA(a1/NVG_PI*0.5f,drawSaturation,0.55f,255)
  1488. );
  1489. nvgFillPaint(m_nvg, paint);
  1490. nvgFill(m_nvg);
  1491. }
  1492. // Circle stroke.
  1493. nvgBeginPath(m_nvg);
  1494. nvgCircle(m_nvg, center[0], center[1], ri-0.5f);
  1495. nvgCircle(m_nvg, center[0], center[1], ro+0.5f);
  1496. nvgStrokeColor(m_nvg, nvgRGBA(0,0,0,64));
  1497. nvgStrokeWidth(m_nvg, 1.0f);
  1498. nvgStroke(m_nvg);
  1499. nvgSave(m_nvg);
  1500. {
  1501. // Hue selector.
  1502. nvgTranslate(m_nvg, center[0], center[1]);
  1503. nvgRotate(m_nvg, hsv[0]*NVG_PI*2.0f);
  1504. nvgStrokeWidth(m_nvg, 2.0f);
  1505. nvgBeginPath(m_nvg);
  1506. nvgRect(m_nvg, ri-1.0f,-3.0f,rd+2.0f,6.0f);
  1507. nvgStrokeColor(m_nvg, nvgRGBA(255,255,255,192));
  1508. nvgStroke(m_nvg);
  1509. // Hue selector drop shadow.
  1510. 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));
  1511. nvgBeginPath(m_nvg);
  1512. nvgRect(m_nvg, ri-2.0f-10.0f,-4.0f-10.0f,ro-ri+4.0f+20.0f,8.0f+20.0f);
  1513. nvgRect(m_nvg, ri-2.0f,-4.0f,ro-ri+4.0f,8.0f);
  1514. nvgPathWinding(m_nvg, NVG_HOLE);
  1515. nvgFillPaint(m_nvg, paint);
  1516. nvgFill(m_nvg);
  1517. // Center triangle stroke.
  1518. nvgBeginPath(m_nvg);
  1519. nvgMoveTo(m_nvg, aa[0], aa[1]);
  1520. nvgLineTo(m_nvg, bb[0], bb[1]);
  1521. nvgLineTo(m_nvg, cc[0], cc[1]);
  1522. nvgClosePath(m_nvg);
  1523. nvgStrokeColor(m_nvg, nvgRGBA(0,0,0,64));
  1524. nvgStroke(m_nvg);
  1525. // Center triangle fill.
  1526. paint = nvgLinearGradient(m_nvg, aa[0], aa[1], bb[0], bb[1], nvgHSL(hsv[0],drawSaturation,0.5f), nvgRGBA(0,0,0,255));
  1527. nvgFillPaint(m_nvg, paint);
  1528. nvgFill(m_nvg);
  1529. 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));
  1530. nvgFillPaint(m_nvg, paint);
  1531. nvgFill(m_nvg);
  1532. // Color selector.
  1533. nvgStrokeWidth(m_nvg, 2.0f);
  1534. nvgBeginPath(m_nvg);
  1535. nvgCircle(m_nvg, sel[0], sel[1], 5);
  1536. nvgStrokeColor(m_nvg, nvgRGBA(255,255,255,192));
  1537. nvgStroke(m_nvg);
  1538. // Color selector stroke.
  1539. paint = nvgRadialGradient(m_nvg, sel[0], sel[1], 7.0f, 9.0f, nvgRGBA(0,0,0,64), nvgRGBA(0,0,0,0));
  1540. nvgBeginPath(m_nvg);
  1541. nvgRect(m_nvg, sel[0]-20.0f, sel[1]-20.0f, 40.0f, 40.0f);
  1542. nvgCircle(m_nvg, sel[0], sel[1], 7.0f);
  1543. nvgPathWinding(m_nvg, NVG_HOLE);
  1544. nvgFillPaint(m_nvg, paint);
  1545. nvgFill(m_nvg);
  1546. }
  1547. nvgRestore(m_nvg);
  1548. nvgRestore(m_nvg);
  1549. }
  1550. int32_t m_mx;
  1551. int32_t m_my;
  1552. int32_t m_scroll;
  1553. uint32_t m_active;
  1554. uint32_t m_hot;
  1555. uint32_t m_hotToBe;
  1556. int32_t m_dragX;
  1557. int32_t m_dragY;
  1558. float m_dragOrig;
  1559. int32_t m_widgetX;
  1560. int32_t m_widgetY;
  1561. int32_t m_widgetW;
  1562. bool m_left;
  1563. bool m_leftPressed;
  1564. bool m_leftReleased;
  1565. bool m_isHot;
  1566. bool m_isActive;
  1567. bool m_wentActive;
  1568. bool m_insideCurrentScroll;
  1569. uint32_t m_areaId;
  1570. uint32_t m_widgetId;
  1571. uint16_t m_scissor;
  1572. float m_tempCoords[MAX_TEMP_COORDS * 2];
  1573. float m_tempNormals[MAX_TEMP_COORDS * 2];
  1574. float m_circleVerts[NUM_CIRCLE_VERTS * 2];
  1575. int32_t m_scrollTop;
  1576. int32_t m_scrollBottom;
  1577. int32_t m_scrollRight;
  1578. int32_t m_scrollAreaTop;
  1579. int32_t m_scrollAreaWidth;
  1580. int32_t m_scrollAreaX;
  1581. int32_t* m_scrollVal;
  1582. int32_t m_focusTop;
  1583. int32_t m_focusBottom;
  1584. uint32_t m_scrollId;
  1585. bool m_insideScrollArea;
  1586. stbtt_bakedchar m_cdata[96]; // ASCII 32..126 is 95 glyphs
  1587. uint16_t m_textureWidth;
  1588. uint16_t m_textureHeight;
  1589. float m_invTextureWidth;
  1590. float m_invTextureHeight;
  1591. float m_halfTexel;
  1592. NVGcontext* m_nvg;
  1593. uint8_t m_view;
  1594. bgfx::UniformHandle u_texColor;
  1595. bgfx::TextureHandle m_fontTexture;
  1596. bgfx::ProgramHandle m_colorProgram;
  1597. bgfx::ProgramHandle m_textureProgram;
  1598. };
  1599. static Imgui s_imgui;
  1600. bool imguiCreate(const void* _data)
  1601. {
  1602. return s_imgui.create(_data);
  1603. }
  1604. void imguiDestroy()
  1605. {
  1606. s_imgui.destroy();
  1607. }
  1608. void imguiBeginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, uint16_t _width, uint16_t _height, uint8_t _view)
  1609. {
  1610. s_imgui.beginFrame(_mx, _my, _button, _scroll, _width, _height, _view);
  1611. }
  1612. void imguiEndFrame()
  1613. {
  1614. s_imgui.endFrame();
  1615. }
  1616. bool imguiBeginScrollArea(const char* _name, int32_t _x, int32_t _y, int32_t _width, int32_t _height, int32_t* _scroll, NVGcontext* _nvg)
  1617. {
  1618. return s_imgui.beginScrollArea(_name, _x, _y, _width, _height, _scroll, _nvg);
  1619. }
  1620. void imguiEndScrollArea()
  1621. {
  1622. return s_imgui.endScrollArea();
  1623. }
  1624. void imguiIndent()
  1625. {
  1626. s_imgui.indent();
  1627. }
  1628. void imguiUnindent()
  1629. {
  1630. s_imgui.unindent();
  1631. }
  1632. void imguiSeparator()
  1633. {
  1634. s_imgui.separator();
  1635. }
  1636. void imguiSeparatorLine()
  1637. {
  1638. s_imgui.separatorLine();
  1639. }
  1640. bool imguiButton(const char* _text, bool _enabled)
  1641. {
  1642. return s_imgui.button(_text, _enabled);
  1643. }
  1644. bool imguiItem(const char* _text, bool _enabled)
  1645. {
  1646. return s_imgui.item(_text, _enabled);
  1647. }
  1648. bool imguiCheck(const char* _text, bool _checked, bool _enabled)
  1649. {
  1650. return s_imgui.check(_text, _checked, _enabled);
  1651. }
  1652. bool imguiCollapse(const char* _text, const char* _subtext, bool _checked, bool _enabled)
  1653. {
  1654. return s_imgui.collapse(_text, _subtext, _checked, _enabled);
  1655. }
  1656. void imguiLabel(const char* _format, ...)
  1657. {
  1658. va_list argList;
  1659. va_start(argList, _format);
  1660. s_imgui.labelVargs(_format, argList);
  1661. va_end(argList);
  1662. }
  1663. void imguiValue(const char* _text)
  1664. {
  1665. s_imgui.value(_text);
  1666. }
  1667. bool imguiSlider(const char* _text, float* _val, float _vmin, float _vmax, float _vinc, bool _enabled)
  1668. {
  1669. return s_imgui.slider(_text, _val, _vmin, _vmax, _vinc, _enabled);
  1670. }
  1671. bool imguiSlider(const char* _text, int32_t* _val, int32_t _vmin, int32_t _vmax, bool _enabled)
  1672. {
  1673. float val = (float)*_val;
  1674. bool result = s_imgui.slider(_text, &val, (float)_vmin, (float)_vmax, 1.0f, _enabled);
  1675. *_val = (int32_t)val;
  1676. return result;
  1677. }
  1678. uint32_t imguiChooseUseMacroInstead(uint32_t _selected, ...)
  1679. {
  1680. va_list argList;
  1681. va_start(argList, _selected);
  1682. const char* str = va_arg(argList, const char*);
  1683. for (uint32_t ii = 0; str != NULL; ++ii, str = va_arg(argList, const char*))
  1684. {
  1685. if (imguiCheck(str, ii == _selected) )
  1686. {
  1687. _selected = ii;
  1688. }
  1689. }
  1690. va_end(argList);
  1691. return _selected;
  1692. }
  1693. void imguiDrawText(int32_t _x, int32_t _y, ImguiTextAlign::Enum _align, const char* _text, uint32_t _argb)
  1694. {
  1695. s_imgui.drawText(_x, _y, _align, _text, _argb);
  1696. }
  1697. void imguiDrawLine(float _x0, float _y0, float _x1, float _y1, float _r, uint32_t _argb)
  1698. {
  1699. s_imgui.drawLine(_x0, _y0, _x1, _y1, _r, _argb);
  1700. }
  1701. void imguiDrawRoundedRect(float _x, float _y, float _width, float _height, float _r, uint32_t _argb)
  1702. {
  1703. s_imgui.drawRoundedRect(_x, _y, _width, _height, _r, _argb);
  1704. }
  1705. void imguiDrawRect(float _x, float _y, float _width, float _height, uint32_t _argb)
  1706. {
  1707. s_imgui.drawRect(_x, _y, _width, _height, _argb);
  1708. }
  1709. int imguiReserve(int _y)
  1710. {
  1711. const int yy = s_imgui.m_widgetY;
  1712. s_imgui.m_widgetY += _y;
  1713. return yy;
  1714. }
  1715. void imguiColorWheel(float _color[3], bool _respectIndentation, bool _enabled)
  1716. {
  1717. s_imgui.colorWheelWidget(_color, _respectIndentation, _enabled);
  1718. }