*/}}

la_modelling.c 110 KB

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