imgui.cpp 84 KB

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