imgui.cpp 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798
  1. /*
  2. * Copyright 2011-2014 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. // This code is based on:
  6. //
  7. // Copyright (c) 2009-2010 Mikko Mononen [email protected]
  8. //
  9. // This software is provided 'as-is', without any express or implied
  10. // warranty. In no event will the authors be held liable for any damages
  11. // arising from the use of this software.
  12. // Permission is granted to anyone to use this software for any purpose,
  13. // including commercial applications, and to alter it and redistribute it
  14. // freely, subject to the following restrictions:
  15. // 1. The origin of this software must not be misrepresented; you must not
  16. // claim that you wrote the original software. If you use this software
  17. // in a product, an acknowledgment in the product documentation would be
  18. // appreciated but is not required.
  19. // 2. Altered source versions must be plainly marked as such, and must not be
  20. // misrepresented as being the original software.
  21. // 3. This notice may not be removed or altered from any source distribution.
  22. //
  23. // Source altered and distributed from https://github.com/AdrienHerubel/imgui
  24. #include <stdio.h>
  25. #include <bx/string.h>
  26. #include <bx/uint32_t.h>
  27. #include <bx/fpumath.h>
  28. #include <bx/handlealloc.h>
  29. #include "../entry/dbg.h"
  30. #include "imgui.h"
  31. #include "../nanovg/nanovg.h"
  32. #include "vs_imgui_color.bin.h"
  33. #include "fs_imgui_color.bin.h"
  34. #include "vs_imgui_texture.bin.h"
  35. #include "fs_imgui_texture.bin.h"
  36. #include "vs_imgui_image.bin.h"
  37. #include "fs_imgui_image.bin.h"
  38. #include "fs_imgui_image_swizz.bin.h"
  39. #define USE_NANOVG_FONT 0
  40. #define IMGUI_CONFIG_MAX_FONTS 20
  41. #define MAX_TEMP_COORDS 100
  42. #define NUM_CIRCLE_VERTS (8 * 4)
  43. static const int32_t BUTTON_HEIGHT = 20;
  44. static const int32_t SLIDER_HEIGHT = 20;
  45. static const int32_t SLIDER_MARKER_WIDTH = 10;
  46. static const int32_t CHECK_SIZE = 8;
  47. static const int32_t DEFAULT_SPACING = 4;
  48. static const int32_t TEXT_HEIGHT = 8;
  49. static const int32_t SCROLL_AREA_PADDING = 6;
  50. static const int32_t AREA_HEADER = 20;
  51. static const int32_t COLOR_WHEEL_PADDING = 60;
  52. static const float s_tabStops[4] = {150, 210, 270, 330};
  53. static void* imguiMalloc(size_t size, void* /*_userptr*/)
  54. {
  55. return malloc(size);
  56. }
  57. static void imguiFree(void* _ptr, void* /*_userptr*/)
  58. {
  59. free(_ptr);
  60. }
  61. #define STBTT_malloc(_x, _y) imguiMalloc(_x, _y)
  62. #define STBTT_free(_x, _y) imguiFree(_x, _y)
  63. #define STB_TRUETYPE_IMPLEMENTATION
  64. #include <stb_truetype/stb_truetype.h>
  65. namespace
  66. {
  67. float sign(float px, float py, float ax, float ay, float bx, float by)
  68. {
  69. return (px - bx) * (ay - by) - (ax - bx) * (py - by);
  70. }
  71. bool pointInTriangle(float px, float py, float ax, float ay, float bx, float by, float cx, float cy)
  72. {
  73. const bool b1 = sign(px, py, ax, ay, bx, by) < 0.0f;
  74. const bool b2 = sign(px, py, bx, by, cx, cy) < 0.0f;
  75. const bool b3 = sign(px, py, cx, cy, ax, ay) < 0.0f;
  76. return ( (b1 == b2) && (b2 == b3) );
  77. }
  78. void closestPointOnLine(float& ox, float &oy, float px, float py, float ax, float ay, float bx, float by)
  79. {
  80. float dx = px - ax;
  81. float dy = py - ay;
  82. float lx = bx - ax;
  83. float ly = by - ay;
  84. float len = sqrtf(lx*lx+ly*ly);
  85. // Normalize.
  86. float invLen = 1.0f/len;
  87. lx*=invLen;
  88. ly*=invLen;
  89. float dot = (dx*lx + dy*ly);
  90. if (dot < 0.0f)
  91. {
  92. ox = ax;
  93. oy = ay;
  94. }
  95. else if (dot > len)
  96. {
  97. ox = bx;
  98. oy = by;
  99. }
  100. else
  101. {
  102. ox = ax + lx*dot;
  103. oy = ay + ly*dot;
  104. }
  105. }
  106. void closestPointOnTriangle(float& ox, float &oy, float px, float py, float ax, float ay, float bx, float by, float cx, float cy)
  107. {
  108. float abx, aby;
  109. float bcx, bcy;
  110. float cax, cay;
  111. closestPointOnLine(abx, aby, px, py, ax, ay, bx, by);
  112. closestPointOnLine(bcx, bcy, px, py, bx, by, cx, cy);
  113. closestPointOnLine(cax, cay, px, py, cx, cy, ax, ay);
  114. const float pabx = px - abx;
  115. const float paby = py - aby;
  116. const float pbcx = px - bcx;
  117. const float pbcy = py - bcy;
  118. const float pcax = px - cax;
  119. const float pcay = py - cay;
  120. const float lab = sqrtf(pabx*pabx+paby*paby);
  121. const float lbc = sqrtf(pbcx*pbcx+pbcy*pbcy);
  122. const float lca = sqrtf(pcax*pcax+pcay*pcay);
  123. const float m = bx::fmin3(lab, lbc, lca);
  124. if (m == lab)
  125. {
  126. ox = abx;
  127. oy = aby;
  128. }
  129. else if (m == lbc)
  130. {
  131. ox = bcx;
  132. oy = bcy;
  133. }
  134. else// if (m == lca).
  135. {
  136. ox = cax;
  137. oy = cay;
  138. }
  139. }
  140. inline float vec2Dot(const float* __restrict _a, const float* __restrict _b)
  141. {
  142. return _a[0]*_b[0] + _a[1]*_b[1];
  143. }
  144. void barycentric(float& _u, float& _v, float& _w
  145. , float _ax, float _ay
  146. , float _bx, float _by
  147. , float _cx, float _cy
  148. , float _px, float _py
  149. )
  150. {
  151. const float v0[2] = { _bx - _ax, _by - _ay };
  152. const float v1[2] = { _cx - _ax, _cy - _ay };
  153. const float v2[2] = { _px - _ax, _py - _ay };
  154. const float d00 = vec2Dot(v0, v0);
  155. const float d01 = vec2Dot(v0, v1);
  156. const float d11 = vec2Dot(v1, v1);
  157. const float d20 = vec2Dot(v2, v0);
  158. const float d21 = vec2Dot(v2, v1);
  159. const float denom = d00 * d11 - d01 * d01;
  160. _v = (d11 * d20 - d01 * d21) / denom;
  161. _w = (d00 * d21 - d01 * d20) / denom;
  162. _u = 1.0f - _v - _w;
  163. }
  164. struct PosColorVertex
  165. {
  166. float m_x;
  167. float m_y;
  168. uint32_t m_abgr;
  169. static void init()
  170. {
  171. ms_decl
  172. .begin()
  173. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  174. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  175. .end();
  176. }
  177. static bgfx::VertexDecl ms_decl;
  178. };
  179. bgfx::VertexDecl PosColorVertex::ms_decl;
  180. struct PosColorUvVertex
  181. {
  182. float m_x;
  183. float m_y;
  184. float m_u;
  185. float m_v;
  186. uint32_t m_abgr;
  187. static void init()
  188. {
  189. ms_decl
  190. .begin()
  191. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  192. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  193. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  194. .end();
  195. }
  196. static bgfx::VertexDecl ms_decl;
  197. };
  198. bgfx::VertexDecl PosColorUvVertex::ms_decl;
  199. struct PosUvVertex
  200. {
  201. float m_x;
  202. float m_y;
  203. float m_u;
  204. float m_v;
  205. static void init()
  206. {
  207. ms_decl
  208. .begin()
  209. .add(bgfx::Attrib::Position, 2, bgfx::AttribType::Float)
  210. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  211. .end();
  212. }
  213. static bgfx::VertexDecl ms_decl;
  214. };
  215. bgfx::VertexDecl PosUvVertex::ms_decl;
  216. } // namespace
  217. struct Imgui
  218. {
  219. Imgui()
  220. : m_mx(-1)
  221. , m_my(-1)
  222. , m_scroll(0)
  223. , m_active(0)
  224. , m_hot(0)
  225. , m_hotToBe(0)
  226. , m_dragX(0)
  227. , m_dragY(0)
  228. , m_dragOrig(0)
  229. , m_widgetX(0)
  230. , m_widgetY(0)
  231. , m_widgetW(100)
  232. , m_left(false)
  233. , m_leftPressed(false)
  234. , m_leftReleased(false)
  235. , m_isHot(false)
  236. , m_isActive(false)
  237. , m_wentActive(false)
  238. , m_insideCurrentScroll(false)
  239. , m_areaId(0)
  240. , m_widgetId(0)
  241. , m_enabledAreaIds(0)
  242. , m_scissor(UINT16_MAX)
  243. , m_scrollTop(0)
  244. , m_scrollHeight(0)
  245. , m_scrollRight(0)
  246. , m_scrollAreaTop(0)
  247. , m_scrollAreaWidth(0)
  248. , m_scrollAreaInnerWidth(0)
  249. , m_scrollAreaX(0)
  250. , m_scrollVal(NULL)
  251. , m_scrollId(0)
  252. , m_insideScrollArea(false)
  253. , m_textureWidth(512)
  254. , m_textureHeight(512)
  255. , m_halfTexel(0.0f)
  256. , m_nvg(NULL)
  257. , m_view(31)
  258. , m_viewWidth(0)
  259. , m_viewHeight(0)
  260. , m_currentFontIdx(0)
  261. {
  262. m_invTextureWidth = 1.0f/m_textureWidth;
  263. m_invTextureHeight = 1.0f/m_textureHeight;
  264. u_imageLod.idx = bgfx::invalidHandle;
  265. u_imageSwizzle.idx = bgfx::invalidHandle;
  266. s_texColor.idx = bgfx::invalidHandle;
  267. m_missingTexture.idx = bgfx::invalidHandle;
  268. m_colorProgram.idx = bgfx::invalidHandle;
  269. m_textureProgram.idx = bgfx::invalidHandle;
  270. m_imageProgram.idx = bgfx::invalidHandle;
  271. m_imageSwizzProgram.idx = bgfx::invalidHandle;
  272. }
  273. ImguiFontHandle createFont(const void* _data, float _fontSize)
  274. {
  275. #if !USE_NANOVG_FONT
  276. const ImguiFontHandle handle = { m_fontHandle.alloc() };
  277. const bgfx::Memory* mem = bgfx::alloc(m_textureWidth * m_textureHeight);
  278. stbtt_BakeFontBitmap( (uint8_t*)_data, 0, _fontSize, mem->data, m_textureWidth, m_textureHeight, 32, 96, m_fonts[handle.idx].m_cdata);
  279. m_fonts[handle.idx].m_texture = bgfx::createTexture2D(m_textureWidth, m_textureHeight, 1, bgfx::TextureFormat::R8, BGFX_TEXTURE_NONE, mem);
  280. m_fonts[handle.idx].m_size = _fontSize;
  281. #else
  282. const ImguiFontHandle handle = { bgfx::invalidHandle };
  283. #endif // !USE_NANOVG_FONT
  284. return handle;
  285. }
  286. void setFont(ImguiFontHandle _handle)
  287. {
  288. if (isValid(_handle) )
  289. {
  290. m_currentFontIdx = _handle.idx;
  291. }
  292. }
  293. bgfx::TextureHandle genMissingTexture(uint32_t _width, uint32_t _height, float _lineWidth = 0.02f)
  294. {
  295. const bgfx::Memory* mem = bgfx::alloc(_width*_height*4);
  296. uint32_t* bgra8 = (uint32_t*)mem->data;
  297. const float sx = 0.70710677f;
  298. const float cx = 0.70710677f;
  299. for (uint32_t yy = 0; yy < _height; ++yy)
  300. {
  301. for (uint32_t xx = 0; xx < _width; ++xx)
  302. {
  303. float px = xx / float(_width) * 2.0f - 1.0f;
  304. float py = yy / float(_height) * 2.0f - 1.0f;
  305. float sum = bx::fpulse(px * cx - py * sx, _lineWidth, -_lineWidth)
  306. + bx::fpulse(px * sx + py * cx, _lineWidth, -_lineWidth)
  307. ;
  308. *bgra8++ = sum >= 1.0f ? 0xffff0000 : 0xffffffff;
  309. }
  310. }
  311. return bgfx::createTexture2D(_width, _height, 0, bgfx::TextureFormat::BGRA8, 0, mem);
  312. }
  313. ImguiFontHandle create(const void* _data, float _fontSize)
  314. {
  315. m_nvg = nvgCreate(512, 512, 1, m_view);
  316. nvgCreateFontMem(m_nvg, "default", (unsigned char*)_data, INT32_MAX, 0);
  317. nvgFontSize(m_nvg, _fontSize);
  318. nvgFontFace(m_nvg, "default");
  319. for (int32_t ii = 0; ii < NUM_CIRCLE_VERTS; ++ii)
  320. {
  321. float a = (float)ii / (float)NUM_CIRCLE_VERTS * (float)(M_PI * 2.0);
  322. m_circleVerts[ii * 2 + 0] = cosf(a);
  323. m_circleVerts[ii * 2 + 1] = sinf(a);
  324. }
  325. PosColorVertex::init();
  326. PosColorUvVertex::init();
  327. PosUvVertex::init();
  328. u_imageLod = bgfx::createUniform("u_imageLod", bgfx::UniformType::Uniform1f);
  329. u_imageSwizzle = bgfx::createUniform("u_swizzle", bgfx::UniformType::Uniform4fv);
  330. s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Uniform1i);
  331. const bgfx::Memory* vs_imgui_color;
  332. const bgfx::Memory* fs_imgui_color;
  333. const bgfx::Memory* vs_imgui_texture;
  334. const bgfx::Memory* fs_imgui_texture;
  335. const bgfx::Memory* vs_imgui_image;
  336. const bgfx::Memory* fs_imgui_image;
  337. const bgfx::Memory* fs_imgui_image_swizz;
  338. switch (bgfx::getRendererType() )
  339. {
  340. case bgfx::RendererType::Direct3D9:
  341. vs_imgui_color = bgfx::makeRef(vs_imgui_color_dx9, sizeof(vs_imgui_color_dx9) );
  342. fs_imgui_color = bgfx::makeRef(fs_imgui_color_dx9, sizeof(fs_imgui_color_dx9) );
  343. vs_imgui_texture = bgfx::makeRef(vs_imgui_texture_dx9, sizeof(vs_imgui_texture_dx9) );
  344. fs_imgui_texture = bgfx::makeRef(fs_imgui_texture_dx9, sizeof(fs_imgui_texture_dx9) );
  345. vs_imgui_image = bgfx::makeRef(vs_imgui_image_dx9, sizeof(vs_imgui_image_dx9) );
  346. fs_imgui_image = bgfx::makeRef(fs_imgui_image_dx9, sizeof(fs_imgui_image_dx9) );
  347. fs_imgui_image_swizz = bgfx::makeRef(fs_imgui_image_swizz_dx9, sizeof(fs_imgui_image_swizz_dx9) );
  348. m_halfTexel = 0.5f;
  349. break;
  350. case bgfx::RendererType::Direct3D11:
  351. vs_imgui_color = bgfx::makeRef(vs_imgui_color_dx11, sizeof(vs_imgui_color_dx11) );
  352. fs_imgui_color = bgfx::makeRef(fs_imgui_color_dx11, sizeof(fs_imgui_color_dx11) );
  353. vs_imgui_texture = bgfx::makeRef(vs_imgui_texture_dx11, sizeof(vs_imgui_texture_dx11) );
  354. fs_imgui_texture = bgfx::makeRef(fs_imgui_texture_dx11, sizeof(fs_imgui_texture_dx11) );
  355. vs_imgui_image = bgfx::makeRef(vs_imgui_image_dx11, sizeof(vs_imgui_image_dx11) );
  356. fs_imgui_image = bgfx::makeRef(fs_imgui_image_dx11, sizeof(fs_imgui_image_dx11) );
  357. fs_imgui_image_swizz = bgfx::makeRef(fs_imgui_image_swizz_dx11, sizeof(fs_imgui_image_swizz_dx11) );
  358. break;
  359. default:
  360. vs_imgui_color = bgfx::makeRef(vs_imgui_color_glsl, sizeof(vs_imgui_color_glsl) );
  361. fs_imgui_color = bgfx::makeRef(fs_imgui_color_glsl, sizeof(fs_imgui_color_glsl) );
  362. vs_imgui_texture = bgfx::makeRef(vs_imgui_texture_glsl, sizeof(vs_imgui_texture_glsl) );
  363. fs_imgui_texture = bgfx::makeRef(fs_imgui_texture_glsl, sizeof(fs_imgui_texture_glsl) );
  364. vs_imgui_image = bgfx::makeRef(vs_imgui_image_glsl, sizeof(vs_imgui_image_glsl) );
  365. fs_imgui_image = bgfx::makeRef(fs_imgui_image_glsl, sizeof(fs_imgui_image_glsl) );
  366. fs_imgui_image_swizz = bgfx::makeRef(fs_imgui_image_swizz_glsl, sizeof(fs_imgui_image_swizz_glsl) );
  367. break;
  368. }
  369. bgfx::ShaderHandle vsh;
  370. bgfx::ShaderHandle fsh;
  371. vsh = bgfx::createShader(vs_imgui_color);
  372. fsh = bgfx::createShader(fs_imgui_color);
  373. m_colorProgram = bgfx::createProgram(vsh, fsh);
  374. bgfx::destroyShader(vsh);
  375. bgfx::destroyShader(fsh);
  376. vsh = bgfx::createShader(vs_imgui_texture);
  377. fsh = bgfx::createShader(fs_imgui_texture);
  378. m_textureProgram = bgfx::createProgram(vsh, fsh);
  379. bgfx::destroyShader(vsh);
  380. bgfx::destroyShader(fsh);
  381. vsh = bgfx::createShader(vs_imgui_image);
  382. fsh = bgfx::createShader(fs_imgui_image);
  383. m_imageProgram = bgfx::createProgram(vsh, fsh);
  384. bgfx::destroyShader(fsh);
  385. // Notice: using the same vsh.
  386. fsh = bgfx::createShader(fs_imgui_image_swizz);
  387. m_imageSwizzProgram = bgfx::createProgram(vsh, fsh);
  388. bgfx::destroyShader(fsh);
  389. bgfx::destroyShader(vsh);
  390. m_missingTexture = genMissingTexture(256, 256, 0.04f);
  391. #if !USE_NANOVG_FONT
  392. const ImguiFontHandle handle = createFont(_data, _fontSize);
  393. m_currentFontIdx = handle.idx;
  394. #else
  395. const ImguiFontHandle handle = { bgfx::invalidHandle };
  396. #endif // !USE_NANOVG_FONT
  397. return handle;
  398. }
  399. void destroy()
  400. {
  401. bgfx::destroyUniform(u_imageLod);
  402. bgfx::destroyUniform(u_imageSwizzle);
  403. bgfx::destroyUniform(s_texColor);
  404. #if !USE_NANOVG_FONT
  405. for (uint16_t ii = 0; ii < IMGUI_CONFIG_MAX_FONTS; ++ii)
  406. {
  407. if (bgfx::isValid(m_fonts[ii].m_texture) )
  408. {
  409. bgfx::destroyTexture(m_fonts[ii].m_texture);
  410. }
  411. }
  412. #endif // !USE_NANOVG_FONT
  413. bgfx::destroyTexture(m_missingTexture);
  414. bgfx::destroyProgram(m_colorProgram);
  415. bgfx::destroyProgram(m_textureProgram);
  416. bgfx::destroyProgram(m_imageProgram);
  417. bgfx::destroyProgram(m_imageSwizzProgram);
  418. nvgDelete(m_nvg);
  419. }
  420. bool anyActive() const
  421. {
  422. return m_active != 0;
  423. }
  424. bool isActive(uint32_t _id) const
  425. {
  426. return m_active == _id;
  427. }
  428. bool isActiveInputField(uint32_t _id) const
  429. {
  430. return m_inputField == _id;
  431. }
  432. bool isHot(uint32_t _id) const
  433. {
  434. return m_hot == _id;
  435. }
  436. bool inRect(int32_t _x, int32_t _y, int32_t _width, int32_t _height, bool _checkScroll = true) const
  437. {
  438. return (!_checkScroll || m_insideCurrentScroll)
  439. && m_mx >= _x
  440. && m_mx <= _x + _width
  441. && m_my >= _y
  442. && m_my <= _y + _height;
  443. }
  444. bool isEnabled(uint8_t _areaId)
  445. {
  446. return (m_enabledAreaIds>>_areaId)&0x1;
  447. }
  448. void setEnabled(uint8_t _areaId)
  449. {
  450. m_enabledAreaIds |= (UINT64_C(1)<<_areaId);
  451. }
  452. void clearInput()
  453. {
  454. m_leftPressed = false;
  455. m_leftReleased = false;
  456. m_scroll = 0;
  457. }
  458. void clearActive()
  459. {
  460. m_active = 0;
  461. // mark all UI for this frame as processed
  462. clearInput();
  463. }
  464. void clearActiveInputField()
  465. {
  466. m_inputField = 0;
  467. }
  468. void setActive(uint32_t _id)
  469. {
  470. m_active = _id;
  471. m_wentActive = true;
  472. m_inputField = 0;
  473. }
  474. void setActiveInputField(uint32_t _id)
  475. {
  476. m_inputField = _id;
  477. }
  478. void setHot(uint32_t _id)
  479. {
  480. m_hotToBe = _id;
  481. }
  482. bool buttonLogic(uint32_t _id, bool _over)
  483. {
  484. bool res = false;
  485. // process down
  486. if (!anyActive() )
  487. {
  488. if (_over)
  489. {
  490. setHot(_id);
  491. }
  492. if (isHot(_id)
  493. && m_leftPressed)
  494. {
  495. setActive(_id);
  496. }
  497. }
  498. // if button is active, then react on left up
  499. if (isActive(_id) )
  500. {
  501. m_isActive = true;
  502. if (_over)
  503. {
  504. setHot(_id);
  505. }
  506. if (m_leftReleased)
  507. {
  508. if (isHot(_id) )
  509. {
  510. res = true;
  511. }
  512. clearActive();
  513. }
  514. }
  515. if (isHot(_id) )
  516. {
  517. m_isHot = true;
  518. }
  519. return res;
  520. }
  521. void inputLogic(uint32_t _id, bool _over)
  522. {
  523. if (!anyActive() )
  524. {
  525. if (_over)
  526. {
  527. setHot(_id);
  528. }
  529. if (isHot(_id)
  530. && m_leftPressed)
  531. {
  532. // Toggle active input.
  533. if (isActiveInputField(_id))
  534. {
  535. clearActiveInputField();
  536. }
  537. else
  538. {
  539. setActiveInputField(_id);
  540. }
  541. }
  542. }
  543. if (isHot(_id) )
  544. {
  545. m_isHot = true;
  546. }
  547. if (m_leftPressed
  548. && !m_isHot
  549. && m_inputField != 0)
  550. {
  551. clearActiveInputField();
  552. }
  553. }
  554. void updateInput(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, char _inputChar)
  555. {
  556. bool left = (_button & IMGUI_MBUT_LEFT) != 0;
  557. m_mx = _mx;
  558. m_my = _my;
  559. m_leftPressed = !m_left && left;
  560. m_leftReleased = m_left && !left;
  561. m_left = left;
  562. m_scroll = _scroll;
  563. _inputChar = _inputChar & 0x7f; // ASCII of GTFO! :)
  564. m_lastChar = m_char;
  565. m_char = _inputChar;
  566. }
  567. void beginFrame(int32_t _mx, int32_t _my, uint8_t _button, int32_t _scroll, uint16_t _width, uint16_t _height, char _inputChar, uint8_t _view)
  568. {
  569. nvgBeginFrame(m_nvg, _width, _height, 1.0f, NVG_STRAIGHT_ALPHA);
  570. m_view = _view;
  571. m_viewWidth = _width;
  572. m_viewHeight = _height;
  573. bgfx::setViewSeq(_view, true);
  574. bgfx::setViewRect(_view, 0, 0, _width, _height);
  575. float proj[16];
  576. bx::mtxOrtho(proj, 0.0f, (float)_width, (float)_height, 0.0f, 0.0f, 1000.0f);
  577. bgfx::setViewTransform(_view, NULL, proj);
  578. updateInput(_mx, _my, _button, _scroll, _inputChar);
  579. m_hot = m_hotToBe;
  580. m_hotToBe = 0;
  581. m_wentActive = false;
  582. m_isActive = false;
  583. m_isHot = false;
  584. m_widgetX = 0;
  585. m_widgetY = 0;
  586. m_widgetW = 0;
  587. m_areaId = 0;
  588. m_widgetId = 0;
  589. m_enabledAreaIds = 0;
  590. }
  591. void endFrame()
  592. {
  593. clearInput();
  594. nvgEndFrame(m_nvg);
  595. }
  596. bool beginScrollArea(const char* _name, int32_t _x, int32_t _y, int32_t _width, int32_t _height, int32_t* _scroll, bool _enabled, int32_t _r)
  597. {
  598. m_areaId++;
  599. m_widgetId = 0;
  600. m_scrollId = (m_areaId << 8) | m_widgetId;
  601. if (_enabled)
  602. {
  603. setEnabled(m_areaId);
  604. }
  605. if (0 == _r)
  606. {
  607. drawRect( (float)_x
  608. , (float)_y
  609. , (float)_width + 0.3f /*border fix for seamlessly joining two scroll areas*/
  610. , (float)_height + 0.3f /*border fix for seamlessly joining two scroll areas*/
  611. , imguiRGBA(0, 0, 0, 192)
  612. );
  613. }
  614. else
  615. {
  616. drawRoundedRect( (float)_x
  617. , (float)_y
  618. , (float)_width
  619. , (float)_height
  620. , (float)_r
  621. , imguiRGBA(0, 0, 0, 192)
  622. );
  623. }
  624. const bool hasTitle = (NULL != _name && '\0' != _name[0]);
  625. int32_t header = 0;
  626. if (hasTitle)
  627. {
  628. drawText(_x + 10
  629. , _y + 18
  630. , ImguiTextAlign::Left
  631. , _name
  632. , imguiRGBA(255, 255, 255, 128)
  633. );
  634. header = AREA_HEADER;
  635. }
  636. const int32_t contentX = _x + SCROLL_AREA_PADDING;
  637. const int32_t contentY = _y + SCROLL_AREA_PADDING + header;
  638. const int32_t contentWidth = _width - SCROLL_AREA_PADDING * 3;
  639. const int32_t contentHeight = _height - 2*SCROLL_AREA_PADDING - header;
  640. nvgScissor(m_nvg
  641. , float(contentX)
  642. , float(contentY-1)
  643. , float(contentWidth)
  644. , float(contentHeight+1)
  645. );
  646. m_scissor = bgfx::setScissor(uint16_t(contentX)
  647. , uint16_t(contentY-1)
  648. , uint16_t(contentWidth)
  649. , uint16_t(contentHeight+1)
  650. );
  651. m_widgetX = contentX;
  652. m_widgetY = contentY + (*_scroll);
  653. m_widgetW = _width - SCROLL_AREA_PADDING * 4 - 2;
  654. m_scrollTop = contentY;
  655. m_scrollHeight = contentHeight;
  656. m_scrollRight = _x + _width - SCROLL_AREA_PADDING * 3;
  657. m_scrollVal = _scroll;
  658. m_scrollAreaX = _x;
  659. m_scrollAreaWidth = _width;
  660. m_scrollAreaInnerWidth = m_widgetW;
  661. m_scrollAreaTop = m_widgetY;
  662. m_insideScrollArea = inRect(_x, _y, _width, _height, false);
  663. m_insideCurrentScroll = m_insideScrollArea;
  664. return m_insideScrollArea;
  665. }
  666. void endScrollArea(int32_t _r)
  667. {
  668. // Disable scissoring.
  669. m_scissor = UINT16_MAX;
  670. nvgResetScissor(m_nvg);
  671. // Draw scroll bar
  672. int32_t xx = m_scrollRight + SCROLL_AREA_PADDING / 2;
  673. int32_t yy = m_scrollTop;
  674. int32_t width = SCROLL_AREA_PADDING * 2;
  675. int32_t height = m_scrollHeight;
  676. int32_t stop = m_scrollAreaTop;
  677. int32_t sbot = m_widgetY - DEFAULT_SPACING;
  678. int32_t sh = sbot - stop; // The scrollable area height.
  679. float barHeight = (float)height / (float)sh;
  680. // Handle m_scrollVal properly on variable scrollable area height.
  681. const int32_t diff = height - sh;
  682. if (diff < 0)
  683. {
  684. *m_scrollVal = (*m_scrollVal > diff) ? *m_scrollVal : diff; // m_scrollVal = max(diff, m_scrollVal).
  685. }
  686. else
  687. {
  688. *m_scrollVal = 0;
  689. }
  690. if (barHeight < 1.0f)
  691. {
  692. float barY = bx::fsaturate( (float)(yy - stop) / (float)sh);
  693. // Handle scroll bar logic.
  694. uint32_t hid = m_scrollId;
  695. int32_t hx = xx;
  696. int32_t hy = yy + (int)(barY * height);
  697. int32_t hw = width;
  698. int32_t hh = (int)(barHeight * height);
  699. const int32_t range = height - (hh - 1);
  700. bool over = inRect(hx, hy, hw, hh);
  701. buttonLogic(hid, over);
  702. if (isActive(hid) )
  703. {
  704. float uu = (float)(hy - yy) / (float)range;
  705. if (m_wentActive)
  706. {
  707. m_dragY = m_my;
  708. m_dragOrig = uu;
  709. }
  710. if (m_dragY != m_my)
  711. {
  712. uu = bx::fsaturate(m_dragOrig + (m_my - m_dragY) / (float)range);
  713. *m_scrollVal = (int)(uu * (height - sh) );
  714. }
  715. }
  716. // BG
  717. if (0 == _r)
  718. {
  719. drawRect( (float)xx
  720. , (float)yy
  721. , (float)width
  722. , (float)height
  723. , imguiRGBA(0, 0, 0, 196)
  724. );
  725. }
  726. else
  727. {
  728. drawRoundedRect( (float)xx
  729. , (float)yy
  730. , (float)width
  731. , (float)height
  732. , (float)_r
  733. , imguiRGBA(0, 0, 0, 196)
  734. );
  735. }
  736. // Bar
  737. if (isActive(hid) )
  738. {
  739. if (0 == _r)
  740. {
  741. drawRect( (float)hx
  742. , (float)hy
  743. , (float)hw
  744. , (float)hh
  745. , imguiRGBA(255, 196, 0, 196)
  746. );
  747. }
  748. else
  749. {
  750. drawRoundedRect( (float)hx
  751. , (float)hy
  752. , (float)hw
  753. , (float)hh
  754. , (float)_r
  755. , imguiRGBA(255, 196, 0, 196)
  756. );
  757. }
  758. }
  759. else
  760. {
  761. if (0 == _r)
  762. {
  763. drawRect( (float)hx
  764. , (float)hy
  765. , (float)hw
  766. , (float)hh
  767. , isHot(hid) ? imguiRGBA(255, 196, 0, 96) : imguiRGBA(255, 255, 255, 64)
  768. );
  769. }
  770. else
  771. {
  772. drawRoundedRect( (float)hx
  773. , (float)hy
  774. , (float)hw
  775. , (float)hh
  776. , (float)_r
  777. , isHot(hid) ? imguiRGBA(255, 196, 0, 96) : imguiRGBA(255, 255, 255, 64)
  778. );
  779. }
  780. }
  781. // Handle mouse scrolling.
  782. if (m_insideScrollArea) // && !anyActive() )
  783. {
  784. if (m_scroll)
  785. {
  786. *m_scrollVal += bx::uint32_clamp(20 * m_scroll, 0, sh - height);
  787. }
  788. }
  789. }
  790. m_insideCurrentScroll = false;
  791. }
  792. bool button(const char* _text, bool _enabled, uint32_t _rgb0, int32_t _r)
  793. {
  794. m_widgetId++;
  795. uint16_t id = (m_areaId << 8) | m_widgetId;
  796. int32_t xx = m_widgetX;
  797. int32_t yy = m_widgetY;
  798. int32_t width = m_widgetW;
  799. int32_t height = BUTTON_HEIGHT;
  800. m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  801. bool enabled = _enabled && isEnabled(m_areaId);
  802. bool over = enabled && inRect(xx, yy, width, height);
  803. bool res = buttonLogic(id, over);
  804. const uint32_t rgb0 = _rgb0&0x00ffffff;
  805. if (0 == _r)
  806. {
  807. drawRect( (float)xx
  808. , (float)yy
  809. , (float)width
  810. , (float)height
  811. , rgb0 | imguiRGBA(0, 0, 0, isActive(id) ? 196 : 96)
  812. );
  813. }
  814. else
  815. {
  816. drawRoundedRect( (float)xx
  817. , (float)yy
  818. , (float)width
  819. , (float)height
  820. , (float)_r
  821. , rgb0 | imguiRGBA(0, 0, 0, isActive(id) ? 196 : 96)
  822. );
  823. }
  824. if (enabled)
  825. {
  826. drawText(xx + BUTTON_HEIGHT / 2
  827. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  828. , ImguiTextAlign::Left
  829. , _text
  830. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  831. );
  832. }
  833. else
  834. {
  835. drawText(xx + BUTTON_HEIGHT / 2
  836. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  837. , ImguiTextAlign::Left
  838. , _text
  839. , imguiRGBA(128, 128, 128, 200)
  840. );
  841. }
  842. return res;
  843. }
  844. bool item(const char* _text, bool _enabled)
  845. {
  846. m_widgetId++;
  847. uint16_t id = (m_areaId << 8) | m_widgetId;
  848. int32_t xx = m_widgetX;
  849. int32_t yy = m_widgetY;
  850. int32_t width = m_widgetW;
  851. int32_t height = BUTTON_HEIGHT;
  852. m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  853. bool enabled = _enabled && isEnabled(m_areaId);
  854. bool over = enabled && inRect(xx, yy, width, height);
  855. bool res = buttonLogic(id, over);
  856. if (isHot(id) )
  857. {
  858. drawRoundedRect( (float)xx
  859. , (float)yy
  860. , (float)width
  861. , (float)height
  862. , 2.0f
  863. , imguiRGBA(255, 196, 0, isActive(id) ? 196 : 96)
  864. );
  865. }
  866. if (enabled)
  867. {
  868. drawText(xx + BUTTON_HEIGHT / 2
  869. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  870. , ImguiTextAlign::Left
  871. , _text
  872. , imguiRGBA(255, 255, 255, 200)
  873. );
  874. }
  875. else
  876. {
  877. drawText(xx + BUTTON_HEIGHT / 2
  878. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  879. , ImguiTextAlign::Left
  880. , _text
  881. , imguiRGBA(128, 128, 128, 200)
  882. );
  883. }
  884. return res;
  885. }
  886. bool check(const char* _text, bool _checked, bool _enabled)
  887. {
  888. m_widgetId++;
  889. uint16_t id = (m_areaId << 8) | m_widgetId;
  890. int32_t xx = m_widgetX;
  891. int32_t yy = m_widgetY;
  892. int32_t width = m_widgetW;
  893. int32_t height = BUTTON_HEIGHT;
  894. m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  895. bool enabled = _enabled && isEnabled(m_areaId);
  896. bool over = enabled && inRect(xx, yy, width, height);
  897. bool res = buttonLogic(id, over);
  898. const int32_t cx = xx + BUTTON_HEIGHT / 2 - CHECK_SIZE / 2;
  899. const int32_t cy = yy + BUTTON_HEIGHT / 2 - CHECK_SIZE / 2;
  900. drawRoundedRect( (float)cx - 3
  901. , (float)cy - 3
  902. , (float)CHECK_SIZE + 6
  903. , (float)CHECK_SIZE + 6
  904. , 4
  905. , imguiRGBA(128, 128, 128, isActive(id) ? 196 : 96)
  906. );
  907. if (_checked)
  908. {
  909. if (enabled)
  910. {
  911. drawRoundedRect( (float)cx
  912. , (float)cy
  913. , (float)CHECK_SIZE
  914. , (float)CHECK_SIZE
  915. , (float)CHECK_SIZE / 2 - 1
  916. , imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200)
  917. );
  918. }
  919. else
  920. {
  921. drawRoundedRect( (float)cx
  922. , (float)cy
  923. , (float)CHECK_SIZE
  924. , (float)CHECK_SIZE
  925. , (float)CHECK_SIZE / 2 - 1
  926. , imguiRGBA(128, 128, 128, 200)
  927. );
  928. }
  929. }
  930. if (enabled)
  931. {
  932. drawText(xx + BUTTON_HEIGHT
  933. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  934. , ImguiTextAlign::Left
  935. , _text
  936. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  937. );
  938. }
  939. else
  940. {
  941. drawText(xx + BUTTON_HEIGHT
  942. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  943. , ImguiTextAlign::Left
  944. , _text
  945. , imguiRGBA(128, 128, 128, 200)
  946. );
  947. }
  948. return res;
  949. }
  950. void input(const char* _label, char* _str, uint32_t _len, bool _enabled, int32_t _r)
  951. {
  952. m_widgetId++;
  953. const uint16_t id = (m_areaId << 8) | m_widgetId;
  954. int32_t xx = m_widgetX;
  955. int32_t yy = m_widgetY;
  956. m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  957. const bool drawLabel = (NULL != _label && _label[0] != '\0');
  958. if (drawLabel)
  959. {
  960. drawText(xx
  961. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  962. , ImguiTextAlign::Left
  963. , _label
  964. , imguiRGBA(255, 255, 255, 200)
  965. );
  966. }
  967. // Handle input.
  968. if (isActiveInputField(id) )
  969. {
  970. const size_t cursor = size_t(strlen(_str));
  971. if (m_char == 0x08) //backspace
  972. {
  973. _str[cursor-1] = '\0';
  974. }
  975. else if (m_char == 0x0d || m_char == 0x1b) //enter or escape
  976. {
  977. clearActiveInputField();
  978. }
  979. else if (cursor < _len-1
  980. && 0 != m_char)
  981. {
  982. _str[cursor] = m_char;
  983. _str[cursor+1] = '\0';
  984. }
  985. }
  986. // Draw input area.
  987. int32_t height = BUTTON_HEIGHT;
  988. int32_t width = m_widgetW;
  989. if (drawLabel)
  990. {
  991. uint32_t numVertices = 0; //unused
  992. const int32_t labelWidth = int32_t(getTextLength(m_fonts[m_currentFontIdx].m_cdata, _label, numVertices));
  993. xx += (labelWidth + 6);
  994. width -= (labelWidth + 6);
  995. }
  996. const bool enabled = _enabled && isEnabled(m_areaId);
  997. const bool over = enabled && inRect(xx, yy, width, height);
  998. inputLogic(id, over);
  999. if (0 == _r)
  1000. {
  1001. drawRect( (float)xx
  1002. , (float)yy
  1003. , (float)width
  1004. , (float)height
  1005. , isActiveInputField(id)?imguiRGBA(255,196,0,255):imguiRGBA(128,128,128,96)
  1006. );
  1007. }
  1008. else
  1009. {
  1010. drawRoundedRect( (float)xx
  1011. , (float)yy
  1012. , (float)width
  1013. , (float)height
  1014. , (float)_r
  1015. , isActiveInputField(id)?imguiRGBA(255,196,0,255):imguiRGBA(128,128,128,96)
  1016. );
  1017. }
  1018. if (isActiveInputField(id) )
  1019. {
  1020. drawText(xx + 6
  1021. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1022. , ImguiTextAlign::Left
  1023. , _str
  1024. , imguiRGBA(0, 0, 0, 255)
  1025. );
  1026. }
  1027. else
  1028. {
  1029. drawText(xx + 6
  1030. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1031. , ImguiTextAlign::Left
  1032. , _str
  1033. , isHot(id) ? imguiRGBA(255,196,0,255) : imguiRGBA(255,255,255,255)
  1034. );
  1035. }
  1036. }
  1037. uint8_t tabs(uint8_t _selected, bool _enabled, int32_t _height, int32_t _r, va_list _argList)
  1038. {
  1039. uint8_t count;
  1040. const char* titles[16];
  1041. const char* str = va_arg(_argList, const char*);
  1042. for (count = 0; str != NULL || count >= 16; ++count, str = va_arg(_argList, const char*) )
  1043. {
  1044. titles[count] = str;
  1045. }
  1046. const int32_t yy = m_widgetY;
  1047. m_widgetY += _height + DEFAULT_SPACING;
  1048. uint8_t selected = _selected;
  1049. const int32_t tabWidth = m_widgetW / count;
  1050. const int32_t tabWidthHalf = m_widgetW / (count*2);
  1051. const int32_t textY = yy + _height/2 + int32_t(m_fonts[m_currentFontIdx].m_size)/2 - 2;
  1052. if (0 == _r)
  1053. {
  1054. drawRect( (float)m_widgetX
  1055. , (float)yy
  1056. , (float)m_widgetW
  1057. , (float)_height
  1058. , imguiRGBA(128, 128, 128, 96)
  1059. );
  1060. }
  1061. else
  1062. {
  1063. drawRoundedRect( (float)m_widgetX
  1064. , (float)yy
  1065. , (float)m_widgetW
  1066. , (float)_height
  1067. , (float)_r
  1068. , imguiRGBA(128, 128, 128, 96)
  1069. );
  1070. }
  1071. for (uint8_t ii = 0; ii < count; ++ii)
  1072. {
  1073. m_widgetId++;
  1074. const uint16_t id = (m_areaId << 8) | m_widgetId;
  1075. int32_t xx = m_widgetX + ii*tabWidth;
  1076. int32_t textX = xx + tabWidthHalf;
  1077. const bool enabled = _enabled && isEnabled(m_areaId);
  1078. const bool over = enabled && inRect(xx, yy, tabWidth, _height);
  1079. const bool res = buttonLogic(id, over);
  1080. if (res)
  1081. {
  1082. selected = ii;
  1083. }
  1084. uint32_t textColor;
  1085. if (ii == selected)
  1086. {
  1087. textColor = enabled?imguiRGBA(0,0,0,255):imguiRGBA(255,255,255,100);
  1088. if (0 == _r)
  1089. {
  1090. drawRect( (float)xx
  1091. , (float)yy
  1092. , (float)tabWidth
  1093. , (float)_height
  1094. , enabled?imguiRGBA(255,196,0,200):imguiRGBA(128,128,128,96)
  1095. );
  1096. }
  1097. else
  1098. {
  1099. drawRoundedRect( (float)xx
  1100. , (float)yy
  1101. , (float)tabWidth
  1102. , (float)_height
  1103. , (float)_r
  1104. , enabled?imguiRGBA(255,196,0,200):imguiRGBA(128,128,128,96)
  1105. );
  1106. }
  1107. }
  1108. else
  1109. {
  1110. textColor = isHot(id) ? imguiRGBA(255, 196, 0, enabled?255:100) : imguiRGBA(255, 255, 255, enabled?200:100);
  1111. }
  1112. drawText(textX
  1113. , textY
  1114. , ImguiTextAlign::Center
  1115. , titles[ii]
  1116. , textColor
  1117. );
  1118. }
  1119. return selected;
  1120. }
  1121. void image(bgfx::TextureHandle _image, float _lod, int32_t _width, int32_t _height, ImguiImageAlign::Enum _align)
  1122. {
  1123. int32_t xx;
  1124. if (ImguiImageAlign::Left == _align)
  1125. {
  1126. xx = m_scrollAreaX + SCROLL_AREA_PADDING;
  1127. }
  1128. else if (ImguiImageAlign::LeftIndented == _align)
  1129. {
  1130. xx = m_widgetX;
  1131. }
  1132. else if (ImguiImageAlign::Center == _align)
  1133. {
  1134. xx = m_scrollAreaX + (m_scrollAreaInnerWidth-_width)/2;
  1135. }
  1136. else if (ImguiImageAlign::CenterIndented == _align)
  1137. {
  1138. xx = (m_widgetX + m_scrollAreaInnerWidth + m_scrollAreaX - _width)/2;
  1139. }
  1140. else //if (ImguiImageAlign::Right == _align).
  1141. {
  1142. xx = m_scrollAreaX + m_scrollAreaInnerWidth - _width;
  1143. }
  1144. const int32_t yy = m_widgetY;
  1145. m_widgetY += _height + DEFAULT_SPACING;
  1146. screenQuad(xx, yy, _width, _height);
  1147. bgfx::setUniform(u_imageLod, &_lod);
  1148. bgfx::setTexture(0, s_texColor, bgfx::isValid(_image) ? _image : m_missingTexture);
  1149. bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
  1150. bgfx::setProgram(m_imageProgram);
  1151. bgfx::setScissor(m_scissor);
  1152. bgfx::submit(m_view);
  1153. }
  1154. void image(bgfx::TextureHandle _image, float _lod, float _width, float _aspect, ImguiImageAlign::Enum _align)
  1155. {
  1156. const float width = _width*float(m_scrollAreaInnerWidth);
  1157. const float height = width/_aspect;
  1158. image(_image, _lod, int32_t(width), int32_t(height), _align);
  1159. }
  1160. void imageChannel(bgfx::TextureHandle _image, uint8_t _channel, float _lod, int32_t _width, int32_t _height, ImguiImageAlign::Enum _align)
  1161. {
  1162. BX_CHECK(_channel < 4, "Channel param must be from 0 to 3!");
  1163. int32_t xx;
  1164. if (ImguiImageAlign::Left == _align)
  1165. {
  1166. xx = m_scrollAreaX + SCROLL_AREA_PADDING;
  1167. }
  1168. else if (ImguiImageAlign::LeftIndented == _align)
  1169. {
  1170. xx = m_widgetX;
  1171. }
  1172. else if (ImguiImageAlign::Center == _align)
  1173. {
  1174. xx = m_scrollAreaX + (m_scrollAreaInnerWidth-_width)/2;
  1175. }
  1176. else if (ImguiImageAlign::CenterIndented == _align)
  1177. {
  1178. xx = (m_widgetX + m_scrollAreaInnerWidth + m_scrollAreaX - _width)/2;
  1179. }
  1180. else //if (ImguiImageAlign::Right == _align).
  1181. {
  1182. xx = m_scrollAreaX + m_scrollAreaInnerWidth - _width;
  1183. }
  1184. const int32_t yy = m_widgetY;
  1185. m_widgetY += _height + DEFAULT_SPACING;
  1186. screenQuad(xx, yy, _width, _height);
  1187. bgfx::setUniform(u_imageLod, &_lod);
  1188. float swizz[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  1189. swizz[_channel] = 1.0f;
  1190. bgfx::setUniform(u_imageSwizzle, swizz);
  1191. bgfx::setTexture(0, s_texColor, bgfx::isValid(_image) ? _image : m_missingTexture);
  1192. bgfx::setState(BGFX_STATE_RGB_WRITE|BGFX_STATE_ALPHA_WRITE);
  1193. bgfx::setProgram(m_imageSwizzProgram);
  1194. bgfx::setScissor(m_scissor);
  1195. bgfx::submit(m_view);
  1196. }
  1197. void imageChannel(bgfx::TextureHandle _image, uint8_t _channel, float _lod, float _width, float _aspect, ImguiImageAlign::Enum _align)
  1198. {
  1199. const float width = _width*float(m_scrollAreaInnerWidth);
  1200. const float height = width/_aspect;
  1201. imageChannel(_image, _channel, _lod, int32_t(width), int32_t(height), _align);
  1202. }
  1203. bool collapse(const char* _text, const char* _subtext, bool _checked, bool _enabled)
  1204. {
  1205. m_widgetId++;
  1206. uint16_t id = (m_areaId << 8) | m_widgetId;
  1207. int32_t xx = m_widgetX;
  1208. int32_t yy = m_widgetY;
  1209. int32_t width = m_widgetW;
  1210. int32_t height = BUTTON_HEIGHT;
  1211. m_widgetY += BUTTON_HEIGHT + DEFAULT_SPACING;
  1212. const int32_t cx = xx + BUTTON_HEIGHT/2 - CHECK_SIZE/2;
  1213. const int32_t cy = yy + BUTTON_HEIGHT/2 - CHECK_SIZE/2 + DEFAULT_SPACING/2;
  1214. const int32_t textY = yy + BUTTON_HEIGHT/2 + TEXT_HEIGHT/2 + DEFAULT_SPACING/2;
  1215. bool enabled = _enabled && isEnabled(m_areaId);
  1216. bool over = enabled && inRect(xx, yy, width, height);
  1217. bool res = buttonLogic(id, over);
  1218. if (_checked)
  1219. {
  1220. drawTriangle(cx
  1221. , cy
  1222. , CHECK_SIZE
  1223. , CHECK_SIZE
  1224. , TriangleOrientation::Up
  1225. , imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200)
  1226. );
  1227. }
  1228. else
  1229. {
  1230. drawTriangle(cx
  1231. , cy
  1232. , CHECK_SIZE
  1233. , CHECK_SIZE
  1234. , TriangleOrientation::Right
  1235. , imguiRGBA(255, 255, 255, isActive(id) ? 255 : 200)
  1236. );
  1237. }
  1238. if (enabled)
  1239. {
  1240. drawText(xx + BUTTON_HEIGHT
  1241. , textY
  1242. , ImguiTextAlign::Left
  1243. , _text
  1244. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  1245. );
  1246. }
  1247. else
  1248. {
  1249. drawText(xx + BUTTON_HEIGHT
  1250. , textY
  1251. , ImguiTextAlign::Left
  1252. , _text
  1253. , imguiRGBA(128, 128, 128, 200)
  1254. );
  1255. }
  1256. if (_subtext)
  1257. {
  1258. drawText(xx + width - BUTTON_HEIGHT / 2
  1259. , textY
  1260. , ImguiTextAlign::Right
  1261. , _subtext
  1262. , imguiRGBA(255, 255, 255, 128)
  1263. );
  1264. }
  1265. return res;
  1266. }
  1267. bool borderButton(ImguiBorder::Enum _border, bool _checked, bool _enabled)
  1268. {
  1269. m_widgetId++;
  1270. uint16_t id = (m_areaId << 8) | m_widgetId;
  1271. const int32_t triSize = 12;
  1272. const int32_t borderSize = 15;
  1273. int32_t xx;
  1274. int32_t yy;
  1275. int32_t width;
  1276. int32_t height;
  1277. int32_t triX;
  1278. int32_t triY;
  1279. TriangleOrientation::Enum orientation;
  1280. if (ImguiBorder::Left == _border)
  1281. {
  1282. xx = -borderSize;
  1283. yy = 0;
  1284. width = 2*borderSize;
  1285. height = m_viewHeight;
  1286. triX = 0;
  1287. triY = (m_viewHeight-triSize)/2;
  1288. orientation = _checked ? TriangleOrientation::Left : TriangleOrientation::Right;
  1289. }
  1290. else if (ImguiBorder::Right == _border)
  1291. {
  1292. xx = m_viewWidth - borderSize;
  1293. yy = 0;
  1294. width = 2*borderSize;
  1295. height = m_viewHeight;
  1296. triX = m_viewWidth - triSize - 2;
  1297. triY = (m_viewHeight-width)/2;
  1298. orientation = _checked ? TriangleOrientation::Right : TriangleOrientation::Left;
  1299. }
  1300. else if (ImguiBorder::Top == _border)
  1301. {
  1302. xx = 0;
  1303. yy = -borderSize;
  1304. width = m_viewWidth;
  1305. height = 2*borderSize;
  1306. triX = (m_viewWidth-triSize)/2;
  1307. triY = 0;
  1308. orientation = _checked ? TriangleOrientation::Up : TriangleOrientation::Down;
  1309. }
  1310. else //if (ImguiBorder::Bottom == _border).
  1311. {
  1312. xx = 0;
  1313. yy = m_viewHeight - borderSize;
  1314. width = m_viewWidth;
  1315. height = 2*borderSize;
  1316. triX = (m_viewWidth-triSize)/2;
  1317. triY = m_viewHeight-triSize;
  1318. orientation = _checked ? TriangleOrientation::Down : TriangleOrientation::Up;
  1319. }
  1320. const bool over = _enabled && inRect(xx, yy, width, height, false);
  1321. const bool res = buttonLogic(id, over);
  1322. drawRoundedRect( (float)xx
  1323. , (float)yy
  1324. , (float)width
  1325. , (float)height
  1326. , 0.0f
  1327. , isActive(id) ? imguiRGBA(23, 23, 23, 192) : imguiRGBA(0, 0, 0, 222)
  1328. );
  1329. drawTriangle( triX
  1330. , triY
  1331. , triSize
  1332. , triSize
  1333. , orientation
  1334. , isHot(id) ? imguiRGBA(255, 196, 0, 222) : imguiRGBA(255, 255, 255, 192)
  1335. );
  1336. return res;
  1337. }
  1338. void labelVargs(const char* _format, va_list _argList, bool _enabled)
  1339. {
  1340. char temp[8192];
  1341. char* out = temp;
  1342. int32_t len = bx::vsnprintf(out, sizeof(temp), _format, _argList);
  1343. if ( (int32_t)sizeof(temp) < len)
  1344. {
  1345. out = (char*)alloca(len+1);
  1346. len = bx::vsnprintf(out, len, _format, _argList);
  1347. }
  1348. out[len] = '\0';
  1349. int32_t xx = m_widgetX;
  1350. int32_t yy = m_widgetY;
  1351. m_widgetY += BUTTON_HEIGHT;
  1352. drawText(xx
  1353. , yy + BUTTON_HEIGHT/2 + TEXT_HEIGHT/2
  1354. , ImguiTextAlign::Left
  1355. , out
  1356. , _enabled?imguiRGBA(255, 255, 255, 255):imguiRGBA(255, 255, 255, 128)
  1357. );
  1358. }
  1359. void value(const char* _text)
  1360. {
  1361. const int32_t xx = m_widgetX;
  1362. const int32_t yy = m_widgetY;
  1363. const int32_t ww = m_widgetW;
  1364. m_widgetY += BUTTON_HEIGHT;
  1365. drawText(xx + ww - BUTTON_HEIGHT / 2
  1366. , yy + BUTTON_HEIGHT / 2 + TEXT_HEIGHT / 2
  1367. , ImguiTextAlign::Right
  1368. , _text
  1369. , imguiRGBA(255, 255, 255, 200)
  1370. );
  1371. }
  1372. bool slider(const char* _text, float& _val, float _vmin, float _vmax, float _vinc, bool _enabled)
  1373. {
  1374. m_widgetId++;
  1375. uint16_t id = (m_areaId << 8) | m_widgetId;
  1376. int32_t xx = m_widgetX;
  1377. int32_t yy = m_widgetY;
  1378. int32_t width = m_widgetW;
  1379. int32_t height = SLIDER_HEIGHT;
  1380. m_widgetY += SLIDER_HEIGHT + DEFAULT_SPACING;
  1381. drawRoundedRect( (float)xx, (float)yy, (float)width, (float)height, 4.0f, imguiRGBA(0, 0, 0, 128) );
  1382. const int32_t range = width - SLIDER_MARKER_WIDTH;
  1383. float uu = bx::fsaturate( (_val - _vmin) / (_vmax - _vmin) );
  1384. int32_t m = (int)(uu * range);
  1385. bool enabled = _enabled && isEnabled(m_areaId);
  1386. bool over = enabled && inRect(xx + m, yy, SLIDER_MARKER_WIDTH, SLIDER_HEIGHT);
  1387. bool res = buttonLogic(id, over);
  1388. bool valChanged = false;
  1389. if (isActive(id) )
  1390. {
  1391. if (m_wentActive)
  1392. {
  1393. m_dragX = m_mx;
  1394. m_dragOrig = uu;
  1395. }
  1396. if (m_dragX != m_mx)
  1397. {
  1398. uu = bx::fsaturate(m_dragOrig + (float)(m_mx - m_dragX) / (float)range);
  1399. _val = _vmin + uu * (_vmax - _vmin);
  1400. _val = floorf(_val / _vinc + 0.5f) * _vinc; // Snap to vinc
  1401. m = (int)(uu * range);
  1402. valChanged = true;
  1403. }
  1404. }
  1405. if (isActive(id) )
  1406. {
  1407. drawRoundedRect( (float)(xx + m)
  1408. , (float)yy
  1409. , (float)SLIDER_MARKER_WIDTH
  1410. , (float)SLIDER_HEIGHT
  1411. , 4.0f
  1412. , imguiRGBA(255, 255, 255, 255)
  1413. );
  1414. }
  1415. else
  1416. {
  1417. drawRoundedRect( (float)(xx + m)
  1418. , (float)yy
  1419. , (float)SLIDER_MARKER_WIDTH
  1420. , (float)SLIDER_HEIGHT
  1421. , 4.0f
  1422. , isHot(id) ? imguiRGBA(255, 196, 0, 128) : imguiRGBA(255, 255, 255, 64)
  1423. );
  1424. }
  1425. // TODO: fix this, take a look at 'nicenum'.
  1426. int32_t digits = (int)(ceilf(log10f(_vinc) ) );
  1427. char fmt[16];
  1428. bx::snprintf(fmt, 16, "%%.%df", digits >= 0 ? 0 : -digits);
  1429. char msg[128];
  1430. bx::snprintf(msg, 128, fmt, _val);
  1431. if (enabled)
  1432. {
  1433. drawText(xx + SLIDER_HEIGHT / 2
  1434. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  1435. , ImguiTextAlign::Left
  1436. , _text
  1437. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  1438. );
  1439. drawText(xx + width - SLIDER_HEIGHT / 2
  1440. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  1441. , ImguiTextAlign::Right
  1442. , msg
  1443. , isHot(id) ? imguiRGBA(255, 196, 0, 255) : imguiRGBA(255, 255, 255, 200)
  1444. );
  1445. }
  1446. else
  1447. {
  1448. drawText(xx + SLIDER_HEIGHT / 2
  1449. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  1450. , ImguiTextAlign::Left
  1451. , _text
  1452. , imguiRGBA(128, 128, 128, 200)
  1453. );
  1454. drawText(xx + width - SLIDER_HEIGHT / 2
  1455. , yy + SLIDER_HEIGHT / 2 + TEXT_HEIGHT / 2
  1456. , ImguiTextAlign::Right
  1457. , msg
  1458. , imguiRGBA(128, 128, 128, 200)
  1459. );
  1460. }
  1461. return res || valChanged;
  1462. }
  1463. void indent(uint16_t _width)
  1464. {
  1465. m_widgetX += _width;
  1466. m_widgetW -= _width;
  1467. }
  1468. void unindent(uint16_t _width)
  1469. {
  1470. m_widgetX -= _width;
  1471. m_widgetW += _width;
  1472. }
  1473. void separator(uint16_t _height)
  1474. {
  1475. m_widgetY += _height;
  1476. }
  1477. void separatorLine(uint16_t _height)
  1478. {
  1479. const int32_t rectWidth = m_widgetW;
  1480. const int32_t rectHeight = 1;
  1481. const int32_t xx = m_widgetX;
  1482. const int32_t yy = m_widgetY + _height/2 - rectHeight;
  1483. m_widgetY += _height;
  1484. drawRect( (float)xx
  1485. , (float)yy
  1486. , (float)rectWidth
  1487. , (float)rectHeight
  1488. , imguiRGBA(255, 255, 255, 32)
  1489. );
  1490. }
  1491. void drawPolygon(const float* _coords, uint32_t _numCoords, float _r, uint32_t _abgr)
  1492. {
  1493. _numCoords = bx::uint32_min(_numCoords, MAX_TEMP_COORDS);
  1494. for (uint32_t ii = 0, jj = _numCoords - 1; ii < _numCoords; jj = ii++)
  1495. {
  1496. const float* v0 = &_coords[jj * 2];
  1497. const float* v1 = &_coords[ii * 2];
  1498. float dx = v1[0] - v0[0];
  1499. float dy = v1[1] - v0[1];
  1500. float d = sqrtf(dx * dx + dy * dy);
  1501. if (d > 0)
  1502. {
  1503. d = 1.0f / d;
  1504. dx *= d;
  1505. dy *= d;
  1506. }
  1507. m_tempNormals[jj * 2 + 0] = dy;
  1508. m_tempNormals[jj * 2 + 1] = -dx;
  1509. }
  1510. for (uint32_t ii = 0, jj = _numCoords - 1; ii < _numCoords; jj = ii++)
  1511. {
  1512. float dlx0 = m_tempNormals[jj * 2 + 0];
  1513. float dly0 = m_tempNormals[jj * 2 + 1];
  1514. float dlx1 = m_tempNormals[ii * 2 + 0];
  1515. float dly1 = m_tempNormals[ii * 2 + 1];
  1516. float dmx = (dlx0 + dlx1) * 0.5f;
  1517. float dmy = (dly0 + dly1) * 0.5f;
  1518. float dmr2 = dmx * dmx + dmy * dmy;
  1519. if (dmr2 > 0.000001f)
  1520. {
  1521. float scale = 1.0f / dmr2;
  1522. if (scale > 10.0f)
  1523. {
  1524. scale = 10.0f;
  1525. }
  1526. dmx *= scale;
  1527. dmy *= scale;
  1528. }
  1529. m_tempCoords[ii * 2 + 0] = _coords[ii * 2 + 0] + dmx * _r;
  1530. m_tempCoords[ii * 2 + 1] = _coords[ii * 2 + 1] + dmy * _r;
  1531. }
  1532. uint32_t numVertices = _numCoords*6 + (_numCoords-2)*3;
  1533. if (bgfx::checkAvailTransientVertexBuffer(numVertices, PosColorVertex::ms_decl) )
  1534. {
  1535. bgfx::TransientVertexBuffer tvb;
  1536. bgfx::allocTransientVertexBuffer(&tvb, numVertices, PosColorVertex::ms_decl);
  1537. uint32_t trans = _abgr&0xffffff;
  1538. PosColorVertex* vertex = (PosColorVertex*)tvb.data;
  1539. for (uint32_t ii = 0, jj = _numCoords-1; ii < _numCoords; jj = ii++)
  1540. {
  1541. vertex->m_x = _coords[ii*2+0];
  1542. vertex->m_y = _coords[ii*2+1];
  1543. vertex->m_abgr = _abgr;
  1544. ++vertex;
  1545. vertex->m_x = _coords[jj*2+0];
  1546. vertex->m_y = _coords[jj*2+1];
  1547. vertex->m_abgr = _abgr;
  1548. ++vertex;
  1549. vertex->m_x = m_tempCoords[jj*2+0];
  1550. vertex->m_y = m_tempCoords[jj*2+1];
  1551. vertex->m_abgr = trans;
  1552. ++vertex;
  1553. vertex->m_x = m_tempCoords[jj*2+0];
  1554. vertex->m_y = m_tempCoords[jj*2+1];
  1555. vertex->m_abgr = trans;
  1556. ++vertex;
  1557. vertex->m_x = m_tempCoords[ii*2+0];
  1558. vertex->m_y = m_tempCoords[ii*2+1];
  1559. vertex->m_abgr = trans;
  1560. ++vertex;
  1561. vertex->m_x = _coords[ii*2+0];
  1562. vertex->m_y = _coords[ii*2+1];
  1563. vertex->m_abgr = _abgr;
  1564. ++vertex;
  1565. }
  1566. for (uint32_t ii = 2; ii < _numCoords; ++ii)
  1567. {
  1568. vertex->m_x = _coords[0];
  1569. vertex->m_y = _coords[1];
  1570. vertex->m_abgr = _abgr;
  1571. ++vertex;
  1572. vertex->m_x = _coords[(ii-1)*2+0];
  1573. vertex->m_y = _coords[(ii-1)*2+1];
  1574. vertex->m_abgr = _abgr;
  1575. ++vertex;
  1576. vertex->m_x = _coords[ii*2+0];
  1577. vertex->m_y = _coords[ii*2+1];
  1578. vertex->m_abgr = _abgr;
  1579. ++vertex;
  1580. }
  1581. bgfx::setVertexBuffer(&tvb);
  1582. bgfx::setState(0
  1583. | BGFX_STATE_RGB_WRITE
  1584. | BGFX_STATE_ALPHA_WRITE
  1585. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  1586. );
  1587. bgfx::setProgram(m_colorProgram);
  1588. bgfx::setScissor(m_scissor);
  1589. bgfx::submit(m_view);
  1590. }
  1591. }
  1592. void drawRect(float _x, float _y, float w, float h, uint32_t _argb, float _fth = 1.0f)
  1593. {
  1594. float verts[4 * 2] =
  1595. {
  1596. _x + 0.5f, _y + 0.5f,
  1597. _x + w - 0.5f, _y + 0.5f,
  1598. _x + w - 0.5f, _y + h - 0.5f,
  1599. _x + 0.5f, _y + h - 0.5f,
  1600. };
  1601. drawPolygon(verts, 4, _fth, _argb);
  1602. }
  1603. void drawRoundedRect(float _x, float _y, float w, float h, float r, uint32_t _argb, float _fth = 1.0f)
  1604. {
  1605. const uint32_t num = NUM_CIRCLE_VERTS / 4;
  1606. const float* cverts = m_circleVerts;
  1607. float verts[(num + 1) * 4 * 2];
  1608. float* vv = verts;
  1609. for (uint32_t ii = 0; ii <= num; ++ii)
  1610. {
  1611. *vv++ = _x + w - r + cverts[ii * 2] * r;
  1612. *vv++ = _y + h - r + cverts[ii * 2 + 1] * r;
  1613. }
  1614. for (uint32_t ii = num; ii <= num * 2; ++ii)
  1615. {
  1616. *vv++ = _x + r + cverts[ii * 2] * r;
  1617. *vv++ = _y + h - r + cverts[ii * 2 + 1] * r;
  1618. }
  1619. for (uint32_t ii = num * 2; ii <= num * 3; ++ii)
  1620. {
  1621. *vv++ = _x + r + cverts[ii * 2] * r;
  1622. *vv++ = _y + r + cverts[ii * 2 + 1] * r;
  1623. }
  1624. for (uint32_t ii = num * 3; ii < num * 4; ++ii)
  1625. {
  1626. *vv++ = _x + w - r + cverts[ii * 2] * r;
  1627. *vv++ = _y + r + cverts[ii * 2 + 1] * r;
  1628. }
  1629. *vv++ = _x + w - r + cverts[0] * r;
  1630. *vv++ = _y + r + cverts[1] * r;
  1631. drawPolygon(verts, (num + 1) * 4, _fth, _argb);
  1632. }
  1633. void drawLine(float _x0, float _y0, float _x1, float _y1, float _r, uint32_t _abgr, float _fth = 1.0f)
  1634. {
  1635. float dx = _x1 - _x0;
  1636. float dy = _y1 - _y0;
  1637. float d = sqrtf(dx * dx + dy * dy);
  1638. if (d > 0.0001f)
  1639. {
  1640. d = 1.0f / d;
  1641. dx *= d;
  1642. dy *= d;
  1643. }
  1644. float nx = dy;
  1645. float ny = -dx;
  1646. float verts[4 * 2];
  1647. _r -= _fth;
  1648. _r *= 0.5f;
  1649. if (_r < 0.01f)
  1650. {
  1651. _r = 0.01f;
  1652. }
  1653. dx *= _r;
  1654. dy *= _r;
  1655. nx *= _r;
  1656. ny *= _r;
  1657. verts[0] = _x0 - dx - nx;
  1658. verts[1] = _y0 - dy - ny;
  1659. verts[2] = _x0 - dx + nx;
  1660. verts[3] = _y0 - dy + ny;
  1661. verts[4] = _x1 + dx + nx;
  1662. verts[5] = _y1 + dy + ny;
  1663. verts[6] = _x1 + dx - nx;
  1664. verts[7] = _y1 + dy - ny;
  1665. drawPolygon(verts, 4, _fth, _abgr);
  1666. }
  1667. struct TriangleOrientation
  1668. {
  1669. enum Enum
  1670. {
  1671. Left,
  1672. Right,
  1673. Up,
  1674. Down,
  1675. };
  1676. };
  1677. void drawTriangle(int32_t _x, int32_t _y, int32_t _width, int32_t _height, TriangleOrientation::Enum _orientation, uint32_t _abgr)
  1678. {
  1679. if (TriangleOrientation::Left == _orientation)
  1680. {
  1681. const float verts[3 * 2] =
  1682. {
  1683. (float)_x + 0.5f + (float)_width * 1.0f, (float)_y + 0.5f,
  1684. (float)_x + 0.5f, (float)_y + 0.5f + (float)_height / 2.0f - 0.5f,
  1685. (float)_x + 0.5f + (float)_width * 1.0f, (float)_y + 0.5f + (float)_height - 1.0f,
  1686. };
  1687. drawPolygon(verts, 3, 1.0f, _abgr);
  1688. }
  1689. else if (TriangleOrientation::Right == _orientation)
  1690. {
  1691. const float verts[3 * 2] =
  1692. {
  1693. (float)_x + 0.5f, (float)_y + 0.5f,
  1694. (float)_x + 0.5f + (float)_width * 1.0f, (float)_y + 0.5f + (float)_height / 2.0f - 0.5f,
  1695. (float)_x + 0.5f, (float)_y + 0.5f + (float)_height - 1.0f,
  1696. };
  1697. drawPolygon(verts, 3, 1.0f, _abgr);
  1698. }
  1699. else if (TriangleOrientation::Up == _orientation)
  1700. {
  1701. const float verts[3 * 2] =
  1702. {
  1703. (float)_x + 0.5f, (float)_y + 0.5f + (float)_height - 1.0f,
  1704. (float)_x + 0.5f + (float)_width / 2.0f - 0.5f, (float)_y + 0.5f,
  1705. (float)_x + 0.5f + (float)_width - 1.0f, (float)_y + 0.5f + (float)_height - 1.0f,
  1706. };
  1707. drawPolygon(verts, 3, 1.0f, _abgr);
  1708. }
  1709. else //if (TriangleOrientation::Down == _orientation).
  1710. {
  1711. const float verts[3 * 2] =
  1712. {
  1713. (float)_x + 0.5f, (float)_y + 0.5f,
  1714. (float)_x + 0.5f + (float)_width / 2.0f - 0.5f, (float)_y + 0.5f + (float)_height - 1.0f,
  1715. (float)_x + 0.5f + (float)_width - 1.0f, (float)_y + 0.5f,
  1716. };
  1717. drawPolygon(verts, 3, 1.0f, _abgr);
  1718. }
  1719. }
  1720. #if !USE_NANOVG_FONT
  1721. void getBakedQuad(stbtt_bakedchar* _chardata, int32_t char_index, float* _xpos, float* _ypos, stbtt_aligned_quad* _quad)
  1722. {
  1723. stbtt_bakedchar* b = _chardata + char_index;
  1724. int32_t round_x = STBTT_ifloor(*_xpos + b->xoff);
  1725. int32_t round_y = STBTT_ifloor(*_ypos + b->yoff);
  1726. _quad->x0 = (float)round_x;
  1727. _quad->y0 = (float)round_y;
  1728. _quad->x1 = (float)round_x + b->x1 - b->x0;
  1729. _quad->y1 = (float)round_y + b->y1 - b->y0;
  1730. _quad->s0 = (b->x0 + m_halfTexel) * m_invTextureWidth;
  1731. _quad->t0 = (b->y0 + m_halfTexel) * m_invTextureWidth;
  1732. _quad->s1 = (b->x1 + m_halfTexel) * m_invTextureHeight;
  1733. _quad->t1 = (b->y1 + m_halfTexel) * m_invTextureHeight;
  1734. *_xpos += b->xadvance;
  1735. }
  1736. float getTextLength(stbtt_bakedchar* _chardata, const char* _text, uint32_t& _numVertices)
  1737. {
  1738. float xpos = 0;
  1739. float len = 0;
  1740. uint32_t numVertices = 0;
  1741. while (*_text)
  1742. {
  1743. int32_t ch = (uint8_t)*_text;
  1744. if (ch == '\t')
  1745. {
  1746. for (int32_t ii = 0; ii < 4; ++ii)
  1747. {
  1748. if (xpos < s_tabStops[ii])
  1749. {
  1750. xpos = s_tabStops[ii];
  1751. break;
  1752. }
  1753. }
  1754. }
  1755. else if (ch >= ' '
  1756. && ch < 128)
  1757. {
  1758. stbtt_bakedchar* b = _chardata + ch - ' ';
  1759. int32_t round_x = STBTT_ifloor( (xpos + b->xoff) + 0.5);
  1760. len = round_x + b->x1 - b->x0 + 0.5f;
  1761. xpos += b->xadvance;
  1762. numVertices += 6;
  1763. }
  1764. ++_text;
  1765. }
  1766. _numVertices = numVertices;
  1767. return len;
  1768. }
  1769. #endif // !USE_NANOVG_FONT
  1770. void drawText(int32_t _x, int32_t _y, ImguiTextAlign::Enum _align, const char* _text, uint32_t _abgr)
  1771. {
  1772. drawText( (float)_x, (float)_y, _text, _align, _abgr);
  1773. }
  1774. void drawText(float _x, float _y, const char* _text, ImguiTextAlign::Enum _align, uint32_t _abgr)
  1775. {
  1776. if (NULL == _text
  1777. || '\0' == _text[0])
  1778. {
  1779. return;
  1780. }
  1781. #if USE_NANOVG_FONT
  1782. static uint32_t textAlign[ImguiTextAlign::Count] =
  1783. {
  1784. NVG_ALIGN_LEFT,
  1785. NVG_ALIGN_CENTER,
  1786. NVG_ALIGN_RIGHT,
  1787. };
  1788. nvgTextAlign(m_nvg, textAlign[_align]);
  1789. nvgFontBlur(m_nvg, 0.0f);
  1790. nvgFillColor(m_nvg, nvgRGBAu(_abgr) );
  1791. nvgText(m_nvg, _x, _y, _text, NULL);
  1792. #else
  1793. uint32_t numVertices = 0;
  1794. if (_align == ImguiTextAlign::Center)
  1795. {
  1796. _x -= getTextLength(m_fonts[m_currentFontIdx].m_cdata, _text, numVertices) / 2;
  1797. }
  1798. else if (_align == ImguiTextAlign::Right)
  1799. {
  1800. _x -= getTextLength(m_fonts[m_currentFontIdx].m_cdata, _text, numVertices);
  1801. }
  1802. else // just count vertices
  1803. {
  1804. getTextLength(m_fonts[m_currentFontIdx].m_cdata, _text, numVertices);
  1805. }
  1806. if (bgfx::checkAvailTransientVertexBuffer(numVertices, PosColorUvVertex::ms_decl) )
  1807. {
  1808. bgfx::TransientVertexBuffer tvb;
  1809. bgfx::allocTransientVertexBuffer(&tvb, numVertices, PosColorUvVertex::ms_decl);
  1810. PosColorUvVertex* vertex = (PosColorUvVertex*)tvb.data;
  1811. const float ox = _x;
  1812. while (*_text)
  1813. {
  1814. int32_t ch = (uint8_t)*_text;
  1815. if (ch == '\t')
  1816. {
  1817. for (int32_t i = 0; i < 4; ++i)
  1818. {
  1819. if (_x < s_tabStops[i] + ox)
  1820. {
  1821. _x = s_tabStops[i] + ox;
  1822. break;
  1823. }
  1824. }
  1825. }
  1826. else if (ch >= ' '
  1827. && ch < 128)
  1828. {
  1829. stbtt_aligned_quad quad;
  1830. getBakedQuad(m_fonts[m_currentFontIdx].m_cdata, ch - 32, &_x, &_y, &quad);
  1831. vertex->m_x = quad.x0;
  1832. vertex->m_y = quad.y0;
  1833. vertex->m_u = quad.s0;
  1834. vertex->m_v = quad.t0;
  1835. vertex->m_abgr = _abgr;
  1836. ++vertex;
  1837. vertex->m_x = quad.x1;
  1838. vertex->m_y = quad.y1;
  1839. vertex->m_u = quad.s1;
  1840. vertex->m_v = quad.t1;
  1841. vertex->m_abgr = _abgr;
  1842. ++vertex;
  1843. vertex->m_x = quad.x1;
  1844. vertex->m_y = quad.y0;
  1845. vertex->m_u = quad.s1;
  1846. vertex->m_v = quad.t0;
  1847. vertex->m_abgr = _abgr;
  1848. ++vertex;
  1849. vertex->m_x = quad.x0;
  1850. vertex->m_y = quad.y0;
  1851. vertex->m_u = quad.s0;
  1852. vertex->m_v = quad.t0;
  1853. vertex->m_abgr = _abgr;
  1854. ++vertex;
  1855. vertex->m_x = quad.x0;
  1856. vertex->m_y = quad.y1;
  1857. vertex->m_u = quad.s0;
  1858. vertex->m_v = quad.t1;
  1859. vertex->m_abgr = _abgr;
  1860. ++vertex;
  1861. vertex->m_x = quad.x1;
  1862. vertex->m_y = quad.y1;
  1863. vertex->m_u = quad.s1;
  1864. vertex->m_v = quad.t1;
  1865. vertex->m_abgr = _abgr;
  1866. ++vertex;
  1867. }
  1868. ++_text;
  1869. }
  1870. bgfx::setTexture(0, s_texColor, m_fonts[m_currentFontIdx].m_texture);
  1871. bgfx::setVertexBuffer(&tvb);
  1872. bgfx::setState(0
  1873. | BGFX_STATE_RGB_WRITE
  1874. | BGFX_STATE_ALPHA_WRITE
  1875. | BGFX_STATE_BLEND_FUNC(BGFX_STATE_BLEND_SRC_ALPHA, BGFX_STATE_BLEND_INV_SRC_ALPHA)
  1876. );
  1877. bgfx::setProgram(m_textureProgram);
  1878. bgfx::setScissor(m_scissor);
  1879. bgfx::submit(m_view);
  1880. }
  1881. #endif // USE_NANOVG_FONT
  1882. }
  1883. void screenQuad(int32_t _x, int32_t _y, int32_t _width, uint32_t _height)
  1884. {
  1885. if (bgfx::checkAvailTransientVertexBuffer(6, PosUvVertex::ms_decl) )
  1886. {
  1887. bgfx::TransientVertexBuffer vb;
  1888. bgfx::allocTransientVertexBuffer(&vb, 6, PosUvVertex::ms_decl);
  1889. PosUvVertex* vertex = (PosUvVertex*)vb.data;
  1890. const float widthf = float(_width);
  1891. const float heightf = float(_height);
  1892. const float minx = float(_x);
  1893. const float miny = float(_y);
  1894. const float maxx = minx+widthf;
  1895. const float maxy = miny+heightf;
  1896. const float texelHalfW = m_halfTexel/widthf;
  1897. const float texelHalfH = m_halfTexel/heightf;
  1898. const float minu = texelHalfW;
  1899. const float maxu = 1.0f - texelHalfW;
  1900. const float minv = texelHalfH;
  1901. const float maxv = texelHalfH + 1.0f;
  1902. vertex[0].m_x = minx;
  1903. vertex[0].m_y = miny;
  1904. vertex[0].m_u = minu;
  1905. vertex[0].m_v = minv;
  1906. vertex[1].m_x = maxx;
  1907. vertex[1].m_y = miny;
  1908. vertex[1].m_u = maxu;
  1909. vertex[1].m_v = minv;
  1910. vertex[2].m_x = maxx;
  1911. vertex[2].m_y = maxy;
  1912. vertex[2].m_u = maxu;
  1913. vertex[2].m_v = maxv;
  1914. vertex[3].m_x = maxx;
  1915. vertex[3].m_y = maxy;
  1916. vertex[3].m_u = maxu;
  1917. vertex[3].m_v = maxv;
  1918. vertex[4].m_x = minx;
  1919. vertex[4].m_y = maxy;
  1920. vertex[4].m_u = minu;
  1921. vertex[4].m_v = maxv;
  1922. vertex[5].m_x = minx;
  1923. vertex[5].m_y = miny;
  1924. vertex[5].m_u = minu;
  1925. vertex[5].m_v = minv;
  1926. bgfx::setVertexBuffer(&vb);
  1927. }
  1928. }
  1929. void colorWheelWidget(float _rgb[3], bool _respectIndentation, bool _enabled)
  1930. {
  1931. m_widgetId++;
  1932. const uint16_t wheelId = (m_areaId << 8) | m_widgetId;
  1933. m_widgetId++;
  1934. const uint16_t triangleId = (m_areaId << 8) | m_widgetId;
  1935. const int32_t height = m_scrollAreaWidth - COLOR_WHEEL_PADDING;
  1936. const float heightf = float(height);
  1937. const float widthf = float(m_scrollAreaWidth - COLOR_WHEEL_PADDING);
  1938. const float xx = float( (_respectIndentation ? m_widgetX-SCROLL_AREA_PADDING : m_scrollAreaX) + COLOR_WHEEL_PADDING/2);
  1939. const float yy = float(m_widgetY);
  1940. m_widgetY += height + DEFAULT_SPACING;
  1941. const float ro = (widthf < heightf ? widthf : heightf) * 0.5f - 5.0f; // radiusOuter.
  1942. const float rd = 20.0f; // radiusDelta.
  1943. const float ri = ro - rd; // radiusInner.
  1944. const float aeps = 0.5f / ro; // Half a pixel arc length in radians (2pi cancels out).
  1945. const float center[2] = { xx + widthf*0.5f, yy + heightf*0.5f };
  1946. const float cmx = float(m_mx) - center[0];
  1947. const float cmy = float(m_my) - center[1];
  1948. const float aa[2] = { ri - 6.0f, 0.0f }; // Hue point.
  1949. const float bb[2] = { cosf(-120.0f/180.0f*NVG_PI) * aa[0], sinf(-120.0f/180.0f*NVG_PI) * aa[0] }; // Black point.
  1950. const float cc[2] = { cosf( 120.0f/180.0f*NVG_PI) * aa[0], sinf( 120.0f/180.0f*NVG_PI) * aa[0] }; // White point.
  1951. const float ca[2] = { aa[0] - cc[0], aa[1] - cc[1] };
  1952. const float lenCa = sqrtf(ca[0]*ca[0]+ca[1]*ca[1]);
  1953. const float invLenCa = 1.0f/lenCa;
  1954. const float dirCa[2] = { ca[0]*invLenCa, ca[1]*invLenCa };
  1955. float sel[2];
  1956. float hsv[3];
  1957. bx::rgbToHsv(hsv, _rgb);
  1958. bool enabled = _enabled && isEnabled(m_areaId);
  1959. if (enabled)
  1960. {
  1961. if (m_leftPressed)
  1962. {
  1963. const float len = sqrtf(cmx*cmx + cmy*cmy);
  1964. if (len > ri)
  1965. {
  1966. if (len < ro)
  1967. {
  1968. setActive(wheelId);
  1969. }
  1970. }
  1971. else
  1972. {
  1973. setActive(triangleId);
  1974. }
  1975. }
  1976. if (m_leftReleased
  1977. && (isActive(wheelId) || isActive(triangleId) ) )
  1978. {
  1979. clearActive();
  1980. }
  1981. // Set hue.
  1982. if (m_left
  1983. && isActive(wheelId) )
  1984. {
  1985. hsv[0] = atan2f(cmy, cmx)/NVG_PI*0.5f;
  1986. if (hsv[0] < 0.0f)
  1987. {
  1988. hsv[0]+=1.0f;
  1989. }
  1990. }
  1991. }
  1992. if (enabled
  1993. && m_left
  1994. && isActive(triangleId) )
  1995. {
  1996. float an = -hsv[0]*NVG_PI*2.0f;
  1997. float tmx = (cmx*cosf(an)-cmy*sinf(an) );
  1998. float tmy = (cmx*sinf(an)+cmy*cosf(an) );
  1999. if (pointInTriangle(tmx, tmy, aa[0], aa[1], bb[0], bb[1], cc[0], cc[1]) )
  2000. {
  2001. sel[0] = tmx;
  2002. sel[1] = tmy;
  2003. }
  2004. else
  2005. {
  2006. closestPointOnTriangle(sel[0], sel[1], tmx, tmy, aa[0], aa[1], bb[0], bb[1], cc[0], cc[1]);
  2007. }
  2008. }
  2009. else
  2010. {
  2011. /*
  2012. * bb (black)
  2013. * /\
  2014. * / \
  2015. * / \
  2016. * / \
  2017. * / \
  2018. * / .sel \
  2019. * / \
  2020. * cc(white)/____.ss_______\aa (hue)
  2021. */
  2022. const float ss[2] =
  2023. {
  2024. cc[0] + dirCa[0]*lenCa*hsv[1],
  2025. cc[1] + dirCa[1]*lenCa*hsv[1],
  2026. };
  2027. const float sb[2] = { bb[0]-ss[0], bb[1]-ss[1] };
  2028. const float lenSb = sqrtf(sb[0]*sb[0]+sb[1]*sb[1]);
  2029. const float invLenSb = 1.0f/lenSb;
  2030. const float dirSb[2] = { sb[0]*invLenSb, sb[1]*invLenSb };
  2031. sel[0] = cc[0] + dirCa[0]*lenCa*hsv[1] + dirSb[0]*lenSb*(1.0f - hsv[2]);
  2032. sel[1] = cc[1] + dirCa[1]*lenCa*hsv[1] + dirSb[1]*lenSb*(1.0f - hsv[2]);
  2033. }
  2034. float uu, vv, ww;
  2035. barycentric(uu, vv, ww
  2036. , aa[0], aa[1]
  2037. , bb[0], bb[1]
  2038. , cc[0], cc[1]
  2039. , sel[0], sel[1]
  2040. );
  2041. const float val = bx::fclamp(1.0f-vv, 0.0001f, 1.0f);
  2042. const float sat = bx::fclamp(uu/val, 0.0001f, 1.0f);
  2043. const float out[3] = { hsv[0], sat, val };
  2044. bx::hsvToRgb(_rgb, out);
  2045. // Draw widget.
  2046. nvgSave(m_nvg);
  2047. {
  2048. float saturation;
  2049. uint8_t alpha0;
  2050. uint8_t alpha1;
  2051. if (enabled)
  2052. {
  2053. saturation = 1.0f;
  2054. alpha0 = 255;
  2055. alpha1 = 192;
  2056. }
  2057. else
  2058. {
  2059. saturation = 0.0f;
  2060. alpha0 = 10;
  2061. alpha1 = 10;
  2062. }
  2063. // Circle.
  2064. for (uint8_t ii = 0; ii < 6; ii++)
  2065. {
  2066. const float a0 = float(ii)/6.0f * 2.0f*NVG_PI - aeps;
  2067. const float a1 = float(ii+1.0f)/6.0f * 2.0f*NVG_PI + aeps;
  2068. nvgBeginPath(m_nvg);
  2069. nvgArc(m_nvg, center[0], center[1], ri, a0, a1, NVG_CW);
  2070. nvgArc(m_nvg, center[0], center[1], ro, a1, a0, NVG_CCW);
  2071. nvgClosePath(m_nvg);
  2072. const float ax = center[0] + cosf(a0) * (ri+ro)*0.5f;
  2073. const float ay = center[1] + sinf(a0) * (ri+ro)*0.5f;
  2074. const float bx = center[0] + cosf(a1) * (ri+ro)*0.5f;
  2075. const float by = center[1] + sinf(a1) * (ri+ro)*0.5f;
  2076. NVGpaint paint = nvgLinearGradient(m_nvg
  2077. , ax, ay
  2078. , bx, by
  2079. , nvgHSLA(a0/NVG_PI*0.5f,saturation,0.55f,alpha0)
  2080. , nvgHSLA(a1/NVG_PI*0.5f,saturation,0.55f,alpha0)
  2081. );
  2082. nvgFillPaint(m_nvg, paint);
  2083. nvgFill(m_nvg);
  2084. }
  2085. // Circle stroke.
  2086. nvgBeginPath(m_nvg);
  2087. nvgCircle(m_nvg, center[0], center[1], ri-0.5f);
  2088. nvgCircle(m_nvg, center[0], center[1], ro+0.5f);
  2089. nvgStrokeColor(m_nvg, nvgRGBA(0,0,0,64) );
  2090. nvgStrokeWidth(m_nvg, 1.0f);
  2091. nvgStroke(m_nvg);
  2092. nvgSave(m_nvg);
  2093. {
  2094. // Hue selector.
  2095. nvgTranslate(m_nvg, center[0], center[1]);
  2096. nvgRotate(m_nvg, hsv[0]*NVG_PI*2.0f);
  2097. nvgStrokeWidth(m_nvg, 2.0f);
  2098. nvgBeginPath(m_nvg);
  2099. nvgRect(m_nvg, ri-1.0f,-3.0f,rd+2.0f,6.0f);
  2100. nvgStrokeColor(m_nvg, nvgRGBA(255,255,255,alpha1) );
  2101. nvgStroke(m_nvg);
  2102. // Hue selector drop shadow.
  2103. 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) );
  2104. nvgBeginPath(m_nvg);
  2105. nvgRect(m_nvg, ri-2.0f-10.0f,-4.0f-10.0f,ro-ri+4.0f+20.0f,8.0f+20.0f);
  2106. nvgRect(m_nvg, ri-2.0f,-4.0f,ro-ri+4.0f,8.0f);
  2107. nvgPathWinding(m_nvg, NVG_HOLE);
  2108. nvgFillPaint(m_nvg, paint);
  2109. nvgFill(m_nvg);
  2110. // Center triangle stroke.
  2111. nvgBeginPath(m_nvg);
  2112. nvgMoveTo(m_nvg, aa[0], aa[1]);
  2113. nvgLineTo(m_nvg, bb[0], bb[1]);
  2114. nvgLineTo(m_nvg, cc[0], cc[1]);
  2115. nvgClosePath(m_nvg);
  2116. nvgStrokeColor(m_nvg, nvgRGBA(0,0,0,64) );
  2117. nvgStroke(m_nvg);
  2118. // Center triangle fill.
  2119. paint = nvgLinearGradient(m_nvg, aa[0], aa[1], bb[0], bb[1], nvgHSL(hsv[0],saturation,0.5f), nvgRGBA(0,0,0,alpha0) );
  2120. nvgFillPaint(m_nvg, paint);
  2121. nvgFill(m_nvg);
  2122. 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) );
  2123. nvgFillPaint(m_nvg, paint);
  2124. nvgFill(m_nvg);
  2125. // Color selector.
  2126. nvgStrokeWidth(m_nvg, 2.0f);
  2127. nvgBeginPath(m_nvg);
  2128. nvgCircle(m_nvg, sel[0], sel[1], 5);
  2129. nvgStrokeColor(m_nvg, nvgRGBA(255,255,255,alpha1) );
  2130. nvgStroke(m_nvg);
  2131. // Color selector stroke.
  2132. paint = nvgRadialGradient(m_nvg, sel[0], sel[1], 7.0f, 9.0f, nvgRGBA(0,0,0,64), nvgRGBA(0,0,0,0) );
  2133. nvgBeginPath(m_nvg);
  2134. nvgRect(m_nvg, sel[0]-20.0f, sel[1]-20.0f, 40.0f, 40.0f);
  2135. nvgCircle(m_nvg, sel[0], sel[1], 7.0f);
  2136. nvgPathWinding(m_nvg, NVG_HOLE);
  2137. nvgFillPaint(m_nvg, paint);
  2138. nvgFill(m_nvg);
  2139. }
  2140. nvgRestore(m_nvg);
  2141. }
  2142. nvgRestore(m_nvg);
  2143. }
  2144. int32_t m_mx;
  2145. int32_t m_my;
  2146. int32_t m_scroll;
  2147. uint32_t m_active;
  2148. uint32_t m_hot;
  2149. uint32_t m_hotToBe;
  2150. char m_char;
  2151. char m_lastChar;
  2152. uint32_t m_inputField;
  2153. int32_t m_dragX;
  2154. int32_t m_dragY;
  2155. float m_dragOrig;
  2156. int32_t m_widgetX;
  2157. int32_t m_widgetY;
  2158. int32_t m_widgetW;
  2159. bool m_left;
  2160. bool m_leftPressed;
  2161. bool m_leftReleased;
  2162. bool m_isHot;
  2163. bool m_isActive;
  2164. bool m_wentActive;
  2165. bool m_insideCurrentScroll;
  2166. uint8_t m_areaId;
  2167. uint8_t m_widgetId;
  2168. uint64_t m_enabledAreaIds;
  2169. uint16_t m_scissor;
  2170. float m_tempCoords[MAX_TEMP_COORDS * 2];
  2171. float m_tempNormals[MAX_TEMP_COORDS * 2];
  2172. float m_circleVerts[NUM_CIRCLE_VERTS * 2];
  2173. int32_t m_scrollTop;
  2174. int32_t m_scrollHeight;
  2175. int32_t m_scrollRight;
  2176. int32_t m_scrollAreaTop;
  2177. int32_t m_scrollAreaWidth;
  2178. int32_t m_scrollAreaInnerWidth;
  2179. int32_t m_scrollAreaX;
  2180. int32_t* m_scrollVal;
  2181. uint16_t m_scrollId;
  2182. bool m_insideScrollArea;
  2183. uint16_t m_textureWidth;
  2184. uint16_t m_textureHeight;
  2185. float m_invTextureWidth;
  2186. float m_invTextureHeight;
  2187. float m_halfTexel;
  2188. NVGcontext* m_nvg;
  2189. uint8_t m_view;
  2190. uint16_t m_viewWidth;
  2191. uint16_t m_viewHeight;
  2192. #if !USE_NANOVG_FONT
  2193. struct Font
  2194. {
  2195. stbtt_bakedchar m_cdata[96]; // ASCII 32..126 is 95 glyphs
  2196. bgfx::TextureHandle m_texture;
  2197. float m_size;
  2198. };
  2199. uint16_t m_currentFontIdx;
  2200. bx::HandleAllocT<IMGUI_CONFIG_MAX_FONTS> m_fontHandle;
  2201. Font m_fonts[IMGUI_CONFIG_MAX_FONTS];
  2202. #endif // !USE_NANOVG_FONT
  2203. bgfx::UniformHandle u_imageLod;
  2204. bgfx::UniformHandle u_imageSwizzle;
  2205. bgfx::UniformHandle s_texColor;
  2206. bgfx::ProgramHandle m_colorProgram;
  2207. bgfx::ProgramHandle m_textureProgram;
  2208. bgfx::ProgramHandle m_imageProgram;
  2209. bgfx::ProgramHandle m_imageSwizzProgram;
  2210. bgfx::TextureHandle m_missingTexture;
  2211. };
  2212. static Imgui s_imgui;
  2213. ImguiFontHandle imguiCreate(const void* _data, float _fontSize)
  2214. {
  2215. return s_imgui.create(_data, _fontSize);
  2216. }
  2217. void imguiSetFont(ImguiFontHandle _handle)
  2218. {
  2219. s_imgui.setFont(_handle);
  2220. }
  2221. ImguiFontHandle imguiCreateFont(const void* _data, float _fontSize)
  2222. {
  2223. return s_imgui.createFont(_data, _fontSize);
  2224. }
  2225. void imguiDestroy()
  2226. {
  2227. s_imgui.destroy();
  2228. }
  2229. 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)
  2230. {
  2231. s_imgui.beginFrame(_mx, _my, _button, _scroll, _width, _height, _inputChar, _view);
  2232. }
  2233. void imguiEndFrame()
  2234. {
  2235. s_imgui.endFrame();
  2236. }
  2237. bool imguiBorderButton(ImguiBorder::Enum _border, bool _checked, bool _enabled)
  2238. {
  2239. return s_imgui.borderButton(_border, _checked, _enabled);
  2240. }
  2241. bool imguiBeginScrollArea(const char* _name, int32_t _x, int32_t _y, int32_t _width, int32_t _height, int32_t* _scroll, bool _enabled, int32_t _r)
  2242. {
  2243. return s_imgui.beginScrollArea(_name, _x, _y, _width, _height, _scroll, _enabled, _r);
  2244. }
  2245. void imguiEndScrollArea(int32_t _r)
  2246. {
  2247. s_imgui.endScrollArea(_r);
  2248. }
  2249. void imguiIndent(uint16_t _width)
  2250. {
  2251. s_imgui.indent(_width);
  2252. }
  2253. void imguiUnindent(uint16_t _width)
  2254. {
  2255. s_imgui.unindent(_width);
  2256. }
  2257. void imguiSeparator(uint16_t _height)
  2258. {
  2259. s_imgui.separator(_height);
  2260. }
  2261. void imguiSeparatorLine(uint16_t _height)
  2262. {
  2263. s_imgui.separatorLine(_height);
  2264. }
  2265. int32_t imguiGetWidgetX()
  2266. {
  2267. return s_imgui.m_widgetX;
  2268. }
  2269. int32_t imguiGetWidgetY()
  2270. {
  2271. return s_imgui.m_widgetY;
  2272. }
  2273. bool imguiButton(const char* _text, bool _enabled, uint32_t _rgb0, int32_t _r)
  2274. {
  2275. return s_imgui.button(_text, _enabled, _rgb0, _r);
  2276. }
  2277. bool imguiItem(const char* _text, bool _enabled)
  2278. {
  2279. return s_imgui.item(_text, _enabled);
  2280. }
  2281. bool imguiCheck(const char* _text, bool _checked, bool _enabled)
  2282. {
  2283. return s_imgui.check(_text, _checked, _enabled);
  2284. }
  2285. bool imguiCollapse(const char* _text, const char* _subtext, bool _checked, bool _enabled)
  2286. {
  2287. return s_imgui.collapse(_text, _subtext, _checked, _enabled);
  2288. }
  2289. void imguiLabel(const char* _format, ...)
  2290. {
  2291. va_list argList;
  2292. va_start(argList, _format);
  2293. s_imgui.labelVargs(_format, argList, true);
  2294. va_end(argList);
  2295. }
  2296. void imguiLabel(bool _enabled, const char* _format, ...)
  2297. {
  2298. va_list argList;
  2299. va_start(argList, _format);
  2300. s_imgui.labelVargs(_format, argList, _enabled);
  2301. va_end(argList);
  2302. }
  2303. void imguiValue(const char* _text)
  2304. {
  2305. s_imgui.value(_text);
  2306. }
  2307. bool imguiSlider(const char* _text, float& _val, float _vmin, float _vmax, float _vinc, bool _enabled)
  2308. {
  2309. return s_imgui.slider(_text, _val, _vmin, _vmax, _vinc, _enabled);
  2310. }
  2311. bool imguiSlider(const char* _text, int32_t& _val, int32_t _vmin, int32_t _vmax, bool _enabled)
  2312. {
  2313. float val = (float)_val;
  2314. bool result = s_imgui.slider(_text, val, (float)_vmin, (float)_vmax, 1.0f, _enabled);
  2315. _val = (int32_t)val;
  2316. return result;
  2317. }
  2318. void imguiInput(const char* _label, char* _str, uint32_t _len, bool _enabled, int32_t _r)
  2319. {
  2320. s_imgui.input(_label, _str, _len, _enabled, _r);
  2321. }
  2322. uint8_t imguiTabsUseMacroInstead(uint8_t _selected, bool _enabled, ...)
  2323. {
  2324. va_list argList;
  2325. va_start(argList, _enabled);
  2326. const uint8_t result = s_imgui.tabs(_selected, _enabled, BUTTON_HEIGHT, BUTTON_HEIGHT/2 - 1, argList);
  2327. va_end(argList);
  2328. return result;
  2329. }
  2330. uint8_t imguiTabsUseMacroInstead(uint8_t _selected, bool _enabled, int32_t _height, int32_t _r, ...)
  2331. {
  2332. va_list argList;
  2333. va_start(argList, _r);
  2334. const uint8_t result = s_imgui.tabs(_selected, _enabled, _height, _r, argList);
  2335. va_end(argList);
  2336. return result;
  2337. }
  2338. uint32_t imguiChooseUseMacroInstead(uint32_t _selected, ...)
  2339. {
  2340. va_list argList;
  2341. va_start(argList, _selected);
  2342. const char* str = va_arg(argList, const char*);
  2343. for (uint32_t ii = 0; str != NULL; ++ii, str = va_arg(argList, const char*) )
  2344. {
  2345. if (imguiCheck(str, ii == _selected) )
  2346. {
  2347. _selected = ii;
  2348. }
  2349. }
  2350. va_end(argList);
  2351. return _selected;
  2352. }
  2353. void imguiDrawText(int32_t _x, int32_t _y, ImguiTextAlign::Enum _align, const char* _text, uint32_t _argb)
  2354. {
  2355. s_imgui.drawText(_x, _y, _align, _text, _argb);
  2356. }
  2357. void imguiDrawLine(float _x0, float _y0, float _x1, float _y1, float _r, uint32_t _argb)
  2358. {
  2359. s_imgui.drawLine(_x0, _y0, _x1, _y1, _r, _argb);
  2360. }
  2361. void imguiDrawRoundedRect(float _x, float _y, float _width, float _height, float _r, uint32_t _argb)
  2362. {
  2363. s_imgui.drawRoundedRect(_x, _y, _width, _height, _r, _argb);
  2364. }
  2365. void imguiDrawRect(float _x, float _y, float _width, float _height, uint32_t _argb)
  2366. {
  2367. s_imgui.drawRect(_x, _y, _width, _height, _argb);
  2368. }
  2369. void imguiBool(const char* _text, bool& _flag, bool _enabled)
  2370. {
  2371. if (imguiCheck(_text, _flag, _enabled) )
  2372. {
  2373. _flag = !_flag;
  2374. }
  2375. }
  2376. void imguiColorWheel(float _rgb[3], bool _respectIndentation, bool _enabled)
  2377. {
  2378. s_imgui.colorWheelWidget(_rgb, _respectIndentation, _enabled);
  2379. }
  2380. void imguiColorWheel(const char* _text, float _rgb[3], bool& _activated, bool _enabled)
  2381. {
  2382. char buf[128];
  2383. bx::snprintf(buf, sizeof(buf), "[RGB %-2.2f %-2.2f %-2.2f]"
  2384. , _rgb[0]
  2385. , _rgb[1]
  2386. , _rgb[2]
  2387. );
  2388. if (imguiCollapse(_text, buf, _activated, _enabled) )
  2389. {
  2390. _activated = !_activated;
  2391. }
  2392. if (_activated)
  2393. {
  2394. imguiColorWheel(_rgb, false, _enabled);
  2395. }
  2396. }
  2397. void imguiImage(bgfx::TextureHandle _image, float _lod, int32_t _width, int32_t _height, ImguiImageAlign::Enum _align)
  2398. {
  2399. s_imgui.image(_image, _lod, _width, _height, _align);
  2400. }
  2401. void imguiImage(bgfx::TextureHandle _image, float _lod, float _width, float _aspect, ImguiImageAlign::Enum _align)
  2402. {
  2403. s_imgui.image(_image, _lod, _width, _aspect, _align);
  2404. }
  2405. void imguiImageChannel(bgfx::TextureHandle _image, uint8_t _channel, float _lod, int32_t _width, int32_t _height, ImguiImageAlign::Enum _align)
  2406. {
  2407. s_imgui.imageChannel(_image, _channel, _lod, _width, _height, _align);
  2408. }
  2409. void imguiImageChannel(bgfx::TextureHandle _image, uint8_t _channel, float _lod, float _width, float _aspect, ImguiImageAlign::Enum _align)
  2410. {
  2411. s_imgui.imageChannel(_image, _channel, _lod, _width, _aspect, _align);
  2412. }