*/}}

la_modelling.c 66 KB

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