imgui.cpp 85 KB

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