imgui.cpp 55 KB

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