imgui.cpp 83 KB

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