*/}}

nvgtest.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Part of LaGUI demonstration programs
  3. * Copyright (C) 2022-2023 Wu Yiming
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "la_5.h"
  19. extern LA MAIN;
  20. extern tnsMain* T;
  21. void MyPanel(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  22. laColumn* c=laFirstColumn(uil);
  23. laShowCanvas(uil,c,0,"tns","my_nanovg_canvas",-1);
  24. }
  25. typedef struct MyCanvas{
  26. laCanvasExtra e;
  27. NVGcontext* c;
  28. }MyCanvas;
  29. /* From NanoVG */
  30. void drawColorwheel(NVGcontext* vg, float x, float y, float w, float h, float t)
  31. {
  32. int i;
  33. float r0, r1, ax,ay, bx,by, cx,cy, aeps, r;
  34. float hue = sinf(t * 0.12f);
  35. NVGpaint paint;
  36. nvgSave(vg);
  37. /* nvgBeginPath(vg);
  38. nvgRect(vg, x,y,w,h);
  39. nvgFillColor(vg, nvgRGBA(255,0,0,128));
  40. nvgFill(vg);*/
  41. cx = x + w*0.5f;
  42. cy = y + h*0.5f;
  43. r1 = (w < h ? w : h) * 0.5f - 5.0f;
  44. r0 = r1 - 20.0f;
  45. aeps = 0.5f / r1; // half a pixel arc length in radians (2pi cancels out).
  46. for (i = 0; i < 6; i++) {
  47. float a0 = (float)i / 6.0f * NVG_PI * 2.0f - aeps;
  48. float a1 = (float)(i+1.0f) / 6.0f * NVG_PI * 2.0f + aeps;
  49. nvgBeginPath(vg);
  50. nvgArc(vg, cx,cy, r0, a0, a1, NVG_CW);
  51. nvgArc(vg, cx,cy, r1, a1, a0, NVG_CCW);
  52. nvgClosePath(vg);
  53. ax = cx + cosf(a0) * (r0+r1)*0.5f;
  54. ay = cy + sinf(a0) * (r0+r1)*0.5f;
  55. bx = cx + cosf(a1) * (r0+r1)*0.5f;
  56. by = cy + sinf(a1) * (r0+r1)*0.5f;
  57. 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));
  58. nvgFillPaint(vg, paint);
  59. nvgFill(vg);
  60. }
  61. nvgBeginPath(vg);
  62. nvgCircle(vg, cx,cy, r0-0.5f);
  63. nvgCircle(vg, cx,cy, r1+0.5f);
  64. nvgStrokeColor(vg, nvgRGBA(0,0,0,64));
  65. nvgStrokeWidth(vg, 1.0f);
  66. nvgStroke(vg);
  67. // Selector
  68. nvgSave(vg);
  69. nvgTranslate(vg, cx,cy);
  70. nvgRotate(vg, hue*NVG_PI*2);
  71. // Marker on
  72. nvgStrokeWidth(vg, 2.0f);
  73. nvgBeginPath(vg);
  74. nvgRect(vg, r0-1,-3,r1-r0+2,6);
  75. nvgStrokeColor(vg, nvgRGBA(255,255,255,192));
  76. nvgStroke(vg);
  77. paint = nvgBoxGradient(vg, r0-3,-5,r1-r0+6,10, 2,4, nvgRGBA(0,0,0,128), nvgRGBA(0,0,0,0));
  78. nvgBeginPath(vg);
  79. nvgRect(vg, r0-2-10,-4-10,r1-r0+4+20,8+20);
  80. nvgRect(vg, r0-2,-4,r1-r0+4,8);
  81. nvgPathWinding(vg, NVG_HOLE);
  82. nvgFillPaint(vg, paint);
  83. nvgFill(vg);
  84. // Center triangle
  85. r = r0 - 6;
  86. ax = cosf(120.0f/180.0f*NVG_PI) * r;
  87. ay = sinf(120.0f/180.0f*NVG_PI) * r;
  88. bx = cosf(-120.0f/180.0f*NVG_PI) * r;
  89. by = sinf(-120.0f/180.0f*NVG_PI) * r;
  90. nvgBeginPath(vg);
  91. nvgMoveTo(vg, r,0);
  92. nvgLineTo(vg, ax,ay);
  93. nvgLineTo(vg, bx,by);
  94. nvgClosePath(vg);
  95. paint = nvgLinearGradient(vg, r,0, ax,ay, nvgHSLA(hue,1.0f,0.5f,255), nvgRGBA(255,255,255,255));
  96. nvgFillPaint(vg, paint);
  97. nvgFill(vg);
  98. paint = nvgLinearGradient(vg, (r+ax)*0.5f,(0+ay)*0.5f, bx,by, nvgRGBA(0,0,0,0), nvgRGBA(0,0,0,255));
  99. nvgFillPaint(vg, paint);
  100. nvgFill(vg);
  101. nvgStrokeColor(vg, nvgRGBA(0,0,0,64));
  102. nvgStroke(vg);
  103. // Select circle on triangle
  104. ax = cosf(120.0f/180.0f*NVG_PI) * r*0.3f;
  105. ay = sinf(120.0f/180.0f*NVG_PI) * r*0.4f;
  106. nvgStrokeWidth(vg, 2.0f);
  107. nvgBeginPath(vg);
  108. nvgCircle(vg, ax,ay,5);
  109. nvgStrokeColor(vg, nvgRGBA(255,255,255,192));
  110. nvgStroke(vg);
  111. paint = nvgRadialGradient(vg, ax,ay, 7,9, nvgRGBA(0,0,0,64), nvgRGBA(0,0,0,0));
  112. nvgBeginPath(vg);
  113. nvgRect(vg, ax-20,ay-20,40,40);
  114. nvgCircle(vg, ax,ay,7);
  115. nvgPathWinding(vg, NVG_HOLE);
  116. nvgFillPaint(vg, paint);
  117. nvgFill(vg);
  118. nvgRestore(vg);
  119. nvgRestore(vg);
  120. }
  121. void my_CanvasInit(laUiItem *ui){
  122. MyCanvas* mye=memAcquire(sizeof(MyCanvas)); ui->Extra=mye;
  123. la_CanvasInit(ui);
  124. mye->c=nvgCreateGL3(NVG_STENCIL_STROKES|NVG_DEBUG|NVG_ANTIALIAS);
  125. }
  126. void my_CanvasDestroy(laUiItem *ui){
  127. MyCanvas* mye=ui->Extra;
  128. nvgDeleteGL3(mye->c);
  129. la_CanvasDestroy(ui);
  130. }
  131. void my_CanvasDrawCanvas(laBoxedTheme *bt, void *unused_c, laUiItem* ui){
  132. laCanvasExtra* e=ui->Extra; MyCanvas* mye=e;
  133. int W, H; W = ui->R - ui->L; H = ui->B - ui->U;
  134. tnsFlush();
  135. if (!e->OffScr || e->OffScr->pColor[0]->Height != ui->B - ui->U || e->OffScr->pColor[0]->Width != ui->R - ui->L){
  136. if (e->OffScr) tnsDelete2DOffscreen(e->OffScr);
  137. e->OffScr = tnsCreate2DOffscreen(GL_RGBA8, W, H, 0, 0, 1);
  138. }
  139. tnsDrawToOffscreen(e->OffScr,1,0);
  140. tnsViewportWithScissor(0, 0, W, H);
  141. tnsResetViewMatrix();tnsResetModelMatrix();tnsResetProjectionMatrix();
  142. tnsOrtho(e->PanX - W * e->ZoomX / 2, e->PanX + W * e->ZoomX / 2, e->PanY - e->ZoomY * H / 2, e->PanY + e->ZoomY * H / 2, 100, -100);
  143. tnsClearColor(0,0,0,1); tnsClearAll();
  144. NVGcontext* vg=mye->c;
  145. nvgBeginFrame(vg,W,H,1);
  146. drawColorwheel(vg,0,0,W,H,1);
  147. nvgEndFrame(vg);
  148. tnsRestoreFromNanoVG();
  149. }
  150. void my_CanvasDrawOverlay(laUiItem* ui,int h){
  151. laCanvasExtra *e = ui->Extra;
  152. laBoxedTheme *bt = (*ui->Type->Theme);
  153. tnsUseImmShader(); tnsEnableShaderv(T->immShader);
  154. tnsUniformColorMode(T->immShader,0); tnsUniformOutputColorSpace(T->immShader, 0);
  155. tnsDraw2DTextureDirectly(e->OffScr->pColor[0], ui->L, ui->U, ui->R - ui->L, ui->B - ui->U);
  156. tnsFlush();
  157. la_CanvasDefaultOverlay(ui, h);
  158. }
  159. int main(int argc, char *argv[]){
  160. laGetReady();
  161. transSetLanguage("zh-CN");
  162. laCanvasTemplate* ct=laRegisterCanvasTemplate("my_nanovg_canvas", 0, 0, my_CanvasDrawCanvas, my_CanvasDrawOverlay, my_CanvasInit, my_CanvasDestroy);
  163. laRegisterUiTemplate("nanovg_panel","NanoVG Panel", MyPanel,0,0,"NanoVG Demonstration", 0,0,0);
  164. laWindow* w = laDesignWindow(-1,-1,600,600);
  165. laLayout* l = laDesignLayout(w,"My Layout");
  166. laCreatePanel(l->FirstBlock,"nanovg_panel");
  167. laStartWindow(w);
  168. laMainLoop();
  169. return 0;
  170. }