*/}}
1
0

la_modelling.c 105 KB

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