imgui.cpp 82 KB

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