imgui.cpp 81 KB

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