imgui.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  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 "../math.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, (float)_y, (float)_width, (float)_height, 6, imguiRGBA(0, 0, 0, 192) );
  327. drawText(_x + AREA_HEADER / 2, _y + AREA_HEADER / 2, IMGUI_ALIGN_LEFT, _name, imguiRGBA(255, 255, 255, 128) );
  328. // setScissor(_x + SCROLL_AREA_PADDING, _y + SCROLL_AREA_PADDING, _width - SCROLL_AREA_PADDING * 4, _height - AREA_HEADER - SCROLL_AREA_PADDING);
  329. return m_insideScrollArea;
  330. }
  331. void endScrollArea()
  332. {
  333. // Disable scissoring.
  334. // setScissor(-1, -1, -1, -1);
  335. // Draw scroll bar
  336. int32_t xx = m_scrollRight + SCROLL_AREA_PADDING / 2;
  337. int32_t yy = m_scrollTop;
  338. int32_t width = SCROLL_AREA_PADDING * 2;
  339. int32_t height = m_scrollBottom - m_scrollTop;
  340. int32_t stop = m_scrollAreaTop;
  341. int32_t sbot = m_widgetY;
  342. int32_t sh = sbot - stop; // The scrollable area height.
  343. float barHeight = (float)height / (float)sh;
  344. if (barHeight < 1)
  345. {
  346. float barY = (float)(yy - sbot) / (float)sh;
  347. if (barY < 0)
  348. {
  349. barY = 0;
  350. }
  351. if (barY > 1)
  352. {
  353. barY = 1;
  354. }
  355. // Handle scroll bar logic.
  356. uint32_t hid = m_scrollId;
  357. int32_t hx = xx;
  358. int32_t hy = yy + (int)(barY * height);
  359. int32_t hw = width;
  360. int32_t hh = (int)(barHeight * height);
  361. const int32_t range = height - (hh - 1);
  362. bool over = inRect(hx, hy, hw, hh);
  363. buttonLogic(hid, over);
  364. if (isActive(hid) )
  365. {
  366. float u = (float)(hy - yy) / (float)range;
  367. if (m_wentActive)
  368. {
  369. m_dragY = m_my;
  370. m_dragOrig = u;
  371. }
  372. if (m_dragY != m_my)
  373. {
  374. u = m_dragOrig + (m_my - m_dragY) / (float)range;
  375. if (u < 0)
  376. {
  377. u = 0;
  378. }
  379. if (u > 1)
  380. {
  381. u = 1;
  382. }
  383. *m_scrollVal = (int)( (1 - u) * (sh - height) );
  384. }
  385. }
  386. // BG
  387. drawRoundedRect( (float)xx, (float)yy, (float)width, (float)height, (float)width / 2 - 1, imguiRGBA(0, 0, 0, 196) );
  388. // Bar
  389. if (isActive(hid) )
  390. {
  391. drawRoundedRect( (float)hx, (float)hy, (float)hw, (float)hh, (float)width / 2 - 1, imguiRGBA(255, 196, 0, 196) );
  392. }
  393. else
  394. {
  395. drawRoundedRect( (float)hx, (float)hy, (float)hw, (float)hh, (float)width / 2 - 1, isHot(hid) ? imguiRGBA(255, 196, 0, 96) : imguiRGBA(255, 255, 255, 64) );
  396. }
  397. // Handle mouse scrolling.
  398. if (m_insideScrollArea) // && !anyActive())
  399. {
  400. if (m_scroll)
  401. {
  402. *m_scrollVal += 20 * m_scroll;
  403. if (*m_scrollVal < 0)
  404. {
  405. *m_scrollVal = 0;
  406. }
  407. if (*m_scrollVal > (sh - height) )
  408. {
  409. *m_scrollVal = (sh - height);
  410. }
  411. }
  412. }
  413. }
  414. m_insideCurrentScroll = false;
  415. }
  416. bool button(const char* _text, bool _enabled)
  417. {
  418. m_widgetId++;
  419. uint32_t id = (m_areaId << 16) | m_widgetId;
  420. int32_t xx = m_widgetX;
  421. int32_t yy = m_widgetY;
  422. int32_t width = m_widgetW;
  423. int32_t height = BUTTON_HEIGHT;
  424. m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  425. bool over = _enabled && inRect(xx, yy, width, height);
  426. bool res = buttonLogic(id, over);
  427. drawRoundedRect( (float)xx, (float)yy, (float)width, (float)height, (float)BUTTON_HEIGHT / 2 - 1, imguiRGBA(128, 128, 128, isActive(id) ? 196 : 96) );
  428. if (_enabled)
  429. {
  430. drawText(xx + BUTTON_HEIGHT / 2, yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2, IMGUI_ALIGN_LEFT, _text, isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200) );
  431. }
  432. else
  433. {
  434. drawText(xx + BUTTON_HEIGHT / 2, yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2, IMGUI_ALIGN_LEFT, _text, imguiRGBA(128, 128, 128, 200) );
  435. }
  436. return res;
  437. }
  438. bool item(const char* _text, bool _enabled)
  439. {
  440. m_widgetId++;
  441. uint32_t id = (m_areaId << 16) | m_widgetId;
  442. int32_t xx = m_widgetX;
  443. int32_t yy = m_widgetY;
  444. int32_t width = m_widgetW;
  445. int32_t height = BUTTON_HEIGHT;
  446. m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  447. bool over = _enabled && inRect(xx, yy, width, height);
  448. bool res = buttonLogic(id, over);
  449. if (isHot(id) )
  450. {
  451. drawRoundedRect( (float)xx, (float)yy, (float)width, (float)height, 2.0f, imguiRGBA(255, 196, 0, isActive(id) ? 196 : 96) );
  452. }
  453. if (_enabled)
  454. {
  455. drawText(xx + BUTTON_HEIGHT / 2, yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2, IMGUI_ALIGN_LEFT, _text, imguiRGBA(255, 255, 255, 200) );
  456. }
  457. else
  458. {
  459. drawText(xx + BUTTON_HEIGHT / 2, yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2, IMGUI_ALIGN_LEFT, _text, imguiRGBA(128, 128, 128, 200) );
  460. }
  461. return res;
  462. }
  463. bool check(const char* _text, bool _checked, bool _enabled)
  464. {
  465. m_widgetId++;
  466. uint32_t id = (m_areaId << 16) | m_widgetId;
  467. int32_t xx = m_widgetX;
  468. int32_t yy = m_widgetY;
  469. int32_t width = m_widgetW;
  470. int32_t height = BUTTON_HEIGHT;
  471. m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  472. bool over = _enabled && inRect(xx, yy, width, height);
  473. bool res = buttonLogic(id, over);
  474. const int32_t cx = xx + BUTTON_HEIGHT / 2 - CHECK_SIZE / 2;
  475. const int32_t cy = yy + BUTTON_HEIGHT / 2 - CHECK_SIZE / 2;
  476. drawRoundedRect( (float)cx - 3, (float)cy - 3, (float)CHECK_SIZE + 6, (float)CHECK_SIZE + 6, 4, imguiRGBA(128, 128, 128, isActive(id) ? 196 : 96) );
  477. if (_checked)
  478. {
  479. if (_enabled)
  480. {
  481. drawRoundedRect( (float)cx, (float)cy, (float)CHECK_SIZE, (float)CHECK_SIZE, (float)CHECK_SIZE / 2 - 1, imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200) );
  482. }
  483. else
  484. {
  485. drawRoundedRect( (float)cx, (float)cy, (float)CHECK_SIZE, (float)CHECK_SIZE, (float)CHECK_SIZE / 2 - 1, imguiRGBA(128, 128, 128, 200) );
  486. }
  487. }
  488. if (_enabled)
  489. {
  490. drawText(xx + BUTTON_HEIGHT, yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2, IMGUI_ALIGN_LEFT, _text, isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200) );
  491. }
  492. else
  493. {
  494. drawText(xx + BUTTON_HEIGHT, yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2, IMGUI_ALIGN_LEFT, _text, imguiRGBA(128, 128, 128, 200) );
  495. }
  496. return res;
  497. }
  498. bool collapse(const char* _text, const char* _subtext, bool _checked, bool _enabled)
  499. {
  500. m_widgetId++;
  501. uint32_t id = (m_areaId << 16) | m_widgetId;
  502. int32_t x = m_widgetX;
  503. int32_t y = m_widgetY;
  504. int32_t w = m_widgetW;
  505. int32_t h = BUTTON_HEIGHT;
  506. m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  507. const int32_t cx = x + BUTTON_HEIGHT / 2 - CHECK_SIZE / 2;
  508. const int32_t cy = y + BUTTON_HEIGHT / 2 - CHECK_SIZE / 2;
  509. bool over = _enabled && inRect(x, y, w, h);
  510. bool res = buttonLogic(id, over);
  511. if (_checked)
  512. {
  513. drawTriangle(cx, cy, CHECK_SIZE, CHECK_SIZE, 2, imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200) );
  514. }
  515. else
  516. {
  517. drawTriangle(cx, cy, CHECK_SIZE, CHECK_SIZE, 1, imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200) );
  518. }
  519. if (_enabled)
  520. {
  521. drawText(x + BUTTON_HEIGHT, y + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2, IMGUI_ALIGN_LEFT, _text, isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200) );
  522. }
  523. else
  524. {
  525. drawText(x + BUTTON_HEIGHT, y + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2, IMGUI_ALIGN_LEFT, _text, imguiRGBA(128, 128, 128, 200) );
  526. }
  527. if (_subtext)
  528. {
  529. drawText(x + w - BUTTON_HEIGHT / 2, y + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2, IMGUI_ALIGN_RIGHT, _subtext, imguiRGBA(255, 255, 255, 128) );
  530. }
  531. return res;
  532. }
  533. void labelVargs(const char* _format, va_list _argList)
  534. {
  535. char temp[8192];
  536. char* out = temp;
  537. int32_t len = bx::vsnprintf(out, sizeof(temp), _format, _argList);
  538. if ( (int32_t)sizeof(temp) < len)
  539. {
  540. out = (char*)alloca(len+1);
  541. len = bx::vsnprintf(out, len, _format, _argList);
  542. }
  543. out[len] = '\0';
  544. int32_t xx = m_widgetX;
  545. int32_t yy = m_widgetY;
  546. m_widgetY += BUTTON_HEIGHT;
  547. drawText(xx
  548. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  549. , IMGUI_ALIGN_LEFT
  550. , out
  551. , imguiRGBA(255, 255, 255, 255)
  552. );
  553. }
  554. void value(const char* _text)
  555. {
  556. const int32_t xx = m_widgetX;
  557. const int32_t yy = m_widgetY;
  558. const int32_t ww = m_widgetW;
  559. m_widgetY += BUTTON_HEIGHT;
  560. drawText(xx + ww - BUTTON_HEIGHT / 2
  561. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  562. , IMGUI_ALIGN_RIGHT
  563. , _text
  564. , imguiRGBA(255, 255, 255, 200)
  565. );
  566. }
  567. bool slider(const char* _text, float* _val, float _vmin, float _vmax, float _vinc, bool _enabled)
  568. {
  569. m_widgetId++;
  570. uint32_t id = (m_areaId << 16) | m_widgetId;
  571. int32_t xx = m_widgetX;
  572. int32_t yy = m_widgetY;
  573. int32_t width = m_widgetW;
  574. int32_t height = SLIDER_HEIGHT;
  575. m_widgetY += SLIDER_HEIGHT + DEFAULT_SPACING;
  576. drawRoundedRect( (float)xx, (float)yy, (float)width, (float)height, 4.0f, imguiRGBA(0, 0, 0, 128) );
  577. const int32_t range = width - SLIDER_MARKER_WIDTH;
  578. float uu = (*_val - _vmin) / (_vmax - _vmin);
  579. if (uu < 0)
  580. {
  581. uu = 0;
  582. }
  583. if (uu > 1)
  584. {
  585. uu = 1;
  586. }
  587. int32_t m = (int)(uu * range);
  588. bool over = _enabled && inRect(xx + m, yy, SLIDER_MARKER_WIDTH, SLIDER_HEIGHT);
  589. bool res = buttonLogic(id, over);
  590. bool valChanged = false;
  591. if (isActive(id) )
  592. {
  593. if (m_wentActive)
  594. {
  595. m_dragX = m_mx;
  596. m_dragOrig = uu;
  597. }
  598. if (m_dragX != m_mx)
  599. {
  600. uu = m_dragOrig + (float)(m_mx - m_dragX) / (float)range;
  601. if (uu < 0)
  602. {
  603. uu = 0;
  604. }
  605. if (uu > 1)
  606. {
  607. uu = 1;
  608. }
  609. *_val = _vmin + uu * (_vmax - _vmin);
  610. *_val = floorf(*_val / _vinc + 0.5f) * _vinc; // Snap to vinc
  611. m = (int)(uu * range);
  612. valChanged = true;
  613. }
  614. }
  615. if (isActive(id) )
  616. {
  617. drawRoundedRect( (float)(xx + m)
  618. , (float)yy
  619. , (float)SLIDER_MARKER_WIDTH
  620. , (float)SLIDER_HEIGHT
  621. , 4.0f
  622. , imguiRGBA(255, 255, 255, 255)
  623. );
  624. }
  625. else
  626. {
  627. drawRoundedRect( (float)(xx + m)
  628. , (float)yy
  629. , (float)SLIDER_MARKER_WIDTH
  630. , (float)SLIDER_HEIGHT
  631. , 4.0f
  632. , isHot(id) ? imguiRGBA(255, 196, 0, 128) : imguiRGBA(255, 255, 255, 64)
  633. );
  634. }
  635. // TODO: fix this, take a look at 'nicenum'.
  636. int32_t digits = (int)(ceilf(log10f(_vinc) ) );
  637. char fmt[16];
  638. bx::snprintf(fmt, 16, "%%.%df", digits >= 0 ? 0 : -digits);
  639. char msg[128];
  640. bx::snprintf(msg, 128, fmt, *_val);
  641. if (_enabled)
  642. {
  643. drawText(xx + SLIDER_HEIGHT / 2
  644. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  645. , IMGUI_ALIGN_LEFT
  646. , _text
  647. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  648. );
  649. drawText(xx + width - SLIDER_HEIGHT / 2
  650. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  651. , IMGUI_ALIGN_RIGHT
  652. , msg
  653. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  654. );
  655. }
  656. else
  657. {
  658. drawText(xx + SLIDER_HEIGHT / 2
  659. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  660. , IMGUI_ALIGN_LEFT
  661. , _text
  662. , imguiRGBA(128, 128, 128, 200)
  663. );
  664. drawText(xx + width - SLIDER_HEIGHT / 2
  665. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  666. , IMGUI_ALIGN_RIGHT
  667. , msg
  668. , imguiRGBA(128, 128, 128, 200)
  669. );
  670. }
  671. return res || valChanged;
  672. }
  673. void indent()
  674. {
  675. m_widgetX += INDENT_SIZE;
  676. m_widgetW -= INDENT_SIZE;
  677. }
  678. void unindent()
  679. {
  680. m_widgetX -= INDENT_SIZE;
  681. m_widgetW += INDENT_SIZE;
  682. }
  683. void separator()
  684. {
  685. m_widgetY += DEFAULT_SPACING * 3;
  686. }
  687. void separatorLine()
  688. {
  689. int32_t xx = m_widgetX;
  690. int32_t yy = m_widgetY;
  691. int32_t width = m_widgetW;
  692. int32_t height = 1;
  693. m_widgetY += DEFAULT_SPACING * 4;
  694. drawRect( (float)xx
  695. , (float)yy
  696. , (float)width
  697. , (float)height
  698. , imguiRGBA(255, 255, 255, 32)
  699. );
  700. }
  701. void drawPolygon(const float* _coords, uint32_t _numCoords, float _r, uint32_t _abgr)
  702. {
  703. _numCoords = bx::uint32_min(_numCoords, MAX_TEMP_COORDS);
  704. for (uint32_t ii = 0, jj = _numCoords - 1; ii < _numCoords; jj = ii++)
  705. {
  706. const float* v0 = &_coords[jj * 2];
  707. const float* v1 = &_coords[ii * 2];
  708. float dx = v1[0] - v0[0];
  709. float dy = v1[1] - v0[1];
  710. float d = sqrtf(dx * dx + dy * dy);
  711. if (d > 0)
  712. {
  713. d = 1.0f / d;
  714. dx *= d;
  715. dy *= d;
  716. }
  717. m_tempNormals[jj * 2 + 0] = dy;
  718. m_tempNormals[jj * 2 + 1] = -dx;
  719. }
  720. for (uint32_t ii = 0, jj = _numCoords - 1; ii < _numCoords; jj = ii++)
  721. {
  722. float dlx0 = m_tempNormals[jj * 2 + 0];
  723. float dly0 = m_tempNormals[jj * 2 + 1];
  724. float dlx1 = m_tempNormals[ii * 2 + 0];
  725. float dly1 = m_tempNormals[ii * 2 + 1];
  726. float dmx = (dlx0 + dlx1) * 0.5f;
  727. float dmy = (dly0 + dly1) * 0.5f;
  728. float dmr2 = dmx * dmx + dmy * dmy;
  729. if (dmr2 > 0.000001f)
  730. {
  731. float scale = 1.0f / dmr2;
  732. if (scale > 10.0f)
  733. {
  734. scale = 10.0f;
  735. }
  736. dmx *= scale;
  737. dmy *= scale;
  738. }
  739. m_tempCoords[ii * 2 + 0] = _coords[ii * 2 + 0] + dmx * _r;
  740. m_tempCoords[ii * 2 + 1] = _coords[ii * 2 + 1] + dmy * _r;
  741. }
  742. uint32_t numVertices = _numCoords*6 + (_numCoords-2)*3;
  743. if (bgfx::checkAvailTransientVertexBuffer(numVertices, PosColorVertex::ms_decl) )
  744. {
  745. bgfx::TransientVertexBuffer tvb;
  746. bgfx::allocTransientVertexBuffer(&tvb, numVertices, PosColorVertex::ms_decl);
  747. uint32_t trans = _abgr&0xffffff;
  748. PosColorVertex* vertex = (PosColorVertex*)tvb.data;
  749. for (uint32_t ii = 0, jj = _numCoords-1; ii < _numCoords; jj = ii++)
  750. {
  751. vertex->m_x = _coords[ii*2+0];
  752. vertex->m_y = _coords[ii*2+1];
  753. vertex->m_abgr = _abgr;
  754. ++vertex;
  755. vertex->m_x = _coords[jj*2+0];
  756. vertex->m_y = _coords[jj*2+1];
  757. vertex->m_abgr = _abgr;
  758. ++vertex;
  759. vertex->m_x = m_tempCoords[jj*2+0];
  760. vertex->m_y = m_tempCoords[jj*2+1];
  761. vertex->m_abgr = trans;
  762. ++vertex;
  763. vertex->m_x = m_tempCoords[jj*2+0];
  764. vertex->m_y = m_tempCoords[jj*2+1];
  765. vertex->m_abgr = trans;
  766. ++vertex;
  767. vertex->m_x = m_tempCoords[ii*2+0];
  768. vertex->m_y = m_tempCoords[ii*2+1];
  769. vertex->m_abgr = trans;
  770. ++vertex;
  771. vertex->m_x = _coords[ii*2+0];
  772. vertex->m_y = _coords[ii*2+1];
  773. vertex->m_abgr = _abgr;
  774. ++vertex;
  775. }
  776. for (uint32_t ii = 2; ii < _numCoords; ++ii)
  777. {
  778. vertex->m_x = _coords[0];
  779. vertex->m_y = _coords[1];
  780. vertex->m_abgr = _abgr;
  781. ++vertex;
  782. vertex->m_x = _coords[(ii-1)*2+0];
  783. vertex->m_y = _coords[(ii-1)*2+1];
  784. vertex->m_abgr = _abgr;
  785. ++vertex;
  786. vertex->m_x = _coords[ii*2+0];
  787. vertex->m_y = _coords[ii*2+1];
  788. vertex->m_abgr = _abgr;
  789. ++vertex;
  790. }
  791. bgfx::setVertexBuffer(&tvb);
  792. bgfx::setState(0
  793. | BGFX_STATE_RGB_WRITE
  794. | BGFX_STATE_ALPHA_WRITE
  795. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  796. );
  797. bgfx::setProgram(m_colorProgram);
  798. bgfx::submit(m_view);
  799. }
  800. }
  801. void drawRect(float _x, float _y, float w, float h, uint32_t _argb, float _fth = 1.0f)
  802. {
  803. float verts[4 * 2] =
  804. {
  805. _x + 0.5f, _y + 0.5f,
  806. _x + w - 0.5f, _y + 0.5f,
  807. _x + w - 0.5f, _y + h - 0.5f,
  808. _x + 0.5f, _y + h - 0.5f,
  809. };
  810. drawPolygon(verts, 4, _fth, _argb);
  811. }
  812. void drawRoundedRect(float _x, float _y, float w, float h, float r, uint32_t _argb, float _fth = 1.0f)
  813. {
  814. const uint32_t num = NUM_CIRCLE_VERTS / 4;
  815. const float* cverts = m_circleVerts;
  816. float verts[(num + 1) * 4 * 2];
  817. float* vv = verts;
  818. for (uint32_t ii = 0; ii <= num; ++ii)
  819. {
  820. *vv++ = _x + w - r + cverts[ii * 2] * r;
  821. *vv++ = _y + h - r + cverts[ii * 2 + 1] * r;
  822. }
  823. for (uint32_t ii = num; ii <= num * 2; ++ii)
  824. {
  825. *vv++ = _x + r + cverts[ii * 2] * r;
  826. *vv++ = _y + h - r + cverts[ii * 2 + 1] * r;
  827. }
  828. for (uint32_t ii = num * 2; ii <= num * 3; ++ii)
  829. {
  830. *vv++ = _x + r + cverts[ii * 2] * r;
  831. *vv++ = _y + r + cverts[ii * 2 + 1] * r;
  832. }
  833. for (uint32_t ii = num * 3; ii < num * 4; ++ii)
  834. {
  835. *vv++ = _x + w - r + cverts[ii * 2] * r;
  836. *vv++ = _y + r + cverts[ii * 2 + 1] * r;
  837. }
  838. *vv++ = _x + w - r + cverts[0] * r;
  839. *vv++ = _y + r + cverts[1] * r;
  840. drawPolygon(verts, (num + 1) * 4, _fth, _argb);
  841. }
  842. void drawLine(float _x0, float _y0, float _x1, float _y1, float _r, uint32_t _abgr, float _fth = 1.0f)
  843. {
  844. float dx = _x1 - _x0;
  845. float dy = _y1 - _y0;
  846. float d = sqrtf(dx * dx + dy * dy);
  847. if (d > 0.0001f)
  848. {
  849. d = 1.0f / d;
  850. dx *= d;
  851. dy *= d;
  852. }
  853. float nx = dy;
  854. float ny = -dx;
  855. float verts[4 * 2];
  856. _r -= _fth;
  857. _r *= 0.5f;
  858. if (_r < 0.01f)
  859. {
  860. _r = 0.01f;
  861. }
  862. dx *= _r;
  863. dy *= _r;
  864. nx *= _r;
  865. ny *= _r;
  866. verts[0] = _x0 - dx - nx;
  867. verts[1] = _y0 - dy - ny;
  868. verts[2] = _x0 - dx + nx;
  869. verts[3] = _y0 - dy + ny;
  870. verts[4] = _x1 + dx + nx;
  871. verts[5] = _y1 + dy + ny;
  872. verts[6] = _x1 + dx - nx;
  873. verts[7] = _y1 + dy - ny;
  874. drawPolygon(verts, 4, _fth, _abgr);
  875. }
  876. void drawTriangle(int32_t _x, int32_t _y, int32_t _width, int32_t _height, int32_t _flags, uint32_t _abgr)
  877. {
  878. if (1 == _flags)
  879. {
  880. const float verts[3 * 2] =
  881. {
  882. (float)_x + 0.5f, (float)_y + 0.5f,
  883. (float)_x + 0.5f + (float)_width * 1.0f, (float)_y + 0.5f + (float)_height / 2.0f - 0.5f,
  884. (float)_x + 0.5f, (float)_y + 0.5f + (float)_height - 1.0f,
  885. };
  886. drawPolygon(verts, 3, 1.0f, _abgr);
  887. }
  888. else
  889. {
  890. const float verts[3 * 2] =
  891. {
  892. (float)_x + 0.5f, (float)_y + 0.5f + (float)_height - 1.0f,
  893. (float)_x + 0.5f + (float)_width / 2.0f - 0.5f, (float)_y + 0.5f,
  894. (float)_x + 0.5f + (float)_width - 1.0f, (float)_y + 0.5f + (float)_height - 1.0f,
  895. };
  896. drawPolygon(verts, 3, 1.0f, _abgr);
  897. }
  898. }
  899. void getBakedQuad(stbtt_bakedchar* _chardata, int32_t char_index, float* _xpos, float* _ypos, stbtt_aligned_quad* _quad)
  900. {
  901. stbtt_bakedchar* b = _chardata + char_index;
  902. int32_t round_x = STBTT_ifloor(*_xpos + b->xoff);
  903. int32_t round_y = STBTT_ifloor(*_ypos + b->yoff);
  904. _quad->x0 = (float)round_x;
  905. _quad->y0 = (float)round_y;
  906. _quad->x1 = (float)round_x + b->x1 - b->x0;
  907. _quad->y1 = (float)round_y + b->y1 - b->y0;
  908. _quad->s0 = (b->x0 + m_halfTexel) * m_invTextureWidth;
  909. _quad->t0 = (b->y0 + m_halfTexel) * m_invTextureWidth;
  910. _quad->s1 = (b->x1 + m_halfTexel) * m_invTextureHeight;
  911. _quad->t1 = (b->y1 + m_halfTexel) * m_invTextureHeight;
  912. *_xpos += b->xadvance;
  913. }
  914. float getTextLength(stbtt_bakedchar* _chardata, const char* _text, uint32_t& _numVertices)
  915. {
  916. float xpos = 0;
  917. float len = 0;
  918. uint32_t numVertices = 0;
  919. while (*_text)
  920. {
  921. int32_t ch = (uint8_t)*_text;
  922. if (ch == '\t')
  923. {
  924. for (int32_t ii = 0; ii < 4; ++ii)
  925. {
  926. if (xpos < s_tabStops[ii])
  927. {
  928. xpos = s_tabStops[ii];
  929. break;
  930. }
  931. }
  932. }
  933. else if (ch >= ' '
  934. && ch < 128)
  935. {
  936. stbtt_bakedchar* b = _chardata + ch - ' ';
  937. int32_t round_x = STBTT_ifloor( (xpos + b->xoff) + 0.5);
  938. len = round_x + b->x1 - b->x0 + 0.5f;
  939. xpos += b->xadvance;
  940. numVertices += 6;
  941. }
  942. ++_text;
  943. }
  944. _numVertices = numVertices;
  945. return len;
  946. }
  947. void drawText(int32_t _x, int32_t _y, imguiTextAlign _align, const char* _text, uint32_t _abgr)
  948. {
  949. drawText( (float)_x, (float)_y, _text, _align, _abgr);
  950. }
  951. void drawText(float _x, float _y, const char* _text, imguiTextAlign _align, uint32_t _abgr)
  952. {
  953. if (!_text)
  954. {
  955. return;
  956. }
  957. uint32_t numVertices = 0;
  958. if (_align == IMGUI_ALIGN_CENTER)
  959. {
  960. _x -= getTextLength(m_cdata, _text, numVertices) / 2;
  961. }
  962. else if (_align == IMGUI_ALIGN_RIGHT)
  963. {
  964. _x -= getTextLength(m_cdata, _text, numVertices);
  965. }
  966. else // just count vertices
  967. {
  968. getTextLength(m_cdata, _text, numVertices);
  969. }
  970. if (bgfx::checkAvailTransientVertexBuffer(numVertices, PosColorUvVertex::ms_decl) )
  971. {
  972. bgfx::TransientVertexBuffer tvb;
  973. bgfx::allocTransientVertexBuffer(&tvb, numVertices, PosColorUvVertex::ms_decl);
  974. PosColorUvVertex* vertex = (PosColorUvVertex*)tvb.data;
  975. const float ox = _x;
  976. while (*_text)
  977. {
  978. int32_t ch = (uint8_t)*_text;
  979. if (ch == '\t')
  980. {
  981. for (int32_t i = 0; i < 4; ++i)
  982. {
  983. if (_x < s_tabStops[i] + ox)
  984. {
  985. _x = s_tabStops[i] + ox;
  986. break;
  987. }
  988. }
  989. }
  990. else if (ch >= ' '
  991. && ch < 128)
  992. {
  993. stbtt_aligned_quad quad;
  994. getBakedQuad(m_cdata, ch - 32, &_x, &_y, &quad);
  995. vertex->m_x = quad.x0;
  996. vertex->m_y = quad.y0;
  997. vertex->m_u = quad.s0;
  998. vertex->m_v = quad.t0;
  999. vertex->m_abgr = _abgr;
  1000. ++vertex;
  1001. vertex->m_x = quad.x1;
  1002. vertex->m_y = quad.y1;
  1003. vertex->m_u = quad.s1;
  1004. vertex->m_v = quad.t1;
  1005. vertex->m_abgr = _abgr;
  1006. ++vertex;
  1007. vertex->m_x = quad.x1;
  1008. vertex->m_y = quad.y0;
  1009. vertex->m_u = quad.s1;
  1010. vertex->m_v = quad.t0;
  1011. vertex->m_abgr = _abgr;
  1012. ++vertex;
  1013. vertex->m_x = quad.x0;
  1014. vertex->m_y = quad.y0;
  1015. vertex->m_u = quad.s0;
  1016. vertex->m_v = quad.t0;
  1017. vertex->m_abgr = _abgr;
  1018. ++vertex;
  1019. vertex->m_x = quad.x0;
  1020. vertex->m_y = quad.y1;
  1021. vertex->m_u = quad.s0;
  1022. vertex->m_v = quad.t1;
  1023. vertex->m_abgr = _abgr;
  1024. ++vertex;
  1025. vertex->m_x = quad.x1;
  1026. vertex->m_y = quad.y1;
  1027. vertex->m_u = quad.s1;
  1028. vertex->m_v = quad.t1;
  1029. vertex->m_abgr = _abgr;
  1030. ++vertex;
  1031. }
  1032. ++_text;
  1033. }
  1034. bgfx::setTexture(0, u_texColor, m_fontTexture);
  1035. bgfx::setVertexBuffer(&tvb);
  1036. bgfx::setState(0
  1037. | BGFX_STATE_RGB_WRITE
  1038. | BGFX_STATE_ALPHA_WRITE
  1039. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  1040. );
  1041. bgfx::setProgram(m_textureProgram);
  1042. bgfx::submit(m_view);
  1043. }
  1044. }
  1045. int32_t m_mx;
  1046. int32_t m_my;
  1047. int32_t m_scroll;
  1048. uint32_t m_active;
  1049. uint32_t m_hot;
  1050. uint32_t m_hotToBe;
  1051. int32_t m_dragX;
  1052. int32_t m_dragY;
  1053. float m_dragOrig;
  1054. int32_t m_widgetX;
  1055. int32_t m_widgetY;
  1056. int32_t m_widgetW;
  1057. bool m_left;
  1058. bool m_leftPressed;
  1059. bool m_leftReleased;
  1060. bool m_isHot;
  1061. bool m_isActive;
  1062. bool m_wentActive;
  1063. bool m_insideCurrentScroll;
  1064. uint32_t m_areaId;
  1065. uint32_t m_widgetId;
  1066. float m_tempCoords[MAX_TEMP_COORDS * 2];
  1067. float m_tempNormals[MAX_TEMP_COORDS * 2];
  1068. float m_circleVerts[NUM_CIRCLE_VERTS * 2];
  1069. int32_t m_scrollTop;
  1070. int32_t m_scrollBottom;
  1071. int32_t m_scrollRight;
  1072. int32_t m_scrollAreaTop;
  1073. int32_t* m_scrollVal;
  1074. int32_t m_focusTop;
  1075. int32_t m_focusBottom;
  1076. uint32_t m_scrollId;
  1077. bool m_insideScrollArea;
  1078. stbtt_bakedchar m_cdata[96]; // ASCII 32..126 is 95 glyphs
  1079. uint16_t m_textureWidth;
  1080. uint16_t m_textureHeight;
  1081. float m_invTextureWidth;
  1082. float m_invTextureHeight;
  1083. float m_halfTexel;
  1084. uint8_t m_view;
  1085. bgfx::UniformHandle u_texColor;
  1086. bgfx::TextureHandle m_fontTexture;
  1087. bgfx::ProgramHandle m_colorProgram;
  1088. bgfx::ProgramHandle m_textureProgram;
  1089. };
  1090. static Imgui s_imgui;
  1091. bool imguiCreate(void* _data, uint32_t _size)
  1092. {
  1093. return s_imgui.create(_data, _size);
  1094. }
  1095. void imguiDestroy()
  1096. {
  1097. s_imgui.destroy();
  1098. }
  1099. void imguiBeginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, uint16_t _width, uint16_t _height, uint8_t _view)
  1100. {
  1101. s_imgui.beginFrame(_mx, _my, _button, _scroll, _width, _height, _view);
  1102. }
  1103. void imguiEndFrame()
  1104. {
  1105. s_imgui.endFrame();
  1106. }
  1107. bool imguiBeginScrollArea(const char* _name, int32_t _x, int32_t _y, int32_t _width, int32_t _height, int32_t* _scroll)
  1108. {
  1109. return s_imgui.beginScrollArea(_name, _x, _y, _width, _height, _scroll);
  1110. }
  1111. void imguiEndScrollArea()
  1112. {
  1113. return s_imgui.endScrollArea();
  1114. }
  1115. void imguiIndent()
  1116. {
  1117. s_imgui.indent();
  1118. }
  1119. void imguiUnindent()
  1120. {
  1121. s_imgui.unindent();
  1122. }
  1123. void imguiSeparator()
  1124. {
  1125. s_imgui.separator();
  1126. }
  1127. void imguiSeparatorLine()
  1128. {
  1129. s_imgui.separatorLine();
  1130. }
  1131. bool imguiButton(const char* _text, bool _enabled)
  1132. {
  1133. return s_imgui.button(_text, _enabled);
  1134. }
  1135. bool imguiItem(const char* _text, bool _enabled)
  1136. {
  1137. return s_imgui.item(_text, _enabled);
  1138. }
  1139. bool imguiCheck(const char* _text, bool _checked, bool _enabled)
  1140. {
  1141. return s_imgui.check(_text, _checked, _enabled);
  1142. }
  1143. bool imguiCollapse(const char* _text, const char* _subtext, bool _checked, bool _enabled)
  1144. {
  1145. return s_imgui.collapse(_text, _subtext, _checked, _enabled);
  1146. }
  1147. void imguiLabel(const char* _format, ...)
  1148. {
  1149. va_list argList;
  1150. va_start(argList, _format);
  1151. s_imgui.labelVargs(_format, argList);
  1152. va_end(argList);
  1153. }
  1154. void imguiValue(const char* _text)
  1155. {
  1156. s_imgui.value(_text);
  1157. }
  1158. bool imguiSlider(const char* _text, float* _val, float _vmin, float _vmax, float _vinc, bool _enabled)
  1159. {
  1160. return s_imgui.slider(_text, _val, _vmin, _vmax, _vinc, _enabled);
  1161. }
  1162. void imguiDrawText(int32_t _x, int32_t _y, imguiTextAlign _align, const char* _text, uint32_t _argb)
  1163. {
  1164. s_imgui.drawText(_x, _y, _align, _text, _argb);
  1165. }
  1166. void imguiDrawLine(float _x0, float _y0, float _x1, float _y1, float _r, uint32_t _argb)
  1167. {
  1168. s_imgui.drawLine(_x0, _y0, _x1, _y1, _r, _argb);
  1169. }
  1170. void imguiDrawRoundedRect(float _x, float _y, float _width, float _height, float _r, uint32_t _argb)
  1171. {
  1172. s_imgui.drawRoundedRect(_x, _y, _width, _height, _r, _argb);
  1173. }
  1174. void imguiDrawRect(float _x, float _y, float _width, float _height, uint32_t _argb)
  1175. {
  1176. s_imgui.drawRect(_x, _y, _width, _height, _argb);
  1177. }