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.

1183 lines
31KB

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