imgui.cpp 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314
  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 float s_tabStops[4] = {150, 210, 270, 330};
  54. static void* imguiMalloc(size_t size, void* /*_userptr*/)
  55. {
  56. return malloc(size);
  57. }
  58. static void imguiFree(void* _ptr, void* /*_userptr*/)
  59. {
  60. free(_ptr);
  61. }
  62. #define IMGUI_MIN(_a, _b) (_a)<(_b)?(_a):(_b)
  63. #define IMGUI_MAX(_a, _b) (_a)>(_b)?(_a):(_b)
  64. #define IMGUI_CLAMP(_a, _min, _max) IMGUI_MIN(IMGUI_MAX(_a, _min), _max)
  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/stb_truetype.h>
  69. namespace
  70. {
  71. static uint32_t addQuad(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_imageLodEnabled.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(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)(bx::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_imageLodEnabled = bgfx::createUniform("u_imageLodEnabled", bgfx::UniformType::Uniform4fv);
  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_imageLodEnabled);
  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. m_view = _view;
  661. m_viewWidth = _width;
  662. m_viewHeight = _height;
  663. bgfx::setViewName(_view, "IMGUI");
  664. bgfx::setViewSeq(_view, true);
  665. const bgfx::HMD* hmd = bgfx::getHMD();
  666. if (NULL != hmd)
  667. {
  668. m_viewWidth = _width / 2;
  669. float proj[16];
  670. bx::mtxProj(proj, hmd->eye[0].fov, 0.1f, 100.0f);
  671. static float time = 0.0f;
  672. time += 0.05f;
  673. const float dist = 10.0f;
  674. const float offset0 = -proj[8] + (hmd->eye[0].viewOffset[0] / dist * proj[0]);
  675. const float offset1 = -proj[8] + (hmd->eye[1].viewOffset[0] / dist * proj[0]);
  676. float ortho[2][16];
  677. const float viewOffset = _width/4.0f;
  678. const float viewWidth = _width/2.0f;
  679. bx::mtxOrtho(ortho[0], viewOffset, viewOffset + viewWidth, (float)m_viewHeight, 0.0f, 0.0f, 1000.0f, offset0);
  680. bx::mtxOrtho(ortho[1], viewOffset, viewOffset + viewWidth, (float)m_viewHeight, 0.0f, 0.0f, 1000.0f, offset1);
  681. bgfx::setViewTransform(_view, NULL, ortho[0], BGFX_VIEW_STEREO, ortho[1]);
  682. bgfx::setViewRect(_view, 0, 0, hmd->width, hmd->height);
  683. }
  684. else
  685. {
  686. float ortho[16];
  687. bx::mtxOrtho(ortho, 0.0f, (float)m_viewWidth, (float)m_viewHeight, 0.0f, 0.0f, 1000.0f);
  688. bgfx::setViewTransform(_view, NULL, ortho);
  689. bgfx::setViewRect(_view, 0, 0, _width, _height);
  690. }
  691. updateInput(_mx, _my, _button, _scroll, _inputChar);
  692. m_hot = m_hotToBe;
  693. m_hotToBe = 0;
  694. m_wentActive = false;
  695. m_isHot = false;
  696. Area& area = getCurrentArea();
  697. area.m_widgetX = 0;
  698. area.m_widgetY = 0;
  699. area.m_widgetW = 0;
  700. m_areaId.reset();
  701. m_widgetId = 0;
  702. m_enabledAreaIds = 0;
  703. m_insideArea = false;
  704. m_isActivePresent = false;
  705. }
  706. void endFrame()
  707. {
  708. if (m_checkActivePresence && !m_isActivePresent)
  709. {
  710. // The ui element is not present any more, reset active field.
  711. m_active = 0;
  712. }
  713. m_checkActivePresence = (0 != m_active);
  714. clearInput();
  715. }
  716. bool beginScroll(int32_t _height, int32_t* _scroll, bool _enabled)
  717. {
  718. Area& parentArea = getCurrentArea();
  719. m_areaId.next();
  720. const uint32_t scrollId = getId();
  721. Area& area = getCurrentArea();
  722. const uint16_t parentBottom = parentArea.m_scissorY + parentArea.m_scissorHeight;
  723. const uint16_t childBottom = parentArea.m_widgetY + _height;
  724. const uint16_t bottom = IMGUI_MIN(childBottom, parentBottom);
  725. const uint16_t top = IMGUI_MAX(parentArea.m_widgetY, parentArea.m_scissorY);
  726. area.m_contentX = parentArea.m_contentX;
  727. area.m_contentY = parentArea.m_widgetY;
  728. area.m_contentWidth = parentArea.m_contentWidth - (SCROLL_AREA_PADDING*3);
  729. area.m_contentHeight = _height;
  730. area.m_widgetX = parentArea.m_widgetX;
  731. area.m_widgetY = parentArea.m_widgetY + (*_scroll);
  732. area.m_widgetW = parentArea.m_widgetW - (SCROLL_AREA_PADDING*3);
  733. area.m_scissorX = area.m_contentX;
  734. area.m_scissorWidth = area.m_contentWidth;
  735. area.m_scissorY = top;
  736. area.m_scissorHeight = bottom - top;
  737. area.m_scissorEnabled = true;
  738. area.m_height = _height;
  739. area.m_scrollVal = _scroll;
  740. area.m_scrollId = scrollId;
  741. area.m_inside = inRect(parentArea.m_scissorX
  742. , area.m_scissorY
  743. , parentArea.m_scissorWidth
  744. , area.m_scissorHeight
  745. , false
  746. );
  747. parentArea.m_widgetY += (_height + DEFAULT_SPACING);
  748. if (_enabled)
  749. {
  750. setEnabled(m_areaId);
  751. }
  752. nvgScissor(m_nvg
  753. , float(area.m_scissorX)
  754. , float(area.m_scissorY-1)
  755. , float(area.m_scissorWidth)
  756. , float(area.m_scissorHeight+1)
  757. );
  758. m_insideArea |= area.m_inside;
  759. return area.m_inside;
  760. }
  761. void endScroll(int32_t _r)
  762. {
  763. Area& area = getCurrentArea();
  764. area.m_scissorEnabled = false;
  765. const int32_t xx = area.m_contentX + area.m_contentWidth - 1;
  766. const int32_t yy = area.m_contentY;
  767. const int32_t width = SCROLL_AREA_PADDING * 2;
  768. const int32_t height = area.m_height;
  769. const int32_t aa = area.m_contentY+area.m_height;
  770. const int32_t bb = area.m_widgetY-DEFAULT_SPACING;
  771. const int32_t sbot = IMGUI_MAX(aa, bb);
  772. const int32_t stop = area.m_contentY + (*area.m_scrollVal);
  773. const int32_t sh = IMGUI_MAX(1, sbot - stop); // The scrollable area height.
  774. // Handle mouse scrolling.
  775. if (area.m_inside && !anyActive() )
  776. {
  777. if (m_scroll)
  778. {
  779. const int32_t diff = height - sh;
  780. const int32_t val = *area.m_scrollVal + 20*m_scroll;
  781. const int32_t min = (diff < 0) ? diff : *area.m_scrollVal;
  782. const int32_t max = 0;
  783. *area.m_scrollVal = IMGUI_CLAMP(val, min, max);
  784. }
  785. }
  786. const uint32_t hid = area.m_scrollId;
  787. const float barHeight = (float)height / (float)sh;
  788. // Draw and handle scroll click.
  789. if (barHeight < 1.0f)
  790. {
  791. const float barY = bx::fsaturate( (float)(-(*area.m_scrollVal) ) / (float)sh);
  792. // Handle scroll bar logic.
  793. const int32_t hx = xx;
  794. const int32_t hy = yy + (int)(barY * height);
  795. const int32_t hw = width;
  796. const int32_t hh = (int)(barHeight * height);
  797. const int32_t range = height - (hh - 1);
  798. const bool over = inRect(hx, hy, hw, hh);
  799. buttonLogic(hid, over);
  800. if (isActive(hid) )
  801. {
  802. float uu = (float)(hy - yy) / (float)range;
  803. if (m_wentActive)
  804. {
  805. m_dragY = m_my;
  806. m_dragOrig = uu;
  807. }
  808. if (m_dragY != m_my)
  809. {
  810. const int32_t diff = height - sh;
  811. const int32_t drag = m_my - m_dragY;
  812. const float dragFactor = float(sh)/float(height);
  813. const int32_t val = *area.m_scrollVal - int32_t(drag*dragFactor);
  814. const int32_t min = (diff < 0) ? diff : *area.m_scrollVal;
  815. const int32_t max = 0;
  816. *area.m_scrollVal = IMGUI_CLAMP(val, min, max);
  817. m_dragY = m_my;
  818. }
  819. }
  820. // BG
  821. drawRoundedRect( (float)xx
  822. , (float)yy
  823. , (float)width
  824. , (float)height
  825. , (float)_r
  826. , imguiRGBA(0, 0, 0, 196)
  827. );
  828. // Bar
  829. if (isActive(hid) )
  830. {
  831. drawRoundedRect( (float)hx
  832. , (float)hy
  833. , (float)hw
  834. , (float)hh
  835. , (float)_r
  836. , imguiRGBA(255, 196, 0, 196)
  837. );
  838. }
  839. else
  840. {
  841. drawRoundedRect( (float)hx
  842. , (float)hy
  843. , (float)hw
  844. , (float)hh
  845. , (float)_r
  846. , isHot(hid) ? imguiRGBA(255, 196, 0, 96) : imguiRGBA(255, 255, 255, 64)
  847. );
  848. }
  849. }
  850. else
  851. {
  852. // Clear active if scroll is selected but not visible any more.
  853. if (isActive(hid))
  854. {
  855. clearActive();
  856. }
  857. }
  858. nvgResetScissor(m_nvg);
  859. area.m_inside = false;
  860. m_areaId.previous();
  861. }
  862. bool beginArea(const char* _name, int32_t _x, int32_t _y, int32_t _width, int32_t _height, bool _enabled, int32_t _r)
  863. {
  864. m_areaId.next();
  865. const uint32_t scrollId = getId();
  866. const bool hasTitle = (NULL != _name && '\0' != _name[0]);
  867. const int32_t header = hasTitle ? AREA_HEADER : 0;
  868. Area& area = getCurrentArea();
  869. area.m_x = _x;
  870. area.m_y = _y;
  871. area.m_width = _width;
  872. area.m_height = _height;
  873. area.m_contentX = area.m_x + SCROLL_AREA_PADDING;
  874. area.m_contentY = area.m_y + SCROLL_AREA_PADDING + header;
  875. area.m_contentWidth = area.m_width - SCROLL_AREA_PADDING;
  876. area.m_contentHeight = area.m_height - SCROLL_AREA_PADDING*2 - header;
  877. area.m_scissorX = area.m_contentX;
  878. area.m_scissorY = area.m_y + SCROLL_AREA_PADDING + header;
  879. area.m_scissorHeight = area.m_height - SCROLL_AREA_PADDING*2 - header;
  880. area.m_scissorWidth = area.m_contentWidth;
  881. area.m_scissorEnabled = false;
  882. area.m_widgetX = area.m_contentX;
  883. area.m_widgetY = area.m_contentY;
  884. area.m_widgetW = area.m_width - SCROLL_AREA_PADDING*2;
  885. static int32_t s_zeroScroll = 0;
  886. area.m_scrollVal = &s_zeroScroll;
  887. area.m_scrollId = scrollId;
  888. area.m_inside = inRect(area.m_scissorX, area.m_scissorY, area.m_scissorWidth, area.m_scissorHeight, false);
  889. if (_enabled)
  890. {
  891. setEnabled(m_areaId);
  892. }
  893. if (0 == _r)
  894. {
  895. drawRect( (float)_x
  896. , (float)_y
  897. , (float)_width + 0.3f /*border fix for seamlessly joining two scroll areas*/
  898. , (float)_height + 0.3f /*border fix for seamlessly joining two scroll areas*/
  899. , imguiRGBA(0, 0, 0, 192)
  900. );
  901. }
  902. else
  903. {
  904. drawRoundedRect( (float)_x
  905. , (float)_y
  906. , (float)_width
  907. , (float)_height
  908. , (float)_r
  909. , imguiRGBA(0, 0, 0, 192)
  910. );
  911. }
  912. if (hasTitle)
  913. {
  914. drawText(_x + 10
  915. , _y + 18
  916. , ImguiTextAlign::Left
  917. , _name
  918. , imguiRGBA(255, 255, 255, 128)
  919. );
  920. }
  921. area.m_scissorEnabled = true;
  922. nvgBeginFrame(m_nvg, m_viewWidth, m_viewHeight, 1.0f);
  923. nvgScissor(m_nvg
  924. , float(area.m_scissorX)
  925. , float(area.m_scissorY-1)
  926. , float(area.m_scissorWidth)
  927. , float(area.m_scissorHeight+1)
  928. );
  929. m_insideArea |= area.m_inside;
  930. return area.m_inside;
  931. }
  932. void endArea()
  933. {
  934. nvgResetScissor(m_nvg);
  935. nvgEndFrame(m_nvg);
  936. }
  937. bool button(const char* _text, bool _enabled, ImguiAlign::Enum _align, uint32_t _rgb0, int32_t _r)
  938. {
  939. const uint32_t id = getId();
  940. Area& area = getCurrentArea();
  941. const int32_t yy = area.m_widgetY;
  942. const int32_t height = BUTTON_HEIGHT;
  943. area.m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  944. int32_t xx;
  945. int32_t width;
  946. if (ImguiAlign::Left == _align)
  947. {
  948. xx = area.m_contentX + SCROLL_AREA_PADDING;
  949. width = area.m_widgetW;
  950. }
  951. else if (ImguiAlign::LeftIndented == _align
  952. || ImguiAlign::Right == _align)
  953. {
  954. xx = area.m_widgetX;
  955. width = area.m_widgetW-1; //TODO: -1 !
  956. }
  957. else //if (ImguiAlign::Center == _align
  958. //|| ImguiAlign::CenterIndented == _align).
  959. {
  960. xx = area.m_widgetX;
  961. width = area.m_widgetW - (area.m_widgetX-area.m_scissorX);
  962. }
  963. const bool enabled = _enabled && isEnabled(m_areaId);
  964. const bool over = enabled && inRect(xx, yy, width, height);
  965. const bool res = buttonLogic(id, over);
  966. const uint32_t rgb0 = _rgb0&0x00ffffff;
  967. drawRoundedRect( (float)xx
  968. , (float)yy
  969. , (float)width
  970. , (float)height
  971. , (float)_r
  972. , rgb0 | imguiRGBA(0, 0, 0, isActive(id) ? 196 : 96)
  973. );
  974. if (enabled)
  975. {
  976. drawText(xx + BUTTON_HEIGHT / 2
  977. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  978. , ImguiTextAlign::Left
  979. , _text
  980. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  981. );
  982. }
  983. else
  984. {
  985. drawText(xx + BUTTON_HEIGHT / 2
  986. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  987. , ImguiTextAlign::Left
  988. , _text
  989. , imguiRGBA(128, 128, 128, 200)
  990. );
  991. }
  992. return res;
  993. }
  994. bool item(const char* _text, bool _enabled)
  995. {
  996. const uint32_t id = getId();
  997. Area& area = getCurrentArea();
  998. const int32_t xx = area.m_widgetX;
  999. const int32_t yy = area.m_widgetY;
  1000. const int32_t width = area.m_widgetW;
  1001. const int32_t height = BUTTON_HEIGHT;
  1002. area.m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  1003. const bool enabled = _enabled && isEnabled(m_areaId);
  1004. const bool over = enabled && inRect(xx, yy, width, height);
  1005. const bool res = buttonLogic(id, over);
  1006. if (isHot(id) )
  1007. {
  1008. drawRoundedRect( (float)xx
  1009. , (float)yy
  1010. , (float)width
  1011. , (float)height
  1012. , 2.0f
  1013. , imguiRGBA(255, 196, 0, isActive(id) ? 196 : 96)
  1014. );
  1015. }
  1016. if (enabled)
  1017. {
  1018. drawText(xx + BUTTON_HEIGHT / 2
  1019. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1020. , ImguiTextAlign::Left
  1021. , _text
  1022. , imguiRGBA(255, 255, 255, 200)
  1023. );
  1024. }
  1025. else
  1026. {
  1027. drawText(xx + BUTTON_HEIGHT / 2
  1028. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1029. , ImguiTextAlign::Left
  1030. , _text
  1031. , imguiRGBA(128, 128, 128, 200)
  1032. );
  1033. }
  1034. return res;
  1035. }
  1036. bool check(const char* _text, bool _checked, bool _enabled)
  1037. {
  1038. const uint32_t id = getId();
  1039. Area& area = getCurrentArea();
  1040. const int32_t xx = area.m_widgetX;
  1041. const int32_t yy = area.m_widgetY;
  1042. const int32_t width = area.m_widgetW;
  1043. const int32_t height = BUTTON_HEIGHT;
  1044. area.m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  1045. const bool enabled = _enabled && isEnabled(m_areaId);
  1046. const bool over = enabled && inRect(xx, yy, width, height);
  1047. const bool res = buttonLogic(id, over);
  1048. const int32_t cx = xx + BUTTON_HEIGHT / 2 - CHECK_SIZE / 2;
  1049. const int32_t cy = yy + BUTTON_HEIGHT / 2 - CHECK_SIZE / 2;
  1050. drawRoundedRect( (float)cx - 3
  1051. , (float)cy - 3
  1052. , (float)CHECK_SIZE + 6
  1053. , (float)CHECK_SIZE + 6
  1054. , 4
  1055. , imguiRGBA(128, 128, 128, isActive(id) ? 196 : 96)
  1056. );
  1057. if (_checked)
  1058. {
  1059. if (enabled)
  1060. {
  1061. drawRoundedRect( (float)cx
  1062. , (float)cy
  1063. , (float)CHECK_SIZE
  1064. , (float)CHECK_SIZE
  1065. , (float)CHECK_SIZE / 2 - 1
  1066. , imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200)
  1067. );
  1068. }
  1069. else
  1070. {
  1071. drawRoundedRect( (float)cx
  1072. , (float)cy
  1073. , (float)CHECK_SIZE
  1074. , (float)CHECK_SIZE
  1075. , (float)CHECK_SIZE / 2 - 1
  1076. , imguiRGBA(128, 128, 128, 200)
  1077. );
  1078. }
  1079. }
  1080. if (enabled)
  1081. {
  1082. drawText(xx + BUTTON_HEIGHT
  1083. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1084. , ImguiTextAlign::Left
  1085. , _text
  1086. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  1087. );
  1088. }
  1089. else
  1090. {
  1091. drawText(xx + BUTTON_HEIGHT
  1092. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1093. , ImguiTextAlign::Left
  1094. , _text
  1095. , imguiRGBA(128, 128, 128, 200)
  1096. );
  1097. }
  1098. return res;
  1099. }
  1100. void input(const char* _label, char* _str, uint32_t _len, bool _enabled, ImguiAlign::Enum _align, int32_t _r)
  1101. {
  1102. const uint32_t id = getId();
  1103. Area& area = getCurrentArea();
  1104. const int32_t yy = area.m_widgetY;
  1105. area.m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  1106. int32_t xx;
  1107. int32_t width;
  1108. if (ImguiAlign::Left == _align)
  1109. {
  1110. xx = area.m_contentX + SCROLL_AREA_PADDING;
  1111. width = area.m_widgetW;
  1112. }
  1113. else if (ImguiAlign::LeftIndented == _align
  1114. || ImguiAlign::Right == _align)
  1115. {
  1116. xx = area.m_widgetX;
  1117. width = area.m_widgetW-1; //TODO: -1 !
  1118. }
  1119. else //if (ImguiAlign::Center == _align
  1120. //|| ImguiAlign::CenterIndented == _align).
  1121. {
  1122. xx = area.m_widgetX;
  1123. width = area.m_widgetW - (area.m_widgetX-area.m_scissorX);
  1124. }
  1125. const bool drawLabel = (NULL != _label && _label[0] != '\0');
  1126. if (drawLabel)
  1127. {
  1128. drawText(xx
  1129. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1130. , ImguiTextAlign::Left
  1131. , _label
  1132. , imguiRGBA(255, 255, 255, 200)
  1133. );
  1134. }
  1135. // Handle input.
  1136. if (isActiveInputField(id) )
  1137. {
  1138. const size_t cursor = size_t(strlen(_str));
  1139. if (m_char == 0x08) //backspace
  1140. {
  1141. _str[cursor-1] = '\0';
  1142. }
  1143. else if (m_char == 0x0d || m_char == 0x1b) //enter or escape
  1144. {
  1145. clearActiveInputField();
  1146. }
  1147. else if (cursor < _len-1
  1148. && 0 != m_char)
  1149. {
  1150. _str[cursor] = m_char;
  1151. _str[cursor+1] = '\0';
  1152. }
  1153. }
  1154. // Draw input area.
  1155. const int32_t height = BUTTON_HEIGHT;
  1156. if (drawLabel)
  1157. {
  1158. uint32_t numVertices = 0; //unused
  1159. const int32_t labelWidth = int32_t(getTextLength(m_fonts[m_currentFontIdx].m_cdata, _label, numVertices));
  1160. xx += (labelWidth + 6);
  1161. width -= (labelWidth + 6);
  1162. }
  1163. const bool enabled = _enabled && isEnabled(m_areaId);
  1164. const bool over = enabled && inRect(xx, yy, width, height);
  1165. inputLogic(id, over);
  1166. drawRoundedRect( (float)xx
  1167. , (float)yy
  1168. , (float)width
  1169. , (float)height
  1170. , (float)_r
  1171. , isActiveInputField(id)?imguiRGBA(255,196,0,255):imguiRGBA(128,128,128,96)
  1172. );
  1173. if (isActiveInputField(id) )
  1174. {
  1175. drawText(xx + 6
  1176. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1177. , ImguiTextAlign::Left
  1178. , _str
  1179. , imguiRGBA(0, 0, 0, 255)
  1180. );
  1181. }
  1182. else
  1183. {
  1184. drawText(xx + 6
  1185. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1186. , ImguiTextAlign::Left
  1187. , _str
  1188. , isHot(id) ? imguiRGBA(255,196,0,255) : imguiRGBA(255,255,255,255)
  1189. );
  1190. }
  1191. }
  1192. uint8_t tabs(uint8_t _selected, bool _enabled, ImguiAlign::Enum _align, int32_t _height, int32_t _r, uint8_t _nTabs, uint8_t _nEnabled, va_list _argList)
  1193. {
  1194. const char* titles[16];
  1195. bool tabEnabled[16];
  1196. const uint8_t tabCount = IMGUI_MIN(_nTabs, 16);
  1197. const uint8_t enabledCount = IMGUI_MIN(_nEnabled, 16);
  1198. // Read titles.
  1199. {
  1200. uint8_t ii = 0;
  1201. for (; ii < tabCount; ++ii)
  1202. {
  1203. const char* str = va_arg(_argList, const char*);
  1204. titles[ii] = str;
  1205. }
  1206. for (; ii < _nTabs; ++ii)
  1207. {
  1208. const char* str = va_arg(_argList, const char*);
  1209. BX_UNUSED(str);
  1210. }
  1211. }
  1212. // Read enabled tabs.
  1213. {
  1214. uint8_t ii = 0;
  1215. for (; ii < enabledCount; ++ii)
  1216. {
  1217. const bool enabled = (0 != va_arg(_argList, int) );
  1218. tabEnabled[ii] = enabled;
  1219. }
  1220. for (; ii < _nEnabled; ++ii)
  1221. {
  1222. const int enabled = va_arg(_argList, int);
  1223. BX_UNUSED(enabled);
  1224. }
  1225. for (; ii < _nTabs; ++ii)
  1226. {
  1227. tabEnabled[ii] = true;
  1228. }
  1229. }
  1230. Area& area = getCurrentArea();
  1231. const int32_t yy = area.m_widgetY;
  1232. area.m_widgetY += _height + DEFAULT_SPACING;
  1233. int32_t xx;
  1234. int32_t width;
  1235. if (ImguiAlign::Left == _align)
  1236. {
  1237. xx = area.m_contentX + SCROLL_AREA_PADDING;
  1238. width = area.m_widgetW;
  1239. }
  1240. else if (ImguiAlign::LeftIndented == _align
  1241. || ImguiAlign::Right == _align)
  1242. {
  1243. xx = area.m_widgetX;
  1244. width = area.m_widgetW-1; //TODO: -1 !
  1245. }
  1246. else //if (ImguiAlign::Center == _align
  1247. //|| ImguiAlign::CenterIndented == _align).
  1248. {
  1249. xx = area.m_widgetX;
  1250. width = area.m_widgetW - (area.m_widgetX-area.m_scissorX);
  1251. }
  1252. uint8_t selected = _selected;
  1253. const int32_t tabWidth = width / tabCount;
  1254. const int32_t tabWidthHalf = width / (tabCount*2);
  1255. const int32_t textY = yy + _height/2 + int32_t(m_fonts[m_currentFontIdx].m_size)/2 - 2;
  1256. drawRoundedRect( (float)xx
  1257. , (float)yy
  1258. , (float)width
  1259. , (float)_height
  1260. , (float)_r
  1261. , _enabled?imguiRGBA(128,128,128,96):imguiRGBA(128,128,128,64)
  1262. );
  1263. for (uint8_t ii = 0; ii < tabCount; ++ii)
  1264. {
  1265. const uint32_t id = getId();
  1266. int32_t buttonX = xx + ii*width/tabCount;
  1267. int32_t textX = buttonX + tabWidthHalf;
  1268. const bool enabled = _enabled && tabEnabled[ii] && isEnabled(m_areaId);
  1269. const bool over = enabled && inRect(buttonX, yy, tabWidth, _height);
  1270. const bool res = buttonLogic(id, over);
  1271. if (res)
  1272. {
  1273. selected = ii;
  1274. }
  1275. uint32_t textColor;
  1276. if (ii == selected)
  1277. {
  1278. textColor = enabled?imguiRGBA(0,0,0,255):imguiRGBA(255,255,255,100);
  1279. drawRoundedRect( (float)buttonX
  1280. , (float)yy
  1281. , (float)tabWidth
  1282. , (float)_height
  1283. , (float)_r
  1284. , enabled?imguiRGBA(255,196,0,200):imguiRGBA(128,128,128,32)
  1285. );
  1286. }
  1287. else
  1288. {
  1289. textColor = isHot(id) ? imguiRGBA(255, 196, 0, enabled?255:100) : imguiRGBA(255, 255, 255, enabled?200:100);
  1290. }
  1291. drawText(textX
  1292. , textY
  1293. , ImguiTextAlign::Center
  1294. , titles[ii]
  1295. , textColor
  1296. );
  1297. }
  1298. return selected;
  1299. }
  1300. bool image(bgfx::TextureHandle _image, float _lod, int32_t _width, int32_t _height, ImguiAlign::Enum _align, bool _enabled, bool _originBottomLeft)
  1301. {
  1302. const uint32_t id = getId();
  1303. Area& area = getCurrentArea();
  1304. int32_t xx;
  1305. if (ImguiAlign::Left == _align)
  1306. {
  1307. xx = area.m_contentX + SCROLL_AREA_PADDING;
  1308. }
  1309. else if (ImguiAlign::LeftIndented == _align)
  1310. {
  1311. xx = area.m_widgetX;
  1312. }
  1313. else if (ImguiAlign::Center == _align)
  1314. {
  1315. xx = area.m_contentX + (area.m_widgetW-_width)/2;
  1316. }
  1317. else if (ImguiAlign::CenterIndented == _align)
  1318. {
  1319. xx = (area.m_widgetX + area.m_widgetW + area.m_contentX - _width)/2;
  1320. }
  1321. else //if (ImguiAlign::Right == _align).
  1322. {
  1323. xx = area.m_contentX + area.m_widgetW - _width;
  1324. }
  1325. const int32_t yy = area.m_widgetY;
  1326. area.m_widgetY += _height + DEFAULT_SPACING;
  1327. const bool enabled = _enabled && isEnabled(m_areaId);
  1328. const bool over = enabled && inRect(xx, yy, _width, _height);
  1329. const bool res = buttonLogic(id, over);
  1330. const float lodEnabled[4] = { _lod, float(enabled), 0.0f, 0.0f };
  1331. screenQuad(xx, yy, _width, _height, _originBottomLeft);
  1332. bgfx::setUniform(u_imageLodEnabled, lodEnabled);
  1333. bgfx::setTexture(0, s_texColor, bgfx::isValid(_image) ? _image : m_missingTexture);
  1334. bgfx::setState(BGFX_STATE_RGB_WRITE
  1335. |BGFX_STATE_ALPHA_WRITE
  1336. |BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  1337. );
  1338. bgfx::setProgram(m_imageProgram);
  1339. setCurrentScissor();
  1340. bgfx::submit(m_view);
  1341. return res;
  1342. }
  1343. bool image(bgfx::TextureHandle _image, float _lod, float _width, float _aspect, ImguiAlign::Enum _align, bool _enabled, bool _originBottomLeft)
  1344. {
  1345. const float width = _width*float(getCurrentArea().m_widgetW);
  1346. const float height = width/_aspect;
  1347. return image(_image, _lod, int32_t(width), int32_t(height), _align, _enabled, _originBottomLeft);
  1348. }
  1349. bool imageChannel(bgfx::TextureHandle _image, uint8_t _channel, float _lod, int32_t _width, int32_t _height, ImguiAlign::Enum _align, bool _enabled)
  1350. {
  1351. BX_CHECK(_channel < 4, "Channel param must be from 0 to 3!");
  1352. const uint32_t id = getId();
  1353. Area& area = getCurrentArea();
  1354. int32_t xx;
  1355. if (ImguiAlign::Left == _align)
  1356. {
  1357. xx = area.m_contentX + SCROLL_AREA_PADDING;
  1358. }
  1359. else if (ImguiAlign::LeftIndented == _align)
  1360. {
  1361. xx = area.m_widgetX;
  1362. }
  1363. else if (ImguiAlign::Center == _align)
  1364. {
  1365. xx = area.m_contentX + (area.m_widgetW-_width)/2;
  1366. }
  1367. else if (ImguiAlign::CenterIndented == _align)
  1368. {
  1369. xx = (area.m_widgetX + area.m_widgetW + area.m_contentX - _width)/2;
  1370. }
  1371. else //if (ImguiAlign::Right == _align).
  1372. {
  1373. xx = area.m_contentX + area.m_widgetW - _width;
  1374. }
  1375. const int32_t yy = area.m_widgetY;
  1376. area.m_widgetY += _height + DEFAULT_SPACING;
  1377. const bool enabled = _enabled && isEnabled(m_areaId);
  1378. const bool over = enabled && inRect(xx, yy, _width, _height);
  1379. const bool res = buttonLogic(id, over);
  1380. screenQuad(xx, yy, _width, _height);
  1381. const float lodEnabled[4] = { _lod, float(enabled), 0.0f, 0.0f };
  1382. bgfx::setUniform(u_imageLodEnabled, lodEnabled);
  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
  1388. |BGFX_STATE_ALPHA_WRITE
  1389. |BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  1390. );
  1391. bgfx::setProgram(m_imageSwizzProgram);
  1392. setCurrentScissor();
  1393. bgfx::submit(m_view);
  1394. return res;
  1395. }
  1396. bool imageChannel(bgfx::TextureHandle _image, uint8_t _channel, float _lod, float _width, float _aspect, ImguiAlign::Enum _align, bool _enabled)
  1397. {
  1398. const float width = _width*float(getCurrentArea().m_widgetW);
  1399. const float height = width/_aspect;
  1400. return imageChannel(_image, _channel, _lod, int32_t(width), int32_t(height), _align, _enabled);
  1401. }
  1402. bool cubeMap(bgfx::TextureHandle _cubemap, float _lod, bool _cross, ImguiAlign::Enum _align, bool _enabled)
  1403. {
  1404. const uint32_t numVertices = 14;
  1405. const uint32_t numIndices = 36;
  1406. if (bgfx::checkAvailTransientBuffers(numVertices, PosNormalVertex::ms_decl, numIndices) )
  1407. {
  1408. bgfx::TransientVertexBuffer tvb;
  1409. bgfx::allocTransientVertexBuffer(&tvb, numVertices, PosNormalVertex::ms_decl);
  1410. bgfx::TransientIndexBuffer tib;
  1411. bgfx::allocTransientIndexBuffer(&tib, numIndices);
  1412. PosNormalVertex* vertex = (PosNormalVertex*)tvb.data;
  1413. uint16_t* indices = (uint16_t*)tib.data;
  1414. if (_cross)
  1415. {
  1416. vertex->set(0.0f, 0.5f, 0.0f, -1.0f, 1.0f, -1.0f); ++vertex;
  1417. vertex->set(0.0f, 1.0f, 0.0f, -1.0f, -1.0f, -1.0f); ++vertex;
  1418. vertex->set(0.5f, 0.0f, 0.0f, -1.0f, 1.0f, -1.0f); ++vertex;
  1419. vertex->set(0.5f, 0.5f, 0.0f, -1.0f, 1.0f, 1.0f); ++vertex;
  1420. vertex->set(0.5f, 1.0f, 0.0f, -1.0f, -1.0f, 1.0f); ++vertex;
  1421. vertex->set(0.5f, 1.5f, 0.0f, -1.0f, -1.0f, -1.0f); ++vertex;
  1422. vertex->set(1.0f, 0.0f, 0.0f, 1.0f, 1.0f, -1.0f); ++vertex;
  1423. vertex->set(1.0f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f); ++vertex;
  1424. vertex->set(1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f); ++vertex;
  1425. vertex->set(1.0f, 1.5f, 0.0f, 1.0f, -1.0f, -1.0f); ++vertex;
  1426. vertex->set(1.5f, 0.5f, 0.0f, 1.0f, 1.0f, -1.0f); ++vertex;
  1427. vertex->set(1.5f, 1.0f, 0.0f, 1.0f, -1.0f, -1.0f); ++vertex;
  1428. vertex->set(2.0f, 0.5f, 0.0f, -1.0f, 1.0f, -1.0f); ++vertex;
  1429. vertex->set(2.0f, 1.0f, 0.0f, -1.0f, -1.0f, -1.0f); ++vertex;
  1430. indices += addQuad(indices, 0, 3, 4, 1);
  1431. indices += addQuad(indices, 2, 6, 7, 3);
  1432. indices += addQuad(indices, 3, 7, 8, 4);
  1433. indices += addQuad(indices, 4, 8, 9, 5);
  1434. indices += addQuad(indices, 7, 10, 11, 8);
  1435. indices += addQuad(indices, 10, 12, 13, 11);
  1436. }
  1437. else
  1438. {
  1439. vertex->set(0.0f, 0.25f, 0.0f, -1.0f, 1.0f, -1.0f); ++vertex;
  1440. vertex->set(0.0f, 0.75f, 0.0f, -1.0f, -1.0f, -1.0f); ++vertex;
  1441. vertex->set(0.5f, 0.00f, 0.0f, -1.0f, 1.0f, 1.0f); ++vertex;
  1442. vertex->set(0.5f, 0.50f, 0.0f, -1.0f, -1.0f, 1.0f); ++vertex;
  1443. vertex->set(0.5f, 1.00f, 0.0f, 1.0f, -1.0f, -1.0f); ++vertex;
  1444. vertex->set(1.0f, 0.25f, 0.0f, 1.0f, 1.0f, 1.0f); ++vertex;
  1445. vertex->set(1.0f, 0.75f, 0.0f, 1.0f, -1.0f, 1.0f); ++vertex;
  1446. vertex->set(1.0f, 0.25f, 0.0f, 1.0f, 1.0f, 1.0f); ++vertex;
  1447. vertex->set(1.0f, 0.75f, 0.0f, 1.0f, -1.0f, 1.0f); ++vertex;
  1448. vertex->set(1.5f, 0.00f, 0.0f, -1.0f, 1.0f, 1.0f); ++vertex;
  1449. vertex->set(1.5f, 0.50f, 0.0f, 1.0f, 1.0f, -1.0f); ++vertex;
  1450. vertex->set(1.5f, 1.00f, 0.0f, 1.0f, -1.0f, -1.0f); ++vertex;
  1451. vertex->set(2.0f, 0.25f, 0.0f, -1.0f, 1.0f, -1.0f); ++vertex;
  1452. vertex->set(2.0f, 0.75f, 0.0f, -1.0f, -1.0f, -1.0f); ++vertex;
  1453. indices += addQuad(indices, 0, 2, 3, 1);
  1454. indices += addQuad(indices, 1, 3, 6, 4);
  1455. indices += addQuad(indices, 2, 5, 6, 3);
  1456. indices += addQuad(indices, 7, 9, 12, 10);
  1457. indices += addQuad(indices, 7, 10, 11, 8);
  1458. indices += addQuad(indices, 10, 12, 13, 11);
  1459. }
  1460. const uint32_t id = getId();
  1461. Area& area = getCurrentArea();
  1462. int32_t xx;
  1463. int32_t width;
  1464. if (ImguiAlign::Left == _align)
  1465. {
  1466. xx = area.m_contentX + SCROLL_AREA_PADDING;
  1467. width = area.m_widgetW;
  1468. }
  1469. else if (ImguiAlign::LeftIndented == _align
  1470. || ImguiAlign::Right == _align)
  1471. {
  1472. xx = area.m_widgetX;
  1473. width = area.m_widgetW;
  1474. }
  1475. else //if (ImguiAlign::Center == _align
  1476. //|| ImguiAlign::CenterIndented == _align).
  1477. {
  1478. xx = area.m_widgetX;
  1479. width = area.m_widgetW - (area.m_widgetX-area.m_scissorX);
  1480. }
  1481. const uint32_t height = _cross ? (width*3)/4 : (width/2);
  1482. const int32_t yy = area.m_widgetY;
  1483. area.m_widgetY += height + DEFAULT_SPACING;
  1484. const bool enabled = _enabled && isEnabled(m_areaId);
  1485. const bool over = enabled && inRect(xx, yy, width, height);
  1486. const bool res = buttonLogic(id, over);
  1487. const float scale = float(width/2)+0.25f;
  1488. float mtx[16];
  1489. bx::mtxSRT(mtx, scale, scale, 1.0f, 0.0f, 0.0f, 0.0f, float(xx), float(yy), 0.0f);
  1490. const float lodEnabled[4] = { _lod, float(enabled), 0.0f, 0.0f };
  1491. bgfx::setUniform(u_imageLodEnabled, lodEnabled);
  1492. bgfx::setTransform(mtx);
  1493. bgfx::setTexture(0, s_texColor, _cubemap);
  1494. bgfx::setProgram(m_cubeMapProgram);
  1495. bgfx::setVertexBuffer(&tvb);
  1496. bgfx::setIndexBuffer(&tib);
  1497. bgfx::setState(BGFX_STATE_RGB_WRITE
  1498. |BGFX_STATE_ALPHA_WRITE
  1499. |BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  1500. );
  1501. setCurrentScissor();
  1502. bgfx::submit(m_view);
  1503. return res;
  1504. }
  1505. return false;
  1506. }
  1507. bool collapse(const char* _text, const char* _subtext, bool _checked, bool _enabled)
  1508. {
  1509. const uint32_t id = getId();
  1510. Area& area = getCurrentArea();
  1511. const int32_t xx = area.m_widgetX;
  1512. const int32_t yy = area.m_widgetY;
  1513. const int32_t width = area.m_widgetW;
  1514. const int32_t height = BUTTON_HEIGHT;
  1515. area.m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  1516. const int32_t cx = xx + BUTTON_HEIGHT/2 - CHECK_SIZE/2;
  1517. const int32_t cy = yy + BUTTON_HEIGHT/2 - CHECK_SIZE/2 + DEFAULT_SPACING/2;
  1518. const int32_t textY = yy + BUTTON_HEIGHT/2 + TEXT_HEIGHT/2 + DEFAULT_SPACING/2;
  1519. const bool enabled = _enabled && isEnabled(m_areaId);
  1520. const bool over = enabled && inRect(xx, yy, width, height);
  1521. const bool res = buttonLogic(id, over);
  1522. if (_checked)
  1523. {
  1524. drawTriangle(cx
  1525. , cy
  1526. , CHECK_SIZE
  1527. , CHECK_SIZE
  1528. , TriangleOrientation::Up
  1529. , imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200)
  1530. );
  1531. }
  1532. else
  1533. {
  1534. drawTriangle(cx-1 // With -1 is more aesthetically pleasing.
  1535. , cy
  1536. , CHECK_SIZE
  1537. , CHECK_SIZE
  1538. , TriangleOrientation::Right
  1539. , imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200)
  1540. );
  1541. }
  1542. if (enabled)
  1543. {
  1544. drawText(xx + BUTTON_HEIGHT
  1545. , textY
  1546. , ImguiTextAlign::Left
  1547. , _text
  1548. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  1549. );
  1550. }
  1551. else
  1552. {
  1553. drawText(xx + BUTTON_HEIGHT
  1554. , textY
  1555. , ImguiTextAlign::Left
  1556. , _text
  1557. , imguiRGBA(128, 128, 128, 200)
  1558. );
  1559. }
  1560. if (_subtext)
  1561. {
  1562. drawText(xx + width - BUTTON_HEIGHT / 2
  1563. , textY
  1564. , ImguiTextAlign::Right
  1565. , _subtext
  1566. , imguiRGBA(255, 255, 255, 128)
  1567. );
  1568. }
  1569. return res;
  1570. }
  1571. bool borderButton(ImguiBorder::Enum _border, bool _checked, bool _enabled)
  1572. {
  1573. // Since border button isn't part of any area, just use this custom/unique areaId.
  1574. const uint16_t areaId = UINT16_MAX-1;
  1575. const uint32_t id = (areaId << 16) | m_widgetId++;
  1576. updatePresence(id);
  1577. const int32_t triSize = 12;
  1578. const int32_t borderSize = 15;
  1579. int32_t xx;
  1580. int32_t yy;
  1581. int32_t width;
  1582. int32_t height;
  1583. int32_t triX;
  1584. int32_t triY;
  1585. TriangleOrientation::Enum orientation;
  1586. if (ImguiBorder::Left == _border)
  1587. {
  1588. xx = -borderSize;
  1589. yy = 0;
  1590. width = 2*borderSize;
  1591. height = m_viewHeight;
  1592. triX = 0;
  1593. triY = (m_viewHeight-triSize)/2;
  1594. orientation = _checked ? TriangleOrientation::Left : TriangleOrientation::Right;
  1595. }
  1596. else if (ImguiBorder::Right == _border)
  1597. {
  1598. xx = m_viewWidth - borderSize;
  1599. yy = 0;
  1600. width = 2*borderSize;
  1601. height = m_viewHeight;
  1602. triX = m_viewWidth - triSize - 2;
  1603. triY = (m_viewHeight-width)/2;
  1604. orientation = _checked ? TriangleOrientation::Right : TriangleOrientation::Left;
  1605. }
  1606. else if (ImguiBorder::Top == _border)
  1607. {
  1608. xx = 0;
  1609. yy = -borderSize;
  1610. width = m_viewWidth;
  1611. height = 2*borderSize;
  1612. triX = (m_viewWidth-triSize)/2;
  1613. triY = 0;
  1614. orientation = _checked ? TriangleOrientation::Up : TriangleOrientation::Down;
  1615. }
  1616. else //if (ImguiBorder::Bottom == _border).
  1617. {
  1618. xx = 0;
  1619. yy = m_viewHeight - borderSize;
  1620. width = m_viewWidth;
  1621. height = 2*borderSize;
  1622. triX = (m_viewWidth-triSize)/2;
  1623. triY = m_viewHeight-triSize;
  1624. orientation = _checked ? TriangleOrientation::Down : TriangleOrientation::Up;
  1625. }
  1626. const bool over = _enabled && inRect(xx, yy, width, height, false);
  1627. const bool res = buttonLogic(id, over);
  1628. drawRect( (float)xx
  1629. , (float)yy
  1630. , (float)width
  1631. , (float)height
  1632. , isActive(id) ? imguiRGBA(23, 23, 23, 192) : imguiRGBA(0, 0, 0, 222)
  1633. );
  1634. drawTriangle( triX
  1635. , triY
  1636. , triSize
  1637. , triSize
  1638. , orientation
  1639. , isHot(id) ? imguiRGBA(255, 196, 0, 222) : imguiRGBA(255, 255, 255, 192)
  1640. );
  1641. return res;
  1642. }
  1643. void labelVargs(const char* _format, va_list _argList, uint32_t _rgba)
  1644. {
  1645. char temp[8192];
  1646. char* out = temp;
  1647. int32_t len = bx::vsnprintf(out, sizeof(temp), _format, _argList);
  1648. if ( (int32_t)sizeof(temp) < len)
  1649. {
  1650. out = (char*)alloca(len+1);
  1651. len = bx::vsnprintf(out, len, _format, _argList);
  1652. }
  1653. out[len] = '\0';
  1654. Area& area = getCurrentArea();
  1655. const int32_t xx = area.m_widgetX;
  1656. const int32_t yy = area.m_widgetY;
  1657. area.m_widgetY += BUTTON_HEIGHT;
  1658. drawText(xx
  1659. , yy + BUTTON_HEIGHT/2 + TEXT_HEIGHT/2
  1660. , ImguiTextAlign::Left
  1661. , out
  1662. , _rgba
  1663. );
  1664. }
  1665. void value(const char* _text)
  1666. {
  1667. Area& area = getCurrentArea();
  1668. const int32_t xx = area.m_widgetX;
  1669. const int32_t yy = area.m_widgetY;
  1670. const int32_t ww = area.m_widgetW;
  1671. area.m_widgetY += BUTTON_HEIGHT;
  1672. drawText(xx + ww - BUTTON_HEIGHT / 2
  1673. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1674. , ImguiTextAlign::Right
  1675. , _text
  1676. , imguiRGBA(255, 255, 255, 200)
  1677. );
  1678. }
  1679. bool slider(const char* _text, float& _val, float _vmin, float _vmax, float _vinc, bool _enabled, ImguiAlign::Enum _align)
  1680. {
  1681. const uint32_t id = getId();
  1682. Area& area = getCurrentArea();
  1683. const int32_t yy = area.m_widgetY;
  1684. const int32_t height = SLIDER_HEIGHT;
  1685. area.m_widgetY += SLIDER_HEIGHT + DEFAULT_SPACING;
  1686. int32_t xx;
  1687. int32_t width;
  1688. if (ImguiAlign::Left == _align)
  1689. {
  1690. xx = area.m_contentX + SCROLL_AREA_PADDING;
  1691. width = area.m_widgetW;
  1692. }
  1693. else if (ImguiAlign::LeftIndented == _align
  1694. || ImguiAlign::Right == _align)
  1695. {
  1696. xx = area.m_widgetX;
  1697. width = area.m_widgetW;
  1698. }
  1699. else //if (ImguiAlign::Center == _align
  1700. //|| ImguiAlign::CenterIndented == _align).
  1701. {
  1702. xx = area.m_widgetX;
  1703. width = area.m_widgetW - (area.m_widgetX-area.m_scissorX);
  1704. }
  1705. drawRoundedRect( (float)xx, (float)yy, (float)width, (float)height, 4.0f, imguiRGBA(0, 0, 0, 128) );
  1706. const int32_t range = width - SLIDER_MARKER_WIDTH;
  1707. float uu = bx::fsaturate( (_val - _vmin) / (_vmax - _vmin) );
  1708. int32_t m = (int)(uu * range);
  1709. bool valChanged = false;
  1710. const bool enabled = _enabled && isEnabled(m_areaId);
  1711. const bool over = enabled && inRect(xx + m, yy, SLIDER_MARKER_WIDTH, SLIDER_HEIGHT);
  1712. const bool res = buttonLogic(id, over);
  1713. if (isActive(id) )
  1714. {
  1715. if (m_wentActive)
  1716. {
  1717. m_dragX = m_mx;
  1718. m_dragOrig = uu;
  1719. }
  1720. if (m_dragX != m_mx)
  1721. {
  1722. uu = bx::fsaturate(m_dragOrig + (float)(m_mx - m_dragX) / (float)range);
  1723. _val = _vmin + uu * (_vmax - _vmin);
  1724. _val = floorf(_val / _vinc + 0.5f) * _vinc; // Snap to vinc
  1725. m = (int)(uu * range);
  1726. valChanged = true;
  1727. }
  1728. }
  1729. if (isActive(id) )
  1730. {
  1731. drawRoundedRect( (float)(xx + m)
  1732. , (float)yy
  1733. , (float)SLIDER_MARKER_WIDTH
  1734. , (float)SLIDER_HEIGHT
  1735. , 4.0f
  1736. , imguiRGBA(255, 255, 255, 255)
  1737. );
  1738. }
  1739. else
  1740. {
  1741. drawRoundedRect( (float)(xx + m)
  1742. , (float)yy
  1743. , (float)SLIDER_MARKER_WIDTH
  1744. , (float)SLIDER_HEIGHT
  1745. , 4.0f
  1746. , isHot(id) ? imguiRGBA(255, 196, 0, 128) : imguiRGBA(255, 255, 255, 64)
  1747. );
  1748. }
  1749. // TODO: fix this, take a look at 'nicenum'.
  1750. int32_t digits = (int)(ceilf(log10f(_vinc) ) );
  1751. char fmt[16];
  1752. bx::snprintf(fmt, 16, "%%.%df", digits >= 0 ? 0 : -digits);
  1753. char msg[128];
  1754. bx::snprintf(msg, 128, fmt, _val);
  1755. if (enabled)
  1756. {
  1757. drawText(xx + SLIDER_HEIGHT / 2
  1758. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  1759. , ImguiTextAlign::Left
  1760. , _text
  1761. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  1762. );
  1763. drawText(xx + width - SLIDER_HEIGHT / 2
  1764. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  1765. , ImguiTextAlign::Right
  1766. , msg
  1767. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  1768. );
  1769. }
  1770. else
  1771. {
  1772. drawText(xx + SLIDER_HEIGHT / 2
  1773. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  1774. , ImguiTextAlign::Left
  1775. , _text
  1776. , imguiRGBA(128, 128, 128, 200)
  1777. );
  1778. drawText(xx + width - SLIDER_HEIGHT / 2
  1779. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  1780. , ImguiTextAlign::Right
  1781. , msg
  1782. , imguiRGBA(128, 128, 128, 200)
  1783. );
  1784. }
  1785. return res || valChanged;
  1786. }
  1787. void indent(uint16_t _width)
  1788. {
  1789. Area& area = getCurrentArea();
  1790. area.m_widgetX += _width;
  1791. area.m_widgetW -= _width;
  1792. }
  1793. void unindent(uint16_t _width)
  1794. {
  1795. Area& area = getCurrentArea();
  1796. area.m_widgetX -= _width;
  1797. area.m_widgetW += _width;
  1798. }
  1799. void separator(uint16_t _height)
  1800. {
  1801. Area& area = getCurrentArea();
  1802. area.m_widgetY += _height;
  1803. }
  1804. void separatorLine(uint16_t _height)
  1805. {
  1806. Area& area = getCurrentArea();
  1807. const int32_t rectWidth = area.m_widgetW;
  1808. const int32_t rectHeight = 1;
  1809. const int32_t xx = area.m_widgetX;
  1810. const int32_t yy = area.m_widgetY + _height/2 - rectHeight;
  1811. area.m_widgetY += _height;
  1812. drawRect( (float)xx
  1813. , (float)yy
  1814. , (float)rectWidth
  1815. , (float)rectHeight
  1816. , imguiRGBA(255, 255, 255, 32)
  1817. );
  1818. }
  1819. void drawPolygon(const float* _coords, uint32_t _numCoords, float _r, uint32_t _abgr)
  1820. {
  1821. _numCoords = bx::uint32_min(_numCoords, MAX_TEMP_COORDS);
  1822. for (uint32_t ii = 0, jj = _numCoords - 1; ii < _numCoords; jj = ii++)
  1823. {
  1824. const float* v0 = &_coords[jj * 2];
  1825. const float* v1 = &_coords[ii * 2];
  1826. float dx = v1[0] - v0[0];
  1827. float dy = v1[1] - v0[1];
  1828. float d = sqrtf(dx * dx + dy * dy);
  1829. if (d > 0)
  1830. {
  1831. d = 1.0f / d;
  1832. dx *= d;
  1833. dy *= d;
  1834. }
  1835. m_tempNormals[jj * 2 + 0] = dy;
  1836. m_tempNormals[jj * 2 + 1] = -dx;
  1837. }
  1838. for (uint32_t ii = 0, jj = _numCoords - 1; ii < _numCoords; jj = ii++)
  1839. {
  1840. float dlx0 = m_tempNormals[jj * 2 + 0];
  1841. float dly0 = m_tempNormals[jj * 2 + 1];
  1842. float dlx1 = m_tempNormals[ii * 2 + 0];
  1843. float dly1 = m_tempNormals[ii * 2 + 1];
  1844. float dmx = (dlx0 + dlx1) * 0.5f;
  1845. float dmy = (dly0 + dly1) * 0.5f;
  1846. float dmr2 = dmx * dmx + dmy * dmy;
  1847. if (dmr2 > 0.000001f)
  1848. {
  1849. float scale = 1.0f / dmr2;
  1850. if (scale > 10.0f)
  1851. {
  1852. scale = 10.0f;
  1853. }
  1854. dmx *= scale;
  1855. dmy *= scale;
  1856. }
  1857. m_tempCoords[ii * 2 + 0] = _coords[ii * 2 + 0] + dmx * _r;
  1858. m_tempCoords[ii * 2 + 1] = _coords[ii * 2 + 1] + dmy * _r;
  1859. }
  1860. uint32_t numVertices = _numCoords*6 + (_numCoords-2)*3;
  1861. if (bgfx::checkAvailTransientVertexBuffer(numVertices, PosColorVertex::ms_decl) )
  1862. {
  1863. bgfx::TransientVertexBuffer tvb;
  1864. bgfx::allocTransientVertexBuffer(&tvb, numVertices, PosColorVertex::ms_decl);
  1865. uint32_t trans = _abgr&0xffffff;
  1866. PosColorVertex* vertex = (PosColorVertex*)tvb.data;
  1867. for (uint32_t ii = 0, jj = _numCoords-1; ii < _numCoords; jj = ii++)
  1868. {
  1869. vertex->m_x = _coords[ii*2+0];
  1870. vertex->m_y = _coords[ii*2+1];
  1871. vertex->m_abgr = _abgr;
  1872. ++vertex;
  1873. vertex->m_x = _coords[jj*2+0];
  1874. vertex->m_y = _coords[jj*2+1];
  1875. vertex->m_abgr = _abgr;
  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[jj*2+0];
  1882. vertex->m_y = m_tempCoords[jj*2+1];
  1883. vertex->m_abgr = trans;
  1884. ++vertex;
  1885. vertex->m_x = m_tempCoords[ii*2+0];
  1886. vertex->m_y = m_tempCoords[ii*2+1];
  1887. vertex->m_abgr = trans;
  1888. ++vertex;
  1889. vertex->m_x = _coords[ii*2+0];
  1890. vertex->m_y = _coords[ii*2+1];
  1891. vertex->m_abgr = _abgr;
  1892. ++vertex;
  1893. }
  1894. for (uint32_t ii = 2; ii < _numCoords; ++ii)
  1895. {
  1896. vertex->m_x = _coords[0];
  1897. vertex->m_y = _coords[1];
  1898. vertex->m_abgr = _abgr;
  1899. ++vertex;
  1900. vertex->m_x = _coords[(ii-1)*2+0];
  1901. vertex->m_y = _coords[(ii-1)*2+1];
  1902. vertex->m_abgr = _abgr;
  1903. ++vertex;
  1904. vertex->m_x = _coords[ii*2+0];
  1905. vertex->m_y = _coords[ii*2+1];
  1906. vertex->m_abgr = _abgr;
  1907. ++vertex;
  1908. }
  1909. bgfx::setVertexBuffer(&tvb);
  1910. bgfx::setState(0
  1911. | BGFX_STATE_RGB_WRITE
  1912. | BGFX_STATE_ALPHA_WRITE
  1913. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  1914. );
  1915. bgfx::setProgram(m_colorProgram);
  1916. setCurrentScissor();
  1917. bgfx::submit(m_view);
  1918. }
  1919. }
  1920. void drawRect(float _x, float _y, float _w, float _h, uint32_t _argb, float _fth = 1.0f)
  1921. {
  1922. float verts[4 * 2] =
  1923. {
  1924. _x + 0.5f, _y + 0.5f,
  1925. _x + _w - 0.5f, _y + 0.5f,
  1926. _x + _w - 0.5f, _y + _h - 0.5f,
  1927. _x + 0.5f, _y + _h - 0.5f,
  1928. };
  1929. drawPolygon(verts, 4, _fth, _argb);
  1930. }
  1931. void drawRoundedRect(float _x, float _y, float _w, float _h, float _r, uint32_t _argb, float _fth = 1.0f)
  1932. {
  1933. if (0.0f == _r)
  1934. {
  1935. return drawRect(_x, _y, _w, _h, _argb, _fth);
  1936. }
  1937. const uint32_t num = NUM_CIRCLE_VERTS / 4;
  1938. const float* cverts = m_circleVerts;
  1939. float verts[(num + 1) * 4 * 2];
  1940. float* vv = verts;
  1941. for (uint32_t ii = 0; ii <= num; ++ii)
  1942. {
  1943. *vv++ = _x + _w - _r + cverts[ii * 2] * _r;
  1944. *vv++ = _y + _h - _r + cverts[ii * 2 + 1] * _r;
  1945. }
  1946. for (uint32_t ii = num; ii <= num * 2; ++ii)
  1947. {
  1948. *vv++ = _x + _r + cverts[ii * 2] * _r;
  1949. *vv++ = _y + _h - _r + cverts[ii * 2 + 1] * _r;
  1950. }
  1951. for (uint32_t ii = num * 2; ii <= num * 3; ++ii)
  1952. {
  1953. *vv++ = _x + _r + cverts[ii * 2] * _r;
  1954. *vv++ = _y + _r + cverts[ii * 2 + 1] * _r;
  1955. }
  1956. for (uint32_t ii = num * 3; ii < num * 4; ++ii)
  1957. {
  1958. *vv++ = _x + _w - _r + cverts[ii * 2] * _r;
  1959. *vv++ = _y + _r + cverts[ii * 2 + 1] * _r;
  1960. }
  1961. *vv++ = _x + _w - _r + cverts[0] * _r;
  1962. *vv++ = _y + _r + cverts[1] * _r;
  1963. drawPolygon(verts, (num + 1) * 4, _fth, _argb);
  1964. }
  1965. void drawLine(float _x0, float _y0, float _x1, float _y1, float _r, uint32_t _abgr, float _fth = 1.0f)
  1966. {
  1967. float dx = _x1 - _x0;
  1968. float dy = _y1 - _y0;
  1969. float d = sqrtf(dx * dx + dy * dy);
  1970. if (d > 0.0001f)
  1971. {
  1972. d = 1.0f / d;
  1973. dx *= d;
  1974. dy *= d;
  1975. }
  1976. float nx = dy;
  1977. float ny = -dx;
  1978. float verts[4 * 2];
  1979. _r -= _fth;
  1980. _r *= 0.5f;
  1981. if (_r < 0.01f)
  1982. {
  1983. _r = 0.01f;
  1984. }
  1985. dx *= _r;
  1986. dy *= _r;
  1987. nx *= _r;
  1988. ny *= _r;
  1989. verts[0] = _x0 - dx - nx;
  1990. verts[1] = _y0 - dy - ny;
  1991. verts[2] = _x0 - dx + nx;
  1992. verts[3] = _y0 - dy + ny;
  1993. verts[4] = _x1 + dx + nx;
  1994. verts[5] = _y1 + dy + ny;
  1995. verts[6] = _x1 + dx - nx;
  1996. verts[7] = _y1 + dy - ny;
  1997. drawPolygon(verts, 4, _fth, _abgr);
  1998. }
  1999. struct TriangleOrientation
  2000. {
  2001. enum Enum
  2002. {
  2003. Left,
  2004. Right,
  2005. Up,
  2006. Down,
  2007. };
  2008. };
  2009. void drawTriangle(int32_t _x, int32_t _y, int32_t _width, int32_t _height, TriangleOrientation::Enum _orientation, uint32_t _abgr)
  2010. {
  2011. if (TriangleOrientation::Left == _orientation)
  2012. {
  2013. const float verts[3 * 2] =
  2014. {
  2015. (float)_x + 0.5f + (float)_width * 1.0f, (float)_y + 0.5f,
  2016. (float)_x + 0.5f, (float)_y + 0.5f + (float)_height / 2.0f - 0.5f,
  2017. (float)_x + 0.5f + (float)_width * 1.0f, (float)_y + 0.5f + (float)_height - 1.0f,
  2018. };
  2019. drawPolygon(verts, 3, 1.0f, _abgr);
  2020. }
  2021. else if (TriangleOrientation::Right == _orientation)
  2022. {
  2023. const float verts[3 * 2] =
  2024. {
  2025. (float)_x + 0.5f, (float)_y + 0.5f,
  2026. (float)_x + 0.5f + (float)_width * 1.0f, (float)_y + 0.5f + (float)_height / 2.0f - 0.5f,
  2027. (float)_x + 0.5f, (float)_y + 0.5f + (float)_height - 1.0f,
  2028. };
  2029. drawPolygon(verts, 3, 1.0f, _abgr);
  2030. }
  2031. else if (TriangleOrientation::Up == _orientation)
  2032. {
  2033. const float verts[3 * 2] =
  2034. {
  2035. (float)_x + 0.5f, (float)_y + 0.5f + (float)_height - 1.0f,
  2036. (float)_x + 0.5f + (float)_width / 2.0f - 0.5f, (float)_y + 0.5f,
  2037. (float)_x + 0.5f + (float)_width - 1.0f, (float)_y + 0.5f + (float)_height - 1.0f,
  2038. };
  2039. drawPolygon(verts, 3, 1.0f, _abgr);
  2040. }
  2041. else //if (TriangleOrientation::Down == _orientation).
  2042. {
  2043. const float verts[3 * 2] =
  2044. {
  2045. (float)_x + 0.5f, (float)_y + 0.5f,
  2046. (float)_x + 0.5f + (float)_width / 2.0f - 0.5f, (float)_y + 0.5f + (float)_height - 1.0f,
  2047. (float)_x + 0.5f + (float)_width - 1.0f, (float)_y + 0.5f,
  2048. };
  2049. drawPolygon(verts, 3, 1.0f, _abgr);
  2050. }
  2051. }
  2052. #if !USE_NANOVG_FONT
  2053. void getBakedQuad(stbtt_bakedchar* _chardata, int32_t char_index, float* _xpos, float* _ypos, stbtt_aligned_quad* _quad)
  2054. {
  2055. stbtt_bakedchar* b = _chardata + char_index;
  2056. int32_t round_x = STBTT_ifloor(*_xpos + b->xoff);
  2057. int32_t round_y = STBTT_ifloor(*_ypos + b->yoff);
  2058. _quad->x0 = (float)round_x;
  2059. _quad->y0 = (float)round_y;
  2060. _quad->x1 = (float)round_x + b->x1 - b->x0;
  2061. _quad->y1 = (float)round_y + b->y1 - b->y0;
  2062. _quad->s0 = (b->x0 + m_halfTexel) * m_invTextureWidth;
  2063. _quad->t0 = (b->y0 + m_halfTexel) * m_invTextureWidth;
  2064. _quad->s1 = (b->x1 + m_halfTexel) * m_invTextureHeight;
  2065. _quad->t1 = (b->y1 + m_halfTexel) * m_invTextureHeight;
  2066. *_xpos += b->xadvance;
  2067. }
  2068. #endif // !USE_NANOVG_FONT
  2069. void drawText(int32_t _x, int32_t _y, ImguiTextAlign::Enum _align, const char* _text, uint32_t _abgr)
  2070. {
  2071. drawText( (float)_x, (float)_y, _text, _align, _abgr);
  2072. }
  2073. void drawText(float _x, float _y, const char* _text, ImguiTextAlign::Enum _align, uint32_t _abgr)
  2074. {
  2075. if (NULL == _text
  2076. || '\0' == _text[0])
  2077. {
  2078. return;
  2079. }
  2080. #if USE_NANOVG_FONT
  2081. static uint32_t textAlign[ImguiTextAlign::Count] =
  2082. {
  2083. NVG_ALIGN_LEFT,
  2084. NVG_ALIGN_CENTER,
  2085. NVG_ALIGN_RIGHT,
  2086. };
  2087. nvgTextAlign(m_nvg, textAlign[_align]);
  2088. nvgFontBlur(m_nvg, 0.0f);
  2089. nvgFillColor(m_nvg, nvgRGBAu(_abgr) );
  2090. nvgText(m_nvg, _x, _y, _text, NULL);
  2091. #else
  2092. uint32_t numVertices = 0;
  2093. if (_align == ImguiTextAlign::Center)
  2094. {
  2095. _x -= getTextLength(m_fonts[m_currentFontIdx].m_cdata, _text, numVertices) / 2;
  2096. }
  2097. else if (_align == ImguiTextAlign::Right)
  2098. {
  2099. _x -= getTextLength(m_fonts[m_currentFontIdx].m_cdata, _text, numVertices);
  2100. }
  2101. else // just count vertices
  2102. {
  2103. getTextLength(m_fonts[m_currentFontIdx].m_cdata, _text, numVertices);
  2104. }
  2105. if (bgfx::checkAvailTransientVertexBuffer(numVertices, PosColorUvVertex::ms_decl) )
  2106. {
  2107. bgfx::TransientVertexBuffer tvb;
  2108. bgfx::allocTransientVertexBuffer(&tvb, numVertices, PosColorUvVertex::ms_decl);
  2109. PosColorUvVertex* vertex = (PosColorUvVertex*)tvb.data;
  2110. const float ox = _x;
  2111. while (*_text)
  2112. {
  2113. int32_t ch = (uint8_t)*_text;
  2114. if (ch == '\t')
  2115. {
  2116. for (int32_t i = 0; i < 4; ++i)
  2117. {
  2118. if (_x < s_tabStops[i] + ox)
  2119. {
  2120. _x = s_tabStops[i] + ox;
  2121. break;
  2122. }
  2123. }
  2124. }
  2125. else if (ch >= ' '
  2126. && ch < 128)
  2127. {
  2128. stbtt_aligned_quad quad;
  2129. getBakedQuad(m_fonts[m_currentFontIdx].m_cdata, ch - 32, &_x, &_y, &quad);
  2130. vertex->m_x = quad.x0;
  2131. vertex->m_y = quad.y0;
  2132. vertex->m_u = quad.s0;
  2133. vertex->m_v = quad.t0;
  2134. vertex->m_abgr = _abgr;
  2135. ++vertex;
  2136. vertex->m_x = quad.x1;
  2137. vertex->m_y = quad.y1;
  2138. vertex->m_u = quad.s1;
  2139. vertex->m_v = quad.t1;
  2140. vertex->m_abgr = _abgr;
  2141. ++vertex;
  2142. vertex->m_x = quad.x1;
  2143. vertex->m_y = quad.y0;
  2144. vertex->m_u = quad.s1;
  2145. vertex->m_v = quad.t0;
  2146. vertex->m_abgr = _abgr;
  2147. ++vertex;
  2148. vertex->m_x = quad.x0;
  2149. vertex->m_y = quad.y0;
  2150. vertex->m_u = quad.s0;
  2151. vertex->m_v = quad.t0;
  2152. vertex->m_abgr = _abgr;
  2153. ++vertex;
  2154. vertex->m_x = quad.x0;
  2155. vertex->m_y = quad.y1;
  2156. vertex->m_u = quad.s0;
  2157. vertex->m_v = quad.t1;
  2158. vertex->m_abgr = _abgr;
  2159. ++vertex;
  2160. vertex->m_x = quad.x1;
  2161. vertex->m_y = quad.y1;
  2162. vertex->m_u = quad.s1;
  2163. vertex->m_v = quad.t1;
  2164. vertex->m_abgr = _abgr;
  2165. ++vertex;
  2166. }
  2167. ++_text;
  2168. }
  2169. bgfx::setTexture(0, s_texColor, m_fonts[m_currentFontIdx].m_texture);
  2170. bgfx::setVertexBuffer(&tvb);
  2171. bgfx::setState(0
  2172. | BGFX_STATE_RGB_WRITE
  2173. | BGFX_STATE_ALPHA_WRITE
  2174. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  2175. );
  2176. bgfx::setProgram(m_textureProgram);
  2177. setCurrentScissor();
  2178. bgfx::submit(m_view);
  2179. }
  2180. #endif // USE_NANOVG_FONT
  2181. }
  2182. void screenQuad(int32_t _x, int32_t _y, int32_t _width, uint32_t _height, bool _originBottomLeft = false)
  2183. {
  2184. if (bgfx::checkAvailTransientVertexBuffer(6, PosUvVertex::ms_decl) )
  2185. {
  2186. bgfx::TransientVertexBuffer vb;
  2187. bgfx::allocTransientVertexBuffer(&vb, 6, PosUvVertex::ms_decl);
  2188. PosUvVertex* vertex = (PosUvVertex*)vb.data;
  2189. const float widthf = float(_width);
  2190. const float heightf = float(_height);
  2191. const float minx = float(_x);
  2192. const float miny = float(_y);
  2193. const float maxx = minx+widthf;
  2194. const float maxy = miny+heightf;
  2195. const float texelHalfW = m_halfTexel/widthf;
  2196. const float texelHalfH = m_halfTexel/heightf;
  2197. const float minu = texelHalfW;
  2198. const float maxu = 1.0f - texelHalfW;
  2199. const float minv = _originBottomLeft ? texelHalfH+1.0f : texelHalfH ;
  2200. const float maxv = _originBottomLeft ? texelHalfH : texelHalfH+1.0f;
  2201. vertex[0].m_x = minx;
  2202. vertex[0].m_y = miny;
  2203. vertex[0].m_u = minu;
  2204. vertex[0].m_v = minv;
  2205. vertex[1].m_x = maxx;
  2206. vertex[1].m_y = miny;
  2207. vertex[1].m_u = maxu;
  2208. vertex[1].m_v = minv;
  2209. vertex[2].m_x = maxx;
  2210. vertex[2].m_y = maxy;
  2211. vertex[2].m_u = maxu;
  2212. vertex[2].m_v = maxv;
  2213. vertex[3].m_x = maxx;
  2214. vertex[3].m_y = maxy;
  2215. vertex[3].m_u = maxu;
  2216. vertex[3].m_v = maxv;
  2217. vertex[4].m_x = minx;
  2218. vertex[4].m_y = maxy;
  2219. vertex[4].m_u = minu;
  2220. vertex[4].m_v = maxv;
  2221. vertex[5].m_x = minx;
  2222. vertex[5].m_y = miny;
  2223. vertex[5].m_u = minu;
  2224. vertex[5].m_v = minv;
  2225. bgfx::setVertexBuffer(&vb);
  2226. }
  2227. }
  2228. void colorWheelWidget(float _rgb[3], bool _respectIndentation, float _size, bool _enabled)
  2229. {
  2230. const uint32_t wheelId = getId();
  2231. const uint32_t triangleId = getId();
  2232. Area& area = getCurrentArea();
  2233. const float areaX = float(_respectIndentation ? area.m_widgetX : area.m_contentX);
  2234. const float areaW = float(_respectIndentation ? area.m_widgetW : area.m_contentWidth);
  2235. const float width = areaW*_size;
  2236. const float xx = areaX + areaW*0.5f;
  2237. const float yy = float(area.m_widgetY) + width*0.5f;
  2238. const float center[2] = { xx, yy };
  2239. area.m_widgetY += int32_t(width) + DEFAULT_SPACING;
  2240. const float ro = width*0.5f - 5.0f; // radiusOuter.
  2241. const float rd = _size*25.0f; // radiusDelta.
  2242. const float ri = ro - rd; // radiusInner.
  2243. const float aeps = 0.5f / ro; // Half a pixel arc length in radians (2pi cancels out).
  2244. const float cmx = float(m_mx) - center[0];
  2245. const float cmy = float(m_my) - center[1];
  2246. const float aa[2] = { ri - 6.0f, 0.0f }; // Hue point.
  2247. const float bb[2] = { cosf(-120.0f/180.0f*NVG_PI) * aa[0], sinf(-120.0f/180.0f*NVG_PI) * aa[0] }; // Black point.
  2248. const float cc[2] = { cosf( 120.0f/180.0f*NVG_PI) * aa[0], sinf( 120.0f/180.0f*NVG_PI) * aa[0] }; // White point.
  2249. const float ca[2] = { aa[0] - cc[0], aa[1] - cc[1] };
  2250. const float lenCa = sqrtf(ca[0]*ca[0]+ca[1]*ca[1]);
  2251. const float invLenCa = 1.0f/lenCa;
  2252. const float dirCa[2] = { ca[0]*invLenCa, ca[1]*invLenCa };
  2253. float sel[2];
  2254. float hsv[3];
  2255. bx::rgbToHsv(hsv, _rgb);
  2256. const bool enabled = _enabled && isEnabled(m_areaId);
  2257. if (enabled)
  2258. {
  2259. if (m_leftPressed)
  2260. {
  2261. const float len = sqrtf(cmx*cmx + cmy*cmy);
  2262. if (len > ri)
  2263. {
  2264. if (len < ro)
  2265. {
  2266. setActive(wheelId);
  2267. }
  2268. }
  2269. else
  2270. {
  2271. setActive(triangleId);
  2272. }
  2273. }
  2274. if (m_leftReleased
  2275. && (isActive(wheelId) || isActive(triangleId) ) )
  2276. {
  2277. clearActive();
  2278. }
  2279. // Set hue.
  2280. if (m_left
  2281. && isActive(wheelId) )
  2282. {
  2283. hsv[0] = atan2f(cmy, cmx)/NVG_PI*0.5f;
  2284. if (hsv[0] < 0.0f)
  2285. {
  2286. hsv[0]+=1.0f;
  2287. }
  2288. }
  2289. }
  2290. if (enabled
  2291. && m_left
  2292. && isActive(triangleId) )
  2293. {
  2294. float an = -hsv[0]*NVG_PI*2.0f;
  2295. float tmx = (cmx*cosf(an)-cmy*sinf(an) );
  2296. float tmy = (cmx*sinf(an)+cmy*cosf(an) );
  2297. if (pointInTriangle(tmx, tmy, aa[0], aa[1], bb[0], bb[1], cc[0], cc[1]) )
  2298. {
  2299. sel[0] = tmx;
  2300. sel[1] = tmy;
  2301. }
  2302. else
  2303. {
  2304. closestPointOnTriangle(sel[0], sel[1], tmx, tmy, aa[0], aa[1], bb[0], bb[1], cc[0], cc[1]);
  2305. }
  2306. }
  2307. else
  2308. {
  2309. /*
  2310. * bb (black)
  2311. * /\
  2312. * / \
  2313. * / \
  2314. * / \
  2315. * / \
  2316. * / .sel \
  2317. * / \
  2318. * cc(white)/____.ss_______\aa (hue)
  2319. */
  2320. const float ss[2] =
  2321. {
  2322. cc[0] + dirCa[0]*lenCa*hsv[1],
  2323. cc[1] + dirCa[1]*lenCa*hsv[1],
  2324. };
  2325. const float sb[2] = { bb[0]-ss[0], bb[1]-ss[1] };
  2326. const float lenSb = sqrtf(sb[0]*sb[0]+sb[1]*sb[1]);
  2327. const float invLenSb = 1.0f/lenSb;
  2328. const float dirSb[2] = { sb[0]*invLenSb, sb[1]*invLenSb };
  2329. sel[0] = cc[0] + dirCa[0]*lenCa*hsv[1] + dirSb[0]*lenSb*(1.0f - hsv[2]);
  2330. sel[1] = cc[1] + dirCa[1]*lenCa*hsv[1] + dirSb[1]*lenSb*(1.0f - hsv[2]);
  2331. }
  2332. float uu, vv, ww;
  2333. barycentric(uu, vv, ww
  2334. , aa[0], aa[1]
  2335. , bb[0], bb[1]
  2336. , cc[0], cc[1]
  2337. , sel[0], sel[1]
  2338. );
  2339. const float val = bx::fclamp(1.0f-vv, 0.0001f, 1.0f);
  2340. const float sat = bx::fclamp(uu/val, 0.0001f, 1.0f);
  2341. const float out[3] = { hsv[0], sat, val };
  2342. bx::hsvToRgb(_rgb, out);
  2343. // Draw widget.
  2344. nvgSave(m_nvg);
  2345. {
  2346. float saturation;
  2347. uint8_t alpha0;
  2348. uint8_t alpha1;
  2349. if (enabled)
  2350. {
  2351. saturation = 1.0f;
  2352. alpha0 = 255;
  2353. alpha1 = 192;
  2354. }
  2355. else
  2356. {
  2357. saturation = 0.0f;
  2358. alpha0 = 10;
  2359. alpha1 = 10;
  2360. }
  2361. // Circle.
  2362. for (uint8_t ii = 0; ii < 6; ii++)
  2363. {
  2364. const float a0 = float(ii)/6.0f * 2.0f*NVG_PI - aeps;
  2365. const float a1 = float(ii+1.0f)/6.0f * 2.0f*NVG_PI + aeps;
  2366. nvgBeginPath(m_nvg);
  2367. nvgArc(m_nvg, center[0], center[1], ri, a0, a1, NVG_CW);
  2368. nvgArc(m_nvg, center[0], center[1], ro, a1, a0, NVG_CCW);
  2369. nvgClosePath(m_nvg);
  2370. const float ax = center[0] + cosf(a0) * (ri+ro)*0.5f;
  2371. const float ay = center[1] + sinf(a0) * (ri+ro)*0.5f;
  2372. const float bx = center[0] + cosf(a1) * (ri+ro)*0.5f;
  2373. const float by = center[1] + sinf(a1) * (ri+ro)*0.5f;
  2374. NVGpaint paint = nvgLinearGradient(m_nvg
  2375. , ax, ay
  2376. , bx, by
  2377. , nvgHSLA(a0/NVG_PI*0.5f,saturation,0.55f,alpha0)
  2378. , nvgHSLA(a1/NVG_PI*0.5f,saturation,0.55f,alpha0)
  2379. );
  2380. nvgFillPaint(m_nvg, paint);
  2381. nvgFill(m_nvg);
  2382. }
  2383. // Circle stroke.
  2384. nvgBeginPath(m_nvg);
  2385. nvgCircle(m_nvg, center[0], center[1], ri-0.5f);
  2386. nvgCircle(m_nvg, center[0], center[1], ro+0.5f);
  2387. nvgStrokeColor(m_nvg, nvgRGBA(0,0,0,64) );
  2388. nvgStrokeWidth(m_nvg, 1.0f);
  2389. nvgStroke(m_nvg);
  2390. nvgSave(m_nvg);
  2391. {
  2392. // Hue selector.
  2393. nvgTranslate(m_nvg, center[0], center[1]);
  2394. nvgRotate(m_nvg, hsv[0]*NVG_PI*2.0f);
  2395. nvgStrokeWidth(m_nvg, 2.0f);
  2396. nvgBeginPath(m_nvg);
  2397. nvgRect(m_nvg, ri-1.0f,-3.0f,rd+2.0f,6.0f);
  2398. nvgStrokeColor(m_nvg, nvgRGBA(255,255,255,alpha1) );
  2399. nvgStroke(m_nvg);
  2400. // Hue selector drop shadow.
  2401. 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) );
  2402. nvgBeginPath(m_nvg);
  2403. nvgRect(m_nvg, ri-2.0f-10.0f,-4.0f-10.0f,ro-ri+4.0f+20.0f,8.0f+20.0f);
  2404. nvgRect(m_nvg, ri-2.0f,-4.0f,ro-ri+4.0f,8.0f);
  2405. nvgPathWinding(m_nvg, NVG_HOLE);
  2406. nvgFillPaint(m_nvg, paint);
  2407. nvgFill(m_nvg);
  2408. // Center triangle stroke.
  2409. nvgBeginPath(m_nvg);
  2410. nvgMoveTo(m_nvg, aa[0], aa[1]);
  2411. nvgLineTo(m_nvg, bb[0], bb[1]);
  2412. nvgLineTo(m_nvg, cc[0], cc[1]);
  2413. nvgClosePath(m_nvg);
  2414. nvgStrokeColor(m_nvg, nvgRGBA(0,0,0,64) );
  2415. nvgStroke(m_nvg);
  2416. // Center triangle fill.
  2417. paint = nvgLinearGradient(m_nvg, aa[0], aa[1], bb[0], bb[1], nvgHSL(hsv[0],saturation,0.5f), nvgRGBA(0,0,0,alpha0) );
  2418. nvgFillPaint(m_nvg, paint);
  2419. nvgFill(m_nvg);
  2420. 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) );
  2421. nvgFillPaint(m_nvg, paint);
  2422. nvgFill(m_nvg);
  2423. // Color selector.
  2424. nvgStrokeWidth(m_nvg, 2.0f);
  2425. nvgBeginPath(m_nvg);
  2426. nvgCircle(m_nvg, sel[0], sel[1], 5);
  2427. nvgStrokeColor(m_nvg, nvgRGBA(255,255,255,alpha1) );
  2428. nvgStroke(m_nvg);
  2429. // Color selector stroke.
  2430. paint = nvgRadialGradient(m_nvg, sel[0], sel[1], 7.0f, 9.0f, nvgRGBA(0,0,0,64), nvgRGBA(0,0,0,0) );
  2431. nvgBeginPath(m_nvg);
  2432. nvgRect(m_nvg, sel[0]-20.0f, sel[1]-20.0f, 40.0f, 40.0f);
  2433. nvgCircle(m_nvg, sel[0], sel[1], 7.0f);
  2434. nvgPathWinding(m_nvg, NVG_HOLE);
  2435. nvgFillPaint(m_nvg, paint);
  2436. nvgFill(m_nvg);
  2437. }
  2438. nvgRestore(m_nvg);
  2439. }
  2440. nvgRestore(m_nvg);
  2441. }
  2442. struct Area
  2443. {
  2444. int32_t m_x;
  2445. int32_t m_y;
  2446. int32_t m_width;
  2447. int32_t m_height;
  2448. int16_t m_contentX;
  2449. int16_t m_contentY;
  2450. int16_t m_contentWidth;
  2451. int16_t m_contentHeight;
  2452. int16_t m_scissorX;
  2453. int16_t m_scissorY;
  2454. int16_t m_scissorHeight;
  2455. int16_t m_scissorWidth;
  2456. int32_t m_widgetX;
  2457. int32_t m_widgetY;
  2458. int32_t m_widgetW;
  2459. int32_t* m_scrollVal;
  2460. uint32_t m_scrollId;
  2461. bool m_inside;
  2462. bool m_scissorEnabled;
  2463. };
  2464. inline Area& getCurrentArea()
  2465. {
  2466. return m_areas[m_areaId];
  2467. }
  2468. inline void setCurrentScissor()
  2469. {
  2470. const Area& area = getCurrentArea();
  2471. if (area.m_scissorEnabled)
  2472. {
  2473. bgfx::setScissor(uint16_t(IMGUI_MAX(0, area.m_scissorX) )
  2474. , uint16_t(IMGUI_MAX(0, area.m_scissorY-1) )
  2475. , area.m_scissorWidth
  2476. , area.m_scissorHeight+1
  2477. );
  2478. }
  2479. else
  2480. {
  2481. bgfx::setScissor(UINT16_MAX);
  2482. }
  2483. }
  2484. template <typename Ty, uint16_t Max=64>
  2485. struct IdStack
  2486. {
  2487. IdStack()
  2488. {
  2489. reset();
  2490. }
  2491. void reset()
  2492. {
  2493. m_current = 0;
  2494. m_idGen = 0;
  2495. m_ids[0] = 0;
  2496. }
  2497. void next()
  2498. {
  2499. BX_CHECK(Max > (m_current+1), "Param out of bounds!");
  2500. m_ids[++m_current] = ++m_idGen;
  2501. }
  2502. void previous()
  2503. {
  2504. m_current = m_current > 0 ? m_current-1 : 0;
  2505. }
  2506. Ty current() const
  2507. {
  2508. BX_CHECK(Max > (m_current), "Param out of bounds!");
  2509. return m_ids[m_current];
  2510. }
  2511. operator Ty() const
  2512. {
  2513. BX_CHECK(Max > (m_current), "Param out of bounds!");
  2514. return m_ids[m_current];
  2515. }
  2516. private:
  2517. uint16_t m_current;
  2518. Ty m_idGen;
  2519. Ty m_ids[Max];
  2520. };
  2521. int32_t m_mx;
  2522. int32_t m_my;
  2523. int32_t m_scroll;
  2524. uint32_t m_active;
  2525. uint32_t m_hot;
  2526. uint32_t m_hotToBe;
  2527. char m_char;
  2528. char m_lastChar;
  2529. uint32_t m_inputField;
  2530. int32_t m_dragX;
  2531. int32_t m_dragY;
  2532. float m_dragOrig;
  2533. bool m_left;
  2534. bool m_leftPressed;
  2535. bool m_leftReleased;
  2536. bool m_isHot;
  2537. bool m_wentActive;
  2538. bool m_insideArea;
  2539. bool m_isActivePresent;
  2540. bool m_checkActivePresence;
  2541. IdStack<uint16_t> m_areaId;
  2542. uint16_t m_widgetId;
  2543. uint64_t m_enabledAreaIds;
  2544. Area m_areas[64];
  2545. float m_tempCoords[MAX_TEMP_COORDS * 2];
  2546. float m_tempNormals[MAX_TEMP_COORDS * 2];
  2547. float m_circleVerts[NUM_CIRCLE_VERTS * 2];
  2548. uint16_t m_textureWidth;
  2549. uint16_t m_textureHeight;
  2550. float m_invTextureWidth;
  2551. float m_invTextureHeight;
  2552. float m_halfTexel;
  2553. NVGcontext* m_nvg;
  2554. uint8_t m_view;
  2555. uint16_t m_viewWidth;
  2556. uint16_t m_viewHeight;
  2557. #if !USE_NANOVG_FONT
  2558. struct Font
  2559. {
  2560. stbtt_bakedchar m_cdata[96]; // ASCII 32..126 is 95 glyphs
  2561. bgfx::TextureHandle m_texture;
  2562. float m_size;
  2563. };
  2564. uint16_t m_currentFontIdx;
  2565. bx::HandleAllocT<IMGUI_CONFIG_MAX_FONTS> m_fontHandle;
  2566. Font m_fonts[IMGUI_CONFIG_MAX_FONTS];
  2567. #endif // !USE_NANOVG_FONT
  2568. bgfx::UniformHandle u_imageLodEnabled;
  2569. bgfx::UniformHandle u_imageSwizzle;
  2570. bgfx::UniformHandle s_texColor;
  2571. bgfx::ProgramHandle m_colorProgram;
  2572. bgfx::ProgramHandle m_textureProgram;
  2573. bgfx::ProgramHandle m_cubeMapProgram;
  2574. bgfx::ProgramHandle m_imageProgram;
  2575. bgfx::ProgramHandle m_imageSwizzProgram;
  2576. bgfx::TextureHandle m_missingTexture;
  2577. };
  2578. static Imgui s_imgui;
  2579. ImguiFontHandle imguiCreate(const void* _data, float _fontSize)
  2580. {
  2581. return s_imgui.create(_data, _fontSize);
  2582. }
  2583. void imguiDestroy()
  2584. {
  2585. s_imgui.destroy();
  2586. }
  2587. ImguiFontHandle imguiCreateFont(const void* _data, float _fontSize)
  2588. {
  2589. return s_imgui.createFont(_data, _fontSize);
  2590. }
  2591. void imguiSetFont(ImguiFontHandle _handle)
  2592. {
  2593. s_imgui.setFont(_handle);
  2594. }
  2595. ImguiFontHandle imguiGetCurrentFont()
  2596. {
  2597. const ImguiFontHandle handle = { s_imgui.m_currentFontIdx };
  2598. return handle;
  2599. }
  2600. 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)
  2601. {
  2602. s_imgui.beginFrame(_mx, _my, _button, _scroll, _width, _height, _inputChar, _view);
  2603. }
  2604. void imguiEndFrame()
  2605. {
  2606. s_imgui.endFrame();
  2607. }
  2608. void imguiDrawText(int32_t _x, int32_t _y, ImguiTextAlign::Enum _align, const char* _text, uint32_t _argb)
  2609. {
  2610. s_imgui.drawText(_x, _y, _align, _text, _argb);
  2611. }
  2612. void imguiDrawLine(float _x0, float _y0, float _x1, float _y1, float _r, uint32_t _argb)
  2613. {
  2614. s_imgui.drawLine(_x0, _y0, _x1, _y1, _r, _argb);
  2615. }
  2616. void imguiDrawRoundedRect(float _x, float _y, float _width, float _height, float _r, uint32_t _argb)
  2617. {
  2618. s_imgui.drawRoundedRect(_x, _y, _width, _height, _r, _argb);
  2619. }
  2620. void imguiDrawRect(float _x, float _y, float _width, float _height, uint32_t _argb)
  2621. {
  2622. s_imgui.drawRect(_x, _y, _width, _height, _argb);
  2623. }
  2624. bool imguiBorderButton(ImguiBorder::Enum _border, bool _checked, bool _enabled)
  2625. {
  2626. return s_imgui.borderButton(_border, _checked, _enabled);
  2627. }
  2628. bool imguiBeginArea(const char* _name, int _x, int _y, int _width, int _height, bool _enabled, int32_t _r)
  2629. {
  2630. return s_imgui.beginArea(_name, _x, _y, _width, _height, _enabled, _r);
  2631. }
  2632. void imguiEndArea()
  2633. {
  2634. return s_imgui.endArea();
  2635. }
  2636. bool imguiBeginScroll(int32_t _height, int32_t* _scroll, bool _enabled)
  2637. {
  2638. return s_imgui.beginScroll(_height, _scroll, _enabled);
  2639. }
  2640. void imguiEndScroll(int32_t _r)
  2641. {
  2642. s_imgui.endScroll(_r);
  2643. }
  2644. 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)
  2645. {
  2646. const bool result = s_imgui.beginArea(_name, _x, _y, _width, _height, _enabled, _r);
  2647. const bool hasTitle = (NULL != _name && '\0' != _name[0]);
  2648. const int32_t margins = int32_t(hasTitle)*(AREA_HEADER+2*SCROLL_AREA_PADDING-1);
  2649. s_imgui.beginScroll(_height - margins, _scroll, _enabled);
  2650. return result;
  2651. }
  2652. void imguiEndScrollArea(int32_t _r)
  2653. {
  2654. s_imgui.endScroll(_r);
  2655. s_imgui.endArea();
  2656. }
  2657. void imguiIndent(uint16_t _width)
  2658. {
  2659. s_imgui.indent(_width);
  2660. }
  2661. void imguiUnindent(uint16_t _width)
  2662. {
  2663. s_imgui.unindent(_width);
  2664. }
  2665. void imguiSeparator(uint16_t _height)
  2666. {
  2667. s_imgui.separator(_height);
  2668. }
  2669. void imguiSeparatorLine(uint16_t _height)
  2670. {
  2671. s_imgui.separatorLine(_height);
  2672. }
  2673. int32_t imguiGetWidgetX()
  2674. {
  2675. return s_imgui.getCurrentArea().m_widgetX;
  2676. }
  2677. int32_t imguiGetWidgetY()
  2678. {
  2679. return s_imgui.getCurrentArea().m_widgetY;
  2680. }
  2681. void imguiSetCurrentScissor()
  2682. {
  2683. return s_imgui.setCurrentScissor();
  2684. }
  2685. bool imguiButton(const char* _text, bool _enabled, ImguiAlign::Enum _align, uint32_t _rgb0, int32_t _r)
  2686. {
  2687. return s_imgui.button(_text, _enabled, _align, _rgb0, _r);
  2688. }
  2689. bool imguiItem(const char* _text, bool _enabled)
  2690. {
  2691. return s_imgui.item(_text, _enabled);
  2692. }
  2693. bool imguiCheck(const char* _text, bool _checked, bool _enabled)
  2694. {
  2695. return s_imgui.check(_text, _checked, _enabled);
  2696. }
  2697. void imguiBool(const char* _text, bool& _flag, bool _enabled)
  2698. {
  2699. if (imguiCheck(_text, _flag, _enabled) )
  2700. {
  2701. _flag = !_flag;
  2702. }
  2703. }
  2704. bool imguiCollapse(const char* _text, const char* _subtext, bool _checked, bool _enabled)
  2705. {
  2706. return s_imgui.collapse(_text, _subtext, _checked, _enabled);
  2707. }
  2708. void imguiLabel(const char* _format, ...)
  2709. {
  2710. va_list argList;
  2711. va_start(argList, _format);
  2712. s_imgui.labelVargs(_format, argList, imguiRGBA(255, 255, 255, 255) );
  2713. va_end(argList);
  2714. }
  2715. void imguiLabel(uint32_t _rgba, const char* _format, ...)
  2716. {
  2717. va_list argList;
  2718. va_start(argList, _format);
  2719. s_imgui.labelVargs(_format, argList, _rgba);
  2720. va_end(argList);
  2721. }
  2722. void imguiValue(const char* _text)
  2723. {
  2724. s_imgui.value(_text);
  2725. }
  2726. bool imguiSlider(const char* _text, float& _val, float _vmin, float _vmax, float _vinc, bool _enabled, ImguiAlign::Enum _align)
  2727. {
  2728. return s_imgui.slider(_text, _val, _vmin, _vmax, _vinc, _enabled, _align);
  2729. }
  2730. bool imguiSlider(const char* _text, int32_t& _val, int32_t _vmin, int32_t _vmax, bool _enabled, ImguiAlign::Enum _align)
  2731. {
  2732. float val = (float)_val;
  2733. bool result = s_imgui.slider(_text, val, (float)_vmin, (float)_vmax, 1.0f, _enabled, _align);
  2734. _val = (int32_t)val;
  2735. return result;
  2736. }
  2737. void imguiInput(const char* _label, char* _str, uint32_t _len, bool _enabled, ImguiAlign::Enum _align, int32_t _r)
  2738. {
  2739. s_imgui.input(_label, _str, _len, _enabled, _align, _r);
  2740. }
  2741. uint8_t imguiTabs(uint8_t _selected, bool _enabled, ImguiAlign::Enum _align, int32_t _height, int32_t _r, uint8_t _nTabs, uint8_t _nEnabled, ...)
  2742. {
  2743. va_list argList;
  2744. va_start(argList, _nEnabled);
  2745. const uint8_t result = s_imgui.tabs(_selected, _enabled, _align, _height, _r, _nTabs, _nEnabled, argList);
  2746. va_end(argList);
  2747. return result;
  2748. }
  2749. uint8_t imguiTabs(uint8_t _selected, bool _enabled, ImguiAlign::Enum _align, int32_t _height, int32_t _r, uint8_t _nTabs, ...)
  2750. {
  2751. va_list argList;
  2752. va_start(argList, _nTabs);
  2753. const uint8_t result = s_imgui.tabs(_selected, _enabled, _align, _height, _r, _nTabs, 0, argList);
  2754. va_end(argList);
  2755. return result;
  2756. }
  2757. uint32_t imguiChooseUseMacroInstead(uint32_t _selected, ...)
  2758. {
  2759. va_list argList;
  2760. va_start(argList, _selected);
  2761. const char* str = va_arg(argList, const char*);
  2762. for (uint32_t ii = 0; str != NULL; ++ii, str = va_arg(argList, const char*) )
  2763. {
  2764. if (imguiCheck(str, ii == _selected) )
  2765. {
  2766. _selected = ii;
  2767. }
  2768. }
  2769. va_end(argList);
  2770. return _selected;
  2771. }
  2772. void imguiColorWheel(float _rgb[3], bool _respectIndentation, float _size, bool _enabled)
  2773. {
  2774. s_imgui.colorWheelWidget(_rgb, _respectIndentation, _size, _enabled);
  2775. }
  2776. void imguiColorWheel(const char* _text, float _rgb[3], bool& _activated, float _size, bool _enabled)
  2777. {
  2778. char buf[128];
  2779. bx::snprintf(buf, sizeof(buf), "[RGB %-2.2f %-2.2f %-2.2f]"
  2780. , _rgb[0]
  2781. , _rgb[1]
  2782. , _rgb[2]
  2783. );
  2784. if (imguiCollapse(_text, buf, _activated) )
  2785. {
  2786. _activated = !_activated;
  2787. }
  2788. if (_activated)
  2789. {
  2790. imguiColorWheel(_rgb, false, _size, _enabled);
  2791. }
  2792. }
  2793. bool imguiImage(bgfx::TextureHandle _image, float _lod, int32_t _width, int32_t _height, ImguiAlign::Enum _align, bool _enabled, bool _originBottomLeft)
  2794. {
  2795. return s_imgui.image(_image, _lod, _width, _height, _align, _enabled, _originBottomLeft);
  2796. }
  2797. bool imguiImage(bgfx::TextureHandle _image, float _lod, float _width, float _aspect, ImguiAlign::Enum _align, bool _enabled, bool _originBottomLeft)
  2798. {
  2799. return s_imgui.image(_image, _lod, _width, _aspect, _align, _enabled, _originBottomLeft);
  2800. }
  2801. bool imguiImageChannel(bgfx::TextureHandle _image, uint8_t _channel, float _lod, int32_t _width, int32_t _height, ImguiAlign::Enum _align, bool _enabled)
  2802. {
  2803. return s_imgui.imageChannel(_image, _channel, _lod, _width, _height, _align, _enabled);
  2804. }
  2805. bool imguiImageChannel(bgfx::TextureHandle _image, uint8_t _channel, float _lod, float _width, float _aspect, ImguiAlign::Enum _align, bool _enabled)
  2806. {
  2807. return s_imgui.imageChannel(_image, _channel, _lod, _width, _aspect, _align, _enabled);
  2808. }
  2809. bool imguiCube(bgfx::TextureHandle _cubemap, float _lod, bool _cross, ImguiAlign::Enum _align, bool _enabled)
  2810. {
  2811. return s_imgui.cubeMap(_cubemap, _lod, _cross, _align, _enabled);
  2812. }
  2813. float imguiGetTextLength(const char* _text, ImguiFontHandle _handle)
  2814. {
  2815. #if !USE_NANOVG_FONT
  2816. uint32_t numVertices = 0; //unused
  2817. return getTextLength(s_imgui.m_fonts[_handle.idx].m_cdata, _text, numVertices);
  2818. #else
  2819. return 0.0f;
  2820. #endif
  2821. }
  2822. bool imguiMouseOverArea()
  2823. {
  2824. return s_imgui.m_insideArea;
  2825. }