imgui.cpp 84 KB

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