imgui.cpp 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337
  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 float s_tabStops[4] = {150, 210, 270, 330};
  54. static void* imguiMalloc(size_t size, void* /*_userptr*/)
  55. {
  56. return malloc(size);
  57. }
  58. static void imguiFree(void* _ptr, void* /*_userptr*/)
  59. {
  60. free(_ptr);
  61. }
  62. #define IMGUI_MIN(_a, _b) (_a)<(_b)?(_a):(_b)
  63. #define IMGUI_MAX(_a, _b) (_a)>(_b)?(_a):(_b)
  64. #define IMGUI_CLAMP(_a, _min, _max) IMGUI_MIN(IMGUI_MAX(_a, _min), _max)
  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/stb_truetype.h>
  69. namespace
  70. {
  71. static uint32_t addQuad(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_imageLodEnabled.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(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)(bx::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_imageLodEnabled = bgfx::createUniform("u_imageLodEnabled", bgfx::UniformType::Uniform4fv);
  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_imageLodEnabled);
  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. m_view = _view;
  661. m_viewWidth = _width;
  662. m_viewHeight = _height;
  663. bgfx::setViewName(_view, "IMGUI");
  664. bgfx::setViewSeq(_view, true);
  665. const bgfx::HMD* hmd = bgfx::getHMD();
  666. if (NULL != hmd)
  667. {
  668. m_viewWidth = _width / 2;
  669. float proj[16];
  670. bx::mtxProj(proj, hmd->eye[0].fov, 0.1f, 100.0f);
  671. static float time = 0.0f;
  672. time += 0.05f;
  673. const float dist = 10.0f;
  674. const float offset0 = -proj[8] + (hmd->eye[0].viewOffset[0] / dist * proj[0]);
  675. const float offset1 = -proj[8] + (hmd->eye[1].viewOffset[0] / dist * proj[0]);
  676. float ortho[2][16];
  677. const float viewOffset = _width/4.0f;
  678. const float viewWidth = _width/2.0f;
  679. bx::mtxOrtho(ortho[0], viewOffset, viewOffset + viewWidth, (float)m_viewHeight, 0.0f, 0.0f, 1000.0f, offset0);
  680. bx::mtxOrtho(ortho[1], viewOffset, viewOffset + viewWidth, (float)m_viewHeight, 0.0f, 0.0f, 1000.0f, offset1);
  681. bgfx::setViewTransform(_view, NULL, ortho[0], BGFX_VIEW_STEREO, ortho[1]);
  682. bgfx::setViewRect(_view, 0, 0, hmd->width, hmd->height);
  683. }
  684. else
  685. {
  686. float ortho[16];
  687. bx::mtxOrtho(ortho, 0.0f, (float)m_viewWidth, (float)m_viewHeight, 0.0f, 0.0f, 1000.0f);
  688. bgfx::setViewTransform(_view, NULL, ortho);
  689. bgfx::setViewRect(_view, 0, 0, _width, _height);
  690. }
  691. updateInput(_mx, _my, _button, _scroll, _inputChar);
  692. m_hot = m_hotToBe;
  693. m_hotToBe = 0;
  694. m_wentActive = false;
  695. m_isHot = false;
  696. Area& area = getCurrentArea();
  697. area.m_widgetX = 0;
  698. area.m_widgetY = 0;
  699. area.m_widgetW = 0;
  700. m_areaId.reset();
  701. m_widgetId = 0;
  702. m_enabledAreaIds = 0;
  703. m_insideArea = false;
  704. m_isActivePresent = false;
  705. }
  706. void endFrame()
  707. {
  708. if (m_checkActivePresence && !m_isActivePresent)
  709. {
  710. // The ui element is not present any more, reset active field.
  711. m_active = 0;
  712. }
  713. m_checkActivePresence = (0 != m_active);
  714. clearInput();
  715. }
  716. bool beginScroll(int32_t _height, int32_t* _scroll, bool _enabled)
  717. {
  718. Area& parentArea = getCurrentArea();
  719. m_areaId.next();
  720. const uint32_t scrollId = getId();
  721. Area& area = getCurrentArea();
  722. const uint16_t parentBottom = parentArea.m_scissorY + parentArea.m_scissorHeight;
  723. const uint16_t childBottom = parentArea.m_widgetY + _height;
  724. const uint16_t bottom = IMGUI_MIN(childBottom, parentBottom);
  725. const uint16_t top = IMGUI_MAX(parentArea.m_widgetY, parentArea.m_scissorY);
  726. area.m_contentX = parentArea.m_contentX;
  727. area.m_contentY = parentArea.m_widgetY;
  728. area.m_contentWidth = parentArea.m_contentWidth - (SCROLL_AREA_PADDING*3);
  729. area.m_contentHeight = _height;
  730. area.m_widgetX = parentArea.m_widgetX;
  731. area.m_widgetY = parentArea.m_widgetY + (*_scroll);
  732. area.m_widgetW = parentArea.m_widgetW - (SCROLL_AREA_PADDING*3);
  733. area.m_scissorX = area.m_contentX;
  734. area.m_scissorWidth = area.m_contentWidth;
  735. area.m_scissorY = top - 1;
  736. area.m_scissorHeight = bottom - top;
  737. area.m_scissorEnabled = true;
  738. area.m_height = _height;
  739. area.m_scrollVal = _scroll;
  740. area.m_scrollId = scrollId;
  741. area.m_inside = inRect(parentArea.m_scissorX
  742. , area.m_scissorY
  743. , parentArea.m_scissorWidth
  744. , area.m_scissorHeight
  745. , false
  746. );
  747. area.m_didScroll = false;
  748. parentArea.m_widgetY += (_height + DEFAULT_SPACING);
  749. if (_enabled)
  750. {
  751. setEnabled(m_areaId);
  752. }
  753. nvgScissor(m_nvg
  754. , float(area.m_scissorX)
  755. , float(area.m_scissorY-1)
  756. , float(area.m_scissorWidth)
  757. , float(area.m_scissorHeight+1)
  758. );
  759. m_insideArea |= area.m_inside;
  760. return area.m_inside;
  761. }
  762. void endScroll(int32_t _r)
  763. {
  764. Area& area = getCurrentArea();
  765. area.m_scissorEnabled = false;
  766. const int32_t xx = area.m_contentX + area.m_contentWidth - 1;
  767. const int32_t yy = area.m_contentY;
  768. const int32_t width = SCROLL_AREA_PADDING * 2;
  769. const int32_t height = area.m_height;
  770. const int32_t aa = area.m_contentY+area.m_height;
  771. const int32_t bb = area.m_widgetY-DEFAULT_SPACING;
  772. const int32_t sbot = IMGUI_MAX(aa, bb);
  773. const int32_t stop = area.m_contentY + (*area.m_scrollVal);
  774. const int32_t sh = IMGUI_MAX(1, sbot - stop); // The scrollable area height.
  775. const uint32_t hid = area.m_scrollId;
  776. const float barHeight = (float)height / (float)sh;
  777. const bool hasScrollBar = (barHeight < 1.0f);
  778. // Handle mouse scrolling.
  779. if (area.m_inside && !area.m_didScroll && !anyActive() )
  780. {
  781. if (m_scroll)
  782. {
  783. const int32_t diff = height - sh;
  784. const int32_t val = *area.m_scrollVal + 20*m_scroll;
  785. const int32_t min = (diff < 0) ? diff : *area.m_scrollVal;
  786. const int32_t max = 0;
  787. const int32_t newVal = IMGUI_CLAMP(val, min, max);
  788. *area.m_scrollVal = newVal;
  789. if (hasScrollBar)
  790. {
  791. area.m_didScroll = true;
  792. }
  793. }
  794. }
  795. area.m_inside = false;
  796. int32_t* scroll = area.m_scrollVal;
  797. // This must be called here before drawing scroll bars
  798. // so that scissor of parrent area applies.
  799. m_areaId.pop();
  800. // Propagate 'didScroll' to parrent area to avoid scrolling multiple areas at once.
  801. Area& parentArea = getCurrentArea();
  802. parentArea.m_didScroll = (parentArea.m_didScroll || area.m_didScroll);
  803. // Draw and handle scroll click.
  804. if (hasScrollBar)
  805. {
  806. const float barY = bx::fsaturate( (float)(-(*scroll) ) / (float)sh);
  807. // Handle scroll bar logic.
  808. const int32_t hx = xx;
  809. const int32_t hy = yy + (int)(barY * height);
  810. const int32_t hw = width;
  811. const int32_t hh = (int)(barHeight * height);
  812. const int32_t range = height - (hh - 1);
  813. const bool over = inRect(hx, hy, hw, hh);
  814. buttonLogic(hid, over);
  815. if (isActive(hid) )
  816. {
  817. float uu = (float)(hy - yy) / (float)range;
  818. if (m_wentActive)
  819. {
  820. m_dragY = m_my;
  821. m_dragOrig = uu;
  822. }
  823. if (m_dragY != m_my)
  824. {
  825. const int32_t diff = height - sh;
  826. const int32_t drag = m_my - m_dragY;
  827. const float dragFactor = float(sh)/float(height);
  828. const int32_t val = *scroll - int32_t(drag*dragFactor);
  829. const int32_t min = (diff < 0) ? diff : *scroll;
  830. const int32_t max = 0;
  831. *scroll = IMGUI_CLAMP(val, min, max);
  832. m_dragY = m_my;
  833. }
  834. }
  835. // BG
  836. drawRoundedRect( (float)xx
  837. , (float)yy
  838. , (float)width
  839. , (float)height
  840. , (float)_r
  841. , imguiRGBA(0, 0, 0, 196)
  842. );
  843. // Bar
  844. if (isActive(hid) )
  845. {
  846. drawRoundedRect( (float)hx
  847. , (float)hy
  848. , (float)hw
  849. , (float)hh
  850. , (float)_r
  851. , imguiRGBA(255, 196, 0, 196)
  852. );
  853. }
  854. else
  855. {
  856. drawRoundedRect( (float)hx
  857. , (float)hy
  858. , (float)hw
  859. , (float)hh
  860. , (float)_r
  861. , isHot(hid) ? imguiRGBA(255, 196, 0, 96) : imguiRGBA(255, 255, 255, 64)
  862. );
  863. }
  864. }
  865. else
  866. {
  867. // Clear active if scroll is selected but not visible any more.
  868. if (isActive(hid))
  869. {
  870. clearActive();
  871. }
  872. }
  873. nvgScissor(m_nvg
  874. , float(parentArea.m_scissorX)
  875. , float(parentArea.m_scissorY-1)
  876. , float(parentArea.m_scissorWidth)
  877. , float(parentArea.m_scissorHeight+1)
  878. );
  879. }
  880. bool beginArea(const char* _name, int32_t _x, int32_t _y, int32_t _width, int32_t _height, bool _enabled, int32_t _r)
  881. {
  882. m_areaId.next();
  883. const uint32_t scrollId = getId();
  884. const bool hasTitle = (NULL != _name && '\0' != _name[0]);
  885. const int32_t header = hasTitle ? AREA_HEADER : 0;
  886. Area& area = getCurrentArea();
  887. area.m_x = _x;
  888. area.m_y = _y;
  889. area.m_width = _width;
  890. area.m_height = _height;
  891. area.m_contentX = area.m_x + SCROLL_AREA_PADDING;
  892. area.m_contentY = area.m_y + SCROLL_AREA_PADDING + header;
  893. area.m_contentWidth = area.m_width - SCROLL_AREA_PADDING;
  894. area.m_contentHeight = area.m_height - SCROLL_AREA_PADDING*2 - header;
  895. area.m_scissorX = area.m_contentX;
  896. area.m_scissorY = area.m_y + SCROLL_AREA_PADDING + header;
  897. area.m_scissorHeight = area.m_height - SCROLL_AREA_PADDING*2 - header;
  898. area.m_scissorWidth = area.m_contentWidth;
  899. area.m_scissorEnabled = false;
  900. area.m_widgetX = area.m_contentX;
  901. area.m_widgetY = area.m_contentY;
  902. area.m_widgetW = area.m_width - SCROLL_AREA_PADDING*2;
  903. static int32_t s_zeroScroll = 0;
  904. area.m_scrollVal = &s_zeroScroll;
  905. area.m_scrollId = scrollId;
  906. area.m_inside = inRect(area.m_scissorX, area.m_scissorY, area.m_scissorWidth, area.m_scissorHeight, false);
  907. area.m_didScroll = false;
  908. if (_enabled)
  909. {
  910. setEnabled(m_areaId);
  911. }
  912. if (0 == _r)
  913. {
  914. drawRect( (float)_x
  915. , (float)_y
  916. , (float)_width + 0.3f /*border fix for seamlessly joining two scroll areas*/
  917. , (float)_height + 0.3f /*border fix for seamlessly joining two scroll areas*/
  918. , imguiRGBA(0, 0, 0, 192)
  919. );
  920. }
  921. else
  922. {
  923. drawRoundedRect( (float)_x
  924. , (float)_y
  925. , (float)_width
  926. , (float)_height
  927. , (float)_r
  928. , imguiRGBA(0, 0, 0, 192)
  929. );
  930. }
  931. if (hasTitle)
  932. {
  933. drawText(_x + 10
  934. , _y + 18
  935. , ImguiTextAlign::Left
  936. , _name
  937. , imguiRGBA(255, 255, 255, 128)
  938. );
  939. }
  940. area.m_scissorEnabled = true;
  941. nvgBeginFrame(m_nvg, m_viewWidth, m_viewHeight, 1.0f);
  942. nvgScissor(m_nvg
  943. , float(area.m_scissorX)
  944. , float(area.m_scissorY-1)
  945. , float(area.m_scissorWidth)
  946. , float(area.m_scissorHeight+1)
  947. );
  948. m_insideArea |= area.m_inside;
  949. return area.m_inside;
  950. }
  951. void endArea()
  952. {
  953. nvgResetScissor(m_nvg);
  954. nvgEndFrame(m_nvg);
  955. }
  956. bool button(const char* _text, bool _enabled, ImguiAlign::Enum _align, uint32_t _rgb0, int32_t _r)
  957. {
  958. const uint32_t id = getId();
  959. Area& area = getCurrentArea();
  960. const int32_t yy = area.m_widgetY;
  961. const int32_t height = BUTTON_HEIGHT;
  962. area.m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  963. int32_t xx;
  964. int32_t width;
  965. if (ImguiAlign::Left == _align)
  966. {
  967. xx = area.m_contentX + SCROLL_AREA_PADDING;
  968. width = area.m_widgetW;
  969. }
  970. else if (ImguiAlign::LeftIndented == _align
  971. || ImguiAlign::Right == _align)
  972. {
  973. xx = area.m_widgetX;
  974. width = area.m_widgetW-1; //TODO: -1 !
  975. }
  976. else //if (ImguiAlign::Center == _align
  977. //|| ImguiAlign::CenterIndented == _align).
  978. {
  979. xx = area.m_widgetX;
  980. width = area.m_widgetW - (area.m_widgetX-area.m_scissorX);
  981. }
  982. const bool enabled = _enabled && isEnabled(m_areaId);
  983. const bool over = enabled && inRect(xx, yy, width, height);
  984. const bool res = buttonLogic(id, over);
  985. const uint32_t rgb0 = _rgb0&0x00ffffff;
  986. drawRoundedRect( (float)xx
  987. , (float)yy
  988. , (float)width
  989. , (float)height
  990. , (float)_r
  991. , rgb0 | imguiRGBA(0, 0, 0, isActive(id) ? 196 : 96)
  992. );
  993. if (enabled)
  994. {
  995. drawText(xx + BUTTON_HEIGHT / 2
  996. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  997. , ImguiTextAlign::Left
  998. , _text
  999. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  1000. );
  1001. }
  1002. else
  1003. {
  1004. drawText(xx + BUTTON_HEIGHT / 2
  1005. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1006. , ImguiTextAlign::Left
  1007. , _text
  1008. , imguiRGBA(128, 128, 128, 200)
  1009. );
  1010. }
  1011. return res;
  1012. }
  1013. bool item(const char* _text, bool _enabled)
  1014. {
  1015. const uint32_t id = getId();
  1016. Area& area = getCurrentArea();
  1017. const int32_t xx = area.m_widgetX;
  1018. const int32_t yy = area.m_widgetY;
  1019. const int32_t width = area.m_widgetW;
  1020. const int32_t height = BUTTON_HEIGHT;
  1021. area.m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  1022. const bool enabled = _enabled && isEnabled(m_areaId);
  1023. const bool over = enabled && inRect(xx, yy, width, height);
  1024. const bool res = buttonLogic(id, over);
  1025. if (isHot(id) )
  1026. {
  1027. drawRoundedRect( (float)xx
  1028. , (float)yy
  1029. , (float)width
  1030. , (float)height
  1031. , 2.0f
  1032. , imguiRGBA(255, 196, 0, isActive(id) ? 196 : 96)
  1033. );
  1034. }
  1035. if (enabled)
  1036. {
  1037. drawText(xx + BUTTON_HEIGHT / 2
  1038. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1039. , ImguiTextAlign::Left
  1040. , _text
  1041. , imguiRGBA(255, 255, 255, 200)
  1042. );
  1043. }
  1044. else
  1045. {
  1046. drawText(xx + BUTTON_HEIGHT / 2
  1047. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1048. , ImguiTextAlign::Left
  1049. , _text
  1050. , imguiRGBA(128, 128, 128, 200)
  1051. );
  1052. }
  1053. return res;
  1054. }
  1055. bool check(const char* _text, bool _checked, bool _enabled)
  1056. {
  1057. const uint32_t id = getId();
  1058. Area& area = getCurrentArea();
  1059. const int32_t xx = area.m_widgetX;
  1060. const int32_t yy = area.m_widgetY;
  1061. const int32_t width = area.m_widgetW;
  1062. const int32_t height = BUTTON_HEIGHT;
  1063. area.m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  1064. const bool enabled = _enabled && isEnabled(m_areaId);
  1065. const bool over = enabled && inRect(xx, yy, width, height);
  1066. const bool res = buttonLogic(id, over);
  1067. const int32_t cx = xx + BUTTON_HEIGHT / 2 - CHECK_SIZE / 2;
  1068. const int32_t cy = yy + BUTTON_HEIGHT / 2 - CHECK_SIZE / 2;
  1069. drawRoundedRect( (float)cx - 3
  1070. , (float)cy - 3
  1071. , (float)CHECK_SIZE + 6
  1072. , (float)CHECK_SIZE + 6
  1073. , 4
  1074. , imguiRGBA(128, 128, 128, isActive(id) ? 196 : 96)
  1075. );
  1076. if (_checked)
  1077. {
  1078. if (enabled)
  1079. {
  1080. drawRoundedRect( (float)cx
  1081. , (float)cy
  1082. , (float)CHECK_SIZE
  1083. , (float)CHECK_SIZE
  1084. , (float)CHECK_SIZE / 2 - 1
  1085. , imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200)
  1086. );
  1087. }
  1088. else
  1089. {
  1090. drawRoundedRect( (float)cx
  1091. , (float)cy
  1092. , (float)CHECK_SIZE
  1093. , (float)CHECK_SIZE
  1094. , (float)CHECK_SIZE / 2 - 1
  1095. , imguiRGBA(128, 128, 128, 200)
  1096. );
  1097. }
  1098. }
  1099. if (enabled)
  1100. {
  1101. drawText(xx + BUTTON_HEIGHT
  1102. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1103. , ImguiTextAlign::Left
  1104. , _text
  1105. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  1106. );
  1107. }
  1108. else
  1109. {
  1110. drawText(xx + BUTTON_HEIGHT
  1111. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1112. , ImguiTextAlign::Left
  1113. , _text
  1114. , imguiRGBA(128, 128, 128, 200)
  1115. );
  1116. }
  1117. return res;
  1118. }
  1119. void input(const char* _label, char* _str, uint32_t _len, bool _enabled, ImguiAlign::Enum _align, int32_t _r)
  1120. {
  1121. const uint32_t id = getId();
  1122. Area& area = getCurrentArea();
  1123. const int32_t yy = area.m_widgetY;
  1124. area.m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  1125. int32_t xx;
  1126. int32_t width;
  1127. if (ImguiAlign::Left == _align)
  1128. {
  1129. xx = area.m_contentX + SCROLL_AREA_PADDING;
  1130. width = area.m_widgetW;
  1131. }
  1132. else if (ImguiAlign::LeftIndented == _align
  1133. || ImguiAlign::Right == _align)
  1134. {
  1135. xx = area.m_widgetX;
  1136. width = area.m_widgetW-1; //TODO: -1 !
  1137. }
  1138. else //if (ImguiAlign::Center == _align
  1139. //|| ImguiAlign::CenterIndented == _align).
  1140. {
  1141. xx = area.m_widgetX;
  1142. width = area.m_widgetW - (area.m_widgetX-area.m_scissorX);
  1143. }
  1144. const bool drawLabel = (NULL != _label && _label[0] != '\0');
  1145. if (drawLabel)
  1146. {
  1147. drawText(xx
  1148. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1149. , ImguiTextAlign::Left
  1150. , _label
  1151. , imguiRGBA(255, 255, 255, 200)
  1152. );
  1153. }
  1154. // Handle input.
  1155. if (isActiveInputField(id) )
  1156. {
  1157. const size_t cursor = size_t(strlen(_str));
  1158. if (m_char == 0x08) //backspace
  1159. {
  1160. _str[cursor-1] = '\0';
  1161. }
  1162. else if (m_char == 0x0d || m_char == 0x1b) //enter or escape
  1163. {
  1164. clearActiveInputField();
  1165. }
  1166. else if (cursor < _len-1
  1167. && 0 != m_char)
  1168. {
  1169. _str[cursor] = m_char;
  1170. _str[cursor+1] = '\0';
  1171. }
  1172. }
  1173. // Draw input area.
  1174. const int32_t height = BUTTON_HEIGHT;
  1175. if (drawLabel)
  1176. {
  1177. uint32_t numVertices = 0; //unused
  1178. const int32_t labelWidth = int32_t(getTextLength(m_fonts[m_currentFontIdx].m_cdata, _label, numVertices));
  1179. xx += (labelWidth + 6);
  1180. width -= (labelWidth + 6);
  1181. }
  1182. const bool enabled = _enabled && isEnabled(m_areaId);
  1183. const bool over = enabled && inRect(xx, yy, width, height);
  1184. inputLogic(id, over);
  1185. drawRoundedRect( (float)xx
  1186. , (float)yy
  1187. , (float)width
  1188. , (float)height
  1189. , (float)_r
  1190. , isActiveInputField(id)?imguiRGBA(255,196,0,255):imguiRGBA(128,128,128,96)
  1191. );
  1192. if (isActiveInputField(id) )
  1193. {
  1194. drawText(xx + 6
  1195. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1196. , ImguiTextAlign::Left
  1197. , _str
  1198. , imguiRGBA(0, 0, 0, 255)
  1199. );
  1200. }
  1201. else
  1202. {
  1203. drawText(xx + 6
  1204. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1205. , ImguiTextAlign::Left
  1206. , _str
  1207. , isHot(id) ? imguiRGBA(255,196,0,255) : imguiRGBA(255,255,255,255)
  1208. );
  1209. }
  1210. }
  1211. uint8_t tabs(uint8_t _selected, bool _enabled, ImguiAlign::Enum _align, int32_t _height, int32_t _r, uint8_t _nTabs, uint8_t _nEnabled, va_list _argList)
  1212. {
  1213. const char* titles[16];
  1214. bool tabEnabled[16];
  1215. const uint8_t tabCount = IMGUI_MIN(_nTabs, 16);
  1216. const uint8_t enabledCount = IMGUI_MIN(_nEnabled, 16);
  1217. // Read titles.
  1218. {
  1219. uint8_t ii = 0;
  1220. for (; ii < tabCount; ++ii)
  1221. {
  1222. const char* str = va_arg(_argList, const char*);
  1223. titles[ii] = str;
  1224. }
  1225. for (; ii < _nTabs; ++ii)
  1226. {
  1227. const char* str = va_arg(_argList, const char*);
  1228. BX_UNUSED(str);
  1229. }
  1230. }
  1231. // Read enabled tabs.
  1232. {
  1233. uint8_t ii = 0;
  1234. for (; ii < enabledCount; ++ii)
  1235. {
  1236. const bool enabled = (0 != va_arg(_argList, int) );
  1237. tabEnabled[ii] = enabled;
  1238. }
  1239. for (; ii < _nEnabled; ++ii)
  1240. {
  1241. const int enabled = va_arg(_argList, int);
  1242. BX_UNUSED(enabled);
  1243. }
  1244. for (; ii < _nTabs; ++ii)
  1245. {
  1246. tabEnabled[ii] = true;
  1247. }
  1248. }
  1249. Area& area = getCurrentArea();
  1250. const int32_t yy = area.m_widgetY;
  1251. area.m_widgetY += _height + DEFAULT_SPACING;
  1252. int32_t xx;
  1253. int32_t width;
  1254. if (ImguiAlign::Left == _align)
  1255. {
  1256. xx = area.m_contentX + SCROLL_AREA_PADDING;
  1257. width = area.m_widgetW;
  1258. }
  1259. else if (ImguiAlign::LeftIndented == _align
  1260. || ImguiAlign::Right == _align)
  1261. {
  1262. xx = area.m_widgetX;
  1263. width = area.m_widgetW-1; //TODO: -1 !
  1264. }
  1265. else //if (ImguiAlign::Center == _align
  1266. //|| ImguiAlign::CenterIndented == _align).
  1267. {
  1268. xx = area.m_widgetX;
  1269. width = area.m_widgetW - (area.m_widgetX-area.m_scissorX);
  1270. }
  1271. uint8_t selected = _selected;
  1272. const int32_t tabWidth = width / tabCount;
  1273. const int32_t tabWidthHalf = width / (tabCount*2);
  1274. const int32_t textY = yy + _height/2 + int32_t(m_fonts[m_currentFontIdx].m_size)/2 - 2;
  1275. drawRoundedRect( (float)xx
  1276. , (float)yy
  1277. , (float)width
  1278. , (float)_height
  1279. , (float)_r
  1280. , _enabled?imguiRGBA(128,128,128,96):imguiRGBA(128,128,128,64)
  1281. );
  1282. for (uint8_t ii = 0; ii < tabCount; ++ii)
  1283. {
  1284. const uint32_t id = getId();
  1285. int32_t buttonX = xx + ii*width/tabCount;
  1286. int32_t textX = buttonX + tabWidthHalf;
  1287. const bool enabled = _enabled && tabEnabled[ii] && isEnabled(m_areaId);
  1288. const bool over = enabled && inRect(buttonX, yy, tabWidth, _height);
  1289. const bool res = buttonLogic(id, over);
  1290. if (res)
  1291. {
  1292. selected = ii;
  1293. }
  1294. uint32_t textColor;
  1295. if (ii == selected)
  1296. {
  1297. textColor = enabled?imguiRGBA(0,0,0,255):imguiRGBA(255,255,255,100);
  1298. drawRoundedRect( (float)buttonX
  1299. , (float)yy
  1300. , (float)tabWidth
  1301. , (float)_height
  1302. , (float)_r
  1303. , enabled?imguiRGBA(255,196,0,200):imguiRGBA(128,128,128,32)
  1304. );
  1305. }
  1306. else
  1307. {
  1308. textColor = isHot(id) ? imguiRGBA(255, 196, 0, enabled?255:100) : imguiRGBA(255, 255, 255, enabled?200:100);
  1309. }
  1310. drawText(textX
  1311. , textY
  1312. , ImguiTextAlign::Center
  1313. , titles[ii]
  1314. , textColor
  1315. );
  1316. }
  1317. return selected;
  1318. }
  1319. bool image(bgfx::TextureHandle _image, float _lod, int32_t _width, int32_t _height, ImguiAlign::Enum _align, bool _enabled, bool _originBottomLeft)
  1320. {
  1321. const uint32_t id = getId();
  1322. Area& area = getCurrentArea();
  1323. int32_t xx;
  1324. if (ImguiAlign::Left == _align)
  1325. {
  1326. xx = area.m_contentX + SCROLL_AREA_PADDING;
  1327. }
  1328. else if (ImguiAlign::LeftIndented == _align)
  1329. {
  1330. xx = area.m_widgetX;
  1331. }
  1332. else if (ImguiAlign::Center == _align)
  1333. {
  1334. xx = area.m_contentX + (area.m_widgetW-_width)/2;
  1335. }
  1336. else if (ImguiAlign::CenterIndented == _align)
  1337. {
  1338. xx = (area.m_widgetX + area.m_widgetW + area.m_contentX - _width)/2;
  1339. }
  1340. else //if (ImguiAlign::Right == _align).
  1341. {
  1342. xx = area.m_contentX + area.m_widgetW - _width;
  1343. }
  1344. const int32_t yy = area.m_widgetY;
  1345. area.m_widgetY += _height + DEFAULT_SPACING;
  1346. const bool enabled = _enabled && isEnabled(m_areaId);
  1347. const bool over = enabled && inRect(xx, yy, _width, _height);
  1348. const bool res = buttonLogic(id, over);
  1349. const float lodEnabled[4] = { _lod, float(enabled), 0.0f, 0.0f };
  1350. screenQuad(xx, yy, _width, _height, _originBottomLeft);
  1351. bgfx::setUniform(u_imageLodEnabled, lodEnabled);
  1352. bgfx::setTexture(0, s_texColor, bgfx::isValid(_image) ? _image : m_missingTexture);
  1353. bgfx::setState(BGFX_STATE_RGB_WRITE
  1354. |BGFX_STATE_ALPHA_WRITE
  1355. |BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  1356. );
  1357. bgfx::setProgram(m_imageProgram);
  1358. setCurrentScissor();
  1359. bgfx::submit(m_view);
  1360. return res;
  1361. }
  1362. bool image(bgfx::TextureHandle _image, float _lod, float _width, float _aspect, ImguiAlign::Enum _align, bool _enabled, bool _originBottomLeft)
  1363. {
  1364. const float width = _width*float(getCurrentArea().m_widgetW);
  1365. const float height = width/_aspect;
  1366. return image(_image, _lod, int32_t(width), int32_t(height), _align, _enabled, _originBottomLeft);
  1367. }
  1368. bool imageChannel(bgfx::TextureHandle _image, uint8_t _channel, float _lod, int32_t _width, int32_t _height, ImguiAlign::Enum _align, bool _enabled)
  1369. {
  1370. BX_CHECK(_channel < 4, "Channel param must be from 0 to 3!");
  1371. const uint32_t id = getId();
  1372. Area& area = getCurrentArea();
  1373. int32_t xx;
  1374. if (ImguiAlign::Left == _align)
  1375. {
  1376. xx = area.m_contentX + SCROLL_AREA_PADDING;
  1377. }
  1378. else if (ImguiAlign::LeftIndented == _align)
  1379. {
  1380. xx = area.m_widgetX;
  1381. }
  1382. else if (ImguiAlign::Center == _align)
  1383. {
  1384. xx = area.m_contentX + (area.m_widgetW-_width)/2;
  1385. }
  1386. else if (ImguiAlign::CenterIndented == _align)
  1387. {
  1388. xx = (area.m_widgetX + area.m_widgetW + area.m_contentX - _width)/2;
  1389. }
  1390. else //if (ImguiAlign::Right == _align).
  1391. {
  1392. xx = area.m_contentX + area.m_widgetW - _width;
  1393. }
  1394. const int32_t yy = area.m_widgetY;
  1395. area.m_widgetY += _height + DEFAULT_SPACING;
  1396. const bool enabled = _enabled && isEnabled(m_areaId);
  1397. const bool over = enabled && inRect(xx, yy, _width, _height);
  1398. const bool res = buttonLogic(id, over);
  1399. screenQuad(xx, yy, _width, _height);
  1400. const float lodEnabled[4] = { _lod, float(enabled), 0.0f, 0.0f };
  1401. bgfx::setUniform(u_imageLodEnabled, lodEnabled);
  1402. float swizz[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  1403. swizz[_channel] = 1.0f;
  1404. bgfx::setUniform(u_imageSwizzle, swizz);
  1405. bgfx::setTexture(0, s_texColor, bgfx::isValid(_image) ? _image : m_missingTexture);
  1406. bgfx::setState(BGFX_STATE_RGB_WRITE
  1407. |BGFX_STATE_ALPHA_WRITE
  1408. |BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  1409. );
  1410. bgfx::setProgram(m_imageSwizzProgram);
  1411. setCurrentScissor();
  1412. bgfx::submit(m_view);
  1413. return res;
  1414. }
  1415. bool imageChannel(bgfx::TextureHandle _image, uint8_t _channel, float _lod, float _width, float _aspect, ImguiAlign::Enum _align, bool _enabled)
  1416. {
  1417. const float width = _width*float(getCurrentArea().m_widgetW);
  1418. const float height = width/_aspect;
  1419. return imageChannel(_image, _channel, _lod, int32_t(width), int32_t(height), _align, _enabled);
  1420. }
  1421. bool cubeMap(bgfx::TextureHandle _cubemap, float _lod, bool _cross, ImguiAlign::Enum _align, bool _enabled)
  1422. {
  1423. const uint32_t numVertices = 14;
  1424. const uint32_t numIndices = 36;
  1425. if (bgfx::checkAvailTransientBuffers(numVertices, PosNormalVertex::ms_decl, numIndices) )
  1426. {
  1427. bgfx::TransientVertexBuffer tvb;
  1428. bgfx::allocTransientVertexBuffer(&tvb, numVertices, PosNormalVertex::ms_decl);
  1429. bgfx::TransientIndexBuffer tib;
  1430. bgfx::allocTransientIndexBuffer(&tib, numIndices);
  1431. PosNormalVertex* vertex = (PosNormalVertex*)tvb.data;
  1432. uint16_t* indices = (uint16_t*)tib.data;
  1433. if (_cross)
  1434. {
  1435. vertex->set(0.0f, 0.5f, 0.0f, -1.0f, 1.0f, -1.0f); ++vertex;
  1436. vertex->set(0.0f, 1.0f, 0.0f, -1.0f, -1.0f, -1.0f); ++vertex;
  1437. vertex->set(0.5f, 0.0f, 0.0f, -1.0f, 1.0f, -1.0f); ++vertex;
  1438. vertex->set(0.5f, 0.5f, 0.0f, -1.0f, 1.0f, 1.0f); ++vertex;
  1439. vertex->set(0.5f, 1.0f, 0.0f, -1.0f, -1.0f, 1.0f); ++vertex;
  1440. vertex->set(0.5f, 1.5f, 0.0f, -1.0f, -1.0f, -1.0f); ++vertex;
  1441. vertex->set(1.0f, 0.0f, 0.0f, 1.0f, 1.0f, -1.0f); ++vertex;
  1442. vertex->set(1.0f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f); ++vertex;
  1443. vertex->set(1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f); ++vertex;
  1444. vertex->set(1.0f, 1.5f, 0.0f, 1.0f, -1.0f, -1.0f); ++vertex;
  1445. vertex->set(1.5f, 0.5f, 0.0f, 1.0f, 1.0f, -1.0f); ++vertex;
  1446. vertex->set(1.5f, 1.0f, 0.0f, 1.0f, -1.0f, -1.0f); ++vertex;
  1447. vertex->set(2.0f, 0.5f, 0.0f, -1.0f, 1.0f, -1.0f); ++vertex;
  1448. vertex->set(2.0f, 1.0f, 0.0f, -1.0f, -1.0f, -1.0f); ++vertex;
  1449. indices += addQuad(indices, 0, 3, 4, 1);
  1450. indices += addQuad(indices, 2, 6, 7, 3);
  1451. indices += addQuad(indices, 3, 7, 8, 4);
  1452. indices += addQuad(indices, 4, 8, 9, 5);
  1453. indices += addQuad(indices, 7, 10, 11, 8);
  1454. indices += addQuad(indices, 10, 12, 13, 11);
  1455. }
  1456. else
  1457. {
  1458. vertex->set(0.0f, 0.25f, 0.0f, -1.0f, 1.0f, -1.0f); ++vertex;
  1459. vertex->set(0.0f, 0.75f, 0.0f, -1.0f, -1.0f, -1.0f); ++vertex;
  1460. vertex->set(0.5f, 0.00f, 0.0f, -1.0f, 1.0f, 1.0f); ++vertex;
  1461. vertex->set(0.5f, 0.50f, 0.0f, -1.0f, -1.0f, 1.0f); ++vertex;
  1462. vertex->set(0.5f, 1.00f, 0.0f, 1.0f, -1.0f, -1.0f); ++vertex;
  1463. vertex->set(1.0f, 0.25f, 0.0f, 1.0f, 1.0f, 1.0f); ++vertex;
  1464. vertex->set(1.0f, 0.75f, 0.0f, 1.0f, -1.0f, 1.0f); ++vertex;
  1465. vertex->set(1.0f, 0.25f, 0.0f, 1.0f, 1.0f, 1.0f); ++vertex;
  1466. vertex->set(1.0f, 0.75f, 0.0f, 1.0f, -1.0f, 1.0f); ++vertex;
  1467. vertex->set(1.5f, 0.00f, 0.0f, -1.0f, 1.0f, 1.0f); ++vertex;
  1468. vertex->set(1.5f, 0.50f, 0.0f, 1.0f, 1.0f, -1.0f); ++vertex;
  1469. vertex->set(1.5f, 1.00f, 0.0f, 1.0f, -1.0f, -1.0f); ++vertex;
  1470. vertex->set(2.0f, 0.25f, 0.0f, -1.0f, 1.0f, -1.0f); ++vertex;
  1471. vertex->set(2.0f, 0.75f, 0.0f, -1.0f, -1.0f, -1.0f); ++vertex;
  1472. indices += addQuad(indices, 0, 2, 3, 1);
  1473. indices += addQuad(indices, 1, 3, 6, 4);
  1474. indices += addQuad(indices, 2, 5, 6, 3);
  1475. indices += addQuad(indices, 7, 9, 12, 10);
  1476. indices += addQuad(indices, 7, 10, 11, 8);
  1477. indices += addQuad(indices, 10, 12, 13, 11);
  1478. }
  1479. const uint32_t id = getId();
  1480. Area& area = getCurrentArea();
  1481. int32_t xx;
  1482. int32_t width;
  1483. if (ImguiAlign::Left == _align)
  1484. {
  1485. xx = area.m_contentX + SCROLL_AREA_PADDING;
  1486. width = area.m_widgetW;
  1487. }
  1488. else if (ImguiAlign::LeftIndented == _align
  1489. || ImguiAlign::Right == _align)
  1490. {
  1491. xx = area.m_widgetX;
  1492. width = area.m_widgetW;
  1493. }
  1494. else //if (ImguiAlign::Center == _align
  1495. //|| ImguiAlign::CenterIndented == _align).
  1496. {
  1497. xx = area.m_widgetX;
  1498. width = area.m_widgetW - (area.m_widgetX-area.m_scissorX);
  1499. }
  1500. const uint32_t height = _cross ? (width*3)/4 : (width/2);
  1501. const int32_t yy = area.m_widgetY;
  1502. area.m_widgetY += height + DEFAULT_SPACING;
  1503. const bool enabled = _enabled && isEnabled(m_areaId);
  1504. const bool over = enabled && inRect(xx, yy, width, height);
  1505. const bool res = buttonLogic(id, over);
  1506. const float scale = float(width/2)+0.25f;
  1507. float mtx[16];
  1508. bx::mtxSRT(mtx, scale, scale, 1.0f, 0.0f, 0.0f, 0.0f, float(xx), float(yy), 0.0f);
  1509. const float lodEnabled[4] = { _lod, float(enabled), 0.0f, 0.0f };
  1510. bgfx::setUniform(u_imageLodEnabled, lodEnabled);
  1511. bgfx::setTransform(mtx);
  1512. bgfx::setTexture(0, s_texColor, _cubemap);
  1513. bgfx::setProgram(m_cubeMapProgram);
  1514. bgfx::setVertexBuffer(&tvb);
  1515. bgfx::setIndexBuffer(&tib);
  1516. bgfx::setState(BGFX_STATE_RGB_WRITE
  1517. |BGFX_STATE_ALPHA_WRITE
  1518. |BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  1519. );
  1520. setCurrentScissor();
  1521. bgfx::submit(m_view);
  1522. return res;
  1523. }
  1524. return false;
  1525. }
  1526. bool collapse(const char* _text, const char* _subtext, bool _checked, bool _enabled)
  1527. {
  1528. const uint32_t id = getId();
  1529. Area& area = getCurrentArea();
  1530. const int32_t xx = area.m_widgetX;
  1531. const int32_t yy = area.m_widgetY;
  1532. const int32_t width = area.m_widgetW;
  1533. const int32_t height = BUTTON_HEIGHT;
  1534. area.m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  1535. const int32_t cx = xx + BUTTON_HEIGHT/2 - CHECK_SIZE/2;
  1536. const int32_t cy = yy + BUTTON_HEIGHT/2 - CHECK_SIZE/2 + DEFAULT_SPACING/2;
  1537. const int32_t textY = yy + BUTTON_HEIGHT/2 + TEXT_HEIGHT/2 + DEFAULT_SPACING/2;
  1538. const bool enabled = _enabled && isEnabled(m_areaId);
  1539. const bool over = enabled && inRect(xx, yy, width, height);
  1540. const bool res = buttonLogic(id, over);
  1541. if (_checked)
  1542. {
  1543. drawTriangle(cx
  1544. , cy
  1545. , CHECK_SIZE
  1546. , CHECK_SIZE
  1547. , TriangleOrientation::Up
  1548. , imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200)
  1549. );
  1550. }
  1551. else
  1552. {
  1553. drawTriangle(cx-1 // With -1 is more aesthetically pleasing.
  1554. , cy
  1555. , CHECK_SIZE
  1556. , CHECK_SIZE
  1557. , TriangleOrientation::Right
  1558. , imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200)
  1559. );
  1560. }
  1561. if (enabled)
  1562. {
  1563. drawText(xx + BUTTON_HEIGHT
  1564. , textY
  1565. , ImguiTextAlign::Left
  1566. , _text
  1567. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  1568. );
  1569. }
  1570. else
  1571. {
  1572. drawText(xx + BUTTON_HEIGHT
  1573. , textY
  1574. , ImguiTextAlign::Left
  1575. , _text
  1576. , imguiRGBA(128, 128, 128, 200)
  1577. );
  1578. }
  1579. if (_subtext)
  1580. {
  1581. drawText(xx + width - BUTTON_HEIGHT / 2
  1582. , textY
  1583. , ImguiTextAlign::Right
  1584. , _subtext
  1585. , imguiRGBA(255, 255, 255, 128)
  1586. );
  1587. }
  1588. return res;
  1589. }
  1590. bool borderButton(ImguiBorder::Enum _border, bool _checked, bool _enabled)
  1591. {
  1592. // Since border button isn't part of any area, just use this custom/unique areaId.
  1593. const uint16_t areaId = UINT16_MAX-1;
  1594. const uint32_t id = (areaId << 16) | m_widgetId++;
  1595. updatePresence(id);
  1596. const int32_t triSize = 12;
  1597. const int32_t borderSize = 15;
  1598. int32_t xx;
  1599. int32_t yy;
  1600. int32_t width;
  1601. int32_t height;
  1602. int32_t triX;
  1603. int32_t triY;
  1604. TriangleOrientation::Enum orientation;
  1605. if (ImguiBorder::Left == _border)
  1606. {
  1607. xx = -borderSize;
  1608. yy = 0;
  1609. width = 2*borderSize;
  1610. height = m_viewHeight;
  1611. triX = 0;
  1612. triY = (m_viewHeight-triSize)/2;
  1613. orientation = _checked ? TriangleOrientation::Left : TriangleOrientation::Right;
  1614. }
  1615. else if (ImguiBorder::Right == _border)
  1616. {
  1617. xx = m_viewWidth - borderSize;
  1618. yy = 0;
  1619. width = 2*borderSize;
  1620. height = m_viewHeight;
  1621. triX = m_viewWidth - triSize - 2;
  1622. triY = (m_viewHeight-width)/2;
  1623. orientation = _checked ? TriangleOrientation::Right : TriangleOrientation::Left;
  1624. }
  1625. else if (ImguiBorder::Top == _border)
  1626. {
  1627. xx = 0;
  1628. yy = -borderSize;
  1629. width = m_viewWidth;
  1630. height = 2*borderSize;
  1631. triX = (m_viewWidth-triSize)/2;
  1632. triY = 0;
  1633. orientation = _checked ? TriangleOrientation::Up : TriangleOrientation::Down;
  1634. }
  1635. else //if (ImguiBorder::Bottom == _border).
  1636. {
  1637. xx = 0;
  1638. yy = m_viewHeight - borderSize;
  1639. width = m_viewWidth;
  1640. height = 2*borderSize;
  1641. triX = (m_viewWidth-triSize)/2;
  1642. triY = m_viewHeight-triSize;
  1643. orientation = _checked ? TriangleOrientation::Down : TriangleOrientation::Up;
  1644. }
  1645. const bool over = _enabled && inRect(xx, yy, width, height, false);
  1646. const bool res = buttonLogic(id, over);
  1647. drawRect( (float)xx
  1648. , (float)yy
  1649. , (float)width
  1650. , (float)height
  1651. , isActive(id) ? imguiRGBA(23, 23, 23, 192) : imguiRGBA(0, 0, 0, 222)
  1652. );
  1653. drawTriangle( triX
  1654. , triY
  1655. , triSize
  1656. , triSize
  1657. , orientation
  1658. , isHot(id) ? imguiRGBA(255, 196, 0, 222) : imguiRGBA(255, 255, 255, 192)
  1659. );
  1660. return res;
  1661. }
  1662. void labelVargs(const char* _format, va_list _argList, uint32_t _rgba)
  1663. {
  1664. char temp[8192];
  1665. char* out = temp;
  1666. int32_t len = bx::vsnprintf(out, sizeof(temp), _format, _argList);
  1667. if ( (int32_t)sizeof(temp) < len)
  1668. {
  1669. out = (char*)alloca(len+1);
  1670. len = bx::vsnprintf(out, len, _format, _argList);
  1671. }
  1672. out[len] = '\0';
  1673. Area& area = getCurrentArea();
  1674. const int32_t xx = area.m_widgetX;
  1675. const int32_t yy = area.m_widgetY;
  1676. area.m_widgetY += BUTTON_HEIGHT;
  1677. drawText(xx
  1678. , yy + BUTTON_HEIGHT/2 + TEXT_HEIGHT/2
  1679. , ImguiTextAlign::Left
  1680. , out
  1681. , _rgba
  1682. );
  1683. }
  1684. void value(const char* _text)
  1685. {
  1686. Area& area = getCurrentArea();
  1687. const int32_t xx = area.m_widgetX;
  1688. const int32_t yy = area.m_widgetY;
  1689. const int32_t ww = area.m_widgetW;
  1690. area.m_widgetY += BUTTON_HEIGHT;
  1691. drawText(xx + ww - BUTTON_HEIGHT / 2
  1692. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1693. , ImguiTextAlign::Right
  1694. , _text
  1695. , imguiRGBA(255, 255, 255, 200)
  1696. );
  1697. }
  1698. bool slider(const char* _text, float& _val, float _vmin, float _vmax, float _vinc, bool _enabled, ImguiAlign::Enum _align)
  1699. {
  1700. const uint32_t id = getId();
  1701. Area& area = getCurrentArea();
  1702. const int32_t yy = area.m_widgetY;
  1703. const int32_t height = SLIDER_HEIGHT;
  1704. area.m_widgetY += SLIDER_HEIGHT + DEFAULT_SPACING;
  1705. int32_t xx;
  1706. int32_t width;
  1707. if (ImguiAlign::Left == _align)
  1708. {
  1709. xx = area.m_contentX + SCROLL_AREA_PADDING;
  1710. width = area.m_widgetW;
  1711. }
  1712. else if (ImguiAlign::LeftIndented == _align
  1713. || ImguiAlign::Right == _align)
  1714. {
  1715. xx = area.m_widgetX;
  1716. width = area.m_widgetW;
  1717. }
  1718. else //if (ImguiAlign::Center == _align
  1719. //|| ImguiAlign::CenterIndented == _align).
  1720. {
  1721. xx = area.m_widgetX;
  1722. width = area.m_widgetW - (area.m_widgetX-area.m_scissorX);
  1723. }
  1724. drawRoundedRect( (float)xx, (float)yy, (float)width, (float)height, 4.0f, imguiRGBA(0, 0, 0, 128) );
  1725. const int32_t range = width - SLIDER_MARKER_WIDTH;
  1726. float uu = bx::fsaturate( (_val - _vmin) / (_vmax - _vmin) );
  1727. int32_t m = (int)(uu * range);
  1728. bool valChanged = false;
  1729. const bool enabled = _enabled && isEnabled(m_areaId);
  1730. const bool over = enabled && inRect(xx + m, yy, SLIDER_MARKER_WIDTH, SLIDER_HEIGHT);
  1731. const bool res = buttonLogic(id, over);
  1732. if (isActive(id) )
  1733. {
  1734. if (m_wentActive)
  1735. {
  1736. m_dragX = m_mx;
  1737. m_dragOrig = uu;
  1738. }
  1739. if (m_dragX != m_mx)
  1740. {
  1741. uu = bx::fsaturate(m_dragOrig + (float)(m_mx - m_dragX) / (float)range);
  1742. _val = _vmin + uu * (_vmax - _vmin);
  1743. _val = floorf(_val / _vinc + 0.5f) * _vinc; // Snap to vinc
  1744. m = (int)(uu * range);
  1745. valChanged = true;
  1746. }
  1747. }
  1748. if (isActive(id) )
  1749. {
  1750. drawRoundedRect( (float)(xx + m)
  1751. , (float)yy
  1752. , (float)SLIDER_MARKER_WIDTH
  1753. , (float)SLIDER_HEIGHT
  1754. , 4.0f
  1755. , imguiRGBA(255, 255, 255, 255)
  1756. );
  1757. }
  1758. else
  1759. {
  1760. drawRoundedRect( (float)(xx + m)
  1761. , (float)yy
  1762. , (float)SLIDER_MARKER_WIDTH
  1763. , (float)SLIDER_HEIGHT
  1764. , 4.0f
  1765. , isHot(id) ? imguiRGBA(255, 196, 0, 128) : imguiRGBA(255, 255, 255, 64)
  1766. );
  1767. }
  1768. // TODO: fix this, take a look at 'nicenum'.
  1769. int32_t digits = (int)(ceilf(log10f(_vinc) ) );
  1770. char fmt[16];
  1771. bx::snprintf(fmt, 16, "%%.%df", digits >= 0 ? 0 : -digits);
  1772. char msg[128];
  1773. bx::snprintf(msg, 128, fmt, _val);
  1774. if (enabled)
  1775. {
  1776. drawText(xx + SLIDER_HEIGHT / 2
  1777. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  1778. , ImguiTextAlign::Left
  1779. , _text
  1780. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  1781. );
  1782. drawText(xx + width - SLIDER_HEIGHT / 2
  1783. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  1784. , ImguiTextAlign::Right
  1785. , msg
  1786. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  1787. );
  1788. }
  1789. else
  1790. {
  1791. drawText(xx + SLIDER_HEIGHT / 2
  1792. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  1793. , ImguiTextAlign::Left
  1794. , _text
  1795. , imguiRGBA(128, 128, 128, 200)
  1796. );
  1797. drawText(xx + width - SLIDER_HEIGHT / 2
  1798. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  1799. , ImguiTextAlign::Right
  1800. , msg
  1801. , imguiRGBA(128, 128, 128, 200)
  1802. );
  1803. }
  1804. return res || valChanged;
  1805. }
  1806. void indent(uint16_t _width)
  1807. {
  1808. Area& area = getCurrentArea();
  1809. area.m_widgetX += _width;
  1810. area.m_widgetW -= _width;
  1811. }
  1812. void unindent(uint16_t _width)
  1813. {
  1814. Area& area = getCurrentArea();
  1815. area.m_widgetX -= _width;
  1816. area.m_widgetW += _width;
  1817. }
  1818. void separator(uint16_t _height)
  1819. {
  1820. Area& area = getCurrentArea();
  1821. area.m_widgetY += _height;
  1822. }
  1823. void separatorLine(uint16_t _height)
  1824. {
  1825. Area& area = getCurrentArea();
  1826. const int32_t rectWidth = area.m_widgetW;
  1827. const int32_t rectHeight = 1;
  1828. const int32_t xx = area.m_widgetX;
  1829. const int32_t yy = area.m_widgetY + _height/2 - rectHeight;
  1830. area.m_widgetY += _height;
  1831. drawRect( (float)xx
  1832. , (float)yy
  1833. , (float)rectWidth
  1834. , (float)rectHeight
  1835. , imguiRGBA(255, 255, 255, 32)
  1836. );
  1837. }
  1838. void drawPolygon(const float* _coords, uint32_t _numCoords, float _r, uint32_t _abgr)
  1839. {
  1840. _numCoords = bx::uint32_min(_numCoords, MAX_TEMP_COORDS);
  1841. for (uint32_t ii = 0, jj = _numCoords - 1; ii < _numCoords; jj = ii++)
  1842. {
  1843. const float* v0 = &_coords[jj * 2];
  1844. const float* v1 = &_coords[ii * 2];
  1845. float dx = v1[0] - v0[0];
  1846. float dy = v1[1] - v0[1];
  1847. float d = sqrtf(dx * dx + dy * dy);
  1848. if (d > 0)
  1849. {
  1850. d = 1.0f / d;
  1851. dx *= d;
  1852. dy *= d;
  1853. }
  1854. m_tempNormals[jj * 2 + 0] = dy;
  1855. m_tempNormals[jj * 2 + 1] = -dx;
  1856. }
  1857. for (uint32_t ii = 0, jj = _numCoords - 1; ii < _numCoords; jj = ii++)
  1858. {
  1859. float dlx0 = m_tempNormals[jj * 2 + 0];
  1860. float dly0 = m_tempNormals[jj * 2 + 1];
  1861. float dlx1 = m_tempNormals[ii * 2 + 0];
  1862. float dly1 = m_tempNormals[ii * 2 + 1];
  1863. float dmx = (dlx0 + dlx1) * 0.5f;
  1864. float dmy = (dly0 + dly1) * 0.5f;
  1865. float dmr2 = dmx * dmx + dmy * dmy;
  1866. if (dmr2 > 0.000001f)
  1867. {
  1868. float scale = 1.0f / dmr2;
  1869. if (scale > 10.0f)
  1870. {
  1871. scale = 10.0f;
  1872. }
  1873. dmx *= scale;
  1874. dmy *= scale;
  1875. }
  1876. m_tempCoords[ii * 2 + 0] = _coords[ii * 2 + 0] + dmx * _r;
  1877. m_tempCoords[ii * 2 + 1] = _coords[ii * 2 + 1] + dmy * _r;
  1878. }
  1879. uint32_t numVertices = _numCoords*6 + (_numCoords-2)*3;
  1880. if (bgfx::checkAvailTransientVertexBuffer(numVertices, PosColorVertex::ms_decl) )
  1881. {
  1882. bgfx::TransientVertexBuffer tvb;
  1883. bgfx::allocTransientVertexBuffer(&tvb, numVertices, PosColorVertex::ms_decl);
  1884. uint32_t trans = _abgr&0xffffff;
  1885. PosColorVertex* vertex = (PosColorVertex*)tvb.data;
  1886. for (uint32_t ii = 0, jj = _numCoords-1; ii < _numCoords; jj = ii++)
  1887. {
  1888. vertex->m_x = _coords[ii*2+0];
  1889. vertex->m_y = _coords[ii*2+1];
  1890. vertex->m_abgr = _abgr;
  1891. ++vertex;
  1892. vertex->m_x = _coords[jj*2+0];
  1893. vertex->m_y = _coords[jj*2+1];
  1894. vertex->m_abgr = _abgr;
  1895. ++vertex;
  1896. vertex->m_x = m_tempCoords[jj*2+0];
  1897. vertex->m_y = m_tempCoords[jj*2+1];
  1898. vertex->m_abgr = trans;
  1899. ++vertex;
  1900. vertex->m_x = m_tempCoords[jj*2+0];
  1901. vertex->m_y = m_tempCoords[jj*2+1];
  1902. vertex->m_abgr = trans;
  1903. ++vertex;
  1904. vertex->m_x = m_tempCoords[ii*2+0];
  1905. vertex->m_y = m_tempCoords[ii*2+1];
  1906. vertex->m_abgr = trans;
  1907. ++vertex;
  1908. vertex->m_x = _coords[ii*2+0];
  1909. vertex->m_y = _coords[ii*2+1];
  1910. vertex->m_abgr = _abgr;
  1911. ++vertex;
  1912. }
  1913. for (uint32_t ii = 2; ii < _numCoords; ++ii)
  1914. {
  1915. vertex->m_x = _coords[0];
  1916. vertex->m_y = _coords[1];
  1917. vertex->m_abgr = _abgr;
  1918. ++vertex;
  1919. vertex->m_x = _coords[(ii-1)*2+0];
  1920. vertex->m_y = _coords[(ii-1)*2+1];
  1921. vertex->m_abgr = _abgr;
  1922. ++vertex;
  1923. vertex->m_x = _coords[ii*2+0];
  1924. vertex->m_y = _coords[ii*2+1];
  1925. vertex->m_abgr = _abgr;
  1926. ++vertex;
  1927. }
  1928. bgfx::setVertexBuffer(&tvb);
  1929. bgfx::setState(0
  1930. | BGFX_STATE_RGB_WRITE
  1931. | BGFX_STATE_ALPHA_WRITE
  1932. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  1933. );
  1934. bgfx::setProgram(m_colorProgram);
  1935. setCurrentScissor();
  1936. bgfx::submit(m_view);
  1937. }
  1938. }
  1939. void drawRect(float _x, float _y, float _w, float _h, uint32_t _argb, float _fth = 1.0f)
  1940. {
  1941. float verts[4 * 2] =
  1942. {
  1943. _x + 0.5f, _y + 0.5f,
  1944. _x + _w - 0.5f, _y + 0.5f,
  1945. _x + _w - 0.5f, _y + _h - 0.5f,
  1946. _x + 0.5f, _y + _h - 0.5f,
  1947. };
  1948. drawPolygon(verts, 4, _fth, _argb);
  1949. }
  1950. void drawRoundedRect(float _x, float _y, float _w, float _h, float _r, uint32_t _argb, float _fth = 1.0f)
  1951. {
  1952. if (0.0f == _r)
  1953. {
  1954. return drawRect(_x, _y, _w, _h, _argb, _fth);
  1955. }
  1956. const uint32_t num = NUM_CIRCLE_VERTS / 4;
  1957. const float* cverts = m_circleVerts;
  1958. float verts[(num + 1) * 4 * 2];
  1959. float* vv = verts;
  1960. for (uint32_t ii = 0; ii <= num; ++ii)
  1961. {
  1962. *vv++ = _x + _w - _r + cverts[ii * 2] * _r;
  1963. *vv++ = _y + _h - _r + cverts[ii * 2 + 1] * _r;
  1964. }
  1965. for (uint32_t ii = num; ii <= num * 2; ++ii)
  1966. {
  1967. *vv++ = _x + _r + cverts[ii * 2] * _r;
  1968. *vv++ = _y + _h - _r + cverts[ii * 2 + 1] * _r;
  1969. }
  1970. for (uint32_t ii = num * 2; ii <= num * 3; ++ii)
  1971. {
  1972. *vv++ = _x + _r + cverts[ii * 2] * _r;
  1973. *vv++ = _y + _r + cverts[ii * 2 + 1] * _r;
  1974. }
  1975. for (uint32_t ii = num * 3; ii < num * 4; ++ii)
  1976. {
  1977. *vv++ = _x + _w - _r + cverts[ii * 2] * _r;
  1978. *vv++ = _y + _r + cverts[ii * 2 + 1] * _r;
  1979. }
  1980. *vv++ = _x + _w - _r + cverts[0] * _r;
  1981. *vv++ = _y + _r + cverts[1] * _r;
  1982. drawPolygon(verts, (num + 1) * 4, _fth, _argb);
  1983. }
  1984. void drawLine(float _x0, float _y0, float _x1, float _y1, float _r, uint32_t _abgr, float _fth = 1.0f)
  1985. {
  1986. float dx = _x1 - _x0;
  1987. float dy = _y1 - _y0;
  1988. float d = sqrtf(dx * dx + dy * dy);
  1989. if (d > 0.0001f)
  1990. {
  1991. d = 1.0f / d;
  1992. dx *= d;
  1993. dy *= d;
  1994. }
  1995. float nx = dy;
  1996. float ny = -dx;
  1997. float verts[4 * 2];
  1998. _r -= _fth;
  1999. _r *= 0.5f;
  2000. if (_r < 0.01f)
  2001. {
  2002. _r = 0.01f;
  2003. }
  2004. dx *= _r;
  2005. dy *= _r;
  2006. nx *= _r;
  2007. ny *= _r;
  2008. verts[0] = _x0 - dx - nx;
  2009. verts[1] = _y0 - dy - ny;
  2010. verts[2] = _x0 - dx + nx;
  2011. verts[3] = _y0 - dy + ny;
  2012. verts[4] = _x1 + dx + nx;
  2013. verts[5] = _y1 + dy + ny;
  2014. verts[6] = _x1 + dx - nx;
  2015. verts[7] = _y1 + dy - ny;
  2016. drawPolygon(verts, 4, _fth, _abgr);
  2017. }
  2018. struct TriangleOrientation
  2019. {
  2020. enum Enum
  2021. {
  2022. Left,
  2023. Right,
  2024. Up,
  2025. Down,
  2026. };
  2027. };
  2028. void drawTriangle(int32_t _x, int32_t _y, int32_t _width, int32_t _height, TriangleOrientation::Enum _orientation, uint32_t _abgr)
  2029. {
  2030. if (TriangleOrientation::Left == _orientation)
  2031. {
  2032. const float verts[3 * 2] =
  2033. {
  2034. (float)_x + 0.5f + (float)_width * 1.0f, (float)_y + 0.5f,
  2035. (float)_x + 0.5f, (float)_y + 0.5f + (float)_height / 2.0f - 0.5f,
  2036. (float)_x + 0.5f + (float)_width * 1.0f, (float)_y + 0.5f + (float)_height - 1.0f,
  2037. };
  2038. drawPolygon(verts, 3, 1.0f, _abgr);
  2039. }
  2040. else if (TriangleOrientation::Right == _orientation)
  2041. {
  2042. const float verts[3 * 2] =
  2043. {
  2044. (float)_x + 0.5f, (float)_y + 0.5f,
  2045. (float)_x + 0.5f + (float)_width * 1.0f, (float)_y + 0.5f + (float)_height / 2.0f - 0.5f,
  2046. (float)_x + 0.5f, (float)_y + 0.5f + (float)_height - 1.0f,
  2047. };
  2048. drawPolygon(verts, 3, 1.0f, _abgr);
  2049. }
  2050. else if (TriangleOrientation::Up == _orientation)
  2051. {
  2052. const float verts[3 * 2] =
  2053. {
  2054. (float)_x + 0.5f, (float)_y + 0.5f + (float)_height - 1.0f,
  2055. (float)_x + 0.5f + (float)_width / 2.0f - 0.5f, (float)_y + 0.5f,
  2056. (float)_x + 0.5f + (float)_width - 1.0f, (float)_y + 0.5f + (float)_height - 1.0f,
  2057. };
  2058. drawPolygon(verts, 3, 1.0f, _abgr);
  2059. }
  2060. else //if (TriangleOrientation::Down == _orientation).
  2061. {
  2062. const float verts[3 * 2] =
  2063. {
  2064. (float)_x + 0.5f, (float)_y + 0.5f,
  2065. (float)_x + 0.5f + (float)_width / 2.0f - 0.5f, (float)_y + 0.5f + (float)_height - 1.0f,
  2066. (float)_x + 0.5f + (float)_width - 1.0f, (float)_y + 0.5f,
  2067. };
  2068. drawPolygon(verts, 3, 1.0f, _abgr);
  2069. }
  2070. }
  2071. #if !USE_NANOVG_FONT
  2072. void getBakedQuad(stbtt_bakedchar* _chardata, int32_t char_index, float* _xpos, float* _ypos, stbtt_aligned_quad* _quad)
  2073. {
  2074. stbtt_bakedchar* b = _chardata + char_index;
  2075. int32_t round_x = STBTT_ifloor(*_xpos + b->xoff);
  2076. int32_t round_y = STBTT_ifloor(*_ypos + b->yoff);
  2077. _quad->x0 = (float)round_x;
  2078. _quad->y0 = (float)round_y;
  2079. _quad->x1 = (float)round_x + b->x1 - b->x0;
  2080. _quad->y1 = (float)round_y + b->y1 - b->y0;
  2081. _quad->s0 = (b->x0 + m_halfTexel) * m_invTextureWidth;
  2082. _quad->t0 = (b->y0 + m_halfTexel) * m_invTextureWidth;
  2083. _quad->s1 = (b->x1 + m_halfTexel) * m_invTextureHeight;
  2084. _quad->t1 = (b->y1 + m_halfTexel) * m_invTextureHeight;
  2085. *_xpos += b->xadvance;
  2086. }
  2087. #endif // !USE_NANOVG_FONT
  2088. void drawText(int32_t _x, int32_t _y, ImguiTextAlign::Enum _align, const char* _text, uint32_t _abgr)
  2089. {
  2090. drawText( (float)_x, (float)_y, _text, _align, _abgr);
  2091. }
  2092. void drawText(float _x, float _y, const char* _text, ImguiTextAlign::Enum _align, uint32_t _abgr)
  2093. {
  2094. if (NULL == _text
  2095. || '\0' == _text[0])
  2096. {
  2097. return;
  2098. }
  2099. #if USE_NANOVG_FONT
  2100. static uint32_t textAlign[ImguiTextAlign::Count] =
  2101. {
  2102. NVG_ALIGN_LEFT,
  2103. NVG_ALIGN_CENTER,
  2104. NVG_ALIGN_RIGHT,
  2105. };
  2106. nvgTextAlign(m_nvg, textAlign[_align]);
  2107. nvgFontBlur(m_nvg, 0.0f);
  2108. nvgFillColor(m_nvg, nvgRGBAu(_abgr) );
  2109. nvgText(m_nvg, _x, _y, _text, NULL);
  2110. #else
  2111. uint32_t numVertices = 0;
  2112. if (_align == ImguiTextAlign::Center)
  2113. {
  2114. _x -= getTextLength(m_fonts[m_currentFontIdx].m_cdata, _text, numVertices) / 2;
  2115. }
  2116. else if (_align == ImguiTextAlign::Right)
  2117. {
  2118. _x -= getTextLength(m_fonts[m_currentFontIdx].m_cdata, _text, numVertices);
  2119. }
  2120. else // just count vertices
  2121. {
  2122. getTextLength(m_fonts[m_currentFontIdx].m_cdata, _text, numVertices);
  2123. }
  2124. if (bgfx::checkAvailTransientVertexBuffer(numVertices, PosColorUvVertex::ms_decl) )
  2125. {
  2126. bgfx::TransientVertexBuffer tvb;
  2127. bgfx::allocTransientVertexBuffer(&tvb, numVertices, PosColorUvVertex::ms_decl);
  2128. PosColorUvVertex* vertex = (PosColorUvVertex*)tvb.data;
  2129. const float ox = _x;
  2130. while (*_text)
  2131. {
  2132. int32_t ch = (uint8_t)*_text;
  2133. if (ch == '\t')
  2134. {
  2135. for (int32_t i = 0; i < 4; ++i)
  2136. {
  2137. if (_x < s_tabStops[i] + ox)
  2138. {
  2139. _x = s_tabStops[i] + ox;
  2140. break;
  2141. }
  2142. }
  2143. }
  2144. else if (ch >= ' '
  2145. && ch < 128)
  2146. {
  2147. stbtt_aligned_quad quad;
  2148. getBakedQuad(m_fonts[m_currentFontIdx].m_cdata, ch - 32, &_x, &_y, &quad);
  2149. vertex->m_x = quad.x0;
  2150. vertex->m_y = quad.y0;
  2151. vertex->m_u = quad.s0;
  2152. vertex->m_v = quad.t0;
  2153. vertex->m_abgr = _abgr;
  2154. ++vertex;
  2155. vertex->m_x = quad.x1;
  2156. vertex->m_y = quad.y1;
  2157. vertex->m_u = quad.s1;
  2158. vertex->m_v = quad.t1;
  2159. vertex->m_abgr = _abgr;
  2160. ++vertex;
  2161. vertex->m_x = quad.x1;
  2162. vertex->m_y = quad.y0;
  2163. vertex->m_u = quad.s1;
  2164. vertex->m_v = quad.t0;
  2165. vertex->m_abgr = _abgr;
  2166. ++vertex;
  2167. vertex->m_x = quad.x0;
  2168. vertex->m_y = quad.y0;
  2169. vertex->m_u = quad.s0;
  2170. vertex->m_v = quad.t0;
  2171. vertex->m_abgr = _abgr;
  2172. ++vertex;
  2173. vertex->m_x = quad.x0;
  2174. vertex->m_y = quad.y1;
  2175. vertex->m_u = quad.s0;
  2176. vertex->m_v = quad.t1;
  2177. vertex->m_abgr = _abgr;
  2178. ++vertex;
  2179. vertex->m_x = quad.x1;
  2180. vertex->m_y = quad.y1;
  2181. vertex->m_u = quad.s1;
  2182. vertex->m_v = quad.t1;
  2183. vertex->m_abgr = _abgr;
  2184. ++vertex;
  2185. }
  2186. ++_text;
  2187. }
  2188. bgfx::setTexture(0, s_texColor, m_fonts[m_currentFontIdx].m_texture);
  2189. bgfx::setVertexBuffer(&tvb);
  2190. bgfx::setState(0
  2191. | BGFX_STATE_RGB_WRITE
  2192. | BGFX_STATE_ALPHA_WRITE
  2193. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  2194. );
  2195. bgfx::setProgram(m_textureProgram);
  2196. setCurrentScissor();
  2197. bgfx::submit(m_view);
  2198. }
  2199. #endif // USE_NANOVG_FONT
  2200. }
  2201. void screenQuad(int32_t _x, int32_t _y, int32_t _width, uint32_t _height, bool _originBottomLeft = false)
  2202. {
  2203. if (bgfx::checkAvailTransientVertexBuffer(6, PosUvVertex::ms_decl) )
  2204. {
  2205. bgfx::TransientVertexBuffer vb;
  2206. bgfx::allocTransientVertexBuffer(&vb, 6, PosUvVertex::ms_decl);
  2207. PosUvVertex* vertex = (PosUvVertex*)vb.data;
  2208. const float widthf = float(_width);
  2209. const float heightf = float(_height);
  2210. const float minx = float(_x);
  2211. const float miny = float(_y);
  2212. const float maxx = minx+widthf;
  2213. const float maxy = miny+heightf;
  2214. const float texelHalfW = m_halfTexel/widthf;
  2215. const float texelHalfH = m_halfTexel/heightf;
  2216. const float minu = texelHalfW;
  2217. const float maxu = 1.0f - texelHalfW;
  2218. const float minv = _originBottomLeft ? texelHalfH+1.0f : texelHalfH ;
  2219. const float maxv = _originBottomLeft ? texelHalfH : texelHalfH+1.0f;
  2220. vertex[0].m_x = minx;
  2221. vertex[0].m_y = miny;
  2222. vertex[0].m_u = minu;
  2223. vertex[0].m_v = minv;
  2224. vertex[1].m_x = maxx;
  2225. vertex[1].m_y = miny;
  2226. vertex[1].m_u = maxu;
  2227. vertex[1].m_v = minv;
  2228. vertex[2].m_x = maxx;
  2229. vertex[2].m_y = maxy;
  2230. vertex[2].m_u = maxu;
  2231. vertex[2].m_v = maxv;
  2232. vertex[3].m_x = maxx;
  2233. vertex[3].m_y = maxy;
  2234. vertex[3].m_u = maxu;
  2235. vertex[3].m_v = maxv;
  2236. vertex[4].m_x = minx;
  2237. vertex[4].m_y = maxy;
  2238. vertex[4].m_u = minu;
  2239. vertex[4].m_v = maxv;
  2240. vertex[5].m_x = minx;
  2241. vertex[5].m_y = miny;
  2242. vertex[5].m_u = minu;
  2243. vertex[5].m_v = minv;
  2244. bgfx::setVertexBuffer(&vb);
  2245. }
  2246. }
  2247. void colorWheelWidget(float _rgb[3], bool _respectIndentation, float _size, bool _enabled)
  2248. {
  2249. const uint32_t wheelId = getId();
  2250. const uint32_t triangleId = getId();
  2251. Area& area = getCurrentArea();
  2252. const float areaX = float(_respectIndentation ? area.m_widgetX : area.m_contentX);
  2253. const float areaW = float(_respectIndentation ? area.m_widgetW : area.m_contentWidth);
  2254. const float width = areaW*_size;
  2255. const float xx = areaX + areaW*0.5f;
  2256. const float yy = float(area.m_widgetY) + width*0.5f;
  2257. const float center[2] = { xx, yy };
  2258. area.m_widgetY += int32_t(width) + DEFAULT_SPACING;
  2259. const float ro = width*0.5f - 5.0f; // radiusOuter.
  2260. const float rd = _size*25.0f; // radiusDelta.
  2261. const float ri = ro - rd; // radiusInner.
  2262. const float aeps = 0.5f / ro; // Half a pixel arc length in radians (2pi cancels out).
  2263. const float cmx = float(m_mx) - center[0];
  2264. const float cmy = float(m_my) - center[1];
  2265. const float aa[2] = { ri - 6.0f, 0.0f }; // Hue point.
  2266. const float bb[2] = { cosf(-120.0f/180.0f*NVG_PI) * aa[0], sinf(-120.0f/180.0f*NVG_PI) * aa[0] }; // Black point.
  2267. const float cc[2] = { cosf( 120.0f/180.0f*NVG_PI) * aa[0], sinf( 120.0f/180.0f*NVG_PI) * aa[0] }; // White point.
  2268. const float ca[2] = { aa[0] - cc[0], aa[1] - cc[1] };
  2269. const float lenCa = sqrtf(ca[0]*ca[0]+ca[1]*ca[1]);
  2270. const float invLenCa = 1.0f/lenCa;
  2271. const float dirCa[2] = { ca[0]*invLenCa, ca[1]*invLenCa };
  2272. float sel[2];
  2273. float hsv[3];
  2274. bx::rgbToHsv(hsv, _rgb);
  2275. const bool enabled = _enabled && isEnabled(m_areaId);
  2276. if (enabled)
  2277. {
  2278. if (m_leftPressed)
  2279. {
  2280. const float len = sqrtf(cmx*cmx + cmy*cmy);
  2281. if (len > ri)
  2282. {
  2283. if (len < ro)
  2284. {
  2285. setActive(wheelId);
  2286. }
  2287. }
  2288. else
  2289. {
  2290. setActive(triangleId);
  2291. }
  2292. }
  2293. if (m_leftReleased
  2294. && (isActive(wheelId) || isActive(triangleId) ) )
  2295. {
  2296. clearActive();
  2297. }
  2298. // Set hue.
  2299. if (m_left
  2300. && isActive(wheelId) )
  2301. {
  2302. hsv[0] = atan2f(cmy, cmx)/NVG_PI*0.5f;
  2303. if (hsv[0] < 0.0f)
  2304. {
  2305. hsv[0]+=1.0f;
  2306. }
  2307. }
  2308. }
  2309. if (enabled
  2310. && m_left
  2311. && isActive(triangleId) )
  2312. {
  2313. float an = -hsv[0]*NVG_PI*2.0f;
  2314. float tmx = (cmx*cosf(an)-cmy*sinf(an) );
  2315. float tmy = (cmx*sinf(an)+cmy*cosf(an) );
  2316. if (pointInTriangle(tmx, tmy, aa[0], aa[1], bb[0], bb[1], cc[0], cc[1]) )
  2317. {
  2318. sel[0] = tmx;
  2319. sel[1] = tmy;
  2320. }
  2321. else
  2322. {
  2323. closestPointOnTriangle(sel[0], sel[1], tmx, tmy, aa[0], aa[1], bb[0], bb[1], cc[0], cc[1]);
  2324. }
  2325. }
  2326. else
  2327. {
  2328. /*
  2329. * bb (black)
  2330. * /\
  2331. * / \
  2332. * / \
  2333. * / \
  2334. * / \
  2335. * / .sel \
  2336. * / \
  2337. * cc(white)/____.ss_______\aa (hue)
  2338. */
  2339. const float ss[2] =
  2340. {
  2341. cc[0] + dirCa[0]*lenCa*hsv[1],
  2342. cc[1] + dirCa[1]*lenCa*hsv[1],
  2343. };
  2344. const float sb[2] = { bb[0]-ss[0], bb[1]-ss[1] };
  2345. const float lenSb = sqrtf(sb[0]*sb[0]+sb[1]*sb[1]);
  2346. const float invLenSb = 1.0f/lenSb;
  2347. const float dirSb[2] = { sb[0]*invLenSb, sb[1]*invLenSb };
  2348. sel[0] = cc[0] + dirCa[0]*lenCa*hsv[1] + dirSb[0]*lenSb*(1.0f - hsv[2]);
  2349. sel[1] = cc[1] + dirCa[1]*lenCa*hsv[1] + dirSb[1]*lenSb*(1.0f - hsv[2]);
  2350. }
  2351. float uu, vv, ww;
  2352. barycentric(uu, vv, ww
  2353. , aa[0], aa[1]
  2354. , bb[0], bb[1]
  2355. , cc[0], cc[1]
  2356. , sel[0], sel[1]
  2357. );
  2358. const float val = bx::fclamp(1.0f-vv, 0.0001f, 1.0f);
  2359. const float sat = bx::fclamp(uu/val, 0.0001f, 1.0f);
  2360. const float out[3] = { hsv[0], sat, val };
  2361. bx::hsvToRgb(_rgb, out);
  2362. // Draw widget.
  2363. nvgSave(m_nvg);
  2364. {
  2365. float saturation;
  2366. uint8_t alpha0;
  2367. uint8_t alpha1;
  2368. if (enabled)
  2369. {
  2370. saturation = 1.0f;
  2371. alpha0 = 255;
  2372. alpha1 = 192;
  2373. }
  2374. else
  2375. {
  2376. saturation = 0.0f;
  2377. alpha0 = 10;
  2378. alpha1 = 10;
  2379. }
  2380. // Circle.
  2381. for (uint8_t ii = 0; ii < 6; ii++)
  2382. {
  2383. const float a0 = float(ii)/6.0f * 2.0f*NVG_PI - aeps;
  2384. const float a1 = float(ii+1.0f)/6.0f * 2.0f*NVG_PI + aeps;
  2385. nvgBeginPath(m_nvg);
  2386. nvgArc(m_nvg, center[0], center[1], ri, a0, a1, NVG_CW);
  2387. nvgArc(m_nvg, center[0], center[1], ro, a1, a0, NVG_CCW);
  2388. nvgClosePath(m_nvg);
  2389. const float ax = center[0] + cosf(a0) * (ri+ro)*0.5f;
  2390. const float ay = center[1] + sinf(a0) * (ri+ro)*0.5f;
  2391. const float bx = center[0] + cosf(a1) * (ri+ro)*0.5f;
  2392. const float by = center[1] + sinf(a1) * (ri+ro)*0.5f;
  2393. NVGpaint paint = nvgLinearGradient(m_nvg
  2394. , ax, ay
  2395. , bx, by
  2396. , nvgHSLA(a0/NVG_PI*0.5f,saturation,0.55f,alpha0)
  2397. , nvgHSLA(a1/NVG_PI*0.5f,saturation,0.55f,alpha0)
  2398. );
  2399. nvgFillPaint(m_nvg, paint);
  2400. nvgFill(m_nvg);
  2401. }
  2402. // Circle stroke.
  2403. nvgBeginPath(m_nvg);
  2404. nvgCircle(m_nvg, center[0], center[1], ri-0.5f);
  2405. nvgCircle(m_nvg, center[0], center[1], ro+0.5f);
  2406. nvgStrokeColor(m_nvg, nvgRGBA(0,0,0,64) );
  2407. nvgStrokeWidth(m_nvg, 1.0f);
  2408. nvgStroke(m_nvg);
  2409. nvgSave(m_nvg);
  2410. {
  2411. // Hue selector.
  2412. nvgTranslate(m_nvg, center[0], center[1]);
  2413. nvgRotate(m_nvg, hsv[0]*NVG_PI*2.0f);
  2414. nvgStrokeWidth(m_nvg, 2.0f);
  2415. nvgBeginPath(m_nvg);
  2416. nvgRect(m_nvg, ri-1.0f,-3.0f,rd+2.0f,6.0f);
  2417. nvgStrokeColor(m_nvg, nvgRGBA(255,255,255,alpha1) );
  2418. nvgStroke(m_nvg);
  2419. // Hue selector drop shadow.
  2420. 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) );
  2421. nvgBeginPath(m_nvg);
  2422. nvgRect(m_nvg, ri-2.0f-10.0f,-4.0f-10.0f,ro-ri+4.0f+20.0f,8.0f+20.0f);
  2423. nvgRect(m_nvg, ri-2.0f,-4.0f,ro-ri+4.0f,8.0f);
  2424. nvgPathWinding(m_nvg, NVG_HOLE);
  2425. nvgFillPaint(m_nvg, paint);
  2426. nvgFill(m_nvg);
  2427. // Center triangle stroke.
  2428. nvgBeginPath(m_nvg);
  2429. nvgMoveTo(m_nvg, aa[0], aa[1]);
  2430. nvgLineTo(m_nvg, bb[0], bb[1]);
  2431. nvgLineTo(m_nvg, cc[0], cc[1]);
  2432. nvgClosePath(m_nvg);
  2433. nvgStrokeColor(m_nvg, nvgRGBA(0,0,0,64) );
  2434. nvgStroke(m_nvg);
  2435. // Center triangle fill.
  2436. paint = nvgLinearGradient(m_nvg, aa[0], aa[1], bb[0], bb[1], nvgHSL(hsv[0],saturation,0.5f), nvgRGBA(0,0,0,alpha0) );
  2437. nvgFillPaint(m_nvg, paint);
  2438. nvgFill(m_nvg);
  2439. 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) );
  2440. nvgFillPaint(m_nvg, paint);
  2441. nvgFill(m_nvg);
  2442. // Color selector.
  2443. nvgStrokeWidth(m_nvg, 2.0f);
  2444. nvgBeginPath(m_nvg);
  2445. nvgCircle(m_nvg, sel[0], sel[1], 5);
  2446. nvgStrokeColor(m_nvg, nvgRGBA(255,255,255,alpha1) );
  2447. nvgStroke(m_nvg);
  2448. // Color selector stroke.
  2449. paint = nvgRadialGradient(m_nvg, sel[0], sel[1], 7.0f, 9.0f, nvgRGBA(0,0,0,64), nvgRGBA(0,0,0,0) );
  2450. nvgBeginPath(m_nvg);
  2451. nvgRect(m_nvg, sel[0]-20.0f, sel[1]-20.0f, 40.0f, 40.0f);
  2452. nvgCircle(m_nvg, sel[0], sel[1], 7.0f);
  2453. nvgPathWinding(m_nvg, NVG_HOLE);
  2454. nvgFillPaint(m_nvg, paint);
  2455. nvgFill(m_nvg);
  2456. }
  2457. nvgRestore(m_nvg);
  2458. }
  2459. nvgRestore(m_nvg);
  2460. }
  2461. struct Area
  2462. {
  2463. int32_t m_x;
  2464. int32_t m_y;
  2465. int32_t m_width;
  2466. int32_t m_height;
  2467. int16_t m_contentX;
  2468. int16_t m_contentY;
  2469. int16_t m_contentWidth;
  2470. int16_t m_contentHeight;
  2471. int16_t m_scissorX;
  2472. int16_t m_scissorY;
  2473. int16_t m_scissorHeight;
  2474. int16_t m_scissorWidth;
  2475. int32_t m_widgetX;
  2476. int32_t m_widgetY;
  2477. int32_t m_widgetW;
  2478. int32_t* m_scrollVal;
  2479. uint32_t m_scrollId;
  2480. bool m_inside;
  2481. bool m_didScroll;
  2482. bool m_scissorEnabled;
  2483. };
  2484. inline Area& getCurrentArea()
  2485. {
  2486. return m_areas[m_areaId];
  2487. }
  2488. inline void setCurrentScissor()
  2489. {
  2490. const Area& area = getCurrentArea();
  2491. if (area.m_scissorEnabled)
  2492. {
  2493. bgfx::setScissor(uint16_t(IMGUI_MAX(0, area.m_scissorX) )
  2494. , uint16_t(IMGUI_MAX(0, area.m_scissorY-1) )
  2495. , area.m_scissorWidth
  2496. , area.m_scissorHeight+1
  2497. );
  2498. }
  2499. else
  2500. {
  2501. bgfx::setScissor(UINT16_MAX);
  2502. }
  2503. }
  2504. template <typename Ty, uint16_t Max=64>
  2505. struct IdStack
  2506. {
  2507. IdStack()
  2508. {
  2509. reset();
  2510. }
  2511. void reset()
  2512. {
  2513. m_current = 0;
  2514. m_idGen = 0;
  2515. m_ids[0] = 0;
  2516. }
  2517. void next()
  2518. {
  2519. BX_CHECK(Max > (m_current+1), "Param out of bounds!");
  2520. m_ids[++m_current] = ++m_idGen;
  2521. }
  2522. void pop()
  2523. {
  2524. m_current = m_current > 0 ? m_current-1 : 0;
  2525. }
  2526. Ty current() const
  2527. {
  2528. BX_CHECK(Max > (m_current), "Param out of bounds!");
  2529. return m_ids[m_current];
  2530. }
  2531. operator Ty() const
  2532. {
  2533. BX_CHECK(Max > (m_current), "Param out of bounds!");
  2534. return m_ids[m_current];
  2535. }
  2536. private:
  2537. uint16_t m_current;
  2538. Ty m_idGen;
  2539. Ty m_ids[Max];
  2540. };
  2541. int32_t m_mx;
  2542. int32_t m_my;
  2543. int32_t m_scroll;
  2544. uint32_t m_active;
  2545. uint32_t m_hot;
  2546. uint32_t m_hotToBe;
  2547. char m_char;
  2548. char m_lastChar;
  2549. uint32_t m_inputField;
  2550. int32_t m_dragX;
  2551. int32_t m_dragY;
  2552. float m_dragOrig;
  2553. bool m_left;
  2554. bool m_leftPressed;
  2555. bool m_leftReleased;
  2556. bool m_isHot;
  2557. bool m_wentActive;
  2558. bool m_insideArea;
  2559. bool m_isActivePresent;
  2560. bool m_checkActivePresence;
  2561. IdStack<uint16_t> m_areaId;
  2562. uint16_t m_widgetId;
  2563. uint64_t m_enabledAreaIds;
  2564. Area m_areas[64];
  2565. float m_tempCoords[MAX_TEMP_COORDS * 2];
  2566. float m_tempNormals[MAX_TEMP_COORDS * 2];
  2567. float m_circleVerts[NUM_CIRCLE_VERTS * 2];
  2568. uint16_t m_textureWidth;
  2569. uint16_t m_textureHeight;
  2570. float m_invTextureWidth;
  2571. float m_invTextureHeight;
  2572. float m_halfTexel;
  2573. NVGcontext* m_nvg;
  2574. uint8_t m_view;
  2575. uint16_t m_viewWidth;
  2576. uint16_t m_viewHeight;
  2577. #if !USE_NANOVG_FONT
  2578. struct Font
  2579. {
  2580. stbtt_bakedchar m_cdata[96]; // ASCII 32..126 is 95 glyphs
  2581. bgfx::TextureHandle m_texture;
  2582. float m_size;
  2583. };
  2584. uint16_t m_currentFontIdx;
  2585. bx::HandleAllocT<IMGUI_CONFIG_MAX_FONTS> m_fontHandle;
  2586. Font m_fonts[IMGUI_CONFIG_MAX_FONTS];
  2587. #endif // !USE_NANOVG_FONT
  2588. bgfx::UniformHandle u_imageLodEnabled;
  2589. bgfx::UniformHandle u_imageSwizzle;
  2590. bgfx::UniformHandle s_texColor;
  2591. bgfx::ProgramHandle m_colorProgram;
  2592. bgfx::ProgramHandle m_textureProgram;
  2593. bgfx::ProgramHandle m_cubeMapProgram;
  2594. bgfx::ProgramHandle m_imageProgram;
  2595. bgfx::ProgramHandle m_imageSwizzProgram;
  2596. bgfx::TextureHandle m_missingTexture;
  2597. };
  2598. static Imgui s_imgui;
  2599. ImguiFontHandle imguiCreate(const void* _data, float _fontSize)
  2600. {
  2601. return s_imgui.create(_data, _fontSize);
  2602. }
  2603. void imguiDestroy()
  2604. {
  2605. s_imgui.destroy();
  2606. }
  2607. ImguiFontHandle imguiCreateFont(const void* _data, float _fontSize)
  2608. {
  2609. return s_imgui.createFont(_data, _fontSize);
  2610. }
  2611. void imguiSetFont(ImguiFontHandle _handle)
  2612. {
  2613. s_imgui.setFont(_handle);
  2614. }
  2615. ImguiFontHandle imguiGetCurrentFont()
  2616. {
  2617. const ImguiFontHandle handle = { s_imgui.m_currentFontIdx };
  2618. return handle;
  2619. }
  2620. 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)
  2621. {
  2622. s_imgui.beginFrame(_mx, _my, _button, _scroll, _width, _height, _inputChar, _view);
  2623. }
  2624. void imguiEndFrame()
  2625. {
  2626. s_imgui.endFrame();
  2627. }
  2628. void imguiDrawText(int32_t _x, int32_t _y, ImguiTextAlign::Enum _align, const char* _text, uint32_t _argb)
  2629. {
  2630. s_imgui.drawText(_x, _y, _align, _text, _argb);
  2631. }
  2632. void imguiDrawLine(float _x0, float _y0, float _x1, float _y1, float _r, uint32_t _argb)
  2633. {
  2634. s_imgui.drawLine(_x0, _y0, _x1, _y1, _r, _argb);
  2635. }
  2636. void imguiDrawRoundedRect(float _x, float _y, float _width, float _height, float _r, uint32_t _argb)
  2637. {
  2638. s_imgui.drawRoundedRect(_x, _y, _width, _height, _r, _argb);
  2639. }
  2640. void imguiDrawRect(float _x, float _y, float _width, float _height, uint32_t _argb)
  2641. {
  2642. s_imgui.drawRect(_x, _y, _width, _height, _argb);
  2643. }
  2644. bool imguiBorderButton(ImguiBorder::Enum _border, bool _checked, bool _enabled)
  2645. {
  2646. return s_imgui.borderButton(_border, _checked, _enabled);
  2647. }
  2648. bool imguiBeginArea(const char* _name, int _x, int _y, int _width, int _height, bool _enabled, int32_t _r)
  2649. {
  2650. return s_imgui.beginArea(_name, _x, _y, _width, _height, _enabled, _r);
  2651. }
  2652. void imguiEndArea()
  2653. {
  2654. return s_imgui.endArea();
  2655. }
  2656. bool imguiBeginScroll(int32_t _height, int32_t* _scroll, bool _enabled)
  2657. {
  2658. return s_imgui.beginScroll(_height, _scroll, _enabled);
  2659. }
  2660. void imguiEndScroll(int32_t _r)
  2661. {
  2662. s_imgui.endScroll(_r);
  2663. }
  2664. 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)
  2665. {
  2666. const bool result = s_imgui.beginArea(_name, _x, _y, _width, _height, _enabled, _r);
  2667. const bool hasTitle = (NULL != _name && '\0' != _name[0]);
  2668. const int32_t margins = int32_t(hasTitle)*(AREA_HEADER+2*SCROLL_AREA_PADDING-1);
  2669. s_imgui.beginScroll(_height - margins, _scroll, _enabled);
  2670. return result;
  2671. }
  2672. void imguiEndScrollArea(int32_t _r)
  2673. {
  2674. s_imgui.endScroll(_r);
  2675. s_imgui.endArea();
  2676. }
  2677. void imguiIndent(uint16_t _width)
  2678. {
  2679. s_imgui.indent(_width);
  2680. }
  2681. void imguiUnindent(uint16_t _width)
  2682. {
  2683. s_imgui.unindent(_width);
  2684. }
  2685. void imguiSeparator(uint16_t _height)
  2686. {
  2687. s_imgui.separator(_height);
  2688. }
  2689. void imguiSeparatorLine(uint16_t _height)
  2690. {
  2691. s_imgui.separatorLine(_height);
  2692. }
  2693. int32_t imguiGetWidgetX()
  2694. {
  2695. return s_imgui.getCurrentArea().m_widgetX;
  2696. }
  2697. int32_t imguiGetWidgetY()
  2698. {
  2699. return s_imgui.getCurrentArea().m_widgetY;
  2700. }
  2701. void imguiSetCurrentScissor()
  2702. {
  2703. return s_imgui.setCurrentScissor();
  2704. }
  2705. bool imguiButton(const char* _text, bool _enabled, ImguiAlign::Enum _align, uint32_t _rgb0, int32_t _r)
  2706. {
  2707. return s_imgui.button(_text, _enabled, _align, _rgb0, _r);
  2708. }
  2709. bool imguiItem(const char* _text, bool _enabled)
  2710. {
  2711. return s_imgui.item(_text, _enabled);
  2712. }
  2713. bool imguiCheck(const char* _text, bool _checked, bool _enabled)
  2714. {
  2715. return s_imgui.check(_text, _checked, _enabled);
  2716. }
  2717. void imguiBool(const char* _text, bool& _flag, bool _enabled)
  2718. {
  2719. if (imguiCheck(_text, _flag, _enabled) )
  2720. {
  2721. _flag = !_flag;
  2722. }
  2723. }
  2724. bool imguiCollapse(const char* _text, const char* _subtext, bool _checked, bool _enabled)
  2725. {
  2726. return s_imgui.collapse(_text, _subtext, _checked, _enabled);
  2727. }
  2728. void imguiLabel(const char* _format, ...)
  2729. {
  2730. va_list argList;
  2731. va_start(argList, _format);
  2732. s_imgui.labelVargs(_format, argList, imguiRGBA(255, 255, 255, 255) );
  2733. va_end(argList);
  2734. }
  2735. void imguiLabel(uint32_t _rgba, const char* _format, ...)
  2736. {
  2737. va_list argList;
  2738. va_start(argList, _format);
  2739. s_imgui.labelVargs(_format, argList, _rgba);
  2740. va_end(argList);
  2741. }
  2742. void imguiValue(const char* _text)
  2743. {
  2744. s_imgui.value(_text);
  2745. }
  2746. bool imguiSlider(const char* _text, float& _val, float _vmin, float _vmax, float _vinc, bool _enabled, ImguiAlign::Enum _align)
  2747. {
  2748. return s_imgui.slider(_text, _val, _vmin, _vmax, _vinc, _enabled, _align);
  2749. }
  2750. bool imguiSlider(const char* _text, int32_t& _val, int32_t _vmin, int32_t _vmax, bool _enabled, ImguiAlign::Enum _align)
  2751. {
  2752. float val = (float)_val;
  2753. bool result = s_imgui.slider(_text, val, (float)_vmin, (float)_vmax, 1.0f, _enabled, _align);
  2754. _val = (int32_t)val;
  2755. return result;
  2756. }
  2757. void imguiInput(const char* _label, char* _str, uint32_t _len, bool _enabled, ImguiAlign::Enum _align, int32_t _r)
  2758. {
  2759. s_imgui.input(_label, _str, _len, _enabled, _align, _r);
  2760. }
  2761. uint8_t imguiTabs(uint8_t _selected, bool _enabled, ImguiAlign::Enum _align, int32_t _height, int32_t _r, uint8_t _nTabs, uint8_t _nEnabled, ...)
  2762. {
  2763. va_list argList;
  2764. va_start(argList, _nEnabled);
  2765. const uint8_t result = s_imgui.tabs(_selected, _enabled, _align, _height, _r, _nTabs, _nEnabled, argList);
  2766. va_end(argList);
  2767. return result;
  2768. }
  2769. uint8_t imguiTabs(uint8_t _selected, bool _enabled, ImguiAlign::Enum _align, int32_t _height, int32_t _r, uint8_t _nTabs, ...)
  2770. {
  2771. va_list argList;
  2772. va_start(argList, _nTabs);
  2773. const uint8_t result = s_imgui.tabs(_selected, _enabled, _align, _height, _r, _nTabs, 0, argList);
  2774. va_end(argList);
  2775. return result;
  2776. }
  2777. uint32_t imguiChooseUseMacroInstead(uint32_t _selected, ...)
  2778. {
  2779. va_list argList;
  2780. va_start(argList, _selected);
  2781. const char* str = va_arg(argList, const char*);
  2782. for (uint32_t ii = 0; str != NULL; ++ii, str = va_arg(argList, const char*) )
  2783. {
  2784. if (imguiCheck(str, ii == _selected) )
  2785. {
  2786. _selected = ii;
  2787. }
  2788. }
  2789. va_end(argList);
  2790. return _selected;
  2791. }
  2792. void imguiColorWheel(float _rgb[3], bool _respectIndentation, float _size, bool _enabled)
  2793. {
  2794. s_imgui.colorWheelWidget(_rgb, _respectIndentation, _size, _enabled);
  2795. }
  2796. void imguiColorWheel(const char* _text, float _rgb[3], bool& _activated, float _size, bool _enabled)
  2797. {
  2798. char buf[128];
  2799. bx::snprintf(buf, sizeof(buf), "[RGB %-2.2f %-2.2f %-2.2f]"
  2800. , _rgb[0]
  2801. , _rgb[1]
  2802. , _rgb[2]
  2803. );
  2804. if (imguiCollapse(_text, buf, _activated) )
  2805. {
  2806. _activated = !_activated;
  2807. }
  2808. if (_activated)
  2809. {
  2810. imguiColorWheel(_rgb, false, _size, _enabled);
  2811. }
  2812. }
  2813. bool imguiImage(bgfx::TextureHandle _image, float _lod, int32_t _width, int32_t _height, ImguiAlign::Enum _align, bool _enabled, bool _originBottomLeft)
  2814. {
  2815. return s_imgui.image(_image, _lod, _width, _height, _align, _enabled, _originBottomLeft);
  2816. }
  2817. bool imguiImage(bgfx::TextureHandle _image, float _lod, float _width, float _aspect, ImguiAlign::Enum _align, bool _enabled, bool _originBottomLeft)
  2818. {
  2819. return s_imgui.image(_image, _lod, _width, _aspect, _align, _enabled, _originBottomLeft);
  2820. }
  2821. bool imguiImageChannel(bgfx::TextureHandle _image, uint8_t _channel, float _lod, int32_t _width, int32_t _height, ImguiAlign::Enum _align, bool _enabled)
  2822. {
  2823. return s_imgui.imageChannel(_image, _channel, _lod, _width, _height, _align, _enabled);
  2824. }
  2825. bool imguiImageChannel(bgfx::TextureHandle _image, uint8_t _channel, float _lod, float _width, float _aspect, ImguiAlign::Enum _align, bool _enabled)
  2826. {
  2827. return s_imgui.imageChannel(_image, _channel, _lod, _width, _aspect, _align, _enabled);
  2828. }
  2829. bool imguiCube(bgfx::TextureHandle _cubemap, float _lod, bool _cross, ImguiAlign::Enum _align, bool _enabled)
  2830. {
  2831. return s_imgui.cubeMap(_cubemap, _lod, _cross, _align, _enabled);
  2832. }
  2833. float imguiGetTextLength(const char* _text, ImguiFontHandle _handle)
  2834. {
  2835. #if !USE_NANOVG_FONT
  2836. uint32_t numVertices = 0; //unused
  2837. return getTextLength(s_imgui.m_fonts[_handle.idx].m_cdata, _text, numVertices);
  2838. #else
  2839. return 0.0f;
  2840. #endif
  2841. }
  2842. bool imguiMouseOverArea()
  2843. {
  2844. return s_imgui.m_insideArea;
  2845. }