imgui.cpp 91 KB

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