imgui.cpp 92 KB

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