imgui.cpp 89 KB

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