imgui.cpp 75 KB

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