imgui.cpp 50 KB

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