imgui.cpp 90 KB

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