imgui.cpp 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148
  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 <bgfx.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 "dds_imgui_x_texture.h"
  39. #define USE_NANOVG_FONT 0
  40. #define MAX_TEMP_COORDS 100
  41. #define NUM_CIRCLE_VERTS (8 * 4)
  42. static const int32_t BUTTON_HEIGHT = 20;
  43. static const int32_t SLIDER_HEIGHT = 20;
  44. static const int32_t SLIDER_MARKER_WIDTH = 10;
  45. static const int32_t CHECK_SIZE = 8;
  46. static const int32_t DEFAULT_SPACING = 4;
  47. static const int32_t TEXT_HEIGHT = 8;
  48. static const int32_t SCROLL_AREA_PADDING = 6;
  49. static const int32_t INDENT_SIZE = 16;
  50. static const int32_t AREA_HEADER = 28;
  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 STBTT_malloc(_x, _y) imguiMalloc(_x, _y)
  62. #define STBTT_free(_x, _y) imguiFree(_x, _y)
  63. #define STB_TRUETYPE_IMPLEMENTATION
  64. #include <stb_truetype/stb_truetype.h>
  65. namespace
  66. {
  67. float sign(float px, float py, float ax, float ay, float bx, float by)
  68. {
  69. return (px - bx) * (ay - by) - (ax - bx) * (py - by);
  70. }
  71. bool pointInTriangle(float px, float py, float ax, float ay, float bx, float by, float cx, float cy)
  72. {
  73. const bool b1 = sign(px, py, ax, ay, bx, by) < 0.0f;
  74. const bool b2 = sign(px, py, bx, by, cx, cy) < 0.0f;
  75. const bool b3 = sign(px, py, cx, cy, ax, ay) < 0.0f;
  76. return ( (b1 == b2) && (b2 == b3) );
  77. }
  78. void closestPointOnLine(float& ox, float &oy, float px, float py, float ax, float ay, float bx, float by)
  79. {
  80. float dx = px - ax;
  81. float dy = py - ay;
  82. float lx = bx - ax;
  83. float ly = by - ay;
  84. float len = sqrtf(lx*lx+ly*ly);
  85. // Normalize.
  86. float invLen = 1.0f/len;
  87. lx*=invLen;
  88. ly*=invLen;
  89. float dot = (dx*lx + dy*ly);
  90. if (dot < 0.0f)
  91. {
  92. ox = ax;
  93. oy = ay;
  94. }
  95. else if (dot > len)
  96. {
  97. ox = bx;
  98. oy = by;
  99. }
  100. else
  101. {
  102. ox = ax + lx*dot;
  103. oy = ay + ly*dot;
  104. }
  105. }
  106. void closestPointOnTriangle(float& ox, float &oy, float px, float py, float ax, float ay, float bx, float by, float cx, float cy)
  107. {
  108. float abx, aby;
  109. float bcx, bcy;
  110. float cax, cay;
  111. closestPointOnLine(abx, aby, px, py, ax, ay, bx, by);
  112. closestPointOnLine(bcx, bcy, px, py, bx, by, cx, cy);
  113. closestPointOnLine(cax, cay, px, py, cx, cy, ax, ay);
  114. const float pabx = px - abx;
  115. const float paby = py - aby;
  116. const float pbcx = px - bcx;
  117. const float pbcy = py - bcy;
  118. const float pcax = px - cax;
  119. const float pcay = py - cay;
  120. const float lab = sqrtf(pabx*pabx+paby*paby);
  121. const float lbc = sqrtf(pbcx*pbcx+pbcy*pbcy);
  122. const float lca = sqrtf(pcax*pcax+pcay*pcay);
  123. const float m = bx::fmin3(lab, lbc, lca);
  124. if (m == lab)
  125. {
  126. ox = abx;
  127. oy = aby;
  128. }
  129. else if (m == lbc)
  130. {
  131. ox = bcx;
  132. oy = bcy;
  133. }
  134. else// if (m == lca).
  135. {
  136. ox = cax;
  137. oy = cay;
  138. }
  139. }
  140. inline float vec2Dot(const float* __restrict _a, const float* __restrict _b)
  141. {
  142. return _a[0]*_b[0] + _a[1]*_b[1];
  143. }
  144. void barycentric(float& _u, float& _v, float& _w
  145. , float _ax, float _ay
  146. , float _bx, float _by
  147. , float _cx, float _cy
  148. , float _px, float _py
  149. )
  150. {
  151. const float v0[2] = { _bx - _ax, _by - _ay };
  152. const float v1[2] = { _cx - _ax, _cy - _ay };
  153. const float v2[2] = { _px - _ax, _py - _ay };
  154. const float d00 = vec2Dot(v0, v0);
  155. const float d01 = vec2Dot(v0, v1);
  156. const float d11 = vec2Dot(v1, v1);
  157. const float d20 = vec2Dot(v2, v0);
  158. const float d21 = vec2Dot(v2, v1);
  159. const float denom = d00 * d11 - d01 * d01;
  160. _v = (d11 * d20 - d01 * d21) / denom;
  161. _w = (d00 * d21 - d01 * d20) / denom;
  162. _u = 1.0f - _v - _w;
  163. }
  164. struct PosColorVertex
  165. {
  166. float m_x;
  167. float m_y;
  168. uint32_t m_abgr;
  169. static void init()
  170. {
  171. ms_decl
  172. .begin()
  173. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  174. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  175. .end();
  176. }
  177. static bgfx::VertexDecl ms_decl;
  178. };
  179. bgfx::VertexDecl PosColorVertex::ms_decl;
  180. struct PosColorUvVertex
  181. {
  182. float m_x;
  183. float m_y;
  184. float m_u;
  185. float m_v;
  186. uint32_t m_abgr;
  187. static void init()
  188. {
  189. ms_decl
  190. .begin()
  191. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  192. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  193. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  194. .end();
  195. }
  196. static bgfx::VertexDecl ms_decl;
  197. };
  198. bgfx::VertexDecl PosColorUvVertex::ms_decl;
  199. struct PosUvVertex
  200. {
  201. float m_x;
  202. float m_y;
  203. float m_u;
  204. float m_v;
  205. static void init()
  206. {
  207. ms_decl
  208. .begin()
  209. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  210. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  211. .end();
  212. }
  213. static bgfx::VertexDecl ms_decl;
  214. };
  215. bgfx::VertexDecl PosUvVertex::ms_decl;
  216. } // namespace
  217. struct Imgui
  218. {
  219. Imgui()
  220. : m_mx(-1)
  221. , m_my(-1)
  222. , m_scroll(0)
  223. , m_active(0)
  224. , m_hot(0)
  225. , m_hotToBe(0)
  226. , m_dragX(0)
  227. , m_dragY(0)
  228. , m_dragOrig(0)
  229. , m_widgetX(0)
  230. , m_widgetY(0)
  231. , m_widgetW(100)
  232. , m_left(false)
  233. , m_leftPressed(false)
  234. , m_leftReleased(false)
  235. , m_isHot(false)
  236. , m_isActive(false)
  237. , m_wentActive(false)
  238. , m_insideCurrentScroll(false)
  239. , m_areaId(0)
  240. , m_widgetId(0)
  241. , m_enabledAreaIds(0)
  242. , m_scissor(UINT16_MAX)
  243. , m_scrollTop(0)
  244. , m_scrollBottom(0)
  245. , m_scrollRight(0)
  246. , m_scrollAreaTop(0)
  247. , m_scrollAreaWidth(0)
  248. , m_scrollAreaInnerWidth(0)
  249. , m_scrollAreaX(0)
  250. , m_scrollVal(NULL)
  251. , m_focusTop(0)
  252. , m_focusBottom(0)
  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. {
  261. m_invTextureWidth = 1.0f/m_textureWidth;
  262. m_invTextureHeight = 1.0f/m_textureHeight;
  263. u_imageLod.idx = bgfx::invalidHandle;
  264. u_texColor.idx = bgfx::invalidHandle;
  265. #if !USE_NANOVG_FONT
  266. m_fontTexture.idx = bgfx::invalidHandle;
  267. #endif // !USE_NANOVG_FONT
  268. m_missingTexture.idx = bgfx::invalidHandle;
  269. m_colorProgram.idx = bgfx::invalidHandle;
  270. m_textureProgram.idx = bgfx::invalidHandle;
  271. m_imageProgram.idx = bgfx::invalidHandle;
  272. }
  273. bool create(const void* _data)
  274. {
  275. m_nvg = nvgCreate(512, 512, 1, m_view);
  276. nvgCreateFontMem(m_nvg, "default", (unsigned char*)_data, INT32_MAX, 0);
  277. nvgFontSize(m_nvg, 15.0f);
  278. nvgFontFace(m_nvg, "default");
  279. for (int32_t ii = 0; ii < NUM_CIRCLE_VERTS; ++ii)
  280. {
  281. float a = (float)ii / (float)NUM_CIRCLE_VERTS * (float)(M_PI * 2.0);
  282. m_circleVerts[ii * 2 + 0] = cosf(a);
  283. m_circleVerts[ii * 2 + 1] = sinf(a);
  284. }
  285. PosColorVertex::init();
  286. PosColorUvVertex::init();
  287. PosUvVertex::init();
  288. u_imageLod = bgfx::createUniform("u_imageLod", bgfx::UniformType::Uniform1f);
  289. u_texColor = bgfx::createUniform("u_texColor", bgfx::UniformType::Uniform1i);
  290. const bgfx::Memory* vs_imgui_color;
  291. const bgfx::Memory* fs_imgui_color;
  292. const bgfx::Memory* vs_imgui_texture;
  293. const bgfx::Memory* fs_imgui_texture;
  294. const bgfx::Memory* vs_imgui_image;
  295. const bgfx::Memory* fs_imgui_image;
  296. switch (bgfx::getRendererType() )
  297. {
  298. case bgfx::RendererType::Direct3D9:
  299. vs_imgui_color = bgfx::makeRef(vs_imgui_color_dx9, sizeof(vs_imgui_color_dx9) );
  300. fs_imgui_color = bgfx::makeRef(fs_imgui_color_dx9, sizeof(fs_imgui_color_dx9) );
  301. vs_imgui_texture = bgfx::makeRef(vs_imgui_texture_dx9, sizeof(vs_imgui_texture_dx9) );
  302. fs_imgui_texture = bgfx::makeRef(fs_imgui_texture_dx9, sizeof(fs_imgui_texture_dx9) );
  303. vs_imgui_image = bgfx::makeRef(vs_imgui_image_dx9, sizeof(vs_imgui_image_dx9) );
  304. fs_imgui_image = bgfx::makeRef(fs_imgui_image_dx9, sizeof(fs_imgui_image_dx9) );
  305. m_halfTexel = 0.5f;
  306. break;
  307. case bgfx::RendererType::Direct3D11:
  308. vs_imgui_color = bgfx::makeRef(vs_imgui_color_dx11, sizeof(vs_imgui_color_dx11) );
  309. fs_imgui_color = bgfx::makeRef(fs_imgui_color_dx11, sizeof(fs_imgui_color_dx11) );
  310. vs_imgui_texture = bgfx::makeRef(vs_imgui_texture_dx11, sizeof(vs_imgui_texture_dx11) );
  311. fs_imgui_texture = bgfx::makeRef(fs_imgui_texture_dx11, sizeof(fs_imgui_texture_dx11) );
  312. vs_imgui_image = bgfx::makeRef(vs_imgui_image_dx11, sizeof(vs_imgui_image_dx11) );
  313. fs_imgui_image = bgfx::makeRef(fs_imgui_image_dx11, sizeof(fs_imgui_image_dx11) );
  314. break;
  315. default:
  316. vs_imgui_color = bgfx::makeRef(vs_imgui_color_glsl, sizeof(vs_imgui_color_glsl) );
  317. fs_imgui_color = bgfx::makeRef(fs_imgui_color_glsl, sizeof(fs_imgui_color_glsl) );
  318. vs_imgui_texture = bgfx::makeRef(vs_imgui_texture_glsl, sizeof(vs_imgui_texture_glsl) );
  319. fs_imgui_texture = bgfx::makeRef(fs_imgui_texture_glsl, sizeof(fs_imgui_texture_glsl) );
  320. vs_imgui_image = bgfx::makeRef(vs_imgui_image_glsl, sizeof(vs_imgui_image_glsl) );
  321. fs_imgui_image = bgfx::makeRef(fs_imgui_image_glsl, sizeof(fs_imgui_image_glsl) );
  322. break;
  323. }
  324. bgfx::ShaderHandle vsh;
  325. bgfx::ShaderHandle fsh;
  326. vsh = bgfx::createShader(vs_imgui_color);
  327. fsh = bgfx::createShader(fs_imgui_color);
  328. m_colorProgram = bgfx::createProgram(vsh, fsh);
  329. bgfx::destroyShader(vsh);
  330. bgfx::destroyShader(fsh);
  331. vsh = bgfx::createShader(vs_imgui_texture);
  332. fsh = bgfx::createShader(fs_imgui_texture);
  333. m_textureProgram = bgfx::createProgram(vsh, fsh);
  334. bgfx::destroyShader(vsh);
  335. bgfx::destroyShader(fsh);
  336. vsh = bgfx::createShader(vs_imgui_image);
  337. fsh = bgfx::createShader(fs_imgui_image);
  338. m_imageProgram = bgfx::createProgram(vsh, fsh);
  339. bgfx::destroyShader(vsh);
  340. bgfx::destroyShader(fsh);
  341. #if !USE_NANOVG_FONT
  342. const bgfx::Memory* mem = bgfx::alloc(m_textureWidth * m_textureHeight);
  343. stbtt_BakeFontBitmap( (uint8_t*)_data, 0, 15.0f, mem->data, m_textureWidth, m_textureHeight, 32, 96, m_cdata);
  344. m_fontTexture = bgfx::createTexture2D(m_textureWidth, m_textureHeight, 1, bgfx::TextureFormat::R8, BGFX_TEXTURE_NONE, mem);
  345. #endif // !USE_NANOVG_FONT
  346. mem = bgfx::makeRef(s_xTexture, sizeof(s_xTexture));
  347. m_missingTexture = bgfx::createTexture(mem);
  348. return true;
  349. }
  350. void destroy()
  351. {
  352. bgfx::destroyUniform(u_imageLod);
  353. bgfx::destroyUniform(u_texColor);
  354. #if !USE_NANOVG_FONT
  355. bgfx::destroyTexture(m_fontTexture);
  356. #endif // !USE_NANOVG_FONT
  357. bgfx::destroyTexture(m_missingTexture);
  358. bgfx::destroyProgram(m_colorProgram);
  359. bgfx::destroyProgram(m_textureProgram);
  360. bgfx::destroyProgram(m_imageProgram);
  361. nvgDelete(m_nvg);
  362. }
  363. bool anyActive() const
  364. {
  365. return m_active != 0;
  366. }
  367. bool isActive(uint32_t _id) const
  368. {
  369. return m_active == _id;
  370. }
  371. bool isHot(uint32_t _id) const
  372. {
  373. return m_hot == _id;
  374. }
  375. bool inRect(int32_t _x, int32_t _y, int32_t _width, int32_t _height, bool _checkScroll = true) const
  376. {
  377. return (!_checkScroll || m_insideCurrentScroll)
  378. && m_mx >= _x
  379. && m_mx <= _x + _width
  380. && m_my >= _y
  381. && m_my <= _y + _height;
  382. }
  383. bool isEnabled(uint8_t _areaId)
  384. {
  385. return (m_enabledAreaIds>>_areaId)&0x1;
  386. }
  387. void setEnabled(uint8_t _areaId)
  388. {
  389. m_enabledAreaIds |= (UINT64_C(1)<<_areaId);
  390. }
  391. void clearInput()
  392. {
  393. m_leftPressed = false;
  394. m_leftReleased = false;
  395. m_scroll = 0;
  396. }
  397. void clearActive()
  398. {
  399. m_active = 0;
  400. // mark all UI for this frame as processed
  401. clearInput();
  402. }
  403. void setActive(uint32_t _id)
  404. {
  405. m_active = _id;
  406. m_wentActive = true;
  407. }
  408. void setHot(uint32_t _id)
  409. {
  410. m_hotToBe = _id;
  411. }
  412. bool buttonLogic(uint32_t _id, bool _over)
  413. {
  414. bool res = false;
  415. // process down
  416. if (!anyActive() )
  417. {
  418. if (_over)
  419. {
  420. setHot(_id);
  421. }
  422. if (isHot(_id)
  423. && m_leftPressed)
  424. {
  425. setActive(_id);
  426. }
  427. }
  428. // if button is active, then react on left up
  429. if (isActive(_id) )
  430. {
  431. m_isActive = true;
  432. if (_over)
  433. {
  434. setHot(_id);
  435. }
  436. if (m_leftReleased)
  437. {
  438. if (isHot(_id) )
  439. {
  440. res = true;
  441. }
  442. clearActive();
  443. }
  444. }
  445. if (isHot(_id) )
  446. {
  447. m_isHot = true;
  448. }
  449. return res;
  450. }
  451. void updateInput(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll)
  452. {
  453. bool left = (_button & IMGUI_MBUT_LEFT) != 0;
  454. m_mx = _mx;
  455. m_my = _my;
  456. m_leftPressed = !m_left && left;
  457. m_leftReleased = m_left && !left;
  458. m_left = left;
  459. m_scroll = _scroll;
  460. }
  461. void beginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, uint16_t _width, uint16_t _height, uint8_t _view)
  462. {
  463. nvgBeginFrame(m_nvg, _width, _height, 1.0f, NVG_STRAIGHT_ALPHA);
  464. m_view = _view;
  465. bgfx::setViewSeq(_view, true);
  466. bgfx::setViewRect(_view, 0, 0, _width, _height);
  467. float proj[16];
  468. bx::mtxOrtho(proj, 0.0f, (float)_width, (float)_height, 0.0f, 0.0f, 1000.0f);
  469. bgfx::setViewTransform(_view, NULL, proj);
  470. updateInput(_mx, _my, _button, _scroll);
  471. m_hot = m_hotToBe;
  472. m_hotToBe = 0;
  473. m_wentActive = false;
  474. m_isActive = false;
  475. m_isHot = false;
  476. m_widgetX = 0;
  477. m_widgetY = 0;
  478. m_widgetW = 0;
  479. m_areaId = 0;
  480. m_widgetId = 0;
  481. m_enabledAreaIds = 0;
  482. }
  483. void endFrame()
  484. {
  485. clearInput();
  486. nvgEndFrame(m_nvg);
  487. }
  488. bool beginScrollArea(const char* _name, int32_t _x, int32_t _y, int32_t _width, int32_t _height, int32_t* _scroll, bool _enabled)
  489. {
  490. m_areaId++;
  491. m_widgetId = 0;
  492. m_scrollId = (m_areaId << 8) | m_widgetId;
  493. if (_enabled)
  494. {
  495. setEnabled(m_areaId);
  496. }
  497. m_widgetX = _x + SCROLL_AREA_PADDING;
  498. m_widgetY = _y + AREA_HEADER + (*_scroll);
  499. m_widgetW = _width - SCROLL_AREA_PADDING * 4;
  500. m_scrollTop = _y + AREA_HEADER;
  501. m_scrollBottom = _y + _height;
  502. m_scrollRight = _x + _width - SCROLL_AREA_PADDING * 3;
  503. m_scrollVal = _scroll;
  504. m_scrollAreaX = _x;
  505. m_scrollAreaWidth = _width;
  506. m_scrollAreaInnerWidth = m_widgetW;
  507. m_scrollAreaTop = m_widgetY;
  508. m_focusTop = _y - AREA_HEADER;
  509. m_focusBottom = _y - AREA_HEADER + _height;
  510. m_insideScrollArea = inRect(_x, _y, _width, _height, false);
  511. m_insideCurrentScroll = m_insideScrollArea;
  512. drawRoundedRect( (float)_x
  513. , (float)_y
  514. , (float)_width
  515. , (float)_height
  516. , 6
  517. , imguiRGBA(0, 0, 0, 192)
  518. );
  519. drawText(_x + AREA_HEADER / 2
  520. , _y + AREA_HEADER / 2
  521. , ImguiTextAlign::Left
  522. , _name
  523. , imguiRGBA(255, 255, 255, 128)
  524. );
  525. nvgScissor(m_nvg
  526. , float(_x + SCROLL_AREA_PADDING)
  527. , float(_y + AREA_HEADER)
  528. , float(_width - SCROLL_AREA_PADDING * 4)
  529. , float(_height - AREA_HEADER - SCROLL_AREA_PADDING)
  530. );
  531. m_scissor = bgfx::setScissor(uint16_t(_x + SCROLL_AREA_PADDING)
  532. , uint16_t(_y + AREA_HEADER)
  533. , uint16_t(_width - SCROLL_AREA_PADDING * 4)
  534. , uint16_t(_height - AREA_HEADER - SCROLL_AREA_PADDING)
  535. );
  536. return m_insideScrollArea;
  537. }
  538. void endScrollArea()
  539. {
  540. // Disable scissoring.
  541. m_scissor = UINT16_MAX;
  542. nvgResetScissor(m_nvg);
  543. // Draw scroll bar
  544. int32_t xx = m_scrollRight + SCROLL_AREA_PADDING / 2;
  545. int32_t yy = m_scrollTop;
  546. int32_t width = SCROLL_AREA_PADDING * 2;
  547. int32_t height = m_scrollBottom - m_scrollTop;
  548. int32_t stop = m_scrollAreaTop;
  549. int32_t sbot = m_widgetY + SCROLL_AREA_PADDING;
  550. int32_t sh = sbot - stop; // The scrollable area height.
  551. float barHeight = (float)height / (float)sh;
  552. if (barHeight < 1.0f)
  553. {
  554. float barY = bx::fsaturate( (float)(yy - stop) / (float)sh);
  555. // Handle scroll bar logic.
  556. uint32_t hid = m_scrollId;
  557. int32_t hx = xx;
  558. int32_t hy = yy + (int)(barY * height);
  559. int32_t hw = width;
  560. int32_t hh = (int)(barHeight * height);
  561. const int32_t range = height - (hh - 1);
  562. bool over = inRect(hx, hy, hw, hh);
  563. buttonLogic(hid, over);
  564. if (isActive(hid) )
  565. {
  566. float uu = (float)(hy - yy) / (float)range;
  567. if (m_wentActive)
  568. {
  569. m_dragY = m_my;
  570. m_dragOrig = uu;
  571. }
  572. if (m_dragY != m_my)
  573. {
  574. uu = bx::fsaturate(m_dragOrig + (m_my - m_dragY) / (float)range);
  575. *m_scrollVal = (int)(uu * (height - sh) );
  576. }
  577. }
  578. // BG
  579. drawRoundedRect( (float)xx
  580. , (float)yy
  581. , (float)width
  582. , (float)height
  583. , (float)width / 2 - 1
  584. , imguiRGBA(0, 0, 0, 196)
  585. );
  586. // Bar
  587. if (isActive(hid) )
  588. {
  589. drawRoundedRect( (float)hx
  590. , (float)hy
  591. , (float)hw
  592. , (float)hh
  593. , (float)width / 2 - 1
  594. , imguiRGBA(255, 196, 0, 196)
  595. );
  596. }
  597. else
  598. {
  599. drawRoundedRect( (float)hx
  600. , (float)hy
  601. , (float)hw
  602. , (float)hh
  603. , (float)width / 2 - 1
  604. , isHot(hid) ? imguiRGBA(255, 196, 0, 96) : imguiRGBA(255, 255, 255, 64)
  605. );
  606. }
  607. // Handle mouse scrolling.
  608. if (m_insideScrollArea) // && !anyActive() )
  609. {
  610. if (m_scroll)
  611. {
  612. *m_scrollVal += bx::uint32_clamp(20 * m_scroll, 0, sh - height);
  613. }
  614. }
  615. }
  616. m_insideCurrentScroll = false;
  617. }
  618. bool button(const char* _text, bool _enabled)
  619. {
  620. m_widgetId++;
  621. uint16_t id = (m_areaId << 8) | m_widgetId;
  622. int32_t xx = m_widgetX;
  623. int32_t yy = m_widgetY;
  624. int32_t width = m_widgetW;
  625. int32_t height = BUTTON_HEIGHT;
  626. m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  627. bool enabled = _enabled && isEnabled(m_areaId);
  628. bool over = enabled && inRect(xx, yy, width, height);
  629. bool res = buttonLogic(id, over);
  630. drawRoundedRect( (float)xx
  631. , (float)yy
  632. , (float)width
  633. , (float)height
  634. , (float)BUTTON_HEIGHT / 2 - 1
  635. , imguiRGBA(128, 128, 128, isActive(id) ? 196 : 96)
  636. );
  637. if (enabled)
  638. {
  639. drawText(xx + BUTTON_HEIGHT / 2
  640. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  641. , ImguiTextAlign::Left
  642. , _text
  643. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  644. );
  645. }
  646. else
  647. {
  648. drawText(xx + BUTTON_HEIGHT / 2
  649. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  650. , ImguiTextAlign::Left
  651. , _text
  652. , imguiRGBA(128, 128, 128, 200)
  653. );
  654. }
  655. return res;
  656. }
  657. bool item(const char* _text, bool _enabled)
  658. {
  659. m_widgetId++;
  660. uint16_t id = (m_areaId << 8) | m_widgetId;
  661. int32_t xx = m_widgetX;
  662. int32_t yy = m_widgetY;
  663. int32_t width = m_widgetW;
  664. int32_t height = BUTTON_HEIGHT;
  665. m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  666. bool enabled = _enabled && isEnabled(m_areaId);
  667. bool over = enabled && inRect(xx, yy, width, height);
  668. bool res = buttonLogic(id, over);
  669. if (isHot(id) )
  670. {
  671. drawRoundedRect( (float)xx
  672. , (float)yy
  673. , (float)width
  674. , (float)height
  675. , 2.0f
  676. , imguiRGBA(255, 196, 0, isActive(id) ? 196 : 96)
  677. );
  678. }
  679. if (enabled)
  680. {
  681. drawText(xx + BUTTON_HEIGHT / 2
  682. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  683. , ImguiTextAlign::Left
  684. , _text
  685. , imguiRGBA(255, 255, 255, 200)
  686. );
  687. }
  688. else
  689. {
  690. drawText(xx + BUTTON_HEIGHT / 2
  691. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  692. , ImguiTextAlign::Left
  693. , _text
  694. , imguiRGBA(128, 128, 128, 200)
  695. );
  696. }
  697. return res;
  698. }
  699. bool check(const char* _text, bool _checked, bool _enabled)
  700. {
  701. m_widgetId++;
  702. uint16_t id = (m_areaId << 8) | m_widgetId;
  703. int32_t xx = m_widgetX;
  704. int32_t yy = m_widgetY;
  705. int32_t width = m_widgetW;
  706. int32_t height = BUTTON_HEIGHT;
  707. m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  708. bool enabled = _enabled && isEnabled(m_areaId);
  709. bool over = enabled && inRect(xx, yy, width, height);
  710. bool res = buttonLogic(id, over);
  711. const int32_t cx = xx + BUTTON_HEIGHT / 2 - CHECK_SIZE / 2;
  712. const int32_t cy = yy + BUTTON_HEIGHT / 2 - CHECK_SIZE / 2;
  713. drawRoundedRect( (float)cx - 3
  714. , (float)cy - 3
  715. , (float)CHECK_SIZE + 6
  716. , (float)CHECK_SIZE + 6
  717. , 4
  718. , imguiRGBA(128, 128, 128, isActive(id) ? 196 : 96)
  719. );
  720. if (_checked)
  721. {
  722. if (enabled)
  723. {
  724. drawRoundedRect( (float)cx
  725. , (float)cy
  726. , (float)CHECK_SIZE
  727. , (float)CHECK_SIZE
  728. , (float)CHECK_SIZE / 2 - 1
  729. , imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200)
  730. );
  731. }
  732. else
  733. {
  734. drawRoundedRect( (float)cx
  735. , (float)cy
  736. , (float)CHECK_SIZE
  737. , (float)CHECK_SIZE
  738. , (float)CHECK_SIZE / 2 - 1
  739. , imguiRGBA(128, 128, 128, 200)
  740. );
  741. }
  742. }
  743. if (enabled)
  744. {
  745. drawText(xx + BUTTON_HEIGHT
  746. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  747. , ImguiTextAlign::Left
  748. , _text
  749. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  750. );
  751. }
  752. else
  753. {
  754. drawText(xx + BUTTON_HEIGHT
  755. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  756. , ImguiTextAlign::Left
  757. , _text
  758. , imguiRGBA(128, 128, 128, 200)
  759. );
  760. }
  761. return res;
  762. }
  763. void image(bgfx::TextureHandle _image, float _lod, int32_t _width, int32_t _height, ImguiImageAlign::Enum _align)
  764. {
  765. int32_t xx;
  766. if (ImguiImageAlign::Left == _align)
  767. {
  768. xx = m_scrollAreaX + SCROLL_AREA_PADDING;
  769. }
  770. else if (ImguiImageAlign::LeftIndented == _align)
  771. {
  772. xx = m_widgetX;
  773. }
  774. else if (ImguiImageAlign::Center == _align)
  775. {
  776. xx = m_scrollAreaX + (m_scrollAreaInnerWidth-_width)/2;
  777. }
  778. else if (ImguiImageAlign::CenterIndented == _align)
  779. {
  780. xx = (m_widgetX + m_scrollAreaInnerWidth + m_scrollAreaX - _width)/2;
  781. }
  782. else //if (ImguiImageAlign::Right == _align).
  783. {
  784. xx = m_scrollAreaX + m_scrollAreaInnerWidth - _width;
  785. }
  786. const int32_t yy = m_widgetY;
  787. m_widgetY += _height + DEFAULT_SPACING;
  788. screenQuad(xx, yy, _width, _height);
  789. bgfx::setUniform(u_imageLod, &_lod);
  790. bgfx::setTexture(0, u_texColor, bgfx::isValid(_image) ? _image : m_missingTexture);
  791. bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
  792. bgfx::setProgram(m_imageProgram);
  793. bgfx::setScissor(m_scissor);
  794. bgfx::submit(m_view);
  795. }
  796. void image(bgfx::TextureHandle _image, float _lod, float _width, float _aspect, ImguiImageAlign::Enum _align)
  797. {
  798. const float width = _width*float(m_scrollAreaInnerWidth);
  799. const float height = width/_aspect;
  800. image(_image, _lod, int32_t(width), int32_t(height), _align);
  801. }
  802. bool collapse(const char* _text, const char* _subtext, bool _checked, bool _enabled)
  803. {
  804. m_widgetId++;
  805. uint16_t id = (m_areaId << 8) | m_widgetId;
  806. int32_t xx = m_widgetX;
  807. int32_t yy = m_widgetY;
  808. int32_t width = m_widgetW;
  809. int32_t height = BUTTON_HEIGHT;
  810. m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  811. const int32_t cx = xx + BUTTON_HEIGHT / 2 - CHECK_SIZE / 2;
  812. const int32_t cy = yy + BUTTON_HEIGHT / 2 - CHECK_SIZE / 2;
  813. bool enabled = _enabled && isEnabled(m_areaId);
  814. bool over = enabled && inRect(xx, yy, width, height);
  815. bool res = buttonLogic(id, over);
  816. if (_checked)
  817. {
  818. drawTriangle(cx
  819. , cy
  820. , CHECK_SIZE
  821. , CHECK_SIZE
  822. , 2
  823. , imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200)
  824. );
  825. }
  826. else
  827. {
  828. drawTriangle(cx
  829. , cy
  830. , CHECK_SIZE
  831. , CHECK_SIZE
  832. , 1
  833. , imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200)
  834. );
  835. }
  836. if (enabled)
  837. {
  838. drawText(xx + BUTTON_HEIGHT
  839. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  840. , ImguiTextAlign::Left
  841. , _text
  842. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  843. );
  844. }
  845. else
  846. {
  847. drawText(xx + BUTTON_HEIGHT
  848. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  849. , ImguiTextAlign::Left
  850. , _text
  851. , imguiRGBA(128, 128, 128, 200)
  852. );
  853. }
  854. if (_subtext)
  855. {
  856. drawText(xx + width - BUTTON_HEIGHT / 2
  857. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  858. , ImguiTextAlign::Right
  859. , _subtext
  860. , imguiRGBA(255, 255, 255, 128)
  861. );
  862. }
  863. return res;
  864. }
  865. void labelVargs(const char* _format, va_list _argList)
  866. {
  867. char temp[8192];
  868. char* out = temp;
  869. int32_t len = bx::vsnprintf(out, sizeof(temp), _format, _argList);
  870. if ( (int32_t)sizeof(temp) < len)
  871. {
  872. out = (char*)alloca(len+1);
  873. len = bx::vsnprintf(out, len, _format, _argList);
  874. }
  875. out[len] = '\0';
  876. int32_t xx = m_widgetX;
  877. int32_t yy = m_widgetY;
  878. m_widgetY += BUTTON_HEIGHT;
  879. drawText(xx
  880. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  881. , ImguiTextAlign::Left
  882. , out
  883. , imguiRGBA(255, 255, 255, 255)
  884. );
  885. }
  886. void value(const char* _text)
  887. {
  888. const int32_t xx = m_widgetX;
  889. const int32_t yy = m_widgetY;
  890. const int32_t ww = m_widgetW;
  891. m_widgetY += BUTTON_HEIGHT;
  892. drawText(xx + ww - BUTTON_HEIGHT / 2
  893. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  894. , ImguiTextAlign::Right
  895. , _text
  896. , imguiRGBA(255, 255, 255, 200)
  897. );
  898. }
  899. bool slider(const char* _text, float& _val, float _vmin, float _vmax, float _vinc, bool _enabled)
  900. {
  901. m_widgetId++;
  902. uint16_t id = (m_areaId << 8) | m_widgetId;
  903. int32_t xx = m_widgetX;
  904. int32_t yy = m_widgetY;
  905. int32_t width = m_widgetW;
  906. int32_t height = SLIDER_HEIGHT;
  907. m_widgetY += SLIDER_HEIGHT + DEFAULT_SPACING;
  908. drawRoundedRect( (float)xx, (float)yy, (float)width, (float)height, 4.0f, imguiRGBA(0, 0, 0, 128) );
  909. const int32_t range = width - SLIDER_MARKER_WIDTH;
  910. float uu = bx::fsaturate( (_val - _vmin) / (_vmax - _vmin) );
  911. int32_t m = (int)(uu * range);
  912. bool enabled = _enabled && isEnabled(m_areaId);
  913. bool over = enabled && inRect(xx + m, yy, SLIDER_MARKER_WIDTH, SLIDER_HEIGHT);
  914. bool res = buttonLogic(id, over);
  915. bool valChanged = false;
  916. if (isActive(id) )
  917. {
  918. if (m_wentActive)
  919. {
  920. m_dragX = m_mx;
  921. m_dragOrig = uu;
  922. }
  923. if (m_dragX != m_mx)
  924. {
  925. uu = bx::fsaturate(m_dragOrig + (float)(m_mx - m_dragX) / (float)range);
  926. _val = _vmin + uu * (_vmax - _vmin);
  927. _val = floorf(_val / _vinc + 0.5f) * _vinc; // Snap to vinc
  928. m = (int)(uu * range);
  929. valChanged = true;
  930. }
  931. }
  932. if (isActive(id) )
  933. {
  934. drawRoundedRect( (float)(xx + m)
  935. , (float)yy
  936. , (float)SLIDER_MARKER_WIDTH
  937. , (float)SLIDER_HEIGHT
  938. , 4.0f
  939. , imguiRGBA(255, 255, 255, 255)
  940. );
  941. }
  942. else
  943. {
  944. drawRoundedRect( (float)(xx + m)
  945. , (float)yy
  946. , (float)SLIDER_MARKER_WIDTH
  947. , (float)SLIDER_HEIGHT
  948. , 4.0f
  949. , isHot(id) ? imguiRGBA(255, 196, 0, 128) : imguiRGBA(255, 255, 255, 64)
  950. );
  951. }
  952. // TODO: fix this, take a look at 'nicenum'.
  953. int32_t digits = (int)(ceilf(log10f(_vinc) ) );
  954. char fmt[16];
  955. bx::snprintf(fmt, 16, "%%.%df", digits >= 0 ? 0 : -digits);
  956. char msg[128];
  957. bx::snprintf(msg, 128, fmt, _val);
  958. if (enabled)
  959. {
  960. drawText(xx + SLIDER_HEIGHT / 2
  961. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  962. , ImguiTextAlign::Left
  963. , _text
  964. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  965. );
  966. drawText(xx + width - SLIDER_HEIGHT / 2
  967. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  968. , ImguiTextAlign::Right
  969. , msg
  970. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  971. );
  972. }
  973. else
  974. {
  975. drawText(xx + SLIDER_HEIGHT / 2
  976. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  977. , ImguiTextAlign::Left
  978. , _text
  979. , imguiRGBA(128, 128, 128, 200)
  980. );
  981. drawText(xx + width - SLIDER_HEIGHT / 2
  982. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  983. , ImguiTextAlign::Right
  984. , msg
  985. , imguiRGBA(128, 128, 128, 200)
  986. );
  987. }
  988. return res || valChanged;
  989. }
  990. void indent()
  991. {
  992. m_widgetX += INDENT_SIZE;
  993. m_widgetW -= INDENT_SIZE;
  994. }
  995. void unindent()
  996. {
  997. m_widgetX -= INDENT_SIZE;
  998. m_widgetW += INDENT_SIZE;
  999. }
  1000. void separator()
  1001. {
  1002. m_widgetY += DEFAULT_SPACING * 3;
  1003. }
  1004. void separatorLine()
  1005. {
  1006. int32_t xx = m_widgetX;
  1007. int32_t yy = m_widgetY + DEFAULT_SPACING*2;
  1008. int32_t width = m_widgetW;
  1009. int32_t height = 1;
  1010. m_widgetY += DEFAULT_SPACING * 4;
  1011. drawRect( (float)xx
  1012. , (float)yy
  1013. , (float)width
  1014. , (float)height
  1015. , imguiRGBA(255, 255, 255, 32)
  1016. );
  1017. }
  1018. void drawPolygon(const float* _coords, uint32_t _numCoords, float _r, uint32_t _abgr)
  1019. {
  1020. _numCoords = bx::uint32_min(_numCoords, MAX_TEMP_COORDS);
  1021. for (uint32_t ii = 0, jj = _numCoords - 1; ii < _numCoords; jj = ii++)
  1022. {
  1023. const float* v0 = &_coords[jj * 2];
  1024. const float* v1 = &_coords[ii * 2];
  1025. float dx = v1[0] - v0[0];
  1026. float dy = v1[1] - v0[1];
  1027. float d = sqrtf(dx * dx + dy * dy);
  1028. if (d > 0)
  1029. {
  1030. d = 1.0f / d;
  1031. dx *= d;
  1032. dy *= d;
  1033. }
  1034. m_tempNormals[jj * 2 + 0] = dy;
  1035. m_tempNormals[jj * 2 + 1] = -dx;
  1036. }
  1037. for (uint32_t ii = 0, jj = _numCoords - 1; ii < _numCoords; jj = ii++)
  1038. {
  1039. float dlx0 = m_tempNormals[jj * 2 + 0];
  1040. float dly0 = m_tempNormals[jj * 2 + 1];
  1041. float dlx1 = m_tempNormals[ii * 2 + 0];
  1042. float dly1 = m_tempNormals[ii * 2 + 1];
  1043. float dmx = (dlx0 + dlx1) * 0.5f;
  1044. float dmy = (dly0 + dly1) * 0.5f;
  1045. float dmr2 = dmx * dmx + dmy * dmy;
  1046. if (dmr2 > 0.000001f)
  1047. {
  1048. float scale = 1.0f / dmr2;
  1049. if (scale > 10.0f)
  1050. {
  1051. scale = 10.0f;
  1052. }
  1053. dmx *= scale;
  1054. dmy *= scale;
  1055. }
  1056. m_tempCoords[ii * 2 + 0] = _coords[ii * 2 + 0] + dmx * _r;
  1057. m_tempCoords[ii * 2 + 1] = _coords[ii * 2 + 1] + dmy * _r;
  1058. }
  1059. uint32_t numVertices = _numCoords*6 + (_numCoords-2)*3;
  1060. if (bgfx::checkAvailTransientVertexBuffer(numVertices, PosColorVertex::ms_decl) )
  1061. {
  1062. bgfx::TransientVertexBuffer tvb;
  1063. bgfx::allocTransientVertexBuffer(&tvb, numVertices, PosColorVertex::ms_decl);
  1064. uint32_t trans = _abgr&0xffffff;
  1065. PosColorVertex* vertex = (PosColorVertex*)tvb.data;
  1066. for (uint32_t ii = 0, jj = _numCoords-1; ii < _numCoords; jj = ii++)
  1067. {
  1068. vertex->m_x = _coords[ii*2+0];
  1069. vertex->m_y = _coords[ii*2+1];
  1070. vertex->m_abgr = _abgr;
  1071. ++vertex;
  1072. vertex->m_x = _coords[jj*2+0];
  1073. vertex->m_y = _coords[jj*2+1];
  1074. vertex->m_abgr = _abgr;
  1075. ++vertex;
  1076. vertex->m_x = m_tempCoords[jj*2+0];
  1077. vertex->m_y = m_tempCoords[jj*2+1];
  1078. vertex->m_abgr = trans;
  1079. ++vertex;
  1080. vertex->m_x = m_tempCoords[jj*2+0];
  1081. vertex->m_y = m_tempCoords[jj*2+1];
  1082. vertex->m_abgr = trans;
  1083. ++vertex;
  1084. vertex->m_x = m_tempCoords[ii*2+0];
  1085. vertex->m_y = m_tempCoords[ii*2+1];
  1086. vertex->m_abgr = trans;
  1087. ++vertex;
  1088. vertex->m_x = _coords[ii*2+0];
  1089. vertex->m_y = _coords[ii*2+1];
  1090. vertex->m_abgr = _abgr;
  1091. ++vertex;
  1092. }
  1093. for (uint32_t ii = 2; ii < _numCoords; ++ii)
  1094. {
  1095. vertex->m_x = _coords[0];
  1096. vertex->m_y = _coords[1];
  1097. vertex->m_abgr = _abgr;
  1098. ++vertex;
  1099. vertex->m_x = _coords[(ii-1)*2+0];
  1100. vertex->m_y = _coords[(ii-1)*2+1];
  1101. vertex->m_abgr = _abgr;
  1102. ++vertex;
  1103. vertex->m_x = _coords[ii*2+0];
  1104. vertex->m_y = _coords[ii*2+1];
  1105. vertex->m_abgr = _abgr;
  1106. ++vertex;
  1107. }
  1108. bgfx::setVertexBuffer(&tvb);
  1109. bgfx::setState(0
  1110. | BGFX_STATE_RGB_WRITE
  1111. | BGFX_STATE_ALPHA_WRITE
  1112. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  1113. );
  1114. bgfx::setProgram(m_colorProgram);
  1115. bgfx::setScissor(m_scissor);
  1116. bgfx::submit(m_view);
  1117. }
  1118. }
  1119. void drawRect(float _x, float _y, float w, float h, uint32_t _argb, float _fth = 1.0f)
  1120. {
  1121. float verts[4 * 2] =
  1122. {
  1123. _x + 0.5f, _y + 0.5f,
  1124. _x + w - 0.5f, _y + 0.5f,
  1125. _x + w - 0.5f, _y + h - 0.5f,
  1126. _x + 0.5f, _y + h - 0.5f,
  1127. };
  1128. drawPolygon(verts, 4, _fth, _argb);
  1129. }
  1130. void drawRoundedRect(float _x, float _y, float w, float h, float r, uint32_t _argb, float _fth = 1.0f)
  1131. {
  1132. const uint32_t num = NUM_CIRCLE_VERTS / 4;
  1133. const float* cverts = m_circleVerts;
  1134. float verts[(num + 1) * 4 * 2];
  1135. float* vv = verts;
  1136. for (uint32_t ii = 0; ii <= num; ++ii)
  1137. {
  1138. *vv++ = _x + w - r + cverts[ii * 2] * r;
  1139. *vv++ = _y + h - r + cverts[ii * 2 + 1] * r;
  1140. }
  1141. for (uint32_t ii = num; ii <= num * 2; ++ii)
  1142. {
  1143. *vv++ = _x + r + cverts[ii * 2] * r;
  1144. *vv++ = _y + h - r + cverts[ii * 2 + 1] * r;
  1145. }
  1146. for (uint32_t ii = num * 2; ii <= num * 3; ++ii)
  1147. {
  1148. *vv++ = _x + r + cverts[ii * 2] * r;
  1149. *vv++ = _y + r + cverts[ii * 2 + 1] * r;
  1150. }
  1151. for (uint32_t ii = num * 3; ii < num * 4; ++ii)
  1152. {
  1153. *vv++ = _x + w - r + cverts[ii * 2] * r;
  1154. *vv++ = _y + r + cverts[ii * 2 + 1] * r;
  1155. }
  1156. *vv++ = _x + w - r + cverts[0] * r;
  1157. *vv++ = _y + r + cverts[1] * r;
  1158. drawPolygon(verts, (num + 1) * 4, _fth, _argb);
  1159. }
  1160. void drawLine(float _x0, float _y0, float _x1, float _y1, float _r, uint32_t _abgr, float _fth = 1.0f)
  1161. {
  1162. float dx = _x1 - _x0;
  1163. float dy = _y1 - _y0;
  1164. float d = sqrtf(dx * dx + dy * dy);
  1165. if (d > 0.0001f)
  1166. {
  1167. d = 1.0f / d;
  1168. dx *= d;
  1169. dy *= d;
  1170. }
  1171. float nx = dy;
  1172. float ny = -dx;
  1173. float verts[4 * 2];
  1174. _r -= _fth;
  1175. _r *= 0.5f;
  1176. if (_r < 0.01f)
  1177. {
  1178. _r = 0.01f;
  1179. }
  1180. dx *= _r;
  1181. dy *= _r;
  1182. nx *= _r;
  1183. ny *= _r;
  1184. verts[0] = _x0 - dx - nx;
  1185. verts[1] = _y0 - dy - ny;
  1186. verts[2] = _x0 - dx + nx;
  1187. verts[3] = _y0 - dy + ny;
  1188. verts[4] = _x1 + dx + nx;
  1189. verts[5] = _y1 + dy + ny;
  1190. verts[6] = _x1 + dx - nx;
  1191. verts[7] = _y1 + dy - ny;
  1192. drawPolygon(verts, 4, _fth, _abgr);
  1193. }
  1194. void drawTriangle(int32_t _x, int32_t _y, int32_t _width, int32_t _height, int32_t _flags, uint32_t _abgr)
  1195. {
  1196. if (1 == _flags)
  1197. {
  1198. const float verts[3 * 2] =
  1199. {
  1200. (float)_x + 0.5f, (float)_y + 0.5f,
  1201. (float)_x + 0.5f + (float)_width * 1.0f, (float)_y + 0.5f + (float)_height / 2.0f - 0.5f,
  1202. (float)_x + 0.5f, (float)_y + 0.5f + (float)_height - 1.0f,
  1203. };
  1204. drawPolygon(verts, 3, 1.0f, _abgr);
  1205. }
  1206. else
  1207. {
  1208. const float verts[3 * 2] =
  1209. {
  1210. (float)_x + 0.5f, (float)_y + 0.5f + (float)_height - 1.0f,
  1211. (float)_x + 0.5f + (float)_width / 2.0f - 0.5f, (float)_y + 0.5f,
  1212. (float)_x + 0.5f + (float)_width - 1.0f, (float)_y + 0.5f + (float)_height - 1.0f,
  1213. };
  1214. drawPolygon(verts, 3, 1.0f, _abgr);
  1215. }
  1216. }
  1217. #if !USE_NANOVG_FONT
  1218. void getBakedQuad(stbtt_bakedchar* _chardata, int32_t char_index, float* _xpos, float* _ypos, stbtt_aligned_quad* _quad)
  1219. {
  1220. stbtt_bakedchar* b = _chardata + char_index;
  1221. int32_t round_x = STBTT_ifloor(*_xpos + b->xoff);
  1222. int32_t round_y = STBTT_ifloor(*_ypos + b->yoff);
  1223. _quad->x0 = (float)round_x;
  1224. _quad->y0 = (float)round_y;
  1225. _quad->x1 = (float)round_x + b->x1 - b->x0;
  1226. _quad->y1 = (float)round_y + b->y1 - b->y0;
  1227. _quad->s0 = (b->x0 + m_halfTexel) * m_invTextureWidth;
  1228. _quad->t0 = (b->y0 + m_halfTexel) * m_invTextureWidth;
  1229. _quad->s1 = (b->x1 + m_halfTexel) * m_invTextureHeight;
  1230. _quad->t1 = (b->y1 + m_halfTexel) * m_invTextureHeight;
  1231. *_xpos += b->xadvance;
  1232. }
  1233. float getTextLength(stbtt_bakedchar* _chardata, const char* _text, uint32_t& _numVertices)
  1234. {
  1235. float xpos = 0;
  1236. float len = 0;
  1237. uint32_t numVertices = 0;
  1238. while (*_text)
  1239. {
  1240. int32_t ch = (uint8_t)*_text;
  1241. if (ch == '\t')
  1242. {
  1243. for (int32_t ii = 0; ii < 4; ++ii)
  1244. {
  1245. if (xpos < s_tabStops[ii])
  1246. {
  1247. xpos = s_tabStops[ii];
  1248. break;
  1249. }
  1250. }
  1251. }
  1252. else if (ch >= ' '
  1253. && ch < 128)
  1254. {
  1255. stbtt_bakedchar* b = _chardata + ch - ' ';
  1256. int32_t round_x = STBTT_ifloor( (xpos + b->xoff) + 0.5);
  1257. len = round_x + b->x1 - b->x0 + 0.5f;
  1258. xpos += b->xadvance;
  1259. numVertices += 6;
  1260. }
  1261. ++_text;
  1262. }
  1263. _numVertices = numVertices;
  1264. return len;
  1265. }
  1266. #endif // !USE_NANOVG_FONT
  1267. void drawText(int32_t _x, int32_t _y, ImguiTextAlign::Enum _align, const char* _text, uint32_t _abgr)
  1268. {
  1269. drawText( (float)_x, (float)_y, _text, _align, _abgr);
  1270. }
  1271. void drawText(float _x, float _y, const char* _text, ImguiTextAlign::Enum _align, uint32_t _abgr)
  1272. {
  1273. #if USE_NANOVG_FONT
  1274. static uint32_t textAlign[ImguiTextAlign::Count] =
  1275. {
  1276. NVG_ALIGN_LEFT,
  1277. NVG_ALIGN_CENTER,
  1278. NVG_ALIGN_RIGHT,
  1279. };
  1280. nvgTextAlign(m_nvg, textAlign[_align]);
  1281. nvgFontBlur(m_nvg, 0.0f);
  1282. nvgFillColor(m_nvg, nvgRGBAu(_abgr) );
  1283. nvgText(m_nvg, _x, _y, _text, NULL);
  1284. #else
  1285. uint32_t numVertices = 0;
  1286. if (_align == ImguiTextAlign::Center)
  1287. {
  1288. _x -= getTextLength(m_cdata, _text, numVertices) / 2;
  1289. }
  1290. else if (_align == ImguiTextAlign::Right)
  1291. {
  1292. _x -= getTextLength(m_cdata, _text, numVertices);
  1293. }
  1294. else // just count vertices
  1295. {
  1296. getTextLength(m_cdata, _text, numVertices);
  1297. }
  1298. if (bgfx::checkAvailTransientVertexBuffer(numVertices, PosColorUvVertex::ms_decl) )
  1299. {
  1300. bgfx::TransientVertexBuffer tvb;
  1301. bgfx::allocTransientVertexBuffer(&tvb, numVertices, PosColorUvVertex::ms_decl);
  1302. PosColorUvVertex* vertex = (PosColorUvVertex*)tvb.data;
  1303. const float ox = _x;
  1304. while (*_text)
  1305. {
  1306. int32_t ch = (uint8_t)*_text;
  1307. if (ch == '\t')
  1308. {
  1309. for (int32_t i = 0; i < 4; ++i)
  1310. {
  1311. if (_x < s_tabStops[i] + ox)
  1312. {
  1313. _x = s_tabStops[i] + ox;
  1314. break;
  1315. }
  1316. }
  1317. }
  1318. else if (ch >= ' '
  1319. && ch < 128)
  1320. {
  1321. stbtt_aligned_quad quad;
  1322. getBakedQuad(m_cdata, ch - 32, &_x, &_y, &quad);
  1323. vertex->m_x = quad.x0;
  1324. vertex->m_y = quad.y0;
  1325. vertex->m_u = quad.s0;
  1326. vertex->m_v = quad.t0;
  1327. vertex->m_abgr = _abgr;
  1328. ++vertex;
  1329. vertex->m_x = quad.x1;
  1330. vertex->m_y = quad.y1;
  1331. vertex->m_u = quad.s1;
  1332. vertex->m_v = quad.t1;
  1333. vertex->m_abgr = _abgr;
  1334. ++vertex;
  1335. vertex->m_x = quad.x1;
  1336. vertex->m_y = quad.y0;
  1337. vertex->m_u = quad.s1;
  1338. vertex->m_v = quad.t0;
  1339. vertex->m_abgr = _abgr;
  1340. ++vertex;
  1341. vertex->m_x = quad.x0;
  1342. vertex->m_y = quad.y0;
  1343. vertex->m_u = quad.s0;
  1344. vertex->m_v = quad.t0;
  1345. vertex->m_abgr = _abgr;
  1346. ++vertex;
  1347. vertex->m_x = quad.x0;
  1348. vertex->m_y = quad.y1;
  1349. vertex->m_u = quad.s0;
  1350. vertex->m_v = quad.t1;
  1351. vertex->m_abgr = _abgr;
  1352. ++vertex;
  1353. vertex->m_x = quad.x1;
  1354. vertex->m_y = quad.y1;
  1355. vertex->m_u = quad.s1;
  1356. vertex->m_v = quad.t1;
  1357. vertex->m_abgr = _abgr;
  1358. ++vertex;
  1359. }
  1360. ++_text;
  1361. }
  1362. bgfx::setTexture(0, u_texColor, m_fontTexture);
  1363. bgfx::setVertexBuffer(&tvb);
  1364. bgfx::setState(0
  1365. | BGFX_STATE_RGB_WRITE
  1366. | BGFX_STATE_ALPHA_WRITE
  1367. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  1368. );
  1369. bgfx::setProgram(m_textureProgram);
  1370. bgfx::setScissor(m_scissor);
  1371. bgfx::submit(m_view);
  1372. }
  1373. #endif // USE_NANOVG_FONT
  1374. }
  1375. void screenQuad(int32_t _x, int32_t _y, int32_t _width, uint32_t _height)
  1376. {
  1377. if (bgfx::checkAvailTransientVertexBuffer(6, PosUvVertex::ms_decl) )
  1378. {
  1379. bgfx::TransientVertexBuffer vb;
  1380. bgfx::allocTransientVertexBuffer(&vb, 6, PosUvVertex::ms_decl);
  1381. PosUvVertex* vertex = (PosUvVertex*)vb.data;
  1382. const float widthf = float(_width);
  1383. const float heightf = float(_height);
  1384. const float minx = float(_x);
  1385. const float miny = float(_y);
  1386. const float maxx = minx+widthf;
  1387. const float maxy = miny+heightf;
  1388. const float texelHalfW = m_halfTexel/widthf;
  1389. const float texelHalfH = m_halfTexel/heightf;
  1390. const float minu = texelHalfW;
  1391. const float maxu = 1.0f - texelHalfW;
  1392. const float minv = texelHalfH;
  1393. const float maxv = texelHalfH + 1.0f;
  1394. vertex[0].m_x = minx;
  1395. vertex[0].m_y = miny;
  1396. vertex[0].m_u = minu;
  1397. vertex[0].m_v = minv;
  1398. vertex[1].m_x = maxx;
  1399. vertex[1].m_y = miny;
  1400. vertex[1].m_u = maxu;
  1401. vertex[1].m_v = minv;
  1402. vertex[2].m_x = maxx;
  1403. vertex[2].m_y = maxy;
  1404. vertex[2].m_u = maxu;
  1405. vertex[2].m_v = maxv;
  1406. vertex[3].m_x = maxx;
  1407. vertex[3].m_y = maxy;
  1408. vertex[3].m_u = maxu;
  1409. vertex[3].m_v = maxv;
  1410. vertex[4].m_x = minx;
  1411. vertex[4].m_y = maxy;
  1412. vertex[4].m_u = minu;
  1413. vertex[4].m_v = maxv;
  1414. vertex[5].m_x = minx;
  1415. vertex[5].m_y = miny;
  1416. vertex[5].m_u = minu;
  1417. vertex[5].m_v = minv;
  1418. bgfx::setVertexBuffer(&vb);
  1419. }
  1420. }
  1421. void colorWheelWidget(float _rgb[3], bool _respectIndentation, bool _enabled)
  1422. {
  1423. m_widgetId++;
  1424. const uint16_t wheelId = (m_areaId << 8) | m_widgetId;
  1425. m_widgetId++;
  1426. const uint16_t triangleId = (m_areaId << 8) | m_widgetId;
  1427. const int32_t height = m_scrollAreaWidth - COLOR_WHEEL_PADDING;
  1428. const float heightf = float(height);
  1429. const float widthf = float(m_scrollAreaWidth - COLOR_WHEEL_PADDING);
  1430. const float xx = float( (_respectIndentation ? m_widgetX-SCROLL_AREA_PADDING : m_scrollAreaX) + COLOR_WHEEL_PADDING/2);
  1431. const float yy = float(m_widgetY);
  1432. m_widgetY += height + DEFAULT_SPACING;
  1433. const float ro = (widthf < heightf ? widthf : heightf) * 0.5f - 5.0f; // radiusOuter.
  1434. const float rd = 20.0f; // radiusDelta.
  1435. const float ri = ro - rd; // radiusInner.
  1436. const float aeps = 0.5f / ro; // Half a pixel arc length in radians (2pi cancels out).
  1437. const float center[2] = { xx + widthf*0.5f, yy + heightf*0.5f };
  1438. const float cmx = float(m_mx) - center[0];
  1439. const float cmy = float(m_my) - center[1];
  1440. const float aa[2] = { ri - 6.0f, 0.0f }; // Hue point.
  1441. const float bb[2] = { cosf(-120.0f/180.0f*NVG_PI) * aa[0], sinf(-120.0f/180.0f*NVG_PI) * aa[0] }; // Black point.
  1442. const float cc[2] = { cosf( 120.0f/180.0f*NVG_PI) * aa[0], sinf( 120.0f/180.0f*NVG_PI) * aa[0] }; // White point.
  1443. const float ca[2] = { aa[0] - cc[0], aa[1] - cc[1] };
  1444. const float lenCa = sqrtf(ca[0]*ca[0]+ca[1]*ca[1]);
  1445. const float invLenCa = 1.0f/lenCa;
  1446. const float dirCa[2] = { ca[0]*invLenCa, ca[1]*invLenCa };
  1447. float sel[2];
  1448. float hsv[3];
  1449. bx::rgbToHsv(hsv, _rgb);
  1450. bool enabled = _enabled && isEnabled(m_areaId);
  1451. if (enabled)
  1452. {
  1453. if (m_leftPressed)
  1454. {
  1455. const float len = sqrtf(cmx*cmx + cmy*cmy);
  1456. if (len > ri)
  1457. {
  1458. if (len < ro)
  1459. {
  1460. setActive(wheelId);
  1461. }
  1462. }
  1463. else
  1464. {
  1465. setActive(triangleId);
  1466. }
  1467. }
  1468. if (m_leftReleased
  1469. && (isActive(wheelId) || isActive(triangleId) ) )
  1470. {
  1471. clearActive();
  1472. }
  1473. // Set hue.
  1474. if (m_left
  1475. && isActive(wheelId) )
  1476. {
  1477. hsv[0] = atan2f(cmy, cmx)/NVG_PI*0.5f;
  1478. if (hsv[0] < 0.0f)
  1479. {
  1480. hsv[0]+=1.0f;
  1481. }
  1482. }
  1483. }
  1484. if (enabled
  1485. && m_left
  1486. && isActive(triangleId) )
  1487. {
  1488. float an = -hsv[0]*NVG_PI*2.0f;
  1489. float tmx = (cmx*cosf(an)-cmy*sinf(an) );
  1490. float tmy = (cmx*sinf(an)+cmy*cosf(an) );
  1491. if (pointInTriangle(tmx, tmy, aa[0], aa[1], bb[0], bb[1], cc[0], cc[1]) )
  1492. {
  1493. sel[0] = tmx;
  1494. sel[1] = tmy;
  1495. }
  1496. else
  1497. {
  1498. closestPointOnTriangle(sel[0], sel[1], tmx, tmy, aa[0], aa[1], bb[0], bb[1], cc[0], cc[1]);
  1499. }
  1500. }
  1501. else
  1502. {
  1503. ///
  1504. /// bb (black)
  1505. /// /\
  1506. /// / \
  1507. /// / \
  1508. /// / \
  1509. /// / \
  1510. /// / .sel \
  1511. /// / \
  1512. /// cc(white)/____.ss_______\aa (hue)
  1513. ///
  1514. const float ss[2] =
  1515. {
  1516. cc[0] + dirCa[0]*lenCa*hsv[1],
  1517. cc[1] + dirCa[1]*lenCa*hsv[1],
  1518. };
  1519. const float sb[2] = { bb[0]-ss[0], bb[1]-ss[1] };
  1520. const float lenSb = sqrtf(sb[0]*sb[0]+sb[1]*sb[1]);
  1521. const float invLenSb = 1.0f/lenSb;
  1522. const float dirSb[2] = { sb[0]*invLenSb, sb[1]*invLenSb };
  1523. sel[0] = cc[0] + dirCa[0]*lenCa*hsv[1] + dirSb[0]*lenSb*(1.0f - hsv[2]);
  1524. sel[1] = cc[1] + dirCa[1]*lenCa*hsv[1] + dirSb[1]*lenSb*(1.0f - hsv[2]);
  1525. }
  1526. float uu, vv, ww;
  1527. barycentric(uu, vv, ww
  1528. , aa[0], aa[1]
  1529. , bb[0], bb[1]
  1530. , cc[0], cc[1]
  1531. , sel[0], sel[1]
  1532. );
  1533. const float val = bx::fclamp(1.0f-vv, 0.0001f, 1.0f);
  1534. const float sat = bx::fclamp(uu/val, 0.0001f, 1.0f);
  1535. const float out[3] = { hsv[0], sat, val };
  1536. bx::hsvToRgb(_rgb, out);
  1537. // Draw widget.
  1538. nvgSave(m_nvg);
  1539. {
  1540. float saturation;
  1541. uint8_t alpha0;
  1542. uint8_t alpha1;
  1543. if (enabled)
  1544. {
  1545. saturation = 1.0f;
  1546. alpha0 = 255;
  1547. alpha1 = 192;
  1548. }
  1549. else
  1550. {
  1551. saturation = 0.0f;
  1552. alpha0 = 10;
  1553. alpha1 = 10;
  1554. }
  1555. // Circle.
  1556. for (uint8_t ii = 0; ii < 6; ii++)
  1557. {
  1558. const float a0 = float(ii)/6.0f * 2.0f*NVG_PI - aeps;
  1559. const float a1 = float(ii+1.0f)/6.0f * 2.0f*NVG_PI + aeps;
  1560. nvgBeginPath(m_nvg);
  1561. nvgArc(m_nvg, center[0], center[1], ri, a0, a1, NVG_CW);
  1562. nvgArc(m_nvg, center[0], center[1], ro, a1, a0, NVG_CCW);
  1563. nvgClosePath(m_nvg);
  1564. const float ax = center[0] + cosf(a0) * (ri+ro)*0.5f;
  1565. const float ay = center[1] + sinf(a0) * (ri+ro)*0.5f;
  1566. const float bx = center[0] + cosf(a1) * (ri+ro)*0.5f;
  1567. const float by = center[1] + sinf(a1) * (ri+ro)*0.5f;
  1568. NVGpaint paint = nvgLinearGradient(m_nvg
  1569. , ax, ay
  1570. , bx, by
  1571. , nvgHSLA(a0/NVG_PI*0.5f,saturation,0.55f,alpha0)
  1572. , nvgHSLA(a1/NVG_PI*0.5f,saturation,0.55f,alpha0)
  1573. );
  1574. nvgFillPaint(m_nvg, paint);
  1575. nvgFill(m_nvg);
  1576. }
  1577. // Circle stroke.
  1578. nvgBeginPath(m_nvg);
  1579. nvgCircle(m_nvg, center[0], center[1], ri-0.5f);
  1580. nvgCircle(m_nvg, center[0], center[1], ro+0.5f);
  1581. nvgStrokeColor(m_nvg, nvgRGBA(0,0,0,64) );
  1582. nvgStrokeWidth(m_nvg, 1.0f);
  1583. nvgStroke(m_nvg);
  1584. nvgSave(m_nvg);
  1585. {
  1586. // Hue selector.
  1587. nvgTranslate(m_nvg, center[0], center[1]);
  1588. nvgRotate(m_nvg, hsv[0]*NVG_PI*2.0f);
  1589. nvgStrokeWidth(m_nvg, 2.0f);
  1590. nvgBeginPath(m_nvg);
  1591. nvgRect(m_nvg, ri-1.0f,-3.0f,rd+2.0f,6.0f);
  1592. nvgStrokeColor(m_nvg, nvgRGBA(255,255,255,alpha1) );
  1593. nvgStroke(m_nvg);
  1594. // Hue selector drop shadow.
  1595. 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) );
  1596. nvgBeginPath(m_nvg);
  1597. nvgRect(m_nvg, ri-2.0f-10.0f,-4.0f-10.0f,ro-ri+4.0f+20.0f,8.0f+20.0f);
  1598. nvgRect(m_nvg, ri-2.0f,-4.0f,ro-ri+4.0f,8.0f);
  1599. nvgPathWinding(m_nvg, NVG_HOLE);
  1600. nvgFillPaint(m_nvg, paint);
  1601. nvgFill(m_nvg);
  1602. // Center triangle stroke.
  1603. nvgBeginPath(m_nvg);
  1604. nvgMoveTo(m_nvg, aa[0], aa[1]);
  1605. nvgLineTo(m_nvg, bb[0], bb[1]);
  1606. nvgLineTo(m_nvg, cc[0], cc[1]);
  1607. nvgClosePath(m_nvg);
  1608. nvgStrokeColor(m_nvg, nvgRGBA(0,0,0,64) );
  1609. nvgStroke(m_nvg);
  1610. // Center triangle fill.
  1611. paint = nvgLinearGradient(m_nvg, aa[0], aa[1], bb[0], bb[1], nvgHSL(hsv[0],saturation,0.5f), nvgRGBA(0,0,0,alpha0) );
  1612. nvgFillPaint(m_nvg, paint);
  1613. nvgFill(m_nvg);
  1614. 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) );
  1615. nvgFillPaint(m_nvg, paint);
  1616. nvgFill(m_nvg);
  1617. // Color selector.
  1618. nvgStrokeWidth(m_nvg, 2.0f);
  1619. nvgBeginPath(m_nvg);
  1620. nvgCircle(m_nvg, sel[0], sel[1], 5);
  1621. nvgStrokeColor(m_nvg, nvgRGBA(255,255,255,alpha1) );
  1622. nvgStroke(m_nvg);
  1623. // Color selector stroke.
  1624. paint = nvgRadialGradient(m_nvg, sel[0], sel[1], 7.0f, 9.0f, nvgRGBA(0,0,0,64), nvgRGBA(0,0,0,0) );
  1625. nvgBeginPath(m_nvg);
  1626. nvgRect(m_nvg, sel[0]-20.0f, sel[1]-20.0f, 40.0f, 40.0f);
  1627. nvgCircle(m_nvg, sel[0], sel[1], 7.0f);
  1628. nvgPathWinding(m_nvg, NVG_HOLE);
  1629. nvgFillPaint(m_nvg, paint);
  1630. nvgFill(m_nvg);
  1631. }
  1632. nvgRestore(m_nvg);
  1633. }
  1634. nvgRestore(m_nvg);
  1635. }
  1636. int32_t m_mx;
  1637. int32_t m_my;
  1638. int32_t m_scroll;
  1639. uint32_t m_active;
  1640. uint32_t m_hot;
  1641. uint32_t m_hotToBe;
  1642. int32_t m_dragX;
  1643. int32_t m_dragY;
  1644. float m_dragOrig;
  1645. int32_t m_widgetX;
  1646. int32_t m_widgetY;
  1647. int32_t m_widgetW;
  1648. bool m_left;
  1649. bool m_leftPressed;
  1650. bool m_leftReleased;
  1651. bool m_isHot;
  1652. bool m_isActive;
  1653. bool m_wentActive;
  1654. bool m_insideCurrentScroll;
  1655. uint8_t m_areaId;
  1656. uint8_t m_widgetId;
  1657. uint64_t m_enabledAreaIds;
  1658. uint16_t m_scissor;
  1659. float m_tempCoords[MAX_TEMP_COORDS * 2];
  1660. float m_tempNormals[MAX_TEMP_COORDS * 2];
  1661. float m_circleVerts[NUM_CIRCLE_VERTS * 2];
  1662. int32_t m_scrollTop;
  1663. int32_t m_scrollBottom;
  1664. int32_t m_scrollRight;
  1665. int32_t m_scrollAreaTop;
  1666. int32_t m_scrollAreaWidth;
  1667. int32_t m_scrollAreaInnerWidth;
  1668. int32_t m_scrollAreaX;
  1669. int32_t* m_scrollVal;
  1670. int32_t m_focusTop;
  1671. int32_t m_focusBottom;
  1672. uint16_t m_scrollId;
  1673. bool m_insideScrollArea;
  1674. uint16_t m_textureWidth;
  1675. uint16_t m_textureHeight;
  1676. float m_invTextureWidth;
  1677. float m_invTextureHeight;
  1678. float m_halfTexel;
  1679. NVGcontext* m_nvg;
  1680. uint8_t m_view;
  1681. bgfx::UniformHandle u_imageLod;
  1682. bgfx::UniformHandle u_texColor;
  1683. bgfx::ProgramHandle m_colorProgram;
  1684. bgfx::ProgramHandle m_textureProgram;
  1685. bgfx::ProgramHandle m_imageProgram;
  1686. #if !USE_NANOVG_FONT
  1687. stbtt_bakedchar m_cdata[96]; // ASCII 32..126 is 95 glyphs
  1688. bgfx::TextureHandle m_fontTexture;
  1689. #endif // !USE_NANOVG_FONT
  1690. bgfx::TextureHandle m_missingTexture;
  1691. };
  1692. static Imgui s_imgui;
  1693. bool imguiCreate(const void* _data)
  1694. {
  1695. return s_imgui.create(_data);
  1696. }
  1697. void imguiDestroy()
  1698. {
  1699. s_imgui.destroy();
  1700. }
  1701. void imguiBeginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, uint16_t _width, uint16_t _height, uint8_t _view)
  1702. {
  1703. s_imgui.beginFrame(_mx, _my, _button, _scroll, _width, _height, _view);
  1704. }
  1705. void imguiEndFrame()
  1706. {
  1707. s_imgui.endFrame();
  1708. }
  1709. bool imguiBeginScrollArea(const char* _name, int32_t _x, int32_t _y, int32_t _width, int32_t _height, int32_t* _scroll, bool _enabled)
  1710. {
  1711. return s_imgui.beginScrollArea(_name, _x, _y, _width, _height, _scroll, _enabled);
  1712. }
  1713. void imguiEndScrollArea()
  1714. {
  1715. return s_imgui.endScrollArea();
  1716. }
  1717. void imguiIndent()
  1718. {
  1719. s_imgui.indent();
  1720. }
  1721. void imguiUnindent()
  1722. {
  1723. s_imgui.unindent();
  1724. }
  1725. void imguiSeparator()
  1726. {
  1727. s_imgui.separator();
  1728. }
  1729. void imguiSeparatorLine()
  1730. {
  1731. s_imgui.separatorLine();
  1732. }
  1733. bool imguiButton(const char* _text, bool _enabled)
  1734. {
  1735. return s_imgui.button(_text, _enabled);
  1736. }
  1737. bool imguiItem(const char* _text, bool _enabled)
  1738. {
  1739. return s_imgui.item(_text, _enabled);
  1740. }
  1741. bool imguiCheck(const char* _text, bool _checked, bool _enabled)
  1742. {
  1743. return s_imgui.check(_text, _checked, _enabled);
  1744. }
  1745. bool imguiCollapse(const char* _text, const char* _subtext, bool _checked, bool _enabled)
  1746. {
  1747. return s_imgui.collapse(_text, _subtext, _checked, _enabled);
  1748. }
  1749. void imguiLabel(const char* _format, ...)
  1750. {
  1751. va_list argList;
  1752. va_start(argList, _format);
  1753. s_imgui.labelVargs(_format, argList);
  1754. va_end(argList);
  1755. }
  1756. void imguiValue(const char* _text)
  1757. {
  1758. s_imgui.value(_text);
  1759. }
  1760. bool imguiSlider(const char* _text, float& _val, float _vmin, float _vmax, float _vinc, bool _enabled)
  1761. {
  1762. return s_imgui.slider(_text, _val, _vmin, _vmax, _vinc, _enabled);
  1763. }
  1764. bool imguiSlider(const char* _text, int32_t& _val, int32_t _vmin, int32_t _vmax, bool _enabled)
  1765. {
  1766. float val = (float)_val;
  1767. bool result = s_imgui.slider(_text, val, (float)_vmin, (float)_vmax, 1.0f, _enabled);
  1768. _val = (int32_t)val;
  1769. return result;
  1770. }
  1771. uint32_t imguiChooseUseMacroInstead(uint32_t _selected, ...)
  1772. {
  1773. va_list argList;
  1774. va_start(argList, _selected);
  1775. const char* str = va_arg(argList, const char*);
  1776. for (uint32_t ii = 0; str != NULL; ++ii, str = va_arg(argList, const char*) )
  1777. {
  1778. if (imguiCheck(str, ii == _selected) )
  1779. {
  1780. _selected = ii;
  1781. }
  1782. }
  1783. va_end(argList);
  1784. return _selected;
  1785. }
  1786. void imguiDrawText(int32_t _x, int32_t _y, ImguiTextAlign::Enum _align, const char* _text, uint32_t _argb)
  1787. {
  1788. s_imgui.drawText(_x, _y, _align, _text, _argb);
  1789. }
  1790. void imguiDrawLine(float _x0, float _y0, float _x1, float _y1, float _r, uint32_t _argb)
  1791. {
  1792. s_imgui.drawLine(_x0, _y0, _x1, _y1, _r, _argb);
  1793. }
  1794. void imguiDrawRoundedRect(float _x, float _y, float _width, float _height, float _r, uint32_t _argb)
  1795. {
  1796. s_imgui.drawRoundedRect(_x, _y, _width, _height, _r, _argb);
  1797. }
  1798. void imguiDrawRect(float _x, float _y, float _width, float _height, uint32_t _argb)
  1799. {
  1800. s_imgui.drawRect(_x, _y, _width, _height, _argb);
  1801. }
  1802. void imguiBool(const char* _text, bool& _flag, bool _enabled)
  1803. {
  1804. if (imguiCheck(_text, _flag, _enabled) )
  1805. {
  1806. _flag = !_flag;
  1807. }
  1808. }
  1809. void imguiColorWheel(float _rgb[3], bool _respectIndentation, bool _enabled)
  1810. {
  1811. s_imgui.colorWheelWidget(_rgb, _respectIndentation, _enabled);
  1812. }
  1813. void imguiColorWheel(const char* _text, float _rgb[3], bool& _activated, bool _enabled)
  1814. {
  1815. char buf[128];
  1816. bx::snprintf(buf, sizeof(buf), "%s [RGB %-2.2f %-2.2f %-2.2f]"
  1817. , _text
  1818. , _rgb[0]
  1819. , _rgb[1]
  1820. , _rgb[2]
  1821. );
  1822. if (imguiButton(buf, true) )
  1823. {
  1824. _activated = !_activated;
  1825. }
  1826. if (_activated)
  1827. {
  1828. imguiColorWheel(_rgb, false, _enabled);
  1829. }
  1830. }
  1831. void imguiImage(bgfx::TextureHandle _image, float _lod, int32_t _width, int32_t _height, ImguiImageAlign::Enum _align)
  1832. {
  1833. s_imgui.image(_image, _lod, _width, _height, _align);
  1834. }
  1835. void imguiImage(bgfx::TextureHandle _image, float _lod, float _width, float _aspect, ImguiImageAlign::Enum _align)
  1836. {
  1837. s_imgui.image(_image, _lod, _width, _aspect, _align);
  1838. }