imgui.cpp 89 KB

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