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.

874 lines
24KB

  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, NULL);
  90. nvgFontBlur(vg,0);
  91. nvgFillColor(vg, nvgRGBA(220,220,220,160));
  92. nvgText(vg, x+w/2,y+16, title, NULL);
  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), NULL);
  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, NULL);
  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), NULL);
  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, NULL);
  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), NULL);
  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, NULL);
  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, NULL);
  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. uw = nvgTextBounds(vg, units, NULL, 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, NULL);
  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, NULL);
  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, NULL);
  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), NULL);
  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. tw = nvgTextBounds(vg, text, NULL, NULL);
  241. if (preicon != 0) {
  242. nvgFontSize(vg, h*1.3f);
  243. nvgFontFace(vg, "icons");
  244. iw = nvgTextBounds(vg, cpToUTF8(preicon,icon), NULL, 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), NULL);
  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, NULL);
  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, NULL);
  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 drawColorwheel(struct NVGcontext* vg, float x, float y, float w, float h, float t)
  515. {
  516. int i;
  517. float r0, r1, ax,ay, bx,by, cx,cy, aeps, r;
  518. float hue = sinf(t * 0.12f);
  519. struct NVGpaint paint;
  520. nvgSave(vg);
  521. /* nvgBeginPath(vg);
  522. nvgRect(vg, x,y,w,h);
  523. nvgFillColor(vg, nvgRGBA(255,0,0,128));
  524. nvgFill(vg);*/
  525. cx = x + w*0.5f;
  526. cy = y + h*0.5f;
  527. r1 = (w < h ? w : h) * 0.5f - 5.0f;
  528. r0 = r1 - 20.0f;
  529. aeps = 0.5f / r1; // half a pixel arc length in radians (2pi cancels out).
  530. for (i = 0; i < 6; i++) {
  531. float a0 = (float)i / 6.0f * NVG_PI * 2.0f - aeps;
  532. float a1 = (float)(i+1.0f) / 6.0f * NVG_PI * 2.0f + aeps;
  533. nvgBeginPath(vg);
  534. nvgArc(vg, cx,cy, r0, a0, a1, NVG_CW);
  535. nvgArc(vg, cx,cy, r1, a1, a0, NVG_CCW);
  536. nvgClosePath(vg);
  537. ax = cx + cosf(a0) * (r0+r1)*0.5f;
  538. ay = cy + sinf(a0) * (r0+r1)*0.5f;
  539. bx = cx + cosf(a1) * (r0+r1)*0.5f;
  540. by = cy + sinf(a1) * (r0+r1)*0.5f;
  541. 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));
  542. nvgFillPaint(vg, paint);
  543. nvgFill(vg);
  544. }
  545. nvgBeginPath(vg);
  546. nvgCircle(vg, cx,cy, r0-1);
  547. nvgCircle(vg, cx,cy, r1+1);
  548. nvgStrokeColor(vg, nvgRGBA(0,0,0,64));
  549. nvgStrokeWidth(vg, 1.0f);
  550. nvgStroke(vg);
  551. // Selector
  552. nvgSave(vg);
  553. nvgTranslate(vg, cx,cy);
  554. nvgRotate(vg, hue*NVG_PI*2);
  555. // Marker on
  556. nvgStrokeWidth(vg, 2.0f);
  557. nvgBeginPath(vg);
  558. nvgRect(vg, r0-1,-3,r1-r0+2,6);
  559. nvgStrokeColor(vg, nvgRGBA(255,255,255,192));
  560. nvgStroke(vg);
  561. paint = nvgBoxGradient(vg, r0-3,-5,r1-r0+6,10, 2,4, nvgRGBA(0,0,0,128), nvgRGBA(0,0,0,0));
  562. nvgBeginPath(vg);
  563. nvgRect(vg, r0-3-10,-5-10,r1-r0+6+20,10+20);
  564. nvgRect(vg, r0-3,-5,r1-r0+6,10);
  565. nvgPathWinding(vg, NVG_HOLE);
  566. nvgFillPaint(vg, paint);
  567. nvgFill(vg);
  568. // Center triangle
  569. r = r0 - 6;
  570. ax = cosf(120.0f/180.0f*NVG_PI) * r;
  571. ay = sinf(120.0f/180.0f*NVG_PI) * r;
  572. bx = cosf(-120.0f/180.0f*NVG_PI) * r;
  573. by = sinf(-120.0f/180.0f*NVG_PI) * r;
  574. nvgBeginPath(vg);
  575. nvgMoveTo(vg, r,0);
  576. nvgLineTo(vg, ax,ay);
  577. nvgLineTo(vg, bx,by);
  578. nvgClosePath(vg);
  579. paint = nvgLinearGradient(vg, r,0, ax,ay, nvgHSLA(hue,1.0f,0.5f,255), nvgRGBA(255,255,255,255));
  580. nvgFillPaint(vg, paint);
  581. nvgFill(vg);
  582. paint = nvgLinearGradient(vg, (r+ax)*0.5f,(0+ay)*0.5f, bx,by, nvgRGBA(0,0,0,0), nvgRGBA(0,0,0,255));
  583. nvgFillPaint(vg, paint);
  584. nvgFill(vg);
  585. nvgStrokeColor(vg, nvgRGBA(0,0,0,64));
  586. nvgStroke(vg);
  587. // Select circle on triangle
  588. ax = cosf(120.0f/180.0f*NVG_PI) * r*0.3f;
  589. ay = sinf(120.0f/180.0f*NVG_PI) * r*0.4f;
  590. nvgStrokeWidth(vg, 2.0f);
  591. nvgBeginPath(vg);
  592. nvgCircle(vg, ax,ay,5);
  593. nvgStrokeColor(vg, nvgRGBA(255,255,255,192));
  594. nvgStroke(vg);
  595. paint = nvgRadialGradient(vg, ax,ay, 7,9, nvgRGBA(0,0,0,64), nvgRGBA(0,0,0,0));
  596. nvgBeginPath(vg);
  597. nvgRect(vg, ax-20,ay-20,40,40);
  598. nvgCircle(vg, ax,ay,7);
  599. nvgPathWinding(vg, NVG_HOLE);
  600. nvgFillPaint(vg, paint);
  601. nvgFill(vg);
  602. nvgRestore(vg);
  603. nvgRestore(vg);
  604. }
  605. void errorcb(int error, const char* desc)
  606. {
  607. printf("GLFW error: %s\n", desc);
  608. }
  609. int blowup = 0;
  610. static void key(GLFWwindow* window, int key, int scancode, int action, int mods)
  611. {
  612. if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  613. glfwSetWindowShouldClose(window, GL_TRUE);
  614. if (key == GLFW_KEY_SPACE && action == GLFW_PRESS)
  615. blowup = !blowup;
  616. }
  617. int main()
  618. {
  619. GLFWwindow* window;
  620. int fontNormal = -1, fontBold = -1, fontIcons = -1;
  621. struct NVGcontext* vg = NULL;
  622. int images[12];
  623. int i;
  624. if (!glfwInit()) {
  625. printf("Failed to init GLFW.");
  626. return -1;
  627. }
  628. glfwSetErrorCallback(errorcb);
  629. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
  630. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
  631. window = glfwCreateWindow(1000, 600, "NanoVG", NULL, NULL);
  632. if (!window) {
  633. glfwTerminate();
  634. return -1;
  635. }
  636. glfwSetKeyCallback(window, key);
  637. glfwMakeContextCurrent(window);
  638. vg = glnvgCreate();
  639. if (vg == NULL) {
  640. printf("Could not init nanovg.\n");
  641. return -1;
  642. }
  643. for (i = 0; i < 12; i++) {
  644. char file[128];
  645. snprintf(file, 128, "../example/images/image%d.jpg", i+1);
  646. images[i] = nvgCreateImage(vg, file);
  647. if (images[i] == 0) {
  648. printf("Could not load %s.\n", file);
  649. return -1;
  650. }
  651. }
  652. fontIcons = nvgCreateFont(vg, "icons", "../example/entypo.ttf");
  653. if (fontIcons == -1) {
  654. printf("Could not add font icons.\n");
  655. return -1;
  656. }
  657. fontNormal = nvgCreateFont(vg, "sans", "../example/Roboto-Regular.ttf");
  658. // fontNormal = nvgAddFont(vg, "sans", "../example/FiraSans-Regular.ttf");
  659. if (fontNormal == -1) {
  660. printf("Could not add font italic.\n");
  661. return -1;
  662. }
  663. fontBold = nvgCreateFont(vg, "sans-bold", "../example/Roboto-Bold.ttf");
  664. // fontBold = nvgAddFont(vg, "sans-bold", "../example/FiraSans-Bold.ttf");
  665. if (fontBold == -1) {
  666. printf("Could not add font bold.\n");
  667. return -1;
  668. }
  669. glfwSetTime(0);
  670. while (!glfwWindowShouldClose(window))
  671. {
  672. // float sx, sy, dx, dy, lh = 0;
  673. double mx, my;
  674. int width, height;
  675. glfwGetCursorPos(window, &mx, &my);
  676. glfwGetFramebufferSize(window, &width, &height);
  677. // Update and render
  678. glViewport(0, 0, width, height);
  679. glClearColor(0.3f, 0.3f, 0.32f, 1.0f);
  680. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
  681. glEnable(GL_BLEND);
  682. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  683. glEnable(GL_CULL_FACE);
  684. glDisable(GL_TEXTURE_2D);
  685. glMatrixMode(GL_PROJECTION);
  686. glLoadIdentity();
  687. glOrtho(0,width,height,0,-1,1);
  688. glMatrixMode(GL_MODELVIEW);
  689. glLoadIdentity();
  690. glDisable(GL_DEPTH_TEST);
  691. glColor4ub(255,255,255,255);
  692. glEnable(GL_BLEND);
  693. glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  694. float t = glfwGetTime();
  695. float x,y,popy;
  696. nvgBeginFrame(vg);
  697. drawEyes(vg, width - 250, 50, 150, 100, mx, my, t);
  698. drawGraph(vg, 0, height/2, width, height/2, t);
  699. drawColorwheel(vg, width - 300, height - 300, 250.0f, 250.0f, t);
  700. nvgSave(vg);
  701. if (blowup) {
  702. // nvgRotate(vg, 3.0f/180.0f*NVG_PI);
  703. nvgScale(vg, 2.0f, 2.0f);
  704. }
  705. // Widgets
  706. drawWindow(vg, "Widgets `n Stuff", 50, 50, 300, 400);
  707. x = 60; y = 95;
  708. drawSearchBox(vg, "Search", x,y,280,25);
  709. y += 40;
  710. drawDropDown(vg, "Effects", x,y,280,28);
  711. popy = y + 14;
  712. y += 45;
  713. // Form
  714. drawLabel(vg, "Login", x,y, 280,20);
  715. y += 25;
  716. drawEditBox(vg, "Email", x,y, 280,28);
  717. y += 35;
  718. drawEditBox(vg, "Password", x,y, 280,28);
  719. y += 38;
  720. drawCheckBox(vg, "Remember me", x,y, 140,28);
  721. drawButton(vg, ICON_LOGIN, "Sign in", x+138, y, 140, 28, nvgRGBA(0,96,128,255));
  722. y += 45;
  723. // Slider
  724. drawLabel(vg, "Diameter", x,y, 280,20);
  725. y += 25;
  726. drawEditBoxNum(vg, "123.00", "px", x+180,y, 100,28);
  727. drawSlider(vg, 0.4f, x,y, 170,28);
  728. y += 55;
  729. drawButton(vg, ICON_TRASH, "Delete", x, y, 160, 28, nvgRGBA(128,16,8,255));
  730. drawButton(vg, 0, "Cancel", x+170, y, 110, 28, nvgRGBA(0,0,0,0));
  731. // Thumbnails box
  732. drawThumbnails(vg, 365, popy-30, 160, 300, images, 12, t);
  733. nvgRestore(vg);
  734. glEnable(GL_DEPTH_TEST);
  735. glfwSwapBuffers(window);
  736. glfwPollEvents();
  737. }
  738. for (i = 0; i < 12; i++)
  739. nvgDeleteImage(vg, images[i]);
  740. glnvgDelete(vg);
  741. glfwTerminate();
  742. return 0;
  743. }