imgui.cpp 52 KB

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