*/}}

la_modelling.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494
  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. extern LA MAIN;
  20. extern struct _tnsMain *T;
  21. void la_ReadGLocation(tnsOffscreen* off, int x, int y,float* xyz0){
  22. glBindFramebuffer(GL_READ_FRAMEBUFFER, off->FboHandle);
  23. glReadBuffer(GL_COLOR_ATTACHMENT2); glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  24. glReadPixels(x,y,1,1, GL_RGBA, GL_FLOAT, xyz0);
  25. }
  26. int OPINV_SetCursor(laOperator *a, laEvent *e){
  27. if(!a->This || !a->This->EndInstance){ return 0; }
  28. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  29. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root || root->Type!=TNS_OBJECT_ROOT) return 0;
  30. float pos[4]; la_ReadGLocation(ex->OffScr, e->x-ui->L,ui->B-e->y,pos);
  31. if(pos[0]>-1e20){
  32. tnsVectorSet3v(root->GLocation,pos); laNotifyUsers("tns.world"); return LA_FINISHED_PASS;
  33. }else
  34. {
  35. if(!ex->ViewingCamera){ return LA_FINISHED_PASS; }
  36. real vv[4], gp[4]; tnsMatrix44d proj; tnsMatrix44d inv;
  37. real focus=ex->ViewingCamera->FocusDistance, _near=ex->ViewingCamera->ZMin, _far=ex->ViewingCamera->ZMax;
  38. tnsGetCameraViewProjection(proj,ui->R-ui->L,ui->B-ui->U,ex->ViewingCamera); tnsInverse44d(inv,proj);
  39. vv[0]=(real)(e->x-ui->L)/(real)(ui->R-ui->L)*2-1; vv[0]*=focus;
  40. vv[1]=(real)(ui->B-e->y)/(real)(ui->B-ui->U)*2-1; vv[1]*=focus;
  41. vv[2]=tnsGetRatiod(1/_near,1/_far,1/focus)*focus;
  42. vv[3]=focus;
  43. tnsApplyTransform44dTrue(gp, inv, vv);
  44. tnsVectorMultiSelf3d(gp,1/gp[3]);
  45. real vv0[3]={0,0,-1}, vv1[3], ray[3], p0[3]; real t; tnsMatrix44d inv2;
  46. tnsSelfTransformValueChanged(ex->ViewingCamera);tnsInverse44d(inv2,ex->ViewingCamera->Base.GlobalTransform);
  47. tnsApplyRotation43d(vv1,inv2,vv0);
  48. tnsVectorMulti3d(vv0,vv1,focus);
  49. tnsVectorAccum3d(vv0,ex->ViewingCamera->Base.GLocation);
  50. tnsVectorMinus3d(ray, gp, ex->ViewingCamera->Base.GLocation);
  51. tnsNormalizeSelf3d(ray);
  52. if(tnsIntersectPlaneRay(vv1,vv0,ex->ViewingCamera->Base.GLocation,ray,&t)){
  53. tnsVectorMultiSelf3d(ray,t); tnsVectorPlus3d(gp,ray,ex->ViewingCamera->Base.GLocation);
  54. }
  55. tnsVectorSet3v(root->GLocation,gp); laNotifyUsers("tns.world"); return LA_FINISHED_PASS;
  56. }
  57. return LA_FINISHED_PASS;
  58. }
  59. int OPCHK_ThereIsActiveObject(laPropPack *This, laStringSplitor *ss){
  60. if(!This || !This->EndInstance){ return 0; }
  61. laCanvasExtra* ex=This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  62. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  63. tnsMeshObject* mo=root->Active; if(!mo) return 0; return 1;
  64. }
  65. int OPINV_ToggleEdit(laOperator *a, laEvent *e){
  66. if(!a->This || !a->This->EndInstance){ return 0; }
  67. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  68. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  69. tnsMeshObject* mo=root->Active; if(!mo) return 0;
  70. if(mo->Base.Type!=TNS_OBJECT_MESH) return LA_CANCELED;
  71. if(mo->Mode==TNS_MESH_EDIT_MODE) tnsMeshLeaveEditMode(mo); else tnsMeshEnterEditMode(mo);
  72. laRecordInstanceDifferences(mo, "tns_mesh_object"); laPushDifferences("Toggle edit mode", TNS_HINT_GEOMETRY);
  73. laNotifyUsers("tns.world");
  74. return LA_FINISHED_PASS;
  75. }
  76. STRUCTURE(MSelectData){
  77. tnsOffscreen* FBO;
  78. tnsTexture* Color;
  79. void** RefsV; int nextV,maxV;
  80. void** RefsE; int nextE,maxE;
  81. };
  82. MSelectData* la_InitSelectData(int w, int h, tnsCamera* camera){
  83. MSelectData* sd=memAcquireSimple(sizeof(MSelectData));
  84. if (!sd->FBO || sd->FBO->pColor[0]->Height != h || sd->FBO->pColor[0]->Width != w){
  85. if (sd->FBO) tnsDelete2DOffscreen(sd->FBO);
  86. sd->FBO = tnsCreate2DOffscreen(GL_RGB, w, h, 0, 1, 0); sd->Color=sd->FBO->pColor[0];
  87. }
  88. tnsDrawToOffscreen(sd->FBO, 1, 0);
  89. return sd;
  90. }
  91. void la_AssignObjectSelectIDRecursive(tnsObject* root, MSelectData* sd){
  92. for(laListItemPointer*lip=root->ChildObjects.pFirst;lip;lip=lip->pNext){
  93. tnsObject* o=lip->p; if(!o) continue;
  94. arrEnsureLength(&sd->RefsV, sd->nextV, &sd->maxV, sizeof(tnsObject*));
  95. sd->RefsV[sd->nextV]=o; o->SelectID=sd->nextV; sd->nextV++;
  96. if(o->ChildObjects.pFirst){ la_AssignObjectSelectIDRecursive(o,sd); }
  97. }
  98. }
  99. void la_PopulateSelectDataObjects(MSelectData* sd, tnsObject* root, tnsCamera* camera){
  100. arrEnsureLength(&sd->RefsV,0,&sd->maxV,sizeof(tnsObject*));
  101. sd->nextV++; // starting from 1;
  102. la_AssignObjectSelectIDRecursive(root, sd);
  103. if(sd->nextV==1) return; int w=sd->Color->Width, h=sd->Color->Height;
  104. tnsUnbindTexture(); tnsUniformUseTexture(T->immShader,0,0); tnsUseMultiplyColor(0);
  105. tnsEnableShaderv(T->SelectionShader);
  106. glDisableVertexAttribArray(T->SelectionShader->iColor); glVertexAttrib4f(T->SelectionShader->iColor,0,0,0,0);
  107. tnsViewportWithScissor(0,0,w,h);tnsResetViewMatrix();tnsResetModelMatrix();tnsResetProjectionMatrix();
  108. tnsApplyCameraView(w,h,camera);
  109. glClearColor(0,0,0,0); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  110. glEnable(GL_DEPTH_TEST);
  111. tnsDrawObjectTree(root,0,0,0);
  112. glDisable(GL_DEPTH_TEST);
  113. }
  114. void la_PopulateSelectVerts(MSelectData* sd, tnsMeshObject* mo){
  115. arrEnsureLength(&sd->RefsV,0,&sd->maxV,sizeof(tnsMVert*));
  116. if(mo->Base.Type!=TNS_OBJECT_MESH||!mo->mv.pFirst){ return; }
  117. for(tnsMVert* v=mo->mv.pFirst;v;v=v->Item.pNext){
  118. arrEnsureLength(&sd->RefsV, v->i, &sd->maxV, sizeof(tnsObject*));
  119. sd->RefsV[v->i]=v; sd->nextV=TNS_MAX2(v->i, sd->nextV);
  120. }
  121. sd->nextV++;
  122. }
  123. void la_PopulateSelectEdges(MSelectData* sd, tnsMeshObject* mo){
  124. arrEnsureLength(&sd->RefsE,0,&sd->maxE,sizeof(tnsMEdge*));
  125. if(mo->Base.Type!=TNS_OBJECT_MESH||!mo->mv.pFirst){ return; }
  126. for(tnsMEdge* e=mo->me.pFirst;e;e=e->Item.pNext){
  127. arrEnsureLength(&sd->RefsE, e->i, &sd->maxE, sizeof(tnsObject*));
  128. sd->RefsE[e->i]=e; sd->nextE=TNS_MAX2(e->i, sd->nextE);
  129. }
  130. sd->nextE++;
  131. }
  132. void la_PopulateSelectDataPrimitives(MSelectData* sd, tnsMeshObject* mo, tnsCamera* camera, int WhatPrim){
  133. int DoVerts=(WhatPrim==LA_CANVAS_SELECT_MODE_VERTS),DoEdges=(WhatPrim==LA_CANVAS_SELECT_MODE_EDGES);
  134. int Knife=(WhatPrim==LA_CANVAS_SELECT_MODE_KNIFE);
  135. if(DoVerts || Knife){ la_PopulateSelectVerts(sd,mo); }
  136. if(DoEdges || Knife){ la_PopulateSelectEdges(sd,mo); }
  137. int w=sd->Color->Width, h=sd->Color->Height;
  138. tnsUnbindTexture(); tnsUniformUseTexture(T->immShader,0,0); tnsUseMultiplyColor(0);
  139. tnsEnableShaderv(T->SelectionShader);
  140. tnsViewportWithScissor(0,0,w,h);tnsResetViewMatrix();tnsResetModelMatrix();tnsResetProjectionMatrix();
  141. tnsApplyCameraView(w,h,camera);
  142. glClearColor(0,0,0,0); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  143. glEnable(GL_DEPTH_TEST);
  144. tnsPushMatrix(); tnsApplyObjectMatrix(mo);
  145. if(Knife){ glPointSize(5); }
  146. if(DoEdges || Knife){ tnsDrawBatch(mo->Batch, "edges_select",0,0); }
  147. if(DoVerts || Knife){ tnsDrawBatch(mo->Batch, "verts_select",0,0); }
  148. if(Knife){ glPointSize(1); }
  149. tnsPopMatrix();
  150. glDisable(GL_DEPTH_TEST);
  151. }
  152. void la_PadSelectionBuffer(uint8_t* buf, int w, int h, int sx, int sy, int ex, int ey, int real_endx){
  153. if(!sx&&!sy&&!ex&&!ey) return;
  154. uint8_t* pad=calloc(1,sizeof(uint8_t)*4*w*h);
  155. for(int i=0;i<h;i++){
  156. if(i<sy || i>=h-ey){ memset(&pad[i*w*4],0,w*4); continue; }
  157. for(int j=0;j<w;j++){
  158. if(j<sx || j>=w-sx){ memset(&pad[(i*w+j)*4],0,4); continue; }
  159. memcpy(&pad[(i*w+j)*4], &buf[((i-sy)*(real_endx-sx)+j-sx)*4], 4);
  160. }
  161. }
  162. memcpy(buf,pad,sizeof(uint8_t)*4*w*h);
  163. free(pad);
  164. }
  165. uint8_t* la_ReadSelection(MSelectData* sd, u8bit* buf, int x, int y, int w, int h){
  166. glFlush(); glFinish(); glGetError();
  167. glBindFramebuffer(GL_READ_FRAMEBUFFER, sd->FBO->FboHandle);
  168. glReadBuffer(GL_COLOR_ATTACHMENT0);
  169. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  170. glReadPixels(x,y,w,h, GL_RGBA, GL_UNSIGNED_BYTE, buf);
  171. return buf;
  172. }
  173. uint8_t* la_ReadSelectionRadius(MSelectData* sd, int uix, int uiy, int radius){
  174. int w=radius*2; int vh=sd->Color->Height,vw=sd->Color->Width;
  175. uint8_t* buf=calloc(1,sizeof(uint8_t)*4*w*w);
  176. int startx=uix-radius, starty=vh-uiy-radius, endx=uix+radius, endy=vh-uiy+radius;
  177. TNS_CLAMP(startx,0,vw);TNS_CLAMP(endx,0,vw);TNS_CLAMP(starty,0,vh);TNS_CLAMP(endy,0,vh);
  178. int _startx=TNS_MAX2(startx,0), _starty=TNS_MAX2(starty,0), _endx=TNS_MIN2(endx,vw), _endy=TNS_MIN2(endy,vh);
  179. la_ReadSelection(sd, buf, _startx, _starty, _endx-_startx, _endy-_starty);
  180. la_PadSelectionBuffer(buf, w, w, _startx-startx, _starty-starty, endx-_endx, endy-_endy,_endx);
  181. return buf;
  182. }
  183. uint8_t* la_ReadSelectionBox(MSelectData* sd, int uix, int uiy, int uix2, int uiy2){
  184. int vh=sd->Color->Height,vw=sd->Color->Width;
  185. if(uix2<uix) LA_SWAP(int,uix,uix2); if(uiy2<uiy) LA_SWAP(int,uiy,uiy2);
  186. TNS_CLAMP(uix,0,vw);TNS_CLAMP(uix2,0,vw);TNS_CLAMP(uiy,0,vh);TNS_CLAMP(uiy2,0,vh);
  187. int w=uix2-uix, h=uiy2-uiy;
  188. uint8_t* buf=calloc(1,sizeof(uint8_t)*4*w*h);
  189. int startx=uix, starty=vh-uiy2, endx=uix+w, endy=starty+h;
  190. int _startx=TNS_MAX2(startx,0), _starty=TNS_MAX2(starty,0), _endx=TNS_MIN2(endx,vw), _endy=TNS_MIN2(endy,vh);
  191. la_ReadSelection(sd, buf, _startx, _starty, _endx-_startx, _endy-_starty);
  192. la_PadSelectionBuffer(buf, w, h, _startx-startx, _starty-starty, endx-_endx, endy-_endy,_endx);
  193. return buf;
  194. }
  195. int la_SelectGetClosest(MSelectData* sd, int uix, int uiy, int radius, int *ElemType){
  196. *ElemType=0; uint8_t* buf=la_ReadSelectionRadius(sd, uix, uiy, radius); if(!buf) return 0;
  197. int w=radius*2; int MinD=INT_MAX; int MinID=0, d, elemtype=0;
  198. for(int i=0;i<w;i++){
  199. for(int j=0;j<w;j++){
  200. uint8_t* p=&buf[(i*w+j)*4]; int id=(p[0])|(p[1]<<8)|((p[2]<<16)&(~TNS_MMESH_TYPE_BIT));
  201. if(id && (d=tnsDistIdv2(i,j, radius, radius))<MinD ){ MinD=d; MinID=id; elemtype=((p[2]<<16)&TNS_MMESH_TYPE_BIT); }
  202. //printf("%d ",buf[(i*w+j)*4]);
  203. }
  204. //printf("\n");
  205. }
  206. free(buf);
  207. *ElemType=elemtype;
  208. return MinID;
  209. }
  210. int* la_SelectGetBox(MSelectData* sd, int uix, int uiy, int uix2, int uiy2, int* r_length){
  211. uint8_t* buf=la_ReadSelectionBox(sd, uix, uiy, uix2, uiy2); if(!buf) return 0;
  212. int vh=sd->Color->Height,vw=sd->Color->Width;
  213. if(uix2<uix) LA_SWAP(int,uix,uix2); if(uiy2<uiy) LA_SWAP(int,uiy,uiy2);
  214. TNS_CLAMP(uix,0,vw);TNS_CLAMP(uix2,0,vw);TNS_CLAMP(uiy,0,vh);TNS_CLAMP(uiy2,0,vh);
  215. int w=uix2-uix, h=uiy2-uiy;
  216. int* ids=0; int next=0,max=0;
  217. arrEnsureLength(&ids, next, &max, sizeof(int));
  218. for(int i=0;i<h;i++){
  219. for(int j=0;j<w;j++){
  220. uint8_t* p=&buf[(i*w+j)*4]; int id=(p[0])|(p[1]<<8)|((p[2]<<16)&(~TNS_MMESH_TYPE_BIT));
  221. if(id){ int found=0;
  222. for(int a=0;a<next;a++){ if(ids[a]==id){ found=1; break; } }
  223. if(!found){
  224. arrEnsureLength(&ids, next, &max, sizeof(int)); ids[next]=id; next++; }
  225. }
  226. }
  227. }
  228. free(buf);
  229. *r_length=next;
  230. return ids;
  231. }
  232. void la_FreeSelectData(MSelectData* sd){
  233. tnsDelete2DOffscreen(sd->FBO);
  234. free(sd->RefsV);
  235. free(sd->RefsE);
  236. memFree(sd);
  237. }
  238. void* la_SelectGetRef(MSelectData* sd, int id, int elemtype){
  239. if(!elemtype){ if(id>=0 && id<sd->nextV){ return sd->RefsV[id]; } }
  240. elif(elemtype==TNS_MMESH_EDGE_BIT){ if(id>=0 && id<sd->nextE){ return sd->RefsE[id]; } }
  241. return 0;
  242. }
  243. int OPCHK_ViewportAndSceneExists(laPropPack *This, laStringSplitor *ss){
  244. if(!This || !This->EndInstance){ return 0; } laCanvasExtra* ex=This->EndInstance;
  245. laUiItem* ui=ex->ParentUi; tnsObject* root=ui?ui->PP.EndInstance:0;
  246. if(!ex->ViewingCamera || !root){ return 0; }
  247. return 1;
  248. }
  249. void la_DoObjectSelect(tnsObject* root, tnsObject* o, laCanvasExtra* e, int DeselectAll, int Select, int Toggle){
  250. if(DeselectAll){ tnsDeselectAllObjects(root); memAssignRef(root,&root->Active,0); }
  251. if(o){ tnsSelectObject(o, Select, o==root->Active?0:Toggle); memAssignRef(root,&root->Active,o); }
  252. }
  253. void la_DoMeshSelect(tnsMeshObject* mo, void* p, int WhatPrim, int DeselectAll, int Select, int Toggle){
  254. if(DeselectAll){ tnsMMeshDeselectAll(mo); }
  255. if(p){ if(WhatPrim==LA_CANVAS_SELECT_MODE_VERTS) tnsMMeshSelectVert(mo,p,Select,Toggle);
  256. elif(WhatPrim==LA_CANVAS_SELECT_MODE_EDGES) tnsMMeshSelectEdge(mo,p,Select,Toggle); }
  257. }
  258. #define LA_SELECT_MODE_BOX 1
  259. STRUCTURE(MSelectExtra){
  260. MSelectData* sd;
  261. tnsObject* root;
  262. tnsObject* mo;
  263. tnsCamera* cam;
  264. int Mode;
  265. int InSelect;
  266. laListHandle KnifeElements;
  267. void* PendingElem; int PendingElemType, BatchDirty;
  268. int IsLoop;
  269. };
  270. int OPINV_Select(laOperator *a, laEvent *e){
  271. if(!a->This || !a->This->EndInstance){ return 0; }
  272. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  273. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  274. tnsMeshObject* mo=root->Active;
  275. int is_geo=0, SelectMode=ex->SelectMode, ring_band=0;
  276. if(strSame(strGetArgumentString(a->ExtraInstructionsP, "mode"), "toggle")){
  277. if(mo && mo->Base.Type==TNS_OBJECT_MESH && mo->Mode==TNS_MESH_EDIT_MODE){
  278. if(tnsMMeshAnySelected(mo)) tnsMMeshDeselectAll(mo); else tnsMMeshSelectAll(mo);
  279. tnsInvalidateMeshBatch(mo); is_geo=1;
  280. }else{
  281. if(tnsAnyObjectsSelected(root)) tnsDeselectAllObjects(root); else tnsSelectAllObjects(root);
  282. }
  283. laNotifyUsers("tns.world"); laRecordAndPush(0,"tns.world", "Toggle selection",is_geo?TNS_HINT_GEOMETRY:TNS_HINT_TRANSFORM);
  284. return LA_FINISHED;
  285. }
  286. int spk=e->SpecialKeyBit & (LA_KEY_CTRL|LA_KEY_ALT);
  287. if(spk==LA_KEY_ALT){ ring_band=1; SelectMode=LA_CANVAS_SELECT_MODE_EDGES; }
  288. elif(spk==(LA_KEY_CTRL|LA_KEY_ALT)){ ring_band=2; SelectMode=LA_CANVAS_SELECT_MODE_EDGES; }
  289. MSelectData* sd=la_InitSelectData(ex->OffScr->pColor[0]->Width, ex->OffScr->pColor[0]->Height, c);
  290. int DeselectAll=1;
  291. int Append=e->SpecialKeyBit&LA_KEY_SHIFT; if(Append) DeselectAll=0;
  292. if(mo && mo->Base.Type==TNS_OBJECT_MESH && mo->Mode==TNS_MESH_EDIT_MODE){
  293. la_PopulateSelectDataPrimitives(sd, mo, ex->ViewingCamera, SelectMode);
  294. if(strSame(strGetArgumentString(a->ExtraInstructionsP, "mode"), "box")){
  295. MSelectExtra* se=memAcquire(sizeof(MSelectExtra));
  296. ex->OnX=e->x; ex->OnX=e->y;
  297. 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;
  298. }
  299. int elemtype,id=la_SelectGetClosest(sd, e->x-ui->L, e->y-ui->U, LA_RH,&elemtype)-1;
  300. void* p=la_SelectGetRef(sd,id,elemtype);
  301. la_DoMeshSelect(mo, p, SelectMode, DeselectAll, 1, 1); tnsMMeshEnsureSelection(mo,ex->SelectMode);
  302. if(ring_band && p){
  303. tnsMMeshSelectRingBandFrom(mo,p,ring_band,1,Append); tnsMMeshEnsureSelection(mo,ex->SelectMode);
  304. }
  305. tnsInvalidateMeshBatch(mo);
  306. laNotifyUsers("tns.world"); laRecordAndPush(0,"tns.world","Mesh selection",TNS_HINT_GEOMETRY);
  307. }else{
  308. la_PopulateSelectDataObjects(sd,root,ex->ViewingCamera);
  309. if(strSame(strGetArgumentString(a->ExtraInstructionsP, "mode"), "box")){
  310. MSelectExtra* se=memAcquire(sizeof(MSelectExtra));
  311. ex->OnX=e->x; ex->OnX=e->y;
  312. a->CustomData=se; se->sd=sd; se->Mode=LA_SELECT_MODE_BOX; se->cam=c; ex->DrawCursor=1; se->root=root; return LA_RUNNING;
  313. }
  314. int elemtype,id=la_SelectGetClosest(sd, e->x-ui->L, e->y-ui->U, LA_RH*2,&elemtype);
  315. void* p=la_SelectGetRef(sd,id,elemtype); if(p){ la_DoObjectSelect(root, p, ex, DeselectAll, 1, 1); }
  316. else{ la_DoObjectSelect(root, 0, ex, DeselectAll, 1, 1); }
  317. laNotifyUsers("tns.world"); laRecordAndPush(0,"tns.world","Object selection",TNS_HINT_TRANSFORM);
  318. }
  319. la_FreeSelectData(sd);
  320. return LA_FINISHED_PASS;
  321. }
  322. int OPMOD_Select(laOperator *a, laEvent *e){
  323. if(!a->This || !a->This->EndInstance){ return 0; }
  324. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  325. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  326. tnsMeshObject* mo=root->Active;
  327. MSelectExtra* se=a->CustomData;
  328. if(e->Type==LA_L_MOUSE_DOWN){ se->InSelect=1; ex->DrawCursor=2; ex->ClickedX=e->x; ex->ClickedY=e->y; laRedrawCurrentPanel(); }
  329. if(e->Type&LA_MOUSE_EVENT){ ex->OnX=e->x; ex->OnY=e->y; laRedrawCurrentPanel(); }
  330. if(e->Type==LA_R_MOUSE_DOWN || (e->Type == LA_KEY_DOWN && e->key==LA_KEY_ESCAPE)){
  331. ex->DrawCursor=0; la_FreeSelectData(se->sd); memFree(se); laNotifyUsers("tns.world"); return LA_FINISHED;
  332. }
  333. int DeselectAll=1;
  334. int Append=e->SpecialKeyBit&LA_KEY_SHIFT; if(Append) DeselectAll=0;
  335. int Remove=e->SpecialKeyBit&LA_KEY_ALT; if(Remove) DeselectAll=0;
  336. int is_geo=0;
  337. if(se->InSelect && e->Type==LA_L_MOUSE_UP){
  338. if(mo && mo->Base.Type==TNS_OBJECT_MESH && mo->Mode==TNS_MESH_EDIT_MODE){
  339. la_DoMeshSelect(mo, 0, ex->SelectMode, DeselectAll, 0, 0);
  340. 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);
  341. for(int i=0;i<len;i++){
  342. int id=ids[i]-1; void* p=la_SelectGetRef(se->sd,id,ex->SelectMode==LA_CANVAS_SELECT_MODE_EDGES?TNS_MMESH_EDGE_BIT:0);
  343. la_DoMeshSelect(mo, p, ex->SelectMode, 0, !Remove, 0);
  344. }
  345. tnsMMeshEnsureSelection(mo,ex->SelectMode);
  346. tnsInvalidateMeshBatch(mo); is_geo=1;
  347. }else{
  348. la_DoObjectSelect(se->root, 0, ex, DeselectAll, 0, 0);
  349. int len; int* ids=la_SelectGetBox(se->sd, ex->ClickedX, ex->ClickedY, e->x-ui->L, e->y-ui->U, &len);
  350. for(int i=0;i<len;i++){
  351. int id=ids[i]; void* p=la_SelectGetRef(se->sd,id,0);
  352. if(p){ la_DoObjectSelect(se->root, p, ex, 0, !Remove, 0); }
  353. }
  354. }
  355. laNotifyUsers("tns.world"); laRecordAndPush(0,"tns.world","Box selection",is_geo?TNS_HINT_GEOMETRY:TNS_HINT_TRANSFORM);
  356. ex->DrawCursor=0;
  357. la_FreeSelectData(se->sd); memFree(se);
  358. laRedrawCurrentPanel();
  359. return LA_FINISHED;
  360. }
  361. return LA_RUNNING;
  362. }
  363. #define LA_TRANSFORM_MODE_GRAB 1
  364. #define LA_TRANSFORM_MODE_ROTATE 2
  365. #define LA_TRANSFORM_MODE_SCALE 3
  366. STRUCTURE(MTOrigObject){
  367. tnsObject* o;
  368. tnsMatrix44d Global;
  369. tnsMatrix44d Local;
  370. int Discard;
  371. };
  372. STRUCTURE(MTOrigMVert){
  373. tnsVector3d p;
  374. tnsVector3d origp;
  375. tnsMVert* mv;
  376. };
  377. STRUCTURE(MTransformData){
  378. tnsMatrix44d Delta;
  379. tnsMatrix44d ViewProjection;
  380. tnsVector4d Up,Right,Foward;
  381. tnsVector4d TCenter;
  382. int CenterX,CenterY; real Initial;
  383. tnsObject* mo; tnsMatrix44d obmatinv;
  384. tnsCamera* c; tnsObject* root;
  385. int w,h;
  386. void* Originals; int next,max;
  387. int mode;
  388. int LockAxis[3];
  389. int UseLocal;
  390. real DeltaVal, UserDeltaVal;
  391. laStringEdit* Entry; int UseUserDelta;
  392. };
  393. MTransformData* la_InitTransformData(int w, int h, tnsCamera* c){
  394. MTransformData* td=memAcquireSimple(sizeof(MTransformData));
  395. tnsVector4d pu={0,1,0,0}, pr={1,0,0,0}, pf={0,0,1};
  396. tnsGetCameraMovingDeltas(c,w,h,1,0,pr); pr[2]=0; pr[3]=0;
  397. tnsGetCameraMovingDeltas(c,w,h,0,-1,pu); pu[2]=0; pu[3]=0;
  398. tnsMatrix44d inv; tnsInverse44d(inv,c->Base.GlobalTransform);
  399. tnsApplyRotation43d(td->Up,inv,pu);
  400. tnsApplyRotation43d(td->Right,inv,pr);
  401. tnsApplyRotation43d(td->Foward,inv,pf);
  402. tnsGetCameraViewProjection(td->ViewProjection, w,h,c);
  403. td->c=c; td->w=w; td->h=h;
  404. strBeginEdit(&td->Entry, "");
  405. return td;
  406. }
  407. void la_GetTransformInitialScale(MTransformData* td, laUiItem* ui, int x, int y){ td->Initial=tnsDistIdv2(x-ui->L,y-ui->U,td->CenterX,td->CenterY); }
  408. void la_GetTransformInitialRotation(MTransformData* td, laUiItem* ui, int x, int y){ td->Initial=atan2(y-ui->U-td->CenterY,x-ui->L-td->CenterX); }
  409. void la_GetTransformCenter2D(MTransformData* td){
  410. tnsVector4d vp; tnsApplyTransform44d(vp,td->ViewProjection,td->TCenter);
  411. if(td->c->CameraType==TNS_PRESPECTIVE_CAMERA){ tnsVectorMultiSelf3d(vp, 1/vp[3]); }
  412. td->CenterX = (vp[0]/2+0.5f)*td->w; td->CenterY=(-vp[1]/2+0.5f)*td->h;
  413. if(td->CenterX<0||td->CenterY<0) { td->CenterX=td->w/2; td->CenterY=td->h/2; }
  414. }
  415. int la_AddTransformObjectsRecursive(MTransformData* td, tnsObject*root){
  416. int any=0; for(laListItemPointer* lip=root->ChildObjects.pFirst;lip;lip=lip->pNext){
  417. tnsObject* o=lip->p; if(o->ChildObjects.pFirst) any+=la_AddTransformObjectsRecursive(td,o);
  418. if(!(o->Flags&TNS_OBJECT_FLAGS_SELECTED)) continue;
  419. arrEnsureLength(&td->Originals, td->next, &td->max, sizeof(MTOrigObject));
  420. MTOrigObject* to=arrElement(td->Originals, td->next, sizeof(MTOrigObject)); td->next++; to->o=o;
  421. memcpy(to->Global, o->GlobalTransform, sizeof(tnsMatrix44d));
  422. memcpy(to->Local, o->SelfTransform, sizeof(tnsMatrix44d));
  423. tnsVectorAccum3d(td->TCenter,o->GLocation); any++;
  424. 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;
  425. }
  426. return any;
  427. }
  428. int la_PopulateTransformObjects(MTransformData* td, tnsObject* root){
  429. arrEnsureLength(&td->Originals, 0, &td->max, sizeof(MTOrigObject));
  430. int any=la_AddTransformObjectsRecursive(td,root);
  431. tnsVectorMultiSelf3d(td->TCenter, 1.0f/any);
  432. la_GetTransformCenter2D(td);
  433. return any;
  434. }
  435. int la_PopulateTransformVerticies(MTransformData* td, tnsMeshObject* mo){
  436. int any=0; td->mo=mo;
  437. arrEnsureLength(&td->Originals, 0, &td->max, sizeof(MTOrigMVert));
  438. tnsInverse44d(td->obmatinv, mo->Base.GlobalTransform);
  439. for(tnsMVert* mv=mo->mv.pFirst;mv;mv=mv->Item.pNext){
  440. if(!(mv->flags&TNS_MESH_FLAG_SELECTED)) continue;
  441. //printf("v %d ",mv->i);
  442. arrEnsureLength(&td->Originals, td->next, &td->max, sizeof(MTOrigMVert));
  443. MTOrigMVert* to=arrElement(td->Originals, td->next, sizeof(MTOrigMVert)); td->next++; to->mv=mv;
  444. tnsApplyTransform43d(to->p, mo->Base.GlobalTransform, mv->p);
  445. memcpy(to->origp, mv->p, sizeof(tnsVector3d)); any++;
  446. tnsVectorAccum3d(td->TCenter,to->p);
  447. }
  448. //printf(" [totmv %d]\n",mo->totmv);
  449. tnsVectorMultiSelf3d(td->TCenter, 1.0f/any);
  450. la_GetTransformCenter2D(td);
  451. return any;
  452. }
  453. void la_ApplyTranslation(MTransformData* td, int x, int y){
  454. tnsMatrix44d trans; tnsVector3d deltay,delta; tnsVector3d gp;
  455. tnsVectorMulti3d(delta, td->Right, x); tnsVectorMulti3d(deltay, td->Up, y); tnsVectorAccum3d(delta, deltay);
  456. tnsVector3d use_delta={LA_COLOR3(delta)}; real len;
  457. if(td->LockAxis[0]||td->LockAxis[1]||td->LockAxis[2]){ len=tnsLength3d(delta); }
  458. 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; }
  459. 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; }
  460. 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; }
  461. if(td->LockAxis[0]<0){ use_delta[0]=0; real l=tnsLength3d(use_delta); tnsVectorMultiSelf3d(use_delta, l?len/l*len/l:0); }
  462. if(td->LockAxis[1]<0){ use_delta[1]=0; real l=tnsLength3d(use_delta); tnsVectorMultiSelf3d(use_delta, l?len/l*len/l:0); }
  463. if(td->LockAxis[2]<0){ use_delta[2]=0; real l=tnsLength3d(use_delta); tnsVectorMultiSelf3d(use_delta, l?len/l*len/l:0); }
  464. td->DeltaVal=tnsLength3d(use_delta);
  465. if(td->UseUserDelta){
  466. tnsVectorMultiSelf3d(use_delta,1/tnsLength3d(use_delta)*td->UserDeltaVal);
  467. tnsVector3d lock={ td->LockAxis[0], td->LockAxis[1], td->LockAxis[2] };
  468. real dir=tnsDot3d(use_delta, lock, 0); tnsVectorMultiSelf3d(use_delta,(td->UserDeltaVal*dir<=0)?-1:1);
  469. td->DeltaVal=td->UserDeltaVal;
  470. }
  471. if(!td->mo){
  472. for(int i=0;i<td->next;i++){
  473. MTOrigObject* to=arrElement(td->Originals, i, sizeof(MTOrigObject)); memcpy(to->o->GlobalTransform, to->Global,sizeof(tnsMatrix44d));
  474. if(to->Discard){ tnsSelfMatrixChanged(to->o,1); continue; }
  475. tnsGlobalMatrixChanged(to->o, 0);
  476. if(td->UseLocal) tnsMoveObjectLocal(to->o, LA_COLOR3(use_delta)); else tnsMoveObjectGlobal(to->o, LA_COLOR3(use_delta));
  477. }
  478. }else{
  479. tnsMakeTranslationMatrix44d(trans, LA_COLOR3(use_delta));
  480. for(int i=0;i<td->next;i++){ MTOrigMVert* to=arrElement(td->Originals, i, sizeof(MTOrigMVert));
  481. tnsApplyTransform43d(gp, trans, to->p); if(!td->UseLocal) tnsApplyTransform43d(to->mv->p, td->obmatinv, gp); else tnsVectorCopy3d(gp, to->mv->p);
  482. }
  483. tnsInvalidateMeshBatch(td->mo); tnsMMeshCalculateNormal(td->mo);
  484. }
  485. }
  486. void la_ApplyScale(MTransformData* td, int uix, int uiy){
  487. tnsMatrix44d trans; real d=tnsDistIdv2(uix,uiy,td->CenterX,td->CenterY); if(!td->Initial){ td->Initial=100; }
  488. real s=d/td->Initial; tnsVector3d gp;
  489. td->DeltaVal=s; if(td->UseUserDelta) td->DeltaVal=s=td->UserDeltaVal;
  490. if(!td->mo){
  491. for(int i=0;i<td->next;i++){
  492. MTOrigObject* to=arrElement(td->Originals, i, sizeof(MTOrigObject)); memcpy(to->o->GlobalTransform, to->Global,sizeof(tnsMatrix44d));
  493. if(to->Discard){ tnsSelfMatrixChanged(to->o,1); continue; }
  494. tnsGlobalMatrixChanged(to->o, 0); tnsScaleObject(to->o, s, LA_COLOR3(td->TCenter));
  495. }
  496. }else{
  497. tnsMakeScaleMatrix44d(trans,s,s,s);
  498. tnsMatrix44d t1,t2,t3; tnsMakeTranslationMatrix44d(t1,LA_COLOR3(td->TCenter)); tnsInverse44d(t2,t1);
  499. tnsMatrix44d final; tnsMultiply44d(t3,t1,trans); tnsMultiply44d(final,t3,t2);
  500. for(int i=0;i<td->next;i++){ MTOrigMVert* to=arrElement(td->Originals, i, sizeof(MTOrigMVert));
  501. tnsApplyTransform43d(gp, final, to->p); tnsApplyTransform43d(to->mv->p, td->obmatinv, gp);
  502. }
  503. tnsInvalidateMeshBatch(td->mo); tnsMMeshCalculateNormal(td->mo);
  504. }
  505. }
  506. void la_ApplyRotation(MTransformData* td, int uix, int uiy){
  507. tnsMatrix44d trans; real a=atan2(uiy-td->CenterY,uix-td->CenterX);
  508. real angle=a-td->Initial; tnsVector3d gp; tnsVector3d LimFoward={0}; real* use_forward=td->Foward;
  509. if(td->LockAxis[0]||td->LockAxis[1]||td->LockAxis[2]){ use_forward=LimFoward; }
  510. if(td->LockAxis[0]){ LimFoward[0]=1; }
  511. if(td->LockAxis[1]){ LimFoward[1]=1; }
  512. if(td->LockAxis[2]){ LimFoward[2]=1; }
  513. if(td->UseUserDelta) angle=rad(td->UserDeltaVal); td->DeltaVal=deg(angle);
  514. if(!td->mo){
  515. for(int i=0;i<td->next;i++){
  516. MTOrigObject* to=arrElement(td->Originals, i, sizeof(MTOrigObject)); memcpy(to->o->GlobalTransform, to->Global,sizeof(tnsMatrix44d));
  517. if(to->Discard){ tnsSelfMatrixChanged(to->o,1); continue; }
  518. tnsGlobalMatrixChanged(to->o, 0); tnsRotateObjectGlobal(to->o,LA_COLOR3(use_forward),angle,LA_COLOR3(td->TCenter));
  519. }
  520. }else{
  521. tnsMakeRotationMatrix44d(trans, angle, LA_COLOR3(use_forward));
  522. tnsMatrix44d t1,t2,t3; tnsMakeTranslationMatrix44d(t1,LA_COLOR3(td->TCenter)); tnsInverse44d(t2,t1);
  523. tnsMatrix44d final; tnsMultiply44d(t3,t1,trans); tnsMultiply44d(final,t3,t2);
  524. for(int i=0;i<td->next;i++){ MTOrigMVert* to=arrElement(td->Originals, i, sizeof(MTOrigMVert));
  525. tnsApplyTransform43d(gp, final, to->p); tnsApplyTransform43d(to->mv->p, td->obmatinv, gp);
  526. }
  527. tnsInvalidateMeshBatch(td->mo); tnsMMeshCalculateNormal(td->mo);
  528. }
  529. }
  530. void la_CancelTransformObjects(MTransformData* td){
  531. if(!td->mo){
  532. for(int i=0;i<td->next;i++){ MTOrigObject* to=arrElement(td->Originals, i, sizeof(MTOrigObject));
  533. if(to->Discard){ tnsSelfMatrixChanged(to->o,1); continue; }
  534. memcpy(to->o->GlobalTransform, to->Global,sizeof(tnsMatrix44d));
  535. tnsGlobalMatrixChanged(to->o, 1);
  536. }
  537. }else{
  538. for(int i=0;i<td->next;i++){ MTOrigMVert* to=arrElement(td->Originals, i, sizeof(MTOrigMVert)); tnsVectorCopy3d(to->origp,to->mv->p); }
  539. tnsInvalidateMeshBatch(td->mo);
  540. }
  541. }
  542. void la_RecordTransformDifferences(MTransformData* td){
  543. if(!td->mo){
  544. for(int i=0;i<td->next;i++){ MTOrigObject* to=arrElement(td->Originals, i, sizeof(MTOrigObject));
  545. laRecordInstanceDifferences(to->o, "tns_object");
  546. } laPushDifferences(td->mode==LA_TRANSFORM_MODE_GRAB?"Moved objects":td->mode==LA_TRANSFORM_MODE_ROTATE?"Rotated objects":"Scaled objects", TNS_HINT_TRANSFORM);
  547. }else{
  548. laRecordInstanceDifferences(td->mo, "tns_mesh_object");
  549. laPushDifferences(td->mode==LA_TRANSFORM_MODE_GRAB?"Moved primitives":td->mode==LA_TRANSFORM_MODE_ROTATE?"Rotated primitives":"Scaled primitives", TNS_HINT_GEOMETRY);
  550. tnsInvalidateMeshBatch(td->mo);
  551. }
  552. }
  553. void la_FreeTransformData(MTransformData* td){
  554. free(td->Originals);
  555. strEndEdit(&td->Entry, 1);
  556. memFree(td);
  557. }
  558. void la_MakeTransformOperatorHint(laOperator* a, MTransformData* td){
  559. strSafeDestroy(&a->RuntimeHint);
  560. strSafePrint(&a->RuntimeHint, "%s ",
  561. td->mode==LA_TRANSFORM_MODE_GRAB?"Grab":td->mode==LA_TRANSFORM_MODE_ROTATE?"Rotate":td->mode==LA_TRANSFORM_MODE_SCALE?"Scale":"");
  562. char* entry=strGetEditString(td->Entry,0);
  563. strSafePrint(&a->RuntimeHint, "Delta: %.3lf [🔢 %s] 🆇🆈🆉 Lock axis: %s 🈳 %s ", td->DeltaVal, (entry&&entry[0])?entry:"Type...",
  564. td->LockAxis[0]?"X":td->LockAxis[1]?"Y":td->LockAxis[2]?"Z":"None",
  565. td->UseLocal?"Global/[Local]":"[Global]/Local");
  566. free(entry);
  567. if(td->mode==LA_TRANSFORM_MODE_GRAB){ strSafePrint(&a->RuntimeHint, "🡅🆇🆈🆉 Reverse: %s ",
  568. (td->LockAxis[0]<0||td->LockAxis[1]<0||td->LockAxis[2]<0)?"Yes":"No"); }
  569. if(td->mode!=LA_TRANSFORM_MODE_GRAB){ strSafePrint(&a->RuntimeHint,"🅶 Grab "); }
  570. if(td->mode!=LA_TRANSFORM_MODE_SCALE){ strSafePrint(&a->RuntimeHint,"🆂 Scale "); }
  571. if(td->mode!=LA_TRANSFORM_MODE_ROTATE){ strSafePrint(&a->RuntimeHint,"🆁 Rotate "); }
  572. }
  573. int la_InitTransform(laOperator* a, laEvent* e, int mode){
  574. if(!a->This || !a->This->EndInstance){ return 0; }
  575. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  576. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  577. tnsMeshObject* mo=root->Active;
  578. MTransformData* td=la_InitTransformData(ex->OffScr->pColor[0]->Width, ex->OffScr->pColor[0]->Height, c);
  579. a->CustomData = td;
  580. td->mode=mode;
  581. td->root=root;
  582. int ret=0;
  583. if(mo && mo->Base.Type==TNS_OBJECT_MESH && mo->Mode==TNS_MESH_EDIT_MODE){
  584. if(la_PopulateTransformVerticies(td, mo)){ ex->ClickedX=e->x; ex->ClickedY=e->y; ret=1; }
  585. }else{
  586. if(la_PopulateTransformObjects(td,root)){ ex->ClickedX=e->x; ex->ClickedY=e->y; ret=1; }
  587. }
  588. if(ret){
  589. if(mode==LA_TRANSFORM_MODE_SCALE){ la_GetTransformInitialScale(td,ui,e->x,e->y); ex->DrawCursor=LA_CANVAS_CURSOR_ARROW; }
  590. elif(mode==LA_TRANSFORM_MODE_ROTATE){ la_GetTransformInitialRotation(td,ui,e->x,e->y); ex->DrawCursor=LA_CANVAS_CURSOR_ARROW; }
  591. ex->TargetX=td->CenterX+ui->L; ex->TargetY=td->CenterY+ui->U; ex->OnX=e->x; ex->OnY=e->y;
  592. la_MakeTransformOperatorHint(a, td);
  593. laNotifyUsers("tns.world"); return 1;
  594. }
  595. return 0;
  596. }
  597. int OPINV_Grab(laOperator *a, laEvent *e){
  598. if(la_InitTransform(a, e, LA_TRANSFORM_MODE_GRAB)) return LA_RUNNING; return LA_FINISHED_PASS;
  599. }
  600. int OPINV_Scale(laOperator *a, laEvent *e){
  601. if(la_InitTransform(a, e, LA_TRANSFORM_MODE_SCALE)) return LA_RUNNING; return LA_FINISHED_PASS;
  602. }
  603. int OPINV_Rotate(laOperator *a, laEvent *e){
  604. if(la_InitTransform(a, e, LA_TRANSFORM_MODE_ROTATE)) return LA_RUNNING; return LA_FINISHED_PASS;
  605. }
  606. int OPMOD_Transformation(laOperator *a, laEvent *e){
  607. if(!a->This || !a->This->EndInstance){ return 0; }
  608. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  609. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  610. tnsMeshObject* mo=root->Active;
  611. MTransformData* td=a->CustomData;
  612. if (e->Input=='x'||e->Input=='y'||e->Input=='z'||e->Input=='g'||e->Input=='s'||e->Input=='r'||
  613. e->Input=='X'||e->Input=='Y'||e->Input=='Z'||e->Input==' '){ /*pass*/ }
  614. else{ la_ProcessTextEdit(e, td->Entry, 0); }
  615. char* entered;
  616. if(entered=strGetEditString(td->Entry,0)){ int status;
  617. if((status=sscanf(entered,"%lf",&td->UserDeltaVal)) && status!=EOF) td->UseUserDelta=1; else td->UseUserDelta=0;
  618. td->UserDeltaVal=fabs(td->UserDeltaVal); for(char*pc=entered;*pc;pc++){ if(*pc=='-') td->UserDeltaVal=-td->UserDeltaVal; } free(entered);
  619. }
  620. if(e->Type==LA_KEY_DOWN){
  621. int Other=1; if(e->SpecialKeyBit&LA_KEY_SHIFT){ Other=-1; }
  622. 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; }
  623. 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; }
  624. 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; }
  625. if(e->key=='g' && td->mode!=LA_TRANSFORM_MODE_GRAB){ td->mode=LA_TRANSFORM_MODE_GRAB; ex->DrawCursor=0; }
  626. 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; }
  627. 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; }
  628. if(e->key==' '){ td->UseLocal=!td->UseLocal; }
  629. }
  630. if(e->Type==LA_MOUSEMOVE || e->Type==LA_KEY_DOWN){
  631. switch(td->mode){
  632. case LA_TRANSFORM_MODE_GRAB: la_ApplyTranslation(td,e->x-ex->ClickedX, e->y-ex->ClickedY); break;
  633. case LA_TRANSFORM_MODE_SCALE: la_ApplyScale(td,e->x-ui->L, e->y-ui->U); break;
  634. case LA_TRANSFORM_MODE_ROTATE: la_ApplyRotation(td,e->x-ui->L, e->y-ui->U); break;
  635. default: break;
  636. }
  637. ex->OnX=e->x; ex->OnY=e->y;
  638. laNotifyUsers("tns.world");
  639. }
  640. if(e->Type==LA_L_MOUSE_DOWN || (e->Type==LA_KEY_DOWN && e->key==LA_KEY_ENTER)){ ex->DrawCursor=0;
  641. la_RecordTransformDifferences(td);
  642. laNotifyUsers("tns.world"); la_FreeTransformData(td); return LA_FINISHED;
  643. }
  644. if(e->Type==LA_R_MOUSE_DOWN || e->Type==LA_ESCAPE_DOWN){ ex->DrawCursor=0;
  645. la_CancelTransformObjects(td); laNotifyUsers("tns.world"); la_FreeTransformData(td); return LA_FINISHED;
  646. }
  647. la_MakeTransformOperatorHint(a, td);
  648. return LA_RUNNING;
  649. }
  650. int la_ParentableRecursive(tnsObject* root, tnsObject* parent){
  651. for(laListItemPointer* lip=root->ChildObjects.pFirst;lip;lip=lip->pNext){
  652. tnsObject* o=lip->p; if((!o) || (!(o->Flags&TNS_OBJECT_FLAGS_SELECTED)) || (o==parent)) continue;
  653. if(!tnsCheckParentable(o,parent)) return 0;
  654. if(!la_ParentableRecursive(o,parent)) return 0;
  655. }return 1;
  656. }
  657. void la_MakeParentExecuteRecursive(tnsObject* root, tnsObject* parent, int Unparent, int KeepTransform){
  658. laListItemPointer* NextLip;
  659. for(laListItemPointer* lip=root->ChildObjects.pFirst;lip;lip=NextLip){ NextLip=lip->pNext;
  660. tnsObject* o=lip->p; if((!o) || (!(o->Flags&TNS_OBJECT_FLAGS_SELECTED)) || (o==parent)) continue;
  661. if(Unparent) tnsUnparentObject(o, KeepTransform);
  662. else tnsParentObject(o, parent, KeepTransform);
  663. la_MakeParentExecuteRecursive(o,parent,Unparent,KeepTransform);
  664. }
  665. laNotifyUsers("tns.world");
  666. }
  667. int OPINV_MakeParent(laOperator *a, laEvent *e){
  668. if(!a->This || !a->This->EndInstance){ return 0; }
  669. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  670. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  671. tnsObject* mo=root->Active;
  672. int Unparent=0,KeepTransform=1;
  673. char* action=strGetArgumentString(a->ExtraInstructionsP,"action");
  674. char* keep=strGetArgumentString(a->ExtraInstructionsP,"keep_transform");
  675. if(strSame(action,"unparent")){ Unparent=1; }
  676. if(strSame(keep,"false")){ KeepTransform=0; }
  677. if(!Unparent){ if((!mo) || (!(mo->Flags&TNS_OBJECT_FLAGS_SELECTED))) return LA_FINISHED;
  678. 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; }
  679. }
  680. if(keep){
  681. la_MakeParentExecuteRecursive(root,mo,Unparent,KeepTransform);
  682. laRecordInstanceDifferences(&T->World, "tns_world"); laPushDifferences(Unparent?"Unparent":"Parent", TNS_HINT_TRANSFORM);
  683. return LA_FINISHED;
  684. }
  685. laEnableOperatorPanel(a,a->This,e->x,e->y,200,200,0,0,0,0,0,0,0,0,e);
  686. return LA_RUNNING;
  687. }
  688. void laui_MakeParent(laUiList *uil, laPropPack *pp, laPropPack *actinst, laColumn *extracol, int context){
  689. laColumn* c=laFirstColumn(uil);
  690. laShowItemFull(uil,c,pp,"_this_M_make_parent",0,"action=parent;keep_transform=true;text=Keep transform",0,0);
  691. laShowItemFull(uil,c,pp,"_this_M_make_parent",0,"action=parent;keep_transform=false;text=Directly",0,0);
  692. }
  693. void laui_Unparent(laUiList *uil, laPropPack *pp, laPropPack *actinst, laColumn *extracol, int context){
  694. laColumn* c=laFirstColumn(uil);
  695. laShowItemFull(uil,c,pp,"_this_M_unparent",0,"action=unparent;keep_transform=true;text=Keep transform",0,0);
  696. laShowItemFull(uil,c,pp,"_this_M_unparent",0,"action=unparent;keep_transform=false;text=Directly",0,0);
  697. }
  698. int la_ClearTransformationRecursive(tnsObject* root, int global,int location,int rotation,int scale){
  699. int any=0; for(laListItemPointer* lip=root->ChildObjects.pFirst;lip;lip=lip->pNext){
  700. tnsObject* o=lip->p; if(!(o->Flags&TNS_OBJECT_FLAGS_SELECTED)) continue;
  701. if(location){
  702. if(global){ o->GLocation[0]=o->GLocation[1]=o->GLocation[2]=0;}
  703. else{ o->Location[0]=o->Location[1]=o->Location[2]=0; }
  704. }
  705. if(rotation){
  706. if(global){ o->GRotation[0]=o->GRotation[1]=o->GRotation[2]=0;}
  707. else{ o->Rotation[0]=o->Rotation[1]=o->Rotation[2]=0; }
  708. }
  709. if(scale){ if(global){ o->GScale=1;} else{ o->Scale=1; } }
  710. if(global) tnsGlobalTransformValueChanged(o); else tnsSelfTransformValueChanged(o);
  711. laRecordInstanceDifferences(o, "tns_object"); any++;
  712. any+=la_ClearTransformationRecursive(o,global,location,rotation,scale);
  713. } return any;
  714. }
  715. int OPINV_ClearTransformation(laOperator *a, laEvent *e){
  716. if(!a->This || !a->This->EndInstance){ return 0; }
  717. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  718. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  719. int global=0,location=0,rotation=0,scale=0;
  720. if(strSame(strGetArgumentString(a->ExtraInstructionsP,"global"),"true")){ global=1; }
  721. if(strSame(strGetArgumentString(a->ExtraInstructionsP,"location"),"true")){ location=1; }
  722. if(strSame(strGetArgumentString(a->ExtraInstructionsP,"rotation"),"true")){ rotation=1; }
  723. if(strSame(strGetArgumentString(a->ExtraInstructionsP,"scale"),"true")){ scale=1; }
  724. int any=0;
  725. if(location||rotation||scale){
  726. any=la_ClearTransformationRecursive(root,global,location,rotation,scale);
  727. if(any){ laPushDifferences("Clear Transformations", TNS_HINT_TRANSFORM); laNotifyUsers("tns.world"); }
  728. }
  729. return LA_FINISHED;
  730. }
  731. STRUCTURE(MEDupVert){
  732. int oi; tnsMVert* nmv; tnsMVert* omv; int IsBorder;
  733. };
  734. STRUCTURE(MEDupEdge){
  735. int oi; tnsMEdge* nme; tnsMEdge* ome; int IsBorder;
  736. };
  737. STRUCTURE(MEDupFace){
  738. int oi; tnsMFace* nmf; tnsMFace* omf;
  739. };
  740. STRUCTURE(MExtrudeExtra){
  741. MEDupVert* dv; int nextv,maxv;
  742. MEDupEdge* de; int nexte,maxe;
  743. MEDupFace* df; int nextf,maxf;
  744. tnsMeshObject* mo;
  745. int RemoveOriginalFaces;
  746. };
  747. int la_IsSelectionBorderVertex(tnsMVert* mv){
  748. if(!mv->elink.pFirst)
  749. return 0;
  750. int NearSelected=0, NearUnselected=0;
  751. for(laListItemPointer* lip=mv->elink.pFirst;lip;lip=lip->pNext){
  752. tnsMEdge* me=lip->p; if(!(me->fl&&me->fr)) return 1;
  753. tnsMVert* av=tnsMMeshEdgeAnotherVert(me,mv);
  754. if(me->fl->flags&TNS_MESH_FLAG_SELECTED){ NearSelected=1; } else { return 1; }
  755. if(me->fr->flags&TNS_MESH_FLAG_SELECTED){ NearSelected=1; } else { return 1; }
  756. if(NearUnselected && NearSelected) return 1;
  757. }
  758. return 0;
  759. }
  760. int la_IsSelectionBorderEdge(tnsMEdge* me){
  761. if(me->fl&&me->fr){
  762. if ((me->fl->flags&TNS_MESH_FLAG_SELECTED) && (!(me->fr->flags&TNS_MESH_FLAG_SELECTED))||
  763. (me->fr->flags&TNS_MESH_FLAG_SELECTED) && (!(me->fl->flags&TNS_MESH_FLAG_SELECTED))) return 1;
  764. else return (me->fr->flags==me->fl->flags&&me->fl->flags==0);
  765. } return 1;
  766. }
  767. MExtrudeExtra* la_InitExtrude(tnsMeshObject* mo){
  768. MExtrudeExtra* ee=memAcquireSimple(sizeof(MExtrudeExtra));
  769. arrEnsureLength(&ee->dv, ee->nextv, &ee->maxv, sizeof(MEDupVert));
  770. arrEnsureLength(&ee->de, ee->nexte, &ee->maxe, sizeof(MEDupEdge));
  771. arrEnsureLength(&ee->df, ee->nextf, &ee->maxf, sizeof(MEDupFace));
  772. ee->mo=mo;
  773. return ee;
  774. }
  775. void la_ExtrudeMakeDuplication(MExtrudeExtra* ee){
  776. tnsMeshObject* mo=ee->mo;
  777. for(tnsMVert* mv=mo->mv.pFirst;mv;mv=mv->Item.pNext){ if(!(mv->flags&TNS_MESH_FLAG_SELECTED)) continue;
  778. arrEnsureLength(&ee->dv, ee->nextv, &ee->maxv, sizeof(MEDupVert));
  779. MEDupVert* dv=&ee->dv[ee->nextv];
  780. tnsMVert* nmv=tnsMMeshNewVert(mo); tnsVectorCopy3d(mv->p, &nmv->p); dv->oi=mv->i; mv->i=ee->nextv; dv->nmv=nmv; dv->omv=mv;
  781. dv->IsBorder=la_IsSelectionBorderVertex(mv);
  782. ee->nextv++;
  783. }
  784. for(tnsMEdge* me=mo->me.pFirst;me;me=me->Item.pNext){ if(!(me->flags&TNS_MESH_FLAG_SELECTED)) continue;
  785. arrEnsureLength(&ee->de, ee->nexte, &ee->maxe, sizeof(MEDupEdge));
  786. MEDupEdge* de=&ee->de[ee->nexte];
  787. tnsMEdge* nme=tnsMMeshNewEdge(mo); de->oi=me->i; me->i=ee->nexte; de->nme=nme; de->ome=me; de->IsBorder=la_IsSelectionBorderEdge(me);
  788. tnsMMeshEdgeAssignVerts(nme, ee->dv[me->vl->i].nmv, ee->dv[me->vr->i].nmv);
  789. if(de->IsBorder&&me->fl&&me->fr){ ee->RemoveOriginalFaces=1; }
  790. ee->nexte++;
  791. }
  792. for(tnsMFace* mf=mo->mf.pFirst;mf;mf=mf->Item.pNext){
  793. if(!(mf->flags&TNS_MESH_FLAG_SELECTED)) continue;
  794. arrEnsureLength(&ee->df, ee->nextf, &ee->maxf, sizeof(MEDupFace));
  795. MEDupFace* df=&ee->df[ee->nextf];
  796. tnsMFace* nmf=tnsMMeshNewFace(mo); df->oi=mf->i; mf->i=ee->nextf; df->nmf=nmf; df->omf=mf;
  797. for(laListItemPointer*lip=mf->l.pFirst;lip;lip=lip->pNext){
  798. tnsMEdge* ome=lip->p; tnsMMeshFaceAddEdge(nmf,ee->de[ome->i].nme);
  799. }
  800. ee->nextf++;
  801. }
  802. 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; }
  803. 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; }
  804. for(int i=0;i<ee->nextf;i++){
  805. ee->df[i].omf->i=ee->df[i].oi;
  806. ee->df[i].omf->flags&=(~TNS_MESH_FLAG_SELECTED);
  807. ee->df[i].nmf->flags|=TNS_MESH_FLAG_SELECTED; }
  808. }
  809. void la_RemoveOriginalFaces(MExtrudeExtra* ee){
  810. tnsMeshObject* mo=ee->mo;
  811. if(ee->RemoveOriginalFaces){
  812. for(int i=0;i<ee->nextf;i++){ tnsMMeshRemoveFaceOnly(mo, ee->df[i].omf); }
  813. for(int i=0;i<ee->nexte;i++){ if(ee->de[i].IsBorder) continue; tnsMMeshRemoveEdgeFace(mo, ee->de[i].ome); }
  814. for(int i=0;i<ee->nextv;i++){ if(ee->dv[i].IsBorder) continue; tnsMMeshRemoveVertEdgeFace(mo, ee->dv[i].omv); }
  815. }
  816. }
  817. void la_ReconnectFaces(MExtrudeExtra* ee){
  818. tnsMeshObject* mo=ee->mo;
  819. for(int i=0;i<ee->nexte;i++){
  820. if(!ee->de[i].IsBorder) continue; MEDupEdge*de=&ee->de[i];
  821. tnsMMeshMakeFace4v(mo, de->ome->vl, de->ome->vr, de->nme->vr, de->nme->vl);
  822. }
  823. }
  824. void la_FinishExtrude(MExtrudeExtra* ee, int PushDifferences){
  825. tnsMMeshRefreshIndex(ee->mo);
  826. tnsInvalidateMeshBatch(ee->mo);
  827. if(PushDifferences){
  828. laRecordInstanceDifferences(ee->mo, "tns_mesh_object"); laPushDifferences("Extruded", TNS_HINT_GEOMETRY); laNotifyUsers("tns.world");
  829. }
  830. free(ee->dv); free(ee->de); free(ee->df); memFree(ee);
  831. }
  832. int OPINV_Extrude(laOperator *a, laEvent *e){
  833. if(!a->This || !a->This->EndInstance){ return 0; }
  834. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  835. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  836. tnsMeshObject* mo=root->Active; if(mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode!=TNS_MESH_EDIT_MODE) return 0;
  837. if(!tnsMMeshAnySelected(mo)) return LA_FINISHED;
  838. MExtrudeExtra* ee=la_InitExtrude(mo);
  839. la_ExtrudeMakeDuplication(ee);
  840. if(strSame(strGetArgumentString(a->ExtraInstructionsP,"duplicate_only"), "true")){
  841. la_FinishExtrude(ee, 1);
  842. if(la_InitTransform(a, e, LA_TRANSFORM_MODE_GRAB)) return LA_RUNNING; return LA_FINISHED;
  843. }
  844. la_RemoveOriginalFaces(ee);
  845. la_ReconnectFaces(ee);
  846. la_FinishExtrude(ee, 1);
  847. if(la_InitTransform(a, e, LA_TRANSFORM_MODE_GRAB)) return LA_RUNNING; return LA_FINISHED;
  848. return LA_FINISHED;
  849. }
  850. int la_EdgeShouldDeleteVert(tnsMVert* mv){
  851. if(!mv->elink.pFirst) return 0;
  852. int NearSelected=0, NearUnselected=0;
  853. for(laListItemPointer* lip=mv->elink.pFirst;lip;lip=lip->pNext){
  854. tnsMEdge* me=lip->p;
  855. tnsMVert* av=tnsMMeshEdgeAnotherVert(me,mv);
  856. if(av->flags&TNS_MESH_FLAG_SELECTED){ NearSelected=1; } else { NearUnselected=1; }
  857. if(NearUnselected && NearSelected) return 0;
  858. }
  859. if(NearSelected&&(!NearUnselected)) return 1; return 0;
  860. }
  861. int la_FaceShouldDeleteVert(tnsMVert* mv){
  862. if(!mv->elink.pFirst) return 0;
  863. int NearSelected=0, NearUnselected=0, IsBorder=0;
  864. for(laListItemPointer* lip=mv->elink.pFirst;lip;lip=lip->pNext){
  865. tnsMEdge* me=lip->p;
  866. if(!(me->fl&&me->fr)){ IsBorder=1; }
  867. if(me->fl){ if(me->fl->flags&TNS_MESH_FLAG_SELECTED){ NearSelected=1; } else { NearUnselected=1; } }
  868. if(me->fr){ if(me->fr->flags&TNS_MESH_FLAG_SELECTED){ NearSelected=1; } else { NearUnselected=1; } }
  869. if(NearUnselected && NearSelected) return 0;
  870. }
  871. if((!NearUnselected) && IsBorder) return 1;
  872. return 1;
  873. }
  874. int la_FaceShouldDeleteEdge(tnsMEdge* me){
  875. if(me->fl&&me->fr){
  876. if ((me->fl->flags&TNS_MESH_FLAG_SELECTED) && (!(me->fr->flags&TNS_MESH_FLAG_SELECTED))||
  877. (me->fr->flags&TNS_MESH_FLAG_SELECTED) && (!(me->fl->flags&TNS_MESH_FLAG_SELECTED))) return 0;
  878. else return (me->fr->flags==me->fl->flags&&me->fl->flags==1);
  879. }else{
  880. if(me->fl&&me->fl->flags&TNS_MESH_FLAG_SELECTED&&!me->fr) return 1;
  881. if(me->fr&&me->fr->flags&TNS_MESH_FLAG_SELECTED&&!me->fl) return 1;
  882. } return 0;
  883. }
  884. void la_DeleteVertices(tnsMeshObject* mo){
  885. tnsMVert* nextmv; for(tnsMVert*mv=mo->mv.pFirst;mv;mv=nextmv){ nextmv=mv->Item.pNext; if(!(mv->flags&TNS_MESH_FLAG_SELECTED)) continue;
  886. tnsMMeshRemoveVertEdgeFace(mo, mv);
  887. }
  888. }
  889. void la_DeleteEdges(tnsMeshObject* mo){
  890. laListHandle lv={0};
  891. for(tnsMVert*mv=mo->mv.pFirst;mv;mv=mv->Item.pNext){ if(!(mv->flags&TNS_MESH_FLAG_SELECTED)) continue;
  892. if(la_EdgeShouldDeleteVert(mv)) lstAppendPointer(&lv,mv);
  893. }
  894. tnsMEdge* nextme; for(tnsMEdge*me=mo->me.pFirst;me;me=nextme){ nextme=me->Item.pNext; if(!(me->flags&TNS_MESH_FLAG_SELECTED)) continue;
  895. tnsMMeshRemoveEdgeFace(mo, me);
  896. }
  897. tnsMVert* mv; while(mv=lstPopPointer(&lv)){ tnsMMeshRemoveVertEdgeFace(mo,mv); }
  898. }
  899. void la_DeleteFaces(tnsMeshObject* mo, int OnlyFaces){
  900. laListHandle lv={0}, le={0};
  901. if(OnlyFaces){
  902. tnsMFace* nextmf; for(tnsMFace*mf=mo->mf.pFirst;mf;mf=nextmf){ nextmf=mf->Item.pNext; if(!(mf->flags&TNS_MESH_FLAG_SELECTED)) continue;
  903. tnsMMeshRemoveFaceOnly(mo, mf);
  904. }
  905. }else{
  906. for(tnsMVert*mv=mo->mv.pFirst;mv;mv=mv->Item.pNext){ if(!(mv->flags&TNS_MESH_FLAG_SELECTED)) continue;
  907. if(la_FaceShouldDeleteVert(mv)) lstAppendPointer(&lv,mv);
  908. }
  909. for(tnsMEdge*me=mo->me.pFirst;me;me=me->Item.pNext){ if(!(me->flags&TNS_MESH_FLAG_SELECTED)) continue;
  910. if(la_FaceShouldDeleteEdge(me)) lstAppendPointer(&le,me);
  911. }
  912. tnsMFace* nextmf; for(tnsMFace*mf=mo->mf.pFirst;mf;mf=nextmf){ nextmf=mf->Item.pNext; if(!(mf->flags&TNS_MESH_FLAG_SELECTED)) continue;
  913. tnsMMeshRemoveFaceOnly(mo, mf);
  914. }
  915. tnsMEdge* me; while(me=lstPopPointer(&le)){ tnsMMeshRemoveEdgeFace(mo,me); }
  916. tnsMVert* mv; while(mv=lstPopPointer(&lv)){ tnsMMeshRemoveVertEdgeFace(mo,mv); }
  917. }
  918. }
  919. int la_DeleteSelectedObjectsRecursive(tnsObject* root){
  920. int any=0; for(laListItemPointer* lip=root->ChildObjects.pFirst;lip;lip=lip->pNext){ if(!lip->p) continue;
  921. tnsObject* o=lip->p; la_DeleteSelectedObjectsRecursive(lip->p);
  922. if(o->Flags&TNS_OBJECT_FLAGS_SELECTED){ tnsDestroyObject(o); any++; }
  923. }
  924. return any;
  925. }
  926. int OPINV_Delete(laOperator *a, laEvent *e){
  927. if(!a->This || !a->This->EndInstance){ return 0; }
  928. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  929. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  930. tnsMeshObject* mo=root->Active;
  931. if(mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode!=TNS_MESH_EDIT_MODE){
  932. if(la_DeleteSelectedObjectsRecursive(root)){
  933. laRecordInstanceDifferences(&T->World, "tns_world"); laPushDifferences("Deleted objects", TNS_HINT_TRANSFORM); laNotifyUsers("tns.world");
  934. }
  935. }else{
  936. if(!tnsMMeshAnySelected(mo)) return LA_FINISHED;
  937. if(strSame(strGetArgumentString(a->ExtraInstructionsP, "mode"),"vertices")){
  938. la_DeleteVertices(mo);
  939. }elif(strSame(strGetArgumentString(a->ExtraInstructionsP, "mode"),"edges")){
  940. la_DeleteEdges(mo);
  941. }elif(strSame(strGetArgumentString(a->ExtraInstructionsP, "mode"),"faces")){
  942. la_DeleteFaces(mo,0);
  943. }elif(strSame(strGetArgumentString(a->ExtraInstructionsP, "mode"),"only_faces")){
  944. la_DeleteFaces(mo,1);
  945. }else{
  946. laEnableOperatorPanel(a,a->This,e->x,e->y,200,200,0,0,0,0,0,0,0,0,e);
  947. return LA_RUNNING;
  948. }
  949. tnsMMeshDeselectAll(mo);
  950. tnsMMeshRefreshIndex(mo);
  951. tnsInvalidateMeshBatch(mo);
  952. laRecordInstanceDifferences(mo, "tns_mesh_object"); laPushDifferences("Deleted primitives", TNS_HINT_GEOMETRY); laNotifyUsers("tns.world");
  953. }
  954. return LA_FINISHED;
  955. }
  956. void laui_Delete(laUiList *uil, laPropPack *pp, laPropPack *actinst, laColumn *extracol, int context){
  957. laColumn* c=laFirstColumn(uil);
  958. laShowItemFull(uil,c,pp,"_this_M_delete",0,"mode=vertices;text=Vertices",0,0);
  959. laShowItemFull(uil,c,pp,"_this_M_delete",0,"mode=edges;text=Edges",0,0);
  960. laShowItemFull(uil,c,pp,"_this_M_delete",0,"mode=faces;text=Faces",0,0);
  961. laShowItemFull(uil,c,pp,"_this_M_delete",0,"mode=only_faces;text=Only Faces",0,0);
  962. }
  963. STRUCTURE(MIslandInfo){
  964. laListItem Item;
  965. laListHandle v,e,f;int numv,nume,numf;
  966. int Paired,HasBranches;
  967. };
  968. STRUCTURE(MMakeData){
  969. laListHandle Islands; int NumIslands;
  970. };
  971. #define M_SHOULD_INCL_PRIM(m) \
  972. ((!(m->flags&TNS_MESH_FLAG_PICKED)) && (m->flags&TNS_MESH_FLAG_SELECTED))
  973. #define M_SHOULD_USE_OE(oe,sf) \
  974. (((!oe->fl)&&((!sf)||(oe->fr!=sf)))||((!oe->fr)&&((!sf)||(oe->fl!=sf))))
  975. MIslandInfo* la_NewMIsland(MMakeData* md){ MIslandInfo* ii=memAcquireSimple(sizeof(MIslandInfo)); lstAppendItem(&md->Islands, ii); md->NumIslands++; return ii; }
  976. void la_FillIslandFromVert(MIslandInfo* ii, tnsMVert* mv, int SelectMode){
  977. mv->flags|=TNS_MESH_FLAG_PICKED; lstAppendPointer(&ii->v,mv); ii->numv++; int connections=0;
  978. for(laListItemPointer* lip=mv->elink.pFirst;lip;lip=lip->pNext){ tnsMEdge* oe=lip->p; tnsMVert* ov=tnsMMeshEdgeAnotherVert(oe,mv);
  979. if(ov->flags&TNS_MESH_FLAG_SELECTED) connections++; else continue;
  980. if(SelectMode==LA_CANVAS_SELECT_MODE_EDGES){ if(!(oe->flags&TNS_MESH_FLAG_SELECTED)) continue; }
  981. if(M_SHOULD_INCL_PRIM(ov)){ la_FillIslandFromVert(ii,ov,SelectMode); } else { continue; }
  982. if(M_SHOULD_INCL_PRIM(oe)){ lstAppendPointer(&ii->e,oe); ii->nume++; oe->flags|=TNS_MESH_FLAG_PICKED; }
  983. if(oe->fl&&M_SHOULD_INCL_PRIM(oe->fl)){ lstAppendPointer(&ii->f,oe->fl); ii->numf++; oe->fl->flags|=TNS_MESH_FLAG_PICKED; }
  984. if(oe->fr&&M_SHOULD_INCL_PRIM(oe->fr)){ lstAppendPointer(&ii->f,oe->fr); ii->numf++; oe->fr->flags|=TNS_MESH_FLAG_PICKED; }
  985. }
  986. if(connections>2) ii->HasBranches=1;
  987. }
  988. void la_GetSelectionIslands(tnsMeshObject* mo, MMakeData* md, int SelectMode){
  989. tnsMMeshClearExtraFlags(mo);
  990. for(tnsMVert* mv=mo->mv.pFirst;mv;mv=mv->Item.pNext){
  991. if(M_SHOULD_INCL_PRIM(mv)){ MIslandInfo* ii=la_NewMIsland(md); la_FillIslandFromVert(ii,mv,SelectMode); }
  992. }
  993. }
  994. void la_ClearIslands(MMakeData* md){ MIslandInfo* ii; while(ii=lstPopItem(&md->Islands)){ while(lstPopPointer(&ii->v)); memFree(ii); } }
  995. tnsMFace* la_MakeFacesFrom1Vert(tnsMeshObject* mo, tnsMVert* mv){
  996. tnsMEdge* oe1=0,*oe2=0; tnsMVert* ov1,*ov2;
  997. for(laListItemPointer* lip=mv->elink.pFirst;lip;lip=lip->pNext){
  998. 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 */ }
  999. } if(!oe1||!oe2) return 0;
  1000. ov1=tnsMMeshEdgeAnotherVert(oe1,mv); ov2=tnsMMeshEdgeAnotherVert(oe2,mv);
  1001. laListHandle vl={0}; lstAppendPointer(&vl,ov1); lstAppendPointer(&vl,mv); lstAppendPointer(&vl,ov2);
  1002. tnsMFace* f=tnsMMeshMakeFaceN(mo, 3, &vl, 0);
  1003. ov1->flags|=TNS_MESH_FLAG_SELECTED;ov2->flags|=TNS_MESH_FLAG_SELECTED; mv->flags&=(~TNS_MESH_FLAG_SELECTED);
  1004. tnsMMeshEnsureSelectionFromVerts(mo);
  1005. while(lstPopPointer(&vl)); return f;
  1006. }
  1007. tnsMFace* la_MakeFacesFrom2Verts(tnsMeshObject* mo, tnsMVert* mv1, tnsMVert* mv2){
  1008. tnsMEdge* oe1=0,*oe2=0; tnsMVert* ov1,*ov2; tnsMFace* sf=0;
  1009. tnsMEdge* se=tnsMMeshVertShareEdge(mv1,mv2); if(se->fl && se->fr) return 0; sf=se->fl?se->fl:se->fr;
  1010. for(laListItemPointer* lip=mv1->elink.pFirst;lip;lip=lip->pNext){
  1011. 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 */ }
  1012. }
  1013. for(laListItemPointer* lip=mv2->elink.pFirst;lip;lip=lip->pNext){
  1014. 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 */ }
  1015. }
  1016. if(!oe1||!oe2) return 0;
  1017. ov1=tnsMMeshEdgeAnotherVert(oe1,mv1); ov2=tnsMMeshEdgeAnotherVert(oe2,mv2);
  1018. ov1->flags|=TNS_MESH_FLAG_SELECTED;ov2->flags|=TNS_MESH_FLAG_SELECTED; mv1->flags&=(~TNS_MESH_FLAG_SELECTED);mv2->flags&=(~TNS_MESH_FLAG_SELECTED);
  1019. laListHandle vl={0}; int vcount=3;
  1020. lstAppendPointer(&vl,ov1); lstAppendPointer(&vl,mv1); lstAppendPointer(&vl,mv2); if(ov2!=ov1){ lstAppendPointer(&vl,ov2); vcount=4; }
  1021. tnsMFace* f=tnsMMeshMakeFaceN(mo, vcount, &vl, 0); tnsMMeshEnsureSelectionFromVerts(mo);
  1022. while(lstPopPointer(&vl)); return f;
  1023. }
  1024. int la_IsEndingVert(tnsMVert* mv){
  1025. int sel=0; for(laListItemPointer*lip=mv->elink.pFirst;lip;lip=lip->pNext){ tnsMEdge* me=lip->p;
  1026. if(tnsMMeshEdgeAnotherVert(me,mv)->flags&TNS_MESH_FLAG_SELECTED){ sel++; if(sel>1) return 0; }
  1027. }
  1028. if(sel==1) return 1; return 0;
  1029. }
  1030. void la_EnsureIslandVertsSequence(MIslandInfo* ii){
  1031. laListHandle l={0}; tnsMVert* startv=((laListItemPointer*)ii->v.pFirst)->p; laListItemPointer* nextlip;
  1032. 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.
  1033. lstRemovePointer(&ii->v, startv); lstAppendPointer(&l, startv);
  1034. while(ii->v.pFirst){
  1035. for(laListItemPointer*lip=ii->v.pFirst;lip;lip=lip->pNext){ nextlip=lip->pNext;
  1036. if(tnsMMeshVertShareEdge(startv, lip->p)){ startv=lip->p; lstRemoveItem(&ii->v,lip); lstAppendItem(&l, lip); break; }
  1037. }
  1038. }
  1039. memcpy(&ii->v, &l, sizeof(laListHandle));
  1040. }
  1041. MIslandInfo* la_GetNeighborIsland(tnsMVert* from, MMakeData* md){
  1042. int found=0; real dist=1e10; MIslandInfo* rii=0; for(MIslandInfo* ii=md->Islands.pFirst;ii;ii=ii->Item.pNext){ if(ii->Paired) continue;
  1043. found=1; tnsMVert* mv1=((laListItemPointer*)ii->v.pFirst)->p,*mv2=((laListItemPointer*)ii->v.pLast)->p;
  1044. real d1=tnsDist3dv(mv1->p, from->p),d2=tnsDist3dv(mv2->p, from->p);
  1045. if(d2<d1 && d2<dist){ lstReverse(&ii->v); } if(d1<dist || d2<dist){ rii=ii; }
  1046. }
  1047. if(rii) rii->Paired=1;
  1048. return rii;
  1049. }
  1050. int la_MakeFacesFromIslands(tnsMeshObject* mo, MMakeData* md){
  1051. int success=0;
  1052. if(!md->Islands.pFirst) return 0;
  1053. if(md->Islands.pFirst==md->Islands.pLast){
  1054. MIslandInfo* ii=md->Islands.pFirst; if(ii->HasBranches) return 0;
  1055. if(ii->numv==1){ laListItemPointer*lip=ii->v.pFirst; if(la_MakeFacesFrom1Vert(mo,lip->p)) success++; }
  1056. elif(ii->numv==2){ laListItemPointer*lip=ii->v.pFirst,*lip2=ii->v.pLast; if(la_MakeFacesFrom2Verts(mo,lip->p, lip2->p)) success++; }
  1057. else{ la_EnsureIslandVertsSequence(ii); if(tnsMMeshMakeFaceN(mo, ii->numv, &ii->v, 0)) success++; }
  1058. }else{
  1059. for(MIslandInfo* ii=md->Islands.pFirst;ii;ii=ii->Item.pNext){ if(ii->HasBranches) return 0; la_EnsureIslandVertsSequence(ii); }
  1060. laListHandle final={0}; int vcount=0;
  1061. MIslandInfo* ii=md->Islands.pFirst; ii->Paired=1; for(laListItemPointer* lip=ii->v.pFirst;lip;lip=lip->pNext){ lstAppendPointer(&final, lip->p); vcount++; }
  1062. while((ii=la_GetNeighborIsland(((laListItemPointer*)ii->v.pLast)->p, md))){
  1063. for(laListItemPointer* lip=ii->v.pFirst;lip;lip=lip->pNext){ lstAppendPointer(&final, lip->p); vcount++; }
  1064. }
  1065. tnsMEdge* fallback_e=0;
  1066. if(tnsMMeshMakeFaceN(mo,vcount,&final, &fallback_e) || fallback_e) success++;
  1067. while(lstPopPointer(&final));
  1068. }
  1069. return success;
  1070. }
  1071. int OPINV_Make(laOperator *a, laEvent *e){
  1072. if(!a->This || !a->This->EndInstance){ return 0; }
  1073. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  1074. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  1075. tnsMeshObject* mo=root->Active;
  1076. if(!mo||mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode!=TNS_MESH_EDIT_MODE){ return LA_CANCELED; }
  1077. MMakeData md={0};
  1078. la_GetSelectionIslands(mo,&md,ex->SelectMode);
  1079. int success=la_MakeFacesFromIslands(mo,&md); if(success){ tnsMMeshCalculateNormal(mo); }
  1080. la_ClearIslands(&md);
  1081. tnsMMeshRefreshIndex(mo);
  1082. tnsMMeshEnsureSelection(mo,ex->SelectMode);
  1083. tnsInvalidateMeshBatch(mo);
  1084. if(laRecordInstanceDifferences(mo, "tns_mesh_object")) laPushDifferences("Make primitives", TNS_HINT_GEOMETRY);
  1085. laNotifyUsers("tns.world");
  1086. return LA_FINISHED;
  1087. }
  1088. int OPINV_Subdiv(laOperator *a, laEvent *e){
  1089. if(!a->This || !a->This->EndInstance){ return 0; }
  1090. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  1091. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  1092. tnsMeshObject* mo=root->Active;
  1093. if(mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode!=TNS_MESH_EDIT_MODE){ return LA_CANCELED; }
  1094. laListHandle pending={0}; for(tnsMEdge* me=mo->me.pFirst;me;me=me->Item.pNext){ if(me->flags&TNS_MESH_FLAG_SELECTED) lstAppendPointer(&pending, me); }
  1095. if(!pending.pFirst) return LA_FINISHED;
  1096. tnsMEdge* me; while(me=lstPopPointer(&pending)){ tnsMVert* mv=tnsMMeshEdgeInsertVertAt(mo,me,0.5,0,0,0); mv->flags|=TNS_MESH_FLAG_SELECTED; }
  1097. tnsMMeshRefreshIndex(mo);
  1098. tnsMMeshEnsureSelection(mo,ex->SelectMode);
  1099. tnsInvalidateMeshBatch(mo);
  1100. if(laRecordInstanceDifferences(mo, "tns_mesh_object")) laPushDifferences("Subdivide edges", TNS_HINT_GEOMETRY);
  1101. laNotifyUsers("tns.world");
  1102. return LA_FINISHED;
  1103. }
  1104. int OPINV_Add(laOperator *a, laEvent *e){
  1105. if(!a->This || !a->This->EndInstance){ return 0; }
  1106. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  1107. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  1108. tnsMeshObject* mo=root->Active; int ran=0; tnsObject* no=0;
  1109. if((!mo) || mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode!=TNS_MESH_EDIT_MODE){
  1110. if(strSame(strGetArgumentString(a->ExtraInstructionsP, "mode"),"PLANE")){ tnsDeselectAllObjects(root);
  1111. no=tnsCreateMeshPlane(root, "Plane",0,0,0,10); no->Flags|=TNS_OBJECT_FLAGS_SELECTED; memAssignRef(root,&root->Active,no); ran=1; }
  1112. else{ laEnableOperatorPanel(a,a->This,e->x,e->y,200,200,0,0,0,0,0,0,0,0,e); return LA_RUNNING; }
  1113. if(ran){ laRecordAndPush(0,"tns.world","Add object",TNS_HINT_GEOMETRY); laNotifyUsers("tns.world"); }
  1114. }else{
  1115. if(strSame(strGetArgumentString(a->ExtraInstructionsP, "mode"),"PLANE")){
  1116. tnsMMeshDeselectAll(mo); tnsAddMMeshPlane(mo, 10); tnsMMeshEnsureSelection(mo,ex->SelectMode); ran=1;
  1117. }else{ laEnableOperatorPanel(a,a->This,e->x,e->y,200,200,0,0,0,0,0,0,0,0,e); return LA_RUNNING; }
  1118. if(ran){
  1119. tnsMMeshRefreshIndex(mo); tnsInvalidateMeshBatch(mo);
  1120. laRecordInstanceDifferences(mo, "tns_mesh_object"); laPushDifferences("Add primitives", TNS_HINT_GEOMETRY); laNotifyUsers("tns.world");
  1121. }
  1122. }
  1123. return LA_FINISHED;
  1124. }
  1125. void laui_Add(laUiList *uil, laPropPack *pp, laPropPack *actinst, laColumn *extracol, int context){
  1126. laColumn* c=laFirstColumn(uil);
  1127. laShowItemFull(uil,c,pp,"_this_M_add",0,"mode=PLANE;text=Plane",0,0);
  1128. }
  1129. int OPINV_Separate(laOperator *a, laEvent *e){
  1130. if(!a->This || !a->This->EndInstance){ return 0; }
  1131. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  1132. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return LA_CANCELED;
  1133. tnsMeshObject* mo=root->Active; int ran=0;
  1134. if(mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode!=TNS_MESH_EDIT_MODE){
  1135. return LA_CANCELED;
  1136. }
  1137. if(!tnsMMeshAnySelected(mo)) return LA_CANCELED;
  1138. MExtrudeExtra* ee=la_InitExtrude(mo);
  1139. la_ExtrudeMakeDuplication(ee);
  1140. ee->RemoveOriginalFaces=1;la_RemoveOriginalFaces(ee);
  1141. tnsMeshObject* no=tnsCreateMeshEmpty(mo->Base.ParentObject?mo->Base.ParentObject:mo->Base.InRoot, mo->Base.Name->Ptr, 0,0,0);
  1142. tnsCopyObjectTransformationsLocal(no,mo);
  1143. no->Mode=TNS_MESH_EDIT_MODE;
  1144. tnsMVert* nmv; for(tnsMVert* mv=mo->mv.pFirst;mv;mv=nmv){ nmv=mv->Item.pNext; if(!(mv->flags&TNS_MESH_FLAG_SELECTED))continue;
  1145. lstRemoveItem(&mo->mv, mv); lstAppendItem(&no->mv, mv); no->totmv++; mo->totmv--; }
  1146. tnsMEdge* nme; for(tnsMEdge* me=mo->me.pFirst;me;me=nme){ nme=me->Item.pNext; if(!(me->flags&TNS_MESH_FLAG_SELECTED))continue;
  1147. lstRemoveItem(&mo->me, me); lstAppendItem(&no->me, me); no->totme++; mo->totme--; }
  1148. tnsMFace* nmf; for(tnsMFace* mf=mo->mf.pFirst;mf;mf=nmf){ nmf=mf->Item.pNext; if(!(mf->flags&TNS_MESH_FLAG_SELECTED))continue;
  1149. lstRemoveItem(&mo->mf, mf); lstAppendItem(&no->mf, mf); no->totmf++; mo->totmf--; }
  1150. tnsMMeshRefreshIndex(no); tnsMeshLeaveEditMode(no);
  1151. la_FinishExtrude(ee, 0);
  1152. tnsMMeshRefreshIndex(mo); tnsInvalidateMeshBatch(mo);
  1153. laRecordAndPush(0,"tns.world","Separate mesh parts",TNS_HINT_GEOMETRY); laNotifyUsers("tns.world");
  1154. return LA_FINISHED;
  1155. }
  1156. void la_PopulateSelectedMeshObjects(tnsObject* root, laListHandle* l){
  1157. if(root->Type==TNS_OBJECT_MESH && root->Flags&TNS_OBJECT_FLAGS_SELECTED){ lstAppendPointer(l,root); }
  1158. for(laListItemPointer* lip=root->ChildObjects.pFirst;lip;lip=lip->pNext){
  1159. la_PopulateSelectedMeshObjects(lip->p, l);
  1160. }
  1161. }
  1162. int OPINV_Combine(laOperator *a, laEvent *e){
  1163. if(!a->This || !a->This->EndInstance){ return 0; }
  1164. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  1165. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return LA_CANCELED;
  1166. tnsMeshObject* mo=root->Active; int ran=0;
  1167. if(!mo || mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode==TNS_MESH_EDIT_MODE){ return LA_CANCELED; }
  1168. laListHandle pending={0}; la_PopulateSelectedMeshObjects(root,&pending);
  1169. tnsMeshObject* o; while(o=lstPopPointer(&pending)){ if(o==mo || o->Mode==TNS_MESH_EDIT_MODE) continue;
  1170. if(tnsMergeMeshObjects(mo, o)) ran++;
  1171. }
  1172. if(ran){
  1173. tnsMMeshRefreshIndex(mo); tnsInvalidateMeshBatch(mo);
  1174. laRecordAndPush(0,"tns.world","Merge mesh objects",TNS_HINT_GEOMETRY); laNotifyUsers("tns.world");
  1175. }
  1176. return LA_FINISHED;
  1177. }
  1178. int OPINV_Duplicate(laOperator *a, laEvent *e){
  1179. if(!a->This || !a->This->EndInstance){ return 0; }
  1180. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  1181. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return LA_CANCELED;
  1182. tnsMeshObject* mo=root->Active; int ran=0;
  1183. if(!mo || mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode==TNS_MESH_EDIT_MODE){ return LA_CANCELED; }
  1184. laListHandle pending={0}; la_PopulateSelectedMeshObjects(root,&pending);
  1185. tnsMeshObject* o; tnsMeshObject* no;while(o=lstPopPointer(&pending)){ if(o->Mode==TNS_MESH_EDIT_MODE) continue;
  1186. if(no=tnsDuplicateMeshObjects(o)){ no->Base.Flags|=TNS_OBJECT_FLAGS_SELECTED; o->Base.Flags&=(~TNS_OBJECT_FLAGS_SELECTED);
  1187. if(mo==o){ memAssignRef(root,&root->Active,no); } ran++;
  1188. }
  1189. }
  1190. if(ran){
  1191. laRecordAndPush(0,"tns.world","Merge mesh objects",TNS_HINT_GEOMETRY); laNotifyUsers("tns.world");
  1192. if(la_InitTransform(a,e,LA_TRANSFORM_MODE_GRAB)) return LA_RUNNING; return LA_FINISHED;
  1193. }
  1194. return LA_FINISHED;
  1195. }
  1196. int OPINV_RecalculateNormals(laOperator *a, laEvent *e){
  1197. if(!a->This || !a->This->EndInstance){ return 0; }
  1198. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  1199. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return LA_CANCELED;
  1200. tnsMeshObject* mo=root->Active; int ran=0;
  1201. if(!mo || mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode!=TNS_MESH_EDIT_MODE){ return LA_CANCELED; }
  1202. ran=tnsMMeshCalculateNormal(mo);
  1203. if(ran){ tnsInvalidateMeshBatch(mo); laRecordAndPush(0,"tns.world","Recalculate Normals",TNS_HINT_GEOMETRY); laNotifyUsers("tns.world"); }
  1204. return LA_FINISHED;
  1205. }
  1206. STRUCTURE(MKnifeElement){
  1207. laListItem Item;
  1208. void* p;
  1209. int Type;
  1210. };
  1211. void la_KnifeUpdateToolBatch(MSelectExtra* se,tnsObject* o){
  1212. if(se->root->ExtraBatch) tnsDeleteBatch(se->root->ExtraBatch); se->root->ExtraBatch=0;
  1213. int count=lstCountElements(&se->KnifeElements); if((!count) && (!se->PendingElem)) return;
  1214. float* points=calloc((count+1)*3,sizeof(real));
  1215. float* p=points; real tmp[3],trans[4];
  1216. for(MKnifeElement* ke=se->KnifeElements.pFirst;ke;ke=ke->Item.pNext){
  1217. if(ke->Type==TNS_MMESH_EDGE_BIT){ tnsMEdge* me=ke->p; tnsVectorSet3v(tmp,me->vl->p); tnsVectorAccum3d(tmp,me->vr->p);
  1218. tnsVectorMultiSelf3d(tmp,0.5); tnsApplyTransform43d(trans,o->GlobalTransform,tmp); tnsVectorSet3v(p,trans); }
  1219. else{ tnsMVert* mv=ke->p; tnsApplyTransform43d(trans,o->GlobalTransform,mv->p); tnsVectorSet3v(p,trans); }
  1220. p+=3;
  1221. }
  1222. if(se->PendingElem){
  1223. if(se->PendingElemType==TNS_MMESH_EDGE_BIT){ tnsMEdge* me=se->PendingElem; tnsVectorSet3v(tmp,me->vl->p); tnsVectorAccum3d(tmp,me->vr->p);
  1224. tnsVectorMultiSelf3d(tmp,0.5); tnsApplyTransform43d(trans,o->GlobalTransform,tmp); tnsVectorSet3v(p,trans); }
  1225. else{ tnsMVert* mv=se->PendingElem; tnsApplyTransform43d(trans,o->GlobalTransform,mv->p); tnsVectorSet3v(p,trans); }
  1226. }elif(count){
  1227. tnsVectorSet3v(p,p-3);
  1228. }
  1229. uint32_t elem=count;
  1230. tnsBatch* batch=tnsCreateBatch(count+1,3,points,0,0,0,0); tnsBatchCommand*c;
  1231. c=tnsCreateCommand(batch, "hovering_point", 1, 3, GL_POINTS, &elem, 0);
  1232. tnsCommandUseUniformColor(c,laAccentColor(LA_BT_SVERTEX));
  1233. if(count){
  1234. c=tnsCreateCommand(batch, "edges", count+(se->IsLoop?0:1), 3, se->IsLoop?GL_LINE_LOOP:GL_LINE_STRIP, 0, 0);
  1235. tnsCommandUseUniformColor(c,laAccentColor(LA_BT_NORMAL));
  1236. tnsCommandUseWidth(c, 2);
  1237. c=tnsCreateCommand(batch, "points", count, 3, GL_POINTS, 0, 0);
  1238. tnsCommandUseUniformColor(c,laAccentColor(LA_BT_NORMAL));
  1239. tnsCommandUseWidth(c, 6);
  1240. }
  1241. se->root->ExtraBatch=batch;
  1242. free(points);
  1243. }
  1244. int la_KnifeIsDuplicated(MSelectExtra* se, void* ref){
  1245. for(MKnifeElement* ke=se->KnifeElements.pFirst;ke;ke=ke->Item.pNext){
  1246. if(ke->Type==TNS_MMESH_EDGE_BIT && ke->p==ref){ return 1; }
  1247. }
  1248. return 0;
  1249. }
  1250. void la_KnifeAppendCut(MSelectExtra* se){
  1251. MKnifeElement* ke=lstAppendPointerSized(&se->KnifeElements,se->PendingElem,sizeof(MKnifeElement));
  1252. ke->Type=se->PendingElemType;
  1253. }
  1254. int la_KnifeRegisterCuts(MSelectExtra* se, tnsMeshObject* mo, int TryClose){
  1255. if(!mo || mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode!=TNS_MESH_EDIT_MODE) return 0;
  1256. if(!se->KnifeElements.pFirst) return 0;
  1257. tnsMVert* lastv=0,*newv=0,*firstv=0; tnsMEdge* newme=0; int changed=0; tnsMFace* mf=0;
  1258. for(MKnifeElement* ke=se->KnifeElements.pFirst;ke;ke=ke->Item.pNext){
  1259. if(ke->Type==TNS_MMESH_EDGE_BIT){
  1260. newv=tnsMMeshEdgeInsertVertAt(mo,ke->p,0.5,0,0,0); changed=1;
  1261. }
  1262. else{ newv=ke->p; }
  1263. if(lastv){ if(tnsMMeshVertsShareFace(lastv,newv)) newme=tnsMMeshMakeEdge(mo, lastv, newv); changed=1; }
  1264. lastv=newv; if(!firstv) firstv=newv; newv->flags|=TNS_MESH_FLAG_SELECTED;
  1265. if(newme){ newme->flags|=TNS_MESH_FLAG_SELECTED; }
  1266. }
  1267. if(TryClose){ if((mf=tnsMMeshVertsShareFace(lastv,firstv))&&mf->looplen==6) newme=tnsMMeshMakeEdge(mo, lastv, firstv); changed=1; }
  1268. if(changed){ tnsMMeshRefreshIndex(mo); tnsMMeshCalculateNormal(mo); }
  1269. return changed;
  1270. }
  1271. void la_KnifeFinish(MSelectExtra* se, tnsObject*o){
  1272. if(o->ExtraBatch) tnsDeleteBatch(o->ExtraBatch); o->ExtraBatch=0;
  1273. while(lstPopPointer(&se->KnifeElements));
  1274. la_FreeSelectData(se->sd); memFree(se);
  1275. }
  1276. void la_KnifeRefreshLoopCuts(MSelectExtra* se, tnsObject* o,tnsMEdge* me){
  1277. if(!me) return;
  1278. tnsMMeshClearExtraFlags(o);
  1279. laListHandle lst={0}; lstAppendPointer(&lst,me); me->flags|=TNS_MESH_FLAG_PICKED;
  1280. tnsMMeshExpandBandList(o, me, &lst);
  1281. if(lst.pFirst==lst.pLast){ while(lstPopPointer(&lst)); return; }
  1282. tnsMEdge* mme; while((mme=lstPopPointer(&lst))){
  1283. MKnifeElement* ke=lstAppendPointerSized(&se->KnifeElements,mme,sizeof(MKnifeElement));
  1284. ke->Type=TNS_MMESH_EDGE_BIT;
  1285. }
  1286. }
  1287. void la_KnifeSortLoopCuts(MSelectExtra* se){
  1288. laListHandle lst={0};MKnifeElement* ke=se->KnifeElements.pFirst,*NextKe;
  1289. if(!ke) return;
  1290. lstRemoveItem(&se->KnifeElements,ke); lstAppendItem(&lst,ke);
  1291. for(ke=se->KnifeElements.pFirst;ke;ke=NextKe){
  1292. NextKe=ke->Item.pNext; tnsMEdge* me=ke->p,*firste=((MKnifeElement*)lst.pFirst)->p,*laste=((MKnifeElement*)lst.pLast)->p;
  1293. if(tnsMMeshEdgeShareFace(me,firste) && (!tnsMMeshEdgeShareVert(me,firste))){
  1294. lstRemoveItem(&se->KnifeElements,ke); lstPushItem(&lst,ke); ke=se->KnifeElements.pFirst; continue; }
  1295. if(tnsMMeshEdgeShareFace(me,laste) && (!tnsMMeshEdgeShareVert(me,laste))){
  1296. lstRemoveItem(&se->KnifeElements,ke); lstAppendItem(&lst,ke); ke=se->KnifeElements.pFirst; continue; }
  1297. }
  1298. if(se->KnifeElements.pFirst){ /* something went wrong */ while(lstPopPointer(&se->KnifeElements)); while(lstPopPointer(&lst)); }
  1299. se->KnifeElements.pFirst=lst.pFirst; se->KnifeElements.pLast=lst.pLast;
  1300. }
  1301. int OPINV_Knife(laOperator *a, laEvent *e){
  1302. if(!a->This || !a->This->EndInstance){ return 0; }
  1303. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  1304. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return LA_CANCELED;
  1305. tnsMeshObject* mo=root->Active; int ran=0;
  1306. if(!mo || mo->Base.Type!=TNS_OBJECT_MESH || mo->Mode!=TNS_MESH_EDIT_MODE){ return LA_CANCELED; }
  1307. MSelectExtra* se=memAcquire(sizeof(MSelectExtra));
  1308. MSelectData* sd=la_InitSelectData(ex->OffScr->pColor[0]->Width, ex->OffScr->pColor[0]->Height, c);
  1309. a->CustomData=se; se->sd=sd; se->root=root;
  1310. int SelectMode=LA_CANVAS_SELECT_MODE_KNIFE;
  1311. if(strSame(strGetArgumentString(a->ExtraInstructionsP, "mode"), "loop_cut")){
  1312. se->IsLoop=1; SelectMode=LA_CANVAS_SELECT_MODE_EDGES; }
  1313. la_PopulateSelectDataPrimitives(sd,mo,c,SelectMode);
  1314. if(se->IsLoop){ strSafePrint(&a->RuntimeHint,"◧ Cut ◨ Cancel"); }
  1315. else{ strSafePrint(&a->RuntimeHint,"◧ Place Cut ◨ Cancel ⮨ Confirm"); }
  1316. return LA_RUNNING;
  1317. }
  1318. int OPMOD_Knife(laOperator *a, laEvent *e){
  1319. if(!a->This || !a->This->EndInstance || !a->CustomData){ return 0; }
  1320. laCanvasExtra* ex=a->This->EndInstance; tnsCamera*c=ex->ViewingCamera; laUiItem* ui=ex->ParentUi;
  1321. tnsObject*root=ui?ui->PP.EndInstance:0; if(!root) return 0;
  1322. MSelectExtra* se=a->CustomData;
  1323. tnsMeshObject* mo=root->Active;
  1324. int changed=0;
  1325. if(e->Type==LA_R_MOUSE_DOWN || (e->Type == LA_KEY_DOWN && e->key==LA_KEY_ESCAPE)){
  1326. la_KnifeFinish(se,root); laNotifyUsers("tns.world"); return LA_FINISHED;
  1327. }
  1328. if(e->Type&LA_MOUSE_EVENT){
  1329. int elemtype,id=la_SelectGetClosest(se->sd, e->x-ui->L, e->y-ui->U, LA_RH,&elemtype)-1;
  1330. void* p=la_SelectGetRef(se->sd,id,elemtype);
  1331. if(se->IsLoop){
  1332. if(se->PendingElem!=p){ changed=1; while(lstPopPointer(&se->KnifeElements));
  1333. se->PendingElem=p; la_KnifeRefreshLoopCuts(se, mo, se->PendingElem);
  1334. la_KnifeSortLoopCuts(se);
  1335. }
  1336. }else{
  1337. if(la_KnifeIsDuplicated(se,p)) p=0;
  1338. if(se->PendingElem!=p){ changed=1; }
  1339. se->PendingElem=p; se->PendingElemType=elemtype;
  1340. if(e->Type==LA_L_MOUSE_DOWN && p){ la_KnifeAppendCut(se); changed=1; }
  1341. }
  1342. }
  1343. if((e->Type==LA_KEY_DOWN && e->key==LA_KEY_ENTER) || (se->IsLoop && e->Type==LA_L_MOUSE_DOWN)){
  1344. if(la_KnifeRegisterCuts(se,mo,se->IsLoop)){ tnsMMeshEnsureSelection(mo,ex->SelectMode);
  1345. tnsInvalidateMeshBatch(mo); laNotifyUsers("tns.world");
  1346. laRecordAndPush(0,"tns.world",se->IsLoop?"Loop Cut":"Knife Cut",TNS_HINT_GEOMETRY);
  1347. }
  1348. la_KnifeFinish(se,root); return LA_FINISHED;
  1349. }
  1350. if(changed){
  1351. la_KnifeUpdateToolBatch(se,mo); laRedrawCurrentPanel();
  1352. }
  1353. return LA_RUNNING;
  1354. }
  1355. void la_RegisterModellingOperators(){
  1356. laPropContainer *pc; laProp *p;
  1357. laOperatorType *at;
  1358. laEnumProp *ep;
  1359. laCreateOperatorType("M_set_cursor", "Set Cursor", "Set cursor in the viewport", OPCHK_ViewportAndSceneExists, 0, 0, OPINV_SetCursor, 0, 0, LA_EXTRA_TO_PANEL);
  1360. laCreateOperatorType("M_toggle_edit_mode", "Toggle Edit Mode", "Toggle edit mode of the active object", OPCHK_ThereIsActiveObject, 0, 0, OPINV_ToggleEdit, 0, 0, 0);
  1361. laCreateOperatorType("M_select", "Select", "Select things in the viewport", OPCHK_ViewportAndSceneExists, 0, 0, OPINV_Select, OPMOD_Select, 0, LA_EXTRA_TO_PANEL);
  1362. laCreateOperatorType("M_grab", "Grab", "Grab things and move around", OPCHK_ViewportAndSceneExists, 0, 0, OPINV_Grab, OPMOD_Transformation, 0, LA_EXTRA_TO_PANEL);
  1363. laCreateOperatorType("M_scale", "Scale", "Scale selected things", OPCHK_ViewportAndSceneExists, 0, 0, OPINV_Scale, OPMOD_Transformation, 0, LA_EXTRA_TO_PANEL);
  1364. laCreateOperatorType("M_rotate", "Rotate", "Rotation selected things", OPCHK_ViewportAndSceneExists, 0, 0, OPINV_Rotate, OPMOD_Transformation, 0, LA_EXTRA_TO_PANEL);
  1365. 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);
  1366. at->UiDefine = laui_MakeParent;
  1367. at=laCreateOperatorType("M_unparent", "Unparent", "Unparent selected objects", 0, 0, 0, OPINV_MakeParent, OPMOD_FinishOnData, 0, 0);
  1368. at->UiDefine = laui_Unparent;
  1369. laCreateOperatorType("M_clear_transformations", "Clear Transformations", "Clear transformations in objects", 0, 0, 0, OPINV_ClearTransformation, 0, 0, 0);
  1370. laCreateOperatorType("M_extrude", "Extrude", "Extrude parts of the mesh", 0, 0, 0, OPINV_Extrude, OPMOD_Transformation, 0, 0);
  1371. at=laCreateOperatorType("M_delete", "Delete", "Delete parts of the mesh", 0, 0, 0, OPINV_Delete, OPMOD_FinishOnData, 0, 0);
  1372. at->UiDefine=laui_Delete;
  1373. laCreateOperatorType("M_make", "Make", "Make mesh primitive from selected ones", 0, 0, 0, OPINV_Make, 0, 0, 0);
  1374. laCreateOperatorType("M_subdiv", "Subdiv", "Subdivide edges", 0, 0, 0, OPINV_Subdiv, 0, 0, 0);
  1375. at=laCreateOperatorType("M_add", "Add", "Add mesh or primitives", 0, 0, 0, OPINV_Add, OPMOD_FinishOnData, 0, 0);
  1376. at->UiDefine=laui_Add;
  1377. laCreateOperatorType("M_separate", "Separate", "Separate mesh parts", 0, 0, 0, OPINV_Separate, 0, 0, 0);
  1378. laCreateOperatorType("M_combine", "Combine", "Combine mesh objects", 0, 0, 0, OPINV_Combine, 0, 0, 0);
  1379. laCreateOperatorType("M_duplicate", "Duplicate", "Duplicate objects", 0, 0, 0, OPINV_Duplicate, OPMOD_Transformation, 0, 0);
  1380. laCreateOperatorType("M_recalculate_normals", "Recalculate Normals", "Recalculate normals", 0, 0, 0, OPINV_RecalculateNormals, 0, 0, 0);
  1381. laCreateOperatorType("M_knife", "Knife", "Cut through edges", OPCHK_ViewportAndSceneExists, 0, 0, OPINV_Knife, OPMOD_Knife, 0, LA_EXTRA_TO_PANEL);
  1382. }