imgui.cpp 55 KB

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