imgui.cpp 92 KB

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