imgui.cpp 88 KB

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