imgui.cpp 84 KB

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