imgui.cpp 83 KB

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