imgui.cpp 55 KB

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