imgui.cpp 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292
  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_cubemap.bin.h"
  37. #include "fs_imgui_cubemap.bin.h"
  38. #include "vs_imgui_image.bin.h"
  39. #include "fs_imgui_image.bin.h"
  40. #include "fs_imgui_image_swizz.bin.h"
  41. #define USE_NANOVG_FONT 0
  42. #define IMGUI_CONFIG_MAX_FONTS 20
  43. #define MAX_TEMP_COORDS 100
  44. #define NUM_CIRCLE_VERTS (8 * 4)
  45. static const int32_t BUTTON_HEIGHT = 20;
  46. static const int32_t SLIDER_HEIGHT = 20;
  47. static const int32_t SLIDER_MARKER_WIDTH = 10;
  48. static const int32_t CHECK_SIZE = 8;
  49. static const int32_t DEFAULT_SPACING = 4;
  50. static const int32_t TEXT_HEIGHT = 8;
  51. static const int32_t SCROLL_AREA_PADDING = 6;
  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 IMGUI_MIN(_a, _b) (_a)<(_b)?(_a):(_b)
  64. #define IMGUI_MAX(_a, _b) (_a)>(_b)?(_a):(_b)
  65. #define STBTT_malloc(_x, _y) imguiMalloc(_x, _y)
  66. #define STBTT_free(_x, _y) imguiFree(_x, _y)
  67. #define STB_TRUETYPE_IMPLEMENTATION
  68. #include <stb_truetype/stb_truetype.h>
  69. namespace
  70. {
  71. static uint32_t quad(uint16_t* _indices, uint16_t _idx0, uint16_t _idx1, uint16_t _idx2, uint16_t _idx3)
  72. {
  73. _indices[0] = _idx0;
  74. _indices[1] = _idx3;
  75. _indices[2] = _idx1;
  76. _indices[3] = _idx1;
  77. _indices[4] = _idx3;
  78. _indices[5] = _idx2;
  79. return 6;
  80. }
  81. float sign(float px, float py, float ax, float ay, float bx, float by)
  82. {
  83. return (px - bx) * (ay - by) - (ax - bx) * (py - by);
  84. }
  85. bool pointInTriangle(float px, float py, float ax, float ay, float bx, float by, float cx, float cy)
  86. {
  87. const bool b1 = sign(px, py, ax, ay, bx, by) < 0.0f;
  88. const bool b2 = sign(px, py, bx, by, cx, cy) < 0.0f;
  89. const bool b3 = sign(px, py, cx, cy, ax, ay) < 0.0f;
  90. return ( (b1 == b2) && (b2 == b3) );
  91. }
  92. void closestPointOnLine(float& ox, float &oy, float px, float py, float ax, float ay, float bx, float by)
  93. {
  94. float dx = px - ax;
  95. float dy = py - ay;
  96. float lx = bx - ax;
  97. float ly = by - ay;
  98. float len = sqrtf(lx*lx+ly*ly);
  99. // Normalize.
  100. float invLen = 1.0f/len;
  101. lx*=invLen;
  102. ly*=invLen;
  103. float dot = (dx*lx + dy*ly);
  104. if (dot < 0.0f)
  105. {
  106. ox = ax;
  107. oy = ay;
  108. }
  109. else if (dot > len)
  110. {
  111. ox = bx;
  112. oy = by;
  113. }
  114. else
  115. {
  116. ox = ax + lx*dot;
  117. oy = ay + ly*dot;
  118. }
  119. }
  120. void closestPointOnTriangle(float& ox, float &oy, float px, float py, float ax, float ay, float bx, float by, float cx, float cy)
  121. {
  122. float abx, aby;
  123. float bcx, bcy;
  124. float cax, cay;
  125. closestPointOnLine(abx, aby, px, py, ax, ay, bx, by);
  126. closestPointOnLine(bcx, bcy, px, py, bx, by, cx, cy);
  127. closestPointOnLine(cax, cay, px, py, cx, cy, ax, ay);
  128. const float pabx = px - abx;
  129. const float paby = py - aby;
  130. const float pbcx = px - bcx;
  131. const float pbcy = py - bcy;
  132. const float pcax = px - cax;
  133. const float pcay = py - cay;
  134. const float lab = sqrtf(pabx*pabx+paby*paby);
  135. const float lbc = sqrtf(pbcx*pbcx+pbcy*pbcy);
  136. const float lca = sqrtf(pcax*pcax+pcay*pcay);
  137. const float m = bx::fmin3(lab, lbc, lca);
  138. if (m == lab)
  139. {
  140. ox = abx;
  141. oy = aby;
  142. }
  143. else if (m == lbc)
  144. {
  145. ox = bcx;
  146. oy = bcy;
  147. }
  148. else// if (m == lca).
  149. {
  150. ox = cax;
  151. oy = cay;
  152. }
  153. }
  154. inline float vec2Dot(const float* __restrict _a, const float* __restrict _b)
  155. {
  156. return _a[0]*_b[0] + _a[1]*_b[1];
  157. }
  158. void barycentric(float& _u, float& _v, float& _w
  159. , float _ax, float _ay
  160. , float _bx, float _by
  161. , float _cx, float _cy
  162. , float _px, float _py
  163. )
  164. {
  165. const float v0[2] = { _bx - _ax, _by - _ay };
  166. const float v1[2] = { _cx - _ax, _cy - _ay };
  167. const float v2[2] = { _px - _ax, _py - _ay };
  168. const float d00 = vec2Dot(v0, v0);
  169. const float d01 = vec2Dot(v0, v1);
  170. const float d11 = vec2Dot(v1, v1);
  171. const float d20 = vec2Dot(v2, v0);
  172. const float d21 = vec2Dot(v2, v1);
  173. const float denom = d00 * d11 - d01 * d01;
  174. _v = (d11 * d20 - d01 * d21) / denom;
  175. _w = (d00 * d21 - d01 * d20) / denom;
  176. _u = 1.0f - _v - _w;
  177. }
  178. struct PosColorVertex
  179. {
  180. float m_x;
  181. float m_y;
  182. uint32_t m_abgr;
  183. static void init()
  184. {
  185. ms_decl
  186. .begin()
  187. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  188. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  189. .end();
  190. }
  191. static bgfx::VertexDecl ms_decl;
  192. };
  193. bgfx::VertexDecl PosColorVertex::ms_decl;
  194. struct PosColorUvVertex
  195. {
  196. float m_x;
  197. float m_y;
  198. float m_u;
  199. float m_v;
  200. uint32_t m_abgr;
  201. static void init()
  202. {
  203. ms_decl
  204. .begin()
  205. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  206. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  207. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  208. .end();
  209. }
  210. static bgfx::VertexDecl ms_decl;
  211. };
  212. bgfx::VertexDecl PosColorUvVertex::ms_decl;
  213. struct PosUvVertex
  214. {
  215. float m_x;
  216. float m_y;
  217. float m_u;
  218. float m_v;
  219. static void init()
  220. {
  221. ms_decl
  222. .begin()
  223. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  224. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  225. .end();
  226. }
  227. static bgfx::VertexDecl ms_decl;
  228. };
  229. bgfx::VertexDecl PosUvVertex::ms_decl;
  230. struct PosNormalVertex
  231. {
  232. float m_x;
  233. float m_y;
  234. float m_z;
  235. float m_nx;
  236. float m_ny;
  237. float m_nz;
  238. static void init()
  239. {
  240. ms_decl.begin()
  241. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  242. .add(bgfx::Attrib::Normal, 3, bgfx::AttribType::Float)
  243. .end();
  244. }
  245. void set(float _x, float _y, float _z, float _nx, float _ny, float _nz)
  246. {
  247. m_x = _x;
  248. m_y = _y;
  249. m_z = _z;
  250. m_nx = _nx;
  251. m_ny = _ny;
  252. m_nz = _nz;
  253. }
  254. static bgfx::VertexDecl ms_decl;
  255. };
  256. bgfx::VertexDecl PosNormalVertex::ms_decl;
  257. } // namespace
  258. #if !USE_NANOVG_FONT
  259. static float getTextLength(stbtt_bakedchar* _chardata, const char* _text, uint32_t& _numVertices)
  260. {
  261. float xpos = 0;
  262. float len = 0;
  263. uint32_t numVertices = 0;
  264. while (*_text)
  265. {
  266. int32_t ch = (uint8_t)*_text;
  267. if (ch == '\t')
  268. {
  269. for (int32_t ii = 0; ii < 4; ++ii)
  270. {
  271. if (xpos < s_tabStops[ii])
  272. {
  273. xpos = s_tabStops[ii];
  274. break;
  275. }
  276. }
  277. }
  278. else if (ch >= ' '
  279. && ch < 128)
  280. {
  281. stbtt_bakedchar* b = _chardata + ch - ' ';
  282. int32_t round_x = STBTT_ifloor( (xpos + b->xoff) + 0.5);
  283. len = round_x + b->x1 - b->x0 + 0.5f;
  284. xpos += b->xadvance;
  285. numVertices += 6;
  286. }
  287. ++_text;
  288. }
  289. _numVertices = numVertices;
  290. return len;
  291. }
  292. #endif // !USE_NANOVG_FONT
  293. struct Imgui
  294. {
  295. Imgui()
  296. : m_mx(-1)
  297. , m_my(-1)
  298. , m_scroll(0)
  299. , m_active(0)
  300. , m_hot(0)
  301. , m_hotToBe(0)
  302. , m_dragX(0)
  303. , m_dragY(0)
  304. , m_dragOrig(0)
  305. , m_left(false)
  306. , m_leftPressed(false)
  307. , m_leftReleased(false)
  308. , m_isHot(false)
  309. , m_wentActive(false)
  310. , m_insideArea(false)
  311. , m_isActivePresent(false)
  312. , m_checkActivePresence(false)
  313. , m_widgetId(0)
  314. , m_enabledAreaIds(0)
  315. , m_textureWidth(512)
  316. , m_textureHeight(512)
  317. , m_halfTexel(0.0f)
  318. , m_nvg(NULL)
  319. , m_view(31)
  320. , m_viewWidth(0)
  321. , m_viewHeight(0)
  322. , m_currentFontIdx(0)
  323. {
  324. m_areaId.reset();
  325. m_invTextureWidth = 1.0f/m_textureWidth;
  326. m_invTextureHeight = 1.0f/m_textureHeight;
  327. u_imageLod.idx = bgfx::invalidHandle;
  328. u_imageSwizzle.idx = bgfx::invalidHandle;
  329. s_texColor.idx = bgfx::invalidHandle;
  330. m_missingTexture.idx = bgfx::invalidHandle;
  331. m_colorProgram.idx = bgfx::invalidHandle;
  332. m_textureProgram.idx = bgfx::invalidHandle;
  333. m_cubeMapProgram.idx = bgfx::invalidHandle;
  334. m_imageProgram.idx = bgfx::invalidHandle;
  335. m_imageSwizzProgram.idx = bgfx::invalidHandle;
  336. }
  337. ImguiFontHandle createFont(const void* _data, float _fontSize)
  338. {
  339. #if !USE_NANOVG_FONT
  340. const ImguiFontHandle handle = { m_fontHandle.alloc() };
  341. const bgfx::Memory* mem = bgfx::alloc(m_textureWidth * m_textureHeight);
  342. stbtt_BakeFontBitmap( (uint8_t*)_data, 0, _fontSize, mem->data, m_textureWidth, m_textureHeight, 32, 96, m_fonts[handle.idx].m_cdata);
  343. m_fonts[handle.idx].m_texture = bgfx::createTexture2D(m_textureWidth, m_textureHeight, 1, bgfx::TextureFormat::R8, BGFX_TEXTURE_NONE, mem);
  344. m_fonts[handle.idx].m_size = _fontSize;
  345. #else
  346. const ImguiFontHandle handle = { bgfx::invalidHandle };
  347. #endif // !USE_NANOVG_FONT
  348. return handle;
  349. }
  350. void setFont(ImguiFontHandle _handle)
  351. {
  352. if (isValid(_handle) )
  353. {
  354. m_currentFontIdx = _handle.idx;
  355. }
  356. }
  357. bgfx::TextureHandle genMissingTexture(uint32_t _width, uint32_t _height, float _lineWidth = 0.02f)
  358. {
  359. const bgfx::Memory* mem = bgfx::alloc(_width*_height*4);
  360. uint32_t* bgra8 = (uint32_t*)mem->data;
  361. const float sx = 0.70710677f;
  362. const float cx = 0.70710677f;
  363. for (uint32_t yy = 0; yy < _height; ++yy)
  364. {
  365. for (uint32_t xx = 0; xx < _width; ++xx)
  366. {
  367. float px = xx / float(_width) * 2.0f - 1.0f;
  368. float py = yy / float(_height) * 2.0f - 1.0f;
  369. float sum = bx::fpulse(px * cx - py * sx, _lineWidth, -_lineWidth)
  370. + bx::fpulse(px * sx + py * cx, _lineWidth, -_lineWidth)
  371. ;
  372. *bgra8++ = sum >= 1.0f ? 0xffff0000 : 0xffffffff;
  373. }
  374. }
  375. return bgfx::createTexture2D(_width, _height, 0, bgfx::TextureFormat::BGRA8, 0, mem);
  376. }
  377. ImguiFontHandle create(const void* _data, float _fontSize)
  378. {
  379. m_nvg = nvgCreate(512, 512, 1, m_view);
  380. nvgCreateFontMem(m_nvg, "default", (unsigned char*)_data, INT32_MAX, 0);
  381. nvgFontSize(m_nvg, _fontSize);
  382. nvgFontFace(m_nvg, "default");
  383. for (int32_t ii = 0; ii < NUM_CIRCLE_VERTS; ++ii)
  384. {
  385. float a = (float)ii / (float)NUM_CIRCLE_VERTS * (float)(M_PI * 2.0);
  386. m_circleVerts[ii * 2 + 0] = cosf(a);
  387. m_circleVerts[ii * 2 + 1] = sinf(a);
  388. }
  389. PosColorVertex::init();
  390. PosColorUvVertex::init();
  391. PosUvVertex::init();
  392. PosNormalVertex::init();
  393. u_imageLod = bgfx::createUniform("u_imageLod", bgfx::UniformType::Uniform1f);
  394. u_imageSwizzle = bgfx::createUniform("u_swizzle", bgfx::UniformType::Uniform4fv);
  395. s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Uniform1i);
  396. const bgfx::Memory* vs_imgui_color;
  397. const bgfx::Memory* fs_imgui_color;
  398. const bgfx::Memory* vs_imgui_texture;
  399. const bgfx::Memory* fs_imgui_texture;
  400. const bgfx::Memory* vs_imgui_cubemap;
  401. const bgfx::Memory* fs_imgui_cubemap;
  402. const bgfx::Memory* vs_imgui_image;
  403. const bgfx::Memory* fs_imgui_image;
  404. const bgfx::Memory* fs_imgui_image_swizz;
  405. switch (bgfx::getRendererType() )
  406. {
  407. case bgfx::RendererType::Direct3D9:
  408. vs_imgui_color = bgfx::makeRef(vs_imgui_color_dx9, sizeof(vs_imgui_color_dx9) );
  409. fs_imgui_color = bgfx::makeRef(fs_imgui_color_dx9, sizeof(fs_imgui_color_dx9) );
  410. vs_imgui_texture = bgfx::makeRef(vs_imgui_texture_dx9, sizeof(vs_imgui_texture_dx9) );
  411. fs_imgui_texture = bgfx::makeRef(fs_imgui_texture_dx9, sizeof(fs_imgui_texture_dx9) );
  412. vs_imgui_cubemap = bgfx::makeRef(vs_imgui_cubemap_dx9, sizeof(vs_imgui_cubemap_dx9) );
  413. fs_imgui_cubemap = bgfx::makeRef(fs_imgui_cubemap_dx9, sizeof(fs_imgui_cubemap_dx9) );
  414. vs_imgui_image = bgfx::makeRef(vs_imgui_image_dx9, sizeof(vs_imgui_image_dx9) );
  415. fs_imgui_image = bgfx::makeRef(fs_imgui_image_dx9, sizeof(fs_imgui_image_dx9) );
  416. fs_imgui_image_swizz = bgfx::makeRef(fs_imgui_image_swizz_dx9, sizeof(fs_imgui_image_swizz_dx9) );
  417. m_halfTexel = 0.5f;
  418. break;
  419. case bgfx::RendererType::Direct3D11:
  420. vs_imgui_color = bgfx::makeRef(vs_imgui_color_dx11, sizeof(vs_imgui_color_dx11) );
  421. fs_imgui_color = bgfx::makeRef(fs_imgui_color_dx11, sizeof(fs_imgui_color_dx11) );
  422. vs_imgui_texture = bgfx::makeRef(vs_imgui_texture_dx11, sizeof(vs_imgui_texture_dx11) );
  423. fs_imgui_texture = bgfx::makeRef(fs_imgui_texture_dx11, sizeof(fs_imgui_texture_dx11) );
  424. vs_imgui_cubemap = bgfx::makeRef(vs_imgui_cubemap_dx11, sizeof(vs_imgui_cubemap_dx11) );
  425. fs_imgui_cubemap = bgfx::makeRef(fs_imgui_cubemap_dx11, sizeof(fs_imgui_cubemap_dx11) );
  426. vs_imgui_image = bgfx::makeRef(vs_imgui_image_dx11, sizeof(vs_imgui_image_dx11) );
  427. fs_imgui_image = bgfx::makeRef(fs_imgui_image_dx11, sizeof(fs_imgui_image_dx11) );
  428. fs_imgui_image_swizz = bgfx::makeRef(fs_imgui_image_swizz_dx11, sizeof(fs_imgui_image_swizz_dx11) );
  429. break;
  430. default:
  431. vs_imgui_color = bgfx::makeRef(vs_imgui_color_glsl, sizeof(vs_imgui_color_glsl) );
  432. fs_imgui_color = bgfx::makeRef(fs_imgui_color_glsl, sizeof(fs_imgui_color_glsl) );
  433. vs_imgui_texture = bgfx::makeRef(vs_imgui_texture_glsl, sizeof(vs_imgui_texture_glsl) );
  434. fs_imgui_texture = bgfx::makeRef(fs_imgui_texture_glsl, sizeof(fs_imgui_texture_glsl) );
  435. vs_imgui_cubemap = bgfx::makeRef(vs_imgui_cubemap_glsl, sizeof(vs_imgui_cubemap_glsl) );
  436. fs_imgui_cubemap = bgfx::makeRef(fs_imgui_cubemap_glsl, sizeof(fs_imgui_cubemap_glsl) );
  437. vs_imgui_image = bgfx::makeRef(vs_imgui_image_glsl, sizeof(vs_imgui_image_glsl) );
  438. fs_imgui_image = bgfx::makeRef(fs_imgui_image_glsl, sizeof(fs_imgui_image_glsl) );
  439. fs_imgui_image_swizz = bgfx::makeRef(fs_imgui_image_swizz_glsl, sizeof(fs_imgui_image_swizz_glsl) );
  440. break;
  441. }
  442. bgfx::ShaderHandle vsh;
  443. bgfx::ShaderHandle fsh;
  444. vsh = bgfx::createShader(vs_imgui_color);
  445. fsh = bgfx::createShader(fs_imgui_color);
  446. m_colorProgram = bgfx::createProgram(vsh, fsh);
  447. bgfx::destroyShader(vsh);
  448. bgfx::destroyShader(fsh);
  449. vsh = bgfx::createShader(vs_imgui_texture);
  450. fsh = bgfx::createShader(fs_imgui_texture);
  451. m_textureProgram = bgfx::createProgram(vsh, fsh);
  452. bgfx::destroyShader(vsh);
  453. bgfx::destroyShader(fsh);
  454. vsh = bgfx::createShader(vs_imgui_cubemap);
  455. fsh = bgfx::createShader(fs_imgui_cubemap);
  456. m_cubeMapProgram = bgfx::createProgram(vsh, fsh);
  457. bgfx::destroyShader(vsh);
  458. bgfx::destroyShader(fsh);
  459. vsh = bgfx::createShader(vs_imgui_image);
  460. fsh = bgfx::createShader(fs_imgui_image);
  461. m_imageProgram = bgfx::createProgram(vsh, fsh);
  462. bgfx::destroyShader(fsh);
  463. // Notice: using the same vsh.
  464. fsh = bgfx::createShader(fs_imgui_image_swizz);
  465. m_imageSwizzProgram = bgfx::createProgram(vsh, fsh);
  466. bgfx::destroyShader(fsh);
  467. bgfx::destroyShader(vsh);
  468. m_missingTexture = genMissingTexture(256, 256, 0.04f);
  469. #if !USE_NANOVG_FONT
  470. const ImguiFontHandle handle = createFont(_data, _fontSize);
  471. m_currentFontIdx = handle.idx;
  472. #else
  473. const ImguiFontHandle handle = { bgfx::invalidHandle };
  474. #endif // !USE_NANOVG_FONT
  475. return handle;
  476. }
  477. void destroy()
  478. {
  479. bgfx::destroyUniform(u_imageLod);
  480. bgfx::destroyUniform(u_imageSwizzle);
  481. bgfx::destroyUniform(s_texColor);
  482. #if !USE_NANOVG_FONT
  483. for (uint16_t ii = 0; ii < IMGUI_CONFIG_MAX_FONTS; ++ii)
  484. {
  485. if (bgfx::isValid(m_fonts[ii].m_texture) )
  486. {
  487. bgfx::destroyTexture(m_fonts[ii].m_texture);
  488. }
  489. }
  490. #endif // !USE_NANOVG_FONT
  491. bgfx::destroyTexture(m_missingTexture);
  492. bgfx::destroyProgram(m_colorProgram);
  493. bgfx::destroyProgram(m_textureProgram);
  494. bgfx::destroyProgram(m_cubeMapProgram);
  495. bgfx::destroyProgram(m_imageProgram);
  496. bgfx::destroyProgram(m_imageSwizzProgram);
  497. nvgDelete(m_nvg);
  498. }
  499. bool anyActive() const
  500. {
  501. return m_active != 0;
  502. }
  503. inline void updatePresence(uint32_t _id)
  504. {
  505. if (m_checkActivePresence && m_active == _id)
  506. {
  507. m_isActivePresent = true;
  508. }
  509. }
  510. uint32_t getId()
  511. {
  512. const uint32_t id = (m_areaId << 16) | m_widgetId++;
  513. updatePresence(id);
  514. return id;
  515. }
  516. bool isActive(uint32_t _id) const
  517. {
  518. return m_active == _id;
  519. }
  520. bool isActiveInputField(uint32_t _id) const
  521. {
  522. return m_inputField == _id;
  523. }
  524. bool isHot(uint32_t _id) const
  525. {
  526. return m_hot == _id;
  527. }
  528. bool inRect(int32_t _x, int32_t _y, int32_t _width, int32_t _height, bool _checkScroll = true) const
  529. {
  530. return (!_checkScroll || m_areas[m_areaId].m_inside)
  531. && m_mx >= _x
  532. && m_mx <= _x + _width
  533. && m_my >= _y
  534. && m_my <= _y + _height;
  535. }
  536. bool isEnabled(uint16_t _areaId)
  537. {
  538. return (m_enabledAreaIds>>_areaId)&0x1;
  539. }
  540. void setEnabled(uint16_t _areaId)
  541. {
  542. m_enabledAreaIds |= (UINT64_C(1)<<_areaId);
  543. }
  544. void clearInput()
  545. {
  546. m_leftPressed = false;
  547. m_leftReleased = false;
  548. m_scroll = 0;
  549. }
  550. void clearActive()
  551. {
  552. m_active = 0;
  553. // mark all UI for this frame as processed
  554. clearInput();
  555. }
  556. void clearActiveInputField()
  557. {
  558. m_inputField = 0;
  559. }
  560. void setActive(uint32_t _id)
  561. {
  562. m_active = _id;
  563. m_wentActive = true;
  564. m_inputField = 0;
  565. }
  566. void setActiveInputField(uint32_t _id)
  567. {
  568. m_inputField = _id;
  569. }
  570. void setHot(uint32_t _id)
  571. {
  572. m_hotToBe = _id;
  573. }
  574. bool buttonLogic(uint32_t _id, bool _over)
  575. {
  576. bool res = false;
  577. // process down
  578. if (!anyActive() )
  579. {
  580. if (_over)
  581. {
  582. setHot(_id);
  583. }
  584. if (isHot(_id)
  585. && m_leftPressed)
  586. {
  587. setActive(_id);
  588. }
  589. }
  590. // if button is active, then react on left up
  591. if (isActive(_id) )
  592. {
  593. if (_over)
  594. {
  595. setHot(_id);
  596. }
  597. if (m_leftReleased)
  598. {
  599. if (isHot(_id) )
  600. {
  601. res = true;
  602. }
  603. clearActive();
  604. }
  605. }
  606. if (isHot(_id) )
  607. {
  608. m_isHot = true;
  609. }
  610. return res;
  611. }
  612. void inputLogic(uint32_t _id, bool _over)
  613. {
  614. if (!anyActive() )
  615. {
  616. if (_over)
  617. {
  618. setHot(_id);
  619. }
  620. if (isHot(_id)
  621. && m_leftPressed)
  622. {
  623. // Toggle active input.
  624. if (isActiveInputField(_id))
  625. {
  626. clearActiveInputField();
  627. }
  628. else
  629. {
  630. setActiveInputField(_id);
  631. }
  632. }
  633. }
  634. if (isHot(_id) )
  635. {
  636. m_isHot = true;
  637. }
  638. if (m_leftPressed
  639. && !m_isHot
  640. && m_inputField != 0)
  641. {
  642. clearActiveInputField();
  643. }
  644. }
  645. void updateInput(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, char _inputChar)
  646. {
  647. bool left = (_button & IMGUI_MBUT_LEFT) != 0;
  648. m_mx = _mx;
  649. m_my = _my;
  650. m_leftPressed = !m_left && left;
  651. m_leftReleased = m_left && !left;
  652. m_left = left;
  653. m_scroll = _scroll;
  654. _inputChar = _inputChar & 0x7f; // ASCII or GTFO! :)
  655. m_lastChar = m_char;
  656. m_char = _inputChar;
  657. }
  658. 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)
  659. {
  660. nvgBeginFrame(m_nvg, _width, _height, 1.0f, NVG_STRAIGHT_ALPHA);
  661. m_view = _view;
  662. m_viewWidth = _width;
  663. m_viewHeight = _height;
  664. bgfx::setViewSeq(_view, true);
  665. bgfx::setViewRect(_view, 0, 0, _width, _height);
  666. float proj[16];
  667. bx::mtxOrtho(proj, 0.0f, (float)_width, (float)_height, 0.0f, 0.0f, 1000.0f);
  668. bgfx::setViewTransform(_view, NULL, proj);
  669. updateInput(_mx, _my, _button, _scroll, _inputChar);
  670. m_hot = m_hotToBe;
  671. m_hotToBe = 0;
  672. m_wentActive = false;
  673. m_isHot = false;
  674. Area& area = getCurrentArea();
  675. area.m_widgetX = 0;
  676. area.m_widgetY = 0;
  677. area.m_widgetW = 0;
  678. m_areaId.reset();
  679. m_widgetId = 0;
  680. m_enabledAreaIds = 0;
  681. m_insideArea = false;
  682. m_isActivePresent = false;
  683. }
  684. void endFrame()
  685. {
  686. if (m_checkActivePresence && !m_isActivePresent)
  687. {
  688. // The ui element is not present any more, reset active field.
  689. m_active = 0;
  690. }
  691. m_checkActivePresence = (0 != m_active);
  692. clearInput();
  693. nvgEndFrame(m_nvg);
  694. }
  695. bool beginScroll(int32_t _height, int32_t* _scroll, bool _enabled)
  696. {
  697. Area& parentArea = getCurrentArea();
  698. m_areaId.next();
  699. const uint32_t scrollId = getId();
  700. Area& area = getCurrentArea();
  701. const uint16_t parentBottom = parentArea.m_scissorY + parentArea.m_scissorHeight;
  702. const uint16_t childBottom = parentArea.m_widgetY + _height;
  703. const uint16_t bottom = IMGUI_MIN(childBottom, parentBottom);
  704. const uint16_t top = IMGUI_MAX(parentArea.m_widgetY, parentArea.m_scissorY);
  705. area.m_contentX = parentArea.m_contentX;
  706. area.m_contentY = parentArea.m_widgetY;
  707. area.m_contentWidth = parentArea.m_contentWidth - (SCROLL_AREA_PADDING*3);
  708. area.m_contentHeight = _height;
  709. area.m_widgetX = parentArea.m_widgetX;
  710. area.m_widgetY = parentArea.m_widgetY + (*_scroll);
  711. area.m_widgetW = parentArea.m_widgetW - (SCROLL_AREA_PADDING*3);
  712. area.m_scissorX = area.m_contentX;
  713. area.m_scissorWidth = area.m_contentWidth;
  714. area.m_scissorY = top;
  715. area.m_scissorHeight = bottom - top;
  716. area.m_scissorEnabled = true;
  717. area.m_height = _height;
  718. area.m_scrollVal = _scroll;
  719. area.m_scrollId = scrollId;
  720. area.m_inside = inRect(parentArea.m_scissorX
  721. , area.m_scissorY
  722. , parentArea.m_scissorWidth
  723. , area.m_scissorHeight
  724. , false
  725. );
  726. parentArea.m_widgetY += (_height + DEFAULT_SPACING);
  727. if (_enabled)
  728. {
  729. setEnabled(m_areaId);
  730. }
  731. m_insideArea |= area.m_inside;
  732. return area.m_inside;
  733. }
  734. void endScroll(int32_t _r)
  735. {
  736. Area& area = getCurrentArea();
  737. area.m_scissorEnabled = false;
  738. const int32_t xx = area.m_contentX + area.m_contentWidth - 1;
  739. const int32_t yy = area.m_contentY;
  740. const int32_t width = SCROLL_AREA_PADDING * 2;
  741. const int32_t height = area.m_height;
  742. const int32_t stop = area.m_contentY + (*area.m_scrollVal);
  743. const int32_t sbot = area.m_widgetY - DEFAULT_SPACING;
  744. const int32_t sh = IMGUI_MAX(1, sbot - stop); // The scrollable area height.
  745. const float barHeight = (float)height / (float)sh;
  746. const int32_t diff = height - sh;
  747. if (diff < 0)
  748. {
  749. *area.m_scrollVal = (*area.m_scrollVal > diff) ? *area.m_scrollVal : diff;
  750. }
  751. else
  752. {
  753. *area.m_scrollVal = 0;
  754. }
  755. if (barHeight < 1.0f)
  756. {
  757. float barY = bx::fsaturate( (float)(yy - stop) / (float)sh);
  758. // Handle scroll bar logic.
  759. const uint32_t hid = area.m_scrollId;
  760. const int32_t hx = xx;
  761. const int32_t hy = yy + (int)(barY * height);
  762. const int32_t hw = width;
  763. const int32_t hh = (int)(barHeight * height);
  764. const int32_t range = height - (hh - 1);
  765. const bool over = inRect(hx, hy, hw, hh);
  766. buttonLogic(hid, over);
  767. if (isActive(hid) )
  768. {
  769. float uu = (float)(hy - yy) / (float)range;
  770. if (m_wentActive)
  771. {
  772. m_dragY = m_my;
  773. m_dragOrig = uu;
  774. }
  775. if (m_dragY != m_my)
  776. {
  777. uu = bx::fsaturate(m_dragOrig + (m_my - m_dragY) / (float)range);
  778. *area.m_scrollVal = (int)(uu * (height - sh) );
  779. }
  780. }
  781. // BG
  782. if (0 == _r)
  783. {
  784. drawRect( (float)xx
  785. , (float)yy
  786. , (float)width
  787. , (float)height
  788. , imguiRGBA(0, 0, 0, 196)
  789. );
  790. }
  791. else
  792. {
  793. drawRoundedRect( (float)xx
  794. , (float)yy
  795. , (float)width
  796. , (float)height
  797. , (float)_r
  798. , imguiRGBA(0, 0, 0, 196)
  799. );
  800. }
  801. // Bar
  802. if (isActive(hid) )
  803. {
  804. if (0 == _r)
  805. {
  806. drawRect( (float)hx
  807. , (float)hy
  808. , (float)hw
  809. , (float)hh
  810. , imguiRGBA(255, 196, 0, 196)
  811. );
  812. }
  813. else
  814. {
  815. drawRoundedRect( (float)hx
  816. , (float)hy
  817. , (float)hw
  818. , (float)hh
  819. , (float)_r
  820. , imguiRGBA(255, 196, 0, 196)
  821. );
  822. }
  823. }
  824. else
  825. {
  826. if (0 == _r)
  827. {
  828. drawRect( (float)hx
  829. , (float)hy
  830. , (float)hw
  831. , (float)hh
  832. , isHot(hid) ? imguiRGBA(255, 196, 0, 96) : imguiRGBA(255, 255, 255, 64)
  833. );
  834. }
  835. else
  836. {
  837. drawRoundedRect( (float)hx
  838. , (float)hy
  839. , (float)hw
  840. , (float)hh
  841. , (float)_r
  842. , isHot(hid) ? imguiRGBA(255, 196, 0, 96) : imguiRGBA(255, 255, 255, 64)
  843. );
  844. }
  845. }
  846. // Handle mouse scrolling.
  847. if (area.m_inside) // && !anyActive() )
  848. {
  849. if (m_scroll)
  850. {
  851. *area.m_scrollVal += bx::uint32_clamp(20 * m_scroll, 0, sh - height);
  852. }
  853. }
  854. }
  855. area.m_inside = false;
  856. m_areaId.previous();
  857. }
  858. bool beginArea(const char* _name, int32_t _x, int32_t _y, int32_t _width, int32_t _height, bool _enabled, int32_t _r)
  859. {
  860. m_areaId.next();
  861. const uint32_t scrollId = getId();
  862. const bool hasTitle = (NULL != _name && '\0' != _name[0]);
  863. const int32_t header = hasTitle ? AREA_HEADER : 0;
  864. Area& area = getCurrentArea();
  865. area.m_x = _x;
  866. area.m_y = _y;
  867. area.m_width = _width;
  868. area.m_height = _height;
  869. area.m_contentX = area.m_x + SCROLL_AREA_PADDING;
  870. area.m_contentY = area.m_y + SCROLL_AREA_PADDING + header;
  871. area.m_contentWidth = area.m_width - SCROLL_AREA_PADDING;
  872. area.m_contentHeight = area.m_height - SCROLL_AREA_PADDING*2 - header;
  873. area.m_scissorX = area.m_contentX;
  874. area.m_scissorY = area.m_y + SCROLL_AREA_PADDING + header;
  875. area.m_scissorHeight = area.m_height - SCROLL_AREA_PADDING*2 - header;
  876. area.m_scissorWidth = area.m_contentWidth;
  877. area.m_scissorEnabled = false;
  878. area.m_widgetX = area.m_contentX;
  879. area.m_widgetY = area.m_contentY;
  880. area.m_widgetW = area.m_width - SCROLL_AREA_PADDING*2;
  881. static int32_t s_zeroScroll = 0;
  882. area.m_scrollVal = &s_zeroScroll;
  883. area.m_scrollId = scrollId;
  884. area.m_inside = inRect(area.m_scissorX, area.m_scissorY, area.m_scissorWidth, area.m_scissorHeight, false);
  885. if (_enabled)
  886. {
  887. setEnabled(m_areaId);
  888. }
  889. if (0 == _r)
  890. {
  891. drawRect( (float)_x
  892. , (float)_y
  893. , (float)_width + 0.3f /*border fix for seamlessly joining two scroll areas*/
  894. , (float)_height + 0.3f /*border fix for seamlessly joining two scroll areas*/
  895. , imguiRGBA(0, 0, 0, 192)
  896. );
  897. }
  898. else
  899. {
  900. drawRoundedRect( (float)_x
  901. , (float)_y
  902. , (float)_width
  903. , (float)_height
  904. , (float)_r
  905. , imguiRGBA(0, 0, 0, 192)
  906. );
  907. }
  908. if (hasTitle)
  909. {
  910. drawText(_x + 10
  911. , _y + 18
  912. , ImguiTextAlign::Left
  913. , _name
  914. , imguiRGBA(255, 255, 255, 128)
  915. );
  916. }
  917. nvgScissor(m_nvg
  918. , float(area.m_x)
  919. , float(area.m_y-1)
  920. , float(area.m_width)
  921. , float(area.m_height+1)
  922. );
  923. area.m_scissorEnabled = true;
  924. m_insideArea |= area.m_inside;
  925. return area.m_inside;
  926. }
  927. void endArea()
  928. {
  929. nvgResetScissor(m_nvg);
  930. }
  931. bool button(const char* _text, bool _enabled, ImguiAlign::Enum _align, uint32_t _rgb0, int32_t _r)
  932. {
  933. const uint32_t id = getId();
  934. Area& area = getCurrentArea();
  935. const int32_t yy = area.m_widgetY;
  936. const int32_t height = BUTTON_HEIGHT;
  937. area.m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  938. int32_t xx;
  939. int32_t width;
  940. if (ImguiAlign::Left == _align)
  941. {
  942. xx = area.m_contentX + SCROLL_AREA_PADDING;
  943. width = area.m_widgetW;
  944. }
  945. else if (ImguiAlign::LeftIndented == _align
  946. || ImguiAlign::Right == _align)
  947. {
  948. xx = area.m_widgetX;
  949. width = area.m_widgetW-1; //TODO: -1 !
  950. }
  951. else //if (ImguiAlign::Center == _align
  952. //|| ImguiAlign::CenterIndented == _align).
  953. {
  954. xx = area.m_widgetX;
  955. width = area.m_widgetW - (area.m_widgetX-area.m_scissorX);
  956. }
  957. const bool enabled = _enabled && isEnabled(m_areaId);
  958. const bool over = enabled && inRect(xx, yy, width, height);
  959. const bool res = buttonLogic(id, over);
  960. const uint32_t rgb0 = _rgb0&0x00ffffff;
  961. if (0 == _r)
  962. {
  963. drawRect( (float)xx
  964. , (float)yy
  965. , (float)width
  966. , (float)height
  967. , rgb0 | imguiRGBA(0, 0, 0, isActive(id) ? 196 : 96)
  968. );
  969. }
  970. else
  971. {
  972. drawRoundedRect( (float)xx
  973. , (float)yy
  974. , (float)width
  975. , (float)height
  976. , (float)_r
  977. , rgb0 | imguiRGBA(0, 0, 0, isActive(id) ? 196 : 96)
  978. );
  979. }
  980. if (enabled)
  981. {
  982. drawText(xx + BUTTON_HEIGHT / 2
  983. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  984. , ImguiTextAlign::Left
  985. , _text
  986. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  987. );
  988. }
  989. else
  990. {
  991. drawText(xx + BUTTON_HEIGHT / 2
  992. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  993. , ImguiTextAlign::Left
  994. , _text
  995. , imguiRGBA(128, 128, 128, 200)
  996. );
  997. }
  998. return res;
  999. }
  1000. bool item(const char* _text, bool _enabled)
  1001. {
  1002. const uint32_t id = getId();
  1003. Area& area = getCurrentArea();
  1004. const int32_t xx = area.m_widgetX;
  1005. const int32_t yy = area.m_widgetY;
  1006. const int32_t width = area.m_widgetW;
  1007. const int32_t height = BUTTON_HEIGHT;
  1008. area.m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  1009. const bool enabled = _enabled && isEnabled(m_areaId);
  1010. const bool over = enabled && inRect(xx, yy, width, height);
  1011. const bool res = buttonLogic(id, over);
  1012. if (isHot(id) )
  1013. {
  1014. drawRoundedRect( (float)xx
  1015. , (float)yy
  1016. , (float)width
  1017. , (float)height
  1018. , 2.0f
  1019. , imguiRGBA(255, 196, 0, isActive(id) ? 196 : 96)
  1020. );
  1021. }
  1022. if (enabled)
  1023. {
  1024. drawText(xx + BUTTON_HEIGHT / 2
  1025. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1026. , ImguiTextAlign::Left
  1027. , _text
  1028. , imguiRGBA(255, 255, 255, 200)
  1029. );
  1030. }
  1031. else
  1032. {
  1033. drawText(xx + BUTTON_HEIGHT / 2
  1034. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1035. , ImguiTextAlign::Left
  1036. , _text
  1037. , imguiRGBA(128, 128, 128, 200)
  1038. );
  1039. }
  1040. return res;
  1041. }
  1042. bool check(const char* _text, bool _checked, bool _enabled)
  1043. {
  1044. const uint32_t id = getId();
  1045. Area& area = getCurrentArea();
  1046. const int32_t xx = area.m_widgetX;
  1047. const int32_t yy = area.m_widgetY;
  1048. const int32_t width = area.m_widgetW;
  1049. const int32_t height = BUTTON_HEIGHT;
  1050. area.m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  1051. const bool enabled = _enabled && isEnabled(m_areaId);
  1052. const bool over = enabled && inRect(xx, yy, width, height);
  1053. const bool res = buttonLogic(id, over);
  1054. const int32_t cx = xx + BUTTON_HEIGHT / 2 - CHECK_SIZE / 2;
  1055. const int32_t cy = yy + BUTTON_HEIGHT / 2 - CHECK_SIZE / 2;
  1056. drawRoundedRect( (float)cx - 3
  1057. , (float)cy - 3
  1058. , (float)CHECK_SIZE + 6
  1059. , (float)CHECK_SIZE + 6
  1060. , 4
  1061. , imguiRGBA(128, 128, 128, isActive(id) ? 196 : 96)
  1062. );
  1063. if (_checked)
  1064. {
  1065. if (enabled)
  1066. {
  1067. drawRoundedRect( (float)cx
  1068. , (float)cy
  1069. , (float)CHECK_SIZE
  1070. , (float)CHECK_SIZE
  1071. , (float)CHECK_SIZE / 2 - 1
  1072. , imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200)
  1073. );
  1074. }
  1075. else
  1076. {
  1077. drawRoundedRect( (float)cx
  1078. , (float)cy
  1079. , (float)CHECK_SIZE
  1080. , (float)CHECK_SIZE
  1081. , (float)CHECK_SIZE / 2 - 1
  1082. , imguiRGBA(128, 128, 128, 200)
  1083. );
  1084. }
  1085. }
  1086. if (enabled)
  1087. {
  1088. drawText(xx + BUTTON_HEIGHT
  1089. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1090. , ImguiTextAlign::Left
  1091. , _text
  1092. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  1093. );
  1094. }
  1095. else
  1096. {
  1097. drawText(xx + BUTTON_HEIGHT
  1098. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1099. , ImguiTextAlign::Left
  1100. , _text
  1101. , imguiRGBA(128, 128, 128, 200)
  1102. );
  1103. }
  1104. return res;
  1105. }
  1106. void input(const char* _label, char* _str, uint32_t _len, bool _enabled, ImguiAlign::Enum _align, int32_t _r)
  1107. {
  1108. const uint32_t id = getId();
  1109. Area& area = getCurrentArea();
  1110. const int32_t yy = area.m_widgetY;
  1111. area.m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  1112. int32_t xx;
  1113. int32_t width;
  1114. if (ImguiAlign::Left == _align)
  1115. {
  1116. xx = area.m_contentX + SCROLL_AREA_PADDING;
  1117. width = area.m_widgetW;
  1118. }
  1119. else if (ImguiAlign::LeftIndented == _align
  1120. || ImguiAlign::Right == _align)
  1121. {
  1122. xx = area.m_widgetX;
  1123. width = area.m_widgetW-1; //TODO: -1 !
  1124. }
  1125. else //if (ImguiAlign::Center == _align
  1126. //|| ImguiAlign::CenterIndented == _align).
  1127. {
  1128. xx = area.m_widgetX;
  1129. width = area.m_widgetW - (area.m_widgetX-area.m_scissorX);
  1130. }
  1131. const bool drawLabel = (NULL != _label && _label[0] != '\0');
  1132. if (drawLabel)
  1133. {
  1134. drawText(xx
  1135. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1136. , ImguiTextAlign::Left
  1137. , _label
  1138. , imguiRGBA(255, 255, 255, 200)
  1139. );
  1140. }
  1141. // Handle input.
  1142. if (isActiveInputField(id) )
  1143. {
  1144. const size_t cursor = size_t(strlen(_str));
  1145. if (m_char == 0x08) //backspace
  1146. {
  1147. _str[cursor-1] = '\0';
  1148. }
  1149. else if (m_char == 0x0d || m_char == 0x1b) //enter or escape
  1150. {
  1151. clearActiveInputField();
  1152. }
  1153. else if (cursor < _len-1
  1154. && 0 != m_char)
  1155. {
  1156. _str[cursor] = m_char;
  1157. _str[cursor+1] = '\0';
  1158. }
  1159. }
  1160. // Draw input area.
  1161. const int32_t height = BUTTON_HEIGHT;
  1162. if (drawLabel)
  1163. {
  1164. uint32_t numVertices = 0; //unused
  1165. const int32_t labelWidth = int32_t(getTextLength(m_fonts[m_currentFontIdx].m_cdata, _label, numVertices));
  1166. xx += (labelWidth + 6);
  1167. width -= (labelWidth + 6);
  1168. }
  1169. const bool enabled = _enabled && isEnabled(m_areaId);
  1170. const bool over = enabled && inRect(xx, yy, width, height);
  1171. inputLogic(id, over);
  1172. if (0 == _r)
  1173. {
  1174. drawRect( (float)xx
  1175. , (float)yy
  1176. , (float)width
  1177. , (float)height
  1178. , isActiveInputField(id)?imguiRGBA(255,196,0,255):imguiRGBA(128,128,128,96)
  1179. );
  1180. }
  1181. else
  1182. {
  1183. drawRoundedRect( (float)xx
  1184. , (float)yy
  1185. , (float)width
  1186. , (float)height
  1187. , (float)_r
  1188. , isActiveInputField(id)?imguiRGBA(255,196,0,255):imguiRGBA(128,128,128,96)
  1189. );
  1190. }
  1191. if (isActiveInputField(id) )
  1192. {
  1193. drawText(xx + 6
  1194. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1195. , ImguiTextAlign::Left
  1196. , _str
  1197. , imguiRGBA(0, 0, 0, 255)
  1198. );
  1199. }
  1200. else
  1201. {
  1202. drawText(xx + 6
  1203. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1204. , ImguiTextAlign::Left
  1205. , _str
  1206. , isHot(id) ? imguiRGBA(255,196,0,255) : imguiRGBA(255,255,255,255)
  1207. );
  1208. }
  1209. }
  1210. uint8_t tabs(uint8_t _selected, bool _enabled, ImguiAlign::Enum _align, int32_t _height, int32_t _r, va_list _argList)
  1211. {
  1212. BX_UNUSED(_align);
  1213. uint8_t count;
  1214. const char* titles[16];
  1215. const char* str = va_arg(_argList, const char*);
  1216. for (count = 0; str != NULL || count >= 16; ++count, str = va_arg(_argList, const char*) )
  1217. {
  1218. titles[count] = str;
  1219. }
  1220. Area& area = getCurrentArea();
  1221. const int32_t yy = area.m_widgetY;
  1222. area.m_widgetY += _height + DEFAULT_SPACING;
  1223. int32_t xx;
  1224. int32_t width;
  1225. if (ImguiAlign::Left == _align)
  1226. {
  1227. xx = area.m_contentX + SCROLL_AREA_PADDING;
  1228. width = area.m_widgetW;
  1229. }
  1230. else if (ImguiAlign::LeftIndented == _align
  1231. || ImguiAlign::Right == _align)
  1232. {
  1233. xx = area.m_widgetX;
  1234. width = area.m_widgetW-1; //TODO: -1 !
  1235. }
  1236. else //if (ImguiAlign::Center == _align
  1237. //|| ImguiAlign::CenterIndented == _align).
  1238. {
  1239. xx = area.m_widgetX;
  1240. width = area.m_widgetW - (area.m_widgetX-area.m_scissorX);
  1241. }
  1242. uint8_t selected = _selected;
  1243. const int32_t tabWidth = width / count;
  1244. const int32_t tabWidthHalf = width / (count*2);
  1245. const int32_t textY = yy + _height/2 + int32_t(m_fonts[m_currentFontIdx].m_size)/2 - 2;
  1246. if (0 == _r)
  1247. {
  1248. drawRect( (float)xx
  1249. , (float)yy
  1250. , (float)width
  1251. , (float)_height
  1252. , imguiRGBA(128, 128, 128, 96)
  1253. );
  1254. }
  1255. else
  1256. {
  1257. drawRoundedRect( (float)xx
  1258. , (float)yy
  1259. , (float)width
  1260. , (float)_height
  1261. , (float)_r
  1262. , imguiRGBA(128, 128, 128, 96)
  1263. );
  1264. }
  1265. for (uint8_t ii = 0; ii < count; ++ii)
  1266. {
  1267. const uint32_t id = getId();
  1268. int32_t buttonX = xx + ii*tabWidth;
  1269. int32_t textX = buttonX + tabWidthHalf;
  1270. const bool enabled = _enabled && isEnabled(m_areaId);
  1271. const bool over = enabled && inRect(buttonX, yy, tabWidth, _height);
  1272. const bool res = buttonLogic(id, over);
  1273. if (res)
  1274. {
  1275. selected = ii;
  1276. }
  1277. uint32_t textColor;
  1278. if (ii == selected)
  1279. {
  1280. textColor = enabled?imguiRGBA(0,0,0,255):imguiRGBA(255,255,255,100);
  1281. if (0 == _r)
  1282. {
  1283. drawRect( (float)buttonX
  1284. , (float)yy
  1285. , (float)tabWidth
  1286. , (float)_height
  1287. , enabled?imguiRGBA(255,196,0,200):imguiRGBA(128,128,128,96)
  1288. );
  1289. }
  1290. else
  1291. {
  1292. drawRoundedRect( (float)buttonX
  1293. , (float)yy
  1294. , (float)tabWidth
  1295. , (float)_height
  1296. , (float)_r
  1297. , enabled?imguiRGBA(255,196,0,200):imguiRGBA(128,128,128,96)
  1298. );
  1299. }
  1300. }
  1301. else
  1302. {
  1303. textColor = isHot(id) ? imguiRGBA(255, 196, 0, enabled?255:100) : imguiRGBA(255, 255, 255, enabled?200:100);
  1304. }
  1305. drawText(textX
  1306. , textY
  1307. , ImguiTextAlign::Center
  1308. , titles[ii]
  1309. , textColor
  1310. );
  1311. }
  1312. return selected;
  1313. }
  1314. void image(bgfx::TextureHandle _image, float _lod, int32_t _width, int32_t _height, ImguiAlign::Enum _align, bool _originBottomLeft)
  1315. {
  1316. Area& area = getCurrentArea();
  1317. int32_t xx;
  1318. if (ImguiAlign::Left == _align)
  1319. {
  1320. xx = area.m_contentX + SCROLL_AREA_PADDING;
  1321. }
  1322. else if (ImguiAlign::LeftIndented == _align)
  1323. {
  1324. xx = area.m_widgetX;
  1325. }
  1326. else if (ImguiAlign::Center == _align)
  1327. {
  1328. xx = area.m_contentX + (area.m_widgetW-_width)/2;
  1329. }
  1330. else if (ImguiAlign::CenterIndented == _align)
  1331. {
  1332. xx = (area.m_widgetX + area.m_widgetW + area.m_contentX - _width)/2;
  1333. }
  1334. else //if (ImguiAlign::Right == _align).
  1335. {
  1336. xx = area.m_contentX + area.m_widgetW - _width;
  1337. }
  1338. const int32_t yy = area.m_widgetY;
  1339. area.m_widgetY += _height + DEFAULT_SPACING;
  1340. screenQuad(xx, yy, _width, _height, _originBottomLeft);
  1341. bgfx::setUniform(u_imageLod, &_lod);
  1342. bgfx::setTexture(0, s_texColor, bgfx::isValid(_image) ? _image : m_missingTexture);
  1343. bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
  1344. bgfx::setProgram(m_imageProgram);
  1345. setCurrentScissor();
  1346. bgfx::submit(m_view);
  1347. }
  1348. void image(bgfx::TextureHandle _image, float _lod, float _width, float _aspect, ImguiAlign::Enum _align, bool _originBottomLeft)
  1349. {
  1350. const float width = _width*float(getCurrentArea().m_widgetW);
  1351. const float height = width/_aspect;
  1352. image(_image, _lod, int32_t(width), int32_t(height), _align, _originBottomLeft);
  1353. }
  1354. void imageChannel(bgfx::TextureHandle _image, uint8_t _channel, float _lod, int32_t _width, int32_t _height, ImguiAlign::Enum _align)
  1355. {
  1356. BX_CHECK(_channel < 4, "Channel param must be from 0 to 3!");
  1357. Area& area = getCurrentArea();
  1358. int32_t xx;
  1359. if (ImguiAlign::Left == _align)
  1360. {
  1361. xx = area.m_contentX + SCROLL_AREA_PADDING;
  1362. }
  1363. else if (ImguiAlign::LeftIndented == _align)
  1364. {
  1365. xx = area.m_widgetX;
  1366. }
  1367. else if (ImguiAlign::Center == _align)
  1368. {
  1369. xx = area.m_contentX + (area.m_widgetW-_width)/2;
  1370. }
  1371. else if (ImguiAlign::CenterIndented == _align)
  1372. {
  1373. xx = (area.m_widgetX + area.m_widgetW + area.m_contentX - _width)/2;
  1374. }
  1375. else //if (ImguiAlign::Right == _align).
  1376. {
  1377. xx = area.m_contentX + area.m_widgetW - _width;
  1378. }
  1379. const int32_t yy = area.m_widgetY;
  1380. area.m_widgetY += _height + DEFAULT_SPACING;
  1381. screenQuad(xx, yy, _width, _height);
  1382. bgfx::setUniform(u_imageLod, &_lod);
  1383. float swizz[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  1384. swizz[_channel] = 1.0f;
  1385. bgfx::setUniform(u_imageSwizzle, swizz);
  1386. bgfx::setTexture(0, s_texColor, bgfx::isValid(_image) ? _image : m_missingTexture);
  1387. bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
  1388. bgfx::setProgram(m_imageSwizzProgram);
  1389. setCurrentScissor();
  1390. bgfx::submit(m_view);
  1391. }
  1392. void imageChannel(bgfx::TextureHandle _image, uint8_t _channel, float _lod, float _width, float _aspect, ImguiAlign::Enum _align)
  1393. {
  1394. const float width = _width*float(getCurrentArea().m_widgetW);
  1395. const float height = width/_aspect;
  1396. imageChannel(_image, _channel, _lod, int32_t(width), int32_t(height), _align);
  1397. }
  1398. void cubeMap(bgfx::TextureHandle _cubemap, float _lod, bool _cross, ImguiAlign::Enum _align)
  1399. {
  1400. uint32_t numVertices = 14;
  1401. uint32_t numIndices = 36;
  1402. if (bgfx::checkAvailTransientBuffers(numVertices, PosNormalVertex::ms_decl, numIndices) )
  1403. {
  1404. bgfx::TransientVertexBuffer tvb;
  1405. bgfx::allocTransientVertexBuffer(&tvb, numVertices, PosNormalVertex::ms_decl);
  1406. bgfx::TransientIndexBuffer tib;
  1407. bgfx::allocTransientIndexBuffer(&tib, numIndices);
  1408. PosNormalVertex* vertex = (PosNormalVertex*)tvb.data;
  1409. uint16_t* indices = (uint16_t*)tib.data;
  1410. if (_cross)
  1411. {
  1412. vertex->set( 0.0f, 0.5f, 0.0f, -1.0f, 1.0f, -1.0f); ++vertex;
  1413. vertex->set( 0.0f, 1.0f, 0.0f, -1.0f, -1.0f, -1.0f); ++vertex;
  1414. vertex->set( 0.5f, 0.0f, 0.0f, -1.0f, 1.0f, -1.0f); ++vertex;
  1415. vertex->set( 0.5f, 0.5f, 0.0f, -1.0f, 1.0f, 1.0f); ++vertex;
  1416. vertex->set( 0.5f, 1.0f, 0.0f, -1.0f, -1.0f, 1.0f); ++vertex;
  1417. vertex->set( 0.5f, 1.5f, 0.0f, -1.0f, -1.0f, -1.0f); ++vertex;
  1418. vertex->set( 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, -1.0f); ++vertex;
  1419. vertex->set( 1.0f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f); ++vertex;
  1420. vertex->set( 1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f); ++vertex;
  1421. vertex->set( 1.0f, 1.5f, 0.0f, 1.0f, -1.0f, -1.0f); ++vertex;
  1422. vertex->set( 1.5f, 0.5f, 0.0f, 1.0f, 1.0f, -1.0f); ++vertex;
  1423. vertex->set( 1.5f, 1.0f, 0.0f, 1.0f, -1.0f, -1.0f); ++vertex;
  1424. vertex->set( 2.0f, 0.5f, 0.0f, -1.0f, 1.0f, -1.0f); ++vertex;
  1425. vertex->set( 2.0f, 1.0f, 0.0f, -1.0f, -1.0f, -1.0f); ++vertex;
  1426. indices += quad(indices, 0, 3, 4, 1);
  1427. indices += quad(indices, 2, 6, 7, 3);
  1428. indices += quad(indices, 3, 7, 8, 4);
  1429. indices += quad(indices, 4, 8, 9, 5);
  1430. indices += quad(indices, 7, 10, 11, 8);
  1431. indices += quad(indices, 10, 12, 13, 11);
  1432. }
  1433. else
  1434. {
  1435. vertex->set( 0.0f, 0.25f, 0.0f, -1.0f, 1.0f, -1.0f); ++vertex;
  1436. vertex->set( 0.0f, 0.75f, 0.0f, -1.0f, -1.0f, -1.0f); ++vertex;
  1437. vertex->set( 0.5f, 0.00f, 0.0f, -1.0f, 1.0f, 1.0f); ++vertex;
  1438. vertex->set( 0.5f, 0.50f, 0.0f, -1.0f, -1.0f, 1.0f); ++vertex;
  1439. vertex->set( 0.5f, 1.00f, 0.0f, 1.0f, -1.0f, -1.0f); ++vertex;
  1440. vertex->set( 1.0f, 0.25f, 0.0f, 1.0f, 1.0f, 1.0f); ++vertex;
  1441. vertex->set( 1.0f, 0.75f, 0.0f, 1.0f, -1.0f, 1.0f); ++vertex;
  1442. vertex->set( 1.0f, 0.25f, 0.0f, 1.0f, 1.0f, 1.0f); ++vertex;
  1443. vertex->set( 1.0f, 0.75f, 0.0f, 1.0f, -1.0f, 1.0f); ++vertex;
  1444. vertex->set( 1.5f, 0.00f, 0.0f, -1.0f, 1.0f, 1.0f); ++vertex;
  1445. vertex->set( 1.5f, 0.50f, 0.0f, 1.0f, 1.0f, -1.0f); ++vertex;
  1446. vertex->set( 1.5f, 1.00f, 0.0f, 1.0f, -1.0f, -1.0f); ++vertex;
  1447. vertex->set( 2.0f, 0.25f, 0.0f, -1.0f, 1.0f, -1.0f); ++vertex;
  1448. vertex->set( 2.0f, 0.75f, 0.0f, -1.0f, -1.0f, -1.0f); ++vertex;
  1449. indices += quad(indices, 0, 2, 3, 1);
  1450. indices += quad(indices, 1, 3, 6, 4);
  1451. indices += quad(indices, 2, 5, 6, 3);
  1452. indices += quad(indices, 7, 9, 12, 10);
  1453. indices += quad(indices, 7, 10, 11, 8);
  1454. indices += quad(indices, 10, 12, 13, 11);
  1455. }
  1456. Area& area = getCurrentArea();
  1457. int32_t xx;
  1458. int32_t width;
  1459. if (ImguiAlign::Left == _align)
  1460. {
  1461. xx = area.m_contentX + SCROLL_AREA_PADDING;
  1462. width = area.m_widgetW;
  1463. }
  1464. else if (ImguiAlign::LeftIndented == _align
  1465. || ImguiAlign::Right == _align)
  1466. {
  1467. xx = area.m_widgetX;
  1468. width = area.m_widgetW-1; //TODO: -1 !
  1469. }
  1470. else //if (ImguiAlign::Center == _align
  1471. //|| ImguiAlign::CenterIndented == _align).
  1472. {
  1473. xx = area.m_widgetX;
  1474. width = area.m_widgetW - (area.m_widgetX-area.m_scissorX);
  1475. }
  1476. const uint32_t height = _cross ? (width*3)/4 : (width/2);
  1477. const int32_t yy = area.m_widgetY;
  1478. area.m_widgetY += height + DEFAULT_SPACING;
  1479. const float scale = float(width/2);
  1480. float mtx[16];
  1481. bx::mtxSRT(mtx, scale, scale, 1.0f, 0.0f, 0.0f, 0.0f, float(xx), float(yy), 0.0f);
  1482. bgfx::setTransform(mtx);
  1483. bgfx::setUniform(u_imageLod, &_lod);
  1484. bgfx::setTexture(0, s_texColor, _cubemap);
  1485. bgfx::setProgram(m_cubeMapProgram);
  1486. bgfx::setVertexBuffer(&tvb);
  1487. bgfx::setIndexBuffer(&tib);
  1488. bgfx::setState(0
  1489. | BGFX_STATE_RGB_WRITE
  1490. | BGFX_STATE_CULL_CW
  1491. );
  1492. setCurrentScissor();
  1493. bgfx::submit(m_view);
  1494. }
  1495. }
  1496. bool collapse(const char* _text, const char* _subtext, bool _checked, bool _enabled)
  1497. {
  1498. const uint32_t id = getId();
  1499. Area& area = getCurrentArea();
  1500. const int32_t xx = area.m_widgetX;
  1501. const int32_t yy = area.m_widgetY;
  1502. const int32_t width = area.m_widgetW;
  1503. const int32_t height = BUTTON_HEIGHT;
  1504. area.m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  1505. const int32_t cx = xx + BUTTON_HEIGHT/2 - CHECK_SIZE/2;
  1506. const int32_t cy = yy + BUTTON_HEIGHT/2 - CHECK_SIZE/2 + DEFAULT_SPACING/2;
  1507. const int32_t textY = yy + BUTTON_HEIGHT/2 + TEXT_HEIGHT/2 + DEFAULT_SPACING/2;
  1508. const bool enabled = _enabled && isEnabled(m_areaId);
  1509. const bool over = enabled && inRect(xx, yy, width, height);
  1510. const bool res = buttonLogic(id, over);
  1511. if (_checked)
  1512. {
  1513. drawTriangle(cx
  1514. , cy
  1515. , CHECK_SIZE
  1516. , CHECK_SIZE
  1517. , TriangleOrientation::Up
  1518. , imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200)
  1519. );
  1520. }
  1521. else
  1522. {
  1523. drawTriangle(cx
  1524. , cy
  1525. , CHECK_SIZE
  1526. , CHECK_SIZE
  1527. , TriangleOrientation::Right
  1528. , imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200)
  1529. );
  1530. }
  1531. if (enabled)
  1532. {
  1533. drawText(xx + BUTTON_HEIGHT
  1534. , textY
  1535. , ImguiTextAlign::Left
  1536. , _text
  1537. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  1538. );
  1539. }
  1540. else
  1541. {
  1542. drawText(xx + BUTTON_HEIGHT
  1543. , textY
  1544. , ImguiTextAlign::Left
  1545. , _text
  1546. , imguiRGBA(128, 128, 128, 200)
  1547. );
  1548. }
  1549. if (_subtext)
  1550. {
  1551. drawText(xx + width - BUTTON_HEIGHT / 2
  1552. , textY
  1553. , ImguiTextAlign::Right
  1554. , _subtext
  1555. , imguiRGBA(255, 255, 255, 128)
  1556. );
  1557. }
  1558. return res;
  1559. }
  1560. bool borderButton(ImguiBorder::Enum _border, bool _checked, bool _enabled)
  1561. {
  1562. // Since border button isn't part of any area, just use this custom/unique areaId.
  1563. const uint16_t areaId = UINT16_MAX-1;
  1564. const uint32_t id = (areaId << 16) | m_widgetId++;
  1565. updatePresence(id);
  1566. const int32_t triSize = 12;
  1567. const int32_t borderSize = 15;
  1568. int32_t xx;
  1569. int32_t yy;
  1570. int32_t width;
  1571. int32_t height;
  1572. int32_t triX;
  1573. int32_t triY;
  1574. TriangleOrientation::Enum orientation;
  1575. if (ImguiBorder::Left == _border)
  1576. {
  1577. xx = -borderSize;
  1578. yy = 0;
  1579. width = 2*borderSize;
  1580. height = m_viewHeight;
  1581. triX = 0;
  1582. triY = (m_viewHeight-triSize)/2;
  1583. orientation = _checked ? TriangleOrientation::Left : TriangleOrientation::Right;
  1584. }
  1585. else if (ImguiBorder::Right == _border)
  1586. {
  1587. xx = m_viewWidth - borderSize;
  1588. yy = 0;
  1589. width = 2*borderSize;
  1590. height = m_viewHeight;
  1591. triX = m_viewWidth - triSize - 2;
  1592. triY = (m_viewHeight-width)/2;
  1593. orientation = _checked ? TriangleOrientation::Right : TriangleOrientation::Left;
  1594. }
  1595. else if (ImguiBorder::Top == _border)
  1596. {
  1597. xx = 0;
  1598. yy = -borderSize;
  1599. width = m_viewWidth;
  1600. height = 2*borderSize;
  1601. triX = (m_viewWidth-triSize)/2;
  1602. triY = 0;
  1603. orientation = _checked ? TriangleOrientation::Up : TriangleOrientation::Down;
  1604. }
  1605. else //if (ImguiBorder::Bottom == _border).
  1606. {
  1607. xx = 0;
  1608. yy = m_viewHeight - borderSize;
  1609. width = m_viewWidth;
  1610. height = 2*borderSize;
  1611. triX = (m_viewWidth-triSize)/2;
  1612. triY = m_viewHeight-triSize;
  1613. orientation = _checked ? TriangleOrientation::Down : TriangleOrientation::Up;
  1614. }
  1615. const bool over = _enabled && inRect(xx, yy, width, height, false);
  1616. const bool res = buttonLogic(id, over);
  1617. drawRoundedRect( (float)xx
  1618. , (float)yy
  1619. , (float)width
  1620. , (float)height
  1621. , 0.0f
  1622. , isActive(id) ? imguiRGBA(23, 23, 23, 192) : imguiRGBA(0, 0, 0, 222)
  1623. );
  1624. drawTriangle( triX
  1625. , triY
  1626. , triSize
  1627. , triSize
  1628. , orientation
  1629. , isHot(id) ? imguiRGBA(255, 196, 0, 222) : imguiRGBA(255, 255, 255, 192)
  1630. );
  1631. return res;
  1632. }
  1633. void labelVargs(const char* _format, va_list _argList, uint32_t _rgba)
  1634. {
  1635. char temp[8192];
  1636. char* out = temp;
  1637. int32_t len = bx::vsnprintf(out, sizeof(temp), _format, _argList);
  1638. if ( (int32_t)sizeof(temp) < len)
  1639. {
  1640. out = (char*)alloca(len+1);
  1641. len = bx::vsnprintf(out, len, _format, _argList);
  1642. }
  1643. out[len] = '\0';
  1644. Area& area = getCurrentArea();
  1645. const int32_t xx = area.m_widgetX;
  1646. const int32_t yy = area.m_widgetY;
  1647. area.m_widgetY += BUTTON_HEIGHT;
  1648. drawText(xx
  1649. , yy + BUTTON_HEIGHT/2 + TEXT_HEIGHT/2
  1650. , ImguiTextAlign::Left
  1651. , out
  1652. , _rgba
  1653. );
  1654. }
  1655. void value(const char* _text)
  1656. {
  1657. Area& area = getCurrentArea();
  1658. const int32_t xx = area.m_widgetX;
  1659. const int32_t yy = area.m_widgetY;
  1660. const int32_t ww = area.m_widgetW;
  1661. area.m_widgetY += BUTTON_HEIGHT;
  1662. drawText(xx + ww - BUTTON_HEIGHT / 2
  1663. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1664. , ImguiTextAlign::Right
  1665. , _text
  1666. , imguiRGBA(255, 255, 255, 200)
  1667. );
  1668. }
  1669. bool slider(const char* _text, float& _val, float _vmin, float _vmax, float _vinc, bool _enabled, ImguiAlign::Enum _align)
  1670. {
  1671. const uint32_t id = getId();
  1672. Area& area = getCurrentArea();
  1673. const int32_t yy = area.m_widgetY;
  1674. const int32_t height = SLIDER_HEIGHT;
  1675. area.m_widgetY += SLIDER_HEIGHT + DEFAULT_SPACING;
  1676. int32_t xx;
  1677. int32_t width;
  1678. if (ImguiAlign::Left == _align)
  1679. {
  1680. xx = area.m_contentX + SCROLL_AREA_PADDING;
  1681. width = area.m_widgetW;
  1682. }
  1683. else if (ImguiAlign::LeftIndented == _align
  1684. || ImguiAlign::Right == _align)
  1685. {
  1686. xx = area.m_widgetX;
  1687. width = area.m_widgetW;
  1688. }
  1689. else //if (ImguiAlign::Center == _align
  1690. //|| ImguiAlign::CenterIndented == _align).
  1691. {
  1692. xx = area.m_widgetX;
  1693. width = area.m_widgetW - (area.m_widgetX-area.m_scissorX);
  1694. }
  1695. drawRoundedRect( (float)xx, (float)yy, (float)width, (float)height, 4.0f, imguiRGBA(0, 0, 0, 128) );
  1696. const int32_t range = width - SLIDER_MARKER_WIDTH;
  1697. float uu = bx::fsaturate( (_val - _vmin) / (_vmax - _vmin) );
  1698. int32_t m = (int)(uu * range);
  1699. bool valChanged = false;
  1700. const bool enabled = _enabled && isEnabled(m_areaId);
  1701. const bool over = enabled && inRect(xx + m, yy, SLIDER_MARKER_WIDTH, SLIDER_HEIGHT);
  1702. const bool res = buttonLogic(id, over);
  1703. if (isActive(id) )
  1704. {
  1705. if (m_wentActive)
  1706. {
  1707. m_dragX = m_mx;
  1708. m_dragOrig = uu;
  1709. }
  1710. if (m_dragX != m_mx)
  1711. {
  1712. uu = bx::fsaturate(m_dragOrig + (float)(m_mx - m_dragX) / (float)range);
  1713. _val = _vmin + uu * (_vmax - _vmin);
  1714. _val = floorf(_val / _vinc + 0.5f) * _vinc; // Snap to vinc
  1715. m = (int)(uu * range);
  1716. valChanged = true;
  1717. }
  1718. }
  1719. if (isActive(id) )
  1720. {
  1721. drawRoundedRect( (float)(xx + m)
  1722. , (float)yy
  1723. , (float)SLIDER_MARKER_WIDTH
  1724. , (float)SLIDER_HEIGHT
  1725. , 4.0f
  1726. , imguiRGBA(255, 255, 255, 255)
  1727. );
  1728. }
  1729. else
  1730. {
  1731. drawRoundedRect( (float)(xx + m)
  1732. , (float)yy
  1733. , (float)SLIDER_MARKER_WIDTH
  1734. , (float)SLIDER_HEIGHT
  1735. , 4.0f
  1736. , isHot(id) ? imguiRGBA(255, 196, 0, 128) : imguiRGBA(255, 255, 255, 64)
  1737. );
  1738. }
  1739. // TODO: fix this, take a look at 'nicenum'.
  1740. int32_t digits = (int)(ceilf(log10f(_vinc) ) );
  1741. char fmt[16];
  1742. bx::snprintf(fmt, 16, "%%.%df", digits >= 0 ? 0 : -digits);
  1743. char msg[128];
  1744. bx::snprintf(msg, 128, fmt, _val);
  1745. if (enabled)
  1746. {
  1747. drawText(xx + SLIDER_HEIGHT / 2
  1748. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  1749. , ImguiTextAlign::Left
  1750. , _text
  1751. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  1752. );
  1753. drawText(xx + width - SLIDER_HEIGHT / 2
  1754. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  1755. , ImguiTextAlign::Right
  1756. , msg
  1757. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  1758. );
  1759. }
  1760. else
  1761. {
  1762. drawText(xx + SLIDER_HEIGHT / 2
  1763. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  1764. , ImguiTextAlign::Left
  1765. , _text
  1766. , imguiRGBA(128, 128, 128, 200)
  1767. );
  1768. drawText(xx + width - SLIDER_HEIGHT / 2
  1769. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  1770. , ImguiTextAlign::Right
  1771. , msg
  1772. , imguiRGBA(128, 128, 128, 200)
  1773. );
  1774. }
  1775. return res || valChanged;
  1776. }
  1777. void indent(uint16_t _width)
  1778. {
  1779. Area& area = getCurrentArea();
  1780. area.m_widgetX += _width;
  1781. area.m_widgetW -= _width;
  1782. }
  1783. void unindent(uint16_t _width)
  1784. {
  1785. Area& area = getCurrentArea();
  1786. area.m_widgetX -= _width;
  1787. area.m_widgetW += _width;
  1788. }
  1789. void separator(uint16_t _height)
  1790. {
  1791. Area& area = getCurrentArea();
  1792. area.m_widgetY += _height;
  1793. }
  1794. void separatorLine(uint16_t _height)
  1795. {
  1796. Area& area = getCurrentArea();
  1797. const int32_t rectWidth = area.m_widgetW;
  1798. const int32_t rectHeight = 1;
  1799. const int32_t xx = area.m_widgetX;
  1800. const int32_t yy = area.m_widgetY + _height/2 - rectHeight;
  1801. area.m_widgetY += _height;
  1802. drawRect( (float)xx
  1803. , (float)yy
  1804. , (float)rectWidth
  1805. , (float)rectHeight
  1806. , imguiRGBA(255, 255, 255, 32)
  1807. );
  1808. }
  1809. void drawPolygon(const float* _coords, uint32_t _numCoords, float _r, uint32_t _abgr)
  1810. {
  1811. _numCoords = bx::uint32_min(_numCoords, MAX_TEMP_COORDS);
  1812. for (uint32_t ii = 0, jj = _numCoords - 1; ii < _numCoords; jj = ii++)
  1813. {
  1814. const float* v0 = &_coords[jj * 2];
  1815. const float* v1 = &_coords[ii * 2];
  1816. float dx = v1[0] - v0[0];
  1817. float dy = v1[1] - v0[1];
  1818. float d = sqrtf(dx * dx + dy * dy);
  1819. if (d > 0)
  1820. {
  1821. d = 1.0f / d;
  1822. dx *= d;
  1823. dy *= d;
  1824. }
  1825. m_tempNormals[jj * 2 + 0] = dy;
  1826. m_tempNormals[jj * 2 + 1] = -dx;
  1827. }
  1828. for (uint32_t ii = 0, jj = _numCoords - 1; ii < _numCoords; jj = ii++)
  1829. {
  1830. float dlx0 = m_tempNormals[jj * 2 + 0];
  1831. float dly0 = m_tempNormals[jj * 2 + 1];
  1832. float dlx1 = m_tempNormals[ii * 2 + 0];
  1833. float dly1 = m_tempNormals[ii * 2 + 1];
  1834. float dmx = (dlx0 + dlx1) * 0.5f;
  1835. float dmy = (dly0 + dly1) * 0.5f;
  1836. float dmr2 = dmx * dmx + dmy * dmy;
  1837. if (dmr2 > 0.000001f)
  1838. {
  1839. float scale = 1.0f / dmr2;
  1840. if (scale > 10.0f)
  1841. {
  1842. scale = 10.0f;
  1843. }
  1844. dmx *= scale;
  1845. dmy *= scale;
  1846. }
  1847. m_tempCoords[ii * 2 + 0] = _coords[ii * 2 + 0] + dmx * _r;
  1848. m_tempCoords[ii * 2 + 1] = _coords[ii * 2 + 1] + dmy * _r;
  1849. }
  1850. uint32_t numVertices = _numCoords*6 + (_numCoords-2)*3;
  1851. if (bgfx::checkAvailTransientVertexBuffer(numVertices, PosColorVertex::ms_decl) )
  1852. {
  1853. bgfx::TransientVertexBuffer tvb;
  1854. bgfx::allocTransientVertexBuffer(&tvb, numVertices, PosColorVertex::ms_decl);
  1855. uint32_t trans = _abgr&0xffffff;
  1856. PosColorVertex* vertex = (PosColorVertex*)tvb.data;
  1857. for (uint32_t ii = 0, jj = _numCoords-1; ii < _numCoords; jj = ii++)
  1858. {
  1859. vertex->m_x = _coords[ii*2+0];
  1860. vertex->m_y = _coords[ii*2+1];
  1861. vertex->m_abgr = _abgr;
  1862. ++vertex;
  1863. vertex->m_x = _coords[jj*2+0];
  1864. vertex->m_y = _coords[jj*2+1];
  1865. vertex->m_abgr = _abgr;
  1866. ++vertex;
  1867. vertex->m_x = m_tempCoords[jj*2+0];
  1868. vertex->m_y = m_tempCoords[jj*2+1];
  1869. vertex->m_abgr = trans;
  1870. ++vertex;
  1871. vertex->m_x = m_tempCoords[jj*2+0];
  1872. vertex->m_y = m_tempCoords[jj*2+1];
  1873. vertex->m_abgr = trans;
  1874. ++vertex;
  1875. vertex->m_x = m_tempCoords[ii*2+0];
  1876. vertex->m_y = m_tempCoords[ii*2+1];
  1877. vertex->m_abgr = trans;
  1878. ++vertex;
  1879. vertex->m_x = _coords[ii*2+0];
  1880. vertex->m_y = _coords[ii*2+1];
  1881. vertex->m_abgr = _abgr;
  1882. ++vertex;
  1883. }
  1884. for (uint32_t ii = 2; ii < _numCoords; ++ii)
  1885. {
  1886. vertex->m_x = _coords[0];
  1887. vertex->m_y = _coords[1];
  1888. vertex->m_abgr = _abgr;
  1889. ++vertex;
  1890. vertex->m_x = _coords[(ii-1)*2+0];
  1891. vertex->m_y = _coords[(ii-1)*2+1];
  1892. vertex->m_abgr = _abgr;
  1893. ++vertex;
  1894. vertex->m_x = _coords[ii*2+0];
  1895. vertex->m_y = _coords[ii*2+1];
  1896. vertex->m_abgr = _abgr;
  1897. ++vertex;
  1898. }
  1899. bgfx::setVertexBuffer(&tvb);
  1900. bgfx::setState(0
  1901. | BGFX_STATE_RGB_WRITE
  1902. | BGFX_STATE_ALPHA_WRITE
  1903. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  1904. );
  1905. bgfx::setProgram(m_colorProgram);
  1906. setCurrentScissor();
  1907. bgfx::submit(m_view);
  1908. }
  1909. }
  1910. void drawRect(float _x, float _y, float w, float h, uint32_t _argb, float _fth = 1.0f)
  1911. {
  1912. float verts[4 * 2] =
  1913. {
  1914. _x + 0.5f, _y + 0.5f,
  1915. _x + w - 0.5f, _y + 0.5f,
  1916. _x + w - 0.5f, _y + h - 0.5f,
  1917. _x + 0.5f, _y + h - 0.5f,
  1918. };
  1919. drawPolygon(verts, 4, _fth, _argb);
  1920. }
  1921. void drawRoundedRect(float _x, float _y, float w, float h, float r, uint32_t _argb, float _fth = 1.0f)
  1922. {
  1923. const uint32_t num = NUM_CIRCLE_VERTS / 4;
  1924. const float* cverts = m_circleVerts;
  1925. float verts[(num + 1) * 4 * 2];
  1926. float* vv = verts;
  1927. for (uint32_t ii = 0; ii <= num; ++ii)
  1928. {
  1929. *vv++ = _x + w - r + cverts[ii * 2] * r;
  1930. *vv++ = _y + h - r + cverts[ii * 2 + 1] * r;
  1931. }
  1932. for (uint32_t ii = num; ii <= num * 2; ++ii)
  1933. {
  1934. *vv++ = _x + r + cverts[ii * 2] * r;
  1935. *vv++ = _y + h - r + cverts[ii * 2 + 1] * r;
  1936. }
  1937. for (uint32_t ii = num * 2; ii <= num * 3; ++ii)
  1938. {
  1939. *vv++ = _x + r + cverts[ii * 2] * r;
  1940. *vv++ = _y + r + cverts[ii * 2 + 1] * r;
  1941. }
  1942. for (uint32_t ii = num * 3; ii < num * 4; ++ii)
  1943. {
  1944. *vv++ = _x + w - r + cverts[ii * 2] * r;
  1945. *vv++ = _y + r + cverts[ii * 2 + 1] * r;
  1946. }
  1947. *vv++ = _x + w - r + cverts[0] * r;
  1948. *vv++ = _y + r + cverts[1] * r;
  1949. drawPolygon(verts, (num + 1) * 4, _fth, _argb);
  1950. }
  1951. void drawLine(float _x0, float _y0, float _x1, float _y1, float _r, uint32_t _abgr, float _fth = 1.0f)
  1952. {
  1953. float dx = _x1 - _x0;
  1954. float dy = _y1 - _y0;
  1955. float d = sqrtf(dx * dx + dy * dy);
  1956. if (d > 0.0001f)
  1957. {
  1958. d = 1.0f / d;
  1959. dx *= d;
  1960. dy *= d;
  1961. }
  1962. float nx = dy;
  1963. float ny = -dx;
  1964. float verts[4 * 2];
  1965. _r -= _fth;
  1966. _r *= 0.5f;
  1967. if (_r < 0.01f)
  1968. {
  1969. _r = 0.01f;
  1970. }
  1971. dx *= _r;
  1972. dy *= _r;
  1973. nx *= _r;
  1974. ny *= _r;
  1975. verts[0] = _x0 - dx - nx;
  1976. verts[1] = _y0 - dy - ny;
  1977. verts[2] = _x0 - dx + nx;
  1978. verts[3] = _y0 - dy + ny;
  1979. verts[4] = _x1 + dx + nx;
  1980. verts[5] = _y1 + dy + ny;
  1981. verts[6] = _x1 + dx - nx;
  1982. verts[7] = _y1 + dy - ny;
  1983. drawPolygon(verts, 4, _fth, _abgr);
  1984. }
  1985. struct TriangleOrientation
  1986. {
  1987. enum Enum
  1988. {
  1989. Left,
  1990. Right,
  1991. Up,
  1992. Down,
  1993. };
  1994. };
  1995. void drawTriangle(int32_t _x, int32_t _y, int32_t _width, int32_t _height, TriangleOrientation::Enum _orientation, uint32_t _abgr)
  1996. {
  1997. if (TriangleOrientation::Left == _orientation)
  1998. {
  1999. const float verts[3 * 2] =
  2000. {
  2001. (float)_x + 0.5f + (float)_width * 1.0f, (float)_y + 0.5f,
  2002. (float)_x + 0.5f, (float)_y + 0.5f + (float)_height / 2.0f - 0.5f,
  2003. (float)_x + 0.5f + (float)_width * 1.0f, (float)_y + 0.5f + (float)_height - 1.0f,
  2004. };
  2005. drawPolygon(verts, 3, 1.0f, _abgr);
  2006. }
  2007. else if (TriangleOrientation::Right == _orientation)
  2008. {
  2009. const float verts[3 * 2] =
  2010. {
  2011. (float)_x + 0.5f, (float)_y + 0.5f,
  2012. (float)_x + 0.5f + (float)_width * 1.0f, (float)_y + 0.5f + (float)_height / 2.0f - 0.5f,
  2013. (float)_x + 0.5f, (float)_y + 0.5f + (float)_height - 1.0f,
  2014. };
  2015. drawPolygon(verts, 3, 1.0f, _abgr);
  2016. }
  2017. else if (TriangleOrientation::Up == _orientation)
  2018. {
  2019. const float verts[3 * 2] =
  2020. {
  2021. (float)_x + 0.5f, (float)_y + 0.5f + (float)_height - 1.0f,
  2022. (float)_x + 0.5f + (float)_width / 2.0f - 0.5f, (float)_y + 0.5f,
  2023. (float)_x + 0.5f + (float)_width - 1.0f, (float)_y + 0.5f + (float)_height - 1.0f,
  2024. };
  2025. drawPolygon(verts, 3, 1.0f, _abgr);
  2026. }
  2027. else //if (TriangleOrientation::Down == _orientation).
  2028. {
  2029. const float verts[3 * 2] =
  2030. {
  2031. (float)_x + 0.5f, (float)_y + 0.5f,
  2032. (float)_x + 0.5f + (float)_width / 2.0f - 0.5f, (float)_y + 0.5f + (float)_height - 1.0f,
  2033. (float)_x + 0.5f + (float)_width - 1.0f, (float)_y + 0.5f,
  2034. };
  2035. drawPolygon(verts, 3, 1.0f, _abgr);
  2036. }
  2037. }
  2038. #if !USE_NANOVG_FONT
  2039. void getBakedQuad(stbtt_bakedchar* _chardata, int32_t char_index, float* _xpos, float* _ypos, stbtt_aligned_quad* _quad)
  2040. {
  2041. stbtt_bakedchar* b = _chardata + char_index;
  2042. int32_t round_x = STBTT_ifloor(*_xpos + b->xoff);
  2043. int32_t round_y = STBTT_ifloor(*_ypos + b->yoff);
  2044. _quad->x0 = (float)round_x;
  2045. _quad->y0 = (float)round_y;
  2046. _quad->x1 = (float)round_x + b->x1 - b->x0;
  2047. _quad->y1 = (float)round_y + b->y1 - b->y0;
  2048. _quad->s0 = (b->x0 + m_halfTexel) * m_invTextureWidth;
  2049. _quad->t0 = (b->y0 + m_halfTexel) * m_invTextureWidth;
  2050. _quad->s1 = (b->x1 + m_halfTexel) * m_invTextureHeight;
  2051. _quad->t1 = (b->y1 + m_halfTexel) * m_invTextureHeight;
  2052. *_xpos += b->xadvance;
  2053. }
  2054. #endif // !USE_NANOVG_FONT
  2055. void drawText(int32_t _x, int32_t _y, ImguiTextAlign::Enum _align, const char* _text, uint32_t _abgr)
  2056. {
  2057. drawText( (float)_x, (float)_y, _text, _align, _abgr);
  2058. }
  2059. void drawText(float _x, float _y, const char* _text, ImguiTextAlign::Enum _align, uint32_t _abgr)
  2060. {
  2061. if (NULL == _text
  2062. || '\0' == _text[0])
  2063. {
  2064. return;
  2065. }
  2066. #if USE_NANOVG_FONT
  2067. static uint32_t textAlign[ImguiTextAlign::Count] =
  2068. {
  2069. NVG_ALIGN_LEFT,
  2070. NVG_ALIGN_CENTER,
  2071. NVG_ALIGN_RIGHT,
  2072. };
  2073. nvgTextAlign(m_nvg, textAlign[_align]);
  2074. nvgFontBlur(m_nvg, 0.0f);
  2075. nvgFillColor(m_nvg, nvgRGBAu(_abgr) );
  2076. nvgText(m_nvg, _x, _y, _text, NULL);
  2077. #else
  2078. uint32_t numVertices = 0;
  2079. if (_align == ImguiTextAlign::Center)
  2080. {
  2081. _x -= getTextLength(m_fonts[m_currentFontIdx].m_cdata, _text, numVertices) / 2;
  2082. }
  2083. else if (_align == ImguiTextAlign::Right)
  2084. {
  2085. _x -= getTextLength(m_fonts[m_currentFontIdx].m_cdata, _text, numVertices);
  2086. }
  2087. else // just count vertices
  2088. {
  2089. getTextLength(m_fonts[m_currentFontIdx].m_cdata, _text, numVertices);
  2090. }
  2091. if (bgfx::checkAvailTransientVertexBuffer(numVertices, PosColorUvVertex::ms_decl) )
  2092. {
  2093. bgfx::TransientVertexBuffer tvb;
  2094. bgfx::allocTransientVertexBuffer(&tvb, numVertices, PosColorUvVertex::ms_decl);
  2095. PosColorUvVertex* vertex = (PosColorUvVertex*)tvb.data;
  2096. const float ox = _x;
  2097. while (*_text)
  2098. {
  2099. int32_t ch = (uint8_t)*_text;
  2100. if (ch == '\t')
  2101. {
  2102. for (int32_t i = 0; i < 4; ++i)
  2103. {
  2104. if (_x < s_tabStops[i] + ox)
  2105. {
  2106. _x = s_tabStops[i] + ox;
  2107. break;
  2108. }
  2109. }
  2110. }
  2111. else if (ch >= ' '
  2112. && ch < 128)
  2113. {
  2114. stbtt_aligned_quad quad;
  2115. getBakedQuad(m_fonts[m_currentFontIdx].m_cdata, ch - 32, &_x, &_y, &quad);
  2116. vertex->m_x = quad.x0;
  2117. vertex->m_y = quad.y0;
  2118. vertex->m_u = quad.s0;
  2119. vertex->m_v = quad.t0;
  2120. vertex->m_abgr = _abgr;
  2121. ++vertex;
  2122. vertex->m_x = quad.x1;
  2123. vertex->m_y = quad.y1;
  2124. vertex->m_u = quad.s1;
  2125. vertex->m_v = quad.t1;
  2126. vertex->m_abgr = _abgr;
  2127. ++vertex;
  2128. vertex->m_x = quad.x1;
  2129. vertex->m_y = quad.y0;
  2130. vertex->m_u = quad.s1;
  2131. vertex->m_v = quad.t0;
  2132. vertex->m_abgr = _abgr;
  2133. ++vertex;
  2134. vertex->m_x = quad.x0;
  2135. vertex->m_y = quad.y0;
  2136. vertex->m_u = quad.s0;
  2137. vertex->m_v = quad.t0;
  2138. vertex->m_abgr = _abgr;
  2139. ++vertex;
  2140. vertex->m_x = quad.x0;
  2141. vertex->m_y = quad.y1;
  2142. vertex->m_u = quad.s0;
  2143. vertex->m_v = quad.t1;
  2144. vertex->m_abgr = _abgr;
  2145. ++vertex;
  2146. vertex->m_x = quad.x1;
  2147. vertex->m_y = quad.y1;
  2148. vertex->m_u = quad.s1;
  2149. vertex->m_v = quad.t1;
  2150. vertex->m_abgr = _abgr;
  2151. ++vertex;
  2152. }
  2153. ++_text;
  2154. }
  2155. bgfx::setTexture(0, s_texColor, m_fonts[m_currentFontIdx].m_texture);
  2156. bgfx::setVertexBuffer(&tvb);
  2157. bgfx::setState(0
  2158. | BGFX_STATE_RGB_WRITE
  2159. | BGFX_STATE_ALPHA_WRITE
  2160. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  2161. );
  2162. bgfx::setProgram(m_textureProgram);
  2163. setCurrentScissor();
  2164. bgfx::submit(m_view);
  2165. }
  2166. #endif // USE_NANOVG_FONT
  2167. }
  2168. void screenQuad(int32_t _x, int32_t _y, int32_t _width, uint32_t _height, bool _originBottomLeft = false)
  2169. {
  2170. if (bgfx::checkAvailTransientVertexBuffer(6, PosUvVertex::ms_decl) )
  2171. {
  2172. bgfx::TransientVertexBuffer vb;
  2173. bgfx::allocTransientVertexBuffer(&vb, 6, PosUvVertex::ms_decl);
  2174. PosUvVertex* vertex = (PosUvVertex*)vb.data;
  2175. const float widthf = float(_width);
  2176. const float heightf = float(_height);
  2177. const float minx = float(_x);
  2178. const float miny = float(_y);
  2179. const float maxx = minx+widthf;
  2180. const float maxy = miny+heightf;
  2181. const float texelHalfW = m_halfTexel/widthf;
  2182. const float texelHalfH = m_halfTexel/heightf;
  2183. const float minu = texelHalfW;
  2184. const float maxu = 1.0f - texelHalfW;
  2185. const float minv = _originBottomLeft ? texelHalfH+1.0f : texelHalfH ;
  2186. const float maxv = _originBottomLeft ? texelHalfH : texelHalfH+1.0f;
  2187. vertex[0].m_x = minx;
  2188. vertex[0].m_y = miny;
  2189. vertex[0].m_u = minu;
  2190. vertex[0].m_v = minv;
  2191. vertex[1].m_x = maxx;
  2192. vertex[1].m_y = miny;
  2193. vertex[1].m_u = maxu;
  2194. vertex[1].m_v = minv;
  2195. vertex[2].m_x = maxx;
  2196. vertex[2].m_y = maxy;
  2197. vertex[2].m_u = maxu;
  2198. vertex[2].m_v = maxv;
  2199. vertex[3].m_x = maxx;
  2200. vertex[3].m_y = maxy;
  2201. vertex[3].m_u = maxu;
  2202. vertex[3].m_v = maxv;
  2203. vertex[4].m_x = minx;
  2204. vertex[4].m_y = maxy;
  2205. vertex[4].m_u = minu;
  2206. vertex[4].m_v = maxv;
  2207. vertex[5].m_x = minx;
  2208. vertex[5].m_y = miny;
  2209. vertex[5].m_u = minu;
  2210. vertex[5].m_v = minv;
  2211. bgfx::setVertexBuffer(&vb);
  2212. }
  2213. }
  2214. void colorWheelWidget(float _rgb[3], bool _respectIndentation, bool _enabled)
  2215. {
  2216. const uint32_t wheelId = getId();
  2217. const uint32_t triangleId = getId();
  2218. Area& area = getCurrentArea();
  2219. const int32_t height = area.m_contentWidth - COLOR_WHEEL_PADDING;
  2220. const float heightf = float(height);
  2221. const float widthf = float(area.m_contentWidth - COLOR_WHEEL_PADDING);
  2222. const float xx = float( (_respectIndentation ? area.m_widgetX-SCROLL_AREA_PADDING : area.m_contentX) + COLOR_WHEEL_PADDING/2);
  2223. const float yy = float(area.m_widgetY);
  2224. area.m_widgetY += height + DEFAULT_SPACING;
  2225. const float ro = (widthf < heightf ? widthf : heightf) * 0.5f - 5.0f; // radiusOuter.
  2226. const float rd = 20.0f; // radiusDelta.
  2227. const float ri = ro - rd; // radiusInner.
  2228. const float aeps = 0.5f / ro; // Half a pixel arc length in radians (2pi cancels out).
  2229. const float center[2] = { xx + widthf*0.5f, yy + heightf*0.5f };
  2230. const float cmx = float(m_mx) - center[0];
  2231. const float cmy = float(m_my) - center[1];
  2232. const float aa[2] = { ri - 6.0f, 0.0f }; // Hue point.
  2233. const float bb[2] = { cosf(-120.0f/180.0f*NVG_PI) * aa[0], sinf(-120.0f/180.0f*NVG_PI) * aa[0] }; // Black point.
  2234. const float cc[2] = { cosf( 120.0f/180.0f*NVG_PI) * aa[0], sinf( 120.0f/180.0f*NVG_PI) * aa[0] }; // White point.
  2235. const float ca[2] = { aa[0] - cc[0], aa[1] - cc[1] };
  2236. const float lenCa = sqrtf(ca[0]*ca[0]+ca[1]*ca[1]);
  2237. const float invLenCa = 1.0f/lenCa;
  2238. const float dirCa[2] = { ca[0]*invLenCa, ca[1]*invLenCa };
  2239. float sel[2];
  2240. float hsv[3];
  2241. bx::rgbToHsv(hsv, _rgb);
  2242. const bool enabled = _enabled && isEnabled(m_areaId);
  2243. if (enabled)
  2244. {
  2245. if (m_leftPressed)
  2246. {
  2247. const float len = sqrtf(cmx*cmx + cmy*cmy);
  2248. if (len > ri)
  2249. {
  2250. if (len < ro)
  2251. {
  2252. setActive(wheelId);
  2253. }
  2254. }
  2255. else
  2256. {
  2257. setActive(triangleId);
  2258. }
  2259. }
  2260. if (m_leftReleased
  2261. && (isActive(wheelId) || isActive(triangleId) ) )
  2262. {
  2263. clearActive();
  2264. }
  2265. // Set hue.
  2266. if (m_left
  2267. && isActive(wheelId) )
  2268. {
  2269. hsv[0] = atan2f(cmy, cmx)/NVG_PI*0.5f;
  2270. if (hsv[0] < 0.0f)
  2271. {
  2272. hsv[0]+=1.0f;
  2273. }
  2274. }
  2275. }
  2276. if (enabled
  2277. && m_left
  2278. && isActive(triangleId) )
  2279. {
  2280. float an = -hsv[0]*NVG_PI*2.0f;
  2281. float tmx = (cmx*cosf(an)-cmy*sinf(an) );
  2282. float tmy = (cmx*sinf(an)+cmy*cosf(an) );
  2283. if (pointInTriangle(tmx, tmy, aa[0], aa[1], bb[0], bb[1], cc[0], cc[1]) )
  2284. {
  2285. sel[0] = tmx;
  2286. sel[1] = tmy;
  2287. }
  2288. else
  2289. {
  2290. closestPointOnTriangle(sel[0], sel[1], tmx, tmy, aa[0], aa[1], bb[0], bb[1], cc[0], cc[1]);
  2291. }
  2292. }
  2293. else
  2294. {
  2295. /*
  2296. * bb (black)
  2297. * /\
  2298. * / \
  2299. * / \
  2300. * / \
  2301. * / \
  2302. * / .sel \
  2303. * / \
  2304. * cc(white)/____.ss_______\aa (hue)
  2305. */
  2306. const float ss[2] =
  2307. {
  2308. cc[0] + dirCa[0]*lenCa*hsv[1],
  2309. cc[1] + dirCa[1]*lenCa*hsv[1],
  2310. };
  2311. const float sb[2] = { bb[0]-ss[0], bb[1]-ss[1] };
  2312. const float lenSb = sqrtf(sb[0]*sb[0]+sb[1]*sb[1]);
  2313. const float invLenSb = 1.0f/lenSb;
  2314. const float dirSb[2] = { sb[0]*invLenSb, sb[1]*invLenSb };
  2315. sel[0] = cc[0] + dirCa[0]*lenCa*hsv[1] + dirSb[0]*lenSb*(1.0f - hsv[2]);
  2316. sel[1] = cc[1] + dirCa[1]*lenCa*hsv[1] + dirSb[1]*lenSb*(1.0f - hsv[2]);
  2317. }
  2318. float uu, vv, ww;
  2319. barycentric(uu, vv, ww
  2320. , aa[0], aa[1]
  2321. , bb[0], bb[1]
  2322. , cc[0], cc[1]
  2323. , sel[0], sel[1]
  2324. );
  2325. const float val = bx::fclamp(1.0f-vv, 0.0001f, 1.0f);
  2326. const float sat = bx::fclamp(uu/val, 0.0001f, 1.0f);
  2327. const float out[3] = { hsv[0], sat, val };
  2328. bx::hsvToRgb(_rgb, out);
  2329. // Draw widget.
  2330. nvgSave(m_nvg);
  2331. {
  2332. float saturation;
  2333. uint8_t alpha0;
  2334. uint8_t alpha1;
  2335. if (enabled)
  2336. {
  2337. saturation = 1.0f;
  2338. alpha0 = 255;
  2339. alpha1 = 192;
  2340. }
  2341. else
  2342. {
  2343. saturation = 0.0f;
  2344. alpha0 = 10;
  2345. alpha1 = 10;
  2346. }
  2347. // Circle.
  2348. for (uint8_t ii = 0; ii < 6; ii++)
  2349. {
  2350. const float a0 = float(ii)/6.0f * 2.0f*NVG_PI - aeps;
  2351. const float a1 = float(ii+1.0f)/6.0f * 2.0f*NVG_PI + aeps;
  2352. nvgBeginPath(m_nvg);
  2353. nvgArc(m_nvg, center[0], center[1], ri, a0, a1, NVG_CW);
  2354. nvgArc(m_nvg, center[0], center[1], ro, a1, a0, NVG_CCW);
  2355. nvgClosePath(m_nvg);
  2356. const float ax = center[0] + cosf(a0) * (ri+ro)*0.5f;
  2357. const float ay = center[1] + sinf(a0) * (ri+ro)*0.5f;
  2358. const float bx = center[0] + cosf(a1) * (ri+ro)*0.5f;
  2359. const float by = center[1] + sinf(a1) * (ri+ro)*0.5f;
  2360. NVGpaint paint = nvgLinearGradient(m_nvg
  2361. , ax, ay
  2362. , bx, by
  2363. , nvgHSLA(a0/NVG_PI*0.5f,saturation,0.55f,alpha0)
  2364. , nvgHSLA(a1/NVG_PI*0.5f,saturation,0.55f,alpha0)
  2365. );
  2366. nvgFillPaint(m_nvg, paint);
  2367. nvgFill(m_nvg);
  2368. }
  2369. // Circle stroke.
  2370. nvgBeginPath(m_nvg);
  2371. nvgCircle(m_nvg, center[0], center[1], ri-0.5f);
  2372. nvgCircle(m_nvg, center[0], center[1], ro+0.5f);
  2373. nvgStrokeColor(m_nvg, nvgRGBA(0,0,0,64) );
  2374. nvgStrokeWidth(m_nvg, 1.0f);
  2375. nvgStroke(m_nvg);
  2376. nvgSave(m_nvg);
  2377. {
  2378. // Hue selector.
  2379. nvgTranslate(m_nvg, center[0], center[1]);
  2380. nvgRotate(m_nvg, hsv[0]*NVG_PI*2.0f);
  2381. nvgStrokeWidth(m_nvg, 2.0f);
  2382. nvgBeginPath(m_nvg);
  2383. nvgRect(m_nvg, ri-1.0f,-3.0f,rd+2.0f,6.0f);
  2384. nvgStrokeColor(m_nvg, nvgRGBA(255,255,255,alpha1) );
  2385. nvgStroke(m_nvg);
  2386. // Hue selector drop shadow.
  2387. 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) );
  2388. nvgBeginPath(m_nvg);
  2389. nvgRect(m_nvg, ri-2.0f-10.0f,-4.0f-10.0f,ro-ri+4.0f+20.0f,8.0f+20.0f);
  2390. nvgRect(m_nvg, ri-2.0f,-4.0f,ro-ri+4.0f,8.0f);
  2391. nvgPathWinding(m_nvg, NVG_HOLE);
  2392. nvgFillPaint(m_nvg, paint);
  2393. nvgFill(m_nvg);
  2394. // Center triangle stroke.
  2395. nvgBeginPath(m_nvg);
  2396. nvgMoveTo(m_nvg, aa[0], aa[1]);
  2397. nvgLineTo(m_nvg, bb[0], bb[1]);
  2398. nvgLineTo(m_nvg, cc[0], cc[1]);
  2399. nvgClosePath(m_nvg);
  2400. nvgStrokeColor(m_nvg, nvgRGBA(0,0,0,64) );
  2401. nvgStroke(m_nvg);
  2402. // Center triangle fill.
  2403. paint = nvgLinearGradient(m_nvg, aa[0], aa[1], bb[0], bb[1], nvgHSL(hsv[0],saturation,0.5f), nvgRGBA(0,0,0,alpha0) );
  2404. nvgFillPaint(m_nvg, paint);
  2405. nvgFill(m_nvg);
  2406. 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) );
  2407. nvgFillPaint(m_nvg, paint);
  2408. nvgFill(m_nvg);
  2409. // Color selector.
  2410. nvgStrokeWidth(m_nvg, 2.0f);
  2411. nvgBeginPath(m_nvg);
  2412. nvgCircle(m_nvg, sel[0], sel[1], 5);
  2413. nvgStrokeColor(m_nvg, nvgRGBA(255,255,255,alpha1) );
  2414. nvgStroke(m_nvg);
  2415. // Color selector stroke.
  2416. paint = nvgRadialGradient(m_nvg, sel[0], sel[1], 7.0f, 9.0f, nvgRGBA(0,0,0,64), nvgRGBA(0,0,0,0) );
  2417. nvgBeginPath(m_nvg);
  2418. nvgRect(m_nvg, sel[0]-20.0f, sel[1]-20.0f, 40.0f, 40.0f);
  2419. nvgCircle(m_nvg, sel[0], sel[1], 7.0f);
  2420. nvgPathWinding(m_nvg, NVG_HOLE);
  2421. nvgFillPaint(m_nvg, paint);
  2422. nvgFill(m_nvg);
  2423. }
  2424. nvgRestore(m_nvg);
  2425. }
  2426. nvgRestore(m_nvg);
  2427. }
  2428. struct Area
  2429. {
  2430. int32_t m_x;
  2431. int32_t m_y;
  2432. int32_t m_width;
  2433. int32_t m_height;
  2434. int16_t m_contentX;
  2435. int16_t m_contentY;
  2436. int16_t m_contentWidth;
  2437. int16_t m_contentHeight;
  2438. int16_t m_scissorX;
  2439. int16_t m_scissorY;
  2440. int16_t m_scissorHeight;
  2441. int16_t m_scissorWidth;
  2442. int32_t m_widgetX;
  2443. int32_t m_widgetY;
  2444. int32_t m_widgetW;
  2445. int32_t* m_scrollVal;
  2446. uint32_t m_scrollId;
  2447. bool m_inside;
  2448. bool m_scissorEnabled;
  2449. };
  2450. inline Area& getCurrentArea()
  2451. {
  2452. return m_areas[m_areaId];
  2453. }
  2454. inline void setCurrentScissor()
  2455. {
  2456. const Area& area = getCurrentArea();
  2457. if (area.m_scissorEnabled)
  2458. {
  2459. bgfx::setScissor(uint16_t(IMGUI_MAX(0, area.m_scissorX) )
  2460. , uint16_t(IMGUI_MAX(0, area.m_scissorY-1) )
  2461. , area.m_scissorWidth
  2462. , area.m_scissorHeight+1
  2463. );
  2464. }
  2465. else
  2466. {
  2467. bgfx::setScissor(UINT16_MAX);
  2468. }
  2469. }
  2470. template <typename Ty, uint16_t Max=64>
  2471. struct IdStack
  2472. {
  2473. IdStack()
  2474. {
  2475. reset();
  2476. }
  2477. void reset()
  2478. {
  2479. m_current = 0;
  2480. m_idGen = 0;
  2481. m_ids[0] = 0;
  2482. }
  2483. void next()
  2484. {
  2485. BX_CHECK(Max > (m_current+1), "Param out of bounds!");
  2486. m_ids[++m_current] = ++m_idGen;
  2487. }
  2488. void previous()
  2489. {
  2490. m_current = m_current > 0 ? m_current-1 : 0;
  2491. }
  2492. Ty current() const
  2493. {
  2494. BX_CHECK(Max > (m_current), "Param out of bounds!");
  2495. return m_ids[m_current];
  2496. }
  2497. operator Ty() const
  2498. {
  2499. BX_CHECK(Max > (m_current), "Param out of bounds!");
  2500. return m_ids[m_current];
  2501. }
  2502. private:
  2503. uint16_t m_current;
  2504. Ty m_idGen;
  2505. Ty m_ids[Max];
  2506. };
  2507. int32_t m_mx;
  2508. int32_t m_my;
  2509. int32_t m_scroll;
  2510. uint32_t m_active;
  2511. uint32_t m_hot;
  2512. uint32_t m_hotToBe;
  2513. char m_char;
  2514. char m_lastChar;
  2515. uint32_t m_inputField;
  2516. int32_t m_dragX;
  2517. int32_t m_dragY;
  2518. float m_dragOrig;
  2519. bool m_left;
  2520. bool m_leftPressed;
  2521. bool m_leftReleased;
  2522. bool m_isHot;
  2523. bool m_wentActive;
  2524. bool m_insideArea;
  2525. bool m_isActivePresent;
  2526. bool m_checkActivePresence;
  2527. IdStack<uint16_t> m_areaId;
  2528. uint16_t m_widgetId;
  2529. uint64_t m_enabledAreaIds;
  2530. Area m_areas[64];
  2531. float m_tempCoords[MAX_TEMP_COORDS * 2];
  2532. float m_tempNormals[MAX_TEMP_COORDS * 2];
  2533. float m_circleVerts[NUM_CIRCLE_VERTS * 2];
  2534. uint16_t m_textureWidth;
  2535. uint16_t m_textureHeight;
  2536. float m_invTextureWidth;
  2537. float m_invTextureHeight;
  2538. float m_halfTexel;
  2539. NVGcontext* m_nvg;
  2540. uint8_t m_view;
  2541. uint16_t m_viewWidth;
  2542. uint16_t m_viewHeight;
  2543. #if !USE_NANOVG_FONT
  2544. struct Font
  2545. {
  2546. stbtt_bakedchar m_cdata[96]; // ASCII 32..126 is 95 glyphs
  2547. bgfx::TextureHandle m_texture;
  2548. float m_size;
  2549. };
  2550. uint16_t m_currentFontIdx;
  2551. bx::HandleAllocT<IMGUI_CONFIG_MAX_FONTS> m_fontHandle;
  2552. Font m_fonts[IMGUI_CONFIG_MAX_FONTS];
  2553. #endif // !USE_NANOVG_FONT
  2554. bgfx::UniformHandle u_imageLod;
  2555. bgfx::UniformHandle u_imageSwizzle;
  2556. bgfx::UniformHandle s_texColor;
  2557. bgfx::ProgramHandle m_colorProgram;
  2558. bgfx::ProgramHandle m_textureProgram;
  2559. bgfx::ProgramHandle m_cubeMapProgram;
  2560. bgfx::ProgramHandle m_imageProgram;
  2561. bgfx::ProgramHandle m_imageSwizzProgram;
  2562. bgfx::TextureHandle m_missingTexture;
  2563. };
  2564. static Imgui s_imgui;
  2565. ImguiFontHandle imguiCreate(const void* _data, float _fontSize)
  2566. {
  2567. return s_imgui.create(_data, _fontSize);
  2568. }
  2569. void imguiDestroy()
  2570. {
  2571. s_imgui.destroy();
  2572. }
  2573. ImguiFontHandle imguiCreateFont(const void* _data, float _fontSize)
  2574. {
  2575. return s_imgui.createFont(_data, _fontSize);
  2576. }
  2577. void imguiSetFont(ImguiFontHandle _handle)
  2578. {
  2579. s_imgui.setFont(_handle);
  2580. }
  2581. ImguiFontHandle imguiGetCurrentFont()
  2582. {
  2583. const ImguiFontHandle handle = { s_imgui.m_currentFontIdx };
  2584. return handle;
  2585. }
  2586. 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)
  2587. {
  2588. s_imgui.beginFrame(_mx, _my, _button, _scroll, _width, _height, _inputChar, _view);
  2589. }
  2590. void imguiEndFrame()
  2591. {
  2592. s_imgui.endFrame();
  2593. }
  2594. void imguiDrawText(int32_t _x, int32_t _y, ImguiTextAlign::Enum _align, const char* _text, uint32_t _argb)
  2595. {
  2596. s_imgui.drawText(_x, _y, _align, _text, _argb);
  2597. }
  2598. void imguiDrawLine(float _x0, float _y0, float _x1, float _y1, float _r, uint32_t _argb)
  2599. {
  2600. s_imgui.drawLine(_x0, _y0, _x1, _y1, _r, _argb);
  2601. }
  2602. void imguiDrawRoundedRect(float _x, float _y, float _width, float _height, float _r, uint32_t _argb)
  2603. {
  2604. s_imgui.drawRoundedRect(_x, _y, _width, _height, _r, _argb);
  2605. }
  2606. void imguiDrawRect(float _x, float _y, float _width, float _height, uint32_t _argb)
  2607. {
  2608. s_imgui.drawRect(_x, _y, _width, _height, _argb);
  2609. }
  2610. bool imguiBorderButton(ImguiBorder::Enum _border, bool _checked, bool _enabled)
  2611. {
  2612. return s_imgui.borderButton(_border, _checked, _enabled);
  2613. }
  2614. bool imguiBeginArea(const char* _name, int _x, int _y, int _width, int _height, bool _enabled, int32_t _r)
  2615. {
  2616. return s_imgui.beginArea(_name, _x, _y, _width, _height, _enabled, _r);
  2617. }
  2618. void imguiEndArea()
  2619. {
  2620. return s_imgui.endArea();
  2621. }
  2622. bool imguiBeginScroll(int32_t _height, int32_t* _scroll, bool _enabled)
  2623. {
  2624. return s_imgui.beginScroll(_height, _scroll, _enabled);
  2625. }
  2626. void imguiEndScroll(int32_t _r)
  2627. {
  2628. s_imgui.endScroll(_r);
  2629. }
  2630. bool imguiBeginScrollArea(const char* _name, int32_t _x, int32_t _y, int32_t _width, int32_t _height, int32_t* _scroll, bool _enabled, int32_t _r)
  2631. {
  2632. const bool result = s_imgui.beginArea(_name, _x, _y, _width, _height, _enabled, _r);
  2633. const bool hasTitle = (NULL != _name && '\0' != _name[0]);
  2634. const int32_t margins = int32_t(hasTitle)*(AREA_HEADER+2*SCROLL_AREA_PADDING-1);
  2635. s_imgui.beginScroll(_height - margins, _scroll, _enabled);
  2636. return result;
  2637. }
  2638. void imguiEndScrollArea(int32_t _r)
  2639. {
  2640. s_imgui.endScroll(_r);
  2641. s_imgui.endArea();
  2642. }
  2643. void imguiIndent(uint16_t _width)
  2644. {
  2645. s_imgui.indent(_width);
  2646. }
  2647. void imguiUnindent(uint16_t _width)
  2648. {
  2649. s_imgui.unindent(_width);
  2650. }
  2651. void imguiSeparator(uint16_t _height)
  2652. {
  2653. s_imgui.separator(_height);
  2654. }
  2655. void imguiSeparatorLine(uint16_t _height)
  2656. {
  2657. s_imgui.separatorLine(_height);
  2658. }
  2659. int32_t imguiGetWidgetX()
  2660. {
  2661. return s_imgui.getCurrentArea().m_widgetX;
  2662. }
  2663. int32_t imguiGetWidgetY()
  2664. {
  2665. return s_imgui.getCurrentArea().m_widgetY;
  2666. }
  2667. bool imguiButton(const char* _text, bool _enabled, ImguiAlign::Enum _align, uint32_t _rgb0, int32_t _r)
  2668. {
  2669. return s_imgui.button(_text, _enabled, _align, _rgb0, _r);
  2670. }
  2671. bool imguiItem(const char* _text, bool _enabled)
  2672. {
  2673. return s_imgui.item(_text, _enabled);
  2674. }
  2675. bool imguiCheck(const char* _text, bool _checked, bool _enabled)
  2676. {
  2677. return s_imgui.check(_text, _checked, _enabled);
  2678. }
  2679. void imguiBool(const char* _text, bool& _flag, bool _enabled)
  2680. {
  2681. if (imguiCheck(_text, _flag, _enabled) )
  2682. {
  2683. _flag = !_flag;
  2684. }
  2685. }
  2686. bool imguiCollapse(const char* _text, const char* _subtext, bool _checked, bool _enabled)
  2687. {
  2688. return s_imgui.collapse(_text, _subtext, _checked, _enabled);
  2689. }
  2690. void imguiLabel(const char* _format, ...)
  2691. {
  2692. va_list argList;
  2693. va_start(argList, _format);
  2694. s_imgui.labelVargs(_format, argList, imguiRGBA(255, 255, 255, 255) );
  2695. va_end(argList);
  2696. }
  2697. void imguiLabel(uint32_t _rgba, const char* _format, ...)
  2698. {
  2699. va_list argList;
  2700. va_start(argList, _format);
  2701. s_imgui.labelVargs(_format, argList, _rgba);
  2702. va_end(argList);
  2703. }
  2704. void imguiValue(const char* _text)
  2705. {
  2706. s_imgui.value(_text);
  2707. }
  2708. bool imguiSlider(const char* _text, float& _val, float _vmin, float _vmax, float _vinc, bool _enabled, ImguiAlign::Enum _align)
  2709. {
  2710. return s_imgui.slider(_text, _val, _vmin, _vmax, _vinc, _enabled, _align);
  2711. }
  2712. bool imguiSlider(const char* _text, int32_t& _val, int32_t _vmin, int32_t _vmax, bool _enabled, ImguiAlign::Enum _align)
  2713. {
  2714. float val = (float)_val;
  2715. bool result = s_imgui.slider(_text, val, (float)_vmin, (float)_vmax, 1.0f, _enabled, _align);
  2716. _val = (int32_t)val;
  2717. return result;
  2718. }
  2719. void imguiInput(const char* _label, char* _str, uint32_t _len, bool _enabled, ImguiAlign::Enum _align, int32_t _r)
  2720. {
  2721. s_imgui.input(_label, _str, _len, _enabled, _align, _r);
  2722. }
  2723. uint8_t imguiTabsUseMacroInstead(uint8_t _selected, ...)
  2724. {
  2725. va_list argList;
  2726. va_start(argList, _selected);
  2727. const uint8_t result = s_imgui.tabs(_selected, true, ImguiAlign::LeftIndented, BUTTON_HEIGHT, BUTTON_HEIGHT/2 - 1, argList);
  2728. va_end(argList);
  2729. return result;
  2730. }
  2731. uint8_t imguiTabsUseMacroInstead(uint8_t _selected, bool _enabled, ...)
  2732. {
  2733. va_list argList;
  2734. va_start(argList, _enabled);
  2735. const uint8_t result = s_imgui.tabs(_selected, _enabled, ImguiAlign::LeftIndented, BUTTON_HEIGHT, BUTTON_HEIGHT/2 - 1, argList);
  2736. va_end(argList);
  2737. return result;
  2738. }
  2739. uint8_t imguiTabsUseMacroInstead(uint8_t _selected, bool _enabled, ImguiAlign::Enum _align, ...)
  2740. {
  2741. va_list argList;
  2742. va_start(argList, _align);
  2743. const uint8_t result = s_imgui.tabs(_selected, _enabled, _align, BUTTON_HEIGHT, BUTTON_HEIGHT/2 - 1, argList);
  2744. va_end(argList);
  2745. return result;
  2746. }
  2747. uint8_t imguiTabsUseMacroInstead(uint8_t _selected, bool _enabled, ImguiAlign::Enum _align, int32_t _height, int32_t _r, ...)
  2748. {
  2749. va_list argList;
  2750. va_start(argList, _r);
  2751. const uint8_t result = s_imgui.tabs(_selected, _enabled, _align, _height, _r, argList);
  2752. va_end(argList);
  2753. return result;
  2754. }
  2755. uint32_t imguiChooseUseMacroInstead(uint32_t _selected, ...)
  2756. {
  2757. va_list argList;
  2758. va_start(argList, _selected);
  2759. const char* str = va_arg(argList, const char*);
  2760. for (uint32_t ii = 0; str != NULL; ++ii, str = va_arg(argList, const char*) )
  2761. {
  2762. if (imguiCheck(str, ii == _selected) )
  2763. {
  2764. _selected = ii;
  2765. }
  2766. }
  2767. va_end(argList);
  2768. return _selected;
  2769. }
  2770. void imguiColorWheel(float _rgb[3], bool _respectIndentation, bool _enabled)
  2771. {
  2772. s_imgui.colorWheelWidget(_rgb, _respectIndentation, _enabled);
  2773. }
  2774. void imguiColorWheel(const char* _text, float _rgb[3], bool& _activated, bool _enabled)
  2775. {
  2776. char buf[128];
  2777. bx::snprintf(buf, sizeof(buf), "[RGB %-2.2f %-2.2f %-2.2f]"
  2778. , _rgb[0]
  2779. , _rgb[1]
  2780. , _rgb[2]
  2781. );
  2782. if (imguiCollapse(_text, buf, _activated, _enabled) )
  2783. {
  2784. _activated = !_activated;
  2785. }
  2786. if (_activated)
  2787. {
  2788. imguiColorWheel(_rgb, false, _enabled);
  2789. }
  2790. }
  2791. void imguiImage(bgfx::TextureHandle _image, float _lod, int32_t _width, int32_t _height, ImguiAlign::Enum _align, bool _originBottomLeft)
  2792. {
  2793. s_imgui.image(_image, _lod, _width, _height, _align, _originBottomLeft);
  2794. }
  2795. void imguiImage(bgfx::TextureHandle _image, float _lod, float _width, float _aspect, ImguiAlign::Enum _align, bool _originBottomLeft)
  2796. {
  2797. s_imgui.image(_image, _lod, _width, _aspect, _align, _originBottomLeft);
  2798. }
  2799. void imguiImageChannel(bgfx::TextureHandle _image, uint8_t _channel, float _lod, int32_t _width, int32_t _height, ImguiAlign::Enum _align)
  2800. {
  2801. s_imgui.imageChannel(_image, _channel, _lod, _width, _height, _align);
  2802. }
  2803. void imguiImageChannel(bgfx::TextureHandle _image, uint8_t _channel, float _lod, float _width, float _aspect, ImguiAlign::Enum _align)
  2804. {
  2805. s_imgui.imageChannel(_image, _channel, _lod, _width, _aspect, _align);
  2806. }
  2807. void imguiCube(bgfx::TextureHandle _cubemap, float _lod, bool _cross, ImguiAlign::Enum _align)
  2808. {
  2809. s_imgui.cubeMap(_cubemap, _lod, _cross, _align);
  2810. }
  2811. float imguiGetTextLength(const char* _text, ImguiFontHandle _handle)
  2812. {
  2813. #if !USE_NANOVG_FONT
  2814. uint32_t numVertices = 0; //unused
  2815. return getTextLength(s_imgui.m_fonts[_handle.idx].m_cdata, _text, numVertices);
  2816. #else
  2817. return 0.0f;
  2818. #endif
  2819. }
  2820. bool imguiMouseOverArea()
  2821. {
  2822. return s_imgui.m_insideArea;
  2823. }