imgui.cpp 89 KB

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