*/}}
1
0

la_modelling.c 94 KB

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