*/}}

la_tns_shape.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. tnsSPoint* tnsShapeInsertSPointAt(tnsShape* s, tnsSPoint* pivot, int before){
  32. tnsSPoint* sp=memAcquireSimple(sizeof(tnsSPoint));
  33. tnsVectorSet2v(sp->p,pivot->p); tnsVectorSet2v(sp->dl,pivot->dl); tnsVectorSet2v(sp->dr,pivot->dr);
  34. if(before){ lstInsertItemBefore(&s->Points,sp,pivot); }
  35. else{ lstInsertItemAfter(&s->Points,sp,pivot); }
  36. }
  37. void tnsShapeSetHole(tnsShape* s, int closed, int toggle){
  38. if(toggle) tnsShapeSetHole(s,!(s->flags&TNS_SHAPE_HOLE),0);
  39. else{ if(closed) s->flags|=TNS_SHAPE_HOLE; else s->flags&=(~TNS_SHAPE_HOLE); }
  40. }
  41. void tnsShapeSetClosed(tnsShape* s, int closed, int toggle){
  42. if(toggle) tnsShapeSetClosed(s,!(s->flags&TNS_SHAPE_CLOSED),0);
  43. else{ if(closed) s->flags|=TNS_SHAPE_CLOSED; else s->flags&=(~TNS_SHAPE_CLOSED); }
  44. }
  45. void tnsShapeSetEndPoint(tnsShape* s, tnsSPoint* p){
  46. tnsSPoint* ep=s->Points.pLast, *sp=s->Points.pFirst, *np=p->Item.pNext; if(!np || ep==sp || sp->Item.pNext==ep) return;
  47. sp->Item.pPrev=ep; ep->Item.pNext=sp; np->Item.pPrev=0; p->Item.pNext=0;
  48. s->Points.pFirst=np; s->Points.pLast=p;
  49. }
  50. void tnsShapeClearExtraFlags(tnsShapeObject* so){
  51. for(tnsShape* s=so->Shapes.pFirst;s;s=s->Item.pNext){ s->flags&=(~TNS_MESH_FLAG_PICKED);
  52. for(tnsSPoint* sp=s->Points.pFirst;sp;sp=sp->Item.pNext){ sp->flags&=(~TNS_MESH_FLAG_PICKED); }
  53. }
  54. }
  55. void tnsShapeRefreshIndex(tnsShapeObject* so){
  56. u32bit i=0; tnsShape* NextS; for(tnsShape* s=so->Shapes.pFirst;s;s=NextS){ NextS=s->Item.pNext;
  57. if(!s->Points.pFirst){ lstRemoveItem(&so->Shapes,s); memLeave(s); continue; }
  58. for(tnsSPoint* sp=s->Points.pFirst;sp;sp=sp->Item.pNext){ sp->i=i; i++; }
  59. }
  60. }
  61. int tnsShapePointAnySelected(tnsShape* s){
  62. for(tnsSPoint* sp=s->Points.pFirst;sp;sp=sp->Item.pNext){ if(sp->flags&TNS_SPOINT_SELECTED) return 1; } return 0;
  63. }
  64. int tnsShapeAnySelected(tnsShapeObject* so){
  65. for(tnsShape* s=so->Shapes.pFirst;s;s=s->Item.pNext){ if(tnsShapePointAnySelected(s)) return 1; } return 0;
  66. }
  67. void tnsShapeDeselectAll(tnsShapeObject* so){
  68. for(tnsShape* s=so->Shapes.pFirst;s;s=s->Item.pNext){
  69. for(tnsSPoint* sp=s->Points.pFirst;sp;sp=sp->Item.pNext){ sp->flags&=(~TNS_SPOINT_SELECTED); }
  70. }
  71. }
  72. void tnsShapeSelectAll(tnsShapeObject* so){
  73. for(tnsShape* s=so->Shapes.pFirst;s;s=s->Item.pNext){
  74. for(tnsSPoint* sp=s->Points.pFirst;sp;sp=sp->Item.pNext){ sp->flags|=TNS_SPOINT_SELECTED; }
  75. }
  76. }
  77. void tnsShapeSelectPoint(tnsShapeObject* so, tnsSPoint* sp, int select, int toggle, int lr){
  78. if(!so) return; int flag=(lr==1)?TNS_SPOINT_SELECTED_L:(lr==2)?TNS_SPOINT_SELECTED_R:(TNS_SPOINT_SELECTED);
  79. if(!(sp->flags&TNS_SPOINT_BEZIER)){ flag=TNS_SPOINT_SELECTED; }
  80. if(toggle) tnsShapeSelectPoint(so,sp,((sp->flags&flag)==flag?0:1),0,lr);
  81. elif(select) sp->flags|=flag; else sp->flags&=(~flag);
  82. //if(!so->FirstSelectV) so->FirstSelectV=sp; so->LastSelectV=sp;
  83. }
  84. void tnsShapeSelectRingFrom(tnsShapeObject* so, tnsSPoint* sp, int select, int toggle){
  85. int has_idle=0; laListHandle spl={sp->Item.pPrev,sp->Item.pNext};
  86. while(spl.pFirst && ((laListItem*)spl.pFirst)->pPrev){ spl.pFirst=((laListItem*)spl.pFirst)->pPrev; }
  87. while(spl.pLast && ((laListItem*)spl.pLast)->pNext){ spl.pLast=((laListItem*)spl.pLast)->pNext; }
  88. if(toggle){
  89. for(tnsSPoint* isp=spl.pFirst;isp;isp=isp->Item.pNext){
  90. if(isp!=sp && (!(isp->flags&TNS_SPOINT_SELECTED))) has_idle=1;
  91. if(has_idle){ break; }
  92. }
  93. if(has_idle){ select=1; }else{ select=0; }
  94. }
  95. for(tnsSPoint* isp=spl.pFirst;isp;isp=isp->Item.pNext){
  96. if(select){ isp->flags|=TNS_SPOINT_SELECTED; }else{ isp->flags&=(~TNS_SPOINT_SELECTED); }
  97. }
  98. }
  99. void tnsShapeSelectLinked(tnsShapeObject* so){
  100. for(tnsShape* s=so->Shapes.pFirst;s;s=s->Item.pNext){
  101. if(!tnsShapePointAnySelected(s)){ continue; }
  102. for(tnsSPoint* sp=s->Points.pFirst;sp;sp=sp->Item.pNext){ sp->flags|=TNS_SPOINT_SELECTED; }
  103. }
  104. }
  105. tnsShape* tnsShapeRemovePoint(tnsShapeObject* so, tnsShape* s, tnsSPoint* sp, int SplitShape){
  106. if(SplitShape && sp->Item.pNext && sp->Item.pPrev){
  107. if(s->flags&TNS_SHAPE_CLOSED){ tnsShapeSetEndPoint(s,sp); }
  108. tnsShape* ns=tnsNewShape(so); ns->mat=s->mat;
  109. tnsSPoint* NextIP; for(tnsSPoint* ip=sp->Item.pNext;ip;ip=NextIP){ NextIP=ip->Item.pNext;
  110. tnsSPoint* nsp=tnsNewSPoint(ns,ip->p[0],ip->p[1],ip->dl[0],ip->dl[1],ip->dr[0],ip->dr[1]);
  111. nsp->flags=ip->flags; lstRemoveItem(&s->Points,ip);
  112. }
  113. s->flags&=(~TNS_SHAPE_CLOSED); ns->flags&=(~TNS_SHAPE_CLOSED);
  114. lstRemoveItem(&s->Points,sp); memLeave(sp); return ns;
  115. }else{
  116. lstRemoveItem(&s->Points,sp); memLeave(sp); return 0;
  117. }
  118. }
  119. int tnsShapeConnect(tnsShapeObject* so, tnsShape* s1, tnsShape* s2, tnsSPoint* sp1, tnsSPoint* sp2){
  120. if((sp1!=s1->Points.pFirst && sp1!=s1->Points.pLast) || (sp2!=s2->Points.pFirst && sp2!=s2->Points.pLast)) return 0;
  121. if(sp1==s1->Points.pFirst){ lstReverse(&s1->Points);for(tnsSPoint*sp=s1->Points.pFirst;sp;sp=sp->Item.pNext){ LA_SWAP(real,sp->dl[0],sp->dr[0]); LA_SWAP(real,sp->dl[1],sp->dr[1]); } }
  122. if(sp2==s2->Points.pLast){ lstReverse(&s2->Points);for(tnsSPoint*sp=s2->Points.pFirst;sp;sp=sp->Item.pNext){ LA_SWAP(real,sp->dl[0],sp->dr[0]); LA_SWAP(real,sp->dl[1],sp->dr[1]); } }
  123. tnsSPoint* ip;while(ip=lstPopItem(&s2->Points)){
  124. tnsSPoint* nsp=tnsNewSPoint(s1,ip->p[0],ip->p[1],ip->dl[0],ip->dl[1],ip->dr[0],ip->dr[1]);
  125. nsp->flags=ip->flags; memLeave(ip);
  126. }
  127. lstRemoveItem(&so->Shapes.pFirst,s2); memLeave(s2); tnsShapeRefreshIndex(so);
  128. return 1;
  129. }
  130. int tnsShapeConnectSelected(tnsShapeObject* so){
  131. tnsShape* s1=0,*s2=0; tnsSPoint* sp1=0,*sp2=0;
  132. for(tnsShape*s=so->Shapes.pFirst;s;s=s->Item.pNext){ if(s->flags&TNS_SHAPE_CLOSED) continue;
  133. if(!s1){
  134. if(((tnsSPoint*)s->Points.pFirst)->flags&TNS_MESH_FLAG_SELECTED){ s1=s;sp1=s->Points.pFirst; }
  135. elif(((tnsSPoint*)s->Points.pLast)->flags&TNS_MESH_FLAG_SELECTED){ s1=s;sp1=s->Points.pLast; }
  136. }else{
  137. if(((tnsSPoint*)s->Points.pFirst)->flags&TNS_MESH_FLAG_SELECTED){ s2=s;sp2=s->Points.pFirst; break; }
  138. elif(((tnsSPoint*)s->Points.pLast)->flags&TNS_MESH_FLAG_SELECTED){ s2=s;sp2=s->Points.pLast; break; }
  139. }
  140. }
  141. if(s1&&s2&&sp1&&sp2){ return tnsShapeConnect(so,s1,s2,sp1,sp2); } return 0;
  142. }
  143. int tnsShapeExtrudeSelected(tnsShapeObject* so, int DupliOnly){
  144. laListHandle* pairs; tnsShape* NextS; int any=0;
  145. for(tnsShape* s=so->Shapes.pFirst;s;s=NextS){ NextS=s->Item.pNext;
  146. if(s->flags&TNS_MESH_FLAG_PICKED){ continue; }
  147. tnsSPoint* PrevSP=0,*NextSP=0,*nsp=0;
  148. tnsShape* ns=0; int broken=0;
  149. for(tnsSPoint* sp=s->Points.pFirst;sp;sp=sp->Item.pNext){
  150. if(!(sp->flags&TNS_MESH_FLAG_SELECTED)){ broken=1; break; }
  151. }
  152. if(!broken){ any=1;
  153. tnsShape* ns=tnsNewShape(so); for(tnsSPoint* sp=s->Points.pFirst;sp;sp=sp->Item.pNext){
  154. tnsSPoint* nsp=tnsNewSPoint(ns,sp->p[0],sp->p[1],sp->dl[0],sp->dl[1],sp->dr[0],sp->dr[1]); nsp->flags=sp->flags;
  155. }
  156. ns->flags=(s->flags|TNS_MESH_FLAG_PICKED); ns->mat=s->mat;
  157. for(tnsSPoint* sp=s->Points.pFirst;sp;sp=sp->Item.pNext){ sp->flags&=(~TNS_SPOINT_SELECTED); } continue;
  158. }
  159. for(tnsSPoint* sp=s->Points.pFirst;sp;sp=NextSP){ NextSP=sp->Item.pNext;
  160. if(!(sp->flags&TNS_MESH_FLAG_SELECTED)){ PrevSP=sp; continue; }
  161. int doprev=0,donext=0;
  162. if((!PrevSP) || (PrevSP->flags^sp->flags)&TNS_MESH_FLAG_SELECTED){ doprev=1; }
  163. if((!NextSP) || (NextSP->flags^sp->flags)&TNS_MESH_FLAG_SELECTED){ donext=1; }
  164. if(DupliOnly){
  165. if(doprev){ ns=tnsNewShape(so); ns->flags=(s->flags&(~TNS_SHAPE_CLOSED));
  166. ns->flags|=TNS_MESH_FLAG_PICKED; ns->mat=s->mat; any=1;
  167. }
  168. if(ns){ nsp=tnsNewSPoint(ns,sp->p[0],sp->p[1],sp->dl[0],sp->dl[1],sp->dr[0],sp->dr[1]);
  169. nsp->flags=sp->flags;
  170. }
  171. if(donext){ ns=0; }
  172. }else{
  173. if(!(doprev&&donext)){ any=1;
  174. if(doprev) tnsShapeInsertSPointAt(s,sp,1);
  175. elif(donext) tnsShapeInsertSPointAt(s,sp,0);
  176. }else{
  177. if((!NextSP) && donext){ tnsShapeInsertSPointAt(s,sp,1); }
  178. else{ tnsShapeInsertSPointAt(s,sp,0); }
  179. }
  180. }
  181. PrevSP=sp;
  182. }
  183. }
  184. if(DupliOnly){
  185. for(tnsShape* s=so->Shapes.pFirst;s;s=NextS){ NextS=s->Item.pNext;
  186. if(s->flags&TNS_MESH_FLAG_PICKED){ continue; }
  187. for(tnsSPoint* sp=s->Points.pFirst;sp;sp=sp->Item.pNext){ sp->flags&=(~TNS_SPOINT_SELECTED); }
  188. }
  189. }
  190. tnsShapeClearExtraFlags(so);
  191. tnsShapeRefreshIndex(so);
  192. return any;
  193. }
  194. void tnsShapeEnterEditMode(tnsShapeObject* so){
  195. if(so->Mode==TNS_MESH_EDIT_MODE) return;
  196. so->Mode = TNS_MESH_EDIT_MODE; tnsShapeRefreshIndex(so);
  197. tnsInvalidateEvaluation(so);
  198. }
  199. void tnsShapeLeaveEditMode(tnsShapeObject* so){
  200. if(so->Mode==TNS_MESH_OBJECT_MODE) return;
  201. so->Mode = TNS_MESH_OBJECT_MODE;
  202. tnsInvalidateEvaluation(so);
  203. }
  204. tnsShapeObject* tnsDuplicateShapeObject(tnsShapeObject* from){
  205. if(from->Base.Type!=TNS_OBJECT_SHAPE) return 0;
  206. tnsShapeObject* to = memAcquireHyper(sizeof(tnsShapeObject));
  207. tnsInitObjectBase(to, from->Base.ParentObject?from->Base.ParentObject:from->Base.InRoot, from->Base.Name->Ptr, TNS_OBJECT_SHAPE,0,0,0,0,0,0,0,0,1);
  208. tnsCopyObjectTransformationsLocal(to,from);
  209. tnsMaterialSlot* new_current=0;
  210. for(tnsMaterialSlot* ms=from->Materials.pFirst;ms;ms=ms->Item.pNext){
  211. tnsMaterialSlot* nms=tnsNewMaterialSlot(to); nms->Index=ms->Index;
  212. memAssignRef(nms,&nms->Material,ms->Material);
  213. if(ms==from->CurrentMaterial) new_current=nms;
  214. }
  215. memAssignRef(to,&to->CurrentMaterial,new_current);
  216. if(!from->Shapes.pFirst){ return to; }
  217. for(tnsShape* s=from->Shapes.pFirst;s;s=s->Item.pNext){
  218. tnsShape* ns=tnsNewShape(to); for(tnsSPoint* sp=s->Points.pFirst;sp;sp=sp->Item.pNext){
  219. tnsSPoint* nsp=tnsNewSPoint(ns,sp->p[0],sp->p[1],sp->dl[0],sp->dl[1],sp->dr[0],sp->dr[1]); nsp->flags=sp->flags;
  220. }
  221. ns->flags=s->flags;ns->mat=s->mat;
  222. }
  223. return to;
  224. }
  225. void tnsInitShapeSquare(tnsShapeObject* so, real size){
  226. tnsShape* s=tnsNewShape(so);
  227. tnsNewSPoint(s,-size,-size,0,0,0,0)->flags|=TNS_MESH_FLAG_SELECTED;
  228. tnsNewSPoint(s,-size,size,0,0,0,0)->flags|=TNS_MESH_FLAG_SELECTED;
  229. tnsNewSPoint(s,size,size,0,0,0,0)->flags|=TNS_MESH_FLAG_SELECTED;
  230. tnsNewSPoint(s,size,-size,0,0,0,0)->flags|=TNS_MESH_FLAG_SELECTED;
  231. tnsShapeSetClosed(s,1,0); tnsShapeRefreshIndex(so);
  232. }
  233. NVGpaint tns_GetMaterial2D(tnsMaterial* m){
  234. NVGcontext* vg=MAIN.CurrentWindow->nvg;
  235. if(m->GradientMode==TNS_GRADIENT_MODE_LINEAR){
  236. return nvgLinearGradient(vg,m->GradientCenter[0],m->GradientCenter[1],m->GradientSize[0],m->GradientSize[1],
  237. nvgRGBAf(LA_COLOR4(m->Color)),nvgRGBAf(LA_COLOR4(m->Color2)));
  238. }elif(m->GradientMode==TNS_GRADIENT_MODE_BOX){
  239. return nvgBoxGradient(vg,m->GradientCenter[0],m->GradientCenter[1],m->GradientSize[0],m->GradientSize[1],
  240. m->GradientBoxR,m->GradientBoxF,nvgRGBAf(LA_COLOR4(m->Color)),nvgRGBAf(LA_COLOR4(m->Color2)));
  241. }elif(m->GradientMode==TNS_GRADIENT_MODE_RADIAL){
  242. return nvgRadialGradient(vg,m->GradientCenter[0],m->GradientCenter[1],m->GradientSize[0],m->GradientSize[1],
  243. nvgRGBAf(LA_COLOR4(m->Color)),nvgRGBAf(LA_COLOR4(m->Color2)));
  244. }
  245. NVGpaint p={0}; return p;
  246. }
  247. void tns_DrawShape(tnsShape* s, tnsMaterial* mat, real* override_color, int DrawEdit, real PointScale){
  248. if(!s->Points.pFirst) return; int HasSelection=0; int closed=(s->flags&TNS_SHAPE_CLOSED);
  249. NVGcontext* vg=MAIN.CurrentWindow->nvg;
  250. if(DrawEdit==2){
  251. nvgShapeAntiAlias(vg,0);
  252. for(tnsSPoint* sp=s->Points.pFirst;sp;sp=sp->Item.pNext){ real color[4]={0,0,0,1};
  253. nvgBeginPath(vg); nvgCircle(vg,sp->p[0],sp->p[1],PointScale*1);
  254. TNS_ID_TO_COLOR(color,sp->i*3+1); nvgFillColor(vg,nvgRGBAf(LA_COLOR4(color))); nvgFill(vg);
  255. nvgBeginPath(vg); nvgCircle(vg,sp->p[0]+sp->dl[0],sp->p[1]+sp->dl[1],PointScale*1);
  256. TNS_ID_TO_COLOR(color,sp->i*3+2); nvgFillColor(vg,nvgRGBAf(LA_COLOR4(color))); nvgFill(vg);
  257. nvgBeginPath(vg); nvgCircle(vg,sp->p[0]+sp->dr[0],sp->p[1]+sp->dr[1],PointScale*1);
  258. TNS_ID_TO_COLOR(color,sp->i*3+3); nvgFillColor(vg,nvgRGBAf(LA_COLOR4(color))); nvgFill(vg);
  259. }
  260. nvgShapeAntiAlias(vg,1);
  261. return;
  262. }
  263. if(DrawEdit){ nvgBeginPath(vg); }
  264. tnsSPoint* sp1=s->Points.pFirst; nvgMoveTo(vg,sp1->p[0],sp1->p[1]); if(sp1->flags&TNS_SPOINT_SELECTED){HasSelection=1;}
  265. for(tnsSPoint* sp=sp1->Item.pNext;sp;sp=sp->Item.pNext){
  266. if(sp->flags&TNS_SPOINT_SELECTED){HasSelection=1;}
  267. if(sp->Item.pPrev){ tnsSPoint* np=sp->Item.pPrev;
  268. real c1[2],c2[2]; tnsVectorSet2v(c1,np->p); tnsVectorSet2v(c2,sp->p);
  269. if(np->flags&TNS_SPOINT_BEZIER){ tnsVectorAccum2d(c1,np->dr); }
  270. if(sp->flags&TNS_SPOINT_BEZIER){ tnsVectorAccum2d(c2,sp->dl); }
  271. nvgBezierTo(vg,c1[0],c1[1],c2[0],c2[1],sp->p[0],sp->p[1]);
  272. }else{
  273. nvgLineTo(vg,sp->p[0],sp->p[1]);
  274. }
  275. }
  276. if(closed && (sp1!=s->Points.pLast)){
  277. tnsSPoint* np=s->Points.pLast; tnsSPoint* sp=sp1;
  278. real c1[2],c2[2]; tnsVectorSet2v(c1,np->p); tnsVectorSet2v(c2,sp->p);
  279. if(np->flags&TNS_SPOINT_BEZIER){ tnsVectorAccum2d(c1,np->dr); }
  280. if(sp->flags&TNS_SPOINT_BEZIER){ tnsVectorAccum2d(c2,sp->dl); }
  281. nvgBezierTo(vg,c1[0],c1[1],c2[0],c2[1],sp->p[0],sp->p[1]);
  282. nvgClosePath(vg);
  283. nvgPathWinding(vg,(s->flags&TNS_SHAPE_HOLE)?NVG_HOLE:NVG_SOLID);
  284. if(!(s->flags&TNS_SHAPE_HOLE)){
  285. if((!override_color) && mat && mat->GradientMode){
  286. nvgFillPaint(vg,tns_GetMaterial2D(mat));
  287. }else{
  288. nvgFillColor(vg, override_color?nvgRGBAf(LA_COLOR4(override_color)):(mat?nvgRGBAf(LA_COLOR4(mat->Color)):nvgRGBAf(0.8,0.8,0.8,1)));
  289. }
  290. }
  291. }
  292. if(DrawEdit || (!closed)){
  293. tnsVector4d color; tnsVectorCopy4d(laAccentColor(LA_BT_VERTEX),color);
  294. real* ActiveColor=laAccentColor(LA_BT_SVERTEX);
  295. if(DrawEdit && (!HasSelection)) color[3]*=0.4;
  296. nvgStrokeColor(vg,override_color?nvgRGBAf(LA_COLOR4(override_color)):nvgRGBAf(LA_COLOR4(color)));
  297. nvgStrokeWidth(vg,PointScale*4);
  298. if(!DrawEdit){nvgShapeAntiAlias(vg,0);nvgStroke(vg);nvgShapeAntiAlias(vg,1);return;}
  299. nvgStroke(vg);
  300. for(tnsSPoint* sp=s->Points.pFirst;sp;sp=sp->Item.pNext){ int mainsel=sp->flags&TNS_MESH_FLAG_SELECTED;
  301. if(sp->flags&(TNS_SPOINT_FREE|TNS_SPOINT_ALIGNED)){
  302. nvgStrokeWidth(vg,PointScale*2);
  303. nvgStrokeColor(vg,(mainsel||(sp->flags&TNS_SPOINT_SELECTED_L))?nvgRGBAf(LA_COLOR4(ActiveColor)):nvgRGBAf(LA_COLOR4(color)));
  304. nvgBeginPath(vg); nvgMoveTo(vg,sp->p[0]+sp->dl[0],sp->p[1]+sp->dl[1]); nvgLineTo(vg,sp->p[0],sp->p[1]);
  305. nvgStroke(vg);
  306. nvgBeginPath(vg); nvgCircle(vg,sp->p[0]+sp->dl[0],sp->p[1]+sp->dl[1],PointScale*5);
  307. nvgStroke(vg);
  308. nvgStrokeColor(vg,(mainsel||(sp->flags&TNS_SPOINT_SELECTED_R))?nvgRGBAf(LA_COLOR4(ActiveColor)):nvgRGBAf(LA_COLOR4(color)));
  309. nvgBeginPath(vg); nvgMoveTo(vg,sp->p[0]+sp->dr[0],sp->p[1]+sp->dr[1]); nvgLineTo(vg,sp->p[0],sp->p[1]);
  310. nvgStroke(vg);
  311. nvgBeginPath(vg); nvgCircle(vg,sp->p[0]+sp->dr[0],sp->p[1]+sp->dr[1],PointScale*5);
  312. nvgStroke(vg);
  313. }
  314. nvgBeginPath(vg); nvgCircle(vg,sp->p[0],sp->p[1],PointScale*5);
  315. nvgFillColor(vg,mainsel?nvgRGBAf(LA_COLOR4(ActiveColor)):nvgRGBAf(LA_COLOR4(color)));
  316. nvgFill(vg);
  317. }
  318. }
  319. }
  320. void tnsDrawShapeObjectShapes(tnsEvaluatedInstance* ei, la3DObjectDrawExtra* de, real* override_color, int DrawEdit){
  321. tnsShapeObject* so=ei->Object; NVGcontext* vg=MAIN.CurrentWindow->nvg;
  322. real sca[3]; tnsExtractScale44d(ei->Mat,sca);
  323. real rot[3]; tnsExtractXYZEuler44d(ei->Mat,rot);
  324. nvgSave(vg);
  325. if(de->Is3D){
  326. tnsVector4d pos; tnsApplyTransform44d(pos,de->mViewProj,&ei->Mat[12]);
  327. pos[0]/=pos[3]; pos[1]/=pos[3];
  328. nvgTranslate(vg,pos[0]*de->W/2,pos[1]*de->H/2);
  329. nvgScale(vg,LA_RH*sca[0],LA_RH*sca[1]); nvgRotate(vg,rot[2]);
  330. }else{
  331. nvgTranslate(vg,ei->Mat[12],-ei->Mat[13]);
  332. nvgScale(vg,sca[0],-sca[1]); nvgRotate(vg,rot[2]);
  333. }
  334. if(DrawEdit!=2){ int begun=0; int ishole=0,nexthole=0;
  335. for(tnsShape*s=so->Shapes.pFirst;s;s=s->Item.pNext){ tnsMaterial* mat=0;
  336. if(de->DisplayMode==LA_CANVAS_DISPLAY_MATERIAL){
  337. for(tnsMaterialSlot* ms=so->Materials.pFirst;ms;ms=ms->Item.pNext){ if(s->mat==ms->Index){ mat=ms->Material; break; } }
  338. }
  339. if(!begun){ begun=1; nvgBeginPath(vg); }
  340. ishole=s->flags&TNS_SHAPE_HOLE; tnsShape* ns=s->Item.pNext; nexthole=ns?(ns->flags&TNS_SHAPE_HOLE):0;
  341. tns_DrawShape(s,mat,override_color,0,de->PointScale/sca[0]);
  342. if(!nexthole /*(!ns) || (ishole &&(!nexthole))*/){ nvgFill(vg); begun=0; }
  343. }
  344. }
  345. if(DrawEdit||so->Mode==TNS_MESH_EDIT_MODE){
  346. for(tnsShape*s=so->Shapes.pFirst;s;s=s->Item.pNext){
  347. tns_DrawShape(s,0,override_color,DrawEdit?DrawEdit:so->Mode==TNS_MESH_EDIT_MODE,de->PointScale/sca[0]);
  348. }
  349. }
  350. nvgRestore(vg);
  351. }
  352. void tnsDrawShapeObject(tnsEvaluatedInstance* ei, la3DObjectDrawExtra* de){
  353. tnsShapeObject* so=ei->Object; if(!so->Shapes.pFirst){ tnsDrawSingleObjectOrigin(so); }
  354. tnsDrawShapeObjectShapes(ei,de,0,0);
  355. }
  356. void tnsDrawShapeObjectSelectionID(tnsEvaluatedInstance* ei, la3DObjectDrawExtra* de){
  357. int i=ei->InstanceSelectionID; real color[4]={0,0,0,1}; TNS_ID_TO_COLOR(color,i);
  358. tnsDrawShapeObjectShapes(ei,de,color,0);
  359. }
  360. void tnsDrawShapePointsSelectionID(tnsEvaluatedInstance* ei, la3DObjectDrawExtra* de){
  361. tnsDrawShapeObjectShapes(ei,de,0,2);
  362. }
  363. void tnsDrawShapeObjectOverlay(tnsEvaluatedInstance* ei, la3DObjectDrawExtra* de){
  364. int i=ei->InstanceSelectionID; real color[4]; tnsVectorCopy4d(laAccentColor(LA_BT_ACTIVE),color);
  365. if(ei->Object!=ei->Object->InRoot->Active){ color[3]=0.4; }else{ color[3]=0.7; }
  366. tnsDrawShapeObjectShapes(ei,de,color,0);
  367. }
  368. void tnsEvaluateShapeObject(tnsShapeObject* so, tnsEvaluateData* ed){
  369. int DrawTo=TNS_EVAL_LAYER_SOLID;
  370. if(so->Backdrop){ DrawTo=TNS_EVAL_LAYER_BACKDROP; }
  371. tnsAddEvaluatedInstance(ed,so,tnsDrawShapeObject,DrawTo,0,0,0);
  372. if(ed->FillSelectionID){
  373. tnsAddEvaluatedInstance(ed,so,tnsDrawShapeObjectSelectionID,TNS_EVAL_LAYER_SELECTION,0,1,so->Base.SelectID);
  374. }
  375. if(ed->FillOutline && (!ed->OverrideID)){
  376. if((so->Base.Flags&TNS_OBJECT_FLAGS_SELECTED) && (so->Mode!=TNS_MESH_EDIT_MODE)){
  377. tnsAddEvaluatedInstance(ed,so,tnsDrawShapeObjectOverlay,TNS_EVAL_LAYER_OVERLAY,0,0,0);
  378. }
  379. }
  380. }
  381. tnsShapeObject *tnsCreateShapeEmpty(tnsObject *under, char *Name, real AtX, real AtY, real AtZ){
  382. tnsShapeObject *so = memAcquireHyper(sizeof(tnsShapeObject));
  383. tnsInitObjectBase(&so->Base, under, Name, TNS_OBJECT_SHAPE, AtX, AtY, AtZ, 0, 0, 0, 1.0f, TNS_ROTATION_XYZ_EULER, 1.0f);
  384. return so;
  385. }
  386. tnsShapeObject *tnsCreateShapeSquare(tnsObject *under, char *Name, real AtX, real AtY, real AtZ, real size){
  387. tnsShapeObject *so=tnsCreateShapeEmpty(under, Name, AtX, AtY, AtZ);
  388. tnsInitShapeSquare(so, size);
  389. return so;
  390. }
  391. int OPCHK_IsAnyPointSelected(laPropPack *This, laStringSplitor *ss){
  392. if(This && This->EndInstance){
  393. laCanvasExtra* ex=This->EndInstance; laUiItem* ui=ex->ParentUi;
  394. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  395. if(root->Active && root->Active->Type!=TNS_OBJECT_SHAPE) return 0;
  396. tnsShapeObject* so=root->Active; return tnsShapeAnySelected(so);
  397. }
  398. return 0;
  399. }
  400. int OPINV_SetPointHandle(laOperator *a, laEvent *e){
  401. laCanvasExtra* ex=a->This->EndInstance; laUiItem* ui=ex->ParentUi;
  402. tnsObject*root=ui?ui->PP.EndInstance:0;
  403. if(!root || !root->Active || root->Active->Type!=TNS_OBJECT_SHAPE) return LA_FINISHED;
  404. tnsShapeObject* so=root->Active; int ran=0;
  405. char* mode=strGetArgumentString(a->ExtraInstructionsP,"mode");
  406. if(!mode){
  407. laEnableOperatorPanel(a,a->This,e->x,e->y,200,200,0,0,0,0,0,0,0,0,e); return LA_RUNNING;
  408. }
  409. int flags=0,HandleFlags=TNS_SPOINT_ALIGNED|TNS_SPOINT_FREE;
  410. if(strSame(mode,"ALIGNED")){ flags=TNS_SPOINT_ALIGNED; }
  411. elif(strSame(mode,"FREE")){ flags=TNS_SPOINT_FREE; }
  412. tnsVector2d pl={0,0},pr={0,0};
  413. for(tnsShape*s=so->Shapes.pFirst;s;s=s->Item.pNext){
  414. for(tnsSPoint*sp=s->Points.pFirst;sp;sp=sp->Item.pNext){ if(!(sp->flags&TNS_MESH_FLAG_SELECTED)) continue;
  415. int oldf=sp->flags&HandleFlags, hasl=0,hasr=0;
  416. sp->flags&=(~HandleFlags); sp->flags|=flags; ran=1; if(!flags){ continue; }
  417. if(sp->Item.pPrev){ tnsVectorSet2v(pl,((tnsSPoint*)sp->Item.pPrev)->p); hasl=1;}
  418. if(sp->Item.pNext){ tnsVectorSet2v(pr,((tnsSPoint*)sp->Item.pNext)->p); hasr=1;}
  419. if(!hasl){
  420. if(!hasr){ tnsVectorSet2(pl,sp->p[0]-0.5,sp->p[1]); tnsVectorSet2(pr,sp->p[0]+0.5,sp->p[1]); }
  421. else{ tnsVectorSet2(pl,-pr[0]+2*sp->p[0],-pr[1]+2*sp->p[1]); }
  422. }else{
  423. if(!hasr){ tnsVectorSet2(pr,-pl[0]+2*sp->p[0],-pl[1]+2*sp->p[1]); }
  424. }
  425. if(flags==TNS_SPOINT_ALIGNED && flags!=oldf){
  426. tnsVector2d diff; tnsVectorMinus2d(diff,pr,pl); tnsVectorMultiSelf2d(diff,0.25);
  427. tnsVectorSet2v(sp->dr,diff); tnsVectorMultiSelf2d(diff,-1); tnsVectorSet2v(sp->dl,diff);
  428. }elif(flags==TNS_SPOINT_FREE && oldf!=TNS_SPOINT_ALIGNED){
  429. tnsVectorMinus2d(sp->dl,pl,sp->p); tnsVectorMultiSelf2d(sp->dl,0.3);
  430. tnsVectorMinus2d(sp->dr,pr,sp->p); tnsVectorMultiSelf2d(sp->dr,0.3);
  431. }
  432. }
  433. }
  434. if(ran){
  435. laRecordInstanceDifferences(so,"tns_shape_object"); laPushDifferences("Set point handle",TNS_HINT_GEOMETRY);
  436. laNotifyUsers("tns.world");
  437. }
  438. return LA_FINISHED;
  439. }
  440. void laui_SetPointHandle(laUiList *uil, laPropPack *pp, laPropPack *actinst, laColumn *extracol, int context){
  441. laColumn* c=laFirstColumn(uil);
  442. laShowItemFull(uil,c,pp,"_this_M_set_point_handle",0,"mode=NONE;text=Normal",0,0);
  443. laShowItemFull(uil,c,pp,"_this_M_set_point_handle",0,"mode=ALIGNED;text=Aligned",0,0);
  444. laShowItemFull(uil,c,pp,"_this_M_set_point_handle",0,"mode=FREE;text=Free",0,0);
  445. }
  446. int OPINV_SetShapeClosed(laOperator *a, laEvent *e){
  447. laCanvasExtra* ex=a->This->EndInstance; laUiItem* ui=ex->ParentUi;
  448. tnsObject*root=ui?ui->PP.EndInstance:0;
  449. if(!root || !root->Active || root->Active->Type!=TNS_OBJECT_SHAPE) return LA_FINISHED;
  450. tnsShapeObject* so=root->Active; int ran=0;
  451. char* mode=strGetArgumentString(a->ExtraInstructionsP,"mode");
  452. char* res=strGetArgumentString(a->ExtraInstructionsP,"reset");
  453. int set=2; if(strSame(mode,"CLOSE")){ set=1; }elif(strSame(mode,"OPEN")){ set=0; }
  454. int reset=0; if(strSame(res,"TRUE")){ reset=1; }
  455. for(tnsShape*s=so->Shapes.pFirst;s;s=s->Item.pNext){
  456. for(tnsSPoint*sp=s->Points.pFirst;sp;sp=sp->Item.pNext){ if(!(sp->flags&TNS_SPOINT_SELECTED)) continue;
  457. if(reset){ tnsShapeSetEndPoint(s,sp); tnsShapeRefreshIndex(so); tnsShapeSetClosed(s,0,0); }
  458. else{ tnsShapeSetClosed(s,set,set==2); } ran=1; break;
  459. }
  460. }
  461. if(ran){
  462. laRecordInstanceDifferences(so,"tns_shape_object"); laPushDifferences("Open/Close shape",TNS_HINT_GEOMETRY);
  463. laNotifyUsers("tns.world");
  464. }
  465. return LA_FINISHED;
  466. }
  467. int OPINV_SetShapeHole(laOperator *a, laEvent *e){
  468. laCanvasExtra* ex=a->This->EndInstance; laUiItem* ui=ex->ParentUi;
  469. tnsObject*root=ui?ui->PP.EndInstance:0;
  470. if(!root || !root->Active || root->Active->Type!=TNS_OBJECT_SHAPE) return LA_FINISHED;
  471. tnsShapeObject* so=root->Active; int ran=0;
  472. char* mode=strGetArgumentString(a->ExtraInstructionsP,"mode");
  473. int set=2; if(strSame(mode,"HOLE")){ set=1; }elif(strSame(mode,"FILL")){ set=0; }
  474. for(tnsShape*s=so->Shapes.pFirst;s;s=s->Item.pNext){
  475. for(tnsSPoint*sp=s->Points.pFirst;sp;sp=sp->Item.pNext){ if(!(sp->flags&TNS_SPOINT_SELECTED)) continue;
  476. tnsShapeSetHole(s,set,set==2); ran=1; break;
  477. }
  478. }
  479. if(ran){
  480. laRecordInstanceDifferences(so,"tns_shape_object"); laPushDifferences("Open/Close shape",TNS_HINT_GEOMETRY);
  481. laNotifyUsers("tns.world");
  482. }
  483. return LA_FINISHED;
  484. }
  485. int OPINV_ReorderShape(laOperator *a, laEvent *e){
  486. laCanvasExtra* ex=a->This->EndInstance; laUiItem* ui=ex->ParentUi;
  487. tnsObject*root=ui?ui->PP.EndInstance:0;
  488. if(!root || !root->Active || root->Active->Type!=TNS_OBJECT_SHAPE) return LA_FINISHED;
  489. tnsShapeObject* so=root->Active; int ran=0;
  490. char* mode=strGetArgumentString(a->ExtraInstructionsP,"direction");
  491. int set=0; if(strSame(mode,"UP")){ set=1; }elif(strSame(mode,"DOWN")){ set=0; }
  492. tnsShape* ns;
  493. for(tnsShape*s=so->Shapes.pFirst;s;s=ns){ ns=s->Item.pNext;
  494. if((!tnsShapePointAnySelected(s))||s->flags&TNS_MESH_FLAG_PICKED) continue;
  495. if(set) lstMoveUp(&so->Shapes,s); else lstMoveDown(&so->Shapes,s); s->flags|=TNS_MESH_FLAG_PICKED; ran=1;
  496. }
  497. if(ran){ tnsShapeClearExtraFlags(so);
  498. laRecordInstanceDifferences(so,"tns_shape_object"); laPushDifferences("Reorder shapes",TNS_HINT_GEOMETRY);
  499. laNotifyUsers("tns.world");
  500. }
  501. return LA_FINISHED;
  502. }
  503. void la_RegisterShapeOperators(){
  504. laPropContainer *pc; laProp *p;
  505. laOperatorType *at; laEnumProp *ep;
  506. at=laCreateOperatorType("M_set_point_handle", "Set Point Handle", "Set handle type of selected points",OPCHK_IsAnyPointSelected,0,0,OPINV_SetPointHandle,OPMOD_FinishOnData,0,0);
  507. at->UiDefine=laui_SetPointHandle;
  508. at=laCreateOperatorType("M_set_shape_closed", "Set Shape Closed", "Set shape closed or open",OPCHK_IsAnyPointSelected,0,0,OPINV_SetShapeClosed,0,0,0);
  509. at=laCreateOperatorType("M_set_shape_hole", "Set Shape Hole", "Set shape as hole or fill",OPCHK_IsAnyPointSelected,0,0,OPINV_SetShapeHole,0,0,0);
  510. at=laCreateOperatorType("M_reorder_shape", "Reorder shape", "Reorder shapes (move up/down in the stack)",OPCHK_IsAnyPointSelected,0,0,OPINV_ReorderShape,0,0,0);
  511. }