imgui.cpp 91 KB

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