*/}}

la_tns_shape.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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?nvgRGBAf(LA_COLOR4(override_color)):nvgRGBAf(0.8,0.8,0.8,1));
  46. nvgFill(vg);
  47. }
  48. void tnsDrawShapeObjectShapes(tnsEvaluatedInstance* ei, la3DObjectDrawExtra* de, real* override_color){
  49. tnsShapeObject* so=ei->Object; NVGcontext* vg=MAIN.CurrentWindow->nvg;
  50. real sca[3]; tnsExtractScale44d(ei->Mat,sca);
  51. real rot[3]; tnsExtractXYZEuler44d(ei->Mat,rot);
  52. nvgSave(vg);
  53. if(de->Is3D){
  54. tnsVector4d pos; tnsApplyTransform44d(pos,de->mViewProj,&ei->Mat[12]);
  55. pos[0]/=pos[3]; pos[1]/=pos[3];
  56. nvgTranslate(vg,pos[0]*de->W/2,pos[1]*de->H/2);
  57. nvgScale(vg,LA_RH*sca[0],LA_RH*sca[1]); nvgRotate(vg,rot[2]);
  58. }else{
  59. nvgTranslate(vg,ei->Mat[12],-ei->Mat[13]);
  60. nvgScale(vg,sca[0],-sca[1]); nvgRotate(vg,rot[2]);
  61. }
  62. for(tnsShape*s=so->Shapes.pFirst;s;s=s->Item.pNext){ tns_DrawShape(s,override_color); }
  63. nvgRestore(vg);
  64. }
  65. void tnsDrawShapeObject(tnsEvaluatedInstance* ei, la3DObjectDrawExtra* de){
  66. tnsDrawShapeObjectShapes(ei,de,0);
  67. }
  68. void tnsDrawShapeObjectSelectionID(tnsEvaluatedInstance* ei, la3DObjectDrawExtra* de){
  69. int i=ei->InstanceSelectionID; real color[4]={0,0,0,1}; TNS_ID_TO_COLOR(color,i);
  70. tnsDrawShapeObjectShapes(ei,de,color);
  71. }
  72. void tnsDrawShapeObjectOverlay(tnsEvaluatedInstance* ei, la3DObjectDrawExtra* de){
  73. int i=ei->InstanceSelectionID; real color[4]; tnsVectorCopy4d(laAccentColor(LA_BT_ACTIVE),color);
  74. if(ei->Object!=ei->Object->InRoot->Active){ color[3]=0.4; }else{ color[3]=0.7; }
  75. tnsDrawShapeObjectShapes(ei,de,color);
  76. }
  77. void tnsEvaluateShapeObject(tnsShapeObject* so, tnsEvaluateData* ed){
  78. int DrawTo=TNS_EVAL_LAYER_SOLID;
  79. if(so->Backdrop){ DrawTo=TNS_EVAL_LAYER_BACKDROP; }
  80. tnsAddEvaluatedInstance(ed,so,tnsDrawShapeObject,DrawTo,0,0,0);
  81. if(ed->FillSelectionID){
  82. tnsAddEvaluatedInstance(ed,so,tnsDrawShapeObjectSelectionID,TNS_EVAL_LAYER_SELECTION,0,1,so->Base.SelectID);
  83. }
  84. if(ed->FillOutline && (!ed->OverrideID)){
  85. if((so->Base.Flags&TNS_OBJECT_FLAGS_SELECTED) && (so->Mode!=TNS_MESH_EDIT_MODE)){
  86. tnsAddEvaluatedInstance(ed,so,tnsDrawShapeObjectOverlay,TNS_EVAL_LAYER_OVERLAY,0,0,0);
  87. }
  88. }
  89. }
  90. tnsShapeObject *tnsCreateShapeEmpty(tnsObject *under, char *Name, real AtX, real AtY, real AtZ){
  91. tnsShapeObject *so = memAcquireHyper(sizeof(tnsShapeObject));
  92. tnsInitObjectBase(&so->Base, under, Name, TNS_OBJECT_SHAPE, AtX, AtY, AtZ, 0, 0, 0, 1.0f, TNS_ROTATION_XYZ_EULER, 1.0f);
  93. return so;
  94. }
  95. tnsShapeObject *tnsCreateShapeSquare(tnsObject *under, char *Name, real AtX, real AtY, real AtZ, real size){
  96. tnsShapeObject *so=tnsCreateShapeEmpty(under, Name, AtX, AtY, AtZ);
  97. tnsInitShapeSquare(so, size);
  98. return so;
  99. }