imgui.cpp 93 KB

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