imgui.cpp 63 KB

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