*/}}

nvgtest.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #define NANOVG_GL3_IMPLEMENTATION
  19. #include "la_5.h"
  20. extern LA MAIN;
  21. extern tnsMain* T;
  22. void MyPanel(laUiList *uil, laPropPack *This, laPropPack *DetachedProps, laColumn *UNUSED, int context){
  23. laColumn* c=laFirstColumn(uil);
  24. laShowCanvas(uil,c,0,"tns","my_nanovg_canvas",-1);
  25. }
  26. typedef struct MyCanvas{
  27. laCanvasExtra e;
  28. NVGcontext* c;
  29. }MyCanvas;
  30. /* From NanoVG */
  31. void drawColorwheel(NVGcontext* vg, float x, float y, float w, float h, float t)
  32. {
  33. int i;
  34. float r0, r1, ax,ay, bx,by, cx,cy, aeps, r;
  35. float hue = sinf(t * 0.12f);
  36. NVGpaint paint;
  37. nvgSave(vg);
  38. /* nvgBeginPath(vg);
  39. nvgRect(vg, x,y,w,h);
  40. nvgFillColor(vg, nvgRGBA(255,0,0,128));
  41. nvgFill(vg);*/
  42. cx = x + w*0.5f;
  43. cy = y + h*0.5f;
  44. r1 = (w < h ? w : h) * 0.5f - 5.0f;
  45. r0 = r1 - 20.0f;
  46. aeps = 0.5f / r1; // half a pixel arc length in radians (2pi cancels out).
  47. for (i = 0; i < 6; i++) {
  48. float a0 = (float)i / 6.0f * NVG_PI * 2.0f - aeps;
  49. float a1 = (float)(i+1.0f) / 6.0f * NVG_PI * 2.0f + aeps;
  50. nvgBeginPath(vg);
  51. nvgArc(vg, cx,cy, r0, a0, a1, NVG_CW);
  52. nvgArc(vg, cx,cy, r1, a1, a0, NVG_CCW);
  53. nvgClosePath(vg);
  54. ax = cx + cosf(a0) * (r0+r1)*0.5f;
  55. ay = cy + sinf(a0) * (r0+r1)*0.5f;
  56. bx = cx + cosf(a1) * (r0+r1)*0.5f;
  57. by = cy + sinf(a1) * (r0+r1)*0.5f;
  58. 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));
  59. nvgFillPaint(vg, paint);
  60. nvgFill(vg);
  61. }
  62. nvgBeginPath(vg);
  63. nvgCircle(vg, cx,cy, r0-0.5f);
  64. nvgCircle(vg, cx,cy, r1+0.5f);
  65. nvgStrokeColor(vg, nvgRGBA(0,0,0,64));
  66. nvgStrokeWidth(vg, 1.0f);
  67. nvgStroke(vg);
  68. // Selector
  69. nvgSave(vg);
  70. nvgTranslate(vg, cx,cy);
  71. nvgRotate(vg, hue*NVG_PI*2);
  72. // Marker on
  73. nvgStrokeWidth(vg, 2.0f);
  74. nvgBeginPath(vg);
  75. nvgRect(vg, r0-1,-3,r1-r0+2,6);
  76. nvgStrokeColor(vg, nvgRGBA(255,255,255,192));
  77. nvgStroke(vg);
  78. paint = nvgBoxGradient(vg, r0-3,-5,r1-r0+6,10, 2,4, nvgRGBA(0,0,0,128), nvgRGBA(0,0,0,0));
  79. nvgBeginPath(vg);
  80. nvgRect(vg, r0-2-10,-4-10,r1-r0+4+20,8+20);
  81. nvgRect(vg, r0-2,-4,r1-r0+4,8);
  82. nvgPathWinding(vg, NVG_HOLE);
  83. nvgFillPaint(vg, paint);
  84. nvgFill(vg);
  85. // Center triangle
  86. r = r0 - 6;
  87. ax = cosf(120.0f/180.0f*NVG_PI) * r;
  88. ay = sinf(120.0f/180.0f*NVG_PI) * r;
  89. bx = cosf(-120.0f/180.0f*NVG_PI) * r;
  90. by = sinf(-120.0f/180.0f*NVG_PI) * r;
  91. nvgBeginPath(vg);
  92. nvgMoveTo(vg, r,0);
  93. nvgLineTo(vg, ax,ay);
  94. nvgLineTo(vg, bx,by);
  95. nvgClosePath(vg);
  96. paint = nvgLinearGradient(vg, r,0, ax,ay, nvgHSLA(hue,1.0f,0.5f,255), nvgRGBA(255,255,255,255));
  97. nvgFillPaint(vg, paint);
  98. nvgFill(vg);
  99. paint = nvgLinearGradient(vg, (r+ax)*0.5f,(0+ay)*0.5f, bx,by, nvgRGBA(0,0,0,0), nvgRGBA(0,0,0,255));
  100. nvgFillPaint(vg, paint);
  101. nvgFill(vg);
  102. nvgStrokeColor(vg, nvgRGBA(0,0,0,64));
  103. nvgStroke(vg);
  104. // Select circle on triangle
  105. ax = cosf(120.0f/180.0f*NVG_PI) * r*0.3f;
  106. ay = sinf(120.0f/180.0f*NVG_PI) * r*0.4f;
  107. nvgStrokeWidth(vg, 2.0f);
  108. nvgBeginPath(vg);
  109. nvgCircle(vg, ax,ay,5);
  110. nvgStrokeColor(vg, nvgRGBA(255,255,255,192));
  111. nvgStroke(vg);
  112. paint = nvgRadialGradient(vg, ax,ay, 7,9, nvgRGBA(0,0,0,64), nvgRGBA(0,0,0,0));
  113. nvgBeginPath(vg);
  114. nvgRect(vg, ax-20,ay-20,40,40);
  115. nvgCircle(vg, ax,ay,7);
  116. nvgPathWinding(vg, NVG_HOLE);
  117. nvgFillPaint(vg, paint);
  118. nvgFill(vg);
  119. nvgRestore(vg);
  120. nvgRestore(vg);
  121. }
  122. void my_CanvasInit(laUiItem *ui){
  123. MyCanvas* mye=memAcquire(sizeof(MyCanvas)); ui->Extra=mye;
  124. la_CanvasInit(ui);
  125. mye->c=nvgCreateGL3(NVG_STENCIL_STROKES|NVG_DEBUG|NVG_ANTIALIAS);
  126. }
  127. void my_CanvasDestroy(laUiItem *ui){
  128. MyCanvas* mye=ui->Extra;
  129. nvgDeleteGL3(mye->c);
  130. la_CanvasDestroy(ui);
  131. }
  132. void my_CanvasDrawCanvas(laBoxedTheme *bt, void *unused_c, laUiItem* ui){
  133. laCanvasExtra* e=ui->Extra; MyCanvas* mye=e;
  134. int W, H; W = ui->R - ui->L; H = ui->B - ui->U;
  135. tnsFlush();
  136. if (!e->OffScr || e->OffScr->pColor[0]->Height != ui->B - ui->U || e->OffScr->pColor[0]->Width != ui->R - ui->L){
  137. if (e->OffScr) tnsDelete2DOffscreen(e->OffScr);
  138. e->OffScr = tnsCreate2DOffscreen(GL_RGBA8, W, H, 0, 0, 1);
  139. }
  140. tnsDrawToOffscreen(e->OffScr,1,0);
  141. tnsViewportWithScissor(0, 0, W, H);
  142. tnsResetViewMatrix();tnsResetModelMatrix();tnsResetProjectionMatrix();
  143. 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);
  144. tnsClearColor(0,0,0,1); tnsClearAll();
  145. NVGcontext* vg=mye->c;
  146. nvgBeginFrame(vg,W,H,1);
  147. drawColorwheel(vg,0,0,W,H,1);
  148. nvgEndFrame(vg);
  149. tnsRestoreFromNanoVG();
  150. }
  151. void my_CanvasDrawOverlay(laUiItem* ui,int h){
  152. laCanvasExtra *e = ui->Extra;
  153. laBoxedTheme *bt = (*ui->Type->Theme);
  154. tnsUseImmShader(); tnsEnableShaderv(T->immShader);
  155. tnsUniformColorMode(T->immShader,0); tnsUniformOutputColorSpace(T->immShader, 0);
  156. tnsDraw2DTextureDirectly(e->OffScr->pColor[0], ui->L, ui->U, ui->R - ui->L, ui->B - ui->U);
  157. tnsFlush();
  158. la_CanvasDefaultOverlay(ui, h);
  159. }
  160. int main(int argc, char *argv[]){
  161. laGetReady();
  162. transSetLanguage("zh-CN");
  163. laCanvasTemplate* ct=laRegisterCanvasTemplate("my_nanovg_canvas", 0, 0, my_CanvasDrawCanvas, my_CanvasDrawOverlay, my_CanvasInit, my_CanvasDestroy);
  164. laRegisterUiTemplate("nanovg_panel","NanoVG Panel", MyPanel,0,0,"NanoVG Demonstration", 0,0,0);
  165. laWindow* w = laDesignWindow(-1,-1,600,600);
  166. laLayout* l = laDesignLayout(w,"My Layout");
  167. laCreatePanel(l->FirstBlock,"nanovg_panel");
  168. laStartWindow(w);
  169. laMainLoop();
  170. return 0;
  171. }