imgui.cpp 83 KB

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