*/}}
1
0

la_modelling.c 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933
  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 "nanovg.h"
  20. extern LA MAIN;
  21. extern struct _tnsMain *T;
  22. void la_ReadGLocation(tnsOffscreen* off, int x, int y,float* xyz0){
  23. glBindFramebuffer(GL_READ_FRAMEBUFFER, off->FboHandle);
  24. glReadBuffer(GL_COLOR_ATTACHMENT2); glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  25. glReadPixels(x,y,1,1, GL_RGBA, GL_FLOAT, xyz0);
  26. }
  27. int OPINV_SetCursor(laOperator *a, laEvent *e){
  28. if(!a->This || !a->This->EndInstance){ return 0; }
  29. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  30. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root || root->Type!=TNS_OBJECT_ROOT) return 0;
  31. float pos[4]; la_ReadGLocation(ex->OffScr, e->x-ui->L,ui->B-e->y,pos);
  32. if(pos[0]>-1e20){
  33. tnsVectorSet3v(root->GLocation,pos); laNotifyUsers("tns.world"); return LA_FINISHED_PASS;
  34. }else
  35. {
  36. if(!ex->ViewingCamera){ return LA_FINISHED_PASS; }
  37. real vv[4], gp[4]; tnsMatrix44d proj; tnsMatrix44d inv;
  38. real focus=ex->ViewingCamera->FocusDistance, _near=ex->ViewingCamera->ZMin, _far=ex->ViewingCamera->ZMax;
  39. tnsGetCameraViewProjection(proj,ui->R-ui->L,ui->B-ui->U,ex->ViewingCamera); tnsInverse44d(inv,proj);
  40. vv[0]=(real)(e->x-ui->L)/(real)(ui->R-ui->L)*2-1; vv[0]*=focus;
  41. vv[1]=(real)(ui->B-e->y)/(real)(ui->B-ui->U)*2-1; vv[1]*=focus;
  42. vv[2]=tnsGetRatiod(1/_near,1/_far,1/focus)*focus;
  43. vv[3]=focus;
  44. tnsApplyTransform44dTrue(gp, inv, vv);
  45. tnsVectorMultiSelf3d(gp,1/gp[3]);
  46. real vv0[3]={0,0,-1}, vv1[3], ray[3], p0[3]; real t; tnsMatrix44d inv2;
  47. tnsSelfTransformValueChanged(ex->ViewingCamera);tnsInverse44d(inv2,ex->ViewingCamera->Base.GlobalTransform);
  48. tnsApplyRotation43d(vv1,inv2,vv0);
  49. tnsVectorMulti3d(vv0,vv1,focus);
  50. tnsVectorAccum3d(vv0,ex->ViewingCamera->Base.GLocation);
  51. tnsVectorMinus3d(ray, gp, ex->ViewingCamera->Base.GLocation);
  52. tnsNormalizeSelf3d(ray);
  53. if(tnsIntersectPlaneRay(vv1,vv0,ex->ViewingCamera->Base.GLocation,ray,&t)){
  54. tnsVectorMultiSelf3d(ray,t); tnsVectorPlus3d(gp,ray,ex->ViewingCamera->Base.GLocation);
  55. }
  56. tnsVectorSet3v(root->GLocation,gp); laNotifyUsers("tns.world"); return LA_FINISHED_PASS;
  57. }
  58. return LA_FINISHED_PASS;
  59. }
  60. int OPCHK_ThereIsActiveObject(laPropPack *This, laStringSplitor *ss){
  61. if(!This || !This->EndInstance){ return 0; }
  62. laCanvasExtra* ex=This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  63. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  64. tnsMeshObject* mo=root->Active; if(!mo) return 0; return 1;
  65. }
  66. int OPINV_ToggleEdit(laOperator *a, laEvent *e){
  67. if(!a->This || !a->This->EndInstance){ return 0; }
  68. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  69. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  70. tnsObject* o=root->Active; if(!o) return 0;
  71. char* ObType=0;
  72. if(o->Type==TNS_OBJECT_MESH){ tnsMeshObject* mo=o; ObType="tns_mesh_object";
  73. if(mo->Mode==TNS_MESH_EDIT_MODE) tnsMeshLeaveEditMode(mo); else tnsMeshEnterEditMode(mo);
  74. }elif(o->Type==TNS_OBJECT_SHAPE){ tnsShapeObject* so=o; ObType="tns_shape_object";
  75. if(so->Mode==TNS_MESH_EDIT_MODE) tnsShapeLeaveEditMode(so); else tnsShapeEnterEditMode(so);
  76. }else{
  77. return LA_CANCELED;
  78. }
  79. laRecordInstanceDifferences(o, ObType); laPushDifferences("Toggle edit mode", TNS_HINT_GEOMETRY);
  80. laNotifyUsers("tns.world");
  81. return LA_FINISHED_PASS;
  82. }
  83. STRUCTURE(MSelectData){
  84. tnsOffscreen* FBO;
  85. tnsTexture* Color;
  86. void** RefsV; int nextV,maxV;
  87. void** RefsE; int nextE,maxE;
  88. };
  89. MSelectData* la_InitSelectData(int w, int h, tnsCamera* camera){
  90. MSelectData* sd=memAcquireSimple(sizeof(MSelectData));
  91. if (!sd->FBO || sd->FBO->pColor[0]->Height != h || sd->FBO->pColor[0]->Width != w){
  92. if (sd->FBO) tnsDelete2DOffscreen(sd->FBO);
  93. sd->FBO = tnsCreate2DOffscreen(GL_RGB, w, h, 0, 1, 0); sd->Color=sd->FBO->pColor[0];
  94. }
  95. tnsDrawToOffscreen(sd->FBO, 1, 0);
  96. return sd;
  97. }
  98. void la_AssignObjectSelectIDRecursive(tnsObject* root, MSelectData* sd){
  99. for(laListItemPointer*lip=root->ChildObjects.pFirst;lip;lip=lip->pNext){
  100. tnsObject* o=lip->p; if(!o) continue;
  101. arrEnsureLength(&sd->RefsV, sd->nextV, &sd->maxV, sizeof(tnsObject*));
  102. sd->RefsV[sd->nextV]=o; o->SelectID=sd->nextV; sd->nextV++;
  103. if(o->ChildObjects.pFirst){ la_AssignObjectSelectIDRecursive(o,sd); }
  104. }
  105. }
  106. void la_PopulateSelectDataObjects(MSelectData* sd, tnsObject* root, laCanvasExtra* e){
  107. tnsCamera* camera=e->ViewingCamera;
  108. arrEnsureLength(&sd->RefsV,0,&sd->maxV,sizeof(tnsObject*));
  109. sd->nextV++; // starting from 1;
  110. la_AssignObjectSelectIDRecursive(root, sd);
  111. if(sd->nextV==1) return; int w=sd->Color->Width, h=sd->Color->Height;
  112. tnsUnbindTexture(); tnsUniformUseTexture(T->immShader,0,0); tnsUseMultiplyColor(0);
  113. tnsEnableShaderv(T->SelectionShader);
  114. glDisableVertexAttribArray(T->SelectionShader->iColor); glVertexAttrib4f(T->SelectionShader->iColor,0,0,0,0);
  115. tnsViewportWithScissor(0,0,w,h);tnsResetViewMatrix();tnsResetModelMatrix();tnsResetProjectionMatrix();
  116. NVGcontext* vg=MAIN.CurrentWindow->nvg;
  117. la3DObjectDrawExtra de={0}; de.W=w; de.H=h; de.PointScale=1.0f/LA_RH;
  118. tnsMatrix44d mview,mproj; tnsRootObject* ro=root;
  119. if(ro && ro->Is2D){ la_SetCanvasOrtho(e,w,h); de.PointScale=e->ZoomX; }
  120. else{ tnsApplyCameraView(w, h, camera, mview,mproj); tnsMultiply44d(de.mViewProj,mproj,mview); de.Is3D=1; }
  121. la_BeginNVG(vg,e,w,h,ro->Is2D);
  122. glClearColor(0,0,0,0); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glEnable(GL_DEPTH_TEST);
  123. tnsInvalidateEvaluation(root); tnsSetObjectTreeEvaluationArgs(root,root->Active,1,1); tnsEvaluateObjectTree(root,0,0);
  124. tnsDrawObjectTree(root,TNS_EVAL_LAYER_SELECTION,&de,0);
  125. glDisable(GL_DEPTH_TEST);
  126. nvgEndFrame(vg); tnsRestoreFromNanoVG(); tnsEnableShaderv(T->immShader);
  127. }
  128. void la_PopulateSelectVerts(MSelectData* sd, tnsMeshObject* mo){
  129. arrEnsureLength(&sd->RefsV,0,&sd->maxV,sizeof(tnsMVert*));
  130. if(mo->Base.Type!=TNS_OBJECT_MESH||!mo->mv.pFirst){ return; }
  131. for(tnsMVert* v=mo->mv.pFirst;v;v=v->Item.pNext){
  132. arrEnsureLength(&sd->RefsV, v->i, &sd->maxV, sizeof(tnsMVert*));
  133. sd->RefsV[v->i]=v; sd->nextV=TNS_MAX2(v->i, sd->nextV);
  134. }
  135. sd->nextV++;
  136. }
  137. void la_PopulateSelectEdges(MSelectData* sd, tnsMeshObject* mo){
  138. arrEnsureLength(&sd->RefsE,0,&sd->maxE,sizeof(tnsMEdge*));
  139. if(mo->Base.Type!=TNS_OBJECT_MESH||!mo->mv.pFirst){ return; }
  140. for(tnsMEdge* e=mo->me.pFirst;e;e=e->Item.pNext){
  141. arrEnsureLength(&sd->RefsE, e->i, &sd->maxE, sizeof(tnsObject*));
  142. sd->RefsE[e->i]=e; sd->nextE=TNS_MAX2(e->i, sd->nextE);
  143. }
  144. sd->nextE++;
  145. }
  146. void la_PopulateSelectPoints(MSelectData* sd, tnsShapeObject* so){
  147. sd->nextV=0;
  148. arrEnsureLength(&sd->RefsV,sd->nextV,&sd->maxV,sizeof(tnsSPoint*));
  149. if(so->Base.Type!=TNS_OBJECT_SHAPE||!so->Shapes.pFirst){ return; }
  150. for(tnsShape*s=so->Shapes.pFirst;s;s=s->Item.pNext){
  151. for(tnsSPoint*sp=s->Points.pFirst;sp;sp=sp->Item.pNext){
  152. arrEnsureLength(&sd->RefsV, sd->nextV, &sd->maxV, sizeof(tnsSPoint*));
  153. sd->RefsV[sd->nextV]=sp; sd->nextV++;
  154. }
  155. }
  156. sd->nextV++;
  157. }
  158. void la_PopulateSelectDataPrimitives(MSelectData* sd, tnsObject* o, tnsCamera* camera, int WhatPrim, tnsObject* root, laCanvasExtra* e){
  159. int DoVerts=(WhatPrim==LA_CANVAS_SELECT_MODE_VERTS),DoEdges=(WhatPrim==LA_CANVAS_SELECT_MODE_EDGES);
  160. int Knife=(WhatPrim==LA_CANVAS_SELECT_MODE_KNIFE);
  161. tnsMeshObject* mo=o; tnsShapeObject* so=o;
  162. if(o->Type==TNS_OBJECT_MESH){
  163. if(DoVerts || Knife){ la_PopulateSelectVerts(sd,mo); }
  164. if(DoEdges || Knife){ la_PopulateSelectEdges(sd,mo); }
  165. }elif(o->Type==TNS_OBJECT_SHAPE){
  166. la_PopulateSelectPoints(sd,so);
  167. }
  168. int w=sd->Color->Width, h=sd->Color->Height;
  169. tnsUnbindTexture(); tnsUniformUseTexture(T->immShader,0,0); tnsUseMultiplyColor(0);
  170. tnsEnableShaderv(T->SelectionShader);
  171. tnsViewportWithScissor(0,0,w,h);tnsResetViewMatrix();tnsResetModelMatrix();tnsResetProjectionMatrix();
  172. NVGcontext* vg=MAIN.CurrentWindow->nvg;
  173. la3DObjectDrawExtra de={0}; de.W=w; de.H=h; de.PointScale=1.0f/LA_RH;
  174. tnsMatrix44d mview,mproj; tnsRootObject* ro=root;
  175. if(ro && ro->Is2D){ la_SetCanvasOrtho(e,w,h); de.PointScale=e->ZoomX; }
  176. else{ tnsApplyCameraView(w, h, camera, mview,mproj); tnsMultiply44d(de.mViewProj,mproj,mview); de.Is3D=1; }
  177. la_BeginNVG(vg,e,w,h,ro->Is2D);
  178. glClearColor(0,0,0,0); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  179. tnsPushMatrix(); tnsApplyModelMatrix(o->GlobalTransform);glEnable(GL_DEPTH_TEST);
  180. if(o->Type==TNS_OBJECT_MESH){
  181. if(!e->SelectThrough){
  182. glDepthMask(GL_TRUE); glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
  183. tnsUniformUseOffset(T->SelectionShader,0);
  184. tnsDrawBatch(mo->Batch,"body",0,0);
  185. glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
  186. }
  187. }
  188. glDepthMask(GL_FALSE);
  189. tnsUniformUseOffset(T->SelectionShader,1);
  190. if(o->Type==TNS_OBJECT_MESH){
  191. if(DoEdges || Knife){ tnsDrawBatch(mo->Batch, "edges_select",0,0); }
  192. if(DoVerts || Knife){ tnsDrawBatch(mo->Batch, "verts_select",0,0); }
  193. }elif(o->Type==TNS_OBJECT_SHAPE){
  194. tnsEvaluatedInstance ei; memcpy(&ei.Mat,o->GlobalTransform,sizeof(tnsMatrix44d)); ei.Object=o;
  195. tnsDrawShapePointsSelectionID(&ei,&de);
  196. }
  197. tnsUniformUseOffset(T->SelectionShader,0);
  198. tnsPopMatrix();
  199. glDisable(GL_DEPTH_TEST);
  200. nvgEndFrame(vg); tnsRestoreFromNanoVG(); tnsEnableShaderv(T->immShader);
  201. }
  202. void la_PadSelectionBuffer(uint8_t* buf, int w, int h, int sx, int sy, int ex, int ey, int real_endx){
  203. if(!sx&&!sy&&!ex&&!ey) return;
  204. uint8_t* pad=calloc(1,sizeof(uint8_t)*4*w*h);
  205. for(int i=0;i<h;i++){
  206. if(i<sy || i>=h-ey){ memset(&pad[i*w*4],0,w*4); continue; }
  207. for(int j=0;j<w;j++){
  208. if(j<sx || j>=w-sx){ memset(&pad[(i*w+j)*4],0,4); continue; }
  209. memcpy(&pad[(i*w+j)*4], &buf[((i-sy)*(real_endx-sx)+j-sx)*4], 4);
  210. }
  211. }
  212. memcpy(buf,pad,sizeof(uint8_t)*4*w*h);
  213. free(pad);
  214. }
  215. uint8_t* la_ReadSelection(MSelectData* sd, u8bit* buf, int x, int y, int w, int h){
  216. glFlush(); glFinish(); glGetError();
  217. glBindFramebuffer(GL_READ_FRAMEBUFFER, sd->FBO->FboHandle);
  218. glReadBuffer(GL_COLOR_ATTACHMENT0);
  219. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  220. glReadPixels(x,y,w,h, GL_RGBA, GL_UNSIGNED_BYTE, buf);
  221. return buf;
  222. }
  223. uint8_t* la_ReadSelectionRadius(MSelectData* sd, int uix, int uiy, int radius){
  224. int w=radius*2; int vh=sd->Color->Height,vw=sd->Color->Width;
  225. uint8_t* buf=calloc(1,sizeof(uint8_t)*4*w*w);
  226. int startx=uix-radius, starty=vh-uiy-radius, endx=uix+radius, endy=vh-uiy+radius;
  227. TNS_CLAMP(startx,0,vw);TNS_CLAMP(endx,0,vw);TNS_CLAMP(starty,0,vh);TNS_CLAMP(endy,0,vh);
  228. int _startx=TNS_MAX2(startx,0), _starty=TNS_MAX2(starty,0), _endx=TNS_MIN2(endx,vw), _endy=TNS_MIN2(endy,vh);
  229. la_ReadSelection(sd, buf, _startx, _starty, _endx-_startx, _endy-_starty);
  230. la_PadSelectionBuffer(buf, w, w, _startx-startx, _starty-starty, endx-_endx, endy-_endy,_endx);
  231. return buf;
  232. }
  233. uint8_t* la_ReadSelectionBox(MSelectData* sd, int uix, int uiy, int uix2, int uiy2){
  234. int vh=sd->Color->Height,vw=sd->Color->Width;
  235. if(uix2<uix) LA_SWAP(int,uix,uix2); if(uiy2<uiy) LA_SWAP(int,uiy,uiy2);
  236. TNS_CLAMP(uix,0,vw);TNS_CLAMP(uix2,0,vw);TNS_CLAMP(uiy,0,vh);TNS_CLAMP(uiy2,0,vh);
  237. int w=uix2-uix, h=uiy2-uiy;
  238. uint8_t* buf=calloc(1,sizeof(uint8_t)*4*w*h);
  239. int startx=uix, starty=vh-uiy2, endx=uix+w, endy=starty+h;
  240. int _startx=TNS_MAX2(startx,0), _starty=TNS_MAX2(starty,0), _endx=TNS_MIN2(endx,vw), _endy=TNS_MIN2(endy,vh);
  241. la_ReadSelection(sd, buf, _startx, _starty, _endx-_startx, _endy-_starty);
  242. la_PadSelectionBuffer(buf, w, h, _startx-startx, _starty-starty, endx-_endx, endy-_endy,_endx);
  243. return buf;
  244. }
  245. int la_SelectGetClosest(MSelectData* sd, int uix, int uiy, int radius, int *ElemType){
  246. *ElemType=0; uint8_t* buf=la_ReadSelectionRadius(sd, uix, uiy, radius); if(!buf) return 0;
  247. int w=radius*2; int MinD=INT_MAX,MinDe=INT_MAX; int MinID=0,MinIDe=0, d, elemtype=0;
  248. for(int i=0;i<w;i++){
  249. for(int j=0;j<w;j++){
  250. uint8_t* p=&buf[(i*w+j)*4]; int id=(p[0])|(p[1]<<8)|((p[2]<<16)&(~TNS_MMESH_TYPE_BIT)); if(!id){continue;}
  251. d=tnsDistIdv2(i,j, radius, radius); elemtype=((p[2]<<16)&TNS_MMESH_TYPE_BIT);
  252. if(elemtype==TNS_MMESH_EDGE_BIT){ if(d<MinDe){ MinDe=d; MinIDe=id; } }
  253. elif(d<MinD){ MinD=d; MinID=id; }
  254. //printf("%d ",buf[(i*w+j)*4]);
  255. }
  256. //printf("\n");
  257. }
  258. //printf("%d %d\n",MinID, MinIDe);
  259. free(buf);
  260. if(MinID && MinIDe){ if(MinD<MinDe*5){ *ElemType=0; return MinID; } }
  261. if(MinIDe){ *ElemType=TNS_MMESH_EDGE_BIT; return MinIDe; }
  262. if(MinID){ *ElemType=0; return MinID; }
  263. *ElemType=0; return 0;
  264. }
  265. int* la_SelectGetBox(MSelectData* sd, int uix, int uiy, int uix2, int uiy2, int* r_length){
  266. uint8_t* buf=la_ReadSelectionBox(sd, uix, uiy, uix2, uiy2); if(!buf) return 0;
  267. int vh=sd->Color->Height,vw=sd->Color->Width;
  268. if(uix2<uix) LA_SWAP(int,uix,uix2); if(uiy2<uiy) LA_SWAP(int,uiy,uiy2);
  269. TNS_CLAMP(uix,0,vw);TNS_CLAMP(uix2,0,vw);TNS_CLAMP(uiy,0,vh);TNS_CLAMP(uiy2,0,vh);
  270. int w=uix2-uix, h=uiy2-uiy;
  271. int* ids=0; int next=0,max=0;
  272. arrEnsureLength(&ids, next, &max, sizeof(int));
  273. for(int i=0;i<h;i++){
  274. for(int j=0;j<w;j++){
  275. uint8_t* p=&buf[(i*w+j)*4]; int id=(p[0])|(p[1]<<8)|((p[2]<<16)&(~TNS_MMESH_TYPE_BIT));
  276. if(id){ int found=0;
  277. for(int a=0;a<next;a++){ if(ids[a]==id){ found=1; break; } }
  278. if(!found){
  279. arrEnsureLength(&ids, next, &max, sizeof(int)); ids[next]=id; next++; }
  280. }
  281. }
  282. }
  283. free(buf);
  284. *r_length=next;
  285. return ids;
  286. }
  287. void la_FreeSelectData(MSelectData* sd){
  288. tnsDelete2DOffscreen(sd->FBO);
  289. free(sd->RefsV);
  290. free(sd->RefsE);
  291. memFree(sd);
  292. }
  293. void* la_SelectGetRef(MSelectData* sd, int id, int elemtype){
  294. if(!elemtype){ if(id>=0 && id<sd->nextV){ return sd->RefsV[id]; } }
  295. elif(elemtype==TNS_MMESH_EDGE_BIT){ if(id>=0 && id<sd->nextE){ return sd->RefsE[id]; } }
  296. return 0;
  297. }
  298. int OPCHK_ViewportAndSceneExists(laPropPack *This, laStringSplitor *ss){
  299. if(!This || !This->EndInstance){ return 0; } laCanvasExtra* ex=This->EndInstance;
  300. laUiItem* ui=ex->ParentUi; tnsObject* root=ui?ui->PP.EndInstance:0;
  301. if(!ex->ViewingCamera || !root){ return 0; }
  302. return 1;
  303. }
  304. void la_DoObjectSelect(tnsObject* root, tnsObject* o, laCanvasExtra* e, int DeselectAll, int Select, int Toggle){
  305. if(DeselectAll){ tnsDeselectAllObjects(root); memAssignRef(root,&root->Active,0); }
  306. if(o){ tnsSelectObject(o, Select, o==root->Active?0:Toggle); memAssignRef(root,&root->Active,o); }
  307. }
  308. void la_DoMeshSelect(tnsMeshObject* mo, void* p, int WhatPrim, int DeselectAll, int Select, int Toggle){
  309. if(DeselectAll){ tnsMMeshDeselectAll(mo); }
  310. if(p){ if(WhatPrim==LA_CANVAS_SELECT_MODE_VERTS) tnsMMeshSelectVert(mo,p,Select,Toggle);
  311. elif(WhatPrim==LA_CANVAS_SELECT_MODE_EDGES) tnsMMeshSelectEdge(mo,p,Select,Toggle); }
  312. }
  313. void la_DoShapeSelect(tnsShapeObject* so, void* p, int DeselectAll, int Select, int Toggle){
  314. if(DeselectAll){ tnsShapeDeselectAll(so); } if(p) tnsShapeSelectPoint(so,p,Select,Toggle);
  315. }
  316. #define LA_SELECT_MODE_BOX 1
  317. STRUCTURE(MSelectExtra){
  318. MSelectData* sd;
  319. tnsObject* root;
  320. tnsObject* mo;
  321. tnsCamera* cam;
  322. int Mode;
  323. int InSelect;
  324. laListHandle KnifeElements;
  325. void* PendingElem; int PendingElemType, BatchDirty;
  326. int IsLoop;
  327. };
  328. int OPINV_Select(laOperator *a, laEvent *e){
  329. if(!a->This || !a->This->EndInstance){ return 0; }
  330. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  331. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  332. tnsObject* o=root->Active; tnsMeshObject* mo=o; tnsShapeObject* so=o;
  333. int is_geo=0, SelectMode=ex->SelectMode, ring_band=0;
  334. if(strSame(strGetArgumentString(a->ExtraInstructionsP, "mode"), "toggle")){
  335. if(o && o->Type==TNS_OBJECT_MESH && mo->Mode==TNS_MESH_EDIT_MODE){
  336. if(tnsMMeshAnySelected(mo)) tnsMMeshDeselectAll(mo); else tnsMMeshSelectAll(mo);
  337. tnsInvalidateMeshBatch(mo); is_geo=1;
  338. }elif(o && o->Type==TNS_OBJECT_SHAPE && so->Mode==TNS_MESH_EDIT_MODE){ is_geo=1;
  339. if(tnsShapeAnySelected(so)) tnsShapeDeselectAll(so); else tnsShapeSelectAll(so);
  340. //tnsInvalidateMeshBatch(mo);
  341. }else{
  342. if(tnsAnyObjectsSelected(root)) tnsDeselectAllObjects(root); else tnsSelectAllObjects(root);
  343. }
  344. laNotifyUsers("tns.world"); laRecordAndPush(0,"tns.world", "Toggle selection",is_geo?TNS_HINT_GEOMETRY:TNS_HINT_TRANSFORM);
  345. return LA_FINISHED;
  346. }
  347. int spk=e->SpecialKeyBit & (LA_KEY_CTRL|LA_KEY_ALT);
  348. if(spk==LA_KEY_ALT){ ring_band=1; SelectMode=LA_CANVAS_SELECT_MODE_EDGES; }
  349. elif(spk==(LA_KEY_CTRL|LA_KEY_ALT)){ ring_band=2; SelectMode=LA_CANVAS_SELECT_MODE_EDGES; }
  350. MSelectData* sd=la_InitSelectData(ex->OffScr->pColor[0]->Width, ex->OffScr->pColor[0]->Height, c);
  351. int DeselectAll=1;
  352. int Append=e->SpecialKeyBit&LA_KEY_SHIFT; if(Append) DeselectAll=0;
  353. if(o&&o->Type==TNS_OBJECT_MESH && mo->Mode==TNS_MESH_EDIT_MODE){
  354. la_PopulateSelectDataPrimitives(sd, mo, ex->ViewingCamera, SelectMode, root, ex);
  355. if(strSame(strGetArgumentString(a->ExtraInstructionsP, "mode"), "box")){
  356. MSelectExtra* se=memAcquire(sizeof(MSelectExtra));
  357. ex->OnX=e->x; ex->OnX=e->y;
  358. a->CustomData=se; se->sd=sd; se->Mode=LA_SELECT_MODE_BOX; se->mo=mo; se->cam=c; ex->DrawCursor=1; se->root=root; return LA_RUNNING;
  359. }
  360. int elemtype,id=la_SelectGetClosest(sd, e->x-ui->L, e->y-ui->U, LA_RH,&elemtype)-1;
  361. void* p=la_SelectGetRef(sd,id,elemtype);
  362. la_DoMeshSelect(mo, p, SelectMode, DeselectAll, 1, 1); tnsMMeshEnsureSelection(mo,ex->SelectMode);
  363. if(ring_band && p){
  364. tnsMMeshSelectRingBandFrom(mo,p,ring_band,1,Append); tnsMMeshEnsureSelection(mo,ex->SelectMode);
  365. }
  366. tnsInvalidateMeshBatch(mo);
  367. laNotifyUsers("tns.world"); laRecordAndPush(0,"tns.world","Mesh selection",TNS_HINT_GEOMETRY);
  368. }elif(o&&o->Type==TNS_OBJECT_SHAPE && so->Mode==TNS_MESH_EDIT_MODE){
  369. la_PopulateSelectDataPrimitives(sd, mo, ex->ViewingCamera, SelectMode, root, ex);
  370. if(strSame(strGetArgumentString(a->ExtraInstructionsP, "mode"), "box")){
  371. MSelectExtra* se=memAcquire(sizeof(MSelectExtra));
  372. ex->OnX=e->x; ex->OnX=e->y;
  373. a->CustomData=se; se->sd=sd; se->Mode=LA_SELECT_MODE_BOX; se->mo=mo; se->cam=c; ex->DrawCursor=1; se->root=root; return LA_RUNNING;
  374. }
  375. int elemtype,id=la_SelectGetClosest(sd, e->x-ui->L, e->y-ui->U, LA_RH,&elemtype)-1;
  376. void* p=la_SelectGetRef(sd,id,elemtype);
  377. la_DoShapeSelect(mo, p, DeselectAll, 1, 1);
  378. if(ring_band && p){ tnsShapeSelectRingFrom(mo,p,1,Append); }
  379. laNotifyUsers("tns.world"); laRecordAndPush(0,"tns.world","Shape selection",TNS_HINT_GEOMETRY);
  380. }else{
  381. la_PopulateSelectDataObjects(sd,root,ex);
  382. if(strSame(strGetArgumentString(a->ExtraInstructionsP, "mode"), "box")){
  383. MSelectExtra* se=memAcquire(sizeof(MSelectExtra));
  384. ex->OnX=e->x; ex->OnX=e->y;
  385. a->CustomData=se; se->sd=sd; se->Mode=LA_SELECT_MODE_BOX; se->cam=c; ex->DrawCursor=1; se->root=root; return LA_RUNNING;
  386. }
  387. int elemtype,id=la_SelectGetClosest(sd, e->x-ui->L, e->y-ui->U, LA_RH*2,&elemtype);
  388. void* p=la_SelectGetRef(sd,id,elemtype); if(p){ la_DoObjectSelect(root, p, ex, DeselectAll, 1, 1); }
  389. else{ la_DoObjectSelect(root, 0, ex, DeselectAll, 1, 1); }
  390. laNotifyUsers("tns.world"); laRecordAndPush(0,"tns.world","Object selection",TNS_HINT_TRANSFORM);
  391. }
  392. la_FreeSelectData(sd);
  393. return LA_FINISHED_PASS;
  394. }
  395. int OPMOD_Select(laOperator *a, laEvent *e){
  396. if(!a->This || !a->This->EndInstance){ return 0; }
  397. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  398. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  399. tnsObject* o=root->Active; tnsMeshObject* mo=o; tnsShapeObject* so=o;
  400. MSelectExtra* se=a->CustomData;
  401. if(e->Type==LA_L_MOUSE_DOWN){ se->InSelect=1; ex->DrawCursor=2; ex->ClickedX=e->x; ex->ClickedY=e->y; laRedrawCurrentPanel(); }
  402. if(e->Type&LA_MOUSE_EVENT){ ex->OnX=e->x; ex->OnY=e->y; laRedrawCurrentPanel(); }
  403. if(e->Type==LA_R_MOUSE_DOWN || (e->Type == LA_KEY_DOWN && e->key==LA_KEY_ESCAPE)){
  404. ex->DrawCursor=0; la_FreeSelectData(se->sd); memFree(se); laNotifyUsers("tns.world"); return LA_FINISHED;
  405. }
  406. int DeselectAll=1;
  407. int Append=e->SpecialKeyBit&LA_KEY_SHIFT; if(Append) DeselectAll=0;
  408. int Remove=e->SpecialKeyBit&LA_KEY_ALT; if(Remove) DeselectAll=0;
  409. int is_geo=0;
  410. if(se->InSelect && e->Type==LA_L_MOUSE_UP){
  411. if(o && o->Type==TNS_OBJECT_MESH && mo->Mode==TNS_MESH_EDIT_MODE){ is_geo=1;
  412. la_DoMeshSelect(mo, 0, ex->SelectMode, DeselectAll, 0, 0);
  413. int len; int* ids=la_SelectGetBox(se->sd, ex->ClickedX-ui->L, ex->ClickedY-ui->U, e->x-ui->L, e->y-ui->U, &len);
  414. for(int i=0;i<len;i++){
  415. int id=ids[i]-1; void* p=la_SelectGetRef(se->sd,id,ex->SelectMode==LA_CANVAS_SELECT_MODE_EDGES?TNS_MMESH_EDGE_BIT:0);
  416. la_DoMeshSelect(mo, p, ex->SelectMode, 0, !Remove, 0);
  417. }
  418. tnsMMeshEnsureSelection(mo,ex->SelectMode);
  419. tnsInvalidateMeshBatch(mo);
  420. }elif(o && o->Type==TNS_OBJECT_SHAPE && so->Mode==TNS_MESH_EDIT_MODE){ is_geo=1;
  421. la_DoShapeSelect(so, 0, DeselectAll, 0, 0);
  422. int len; int* ids=la_SelectGetBox(se->sd, ex->ClickedX-ui->L, ex->ClickedY-ui->U, e->x-ui->L, e->y-ui->U, &len);
  423. for(int i=0;i<len;i++){
  424. int id=ids[i]-1; void* p=la_SelectGetRef(se->sd,id,ex->SelectMode==LA_CANVAS_SELECT_MODE_EDGES?TNS_MMESH_EDGE_BIT:0);
  425. la_DoShapeSelect(so, p, 0, !Remove, 0);
  426. }
  427. }else{
  428. la_DoObjectSelect(se->root, 0, ex, DeselectAll, 0, 0);
  429. int len; int* ids=la_SelectGetBox(se->sd, ex->ClickedX, ex->ClickedY, e->x-ui->L, e->y-ui->U, &len);
  430. for(int i=0;i<len;i++){
  431. int id=ids[i]; void* p=la_SelectGetRef(se->sd,id,0);
  432. if(p){ la_DoObjectSelect(se->root, p, ex, 0, !Remove, 0); }
  433. }
  434. }
  435. laNotifyUsers("tns.world"); laRecordAndPush(0,"tns.world","Box selection",is_geo?TNS_HINT_GEOMETRY:TNS_HINT_TRANSFORM);
  436. ex->DrawCursor=0;
  437. la_FreeSelectData(se->sd); memFree(se);
  438. laRedrawCurrentPanel();
  439. return LA_FINISHED;
  440. }
  441. return LA_RUNNING;
  442. }
  443. #define LA_TRANSFORM_MODE_GRAB 1
  444. #define LA_TRANSFORM_MODE_ROTATE 2
  445. #define LA_TRANSFORM_MODE_SCALE 3
  446. #define LA_TRANSFORM_RESTORE_LOC 1
  447. #define LA_TRANSFORM_RESTORE_ROT 2
  448. #define LA_TRANSFORM_RESTORE_SCA 3
  449. STRUCTURE(MTOrigObject){
  450. tnsObject* o;
  451. tnsMatrix44d Global;
  452. tnsMatrix44d Local;
  453. int Discard;
  454. };
  455. STRUCTURE(MTOrigMVert){
  456. tnsVector3d p;
  457. tnsVector3d origp;
  458. tnsMVert* mv;
  459. };
  460. STRUCTURE(MTOrigSPoint){
  461. tnsVector2d p;
  462. tnsVector2d origp;
  463. tnsSPoint* sp;
  464. };
  465. STRUCTURE(MTransformData){
  466. tnsMatrix44d Delta;
  467. tnsMatrix44d ViewProjection;
  468. tnsVector4d Up,Right,Foward;
  469. tnsVector4d TCenter,TLCenter;
  470. int CenterX,CenterY; real Initial;
  471. tnsObject* mo; tnsShapeObject* so;
  472. tnsMatrix44d obmatinv,deltainv;
  473. tnsCamera* c; tnsObject* root;
  474. int w,h;
  475. void* Originals; int next,max;
  476. int mode;
  477. int LockAxis[3];
  478. int UseLocal;
  479. real DeltaVal, UserDeltaVal;
  480. laStringEdit* Entry; int UseUserDelta;
  481. int CanvasDeltaMode;
  482. int Is2D; real zoomx,zoomy;
  483. };
  484. MTransformData* la_InitTransformData(int w, int h, tnsCamera* c, int CanvasDeltaMode, int Is2D,real zoomx,real zoomy){
  485. MTransformData* td=memAcquireSimple(sizeof(MTransformData));
  486. tnsVector4d pu={0,1,0,0}, pr={1,0,0,0}, pf={0,0,1};
  487. tnsGetCameraMovingDeltas(c,w,h,1,0,pr); pr[2]=0; pr[3]=0;
  488. tnsGetCameraMovingDeltas(c,w,h,0,-1,pu); pu[2]=0; pu[3]=0;
  489. tnsMatrix44d inv; tnsInverse44d(inv,c->Base.GlobalTransform);
  490. tnsApplyRotation43d(td->Up,inv,pu);
  491. tnsApplyRotation43d(td->Right,inv,pr);
  492. tnsApplyRotation43d(td->Foward,inv,pf);
  493. tnsGetCameraViewProjection(td->ViewProjection, w,h,c);
  494. td->c=c; td->w=w; td->h=h; td->CanvasDeltaMode=CanvasDeltaMode;
  495. strBeginEdit(&td->Entry, "");
  496. td->Is2D=Is2D; td->zoomx=zoomx; td->zoomy=zoomy;
  497. return td;
  498. }
  499. void la_GetTransformInitialScale(MTransformData* td, laUiItem* ui, int x, int y){ td->Initial=tnsDistIdv2(x-ui->L,y-ui->U,td->CenterX,td->CenterY); }
  500. void la_GetTransformInitialRotation(MTransformData* td, laUiItem* ui, int x, int y){ td->Initial=atan2(y-ui->U-td->CenterY,x-ui->L-td->CenterX); }
  501. void la_GetTransformCenter2D(MTransformData* td){
  502. tnsVector4d vp; tnsApplyTransform44d(vp,td->ViewProjection,td->TCenter);
  503. if(td->c->CameraType==TNS_CAMERA_PERSP){ tnsVectorMultiSelf3d(vp, 1/vp[3]); }
  504. td->CenterX = (vp[0]/2+0.5f)*td->w; td->CenterY=(-vp[1]/2+0.5f)*td->h;
  505. if(td->CenterX<0||td->CenterY<0) { td->CenterX=td->w/2; td->CenterY=td->h/2; }
  506. }
  507. int la_AddTransformObjectsRecursive(MTransformData* td, tnsObject*root){
  508. int any=0; for(laListItemPointer* lip=root->ChildObjects.pFirst;lip;lip=lip->pNext){
  509. tnsObject* o=lip->p; if(o->ChildObjects.pFirst) any+=la_AddTransformObjectsRecursive(td,o);
  510. if(!(o->Flags&TNS_OBJECT_FLAGS_SELECTED)) continue;
  511. arrEnsureLength(&td->Originals, td->next, &td->max, sizeof(MTOrigObject));
  512. MTOrigObject* to=arrElement(td->Originals, td->next, sizeof(MTOrigObject)); td->next++; to->o=o;
  513. memcpy(to->Global, o->GlobalTransform, sizeof(tnsMatrix44d));
  514. memcpy(to->Local, o->SelfTransform, sizeof(tnsMatrix44d));
  515. tnsVectorAccum3d(td->TCenter,o->GLocation); any++;
  516. int discard=0; tnsObject* po=o; while(po=po->ParentObject){ if(po->Flags&TNS_OBJECT_FLAGS_SELECTED){ discard=1; break;} } if(discard) to->Discard=1;
  517. }
  518. return any;
  519. }
  520. int la_PopulateTransformObjects(MTransformData* td, tnsObject* root){
  521. arrEnsureLength(&td->Originals, 0, &td->max, sizeof(MTOrigObject));
  522. int any=la_AddTransformObjectsRecursive(td,root);
  523. tnsVectorMultiSelf3d(td->TCenter, 1.0f/any);
  524. la_GetTransformCenter2D(td);
  525. return any;
  526. }
  527. int la_PopulateTransformVerticies(MTransformData* td, tnsMeshObject* mo){
  528. int any=0; td->mo=mo;
  529. arrEnsureLength(&td->Originals, 0, &td->max, sizeof(MTOrigMVert));
  530. tnsInverse44d(td->obmatinv, mo->Base.GlobalTransform);
  531. tnsInverse44d(td->deltainv, mo->Base.DeltaTransform);
  532. for(tnsMVert* mv=mo->mv.pFirst;mv;mv=mv->Item.pNext){
  533. if(!(mv->flags&TNS_MESH_FLAG_SELECTED)) continue;
  534. //printf("v %d ",mv->i);
  535. arrEnsureLength(&td->Originals, td->next, &td->max, sizeof(MTOrigMVert));
  536. MTOrigMVert* to=arrElement(td->Originals, td->next, sizeof(MTOrigMVert)); td->next++; to->mv=mv;
  537. tnsApplyTransform43d(to->p, mo->Base.GlobalTransform, mv->p);
  538. memcpy(to->origp, mv->p, sizeof(tnsVector3d)); any++;
  539. tnsVectorAccum3d(td->TCenter,to->p);
  540. tnsVectorAccum3d(td->TLCenter,mv->p);
  541. }
  542. //printf(" [totmv %d]\n",mo->totmv);
  543. tnsVectorMultiSelf3d(td->TCenter, 1.0f/any);
  544. tnsVectorMultiSelf3d(td->TLCenter, 1.0f/any);
  545. la_GetTransformCenter2D(td);
  546. return any;
  547. }
  548. int la_PopulateTransformPoints(MTransformData* td, tnsShapeObject* so){
  549. int any=0; td->so=so; td->next=0;
  550. arrEnsureLength(&td->Originals, 0, &td->max, sizeof(MTOrigSPoint));
  551. tnsInverse44d(td->obmatinv, so->Base.GlobalTransform);
  552. tnsInverse44d(td->deltainv, so->Base.DeltaTransform);
  553. for(tnsShape* s=so->Shapes.pFirst;s;s=s->Item.pNext){
  554. for(tnsSPoint* sp=s->Points.pFirst;sp;sp=sp->Item.pNext){
  555. if(!(sp->flags&TNS_MESH_FLAG_SELECTED)) continue;
  556. arrEnsureLength(&td->Originals, td->next, &td->max, sizeof(MTOrigSPoint));
  557. MTOrigSPoint* to=arrElement(td->Originals, td->next, sizeof(MTOrigSPoint)); td->next++; to->sp=sp;
  558. tnsVector3d tp; tnsVector3d fp={sp->p[0],sp->p[1],0};
  559. tnsApplyTransform43d(tp, so->Base.GlobalTransform, fp);
  560. tnsVectorSet2v(to->p,tp); tnsVectorSet2v(to->origp,sp->p); any++;
  561. tnsVectorAccum2d(td->TCenter,to->p);
  562. tnsVectorAccum2d(td->TLCenter,sp->p);
  563. }
  564. }
  565. tnsVectorMultiSelf2d(td->TCenter, 1.0f/any);
  566. tnsVectorMultiSelf2d(td->TLCenter, 1.0f/any);
  567. la_GetTransformCenter2D(td);
  568. return any;
  569. }
  570. void la_ApplyTranslation(MTransformData* td, int x, int y){
  571. tnsMatrix44d trans; tnsVector3d deltay,delta; tnsVector3d gp; tnsVector3d use_delta;
  572. if(td->Is2D || td->so){
  573. use_delta[0]=x*(td->so?(1.0f/LA_RH):td->zoomx); use_delta[1]=-y*(td->so?(1.0f/LA_RH):td->zoomy); use_delta[2]=0;
  574. if(td->LockAxis[0]){ use_delta[1]=0; } if(td->LockAxis[1]){ use_delta[0]=0; }
  575. }else{
  576. tnsVectorMulti3d(delta, td->Right, x); tnsVectorMulti3d(deltay, td->Up, y); tnsVectorAccum3d(delta, deltay);
  577. tnsVectorSet3v(use_delta,delta); real len;
  578. if(td->LockAxis[0]||td->LockAxis[1]||td->LockAxis[2]){ len=tnsLength3d(delta); }
  579. if(td->LockAxis[0]>0){ use_delta[1]=use_delta[2]=0; real l=fabs(use_delta[0]); use_delta[0]=l?use_delta[0]*len/l:1e-7; }
  580. if(td->LockAxis[1]>0){ use_delta[0]=use_delta[2]=0; real l=fabs(use_delta[1]); use_delta[1]=l?use_delta[1]*len/l:1e-7; }
  581. if(td->LockAxis[2]>0){ use_delta[0]=use_delta[1]=0; real l=fabs(use_delta[2]); use_delta[2]=l?use_delta[2]*len/l:1e-7; }
  582. if(td->LockAxis[0]<0){ use_delta[0]=0; real l=tnsLength3d(use_delta); tnsVectorMultiSelf3d(use_delta, l?len/l*len/l:0); }
  583. if(td->LockAxis[1]<0){ use_delta[1]=0; real l=tnsLength3d(use_delta); tnsVectorMultiSelf3d(use_delta, l?len/l*len/l:0); }
  584. if(td->LockAxis[2]<0){ use_delta[2]=0; real l=tnsLength3d(use_delta); tnsVectorMultiSelf3d(use_delta, l?len/l*len/l:0); }
  585. }
  586. td->DeltaVal=tnsLength3d(use_delta);
  587. if(td->UseUserDelta){
  588. tnsVectorMultiSelf3d(use_delta,1/tnsLength3d(use_delta)*td->UserDeltaVal);
  589. tnsVector3d lock={ td->LockAxis[0], td->LockAxis[1], td->LockAxis[2] };
  590. real dir=tnsDot3d(use_delta, lock, 0); tnsVectorMultiSelf3d(use_delta,(td->UserDeltaVal*dir<=0)?-1:1);
  591. td->DeltaVal=td->UserDeltaVal;
  592. }
  593. if(td->mo){
  594. tnsMakeTranslationMatrix44d(trans, LA_COLOR3(use_delta)); tnsMatrix44d final;
  595. if(!td->UseLocal) tnsMultiply44d(final,td->obmatinv,trans);
  596. else tnsMultiply44d(final,trans,td->obmatinv);
  597. for(int i=0;i<td->next;i++){ MTOrigMVert* to=arrElement(td->Originals, i, sizeof(MTOrigMVert));
  598. tnsApplyTransform43d(to->mv->p, final, to->p);
  599. }
  600. tnsInvalidateMeshBatch(td->mo); tnsMMeshCalculateNormal(td->mo);
  601. }elif(td->so){
  602. tnsMakeTranslationMatrix44d(trans, LA_COLOR3(use_delta)); tnsMatrix44d final;
  603. if(!td->UseLocal) tnsMultiply44d(final,td->obmatinv,trans);
  604. else tnsMultiply44d(final,trans,td->obmatinv);
  605. for(int i=0;i<td->next;i++){ MTOrigSPoint* to=arrElement(td->Originals, i, sizeof(MTOrigSPoint));
  606. tnsVector3d fp={to->p[0],to->p[1],0}, tp;
  607. tnsApplyTransform43d(tp, final, fp); tnsVectorSet3v(to->sp->p,tp);
  608. }
  609. }else{
  610. for(int i=0;i<td->next;i++){
  611. MTOrigObject* to=arrElement(td->Originals, i, sizeof(MTOrigObject)); memcpy(to->o->GlobalTransform, to->Global,sizeof(tnsMatrix44d));
  612. if(to->Discard){ tnsSelfMatrixChanged(to->o,1); continue; }
  613. if(td->CanvasDeltaMode) tnsGlobalMatrixChangedForDelta(to->o, 0); else tnsGlobalMatrixChanged(to->o, 0);
  614. if(td->UseLocal){
  615. if(td->CanvasDeltaMode) tnsMoveObjectDelta(to->o, LA_COLOR3(use_delta));
  616. else tnsMoveObjectLocal(to->o, LA_COLOR3(use_delta));
  617. }else{
  618. if(td->CanvasDeltaMode) tnsMoveObjectGlobalForDelta(to->o, LA_COLOR3(use_delta));
  619. else tnsMoveObjectGlobal(to->o, LA_COLOR3(use_delta));
  620. }
  621. }
  622. }
  623. }
  624. void la_ApplyScale(MTransformData* td, int uix, int uiy){
  625. tnsMatrix44d trans; real d=tnsDistIdv2(uix,uiy,td->CenterX,td->CenterY); if(!td->Initial){ td->Initial=100; }
  626. real s=d/td->Initial; tnsVector3d gp;
  627. td->DeltaVal=s; if(td->UseUserDelta) td->DeltaVal=s=td->UserDeltaVal;
  628. tnsMatrix44d t1,t2,t3,final;
  629. if(td->mo || td->so){
  630. tnsVector3d use_delta={s,s,s};\
  631. if(td->LockAxis[0]>0){ use_delta[1]=use_delta[2]=1;}
  632. if(td->LockAxis[1]>0){ use_delta[0]=use_delta[2]=1;}
  633. if(td->LockAxis[2]>0){ use_delta[0]=use_delta[1]=1;}
  634. if(td->LockAxis[0]<0){ use_delta[0]=1; }
  635. if(td->LockAxis[1]<0){ use_delta[1]=1; }
  636. if(td->LockAxis[2]<0){ use_delta[2]=1; }
  637. tnsMakeScaleMatrix44d(trans,LA_COLOR3(use_delta));
  638. tnsMakeTranslationMatrix44d(t1,LA_COLOR3(td->TCenter)); tnsInverse44d(t2,t1);
  639. tnsMultiply44d(t3,t1,trans); tnsMultiply44d(t1,t3,t2);
  640. if(!td->UseLocal) tnsMultiply44d(final,td->obmatinv,t1);
  641. else tnsMultiply44d(final,t1,td->obmatinv);
  642. }
  643. if(td->mo){
  644. for(int i=0;i<td->next;i++){ MTOrigMVert* to=arrElement(td->Originals, i, sizeof(MTOrigMVert));
  645. tnsApplyTransform43d(to->mv->p, final, to->p);
  646. }
  647. tnsInvalidateMeshBatch(td->mo); tnsMMeshCalculateNormal(td->mo);
  648. }elif(td->so){
  649. for(int i=0;i<td->next;i++){ MTOrigSPoint* to=arrElement(td->Originals, i, sizeof(MTOrigSPoint));
  650. tnsVector3d fp={to->p[0],to->p[1],0}, tp;
  651. tnsApplyTransform43d(tp, final, fp); tnsVectorSet3v(to->sp->p,tp);
  652. }
  653. }else{
  654. for(int i=0;i<td->next;i++){
  655. MTOrigObject* to=arrElement(td->Originals, i, sizeof(MTOrigObject)); memcpy(to->o->GlobalTransform, to->Global,sizeof(tnsMatrix44d));
  656. if(to->Discard){ tnsSelfMatrixChanged(to->o,1); continue; }
  657. if(td->CanvasDeltaMode) tnsGlobalMatrixChangedForDelta(to->o, 0); else tnsGlobalMatrixChanged(to->o, 0);
  658. if(td->CanvasDeltaMode) tnsScaleObjectDelta(to->o,s,s,s,LA_COLOR3(td->TCenter));
  659. else tnsScaleObject(to->o, s,s,s, LA_COLOR3(td->TCenter));
  660. }
  661. }
  662. }
  663. void la_ApplyRotation(MTransformData* td, int uix, int uiy){
  664. tnsMatrix44d trans; real a=atan2(uiy-td->CenterY,uix-td->CenterX);
  665. real angle=a-td->Initial; tnsVector3d gp; tnsVector3d LimFoward={0}; real* use_forward=td->Foward;
  666. tnsMatrix44d t1,t2,t3,final;
  667. if(td->Is2D || td->so){ use_forward=LimFoward; LimFoward[2]=1; }
  668. if(td->LockAxis[0]||td->LockAxis[1]||td->LockAxis[2]){ use_forward=LimFoward; }
  669. if(td->LockAxis[0]){ LimFoward[0]=1; LimFoward[2]=0; }
  670. if(td->LockAxis[1]){ LimFoward[1]=1; LimFoward[2]=0; }
  671. if(td->LockAxis[2]){ LimFoward[2]=1; }
  672. if(td->UseUserDelta) angle=rad(td->UserDeltaVal); td->DeltaVal=deg(angle);
  673. if(td->mo||td->so){
  674. tnsMakeRotationMatrix44d(trans, angle, LA_COLOR3(use_forward));
  675. tnsMakeTranslationMatrix44d(t1,LA_COLOR3(td->UseLocal?td->TLCenter:td->TCenter)); tnsInverse44d(t2,t1);
  676. tnsMultiply44d(t3,t1,trans); tnsMultiply44d(t1,t3,t2);
  677. if(!td->UseLocal) tnsMultiply44d(final,td->obmatinv,t1);
  678. else tnsMultiply44d(final,t1,td->obmatinv);
  679. }
  680. if(td->mo){
  681. for(int i=0;i<td->next;i++){ MTOrigMVert* to=arrElement(td->Originals, i, sizeof(MTOrigMVert));
  682. tnsApplyTransform43d(to->mv->p, final, to->p);
  683. }
  684. tnsInvalidateMeshBatch(td->mo); tnsMMeshCalculateNormal(td->mo);
  685. }elif(td->so){
  686. for(int i=0;i<td->next;i++){ MTOrigSPoint* to=arrElement(td->Originals, i, sizeof(MTOrigSPoint));
  687. tnsVector3d fp={to->p[0],to->p[1],0}, tp;
  688. tnsApplyTransform43d(tp, final, fp); tnsVectorSet3v(to->sp->p,tp);
  689. }
  690. }else{
  691. for(int i=0;i<td->next;i++){
  692. MTOrigObject* to=arrElement(td->Originals, i, sizeof(MTOrigObject)); memcpy(to->o->GlobalTransform, to->Global,sizeof(tnsMatrix44d));
  693. if(to->Discard){ tnsSelfMatrixChanged(to->o,1); continue; }
  694. if(td->CanvasDeltaMode) tnsGlobalMatrixChangedForDelta(to->o, 0); else tnsGlobalMatrixChanged(to->o, 0);
  695. if(td->UseLocal){
  696. if(td->CanvasDeltaMode) tnsRotateObjectDelta(to->o,LA_COLOR3(use_forward),angle);
  697. else tnsRotateObjectLocal(to->o,LA_COLOR3(use_forward),angle);
  698. }else{
  699. if(td->CanvasDeltaMode) tnsRotateObjectGlobalForDelta(to->o,LA_COLOR3(use_forward),angle,LA_COLOR3(td->TCenter));
  700. else tnsRotateObjectGlobal(to->o,LA_COLOR3(use_forward),angle,LA_COLOR3(td->TCenter));
  701. }
  702. }
  703. }
  704. }
  705. void la_CancelTransformObjects(MTransformData* td){
  706. if(td->mo){
  707. for(int i=0;i<td->next;i++){ MTOrigMVert* to=arrElement(td->Originals, i, sizeof(MTOrigMVert)); tnsVectorCopy3d(to->origp,to->mv->p); }
  708. tnsInvalidateMeshBatch(td->mo);
  709. }elif(td->so){
  710. for(int i=0;i<td->next;i++){ MTOrigSPoint* to=arrElement(td->Originals, i, sizeof(MTOrigSPoint)); tnsVectorCopy2d(to->origp,to->sp->p); }
  711. }else{
  712. for(int i=0;i<td->next;i++){ MTOrigObject* to=arrElement(td->Originals, i, sizeof(MTOrigObject));
  713. if(to->Discard){ tnsSelfMatrixChanged(to->o,1); continue; }
  714. memcpy(to->o->GlobalTransform, to->Global,sizeof(tnsMatrix44d));
  715. if(td->CanvasDeltaMode) tnsGlobalMatrixChangedForDelta(to->o, 1);
  716. else tnsGlobalMatrixChanged(to->o, 1);
  717. }
  718. }
  719. }
  720. void la_RecordTransformDifferences(MTransformData* td){
  721. if(td->mo){
  722. laRecordInstanceDifferences(td->mo, "tns_mesh_object");
  723. laPushDifferences(td->mode==LA_TRANSFORM_MODE_GRAB?"Moved primitives":td->mode==LA_TRANSFORM_MODE_ROTATE?"Rotated primitives":"Scaled primitives", TNS_HINT_GEOMETRY);
  724. tnsInvalidateMeshBatch(td->mo);
  725. }elif(td->so){
  726. laRecordInstanceDifferences(td->so, "tns_shape_object");
  727. laPushDifferences(td->mode==LA_TRANSFORM_MODE_GRAB?"Moved points":td->mode==LA_TRANSFORM_MODE_ROTATE?"Rotated points":"Scaled points", TNS_HINT_GEOMETRY);
  728. }else{
  729. for(int i=0;i<td->next;i++){ MTOrigObject* to=arrElement(td->Originals, i, sizeof(MTOrigObject));
  730. laRecordInstanceDifferences(to->o, "tns_object");
  731. } laPushDifferences(td->mode==LA_TRANSFORM_MODE_GRAB?"Moved objects":td->mode==LA_TRANSFORM_MODE_ROTATE?"Rotated objects":"Scaled objects", TNS_HINT_TRANSFORM);
  732. }
  733. }
  734. void la_FreeTransformData(MTransformData* td){
  735. free(td->Originals);
  736. strEndEdit(&td->Entry, 1);
  737. memFree(td);
  738. }
  739. void la_MakeTransformOperatorHint(laOperator* a, MTransformData* td){
  740. strSafeDestroy(&a->RuntimeHint);
  741. strSafePrint(&a->RuntimeHint, "%s ",
  742. td->mode==LA_TRANSFORM_MODE_GRAB?"Grab":td->mode==LA_TRANSFORM_MODE_ROTATE?"Rotate":td->mode==LA_TRANSFORM_MODE_SCALE?"Scale":"");
  743. char* entry=strGetEditString(td->Entry,0);
  744. strSafePrint(&a->RuntimeHint, "Delta: %.3lf [🔢 %s] 🆇🆈🆉 Lock axis: %s 🈳 %s ", td->DeltaVal, (entry&&entry[0])?entry:"Type...",
  745. td->LockAxis[0]?"X":td->LockAxis[1]?"Y":td->LockAxis[2]?"Z":"None",
  746. td->UseLocal?"Global/[Local]":"[Global]/Local");
  747. free(entry);
  748. if(td->mode==LA_TRANSFORM_MODE_GRAB){ strSafePrint(&a->RuntimeHint, "🡅🆇🆈🆉 Reverse: %s ",
  749. (td->LockAxis[0]<0||td->LockAxis[1]<0||td->LockAxis[2]<0)?"Yes":"No"); }
  750. if(td->mode!=LA_TRANSFORM_MODE_GRAB){ strSafePrint(&a->RuntimeHint,"🅶 Grab "); }
  751. if(td->mode!=LA_TRANSFORM_MODE_SCALE){ strSafePrint(&a->RuntimeHint,"🆂 Scale "); }
  752. if(td->mode!=LA_TRANSFORM_MODE_ROTATE){ strSafePrint(&a->RuntimeHint,"🆁 Rotate "); }
  753. }
  754. void la_RestoreTransform(tnsObject* ob, int restore_type, int restore_delta){
  755. if(restore_delta){
  756. if(restore_type==LA_TRANSFORM_RESTORE_LOC){ tnsVectorSet3(ob->DLocation,0,0,0); }
  757. elif(restore_type==LA_TRANSFORM_RESTORE_ROT){ tnsVectorSet3(ob->DRotation,0,0,0); }
  758. elif(restore_type==LA_TRANSFORM_RESTORE_SCA){ tnsVectorSet3(ob->DScale,1,1,1); }
  759. tnsDeltaTransformValueChanged(ob);
  760. }else{
  761. if(restore_type==LA_TRANSFORM_RESTORE_LOC){ tnsVectorSet3(ob->Location,0,0,0); }
  762. elif(restore_type==LA_TRANSFORM_RESTORE_ROT){ tnsVectorSet3(ob->Rotation,0,0,0); }
  763. elif(restore_type==LA_TRANSFORM_RESTORE_SCA){ tnsVectorSet3(ob->Scale,1,1,1); }
  764. tnsSelfTransformValueChanged(ob);
  765. }
  766. }
  767. int la_InitTransform(laOperator* a, laEvent* e, int mode, int restore_type, int restore_delta){
  768. if(!a->This || !a->This->EndInstance){ return 0; }
  769. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  770. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  771. tnsObject* o=root->Active; tnsRootObject*ro=root;
  772. tnsMeshObject* mo=o; tnsShapeObject* so=o;
  773. MTransformData* td=la_InitTransformData(
  774. ex->OffScr->pColor[0]->Width, ex->OffScr->pColor[0]->Height, c, ex->DeltaMode,
  775. ro->Is2D, ex->ZoomX,ex->ZoomY);
  776. a->CustomData = td;
  777. td->mode=mode;
  778. td->root=root;
  779. int ret=0;
  780. if(o && o->Type==TNS_OBJECT_MESH && mo->Mode==TNS_MESH_EDIT_MODE){
  781. if(la_PopulateTransformVerticies(td, mo)){ ex->ClickedX=e->x; ex->ClickedY=e->y; ret=1; }
  782. }if(o && o->Type==TNS_OBJECT_SHAPE && so->Mode==TNS_MESH_EDIT_MODE){
  783. if(la_PopulateTransformPoints(td, so)){ ex->ClickedX=e->x; ex->ClickedY=e->y; ret=1; }
  784. }else{
  785. if(la_PopulateTransformObjects(td,root)){ ex->ClickedX=e->x; ex->ClickedY=e->y; ret=1; }
  786. if(ret && restore_type){
  787. for(int i=0;i<td->next;i++){ MTOrigObject* ob=arrElement(td->Originals, i, sizeof(MTOrigObject));
  788. la_RestoreTransform(ob->o,restore_type,restore_delta);
  789. }
  790. la_RecordTransformDifferences(td); laNotifyUsers("tns.world"); la_FreeTransformData(td);
  791. return 0;
  792. }
  793. }
  794. if(ret){
  795. if(mode==LA_TRANSFORM_MODE_SCALE){ la_GetTransformInitialScale(td,ui,e->x,e->y); ex->DrawCursor=LA_CANVAS_CURSOR_ARROW; }
  796. elif(mode==LA_TRANSFORM_MODE_ROTATE){ la_GetTransformInitialRotation(td,ui,e->x,e->y); ex->DrawCursor=LA_CANVAS_CURSOR_ARROW; }
  797. ex->TargetX=td->CenterX+ui->L; ex->TargetY=td->CenterY+ui->U; ex->OnX=e->x; ex->OnY=e->y;
  798. la_MakeTransformOperatorHint(a, td);
  799. laNotifyUsers("tns.world"); return 1;
  800. }
  801. return 0;
  802. }
  803. int OPINV_Grab(laOperator *a, laEvent *e){
  804. if(la_InitTransform(a, e, LA_TRANSFORM_MODE_GRAB,0,0)) return LA_RUNNING; return LA_FINISHED_PASS;
  805. }
  806. int OPINV_Scale(laOperator *a, laEvent *e){
  807. if(la_InitTransform(a, e, LA_TRANSFORM_MODE_SCALE,0,0)) return LA_RUNNING; return LA_FINISHED_PASS;
  808. }
  809. int OPINV_Rotate(laOperator *a, laEvent *e){
  810. if(la_InitTransform(a, e, LA_TRANSFORM_MODE_ROTATE,0,0)) return LA_RUNNING; return LA_FINISHED_PASS;
  811. }
  812. int OPMOD_Transformation(laOperator *a, laEvent *e){
  813. if(!a->This || !a->This->EndInstance){ return 0; }
  814. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  815. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  816. tnsMeshObject* mo=root->Active;
  817. MTransformData* td=a->CustomData;
  818. if (e->Input=='x'||e->Input=='y'||e->Input=='z'||e->Input=='g'||e->Input=='s'||e->Input=='r'||
  819. e->Input=='X'||e->Input=='Y'||e->Input=='Z'||e->Input==' '){ /*pass*/ }
  820. else{ la_ProcessTextEdit(e, td->Entry, 0); }
  821. char* entered;
  822. if(entered=strGetEditString(td->Entry,0)){ int status;
  823. if((status=sscanf(entered,"%lf",&td->UserDeltaVal)) && status!=EOF) td->UseUserDelta=1; else td->UseUserDelta=0;
  824. td->UserDeltaVal=fabs(td->UserDeltaVal); for(char*pc=entered;*pc;pc++){ if(*pc=='-') td->UserDeltaVal=-td->UserDeltaVal; } free(entered);
  825. }
  826. if(e->Type==LA_KEY_DOWN){
  827. int Other=1; if(e->SpecialKeyBit&LA_KEY_SHIFT){ Other=-1; }
  828. if(e->key=='x'){ td->LockAxis[0]=Other*((Other<0&&td->LockAxis[0])?td->LockAxis[0]:!td->LockAxis[0]); td->LockAxis[1]=td->LockAxis[2]=0; }
  829. if(e->key=='y'){ td->LockAxis[1]=Other*((Other<0&&td->LockAxis[1])?td->LockAxis[1]:!td->LockAxis[1]); td->LockAxis[0]=td->LockAxis[2]=0; }
  830. if(e->key=='z'){ td->LockAxis[2]=Other*((Other<0&&td->LockAxis[2])?td->LockAxis[2]:!td->LockAxis[2]); td->LockAxis[0]=td->LockAxis[1]=0; }
  831. if(e->key=='g' && td->mode!=LA_TRANSFORM_MODE_GRAB){ td->mode=LA_TRANSFORM_MODE_GRAB; ex->DrawCursor=0; }
  832. if(e->key=='s' && td->mode!=LA_TRANSFORM_MODE_SCALE){ td->mode=LA_TRANSFORM_MODE_SCALE; la_GetTransformInitialScale(td,ui,e->x,e->y);ex->DrawCursor=LA_CANVAS_CURSOR_ARROW; }
  833. if(e->key=='r' && td->mode!=LA_TRANSFORM_MODE_ROTATE){ td->mode=LA_TRANSFORM_MODE_ROTATE; la_GetTransformInitialRotation(td,ui,e->x,e->y);ex->DrawCursor=LA_CANVAS_CURSOR_ARROW; }
  834. if(e->key==' '){ td->UseLocal=!td->UseLocal; }
  835. }
  836. if(e->Type==LA_MOUSEMOVE || e->Type==LA_KEY_DOWN){
  837. switch(td->mode){
  838. case LA_TRANSFORM_MODE_GRAB: la_ApplyTranslation(td,e->x-ex->ClickedX, e->y-ex->ClickedY); break;
  839. case LA_TRANSFORM_MODE_SCALE: la_ApplyScale(td,e->x-ui->L, e->y-ui->U); break;
  840. case LA_TRANSFORM_MODE_ROTATE: la_ApplyRotation(td,e->x-ui->L, e->y-ui->U); break;
  841. default: break;
  842. }
  843. ex->OnX=e->x; ex->OnY=e->y;
  844. laNotifyUsers("tns.world");
  845. }
  846. if(e->Type==LA_L_MOUSE_DOWN || (e->Type==LA_KEY_DOWN && e->key==LA_KEY_ENTER)){ ex->DrawCursor=0;
  847. la_RecordTransformDifferences(td);
  848. laNotifyUsers("tns.world"); la_FreeTransformData(td); return LA_FINISHED;
  849. }
  850. if(e->Type==LA_R_MOUSE_DOWN || e->Type==LA_ESCAPE_DOWN){ ex->DrawCursor=0;
  851. la_CancelTransformObjects(td); laNotifyUsers("tns.world"); la_FreeTransformData(td); return LA_FINISHED;
  852. }
  853. la_MakeTransformOperatorHint(a, td);
  854. return LA_RUNNING;
  855. }
  856. int OPINV_ClearTransformation(laOperator *a, laEvent *e){
  857. laCanvasExtra* ex=a->This->EndInstance; int restore=0;
  858. char* channel=strGetArgumentString(a->ExtraInstructionsP,"channel");
  859. if(channel){
  860. if(channel[0]=='L' || channel[0]=='l'){ restore=LA_TRANSFORM_RESTORE_LOC; }
  861. if(channel[0]=='R' || channel[0]=='r'){ restore=LA_TRANSFORM_RESTORE_ROT; }
  862. if(channel[0]=='S' || channel[0]=='s'){ restore=LA_TRANSFORM_RESTORE_SCA; }
  863. }else{
  864. laEnableOperatorPanel(a,a->This,e->x,e->y,150,200,0,0,0,0,0,0,0,0,e); return LA_RUNNING;
  865. }
  866. char* dt=strGetArgumentString(a->ExtraInstructionsP,"delta");
  867. int delta=dt?strSame(dt,"true"):ex->DeltaMode;
  868. la_InitTransform(a,e,0,restore,delta);
  869. return LA_FINISHED;
  870. }
  871. void laui_ClearTransformation(laUiList *uil, laPropPack *pp, laPropPack *actinst, laColumn *extracol, int context){
  872. laColumn* c=laFirstColumn(uil), *cl,*cr; laSplitColumn(uil,c,0.6); cl=laLeftColumn(c,0); cr=laRightColumn(c,0);
  873. laShowItemFull(uil,cl,pp,"_this_M_clear_transformations",0,"channel=loc;text=Location",0,0);
  874. laShowItemFull(uil,cl,pp,"_this_M_clear_transformations",0,"channel=rot;text=Rotation",0,0);
  875. laShowItemFull(uil,cl,pp,"_this_M_clear_transformations",0,"channel=sca;text=Scale",0,0);
  876. laShowItemFull(uil,cr,pp,"_this_M_clear_transformations",0,"channel=loc;text=Delta;delta=true",0,0);
  877. laShowItemFull(uil,cr,pp,"_this_M_clear_transformations",0,"channel=rot;text=Delta;delta=true",0,0);
  878. laShowItemFull(uil,cr,pp,"_this_M_clear_transformations",0,"channel=sca;text=Delta;delta=true",0,0);
  879. }
  880. int la_ParentableRecursive(tnsObject* root, tnsObject* parent){
  881. for(laListItemPointer* lip=root->ChildObjects.pFirst;lip;lip=lip->pNext){
  882. tnsObject* o=lip->p; if((!o) || (!(o->Flags&TNS_OBJECT_FLAGS_SELECTED)) || (o==parent)) continue;
  883. if(!tnsCheckParentable(o,parent)) return 0;
  884. if(!la_ParentableRecursive(o,parent)) return 0;
  885. }return 1;
  886. }
  887. void la_MakeParentExecuteRecursive(tnsObject* root, tnsObject* parent, int Unparent, int KeepTransform){
  888. laListItemPointer* NextLip;
  889. if(root->Type!=TNS_OBJECT_ROOT){
  890. if((root->Flags&TNS_OBJECT_FLAGS_SELECTED)&&root->InRoot==parent->InRoot){
  891. if(Unparent) tnsUnparentObject(root, KeepTransform);
  892. elif(root!=parent) tnsParentObject(root, parent, KeepTransform);
  893. }
  894. }
  895. for(laListItemPointer* lip=root->ChildObjects.pFirst;lip;lip=NextLip){ NextLip=lip->pNext;
  896. tnsObject* o=lip->p; if(!o) continue; la_MakeParentExecuteRecursive(o,parent,Unparent,KeepTransform);
  897. }
  898. laNotifyUsers("tns.world");
  899. }
  900. int OPINV_MakeParent(laOperator *a, laEvent *e){
  901. if(!a->This || !a->This->EndInstance){ return 0; }
  902. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  903. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  904. tnsObject* mo=root->Active;
  905. int Unparent=0,KeepTransform=1;
  906. char* action=strGetArgumentString(a->ExtraInstructionsP,"action");
  907. char* keep=strGetArgumentString(a->ExtraInstructionsP,"keep_transform");
  908. if(strSame(action,"unparent")){ Unparent=1; }
  909. if(strSame(keep,"false")){ KeepTransform=0; }
  910. if(!Unparent){ if((!mo) || (!(mo->Flags&TNS_OBJECT_FLAGS_SELECTED))) return LA_FINISHED;
  911. if(!la_ParentableRecursive(root,mo)){ laEnableMessagePanel(0,0,"It didn't work","There are loops in parenting",e->x,e->y,0,e); return LA_FINISHED; }
  912. }
  913. if(keep){
  914. la_MakeParentExecuteRecursive(root,mo,Unparent,KeepTransform);
  915. laRecordInstanceDifferences(T->World, "tns_world"); laPushDifferences(Unparent?"Unparent":"Parent", TNS_HINT_TRANSFORM);
  916. return LA_FINISHED;
  917. }
  918. laEnableOperatorPanel(a,a->This,e->x,e->y,200,200,0,0,0,0,0,0,0,0,e);
  919. return LA_RUNNING;
  920. }
  921. void laui_MakeParent(laUiList *uil, laPropPack *pp, laPropPack *actinst, laColumn *extracol, int context){
  922. laColumn* c=laFirstColumn(uil);
  923. laShowItemFull(uil,c,pp,"_this_M_make_parent",0,"action=parent;keep_transform=true;text=Keep transform",0,0);
  924. laShowItemFull(uil,c,pp,"_this_M_make_parent",0,"action=parent;keep_transform=false;text=Directly",0,0);
  925. }
  926. void laui_Unparent(laUiList *uil, laPropPack *pp, laPropPack *actinst, laColumn *extracol, int context){
  927. laColumn* c=laFirstColumn(uil);
  928. laShowItemFull(uil,c,pp,"_this_M_unparent",0,"action=unparent;keep_transform=true;text=Keep transform",0,0);
  929. laShowItemFull(uil,c,pp,"_this_M_unparent",0,"action=unparent;keep_transform=false;text=Directly",0,0);
  930. }
  931. STRUCTURE(MEDupVert){
  932. int oi; tnsMVert* nmv; tnsMVert* omv; int IsBorder;
  933. };
  934. STRUCTURE(MEDupEdge){
  935. int oi; tnsMEdge* nme; tnsMEdge* ome; int IsBorder;
  936. };
  937. STRUCTURE(MEDupFace){
  938. int oi; tnsMFace* nmf; tnsMFace* omf;
  939. };
  940. STRUCTURE(MExtrudeExtra){
  941. MEDupVert* dv; int nextv,maxv;
  942. MEDupEdge* de; int nexte,maxe;
  943. MEDupFace* df; int nextf,maxf;
  944. tnsMeshObject* mo;
  945. int RemoveOriginalFaces;
  946. };
  947. int la_IsSelectionBorderVertex(tnsMVert* mv){
  948. if(!mv->elink.pFirst)
  949. return 0;
  950. int NearSelected=0, NearUnselected=0;
  951. for(laListItemPointer* lip=mv->elink.pFirst;lip;lip=lip->pNext){
  952. tnsMEdge* me=lip->p; if(!(me->fl&&me->fr)) return 1;
  953. tnsMVert* av=tnsMMeshEdgeAnotherVert(me,mv);
  954. if(me->fl->flags&TNS_MESH_FLAG_SELECTED){ NearSelected=1; } else { return 1; }
  955. if(me->fr->flags&TNS_MESH_FLAG_SELECTED){ NearSelected=1; } else { return 1; }
  956. if(NearUnselected && NearSelected) return 1;
  957. }
  958. return 0;
  959. }
  960. int la_IsSelectionBorderEdge(tnsMEdge* me){
  961. if(me->fl&&me->fr){
  962. if ((me->fl->flags&TNS_MESH_FLAG_SELECTED) && (!(me->fr->flags&TNS_MESH_FLAG_SELECTED))||
  963. (me->fr->flags&TNS_MESH_FLAG_SELECTED) && (!(me->fl->flags&TNS_MESH_FLAG_SELECTED))) return 1;
  964. else return (me->fr->flags==me->fl->flags&&me->fl->flags==0);
  965. } return 1;
  966. }
  967. MExtrudeExtra* la_InitExtrude(tnsMeshObject* mo){
  968. MExtrudeExtra* ee=memAcquireSimple(sizeof(MExtrudeExtra));
  969. arrEnsureLength(&ee->dv, ee->nextv, &ee->maxv, sizeof(MEDupVert));
  970. arrEnsureLength(&ee->de, ee->nexte, &ee->maxe, sizeof(MEDupEdge));
  971. arrEnsureLength(&ee->df, ee->nextf, &ee->maxf, sizeof(MEDupFace));
  972. ee->mo=mo;
  973. return ee;
  974. }
  975. void la_ExtrudeMakeDuplication(MExtrudeExtra* ee){
  976. tnsMeshObject* mo=ee->mo;
  977. for(tnsMVert* mv=mo->mv.pFirst;mv;mv=mv->Item.pNext){ if(!(mv->flags&TNS_MESH_FLAG_SELECTED)) continue;
  978. arrEnsureLength(&ee->dv, ee->nextv, &ee->maxv, sizeof(MEDupVert));
  979. MEDupVert* dv=&ee->dv[ee->nextv];
  980. tnsMVert* nmv=tnsMMeshNewVert(mo); tnsVectorCopy3d(mv->p, &nmv->p); dv->oi=mv->i; mv->i=ee->nextv; dv->nmv=nmv; dv->omv=mv;
  981. dv->IsBorder=la_IsSelectionBorderVertex(mv);
  982. ee->nextv++;
  983. }
  984. for(tnsMEdge* me=mo->me.pFirst;me;me=me->Item.pNext){ if(!(me->flags&TNS_MESH_FLAG_SELECTED)) continue;
  985. arrEnsureLength(&ee->de, ee->nexte, &ee->maxe, sizeof(MEDupEdge));
  986. MEDupEdge* de=&ee->de[ee->nexte];
  987. tnsMEdge* nme=tnsMMeshNewEdge(mo); de->oi=me->i; me->i=ee->nexte; de->nme=nme; de->ome=me; de->IsBorder=la_IsSelectionBorderEdge(me);
  988. tnsMMeshEdgeAssignVerts(nme, ee->dv[me->vl->i].nmv, ee->dv[me->vr->i].nmv);
  989. if(de->IsBorder&&me->fl&&me->fr){ ee->RemoveOriginalFaces=1; }
  990. ee->nexte++;
  991. }
  992. for(tnsMFace* mf=mo->mf.pFirst;mf;mf=mf->Item.pNext){
  993. if(!(mf->flags&TNS_MESH_FLAG_SELECTED)) continue;
  994. arrEnsureLength(&ee->df, ee->nextf, &ee->maxf, sizeof(MEDupFace));
  995. MEDupFace* df=&ee->df[ee->nextf];
  996. tnsMFace* nmf=tnsMMeshNewFace(mo); df->oi=mf->i; mf->i=ee->nextf; df->nmf=nmf; df->omf=mf; nmf->mat=mf->mat;
  997. for(laListItemPointer*lip=mf->l.pFirst;lip;lip=lip->pNext){
  998. tnsMEdge* ome=lip->p; tnsMMeshFaceAddEdge(nmf,ee->de[ome->i].nme);
  999. }
  1000. ee->nextf++;
  1001. }
  1002. for(int i=0;i<ee->nextv;i++){ ee->dv[i].omv->i=ee->dv[i].oi; ee->dv[i].omv->flags&=(~TNS_MESH_FLAG_SELECTED); ee->dv[i].nmv->flags|=TNS_MESH_FLAG_SELECTED; }
  1003. for(int i=0;i<ee->nexte;i++){ ee->de[i].ome->i=ee->de[i].oi; ee->de[i].ome->flags&=(~TNS_MESH_FLAG_SELECTED); ee->de[i].nme->flags|=TNS_MESH_FLAG_SELECTED; }
  1004. for(int i=0;i<ee->nextf;i++){
  1005. ee->df[i].omf->i=ee->df[i].oi;
  1006. ee->df[i].omf->flags&=(~TNS_MESH_FLAG_SELECTED);
  1007. ee->df[i].nmf->flags|=TNS_MESH_FLAG_SELECTED; }
  1008. }
  1009. void la_RemoveOriginalFaces(MExtrudeExtra* ee){
  1010. tnsMeshObject* mo=ee->mo;
  1011. if(ee->RemoveOriginalFaces){
  1012. for(int i=0;i<ee->nextf;i++){ tnsMMeshRemoveFaceOnly(mo, ee->df[i].omf); }
  1013. for(int i=0;i<ee->nexte;i++){ if(ee->de[i].IsBorder) continue; tnsMMeshRemoveEdgeFace(mo, ee->de[i].ome); }
  1014. for(int i=0;i<ee->nextv;i++){ if(ee->dv[i].IsBorder) continue; tnsMMeshRemoveVertEdgeFace(mo, ee->dv[i].omv); }
  1015. }
  1016. }
  1017. void la_ReconnectFaces(MExtrudeExtra* ee){
  1018. tnsMeshObject* mo=ee->mo;
  1019. for(int i=0;i<ee->nexte;i++){
  1020. if(!ee->de[i].IsBorder) continue; MEDupEdge*de=&ee->de[i];
  1021. tnsMMeshMakeFace4v(mo, de->ome->vl, de->ome->vr, de->nme->vr, de->nme->vl);
  1022. }
  1023. }
  1024. void la_FinishExtrude(MExtrudeExtra* ee, int PushDifferences){
  1025. tnsMMeshRefreshIndex(ee->mo);
  1026. tnsInvalidateMeshBatch(ee->mo);
  1027. if(PushDifferences){
  1028. laRecordInstanceDifferences(ee->mo, "tns_mesh_object"); laPushDifferences("Extruded", TNS_HINT_GEOMETRY); laNotifyUsers("tns.world");
  1029. }
  1030. free(ee->dv); free(ee->de); free(ee->df); memFree(ee);
  1031. }
  1032. int OPINV_Extrude(laOperator *a, laEvent *e){
  1033. if(!a->This || !a->This->EndInstance){ return 0; }
  1034. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  1035. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  1036. tnsMeshObject* mo=root->Active; if(mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode!=TNS_MESH_EDIT_MODE) return 0;
  1037. if(!tnsMMeshAnySelected(mo)) return LA_FINISHED;
  1038. MExtrudeExtra* ee=la_InitExtrude(mo);
  1039. la_ExtrudeMakeDuplication(ee);
  1040. if(strSame(strGetArgumentString(a->ExtraInstructionsP,"duplicate_only"), "true")){
  1041. la_FinishExtrude(ee, 1);
  1042. if(la_InitTransform(a, e, LA_TRANSFORM_MODE_GRAB,0,0)) return LA_RUNNING; return LA_FINISHED;
  1043. }
  1044. la_RemoveOriginalFaces(ee);
  1045. la_ReconnectFaces(ee);
  1046. la_FinishExtrude(ee, 1);
  1047. if(la_InitTransform(a, e, LA_TRANSFORM_MODE_GRAB,0,0)) return LA_RUNNING; return LA_FINISHED;
  1048. return LA_FINISHED;
  1049. }
  1050. int la_EdgeShouldDeleteVert(tnsMVert* mv){
  1051. if(!mv->elink.pFirst) return 0;
  1052. int NearSelected=0, NearUnselected=0;
  1053. for(laListItemPointer* lip=mv->elink.pFirst;lip;lip=lip->pNext){
  1054. tnsMEdge* me=lip->p;
  1055. tnsMVert* av=tnsMMeshEdgeAnotherVert(me,mv);
  1056. if(av->flags&TNS_MESH_FLAG_SELECTED){ NearSelected=1; } else { NearUnselected=1; }
  1057. if(NearUnselected && NearSelected) return 0;
  1058. }
  1059. if(NearSelected&&(!NearUnselected)) return 1; return 0;
  1060. }
  1061. int la_FaceShouldDeleteVert(tnsMVert* mv){
  1062. if(!mv->elink.pFirst) return 0;
  1063. int NearSelected=0, NearUnselected=0, IsBorder=0;
  1064. for(laListItemPointer* lip=mv->elink.pFirst;lip;lip=lip->pNext){
  1065. tnsMEdge* me=lip->p;
  1066. if(!(me->fl&&me->fr)){ IsBorder=1; }
  1067. if(me->fl){ if(me->fl->flags&TNS_MESH_FLAG_SELECTED){ NearSelected=1; } else { NearUnselected=1; } }
  1068. if(me->fr){ if(me->fr->flags&TNS_MESH_FLAG_SELECTED){ NearSelected=1; } else { NearUnselected=1; } }
  1069. if(NearUnselected && NearSelected) return 0;
  1070. }
  1071. if((!NearUnselected) && IsBorder) return 1;
  1072. return 1;
  1073. }
  1074. int la_FaceShouldDeleteEdge(tnsMEdge* me){
  1075. if(me->fl&&me->fr){
  1076. if ((me->fl->flags&TNS_MESH_FLAG_SELECTED) && (!(me->fr->flags&TNS_MESH_FLAG_SELECTED))||
  1077. (me->fr->flags&TNS_MESH_FLAG_SELECTED) && (!(me->fl->flags&TNS_MESH_FLAG_SELECTED))) return 0;
  1078. else return (me->fr->flags==me->fl->flags&&me->fl->flags==1);
  1079. }else{
  1080. if(me->fl&&me->fl->flags&TNS_MESH_FLAG_SELECTED&&!me->fr) return 1;
  1081. if(me->fr&&me->fr->flags&TNS_MESH_FLAG_SELECTED&&!me->fl) return 1;
  1082. } return 0;
  1083. }
  1084. void la_DeleteVertices(tnsMeshObject* mo){
  1085. tnsMVert* nextmv; for(tnsMVert*mv=mo->mv.pFirst;mv;mv=nextmv){ nextmv=mv->Item.pNext; if(!(mv->flags&TNS_MESH_FLAG_SELECTED)) continue;
  1086. tnsMMeshRemoveVertEdgeFace(mo, mv);
  1087. }
  1088. }
  1089. void la_DeleteEdges(tnsMeshObject* mo){
  1090. laListHandle lv={0};
  1091. for(tnsMVert*mv=mo->mv.pFirst;mv;mv=mv->Item.pNext){ if(!(mv->flags&TNS_MESH_FLAG_SELECTED)) continue;
  1092. if(la_EdgeShouldDeleteVert(mv)) lstAppendPointer(&lv,mv);
  1093. }
  1094. tnsMEdge* nextme; for(tnsMEdge*me=mo->me.pFirst;me;me=nextme){ nextme=me->Item.pNext; if(!(me->flags&TNS_MESH_FLAG_SELECTED)) continue;
  1095. tnsMMeshRemoveEdgeFace(mo, me);
  1096. }
  1097. tnsMVert* mv; while(mv=lstPopPointer(&lv)){ tnsMMeshRemoveVertEdgeFace(mo,mv); }
  1098. }
  1099. void la_DeleteFaces(tnsMeshObject* mo, int OnlyFaces){
  1100. laListHandle lv={0}, le={0};
  1101. if(OnlyFaces){
  1102. tnsMFace* nextmf; for(tnsMFace*mf=mo->mf.pFirst;mf;mf=nextmf){ nextmf=mf->Item.pNext; if(!(mf->flags&TNS_MESH_FLAG_SELECTED)) continue;
  1103. tnsMMeshRemoveFaceOnly(mo, mf);
  1104. }
  1105. }else{
  1106. for(tnsMVert*mv=mo->mv.pFirst;mv;mv=mv->Item.pNext){ if(!(mv->flags&TNS_MESH_FLAG_SELECTED)) continue;
  1107. if(la_FaceShouldDeleteVert(mv)) lstAppendPointer(&lv,mv);
  1108. }
  1109. for(tnsMEdge*me=mo->me.pFirst;me;me=me->Item.pNext){ if(!(me->flags&TNS_MESH_FLAG_SELECTED)) continue;
  1110. if(la_FaceShouldDeleteEdge(me)) lstAppendPointer(&le,me);
  1111. }
  1112. tnsMFace* nextmf; for(tnsMFace*mf=mo->mf.pFirst;mf;mf=nextmf){ nextmf=mf->Item.pNext; if(!(mf->flags&TNS_MESH_FLAG_SELECTED)) continue;
  1113. tnsMMeshRemoveFaceOnly(mo, mf);
  1114. }
  1115. tnsMEdge* me; while(me=lstPopPointer(&le)){ tnsMMeshRemoveEdgeFace(mo,me); }
  1116. tnsMVert* mv; while(mv=lstPopPointer(&lv)){ tnsMMeshRemoveVertEdgeFace(mo,mv); }
  1117. }
  1118. }
  1119. int la_DeleteSelectedObjectsRecursive(tnsObject* root){
  1120. int any=0; for(laListItemPointer* lip=root->ChildObjects.pFirst;lip;lip=lip->pNext){ if(!lip->p) continue;
  1121. tnsObject* o=lip->p; la_DeleteSelectedObjectsRecursive(lip->p);
  1122. if(o->Flags&TNS_OBJECT_FLAGS_SELECTED){ tnsDestroyObject(o); any++; }
  1123. }
  1124. return any;
  1125. }
  1126. int OPINV_Delete(laOperator *a, laEvent *e){
  1127. if(!a->This || !a->This->EndInstance){ return 0; }
  1128. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  1129. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  1130. tnsMeshObject* mo=root->Active; if(!mo) return 0;
  1131. if(mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode!=TNS_MESH_EDIT_MODE){
  1132. if(la_DeleteSelectedObjectsRecursive(root)){
  1133. laRecordInstanceDifferences(T->World, "tns_world"); laPushDifferences("Deleted objects", TNS_HINT_TRANSFORM); laNotifyUsers("tns.world");
  1134. }
  1135. }else{
  1136. if(!tnsMMeshAnySelected(mo)) return LA_FINISHED;
  1137. if(strSame(strGetArgumentString(a->ExtraInstructionsP, "mode"),"vertices")){
  1138. la_DeleteVertices(mo);
  1139. }elif(strSame(strGetArgumentString(a->ExtraInstructionsP, "mode"),"edges")){
  1140. la_DeleteEdges(mo);
  1141. }elif(strSame(strGetArgumentString(a->ExtraInstructionsP, "mode"),"faces")){
  1142. la_DeleteFaces(mo,0);
  1143. }elif(strSame(strGetArgumentString(a->ExtraInstructionsP, "mode"),"only_faces")){
  1144. la_DeleteFaces(mo,1);
  1145. }else{
  1146. laEnableOperatorPanel(a,a->This,e->x,e->y,200,200,0,0,0,0,0,0,0,0,e);
  1147. return LA_RUNNING;
  1148. }
  1149. tnsMMeshDeselectAll(mo);
  1150. tnsMMeshRefreshIndex(mo);
  1151. tnsInvalidateMeshBatch(mo);
  1152. laRecordInstanceDifferences(mo, "tns_mesh_object"); laPushDifferences("Deleted primitives", TNS_HINT_GEOMETRY); laNotifyUsers("tns.world");
  1153. }
  1154. return LA_FINISHED;
  1155. }
  1156. void laui_Delete(laUiList *uil, laPropPack *pp, laPropPack *actinst, laColumn *extracol, int context){
  1157. laColumn* c=laFirstColumn(uil);
  1158. laShowItemFull(uil,c,pp,"_this_M_delete",0,"mode=vertices;text=Vertices",0,0);
  1159. laShowItemFull(uil,c,pp,"_this_M_delete",0,"mode=edges;text=Edges",0,0);
  1160. laShowItemFull(uil,c,pp,"_this_M_delete",0,"mode=faces;text=Faces",0,0);
  1161. laShowItemFull(uil,c,pp,"_this_M_delete",0,"mode=only_faces;text=Only Faces",0,0);
  1162. }
  1163. STRUCTURE(MIslandInfo){
  1164. laListItem Item;
  1165. laListHandle v,e,f;int numv,nume,numf;
  1166. int Paired,HasBranches;
  1167. };
  1168. STRUCTURE(MMakeData){
  1169. laListHandle Islands; int NumIslands;
  1170. };
  1171. #define M_SHOULD_INCL_PRIM(m) \
  1172. ((!(m->flags&TNS_MESH_FLAG_PICKED)) && (m->flags&TNS_MESH_FLAG_SELECTED))
  1173. #define M_SHOULD_USE_OE(oe,sf) \
  1174. (((!oe->fl)&&((!sf)||(oe->fr!=sf)))||((!oe->fr)&&((!sf)||(oe->fl!=sf))))
  1175. MIslandInfo* la_NewMIsland(MMakeData* md){ MIslandInfo* ii=memAcquireSimple(sizeof(MIslandInfo)); lstAppendItem(&md->Islands, ii); md->NumIslands++; return ii; }
  1176. void la_FillIslandFromVert(MIslandInfo* ii, tnsMVert* mv, int SelectMode){
  1177. mv->flags|=TNS_MESH_FLAG_PICKED; lstAppendPointer(&ii->v,mv); ii->numv++; int connections=0;
  1178. for(laListItemPointer* lip=mv->elink.pFirst;lip;lip=lip->pNext){ tnsMEdge* oe=lip->p; tnsMVert* ov=tnsMMeshEdgeAnotherVert(oe,mv);
  1179. if(ov->flags&TNS_MESH_FLAG_SELECTED) connections++; else continue;
  1180. if(SelectMode==LA_CANVAS_SELECT_MODE_EDGES){ if(!(oe->flags&TNS_MESH_FLAG_SELECTED)) continue; }
  1181. if(M_SHOULD_INCL_PRIM(ov)){ la_FillIslandFromVert(ii,ov,SelectMode); } else { continue; }
  1182. if(M_SHOULD_INCL_PRIM(oe)){ lstAppendPointer(&ii->e,oe); ii->nume++; oe->flags|=TNS_MESH_FLAG_PICKED; }
  1183. if(oe->fl&&M_SHOULD_INCL_PRIM(oe->fl)){ lstAppendPointer(&ii->f,oe->fl); ii->numf++; oe->fl->flags|=TNS_MESH_FLAG_PICKED; }
  1184. if(oe->fr&&M_SHOULD_INCL_PRIM(oe->fr)){ lstAppendPointer(&ii->f,oe->fr); ii->numf++; oe->fr->flags|=TNS_MESH_FLAG_PICKED; }
  1185. }
  1186. if(connections>2) ii->HasBranches=1;
  1187. }
  1188. void la_GetSelectionIslands(tnsMeshObject* mo, MMakeData* md, int SelectMode){
  1189. tnsMMeshClearExtraFlags(mo);
  1190. for(tnsMVert* mv=mo->mv.pFirst;mv;mv=mv->Item.pNext){
  1191. if(M_SHOULD_INCL_PRIM(mv)){ MIslandInfo* ii=la_NewMIsland(md); la_FillIslandFromVert(ii,mv,SelectMode); }
  1192. }
  1193. }
  1194. void la_ClearIslands(MMakeData* md){ MIslandInfo* ii; while(ii=lstPopItem(&md->Islands)){ while(lstPopPointer(&ii->v)); memFree(ii); } }
  1195. tnsMFace* la_MakeFacesFrom1Vert(tnsMeshObject* mo, tnsMVert* mv){
  1196. tnsMEdge* oe1=0,*oe2=0; tnsMVert* ov1,*ov2;
  1197. for(laListItemPointer* lip=mv->elink.pFirst;lip;lip=lip->pNext){
  1198. tnsMEdge* oe=lip->p; if(oe->flags&TNS_MESH_FLAG_SELECTED) continue; if((!oe->fl)||(!oe->fr)){ if(!oe1)oe1=oe;elif(!oe2)oe2=oe;else return 0; /* more than 2 empty edges connected */ }
  1199. } if(!oe1||!oe2) return 0;
  1200. ov1=tnsMMeshEdgeAnotherVert(oe1,mv); ov2=tnsMMeshEdgeAnotherVert(oe2,mv);
  1201. laListHandle vl={0}; lstAppendPointer(&vl,ov1); lstAppendPointer(&vl,mv); lstAppendPointer(&vl,ov2);
  1202. tnsMFace* f=tnsMMeshMakeFaceN(mo, 3, &vl, 0);
  1203. ov1->flags|=TNS_MESH_FLAG_SELECTED;ov2->flags|=TNS_MESH_FLAG_SELECTED; mv->flags&=(~TNS_MESH_FLAG_SELECTED);
  1204. tnsMMeshEnsureSelectionFromVerts(mo);
  1205. while(lstPopPointer(&vl)); return f;
  1206. }
  1207. tnsMFace* la_MakeFacesFrom2Verts(tnsMeshObject* mo, tnsMVert* mv1, tnsMVert* mv2){
  1208. tnsMEdge* oe1=0,*oe2=0; tnsMVert* ov1,*ov2; tnsMFace* sf=0;
  1209. tnsMEdge* se=tnsMMeshVertsShareEdge(mv1,mv2); if(se->fl && se->fr) return 0; sf=se->fl?se->fl:se->fr;
  1210. for(laListItemPointer* lip=mv1->elink.pFirst;lip;lip=lip->pNext){
  1211. tnsMEdge* oe=lip->p; if(oe->flags&TNS_MESH_FLAG_SELECTED) continue; if(M_SHOULD_USE_OE(oe,sf)){ if(!oe1)oe1=oe;else return 0; /* more than 1 empty edge connected */ }
  1212. }
  1213. for(laListItemPointer* lip=mv2->elink.pFirst;lip;lip=lip->pNext){
  1214. tnsMEdge* oe=lip->p; if(oe->flags&TNS_MESH_FLAG_SELECTED) continue; if(M_SHOULD_USE_OE(oe,sf)){ if(!oe2)oe2=oe;else return 0; /* more than 1 empty edge connected */ }
  1215. }
  1216. if(!oe1||!oe2) return 0;
  1217. ov1=tnsMMeshEdgeAnotherVert(oe1,mv1); ov2=tnsMMeshEdgeAnotherVert(oe2,mv2);
  1218. ov1->flags|=TNS_MESH_FLAG_SELECTED;ov2->flags|=TNS_MESH_FLAG_SELECTED; mv1->flags&=(~TNS_MESH_FLAG_SELECTED);mv2->flags&=(~TNS_MESH_FLAG_SELECTED);
  1219. laListHandle vl={0}; int vcount=3;
  1220. lstAppendPointer(&vl,ov1); lstAppendPointer(&vl,mv1); lstAppendPointer(&vl,mv2); if(ov2!=ov1){ lstAppendPointer(&vl,ov2); vcount=4; }
  1221. tnsMFace* f=tnsMMeshMakeFaceN(mo, vcount, &vl, 0); tnsMMeshEnsureSelectionFromVerts(mo);
  1222. while(lstPopPointer(&vl)); return f;
  1223. }
  1224. int la_IsEndingVert(tnsMVert* mv){
  1225. int sel=0; for(laListItemPointer*lip=mv->elink.pFirst;lip;lip=lip->pNext){ tnsMEdge* me=lip->p;
  1226. if(tnsMMeshEdgeAnotherVert(me,mv)->flags&TNS_MESH_FLAG_SELECTED){ sel++; if(sel>1) return 0; }
  1227. }
  1228. if(sel==1) return 1; return 0;
  1229. }
  1230. void la_EnsureIslandVertsSequence(MIslandInfo* ii){
  1231. laListHandle l={0}; tnsMVert* startv=((laListItemPointer*)ii->v.pFirst)->p; laListItemPointer* nextlip;
  1232. for(laListItemPointer*lip=ii->v.pFirst;lip;lip=lip->pNext){ if(la_IsEndingVert(lip->p)){ startv=lip->p; break; } } // otherwise a loop, doesn't matter.
  1233. lstRemovePointer(&ii->v, startv); lstAppendPointer(&l, startv);
  1234. while(ii->v.pFirst){
  1235. for(laListItemPointer*lip=ii->v.pFirst;lip;lip=lip->pNext){ nextlip=lip->pNext;
  1236. if(tnsMMeshVertsShareEdge(startv, lip->p)){ startv=lip->p; lstRemoveItem(&ii->v,lip); lstAppendItem(&l, lip); break; }
  1237. }
  1238. }
  1239. memcpy(&ii->v, &l, sizeof(laListHandle));
  1240. }
  1241. MIslandInfo* la_GetNeighborIsland(tnsMVert* from, MMakeData* md){
  1242. int found=0; real dist=1e10; MIslandInfo* rii=0; for(MIslandInfo* ii=md->Islands.pFirst;ii;ii=ii->Item.pNext){ if(ii->Paired) continue;
  1243. found=1; tnsMVert* mv1=((laListItemPointer*)ii->v.pFirst)->p,*mv2=((laListItemPointer*)ii->v.pLast)->p;
  1244. real d1=tnsDist3dv(mv1->p, from->p),d2=tnsDist3dv(mv2->p, from->p);
  1245. if(d2<d1 && d2<dist){ lstReverse(&ii->v); } if(d1<dist || d2<dist){ rii=ii; }
  1246. }
  1247. if(rii) rii->Paired=1;
  1248. return rii;
  1249. }
  1250. int la_MakeFacesFromIslands(tnsMeshObject* mo, MMakeData* md){
  1251. int success=0;
  1252. if(!md->Islands.pFirst) return 0;
  1253. if(md->Islands.pFirst==md->Islands.pLast){
  1254. MIslandInfo* ii=md->Islands.pFirst; if(ii->HasBranches) return 0;
  1255. if(ii->numv==1){ laListItemPointer*lip=ii->v.pFirst; if(la_MakeFacesFrom1Vert(mo,lip->p)) success++; }
  1256. elif(ii->numv==2){ laListItemPointer*lip=ii->v.pFirst,*lip2=ii->v.pLast; if(la_MakeFacesFrom2Verts(mo,lip->p, lip2->p)) success++; }
  1257. else{ la_EnsureIslandVertsSequence(ii); if(tnsMMeshMakeFaceN(mo, ii->numv, &ii->v, 0)) success++; }
  1258. }else{
  1259. for(MIslandInfo* ii=md->Islands.pFirst;ii;ii=ii->Item.pNext){ if(ii->HasBranches) return 0; la_EnsureIslandVertsSequence(ii); }
  1260. laListHandle final={0}; int vcount=0;
  1261. MIslandInfo* ii=md->Islands.pFirst; ii->Paired=1; for(laListItemPointer* lip=ii->v.pFirst;lip;lip=lip->pNext){ lstAppendPointer(&final, lip->p); vcount++; }
  1262. while((ii=la_GetNeighborIsland(((laListItemPointer*)ii->v.pLast)->p, md))){
  1263. for(laListItemPointer* lip=ii->v.pFirst;lip;lip=lip->pNext){ lstAppendPointer(&final, lip->p); vcount++; }
  1264. }
  1265. tnsMEdge* fallback_e=0;
  1266. if(tnsMMeshMakeFaceN(mo,vcount,&final, &fallback_e) || fallback_e) success++;
  1267. while(lstPopPointer(&final));
  1268. }
  1269. return success;
  1270. }
  1271. int OPINV_Make(laOperator *a, laEvent *e){
  1272. if(!a->This || !a->This->EndInstance){ return 0; }
  1273. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  1274. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  1275. tnsMeshObject* mo=root->Active;
  1276. if(!mo||mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode!=TNS_MESH_EDIT_MODE){ return LA_CANCELED; }
  1277. MMakeData md={0};
  1278. la_GetSelectionIslands(mo,&md,ex->SelectMode);
  1279. int success=la_MakeFacesFromIslands(mo,&md); if(success){ tnsMMeshCalculateNormal(mo); }
  1280. la_ClearIslands(&md);
  1281. tnsMMeshRefreshIndex(mo);
  1282. tnsMMeshEnsureSelection(mo,ex->SelectMode);
  1283. tnsInvalidateMeshBatch(mo);
  1284. if(laRecordInstanceDifferences(mo, "tns_mesh_object")) laPushDifferences("Make primitives", TNS_HINT_GEOMETRY);
  1285. laNotifyUsers("tns.world");
  1286. return LA_FINISHED;
  1287. }
  1288. int OPINV_Subdiv(laOperator *a, laEvent *e){
  1289. if(!a->This || !a->This->EndInstance){ return 0; }
  1290. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  1291. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  1292. tnsMeshObject* mo=root->Active;
  1293. if(mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode!=TNS_MESH_EDIT_MODE){ return LA_CANCELED; }
  1294. laListHandle pending={0}; for(tnsMEdge* me=mo->me.pFirst;me;me=me->Item.pNext){ if(me->flags&TNS_MESH_FLAG_SELECTED) lstAppendPointer(&pending, me); }
  1295. if(!pending.pFirst) return LA_FINISHED;
  1296. tnsMEdge* me; while(me=lstPopPointer(&pending)){ tnsMVert* mv=tnsMMeshEdgeInsertVertAt(mo,me,0.5,0,0,0); mv->flags|=TNS_MESH_FLAG_SELECTED; }
  1297. tnsMMeshRefreshIndex(mo);
  1298. tnsMMeshEnsureSelection(mo,ex->SelectMode);
  1299. tnsInvalidateMeshBatch(mo);
  1300. if(laRecordInstanceDifferences(mo, "tns_mesh_object")) laPushDifferences("Subdivide edges", TNS_HINT_GEOMETRY);
  1301. laNotifyUsers("tns.world");
  1302. return LA_FINISHED;
  1303. }
  1304. #define LA_ADD_CTX_OBJECT 0
  1305. #define LA_ADD_CTX_MESH 1
  1306. #define LA_ADD_CTX_SHAPE 2
  1307. STRUCTURE(laObjectAddData){
  1308. int Context;
  1309. };
  1310. int OPINV_Add(laOperator *a, laEvent *e){
  1311. if(!a->This || !a->This->EndInstance){ return 0; }
  1312. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  1313. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  1314. tnsMeshObject* mo=root->Active; int ran=0; tnsObject* no=0;
  1315. laObjectAddData *ad=memAcquire(sizeof(laObjectAddData));a->CustomData=ad;
  1316. char* str=strGetArgumentString(a->ExtraInstructionsP, "mode");
  1317. if((!mo) || mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode!=TNS_MESH_EDIT_MODE){ ad->Context=LA_ADD_CTX_OBJECT;
  1318. if(strSame(str,"PLANE")){ tnsDeselectAllObjects(root);
  1319. no=tnsCreateMeshPlane(root, "Plane",0,0,0,1); no->Flags|=TNS_OBJECT_FLAGS_SELECTED; memAssignRef(root,&root->Active,no); ran=1; }
  1320. elif(strSame(str,"INSTANCER")){ tnsDeselectAllObjects(root);
  1321. no=tnsCreateInstancer(root, "Instancer",0,0,0); no->Flags|=TNS_OBJECT_FLAGS_SELECTED; memAssignRef(root,&root->Active,no); ran=1; }
  1322. elif(strSame(str,"SQUARE")){ tnsDeselectAllObjects(root);
  1323. no=tnsCreateShapeSquare(root, "Square",0,0,0,1); no->Flags|=TNS_OBJECT_FLAGS_SELECTED; memAssignRef(root,&root->Active,no); ran=1; }
  1324. else{ laEnableOperatorPanel(a,a->This,e->x,e->y,200,200,0,0,0,0,0,0,0,0,e); return LA_RUNNING; }
  1325. if(ran){ laRecordAndPush(0,"tns.world","Add object",TNS_HINT_GEOMETRY); laNotifyUsers("tns.world"); }
  1326. }elif(mo->Base.Type==TNS_OBJECT_MESH && mo->Mode!=TNS_MESH_EDIT_MODE){ ad->Context=LA_ADD_CTX_MESH;
  1327. if(strSame(str,"PLANE")){
  1328. tnsMMeshDeselectAll(mo); tnsAddMMeshPlane(mo, 1); tnsMMeshEnsureSelection(mo,ex->SelectMode); ran=1;
  1329. }else{ laEnableOperatorPanel(a,a->This,e->x,e->y,200,200,0,0,0,0,0,0,0,0,e); return LA_RUNNING; }
  1330. if(ran){
  1331. tnsMMeshRefreshIndex(mo); tnsInvalidateMeshBatch(mo);
  1332. laRecordInstanceDifferences(mo, "tns_mesh_object"); laPushDifferences("Add primitives", TNS_HINT_GEOMETRY); laNotifyUsers("tns.world");
  1333. }
  1334. }elif(mo->Base.Type==TNS_OBJECT_SHAPE && mo->Mode!=TNS_MESH_EDIT_MODE){ ad->Context=LA_ADD_CTX_SHAPE;
  1335. if(strSame(str,"SQUARE")){
  1336. //stuff
  1337. ran=1;
  1338. }else{ laEnableOperatorPanel(a,a->This,e->x,e->y,200,200,0,0,0,0,0,0,0,0,e); return LA_RUNNING; }
  1339. if(ran){
  1340. //stuff
  1341. laRecordInstanceDifferences(mo, "tns_shape_object"); laPushDifferences("Add primitives", TNS_HINT_GEOMETRY); laNotifyUsers("tns.world");
  1342. }
  1343. }
  1344. return LA_FINISHED;
  1345. }
  1346. void laui_Add(laUiList *uil, laPropPack *pp, laPropPack *actinst, laColumn *extracol, int context){
  1347. laColumn* c=laFirstColumn(uil);
  1348. laUiItem* b=laOnConditionThat(uil,c,laEqual(laPropExpression(actinst,"context"),laIntExpression(LA_ADD_CTX_OBJECT)));{
  1349. laShowLabel(uil,c,"Empty",0,0)->Flags|=LA_TEXT_MONO|LA_UI_FLAGS_DISABLED;
  1350. laShowItemFull(uil,c,pp,"_this_M_add",0,"mode=INSTANCER;text=Instancer",0,0);
  1351. laShowLabel(uil,c,"Primitives",0,0)->Flags|=LA_TEXT_MONO|LA_UI_FLAGS_DISABLED;
  1352. laShowItemFull(uil,c,pp,"_this_M_add",0,"mode=PLANE;text=Plane",0,0);
  1353. laShowLabel(uil,c,"Shapes",0,0)->Flags|=LA_TEXT_MONO|LA_UI_FLAGS_DISABLED;
  1354. laShowItemFull(uil,c,pp,"_this_M_add",0,"mode=SQUARE;text=Square",0,0);
  1355. }laEndCondition(uil,b);
  1356. b=laOnConditionThat(uil,c,laEqual(laPropExpression(actinst,"context"),laIntExpression(LA_ADD_CTX_MESH)));{
  1357. laShowLabel(uil,c,"Primitives",0,0)->Flags|=LA_TEXT_MONO|LA_UI_FLAGS_DISABLED;
  1358. laShowItemFull(uil,c,pp,"_this_M_add",0,"mode=PLANE;text=Plane",0,0);
  1359. }laEndCondition(uil,b);
  1360. b=laOnConditionThat(uil,c,laEqual(laPropExpression(actinst,"context"),laIntExpression(LA_ADD_CTX_SHAPE)));{
  1361. laShowLabel(uil,c,"Shapes",0,0)->Flags|=LA_TEXT_MONO|LA_UI_FLAGS_DISABLED;
  1362. laShowItemFull(uil,c,pp,"_this_M_add",0,"mode=SQUARE;text=Square",0,0);
  1363. }laEndCondition(uil,b);
  1364. }
  1365. int OPINV_Separate(laOperator *a, laEvent *e){
  1366. if(!a->This || !a->This->EndInstance){ return 0; }
  1367. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  1368. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return LA_CANCELED;
  1369. tnsMeshObject* mo=root->Active; int ran=0;
  1370. if(mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode!=TNS_MESH_EDIT_MODE){
  1371. return LA_CANCELED;
  1372. }
  1373. if(!tnsMMeshAnySelected(mo)) return LA_CANCELED;
  1374. MExtrudeExtra* ee=la_InitExtrude(mo);
  1375. la_ExtrudeMakeDuplication(ee);
  1376. ee->RemoveOriginalFaces=1;la_RemoveOriginalFaces(ee);
  1377. tnsMeshObject* no=tnsCreateMeshEmpty(mo->Base.ParentObject?mo->Base.ParentObject:mo->Base.InRoot, mo->Base.Name->Ptr, 0,0,0);
  1378. tnsCopyObjectTransformationsLocal(no,mo);
  1379. no->Mode=TNS_MESH_EDIT_MODE;
  1380. tnsMVert* nmv; for(tnsMVert* mv=mo->mv.pFirst;mv;mv=nmv){ nmv=mv->Item.pNext; if(!(mv->flags&TNS_MESH_FLAG_SELECTED))continue;
  1381. lstRemoveItem(&mo->mv, mv); lstAppendItem(&no->mv, mv); no->totmv++; mo->totmv--; }
  1382. tnsMEdge* nme; for(tnsMEdge* me=mo->me.pFirst;me;me=nme){ nme=me->Item.pNext; if(!(me->flags&TNS_MESH_FLAG_SELECTED))continue;
  1383. lstRemoveItem(&mo->me, me); lstAppendItem(&no->me, me); no->totme++; mo->totme--; }
  1384. tnsMFace* nmf; for(tnsMFace* mf=mo->mf.pFirst;mf;mf=nmf){ nmf=mf->Item.pNext; if(!(mf->flags&TNS_MESH_FLAG_SELECTED))continue;
  1385. lstRemoveItem(&mo->mf, mf); lstAppendItem(&no->mf, mf); no->totmf++; mo->totmf--; }
  1386. tnsMMeshRefreshIndex(no); tnsMeshLeaveEditMode(no);
  1387. la_FinishExtrude(ee, 0);
  1388. tnsMMeshRefreshIndex(mo); tnsInvalidateMeshBatch(mo);
  1389. laRecordAndPush(0,"tns.world","Separate mesh parts",TNS_HINT_GEOMETRY); laNotifyUsers("tns.world");
  1390. return LA_FINISHED;
  1391. }
  1392. void la_PopulateSelectedObjects(tnsObject* root, laListHandle* l, int FilterType){
  1393. if(root->Flags&TNS_OBJECT_FLAGS_SELECTED){ if(root->Type && ((!FilterType) || root->Type==FilterType)) lstAppendPointer(l,root); }
  1394. for(laListItemPointer* lip=root->ChildObjects.pFirst;lip;lip=lip->pNext){
  1395. la_PopulateSelectedObjects(lip->p, l, FilterType);
  1396. }
  1397. }
  1398. int OPINV_Combine(laOperator *a, laEvent *e){
  1399. if(!a->This || !a->This->EndInstance){ return 0; }
  1400. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  1401. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return LA_CANCELED;
  1402. tnsMeshObject* mo=root->Active; int ran=0;
  1403. if(!mo || mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode==TNS_MESH_EDIT_MODE){ return LA_CANCELED; }
  1404. laListHandle pending={0}; la_PopulateSelectedObjects(root,&pending,TNS_OBJECT_MESH);
  1405. tnsMeshObject* o; while(o=lstPopPointer(&pending)){ if(o==mo || o->Mode==TNS_MESH_EDIT_MODE) continue;
  1406. if(tnsMergeMeshObjects(mo, o)) ran++;
  1407. }
  1408. if(ran){
  1409. tnsMMeshRefreshIndex(mo); tnsInvalidateMeshBatch(mo);
  1410. laRecordAndPush(0,"tns.world","Merge mesh objects",TNS_HINT_GEOMETRY); laNotifyUsers("tns.world");
  1411. }
  1412. return LA_FINISHED;
  1413. }
  1414. int OPINV_Duplicate(laOperator *a, laEvent *e){
  1415. if(!a->This || !a->This->EndInstance){ return 0; }
  1416. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  1417. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return LA_CANCELED;
  1418. tnsMeshObject* mo=root->Active; int ran=0;
  1419. if(!mo || mo->Mode==TNS_MESH_EDIT_MODE){ return LA_CANCELED; }
  1420. laListHandle pending={0}; la_PopulateSelectedObjects(root,&pending,0);
  1421. tnsMeshObject* o; tnsMeshObject* no; laListItemPointer* lip;
  1422. for(lip=pending.pFirst;lip;lip=lip->pNext){ o=lip->p; o->Base.EditDuplicateTemp=0; if (o->Mode == TNS_MESH_EDIT_MODE) continue;
  1423. if(o->Base.Type==TNS_OBJECT_MESH) no = tnsDuplicateMeshObject(o);
  1424. elif(o->Base.Type==TNS_OBJECT_SHAPE) no = tnsDuplicateShapeObject(o);
  1425. elif(o->Base.Type==TNS_OBJECT_INSTANCER) no = tnsDuplicateShapeObject(o);
  1426. if(no){ o->Base.EditDuplicateTemp=no;
  1427. no->Base.Flags |= TNS_OBJECT_FLAGS_SELECTED; o->Base.Flags &= (~TNS_OBJECT_FLAGS_SELECTED);
  1428. if (mo == o) { memAssignRef(root, &root->Active, no); } ran++;
  1429. }
  1430. }
  1431. for (lip = pending.pFirst;lip;lip=lip->pNext){ o=lip->p; no = o->Base.EditDuplicateTemp;
  1432. tnsObject* NewParent=o->Base.ParentObject?o->Base.ParentObject->EditDuplicateTemp:0;
  1433. if(NewParent){ tnsUnparentObject(no,1); tnsParentObject(no,NewParent,1); }
  1434. }
  1435. while(o=lstPopPointer(&pending)){ o->Base.EditDuplicateTemp=0; }
  1436. if(ran){
  1437. laRecordAndPush(0,"tns.world","Duplicated objects",TNS_HINT_GEOMETRY); laNotifyUsers("tns.world");
  1438. if(la_InitTransform(a,e,LA_TRANSFORM_MODE_GRAB,0,0)) return LA_RUNNING; return LA_FINISHED;
  1439. }
  1440. return LA_FINISHED;
  1441. }
  1442. int OPINV_RecalculateNormals(laOperator *a, laEvent *e){
  1443. if(!a->This || !a->This->EndInstance){ return 0; }
  1444. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  1445. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return LA_CANCELED;
  1446. tnsMeshObject* mo=root->Active; int ran=0;
  1447. if(!mo || mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode!=TNS_MESH_EDIT_MODE){ return LA_CANCELED; }
  1448. ran=tnsMMeshCalculateNormal(mo);
  1449. if(ran){ tnsInvalidateMeshBatch(mo); laRecordAndPush(0,"tns.world","Recalculate Normals",TNS_HINT_GEOMETRY); laNotifyUsers("tns.world"); }
  1450. return LA_FINISHED;
  1451. }
  1452. STRUCTURE(MKnifeElement){
  1453. laListItem Item;
  1454. void* p;
  1455. int Type;
  1456. };
  1457. void la_KnifeUpdateToolBatch(MSelectExtra* se,tnsMeshObject* o){
  1458. if(o->ExtraBatch) tnsDeleteBatch(o->ExtraBatch); o->ExtraBatch=0;
  1459. int count=lstCountElements(&se->KnifeElements); if((!count) && (!se->PendingElem)) return;
  1460. float* points=calloc((count+1)*3,sizeof(real));
  1461. float* p=points; real tmp[3],trans[4];
  1462. for(MKnifeElement* ke=se->KnifeElements.pFirst;ke;ke=ke->Item.pNext){
  1463. if(ke->Type==TNS_MMESH_EDGE_BIT){ tnsMEdge* me=ke->p; tnsVectorSet3v(tmp,me->vl->p); tnsVectorAccum3d(tmp,me->vr->p);
  1464. tnsVectorMultiSelf3d(tmp,0.5); tnsVectorSet3v(p,tmp); }
  1465. else{ tnsMVert* mv=ke->p; tnsVectorSet3v(p,mv->p); }
  1466. p+=3;
  1467. }
  1468. if(se->PendingElem){
  1469. if(se->PendingElemType==TNS_MMESH_EDGE_BIT){ tnsMEdge* me=se->PendingElem; tnsVectorSet3v(tmp,me->vl->p); tnsVectorAccum3d(tmp,me->vr->p);
  1470. tnsVectorMultiSelf3d(tmp,0.5); tnsVectorSet3v(p,trans); }
  1471. else{ tnsMVert* mv=se->PendingElem; tnsVectorSet3v(p,mv->p); }
  1472. }elif(count){
  1473. tnsVectorSet3v(p,p-3);
  1474. }
  1475. uint32_t elem=count;
  1476. tnsBatch* batch=tnsCreateBatch(count+1,3,points,0,0,0,0); tnsBatchCommand*c;
  1477. c=tnsCreateCommand(batch, "hovering_point", 1, 3, GL_POINTS, &elem, 0);
  1478. tnsCommandUseUniformColor(c,laAccentColor(LA_BT_SVERTEX));
  1479. tnsCommandUseWidth(c, 8);
  1480. if(count){
  1481. c=tnsCreateCommand(batch, "edges", count+(se->IsLoop?0:1), 3, se->IsLoop?GL_LINE_LOOP:GL_LINE_STRIP, 0, 0);
  1482. tnsCommandUseUniformColor(c,laAccentColor(LA_BT_NORMAL));
  1483. tnsCommandUseWidth(c, 2);
  1484. c=tnsCreateCommand(batch, "points", count, 3, GL_POINTS, 0, 0);
  1485. tnsCommandUseUniformColor(c,laAccentColor(LA_BT_NORMAL));
  1486. tnsCommandUseWidth(c, 6);
  1487. }
  1488. o->ExtraBatch=batch;
  1489. free(points);
  1490. }
  1491. int la_KnifeIsDuplicated(MSelectExtra* se, void* ref){
  1492. for(MKnifeElement* ke=se->KnifeElements.pFirst;ke;ke=ke->Item.pNext){
  1493. if(ke->Type==TNS_MMESH_EDGE_BIT && ke->p==ref){ return 1; }
  1494. }
  1495. return 0;
  1496. }
  1497. void la_KnifeAppendCut(MSelectExtra* se){
  1498. MKnifeElement* ke=lstAppendPointerSized(&se->KnifeElements,se->PendingElem,sizeof(MKnifeElement));
  1499. ke->Type=se->PendingElemType;
  1500. }
  1501. int la_KnifeRegisterCuts(MSelectExtra* se, tnsMeshObject* mo, int TryClose){
  1502. if(!mo || mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode!=TNS_MESH_EDIT_MODE) return 0;
  1503. if(!se->KnifeElements.pFirst) return 0;
  1504. tnsMVert* lastv=0,*newv=0,*firstv=0; tnsMEdge* newme=0; int changed=0; tnsMFace* mf=0;
  1505. for(MKnifeElement* ke=se->KnifeElements.pFirst;ke;ke=ke->Item.pNext){
  1506. if(ke->Type==TNS_MMESH_EDGE_BIT){
  1507. newv=tnsMMeshEdgeInsertVertAt(mo,ke->p,0.5,0,0,0); changed=1;
  1508. }
  1509. else{ newv=ke->p; }
  1510. if(lastv){ if(tnsMMeshVertsShareFace(lastv,newv)) newme=tnsMMeshMakeEdge(mo, lastv, newv); changed=1; }
  1511. lastv=newv; if(!firstv) firstv=newv; newv->flags|=TNS_MESH_FLAG_SELECTED;
  1512. if(newme){ newme->flags|=TNS_MESH_FLAG_SELECTED; }
  1513. }
  1514. if(TryClose){ if((mf=tnsMMeshVertsShareFace(lastv,firstv))&&mf->looplen==6) newme=tnsMMeshMakeEdge(mo, lastv, firstv); changed=1; }
  1515. if(changed){ tnsMMeshRefreshIndex(mo); tnsMMeshCalculateNormal(mo); }
  1516. return changed;
  1517. }
  1518. void la_KnifeFinish(MSelectExtra* se, tnsMeshObject*o){
  1519. if(o->ExtraBatch) tnsDeleteBatch(o->ExtraBatch); o->ExtraBatch=0;
  1520. while(lstPopPointer(&se->KnifeElements));
  1521. la_FreeSelectData(se->sd); memFree(se);
  1522. }
  1523. void la_KnifeRefreshLoopCuts(MSelectExtra* se, tnsObject* o,tnsMEdge* me){
  1524. if(!me) return;
  1525. tnsMMeshClearExtraFlags(o);
  1526. laListHandle lst={0}; lstAppendPointer(&lst,me); me->flags|=TNS_MESH_FLAG_PICKED;
  1527. tnsMMeshExpandBandList(o, me, &lst);
  1528. if(lst.pFirst==lst.pLast){ while(lstPopPointer(&lst)); return; }
  1529. tnsMEdge* mme; while((mme=lstPopPointer(&lst))){
  1530. MKnifeElement* ke=lstAppendPointerSized(&se->KnifeElements,mme,sizeof(MKnifeElement));
  1531. ke->Type=TNS_MMESH_EDGE_BIT;
  1532. }
  1533. }
  1534. void la_KnifeSortLoopCuts(MSelectExtra* se){
  1535. laListHandle lst={0};MKnifeElement* ke=se->KnifeElements.pFirst,*NextKe;
  1536. if(!ke) return;
  1537. lstRemoveItem(&se->KnifeElements,ke); lstAppendItem(&lst,ke);
  1538. for(ke=se->KnifeElements.pFirst;ke;ke=NextKe){
  1539. NextKe=ke->Item.pNext; tnsMEdge* me=ke->p,*firste=((MKnifeElement*)lst.pFirst)->p,*laste=((MKnifeElement*)lst.pLast)->p;
  1540. if(tnsMMeshEdgeShareFace(me,firste) && (!tnsMMeshEdgeShareVert(me,firste))){
  1541. lstRemoveItem(&se->KnifeElements,ke); lstPushItem(&lst,ke); ke=se->KnifeElements.pFirst; continue; }
  1542. if(tnsMMeshEdgeShareFace(me,laste) && (!tnsMMeshEdgeShareVert(me,laste))){
  1543. lstRemoveItem(&se->KnifeElements,ke); lstAppendItem(&lst,ke); ke=se->KnifeElements.pFirst; continue; }
  1544. }
  1545. if(se->KnifeElements.pFirst){ /* something went wrong */ while(lstPopPointer(&se->KnifeElements)); while(lstPopPointer(&lst)); }
  1546. se->KnifeElements.pFirst=lst.pFirst; se->KnifeElements.pLast=lst.pLast;
  1547. }
  1548. int OPINV_Knife(laOperator *a, laEvent *e){
  1549. if(!a->This || !a->This->EndInstance){ return 0; }
  1550. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  1551. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return LA_CANCELED;
  1552. tnsMeshObject* mo=root->Active; int ran=0;
  1553. if(!mo || mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode!=TNS_MESH_EDIT_MODE){ return LA_CANCELED; }
  1554. MSelectExtra* se=memAcquire(sizeof(MSelectExtra));
  1555. MSelectData* sd=la_InitSelectData(ex->OffScr->pColor[0]->Width, ex->OffScr->pColor[0]->Height, c);
  1556. a->CustomData=se; se->sd=sd; se->root=root;
  1557. int SelectMode=LA_CANVAS_SELECT_MODE_KNIFE;
  1558. if(strSame(strGetArgumentString(a->ExtraInstructionsP, "mode"), "loop_cut")){
  1559. se->IsLoop=1; SelectMode=LA_CANVAS_SELECT_MODE_EDGES; }
  1560. la_PopulateSelectDataPrimitives(sd,mo,c,SelectMode,root,ex);
  1561. if(se->IsLoop){ strSafePrint(&a->RuntimeHint,"◧ Cut ◨ Cancel"); }
  1562. else{ strSafePrint(&a->RuntimeHint,"◧ Place Cut ◨ Cancel ⮨ Confirm"); }
  1563. return LA_RUNNING;
  1564. }
  1565. int OPMOD_Knife(laOperator *a, laEvent *e){
  1566. if(!a->This || !a->This->EndInstance || !a->CustomData){ return 0; }
  1567. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  1568. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  1569. MSelectExtra* se=a->CustomData;
  1570. tnsMeshObject* mo=root->Active;
  1571. int changed=0;
  1572. if(e->Type==LA_R_MOUSE_DOWN || (e->Type == LA_KEY_DOWN && e->key==LA_KEY_ESCAPE)){
  1573. la_KnifeFinish(se,mo); laNotifyUsers("tns.world"); return LA_FINISHED;
  1574. }
  1575. if(e->Type&LA_MOUSE_EVENT){
  1576. int elemtype,id=la_SelectGetClosest(se->sd, e->x-ui->L, e->y-ui->U, LA_RH,&elemtype)-1;
  1577. void* p=la_SelectGetRef(se->sd,id,elemtype);
  1578. if(se->IsLoop){
  1579. if(se->PendingElem!=p){ changed=1; while(lstPopPointer(&se->KnifeElements));
  1580. se->PendingElem=p; la_KnifeRefreshLoopCuts(se, mo, se->PendingElem);
  1581. la_KnifeSortLoopCuts(se);
  1582. }
  1583. }else{
  1584. if(la_KnifeIsDuplicated(se,p)) p=0;
  1585. if(se->PendingElem!=p){ changed=1; }
  1586. se->PendingElem=p; se->PendingElemType=elemtype;
  1587. if(e->Type==LA_L_MOUSE_DOWN && p){ la_KnifeAppendCut(se); changed=1; }
  1588. }
  1589. }
  1590. if((e->Type==LA_KEY_DOWN && e->key==LA_KEY_ENTER) || (se->IsLoop && e->Type==LA_L_MOUSE_DOWN)){
  1591. if(la_KnifeRegisterCuts(se,mo,se->IsLoop)){ tnsMMeshEnsureSelection(mo,ex->SelectMode);
  1592. tnsInvalidateMeshBatch(mo); laNotifyUsers("tns.world");
  1593. laRecordAndPush(0,"tns.world",se->IsLoop?"Loop Cut":"Knife Cut",TNS_HINT_GEOMETRY);
  1594. }
  1595. la_KnifeFinish(se,mo); return LA_FINISHED;
  1596. }
  1597. if(changed){
  1598. la_KnifeUpdateToolBatch(se,mo); laRedrawCurrentPanel();
  1599. }
  1600. return LA_RUNNING;
  1601. }
  1602. #define LA_MERGE_MODE_CENTER 0
  1603. #define LA_MERGE_MODE_FIRST 1
  1604. #define LA_MERGE_MODE_LAST 2
  1605. int la_MergeGetCenter(tnsMeshObject* mo, int SelectMode, int MergeMode, real* center){
  1606. real pos[3]={0},tmp[3]={0}; int count=0;
  1607. tnsMVert* usev=0; tnsMEdge* usee=0;
  1608. if(MergeMode!=LA_MERGE_MODE_CENTER){
  1609. if(MergeMode==LA_MERGE_MODE_FIRST){ usev=mo->FirstSelectV; usee=mo->FirstSelectE; }
  1610. elif(MergeMode==LA_MERGE_MODE_LAST){ usev=mo->LastSelectV; usee=mo->LastSelectE; }
  1611. if(SelectMode==LA_CANVAS_SELECT_MODE_VERTS && usev){ tnsVectorCopy3d(usev->p,center); return 1; }
  1612. elif(SelectMode==LA_CANVAS_SELECT_MODE_EDGES && usee){
  1613. tnsVectorAccum3d(pos,usee->vl->p);tnsVectorAccum3d(pos,usee->vr->p);
  1614. tnsVectorMultiSelf3d(pos,0.5f); tnsVectorCopy3d(pos,center); return 1;
  1615. }
  1616. }
  1617. for(tnsMVert* mv=mo->mv.pFirst;mv;mv=mv->Item.pNext){ if(!(mv->flags&TNS_MESH_FLAG_SELECTED)) continue;
  1618. tnsVectorAccum3d(pos,mv->p); count++;
  1619. }
  1620. if(!count) return 0;
  1621. tnsVectorMultiSelf3d(pos,1.0f/count); tnsVectorCopy3d(pos,center);
  1622. return 1;
  1623. }
  1624. void la_MergeSelected(tnsMeshObject* mo, real* center, int* count_success, int* count_fail){
  1625. tnsMVert* fmv=0,*NextMv=0; int success=0,fail=0,has_success=0;
  1626. do{ fail=0; has_success=0;
  1627. for(tnsMVert* mv=mo->mv.pFirst;mv;mv=NextMv){ NextMv=mv->Item.pNext;
  1628. if((!(mv->flags&TNS_MESH_FLAG_SELECTED))||mv==fmv) continue; if(!fmv){ fmv=mv; success++; continue; }
  1629. if(tnsMMeshMergeVerts(mo,fmv,mv)){ success++; has_success=1; }else{ fail++; }
  1630. }
  1631. }while(has_success&&fail);
  1632. if(count_success){ *count_success=success; } if(count_fail){ *count_fail=fail; }
  1633. if(!fmv) return; tnsVectorCopy3d(center,fmv->p);
  1634. tnsMMeshRefreshIndex(mo);
  1635. }
  1636. int OPINV_Merge(laOperator *a, laEvent *e){
  1637. if(!a->This || !a->This->EndInstance){ return 0; }
  1638. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  1639. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return LA_CANCELED;
  1640. tnsMeshObject* mo=root->Active; int ran=0;
  1641. if(!mo || mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode!=TNS_MESH_EDIT_MODE){ return LA_CANCELED; }
  1642. char* arg=strGetArgumentString(a->ExtraInstructionsP, "towards");
  1643. if(arg){
  1644. int MergeMode=LA_MERGE_MODE_CENTER; real center[3];
  1645. if(strSame(arg,"FIRST")){ MergeMode=LA_MERGE_MODE_FIRST; }
  1646. elif(strSame(arg,"LAST")){ MergeMode=LA_MERGE_MODE_LAST; }
  1647. if(!(la_MergeGetCenter(mo,ex->SelectMode,MergeMode,center))){
  1648. laEnableMessagePanel(a, 0, "Can't perform merging:", "No vertices selected.", e->x, e->y, 0, e);
  1649. return LA_FINISHED;
  1650. }
  1651. int success,fail;
  1652. la_MergeSelected(mo,center,&success,&fail); //printf("succeeded: %d failed:%d\n",success,fail);
  1653. if(fail){
  1654. char* msg[256]; sprintf(msg,"Succeeded: %d vertices, failed: %d vertices.",success,fail);
  1655. laEnableMessagePanel(a, 0, "Merging partially succeeded.", msg, e->x, e->y, 0, e);
  1656. }
  1657. if(success){
  1658. tnsMMeshEnsureSelection(mo,ex->SelectMode); tnsInvalidateMeshBatch(mo); laNotifyUsers("tns.world");
  1659. laRecordAndPush(0,"tns.world","Merge vertices",TNS_HINT_GEOMETRY);
  1660. }
  1661. }else{
  1662. laEnableOperatorPanel(a,a->This,e->x,e->y,200,200,0,0,0,0,0,0,0,0,e); return LA_RUNNING;
  1663. }
  1664. return LA_FINISHED;
  1665. }
  1666. void laui_Merge(laUiList *uil, laPropPack *pp, laPropPack *actinst, laColumn *extracol, int context){
  1667. laColumn* c=laFirstColumn(uil);
  1668. laShowItemFull(uil,c,pp,"_this_M_merge",0,"towards=CENTER;text=Center",0,0);
  1669. laShowItemFull(uil,c,pp,"_this_M_merge",0,"towards=FIRST;text=First",0,0);
  1670. laShowItemFull(uil,c,pp,"_this_M_merge",0,"towards=LAST;text=Last",0,0);
  1671. }
  1672. int OPINV_NewRootObject(laOperator *a, laEvent *e){
  1673. int use_2d=0;
  1674. if(strSame(strGetArgumentString(&a->ExtraInstructionsP,"type"),"2d")){ use_2d=1; }
  1675. tnsCreateRootObject("Root", use_2d); laNotifyUsers("tns.world.root_objects"); laNotifyUsers("tns.world.active_root");
  1676. laRecordDifferences(0,"tns.world"); laPushDifferences("New root object",0);
  1677. return LA_FINISHED;
  1678. }
  1679. int OPCHK_RemoveRootObjects(laPropPack *This, laStringSplitor *ss){
  1680. if(This && This->EndInstance){
  1681. if(la_EnsureSubTarget(This->LastPs->p,0)!=TNS_PC_OBJECT_GENERIC) return 0;
  1682. if(((tnsObject*)This->EndInstance)->Type!=TNS_OBJECT_ROOT) return 0;
  1683. }else{ if(!T->World->ActiveRoot) return 0; }
  1684. return 1;
  1685. }
  1686. int OPINV_RemoveRootObject(laOperator *a, laEvent *e){
  1687. tnsObject* root=0;
  1688. if(a->This && a->This->EndInstance){ root=a->This->EndInstance; }
  1689. else{ root=T->World->ActiveRoot; }
  1690. laNotifyDetached(root,root->Item.pNext?root->Item.pNext:root->Item.pPrev);
  1691. tnsDestroyRootObject(root);
  1692. laNotifyUsers("tns.world.root_objects"); laNotifyUsers("tns.world.active_root");
  1693. laRecordDifferences(0,"tns.world"); laPushDifferences("Removed root object",0);
  1694. return LA_FINISHED;
  1695. }
  1696. int OPINV_NewMaterial(laOperator *a, laEvent *e){
  1697. tnsMaterial* mat=tnsCreateMaterial("Material"); laNotifyUsers("tns.world.materials");
  1698. if(a->This && a->This->EndInstance && la_EnsureSubTarget(a->This->LastPs->p,a->This->EndInstance)==TNS_PC_OBJECT_MESH){
  1699. tnsMeshObject* mo=a->This->EndInstance; if(mo->CurrentMaterial){
  1700. memAssignRef(mo->CurrentMaterial,&mo->CurrentMaterial->Material,mat);
  1701. tnsInvalidateMeshBatch(mo);
  1702. laRecordInstanceDifferences(mo,"tns_mesh_object"); laNotifyInstanceUsers(mo);
  1703. }
  1704. }
  1705. laDetachedTrySet("material",mat);
  1706. laRecordDifferences(0,"tns.world.materials"); laPushDifferences("New material",0);
  1707. return LA_FINISHED;
  1708. }
  1709. int OPCHK_RemoveMaterial(laPropPack *This, laStringSplitor *ss){
  1710. if(This && This->EndInstance){ if(la_EnsureSubTarget(This->LastPs->p,0)==TNS_PC_MATERIAL) return 1; }
  1711. return 0;
  1712. }
  1713. int OPINV_RemoveMaterial(laOperator *a, laEvent *e){
  1714. if(!a->This || !a->This->EndInstance){ return LA_FINISHED; }
  1715. tnsMaterial* mat=a->This->EndInstance;
  1716. laNotifyDetached(mat,mat->Item.pNext?mat->Item.pNext:mat->Item.pPrev);
  1717. tnsRemoveMaterial(mat); laNotifyUsers("tns.world");
  1718. laRecordDifferences(0,"tns.world"); laPushDifferences("Removed material",0);
  1719. return LA_FINISHED;
  1720. }
  1721. int OPCHK_NewMaterialSlot(laPropPack *This, laStringSplitor *ss){
  1722. if(This && This->EndInstance){ if(la_EnsureSubTarget(This->LastPs->p,This->EndInstance)==TNS_PC_OBJECT_MESH) return 1; } return 0;
  1723. }
  1724. int OPINV_NewMaterialSlot(laOperator *a, laEvent *e){
  1725. if(!a->This || !a->This->EndInstance){ return LA_FINISHED; } tnsMeshObject* mo=a->This->EndInstance;
  1726. tnsNewMaterialSlot(mo); laNotifyInstanceUsers(mo);
  1727. laRecordDifferences(0,"tns.world.objects"); laPushDifferences("New material slot",0);
  1728. return LA_FINISHED;
  1729. }
  1730. int OPCHK_RemoveMaterialSlot(laPropPack *This, laStringSplitor *ss){
  1731. if(This && This->EndInstance){ if(la_EnsureSubTarget(This->LastPs->p,0)==TNS_PC_MATERIAL_SLOT) return 1; } return 0;
  1732. }
  1733. int OPINV_RemoveMaterialSlot(laOperator *a, laEvent *e){
  1734. if(!a->This || !a->This->EndInstance){ return LA_FINISHED; } tnsMaterialSlot* ms=a->This->EndInstance;
  1735. laNotifyInstanceUsers(ms->Parent); tnsRemoveMaterialSlot(ms->Parent,ms);
  1736. laRecordDifferences(0,"tns.world.objects"); laPushDifferences("Remove material slot",0);
  1737. return LA_FINISHED;
  1738. }
  1739. int OPCHK_AssignMaterialSlot(laPropPack *This, laStringSplitor *ss){
  1740. if(This && This->EndInstance){
  1741. if(la_EnsureSubTarget(This->LastPs->p,This->EndInstance)==TNS_PC_OBJECT_MESH){
  1742. tnsMeshObject* mo=This->EndInstance; if(mo->Mode==TNS_MESH_EDIT_MODE && mo->CurrentMaterial) return 1;
  1743. }
  1744. } return 0;
  1745. }
  1746. int OPINV_AssignMaterialSlot(laOperator *a, laEvent *e){
  1747. if(!a->This || !a->This->EndInstance){ return LA_FINISHED; }
  1748. tnsMeshObject* mo=a->This->EndInstance; tnsMaterialSlot* ms=mo->CurrentMaterial;
  1749. if(mo->Mode!=TNS_MESH_EDIT_MODE || !ms) return LA_FINISHED;
  1750. tnsAssignMaterialSlot(mo,ms); tnsInvalidateMeshBatch(mo);
  1751. laNotifyInstanceUsers(mo); laNotifyUsers("tns.world");
  1752. laRecordInstanceDifferences(mo,"tns_mesh_object"); laPushDifferences("Assign material slot",0);
  1753. return LA_FINISHED;
  1754. }
  1755. int OPCHK_RefreshMaterialShader(laPropPack *This, laStringSplitor *ss){
  1756. if(This && This->EndInstance){
  1757. if(la_EnsureSubTarget(This->LastPs->p,This->EndInstance)==TNS_PC_MATERIAL){ return 1; }
  1758. } return 0;
  1759. }
  1760. int OPINV_RefreshMaterialShader(laOperator *a, laEvent *e){
  1761. if(!a->This || !a->This->EndInstance){ return LA_FINISHED; }
  1762. tnsMaterial* m=a->This->EndInstance;
  1763. if(m->AsLibrary){ tnsRefreshMaterialLibraries(); return LA_FINISHED; }
  1764. tnsEnsureMaterialShader(m,1); tns_InvalidateMeshWithMaterial(m); laNotifyUsers("tns.world");
  1765. return LA_FINISHED;
  1766. }
  1767. void la_RegisterModellingOperators(){
  1768. laPropContainer *pc; laProp *p;
  1769. laOperatorType *at; laEnumProp *ep;
  1770. laCreateOperatorType("M_new_root", "New Root", "Create a new root object", 0, 0, 0, OPINV_NewRootObject, 0, '+', 0);
  1771. laCreateOperatorType("M_remove_root", "Remove Root", "Remove root object", OPCHK_RemoveRootObjects, 0, 0, OPINV_RemoveRootObject, 0 ,L'🗴', 0);
  1772. laCreateOperatorType("M_new_material", "New Material", "Create a new material", 0, 0, 0, OPINV_NewMaterial, 0, '+', 0);
  1773. laCreateOperatorType("M_remove_material", "Remove Material", "Remove a material", OPCHK_RemoveMaterial, 0, 0, OPINV_RemoveMaterial, 0 ,L'🗴', 0);
  1774. laCreateOperatorType("M_new_material_slot", "New Material Slot", "Create a new material slot", OPCHK_NewMaterialSlot, 0, 0, OPINV_NewMaterialSlot, 0, '+', 0);
  1775. laCreateOperatorType("M_remove_material_slot", "Remove Material Slot", "Remove a material slot", OPCHK_RemoveMaterialSlot, 0, 0, OPINV_RemoveMaterialSlot, 0 ,L'🗴', 0);
  1776. laCreateOperatorType("M_assign_material_slot", "Assign Material Slot", "Assign faces to a material slot", OPCHK_AssignMaterialSlot, 0, 0, OPINV_AssignMaterialSlot, 0 ,L'🖌', 0);
  1777. laCreateOperatorType("M_refresh_material_shader", "Refresh Material Shader", "Refresh material shader", OPCHK_RefreshMaterialShader, 0, 0, OPINV_RefreshMaterialShader, 0 ,L'🗘', 0);
  1778. laCreateOperatorType("M_set_cursor", "Set Cursor", "Set cursor in the viewport", OPCHK_ViewportAndSceneExists, 0, 0, OPINV_SetCursor, 0, 0, LA_EXTRA_TO_PANEL);
  1779. laCreateOperatorType("M_toggle_edit_mode", "Toggle Edit Mode", "Toggle edit mode of the active object", OPCHK_ThereIsActiveObject, 0, 0, OPINV_ToggleEdit, 0, 0, 0);
  1780. laCreateOperatorType("M_select", "Select", "Select things in the viewport", OPCHK_ViewportAndSceneExists, 0, 0, OPINV_Select, OPMOD_Select, 0, LA_EXTRA_TO_PANEL);
  1781. laCreateOperatorType("M_grab", "Grab", "Grab things and move around", OPCHK_ViewportAndSceneExists, 0, 0, OPINV_Grab, OPMOD_Transformation, 0, LA_EXTRA_TO_PANEL);
  1782. laCreateOperatorType("M_scale", "Scale", "Scale selected things", OPCHK_ViewportAndSceneExists, 0, 0, OPINV_Scale, OPMOD_Transformation, 0, LA_EXTRA_TO_PANEL);
  1783. laCreateOperatorType("M_rotate", "Rotate", "Rotation selected things", OPCHK_ViewportAndSceneExists, 0, 0, OPINV_Rotate, OPMOD_Transformation, 0, LA_EXTRA_TO_PANEL);
  1784. at=laCreateOperatorType("M_clear_transformations", "Clear Transformations", "Clear object transformations", 0, 0, 0, OPINV_ClearTransformation, OPMOD_FinishOnData, 0, 0);
  1785. at->UiDefine=laui_ClearTransformation;
  1786. at=laCreateOperatorType("M_make_parent", "Make Parent", "Parent objects to active objects or unparent selected ones", 0, 0, 0, OPINV_MakeParent, OPMOD_FinishOnData, 0, 0);
  1787. at->UiDefine = laui_MakeParent;
  1788. at=laCreateOperatorType("M_unparent", "Unparent", "Unparent selected objects", 0, 0, 0, OPINV_MakeParent, OPMOD_FinishOnData, 0, 0);
  1789. at->UiDefine = laui_Unparent;
  1790. laCreateOperatorType("M_extrude", "Extrude", "Extrude parts of the mesh", 0, 0, 0, OPINV_Extrude, OPMOD_Transformation, 0, 0);
  1791. at=laCreateOperatorType("M_delete", "Delete", "Delete parts of the mesh", 0, 0, 0, OPINV_Delete, OPMOD_FinishOnData, 0, 0);
  1792. at->UiDefine=laui_Delete;
  1793. laCreateOperatorType("M_make", "Make", "Make mesh primitive from selected ones", 0, 0, 0, OPINV_Make, 0, 0, 0);
  1794. laCreateOperatorType("M_subdiv", "Subdiv", "Subdivide edges", 0, 0, 0, OPINV_Subdiv, 0, 0, 0);
  1795. at=laCreateOperatorType("M_add", "Add", "Add mesh or primitives", 0, 0, 0, OPINV_Add, OPMOD_FinishOnData, 0, 0);
  1796. at->UiDefine=laui_Add; pc = laDefineOperatorProps(at, 2);
  1797. p=laAddEnumProperty(pc,"context","Context","Context of adding",0,0,0,0,0,offsetof(laObjectAddData,Context),0,0,0,0,0,0,0,0,0,0);
  1798. laAddEnumItemAs(p,"OBJECT","Object","Object context",LA_ADD_CTX_OBJECT,0);
  1799. laAddEnumItemAs(p,"MESH","Mesh","Mesh context",LA_ADD_CTX_MESH,0);
  1800. laAddEnumItemAs(p,"SHAPE","Shape","Shape context",LA_ADD_CTX_SHAPE,0);
  1801. laCreateOperatorType("M_separate", "Separate", "Separate mesh parts", 0, 0, 0, OPINV_Separate, 0, 0, 0);
  1802. laCreateOperatorType("M_combine", "Combine", "Combine mesh objects", 0, 0, 0, OPINV_Combine, 0, 0, 0);
  1803. laCreateOperatorType("M_duplicate", "Duplicate", "Duplicate objects", 0, 0, 0, OPINV_Duplicate, OPMOD_Transformation, 0, 0);
  1804. laCreateOperatorType("M_recalculate_normals", "Recalculate Normals", "Recalculate normals", 0, 0, 0, OPINV_RecalculateNormals, 0, 0, 0);
  1805. laCreateOperatorType("M_knife", "Knife", "Cut through edges", OPCHK_ViewportAndSceneExists, 0, 0, OPINV_Knife, OPMOD_Knife, 0, LA_EXTRA_TO_PANEL);
  1806. at=laCreateOperatorType("M_merge", "Merge", "Merge vertices", OPCHK_ViewportAndSceneExists, 0, 0, OPINV_Merge, OPMOD_FinishOnData, 0, LA_EXTRA_TO_PANEL);
  1807. at->UiDefine=laui_Merge;
  1808. }