imgui.cpp 82 KB

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