imgui.cpp 89 KB

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