imgui.cpp 92 KB

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