imgui.cpp 64 KB

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