You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

856 lines
23KB

  1. #include "demo.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #ifdef NANOVG_GLEW
  6. # include <GL/glew.h>
  7. #endif
  8. #include <GLFW/glfw3.h>
  9. #include "nanovg.h"
  10. #ifdef _MSC_VER
  11. #define snprintf _snprintf
  12. #else
  13. #include <iconv.h>
  14. #endif
  15. #define ICON_SEARCH 0x1F50D
  16. #define ICON_CIRCLED_CROSS 0x2716
  17. #define ICON_CHEVRON_RIGHT 0xE75E
  18. #define ICON_CHECK 0x2713
  19. #define ICON_LOGIN 0xE740
  20. #define ICON_TRASH 0xE729
  21. static char* cpToUTF8(int cp, char* str)
  22. {
  23. int n = 0;
  24. if (cp < 0x80) n = 1;
  25. else if (cp < 0x800) n = 2;
  26. else if (cp < 0x10000) n = 3;
  27. else if (cp < 0x200000) n = 4;
  28. else if (cp < 0x4000000) n = 5;
  29. else if (cp <= 0x7fffffff) n = 6;
  30. str[n] = '\0';
  31. switch (n) {
  32. case 6: str[5] = 0x80 | (cp & 0x3f); cp = cp >> 6; cp |= 0x4000000;
  33. case 5: str[4] = 0x80 | (cp & 0x3f); cp = cp >> 6; cp |= 0x200000;
  34. case 4: str[3] = 0x80 | (cp & 0x3f); cp = cp >> 6; cp |= 0x10000;
  35. case 3: str[2] = 0x80 | (cp & 0x3f); cp = cp >> 6; cp |= 0x800;
  36. case 2: str[1] = 0x80 | (cp & 0x3f); cp = cp >> 6; cp |= 0xc0;
  37. case 1: str[0] = cp;
  38. }
  39. return str;
  40. }
  41. void drawWindow(struct NVGcontext* vg, const char* title, float x, float y, float w, float h)
  42. {
  43. float cornerRadius = 3.0f;
  44. struct NVGpaint shadowPaint;
  45. struct NVGpaint headerPaint;
  46. nvgSave(vg);
  47. // nvgClearState(vg);
  48. // Window
  49. nvgBeginPath(vg);
  50. nvgRoundedRect(vg, x,y, w,h, cornerRadius);
  51. nvgFillColor(vg, nvgRGBA(28,30,34,192));
  52. // nvgFillColor(vg, nvgRGBA(0,0,0,128));
  53. nvgFill(vg);
  54. // Drop shadow
  55. shadowPaint = nvgBoxGradient(vg, x,y+2, w,h, cornerRadius*2, 10, nvgRGBA(0,0,0,128), nvgRGBA(0,0,0,0));
  56. nvgBeginPath(vg);
  57. nvgRect(vg, x-10,y-10, w+20,h+30);
  58. nvgRoundedRect(vg, x,y, w,h, cornerRadius);
  59. nvgPathWinding(vg, NVG_HOLE);
  60. nvgFillPaint(vg, shadowPaint);
  61. nvgFill(vg);
  62. // Header
  63. headerPaint = nvgLinearGradient(vg, x,y,x,y+15, nvgRGBA(255,255,255,8), nvgRGBA(0,0,0,16));
  64. nvgBeginPath(vg);
  65. nvgRoundedRect(vg, x+1,y+1, w-2,30, cornerRadius-1);
  66. nvgFillPaint(vg, headerPaint);
  67. nvgFill(vg);
  68. nvgBeginPath(vg);
  69. nvgMoveTo(vg, x+0.5f, y+0.5f+30);
  70. nvgLineTo(vg, x+0.5f+w-1, y+0.5f+30);
  71. nvgStrokeColor(vg, nvgRGBA(0,0,0,32));
  72. nvgStroke(vg);
  73. nvgFontSize(vg, 18.0f);
  74. nvgFontFace(vg, "sans-bold");
  75. nvgTextAlign(vg,NVG_ALIGN_CENTER|NVG_ALIGN_MIDDLE);
  76. nvgFontBlur(vg,2);
  77. nvgFillColor(vg, nvgRGBA(0,0,0,128));
  78. nvgText(vg, x+w/2,y+16+1, title, NULL);
  79. nvgFontBlur(vg,0);
  80. nvgFillColor(vg, nvgRGBA(220,220,220,160));
  81. nvgText(vg, x+w/2,y+16, title, NULL);
  82. nvgRestore(vg);
  83. }
  84. void drawSearchBox(struct NVGcontext* vg, const char* text, float x, float y, float w, float h)
  85. {
  86. struct NVGpaint bg;
  87. char icon[8];
  88. float cornerRadius = h/2-1;
  89. // Edit
  90. bg = nvgBoxGradient(vg, x,y+1.5f, w,h, h/2,5, nvgRGBA(0,0,0,16), nvgRGBA(0,0,0,92));
  91. nvgBeginPath(vg);
  92. nvgRoundedRect(vg, x,y, w,h, cornerRadius);
  93. nvgFillPaint(vg, bg);
  94. nvgFill(vg);
  95. /* nvgBeginPath(vg);
  96. nvgRoundedRect(vg, x+0.5f,y+0.5f, w-1,h-1, cornerRadius-0.5f);
  97. nvgStrokeColor(vg, nvgRGBA(0,0,0,48));
  98. nvgStroke(vg);*/
  99. nvgFontSize(vg, h*1.3f);
  100. nvgFontFace(vg, "icons");
  101. nvgFillColor(vg, nvgRGBA(255,255,255,64));
  102. nvgTextAlign(vg,NVG_ALIGN_CENTER|NVG_ALIGN_MIDDLE);
  103. nvgText(vg, x+h*0.55f, y+h*0.55f, cpToUTF8(ICON_SEARCH,icon), NULL);
  104. nvgFontSize(vg, 20.0f);
  105. nvgFontFace(vg, "sans");
  106. nvgFillColor(vg, nvgRGBA(255,255,255,32));
  107. nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
  108. nvgText(vg, x+h*1.05f,y+h*0.5f,text, NULL);
  109. nvgFontSize(vg, h*1.3f);
  110. nvgFontFace(vg, "icons");
  111. nvgFillColor(vg, nvgRGBA(255,255,255,32));
  112. nvgTextAlign(vg,NVG_ALIGN_CENTER|NVG_ALIGN_MIDDLE);
  113. nvgText(vg, x+w-h*0.55f, y+h*0.55f, cpToUTF8(ICON_CIRCLED_CROSS,icon), NULL);
  114. }
  115. void drawDropDown(struct NVGcontext* vg, const char* text, float x, float y, float w, float h)
  116. {
  117. struct NVGpaint bg;
  118. char icon[8];
  119. float cornerRadius = 4.0f;
  120. bg = nvgLinearGradient(vg, x,y,x,y+h, nvgRGBA(255,255,255,16), nvgRGBA(0,0,0,16));
  121. nvgBeginPath(vg);
  122. nvgRoundedRect(vg, x+1,y+1, w-2,h-2, cornerRadius-1);
  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, 20.0f);
  130. nvgFontFace(vg, "sans");
  131. nvgFillColor(vg, nvgRGBA(255,255,255,160));
  132. nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
  133. nvgText(vg, x+h*0.3f,y+h*0.5f,text, NULL);
  134. nvgFontSize(vg, h*1.3f);
  135. nvgFontFace(vg, "icons");
  136. nvgFillColor(vg, nvgRGBA(255,255,255,64));
  137. nvgTextAlign(vg,NVG_ALIGN_CENTER|NVG_ALIGN_MIDDLE);
  138. nvgText(vg, x+w-h*0.5f, y+h*0.5f, cpToUTF8(ICON_CHEVRON_RIGHT,icon), NULL);
  139. }
  140. void drawLabel(struct NVGcontext* vg, const char* text, float x, float y, float w, float h)
  141. {
  142. nvgFontSize(vg, 18.0f);
  143. nvgFontFace(vg, "sans");
  144. nvgFillColor(vg, nvgRGBA(255,255,255,128));
  145. nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
  146. nvgText(vg, x,y+h*0.5f,text, NULL);
  147. }
  148. void drawEditBoxBase(struct NVGcontext* vg, float x, float y, float w, float h)
  149. {
  150. struct NVGpaint bg;
  151. // Edit
  152. 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));
  153. nvgBeginPath(vg);
  154. nvgRoundedRect(vg, x+1,y+1, w-2,h-2, 4-1);
  155. nvgFillPaint(vg, bg);
  156. nvgFill(vg);
  157. nvgBeginPath(vg);
  158. nvgRoundedRect(vg, x+0.5f,y+0.5f, w-1,h-1, 4-0.5f);
  159. nvgStrokeColor(vg, nvgRGBA(0,0,0,48));
  160. nvgStroke(vg);
  161. }
  162. void drawEditBox(struct NVGcontext* vg, const char* text, float x, float y, float w, float h)
  163. {
  164. drawEditBoxBase(vg, x,y, w,h);
  165. nvgFontSize(vg, 20.0f);
  166. nvgFontFace(vg, "sans");
  167. nvgFillColor(vg, nvgRGBA(255,255,255,64));
  168. nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
  169. nvgText(vg, x+h*0.3f,y+h*0.5f,text, NULL);
  170. }
  171. void drawEditBoxNum(struct NVGcontext* vg,
  172. const char* text, const char* units, float x, float y, float w, float h)
  173. {
  174. float uw;
  175. drawEditBoxBase(vg, x,y, w,h);
  176. uw = nvgTextBounds(vg, units, NULL, NULL);
  177. nvgFontSize(vg, 18.0f);
  178. nvgFontFace(vg, "sans");
  179. nvgFillColor(vg, nvgRGBA(255,255,255,64));
  180. nvgTextAlign(vg,NVG_ALIGN_RIGHT|NVG_ALIGN_MIDDLE);
  181. nvgText(vg, x+w-h*0.3f,y+h*0.5f,units, NULL);
  182. nvgFontSize(vg, 20.0f);
  183. nvgFontFace(vg, "sans");
  184. nvgFillColor(vg, nvgRGBA(255,255,255,128));
  185. nvgTextAlign(vg,NVG_ALIGN_RIGHT|NVG_ALIGN_MIDDLE);
  186. nvgText(vg, x+w-uw-h*0.5f,y+h*0.5f,text, NULL);
  187. }
  188. void drawCheckBox(struct NVGcontext* vg, const char* text, float x, float y, float w, float h)
  189. {
  190. struct NVGpaint bg;
  191. char icon[8];
  192. nvgFontSize(vg, 18.0f);
  193. nvgFontFace(vg, "sans");
  194. nvgFillColor(vg, nvgRGBA(255,255,255,160));
  195. nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
  196. nvgText(vg, x+28,y+h*0.5f,text, NULL);
  197. 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));
  198. nvgBeginPath(vg);
  199. nvgRoundedRect(vg, x+1,y+(int)(h*0.5f)-9, 18,18, 3);
  200. nvgFillPaint(vg, bg);
  201. nvgFill(vg);
  202. nvgFontSize(vg, 40);
  203. nvgFontFace(vg, "icons");
  204. nvgFillColor(vg, nvgRGBA(255,255,255,128));
  205. nvgTextAlign(vg,NVG_ALIGN_CENTER|NVG_ALIGN_MIDDLE);
  206. nvgText(vg, x+9+2, y+h*0.5f, cpToUTF8(ICON_CHECK,icon), NULL);
  207. }
  208. void drawButton(struct NVGcontext* vg, int preicon, const char* text, float x, float y, float w, float h, unsigned int col)
  209. {
  210. struct NVGpaint bg;
  211. char icon[8];
  212. float cornerRadius = 4.0f;
  213. float tw = 0, iw = 0;
  214. bg = nvgLinearGradient(vg, x,y,x,y+h, nvgRGBA(255,255,255,col==0?16:32), nvgRGBA(0,0,0,col==0?16:32));
  215. nvgBeginPath(vg);
  216. nvgRoundedRect(vg, x+1,y+1, w-2,h-2, cornerRadius-1);
  217. if (col != 0) {
  218. nvgFillColor(vg, col);
  219. nvgFill(vg);
  220. }
  221. nvgFillPaint(vg, bg);
  222. nvgFill(vg);
  223. nvgBeginPath(vg);
  224. nvgRoundedRect(vg, x+0.5f,y+0.5f, w-1,h-1, cornerRadius-0.5f);
  225. nvgStrokeColor(vg, nvgRGBA(0,0,0,48));
  226. nvgStroke(vg);
  227. nvgFontSize(vg, 20.0f);
  228. nvgFontFace(vg, "sans-bold");
  229. tw = nvgTextBounds(vg, text, NULL, NULL);
  230. if (preicon != 0) {
  231. nvgFontSize(vg, h*1.3f);
  232. nvgFontFace(vg, "icons");
  233. iw = nvgTextBounds(vg, cpToUTF8(preicon,icon), NULL, NULL);
  234. iw += h*0.15f;
  235. }
  236. if (preicon != 0) {
  237. nvgFontSize(vg, h*1.3f);
  238. nvgFontFace(vg, "icons");
  239. nvgFillColor(vg, nvgRGBA(255,255,255,96));
  240. nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
  241. nvgText(vg, x+w*0.5f-tw*0.5f-iw*0.75f, y+h*0.5f, cpToUTF8(preicon,icon), NULL);
  242. }
  243. nvgFontSize(vg, 20.0f);
  244. nvgFontFace(vg, "sans-bold");
  245. nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
  246. nvgFillColor(vg, nvgRGBA(0,0,0,160));
  247. nvgText(vg, x+w*0.5f-tw*0.5f+iw*0.25f,y+h*0.5f-1,text, NULL);
  248. nvgFillColor(vg, nvgRGBA(255,255,255,160));
  249. nvgText(vg, x+w*0.5f-tw*0.5f+iw*0.25f,y+h*0.5f,text, NULL);
  250. }
  251. void drawSlider(struct NVGcontext* vg, float pos, float x, float y, float w, float h)
  252. {
  253. struct NVGpaint bg, knob;
  254. float cy = y+(int)(h*0.5f);
  255. float kr = (int)(h*0.25f);
  256. nvgSave(vg);
  257. // nvgClearState(vg);
  258. // Slot
  259. bg = nvgBoxGradient(vg, x,cy-2+1, w,4, 2,2, nvgRGBA(0,0,0,32), nvgRGBA(0,0,0,128));
  260. nvgBeginPath(vg);
  261. nvgRoundedRect(vg, x,cy-2, w,4, 2);
  262. nvgFillPaint(vg, bg);
  263. nvgFill(vg);
  264. // Knob Shadow
  265. bg = nvgRadialGradient(vg, x+(int)(pos*w),cy+1, kr-3,kr+3, nvgRGBA(0,0,0,64), nvgRGBA(0,0,0,0));
  266. nvgBeginPath(vg);
  267. nvgRect(vg, x+(int)(pos*w)-kr-5,cy-kr-5,kr*2+5+5,kr*2+5+5+3);
  268. nvgCircle(vg, x+(int)(pos*w),cy, kr);
  269. nvgPathWinding(vg, NVG_HOLE);
  270. nvgFillPaint(vg, bg);
  271. nvgFill(vg);
  272. // Knob
  273. knob = nvgLinearGradient(vg, x,cy-kr,x,cy+kr, nvgRGBA(255,255,255,16), nvgRGBA(0,0,0,16));
  274. nvgBeginPath(vg);
  275. nvgCircle(vg, x+(int)(pos*w),cy, kr-1);
  276. nvgFillColor(vg, nvgRGBA(40,43,48,255));
  277. nvgFill(vg);
  278. nvgFillPaint(vg, knob);
  279. nvgFill(vg);
  280. nvgBeginPath(vg);
  281. nvgCircle(vg, x+(int)(pos*w),cy, kr-0.5f);
  282. nvgStrokeColor(vg, nvgRGBA(0,0,0,92));
  283. nvgStroke(vg);
  284. nvgRestore(vg);
  285. }
  286. void drawEyes(struct NVGcontext* vg, float x, float y, float w, float h, float mx, float my, float t)
  287. {
  288. struct NVGpaint gloss, bg;
  289. float ex = w *0.23f;
  290. float ey = h * 0.5f;
  291. float lx = x + ex;
  292. float ly = y + ey;
  293. float rx = x + w - ex;
  294. float ry = y + ey;
  295. float dx,dy,d;
  296. float br = (ex < ey ? ex : ey) * 0.5f;
  297. float blink = 1 - pow(sinf(t*0.5f),200)*0.8f;
  298. bg = nvgLinearGradient(vg, x,y+h*0.5f,x+w*0.1f,y+h, nvgRGBA(0,0,0,32), nvgRGBA(0,0,0,16));
  299. nvgBeginPath(vg);
  300. nvgEllipse(vg, lx+3.0f,ly+16.0f, ex,ey);
  301. nvgEllipse(vg, rx+3.0f,ry+16.0f, ex,ey);
  302. nvgFillPaint(vg, bg);
  303. nvgFill(vg);
  304. bg = nvgLinearGradient(vg, x,y+h*0.25f,x+w*0.1f,y+h, nvgRGBA(220,220,220,255), nvgRGBA(128,128,128,255));
  305. nvgBeginPath(vg);
  306. nvgEllipse(vg, lx,ly, ex,ey);
  307. nvgEllipse(vg, rx,ry, ex,ey);
  308. nvgFillPaint(vg, bg);
  309. nvgFill(vg);
  310. dx = (mx - rx) / (ex * 10);
  311. dy = (my - ry) / (ey * 10);
  312. d = sqrtf(dx*dx+dy*dy);
  313. if (d > 1.0f) {
  314. dx /= d; dy /= d;
  315. }
  316. dx *= ex*0.4f;
  317. dy *= ey*0.5f;
  318. nvgBeginPath(vg);
  319. nvgEllipse(vg, lx+dx,ly+dy+ey*0.25f*(1-blink), br,br*blink);
  320. nvgFillColor(vg, nvgRGBA(32,32,32,255));
  321. nvgFill(vg);
  322. dx = (mx - rx) / (ex * 10);
  323. dy = (my - ry) / (ey * 10);
  324. d = sqrtf(dx*dx+dy*dy);
  325. if (d > 1.0f) {
  326. dx /= d; dy /= d;
  327. }
  328. dx *= ex*0.4f;
  329. dy *= ey*0.5f;
  330. nvgBeginPath(vg);
  331. nvgEllipse(vg, rx+dx,ry+dy+ey*0.25f*(1-blink), br,br*blink);
  332. nvgFillColor(vg, nvgRGBA(32,32,32,255));
  333. nvgFill(vg);
  334. 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));
  335. nvgBeginPath(vg);
  336. nvgEllipse(vg, lx,ly, ex,ey);
  337. nvgFillPaint(vg, gloss);
  338. nvgFill(vg);
  339. 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));
  340. nvgBeginPath(vg);
  341. nvgEllipse(vg, rx,ry, ex,ey);
  342. nvgFillPaint(vg, gloss);
  343. nvgFill(vg);
  344. }
  345. void drawGraph(struct NVGcontext* vg, float x, float y, float w, float h, float t)
  346. {
  347. struct NVGpaint bg;
  348. float samples[6];
  349. float sx[6], sy[6];
  350. float dx = w/5.0f;
  351. int i;
  352. samples[0] = (1+sinf(t*1.2345f+cosf(t*0.33457f)*0.44f))*0.5f;
  353. samples[1] = (1+sinf(t*0.68363f+cosf(t*1.3f)*1.55f))*0.5f;
  354. samples[2] = (1+sinf(t*1.1642f+cosf(t*0.33457)*1.24f))*0.5f;
  355. samples[3] = (1+sinf(t*0.56345f+cosf(t*1.63f)*0.14f))*0.5f;
  356. samples[4] = (1+sinf(t*1.6245f+cosf(t*0.254f)*0.3f))*0.5f;
  357. samples[5] = (1+sinf(t*0.345f+cosf(t*0.03f)*0.6f))*0.5f;
  358. for (i = 0; i < 6; i++) {
  359. sx[i] = x+i*dx;
  360. sy[i] = y+h*samples[i]*0.8f;
  361. }
  362. // Graph background
  363. bg = nvgLinearGradient(vg, x,y,x,y+h, nvgRGBA(0,160,192,0), nvgRGBA(0,160,192,64));
  364. nvgBeginPath(vg);
  365. nvgMoveTo(vg, sx[0], sy[0]);
  366. for (i = 1; i < 6; i++)
  367. nvgBezierTo(vg, sx[i-1]+dx*0.5f,sy[i-1], sx[i]-dx*0.5f,sy[i], sx[i],sy[i]);
  368. nvgLineTo(vg, x+w, y+h);
  369. nvgLineTo(vg, x, y+h);
  370. nvgFillPaint(vg, bg);
  371. nvgFill(vg);
  372. // Graph line
  373. nvgBeginPath(vg);
  374. nvgMoveTo(vg, sx[0], sy[0]+2);
  375. for (i = 1; i < 6; i++)
  376. 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);
  377. nvgStrokeColor(vg, nvgRGBA(0,0,0,32));
  378. nvgStrokeWidth(vg, 3.0f);
  379. nvgStroke(vg);
  380. nvgBeginPath(vg);
  381. nvgMoveTo(vg, sx[0], sy[0]);
  382. for (i = 1; i < 6; i++)
  383. nvgBezierTo(vg, sx[i-1]+dx*0.5f,sy[i-1], sx[i]-dx*0.5f,sy[i], sx[i],sy[i]);
  384. nvgStrokeColor(vg, nvgRGBA(0,160,192,255));
  385. nvgStrokeWidth(vg, 3.0f);
  386. nvgStroke(vg);
  387. // Graph sample pos
  388. for (i = 0; i < 6; i++) {
  389. bg = nvgRadialGradient(vg, sx[i],sy[i]+2, 3.0f,8.0f, nvgRGBA(0,0,0,32), nvgRGBA(0,0,0,0));
  390. nvgBeginPath(vg);
  391. nvgRect(vg, sx[i]-10, sy[i]-10+2, 20,20);
  392. nvgFillPaint(vg, bg);
  393. nvgFill(vg);
  394. }
  395. nvgBeginPath(vg);
  396. for (i = 0; i < 6; i++)
  397. nvgCircle(vg, sx[i], sy[i], 4.0f);
  398. nvgFillColor(vg, nvgRGBA(0,160,192,255));
  399. nvgFill(vg);
  400. nvgBeginPath(vg);
  401. for (i = 0; i < 6; i++)
  402. nvgCircle(vg, sx[i], sy[i], 2.0f);
  403. nvgFillColor(vg, nvgRGBA(220,220,220,255));
  404. nvgFill(vg);
  405. nvgStrokeWidth(vg, 1.0f);
  406. }
  407. void drawThumbnails(struct NVGcontext* vg, float x, float y, float w, float h, const int* images, int nimages, float t)
  408. {
  409. float cornerRadius = 3.0f;
  410. struct NVGpaint shadowPaint, imgPaint, fadePaint;
  411. float ix,iy,iw,ih;
  412. float thumb = 60.0f;
  413. float arry = 30.5f;
  414. int imgw, imgh;
  415. float stackh = (nimages/2) * (thumb+10) + 10;
  416. int i;
  417. float u = (1+cosf(t*0.5f))*0.5f;
  418. nvgSave(vg);
  419. // nvgClearState(vg);
  420. // Drop shadow
  421. shadowPaint = nvgBoxGradient(vg, x,y+4, w,h, cornerRadius*2, 20, nvgRGBA(0,0,0,128), nvgRGBA(0,0,0,0));
  422. nvgBeginPath(vg);
  423. nvgRect(vg, x-10,y-10, w+20,h+30);
  424. nvgRoundedRect(vg, x,y, w,h, cornerRadius);
  425. nvgPathWinding(vg, NVG_HOLE);
  426. nvgFillPaint(vg, shadowPaint);
  427. nvgFill(vg);
  428. // Window
  429. nvgBeginPath(vg);
  430. nvgRoundedRect(vg, x,y, w,h, cornerRadius);
  431. nvgMoveTo(vg, x-10,y+arry);
  432. nvgLineTo(vg, x+1,y+arry-11);
  433. nvgLineTo(vg, x+1,y+arry+11);
  434. nvgFillColor(vg, nvgRGBA(200,200,200,255));
  435. nvgFill(vg);
  436. nvgSave(vg);
  437. nvgScissor(vg, x,y,w,h);
  438. nvgTranslate(vg, 0, -(stackh - h)*u);
  439. for (i = 0; i < nimages; i++) {
  440. float tx, ty;
  441. tx = x+10;
  442. ty = y+10;
  443. tx += (i%2) * (thumb+10);
  444. ty += (i/2) * (thumb+10);
  445. nvgImageSize(vg, images[i], &imgw, &imgh);
  446. if (imgw < imgh) {
  447. iw = thumb;
  448. ih = iw * (float)imgh/(float)imgw;
  449. ix = 0;
  450. iy = -(ih-thumb)*0.5f;
  451. } else {
  452. ih = thumb;
  453. iw = ih * (float)imgw/(float)imgh;
  454. ix = -(iw-thumb)*0.5f;
  455. iy = 0;
  456. }
  457. imgPaint = nvgImagePattern(vg, tx+ix, ty+iy, iw,ih, 0.0f/180.0f*NVG_PI, images[i], 0);
  458. nvgBeginPath(vg);
  459. nvgRoundedRect(vg, tx,ty, thumb,thumb, 5);
  460. nvgFillPaint(vg, imgPaint);
  461. nvgFill(vg);
  462. shadowPaint = nvgBoxGradient(vg, tx-1,ty, thumb+2,thumb+2, 5, 3, nvgRGBA(0,0,0,128), nvgRGBA(0,0,0,0));
  463. nvgBeginPath(vg);
  464. nvgRect(vg, tx-5,ty-5, thumb+10,thumb+10);
  465. nvgRoundedRect(vg, tx,ty, thumb,thumb, 6);
  466. nvgPathWinding(vg, NVG_HOLE);
  467. nvgFillPaint(vg, shadowPaint);
  468. nvgFill(vg);
  469. nvgBeginPath(vg);
  470. nvgRoundedRect(vg, tx+0.5f,ty+0.5f, thumb-1,thumb-1, 4-0.5f);
  471. nvgStrokeWidth(vg,1.0f);
  472. nvgStrokeColor(vg, nvgRGBA(255,255,255,192));
  473. nvgStroke(vg);
  474. }
  475. nvgRestore(vg);
  476. // Hide fades
  477. fadePaint = nvgLinearGradient(vg, x,y,x,y+6, nvgRGBA(200,200,200,255), nvgRGBA(200,200,200,0));
  478. nvgBeginPath(vg);
  479. nvgRect(vg, x+4,y,w-8,6);
  480. nvgFillPaint(vg, fadePaint);
  481. nvgFill(vg);
  482. fadePaint = nvgLinearGradient(vg, x,y+h,x,y+h-6, nvgRGBA(200,200,200,255), nvgRGBA(200,200,200,0));
  483. nvgBeginPath(vg);
  484. nvgRect(vg, x+4,y+h-6,w-8,6);
  485. nvgFillPaint(vg, fadePaint);
  486. nvgFill(vg);
  487. // Scroll bar
  488. 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));
  489. nvgBeginPath(vg);
  490. nvgRoundedRect(vg, x+w-12,y+4, 8,h-8, 3);
  491. nvgFillPaint(vg, shadowPaint);
  492. // nvgFillColor(vg, nvgRGBA(255,0,0,128));
  493. nvgFill(vg);
  494. float scrollh = (h/stackh) * (h-8);
  495. 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));
  496. nvgBeginPath(vg);
  497. nvgRoundedRect(vg, x+w-12+1,y+4+1 + (h-8-scrollh)*u, 8-2,scrollh-2, 2);
  498. nvgFillPaint(vg, shadowPaint);
  499. // nvgFillColor(vg, nvgRGBA(0,0,0,128));
  500. nvgFill(vg);
  501. nvgRestore(vg);
  502. }
  503. void drawColorwheel(struct NVGcontext* vg, float x, float y, float w, float h, float t)
  504. {
  505. int i;
  506. float r0, r1, ax,ay, bx,by, cx,cy, aeps, r;
  507. float hue = sinf(t * 0.12f);
  508. struct NVGpaint paint;
  509. nvgSave(vg);
  510. /* nvgBeginPath(vg);
  511. nvgRect(vg, x,y,w,h);
  512. nvgFillColor(vg, nvgRGBA(255,0,0,128));
  513. nvgFill(vg);*/
  514. cx = x + w*0.5f;
  515. cy = y + h*0.5f;
  516. r1 = (w < h ? w : h) * 0.5f - 5.0f;
  517. r0 = r1 - 20.0f;
  518. aeps = 0.5f / r1; // half a pixel arc length in radians (2pi cancels out).
  519. for (i = 0; i < 6; i++) {
  520. float a0 = (float)i / 6.0f * NVG_PI * 2.0f - aeps;
  521. float a1 = (float)(i+1.0f) / 6.0f * NVG_PI * 2.0f + aeps;
  522. nvgBeginPath(vg);
  523. nvgArc(vg, cx,cy, r0, a0, a1, NVG_CW);
  524. nvgArc(vg, cx,cy, r1, a1, a0, NVG_CCW);
  525. nvgClosePath(vg);
  526. ax = cx + cosf(a0) * (r0+r1)*0.5f;
  527. ay = cy + sinf(a0) * (r0+r1)*0.5f;
  528. bx = cx + cosf(a1) * (r0+r1)*0.5f;
  529. by = cy + sinf(a1) * (r0+r1)*0.5f;
  530. 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));
  531. nvgFillPaint(vg, paint);
  532. nvgFill(vg);
  533. }
  534. nvgBeginPath(vg);
  535. nvgCircle(vg, cx,cy, r0-1);
  536. nvgCircle(vg, cx,cy, r1+1);
  537. nvgStrokeColor(vg, nvgRGBA(0,0,0,64));
  538. nvgStrokeWidth(vg, 1.0f);
  539. nvgStroke(vg);
  540. // Selector
  541. nvgSave(vg);
  542. nvgTranslate(vg, cx,cy);
  543. nvgRotate(vg, hue*NVG_PI*2);
  544. // Marker on
  545. nvgStrokeWidth(vg, 2.0f);
  546. nvgBeginPath(vg);
  547. nvgRect(vg, r0-1,-3,r1-r0+2,6);
  548. nvgStrokeColor(vg, nvgRGBA(255,255,255,192));
  549. nvgStroke(vg);
  550. paint = nvgBoxGradient(vg, r0-3,-5,r1-r0+6,10, 2,4, nvgRGBA(0,0,0,128), nvgRGBA(0,0,0,0));
  551. nvgBeginPath(vg);
  552. nvgRect(vg, r0-3-10,-5-10,r1-r0+6+20,10+20);
  553. nvgRect(vg, r0-3,-5,r1-r0+6,10);
  554. nvgPathWinding(vg, NVG_HOLE);
  555. nvgFillPaint(vg, paint);
  556. nvgFill(vg);
  557. // Center triangle
  558. r = r0 - 6;
  559. ax = cosf(120.0f/180.0f*NVG_PI) * r;
  560. ay = sinf(120.0f/180.0f*NVG_PI) * r;
  561. bx = cosf(-120.0f/180.0f*NVG_PI) * r;
  562. by = sinf(-120.0f/180.0f*NVG_PI) * r;
  563. nvgBeginPath(vg);
  564. nvgMoveTo(vg, r,0);
  565. nvgLineTo(vg, ax,ay);
  566. nvgLineTo(vg, bx,by);
  567. nvgClosePath(vg);
  568. paint = nvgLinearGradient(vg, r,0, ax,ay, nvgHSLA(hue,1.0f,0.5f,255), nvgRGBA(255,255,255,255));
  569. nvgFillPaint(vg, paint);
  570. nvgFill(vg);
  571. paint = nvgLinearGradient(vg, (r+ax)*0.5f,(0+ay)*0.5f, bx,by, nvgRGBA(0,0,0,0), nvgRGBA(0,0,0,255));
  572. nvgFillPaint(vg, paint);
  573. nvgFill(vg);
  574. nvgStrokeColor(vg, nvgRGBA(0,0,0,64));
  575. nvgStroke(vg);
  576. // Select circle on triangle
  577. ax = cosf(120.0f/180.0f*NVG_PI) * r*0.3f;
  578. ay = sinf(120.0f/180.0f*NVG_PI) * r*0.4f;
  579. nvgStrokeWidth(vg, 2.0f);
  580. nvgBeginPath(vg);
  581. nvgCircle(vg, ax,ay,5);
  582. nvgStrokeColor(vg, nvgRGBA(255,255,255,192));
  583. nvgStroke(vg);
  584. paint = nvgRadialGradient(vg, ax,ay, 7,9, nvgRGBA(0,0,0,64), nvgRGBA(0,0,0,0));
  585. nvgBeginPath(vg);
  586. nvgRect(vg, ax-20,ay-20,40,40);
  587. nvgCircle(vg, ax,ay,7);
  588. nvgPathWinding(vg, NVG_HOLE);
  589. nvgFillPaint(vg, paint);
  590. nvgFill(vg);
  591. nvgRestore(vg);
  592. nvgRestore(vg);
  593. }
  594. int loadDemoData(struct NVGcontext* vg, struct DemoData* data)
  595. {
  596. int i;
  597. if (vg == NULL)
  598. return -1;
  599. for (i = 0; i < 12; i++) {
  600. char file[128];
  601. snprintf(file, 128, "../example/images/image%d.jpg", i+1);
  602. data->images[i] = nvgCreateImage(vg, file);
  603. if (data->images[i] == 0) {
  604. printf("Could not load %s.\n", file);
  605. return -1;
  606. }
  607. }
  608. data->fontIcons = nvgCreateFont(vg, "icons", "../example/entypo.ttf");
  609. if (data->fontIcons == -1) {
  610. printf("Could not add font icons.\n");
  611. return -1;
  612. }
  613. data->fontNormal = nvgCreateFont(vg, "sans", "../example/Roboto-Regular.ttf");
  614. if (data->fontNormal == -1) {
  615. printf("Could not add font italic.\n");
  616. return -1;
  617. }
  618. data->fontBold = nvgCreateFont(vg, "sans-bold", "../example/Roboto-Bold.ttf");
  619. if (data->fontBold == -1) {
  620. printf("Could not add font bold.\n");
  621. return -1;
  622. }
  623. return 0;
  624. }
  625. void freeDemoData(struct NVGcontext* vg, struct DemoData* data)
  626. {
  627. int i;
  628. if (vg == NULL)
  629. return;
  630. for (i = 0; i < 12; i++)
  631. nvgDeleteImage(vg, data->images[i]);
  632. }
  633. void renderDemo(struct NVGcontext* vg, float mx, float my, float width, float height,
  634. float t, int blowup, struct DemoData* data)
  635. {
  636. float x,y,popy;
  637. drawEyes(vg, width - 250, 50, 150, 100, mx, my, t);
  638. drawGraph(vg, 0, height/2, width, height/2, t);
  639. drawColorwheel(vg, width - 300, height - 300, 250.0f, 250.0f, t);
  640. nvgSave(vg);
  641. if (blowup) {
  642. nvgRotate(vg, sinf(t*0.3f)*5.0f/180.0f*NVG_PI);
  643. nvgScale(vg, 2.0f, 2.0f);
  644. }
  645. // Widgets
  646. drawWindow(vg, "Widgets `n Stuff", 50, 50, 300, 400);
  647. x = 60; y = 95;
  648. drawSearchBox(vg, "Search", x,y,280,25);
  649. y += 40;
  650. drawDropDown(vg, "Effects", x,y,280,28);
  651. popy = y + 14;
  652. y += 45;
  653. // Form
  654. drawLabel(vg, "Login", x,y, 280,20);
  655. y += 25;
  656. drawEditBox(vg, "Email", x,y, 280,28);
  657. y += 35;
  658. drawEditBox(vg, "Password", x,y, 280,28);
  659. y += 38;
  660. drawCheckBox(vg, "Remember me", x,y, 140,28);
  661. drawButton(vg, ICON_LOGIN, "Sign in", x+138, y, 140, 28, nvgRGBA(0,96,128,255));
  662. y += 45;
  663. // Slider
  664. drawLabel(vg, "Diameter", x,y, 280,20);
  665. y += 25;
  666. drawEditBoxNum(vg, "123.00", "px", x+180,y, 100,28);
  667. drawSlider(vg, 0.4f, x,y, 170,28);
  668. y += 55;
  669. drawButton(vg, ICON_TRASH, "Delete", x, y, 160, 28, nvgRGBA(128,16,8,255));
  670. drawButton(vg, 0, "Cancel", x+170, y, 110, 28, nvgRGBA(0,0,0,0));
  671. // Thumbnails box
  672. drawThumbnails(vg, 365, popy-30, 160, 300, data->images, 12, t);
  673. nvgRestore(vg);
  674. }
  675. void initFPS(struct FPScounter* fps)
  676. {
  677. memset(fps, 0, sizeof(struct FPScounter));
  678. }
  679. void updateFPS(struct FPScounter* fps, float frameTime)
  680. {
  681. fps->head = (fps->head+1) % FPS_HISTORY_COUNT;
  682. fps->values[fps->head] = frameTime;
  683. }
  684. #define AVG_SIZE 20
  685. void renderFPS(struct NVGcontext* vg, float x, float y, struct FPScounter* fps)
  686. {
  687. int i, head;
  688. float avg, w, h;
  689. char str[64];
  690. avg = 0;
  691. head = fps->head;
  692. for (i = 0; i < AVG_SIZE; i++) {
  693. avg += fps->values[head];
  694. head = (head+FPS_HISTORY_COUNT-1) % FPS_HISTORY_COUNT;
  695. }
  696. avg /= (float)AVG_SIZE;
  697. w = 200;
  698. h = 30;
  699. nvgBeginPath(vg);
  700. nvgRect(vg, x,y, w,h);
  701. nvgFillColor(vg, nvgRGBA(0,0,0,128));
  702. nvgFill(vg);
  703. nvgBeginPath(vg);
  704. nvgMoveTo(vg, x, y+h);
  705. for (i = 0; i < FPS_HISTORY_COUNT; i++) {
  706. float v = 1.0f / (0.00001f + fps->values[(fps->head+i) % FPS_HISTORY_COUNT]);
  707. if (v > 80.0f) v = 80.0f;
  708. float vx = x + ((float)i/(FPS_HISTORY_COUNT-1)) * w;
  709. float vy = y + h - ((v / 80.0f) * h);
  710. nvgLineTo(vg, vx, vy);
  711. }
  712. nvgLineTo(vg, x+w, y+h);
  713. nvgFillColor(vg, nvgRGBA(255,192,0,128));
  714. nvgFill(vg);
  715. nvgFontFace(vg, "sans");
  716. nvgFontSize(vg, 18.0f);
  717. nvgTextAlign(vg,NVG_ALIGN_RIGHT|NVG_ALIGN_MIDDLE);
  718. nvgFillColor(vg, nvgRGBA(240,240,240,255));
  719. sprintf(str, "%.2f FPS", 1.0f / avg);
  720. nvgText(vg, x+w-5,y+h/2, str, NULL);
  721. nvgFontSize(vg, 15.0f);
  722. nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
  723. nvgFillColor(vg, nvgRGBA(240,240,240,160));
  724. sprintf(str, "%.2f ms", avg * 1000.0f);
  725. nvgText(vg, x+5,y+h/2, str, NULL);
  726. }