*/}}

la_modelling.c 105 KB

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