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.

1227 lines
32KB

  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(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(NVGcontext* vg, const char* title, float x, float y, float w, float h)
  57. {
  58. float cornerRadius = 3.0f;
  59. NVGpaint shadowPaint;
  60. 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, 15.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(NVGcontext* vg, const char* text, float x, float y, float w, float h)
  100. {
  101. 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, 17.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(NVGcontext* vg, const char* text, float x, float y, float w, float h)
  131. {
  132. 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, 17.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(NVGcontext* vg, const char* text, float x, float y, float w, float h)
  156. {
  157. NVG_NOTUSED(w);
  158. nvgFontSize(vg, 15.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(NVGcontext* vg, float x, float y, float w, float h)
  165. {
  166. 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(NVGcontext* vg, const char* text, float x, float y, float w, float h)
  179. {
  180. drawEditBoxBase(vg, x,y, w,h);
  181. nvgFontSize(vg, 17.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(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, 15.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, 17.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(NVGcontext* vg, const char* text, float x, float y, float w, float h)
  205. {
  206. NVGpaint bg;
  207. char icon[8];
  208. NVG_NOTUSED(w);
  209. nvgFontSize(vg, 15.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, 33);
  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(NVGcontext* vg, int preicon, const char* text, float x, float y, float w, float h, NVGcolor col)
  226. {
  227. 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, 17.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, 17.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(NVGcontext* vg, float pos, float x, float y, float w, float h)
  269. {
  270. 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(NVGcontext* vg, float x, float y, float w, float h, float mx, float my, float t)
  304. {
  305. 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(NVGcontext* vg, float x, float y, float w, float h, float t)
  363. {
  364. 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(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. 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(NVGcontext* vg, float x, float y, float w, float h, const int* images, int nimages, float t)
  447. {
  448. float cornerRadius = 3.0f;
  449. 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], 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(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. 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(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(NVGcontext* vg, 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. data->fontEmoji = nvgCreateFont(vg, "emoji", "../example/NotoEmoji-Regular.ttf");
  715. if (data->fontEmoji == -1) {
  716. printf("Could not add font emoji.\n");
  717. return -1;
  718. }
  719. nvgAddFallbackFontId(vg, data->fontNormal, data->fontEmoji);
  720. nvgAddFallbackFontId(vg, data->fontBold, data->fontEmoji);
  721. return 0;
  722. }
  723. void freeDemoData(NVGcontext* vg, DemoData* data)
  724. {
  725. int i;
  726. if (vg == NULL)
  727. return;
  728. for (i = 0; i < 12; i++)
  729. nvgDeleteImage(vg, data->images[i]);
  730. }
  731. void drawParagraph(NVGcontext* vg, float x, float y, float width, float height, float mx, float my)
  732. {
  733. NVGtextRow rows[3];
  734. NVGglyphPosition glyphs[100];
  735. 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.🎉";
  736. const char* start;
  737. const char* end;
  738. int nrows, i, nglyphs, j, lnum = 0;
  739. float lineh;
  740. float caretx, px;
  741. float bounds[4];
  742. float a;
  743. float gx,gy;
  744. int gutter = 0;
  745. NVG_NOTUSED(height);
  746. nvgSave(vg);
  747. nvgFontSize(vg, 15.0f);
  748. nvgFontFace(vg, "sans");
  749. nvgTextAlign(vg, NVG_ALIGN_LEFT|NVG_ALIGN_TOP);
  750. nvgTextMetrics(vg, NULL, NULL, &lineh);
  751. // The text break API can be used to fill a large buffer of rows,
  752. // or to iterate over the text just few lines (or just one) at a time.
  753. // The "next" variable of the last returned item tells where to continue.
  754. start = text;
  755. end = text + strlen(text);
  756. while ((nrows = nvgTextBreakLines(vg, start, end, width, rows, 3))) {
  757. for (i = 0; i < nrows; i++) {
  758. NVGtextRow* row = &rows[i];
  759. int hit = mx > x && mx < (x+width) && my >= y && my < (y+lineh);
  760. nvgBeginPath(vg);
  761. nvgFillColor(vg, nvgRGBA(255,255,255,hit?64:16));
  762. nvgRect(vg, x, y, row->width, lineh);
  763. nvgFill(vg);
  764. nvgFillColor(vg, nvgRGBA(255,255,255,255));
  765. nvgText(vg, x, y, row->start, row->end);
  766. if (hit) {
  767. caretx = (mx < x+row->width/2) ? x : x+row->width;
  768. px = x;
  769. nglyphs = nvgTextGlyphPositions(vg, x, y, row->start, row->end, glyphs, 100);
  770. for (j = 0; j < nglyphs; j++) {
  771. float x0 = glyphs[j].x;
  772. float x1 = (j+1 < nglyphs) ? glyphs[j+1].x : x+row->width;
  773. float gx = x0 * 0.3f + x1 * 0.7f;
  774. if (mx >= px && mx < gx)
  775. caretx = glyphs[j].x;
  776. px = gx;
  777. }
  778. nvgBeginPath(vg);
  779. nvgFillColor(vg, nvgRGBA(255,192,0,255));
  780. nvgRect(vg, caretx, y, 1, lineh);
  781. nvgFill(vg);
  782. gutter = lnum+1;
  783. gx = x - 10;
  784. gy = y + lineh/2;
  785. }
  786. lnum++;
  787. y += lineh;
  788. }
  789. // Keep going...
  790. start = rows[nrows-1].next;
  791. }
  792. if (gutter) {
  793. char txt[16];
  794. snprintf(txt, sizeof(txt), "%d", gutter);
  795. nvgFontSize(vg, 12.0f);
  796. nvgTextAlign(vg, NVG_ALIGN_RIGHT|NVG_ALIGN_MIDDLE);
  797. nvgTextBounds(vg, gx,gy, txt, NULL, bounds);
  798. nvgBeginPath(vg);
  799. nvgFillColor(vg, nvgRGBA(255,192,0,255));
  800. 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);
  801. nvgFill(vg);
  802. nvgFillColor(vg, nvgRGBA(32,32,32,255));
  803. nvgText(vg, gx,gy, txt, NULL);
  804. }
  805. y += 20.0f;
  806. nvgFontSize(vg, 11.0f);
  807. nvgTextAlign(vg, NVG_ALIGN_LEFT|NVG_ALIGN_TOP);
  808. nvgTextLineHeight(vg, 1.2f);
  809. nvgTextBoxBounds(vg, x,y, 150, "Hover your mouse over the text to see calculated caret position.", NULL, bounds);
  810. // Fade the tooltip out when close to it.
  811. gx = fabsf((mx - (bounds[0]+bounds[2])*0.5f) / (bounds[0] - bounds[2]));
  812. gy = fabsf((my - (bounds[1]+bounds[3])*0.5f) / (bounds[1] - bounds[3]));
  813. a = maxf(gx, gy) - 0.5f;
  814. a = clampf(a, 0, 1);
  815. nvgGlobalAlpha(vg, a);
  816. nvgBeginPath(vg);
  817. nvgFillColor(vg, nvgRGBA(220,220,220,255));
  818. nvgRoundedRect(vg, bounds[0]-2,bounds[1]-2, (int)(bounds[2]-bounds[0])+4, (int)(bounds[3]-bounds[1])+4, 3);
  819. px = (int)((bounds[2]+bounds[0])/2);
  820. nvgMoveTo(vg, px,bounds[1] - 10);
  821. nvgLineTo(vg, px+7,bounds[1]+1);
  822. nvgLineTo(vg, px-7,bounds[1]+1);
  823. nvgFill(vg);
  824. nvgFillColor(vg, nvgRGBA(0,0,0,220));
  825. nvgTextBox(vg, x,y, 150, "Hover your mouse over the text to see calculated caret position.", NULL);
  826. nvgRestore(vg);
  827. }
  828. void drawWidths(NVGcontext* vg, float x, float y, float width)
  829. {
  830. int i;
  831. nvgSave(vg);
  832. nvgStrokeColor(vg, nvgRGBA(0,0,0,255));
  833. for (i = 0; i < 20; i++) {
  834. float w = (i+0.5f)*0.1f;
  835. nvgStrokeWidth(vg, w);
  836. nvgBeginPath(vg);
  837. nvgMoveTo(vg, x,y);
  838. nvgLineTo(vg, x+width,y+width*0.3f);
  839. nvgStroke(vg);
  840. y += 10;
  841. }
  842. nvgRestore(vg);
  843. }
  844. void drawCaps(NVGcontext* vg, float x, float y, float width)
  845. {
  846. int i;
  847. int caps[3] = {NVG_BUTT, NVG_ROUND, NVG_SQUARE};
  848. float lineWidth = 8.0f;
  849. nvgSave(vg);
  850. nvgBeginPath(vg);
  851. nvgRect(vg, x-lineWidth/2, y, width+lineWidth, 40);
  852. nvgFillColor(vg, nvgRGBA(255,255,255,32));
  853. nvgFill(vg);
  854. nvgBeginPath(vg);
  855. nvgRect(vg, x, y, width, 40);
  856. nvgFillColor(vg, nvgRGBA(255,255,255,32));
  857. nvgFill(vg);
  858. nvgStrokeWidth(vg, lineWidth);
  859. for (i = 0; i < 3; i++) {
  860. nvgLineCap(vg, caps[i]);
  861. nvgStrokeColor(vg, nvgRGBA(0,0,0,255));
  862. nvgBeginPath(vg);
  863. nvgMoveTo(vg, x, y + i*10 + 5);
  864. nvgLineTo(vg, x+width, y + i*10 + 5);
  865. nvgStroke(vg);
  866. }
  867. nvgRestore(vg);
  868. }
  869. void drawScissor(NVGcontext* vg, float x, float y, float t)
  870. {
  871. nvgSave(vg);
  872. // Draw first rect and set scissor to it's area.
  873. nvgTranslate(vg, x, y);
  874. nvgRotate(vg, nvgDegToRad(5));
  875. nvgBeginPath(vg);
  876. nvgRect(vg, -20,-20,60,40);
  877. nvgFillColor(vg, nvgRGBA(255,0,0,255));
  878. nvgFill(vg);
  879. nvgScissor(vg, -20,-20,60,40);
  880. // Draw second rectangle with offset and rotation.
  881. nvgTranslate(vg, 40,0);
  882. nvgRotate(vg, t);
  883. // Draw the intended second rectangle without any scissoring.
  884. nvgSave(vg);
  885. nvgResetScissor(vg);
  886. nvgBeginPath(vg);
  887. nvgRect(vg, -20,-10,60,30);
  888. nvgFillColor(vg, nvgRGBA(255,128,0,64));
  889. nvgFill(vg);
  890. nvgRestore(vg);
  891. // Draw second rectangle with combined scissoring.
  892. nvgIntersectScissor(vg, -20,-10,60,30);
  893. nvgBeginPath(vg);
  894. nvgRect(vg, -20,-10,60,30);
  895. nvgFillColor(vg, nvgRGBA(255,128,0,255));
  896. nvgFill(vg);
  897. nvgRestore(vg);
  898. }
  899. void renderDemo(NVGcontext* vg, float mx, float my, float width, float height,
  900. float t, int blowup, DemoData* data)
  901. {
  902. float x,y,popy;
  903. drawEyes(vg, width - 250, 50, 150, 100, mx, my, t);
  904. drawParagraph(vg, width - 450, 50, 150, 100, mx, my);
  905. drawGraph(vg, 0, height/2, width, height/2, t);
  906. drawColorwheel(vg, width - 300, height - 300, 250.0f, 250.0f, t);
  907. // Line joints
  908. drawLines(vg, 120, height-50, 600, 50, t);
  909. // Line caps
  910. drawWidths(vg, 10, 50, 30);
  911. // Line caps
  912. drawCaps(vg, 10, 300, 30);
  913. drawScissor(vg, 50, height-80, t);
  914. nvgSave(vg);
  915. if (blowup) {
  916. nvgRotate(vg, sinf(t*0.3f)*5.0f/180.0f*NVG_PI);
  917. nvgScale(vg, 2.0f, 2.0f);
  918. }
  919. // Widgets
  920. drawWindow(vg, "Widgets `n Stuff", 50, 50, 300, 400);
  921. x = 60; y = 95;
  922. drawSearchBox(vg, "Search", x,y,280,25);
  923. y += 40;
  924. drawDropDown(vg, "Effects", x,y,280,28);
  925. popy = y + 14;
  926. y += 45;
  927. // Form
  928. drawLabel(vg, "Login", x,y, 280,20);
  929. y += 25;
  930. drawEditBox(vg, "Email", x,y, 280,28);
  931. y += 35;
  932. drawEditBox(vg, "Password", x,y, 280,28);
  933. y += 38;
  934. drawCheckBox(vg, "Remember me", x,y, 140,28);
  935. drawButton(vg, ICON_LOGIN, "Sign in", x+138, y, 140, 28, nvgRGBA(0,96,128,255));
  936. y += 45;
  937. // Slider
  938. drawLabel(vg, "Diameter", x,y, 280,20);
  939. y += 25;
  940. drawEditBoxNum(vg, "123.00", "px", x+180,y, 100,28);
  941. drawSlider(vg, 0.4f, x,y, 170,28);
  942. y += 55;
  943. drawButton(vg, ICON_TRASH, "Delete", x, y, 160, 28, nvgRGBA(128,16,8,255));
  944. drawButton(vg, 0, "Cancel", x+170, y, 110, 28, nvgRGBA(0,0,0,0));
  945. // Thumbnails box
  946. drawThumbnails(vg, 365, popy-30, 160, 300, data->images, 12, t);
  947. nvgRestore(vg);
  948. }
  949. static int mini(int a, int b) { return a < b ? a : b; }
  950. static void unpremultiplyAlpha(unsigned char* image, int w, int h, int stride)
  951. {
  952. int x,y;
  953. // Unpremultiply
  954. for (y = 0; y < h; y++) {
  955. unsigned char *row = &image[y*stride];
  956. for (x = 0; x < w; x++) {
  957. int r = row[0], g = row[1], b = row[2], a = row[3];
  958. if (a != 0) {
  959. row[0] = (int)mini(r*255/a, 255);
  960. row[1] = (int)mini(g*255/a, 255);
  961. row[2] = (int)mini(b*255/a, 255);
  962. }
  963. row += 4;
  964. }
  965. }
  966. // Defringe
  967. for (y = 0; y < h; y++) {
  968. unsigned char *row = &image[y*stride];
  969. for (x = 0; x < w; x++) {
  970. int r = 0, g = 0, b = 0, a = row[3], n = 0;
  971. if (a == 0) {
  972. if (x-1 > 0 && row[-1] != 0) {
  973. r += row[-4];
  974. g += row[-3];
  975. b += row[-2];
  976. n++;
  977. }
  978. if (x+1 < w && row[7] != 0) {
  979. r += row[4];
  980. g += row[5];
  981. b += row[6];
  982. n++;
  983. }
  984. if (y-1 > 0 && row[-stride+3] != 0) {
  985. r += row[-stride];
  986. g += row[-stride+1];
  987. b += row[-stride+2];
  988. n++;
  989. }
  990. if (y+1 < h && row[stride+3] != 0) {
  991. r += row[stride];
  992. g += row[stride+1];
  993. b += row[stride+2];
  994. n++;
  995. }
  996. if (n > 0) {
  997. row[0] = r/n;
  998. row[1] = g/n;
  999. row[2] = b/n;
  1000. }
  1001. }
  1002. row += 4;
  1003. }
  1004. }
  1005. }
  1006. static void setAlpha(unsigned char* image, int w, int h, int stride, unsigned char a)
  1007. {
  1008. int x, y;
  1009. for (y = 0; y < h; y++) {
  1010. unsigned char* row = &image[y*stride];
  1011. for (x = 0; x < w; x++)
  1012. row[x*4+3] = a;
  1013. }
  1014. }
  1015. static void flipHorizontal(unsigned char* image, int w, int h, int stride)
  1016. {
  1017. int i = 0, j = h-1, k;
  1018. while (i < j) {
  1019. unsigned char* ri = &image[i * stride];
  1020. unsigned char* rj = &image[j * stride];
  1021. for (k = 0; k < w*4; k++) {
  1022. unsigned char t = ri[k];
  1023. ri[k] = rj[k];
  1024. rj[k] = t;
  1025. }
  1026. i++;
  1027. j--;
  1028. }
  1029. }
  1030. void saveScreenShot(int w, int h, int premult, const char* name)
  1031. {
  1032. unsigned char* image = (unsigned char*)malloc(w*h*4);
  1033. if (image == NULL)
  1034. return;
  1035. glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, image);
  1036. if (premult)
  1037. unpremultiplyAlpha(image, w, h, w*4);
  1038. else
  1039. setAlpha(image, w, h, w*4, 255);
  1040. flipHorizontal(image, w, h, w*4);
  1041. stbi_write_png(name, w, h, 4, image, w*4);
  1042. free(image);
  1043. }