imgui.cpp 85 KB

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