imgui.cpp 81 KB

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