imgui.cpp 84 KB

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