imgui.cpp 58 KB

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