imgui.cpp 51 KB

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