imgui.cpp 88 KB

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