*/}}

la_tns_shape.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * LaGUI: A graphical application framework.
  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. #include <math.h>
  20. extern LA MAIN;
  21. extern tnsMain *T;
  22. tnsShape* tnsNewShape(tnsShapeObject* so){
  23. tnsShape* s=memAcquireSimple(sizeof(tnsShape));
  24. lstAppendItem(&so->Shapes,s); return s;
  25. }
  26. tnsSPoint* tnsNewSPoint(tnsShape* s,real x, real y,real ldx,real ldy, real rdx,real rdy){
  27. tnsSPoint* sp=memAcquireSimple(sizeof(tnsSPoint));
  28. tnsVectorSet2(sp->p,x,y); tnsVectorSet2(sp->dl,ldx,ldy); tnsVectorSet2(sp->dr,rdx,rdy);
  29. lstAppendItem(&s->Points,sp); return sp;
  30. }
  31. void tnsInitShapeSquare(tnsShapeObject* so, real size){
  32. tnsShape* s=tnsNewShape(so);
  33. tnsNewSPoint(s,-size,-size,0,0,0,0)->flags|=TNS_MESH_FLAG_SELECTED;
  34. tnsNewSPoint(s,-size,size,0,0,0,0)->flags|=TNS_MESH_FLAG_SELECTED;
  35. tnsNewSPoint(s,size,size,0,0,0,0)->flags|=TNS_MESH_FLAG_SELECTED;
  36. tnsNewSPoint(s,size,-size,0,0,0,0)->flags|=TNS_MESH_FLAG_SELECTED;
  37. }
  38. void tns_DrawShape(tnsShape* s, real* override_color){
  39. if(!s->Points.pFirst) return;
  40. NVGcontext* vg=MAIN.CurrentWindow->nvg; nvgBeginPath(vg);
  41. tnsSPoint* sp1=s->Points.pFirst; nvgMoveTo(vg,sp1->p[0],sp1->p[1]);
  42. for(tnsSPoint* sp=sp1->Item.pNext;sp;sp=sp->Item.pNext){
  43. nvgLineTo(vg,sp->p[0],sp->p[1]);
  44. }
  45. nvgFillColor(vg, override_color?nvgRGBA(LA_COLOR4(override_color)):nvgRGBAf(0.8,0.8,0.8,1));
  46. nvgFill(vg);
  47. }
  48. void tnsDrawShapeObject(tnsEvaluatedInstance* ei,la3DObjectDrawExtra* de){
  49. tnsShapeObject* so=ei->Object;
  50. for(tnsShape*s=so->Shapes.pFirst;s;s=s->Item.pNext){
  51. tns_DrawShape(s,0);
  52. }
  53. }
  54. void tnsDrawShapeObjectSelectionID(tnsEvaluatedInstance* ei, void* unused){
  55. tnsShapeObject* so=ei->Object;
  56. int i=ei->InstanceSelectionID; real color[4]={0,0,0,1}; TNS_ID_TO_COLOR(color,i);
  57. for(tnsShape*s=so->Shapes.pFirst;s;s=s->Item.pNext){
  58. tns_DrawShape(s,color);
  59. }
  60. }
  61. void tnsEvaluateShapeObject(tnsShapeObject* so, tnsEvaluateData* ed){
  62. tnsAddEvaluatedInstance(ed,so,tnsDrawShapeObject,TNS_EVAL_LAYER_SOLID,0,0,0);
  63. if(ed->FillSelectionID){
  64. tnsAddEvaluatedInstance(ed,so,tnsDrawShapeObjectSelectionID,TNS_EVAL_LAYER_SELECTION,0,1,so->Base.SelectID);
  65. }
  66. }
  67. tnsShapeObject *tnsCreateShapeEmpty(tnsObject *under, char *Name, real AtX, real AtY, real AtZ){
  68. tnsShapeObject *so = memAcquireHyper(sizeof(tnsShapeObject));
  69. tnsInitObjectBase(&so->Base, under, Name, TNS_OBJECT_SHAPE, AtX, AtY, AtZ, 0, 0, 0, 1.0f, TNS_ROTATION_XYZ_EULER, 1.0f);
  70. return so;
  71. }
  72. tnsShapeObject *tnsCreateShapeSquare(tnsObject *under, char *Name, real AtX, real AtY, real AtZ, real size){
  73. tnsShapeObject *so=tnsCreateShapeEmpty(under, Name, AtX, AtY, AtZ);
  74. tnsInitShapeSquare(so, size);
  75. return so;
  76. }