imgui.cpp 91 KB

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