imgui.cpp 84 KB

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