nanovg.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  1. /*
  2. * Copyright 2011-2026 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. //
  6. // Copyright (c) 2013 Mikko Mononen [email protected]
  7. //
  8. // This software is provided 'as-is', without any express or implied
  9. // warranty. In no event will the authors be held liable for any damages
  10. // arising from the use of this software.
  11. // Permission is granted to anyone to use this software for any purpose,
  12. // including commercial applications, and to alter it and redistribute it
  13. // freely, subject to the following restrictions:
  14. // 1. The origin of this software must not be misrepresented; you must not
  15. // claim that you wrote the original software. If you use this software
  16. // in a product, an acknowledgment in the product documentation would be
  17. // appreciated but is not required.
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. // 3. This notice may not be removed or altered from any source distribution.
  21. //
  22. #include "common.h"
  23. #include "bgfx_utils.h"
  24. #include <bx/string.h>
  25. #include <bx/timer.h>
  26. #include <bimg/decode.h>
  27. #include "entry/entry.h"
  28. #include "imgui/imgui.h"
  29. #include "nanovg/nanovg.h"
  30. BX_PRAGMA_DIAGNOSTIC_PUSH();
  31. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wunused-parameter");
  32. #define BLENDISH_IMPLEMENTATION
  33. #include "blendish.h"
  34. BX_PRAGMA_DIAGNOSTIC_POP();
  35. namespace
  36. {
  37. #define ICON_SEARCH 0x1F50D
  38. #define ICON_CIRCLED_CROSS 0x2716
  39. #define ICON_CHEVRON_RIGHT 0xE75E
  40. #define ICON_CHECK 0x2713
  41. #define ICON_LOGIN 0xE740
  42. #define ICON_TRASH 0xE729
  43. // Returns 1 if col.rgba is 0.0f,0.0f,0.0f,0.0f, 0 otherwise
  44. int isBlack( struct NVGcolor col )
  45. {
  46. if( col.r == 0.0f && col.g == 0.0f && col.b == 0.0f && col.a == 0.0f )
  47. {
  48. return 1;
  49. }
  50. return 0;
  51. }
  52. static char* cpToUTF8(int cp, char* str)
  53. {
  54. int n = 0;
  55. if (cp < 0x80) n = 1;
  56. else if (cp < 0x800) n = 2;
  57. else if (cp < 0x10000) n = 3;
  58. else if (cp < 0x200000) n = 4;
  59. else if (cp < 0x4000000) n = 5;
  60. else if (cp <= 0x7fffffff) n = 6;
  61. str[n] = '\0';
  62. switch (n)
  63. {
  64. case 6: str[5] = 0x80 | (cp & 0x3f); cp = cp >> 6; cp |= 0x4000000; [[fallthrough]];
  65. case 5: str[4] = 0x80 | (cp & 0x3f); cp = cp >> 6; cp |= 0x200000; [[fallthrough]];
  66. case 4: str[3] = 0x80 | (cp & 0x3f); cp = cp >> 6; cp |= 0x10000; [[fallthrough]];
  67. case 3: str[2] = 0x80 | (cp & 0x3f); cp = cp >> 6; cp |= 0x800; [[fallthrough]];
  68. case 2: str[1] = 0x80 | (cp & 0x3f); cp = cp >> 6; cp |= 0xc0; [[fallthrough]];
  69. case 1: str[0] = char(cp); break;
  70. }
  71. return str;
  72. }
  73. void drawWindow(struct NVGcontext* vg, const char* title, float x, float y, float w, float h)
  74. {
  75. float cornerRadius = 3.0f;
  76. struct NVGpaint shadowPaint;
  77. struct NVGpaint headerPaint;
  78. nvgSave(vg);
  79. // nvgClearState(vg);
  80. // Window
  81. nvgBeginPath(vg);
  82. nvgRoundedRect(vg, x,y, w,h, cornerRadius);
  83. nvgFillColor(vg, nvgRGBA(28,30,34,192) );
  84. // nvgFillColor(vg, nvgRGBA(0,0,0,128) );
  85. nvgFill(vg);
  86. // Drop shadow
  87. shadowPaint = nvgBoxGradient(vg, x,y+2, w,h, cornerRadius*2, 10, nvgRGBA(0,0,0,128), nvgRGBA(0,0,0,0) );
  88. nvgBeginPath(vg);
  89. nvgRect(vg, x-10,y-10, w+20,h+30);
  90. nvgRoundedRect(vg, x,y, w,h, cornerRadius);
  91. nvgPathWinding(vg, NVG_HOLE);
  92. nvgFillPaint(vg, shadowPaint);
  93. nvgFill(vg);
  94. // Header
  95. headerPaint = nvgLinearGradient(vg, x,y,x,y+15, nvgRGBA(255,255,255,8), nvgRGBA(0,0,0,16) );
  96. nvgBeginPath(vg);
  97. nvgRoundedRect(vg, x+1,y+1, w-2,30, cornerRadius-1);
  98. nvgFillPaint(vg, headerPaint);
  99. nvgFill(vg);
  100. nvgBeginPath(vg);
  101. nvgMoveTo(vg, x+0.5f, y+0.5f+30);
  102. nvgLineTo(vg, x+0.5f+w-1, y+0.5f+30);
  103. nvgStrokeColor(vg, nvgRGBA(0,0,0,32) );
  104. nvgStroke(vg);
  105. nvgFontSize(vg, 18.0f);
  106. nvgFontFace(vg, "sans-bold");
  107. nvgTextAlign(vg,NVG_ALIGN_CENTER|NVG_ALIGN_MIDDLE);
  108. nvgFontBlur(vg,2);
  109. nvgFillColor(vg, nvgRGBA(0,0,0,128) );
  110. nvgText(vg, x+w/2,y+16+1, title, NULL);
  111. nvgFontBlur(vg,0);
  112. nvgFillColor(vg, nvgRGBA(220,220,220,160) );
  113. nvgText(vg, x+w/2,y+16, title, NULL);
  114. nvgRestore(vg);
  115. }
  116. void drawSearchBox(struct NVGcontext* vg, const char* text, float x, float y, float w, float h)
  117. {
  118. struct NVGpaint bg;
  119. char icon[8];
  120. float cornerRadius = h/2-1;
  121. // Edit
  122. bg = nvgBoxGradient(vg, x,y+1.5f, w,h, h/2,5, nvgRGBA(0,0,0,16), nvgRGBA(0,0,0,92) );
  123. nvgBeginPath(vg);
  124. nvgRoundedRect(vg, x,y, w,h, cornerRadius);
  125. nvgFillPaint(vg, bg);
  126. nvgFill(vg);
  127. /* nvgBeginPath(vg);
  128. nvgRoundedRect(vg, x+0.5f,y+0.5f, w-1,h-1, cornerRadius-0.5f);
  129. nvgStrokeColor(vg, nvgRGBA(0,0,0,48) );
  130. nvgStroke(vg);*/
  131. nvgFontSize(vg, h*1.3f);
  132. nvgFontFace(vg, "icons");
  133. nvgFillColor(vg, nvgRGBA(255,255,255,64) );
  134. nvgTextAlign(vg,NVG_ALIGN_CENTER|NVG_ALIGN_MIDDLE);
  135. nvgText(vg, x+h*0.55f, y+h*0.55f, cpToUTF8(ICON_SEARCH,icon), NULL);
  136. nvgFontSize(vg, 20.0f);
  137. nvgFontFace(vg, "sans");
  138. nvgFillColor(vg, nvgRGBA(255,255,255,32) );
  139. nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
  140. nvgText(vg, x+h*1.05f,y+h*0.5f,text, NULL);
  141. nvgFontSize(vg, h*1.3f);
  142. nvgFontFace(vg, "icons");
  143. nvgFillColor(vg, nvgRGBA(255,255,255,32) );
  144. nvgTextAlign(vg,NVG_ALIGN_CENTER|NVG_ALIGN_MIDDLE);
  145. nvgText(vg, x+w-h*0.55f, y+h*0.55f, cpToUTF8(ICON_CIRCLED_CROSS,icon), NULL);
  146. }
  147. void drawDropDown(struct NVGcontext* vg, const char* text, float x, float y, float w, float h)
  148. {
  149. struct NVGpaint bg;
  150. char icon[8];
  151. float cornerRadius = 4.0f;
  152. bg = nvgLinearGradient(vg, x,y,x,y+h, nvgRGBA(255,255,255,16), nvgRGBA(0,0,0,16) );
  153. nvgBeginPath(vg);
  154. nvgRoundedRect(vg, x+1,y+1, w-2,h-2, cornerRadius-1);
  155. nvgFillPaint(vg, bg);
  156. nvgFill(vg);
  157. nvgBeginPath(vg);
  158. nvgRoundedRect(vg, x+0.5f,y+0.5f, w-1,h-1, cornerRadius-0.5f);
  159. nvgStrokeColor(vg, nvgRGBA(0,0,0,48) );
  160. nvgStroke(vg);
  161. nvgFontSize(vg, 20.0f);
  162. nvgFontFace(vg, "sans");
  163. nvgFillColor(vg, nvgRGBA(255,255,255,160) );
  164. nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
  165. nvgText(vg, x+h*0.3f,y+h*0.5f,text, NULL);
  166. nvgFontSize(vg, h*1.3f);
  167. nvgFontFace(vg, "icons");
  168. nvgFillColor(vg, nvgRGBA(255,255,255,64) );
  169. nvgTextAlign(vg,NVG_ALIGN_CENTER|NVG_ALIGN_MIDDLE);
  170. nvgText(vg, x+w-h*0.5f, y+h*0.5f, cpToUTF8(ICON_CHEVRON_RIGHT,icon), NULL);
  171. }
  172. void drawLabel(struct NVGcontext* vg, const char* text, float x, float y, float w, float h)
  173. {
  174. NVG_NOTUSED(w);
  175. nvgFontSize(vg, 18.0f);
  176. nvgFontFace(vg, "sans");
  177. nvgFillColor(vg, nvgRGBA(255,255,255,128) );
  178. nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
  179. nvgText(vg, x,y+h*0.5f,text, NULL);
  180. }
  181. void drawEditBoxBase(struct NVGcontext* vg, float x, float y, float w, float h)
  182. {
  183. struct NVGpaint bg;
  184. // Edit
  185. bg = nvgBoxGradient(vg, x+1,y+1+1.5f, w-2,h-2, 3,4, nvgRGBA(255,255,255,32), nvgRGBA(32,32,32,32) );
  186. nvgBeginPath(vg);
  187. nvgRoundedRect(vg, x+1,y+1, w-2,h-2, 4-1);
  188. nvgFillPaint(vg, bg);
  189. nvgFill(vg);
  190. nvgBeginPath(vg);
  191. nvgRoundedRect(vg, x+0.5f,y+0.5f, w-1,h-1, 4-0.5f);
  192. nvgStrokeColor(vg, nvgRGBA(0,0,0,48) );
  193. nvgStroke(vg);
  194. }
  195. void drawEditBox(struct NVGcontext* vg, const char* text, float x, float y, float w, float h)
  196. {
  197. drawEditBoxBase(vg, x,y, w,h);
  198. nvgFontSize(vg, 20.0f);
  199. nvgFontFace(vg, "sans");
  200. nvgFillColor(vg, nvgRGBA(255,255,255,64) );
  201. nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
  202. nvgText(vg, x+h*0.3f,y+h*0.5f,text, NULL);
  203. }
  204. void drawEditBoxNum(struct NVGcontext* vg,
  205. const char* text, const char* units, float x, float y, float w, float h)
  206. {
  207. float uw;
  208. drawEditBoxBase(vg, x,y, w,h);
  209. uw = nvgTextBounds(vg, 0,0, units, NULL, NULL);
  210. nvgFontSize(vg, 18.0f);
  211. nvgFontFace(vg, "sans");
  212. nvgFillColor(vg, nvgRGBA(255,255,255,64) );
  213. nvgTextAlign(vg,NVG_ALIGN_RIGHT|NVG_ALIGN_MIDDLE);
  214. nvgText(vg, x+w-h*0.3f,y+h*0.5f,units, NULL);
  215. nvgFontSize(vg, 20.0f);
  216. nvgFontFace(vg, "sans");
  217. nvgFillColor(vg, nvgRGBA(255,255,255,128) );
  218. nvgTextAlign(vg,NVG_ALIGN_RIGHT|NVG_ALIGN_MIDDLE);
  219. nvgText(vg, x+w-uw-h*0.5f,y+h*0.5f,text, NULL);
  220. }
  221. void drawCheckBox(struct NVGcontext* vg, const char* text, float x, float y, float w, float h)
  222. {
  223. struct NVGpaint bg;
  224. char icon[8];
  225. NVG_NOTUSED(w);
  226. nvgFontSize(vg, 18.0f);
  227. nvgFontFace(vg, "sans");
  228. nvgFillColor(vg, nvgRGBA(255,255,255,160) );
  229. nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
  230. nvgText(vg, x+28,y+h*0.5f,text, NULL);
  231. bg = nvgBoxGradient(vg, x+1,y+(int)(h*0.5f)-9+1, 18,18, 3,3, nvgRGBA(0,0,0,32), nvgRGBA(0,0,0,92) );
  232. nvgBeginPath(vg);
  233. nvgRoundedRect(vg, x+1,y+(int)(h*0.5f)-9, 18,18, 3);
  234. nvgFillPaint(vg, bg);
  235. nvgFill(vg);
  236. nvgFontSize(vg, 40);
  237. nvgFontFace(vg, "icons");
  238. nvgFillColor(vg, nvgRGBA(255,255,255,128) );
  239. nvgTextAlign(vg,NVG_ALIGN_CENTER|NVG_ALIGN_MIDDLE);
  240. nvgText(vg, x+9+2, y+h*0.5f, cpToUTF8(ICON_CHECK,icon), NULL);
  241. }
  242. void drawButton(struct NVGcontext* vg, int preicon, const char* text, float x, float y, float w, float h, struct NVGcolor col)
  243. {
  244. struct NVGpaint bg;
  245. char icon[8];
  246. float cornerRadius = 4.0f;
  247. float tw = 0, iw = 0;
  248. bg = nvgLinearGradient(vg, x,y,x,y+h, nvgRGBA(255,255,255,isBlack(col)?16:32), nvgRGBA(0,0,0,isBlack(col)?16:32) );
  249. nvgBeginPath(vg);
  250. nvgRoundedRect(vg, x+1,y+1, w-2,h-2, cornerRadius-1);
  251. if (!isBlack(col) ) {
  252. nvgFillColor(vg, col);
  253. nvgFill(vg);
  254. }
  255. nvgFillPaint(vg, bg);
  256. nvgFill(vg);
  257. nvgBeginPath(vg);
  258. nvgRoundedRect(vg, x+0.5f,y+0.5f, w-1,h-1, cornerRadius-0.5f);
  259. nvgStrokeColor(vg, nvgRGBA(0,0,0,48) );
  260. nvgStroke(vg);
  261. nvgFontSize(vg, 20.0f);
  262. nvgFontFace(vg, "sans-bold");
  263. tw = nvgTextBounds(vg, 0,0, text, NULL, NULL);
  264. if (preicon != 0) {
  265. nvgFontSize(vg, h*1.3f);
  266. nvgFontFace(vg, "icons");
  267. iw = nvgTextBounds(vg, 0,0, cpToUTF8(preicon,icon), NULL, NULL);
  268. iw += h*0.15f;
  269. }
  270. if (preicon != 0) {
  271. nvgFontSize(vg, h*1.3f);
  272. nvgFontFace(vg, "icons");
  273. nvgFillColor(vg, nvgRGBA(255,255,255,96) );
  274. nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
  275. nvgText(vg, x+w*0.5f-tw*0.5f-iw*0.75f, y+h*0.5f, cpToUTF8(preicon,icon), NULL);
  276. }
  277. nvgFontSize(vg, 20.0f);
  278. nvgFontFace(vg, "sans-bold");
  279. nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
  280. nvgFillColor(vg, nvgRGBA(0,0,0,160) );
  281. nvgText(vg, x+w*0.5f-tw*0.5f+iw*0.25f,y+h*0.5f-1,text, NULL);
  282. nvgFillColor(vg, nvgRGBA(255,255,255,160) );
  283. nvgText(vg, x+w*0.5f-tw*0.5f+iw*0.25f,y+h*0.5f,text, NULL);
  284. }
  285. void drawSlider(struct NVGcontext* vg, float pos, float x, float y, float w, float h)
  286. {
  287. struct NVGpaint bg, knob;
  288. float cy = y+(int)(h*0.5f);
  289. float kr = (float)( (int)(h*0.25f) );
  290. nvgSave(vg);
  291. // nvgClearState(vg);
  292. // Slot
  293. bg = nvgBoxGradient(vg, x,cy-2+1, w,4, 2,2, nvgRGBA(0,0,0,32), nvgRGBA(0,0,0,128) );
  294. nvgBeginPath(vg);
  295. nvgRoundedRect(vg, x,cy-2, w,4, 2);
  296. nvgFillPaint(vg, bg);
  297. nvgFill(vg);
  298. // Knob Shadow
  299. bg = nvgRadialGradient(vg, x+(int)(pos*w),cy+1, kr-3,kr+3, nvgRGBA(0,0,0,64), nvgRGBA(0,0,0,0) );
  300. nvgBeginPath(vg);
  301. nvgRect(vg, x+(int)(pos*w)-kr-5,cy-kr-5,kr*2+5+5,kr*2+5+5+3);
  302. nvgCircle(vg, x+(int)(pos*w),cy, kr);
  303. nvgPathWinding(vg, NVG_HOLE);
  304. nvgFillPaint(vg, bg);
  305. nvgFill(vg);
  306. // Knob
  307. knob = nvgLinearGradient(vg, x,cy-kr,x,cy+kr, nvgRGBA(255,255,255,16), nvgRGBA(0,0,0,16) );
  308. nvgBeginPath(vg);
  309. nvgCircle(vg, x+(int)(pos*w),cy, kr-1);
  310. nvgFillColor(vg, nvgRGBA(40,43,48,255) );
  311. nvgFill(vg);
  312. nvgFillPaint(vg, knob);
  313. nvgFill(vg);
  314. nvgBeginPath(vg);
  315. nvgCircle(vg, x+(int)(pos*w),cy, kr-0.5f);
  316. nvgStrokeColor(vg, nvgRGBA(0,0,0,92) );
  317. nvgStroke(vg);
  318. nvgRestore(vg);
  319. }
  320. void drawEyes(struct NVGcontext* vg, float x, float y, float w, float h, float mx, float my, float t)
  321. {
  322. struct NVGpaint gloss, bg;
  323. float ex = w *0.23f;
  324. float ey = h * 0.5f;
  325. float lx = x + ex;
  326. float ly = y + ey;
  327. float rx = x + w - ex;
  328. float ry = y + ey;
  329. float dx,dy,d;
  330. float br = (ex < ey ? ex : ey) * 0.5f;
  331. float blink = 1 - powf(sinf(t*0.5f),200)*0.8f;
  332. bg = nvgLinearGradient(vg, x,y+h*0.5f,x+w*0.1f,y+h, nvgRGBA(0,0,0,32), nvgRGBA(0,0,0,16) );
  333. nvgBeginPath(vg);
  334. nvgEllipse(vg, lx+3.0f,ly+16.0f, ex,ey);
  335. nvgEllipse(vg, rx+3.0f,ry+16.0f, ex,ey);
  336. nvgFillPaint(vg, bg);
  337. nvgFill(vg);
  338. bg = nvgLinearGradient(vg, x,y+h*0.25f,x+w*0.1f,y+h, nvgRGBA(220,220,220,255), nvgRGBA(128,128,128,255) );
  339. nvgBeginPath(vg);
  340. nvgEllipse(vg, lx,ly, ex,ey);
  341. nvgEllipse(vg, rx,ry, ex,ey);
  342. nvgFillPaint(vg, bg);
  343. nvgFill(vg);
  344. dx = (mx - rx) / (ex * 10);
  345. dy = (my - ry) / (ey * 10);
  346. d = sqrtf(dx*dx+dy*dy);
  347. if (d > 1.0f) {
  348. dx /= d; dy /= d;
  349. }
  350. dx *= ex*0.4f;
  351. dy *= ey*0.5f;
  352. nvgBeginPath(vg);
  353. nvgEllipse(vg, lx+dx,ly+dy+ey*0.25f*(1-blink), br,br*blink);
  354. nvgFillColor(vg, nvgRGBA(32,32,32,255) );
  355. nvgFill(vg);
  356. dx = (mx - rx) / (ex * 10);
  357. dy = (my - ry) / (ey * 10);
  358. d = sqrtf(dx*dx+dy*dy);
  359. if (d > 1.0f) {
  360. dx /= d; dy /= d;
  361. }
  362. dx *= ex*0.4f;
  363. dy *= ey*0.5f;
  364. nvgBeginPath(vg);
  365. nvgEllipse(vg, rx+dx,ry+dy+ey*0.25f*(1-blink), br,br*blink);
  366. nvgFillColor(vg, nvgRGBA(32,32,32,255) );
  367. nvgFill(vg);
  368. gloss = nvgRadialGradient(vg, lx-ex*0.25f,ly-ey*0.5f, ex*0.1f,ex*0.75f, nvgRGBA(255,255,255,128), nvgRGBA(255,255,255,0) );
  369. nvgBeginPath(vg);
  370. nvgEllipse(vg, lx,ly, ex,ey);
  371. nvgFillPaint(vg, gloss);
  372. nvgFill(vg);
  373. gloss = nvgRadialGradient(vg, rx-ex*0.25f,ry-ey*0.5f, ex*0.1f,ex*0.75f, nvgRGBA(255,255,255,128), nvgRGBA(255,255,255,0) );
  374. nvgBeginPath(vg);
  375. nvgEllipse(vg, rx,ry, ex,ey);
  376. nvgFillPaint(vg, gloss);
  377. nvgFill(vg);
  378. }
  379. void drawGraph(struct NVGcontext* vg, float x, float y, float w, float h, float t)
  380. {
  381. struct NVGpaint bg;
  382. float samples[6];
  383. float sx[6], sy[6];
  384. float dx = w/5.0f;
  385. int i;
  386. samples[0] = (1+sinf(t*1.2345f+cosf(t*0.33457f)*0.44f) )*0.5f;
  387. samples[1] = (1+sinf(t*0.68363f+cosf(t*1.3f)*1.55f) )*0.5f;
  388. samples[2] = (1+sinf(t*1.1642f+cosf(t*0.33457f)*1.24f) )*0.5f;
  389. samples[3] = (1+sinf(t*0.56345f+cosf(t*1.63f)*0.14f) )*0.5f;
  390. samples[4] = (1+sinf(t*1.6245f+cosf(t*0.254f)*0.3f) )*0.5f;
  391. samples[5] = (1+sinf(t*0.345f+cosf(t*0.03f)*0.6f) )*0.5f;
  392. for (i = 0; i < 6; i++) {
  393. sx[i] = x+i*dx;
  394. sy[i] = y+h*samples[i]*0.8f;
  395. }
  396. // Graph background
  397. bg = nvgLinearGradient(vg, x,y,x,y+h, nvgRGBA(0,160,192,0), nvgRGBA(0,160,192,64) );
  398. nvgBeginPath(vg);
  399. nvgMoveTo(vg, sx[0], sy[0]);
  400. for (i = 1; i < 6; i++)
  401. nvgBezierTo(vg, sx[i-1]+dx*0.5f,sy[i-1], sx[i]-dx*0.5f,sy[i], sx[i],sy[i]);
  402. nvgLineTo(vg, x+w, y+h);
  403. nvgLineTo(vg, x, y+h);
  404. nvgFillPaint(vg, bg);
  405. nvgFill(vg);
  406. // Graph line
  407. nvgBeginPath(vg);
  408. nvgMoveTo(vg, sx[0], sy[0]+2);
  409. for (i = 1; i < 6; i++)
  410. nvgBezierTo(vg, sx[i-1]+dx*0.5f,sy[i-1]+2, sx[i]-dx*0.5f,sy[i]+2, sx[i],sy[i]+2);
  411. nvgStrokeColor(vg, nvgRGBA(0,0,0,32) );
  412. nvgStrokeWidth(vg, 3.0f);
  413. nvgStroke(vg);
  414. nvgBeginPath(vg);
  415. nvgMoveTo(vg, sx[0], sy[0]);
  416. for (i = 1; i < 6; i++)
  417. nvgBezierTo(vg, sx[i-1]+dx*0.5f,sy[i-1], sx[i]-dx*0.5f,sy[i], sx[i],sy[i]);
  418. nvgStrokeColor(vg, nvgRGBA(0,160,192,255) );
  419. nvgStrokeWidth(vg, 3.0f);
  420. nvgStroke(vg);
  421. // Graph sample pos
  422. for (i = 0; i < 6; i++) {
  423. bg = nvgRadialGradient(vg, sx[i],sy[i]+2, 3.0f,8.0f, nvgRGBA(0,0,0,32), nvgRGBA(0,0,0,0) );
  424. nvgBeginPath(vg);
  425. nvgRect(vg, sx[i]-10, sy[i]-10+2, 20,20);
  426. nvgFillPaint(vg, bg);
  427. nvgFill(vg);
  428. }
  429. nvgBeginPath(vg);
  430. for (i = 0; i < 6; i++)
  431. nvgCircle(vg, sx[i], sy[i], 4.0f);
  432. nvgFillColor(vg, nvgRGBA(0,160,192,255) );
  433. nvgFill(vg);
  434. nvgBeginPath(vg);
  435. for (i = 0; i < 6; i++)
  436. nvgCircle(vg, sx[i], sy[i], 2.0f);
  437. nvgFillColor(vg, nvgRGBA(220,220,220,255) );
  438. nvgFill(vg);
  439. nvgStrokeWidth(vg, 1.0f);
  440. }
  441. void drawSpinner(struct NVGcontext* vg, float cx, float cy, float r, float t)
  442. {
  443. float a0 = 0.0f + t*6;
  444. float a1 = NVG_PI + t*6;
  445. float r0 = r;
  446. float r1 = r * 0.75f;
  447. float ax,ay, bx,by;
  448. struct NVGpaint paint;
  449. nvgSave(vg);
  450. nvgBeginPath(vg);
  451. nvgArc(vg, cx,cy, r0, a0, a1, NVG_CW);
  452. nvgArc(vg, cx,cy, r1, a1, a0, NVG_CCW);
  453. nvgClosePath(vg);
  454. ax = cx + cosf(a0) * (r0+r1)*0.5f;
  455. ay = cy + sinf(a0) * (r0+r1)*0.5f;
  456. bx = cx + cosf(a1) * (r0+r1)*0.5f;
  457. by = cy + sinf(a1) * (r0+r1)*0.5f;
  458. paint = nvgLinearGradient(vg, ax,ay, bx,by, nvgRGBA(0,0,0,0), nvgRGBA(0,0,0,128) );
  459. nvgFillPaint(vg, paint);
  460. nvgFill(vg);
  461. nvgRestore(vg);
  462. }
  463. void drawThumbnails(struct NVGcontext* vg, float x, float y, float w, float h, const int* images, int nimages, float t)
  464. {
  465. float cornerRadius = 3.0f;
  466. struct NVGpaint shadowPaint, imgPaint, fadePaint;
  467. float ix,iy,iw,ih;
  468. float thumb = 60.0f;
  469. float arry = 30.5f;
  470. int imgw, imgh;
  471. float stackh = (nimages/2) * (thumb+10) + 10;
  472. int i;
  473. float u = (1+cosf(t*0.5f) )*0.5f;
  474. float u2 = (1-cosf(t*0.2f) )*0.5f;
  475. float scrollh, dv;
  476. nvgSave(vg);
  477. // nvgClearState(vg);
  478. // Drop shadow
  479. shadowPaint = nvgBoxGradient(vg, x,y+4, w,h, cornerRadius*2, 20, nvgRGBA(0,0,0,128), nvgRGBA(0,0,0,0) );
  480. nvgBeginPath(vg);
  481. nvgRect(vg, x-10,y-10, w+20,h+30);
  482. nvgRoundedRect(vg, x,y, w,h, cornerRadius);
  483. nvgPathWinding(vg, NVG_HOLE);
  484. nvgFillPaint(vg, shadowPaint);
  485. nvgFill(vg);
  486. // Window
  487. nvgBeginPath(vg);
  488. nvgRoundedRect(vg, x,y, w,h, cornerRadius);
  489. nvgMoveTo(vg, x-10,y+arry);
  490. nvgLineTo(vg, x+1,y+arry-11);
  491. nvgLineTo(vg, x+1,y+arry+11);
  492. nvgFillColor(vg, nvgRGBA(200,200,200,255) );
  493. nvgFill(vg);
  494. nvgSave(vg);
  495. nvgScissor(vg, x,y,w,h);
  496. nvgTranslate(vg, 0, -(stackh - h)*u);
  497. dv = 1.0f / (float)(nimages-1);
  498. for (i = 0; i < nimages; i++) {
  499. float tx, ty, v, a;
  500. tx = x+10;
  501. ty = y+10;
  502. tx += (i%2) * (thumb+10);
  503. ty += (i/2) * (thumb+10);
  504. nvgImageSize(vg, images[i], &imgw, &imgh);
  505. if (imgw < imgh) {
  506. iw = thumb;
  507. ih = iw * (float)imgh/(float)imgw;
  508. ix = 0;
  509. iy = -(ih-thumb)*0.5f;
  510. } else {
  511. ih = thumb;
  512. iw = ih * (float)imgw/(float)imgh;
  513. ix = -(iw-thumb)*0.5f;
  514. iy = 0;
  515. }
  516. v = i * dv;
  517. a = bx::clamp((u2-v) / dv, 0.0f, 1.0f);
  518. if (a < 1.0f)
  519. drawSpinner(vg, tx+thumb/2,ty+thumb/2, thumb*0.25f, t);
  520. imgPaint = nvgImagePattern(vg, tx+ix, ty+iy, iw,ih, 0.0f/180.0f*NVG_PI, images[i], a);
  521. nvgBeginPath(vg);
  522. nvgRoundedRect(vg, tx,ty, thumb,thumb, 5);
  523. nvgFillPaint(vg, imgPaint);
  524. nvgFill(vg);
  525. shadowPaint = nvgBoxGradient(vg, tx-1,ty, thumb+2,thumb+2, 5, 3, nvgRGBA(0,0,0,128), nvgRGBA(0,0,0,0) );
  526. nvgBeginPath(vg);
  527. nvgRect(vg, tx-5,ty-5, thumb+10,thumb+10);
  528. nvgRoundedRect(vg, tx,ty, thumb,thumb, 6);
  529. nvgPathWinding(vg, NVG_HOLE);
  530. nvgFillPaint(vg, shadowPaint);
  531. nvgFill(vg);
  532. nvgBeginPath(vg);
  533. nvgRoundedRect(vg, tx+0.5f,ty+0.5f, thumb-1,thumb-1, 4-0.5f);
  534. nvgStrokeWidth(vg,1.0f);
  535. nvgStrokeColor(vg, nvgRGBA(255,255,255,192) );
  536. nvgStroke(vg);
  537. }
  538. nvgRestore(vg);
  539. // Hide fades
  540. fadePaint = nvgLinearGradient(vg, x,y,x,y+6, nvgRGBA(200,200,200,255), nvgRGBA(200,200,200,0) );
  541. nvgBeginPath(vg);
  542. nvgRect(vg, x+4,y,w-8,6);
  543. nvgFillPaint(vg, fadePaint);
  544. nvgFill(vg);
  545. fadePaint = nvgLinearGradient(vg, x,y+h,x,y+h-6, nvgRGBA(200,200,200,255), nvgRGBA(200,200,200,0) );
  546. nvgBeginPath(vg);
  547. nvgRect(vg, x+4,y+h-6,w-8,6);
  548. nvgFillPaint(vg, fadePaint);
  549. nvgFill(vg);
  550. // Scroll bar
  551. shadowPaint = nvgBoxGradient(vg, x+w-12+1,y+4+1, 8,h-8, 3,4, nvgRGBA(0,0,0,32), nvgRGBA(0,0,0,92) );
  552. nvgBeginPath(vg);
  553. nvgRoundedRect(vg, x+w-12,y+4, 8,h-8, 3);
  554. nvgFillPaint(vg, shadowPaint);
  555. // nvgFillColor(vg, nvgRGBA(255,0,0,128) );
  556. nvgFill(vg);
  557. scrollh = (h/stackh) * (h-8);
  558. shadowPaint = nvgBoxGradient(vg, x+w-12-1,y+4+(h-8-scrollh)*u-1, 8,scrollh, 3,4, nvgRGBA(220,220,220,255), nvgRGBA(128,128,128,255) );
  559. nvgBeginPath(vg);
  560. nvgRoundedRect(vg, x+w-12+1,y+4+1 + (h-8-scrollh)*u, 8-2,scrollh-2, 2);
  561. nvgFillPaint(vg, shadowPaint);
  562. // nvgFillColor(vg, nvgRGBA(0,0,0,128) );
  563. nvgFill(vg);
  564. nvgRestore(vg);
  565. }
  566. void drawColorwheel(struct NVGcontext* vg, float x, float y, float w, float h, float t)
  567. {
  568. int i;
  569. float r0, r1, ax,ay, bx,by, cx,cy, aeps, r;
  570. float hue = sinf(t * 0.12f);
  571. struct NVGpaint paint;
  572. nvgSave(vg);
  573. /* nvgBeginPath(vg);
  574. nvgRect(vg, x,y,w,h);
  575. nvgFillColor(vg, nvgRGBA(255,0,0,128) );
  576. nvgFill(vg);*/
  577. cx = x + w*0.5f;
  578. cy = y + h*0.5f;
  579. r1 = (w < h ? w : h) * 0.5f - 5.0f;
  580. r0 = r1 - 20.0f;
  581. aeps = 0.5f / r1; // half a pixel arc length in radians (2pi cancels out).
  582. for (i = 0; i < 6; i++)
  583. {
  584. float a0 = (float)i / 6.0f * NVG_PI * 2.0f - aeps;
  585. float a1 = (float)(i+1.0f) / 6.0f * NVG_PI * 2.0f + aeps;
  586. nvgBeginPath(vg);
  587. nvgArc(vg, cx,cy, r0, a0, a1, NVG_CW);
  588. nvgArc(vg, cx,cy, r1, a1, a0, NVG_CCW);
  589. nvgClosePath(vg);
  590. ax = cx + cosf(a0) * (r0+r1)*0.5f;
  591. ay = cy + sinf(a0) * (r0+r1)*0.5f;
  592. bx = cx + cosf(a1) * (r0+r1)*0.5f;
  593. by = cy + sinf(a1) * (r0+r1)*0.5f;
  594. paint = nvgLinearGradient(vg, ax,ay, bx,by, nvgHSLA(a0/(NVG_PI*2),1.0f,0.55f,255), nvgHSLA(a1/(NVG_PI*2),1.0f,0.55f,255) );
  595. nvgFillPaint(vg, paint);
  596. nvgFill(vg);
  597. }
  598. nvgBeginPath(vg);
  599. nvgCircle(vg, cx,cy, r0-0.5f);
  600. nvgCircle(vg, cx,cy, r1+0.5f);
  601. nvgStrokeColor(vg, nvgRGBA(0,0,0,64) );
  602. nvgStrokeWidth(vg, 1.0f);
  603. nvgStroke(vg);
  604. // Selector
  605. nvgSave(vg);
  606. nvgTranslate(vg, cx,cy);
  607. nvgRotate(vg, hue*NVG_PI*2);
  608. // Marker on
  609. nvgStrokeWidth(vg, 2.0f);
  610. nvgBeginPath(vg);
  611. nvgRect(vg, r0-1,-3,r1-r0+2,6);
  612. nvgStrokeColor(vg, nvgRGBA(255,255,255,192) );
  613. nvgStroke(vg);
  614. paint = nvgBoxGradient(vg, r0-3,-5,r1-r0+6,10, 2,4, nvgRGBA(0,0,0,128), nvgRGBA(0,0,0,0) );
  615. nvgBeginPath(vg);
  616. nvgRect(vg, r0-2-10,-4-10,r1-r0+4+20,8+20);
  617. nvgRect(vg, r0-2,-4,r1-r0+4,8);
  618. nvgPathWinding(vg, NVG_HOLE);
  619. nvgFillPaint(vg, paint);
  620. nvgFill(vg);
  621. // Center triangle
  622. r = r0 - 6;
  623. ax = cosf(120.0f/180.0f*NVG_PI) * r;
  624. ay = sinf(120.0f/180.0f*NVG_PI) * r;
  625. bx = cosf(-120.0f/180.0f*NVG_PI) * r;
  626. by = sinf(-120.0f/180.0f*NVG_PI) * r;
  627. nvgBeginPath(vg);
  628. nvgMoveTo(vg, r,0);
  629. nvgLineTo(vg, ax,ay);
  630. nvgLineTo(vg, bx,by);
  631. nvgClosePath(vg);
  632. paint = nvgLinearGradient(vg, r,0, ax,ay, nvgHSLA(hue,1.0f,0.5f,255), nvgRGBA(255,255,255,255) );
  633. nvgFillPaint(vg, paint);
  634. nvgFill(vg);
  635. paint = nvgLinearGradient(vg, (r+ax)*0.5f,(0+ay)*0.5f, bx,by, nvgRGBA(0,0,0,0), nvgRGBA(0,0,0,255) );
  636. nvgFillPaint(vg, paint);
  637. nvgFill(vg);
  638. nvgStrokeColor(vg, nvgRGBA(0,0,0,64) );
  639. nvgStroke(vg);
  640. // Select circle on triangle
  641. ax = cosf(120.0f/180.0f*NVG_PI) * r*0.3f;
  642. ay = sinf(120.0f/180.0f*NVG_PI) * r*0.4f;
  643. nvgStrokeWidth(vg, 2.0f);
  644. nvgBeginPath(vg);
  645. nvgCircle(vg, ax,ay,5);
  646. nvgStrokeColor(vg, nvgRGBA(255,255,255,192) );
  647. nvgStroke(vg);
  648. paint = nvgRadialGradient(vg, ax,ay, 7,9, nvgRGBA(0,0,0,64), nvgRGBA(0,0,0,0) );
  649. nvgBeginPath(vg);
  650. nvgRect(vg, ax-20,ay-20,40,40);
  651. nvgCircle(vg, ax,ay,7);
  652. nvgPathWinding(vg, NVG_HOLE);
  653. nvgFillPaint(vg, paint);
  654. nvgFill(vg);
  655. nvgRestore(vg);
  656. nvgRestore(vg);
  657. }
  658. void drawLines(struct NVGcontext* vg, float x, float y, float w, float h, float t)
  659. {
  660. int i, j;
  661. float pad = 5.0f, s = w/9.0f - pad*2;
  662. float pts[4*2], fx, fy;
  663. int joins[3] = {NVG_MITER, NVG_ROUND, NVG_BEVEL};
  664. int caps[3] = {NVG_BUTT, NVG_ROUND, NVG_SQUARE};
  665. NVG_NOTUSED(h);
  666. nvgSave(vg);
  667. pts[0] = -s*0.25f + cosf(t*0.3f) * s*0.5f;
  668. pts[1] = sinf(t*0.3f) * s*0.5f;
  669. pts[2] = -s*0.25f;
  670. pts[3] = 0;
  671. pts[4] = s*0.25f;
  672. pts[5] = 0;
  673. pts[6] = s*0.25f + cosf(-t*0.3f) * s*0.5f;
  674. pts[7] = sinf(-t*0.3f) * s*0.5f;
  675. for (i = 0; i < 3; i++)
  676. {
  677. for (j = 0; j < 3; j++)
  678. {
  679. fx = x + s*0.5f + (i*3+j)/9.0f*w + pad;
  680. fy = y - s*0.5f + pad;
  681. nvgLineCap(vg, caps[i]);
  682. nvgLineJoin(vg, joins[j]);
  683. nvgStrokeWidth(vg, s*0.3f);
  684. nvgStrokeColor(vg, nvgRGBA(0,0,0,160) );
  685. nvgBeginPath(vg);
  686. nvgMoveTo(vg, fx+pts[0], fy+pts[1]);
  687. nvgLineTo(vg, fx+pts[2], fy+pts[3]);
  688. nvgLineTo(vg, fx+pts[4], fy+pts[5]);
  689. nvgLineTo(vg, fx+pts[6], fy+pts[7]);
  690. nvgStroke(vg);
  691. nvgLineCap(vg, NVG_BUTT);
  692. nvgLineJoin(vg, NVG_BEVEL);
  693. nvgStrokeWidth(vg, 1.0f);
  694. nvgStrokeColor(vg, nvgRGBA(0,192,255,255) );
  695. nvgBeginPath(vg);
  696. nvgMoveTo(vg, fx+pts[0], fy+pts[1]);
  697. nvgLineTo(vg, fx+pts[2], fy+pts[3]);
  698. nvgLineTo(vg, fx+pts[4], fy+pts[5]);
  699. nvgLineTo(vg, fx+pts[6], fy+pts[7]);
  700. nvgStroke(vg);
  701. }
  702. }
  703. nvgRestore(vg);
  704. }
  705. void drawBlendish(struct NVGcontext* _vg, float _x, float _y, float _w, float _h, float _t)
  706. {
  707. float x = _x;
  708. float y = _y;
  709. bndBackground(_vg, _x - 10.0f, _y - 10.0f, _w, _h);
  710. bndToolButton(_vg, x, y, 120.0f, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_DEFAULT, BND_ICONID(6, 3), "Default");
  711. y += 25.0f;
  712. bndToolButton(_vg, x, y, 120.0f, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_HOVER, BND_ICONID(6, 3), "Hovered");
  713. y += 25.0f;
  714. bndToolButton(_vg, x, y, 120.0f, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_ACTIVE, BND_ICONID(6, 3), "Active");
  715. y += 40.0f;
  716. bndRadioButton(_vg, x, y, 80.0f, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_DEFAULT, -1, "Default");
  717. y += 25.0f;
  718. bndRadioButton(_vg, x, y, 80.0f, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_HOVER, -1, "Hovered");
  719. y += 25.0f;
  720. bndRadioButton(_vg, x, y, 80.0f, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_ACTIVE, -1, "Active");
  721. y += 25.0f;
  722. bndLabel(_vg, x, y, 120.0f, BND_WIDGET_HEIGHT, -1, "Label:");
  723. y += float(BND_WIDGET_HEIGHT);
  724. bndChoiceButton(_vg, x, y, 80.0f, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_DEFAULT, -1, "Default");
  725. y += 25.0f;
  726. bndChoiceButton(_vg, x, y, 80.0f, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_HOVER, -1, "Hovered");
  727. y += 25.0f;
  728. bndChoiceButton(_vg, x, y, 80.0f, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_ACTIVE, -1, "Active");
  729. y += 25.0f;
  730. float ry = y;
  731. float rx = x;
  732. y = _y;
  733. x += 130.0f;
  734. bndOptionButton(_vg, x, y, 120.0f, BND_WIDGET_HEIGHT, BND_DEFAULT, "Default");
  735. y += 25.0f;
  736. bndOptionButton(_vg, x, y, 120.0f, BND_WIDGET_HEIGHT, BND_HOVER, "Hovered");
  737. y += 25.0f;
  738. bndOptionButton(_vg, x, y, 120.0f, BND_WIDGET_HEIGHT, BND_ACTIVE, "Active");
  739. y += 40.0f;
  740. bndNumberField(_vg, x, y, 120.0f, BND_WIDGET_HEIGHT, BND_CORNER_DOWN, BND_DEFAULT, "Top", "100");
  741. y += float(BND_WIDGET_HEIGHT) - 2.0f;
  742. bndNumberField(_vg, x, y, 120.0f, BND_WIDGET_HEIGHT, BND_CORNER_ALL, BND_DEFAULT, "Center", "100");
  743. y += float(BND_WIDGET_HEIGHT) - 2.0f;
  744. bndNumberField(_vg, x, y, 120.0f, BND_WIDGET_HEIGHT, BND_CORNER_TOP, BND_DEFAULT, "Bottom", "100");
  745. float mx = x - 30.0f;
  746. float my = y - 12.0f;
  747. float mw = 120.0f;
  748. bndMenuBackground(_vg, mx, my, mw, 120.0f, BND_CORNER_TOP);
  749. bndMenuLabel(_vg, mx, my, mw, BND_WIDGET_HEIGHT, -1, "Menu Title");
  750. my += float(BND_WIDGET_HEIGHT) - 2.0f;
  751. bndMenuItem(_vg, mx, my, mw, BND_WIDGET_HEIGHT, BND_DEFAULT, BND_ICONID(17, 3), "Default");
  752. my += float(BND_WIDGET_HEIGHT) - 2.0f;
  753. bndMenuItem(_vg, mx, my, mw, BND_WIDGET_HEIGHT, BND_HOVER, BND_ICONID(18, 3), "Hovered");
  754. my += float(BND_WIDGET_HEIGHT) - 2.0f;
  755. bndMenuItem(_vg, mx, my, mw, BND_WIDGET_HEIGHT, BND_ACTIVE, BND_ICONID(19, 3), "Active");
  756. y = _y;
  757. x += 130.0f;
  758. float ox = x;
  759. bndNumberField(_vg, x, y, 120.0f, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_DEFAULT, "Default", "100");
  760. y += 25.0f;
  761. bndNumberField(_vg, x, y, 120.0f, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_HOVER, "Hovered", "100");
  762. y += 25.0f;
  763. bndNumberField(_vg, x, y, 120.0f, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_ACTIVE, "Active", "100");
  764. y += 40.0f;
  765. bndRadioButton(_vg, x, y, 60.0f, BND_WIDGET_HEIGHT, BND_CORNER_RIGHT, BND_DEFAULT, -1, "One");
  766. x += 60.0f - 1.0f;
  767. bndRadioButton(_vg, x, y, 60.0f, BND_WIDGET_HEIGHT, BND_CORNER_ALL, BND_DEFAULT, -1, "Two");
  768. x += 60.0f - 1.0f;
  769. bndRadioButton(_vg, x, y, 60.0f, BND_WIDGET_HEIGHT, BND_CORNER_ALL, BND_DEFAULT, -1, "Three");
  770. x += 60.0f - 1.0f;
  771. bndRadioButton(_vg, x, y, 60.0f, BND_WIDGET_HEIGHT, BND_CORNER_LEFT, BND_ACTIVE, -1, "Butts");
  772. x = ox;
  773. y += 40.0f;
  774. float progress_value = fmodf(_t / 10.0f, 1.0f);
  775. char progress_label[32];
  776. bx::snprintf(progress_label, BX_COUNTOF(progress_label), "%d%%", int(progress_value * 100 + 0.5f) );
  777. bndSlider(_vg, x, y, 240, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_DEFAULT, progress_value, "Default", progress_label);
  778. y += 25.0f;
  779. bndSlider(_vg, x, y, 240, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_HOVER, progress_value, "Hovered", progress_label);
  780. y += 25.0f;
  781. bndSlider(_vg, x, y, 240, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_ACTIVE, progress_value, "Active", progress_label);
  782. float rw = x + 240.0f - rx;
  783. float s_offset = sinf(_t / 2.0f) * 0.5f + 0.5f;
  784. float s_size = cosf(_t / 3.11f) * 0.5f + 0.5f;
  785. bndScrollBar(_vg, rx, ry, rw, BND_SCROLLBAR_HEIGHT, BND_DEFAULT, s_offset, s_size);
  786. ry += 20.0f;
  787. bndScrollBar(_vg, rx, ry, rw, BND_SCROLLBAR_HEIGHT, BND_HOVER, s_offset, s_size);
  788. ry += 20.0f;
  789. bndScrollBar(_vg, rx, ry, rw, BND_SCROLLBAR_HEIGHT, BND_ACTIVE, s_offset, s_size);
  790. const char edit_text[] = "The quick brown fox";
  791. int textlen = int(strlen(edit_text) + 1);
  792. int t = int(_t * 2);
  793. int idx1 = (t / textlen) % textlen;
  794. int idx2 = idx1 + (t % (textlen - idx1) );
  795. ry += 25.0f;
  796. bndTextField(_vg, rx, ry, 240.0f, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_DEFAULT, -1, edit_text, idx1, idx2);
  797. ry += 25.0f;
  798. bndTextField(_vg, rx, ry, 240.0f, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_HOVER, -1, edit_text, idx1, idx2);
  799. ry += 25.0f;
  800. bndTextField(_vg, rx, ry, 240.0f, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_ACTIVE, -1, edit_text, idx1, idx2);
  801. rx += rw + 20.0f;
  802. ry = _y;
  803. bndScrollBar(_vg, rx, ry, BND_SCROLLBAR_WIDTH, 240.0f, BND_DEFAULT, s_offset, s_size);
  804. rx += 20.0f;
  805. bndScrollBar(_vg, rx, ry, BND_SCROLLBAR_WIDTH, 240.0f, BND_HOVER, s_offset, s_size);
  806. rx += 20.0f;
  807. bndScrollBar(_vg, rx, ry, BND_SCROLLBAR_WIDTH, 240.0f, BND_ACTIVE, s_offset, s_size);
  808. x = ox;
  809. y += 40.0f;
  810. bndToolButton(_vg, x, y, BND_TOOL_WIDTH, BND_WIDGET_HEIGHT, BND_CORNER_RIGHT, BND_DEFAULT, BND_ICONID(0, 10), NULL);
  811. x += BND_TOOL_WIDTH - 1;
  812. bndToolButton(_vg, x, y, BND_TOOL_WIDTH, BND_WIDGET_HEIGHT, BND_CORNER_ALL, BND_DEFAULT, BND_ICONID(1, 10), NULL);
  813. x += BND_TOOL_WIDTH - 1;
  814. bndToolButton(_vg, x, y, BND_TOOL_WIDTH, BND_WIDGET_HEIGHT, BND_CORNER_ALL, BND_DEFAULT, BND_ICONID(2, 10), NULL);
  815. x += BND_TOOL_WIDTH - 1;
  816. bndToolButton(_vg, x, y, BND_TOOL_WIDTH, BND_WIDGET_HEIGHT, BND_CORNER_ALL, BND_DEFAULT, BND_ICONID(3, 10), NULL);
  817. x += BND_TOOL_WIDTH - 1;
  818. bndToolButton(_vg, x, y, BND_TOOL_WIDTH, BND_WIDGET_HEIGHT, BND_CORNER_ALL, BND_DEFAULT, BND_ICONID(4, 10), NULL);
  819. x += BND_TOOL_WIDTH - 1;
  820. bndToolButton(_vg, x, y, BND_TOOL_WIDTH, BND_WIDGET_HEIGHT, BND_CORNER_LEFT, BND_DEFAULT, BND_ICONID(5, 10), NULL);
  821. x += BND_TOOL_WIDTH - 1;
  822. x += 5.0f;
  823. bndRadioButton(_vg, x, y, BND_TOOL_WIDTH, BND_WIDGET_HEIGHT, BND_CORNER_RIGHT, BND_DEFAULT, BND_ICONID(0, 11), NULL);
  824. x += BND_TOOL_WIDTH - 1;
  825. bndRadioButton(_vg, x, y, BND_TOOL_WIDTH, BND_WIDGET_HEIGHT, BND_CORNER_ALL, BND_DEFAULT, BND_ICONID(1, 11), NULL);
  826. x += BND_TOOL_WIDTH - 1;
  827. bndRadioButton(_vg, x, y, BND_TOOL_WIDTH, BND_WIDGET_HEIGHT, BND_CORNER_ALL, BND_DEFAULT, BND_ICONID(2, 11), NULL);
  828. x += BND_TOOL_WIDTH - 1;
  829. bndRadioButton(_vg, x, y, BND_TOOL_WIDTH, BND_WIDGET_HEIGHT, BND_CORNER_ALL, BND_DEFAULT, BND_ICONID(3, 11), NULL);
  830. x += BND_TOOL_WIDTH - 1;
  831. bndRadioButton(_vg, x, y, BND_TOOL_WIDTH, BND_WIDGET_HEIGHT, BND_CORNER_ALL, BND_ACTIVE, BND_ICONID(4, 11), NULL);
  832. x += BND_TOOL_WIDTH - 1;
  833. bndRadioButton(_vg, x, y, BND_TOOL_WIDTH, BND_WIDGET_HEIGHT, BND_CORNER_LEFT, BND_DEFAULT, BND_ICONID(5, 11), NULL);
  834. }
  835. struct DemoData
  836. {
  837. int fontNormal, fontBold, fontIcons, fontEmoji;
  838. int images[12];
  839. };
  840. int32_t createImage(struct NVGcontext* _ctx, const char* _filePath, int _imageFlags)
  841. {
  842. uint32_t size;
  843. void* data = load(_filePath, &size);
  844. if (NULL == data)
  845. {
  846. return 0;
  847. }
  848. bimg::ImageContainer* imageContainer = bimg::imageParse(
  849. entry::getAllocator()
  850. , data
  851. , size
  852. , bimg::TextureFormat::RGBA8
  853. );
  854. unload(data);
  855. if (NULL == imageContainer)
  856. {
  857. return 0;
  858. }
  859. int32_t texId = nvgCreateImageRGBA(
  860. _ctx
  861. , imageContainer->m_width
  862. , imageContainer->m_height
  863. , _imageFlags
  864. , (const uint8_t*)imageContainer->m_data
  865. );
  866. bimg::imageFree(imageContainer);
  867. return texId;
  868. }
  869. int32_t createFont(NVGcontext* _ctx, const char* _name, const char* _filePath)
  870. {
  871. uint32_t size;
  872. void* data = load(_filePath, &size);
  873. if (NULL == data)
  874. {
  875. return -1;
  876. }
  877. return nvgCreateFontMem(_ctx, _name, (uint8_t*)data, size, 0);
  878. }
  879. int loadDemoData(struct NVGcontext* vg, struct DemoData* data)
  880. {
  881. int i;
  882. if (vg == NULL)
  883. return -1;
  884. for (i = 0; i < 12; i++)
  885. {
  886. char file[128];
  887. bx::snprintf(file, 128, "images/image%d.jpg", i+1);
  888. data->images[i] = createImage(vg, file, 0);
  889. if (data->images[i] == 0)
  890. {
  891. bx::debugPrintf("Could not load %s.\n", file);
  892. return -1;
  893. }
  894. }
  895. int32_t result = 0;
  896. data->fontIcons = createFont(vg, "icons", "font/entypo.ttf");
  897. if (data->fontIcons == -1)
  898. {
  899. bx::debugPrintf("Could not add font icons.\n");
  900. result = -1;
  901. }
  902. data->fontNormal = createFont(vg, "sans", "font/roboto-regular.ttf");
  903. if (data->fontNormal == -1)
  904. {
  905. bx::debugPrintf("Could not add font italic.\n");
  906. result = -1;
  907. }
  908. data->fontBold = createFont(vg, "sans-bold", "font/roboto-bold.ttf");
  909. if (data->fontBold == -1)
  910. {
  911. bx::debugPrintf("Could not add font bold.\n");
  912. result = -1;
  913. }
  914. data->fontEmoji = createFont(vg, "emoji", "font/NotoEmoji-Regular.ttf");
  915. if (data->fontEmoji == -1)
  916. {
  917. bx::debugPrintf("Could not add font emoji.\n");
  918. result = -1;
  919. }
  920. nvgAddFallbackFontId(vg, data->fontNormal, data->fontEmoji);
  921. nvgAddFallbackFontId(vg, data->fontBold, data->fontEmoji);
  922. return result;
  923. }
  924. void freeDemoData(struct NVGcontext* vg, struct DemoData* data)
  925. {
  926. int i;
  927. if (vg == NULL)
  928. return;
  929. for (i = 0; i < 12; i++)
  930. nvgDeleteImage(vg, data->images[i]);
  931. }
  932. void drawParagraph(struct NVGcontext* vg, float x, float y, float width, float height, float mx, float my)
  933. {
  934. struct NVGtextRow rows[3];
  935. struct NVGglyphPosition glyphs[100];
  936. const char* text = "This is longer chunk of text.\n \n Would have used lorem ipsum but she was busy jumping over the lazy dog with the fox and all the men who came to the aid of the party.🎉";
  937. const char* start;
  938. const char* end;
  939. int nrows, i, nglyphs, j, lnum = 0;
  940. float lineh;
  941. float caretx, px;
  942. float bounds[4];
  943. float a;
  944. float gx = 0.0f, gy = 0.0f;
  945. int gutter = 0;
  946. NVG_NOTUSED(height);
  947. nvgSave(vg);
  948. nvgFontSize(vg, 18.0f);
  949. nvgFontFace(vg, "sans");
  950. nvgTextAlign(vg, NVG_ALIGN_LEFT|NVG_ALIGN_TOP);
  951. nvgTextMetrics(vg, NULL, NULL, &lineh);
  952. // The text break API can be used to fill a large buffer of rows,
  953. // or to iterate over the text just few lines (or just one) at a time.
  954. // The "next" variable of the last returned item tells where to continue.
  955. start = text;
  956. end = text + strlen(text);
  957. for (nrows = nvgTextBreakLines(vg, start, end, width, rows, 3); 0 != nrows; nrows = nvgTextBreakLines(vg, start, end, width, rows, 3) )
  958. {
  959. for (i = 0; i < nrows; i++) {
  960. struct NVGtextRow* row = &rows[i];
  961. int hit = mx > x && mx < (x+width) && my >= y && my < (y+lineh);
  962. nvgBeginPath(vg);
  963. nvgFillColor(vg, nvgRGBA(255,255,255,hit?64:16) );
  964. nvgRect(vg, x, y, row->width, lineh);
  965. nvgFill(vg);
  966. nvgFillColor(vg, nvgRGBA(255,255,255,255) );
  967. nvgText(vg, x, y, row->start, row->end);
  968. if (hit) {
  969. caretx = (mx < x+row->width/2) ? x : x+row->width;
  970. px = x;
  971. nglyphs = nvgTextGlyphPositions(vg, x, y, row->start, row->end, glyphs, 100);
  972. for (j = 0; j < nglyphs; j++) {
  973. float x0 = glyphs[j].x;
  974. float x1 = (j+1 < nglyphs) ? glyphs[j+1].x : x+row->width;
  975. float tgx = x0 * 0.3f + x1 * 0.7f;
  976. if (mx >= px && mx < tgx)
  977. caretx = glyphs[j].x;
  978. px = tgx;
  979. }
  980. nvgBeginPath(vg);
  981. nvgFillColor(vg, nvgRGBA(255,192,0,255) );
  982. nvgRect(vg, caretx, y, 1, lineh);
  983. nvgFill(vg);
  984. gutter = lnum+1;
  985. gx = x - 10;
  986. gy = y + lineh/2;
  987. }
  988. lnum++;
  989. y += lineh;
  990. }
  991. // Keep going...
  992. start = rows[nrows-1].next;
  993. }
  994. if (gutter)
  995. {
  996. char txt[16];
  997. bx::snprintf(txt, sizeof(txt), "%d", gutter);
  998. nvgFontSize(vg, 13.0f);
  999. nvgTextAlign(vg, NVG_ALIGN_RIGHT|NVG_ALIGN_MIDDLE);
  1000. nvgTextBounds(vg, gx,gy, txt, NULL, bounds);
  1001. nvgBeginPath(vg);
  1002. nvgFillColor(vg, nvgRGBA(255,192,0,255) );
  1003. nvgRoundedRect(vg
  1004. , bx::round(bounds[0])-4.0f
  1005. , bx::round(bounds[1])-2.0f
  1006. , bx::round(bounds[2]-bounds[0])+8.0f
  1007. , bx::round(bounds[3]-bounds[1])+4.0f
  1008. , (bx::round(bounds[3]-bounds[1])+4.0f)/2.0f-1.0f
  1009. );
  1010. nvgFill(vg);
  1011. nvgFillColor(vg, nvgRGBA(32,32,32,255) );
  1012. nvgText(vg, gx,gy, txt, NULL);
  1013. }
  1014. y += 20.0f;
  1015. nvgFontSize(vg, 13.0f);
  1016. nvgTextAlign(vg, NVG_ALIGN_LEFT|NVG_ALIGN_TOP);
  1017. nvgTextLineHeight(vg, 1.2f);
  1018. nvgTextBoxBounds(vg, x,y, 150, "Hover your mouse over the text to see calculated caret position.", NULL, bounds);
  1019. // Fade the tooltip out when close to it.
  1020. gx = bx::abs((mx - (bounds[0]+bounds[2])*0.5f) / (bounds[0] - bounds[2]) );
  1021. gy = bx::abs((my - (bounds[1]+bounds[3])*0.5f) / (bounds[1] - bounds[3]) );
  1022. a = bx::max(gx, gy) - 0.5f;
  1023. a = bx::clamp(a, 0.0f, 1.0f);
  1024. nvgGlobalAlpha(vg, a);
  1025. nvgBeginPath(vg);
  1026. nvgFillColor(vg, nvgRGBA(220,220,220,255) );
  1027. nvgRoundedRect(vg
  1028. , bx::round(bounds[0]-2.0f)
  1029. , bx::round(bounds[1]-2.0f)
  1030. , bx::round(bounds[2]-bounds[0])+4.0f
  1031. , bx::round(bounds[3]-bounds[1])+4.0f
  1032. , 3.0f
  1033. );
  1034. px = float( (int)( (bounds[2]+bounds[0])/2) );
  1035. nvgMoveTo(vg, px,bounds[1] - 10);
  1036. nvgLineTo(vg, px+7,bounds[1]+1);
  1037. nvgLineTo(vg, px-7,bounds[1]+1);
  1038. nvgFill(vg);
  1039. nvgFillColor(vg, nvgRGBA(0,0,0,220) );
  1040. nvgTextBox(vg, x,y, 150, "Hover your mouse over the text to see calculated caret position.", NULL);
  1041. nvgRestore(vg);
  1042. }
  1043. void drawWidths(struct NVGcontext* vg, float x, float y, float width)
  1044. {
  1045. int i;
  1046. nvgSave(vg);
  1047. nvgStrokeColor(vg, nvgRGBA(0,0,0,255) );
  1048. for (i = 0; i < 20; i++)
  1049. {
  1050. float w = (i+0.5f)*0.1f;
  1051. nvgStrokeWidth(vg, w);
  1052. nvgBeginPath(vg);
  1053. nvgMoveTo(vg, x,y);
  1054. nvgLineTo(vg, x+width,y+width*0.3f);
  1055. nvgStroke(vg);
  1056. y += 10;
  1057. }
  1058. nvgRestore(vg);
  1059. }
  1060. void drawCaps(struct NVGcontext* vg, float x, float y, float width)
  1061. {
  1062. int i;
  1063. int caps[3] = {NVG_BUTT, NVG_ROUND, NVG_SQUARE};
  1064. float lineWidth = 8.0f;
  1065. nvgSave(vg);
  1066. nvgBeginPath(vg);
  1067. nvgRect(vg, x-lineWidth/2, y, width+lineWidth, 40);
  1068. nvgFillColor(vg, nvgRGBA(255,255,255,32) );
  1069. nvgFill(vg);
  1070. nvgBeginPath(vg);
  1071. nvgRect(vg, x, y, width, 40);
  1072. nvgFillColor(vg, nvgRGBA(255,255,255,32) );
  1073. nvgFill(vg);
  1074. nvgStrokeWidth(vg, lineWidth);
  1075. for (i = 0; i < 3; i++) {
  1076. nvgLineCap(vg, caps[i]);
  1077. nvgStrokeColor(vg, nvgRGBA(0,0,0,255) );
  1078. nvgBeginPath(vg);
  1079. nvgMoveTo(vg, x, y + i*10 + 5);
  1080. nvgLineTo(vg, x+width, y + i*10 + 5);
  1081. nvgStroke(vg);
  1082. }
  1083. nvgRestore(vg);
  1084. }
  1085. void drawScissor(struct NVGcontext* vg, float x, float y, float t)
  1086. {
  1087. nvgSave(vg);
  1088. // Draw first rect and set scissor to it's area.
  1089. nvgTranslate(vg, x, y);
  1090. nvgRotate(vg, nvgDegToRad(5) );
  1091. nvgBeginPath(vg);
  1092. nvgRect(vg, -20,-20,60,40);
  1093. nvgFillColor(vg, nvgRGBA(255,0,0,255) );
  1094. nvgFill(vg);
  1095. nvgScissor(vg, -20,-20,60,40);
  1096. // Draw second rectangle with offset and rotation.
  1097. nvgTranslate(vg, 40,0);
  1098. nvgRotate(vg, t);
  1099. // Draw the intended second rectangle without any scissoring.
  1100. nvgSave(vg);
  1101. nvgResetScissor(vg);
  1102. nvgBeginPath(vg);
  1103. nvgRect(vg, -20,-10,60,30);
  1104. nvgFillColor(vg, nvgRGBA(255,128,0,64) );
  1105. nvgFill(vg);
  1106. nvgRestore(vg);
  1107. // Draw second rectangle with combined scissoring.
  1108. nvgIntersectScissor(vg, -20,-10,60,30);
  1109. nvgBeginPath(vg);
  1110. nvgRect(vg, -20,-10,60,30);
  1111. nvgFillColor(vg, nvgRGBA(255,128,0,255) );
  1112. nvgFill(vg);
  1113. nvgRestore(vg);
  1114. }
  1115. void renderDemo(struct NVGcontext* vg, float mx, float my, float width, float height, float t, int blowup, struct DemoData* data)
  1116. {
  1117. float x,y,popx,popy;
  1118. drawEyes(vg, width-800, height-240, 150, 100, mx, my, t);
  1119. drawParagraph(vg, width - 550, 35, 150, 100, mx, my);
  1120. drawGraph(vg, 0, height/2, width, height/2, t);
  1121. drawColorwheel(vg, width-350, 35, 250.0f, 250.0f, t);
  1122. // Line joints
  1123. drawLines(vg, 50, height-50, 600, 35, t);
  1124. // Line caps
  1125. drawWidths(vg, width-50, 35, 30);
  1126. // Line caps
  1127. drawCaps(vg, width-50, 260, 30);
  1128. drawScissor(vg, 40, height-150, t);
  1129. nvgSave(vg);
  1130. if (blowup)
  1131. {
  1132. nvgRotate(vg, sinf(t*0.3f)*5.0f/180.0f*NVG_PI);
  1133. nvgScale(vg, 2.0f, 2.0f);
  1134. }
  1135. // Widgets.
  1136. x = width-520; y = height-420;
  1137. drawWindow(vg, "Widgets `n Stuff", x, y, 300, 400);
  1138. x += 10;
  1139. y += 45;
  1140. drawSearchBox(vg, "Search", x,y,280,25);
  1141. y += 40;
  1142. drawDropDown(vg, "Effects", x,y,280,28);
  1143. popx = x + 300;
  1144. popy = y + 14;
  1145. y += 45;
  1146. // Form
  1147. drawLabel(vg, "Login", x,y, 280,20);
  1148. y += 25;
  1149. drawEditBox(vg, "Email", x,y, 280,28);
  1150. y += 35;
  1151. drawEditBox(vg, "Password", x,y, 280,28);
  1152. y += 38;
  1153. drawCheckBox(vg, "Remember me", x,y, 140,28);
  1154. drawButton(vg, ICON_LOGIN, "Sign in", x+138, y, 140, 28, nvgRGBA(0,96,128,255) );
  1155. y += 45;
  1156. // Slider
  1157. drawLabel(vg, "Diameter", x,y, 280,20);
  1158. y += 25;
  1159. drawEditBoxNum(vg, "123.00", "px", x+180,y, 100,28);
  1160. drawSlider(vg, 0.4f, x,y, 170,28);
  1161. y += 55;
  1162. drawButton(vg, ICON_TRASH, "Delete", x, y, 160, 28, nvgRGBA(128,16,8,255) );
  1163. drawButton(vg, 0, "Cancel", x+170, y, 110, 28, nvgRGBA(0,0,0,0) );
  1164. // Thumbnails box
  1165. drawThumbnails(vg, popx, popy-30, 160, 300, data->images, 12, t);
  1166. // Blendish
  1167. drawBlendish(vg, 10, 62, 600.0f, 420.0f, t);
  1168. nvgRestore(vg);
  1169. }
  1170. class ExampleNanoVG : public entry::AppI
  1171. {
  1172. public:
  1173. ExampleNanoVG(const char* _name, const char* _description, const char* _url)
  1174. : entry::AppI(_name, _description, _url)
  1175. {
  1176. }
  1177. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  1178. {
  1179. Args args(_argc, _argv);
  1180. m_width = _width;
  1181. m_height = _height;
  1182. m_debug = BGFX_DEBUG_NONE;
  1183. m_reset = BGFX_RESET_VSYNC;
  1184. bgfx::Init init;
  1185. init.type = args.m_type;
  1186. init.vendorId = args.m_pciId;
  1187. init.platformData.nwh = entry::getNativeWindowHandle(entry::kDefaultWindowHandle);
  1188. init.platformData.ndt = entry::getNativeDisplayHandle();
  1189. init.platformData.type = entry::getNativeWindowHandleType();
  1190. init.resolution.width = m_width;
  1191. init.resolution.height = m_height;
  1192. init.resolution.reset = m_reset;
  1193. bgfx::init(init);
  1194. // Enable debug text.
  1195. bgfx::setDebug(m_debug);
  1196. // Set view 0 clear state.
  1197. bgfx::setViewClear(0
  1198. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  1199. , 0x303030ff
  1200. , 1.0f
  1201. , 0
  1202. );
  1203. imguiCreate();
  1204. m_nvg = nvgCreate(1, 0);
  1205. bgfx::setViewMode(0, bgfx::ViewMode::Sequential);
  1206. loadDemoData(m_nvg, &m_data);
  1207. bndSetFont(createFont(m_nvg, "droidsans", "font/droidsans.ttf") );
  1208. bndSetIconImage(createImage(m_nvg, "images/blender_icons16.png", 0) );
  1209. m_frameTime.reset();
  1210. }
  1211. int shutdown() override
  1212. {
  1213. freeDemoData(m_nvg, &m_data);
  1214. nvgDelete(m_nvg);
  1215. imguiDestroy();
  1216. // Shutdown bgfx.
  1217. bgfx::shutdown();
  1218. return 0;
  1219. }
  1220. bool update() override
  1221. {
  1222. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  1223. {
  1224. m_frameTime.frame();
  1225. const float time = bx::toSeconds<float>(m_frameTime.getDurationTime() );
  1226. imguiBeginFrame(m_mouseState.m_mx
  1227. , m_mouseState.m_my
  1228. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  1229. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  1230. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  1231. , m_mouseState.m_mz
  1232. , uint16_t(m_width)
  1233. , uint16_t(m_height)
  1234. );
  1235. showExampleDialog(this);
  1236. imguiEndFrame();
  1237. // Set view 0 default viewport.
  1238. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  1239. // This dummy draw call is here to make sure that view 0 is cleared
  1240. // if no other draw calls are submitted to view 0.
  1241. bgfx::touch(0);
  1242. nvgBeginFrame(m_nvg, float(m_width), float(m_height), 1.0f);
  1243. renderDemo(m_nvg, float(m_mouseState.m_mx), float(m_mouseState.m_my), float(m_width), float(m_height), time, 0, &m_data);
  1244. nvgEndFrame(m_nvg);
  1245. // Advance to next frame. Rendering thread will be kicked to
  1246. // process submitted rendering primitives.
  1247. bgfx::frame();
  1248. return true;
  1249. }
  1250. return false;
  1251. }
  1252. uint32_t m_width;
  1253. uint32_t m_height;
  1254. uint32_t m_debug;
  1255. uint32_t m_reset;
  1256. entry::MouseState m_mouseState;
  1257. NVGcontext* m_nvg;
  1258. DemoData m_data;
  1259. FrameTime m_frameTime;
  1260. };
  1261. } // namespace
  1262. ENTRY_IMPLEMENT_MAIN(
  1263. ExampleNanoVG
  1264. , "20-nanovg"
  1265. , "NanoVG is small antialiased vector graphics rendering library."
  1266. , "https://bkaradzic.github.io/bgfx/examples.html#nanovg"
  1267. );