imgui.cpp 34 KB

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