imgui.cpp 84 KB

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