imgui.cpp 50 KB

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