*/}}

la_modelling.c 67 KB

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