imgui.cpp 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104
  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 "fs_imgui_image_swizz.bin.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 AREA_HEADER = 20;
  51. static const int32_t COLOR_WHEEL_PADDING = 60;
  52. static const float s_tabStops[4] = {150, 210, 270, 330};
  53. static void* imguiMalloc(size_t size, void* /*_userptr*/)
  54. {
  55. return malloc(size);
  56. }
  57. static void imguiFree(void* _ptr, void* /*_userptr*/)
  58. {
  59. free(_ptr);
  60. }
  61. #define IMGUI_MIN(_a, _b) (_a)<(_b)?(_a):(_b)
  62. #define IMGUI_MAX(_a, _b) (_a)>(_b)?(_a):(_b)
  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. #if !USE_NANOVG_FONT
  220. static float getTextLength(stbtt_bakedchar* _chardata, const char* _text, uint32_t& _numVertices)
  221. {
  222. float xpos = 0;
  223. float len = 0;
  224. uint32_t numVertices = 0;
  225. while (*_text)
  226. {
  227. int32_t ch = (uint8_t)*_text;
  228. if (ch == '\t')
  229. {
  230. for (int32_t ii = 0; ii < 4; ++ii)
  231. {
  232. if (xpos < s_tabStops[ii])
  233. {
  234. xpos = s_tabStops[ii];
  235. break;
  236. }
  237. }
  238. }
  239. else if (ch >= ' '
  240. && ch < 128)
  241. {
  242. stbtt_bakedchar* b = _chardata + ch - ' ';
  243. int32_t round_x = STBTT_ifloor( (xpos + b->xoff) + 0.5);
  244. len = round_x + b->x1 - b->x0 + 0.5f;
  245. xpos += b->xadvance;
  246. numVertices += 6;
  247. }
  248. ++_text;
  249. }
  250. _numVertices = numVertices;
  251. return len;
  252. }
  253. #endif // !USE_NANOVG_FONT
  254. struct Imgui
  255. {
  256. Imgui()
  257. : m_mx(-1)
  258. , m_my(-1)
  259. , m_scroll(0)
  260. , m_active(0)
  261. , m_hot(0)
  262. , m_hotToBe(0)
  263. , m_dragX(0)
  264. , m_dragY(0)
  265. , m_dragOrig(0)
  266. , m_left(false)
  267. , m_leftPressed(false)
  268. , m_leftReleased(false)
  269. , m_isHot(false)
  270. , m_wentActive(false)
  271. , m_insideArea(false)
  272. , m_isActivePresent(false)
  273. , m_checkActivePresence(false)
  274. , m_widgetId(0)
  275. , m_enabledAreaIds(0)
  276. , m_textureWidth(512)
  277. , m_textureHeight(512)
  278. , m_halfTexel(0.0f)
  279. , m_nvg(NULL)
  280. , m_view(31)
  281. , m_viewWidth(0)
  282. , m_viewHeight(0)
  283. , m_currentFontIdx(0)
  284. {
  285. m_areaId.reset();
  286. m_invTextureWidth = 1.0f/m_textureWidth;
  287. m_invTextureHeight = 1.0f/m_textureHeight;
  288. u_imageLod.idx = bgfx::invalidHandle;
  289. u_imageSwizzle.idx = bgfx::invalidHandle;
  290. s_texColor.idx = bgfx::invalidHandle;
  291. m_missingTexture.idx = bgfx::invalidHandle;
  292. m_colorProgram.idx = bgfx::invalidHandle;
  293. m_textureProgram.idx = bgfx::invalidHandle;
  294. m_imageProgram.idx = bgfx::invalidHandle;
  295. m_imageSwizzProgram.idx = bgfx::invalidHandle;
  296. }
  297. ImguiFontHandle createFont(const void* _data, float _fontSize)
  298. {
  299. #if !USE_NANOVG_FONT
  300. const ImguiFontHandle handle = { m_fontHandle.alloc() };
  301. const bgfx::Memory* mem = bgfx::alloc(m_textureWidth * m_textureHeight);
  302. stbtt_BakeFontBitmap( (uint8_t*)_data, 0, _fontSize, mem->data, m_textureWidth, m_textureHeight, 32, 96, m_fonts[handle.idx].m_cdata);
  303. m_fonts[handle.idx].m_texture = bgfx::createTexture2D(m_textureWidth, m_textureHeight, 1, bgfx::TextureFormat::R8, BGFX_TEXTURE_NONE, mem);
  304. m_fonts[handle.idx].m_size = _fontSize;
  305. #else
  306. const ImguiFontHandle handle = { bgfx::invalidHandle };
  307. #endif // !USE_NANOVG_FONT
  308. return handle;
  309. }
  310. void setFont(ImguiFontHandle _handle)
  311. {
  312. if (isValid(_handle) )
  313. {
  314. m_currentFontIdx = _handle.idx;
  315. }
  316. }
  317. bgfx::TextureHandle genMissingTexture(uint32_t _width, uint32_t _height, float _lineWidth = 0.02f)
  318. {
  319. const bgfx::Memory* mem = bgfx::alloc(_width*_height*4);
  320. uint32_t* bgra8 = (uint32_t*)mem->data;
  321. const float sx = 0.70710677f;
  322. const float cx = 0.70710677f;
  323. for (uint32_t yy = 0; yy < _height; ++yy)
  324. {
  325. for (uint32_t xx = 0; xx < _width; ++xx)
  326. {
  327. float px = xx / float(_width) * 2.0f - 1.0f;
  328. float py = yy / float(_height) * 2.0f - 1.0f;
  329. float sum = bx::fpulse(px * cx - py * sx, _lineWidth, -_lineWidth)
  330. + bx::fpulse(px * sx + py * cx, _lineWidth, -_lineWidth)
  331. ;
  332. *bgra8++ = sum >= 1.0f ? 0xffff0000 : 0xffffffff;
  333. }
  334. }
  335. return bgfx::createTexture2D(_width, _height, 0, bgfx::TextureFormat::BGRA8, 0, mem);
  336. }
  337. ImguiFontHandle create(const void* _data, float _fontSize)
  338. {
  339. m_nvg = nvgCreate(512, 512, 1, m_view);
  340. nvgCreateFontMem(m_nvg, "default", (unsigned char*)_data, INT32_MAX, 0);
  341. nvgFontSize(m_nvg, _fontSize);
  342. nvgFontFace(m_nvg, "default");
  343. for (int32_t ii = 0; ii < NUM_CIRCLE_VERTS; ++ii)
  344. {
  345. float a = (float)ii / (float)NUM_CIRCLE_VERTS * (float)(M_PI * 2.0);
  346. m_circleVerts[ii * 2 + 0] = cosf(a);
  347. m_circleVerts[ii * 2 + 1] = sinf(a);
  348. }
  349. PosColorVertex::init();
  350. PosColorUvVertex::init();
  351. PosUvVertex::init();
  352. u_imageLod = bgfx::createUniform("u_imageLod", bgfx::UniformType::Uniform1f);
  353. u_imageSwizzle = bgfx::createUniform("u_swizzle", bgfx::UniformType::Uniform4fv);
  354. s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Uniform1i);
  355. const bgfx::Memory* vs_imgui_color;
  356. const bgfx::Memory* fs_imgui_color;
  357. const bgfx::Memory* vs_imgui_texture;
  358. const bgfx::Memory* fs_imgui_texture;
  359. const bgfx::Memory* vs_imgui_image;
  360. const bgfx::Memory* fs_imgui_image;
  361. const bgfx::Memory* fs_imgui_image_swizz;
  362. switch (bgfx::getRendererType() )
  363. {
  364. case bgfx::RendererType::Direct3D9:
  365. vs_imgui_color = bgfx::makeRef(vs_imgui_color_dx9, sizeof(vs_imgui_color_dx9) );
  366. fs_imgui_color = bgfx::makeRef(fs_imgui_color_dx9, sizeof(fs_imgui_color_dx9) );
  367. vs_imgui_texture = bgfx::makeRef(vs_imgui_texture_dx9, sizeof(vs_imgui_texture_dx9) );
  368. fs_imgui_texture = bgfx::makeRef(fs_imgui_texture_dx9, sizeof(fs_imgui_texture_dx9) );
  369. vs_imgui_image = bgfx::makeRef(vs_imgui_image_dx9, sizeof(vs_imgui_image_dx9) );
  370. fs_imgui_image = bgfx::makeRef(fs_imgui_image_dx9, sizeof(fs_imgui_image_dx9) );
  371. fs_imgui_image_swizz = bgfx::makeRef(fs_imgui_image_swizz_dx9, sizeof(fs_imgui_image_swizz_dx9) );
  372. m_halfTexel = 0.5f;
  373. break;
  374. case bgfx::RendererType::Direct3D11:
  375. vs_imgui_color = bgfx::makeRef(vs_imgui_color_dx11, sizeof(vs_imgui_color_dx11) );
  376. fs_imgui_color = bgfx::makeRef(fs_imgui_color_dx11, sizeof(fs_imgui_color_dx11) );
  377. vs_imgui_texture = bgfx::makeRef(vs_imgui_texture_dx11, sizeof(vs_imgui_texture_dx11) );
  378. fs_imgui_texture = bgfx::makeRef(fs_imgui_texture_dx11, sizeof(fs_imgui_texture_dx11) );
  379. vs_imgui_image = bgfx::makeRef(vs_imgui_image_dx11, sizeof(vs_imgui_image_dx11) );
  380. fs_imgui_image = bgfx::makeRef(fs_imgui_image_dx11, sizeof(fs_imgui_image_dx11) );
  381. fs_imgui_image_swizz = bgfx::makeRef(fs_imgui_image_swizz_dx11, sizeof(fs_imgui_image_swizz_dx11) );
  382. break;
  383. default:
  384. vs_imgui_color = bgfx::makeRef(vs_imgui_color_glsl, sizeof(vs_imgui_color_glsl) );
  385. fs_imgui_color = bgfx::makeRef(fs_imgui_color_glsl, sizeof(fs_imgui_color_glsl) );
  386. vs_imgui_texture = bgfx::makeRef(vs_imgui_texture_glsl, sizeof(vs_imgui_texture_glsl) );
  387. fs_imgui_texture = bgfx::makeRef(fs_imgui_texture_glsl, sizeof(fs_imgui_texture_glsl) );
  388. vs_imgui_image = bgfx::makeRef(vs_imgui_image_glsl, sizeof(vs_imgui_image_glsl) );
  389. fs_imgui_image = bgfx::makeRef(fs_imgui_image_glsl, sizeof(fs_imgui_image_glsl) );
  390. fs_imgui_image_swizz = bgfx::makeRef(fs_imgui_image_swizz_glsl, sizeof(fs_imgui_image_swizz_glsl) );
  391. break;
  392. }
  393. bgfx::ShaderHandle vsh;
  394. bgfx::ShaderHandle fsh;
  395. vsh = bgfx::createShader(vs_imgui_color);
  396. fsh = bgfx::createShader(fs_imgui_color);
  397. m_colorProgram = bgfx::createProgram(vsh, fsh);
  398. bgfx::destroyShader(vsh);
  399. bgfx::destroyShader(fsh);
  400. vsh = bgfx::createShader(vs_imgui_texture);
  401. fsh = bgfx::createShader(fs_imgui_texture);
  402. m_textureProgram = bgfx::createProgram(vsh, fsh);
  403. bgfx::destroyShader(vsh);
  404. bgfx::destroyShader(fsh);
  405. vsh = bgfx::createShader(vs_imgui_image);
  406. fsh = bgfx::createShader(fs_imgui_image);
  407. m_imageProgram = bgfx::createProgram(vsh, fsh);
  408. bgfx::destroyShader(fsh);
  409. // Notice: using the same vsh.
  410. fsh = bgfx::createShader(fs_imgui_image_swizz);
  411. m_imageSwizzProgram = bgfx::createProgram(vsh, fsh);
  412. bgfx::destroyShader(fsh);
  413. bgfx::destroyShader(vsh);
  414. m_missingTexture = genMissingTexture(256, 256, 0.04f);
  415. #if !USE_NANOVG_FONT
  416. const ImguiFontHandle handle = createFont(_data, _fontSize);
  417. m_currentFontIdx = handle.idx;
  418. #else
  419. const ImguiFontHandle handle = { bgfx::invalidHandle };
  420. #endif // !USE_NANOVG_FONT
  421. return handle;
  422. }
  423. void destroy()
  424. {
  425. bgfx::destroyUniform(u_imageLod);
  426. bgfx::destroyUniform(u_imageSwizzle);
  427. bgfx::destroyUniform(s_texColor);
  428. #if !USE_NANOVG_FONT
  429. for (uint16_t ii = 0; ii < IMGUI_CONFIG_MAX_FONTS; ++ii)
  430. {
  431. if (bgfx::isValid(m_fonts[ii].m_texture) )
  432. {
  433. bgfx::destroyTexture(m_fonts[ii].m_texture);
  434. }
  435. }
  436. #endif // !USE_NANOVG_FONT
  437. bgfx::destroyTexture(m_missingTexture);
  438. bgfx::destroyProgram(m_colorProgram);
  439. bgfx::destroyProgram(m_textureProgram);
  440. bgfx::destroyProgram(m_imageProgram);
  441. bgfx::destroyProgram(m_imageSwizzProgram);
  442. nvgDelete(m_nvg);
  443. }
  444. bool anyActive() const
  445. {
  446. return m_active != 0;
  447. }
  448. inline void updatePresence(uint32_t _id)
  449. {
  450. if (m_checkActivePresence && m_active == _id)
  451. {
  452. m_isActivePresent = true;
  453. }
  454. }
  455. uint32_t getId()
  456. {
  457. const uint32_t id = (m_areaId << 16) | m_widgetId++;
  458. updatePresence(id);
  459. return id;
  460. }
  461. bool isActive(uint32_t _id) const
  462. {
  463. return m_active == _id;
  464. }
  465. bool isActiveInputField(uint32_t _id) const
  466. {
  467. return m_inputField == _id;
  468. }
  469. bool isHot(uint32_t _id) const
  470. {
  471. return m_hot == _id;
  472. }
  473. bool inRect(int32_t _x, int32_t _y, int32_t _width, int32_t _height, bool _checkScroll = true) const
  474. {
  475. return (!_checkScroll || m_areas[m_areaId].m_inside)
  476. && m_mx >= _x
  477. && m_mx <= _x + _width
  478. && m_my >= _y
  479. && m_my <= _y + _height;
  480. }
  481. bool isEnabled(uint16_t _areaId)
  482. {
  483. return (m_enabledAreaIds>>_areaId)&0x1;
  484. }
  485. void setEnabled(uint16_t _areaId)
  486. {
  487. m_enabledAreaIds |= (UINT64_C(1)<<_areaId);
  488. }
  489. void clearInput()
  490. {
  491. m_leftPressed = false;
  492. m_leftReleased = false;
  493. m_scroll = 0;
  494. }
  495. void clearActive()
  496. {
  497. m_active = 0;
  498. // mark all UI for this frame as processed
  499. clearInput();
  500. }
  501. void clearActiveInputField()
  502. {
  503. m_inputField = 0;
  504. }
  505. void setActive(uint32_t _id)
  506. {
  507. m_active = _id;
  508. m_wentActive = true;
  509. m_inputField = 0;
  510. }
  511. void setActiveInputField(uint32_t _id)
  512. {
  513. m_inputField = _id;
  514. }
  515. void setHot(uint32_t _id)
  516. {
  517. m_hotToBe = _id;
  518. }
  519. bool buttonLogic(uint32_t _id, bool _over)
  520. {
  521. bool res = false;
  522. // process down
  523. if (!anyActive() )
  524. {
  525. if (_over)
  526. {
  527. setHot(_id);
  528. }
  529. if (isHot(_id)
  530. && m_leftPressed)
  531. {
  532. setActive(_id);
  533. }
  534. }
  535. // if button is active, then react on left up
  536. if (isActive(_id) )
  537. {
  538. if (_over)
  539. {
  540. setHot(_id);
  541. }
  542. if (m_leftReleased)
  543. {
  544. if (isHot(_id) )
  545. {
  546. res = true;
  547. }
  548. clearActive();
  549. }
  550. }
  551. if (isHot(_id) )
  552. {
  553. m_isHot = true;
  554. }
  555. return res;
  556. }
  557. void inputLogic(uint32_t _id, bool _over)
  558. {
  559. if (!anyActive() )
  560. {
  561. if (_over)
  562. {
  563. setHot(_id);
  564. }
  565. if (isHot(_id)
  566. && m_leftPressed)
  567. {
  568. // Toggle active input.
  569. if (isActiveInputField(_id))
  570. {
  571. clearActiveInputField();
  572. }
  573. else
  574. {
  575. setActiveInputField(_id);
  576. }
  577. }
  578. }
  579. if (isHot(_id) )
  580. {
  581. m_isHot = true;
  582. }
  583. if (m_leftPressed
  584. && !m_isHot
  585. && m_inputField != 0)
  586. {
  587. clearActiveInputField();
  588. }
  589. }
  590. void updateInput(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, char _inputChar)
  591. {
  592. bool left = (_button & IMGUI_MBUT_LEFT) != 0;
  593. m_mx = _mx;
  594. m_my = _my;
  595. m_leftPressed = !m_left && left;
  596. m_leftReleased = m_left && !left;
  597. m_left = left;
  598. m_scroll = _scroll;
  599. _inputChar = _inputChar & 0x7f; // ASCII or GTFO! :)
  600. m_lastChar = m_char;
  601. m_char = _inputChar;
  602. }
  603. 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)
  604. {
  605. nvgBeginFrame(m_nvg, _width, _height, 1.0f, NVG_STRAIGHT_ALPHA);
  606. m_view = _view;
  607. m_viewWidth = _width;
  608. m_viewHeight = _height;
  609. bgfx::setViewSeq(_view, true);
  610. bgfx::setViewRect(_view, 0, 0, _width, _height);
  611. float proj[16];
  612. bx::mtxOrtho(proj, 0.0f, (float)_width, (float)_height, 0.0f, 0.0f, 1000.0f);
  613. bgfx::setViewTransform(_view, NULL, proj);
  614. updateInput(_mx, _my, _button, _scroll, _inputChar);
  615. m_hot = m_hotToBe;
  616. m_hotToBe = 0;
  617. m_wentActive = false;
  618. m_isHot = false;
  619. Area& area = getCurrentArea();
  620. area.m_widgetX = 0;
  621. area.m_widgetY = 0;
  622. area.m_widgetW = 0;
  623. m_areaId.reset();
  624. m_widgetId = 0;
  625. m_enabledAreaIds = 0;
  626. m_insideArea = false;
  627. m_isActivePresent = false;
  628. }
  629. void endFrame()
  630. {
  631. if (m_checkActivePresence && !m_isActivePresent)
  632. {
  633. // The ui element is not present any more, reset active field.
  634. m_active = 0;
  635. }
  636. m_checkActivePresence = (0 != m_active);
  637. clearInput();
  638. nvgEndFrame(m_nvg);
  639. }
  640. bool beginScroll(int32_t _height, int32_t* _scroll, bool _enabled)
  641. {
  642. Area& parentArea = getCurrentArea();
  643. m_areaId.next();
  644. const uint32_t scrollId = getId();
  645. Area& area = getCurrentArea();
  646. const uint16_t parentBottom = parentArea.m_scissorY + parentArea.m_scissorHeight;
  647. const uint16_t childBottom = parentArea.m_widgetY + _height;
  648. const uint16_t bottom = IMGUI_MIN(childBottom, parentBottom);
  649. const uint16_t top = IMGUI_MAX(parentArea.m_widgetY, parentArea.m_scissorY);
  650. area.m_contentX = parentArea.m_contentX;
  651. area.m_contentY = parentArea.m_widgetY;
  652. area.m_contentWidth = parentArea.m_contentWidth - (SCROLL_AREA_PADDING*3);
  653. area.m_contentHeight = _height;
  654. area.m_widgetX = parentArea.m_widgetX;
  655. area.m_widgetY = parentArea.m_widgetY + (*_scroll);
  656. area.m_widgetW = parentArea.m_widgetW - (SCROLL_AREA_PADDING*3);
  657. area.m_scissorX = area.m_contentX;
  658. area.m_scissorWidth = area.m_contentWidth;
  659. area.m_scissorY = top;
  660. area.m_scissorHeight = bottom - top;
  661. area.m_scissorEnabled = true;
  662. area.m_height = _height;
  663. area.m_scrollVal = _scroll;
  664. area.m_scrollId = scrollId;
  665. area.m_inside = inRect(parentArea.m_scissorX
  666. , area.m_scissorY
  667. , parentArea.m_scissorWidth
  668. , area.m_scissorHeight
  669. , false
  670. );
  671. parentArea.m_widgetY += (_height + DEFAULT_SPACING);
  672. if (_enabled)
  673. {
  674. setEnabled(m_areaId);
  675. }
  676. m_insideArea |= area.m_inside;
  677. return area.m_inside;
  678. }
  679. void endScroll(int32_t _r)
  680. {
  681. Area& area = getCurrentArea();
  682. area.m_scissorEnabled = false;
  683. const int32_t xx = area.m_contentX + area.m_contentWidth - 1;
  684. const int32_t yy = area.m_contentY;
  685. const int32_t width = SCROLL_AREA_PADDING * 2;
  686. const int32_t height = area.m_height;
  687. const int32_t stop = area.m_contentY + (*area.m_scrollVal);
  688. const int32_t sbot = area.m_widgetY - DEFAULT_SPACING;
  689. const int32_t sh = IMGUI_MAX(1, sbot - stop); // The scrollable area height.
  690. const float barHeight = (float)height / (float)sh;
  691. const int32_t diff = height - sh;
  692. if (diff < 0)
  693. {
  694. *area.m_scrollVal = (*area.m_scrollVal > diff) ? *area.m_scrollVal : diff;
  695. }
  696. else
  697. {
  698. *area.m_scrollVal = 0;
  699. }
  700. if (barHeight < 1.0f)
  701. {
  702. float barY = bx::fsaturate( (float)(yy - stop) / (float)sh);
  703. // Handle scroll bar logic.
  704. const uint32_t hid = area.m_scrollId;
  705. const int32_t hx = xx;
  706. const int32_t hy = yy + (int)(barY * height);
  707. const int32_t hw = width;
  708. const int32_t hh = (int)(barHeight * height);
  709. const int32_t range = height - (hh - 1);
  710. const bool over = inRect(hx, hy, hw, hh);
  711. buttonLogic(hid, over);
  712. if (isActive(hid) )
  713. {
  714. float uu = (float)(hy - yy) / (float)range;
  715. if (m_wentActive)
  716. {
  717. m_dragY = m_my;
  718. m_dragOrig = uu;
  719. }
  720. if (m_dragY != m_my)
  721. {
  722. uu = bx::fsaturate(m_dragOrig + (m_my - m_dragY) / (float)range);
  723. *area.m_scrollVal = (int)(uu * (height - sh) );
  724. }
  725. }
  726. // BG
  727. if (0 == _r)
  728. {
  729. drawRect( (float)xx
  730. , (float)yy
  731. , (float)width
  732. , (float)height
  733. , imguiRGBA(0, 0, 0, 196)
  734. );
  735. }
  736. else
  737. {
  738. drawRoundedRect( (float)xx
  739. , (float)yy
  740. , (float)width
  741. , (float)height
  742. , (float)_r
  743. , imguiRGBA(0, 0, 0, 196)
  744. );
  745. }
  746. // Bar
  747. if (isActive(hid) )
  748. {
  749. if (0 == _r)
  750. {
  751. drawRect( (float)hx
  752. , (float)hy
  753. , (float)hw
  754. , (float)hh
  755. , imguiRGBA(255, 196, 0, 196)
  756. );
  757. }
  758. else
  759. {
  760. drawRoundedRect( (float)hx
  761. , (float)hy
  762. , (float)hw
  763. , (float)hh
  764. , (float)_r
  765. , imguiRGBA(255, 196, 0, 196)
  766. );
  767. }
  768. }
  769. else
  770. {
  771. if (0 == _r)
  772. {
  773. drawRect( (float)hx
  774. , (float)hy
  775. , (float)hw
  776. , (float)hh
  777. , isHot(hid) ? imguiRGBA(255, 196, 0, 96) : imguiRGBA(255, 255, 255, 64)
  778. );
  779. }
  780. else
  781. {
  782. drawRoundedRect( (float)hx
  783. , (float)hy
  784. , (float)hw
  785. , (float)hh
  786. , (float)_r
  787. , isHot(hid) ? imguiRGBA(255, 196, 0, 96) : imguiRGBA(255, 255, 255, 64)
  788. );
  789. }
  790. }
  791. // Handle mouse scrolling.
  792. if (area.m_inside) // && !anyActive() )
  793. {
  794. if (m_scroll)
  795. {
  796. *area.m_scrollVal += bx::uint32_clamp(20 * m_scroll, 0, sh - height);
  797. }
  798. }
  799. }
  800. area.m_inside = false;
  801. m_areaId.previous();
  802. }
  803. bool beginArea(const char* _name, int32_t _x, int32_t _y, int32_t _width, int32_t _height, bool _enabled, int32_t _r)
  804. {
  805. m_areaId.next();
  806. const uint32_t scrollId = getId();
  807. const bool hasTitle = (NULL != _name && '\0' != _name[0]);
  808. const int32_t header = hasTitle ? AREA_HEADER : 0;
  809. Area& area = getCurrentArea();
  810. area.m_x = _x;
  811. area.m_y = _y;
  812. area.m_width = _width;
  813. area.m_height = _height;
  814. area.m_contentX = area.m_x + SCROLL_AREA_PADDING;
  815. area.m_contentY = area.m_y + SCROLL_AREA_PADDING + header;
  816. area.m_contentWidth = area.m_width - SCROLL_AREA_PADDING;
  817. area.m_contentHeight = area.m_height - SCROLL_AREA_PADDING*2 - header;
  818. area.m_scissorX = area.m_contentX;
  819. area.m_scissorY = area.m_y + SCROLL_AREA_PADDING + header;
  820. area.m_scissorHeight = area.m_height - SCROLL_AREA_PADDING*2 - header;
  821. area.m_scissorWidth = area.m_contentWidth;
  822. area.m_scissorEnabled = false;
  823. area.m_widgetX = area.m_contentX;
  824. area.m_widgetY = area.m_contentY;
  825. area.m_widgetW = area.m_width - SCROLL_AREA_PADDING*2;
  826. static int32_t s_zeroScroll = 0;
  827. area.m_scrollVal = &s_zeroScroll;
  828. area.m_scrollId = scrollId;
  829. area.m_inside = inRect(area.m_scissorX, area.m_scissorY, area.m_scissorWidth, area.m_scissorHeight, false);
  830. if (_enabled)
  831. {
  832. setEnabled(m_areaId);
  833. }
  834. if (0 == _r)
  835. {
  836. drawRect( (float)_x
  837. , (float)_y
  838. , (float)_width + 0.3f /*border fix for seamlessly joining two scroll areas*/
  839. , (float)_height + 0.3f /*border fix for seamlessly joining two scroll areas*/
  840. , imguiRGBA(0, 0, 0, 192)
  841. );
  842. }
  843. else
  844. {
  845. drawRoundedRect( (float)_x
  846. , (float)_y
  847. , (float)_width
  848. , (float)_height
  849. , (float)_r
  850. , imguiRGBA(0, 0, 0, 192)
  851. );
  852. }
  853. if (hasTitle)
  854. {
  855. drawText(_x + 10
  856. , _y + 18
  857. , ImguiTextAlign::Left
  858. , _name
  859. , imguiRGBA(255, 255, 255, 128)
  860. );
  861. }
  862. nvgScissor(m_nvg
  863. , float(area.m_x)
  864. , float(area.m_y-1)
  865. , float(area.m_width)
  866. , float(area.m_height+1)
  867. );
  868. area.m_scissorEnabled = true;
  869. m_insideArea |= area.m_inside;
  870. return area.m_inside;
  871. }
  872. void endArea()
  873. {
  874. nvgResetScissor(m_nvg);
  875. }
  876. bool button(const char* _text, bool _enabled, ImguiAlign::Enum _align, uint32_t _rgb0, int32_t _r)
  877. {
  878. const uint32_t id = getId();
  879. Area& area = getCurrentArea();
  880. const int32_t yy = area.m_widgetY;
  881. const int32_t height = BUTTON_HEIGHT;
  882. area.m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  883. int32_t xx;
  884. int32_t width;
  885. if (ImguiAlign::Left == _align)
  886. {
  887. xx = area.m_contentX + SCROLL_AREA_PADDING;
  888. width = area.m_widgetW;
  889. }
  890. else if (ImguiAlign::LeftIndented == _align
  891. || ImguiAlign::Right == _align)
  892. {
  893. xx = area.m_widgetX;
  894. width = area.m_widgetW-1; //TODO: -1 !
  895. }
  896. else //if (ImguiAlign::Center == _align
  897. //|| ImguiAlign::CenterIndented == _align).
  898. {
  899. xx = area.m_widgetX;
  900. width = area.m_widgetW - (area.m_widgetX-area.m_scissorX);
  901. }
  902. const bool enabled = _enabled && isEnabled(m_areaId);
  903. const bool over = enabled && inRect(xx, yy, width, height);
  904. const bool res = buttonLogic(id, over);
  905. const uint32_t rgb0 = _rgb0&0x00ffffff;
  906. if (0 == _r)
  907. {
  908. drawRect( (float)xx
  909. , (float)yy
  910. , (float)width
  911. , (float)height
  912. , rgb0 | imguiRGBA(0, 0, 0, isActive(id) ? 196 : 96)
  913. );
  914. }
  915. else
  916. {
  917. drawRoundedRect( (float)xx
  918. , (float)yy
  919. , (float)width
  920. , (float)height
  921. , (float)_r
  922. , rgb0 | imguiRGBA(0, 0, 0, isActive(id) ? 196 : 96)
  923. );
  924. }
  925. if (enabled)
  926. {
  927. drawText(xx + BUTTON_HEIGHT / 2
  928. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  929. , ImguiTextAlign::Left
  930. , _text
  931. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  932. );
  933. }
  934. else
  935. {
  936. drawText(xx + BUTTON_HEIGHT / 2
  937. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  938. , ImguiTextAlign::Left
  939. , _text
  940. , imguiRGBA(128, 128, 128, 200)
  941. );
  942. }
  943. return res;
  944. }
  945. bool item(const char* _text, bool _enabled)
  946. {
  947. const uint32_t id = getId();
  948. Area& area = getCurrentArea();
  949. const int32_t xx = area.m_widgetX;
  950. const int32_t yy = area.m_widgetY;
  951. const int32_t width = area.m_widgetW;
  952. const int32_t height = BUTTON_HEIGHT;
  953. area.m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  954. const bool enabled = _enabled && isEnabled(m_areaId);
  955. const bool over = enabled && inRect(xx, yy, width, height);
  956. const bool res = buttonLogic(id, over);
  957. if (isHot(id) )
  958. {
  959. drawRoundedRect( (float)xx
  960. , (float)yy
  961. , (float)width
  962. , (float)height
  963. , 2.0f
  964. , imguiRGBA(255, 196, 0, isActive(id) ? 196 : 96)
  965. );
  966. }
  967. if (enabled)
  968. {
  969. drawText(xx + BUTTON_HEIGHT / 2
  970. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  971. , ImguiTextAlign::Left
  972. , _text
  973. , imguiRGBA(255, 255, 255, 200)
  974. );
  975. }
  976. else
  977. {
  978. drawText(xx + BUTTON_HEIGHT / 2
  979. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  980. , ImguiTextAlign::Left
  981. , _text
  982. , imguiRGBA(128, 128, 128, 200)
  983. );
  984. }
  985. return res;
  986. }
  987. bool check(const char* _text, bool _checked, bool _enabled)
  988. {
  989. const uint32_t id = getId();
  990. Area& area = getCurrentArea();
  991. const int32_t xx = area.m_widgetX;
  992. const int32_t yy = area.m_widgetY;
  993. const int32_t width = area.m_widgetW;
  994. const int32_t height = BUTTON_HEIGHT;
  995. area.m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  996. const bool enabled = _enabled && isEnabled(m_areaId);
  997. const bool over = enabled && inRect(xx, yy, width, height);
  998. const bool res = buttonLogic(id, over);
  999. const int32_t cx = xx + BUTTON_HEIGHT / 2 - CHECK_SIZE / 2;
  1000. const int32_t cy = yy + BUTTON_HEIGHT / 2 - CHECK_SIZE / 2;
  1001. drawRoundedRect( (float)cx - 3
  1002. , (float)cy - 3
  1003. , (float)CHECK_SIZE + 6
  1004. , (float)CHECK_SIZE + 6
  1005. , 4
  1006. , imguiRGBA(128, 128, 128, isActive(id) ? 196 : 96)
  1007. );
  1008. if (_checked)
  1009. {
  1010. if (enabled)
  1011. {
  1012. drawRoundedRect( (float)cx
  1013. , (float)cy
  1014. , (float)CHECK_SIZE
  1015. , (float)CHECK_SIZE
  1016. , (float)CHECK_SIZE / 2 - 1
  1017. , imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200)
  1018. );
  1019. }
  1020. else
  1021. {
  1022. drawRoundedRect( (float)cx
  1023. , (float)cy
  1024. , (float)CHECK_SIZE
  1025. , (float)CHECK_SIZE
  1026. , (float)CHECK_SIZE / 2 - 1
  1027. , imguiRGBA(128, 128, 128, 200)
  1028. );
  1029. }
  1030. }
  1031. if (enabled)
  1032. {
  1033. drawText(xx + BUTTON_HEIGHT
  1034. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1035. , ImguiTextAlign::Left
  1036. , _text
  1037. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  1038. );
  1039. }
  1040. else
  1041. {
  1042. drawText(xx + BUTTON_HEIGHT
  1043. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1044. , ImguiTextAlign::Left
  1045. , _text
  1046. , imguiRGBA(128, 128, 128, 200)
  1047. );
  1048. }
  1049. return res;
  1050. }
  1051. void input(const char* _label, char* _str, uint32_t _len, bool _enabled, ImguiAlign::Enum _align, int32_t _r)
  1052. {
  1053. const uint32_t id = getId();
  1054. Area& area = getCurrentArea();
  1055. const int32_t yy = area.m_widgetY;
  1056. area.m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  1057. int32_t xx;
  1058. int32_t width;
  1059. if (ImguiAlign::Left == _align)
  1060. {
  1061. xx = area.m_contentX + SCROLL_AREA_PADDING;
  1062. width = area.m_widgetW;
  1063. }
  1064. else if (ImguiAlign::LeftIndented == _align
  1065. || ImguiAlign::Right == _align)
  1066. {
  1067. xx = area.m_widgetX;
  1068. width = area.m_widgetW-1; //TODO: -1 !
  1069. }
  1070. else //if (ImguiAlign::Center == _align
  1071. //|| ImguiAlign::CenterIndented == _align).
  1072. {
  1073. xx = area.m_widgetX;
  1074. width = area.m_widgetW - (area.m_widgetX-area.m_scissorX);
  1075. }
  1076. const bool drawLabel = (NULL != _label && _label[0] != '\0');
  1077. if (drawLabel)
  1078. {
  1079. drawText(xx
  1080. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1081. , ImguiTextAlign::Left
  1082. , _label
  1083. , imguiRGBA(255, 255, 255, 200)
  1084. );
  1085. }
  1086. // Handle input.
  1087. if (isActiveInputField(id) )
  1088. {
  1089. const size_t cursor = size_t(strlen(_str));
  1090. if (m_char == 0x08) //backspace
  1091. {
  1092. _str[cursor-1] = '\0';
  1093. }
  1094. else if (m_char == 0x0d || m_char == 0x1b) //enter or escape
  1095. {
  1096. clearActiveInputField();
  1097. }
  1098. else if (cursor < _len-1
  1099. && 0 != m_char)
  1100. {
  1101. _str[cursor] = m_char;
  1102. _str[cursor+1] = '\0';
  1103. }
  1104. }
  1105. // Draw input area.
  1106. const int32_t height = BUTTON_HEIGHT;
  1107. if (drawLabel)
  1108. {
  1109. uint32_t numVertices = 0; //unused
  1110. const int32_t labelWidth = int32_t(getTextLength(m_fonts[m_currentFontIdx].m_cdata, _label, numVertices));
  1111. xx += (labelWidth + 6);
  1112. width -= (labelWidth + 6);
  1113. }
  1114. const bool enabled = _enabled && isEnabled(m_areaId);
  1115. const bool over = enabled && inRect(xx, yy, width, height);
  1116. inputLogic(id, over);
  1117. if (0 == _r)
  1118. {
  1119. drawRect( (float)xx
  1120. , (float)yy
  1121. , (float)width
  1122. , (float)height
  1123. , isActiveInputField(id)?imguiRGBA(255,196,0,255):imguiRGBA(128,128,128,96)
  1124. );
  1125. }
  1126. else
  1127. {
  1128. drawRoundedRect( (float)xx
  1129. , (float)yy
  1130. , (float)width
  1131. , (float)height
  1132. , (float)_r
  1133. , isActiveInputField(id)?imguiRGBA(255,196,0,255):imguiRGBA(128,128,128,96)
  1134. );
  1135. }
  1136. if (isActiveInputField(id) )
  1137. {
  1138. drawText(xx + 6
  1139. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1140. , ImguiTextAlign::Left
  1141. , _str
  1142. , imguiRGBA(0, 0, 0, 255)
  1143. );
  1144. }
  1145. else
  1146. {
  1147. drawText(xx + 6
  1148. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1149. , ImguiTextAlign::Left
  1150. , _str
  1151. , isHot(id) ? imguiRGBA(255,196,0,255) : imguiRGBA(255,255,255,255)
  1152. );
  1153. }
  1154. }
  1155. uint8_t tabs(uint8_t _selected, bool _enabled, ImguiAlign::Enum _align, int32_t _height, int32_t _r, va_list _argList)
  1156. {
  1157. BX_UNUSED(_align);
  1158. uint8_t count;
  1159. const char* titles[16];
  1160. const char* str = va_arg(_argList, const char*);
  1161. for (count = 0; str != NULL || count >= 16; ++count, str = va_arg(_argList, const char*) )
  1162. {
  1163. titles[count] = str;
  1164. }
  1165. Area& area = getCurrentArea();
  1166. const int32_t yy = area.m_widgetY;
  1167. area.m_widgetY += _height + DEFAULT_SPACING;
  1168. int32_t xx;
  1169. int32_t width;
  1170. if (ImguiAlign::Left == _align)
  1171. {
  1172. xx = area.m_contentX + SCROLL_AREA_PADDING;
  1173. width = area.m_widgetW;
  1174. }
  1175. else if (ImguiAlign::LeftIndented == _align
  1176. || ImguiAlign::Right == _align)
  1177. {
  1178. xx = area.m_widgetX;
  1179. width = area.m_widgetW-1; //TODO: -1 !
  1180. }
  1181. else //if (ImguiAlign::Center == _align
  1182. //|| ImguiAlign::CenterIndented == _align).
  1183. {
  1184. xx = area.m_widgetX;
  1185. width = area.m_widgetW - (area.m_widgetX-area.m_scissorX);
  1186. }
  1187. uint8_t selected = _selected;
  1188. const int32_t tabWidth = width / count;
  1189. const int32_t tabWidthHalf = width / (count*2);
  1190. const int32_t textY = yy + _height/2 + int32_t(m_fonts[m_currentFontIdx].m_size)/2 - 2;
  1191. if (0 == _r)
  1192. {
  1193. drawRect( (float)xx
  1194. , (float)yy
  1195. , (float)width
  1196. , (float)_height
  1197. , imguiRGBA(128, 128, 128, 96)
  1198. );
  1199. }
  1200. else
  1201. {
  1202. drawRoundedRect( (float)xx
  1203. , (float)yy
  1204. , (float)width
  1205. , (float)_height
  1206. , (float)_r
  1207. , imguiRGBA(128, 128, 128, 96)
  1208. );
  1209. }
  1210. for (uint8_t ii = 0; ii < count; ++ii)
  1211. {
  1212. const uint32_t id = getId();
  1213. int32_t buttonX = xx + ii*tabWidth;
  1214. int32_t textX = buttonX + tabWidthHalf;
  1215. const bool enabled = _enabled && isEnabled(m_areaId);
  1216. const bool over = enabled && inRect(buttonX, yy, tabWidth, _height);
  1217. const bool res = buttonLogic(id, over);
  1218. if (res)
  1219. {
  1220. selected = ii;
  1221. }
  1222. uint32_t textColor;
  1223. if (ii == selected)
  1224. {
  1225. textColor = enabled?imguiRGBA(0,0,0,255):imguiRGBA(255,255,255,100);
  1226. if (0 == _r)
  1227. {
  1228. drawRect( (float)buttonX
  1229. , (float)yy
  1230. , (float)tabWidth
  1231. , (float)_height
  1232. , enabled?imguiRGBA(255,196,0,200):imguiRGBA(128,128,128,96)
  1233. );
  1234. }
  1235. else
  1236. {
  1237. drawRoundedRect( (float)buttonX
  1238. , (float)yy
  1239. , (float)tabWidth
  1240. , (float)_height
  1241. , (float)_r
  1242. , enabled?imguiRGBA(255,196,0,200):imguiRGBA(128,128,128,96)
  1243. );
  1244. }
  1245. }
  1246. else
  1247. {
  1248. textColor = isHot(id) ? imguiRGBA(255, 196, 0, enabled?255:100) : imguiRGBA(255, 255, 255, enabled?200:100);
  1249. }
  1250. drawText(textX
  1251. , textY
  1252. , ImguiTextAlign::Center
  1253. , titles[ii]
  1254. , textColor
  1255. );
  1256. }
  1257. return selected;
  1258. }
  1259. void image(bgfx::TextureHandle _image, float _lod, int32_t _width, int32_t _height, ImguiAlign::Enum _align)
  1260. {
  1261. Area& area = getCurrentArea();
  1262. int32_t xx;
  1263. if (ImguiAlign::Left == _align)
  1264. {
  1265. xx = area.m_contentX + SCROLL_AREA_PADDING;
  1266. }
  1267. else if (ImguiAlign::LeftIndented == _align)
  1268. {
  1269. xx = area.m_widgetX;
  1270. }
  1271. else if (ImguiAlign::Center == _align)
  1272. {
  1273. xx = area.m_contentX + (area.m_widgetW-_width)/2;
  1274. }
  1275. else if (ImguiAlign::CenterIndented == _align)
  1276. {
  1277. xx = (area.m_widgetX + area.m_widgetW + area.m_contentX - _width)/2;
  1278. }
  1279. else //if (ImguiAlign::Right == _align).
  1280. {
  1281. xx = area.m_contentX + area.m_widgetW - _width;
  1282. }
  1283. const int32_t yy = area.m_widgetY;
  1284. area.m_widgetY += _height + DEFAULT_SPACING;
  1285. screenQuad(xx, yy, _width, _height);
  1286. bgfx::setUniform(u_imageLod, &_lod);
  1287. bgfx::setTexture(0, s_texColor, bgfx::isValid(_image) ? _image : m_missingTexture);
  1288. bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
  1289. bgfx::setProgram(m_imageProgram);
  1290. setCurrentScissor();
  1291. bgfx::submit(m_view);
  1292. }
  1293. void image(bgfx::TextureHandle _image, float _lod, float _width, float _aspect, ImguiAlign::Enum _align)
  1294. {
  1295. const float width = _width*float(getCurrentArea().m_widgetW);
  1296. const float height = width/_aspect;
  1297. image(_image, _lod, int32_t(width), int32_t(height), _align);
  1298. }
  1299. void imageChannel(bgfx::TextureHandle _image, uint8_t _channel, float _lod, int32_t _width, int32_t _height, ImguiAlign::Enum _align)
  1300. {
  1301. BX_CHECK(_channel < 4, "Channel param must be from 0 to 3!");
  1302. Area& area = getCurrentArea();
  1303. int32_t xx;
  1304. if (ImguiAlign::Left == _align)
  1305. {
  1306. xx = area.m_contentX + SCROLL_AREA_PADDING;
  1307. }
  1308. else if (ImguiAlign::LeftIndented == _align)
  1309. {
  1310. xx = area.m_widgetX;
  1311. }
  1312. else if (ImguiAlign::Center == _align)
  1313. {
  1314. xx = area.m_contentX + (area.m_widgetW-_width)/2;
  1315. }
  1316. else if (ImguiAlign::CenterIndented == _align)
  1317. {
  1318. xx = (area.m_widgetX + area.m_widgetW + area.m_contentX - _width)/2;
  1319. }
  1320. else //if (ImguiAlign::Right == _align).
  1321. {
  1322. xx = area.m_contentX + area.m_widgetW - _width;
  1323. }
  1324. const int32_t yy = area.m_widgetY;
  1325. area.m_widgetY += _height + DEFAULT_SPACING;
  1326. screenQuad(xx, yy, _width, _height);
  1327. bgfx::setUniform(u_imageLod, &_lod);
  1328. float swizz[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  1329. swizz[_channel] = 1.0f;
  1330. bgfx::setUniform(u_imageSwizzle, swizz);
  1331. bgfx::setTexture(0, s_texColor, bgfx::isValid(_image) ? _image : m_missingTexture);
  1332. bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
  1333. bgfx::setProgram(m_imageSwizzProgram);
  1334. setCurrentScissor();
  1335. bgfx::submit(m_view);
  1336. }
  1337. void imageChannel(bgfx::TextureHandle _image, uint8_t _channel, float _lod, float _width, float _aspect, ImguiAlign::Enum _align)
  1338. {
  1339. const float width = _width*float(getCurrentArea().m_widgetW);
  1340. const float height = width/_aspect;
  1341. imageChannel(_image, _channel, _lod, int32_t(width), int32_t(height), _align);
  1342. }
  1343. bool collapse(const char* _text, const char* _subtext, bool _checked, bool _enabled)
  1344. {
  1345. const uint32_t id = getId();
  1346. Area& area = getCurrentArea();
  1347. const int32_t xx = area.m_widgetX;
  1348. const int32_t yy = area.m_widgetY;
  1349. const int32_t width = area.m_widgetW;
  1350. const int32_t height = BUTTON_HEIGHT;
  1351. area.m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  1352. const int32_t cx = xx + BUTTON_HEIGHT/2 - CHECK_SIZE/2;
  1353. const int32_t cy = yy + BUTTON_HEIGHT/2 - CHECK_SIZE/2 + DEFAULT_SPACING/2;
  1354. const int32_t textY = yy + BUTTON_HEIGHT/2 + TEXT_HEIGHT/2 + DEFAULT_SPACING/2;
  1355. const bool enabled = _enabled && isEnabled(m_areaId);
  1356. const bool over = enabled && inRect(xx, yy, width, height);
  1357. const bool res = buttonLogic(id, over);
  1358. if (_checked)
  1359. {
  1360. drawTriangle(cx
  1361. , cy
  1362. , CHECK_SIZE
  1363. , CHECK_SIZE
  1364. , TriangleOrientation::Up
  1365. , imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200)
  1366. );
  1367. }
  1368. else
  1369. {
  1370. drawTriangle(cx
  1371. , cy
  1372. , CHECK_SIZE
  1373. , CHECK_SIZE
  1374. , TriangleOrientation::Right
  1375. , imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200)
  1376. );
  1377. }
  1378. if (enabled)
  1379. {
  1380. drawText(xx + BUTTON_HEIGHT
  1381. , textY
  1382. , ImguiTextAlign::Left
  1383. , _text
  1384. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  1385. );
  1386. }
  1387. else
  1388. {
  1389. drawText(xx + BUTTON_HEIGHT
  1390. , textY
  1391. , ImguiTextAlign::Left
  1392. , _text
  1393. , imguiRGBA(128, 128, 128, 200)
  1394. );
  1395. }
  1396. if (_subtext)
  1397. {
  1398. drawText(xx + width - BUTTON_HEIGHT / 2
  1399. , textY
  1400. , ImguiTextAlign::Right
  1401. , _subtext
  1402. , imguiRGBA(255, 255, 255, 128)
  1403. );
  1404. }
  1405. return res;
  1406. }
  1407. bool borderButton(ImguiBorder::Enum _border, bool _checked, bool _enabled)
  1408. {
  1409. // Since border button isn't part of any area, just use this custom/unique areaId.
  1410. const uint16_t areaId = UINT16_MAX-1;
  1411. const uint32_t id = (areaId << 16) | m_widgetId++;
  1412. updatePresence(id);
  1413. const int32_t triSize = 12;
  1414. const int32_t borderSize = 15;
  1415. int32_t xx;
  1416. int32_t yy;
  1417. int32_t width;
  1418. int32_t height;
  1419. int32_t triX;
  1420. int32_t triY;
  1421. TriangleOrientation::Enum orientation;
  1422. if (ImguiBorder::Left == _border)
  1423. {
  1424. xx = -borderSize;
  1425. yy = 0;
  1426. width = 2*borderSize;
  1427. height = m_viewHeight;
  1428. triX = 0;
  1429. triY = (m_viewHeight-triSize)/2;
  1430. orientation = _checked ? TriangleOrientation::Left : TriangleOrientation::Right;
  1431. }
  1432. else if (ImguiBorder::Right == _border)
  1433. {
  1434. xx = m_viewWidth - borderSize;
  1435. yy = 0;
  1436. width = 2*borderSize;
  1437. height = m_viewHeight;
  1438. triX = m_viewWidth - triSize - 2;
  1439. triY = (m_viewHeight-width)/2;
  1440. orientation = _checked ? TriangleOrientation::Right : TriangleOrientation::Left;
  1441. }
  1442. else if (ImguiBorder::Top == _border)
  1443. {
  1444. xx = 0;
  1445. yy = -borderSize;
  1446. width = m_viewWidth;
  1447. height = 2*borderSize;
  1448. triX = (m_viewWidth-triSize)/2;
  1449. triY = 0;
  1450. orientation = _checked ? TriangleOrientation::Up : TriangleOrientation::Down;
  1451. }
  1452. else //if (ImguiBorder::Bottom == _border).
  1453. {
  1454. xx = 0;
  1455. yy = m_viewHeight - borderSize;
  1456. width = m_viewWidth;
  1457. height = 2*borderSize;
  1458. triX = (m_viewWidth-triSize)/2;
  1459. triY = m_viewHeight-triSize;
  1460. orientation = _checked ? TriangleOrientation::Down : TriangleOrientation::Up;
  1461. }
  1462. const bool over = _enabled && inRect(xx, yy, width, height, false);
  1463. const bool res = buttonLogic(id, over);
  1464. drawRoundedRect( (float)xx
  1465. , (float)yy
  1466. , (float)width
  1467. , (float)height
  1468. , 0.0f
  1469. , isActive(id) ? imguiRGBA(23, 23, 23, 192) : imguiRGBA(0, 0, 0, 222)
  1470. );
  1471. drawTriangle( triX
  1472. , triY
  1473. , triSize
  1474. , triSize
  1475. , orientation
  1476. , isHot(id) ? imguiRGBA(255, 196, 0, 222) : imguiRGBA(255, 255, 255, 192)
  1477. );
  1478. return res;
  1479. }
  1480. void labelVargs(const char* _format, va_list _argList, uint32_t _rgba)
  1481. {
  1482. char temp[8192];
  1483. char* out = temp;
  1484. int32_t len = bx::vsnprintf(out, sizeof(temp), _format, _argList);
  1485. if ( (int32_t)sizeof(temp) < len)
  1486. {
  1487. out = (char*)alloca(len+1);
  1488. len = bx::vsnprintf(out, len, _format, _argList);
  1489. }
  1490. out[len] = '\0';
  1491. Area& area = getCurrentArea();
  1492. const int32_t xx = area.m_widgetX;
  1493. const int32_t yy = area.m_widgetY;
  1494. area.m_widgetY += BUTTON_HEIGHT;
  1495. drawText(xx
  1496. , yy + BUTTON_HEIGHT/2 + TEXT_HEIGHT/2
  1497. , ImguiTextAlign::Left
  1498. , out
  1499. , _rgba
  1500. );
  1501. }
  1502. void value(const char* _text)
  1503. {
  1504. Area& area = getCurrentArea();
  1505. const int32_t xx = area.m_widgetX;
  1506. const int32_t yy = area.m_widgetY;
  1507. const int32_t ww = area.m_widgetW;
  1508. area.m_widgetY += BUTTON_HEIGHT;
  1509. drawText(xx + ww - BUTTON_HEIGHT / 2
  1510. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1511. , ImguiTextAlign::Right
  1512. , _text
  1513. , imguiRGBA(255, 255, 255, 200)
  1514. );
  1515. }
  1516. bool slider(const char* _text, float& _val, float _vmin, float _vmax, float _vinc, bool _enabled, ImguiAlign::Enum _align)
  1517. {
  1518. const uint32_t id = getId();
  1519. Area& area = getCurrentArea();
  1520. const int32_t yy = area.m_widgetY;
  1521. const int32_t height = SLIDER_HEIGHT;
  1522. area.m_widgetY += SLIDER_HEIGHT + DEFAULT_SPACING;
  1523. int32_t xx;
  1524. int32_t width;
  1525. if (ImguiAlign::Left == _align)
  1526. {
  1527. xx = area.m_contentX + SCROLL_AREA_PADDING;
  1528. width = area.m_widgetW;
  1529. }
  1530. else if (ImguiAlign::LeftIndented == _align
  1531. || ImguiAlign::Right == _align)
  1532. {
  1533. xx = area.m_widgetX;
  1534. width = area.m_widgetW;
  1535. }
  1536. else //if (ImguiAlign::Center == _align
  1537. //|| ImguiAlign::CenterIndented == _align).
  1538. {
  1539. xx = area.m_widgetX;
  1540. width = area.m_widgetW - (area.m_widgetX-area.m_scissorX);
  1541. }
  1542. drawRoundedRect( (float)xx, (float)yy, (float)width, (float)height, 4.0f, imguiRGBA(0, 0, 0, 128) );
  1543. const int32_t range = width - SLIDER_MARKER_WIDTH;
  1544. float uu = bx::fsaturate( (_val - _vmin) / (_vmax - _vmin) );
  1545. int32_t m = (int)(uu * range);
  1546. bool valChanged = false;
  1547. const bool enabled = _enabled && isEnabled(m_areaId);
  1548. const bool over = enabled && inRect(xx + m, yy, SLIDER_MARKER_WIDTH, SLIDER_HEIGHT);
  1549. const bool res = buttonLogic(id, over);
  1550. if (isActive(id) )
  1551. {
  1552. if (m_wentActive)
  1553. {
  1554. m_dragX = m_mx;
  1555. m_dragOrig = uu;
  1556. }
  1557. if (m_dragX != m_mx)
  1558. {
  1559. uu = bx::fsaturate(m_dragOrig + (float)(m_mx - m_dragX) / (float)range);
  1560. _val = _vmin + uu * (_vmax - _vmin);
  1561. _val = floorf(_val / _vinc + 0.5f) * _vinc; // Snap to vinc
  1562. m = (int)(uu * range);
  1563. valChanged = true;
  1564. }
  1565. }
  1566. if (isActive(id) )
  1567. {
  1568. drawRoundedRect( (float)(xx + m)
  1569. , (float)yy
  1570. , (float)SLIDER_MARKER_WIDTH
  1571. , (float)SLIDER_HEIGHT
  1572. , 4.0f
  1573. , imguiRGBA(255, 255, 255, 255)
  1574. );
  1575. }
  1576. else
  1577. {
  1578. drawRoundedRect( (float)(xx + m)
  1579. , (float)yy
  1580. , (float)SLIDER_MARKER_WIDTH
  1581. , (float)SLIDER_HEIGHT
  1582. , 4.0f
  1583. , isHot(id) ? imguiRGBA(255, 196, 0, 128) : imguiRGBA(255, 255, 255, 64)
  1584. );
  1585. }
  1586. // TODO: fix this, take a look at 'nicenum'.
  1587. int32_t digits = (int)(ceilf(log10f(_vinc) ) );
  1588. char fmt[16];
  1589. bx::snprintf(fmt, 16, "%%.%df", digits >= 0 ? 0 : -digits);
  1590. char msg[128];
  1591. bx::snprintf(msg, 128, fmt, _val);
  1592. if (enabled)
  1593. {
  1594. drawText(xx + SLIDER_HEIGHT / 2
  1595. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  1596. , ImguiTextAlign::Left
  1597. , _text
  1598. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  1599. );
  1600. drawText(xx + width - SLIDER_HEIGHT / 2
  1601. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  1602. , ImguiTextAlign::Right
  1603. , msg
  1604. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  1605. );
  1606. }
  1607. else
  1608. {
  1609. drawText(xx + SLIDER_HEIGHT / 2
  1610. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  1611. , ImguiTextAlign::Left
  1612. , _text
  1613. , imguiRGBA(128, 128, 128, 200)
  1614. );
  1615. drawText(xx + width - SLIDER_HEIGHT / 2
  1616. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  1617. , ImguiTextAlign::Right
  1618. , msg
  1619. , imguiRGBA(128, 128, 128, 200)
  1620. );
  1621. }
  1622. return res || valChanged;
  1623. }
  1624. void indent(uint16_t _width)
  1625. {
  1626. Area& area = getCurrentArea();
  1627. area.m_widgetX += _width;
  1628. area.m_widgetW -= _width;
  1629. }
  1630. void unindent(uint16_t _width)
  1631. {
  1632. Area& area = getCurrentArea();
  1633. area.m_widgetX -= _width;
  1634. area.m_widgetW += _width;
  1635. }
  1636. void separator(uint16_t _height)
  1637. {
  1638. Area& area = getCurrentArea();
  1639. area.m_widgetY += _height;
  1640. }
  1641. void separatorLine(uint16_t _height)
  1642. {
  1643. Area& area = getCurrentArea();
  1644. const int32_t rectWidth = area.m_widgetW;
  1645. const int32_t rectHeight = 1;
  1646. const int32_t xx = area.m_widgetX;
  1647. const int32_t yy = area.m_widgetY + _height/2 - rectHeight;
  1648. area.m_widgetY += _height;
  1649. drawRect( (float)xx
  1650. , (float)yy
  1651. , (float)rectWidth
  1652. , (float)rectHeight
  1653. , imguiRGBA(255, 255, 255, 32)
  1654. );
  1655. }
  1656. void drawPolygon(const float* _coords, uint32_t _numCoords, float _r, uint32_t _abgr)
  1657. {
  1658. _numCoords = bx::uint32_min(_numCoords, MAX_TEMP_COORDS);
  1659. for (uint32_t ii = 0, jj = _numCoords - 1; ii < _numCoords; jj = ii++)
  1660. {
  1661. const float* v0 = &_coords[jj * 2];
  1662. const float* v1 = &_coords[ii * 2];
  1663. float dx = v1[0] - v0[0];
  1664. float dy = v1[1] - v0[1];
  1665. float d = sqrtf(dx * dx + dy * dy);
  1666. if (d > 0)
  1667. {
  1668. d = 1.0f / d;
  1669. dx *= d;
  1670. dy *= d;
  1671. }
  1672. m_tempNormals[jj * 2 + 0] = dy;
  1673. m_tempNormals[jj * 2 + 1] = -dx;
  1674. }
  1675. for (uint32_t ii = 0, jj = _numCoords - 1; ii < _numCoords; jj = ii++)
  1676. {
  1677. float dlx0 = m_tempNormals[jj * 2 + 0];
  1678. float dly0 = m_tempNormals[jj * 2 + 1];
  1679. float dlx1 = m_tempNormals[ii * 2 + 0];
  1680. float dly1 = m_tempNormals[ii * 2 + 1];
  1681. float dmx = (dlx0 + dlx1) * 0.5f;
  1682. float dmy = (dly0 + dly1) * 0.5f;
  1683. float dmr2 = dmx * dmx + dmy * dmy;
  1684. if (dmr2 > 0.000001f)
  1685. {
  1686. float scale = 1.0f / dmr2;
  1687. if (scale > 10.0f)
  1688. {
  1689. scale = 10.0f;
  1690. }
  1691. dmx *= scale;
  1692. dmy *= scale;
  1693. }
  1694. m_tempCoords[ii * 2 + 0] = _coords[ii * 2 + 0] + dmx * _r;
  1695. m_tempCoords[ii * 2 + 1] = _coords[ii * 2 + 1] + dmy * _r;
  1696. }
  1697. uint32_t numVertices = _numCoords*6 + (_numCoords-2)*3;
  1698. if (bgfx::checkAvailTransientVertexBuffer(numVertices, PosColorVertex::ms_decl) )
  1699. {
  1700. bgfx::TransientVertexBuffer tvb;
  1701. bgfx::allocTransientVertexBuffer(&tvb, numVertices, PosColorVertex::ms_decl);
  1702. uint32_t trans = _abgr&0xffffff;
  1703. PosColorVertex* vertex = (PosColorVertex*)tvb.data;
  1704. for (uint32_t ii = 0, jj = _numCoords-1; ii < _numCoords; jj = ii++)
  1705. {
  1706. vertex->m_x = _coords[ii*2+0];
  1707. vertex->m_y = _coords[ii*2+1];
  1708. vertex->m_abgr = _abgr;
  1709. ++vertex;
  1710. vertex->m_x = _coords[jj*2+0];
  1711. vertex->m_y = _coords[jj*2+1];
  1712. vertex->m_abgr = _abgr;
  1713. ++vertex;
  1714. vertex->m_x = m_tempCoords[jj*2+0];
  1715. vertex->m_y = m_tempCoords[jj*2+1];
  1716. vertex->m_abgr = trans;
  1717. ++vertex;
  1718. vertex->m_x = m_tempCoords[jj*2+0];
  1719. vertex->m_y = m_tempCoords[jj*2+1];
  1720. vertex->m_abgr = trans;
  1721. ++vertex;
  1722. vertex->m_x = m_tempCoords[ii*2+0];
  1723. vertex->m_y = m_tempCoords[ii*2+1];
  1724. vertex->m_abgr = trans;
  1725. ++vertex;
  1726. vertex->m_x = _coords[ii*2+0];
  1727. vertex->m_y = _coords[ii*2+1];
  1728. vertex->m_abgr = _abgr;
  1729. ++vertex;
  1730. }
  1731. for (uint32_t ii = 2; ii < _numCoords; ++ii)
  1732. {
  1733. vertex->m_x = _coords[0];
  1734. vertex->m_y = _coords[1];
  1735. vertex->m_abgr = _abgr;
  1736. ++vertex;
  1737. vertex->m_x = _coords[(ii-1)*2+0];
  1738. vertex->m_y = _coords[(ii-1)*2+1];
  1739. vertex->m_abgr = _abgr;
  1740. ++vertex;
  1741. vertex->m_x = _coords[ii*2+0];
  1742. vertex->m_y = _coords[ii*2+1];
  1743. vertex->m_abgr = _abgr;
  1744. ++vertex;
  1745. }
  1746. bgfx::setVertexBuffer(&tvb);
  1747. bgfx::setState(0
  1748. | BGFX_STATE_RGB_WRITE
  1749. | BGFX_STATE_ALPHA_WRITE
  1750. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  1751. );
  1752. bgfx::setProgram(m_colorProgram);
  1753. setCurrentScissor();
  1754. bgfx::submit(m_view);
  1755. }
  1756. }
  1757. void drawRect(float _x, float _y, float w, float h, uint32_t _argb, float _fth = 1.0f)
  1758. {
  1759. float verts[4 * 2] =
  1760. {
  1761. _x + 0.5f, _y + 0.5f,
  1762. _x + w - 0.5f, _y + 0.5f,
  1763. _x + w - 0.5f, _y + h - 0.5f,
  1764. _x + 0.5f, _y + h - 0.5f,
  1765. };
  1766. drawPolygon(verts, 4, _fth, _argb);
  1767. }
  1768. void drawRoundedRect(float _x, float _y, float w, float h, float r, uint32_t _argb, float _fth = 1.0f)
  1769. {
  1770. const uint32_t num = NUM_CIRCLE_VERTS / 4;
  1771. const float* cverts = m_circleVerts;
  1772. float verts[(num + 1) * 4 * 2];
  1773. float* vv = verts;
  1774. for (uint32_t ii = 0; ii <= num; ++ii)
  1775. {
  1776. *vv++ = _x + w - r + cverts[ii * 2] * r;
  1777. *vv++ = _y + h - r + cverts[ii * 2 + 1] * r;
  1778. }
  1779. for (uint32_t ii = num; ii <= num * 2; ++ii)
  1780. {
  1781. *vv++ = _x + r + cverts[ii * 2] * r;
  1782. *vv++ = _y + h - r + cverts[ii * 2 + 1] * r;
  1783. }
  1784. for (uint32_t ii = num * 2; ii <= num * 3; ++ii)
  1785. {
  1786. *vv++ = _x + r + cverts[ii * 2] * r;
  1787. *vv++ = _y + r + cverts[ii * 2 + 1] * r;
  1788. }
  1789. for (uint32_t ii = num * 3; ii < num * 4; ++ii)
  1790. {
  1791. *vv++ = _x + w - r + cverts[ii * 2] * r;
  1792. *vv++ = _y + r + cverts[ii * 2 + 1] * r;
  1793. }
  1794. *vv++ = _x + w - r + cverts[0] * r;
  1795. *vv++ = _y + r + cverts[1] * r;
  1796. drawPolygon(verts, (num + 1) * 4, _fth, _argb);
  1797. }
  1798. void drawLine(float _x0, float _y0, float _x1, float _y1, float _r, uint32_t _abgr, float _fth = 1.0f)
  1799. {
  1800. float dx = _x1 - _x0;
  1801. float dy = _y1 - _y0;
  1802. float d = sqrtf(dx * dx + dy * dy);
  1803. if (d > 0.0001f)
  1804. {
  1805. d = 1.0f / d;
  1806. dx *= d;
  1807. dy *= d;
  1808. }
  1809. float nx = dy;
  1810. float ny = -dx;
  1811. float verts[4 * 2];
  1812. _r -= _fth;
  1813. _r *= 0.5f;
  1814. if (_r < 0.01f)
  1815. {
  1816. _r = 0.01f;
  1817. }
  1818. dx *= _r;
  1819. dy *= _r;
  1820. nx *= _r;
  1821. ny *= _r;
  1822. verts[0] = _x0 - dx - nx;
  1823. verts[1] = _y0 - dy - ny;
  1824. verts[2] = _x0 - dx + nx;
  1825. verts[3] = _y0 - dy + ny;
  1826. verts[4] = _x1 + dx + nx;
  1827. verts[5] = _y1 + dy + ny;
  1828. verts[6] = _x1 + dx - nx;
  1829. verts[7] = _y1 + dy - ny;
  1830. drawPolygon(verts, 4, _fth, _abgr);
  1831. }
  1832. struct TriangleOrientation
  1833. {
  1834. enum Enum
  1835. {
  1836. Left,
  1837. Right,
  1838. Up,
  1839. Down,
  1840. };
  1841. };
  1842. void drawTriangle(int32_t _x, int32_t _y, int32_t _width, int32_t _height, TriangleOrientation::Enum _orientation, uint32_t _abgr)
  1843. {
  1844. if (TriangleOrientation::Left == _orientation)
  1845. {
  1846. const float verts[3 * 2] =
  1847. {
  1848. (float)_x + 0.5f + (float)_width * 1.0f, (float)_y + 0.5f,
  1849. (float)_x + 0.5f, (float)_y + 0.5f + (float)_height / 2.0f - 0.5f,
  1850. (float)_x + 0.5f + (float)_width * 1.0f, (float)_y + 0.5f + (float)_height - 1.0f,
  1851. };
  1852. drawPolygon(verts, 3, 1.0f, _abgr);
  1853. }
  1854. else if (TriangleOrientation::Right == _orientation)
  1855. {
  1856. const float verts[3 * 2] =
  1857. {
  1858. (float)_x + 0.5f, (float)_y + 0.5f,
  1859. (float)_x + 0.5f + (float)_width * 1.0f, (float)_y + 0.5f + (float)_height / 2.0f - 0.5f,
  1860. (float)_x + 0.5f, (float)_y + 0.5f + (float)_height - 1.0f,
  1861. };
  1862. drawPolygon(verts, 3, 1.0f, _abgr);
  1863. }
  1864. else if (TriangleOrientation::Up == _orientation)
  1865. {
  1866. const float verts[3 * 2] =
  1867. {
  1868. (float)_x + 0.5f, (float)_y + 0.5f + (float)_height - 1.0f,
  1869. (float)_x + 0.5f + (float)_width / 2.0f - 0.5f, (float)_y + 0.5f,
  1870. (float)_x + 0.5f + (float)_width - 1.0f, (float)_y + 0.5f + (float)_height - 1.0f,
  1871. };
  1872. drawPolygon(verts, 3, 1.0f, _abgr);
  1873. }
  1874. else //if (TriangleOrientation::Down == _orientation).
  1875. {
  1876. const float verts[3 * 2] =
  1877. {
  1878. (float)_x + 0.5f, (float)_y + 0.5f,
  1879. (float)_x + 0.5f + (float)_width / 2.0f - 0.5f, (float)_y + 0.5f + (float)_height - 1.0f,
  1880. (float)_x + 0.5f + (float)_width - 1.0f, (float)_y + 0.5f,
  1881. };
  1882. drawPolygon(verts, 3, 1.0f, _abgr);
  1883. }
  1884. }
  1885. #if !USE_NANOVG_FONT
  1886. void getBakedQuad(stbtt_bakedchar* _chardata, int32_t char_index, float* _xpos, float* _ypos, stbtt_aligned_quad* _quad)
  1887. {
  1888. stbtt_bakedchar* b = _chardata + char_index;
  1889. int32_t round_x = STBTT_ifloor(*_xpos + b->xoff);
  1890. int32_t round_y = STBTT_ifloor(*_ypos + b->yoff);
  1891. _quad->x0 = (float)round_x;
  1892. _quad->y0 = (float)round_y;
  1893. _quad->x1 = (float)round_x + b->x1 - b->x0;
  1894. _quad->y1 = (float)round_y + b->y1 - b->y0;
  1895. _quad->s0 = (b->x0 + m_halfTexel) * m_invTextureWidth;
  1896. _quad->t0 = (b->y0 + m_halfTexel) * m_invTextureWidth;
  1897. _quad->s1 = (b->x1 + m_halfTexel) * m_invTextureHeight;
  1898. _quad->t1 = (b->y1 + m_halfTexel) * m_invTextureHeight;
  1899. *_xpos += b->xadvance;
  1900. }
  1901. #endif // !USE_NANOVG_FONT
  1902. void drawText(int32_t _x, int32_t _y, ImguiTextAlign::Enum _align, const char* _text, uint32_t _abgr)
  1903. {
  1904. drawText( (float)_x, (float)_y, _text, _align, _abgr);
  1905. }
  1906. void drawText(float _x, float _y, const char* _text, ImguiTextAlign::Enum _align, uint32_t _abgr)
  1907. {
  1908. if (NULL == _text
  1909. || '\0' == _text[0])
  1910. {
  1911. return;
  1912. }
  1913. #if USE_NANOVG_FONT
  1914. static uint32_t textAlign[ImguiTextAlign::Count] =
  1915. {
  1916. NVG_ALIGN_LEFT,
  1917. NVG_ALIGN_CENTER,
  1918. NVG_ALIGN_RIGHT,
  1919. };
  1920. nvgTextAlign(m_nvg, textAlign[_align]);
  1921. nvgFontBlur(m_nvg, 0.0f);
  1922. nvgFillColor(m_nvg, nvgRGBAu(_abgr) );
  1923. nvgText(m_nvg, _x, _y, _text, NULL);
  1924. #else
  1925. uint32_t numVertices = 0;
  1926. if (_align == ImguiTextAlign::Center)
  1927. {
  1928. _x -= getTextLength(m_fonts[m_currentFontIdx].m_cdata, _text, numVertices) / 2;
  1929. }
  1930. else if (_align == ImguiTextAlign::Right)
  1931. {
  1932. _x -= getTextLength(m_fonts[m_currentFontIdx].m_cdata, _text, numVertices);
  1933. }
  1934. else // just count vertices
  1935. {
  1936. getTextLength(m_fonts[m_currentFontIdx].m_cdata, _text, numVertices);
  1937. }
  1938. if (bgfx::checkAvailTransientVertexBuffer(numVertices, PosColorUvVertex::ms_decl) )
  1939. {
  1940. bgfx::TransientVertexBuffer tvb;
  1941. bgfx::allocTransientVertexBuffer(&tvb, numVertices, PosColorUvVertex::ms_decl);
  1942. PosColorUvVertex* vertex = (PosColorUvVertex*)tvb.data;
  1943. const float ox = _x;
  1944. while (*_text)
  1945. {
  1946. int32_t ch = (uint8_t)*_text;
  1947. if (ch == '\t')
  1948. {
  1949. for (int32_t i = 0; i < 4; ++i)
  1950. {
  1951. if (_x < s_tabStops[i] + ox)
  1952. {
  1953. _x = s_tabStops[i] + ox;
  1954. break;
  1955. }
  1956. }
  1957. }
  1958. else if (ch >= ' '
  1959. && ch < 128)
  1960. {
  1961. stbtt_aligned_quad quad;
  1962. getBakedQuad(m_fonts[m_currentFontIdx].m_cdata, ch - 32, &_x, &_y, &quad);
  1963. vertex->m_x = quad.x0;
  1964. vertex->m_y = quad.y0;
  1965. vertex->m_u = quad.s0;
  1966. vertex->m_v = quad.t0;
  1967. vertex->m_abgr = _abgr;
  1968. ++vertex;
  1969. vertex->m_x = quad.x1;
  1970. vertex->m_y = quad.y1;
  1971. vertex->m_u = quad.s1;
  1972. vertex->m_v = quad.t1;
  1973. vertex->m_abgr = _abgr;
  1974. ++vertex;
  1975. vertex->m_x = quad.x1;
  1976. vertex->m_y = quad.y0;
  1977. vertex->m_u = quad.s1;
  1978. vertex->m_v = quad.t0;
  1979. vertex->m_abgr = _abgr;
  1980. ++vertex;
  1981. vertex->m_x = quad.x0;
  1982. vertex->m_y = quad.y0;
  1983. vertex->m_u = quad.s0;
  1984. vertex->m_v = quad.t0;
  1985. vertex->m_abgr = _abgr;
  1986. ++vertex;
  1987. vertex->m_x = quad.x0;
  1988. vertex->m_y = quad.y1;
  1989. vertex->m_u = quad.s0;
  1990. vertex->m_v = quad.t1;
  1991. vertex->m_abgr = _abgr;
  1992. ++vertex;
  1993. vertex->m_x = quad.x1;
  1994. vertex->m_y = quad.y1;
  1995. vertex->m_u = quad.s1;
  1996. vertex->m_v = quad.t1;
  1997. vertex->m_abgr = _abgr;
  1998. ++vertex;
  1999. }
  2000. ++_text;
  2001. }
  2002. bgfx::setTexture(0, s_texColor, m_fonts[m_currentFontIdx].m_texture);
  2003. bgfx::setVertexBuffer(&tvb);
  2004. bgfx::setState(0
  2005. | BGFX_STATE_RGB_WRITE
  2006. | BGFX_STATE_ALPHA_WRITE
  2007. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  2008. );
  2009. bgfx::setProgram(m_textureProgram);
  2010. setCurrentScissor();
  2011. bgfx::submit(m_view);
  2012. }
  2013. #endif // USE_NANOVG_FONT
  2014. }
  2015. void screenQuad(int32_t _x, int32_t _y, int32_t _width, uint32_t _height)
  2016. {
  2017. if (bgfx::checkAvailTransientVertexBuffer(6, PosUvVertex::ms_decl) )
  2018. {
  2019. bgfx::TransientVertexBuffer vb;
  2020. bgfx::allocTransientVertexBuffer(&vb, 6, PosUvVertex::ms_decl);
  2021. PosUvVertex* vertex = (PosUvVertex*)vb.data;
  2022. const float widthf = float(_width);
  2023. const float heightf = float(_height);
  2024. const float minx = float(_x);
  2025. const float miny = float(_y);
  2026. const float maxx = minx+widthf;
  2027. const float maxy = miny+heightf;
  2028. const float texelHalfW = m_halfTexel/widthf;
  2029. const float texelHalfH = m_halfTexel/heightf;
  2030. const float minu = texelHalfW;
  2031. const float maxu = 1.0f - texelHalfW;
  2032. const float minv = texelHalfH;
  2033. const float maxv = texelHalfH + 1.0f;
  2034. vertex[0].m_x = minx;
  2035. vertex[0].m_y = miny;
  2036. vertex[0].m_u = minu;
  2037. vertex[0].m_v = minv;
  2038. vertex[1].m_x = maxx;
  2039. vertex[1].m_y = miny;
  2040. vertex[1].m_u = maxu;
  2041. vertex[1].m_v = minv;
  2042. vertex[2].m_x = maxx;
  2043. vertex[2].m_y = maxy;
  2044. vertex[2].m_u = maxu;
  2045. vertex[2].m_v = maxv;
  2046. vertex[3].m_x = maxx;
  2047. vertex[3].m_y = maxy;
  2048. vertex[3].m_u = maxu;
  2049. vertex[3].m_v = maxv;
  2050. vertex[4].m_x = minx;
  2051. vertex[4].m_y = maxy;
  2052. vertex[4].m_u = minu;
  2053. vertex[4].m_v = maxv;
  2054. vertex[5].m_x = minx;
  2055. vertex[5].m_y = miny;
  2056. vertex[5].m_u = minu;
  2057. vertex[5].m_v = minv;
  2058. bgfx::setVertexBuffer(&vb);
  2059. }
  2060. }
  2061. void colorWheelWidget(float _rgb[3], bool _respectIndentation, bool _enabled)
  2062. {
  2063. const uint32_t wheelId = getId();
  2064. const uint32_t triangleId = getId();
  2065. Area& area = getCurrentArea();
  2066. const int32_t height = area.m_contentWidth - COLOR_WHEEL_PADDING;
  2067. const float heightf = float(height);
  2068. const float widthf = float(area.m_contentWidth - COLOR_WHEEL_PADDING);
  2069. const float xx = float( (_respectIndentation ? area.m_widgetX-SCROLL_AREA_PADDING : area.m_contentX) + COLOR_WHEEL_PADDING/2);
  2070. const float yy = float(area.m_widgetY);
  2071. area.m_widgetY += height + DEFAULT_SPACING;
  2072. const float ro = (widthf < heightf ? widthf : heightf) * 0.5f - 5.0f; // radiusOuter.
  2073. const float rd = 20.0f; // radiusDelta.
  2074. const float ri = ro - rd; // radiusInner.
  2075. const float aeps = 0.5f / ro; // Half a pixel arc length in radians (2pi cancels out).
  2076. const float center[2] = { xx + widthf*0.5f, yy + heightf*0.5f };
  2077. const float cmx = float(m_mx) - center[0];
  2078. const float cmy = float(m_my) - center[1];
  2079. const float aa[2] = { ri - 6.0f, 0.0f }; // Hue point.
  2080. const float bb[2] = { cosf(-120.0f/180.0f*NVG_PI) * aa[0], sinf(-120.0f/180.0f*NVG_PI) * aa[0] }; // Black point.
  2081. const float cc[2] = { cosf( 120.0f/180.0f*NVG_PI) * aa[0], sinf( 120.0f/180.0f*NVG_PI) * aa[0] }; // White point.
  2082. const float ca[2] = { aa[0] - cc[0], aa[1] - cc[1] };
  2083. const float lenCa = sqrtf(ca[0]*ca[0]+ca[1]*ca[1]);
  2084. const float invLenCa = 1.0f/lenCa;
  2085. const float dirCa[2] = { ca[0]*invLenCa, ca[1]*invLenCa };
  2086. float sel[2];
  2087. float hsv[3];
  2088. bx::rgbToHsv(hsv, _rgb);
  2089. const bool enabled = _enabled && isEnabled(m_areaId);
  2090. if (enabled)
  2091. {
  2092. if (m_leftPressed)
  2093. {
  2094. const float len = sqrtf(cmx*cmx + cmy*cmy);
  2095. if (len > ri)
  2096. {
  2097. if (len < ro)
  2098. {
  2099. setActive(wheelId);
  2100. }
  2101. }
  2102. else
  2103. {
  2104. setActive(triangleId);
  2105. }
  2106. }
  2107. if (m_leftReleased
  2108. && (isActive(wheelId) || isActive(triangleId) ) )
  2109. {
  2110. clearActive();
  2111. }
  2112. // Set hue.
  2113. if (m_left
  2114. && isActive(wheelId) )
  2115. {
  2116. hsv[0] = atan2f(cmy, cmx)/NVG_PI*0.5f;
  2117. if (hsv[0] < 0.0f)
  2118. {
  2119. hsv[0]+=1.0f;
  2120. }
  2121. }
  2122. }
  2123. if (enabled
  2124. && m_left
  2125. && isActive(triangleId) )
  2126. {
  2127. float an = -hsv[0]*NVG_PI*2.0f;
  2128. float tmx = (cmx*cosf(an)-cmy*sinf(an) );
  2129. float tmy = (cmx*sinf(an)+cmy*cosf(an) );
  2130. if (pointInTriangle(tmx, tmy, aa[0], aa[1], bb[0], bb[1], cc[0], cc[1]) )
  2131. {
  2132. sel[0] = tmx;
  2133. sel[1] = tmy;
  2134. }
  2135. else
  2136. {
  2137. closestPointOnTriangle(sel[0], sel[1], tmx, tmy, aa[0], aa[1], bb[0], bb[1], cc[0], cc[1]);
  2138. }
  2139. }
  2140. else
  2141. {
  2142. /*
  2143. * bb (black)
  2144. * /\
  2145. * / \
  2146. * / \
  2147. * / \
  2148. * / \
  2149. * / .sel \
  2150. * / \
  2151. * cc(white)/____.ss_______\aa (hue)
  2152. */
  2153. const float ss[2] =
  2154. {
  2155. cc[0] + dirCa[0]*lenCa*hsv[1],
  2156. cc[1] + dirCa[1]*lenCa*hsv[1],
  2157. };
  2158. const float sb[2] = { bb[0]-ss[0], bb[1]-ss[1] };
  2159. const float lenSb = sqrtf(sb[0]*sb[0]+sb[1]*sb[1]);
  2160. const float invLenSb = 1.0f/lenSb;
  2161. const float dirSb[2] = { sb[0]*invLenSb, sb[1]*invLenSb };
  2162. sel[0] = cc[0] + dirCa[0]*lenCa*hsv[1] + dirSb[0]*lenSb*(1.0f - hsv[2]);
  2163. sel[1] = cc[1] + dirCa[1]*lenCa*hsv[1] + dirSb[1]*lenSb*(1.0f - hsv[2]);
  2164. }
  2165. float uu, vv, ww;
  2166. barycentric(uu, vv, ww
  2167. , aa[0], aa[1]
  2168. , bb[0], bb[1]
  2169. , cc[0], cc[1]
  2170. , sel[0], sel[1]
  2171. );
  2172. const float val = bx::fclamp(1.0f-vv, 0.0001f, 1.0f);
  2173. const float sat = bx::fclamp(uu/val, 0.0001f, 1.0f);
  2174. const float out[3] = { hsv[0], sat, val };
  2175. bx::hsvToRgb(_rgb, out);
  2176. // Draw widget.
  2177. nvgSave(m_nvg);
  2178. {
  2179. float saturation;
  2180. uint8_t alpha0;
  2181. uint8_t alpha1;
  2182. if (enabled)
  2183. {
  2184. saturation = 1.0f;
  2185. alpha0 = 255;
  2186. alpha1 = 192;
  2187. }
  2188. else
  2189. {
  2190. saturation = 0.0f;
  2191. alpha0 = 10;
  2192. alpha1 = 10;
  2193. }
  2194. // Circle.
  2195. for (uint8_t ii = 0; ii < 6; ii++)
  2196. {
  2197. const float a0 = float(ii)/6.0f * 2.0f*NVG_PI - aeps;
  2198. const float a1 = float(ii+1.0f)/6.0f * 2.0f*NVG_PI + aeps;
  2199. nvgBeginPath(m_nvg);
  2200. nvgArc(m_nvg, center[0], center[1], ri, a0, a1, NVG_CW);
  2201. nvgArc(m_nvg, center[0], center[1], ro, a1, a0, NVG_CCW);
  2202. nvgClosePath(m_nvg);
  2203. const float ax = center[0] + cosf(a0) * (ri+ro)*0.5f;
  2204. const float ay = center[1] + sinf(a0) * (ri+ro)*0.5f;
  2205. const float bx = center[0] + cosf(a1) * (ri+ro)*0.5f;
  2206. const float by = center[1] + sinf(a1) * (ri+ro)*0.5f;
  2207. NVGpaint paint = nvgLinearGradient(m_nvg
  2208. , ax, ay
  2209. , bx, by
  2210. , nvgHSLA(a0/NVG_PI*0.5f,saturation,0.55f,alpha0)
  2211. , nvgHSLA(a1/NVG_PI*0.5f,saturation,0.55f,alpha0)
  2212. );
  2213. nvgFillPaint(m_nvg, paint);
  2214. nvgFill(m_nvg);
  2215. }
  2216. // Circle stroke.
  2217. nvgBeginPath(m_nvg);
  2218. nvgCircle(m_nvg, center[0], center[1], ri-0.5f);
  2219. nvgCircle(m_nvg, center[0], center[1], ro+0.5f);
  2220. nvgStrokeColor(m_nvg, nvgRGBA(0,0,0,64) );
  2221. nvgStrokeWidth(m_nvg, 1.0f);
  2222. nvgStroke(m_nvg);
  2223. nvgSave(m_nvg);
  2224. {
  2225. // Hue selector.
  2226. nvgTranslate(m_nvg, center[0], center[1]);
  2227. nvgRotate(m_nvg, hsv[0]*NVG_PI*2.0f);
  2228. nvgStrokeWidth(m_nvg, 2.0f);
  2229. nvgBeginPath(m_nvg);
  2230. nvgRect(m_nvg, ri-1.0f,-3.0f,rd+2.0f,6.0f);
  2231. nvgStrokeColor(m_nvg, nvgRGBA(255,255,255,alpha1) );
  2232. nvgStroke(m_nvg);
  2233. // Hue selector drop shadow.
  2234. 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) );
  2235. nvgBeginPath(m_nvg);
  2236. nvgRect(m_nvg, ri-2.0f-10.0f,-4.0f-10.0f,ro-ri+4.0f+20.0f,8.0f+20.0f);
  2237. nvgRect(m_nvg, ri-2.0f,-4.0f,ro-ri+4.0f,8.0f);
  2238. nvgPathWinding(m_nvg, NVG_HOLE);
  2239. nvgFillPaint(m_nvg, paint);
  2240. nvgFill(m_nvg);
  2241. // Center triangle stroke.
  2242. nvgBeginPath(m_nvg);
  2243. nvgMoveTo(m_nvg, aa[0], aa[1]);
  2244. nvgLineTo(m_nvg, bb[0], bb[1]);
  2245. nvgLineTo(m_nvg, cc[0], cc[1]);
  2246. nvgClosePath(m_nvg);
  2247. nvgStrokeColor(m_nvg, nvgRGBA(0,0,0,64) );
  2248. nvgStroke(m_nvg);
  2249. // Center triangle fill.
  2250. paint = nvgLinearGradient(m_nvg, aa[0], aa[1], bb[0], bb[1], nvgHSL(hsv[0],saturation,0.5f), nvgRGBA(0,0,0,alpha0) );
  2251. nvgFillPaint(m_nvg, paint);
  2252. nvgFill(m_nvg);
  2253. 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) );
  2254. nvgFillPaint(m_nvg, paint);
  2255. nvgFill(m_nvg);
  2256. // Color selector.
  2257. nvgStrokeWidth(m_nvg, 2.0f);
  2258. nvgBeginPath(m_nvg);
  2259. nvgCircle(m_nvg, sel[0], sel[1], 5);
  2260. nvgStrokeColor(m_nvg, nvgRGBA(255,255,255,alpha1) );
  2261. nvgStroke(m_nvg);
  2262. // Color selector stroke.
  2263. paint = nvgRadialGradient(m_nvg, sel[0], sel[1], 7.0f, 9.0f, nvgRGBA(0,0,0,64), nvgRGBA(0,0,0,0) );
  2264. nvgBeginPath(m_nvg);
  2265. nvgRect(m_nvg, sel[0]-20.0f, sel[1]-20.0f, 40.0f, 40.0f);
  2266. nvgCircle(m_nvg, sel[0], sel[1], 7.0f);
  2267. nvgPathWinding(m_nvg, NVG_HOLE);
  2268. nvgFillPaint(m_nvg, paint);
  2269. nvgFill(m_nvg);
  2270. }
  2271. nvgRestore(m_nvg);
  2272. }
  2273. nvgRestore(m_nvg);
  2274. }
  2275. struct Area
  2276. {
  2277. int32_t m_x;
  2278. int32_t m_y;
  2279. int32_t m_width;
  2280. int32_t m_height;
  2281. int16_t m_contentX;
  2282. int16_t m_contentY;
  2283. int16_t m_contentWidth;
  2284. int16_t m_contentHeight;
  2285. int16_t m_scissorX;
  2286. int16_t m_scissorY;
  2287. int16_t m_scissorHeight;
  2288. int16_t m_scissorWidth;
  2289. int32_t m_widgetX;
  2290. int32_t m_widgetY;
  2291. int32_t m_widgetW;
  2292. int32_t* m_scrollVal;
  2293. uint32_t m_scrollId;
  2294. bool m_inside;
  2295. bool m_scissorEnabled;
  2296. };
  2297. inline Area& getCurrentArea()
  2298. {
  2299. return m_areas[m_areaId];
  2300. }
  2301. inline void setCurrentScissor()
  2302. {
  2303. const Area& area = getCurrentArea();
  2304. if (area.m_scissorEnabled)
  2305. {
  2306. bgfx::setScissor(uint16_t(IMGUI_MAX(0, area.m_scissorX) )
  2307. , uint16_t(IMGUI_MAX(0, area.m_scissorY-1) )
  2308. , area.m_scissorWidth
  2309. , area.m_scissorHeight+1
  2310. );
  2311. }
  2312. else
  2313. {
  2314. bgfx::setScissor(UINT16_MAX);
  2315. }
  2316. }
  2317. template <typename Ty, uint16_t Max=64>
  2318. struct IdStack
  2319. {
  2320. IdStack()
  2321. {
  2322. reset();
  2323. }
  2324. void reset()
  2325. {
  2326. m_current = 0;
  2327. m_idGen = 0;
  2328. m_ids[0] = 0;
  2329. }
  2330. void next()
  2331. {
  2332. BX_CHECK(Max > (m_current+1), "Param out of bounds!");
  2333. m_ids[++m_current] = ++m_idGen;
  2334. }
  2335. void previous()
  2336. {
  2337. m_current = m_current > 0 ? m_current-1 : 0;
  2338. }
  2339. Ty current() const
  2340. {
  2341. BX_CHECK(Max > (m_current), "Param out of bounds!");
  2342. return m_ids[m_current];
  2343. }
  2344. operator Ty() const
  2345. {
  2346. BX_CHECK(Max > (m_current), "Param out of bounds!");
  2347. return m_ids[m_current];
  2348. }
  2349. private:
  2350. uint16_t m_current;
  2351. Ty m_idGen;
  2352. Ty m_ids[Max];
  2353. };
  2354. int32_t m_mx;
  2355. int32_t m_my;
  2356. int32_t m_scroll;
  2357. uint32_t m_active;
  2358. uint32_t m_hot;
  2359. uint32_t m_hotToBe;
  2360. char m_char;
  2361. char m_lastChar;
  2362. uint32_t m_inputField;
  2363. int32_t m_dragX;
  2364. int32_t m_dragY;
  2365. float m_dragOrig;
  2366. bool m_left;
  2367. bool m_leftPressed;
  2368. bool m_leftReleased;
  2369. bool m_isHot;
  2370. bool m_wentActive;
  2371. bool m_insideArea;
  2372. bool m_isActivePresent;
  2373. bool m_checkActivePresence;
  2374. IdStack<uint16_t> m_areaId;
  2375. uint16_t m_widgetId;
  2376. uint64_t m_enabledAreaIds;
  2377. Area m_areas[64];
  2378. float m_tempCoords[MAX_TEMP_COORDS * 2];
  2379. float m_tempNormals[MAX_TEMP_COORDS * 2];
  2380. float m_circleVerts[NUM_CIRCLE_VERTS * 2];
  2381. uint16_t m_textureWidth;
  2382. uint16_t m_textureHeight;
  2383. float m_invTextureWidth;
  2384. float m_invTextureHeight;
  2385. float m_halfTexel;
  2386. NVGcontext* m_nvg;
  2387. uint8_t m_view;
  2388. uint16_t m_viewWidth;
  2389. uint16_t m_viewHeight;
  2390. #if !USE_NANOVG_FONT
  2391. struct Font
  2392. {
  2393. stbtt_bakedchar m_cdata[96]; // ASCII 32..126 is 95 glyphs
  2394. bgfx::TextureHandle m_texture;
  2395. float m_size;
  2396. };
  2397. uint16_t m_currentFontIdx;
  2398. bx::HandleAllocT<IMGUI_CONFIG_MAX_FONTS> m_fontHandle;
  2399. Font m_fonts[IMGUI_CONFIG_MAX_FONTS];
  2400. #endif // !USE_NANOVG_FONT
  2401. bgfx::UniformHandle u_imageLod;
  2402. bgfx::UniformHandle u_imageSwizzle;
  2403. bgfx::UniformHandle s_texColor;
  2404. bgfx::ProgramHandle m_colorProgram;
  2405. bgfx::ProgramHandle m_textureProgram;
  2406. bgfx::ProgramHandle m_imageProgram;
  2407. bgfx::ProgramHandle m_imageSwizzProgram;
  2408. bgfx::TextureHandle m_missingTexture;
  2409. };
  2410. static Imgui s_imgui;
  2411. ImguiFontHandle imguiCreate(const void* _data, float _fontSize)
  2412. {
  2413. return s_imgui.create(_data, _fontSize);
  2414. }
  2415. void imguiDestroy()
  2416. {
  2417. s_imgui.destroy();
  2418. }
  2419. ImguiFontHandle imguiCreateFont(const void* _data, float _fontSize)
  2420. {
  2421. return s_imgui.createFont(_data, _fontSize);
  2422. }
  2423. void imguiSetFont(ImguiFontHandle _handle)
  2424. {
  2425. s_imgui.setFont(_handle);
  2426. }
  2427. ImguiFontHandle imguiGetCurrentFont()
  2428. {
  2429. const ImguiFontHandle handle = { s_imgui.m_currentFontIdx };
  2430. return handle;
  2431. }
  2432. 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)
  2433. {
  2434. s_imgui.beginFrame(_mx, _my, _button, _scroll, _width, _height, _inputChar, _view);
  2435. }
  2436. void imguiEndFrame()
  2437. {
  2438. s_imgui.endFrame();
  2439. }
  2440. void imguiDrawText(int32_t _x, int32_t _y, ImguiTextAlign::Enum _align, const char* _text, uint32_t _argb)
  2441. {
  2442. s_imgui.drawText(_x, _y, _align, _text, _argb);
  2443. }
  2444. void imguiDrawLine(float _x0, float _y0, float _x1, float _y1, float _r, uint32_t _argb)
  2445. {
  2446. s_imgui.drawLine(_x0, _y0, _x1, _y1, _r, _argb);
  2447. }
  2448. void imguiDrawRoundedRect(float _x, float _y, float _width, float _height, float _r, uint32_t _argb)
  2449. {
  2450. s_imgui.drawRoundedRect(_x, _y, _width, _height, _r, _argb);
  2451. }
  2452. void imguiDrawRect(float _x, float _y, float _width, float _height, uint32_t _argb)
  2453. {
  2454. s_imgui.drawRect(_x, _y, _width, _height, _argb);
  2455. }
  2456. bool imguiBorderButton(ImguiBorder::Enum _border, bool _checked, bool _enabled)
  2457. {
  2458. return s_imgui.borderButton(_border, _checked, _enabled);
  2459. }
  2460. bool imguiBeginArea(const char* _name, int _x, int _y, int _width, int _height, bool _enabled, int32_t _r)
  2461. {
  2462. return s_imgui.beginArea(_name, _x, _y, _width, _height, _enabled, _r);
  2463. }
  2464. void imguiEndArea()
  2465. {
  2466. return s_imgui.endArea();
  2467. }
  2468. bool imguiBeginScroll(int32_t _height, int32_t* _scroll, bool _enabled)
  2469. {
  2470. return s_imgui.beginScroll(_height, _scroll, _enabled);
  2471. }
  2472. void imguiEndScroll(int32_t _r)
  2473. {
  2474. s_imgui.endScroll(_r);
  2475. }
  2476. bool imguiBeginScrollArea(const char* _name, int32_t _x, int32_t _y, int32_t _width, int32_t _height, int32_t* _scroll, bool _enabled, int32_t _r)
  2477. {
  2478. const bool result = s_imgui.beginArea(_name, _x, _y, _width, _height, _enabled, _r);
  2479. const bool hasTitle = (NULL != _name && '\0' != _name[0]);
  2480. const int32_t margins = int32_t(hasTitle)*(AREA_HEADER+2*SCROLL_AREA_PADDING-1);
  2481. s_imgui.beginScroll(_height - margins, _scroll, _enabled);
  2482. return result;
  2483. }
  2484. void imguiEndScrollArea(int32_t _r)
  2485. {
  2486. s_imgui.endScroll(_r);
  2487. s_imgui.endArea();
  2488. }
  2489. void imguiIndent(uint16_t _width)
  2490. {
  2491. s_imgui.indent(_width);
  2492. }
  2493. void imguiUnindent(uint16_t _width)
  2494. {
  2495. s_imgui.unindent(_width);
  2496. }
  2497. void imguiSeparator(uint16_t _height)
  2498. {
  2499. s_imgui.separator(_height);
  2500. }
  2501. void imguiSeparatorLine(uint16_t _height)
  2502. {
  2503. s_imgui.separatorLine(_height);
  2504. }
  2505. int32_t imguiGetWidgetX()
  2506. {
  2507. return s_imgui.getCurrentArea().m_widgetX;
  2508. }
  2509. int32_t imguiGetWidgetY()
  2510. {
  2511. return s_imgui.getCurrentArea().m_widgetY;
  2512. }
  2513. bool imguiButton(const char* _text, bool _enabled, ImguiAlign::Enum _align, uint32_t _rgb0, int32_t _r)
  2514. {
  2515. return s_imgui.button(_text, _enabled, _align, _rgb0, _r);
  2516. }
  2517. bool imguiItem(const char* _text, bool _enabled)
  2518. {
  2519. return s_imgui.item(_text, _enabled);
  2520. }
  2521. bool imguiCheck(const char* _text, bool _checked, bool _enabled)
  2522. {
  2523. return s_imgui.check(_text, _checked, _enabled);
  2524. }
  2525. void imguiBool(const char* _text, bool& _flag, bool _enabled)
  2526. {
  2527. if (imguiCheck(_text, _flag, _enabled) )
  2528. {
  2529. _flag = !_flag;
  2530. }
  2531. }
  2532. bool imguiCollapse(const char* _text, const char* _subtext, bool _checked, bool _enabled)
  2533. {
  2534. return s_imgui.collapse(_text, _subtext, _checked, _enabled);
  2535. }
  2536. void imguiLabel(const char* _format, ...)
  2537. {
  2538. va_list argList;
  2539. va_start(argList, _format);
  2540. s_imgui.labelVargs(_format, argList, imguiRGBA(255, 255, 255, 255) );
  2541. va_end(argList);
  2542. }
  2543. void imguiLabel(uint32_t _rgba, const char* _format, ...)
  2544. {
  2545. va_list argList;
  2546. va_start(argList, _format);
  2547. s_imgui.labelVargs(_format, argList, _rgba);
  2548. va_end(argList);
  2549. }
  2550. void imguiValue(const char* _text)
  2551. {
  2552. s_imgui.value(_text);
  2553. }
  2554. bool imguiSlider(const char* _text, float& _val, float _vmin, float _vmax, float _vinc, bool _enabled, ImguiAlign::Enum _align)
  2555. {
  2556. return s_imgui.slider(_text, _val, _vmin, _vmax, _vinc, _enabled, _align);
  2557. }
  2558. bool imguiSlider(const char* _text, int32_t& _val, int32_t _vmin, int32_t _vmax, bool _enabled, ImguiAlign::Enum _align)
  2559. {
  2560. float val = (float)_val;
  2561. bool result = s_imgui.slider(_text, val, (float)_vmin, (float)_vmax, 1.0f, _enabled, _align);
  2562. _val = (int32_t)val;
  2563. return result;
  2564. }
  2565. void imguiInput(const char* _label, char* _str, uint32_t _len, bool _enabled, ImguiAlign::Enum _align, int32_t _r)
  2566. {
  2567. s_imgui.input(_label, _str, _len, _enabled, _align, _r);
  2568. }
  2569. uint8_t imguiTabsUseMacroInstead(uint8_t _selected, ...)
  2570. {
  2571. va_list argList;
  2572. va_start(argList, _selected);
  2573. const uint8_t result = s_imgui.tabs(_selected, true, ImguiAlign::LeftIndented, BUTTON_HEIGHT, BUTTON_HEIGHT/2 - 1, argList);
  2574. va_end(argList);
  2575. return result;
  2576. }
  2577. uint8_t imguiTabsUseMacroInstead(uint8_t _selected, bool _enabled, ...)
  2578. {
  2579. va_list argList;
  2580. va_start(argList, _enabled);
  2581. const uint8_t result = s_imgui.tabs(_selected, _enabled, ImguiAlign::LeftIndented, BUTTON_HEIGHT, BUTTON_HEIGHT/2 - 1, argList);
  2582. va_end(argList);
  2583. return result;
  2584. }
  2585. uint8_t imguiTabsUseMacroInstead(uint8_t _selected, bool _enabled, ImguiAlign::Enum _align, ...)
  2586. {
  2587. va_list argList;
  2588. va_start(argList, _align);
  2589. const uint8_t result = s_imgui.tabs(_selected, _enabled, _align, BUTTON_HEIGHT, BUTTON_HEIGHT/2 - 1, argList);
  2590. va_end(argList);
  2591. return result;
  2592. }
  2593. uint8_t imguiTabsUseMacroInstead(uint8_t _selected, bool _enabled, ImguiAlign::Enum _align, int32_t _height, int32_t _r, ...)
  2594. {
  2595. va_list argList;
  2596. va_start(argList, _r);
  2597. const uint8_t result = s_imgui.tabs(_selected, _enabled, _align, _height, _r, argList);
  2598. va_end(argList);
  2599. return result;
  2600. }
  2601. uint32_t imguiChooseUseMacroInstead(uint32_t _selected, ...)
  2602. {
  2603. va_list argList;
  2604. va_start(argList, _selected);
  2605. const char* str = va_arg(argList, const char*);
  2606. for (uint32_t ii = 0; str != NULL; ++ii, str = va_arg(argList, const char*) )
  2607. {
  2608. if (imguiCheck(str, ii == _selected) )
  2609. {
  2610. _selected = ii;
  2611. }
  2612. }
  2613. va_end(argList);
  2614. return _selected;
  2615. }
  2616. void imguiColorWheel(float _rgb[3], bool _respectIndentation, bool _enabled)
  2617. {
  2618. s_imgui.colorWheelWidget(_rgb, _respectIndentation, _enabled);
  2619. }
  2620. void imguiColorWheel(const char* _text, float _rgb[3], bool& _activated, bool _enabled)
  2621. {
  2622. char buf[128];
  2623. bx::snprintf(buf, sizeof(buf), "[RGB %-2.2f %-2.2f %-2.2f]"
  2624. , _rgb[0]
  2625. , _rgb[1]
  2626. , _rgb[2]
  2627. );
  2628. if (imguiCollapse(_text, buf, _activated, _enabled) )
  2629. {
  2630. _activated = !_activated;
  2631. }
  2632. if (_activated)
  2633. {
  2634. imguiColorWheel(_rgb, false, _enabled);
  2635. }
  2636. }
  2637. void imguiImage(bgfx::TextureHandle _image, float _lod, int32_t _width, int32_t _height, ImguiAlign::Enum _align)
  2638. {
  2639. s_imgui.image(_image, _lod, _width, _height, _align);
  2640. }
  2641. void imguiImage(bgfx::TextureHandle _image, float _lod, float _width, float _aspect, ImguiAlign::Enum _align)
  2642. {
  2643. s_imgui.image(_image, _lod, _width, _aspect, _align);
  2644. }
  2645. void imguiImageChannel(bgfx::TextureHandle _image, uint8_t _channel, float _lod, int32_t _width, int32_t _height, ImguiAlign::Enum _align)
  2646. {
  2647. s_imgui.imageChannel(_image, _channel, _lod, _width, _height, _align);
  2648. }
  2649. void imguiImageChannel(bgfx::TextureHandle _image, uint8_t _channel, float _lod, float _width, float _aspect, ImguiAlign::Enum _align)
  2650. {
  2651. s_imgui.imageChannel(_image, _channel, _lod, _width, _aspect, _align);
  2652. }
  2653. float imguiGetTextLength(const char* _text, ImguiFontHandle _handle)
  2654. {
  2655. #if !USE_NANOVG_FONT
  2656. uint32_t numVertices = 0; //unused
  2657. return getTextLength(s_imgui.m_fonts[_handle.idx].m_cdata, _text, numVertices);
  2658. #else
  2659. return 0.0f;
  2660. #endif
  2661. }
  2662. bool imguiMouseOverArea()
  2663. {
  2664. return s_imgui.m_insideArea;
  2665. }