imgui.cpp 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559
  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 <bgfx.h>
  28. #include "../entry/dbg.h"
  29. #include "imgui.h"
  30. #include "../fpumath.h"
  31. #include "vs_imgui_color.bin.h"
  32. #include "fs_imgui_color.bin.h"
  33. #include "vs_imgui_texture.bin.h"
  34. #include "fs_imgui_texture.bin.h"
  35. #define MAX_TEMP_COORDS 100
  36. #define NUM_CIRCLE_VERTS (8 * 4)
  37. static const int32_t BUTTON_HEIGHT = 20;
  38. static const int32_t SLIDER_HEIGHT = 20;
  39. static const int32_t SLIDER_MARKER_WIDTH = 10;
  40. static const int32_t CHECK_SIZE = 8;
  41. static const int32_t DEFAULT_SPACING = 4;
  42. static const int32_t TEXT_HEIGHT = 8;
  43. static const int32_t SCROLL_AREA_PADDING = 6;
  44. static const int32_t INDENT_SIZE = 16;
  45. static const int32_t AREA_HEADER = 28;
  46. static const float s_tabStops[4] = {150, 210, 270, 330};
  47. static void* imguiMalloc(size_t size, void* /*_userptr*/)
  48. {
  49. return malloc(size);
  50. }
  51. static void imguiFree(void* _ptr, void* /*_userptr*/)
  52. {
  53. free(_ptr);
  54. }
  55. #define STBTT_malloc(_x, _y) imguiMalloc(_x, _y)
  56. #define STBTT_free(_x, _y) imguiFree(_x, _y)
  57. #define STB_TRUETYPE_IMPLEMENTATION
  58. #include <stb_truetype/stb_truetype.h>
  59. namespace
  60. {
  61. struct PosColorVertex
  62. {
  63. float m_x;
  64. float m_y;
  65. uint32_t m_abgr;
  66. static void init()
  67. {
  68. ms_decl.begin();
  69. ms_decl.add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float);
  70. ms_decl.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true);
  71. ms_decl.end();
  72. }
  73. static bgfx::VertexDecl ms_decl;
  74. };
  75. bgfx::VertexDecl PosColorVertex::ms_decl;
  76. struct PosColorUvVertex
  77. {
  78. float m_x;
  79. float m_y;
  80. float m_u;
  81. float m_v;
  82. uint32_t m_abgr;
  83. static void init()
  84. {
  85. ms_decl.begin();
  86. ms_decl.add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float);
  87. ms_decl.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float);
  88. ms_decl.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true);
  89. ms_decl.end();
  90. }
  91. static bgfx::VertexDecl ms_decl;
  92. };
  93. bgfx::VertexDecl PosColorUvVertex::ms_decl;
  94. } // namespace
  95. struct Imgui
  96. {
  97. Imgui()
  98. : m_mx(-1)
  99. , m_my(-1)
  100. , m_scroll(0)
  101. , m_active(0)
  102. , m_hot(0)
  103. , m_hotToBe(0)
  104. , m_dragX(0)
  105. , m_dragY(0)
  106. , m_dragOrig(0)
  107. , m_widgetX(0)
  108. , m_widgetY(0)
  109. , m_widgetW(100)
  110. , m_left(false)
  111. , m_leftPressed(false)
  112. , m_leftReleased(false)
  113. , m_isHot(false)
  114. , m_isActive(false)
  115. , m_wentActive(false)
  116. , m_insideCurrentScroll(false)
  117. , m_areaId(0)
  118. , m_widgetId(0)
  119. , m_scissor(UINT16_MAX)
  120. , m_scrollTop(0)
  121. , m_scrollBottom(0)
  122. , m_scrollRight(0)
  123. , m_scrollAreaTop(0)
  124. , m_scrollVal(NULL)
  125. , m_focusTop(0)
  126. , m_focusBottom(0)
  127. , m_scrollId(0)
  128. , m_insideScrollArea(false)
  129. , m_textureWidth(512)
  130. , m_textureHeight(512)
  131. , m_halfTexel(0.0f)
  132. , m_view(31)
  133. {
  134. m_invTextureWidth = 1.0f/m_textureWidth;
  135. m_invTextureHeight = 1.0f/m_textureHeight;
  136. u_texColor.idx = bgfx::invalidHandle;
  137. m_fontTexture.idx = bgfx::invalidHandle;
  138. m_colorProgram.idx = bgfx::invalidHandle;
  139. m_textureProgram.idx = bgfx::invalidHandle;
  140. }
  141. bool create(const void* _data, uint32_t /*_size*/)
  142. {
  143. for (int32_t ii = 0; ii < NUM_CIRCLE_VERTS; ++ii)
  144. {
  145. float a = (float)ii / (float)NUM_CIRCLE_VERTS * (float)(M_PI * 2.0);
  146. m_circleVerts[ii * 2 + 0] = cosf(a);
  147. m_circleVerts[ii * 2 + 1] = sinf(a);
  148. }
  149. PosColorVertex::init();
  150. PosColorUvVertex::init();
  151. u_texColor = bgfx::createUniform("u_texColor", bgfx::UniformType::Uniform1i);
  152. const bgfx::Memory* vs_imgui_color;
  153. const bgfx::Memory* fs_imgui_color;
  154. const bgfx::Memory* vs_imgui_texture;
  155. const bgfx::Memory* fs_imgui_texture;
  156. switch (bgfx::getRendererType() )
  157. {
  158. case bgfx::RendererType::Direct3D9:
  159. vs_imgui_color = bgfx::makeRef(vs_imgui_color_dx9, sizeof(vs_imgui_color_dx9) );
  160. fs_imgui_color = bgfx::makeRef(fs_imgui_color_dx9, sizeof(fs_imgui_color_dx9) );
  161. vs_imgui_texture = bgfx::makeRef(vs_imgui_texture_dx9, sizeof(vs_imgui_texture_dx9) );
  162. fs_imgui_texture = bgfx::makeRef(fs_imgui_texture_dx9, sizeof(fs_imgui_texture_dx9) );
  163. m_halfTexel = 0.5f;
  164. break;
  165. case bgfx::RendererType::Direct3D11:
  166. vs_imgui_color = bgfx::makeRef(vs_imgui_color_dx11, sizeof(vs_imgui_color_dx11) );
  167. fs_imgui_color = bgfx::makeRef(fs_imgui_color_dx11, sizeof(fs_imgui_color_dx11) );
  168. vs_imgui_texture = bgfx::makeRef(vs_imgui_texture_dx11, sizeof(vs_imgui_texture_dx11) );
  169. fs_imgui_texture = bgfx::makeRef(fs_imgui_texture_dx11, sizeof(fs_imgui_texture_dx11) );
  170. break;
  171. default:
  172. vs_imgui_color = bgfx::makeRef(vs_imgui_color_glsl, sizeof(vs_imgui_color_glsl) );
  173. fs_imgui_color = bgfx::makeRef(fs_imgui_color_glsl, sizeof(fs_imgui_color_glsl) );
  174. vs_imgui_texture = bgfx::makeRef(vs_imgui_texture_glsl, sizeof(vs_imgui_texture_glsl) );
  175. fs_imgui_texture = bgfx::makeRef(fs_imgui_texture_glsl, sizeof(fs_imgui_texture_glsl) );
  176. break;
  177. }
  178. bgfx::VertexShaderHandle vsh;
  179. bgfx::FragmentShaderHandle fsh;
  180. vsh = bgfx::createVertexShader(vs_imgui_color);
  181. fsh = bgfx::createFragmentShader(fs_imgui_color);
  182. m_colorProgram = bgfx::createProgram(vsh, fsh);
  183. bgfx::destroyVertexShader(vsh);
  184. bgfx::destroyFragmentShader(fsh);
  185. vsh = bgfx::createVertexShader(vs_imgui_texture);
  186. fsh = bgfx::createFragmentShader(fs_imgui_texture);
  187. m_textureProgram = bgfx::createProgram(vsh, fsh);
  188. bgfx::destroyVertexShader(vsh);
  189. bgfx::destroyFragmentShader(fsh);
  190. const bgfx::Memory* mem = bgfx::alloc(m_textureWidth * m_textureHeight);
  191. stbtt_BakeFontBitmap( (uint8_t*)_data, 0, 15.0f, mem->data, m_textureWidth, m_textureHeight, 32, 96, m_cdata);
  192. m_fontTexture = bgfx::createTexture2D(m_textureWidth, m_textureHeight, 1, bgfx::TextureFormat::R8, BGFX_TEXTURE_NONE, mem);
  193. return true;
  194. }
  195. void destroy()
  196. {
  197. bgfx::destroyUniform(u_texColor);
  198. bgfx::destroyTexture(m_fontTexture);
  199. bgfx::destroyProgram(m_colorProgram);
  200. bgfx::destroyProgram(m_textureProgram);
  201. }
  202. bool anyActive() const
  203. {
  204. return m_active != 0;
  205. }
  206. bool isActive(uint32_t _id) const
  207. {
  208. return m_active == _id;
  209. }
  210. bool isHot(uint32_t _id) const
  211. {
  212. return m_hot == _id;
  213. }
  214. bool inRect(int32_t _x, int32_t _y, int32_t _width, int32_t _height, bool _checkScroll = true) const
  215. {
  216. return (!_checkScroll || m_insideCurrentScroll)
  217. && m_mx >= _x
  218. && m_mx <= _x + _width
  219. && m_my >= _y
  220. && m_my <= _y + _height;
  221. }
  222. void clearInput()
  223. {
  224. m_leftPressed = false;
  225. m_leftReleased = false;
  226. m_scroll = 0;
  227. }
  228. void clearActive()
  229. {
  230. m_active = 0;
  231. // mark all UI for this frame as processed
  232. clearInput();
  233. }
  234. void setActive(uint32_t _id)
  235. {
  236. m_active = _id;
  237. m_wentActive = true;
  238. }
  239. void setHot(uint32_t _id)
  240. {
  241. m_hotToBe = _id;
  242. }
  243. bool buttonLogic(uint32_t _id, bool _over)
  244. {
  245. bool res = false;
  246. // process down
  247. if (!anyActive() )
  248. {
  249. if (_over)
  250. {
  251. setHot(_id);
  252. }
  253. if (isHot(_id)
  254. && m_leftPressed)
  255. {
  256. setActive(_id);
  257. }
  258. }
  259. // if button is active, then react on left up
  260. if (isActive(_id) )
  261. {
  262. m_isActive = true;
  263. if (_over)
  264. {
  265. setHot(_id);
  266. }
  267. if (m_leftReleased)
  268. {
  269. if (isHot(_id) )
  270. {
  271. res = true;
  272. }
  273. clearActive();
  274. }
  275. }
  276. if (isHot(_id) )
  277. {
  278. m_isHot = true;
  279. }
  280. return res;
  281. }
  282. void updateInput(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll)
  283. {
  284. bool left = (_button & IMGUI_MBUT_LEFT) != 0;
  285. m_mx = _mx;
  286. m_my = _my;
  287. m_leftPressed = !m_left && left;
  288. m_leftReleased = m_left && !left;
  289. m_left = left;
  290. m_scroll = _scroll;
  291. }
  292. void beginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, uint16_t _width, uint16_t _height, uint8_t _view)
  293. {
  294. m_view = _view;
  295. bgfx::setViewSeq(_view, true);
  296. bgfx::setViewRect(_view, 0, 0, _width, _height);
  297. float proj[16];
  298. mtxOrtho(proj, 0.0f, (float)_width, (float)_height, 0.0f, 0.0f, 1000.0f);
  299. bgfx::setViewTransform(_view, NULL, proj);
  300. updateInput(_mx, _my, _button, _scroll);
  301. m_hot = m_hotToBe;
  302. m_hotToBe = 0;
  303. m_wentActive = false;
  304. m_isActive = false;
  305. m_isHot = false;
  306. m_widgetX = 0;
  307. m_widgetY = 0;
  308. m_widgetW = 0;
  309. m_areaId = 1;
  310. m_widgetId = 1;
  311. }
  312. void endFrame()
  313. {
  314. clearInput();
  315. }
  316. bool beginScrollArea(const char* _name, int32_t _x, int32_t _y, int32_t _width, int32_t _height, int32_t* _scroll)
  317. {
  318. m_areaId++;
  319. m_widgetId = 0;
  320. m_scrollId = (m_areaId << 16) | m_widgetId;
  321. m_widgetX = _x + SCROLL_AREA_PADDING;
  322. m_widgetY = _y + + AREA_HEADER + (*_scroll);
  323. m_widgetW = _width - SCROLL_AREA_PADDING * 4;
  324. m_scrollTop = _y + SCROLL_AREA_PADDING;
  325. m_scrollBottom = _y - AREA_HEADER + _height;
  326. m_scrollRight = _x + _width - SCROLL_AREA_PADDING * 3;
  327. m_scrollVal = _scroll;
  328. m_scrollAreaTop = m_widgetY;
  329. m_focusTop = _y - AREA_HEADER;
  330. m_focusBottom = _y - AREA_HEADER + _height;
  331. m_insideScrollArea = inRect(_x, _y, _width, _height, false);
  332. m_insideCurrentScroll = m_insideScrollArea;
  333. drawRoundedRect( (float)_x
  334. , (float)_y
  335. , (float)_width
  336. , (float)_height
  337. , 6
  338. , imguiRGBA(0, 0, 0, 192)
  339. );
  340. drawText(_x + AREA_HEADER / 2
  341. , _y + AREA_HEADER / 2
  342. , ImguiTextAlign::Left
  343. , _name
  344. , imguiRGBA(255, 255, 255, 128)
  345. );
  346. m_scissor = bgfx::setScissor(_x + SCROLL_AREA_PADDING, _y + SCROLL_AREA_PADDING, _width - SCROLL_AREA_PADDING * 4, _height - AREA_HEADER - SCROLL_AREA_PADDING);
  347. return m_insideScrollArea;
  348. }
  349. void endScrollArea()
  350. {
  351. // Disable scissoring.
  352. m_scissor = UINT16_MAX;
  353. // Draw scroll bar
  354. int32_t xx = m_scrollRight + SCROLL_AREA_PADDING / 2;
  355. int32_t yy = m_scrollTop;
  356. int32_t width = SCROLL_AREA_PADDING * 2;
  357. int32_t height = m_scrollBottom - m_scrollTop;
  358. int32_t stop = m_scrollAreaTop;
  359. int32_t sbot = m_widgetY;
  360. int32_t sh = sbot - stop; // The scrollable area height.
  361. float barHeight = (float)height / (float)sh;
  362. if (barHeight < 1)
  363. {
  364. float barY = (float)(yy - sbot) / (float)sh;
  365. if (barY < 0)
  366. {
  367. barY = 0;
  368. }
  369. if (barY > 1)
  370. {
  371. barY = 1;
  372. }
  373. // Handle scroll bar logic.
  374. uint32_t hid = m_scrollId;
  375. int32_t hx = xx;
  376. int32_t hy = yy + (int)(barY * height);
  377. int32_t hw = width;
  378. int32_t hh = (int)(barHeight * height);
  379. const int32_t range = height - (hh - 1);
  380. bool over = inRect(hx, hy, hw, hh);
  381. buttonLogic(hid, over);
  382. if (isActive(hid) )
  383. {
  384. float u = (float)(hy - yy) / (float)range;
  385. if (m_wentActive)
  386. {
  387. m_dragY = m_my;
  388. m_dragOrig = u;
  389. }
  390. if (m_dragY != m_my)
  391. {
  392. u = m_dragOrig + (m_my - m_dragY) / (float)range;
  393. if (u < 0)
  394. {
  395. u = 0;
  396. }
  397. if (u > 1)
  398. {
  399. u = 1;
  400. }
  401. *m_scrollVal = (int)( (1 - u) * (sh - height) );
  402. }
  403. }
  404. // BG
  405. drawRoundedRect( (float)xx
  406. , (float)yy
  407. , (float)width
  408. , (float)height
  409. , (float)width / 2 - 1
  410. , imguiRGBA(0, 0, 0, 196)
  411. );
  412. // Bar
  413. if (isActive(hid) )
  414. {
  415. drawRoundedRect( (float)hx
  416. , (float)hy
  417. , (float)hw
  418. , (float)hh
  419. , (float)width / 2 - 1
  420. , imguiRGBA(255, 196, 0, 196)
  421. );
  422. }
  423. else
  424. {
  425. drawRoundedRect( (float)hx
  426. , (float)hy
  427. , (float)hw
  428. , (float)hh
  429. , (float)width / 2 - 1
  430. , isHot(hid) ? imguiRGBA(255, 196, 0, 96) : imguiRGBA(255, 255, 255, 64)
  431. );
  432. }
  433. // Handle mouse scrolling.
  434. if (m_insideScrollArea) // && !anyActive())
  435. {
  436. if (m_scroll)
  437. {
  438. *m_scrollVal += 20 * m_scroll;
  439. if (*m_scrollVal < 0)
  440. {
  441. *m_scrollVal = 0;
  442. }
  443. if (*m_scrollVal > (sh - height) )
  444. {
  445. *m_scrollVal = (sh - height);
  446. }
  447. }
  448. }
  449. }
  450. m_insideCurrentScroll = false;
  451. }
  452. bool button(const char* _text, bool _enabled)
  453. {
  454. m_widgetId++;
  455. uint32_t id = (m_areaId << 16) | m_widgetId;
  456. int32_t xx = m_widgetX;
  457. int32_t yy = m_widgetY;
  458. int32_t width = m_widgetW;
  459. int32_t height = BUTTON_HEIGHT;
  460. m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  461. bool over = _enabled && inRect(xx, yy, width, height);
  462. bool res = buttonLogic(id, over);
  463. drawRoundedRect( (float)xx
  464. , (float)yy
  465. , (float)width
  466. , (float)height
  467. , (float)BUTTON_HEIGHT / 2 - 1
  468. , imguiRGBA(128, 128, 128, isActive(id) ? 196 : 96)
  469. );
  470. if (_enabled)
  471. {
  472. drawText(xx + BUTTON_HEIGHT / 2
  473. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  474. , ImguiTextAlign::Left
  475. , _text
  476. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  477. );
  478. }
  479. else
  480. {
  481. drawText(xx + BUTTON_HEIGHT / 2
  482. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  483. , ImguiTextAlign::Left
  484. , _text
  485. , imguiRGBA(128, 128, 128, 200)
  486. );
  487. }
  488. return res;
  489. }
  490. bool item(const char* _text, bool _enabled)
  491. {
  492. m_widgetId++;
  493. uint32_t id = (m_areaId << 16) | m_widgetId;
  494. int32_t xx = m_widgetX;
  495. int32_t yy = m_widgetY;
  496. int32_t width = m_widgetW;
  497. int32_t height = BUTTON_HEIGHT;
  498. m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  499. bool over = _enabled && inRect(xx, yy, width, height);
  500. bool res = buttonLogic(id, over);
  501. if (isHot(id) )
  502. {
  503. drawRoundedRect( (float)xx
  504. , (float)yy
  505. , (float)width
  506. , (float)height
  507. , 2.0f
  508. , imguiRGBA(255, 196, 0, isActive(id) ? 196 : 96)
  509. );
  510. }
  511. if (_enabled)
  512. {
  513. drawText(xx + BUTTON_HEIGHT / 2
  514. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  515. , ImguiTextAlign::Left
  516. , _text
  517. , imguiRGBA(255, 255, 255, 200)
  518. );
  519. }
  520. else
  521. {
  522. drawText(xx + BUTTON_HEIGHT / 2
  523. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  524. , ImguiTextAlign::Left
  525. , _text
  526. , imguiRGBA(128, 128, 128, 200)
  527. );
  528. }
  529. return res;
  530. }
  531. bool check(const char* _text, bool _checked, bool _enabled)
  532. {
  533. m_widgetId++;
  534. uint32_t id = (m_areaId << 16) | m_widgetId;
  535. int32_t xx = m_widgetX;
  536. int32_t yy = m_widgetY;
  537. int32_t width = m_widgetW;
  538. int32_t height = BUTTON_HEIGHT;
  539. m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  540. bool over = _enabled && inRect(xx, yy, width, height);
  541. bool res = buttonLogic(id, over);
  542. const int32_t cx = xx + BUTTON_HEIGHT / 2 - CHECK_SIZE / 2;
  543. const int32_t cy = yy + BUTTON_HEIGHT / 2 - CHECK_SIZE / 2;
  544. drawRoundedRect( (float)cx - 3
  545. , (float)cy - 3
  546. , (float)CHECK_SIZE + 6
  547. , (float)CHECK_SIZE + 6
  548. , 4
  549. , imguiRGBA(128, 128, 128, isActive(id) ? 196 : 96)
  550. );
  551. if (_checked)
  552. {
  553. if (_enabled)
  554. {
  555. drawRoundedRect( (float)cx
  556. , (float)cy
  557. , (float)CHECK_SIZE
  558. , (float)CHECK_SIZE
  559. , (float)CHECK_SIZE / 2 - 1
  560. , imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200)
  561. );
  562. }
  563. else
  564. {
  565. drawRoundedRect( (float)cx
  566. , (float)cy
  567. , (float)CHECK_SIZE
  568. , (float)CHECK_SIZE
  569. , (float)CHECK_SIZE / 2 - 1
  570. , imguiRGBA(128, 128, 128, 200)
  571. );
  572. }
  573. }
  574. if (_enabled)
  575. {
  576. drawText(xx + BUTTON_HEIGHT
  577. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  578. , ImguiTextAlign::Left
  579. , _text
  580. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  581. );
  582. }
  583. else
  584. {
  585. drawText(xx + BUTTON_HEIGHT
  586. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  587. , ImguiTextAlign::Left
  588. , _text
  589. , imguiRGBA(128, 128, 128, 200)
  590. );
  591. }
  592. return res;
  593. }
  594. bool collapse(const char* _text, const char* _subtext, bool _checked, bool _enabled)
  595. {
  596. m_widgetId++;
  597. uint32_t id = (m_areaId << 16) | m_widgetId;
  598. int32_t xx = m_widgetX;
  599. int32_t yy = m_widgetY;
  600. int32_t width = m_widgetW;
  601. int32_t height = BUTTON_HEIGHT;
  602. m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  603. const int32_t cx = xx + BUTTON_HEIGHT / 2 - CHECK_SIZE / 2;
  604. const int32_t cy = yy + BUTTON_HEIGHT / 2 - CHECK_SIZE / 2;
  605. bool over = _enabled && inRect(xx, yy, width, height);
  606. bool res = buttonLogic(id, over);
  607. if (_checked)
  608. {
  609. drawTriangle(cx
  610. , cy
  611. , CHECK_SIZE
  612. , CHECK_SIZE
  613. , 2
  614. , imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200)
  615. );
  616. }
  617. else
  618. {
  619. drawTriangle(cx
  620. , cy
  621. , CHECK_SIZE
  622. , CHECK_SIZE
  623. , 1
  624. , imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200)
  625. );
  626. }
  627. if (_enabled)
  628. {
  629. drawText(xx + BUTTON_HEIGHT
  630. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  631. , ImguiTextAlign::Left
  632. , _text
  633. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  634. );
  635. }
  636. else
  637. {
  638. drawText(xx + BUTTON_HEIGHT
  639. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  640. , ImguiTextAlign::Left
  641. , _text
  642. , imguiRGBA(128, 128, 128, 200)
  643. );
  644. }
  645. if (_subtext)
  646. {
  647. drawText(xx + width - BUTTON_HEIGHT / 2
  648. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  649. , ImguiTextAlign::Right
  650. , _subtext
  651. , imguiRGBA(255, 255, 255, 128)
  652. );
  653. }
  654. return res;
  655. }
  656. void labelVargs(const char* _format, va_list _argList)
  657. {
  658. char temp[8192];
  659. char* out = temp;
  660. int32_t len = bx::vsnprintf(out, sizeof(temp), _format, _argList);
  661. if ( (int32_t)sizeof(temp) < len)
  662. {
  663. out = (char*)alloca(len+1);
  664. len = bx::vsnprintf(out, len, _format, _argList);
  665. }
  666. out[len] = '\0';
  667. int32_t xx = m_widgetX;
  668. int32_t yy = m_widgetY;
  669. m_widgetY += BUTTON_HEIGHT;
  670. drawText(xx
  671. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  672. , ImguiTextAlign::Left
  673. , out
  674. , imguiRGBA(255, 255, 255, 255)
  675. );
  676. }
  677. void value(const char* _text)
  678. {
  679. const int32_t xx = m_widgetX;
  680. const int32_t yy = m_widgetY;
  681. const int32_t ww = m_widgetW;
  682. m_widgetY += BUTTON_HEIGHT;
  683. drawText(xx + ww - BUTTON_HEIGHT / 2
  684. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  685. , ImguiTextAlign::Right
  686. , _text
  687. , imguiRGBA(255, 255, 255, 200)
  688. );
  689. }
  690. bool slider(const char* _text, float* _val, float _vmin, float _vmax, float _vinc, bool _enabled)
  691. {
  692. m_widgetId++;
  693. uint32_t id = (m_areaId << 16) | m_widgetId;
  694. int32_t xx = m_widgetX;
  695. int32_t yy = m_widgetY;
  696. int32_t width = m_widgetW;
  697. int32_t height = SLIDER_HEIGHT;
  698. m_widgetY += SLIDER_HEIGHT + DEFAULT_SPACING;
  699. drawRoundedRect( (float)xx, (float)yy, (float)width, (float)height, 4.0f, imguiRGBA(0, 0, 0, 128) );
  700. const int32_t range = width - SLIDER_MARKER_WIDTH;
  701. float uu = (*_val - _vmin) / (_vmax - _vmin);
  702. if (uu < 0)
  703. {
  704. uu = 0;
  705. }
  706. if (uu > 1)
  707. {
  708. uu = 1;
  709. }
  710. int32_t m = (int)(uu * range);
  711. bool over = _enabled && inRect(xx + m, yy, SLIDER_MARKER_WIDTH, SLIDER_HEIGHT);
  712. bool res = buttonLogic(id, over);
  713. bool valChanged = false;
  714. if (isActive(id) )
  715. {
  716. if (m_wentActive)
  717. {
  718. m_dragX = m_mx;
  719. m_dragOrig = uu;
  720. }
  721. if (m_dragX != m_mx)
  722. {
  723. uu = m_dragOrig + (float)(m_mx - m_dragX) / (float)range;
  724. if (uu < 0)
  725. {
  726. uu = 0;
  727. }
  728. if (uu > 1)
  729. {
  730. uu = 1;
  731. }
  732. *_val = _vmin + uu * (_vmax - _vmin);
  733. *_val = floorf(*_val / _vinc + 0.5f) * _vinc; // Snap to vinc
  734. m = (int)(uu * range);
  735. valChanged = true;
  736. }
  737. }
  738. if (isActive(id) )
  739. {
  740. drawRoundedRect( (float)(xx + m)
  741. , (float)yy
  742. , (float)SLIDER_MARKER_WIDTH
  743. , (float)SLIDER_HEIGHT
  744. , 4.0f
  745. , imguiRGBA(255, 255, 255, 255)
  746. );
  747. }
  748. else
  749. {
  750. drawRoundedRect( (float)(xx + m)
  751. , (float)yy
  752. , (float)SLIDER_MARKER_WIDTH
  753. , (float)SLIDER_HEIGHT
  754. , 4.0f
  755. , isHot(id) ? imguiRGBA(255, 196, 0, 128) : imguiRGBA(255, 255, 255, 64)
  756. );
  757. }
  758. // TODO: fix this, take a look at 'nicenum'.
  759. int32_t digits = (int)(ceilf(log10f(_vinc) ) );
  760. char fmt[16];
  761. bx::snprintf(fmt, 16, "%%.%df", digits >= 0 ? 0 : -digits);
  762. char msg[128];
  763. bx::snprintf(msg, 128, fmt, *_val);
  764. if (_enabled)
  765. {
  766. drawText(xx + SLIDER_HEIGHT / 2
  767. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  768. , ImguiTextAlign::Left
  769. , _text
  770. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  771. );
  772. drawText(xx + width - SLIDER_HEIGHT / 2
  773. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  774. , ImguiTextAlign::Right
  775. , msg
  776. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  777. );
  778. }
  779. else
  780. {
  781. drawText(xx + SLIDER_HEIGHT / 2
  782. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  783. , ImguiTextAlign::Left
  784. , _text
  785. , imguiRGBA(128, 128, 128, 200)
  786. );
  787. drawText(xx + width - SLIDER_HEIGHT / 2
  788. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  789. , ImguiTextAlign::Right
  790. , msg
  791. , imguiRGBA(128, 128, 128, 200)
  792. );
  793. }
  794. return res || valChanged;
  795. }
  796. void indent()
  797. {
  798. m_widgetX += INDENT_SIZE;
  799. m_widgetW -= INDENT_SIZE;
  800. }
  801. void unindent()
  802. {
  803. m_widgetX -= INDENT_SIZE;
  804. m_widgetW += INDENT_SIZE;
  805. }
  806. void separator()
  807. {
  808. m_widgetY += DEFAULT_SPACING * 3;
  809. }
  810. void separatorLine()
  811. {
  812. int32_t xx = m_widgetX;
  813. int32_t yy = m_widgetY;
  814. int32_t width = m_widgetW;
  815. int32_t height = 1;
  816. m_widgetY += DEFAULT_SPACING * 4;
  817. drawRect( (float)xx
  818. , (float)yy
  819. , (float)width
  820. , (float)height
  821. , imguiRGBA(255, 255, 255, 32)
  822. );
  823. }
  824. void drawPolygon(const float* _coords, uint32_t _numCoords, float _r, uint32_t _abgr)
  825. {
  826. _numCoords = bx::uint32_min(_numCoords, MAX_TEMP_COORDS);
  827. for (uint32_t ii = 0, jj = _numCoords - 1; ii < _numCoords; jj = ii++)
  828. {
  829. const float* v0 = &_coords[jj * 2];
  830. const float* v1 = &_coords[ii * 2];
  831. float dx = v1[0] - v0[0];
  832. float dy = v1[1] - v0[1];
  833. float d = sqrtf(dx * dx + dy * dy);
  834. if (d > 0)
  835. {
  836. d = 1.0f / d;
  837. dx *= d;
  838. dy *= d;
  839. }
  840. m_tempNormals[jj * 2 + 0] = dy;
  841. m_tempNormals[jj * 2 + 1] = -dx;
  842. }
  843. for (uint32_t ii = 0, jj = _numCoords - 1; ii < _numCoords; jj = ii++)
  844. {
  845. float dlx0 = m_tempNormals[jj * 2 + 0];
  846. float dly0 = m_tempNormals[jj * 2 + 1];
  847. float dlx1 = m_tempNormals[ii * 2 + 0];
  848. float dly1 = m_tempNormals[ii * 2 + 1];
  849. float dmx = (dlx0 + dlx1) * 0.5f;
  850. float dmy = (dly0 + dly1) * 0.5f;
  851. float dmr2 = dmx * dmx + dmy * dmy;
  852. if (dmr2 > 0.000001f)
  853. {
  854. float scale = 1.0f / dmr2;
  855. if (scale > 10.0f)
  856. {
  857. scale = 10.0f;
  858. }
  859. dmx *= scale;
  860. dmy *= scale;
  861. }
  862. m_tempCoords[ii * 2 + 0] = _coords[ii * 2 + 0] + dmx * _r;
  863. m_tempCoords[ii * 2 + 1] = _coords[ii * 2 + 1] + dmy * _r;
  864. }
  865. uint32_t numVertices = _numCoords*6 + (_numCoords-2)*3;
  866. if (bgfx::checkAvailTransientVertexBuffer(numVertices, PosColorVertex::ms_decl) )
  867. {
  868. bgfx::TransientVertexBuffer tvb;
  869. bgfx::allocTransientVertexBuffer(&tvb, numVertices, PosColorVertex::ms_decl);
  870. uint32_t trans = _abgr&0xffffff;
  871. PosColorVertex* vertex = (PosColorVertex*)tvb.data;
  872. for (uint32_t ii = 0, jj = _numCoords-1; ii < _numCoords; jj = ii++)
  873. {
  874. vertex->m_x = _coords[ii*2+0];
  875. vertex->m_y = _coords[ii*2+1];
  876. vertex->m_abgr = _abgr;
  877. ++vertex;
  878. vertex->m_x = _coords[jj*2+0];
  879. vertex->m_y = _coords[jj*2+1];
  880. vertex->m_abgr = _abgr;
  881. ++vertex;
  882. vertex->m_x = m_tempCoords[jj*2+0];
  883. vertex->m_y = m_tempCoords[jj*2+1];
  884. vertex->m_abgr = trans;
  885. ++vertex;
  886. vertex->m_x = m_tempCoords[jj*2+0];
  887. vertex->m_y = m_tempCoords[jj*2+1];
  888. vertex->m_abgr = trans;
  889. ++vertex;
  890. vertex->m_x = m_tempCoords[ii*2+0];
  891. vertex->m_y = m_tempCoords[ii*2+1];
  892. vertex->m_abgr = trans;
  893. ++vertex;
  894. vertex->m_x = _coords[ii*2+0];
  895. vertex->m_y = _coords[ii*2+1];
  896. vertex->m_abgr = _abgr;
  897. ++vertex;
  898. }
  899. for (uint32_t ii = 2; ii < _numCoords; ++ii)
  900. {
  901. vertex->m_x = _coords[0];
  902. vertex->m_y = _coords[1];
  903. vertex->m_abgr = _abgr;
  904. ++vertex;
  905. vertex->m_x = _coords[(ii-1)*2+0];
  906. vertex->m_y = _coords[(ii-1)*2+1];
  907. vertex->m_abgr = _abgr;
  908. ++vertex;
  909. vertex->m_x = _coords[ii*2+0];
  910. vertex->m_y = _coords[ii*2+1];
  911. vertex->m_abgr = _abgr;
  912. ++vertex;
  913. }
  914. bgfx::setVertexBuffer(&tvb);
  915. bgfx::setState(0
  916. | BGFX_STATE_RGB_WRITE
  917. | BGFX_STATE_ALPHA_WRITE
  918. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  919. );
  920. bgfx::setProgram(m_colorProgram);
  921. bgfx::setScissor(m_scissor);
  922. bgfx::submit(m_view);
  923. }
  924. }
  925. void drawRect(float _x, float _y, float w, float h, uint32_t _argb, float _fth = 1.0f)
  926. {
  927. float verts[4 * 2] =
  928. {
  929. _x + 0.5f, _y + 0.5f,
  930. _x + w - 0.5f, _y + 0.5f,
  931. _x + w - 0.5f, _y + h - 0.5f,
  932. _x + 0.5f, _y + h - 0.5f,
  933. };
  934. drawPolygon(verts, 4, _fth, _argb);
  935. }
  936. void drawRoundedRect(float _x, float _y, float w, float h, float r, uint32_t _argb, float _fth = 1.0f)
  937. {
  938. const uint32_t num = NUM_CIRCLE_VERTS / 4;
  939. const float* cverts = m_circleVerts;
  940. float verts[(num + 1) * 4 * 2];
  941. float* vv = verts;
  942. for (uint32_t ii = 0; ii <= num; ++ii)
  943. {
  944. *vv++ = _x + w - r + cverts[ii * 2] * r;
  945. *vv++ = _y + h - r + cverts[ii * 2 + 1] * r;
  946. }
  947. for (uint32_t ii = num; ii <= num * 2; ++ii)
  948. {
  949. *vv++ = _x + r + cverts[ii * 2] * r;
  950. *vv++ = _y + h - r + cverts[ii * 2 + 1] * r;
  951. }
  952. for (uint32_t ii = num * 2; ii <= num * 3; ++ii)
  953. {
  954. *vv++ = _x + r + cverts[ii * 2] * r;
  955. *vv++ = _y + r + cverts[ii * 2 + 1] * r;
  956. }
  957. for (uint32_t ii = num * 3; ii < num * 4; ++ii)
  958. {
  959. *vv++ = _x + w - r + cverts[ii * 2] * r;
  960. *vv++ = _y + r + cverts[ii * 2 + 1] * r;
  961. }
  962. *vv++ = _x + w - r + cverts[0] * r;
  963. *vv++ = _y + r + cverts[1] * r;
  964. drawPolygon(verts, (num + 1) * 4, _fth, _argb);
  965. }
  966. void drawLine(float _x0, float _y0, float _x1, float _y1, float _r, uint32_t _abgr, float _fth = 1.0f)
  967. {
  968. float dx = _x1 - _x0;
  969. float dy = _y1 - _y0;
  970. float d = sqrtf(dx * dx + dy * dy);
  971. if (d > 0.0001f)
  972. {
  973. d = 1.0f / d;
  974. dx *= d;
  975. dy *= d;
  976. }
  977. float nx = dy;
  978. float ny = -dx;
  979. float verts[4 * 2];
  980. _r -= _fth;
  981. _r *= 0.5f;
  982. if (_r < 0.01f)
  983. {
  984. _r = 0.01f;
  985. }
  986. dx *= _r;
  987. dy *= _r;
  988. nx *= _r;
  989. ny *= _r;
  990. verts[0] = _x0 - dx - nx;
  991. verts[1] = _y0 - dy - ny;
  992. verts[2] = _x0 - dx + nx;
  993. verts[3] = _y0 - dy + ny;
  994. verts[4] = _x1 + dx + nx;
  995. verts[5] = _y1 + dy + ny;
  996. verts[6] = _x1 + dx - nx;
  997. verts[7] = _y1 + dy - ny;
  998. drawPolygon(verts, 4, _fth, _abgr);
  999. }
  1000. void drawTriangle(int32_t _x, int32_t _y, int32_t _width, int32_t _height, int32_t _flags, uint32_t _abgr)
  1001. {
  1002. if (1 == _flags)
  1003. {
  1004. const float verts[3 * 2] =
  1005. {
  1006. (float)_x + 0.5f, (float)_y + 0.5f,
  1007. (float)_x + 0.5f + (float)_width * 1.0f, (float)_y + 0.5f + (float)_height / 2.0f - 0.5f,
  1008. (float)_x + 0.5f, (float)_y + 0.5f + (float)_height - 1.0f,
  1009. };
  1010. drawPolygon(verts, 3, 1.0f, _abgr);
  1011. }
  1012. else
  1013. {
  1014. const float verts[3 * 2] =
  1015. {
  1016. (float)_x + 0.5f, (float)_y + 0.5f + (float)_height - 1.0f,
  1017. (float)_x + 0.5f + (float)_width / 2.0f - 0.5f, (float)_y + 0.5f,
  1018. (float)_x + 0.5f + (float)_width - 1.0f, (float)_y + 0.5f + (float)_height - 1.0f,
  1019. };
  1020. drawPolygon(verts, 3, 1.0f, _abgr);
  1021. }
  1022. }
  1023. void getBakedQuad(stbtt_bakedchar* _chardata, int32_t char_index, float* _xpos, float* _ypos, stbtt_aligned_quad* _quad)
  1024. {
  1025. stbtt_bakedchar* b = _chardata + char_index;
  1026. int32_t round_x = STBTT_ifloor(*_xpos + b->xoff);
  1027. int32_t round_y = STBTT_ifloor(*_ypos + b->yoff);
  1028. _quad->x0 = (float)round_x;
  1029. _quad->y0 = (float)round_y;
  1030. _quad->x1 = (float)round_x + b->x1 - b->x0;
  1031. _quad->y1 = (float)round_y + b->y1 - b->y0;
  1032. _quad->s0 = (b->x0 + m_halfTexel) * m_invTextureWidth;
  1033. _quad->t0 = (b->y0 + m_halfTexel) * m_invTextureWidth;
  1034. _quad->s1 = (b->x1 + m_halfTexel) * m_invTextureHeight;
  1035. _quad->t1 = (b->y1 + m_halfTexel) * m_invTextureHeight;
  1036. *_xpos += b->xadvance;
  1037. }
  1038. float getTextLength(stbtt_bakedchar* _chardata, const char* _text, uint32_t& _numVertices)
  1039. {
  1040. float xpos = 0;
  1041. float len = 0;
  1042. uint32_t numVertices = 0;
  1043. while (*_text)
  1044. {
  1045. int32_t ch = (uint8_t)*_text;
  1046. if (ch == '\t')
  1047. {
  1048. for (int32_t ii = 0; ii < 4; ++ii)
  1049. {
  1050. if (xpos < s_tabStops[ii])
  1051. {
  1052. xpos = s_tabStops[ii];
  1053. break;
  1054. }
  1055. }
  1056. }
  1057. else if (ch >= ' '
  1058. && ch < 128)
  1059. {
  1060. stbtt_bakedchar* b = _chardata + ch - ' ';
  1061. int32_t round_x = STBTT_ifloor( (xpos + b->xoff) + 0.5);
  1062. len = round_x + b->x1 - b->x0 + 0.5f;
  1063. xpos += b->xadvance;
  1064. numVertices += 6;
  1065. }
  1066. ++_text;
  1067. }
  1068. _numVertices = numVertices;
  1069. return len;
  1070. }
  1071. void drawText(int32_t _x, int32_t _y, ImguiTextAlign::Enum _align, const char* _text, uint32_t _abgr)
  1072. {
  1073. drawText( (float)_x, (float)_y, _text, _align, _abgr);
  1074. }
  1075. void drawText(float _x, float _y, const char* _text, ImguiTextAlign::Enum _align, uint32_t _abgr)
  1076. {
  1077. if (!_text)
  1078. {
  1079. return;
  1080. }
  1081. uint32_t numVertices = 0;
  1082. if (_align == ImguiTextAlign::Center)
  1083. {
  1084. _x -= getTextLength(m_cdata, _text, numVertices) / 2;
  1085. }
  1086. else if (_align == ImguiTextAlign::Right)
  1087. {
  1088. _x -= getTextLength(m_cdata, _text, numVertices);
  1089. }
  1090. else // just count vertices
  1091. {
  1092. getTextLength(m_cdata, _text, numVertices);
  1093. }
  1094. if (bgfx::checkAvailTransientVertexBuffer(numVertices, PosColorUvVertex::ms_decl) )
  1095. {
  1096. bgfx::TransientVertexBuffer tvb;
  1097. bgfx::allocTransientVertexBuffer(&tvb, numVertices, PosColorUvVertex::ms_decl);
  1098. PosColorUvVertex* vertex = (PosColorUvVertex*)tvb.data;
  1099. const float ox = _x;
  1100. while (*_text)
  1101. {
  1102. int32_t ch = (uint8_t)*_text;
  1103. if (ch == '\t')
  1104. {
  1105. for (int32_t i = 0; i < 4; ++i)
  1106. {
  1107. if (_x < s_tabStops[i] + ox)
  1108. {
  1109. _x = s_tabStops[i] + ox;
  1110. break;
  1111. }
  1112. }
  1113. }
  1114. else if (ch >= ' '
  1115. && ch < 128)
  1116. {
  1117. stbtt_aligned_quad quad;
  1118. getBakedQuad(m_cdata, ch - 32, &_x, &_y, &quad);
  1119. vertex->m_x = quad.x0;
  1120. vertex->m_y = quad.y0;
  1121. vertex->m_u = quad.s0;
  1122. vertex->m_v = quad.t0;
  1123. vertex->m_abgr = _abgr;
  1124. ++vertex;
  1125. vertex->m_x = quad.x1;
  1126. vertex->m_y = quad.y1;
  1127. vertex->m_u = quad.s1;
  1128. vertex->m_v = quad.t1;
  1129. vertex->m_abgr = _abgr;
  1130. ++vertex;
  1131. vertex->m_x = quad.x1;
  1132. vertex->m_y = quad.y0;
  1133. vertex->m_u = quad.s1;
  1134. vertex->m_v = quad.t0;
  1135. vertex->m_abgr = _abgr;
  1136. ++vertex;
  1137. vertex->m_x = quad.x0;
  1138. vertex->m_y = quad.y0;
  1139. vertex->m_u = quad.s0;
  1140. vertex->m_v = quad.t0;
  1141. vertex->m_abgr = _abgr;
  1142. ++vertex;
  1143. vertex->m_x = quad.x0;
  1144. vertex->m_y = quad.y1;
  1145. vertex->m_u = quad.s0;
  1146. vertex->m_v = quad.t1;
  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. }
  1156. ++_text;
  1157. }
  1158. bgfx::setTexture(0, u_texColor, m_fontTexture);
  1159. bgfx::setVertexBuffer(&tvb);
  1160. bgfx::setState(0
  1161. | BGFX_STATE_RGB_WRITE
  1162. | BGFX_STATE_ALPHA_WRITE
  1163. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  1164. );
  1165. bgfx::setProgram(m_textureProgram);
  1166. bgfx::setScissor(m_scissor);
  1167. bgfx::submit(m_view);
  1168. }
  1169. }
  1170. int32_t m_mx;
  1171. int32_t m_my;
  1172. int32_t m_scroll;
  1173. uint32_t m_active;
  1174. uint32_t m_hot;
  1175. uint32_t m_hotToBe;
  1176. int32_t m_dragX;
  1177. int32_t m_dragY;
  1178. float m_dragOrig;
  1179. int32_t m_widgetX;
  1180. int32_t m_widgetY;
  1181. int32_t m_widgetW;
  1182. bool m_left;
  1183. bool m_leftPressed;
  1184. bool m_leftReleased;
  1185. bool m_isHot;
  1186. bool m_isActive;
  1187. bool m_wentActive;
  1188. bool m_insideCurrentScroll;
  1189. uint32_t m_areaId;
  1190. uint32_t m_widgetId;
  1191. uint16_t m_scissor;
  1192. float m_tempCoords[MAX_TEMP_COORDS * 2];
  1193. float m_tempNormals[MAX_TEMP_COORDS * 2];
  1194. float m_circleVerts[NUM_CIRCLE_VERTS * 2];
  1195. int32_t m_scrollTop;
  1196. int32_t m_scrollBottom;
  1197. int32_t m_scrollRight;
  1198. int32_t m_scrollAreaTop;
  1199. int32_t* m_scrollVal;
  1200. int32_t m_focusTop;
  1201. int32_t m_focusBottom;
  1202. uint32_t m_scrollId;
  1203. bool m_insideScrollArea;
  1204. stbtt_bakedchar m_cdata[96]; // ASCII 32..126 is 95 glyphs
  1205. uint16_t m_textureWidth;
  1206. uint16_t m_textureHeight;
  1207. float m_invTextureWidth;
  1208. float m_invTextureHeight;
  1209. float m_halfTexel;
  1210. uint8_t m_view;
  1211. bgfx::UniformHandle u_texColor;
  1212. bgfx::TextureHandle m_fontTexture;
  1213. bgfx::ProgramHandle m_colorProgram;
  1214. bgfx::ProgramHandle m_textureProgram;
  1215. };
  1216. static Imgui s_imgui;
  1217. bool imguiCreate(const void* _data, uint32_t _size)
  1218. {
  1219. return s_imgui.create(_data, _size);
  1220. }
  1221. void imguiDestroy()
  1222. {
  1223. s_imgui.destroy();
  1224. }
  1225. void imguiBeginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, uint16_t _width, uint16_t _height, uint8_t _view)
  1226. {
  1227. s_imgui.beginFrame(_mx, _my, _button, _scroll, _width, _height, _view);
  1228. }
  1229. void imguiEndFrame()
  1230. {
  1231. s_imgui.endFrame();
  1232. }
  1233. bool imguiBeginScrollArea(const char* _name, int32_t _x, int32_t _y, int32_t _width, int32_t _height, int32_t* _scroll)
  1234. {
  1235. return s_imgui.beginScrollArea(_name, _x, _y, _width, _height, _scroll);
  1236. }
  1237. void imguiEndScrollArea()
  1238. {
  1239. return s_imgui.endScrollArea();
  1240. }
  1241. void imguiIndent()
  1242. {
  1243. s_imgui.indent();
  1244. }
  1245. void imguiUnindent()
  1246. {
  1247. s_imgui.unindent();
  1248. }
  1249. void imguiSeparator()
  1250. {
  1251. s_imgui.separator();
  1252. }
  1253. void imguiSeparatorLine()
  1254. {
  1255. s_imgui.separatorLine();
  1256. }
  1257. bool imguiButton(const char* _text, bool _enabled)
  1258. {
  1259. return s_imgui.button(_text, _enabled);
  1260. }
  1261. bool imguiItem(const char* _text, bool _enabled)
  1262. {
  1263. return s_imgui.item(_text, _enabled);
  1264. }
  1265. bool imguiCheck(const char* _text, bool _checked, bool _enabled)
  1266. {
  1267. return s_imgui.check(_text, _checked, _enabled);
  1268. }
  1269. bool imguiCollapse(const char* _text, const char* _subtext, bool _checked, bool _enabled)
  1270. {
  1271. return s_imgui.collapse(_text, _subtext, _checked, _enabled);
  1272. }
  1273. void imguiLabel(const char* _format, ...)
  1274. {
  1275. va_list argList;
  1276. va_start(argList, _format);
  1277. s_imgui.labelVargs(_format, argList);
  1278. va_end(argList);
  1279. }
  1280. void imguiValue(const char* _text)
  1281. {
  1282. s_imgui.value(_text);
  1283. }
  1284. bool imguiSlider(const char* _text, float* _val, float _vmin, float _vmax, float _vinc, bool _enabled)
  1285. {
  1286. return s_imgui.slider(_text, _val, _vmin, _vmax, _vinc, _enabled);
  1287. }
  1288. bool imguiSlider(const char* _text, int32_t* _val, int32_t _vmin, int32_t _vmax, bool _enabled)
  1289. {
  1290. float val = (float)*_val;
  1291. bool result = s_imgui.slider(_text, &val, (float)_vmin, (float)_vmax, 1.0f, _enabled);
  1292. *_val = (int32_t)val;
  1293. return result;
  1294. }
  1295. uint32_t imguiChooseUseMacroInstead(uint32_t _selected, ...)
  1296. {
  1297. va_list argList;
  1298. va_start(argList, _selected);
  1299. const char* str = va_arg(argList, const char*);
  1300. for (uint32_t ii = 0; str != NULL; ++ii, str = va_arg(argList, const char*))
  1301. {
  1302. if (imguiCheck(str, ii == _selected) )
  1303. {
  1304. _selected = ii;
  1305. }
  1306. }
  1307. va_end(argList);
  1308. return _selected;
  1309. }
  1310. void imguiDrawText(int32_t _x, int32_t _y, ImguiTextAlign::Enum _align, const char* _text, uint32_t _argb)
  1311. {
  1312. s_imgui.drawText(_x, _y, _align, _text, _argb);
  1313. }
  1314. void imguiDrawLine(float _x0, float _y0, float _x1, float _y1, float _r, uint32_t _argb)
  1315. {
  1316. s_imgui.drawLine(_x0, _y0, _x1, _y1, _r, _argb);
  1317. }
  1318. void imguiDrawRoundedRect(float _x, float _y, float _width, float _height, float _r, uint32_t _argb)
  1319. {
  1320. s_imgui.drawRoundedRect(_x, _y, _width, _height, _r, _argb);
  1321. }
  1322. void imguiDrawRect(float _x, float _y, float _width, float _height, uint32_t _argb)
  1323. {
  1324. s_imgui.drawRect(_x, _y, _width, _height, _argb);
  1325. }