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.

768 lines
21KB

  1. //
  2. // Copyright (c) 2013 Mikko Mononen memon@inside.org
  3. //
  4. // This software is provided 'as-is', without any express or implied
  5. // warranty. In no event will the authors be held liable for any damages
  6. // arising from the use of this software.
  7. // Permission is granted to anyone to use this software for any purpose,
  8. // including commercial applications, and to alter it and redistribute it
  9. // freely, subject to the following restrictions:
  10. // 1. The origin of this software must not be misrepresented; you must not
  11. // claim that you wrote the original software. If you use this software
  12. // in a product, an acknowledgment in the product documentation would be
  13. // appreciated but is not required.
  14. // 2. Altered source versions must be plainly marked as such, and must not be
  15. // misrepresented as being the original software.
  16. // 3. This notice may not be removed or altered from any source distribution.
  17. //
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <iconv.h>
  21. #include <math.h>
  22. #include <GLFW/glfw3.h>
  23. #include "nanovg.h"
  24. #define GLNANOVG_IMPLEMENTATION
  25. #include "glnanovg.h"
  26. #define ICON_SEARCH 0x1F50D
  27. #define ICON_CIRCLED_CROSS 0x2716
  28. #define ICON_CHEVRON_RIGHT 0xE75E
  29. #define ICON_CHECK 0x2713
  30. #define ICON_LOGIN 0xE740
  31. #define ICON_TRASH 0xE729
  32. static char* cpToUTF8(int cp, char* str)
  33. {
  34. int n = 0;
  35. if (cp < 0x80) n = 1;
  36. else if (cp < 0x800) n = 2;
  37. else if (cp < 0x10000) n = 3;
  38. else if (cp < 0x200000) n = 4;
  39. else if (cp < 0x4000000) n = 5;
  40. else if (cp <= 0x7fffffff) n = 6;
  41. str[n] = '\0';
  42. switch (n) {
  43. case 6: str[5] = 0x80 | (cp & 0x3f); cp = cp >> 6; cp |= 0x4000000;
  44. case 5: str[4] = 0x80 | (cp & 0x3f); cp = cp >> 6; cp |= 0x200000;
  45. case 4: str[3] = 0x80 | (cp & 0x3f); cp = cp >> 6; cp |= 0x10000;
  46. case 3: str[2] = 0x80 | (cp & 0x3f); cp = cp >> 6; cp |= 0x800;
  47. case 2: str[1] = 0x80 | (cp & 0x3f); cp = cp >> 6; cp |= 0xc0;
  48. case 1: str[0] = cp;
  49. }
  50. return str;
  51. }
  52. void drawWindow(struct NVGcontext* vg, const char* title, float x, float y, float w, float h)
  53. {
  54. float cornerRadius = 3.0f;
  55. struct NVGpaint shadowPaint;
  56. struct NVGpaint headerPaint;
  57. nvgSave(vg);
  58. // nvgClearState(vg);
  59. // Window
  60. nvgBeginPath(vg);
  61. nvgRoundedRect(vg, x,y, w,h, cornerRadius);
  62. nvgFillColor(vg, nvgRGBA(28,30,34,192));
  63. // nvgFillColor(vg, nvgRGBA(0,0,0,128));
  64. nvgFill(vg);
  65. // Drop shadow
  66. shadowPaint = nvgBoxGradient(vg, x,y+2, w,h, cornerRadius*2, 10, nvgRGBA(0,0,0,128), nvgRGBA(0,0,0,0));
  67. nvgBeginPath(vg);
  68. nvgRect(vg, x-10,y-10, w+20,h+30);
  69. nvgRoundedRect(vg, x,y, w,h, cornerRadius);
  70. nvgPathWinding(vg, NVG_HOLE);
  71. nvgFillPaint(vg, shadowPaint);
  72. nvgFill(vg);
  73. // Header
  74. headerPaint = nvgLinearGradient(vg, x,y,x,y+15, nvgRGBA(255,255,255,8), nvgRGBA(0,0,0,16));
  75. nvgBeginPath(vg);
  76. nvgRoundedRect(vg, x+1,y+1, w-2,30, cornerRadius-1);
  77. nvgFillPaint(vg, headerPaint);
  78. nvgFill(vg);
  79. nvgBeginPath(vg);
  80. nvgMoveTo(vg, x+0.5f, y+0.5f+30);
  81. nvgLineTo(vg, x+0.5f+w-1, y+0.5f+30);
  82. nvgStrokeColor(vg, nvgRGBA(0,0,0,32));
  83. nvgStroke(vg);
  84. nvgFontSize(vg, 18.0f);
  85. nvgFontFace(vg, "sans-bold");
  86. nvgTextAlign(vg,NVG_ALIGN_CENTER|NVG_ALIGN_MIDDLE);
  87. nvgFontBlur(vg,2);
  88. nvgFillColor(vg, nvgRGBA(0,0,0,128));
  89. nvgText(vg, x+w/2,y+16+1, title);
  90. nvgFontBlur(vg,0);
  91. nvgFillColor(vg, nvgRGBA(220,220,220,160));
  92. nvgText(vg, x+w/2,y+16, title);
  93. nvgRestore(vg);
  94. }
  95. void drawSearchBox(struct NVGcontext* vg, const char* text, float x, float y, float w, float h)
  96. {
  97. struct NVGpaint bg;
  98. char icon[8];
  99. float cornerRadius = h/2-1;
  100. // Edit
  101. bg = nvgBoxGradient(vg, x,y+1.5f, w,h, h/2,5, nvgRGBA(0,0,0,16), nvgRGBA(0,0,0,92));
  102. nvgBeginPath(vg);
  103. nvgRoundedRect(vg, x,y, w,h, cornerRadius);
  104. nvgFillPaint(vg, bg);
  105. nvgFill(vg);
  106. /* nvgBeginPath(vg);
  107. nvgRoundedRect(vg, x+0.5f,y+0.5f, w-1,h-1, cornerRadius-0.5f);
  108. nvgStrokeColor(vg, nvgRGBA(0,0,0,48));
  109. nvgStroke(vg);*/
  110. nvgFontSize(vg, h*1.3f);
  111. nvgFontFace(vg, "icons");
  112. nvgFillColor(vg, nvgRGBA(255,255,255,64));
  113. nvgTextAlign(vg,NVG_ALIGN_CENTER|NVG_ALIGN_MIDDLE);
  114. nvgText(vg, x+h*0.55f, y+h*0.55f, cpToUTF8(ICON_SEARCH,icon));
  115. nvgFontSize(vg, 20.0f);
  116. nvgFontFace(vg, "sans");
  117. nvgFillColor(vg, nvgRGBA(255,255,255,32));
  118. nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
  119. nvgText(vg, x+h*1.05f,y+h*0.5f,text);
  120. nvgFontSize(vg, h*1.3f);
  121. nvgFontFace(vg, "icons");
  122. nvgFillColor(vg, nvgRGBA(255,255,255,32));
  123. nvgTextAlign(vg,NVG_ALIGN_CENTER|NVG_ALIGN_MIDDLE);
  124. nvgText(vg, x+w-h*0.55f, y+h*0.55f, cpToUTF8(ICON_CIRCLED_CROSS,icon));
  125. }
  126. void drawDropDown(struct NVGcontext* vg, const char* text, float x, float y, float w, float h)
  127. {
  128. struct NVGpaint bg;
  129. char icon[8];
  130. float cornerRadius = 4.0f;
  131. bg = nvgLinearGradient(vg, x,y,x,y+h, nvgRGBA(255,255,255,16), nvgRGBA(0,0,0,16));
  132. nvgBeginPath(vg);
  133. nvgRoundedRect(vg, x+1,y+1, w-2,h-2, cornerRadius-1);
  134. nvgFillPaint(vg, bg);
  135. nvgFill(vg);
  136. nvgBeginPath(vg);
  137. nvgRoundedRect(vg, x+0.5f,y+0.5f, w-1,h-1, cornerRadius-0.5f);
  138. nvgStrokeColor(vg, nvgRGBA(0,0,0,48));
  139. nvgStroke(vg);
  140. nvgFontSize(vg, 20.0f);
  141. nvgFontFace(vg, "sans");
  142. nvgFillColor(vg, nvgRGBA(255,255,255,160));
  143. nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
  144. nvgText(vg, x+h*0.3f,y+h*0.5f,text);
  145. nvgFontSize(vg, h*1.3f);
  146. nvgFontFace(vg, "icons");
  147. nvgFillColor(vg, nvgRGBA(255,255,255,64));
  148. nvgTextAlign(vg,NVG_ALIGN_CENTER|NVG_ALIGN_MIDDLE);
  149. nvgText(vg, x+w-h*0.5f, y+h*0.5f, cpToUTF8(ICON_CHEVRON_RIGHT,icon));
  150. }
  151. void drawLabel(struct NVGcontext* vg, const char* text, float x, float y, float w, float h)
  152. {
  153. nvgFontSize(vg, 18.0f);
  154. nvgFontFace(vg, "sans");
  155. nvgFillColor(vg, nvgRGBA(255,255,255,128));
  156. nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
  157. nvgText(vg, x,y+h*0.5f,text);
  158. }
  159. void drawEditBoxBase(struct NVGcontext* vg, float x, float y, float w, float h)
  160. {
  161. struct NVGpaint bg;
  162. // Edit
  163. 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));
  164. nvgBeginPath(vg);
  165. nvgRoundedRect(vg, x+1,y+1, w-2,h-2, 4-1);
  166. nvgFillPaint(vg, bg);
  167. nvgFill(vg);
  168. nvgBeginPath(vg);
  169. nvgRoundedRect(vg, x+0.5f,y+0.5f, w-1,h-1, 4-0.5f);
  170. nvgStrokeColor(vg, nvgRGBA(0,0,0,48));
  171. nvgStroke(vg);
  172. }
  173. void drawEditBox(struct NVGcontext* vg, const char* text, float x, float y, float w, float h)
  174. {
  175. drawEditBoxBase(vg, x,y, w,h);
  176. nvgFontSize(vg, 20.0f);
  177. nvgFontFace(vg, "sans");
  178. nvgFillColor(vg, nvgRGBA(255,255,255,64));
  179. nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
  180. nvgText(vg, x+h*0.3f,y+h*0.5f,text);
  181. }
  182. void drawEditBoxNum(struct NVGcontext* vg,
  183. const char* text, const char* units, float x, float y, float w, float h)
  184. {
  185. float uw;
  186. drawEditBoxBase(vg, x,y, w,h);
  187. nvgTextBounds(vg, units, &uw, NULL);
  188. nvgFontSize(vg, 18.0f);
  189. nvgFontFace(vg, "sans");
  190. nvgFillColor(vg, nvgRGBA(255,255,255,64));
  191. nvgTextAlign(vg,NVG_ALIGN_RIGHT|NVG_ALIGN_MIDDLE);
  192. nvgText(vg, x+w-h*0.3f,y+h*0.5f,units);
  193. nvgFontSize(vg, 20.0f);
  194. nvgFontFace(vg, "sans");
  195. nvgFillColor(vg, nvgRGBA(255,255,255,128));
  196. nvgTextAlign(vg,NVG_ALIGN_RIGHT|NVG_ALIGN_MIDDLE);
  197. nvgText(vg, x+w-uw-h*0.5f,y+h*0.5f,text);
  198. }
  199. void drawCheckBox(struct NVGcontext* vg, const char* text, float x, float y, float w, float h)
  200. {
  201. struct NVGpaint bg;
  202. char icon[8];
  203. nvgFontSize(vg, 18.0f);
  204. nvgFontFace(vg, "sans");
  205. nvgFillColor(vg, nvgRGBA(255,255,255,160));
  206. nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
  207. nvgText(vg, x+28,y+h*0.5f,text);
  208. 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));
  209. nvgBeginPath(vg);
  210. nvgRoundedRect(vg, x+1,y+(int)(h*0.5f)-9, 18,18, 3);
  211. nvgFillPaint(vg, bg);
  212. nvgFill(vg);
  213. nvgFontSize(vg, 40);
  214. nvgFontFace(vg, "icons");
  215. nvgFillColor(vg, nvgRGBA(255,255,255,128));
  216. nvgTextAlign(vg,NVG_ALIGN_CENTER|NVG_ALIGN_MIDDLE);
  217. nvgText(vg, x+9+2, y+h*0.5f, cpToUTF8(ICON_CHECK,icon));
  218. }
  219. void drawButton(struct NVGcontext* vg, int preicon, const char* text, float x, float y, float w, float h, unsigned int col)
  220. {
  221. struct NVGpaint bg;
  222. char icon[8];
  223. float cornerRadius = 4.0f;
  224. float tw = 0, iw = 0;
  225. bg = nvgLinearGradient(vg, x,y,x,y+h, nvgRGBA(255,255,255,col==0?16:32), nvgRGBA(0,0,0,col==0?16:32));
  226. nvgBeginPath(vg);
  227. nvgRoundedRect(vg, x+1,y+1, w-2,h-2, cornerRadius-1);
  228. if (col != 0) {
  229. nvgFillColor(vg, col);
  230. nvgFill(vg);
  231. }
  232. nvgFillPaint(vg, bg);
  233. nvgFill(vg);
  234. nvgBeginPath(vg);
  235. nvgRoundedRect(vg, x+0.5f,y+0.5f, w-1,h-1, cornerRadius-0.5f);
  236. nvgStrokeColor(vg, nvgRGBA(0,0,0,48));
  237. nvgStroke(vg);
  238. nvgFontSize(vg, 20.0f);
  239. nvgFontFace(vg, "sans-bold");
  240. nvgTextBounds(vg, text, &tw, NULL);
  241. if (preicon != 0) {
  242. nvgFontSize(vg, h*1.3f);
  243. nvgFontFace(vg, "icons");
  244. nvgTextBounds(vg, cpToUTF8(preicon,icon), &iw, NULL);
  245. iw += h*0.15f;
  246. }
  247. if (preicon != 0) {
  248. nvgFontSize(vg, h*1.3f);
  249. nvgFontFace(vg, "icons");
  250. nvgFillColor(vg, nvgRGBA(255,255,255,96));
  251. nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
  252. nvgText(vg, x+w*0.5f-tw*0.5f-iw*0.75f, y+h*0.5f, cpToUTF8(preicon,icon));
  253. }
  254. nvgFontSize(vg, 20.0f);
  255. nvgFontFace(vg, "sans-bold");
  256. nvgTextAlign(vg,NVG_ALIGN_LEFT|NVG_ALIGN_MIDDLE);
  257. nvgFillColor(vg, nvgRGBA(0,0,0,160));
  258. nvgText(vg, x+w*0.5f-tw*0.5f+iw*0.25f,y+h*0.5f-1,text);
  259. nvgFillColor(vg, nvgRGBA(255,255,255,160));
  260. nvgText(vg, x+w*0.5f-tw*0.5f+iw*0.25f,y+h*0.5f,text);
  261. }
  262. void drawSlider(struct NVGcontext* vg, float pos, float x, float y, float w, float h)
  263. {
  264. struct NVGpaint bg, knob;
  265. float cy = y+(int)(h*0.5f);
  266. float kr = (int)(h*0.25f);
  267. nvgSave(vg);
  268. // nvgClearState(vg);
  269. // Slot
  270. bg = nvgBoxGradient(vg, x,cy-2+1, w,4, 2,2, nvgRGBA(0,0,0,32), nvgRGBA(0,0,0,128));
  271. nvgBeginPath(vg);
  272. nvgRoundedRect(vg, x,cy-2, w,4, 2);
  273. nvgFillPaint(vg, bg);
  274. nvgFill(vg);
  275. // Knob Shadow
  276. bg = nvgRadialGradient(vg, x+(int)(pos*w),cy+1, kr-3,kr+3, nvgRGBA(0,0,0,64), nvgRGBA(0,0,0,0));
  277. nvgBeginPath(vg);
  278. nvgRect(vg, x+(int)(pos*w)-kr-5,cy-kr-5,kr*2+5+5,kr*2+5+5+3);
  279. nvgCircle(vg, x+(int)(pos*w),cy, kr);
  280. nvgPathWinding(vg, NVG_HOLE);
  281. nvgFillPaint(vg, bg);
  282. nvgFill(vg);
  283. // Knob
  284. knob = nvgLinearGradient(vg, x,cy-kr,x,cy+kr, nvgRGBA(255,255,255,16), nvgRGBA(0,0,0,16));
  285. nvgBeginPath(vg);
  286. nvgCircle(vg, x+(int)(pos*w),cy, kr-1);
  287. nvgFillColor(vg, nvgRGBA(40,43,48,255));
  288. nvgFill(vg);
  289. nvgFillPaint(vg, knob);
  290. nvgFill(vg);
  291. nvgBeginPath(vg);
  292. nvgCircle(vg, x+(int)(pos*w),cy, kr-0.5f);
  293. nvgStrokeColor(vg, nvgRGBA(0,0,0,92));
  294. nvgStroke(vg);
  295. nvgRestore(vg);
  296. }
  297. void drawEyes(struct NVGcontext* vg, float x, float y, float w, float h, float mx, float my, float t)
  298. {
  299. struct NVGpaint gloss, bg;
  300. float ex = w *0.23f;
  301. float ey = h * 0.5f;
  302. float lx = x + ex;
  303. float ly = y + ey;
  304. float rx = x + w - ex;
  305. float ry = y + ey;
  306. float dx,dy,d;
  307. float br = (ex < ey ? ex : ey) * 0.5f;
  308. float blink = 1 - pow(sinf(t*0.5f),200)*0.8f;
  309. bg = nvgLinearGradient(vg, x,y+h*0.5f,x+w*0.1f,y+h, nvgRGBA(0,0,0,32), nvgRGBA(0,0,0,16));
  310. nvgBeginPath(vg);
  311. nvgEllipse(vg, lx+3.0f,ly+16.0f, ex,ey);
  312. nvgEllipse(vg, rx+3.0f,ry+16.0f, ex,ey);
  313. nvgFillPaint(vg, bg);
  314. nvgFill(vg);
  315. bg = nvgLinearGradient(vg, x,y+h*0.25f,x+w*0.1f,y+h, nvgRGBA(220,220,220,255), nvgRGBA(128,128,128,255));
  316. nvgBeginPath(vg);
  317. nvgEllipse(vg, lx,ly, ex,ey);
  318. nvgEllipse(vg, rx,ry, ex,ey);
  319. nvgFillPaint(vg, bg);
  320. nvgFill(vg);
  321. dx = (mx - rx) / (ex * 10);
  322. dy = (my - ry) / (ey * 10);
  323. d = sqrtf(dx*dx+dy*dy);
  324. if (d > 1.0f) {
  325. dx /= d; dy /= d;
  326. }
  327. dx *= ex*0.4f;
  328. dy *= ey*0.5f;
  329. nvgBeginPath(vg);
  330. nvgEllipse(vg, lx+dx,ly+dy+ey*0.25f*(1-blink), br,br*blink);
  331. nvgFillColor(vg, nvgRGBA(32,32,32,255));
  332. nvgFill(vg);
  333. dx = (mx - rx) / (ex * 10);
  334. dy = (my - ry) / (ey * 10);
  335. d = sqrtf(dx*dx+dy*dy);
  336. if (d > 1.0f) {
  337. dx /= d; dy /= d;
  338. }
  339. dx *= ex*0.4f;
  340. dy *= ey*0.5f;
  341. nvgBeginPath(vg);
  342. nvgEllipse(vg, rx+dx,ry+dy+ey*0.25f*(1-blink), br,br*blink);
  343. nvgFillColor(vg, nvgRGBA(32,32,32,255));
  344. nvgFill(vg);
  345. 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));
  346. nvgBeginPath(vg);
  347. nvgEllipse(vg, lx,ly, ex,ey);
  348. nvgFillPaint(vg, gloss);
  349. nvgFill(vg);
  350. 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));
  351. nvgBeginPath(vg);
  352. nvgEllipse(vg, rx,ry, ex,ey);
  353. nvgFillPaint(vg, gloss);
  354. nvgFill(vg);
  355. }
  356. void drawGraph(struct NVGcontext* vg, float x, float y, float w, float h, float t)
  357. {
  358. struct NVGpaint bg;
  359. float samples[6];
  360. float sx[6], sy[6];
  361. float dx = w/5.0f;
  362. int i;
  363. samples[0] = (1+sinf(t*1.2345f+cosf(t*0.33457f)*0.44f))*0.5f;
  364. samples[1] = (1+sinf(t*0.68363f+cosf(t*1.3f)*1.55f))*0.5f;
  365. samples[2] = (1+sinf(t*1.1642f+cosf(t*0.33457)*1.24f))*0.5f;
  366. samples[3] = (1+sinf(t*0.56345f+cosf(t*1.63f)*0.14f))*0.5f;
  367. samples[4] = (1+sinf(t*1.6245f+cosf(t*0.254f)*0.3f))*0.5f;
  368. samples[5] = (1+sinf(t*0.345f+cosf(t*0.03f)*0.6f))*0.5f;
  369. for (i = 0; i < 6; i++) {
  370. sx[i] = x+i*dx;
  371. sy[i] = y+h*samples[i]*0.8f;
  372. }
  373. // Graph background
  374. bg = nvgLinearGradient(vg, x,y,x,y+h, nvgRGBA(0,160,192,0), nvgRGBA(0,160,192,64));
  375. nvgBeginPath(vg);
  376. nvgMoveTo(vg, sx[0], sy[0]);
  377. for (i = 1; i < 6; i++)
  378. nvgBezierTo(vg, sx[i-1]+dx*0.5f,sy[i-1], sx[i]-dx*0.5f,sy[i], sx[i],sy[i]);
  379. nvgLineTo(vg, x+w, y+h);
  380. nvgLineTo(vg, x, y+h);
  381. nvgFillPaint(vg, bg);
  382. nvgFill(vg);
  383. // Graph line
  384. nvgBeginPath(vg);
  385. nvgMoveTo(vg, sx[0], sy[0]+2);
  386. for (i = 1; i < 6; i++)
  387. 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);
  388. nvgStrokeColor(vg, nvgRGBA(0,0,0,32));
  389. nvgStrokeWidth(vg, 3.0f);
  390. nvgStroke(vg);
  391. nvgBeginPath(vg);
  392. nvgMoveTo(vg, sx[0], sy[0]);
  393. for (i = 1; i < 6; i++)
  394. nvgBezierTo(vg, sx[i-1]+dx*0.5f,sy[i-1], sx[i]-dx*0.5f,sy[i], sx[i],sy[i]);
  395. nvgStrokeColor(vg, nvgRGBA(0,160,192,255));
  396. nvgStrokeWidth(vg, 3.0f);
  397. nvgStroke(vg);
  398. // Graph sample pos
  399. for (i = 0; i < 6; i++) {
  400. bg = nvgRadialGradient(vg, sx[i],sy[i]+2, 3.0f,8.0f, nvgRGBA(0,0,0,32), nvgRGBA(0,0,0,0));
  401. nvgBeginPath(vg);
  402. nvgRect(vg, sx[i]-10, sy[i]-10+2, 20,20);
  403. nvgFillPaint(vg, bg);
  404. nvgFill(vg);
  405. }
  406. nvgBeginPath(vg);
  407. for (i = 0; i < 6; i++)
  408. nvgCircle(vg, sx[i], sy[i], 4.0f);
  409. nvgFillColor(vg, nvgRGBA(0,160,192,255));
  410. nvgFill(vg);
  411. nvgBeginPath(vg);
  412. for (i = 0; i < 6; i++)
  413. nvgCircle(vg, sx[i], sy[i], 2.0f);
  414. nvgFillColor(vg, nvgRGBA(220,220,220,255));
  415. nvgFill(vg);
  416. nvgStrokeWidth(vg, 1.0f);
  417. }
  418. void drawThumbnails(struct NVGcontext* vg, float x, float y, float w, float h, const int* images, int nimages, float t)
  419. {
  420. float cornerRadius = 3.0f;
  421. struct NVGpaint shadowPaint, imgPaint, fadePaint;
  422. float ix,iy,iw,ih;
  423. float thumb = 60.0f;
  424. float arry = 30.5f;
  425. int imgw, imgh;
  426. float stackh = (nimages/2) * (thumb+10) + 10;
  427. int i;
  428. float u = (1+cosf(t*0.5f))*0.5f;
  429. nvgSave(vg);
  430. // nvgClearState(vg);
  431. // Drop shadow
  432. shadowPaint = nvgBoxGradient(vg, x,y+4, w,h, cornerRadius*2, 20, nvgRGBA(0,0,0,128), nvgRGBA(0,0,0,0));
  433. nvgBeginPath(vg);
  434. nvgRect(vg, x-10,y-10, w+20,h+30);
  435. nvgRoundedRect(vg, x,y, w,h, cornerRadius);
  436. nvgPathWinding(vg, NVG_HOLE);
  437. nvgFillPaint(vg, shadowPaint);
  438. nvgFill(vg);
  439. // Window
  440. nvgBeginPath(vg);
  441. nvgRoundedRect(vg, x,y, w,h, cornerRadius);
  442. nvgMoveTo(vg, x-10,y+arry);
  443. nvgLineTo(vg, x+1,y+arry-11);
  444. nvgLineTo(vg, x+1,y+arry+11);
  445. nvgFillColor(vg, nvgRGBA(200,200,200,255));
  446. nvgFill(vg);
  447. nvgSave(vg);
  448. nvgScissor(vg, x,y,w,h);
  449. nvgTranslate(vg, 0, -(stackh - h)*u);
  450. for (i = 0; i < nimages; i++) {
  451. float tx, ty;
  452. tx = x+10;
  453. ty = y+10;
  454. tx += (i%2) * (thumb+10);
  455. ty += (i/2) * (thumb+10);
  456. nvgImageSize(vg, images[i], &imgw, &imgh);
  457. if (imgw < imgh) {
  458. iw = thumb;
  459. ih = iw * (float)imgh/(float)imgw;
  460. ix = 0;
  461. iy = -(ih-thumb)*0.5f;
  462. } else {
  463. ih = thumb;
  464. iw = ih * (float)imgw/(float)imgh;
  465. ix = -(iw-thumb)*0.5f;
  466. iy = 0;
  467. }
  468. imgPaint = nvgImagePattern(vg, tx+ix, ty+iy, iw,ih, 0.0f/180.0f*NVG_PI, images[i], 0);
  469. nvgBeginPath(vg);
  470. nvgRoundedRect(vg, tx,ty, thumb,thumb, 5);
  471. nvgFillPaint(vg, imgPaint);
  472. nvgFill(vg);
  473. shadowPaint = nvgBoxGradient(vg, tx-1,ty, thumb+2,thumb+2, 5, 3, nvgRGBA(0,0,0,128), nvgRGBA(0,0,0,0));
  474. nvgBeginPath(vg);
  475. nvgRect(vg, tx-5,ty-5, thumb+10,thumb+10);
  476. nvgRoundedRect(vg, tx,ty, thumb,thumb, 6);
  477. nvgPathWinding(vg, NVG_HOLE);
  478. nvgFillPaint(vg, shadowPaint);
  479. nvgFill(vg);
  480. nvgBeginPath(vg);
  481. nvgRoundedRect(vg, tx+0.5f,ty+0.5f, thumb-1,thumb-1, 4-0.5f);
  482. nvgStrokeWidth(vg,1.0f);
  483. nvgStrokeColor(vg, nvgRGBA(255,255,255,192));
  484. nvgStroke(vg);
  485. }
  486. nvgRestore(vg);
  487. // Hide fades
  488. fadePaint = nvgLinearGradient(vg, x,y,x,y+6, nvgRGBA(200,200,200,255), nvgRGBA(200,200,200,0));
  489. nvgBeginPath(vg);
  490. nvgRect(vg, x+4,y,w-8,6);
  491. nvgFillPaint(vg, fadePaint);
  492. nvgFill(vg);
  493. fadePaint = nvgLinearGradient(vg, x,y+h,x,y+h-6, nvgRGBA(200,200,200,255), nvgRGBA(200,200,200,0));
  494. nvgBeginPath(vg);
  495. nvgRect(vg, x+4,y+h-6,w-8,6);
  496. nvgFillPaint(vg, fadePaint);
  497. nvgFill(vg);
  498. // Scroll bar
  499. 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));
  500. nvgBeginPath(vg);
  501. nvgRoundedRect(vg, x+w-12,y+4, 8,h-8, 3);
  502. nvgFillPaint(vg, shadowPaint);
  503. // nvgFillColor(vg, nvgRGBA(255,0,0,128));
  504. nvgFill(vg);
  505. float scrollh = (h/stackh) * (h-8);
  506. 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));
  507. nvgBeginPath(vg);
  508. nvgRoundedRect(vg, x+w-12+1,y+4+1 + (h-8-scrollh)*u, 8-2,scrollh-2, 2);
  509. nvgFillPaint(vg, shadowPaint);
  510. // nvgFillColor(vg, nvgRGBA(0,0,0,128));
  511. nvgFill(vg);
  512. nvgRestore(vg);
  513. }
  514. void errorcb(int error, const char* desc)
  515. {
  516. printf("GLFW error: %s\n", desc);
  517. }
  518. int blowup = 0;
  519. static void key(GLFWwindow* window, int key, int scancode, int action, int mods)
  520. {
  521. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  522. glfwSetWindowShouldClose(window, GL_TRUE);
  523. if (key == GLFW_KEY_SPACE && action == GLFW_PRESS)
  524. blowup = !blowup;
  525. }
  526. int main()
  527. {
  528. GLFWwindow* window;
  529. int fontNormal = -1, fontBold = -1, fontIcons = -1;
  530. struct NVGcontext* vg = NULL;
  531. int images[12];
  532. int i;
  533. if (!glfwInit()) {
  534. printf("Failed to init GLFW.");
  535. return -1;
  536. }
  537. glfwSetErrorCallback(errorcb);
  538. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
  539. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
  540. window = glfwCreateWindow(1000, 600, "NanoVG", NULL, NULL);
  541. if (!window) {
  542. glfwTerminate();
  543. return -1;
  544. }
  545. glfwSetKeyCallback(window, key);
  546. glfwMakeContextCurrent(window);
  547. vg = glnvgCreate();
  548. if (vg == NULL) {
  549. printf("Could not init nanovg.\n");
  550. return -1;
  551. }
  552. for (i = 0; i < 12; i++) {
  553. char file[128];
  554. snprintf(file, 128, "../example/images/image%d.jpg", i+1);
  555. images[i] = nvgCreateImage(vg, file);
  556. if (images[i] == 0) {
  557. printf("Could not load %s.\n", file);
  558. return -1;
  559. }
  560. }
  561. fontIcons = nvgCreateFont(vg, "icons", "../example/entypo.ttf");
  562. if (fontIcons == -1) {
  563. printf("Could not add font icons.\n");
  564. return -1;
  565. }
  566. fontNormal = nvgCreateFont(vg, "sans", "../example/Roboto-Regular.ttf");
  567. // fontNormal = nvgAddFont(vg, "sans", "../example/FiraSans-Regular.ttf");
  568. if (fontNormal == -1) {
  569. printf("Could not add font italic.\n");
  570. return -1;
  571. }
  572. fontBold = nvgCreateFont(vg, "sans-bold", "../example/Roboto-Bold.ttf");
  573. // fontBold = nvgAddFont(vg, "sans-bold", "../example/FiraSans-Bold.ttf");
  574. if (fontBold == -1) {
  575. printf("Could not add font bold.\n");
  576. return -1;
  577. }
  578. glfwSetTime(0);
  579. while (!glfwWindowShouldClose(window))
  580. {
  581. // float sx, sy, dx, dy, lh = 0;
  582. double mx, my;
  583. int width, height;
  584. glfwGetCursorPos(window, &mx, &my);
  585. glfwGetFramebufferSize(window, &width, &height);
  586. // Update and render
  587. glViewport(0, 0, width, height);
  588. glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
  589. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  590. glEnable(GL_BLEND);
  591. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  592. glEnable(GL_CULL_FACE);
  593. glDisable(GL_TEXTURE_2D);
  594. glMatrixMode(GL_PROJECTION);
  595. glLoadIdentity();
  596. glOrtho(0,width,height,0,-1,1);
  597. glMatrixMode(GL_MODELVIEW);
  598. glLoadIdentity();
  599. glDisable(GL_DEPTH_TEST);
  600. glColor4ub(255,255,255,255);
  601. glEnable(GL_BLEND);
  602. glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  603. float t = glfwGetTime();
  604. float x,y,popy;
  605. nvgBeginFrame(vg);
  606. drawEyes(vg, width - 250, 50, 200, 120, mx, my, t);
  607. drawGraph(vg, 0, height/2, width, height/2, t);
  608. nvgSave(vg);
  609. if (blowup) {
  610. // nvgRotate(vg, 3.0f/180.0f*NVG_PI);
  611. nvgScale(vg, 2.0f, 2.0f);
  612. }
  613. // Widgets
  614. drawWindow(vg, "Widgets `n Stuff", 20, 20, 300, 400);
  615. x = 30; y = 65;
  616. drawSearchBox(vg, "Search", x,y,280,25);
  617. y += 40;
  618. drawDropDown(vg, "Effects", x,y,280,28);
  619. popy = y + 14;
  620. y += 45;
  621. // Form
  622. drawLabel(vg, "Login", x,y, 280,20);
  623. y += 25;
  624. drawEditBox(vg, "Email", x,y, 280,28);
  625. y += 35;
  626. drawEditBox(vg, "Password", x,y, 280,28);
  627. y += 38;
  628. drawCheckBox(vg, "Remember me", x,y, 140,28);
  629. drawButton(vg, ICON_LOGIN, "Sign in", x+138, y, 140, 28, nvgRGBA(0,96,128,255));
  630. y += 45;
  631. // Slider
  632. drawLabel(vg, "Diameter", x,y, 280,20);
  633. y += 25;
  634. drawEditBoxNum(vg, "123.00", "px", x+180,y, 100,28);
  635. drawSlider(vg, 0.4f, x,y, 170,28);
  636. y += 55;
  637. drawButton(vg, ICON_TRASH, "Delete", x, y, 160, 28, nvgRGBA(128,16,8,255));
  638. drawButton(vg, 0, "Cancel", x+170, y, 110, 28, nvgRGBA(0,0,0,0));
  639. // Thumbnails box
  640. drawThumbnails(vg, 325, popy-30, 160, 300, images, 12, t);
  641. nvgRestore(vg);
  642. glEnable(GL_DEPTH_TEST);
  643. glfwSwapBuffers(window);
  644. glfwPollEvents();
  645. }
  646. for (i = 0; i < 12; i++)
  647. nvgDeleteImage(vg, images[i]);
  648. glnvgDelete(vg);
  649. glfwTerminate();
  650. return 0;
  651. }