imgui.cpp 63 KB

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