imgui.cpp 88 KB

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