imgui.cpp 81 KB

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