*/}}

la_modelling.c 109 KB

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