imgui.cpp 90 KB

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