imgui.cpp 66 KB

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