imgui.cpp 84 KB

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