imgui.cpp 61 KB

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