nanovg.cpp 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  1. /*
  2. * Copyright 2011-2015 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  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 <stdio.h>
  24. #include <math.h>
  25. #include <bgfx.h>
  26. #include <bx/string.h>
  27. #include <bx/timer.h>
  28. #include "entry/entry.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. #define ICON_SEARCH 0x1F50D
  36. #define ICON_CIRCLED_CROSS 0x2716
  37. #define ICON_CHEVRON_RIGHT 0xE75E
  38. #define ICON_CHECK 0x2713
  39. #define ICON_LOGIN 0xE740
  40. #define ICON_TRASH 0xE729
  41. // Returns 1 if col.rgba is 0.0f,0.0f,0.0f,0.0f, 0 otherwise
  42. int isBlack( struct NVGcolor col )
  43. {
  44. if( col.r == 0.0f && col.g == 0.0f && col.b == 0.0f && col.a == 0.0f )
  45. {
  46. return 1;
  47. }
  48. return 0;
  49. }
  50. static char* cpToUTF8(int cp, char* str)
  51. {
  52. int n = 0;
  53. if (cp < 0x80) n = 1;
  54. else if (cp < 0x800) n = 2;
  55. else if (cp < 0x10000) n = 3;
  56. else if (cp < 0x200000) n = 4;
  57. else if (cp < 0x4000000) n = 5;
  58. else if (cp <= 0x7fffffff) n = 6;
  59. str[n] = '\0';
  60. switch (n)
  61. {
  62. case 6: str[5] = 0x80 | (cp & 0x3f); cp = cp >> 6; cp |= 0x4000000;
  63. case 5: str[4] = 0x80 | (cp & 0x3f); cp = cp >> 6; cp |= 0x200000;
  64. case 4: str[3] = 0x80 | (cp & 0x3f); cp = cp >> 6; cp |= 0x10000;
  65. case 3: str[2] = 0x80 | (cp & 0x3f); cp = cp >> 6; cp |= 0x800;
  66. case 2: str[1] = 0x80 | (cp & 0x3f); cp = cp >> 6; cp |= 0xc0;
  67. case 1: str[0] = cp;
  68. }
  69. return str;
  70. }
  71. void drawWindow(struct NVGcontext* vg, const char* title, float x, float y, float w, float h)
  72. {
  73. float cornerRadius = 3.0f;
  74. struct NVGpaint shadowPaint;
  75. struct NVGpaint headerPaint;
  76. nvgSave(vg);
  77. // nvgClearState(vg);
  78. // Window
  79. nvgBeginPath(vg);
  80. nvgRoundedRect(vg, x,y, w,h, cornerRadius);
  81. nvgFillColor(vg, nvgRGBA(28,30,34,192));
  82. // nvgFillColor(vg, nvgRGBA(0,0,0,128));
  83. nvgFill(vg);
  84. // Drop shadow
  85. shadowPaint = nvgBoxGradient(vg, x,y+2, w,h, cornerRadius*2, 10, nvgRGBA(0,0,0,128), nvgRGBA(0,0,0,0));
  86. nvgBeginPath(vg);
  87. nvgRect(vg, x-10,y-10, w+20,h+30);
  88. nvgRoundedRect(vg, x,y, w,h, cornerRadius);
  89. nvgPathWinding(vg, NVG_HOLE);
  90. nvgFillPaint(vg, shadowPaint);
  91. nvgFill(vg);
  92. // Header
  93. headerPaint = nvgLinearGradient(vg, x,y,x,y+15, nvgRGBA(255,255,255,8), nvgRGBA(0,0,0,16));
  94. nvgBeginPath(vg);
  95. nvgRoundedRect(vg, x+1,y+1, w-2,30, cornerRadius-1);
  96. nvgFillPaint(vg, headerPaint);
  97. nvgFill(vg);
  98. nvgBeginPath(vg);
  99. nvgMoveTo(vg, x+0.5f, y+0.5f+30);
  100. nvgLineTo(vg, x+0.5f+w-1, y+0.5f+30);
  101. nvgStrokeColor(vg, nvgRGBA(0,0,0,32));
  102. nvgStroke(vg);
  103. nvgFontSize(vg, 18.0f);
  104. nvgFontFace(vg, "sans-bold");
  105. nvgTextAlign(vg,NVG_ALIGN_CENTER|NVG_ALIGN_MIDDLE);
  106. nvgFontBlur(vg,2);
  107. nvgFillColor(vg, nvgRGBA(0,0,0,128));
  108. nvgText(vg, x+w/2,y+16+1, title, NULL);
  109. nvgFontBlur(vg,0);
  110. nvgFillColor(vg, nvgRGBA(220,220,220,160));
  111. nvgText(vg, x+w/2,y+16, title, NULL);
  112. nvgRestore(vg);
  113. }
  114. void drawSearchBox(struct NVGcontext* vg, const char* text, float x, float y, float w, float h)
  115. {
  116. struct NVGpaint bg;
  117. char icon[8];
  118. float cornerRadius = h/2-1;
  119. // Edit
  120. bg = nvgBoxGradient(vg, x,y+1.5f, w,h, h/2,5, nvgRGBA(0,0,0,16), nvgRGBA(0,0,0,92));
  121. nvgBeginPath(vg);
  122. nvgRoundedRect(vg, x,y, w,h, cornerRadius);
  123. nvgFillPaint(vg, bg);
  124. nvgFill(vg);
  125. /* nvgBeginPath(vg);
  126. nvgRoundedRect(vg, x+0.5f,y+0.5f, w-1,h-1, cornerRadius-0.5f);
  127. nvgStrokeColor(vg, nvgRGBA(0,0,0,48));
  128. nvgStroke(vg);*/
  129. nvgFontSize(vg, h*1.3f);
  130. nvgFontFace(vg, "icons");
  131. nvgFillColor(vg, nvgRGBA(255,255,255,64));
  132. nvgTextAlign(vg,NVG_ALIGN_CENTER|NVG_ALIGN_MIDDLE);
  133. nvgText(vg, x+h*0.55f, y+h*0.55f, cpToUTF8(ICON_SEARCH,icon), NULL);
  134. nvgFontSize(vg, 20.0f);
  135. nvgFontFace(vg, "sans");
  136. nvgFillColor(vg, nvgRGBA(255,255,255,32));
  137. nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
  138. nvgText(vg, x+h*1.05f,y+h*0.5f,text, NULL);
  139. nvgFontSize(vg, h*1.3f);
  140. nvgFontFace(vg, "icons");
  141. nvgFillColor(vg, nvgRGBA(255,255,255,32));
  142. nvgTextAlign(vg,NVG_ALIGN_CENTER|NVG_ALIGN_MIDDLE);
  143. nvgText(vg, x+w-h*0.55f, y+h*0.55f, cpToUTF8(ICON_CIRCLED_CROSS,icon), NULL);
  144. }
  145. void drawDropDown(struct NVGcontext* vg, const char* text, float x, float y, float w, float h)
  146. {
  147. struct NVGpaint bg;
  148. char icon[8];
  149. float cornerRadius = 4.0f;
  150. bg = nvgLinearGradient(vg, x,y,x,y+h, nvgRGBA(255,255,255,16), nvgRGBA(0,0,0,16));
  151. nvgBeginPath(vg);
  152. nvgRoundedRect(vg, x+1,y+1, w-2,h-2, cornerRadius-1);
  153. nvgFillPaint(vg, bg);
  154. nvgFill(vg);
  155. nvgBeginPath(vg);
  156. nvgRoundedRect(vg, x+0.5f,y+0.5f, w-1,h-1, cornerRadius-0.5f);
  157. nvgStrokeColor(vg, nvgRGBA(0,0,0,48));
  158. nvgStroke(vg);
  159. nvgFontSize(vg, 20.0f);
  160. nvgFontFace(vg, "sans");
  161. nvgFillColor(vg, nvgRGBA(255,255,255,160));
  162. nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
  163. nvgText(vg, x+h*0.3f,y+h*0.5f,text, NULL);
  164. nvgFontSize(vg, h*1.3f);
  165. nvgFontFace(vg, "icons");
  166. nvgFillColor(vg, nvgRGBA(255,255,255,64));
  167. nvgTextAlign(vg,NVG_ALIGN_CENTER|NVG_ALIGN_MIDDLE);
  168. nvgText(vg, x+w-h*0.5f, y+h*0.5f, cpToUTF8(ICON_CHEVRON_RIGHT,icon), NULL);
  169. }
  170. void drawLabel(struct NVGcontext* vg, const char* text, float x, float y, float w, float h)
  171. {
  172. NVG_NOTUSED(w);
  173. nvgFontSize(vg, 18.0f);
  174. nvgFontFace(vg, "sans");
  175. nvgFillColor(vg, nvgRGBA(255,255,255,128));
  176. nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
  177. nvgText(vg, x,y+h*0.5f,text, NULL);
  178. }
  179. void drawEditBoxBase(struct NVGcontext* vg, float x, float y, float w, float h)
  180. {
  181. struct NVGpaint bg;
  182. // Edit
  183. 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));
  184. nvgBeginPath(vg);
  185. nvgRoundedRect(vg, x+1,y+1, w-2,h-2, 4-1);
  186. nvgFillPaint(vg, bg);
  187. nvgFill(vg);
  188. nvgBeginPath(vg);
  189. nvgRoundedRect(vg, x+0.5f,y+0.5f, w-1,h-1, 4-0.5f);
  190. nvgStrokeColor(vg, nvgRGBA(0,0,0,48));
  191. nvgStroke(vg);
  192. }
  193. void drawEditBox(struct NVGcontext* vg, const char* text, float x, float y, float w, float h)
  194. {
  195. drawEditBoxBase(vg, x,y, w,h);
  196. nvgFontSize(vg, 20.0f);
  197. nvgFontFace(vg, "sans");
  198. nvgFillColor(vg, nvgRGBA(255,255,255,64));
  199. nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
  200. nvgText(vg, x+h*0.3f,y+h*0.5f,text, NULL);
  201. }
  202. void drawEditBoxNum(struct NVGcontext* vg,
  203. const char* text, const char* units, float x, float y, float w, float h)
  204. {
  205. float uw;
  206. drawEditBoxBase(vg, x,y, w,h);
  207. uw = nvgTextBounds(vg, 0,0, units, NULL, NULL);
  208. nvgFontSize(vg, 18.0f);
  209. nvgFontFace(vg, "sans");
  210. nvgFillColor(vg, nvgRGBA(255,255,255,64));
  211. nvgTextAlign(vg,NVG_ALIGN_RIGHT|NVG_ALIGN_MIDDLE);
  212. nvgText(vg, x+w-h*0.3f,y+h*0.5f,units, NULL);
  213. nvgFontSize(vg, 20.0f);
  214. nvgFontFace(vg, "sans");
  215. nvgFillColor(vg, nvgRGBA(255,255,255,128));
  216. nvgTextAlign(vg,NVG_ALIGN_RIGHT|NVG_ALIGN_MIDDLE);
  217. nvgText(vg, x+w-uw-h*0.5f,y+h*0.5f,text, NULL);
  218. }
  219. void drawCheckBox(struct NVGcontext* vg, const char* text, float x, float y, float w, float h)
  220. {
  221. struct NVGpaint bg;
  222. char icon[8];
  223. NVG_NOTUSED(w);
  224. nvgFontSize(vg, 18.0f);
  225. nvgFontFace(vg, "sans");
  226. nvgFillColor(vg, nvgRGBA(255,255,255,160));
  227. nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
  228. nvgText(vg, x+28,y+h*0.5f,text, NULL);
  229. 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));
  230. nvgBeginPath(vg);
  231. nvgRoundedRect(vg, x+1,y+(int)(h*0.5f)-9, 18,18, 3);
  232. nvgFillPaint(vg, bg);
  233. nvgFill(vg);
  234. nvgFontSize(vg, 40);
  235. nvgFontFace(vg, "icons");
  236. nvgFillColor(vg, nvgRGBA(255,255,255,128));
  237. nvgTextAlign(vg,NVG_ALIGN_CENTER|NVG_ALIGN_MIDDLE);
  238. nvgText(vg, x+9+2, y+h*0.5f, cpToUTF8(ICON_CHECK,icon), NULL);
  239. }
  240. void drawButton(struct NVGcontext* vg, int preicon, const char* text, float x, float y, float w, float h, struct NVGcolor col)
  241. {
  242. struct NVGpaint bg;
  243. char icon[8];
  244. float cornerRadius = 4.0f;
  245. float tw = 0, iw = 0;
  246. bg = nvgLinearGradient(vg, x,y,x,y+h, nvgRGBA(255,255,255,isBlack(col)?16:32), nvgRGBA(0,0,0,isBlack(col)?16:32));
  247. nvgBeginPath(vg);
  248. nvgRoundedRect(vg, x+1,y+1, w-2,h-2, cornerRadius-1);
  249. if (!isBlack(col)) {
  250. nvgFillColor(vg, col);
  251. nvgFill(vg);
  252. }
  253. nvgFillPaint(vg, bg);
  254. nvgFill(vg);
  255. nvgBeginPath(vg);
  256. nvgRoundedRect(vg, x+0.5f,y+0.5f, w-1,h-1, cornerRadius-0.5f);
  257. nvgStrokeColor(vg, nvgRGBA(0,0,0,48));
  258. nvgStroke(vg);
  259. nvgFontSize(vg, 20.0f);
  260. nvgFontFace(vg, "sans-bold");
  261. tw = nvgTextBounds(vg, 0,0, text, NULL, NULL);
  262. if (preicon != 0) {
  263. nvgFontSize(vg, h*1.3f);
  264. nvgFontFace(vg, "icons");
  265. iw = nvgTextBounds(vg, 0,0, cpToUTF8(preicon,icon), NULL, NULL);
  266. iw += h*0.15f;
  267. }
  268. if (preicon != 0) {
  269. nvgFontSize(vg, h*1.3f);
  270. nvgFontFace(vg, "icons");
  271. nvgFillColor(vg, nvgRGBA(255,255,255,96));
  272. nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
  273. nvgText(vg, x+w*0.5f-tw*0.5f-iw*0.75f, y+h*0.5f, cpToUTF8(preicon,icon), NULL);
  274. }
  275. nvgFontSize(vg, 20.0f);
  276. nvgFontFace(vg, "sans-bold");
  277. nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
  278. nvgFillColor(vg, nvgRGBA(0,0,0,160));
  279. nvgText(vg, x+w*0.5f-tw*0.5f+iw*0.25f,y+h*0.5f-1,text, NULL);
  280. nvgFillColor(vg, nvgRGBA(255,255,255,160));
  281. nvgText(vg, x+w*0.5f-tw*0.5f+iw*0.25f,y+h*0.5f,text, NULL);
  282. }
  283. void drawSlider(struct NVGcontext* vg, float pos, float x, float y, float w, float h)
  284. {
  285. struct NVGpaint bg, knob;
  286. float cy = y+(int)(h*0.5f);
  287. float kr = (float)( (int)(h*0.25f) );
  288. nvgSave(vg);
  289. // nvgClearState(vg);
  290. // Slot
  291. bg = nvgBoxGradient(vg, x,cy-2+1, w,4, 2,2, nvgRGBA(0,0,0,32), nvgRGBA(0,0,0,128));
  292. nvgBeginPath(vg);
  293. nvgRoundedRect(vg, x,cy-2, w,4, 2);
  294. nvgFillPaint(vg, bg);
  295. nvgFill(vg);
  296. // Knob Shadow
  297. bg = nvgRadialGradient(vg, x+(int)(pos*w),cy+1, kr-3,kr+3, nvgRGBA(0,0,0,64), nvgRGBA(0,0,0,0));
  298. nvgBeginPath(vg);
  299. nvgRect(vg, x+(int)(pos*w)-kr-5,cy-kr-5,kr*2+5+5,kr*2+5+5+3);
  300. nvgCircle(vg, x+(int)(pos*w),cy, kr);
  301. nvgPathWinding(vg, NVG_HOLE);
  302. nvgFillPaint(vg, bg);
  303. nvgFill(vg);
  304. // Knob
  305. knob = nvgLinearGradient(vg, x,cy-kr,x,cy+kr, nvgRGBA(255,255,255,16), nvgRGBA(0,0,0,16));
  306. nvgBeginPath(vg);
  307. nvgCircle(vg, x+(int)(pos*w),cy, kr-1);
  308. nvgFillColor(vg, nvgRGBA(40,43,48,255));
  309. nvgFill(vg);
  310. nvgFillPaint(vg, knob);
  311. nvgFill(vg);
  312. nvgBeginPath(vg);
  313. nvgCircle(vg, x+(int)(pos*w),cy, kr-0.5f);
  314. nvgStrokeColor(vg, nvgRGBA(0,0,0,92));
  315. nvgStroke(vg);
  316. nvgRestore(vg);
  317. }
  318. void drawEyes(struct NVGcontext* vg, float x, float y, float w, float h, float mx, float my, float t)
  319. {
  320. struct NVGpaint gloss, bg;
  321. float ex = w *0.23f;
  322. float ey = h * 0.5f;
  323. float lx = x + ex;
  324. float ly = y + ey;
  325. float rx = x + w - ex;
  326. float ry = y + ey;
  327. float dx,dy,d;
  328. float br = (ex < ey ? ex : ey) * 0.5f;
  329. float blink = 1 - pow(sinf(t*0.5f),200)*0.8f;
  330. bg = nvgLinearGradient(vg, x,y+h*0.5f,x+w*0.1f,y+h, nvgRGBA(0,0,0,32), nvgRGBA(0,0,0,16));
  331. nvgBeginPath(vg);
  332. nvgEllipse(vg, lx+3.0f,ly+16.0f, ex,ey);
  333. nvgEllipse(vg, rx+3.0f,ry+16.0f, ex,ey);
  334. nvgFillPaint(vg, bg);
  335. nvgFill(vg);
  336. bg = nvgLinearGradient(vg, x,y+h*0.25f,x+w*0.1f,y+h, nvgRGBA(220,220,220,255), nvgRGBA(128,128,128,255));
  337. nvgBeginPath(vg);
  338. nvgEllipse(vg, lx,ly, ex,ey);
  339. nvgEllipse(vg, rx,ry, ex,ey);
  340. nvgFillPaint(vg, bg);
  341. nvgFill(vg);
  342. dx = (mx - rx) / (ex * 10);
  343. dy = (my - ry) / (ey * 10);
  344. d = sqrtf(dx*dx+dy*dy);
  345. if (d > 1.0f) {
  346. dx /= d; dy /= d;
  347. }
  348. dx *= ex*0.4f;
  349. dy *= ey*0.5f;
  350. nvgBeginPath(vg);
  351. nvgEllipse(vg, lx+dx,ly+dy+ey*0.25f*(1-blink), br,br*blink);
  352. nvgFillColor(vg, nvgRGBA(32,32,32,255));
  353. nvgFill(vg);
  354. dx = (mx - rx) / (ex * 10);
  355. dy = (my - ry) / (ey * 10);
  356. d = sqrtf(dx*dx+dy*dy);
  357. if (d > 1.0f) {
  358. dx /= d; dy /= d;
  359. }
  360. dx *= ex*0.4f;
  361. dy *= ey*0.5f;
  362. nvgBeginPath(vg);
  363. nvgEllipse(vg, rx+dx,ry+dy+ey*0.25f*(1-blink), br,br*blink);
  364. nvgFillColor(vg, nvgRGBA(32,32,32,255));
  365. nvgFill(vg);
  366. 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));
  367. nvgBeginPath(vg);
  368. nvgEllipse(vg, lx,ly, ex,ey);
  369. nvgFillPaint(vg, gloss);
  370. nvgFill(vg);
  371. 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));
  372. nvgBeginPath(vg);
  373. nvgEllipse(vg, rx,ry, ex,ey);
  374. nvgFillPaint(vg, gloss);
  375. nvgFill(vg);
  376. }
  377. void drawGraph(struct NVGcontext* vg, float x, float y, float w, float h, float t)
  378. {
  379. struct NVGpaint bg;
  380. float samples[6];
  381. float sx[6], sy[6];
  382. float dx = w/5.0f;
  383. int i;
  384. samples[0] = (1+sinf(t*1.2345f+cosf(t*0.33457f)*0.44f))*0.5f;
  385. samples[1] = (1+sinf(t*0.68363f+cosf(t*1.3f)*1.55f))*0.5f;
  386. samples[2] = (1+sinf(t*1.1642f+cosf(t*0.33457f)*1.24f))*0.5f;
  387. samples[3] = (1+sinf(t*0.56345f+cosf(t*1.63f)*0.14f))*0.5f;
  388. samples[4] = (1+sinf(t*1.6245f+cosf(t*0.254f)*0.3f))*0.5f;
  389. samples[5] = (1+sinf(t*0.345f+cosf(t*0.03f)*0.6f))*0.5f;
  390. for (i = 0; i < 6; i++) {
  391. sx[i] = x+i*dx;
  392. sy[i] = y+h*samples[i]*0.8f;
  393. }
  394. // Graph background
  395. bg = nvgLinearGradient(vg, x,y,x,y+h, nvgRGBA(0,160,192,0), nvgRGBA(0,160,192,64));
  396. nvgBeginPath(vg);
  397. nvgMoveTo(vg, sx[0], sy[0]);
  398. for (i = 1; i < 6; i++)
  399. nvgBezierTo(vg, sx[i-1]+dx*0.5f,sy[i-1], sx[i]-dx*0.5f,sy[i], sx[i],sy[i]);
  400. nvgLineTo(vg, x+w, y+h);
  401. nvgLineTo(vg, x, y+h);
  402. nvgFillPaint(vg, bg);
  403. nvgFill(vg);
  404. // Graph line
  405. nvgBeginPath(vg);
  406. nvgMoveTo(vg, sx[0], sy[0]+2);
  407. for (i = 1; i < 6; i++)
  408. 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);
  409. nvgStrokeColor(vg, nvgRGBA(0,0,0,32));
  410. nvgStrokeWidth(vg, 3.0f);
  411. nvgStroke(vg);
  412. nvgBeginPath(vg);
  413. nvgMoveTo(vg, sx[0], sy[0]);
  414. for (i = 1; i < 6; i++)
  415. nvgBezierTo(vg, sx[i-1]+dx*0.5f,sy[i-1], sx[i]-dx*0.5f,sy[i], sx[i],sy[i]);
  416. nvgStrokeColor(vg, nvgRGBA(0,160,192,255));
  417. nvgStrokeWidth(vg, 3.0f);
  418. nvgStroke(vg);
  419. // Graph sample pos
  420. for (i = 0; i < 6; i++) {
  421. bg = nvgRadialGradient(vg, sx[i],sy[i]+2, 3.0f,8.0f, nvgRGBA(0,0,0,32), nvgRGBA(0,0,0,0));
  422. nvgBeginPath(vg);
  423. nvgRect(vg, sx[i]-10, sy[i]-10+2, 20,20);
  424. nvgFillPaint(vg, bg);
  425. nvgFill(vg);
  426. }
  427. nvgBeginPath(vg);
  428. for (i = 0; i < 6; i++)
  429. nvgCircle(vg, sx[i], sy[i], 4.0f);
  430. nvgFillColor(vg, nvgRGBA(0,160,192,255));
  431. nvgFill(vg);
  432. nvgBeginPath(vg);
  433. for (i = 0; i < 6; i++)
  434. nvgCircle(vg, sx[i], sy[i], 2.0f);
  435. nvgFillColor(vg, nvgRGBA(220,220,220,255));
  436. nvgFill(vg);
  437. nvgStrokeWidth(vg, 1.0f);
  438. }
  439. void drawThumbnails(struct NVGcontext* vg, float x, float y, float w, float h, const int* images, int nimages, float t)
  440. {
  441. float cornerRadius = 3.0f;
  442. struct NVGpaint shadowPaint, imgPaint, fadePaint;
  443. float ix,iy,iw,ih;
  444. float thumb = 60.0f;
  445. float arry = 30.5f;
  446. int imgw, imgh;
  447. float stackh = (nimages/2) * (thumb+10) + 10;
  448. int i;
  449. float u = (1+cosf(t*0.5f))*0.5f;
  450. float scrollh;
  451. nvgSave(vg);
  452. // nvgClearState(vg);
  453. // Drop shadow
  454. shadowPaint = nvgBoxGradient(vg, x,y+4, w,h, cornerRadius*2, 20, nvgRGBA(0,0,0,128), nvgRGBA(0,0,0,0));
  455. nvgBeginPath(vg);
  456. nvgRect(vg, x-10,y-10, w+20,h+30);
  457. nvgRoundedRect(vg, x,y, w,h, cornerRadius);
  458. nvgPathWinding(vg, NVG_HOLE);
  459. nvgFillPaint(vg, shadowPaint);
  460. nvgFill(vg);
  461. // Window
  462. nvgBeginPath(vg);
  463. nvgRoundedRect(vg, x,y, w,h, cornerRadius);
  464. nvgMoveTo(vg, x-10,y+arry);
  465. nvgLineTo(vg, x+1,y+arry-11);
  466. nvgLineTo(vg, x+1,y+arry+11);
  467. nvgFillColor(vg, nvgRGBA(200,200,200,255));
  468. nvgFill(vg);
  469. nvgSave(vg);
  470. nvgScissor(vg, x,y,w,h);
  471. nvgTranslate(vg, 0, -(stackh - h)*u);
  472. for (i = 0; i < nimages; i++) {
  473. float tx, ty;
  474. tx = x+10;
  475. ty = y+10;
  476. tx += (i%2) * (thumb+10);
  477. ty += (i/2) * (thumb+10);
  478. nvgImageSize(vg, images[i], &imgw, &imgh);
  479. if (imgw < imgh) {
  480. iw = thumb;
  481. ih = iw * (float)imgh/(float)imgw;
  482. ix = 0;
  483. iy = -(ih-thumb)*0.5f;
  484. } else {
  485. ih = thumb;
  486. iw = ih * (float)imgw/(float)imgh;
  487. ix = -(iw-thumb)*0.5f;
  488. iy = 0;
  489. }
  490. imgPaint = nvgImagePattern(vg, tx+ix, ty+iy, iw,ih, 0.0f/180.0f*NVG_PI, images[i], 0);
  491. nvgBeginPath(vg);
  492. nvgRoundedRect(vg, tx,ty, thumb,thumb, 5);
  493. nvgFillPaint(vg, imgPaint);
  494. nvgFill(vg);
  495. shadowPaint = nvgBoxGradient(vg, tx-1,ty, thumb+2,thumb+2, 5, 3, nvgRGBA(0,0,0,128), nvgRGBA(0,0,0,0));
  496. nvgBeginPath(vg);
  497. nvgRect(vg, tx-5,ty-5, thumb+10,thumb+10);
  498. nvgRoundedRect(vg, tx,ty, thumb,thumb, 6);
  499. nvgPathWinding(vg, NVG_HOLE);
  500. nvgFillPaint(vg, shadowPaint);
  501. nvgFill(vg);
  502. nvgBeginPath(vg);
  503. nvgRoundedRect(vg, tx+0.5f,ty+0.5f, thumb-1,thumb-1, 4-0.5f);
  504. nvgStrokeWidth(vg,1.0f);
  505. nvgStrokeColor(vg, nvgRGBA(255,255,255,192));
  506. nvgStroke(vg);
  507. }
  508. nvgRestore(vg);
  509. // Hide fades
  510. fadePaint = nvgLinearGradient(vg, x,y,x,y+6, nvgRGBA(200,200,200,255), nvgRGBA(200,200,200,0));
  511. nvgBeginPath(vg);
  512. nvgRect(vg, x+4,y,w-8,6);
  513. nvgFillPaint(vg, fadePaint);
  514. nvgFill(vg);
  515. fadePaint = nvgLinearGradient(vg, x,y+h,x,y+h-6, nvgRGBA(200,200,200,255), nvgRGBA(200,200,200,0));
  516. nvgBeginPath(vg);
  517. nvgRect(vg, x+4,y+h-6,w-8,6);
  518. nvgFillPaint(vg, fadePaint);
  519. nvgFill(vg);
  520. // Scroll bar
  521. 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));
  522. nvgBeginPath(vg);
  523. nvgRoundedRect(vg, x+w-12,y+4, 8,h-8, 3);
  524. nvgFillPaint(vg, shadowPaint);
  525. // nvgFillColor(vg, nvgRGBA(255,0,0,128));
  526. nvgFill(vg);
  527. scrollh = (h/stackh) * (h-8);
  528. 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));
  529. nvgBeginPath(vg);
  530. nvgRoundedRect(vg, x+w-12+1,y+4+1 + (h-8-scrollh)*u, 8-2,scrollh-2, 2);
  531. nvgFillPaint(vg, shadowPaint);
  532. // nvgFillColor(vg, nvgRGBA(0,0,0,128));
  533. nvgFill(vg);
  534. nvgRestore(vg);
  535. }
  536. void drawColorwheel(struct NVGcontext* vg, float x, float y, float w, float h, float t)
  537. {
  538. int i;
  539. float r0, r1, ax,ay, bx,by, cx,cy, aeps, r;
  540. float hue = sinf(t * 0.12f);
  541. struct NVGpaint paint;
  542. nvgSave(vg);
  543. /* nvgBeginPath(vg);
  544. nvgRect(vg, x,y,w,h);
  545. nvgFillColor(vg, nvgRGBA(255,0,0,128));
  546. nvgFill(vg);*/
  547. cx = x + w*0.5f;
  548. cy = y + h*0.5f;
  549. r1 = (w < h ? w : h) * 0.5f - 5.0f;
  550. r0 = r1 - 20.0f;
  551. aeps = 0.5f / r1; // half a pixel arc length in radians (2pi cancels out).
  552. for (i = 0; i < 6; i++)
  553. {
  554. float a0 = (float)i / 6.0f * NVG_PI * 2.0f - aeps;
  555. float a1 = (float)(i+1.0f) / 6.0f * NVG_PI * 2.0f + aeps;
  556. nvgBeginPath(vg);
  557. nvgArc(vg, cx,cy, r0, a0, a1, NVG_CW);
  558. nvgArc(vg, cx,cy, r1, a1, a0, NVG_CCW);
  559. nvgClosePath(vg);
  560. ax = cx + cosf(a0) * (r0+r1)*0.5f;
  561. ay = cy + sinf(a0) * (r0+r1)*0.5f;
  562. bx = cx + cosf(a1) * (r0+r1)*0.5f;
  563. by = cy + sinf(a1) * (r0+r1)*0.5f;
  564. 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));
  565. nvgFillPaint(vg, paint);
  566. nvgFill(vg);
  567. }
  568. nvgBeginPath(vg);
  569. nvgCircle(vg, cx,cy, r0-0.5f);
  570. nvgCircle(vg, cx,cy, r1+0.5f);
  571. nvgStrokeColor(vg, nvgRGBA(0,0,0,64));
  572. nvgStrokeWidth(vg, 1.0f);
  573. nvgStroke(vg);
  574. // Selector
  575. nvgSave(vg);
  576. nvgTranslate(vg, cx,cy);
  577. nvgRotate(vg, hue*NVG_PI*2);
  578. // Marker on
  579. nvgStrokeWidth(vg, 2.0f);
  580. nvgBeginPath(vg);
  581. nvgRect(vg, r0-1,-3,r1-r0+2,6);
  582. nvgStrokeColor(vg, nvgRGBA(255,255,255,192));
  583. nvgStroke(vg);
  584. paint = nvgBoxGradient(vg, r0-3,-5,r1-r0+6,10, 2,4, nvgRGBA(0,0,0,128), nvgRGBA(0,0,0,0));
  585. nvgBeginPath(vg);
  586. nvgRect(vg, r0-2-10,-4-10,r1-r0+4+20,8+20);
  587. nvgRect(vg, r0-2,-4,r1-r0+4,8);
  588. nvgPathWinding(vg, NVG_HOLE);
  589. nvgFillPaint(vg, paint);
  590. nvgFill(vg);
  591. // Center triangle
  592. r = r0 - 6;
  593. ax = cosf(120.0f/180.0f*NVG_PI) * r;
  594. ay = sinf(120.0f/180.0f*NVG_PI) * r;
  595. bx = cosf(-120.0f/180.0f*NVG_PI) * r;
  596. by = sinf(-120.0f/180.0f*NVG_PI) * r;
  597. nvgBeginPath(vg);
  598. nvgMoveTo(vg, r,0);
  599. nvgLineTo(vg, ax,ay);
  600. nvgLineTo(vg, bx,by);
  601. nvgClosePath(vg);
  602. paint = nvgLinearGradient(vg, r,0, ax,ay, nvgHSLA(hue,1.0f,0.5f,255), nvgRGBA(255,255,255,255));
  603. nvgFillPaint(vg, paint);
  604. nvgFill(vg);
  605. paint = nvgLinearGradient(vg, (r+ax)*0.5f,(0+ay)*0.5f, bx,by, nvgRGBA(0,0,0,0), nvgRGBA(0,0,0,255));
  606. nvgFillPaint(vg, paint);
  607. nvgFill(vg);
  608. nvgStrokeColor(vg, nvgRGBA(0,0,0,64));
  609. nvgStroke(vg);
  610. // Select circle on triangle
  611. ax = cosf(120.0f/180.0f*NVG_PI) * r*0.3f;
  612. ay = sinf(120.0f/180.0f*NVG_PI) * r*0.4f;
  613. nvgStrokeWidth(vg, 2.0f);
  614. nvgBeginPath(vg);
  615. nvgCircle(vg, ax,ay,5);
  616. nvgStrokeColor(vg, nvgRGBA(255,255,255,192));
  617. nvgStroke(vg);
  618. paint = nvgRadialGradient(vg, ax,ay, 7,9, nvgRGBA(0,0,0,64), nvgRGBA(0,0,0,0));
  619. nvgBeginPath(vg);
  620. nvgRect(vg, ax-20,ay-20,40,40);
  621. nvgCircle(vg, ax,ay,7);
  622. nvgPathWinding(vg, NVG_HOLE);
  623. nvgFillPaint(vg, paint);
  624. nvgFill(vg);
  625. nvgRestore(vg);
  626. nvgRestore(vg);
  627. }
  628. void drawLines(struct NVGcontext* vg, float x, float y, float w, float h, float t)
  629. {
  630. int i, j;
  631. float pad = 5.0f, s = w/9.0f - pad*2;
  632. float pts[4*2], fx, fy;
  633. int joins[3] = {NVG_MITER, NVG_ROUND, NVG_BEVEL};
  634. int caps[3] = {NVG_BUTT, NVG_ROUND, NVG_SQUARE};
  635. NVG_NOTUSED(h);
  636. nvgSave(vg);
  637. pts[0] = -s*0.25f + cosf(t*0.3f) * s*0.5f;
  638. pts[1] = sinf(t*0.3f) * s*0.5f;
  639. pts[2] = -s*0.25f;
  640. pts[3] = 0;
  641. pts[4] = s*0.25f;
  642. pts[5] = 0;
  643. pts[6] = s*0.25f + cosf(-t*0.3f) * s*0.5f;
  644. pts[7] = sinf(-t*0.3f) * s*0.5f;
  645. for (i = 0; i < 3; i++)
  646. {
  647. for (j = 0; j < 3; j++)
  648. {
  649. fx = x + s*0.5f + (i*3+j)/9.0f*w + pad;
  650. fy = y - s*0.5f + pad;
  651. nvgLineCap(vg, caps[i]);
  652. nvgLineJoin(vg, joins[j]);
  653. nvgStrokeWidth(vg, s*0.3f);
  654. nvgStrokeColor(vg, nvgRGBA(0,0,0,160));
  655. nvgBeginPath(vg);
  656. nvgMoveTo(vg, fx+pts[0], fy+pts[1]);
  657. nvgLineTo(vg, fx+pts[2], fy+pts[3]);
  658. nvgLineTo(vg, fx+pts[4], fy+pts[5]);
  659. nvgLineTo(vg, fx+pts[6], fy+pts[7]);
  660. nvgStroke(vg);
  661. nvgLineCap(vg, NVG_BUTT);
  662. nvgLineJoin(vg, NVG_BEVEL);
  663. nvgStrokeWidth(vg, 1.0f);
  664. nvgStrokeColor(vg, nvgRGBA(0,192,255,255));
  665. nvgBeginPath(vg);
  666. nvgMoveTo(vg, fx+pts[0], fy+pts[1]);
  667. nvgLineTo(vg, fx+pts[2], fy+pts[3]);
  668. nvgLineTo(vg, fx+pts[4], fy+pts[5]);
  669. nvgLineTo(vg, fx+pts[6], fy+pts[7]);
  670. nvgStroke(vg);
  671. }
  672. }
  673. nvgRestore(vg);
  674. }
  675. void drawBlendish(struct NVGcontext* _vg, float _x, float _y, float _w, float _h, float _t)
  676. {
  677. float x = _x;
  678. float y = _y;
  679. bndBackground(_vg, _x - 10.0f, _y - 10.0f, _w, _h);
  680. bndToolButton(_vg, x, y, 120.0f, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_DEFAULT, BND_ICONID(6, 3), "Default");
  681. y += 25.0f;
  682. bndToolButton(_vg, x, y, 120.0f, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_HOVER, BND_ICONID(6, 3), "Hovered");
  683. y += 25.0f;
  684. bndToolButton(_vg, x, y, 120.0f, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_ACTIVE, BND_ICONID(6, 3), "Active");
  685. y += 40.0f;
  686. bndRadioButton(_vg, x, y, 80.0f, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_DEFAULT, -1, "Default");
  687. y += 25.0f;
  688. bndRadioButton(_vg, x, y, 80.0f, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_HOVER, -1, "Hovered");
  689. y += 25.0f;
  690. bndRadioButton(_vg, x, y, 80.0f, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_ACTIVE, -1, "Active");
  691. y += 25.0f;
  692. bndLabel(_vg, x, y, 120.0f, BND_WIDGET_HEIGHT, -1, "Label:");
  693. y += BND_WIDGET_HEIGHT;
  694. bndChoiceButton(_vg, x, y, 80.0f, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_DEFAULT, -1, "Default");
  695. y += 25.0f;
  696. bndChoiceButton(_vg, x, y, 80.0f, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_HOVER, -1, "Hovered");
  697. y += 25.0f;
  698. bndChoiceButton(_vg, x, y, 80.0f, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_ACTIVE, -1, "Active");
  699. y += 25.0f;
  700. float ry = y;
  701. float rx = x;
  702. y = _y;
  703. x += 130.0f;
  704. bndOptionButton(_vg, x, y, 120.0f, BND_WIDGET_HEIGHT, BND_DEFAULT, "Default");
  705. y += 25.0f;
  706. bndOptionButton(_vg, x, y, 120.0f, BND_WIDGET_HEIGHT, BND_HOVER, "Hovered");
  707. y += 25.0f;
  708. bndOptionButton(_vg, x, y, 120.0f, BND_WIDGET_HEIGHT, BND_ACTIVE, "Active");
  709. y += 40.0f;
  710. bndNumberField(_vg, x, y, 120.0f, BND_WIDGET_HEIGHT, BND_CORNER_DOWN, BND_DEFAULT, "Top", "100");
  711. y += BND_WIDGET_HEIGHT - 2.0f;
  712. bndNumberField(_vg, x, y, 120.0f, BND_WIDGET_HEIGHT, BND_CORNER_ALL, BND_DEFAULT, "Center", "100");
  713. y += BND_WIDGET_HEIGHT - 2.0f;
  714. bndNumberField(_vg, x, y, 120.0f, BND_WIDGET_HEIGHT, BND_CORNER_TOP, BND_DEFAULT, "Bottom", "100");
  715. float mx = x - 30.0f;
  716. float my = y - 12.0f;
  717. float mw = 120.0f;
  718. bndMenuBackground(_vg, mx, my, mw, 120.0f, BND_CORNER_TOP);
  719. bndMenuLabel(_vg, mx, my, mw, BND_WIDGET_HEIGHT, -1, "Menu Title");
  720. my += BND_WIDGET_HEIGHT - 2.0f;
  721. bndMenuItem(_vg, mx, my, mw, BND_WIDGET_HEIGHT, BND_DEFAULT, BND_ICONID(17, 3), "Default");
  722. my += BND_WIDGET_HEIGHT - 2.0f;
  723. bndMenuItem(_vg, mx, my, mw, BND_WIDGET_HEIGHT, BND_HOVER, BND_ICONID(18, 3), "Hovered");
  724. my += BND_WIDGET_HEIGHT - 2.0f;
  725. bndMenuItem(_vg, mx, my, mw, BND_WIDGET_HEIGHT, BND_ACTIVE, BND_ICONID(19, 3), "Active");
  726. y = _y;
  727. x += 130.0f;
  728. float ox = x;
  729. bndNumberField(_vg, x, y, 120.0f, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_DEFAULT, "Default", "100");
  730. y += 25.0f;
  731. bndNumberField(_vg, x, y, 120.0f, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_HOVER, "Hovered", "100");
  732. y += 25.0f;
  733. bndNumberField(_vg, x, y, 120.0f, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_ACTIVE, "Active", "100");
  734. y += 40.0f;
  735. bndRadioButton(_vg, x, y, 60.0f, BND_WIDGET_HEIGHT, BND_CORNER_RIGHT, BND_DEFAULT, -1, "One");
  736. x += 60.0f - 1.0f;
  737. bndRadioButton(_vg, x, y, 60.0f, BND_WIDGET_HEIGHT, BND_CORNER_ALL, BND_DEFAULT, -1, "Two");
  738. x += 60.0f - 1.0f;
  739. bndRadioButton(_vg, x, y, 60.0f, BND_WIDGET_HEIGHT, BND_CORNER_ALL, BND_DEFAULT, -1, "Three");
  740. x += 60.0f - 1.0f;
  741. bndRadioButton(_vg, x, y, 60.0f, BND_WIDGET_HEIGHT, BND_CORNER_LEFT, BND_ACTIVE, -1, "Butts");
  742. x = ox;
  743. y += 40.0f;
  744. float progress_value = fmodf(_t / 10.0f, 1.0f);
  745. char progress_label[32];
  746. bx::snprintf(progress_label, BX_COUNTOF(progress_label), "%d%%", int(progress_value * 100 + 0.5f) );
  747. bndSlider(_vg, x, y, 240, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_DEFAULT, progress_value, "Default", progress_label);
  748. y += 25.0f;
  749. bndSlider(_vg, x, y, 240, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_HOVER, progress_value, "Hovered", progress_label);
  750. y += 25.0f;
  751. bndSlider(_vg, x, y, 240, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_ACTIVE, progress_value, "Active", progress_label);
  752. float rw = x + 240.0f - rx;
  753. float s_offset = sinf(_t / 2.0f) * 0.5f + 0.5f;
  754. float s_size = cosf(_t / 3.11f) * 0.5f + 0.5f;
  755. bndScrollBar(_vg, rx, ry, rw, BND_SCROLLBAR_HEIGHT, BND_DEFAULT, s_offset, s_size);
  756. ry += 20.0f;
  757. bndScrollBar(_vg, rx, ry, rw, BND_SCROLLBAR_HEIGHT, BND_HOVER, s_offset, s_size);
  758. ry += 20.0f;
  759. bndScrollBar(_vg, rx, ry, rw, BND_SCROLLBAR_HEIGHT, BND_ACTIVE, s_offset, s_size);
  760. const char edit_text[] = "The quick brown fox";
  761. int textlen = int(strlen(edit_text) + 1);
  762. int t = int(_t * 2);
  763. int idx1 = (t / textlen) % textlen;
  764. int idx2 = idx1 + (t % (textlen - idx1) );
  765. ry += 25.0f;
  766. bndTextField(_vg, rx, ry, 240.0f, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_DEFAULT, -1, edit_text, idx1, idx2);
  767. ry += 25.0f;
  768. bndTextField(_vg, rx, ry, 240.0f, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_HOVER, -1, edit_text, idx1, idx2);
  769. ry += 25.0f;
  770. bndTextField(_vg, rx, ry, 240.0f, BND_WIDGET_HEIGHT, BND_CORNER_NONE, BND_ACTIVE, -1, edit_text, idx1, idx2);
  771. rx += rw + 20.0f;
  772. ry = _y;
  773. bndScrollBar(_vg, rx, ry, BND_SCROLLBAR_WIDTH, 240.0f, BND_DEFAULT, s_offset, s_size);
  774. rx += 20.0f;
  775. bndScrollBar(_vg, rx, ry, BND_SCROLLBAR_WIDTH, 240.0f, BND_HOVER, s_offset, s_size);
  776. rx += 20.0f;
  777. bndScrollBar(_vg, rx, ry, BND_SCROLLBAR_WIDTH, 240.0f, BND_ACTIVE, s_offset, s_size);
  778. x = ox;
  779. y += 40.0f;
  780. bndToolButton(_vg, x, y, BND_TOOL_WIDTH, BND_WIDGET_HEIGHT, BND_CORNER_RIGHT, BND_DEFAULT, BND_ICONID(0, 10), NULL);
  781. x += BND_TOOL_WIDTH - 1;
  782. bndToolButton(_vg, x, y, BND_TOOL_WIDTH, BND_WIDGET_HEIGHT, BND_CORNER_ALL, BND_DEFAULT, BND_ICONID(1, 10), NULL);
  783. x += BND_TOOL_WIDTH - 1;
  784. bndToolButton(_vg, x, y, BND_TOOL_WIDTH, BND_WIDGET_HEIGHT, BND_CORNER_ALL, BND_DEFAULT, BND_ICONID(2, 10), NULL);
  785. x += BND_TOOL_WIDTH - 1;
  786. bndToolButton(_vg, x, y, BND_TOOL_WIDTH, BND_WIDGET_HEIGHT, BND_CORNER_ALL, BND_DEFAULT, BND_ICONID(3, 10), NULL);
  787. x += BND_TOOL_WIDTH - 1;
  788. bndToolButton(_vg, x, y, BND_TOOL_WIDTH, BND_WIDGET_HEIGHT, BND_CORNER_ALL, BND_DEFAULT, BND_ICONID(4, 10), NULL);
  789. x += BND_TOOL_WIDTH - 1;
  790. bndToolButton(_vg, x, y, BND_TOOL_WIDTH, BND_WIDGET_HEIGHT, BND_CORNER_LEFT, BND_DEFAULT, BND_ICONID(5, 10), NULL);
  791. x += BND_TOOL_WIDTH - 1;
  792. x += 5.0f;
  793. bndRadioButton(_vg, x, y, BND_TOOL_WIDTH, BND_WIDGET_HEIGHT, BND_CORNER_RIGHT, BND_DEFAULT, BND_ICONID(0, 11), NULL);
  794. x += BND_TOOL_WIDTH - 1;
  795. bndRadioButton(_vg, x, y, BND_TOOL_WIDTH, BND_WIDGET_HEIGHT, BND_CORNER_ALL, BND_DEFAULT, BND_ICONID(1, 11), NULL);
  796. x += BND_TOOL_WIDTH - 1;
  797. bndRadioButton(_vg, x, y, BND_TOOL_WIDTH, BND_WIDGET_HEIGHT, BND_CORNER_ALL, BND_DEFAULT, BND_ICONID(2, 11), NULL);
  798. x += BND_TOOL_WIDTH - 1;
  799. bndRadioButton(_vg, x, y, BND_TOOL_WIDTH, BND_WIDGET_HEIGHT, BND_CORNER_ALL, BND_DEFAULT, BND_ICONID(3, 11), NULL);
  800. x += BND_TOOL_WIDTH - 1;
  801. bndRadioButton(_vg, x, y, BND_TOOL_WIDTH, BND_WIDGET_HEIGHT, BND_CORNER_ALL, BND_ACTIVE, BND_ICONID(4, 11), NULL);
  802. x += BND_TOOL_WIDTH - 1;
  803. bndRadioButton(_vg, x, y, BND_TOOL_WIDTH, BND_WIDGET_HEIGHT, BND_CORNER_LEFT, BND_DEFAULT, BND_ICONID(5, 11), NULL);
  804. }
  805. struct DemoData
  806. {
  807. int fontNormal, fontBold, fontIcons;
  808. int images[12];
  809. };
  810. int loadDemoData(struct NVGcontext* vg, struct DemoData* data)
  811. {
  812. for (uint32_t ii = 0; ii < 12; ++ii)
  813. {
  814. char file[128];
  815. bx::snprintf(file, 128, "images/image%d.jpg", ii+1);
  816. data->images[ii] = nvgCreateImage(vg, file, 0);
  817. if (data->images[ii] == bgfx::invalidHandle)
  818. {
  819. printf("Could not load %s.\n", file);
  820. return -1;
  821. }
  822. }
  823. data->fontIcons = nvgCreateFont(vg, "icons", "font/entypo.ttf");
  824. if (data->fontIcons == -1)
  825. {
  826. printf("Could not add font icons.\n");
  827. return -1;
  828. }
  829. data->fontNormal = nvgCreateFont(vg, "sans", "font/roboto-regular.ttf");
  830. if (data->fontNormal == -1)
  831. {
  832. printf("Could not add font italic.\n");
  833. return -1;
  834. }
  835. data->fontBold = nvgCreateFont(vg, "sans-bold", "font/roboto-bold.ttf");
  836. if (data->fontBold == -1)
  837. {
  838. printf("Could not add font bold.\n");
  839. return -1;
  840. }
  841. return 0;
  842. }
  843. void freeDemoData(struct NVGcontext* vg, struct DemoData* data)
  844. {
  845. int i;
  846. if (vg == NULL)
  847. return;
  848. for (i = 0; i < 12; i++)
  849. nvgDeleteImage(vg, data->images[i]);
  850. }
  851. #if defined(_MSC_VER) && (_MSC_VER < 1800)
  852. inline float round(float _f)
  853. {
  854. return float(int(_f) );
  855. }
  856. #endif
  857. void drawParagraph(struct NVGcontext* vg, float x, float y, float width, float height, float mx, float my)
  858. {
  859. struct NVGtextRow rows[3];
  860. struct NVGglyphPosition glyphs[100];
  861. 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.";
  862. const char* start;
  863. const char* end;
  864. int nrows, i, nglyphs, j, lnum = 0;
  865. float lineh;
  866. float caretx, px;
  867. float bounds[4];
  868. float gx = 0.0f, gy = 0.0f;
  869. int gutter = 0;
  870. NVG_NOTUSED(height);
  871. nvgSave(vg);
  872. nvgFontSize(vg, 18.0f);
  873. nvgFontFace(vg, "sans");
  874. nvgTextAlign(vg, NVG_ALIGN_LEFT|NVG_ALIGN_TOP);
  875. nvgTextMetrics(vg, NULL, NULL, &lineh);
  876. // The text break API can be used to fill a large buffer of rows,
  877. // or to iterate over the text just few lines (or just one) at a time.
  878. // The "next" variable of the last returned item tells where to continue.
  879. start = text;
  880. end = text + strlen(text);
  881. for (nrows = nvgTextBreakLines(vg, start, end, width, rows, 3); 0 != nrows; nrows = nvgTextBreakLines(vg, start, end, width, rows, 3) )
  882. {
  883. for (i = 0; i < nrows; i++) {
  884. struct NVGtextRow* row = &rows[i];
  885. int hit = mx > x && mx < (x+width) && my >= y && my < (y+lineh);
  886. nvgBeginPath(vg);
  887. nvgFillColor(vg, nvgRGBA(255,255,255,hit?64:8));
  888. nvgRect(vg, x, y, row->width, lineh);
  889. nvgFill(vg);
  890. nvgFillColor(vg, nvgRGBA(255,255,255,255));
  891. nvgText(vg, x, y, row->start, row->end);
  892. if (hit) {
  893. caretx = (mx < x+row->width/2) ? x : x+row->width;
  894. px = x;
  895. nglyphs = nvgTextGlyphPositions(vg, x, y, row->start, row->end, glyphs, 100);
  896. for (j = 0; j < nglyphs; j++) {
  897. float x0 = glyphs[j].x;
  898. float x1 = (j+1 < nglyphs) ? glyphs[j+1].x : x+row->width;
  899. float gx = x0 * 0.3f + x1 * 0.7f;
  900. if (mx >= px && mx < gx)
  901. caretx = glyphs[j].x;
  902. px = gx;
  903. }
  904. nvgBeginPath(vg);
  905. nvgFillColor(vg, nvgRGBA(255,192,0,255));
  906. nvgRect(vg, caretx, y, 1, lineh);
  907. nvgFill(vg);
  908. gutter = lnum+1;
  909. gx = x - 10;
  910. gy = y + lineh/2;
  911. }
  912. lnum++;
  913. y += lineh;
  914. }
  915. // Keep going...
  916. start = rows[nrows-1].next;
  917. }
  918. if (gutter)
  919. {
  920. char txt[16];
  921. bx::snprintf(txt, sizeof(txt), "%d", gutter);
  922. nvgFontSize(vg, 13.0f);
  923. nvgTextAlign(vg, NVG_ALIGN_RIGHT|NVG_ALIGN_MIDDLE);
  924. nvgTextBounds(vg, gx,gy, txt, NULL, bounds);
  925. nvgBeginPath(vg);
  926. nvgFillColor(vg, nvgRGBA(255,192,0,255));
  927. nvgRoundedRect(vg
  928. , round(bounds[0])-4.0f
  929. , round(bounds[1])-2.0f
  930. , round(bounds[2]-bounds[0])+8.0f
  931. , round(bounds[3]-bounds[1])+4.0f
  932. , (round(bounds[3]-bounds[1])+4.0f)/2.0f-1.0f
  933. );
  934. nvgFill(vg);
  935. nvgFillColor(vg, nvgRGBA(32,32,32,255));
  936. nvgText(vg, gx,gy, txt, NULL);
  937. }
  938. y += 20.0f;
  939. nvgFontSize(vg, 13.0f);
  940. nvgTextAlign(vg, NVG_ALIGN_LEFT|NVG_ALIGN_TOP);
  941. nvgTextLineHeight(vg, 1.2f);
  942. nvgTextBoxBounds(vg, x,y, 150, "Hover your mouse over the text to see calculated caret position.", NULL, bounds);
  943. nvgBeginPath(vg);
  944. nvgFillColor(vg, nvgRGBA(220,220,220,255));
  945. nvgRoundedRect(vg
  946. , round(bounds[0]-2.0f)
  947. , round(bounds[1]-2.0f)
  948. , round(bounds[2]-bounds[0])+4.0f
  949. , round(bounds[3]-bounds[1])+4.0f
  950. , 3.0f
  951. );
  952. px = float( (int)((bounds[2]+bounds[0])/2) );
  953. nvgMoveTo(vg, px,bounds[1] - 10);
  954. nvgLineTo(vg, px+7,bounds[1]+1);
  955. nvgLineTo(vg, px-7,bounds[1]+1);
  956. nvgFill(vg);
  957. nvgFillColor(vg, nvgRGBA(0,0,0,220));
  958. nvgTextBox(vg, x,y, 150, "Hover your mouse over the text to see calculated caret position.", NULL);
  959. nvgRestore(vg);
  960. }
  961. void drawWidths(struct NVGcontext* vg, float x, float y, float width)
  962. {
  963. nvgSave(vg);
  964. nvgStrokeColor(vg, nvgRGBA(0,0,0,255));
  965. for (uint32_t ii = 0; ii < 20; ++ii)
  966. {
  967. float w = (ii+0.5f)*0.1f;
  968. nvgStrokeWidth(vg, w);
  969. nvgBeginPath(vg);
  970. nvgMoveTo(vg, x,y);
  971. nvgLineTo(vg, x+width,y+width*0.3f);
  972. nvgStroke(vg);
  973. y += 10;
  974. }
  975. nvgRestore(vg);
  976. }
  977. void renderDemo(struct NVGcontext* vg, float mx, float my, float width, float height, float t, int blowup, struct DemoData* data)
  978. {
  979. float x,y,popx,popy;
  980. drawEyes(vg, width-800, height-240, 150, 100, mx, my, t);
  981. drawParagraph(vg, width - 550, 35, 150, 100, mx, my);
  982. drawGraph(vg, 0, height/2, width, height/2, t);
  983. drawColorwheel(vg, width-350, 35, 250.0f, 250.0f, t);
  984. // Line joints
  985. drawLines(vg, 50, height-50, 600, 35, t);
  986. nvgSave(vg);
  987. if (blowup)
  988. {
  989. nvgRotate(vg, sinf(t*0.3f)*5.0f/180.0f*NVG_PI);
  990. nvgScale(vg, 2.0f, 2.0f);
  991. }
  992. // Line width.
  993. drawWidths(vg, width-50, 35, 30);
  994. // Widgets.
  995. x = width-520; y = height-420;
  996. drawWindow(vg, "Widgets `n Stuff", x, y, 300, 400);
  997. x += 10;
  998. y += 45;
  999. drawSearchBox(vg, "Search", x,y,280,25);
  1000. y += 40;
  1001. drawDropDown(vg, "Effects", x,y,280,28);
  1002. popx = x + 300;
  1003. popy = y + 14;
  1004. y += 45;
  1005. // Form
  1006. drawLabel(vg, "Login", x,y, 280,20);
  1007. y += 25;
  1008. drawEditBox(vg, "Email", x,y, 280,28);
  1009. y += 35;
  1010. drawEditBox(vg, "Password", x,y, 280,28);
  1011. y += 38;
  1012. drawCheckBox(vg, "Remember me", x,y, 140,28);
  1013. drawButton(vg, ICON_LOGIN, "Sign in", x+138, y, 140, 28, nvgRGBA(0,96,128,255));
  1014. y += 45;
  1015. // Slider
  1016. drawLabel(vg, "Diameter", x,y, 280,20);
  1017. y += 25;
  1018. drawEditBoxNum(vg, "123.00", "px", x+180,y, 100,28);
  1019. drawSlider(vg, 0.4f, x,y, 170,28);
  1020. y += 55;
  1021. drawButton(vg, ICON_TRASH, "Delete", x, y, 160, 28, nvgRGBA(128,16,8,255));
  1022. drawButton(vg, 0, "Cancel", x+170, y, 110, 28, nvgRGBA(0,0,0,0));
  1023. // Thumbnails box
  1024. drawThumbnails(vg, popx, popy-30, 160, 300, data->images, 12, t);
  1025. // Blendish
  1026. drawBlendish(vg, 10, 62, 600.0f, 420.0f, t);
  1027. nvgRestore(vg);
  1028. }
  1029. int _main_(int /*_argc*/, char** /*_argv*/)
  1030. {
  1031. uint32_t width = 1280;
  1032. uint32_t height = 720;
  1033. uint32_t debug = BGFX_DEBUG_TEXT;
  1034. uint32_t reset = BGFX_RESET_VSYNC;
  1035. bgfx::init();
  1036. bgfx::reset(width, height, reset);
  1037. // Enable debug text.
  1038. bgfx::setDebug(debug);
  1039. // Set view 0 clear state.
  1040. bgfx::setViewClear(0
  1041. , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT
  1042. , 0x303030ff
  1043. , 1.0f
  1044. , 0
  1045. );
  1046. NVGcontext* nvg = nvgCreate(1, 0);
  1047. bgfx::setViewSeq(0, true);
  1048. DemoData data;
  1049. loadDemoData(nvg, &data);
  1050. bndSetFont(nvgCreateFont(nvg, "droidsans", "font/droidsans.ttf") );
  1051. bndSetIconImage(nvgCreateImage(nvg, "images/blender_icons16.png", 0) );
  1052. int64_t timeOffset = bx::getHPCounter();
  1053. entry::MouseState mouseState;
  1054. while (!entry::processEvents(width, height, debug, reset, &mouseState) )
  1055. {
  1056. int64_t now = bx::getHPCounter();
  1057. const double freq = double(bx::getHPFrequency() );
  1058. float time = (float)( (now-timeOffset)/freq);
  1059. // Set view 0 default viewport.
  1060. bgfx::setViewRect(0, 0, 0, width, height);
  1061. // This dummy draw call is here to make sure that view 0 is cleared
  1062. // if no other draw calls are submitted to view 0.
  1063. bgfx::submit(0);
  1064. // Use debug font to print information about this example.
  1065. bgfx::dbgTextClear();
  1066. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/20-nanovg");
  1067. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: NanoVG is small antialiased vector graphics rendering library.");
  1068. nvgBeginFrame(nvg, width, height, 1.0f);
  1069. renderDemo(nvg, float(mouseState.m_mx), float(mouseState.m_my), float(width), float(height), time, 0, &data);
  1070. nvgEndFrame(nvg);
  1071. // Advance to next frame. Rendering thread will be kicked to
  1072. // process submitted rendering primitives.
  1073. bgfx::frame();
  1074. }
  1075. freeDemoData(nvg, &data);
  1076. nvgDelete(nvg);
  1077. // Shutdown bgfx.
  1078. bgfx::shutdown();
  1079. return 0;
  1080. }