*/}}

la_tns_drivers.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. extern LA MAIN;
  20. extern struct _tnsMain *T;
  21. laBaseNodeType TNS_IDN_TRANSFORM;
  22. laBaseNodeType TNS_IDN_MAKE_TRANSFORM;
  23. laPropContainer* TNS_PC_IDN_TRANSFORM;
  24. laPropContainer* TNS_PC_IDN_MAKE_TRANSFORM;
  25. void IDN_TransformInit(tnsTransformNode* n, int NoCreate){
  26. if(NoCreate){return;}
  27. n->Mat=laCreateInSocket("MAT",0); strSafeSet(&n->Base.Name,"Transform");
  28. }
  29. void IDN_TransformDestroy(tnsTransformNode* n){
  30. laDestroyInSocket(n->Mat); strSafeDestroy(&n->Base.Name);
  31. }
  32. int IDN_TransformVisit(tnsTransformNode* n, laNodeVisitInfo* vi){
  33. if(LA_SRC_AND_PARENT(n->Mat)){ laBaseNode*bn=n->Mat->Source->Parent; LA_VISIT_NODE(bn,vi); }
  34. LA_ADD_THIS_NODE(n,vi);
  35. return LA_DAG_FLAG_PERM;
  36. }
  37. int IDN_TransformEval(tnsTransformNode* n){
  38. if(!n->Target) return 0;
  39. if((!n->Mat->Source) || (n->Mat->Source->DataType!=(LA_PROP_FLOAT|LA_PROP_ARRAY)) || (n->Mat->Source->ArrLen!=16)){
  40. tnsLoadIdentity44d(n->Target->DeltaTransform); tnsSelfMatrixChanged(n->Target, 1);
  41. }else{
  42. memcpy(n->Target->DeltaTransform, n->Mat->Source->Data, sizeof(tnsMatrix44d)); tnsSelfMatrixChanged(n->Target, 1);
  43. }
  44. laNotifyInstanceUsers(n->Target);
  45. return 1;
  46. }
  47. void IDN_TransformCopy(tnsTransformNode* new, tnsTransformNode* old, int DoRematch){
  48. if(DoRematch){ LA_IDN_NEW_LINK(Mat) return; }
  49. memAssignRef(new,&new->Target,old->Target);
  50. }
  51. void tnsui_TransformNode(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
  52. laColumn* c=laFirstColumn(uil); tnsTransformNode*n=This->EndInstance;
  53. LA_BASE_NODE_HEADER(uil,c,This);
  54. laColumn* cl,*cr; laSplitColumn(uil,c,0.3); cl=laLeftColumn(c,0); cr=laRightColumn(c,0);
  55. laUiItem* b=laBeginRow(uil,cl,0,0);
  56. laShowNodeSocket(uil,cl,This,"mat",0)->Flags|=LA_UI_SOCKET_LABEL_E;
  57. laEndRow(uil,b);
  58. laShowItem(uil,cr,This,"target");
  59. }
  60. void IDN_MakeTransformInit(tnsMakeTransformNode* n, int NoCreate){
  61. if(NoCreate){return;}
  62. n->Out=laCreateOutSocket(n,"MAT",0); strSafeSet(&n->Base.Name,"Make Transform");
  63. n->Loc=laCreateInSocket("LOC",0);
  64. n->Rot=laCreateInSocket("ROT",0); n->Angle=laCreateInSocket("ANGLE",0); n->UseRot[2]=1;
  65. n->Sca=laCreateInSocket("SCALE",0); n->UseSca=1;
  66. }
  67. void IDN_MakeTransformDestroy(tnsMakeTransformNode* n){
  68. laDestroyOutSocket(n->Out); strSafeDestroy(&n->Base.Name);
  69. laDestroyInSocket(n->Loc);
  70. laDestroyInSocket(n->Rot); laDestroyInSocket(n->Angle);
  71. laDestroyInSocket(n->Sca);
  72. }
  73. int IDN_MakeTransformVisit(tnsMakeTransformNode* n, laNodeVisitInfo* vi){
  74. LA_GUARD_THIS_NODE(n,vi);
  75. if(LA_SRC_AND_PARENT(n->Loc)){ laBaseNode*bn=n->Loc->Source->Parent; LA_VISIT_NODE(bn,vi); }
  76. if(LA_SRC_AND_PARENT(n->Rot)){ laBaseNode*bn=n->Rot->Source->Parent; LA_VISIT_NODE(bn,vi); }
  77. if(LA_SRC_AND_PARENT(n->Sca)){ laBaseNode*bn=n->Sca->Source->Parent; LA_VISIT_NODE(bn,vi); }
  78. if(LA_SRC_AND_PARENT(n->Angle)){ laBaseNode*bn=n->Angle->Source->Parent; LA_VISIT_NODE(bn,vi); }
  79. LA_ADD_THIS_NODE(n,vi);
  80. return LA_DAG_FLAG_PERM;
  81. }
  82. int IDN_MakeTransformEval(tnsMakeTransformNode* n){
  83. tnsMatrix44d mm,mt,mr,ms; real* UseT,*UseR,*UseA,*UseS; tnsVector3d nr;
  84. if(LA_SRC_AND_PARENT(n->Loc) && n->Loc->Source->ArrLen>=3 && n->Loc->Source->DataType==LA_PROP_FLOAT|LA_PROP_ARRAY){ UseT=n->Loc->Source->Data; }else{ UseT=n->UseLoc; }
  85. if(LA_SRC_AND_PARENT(n->Rot) && n->Rot->Source->ArrLen>=3 && n->Rot->Source->DataType==LA_PROP_FLOAT|LA_PROP_ARRAY){ UseR=n->Rot->Source->Data; }else{ UseR=n->UseRot; }
  86. if(LA_SRC_AND_PARENT(n->Angle) && n->Angle->Source->DataType&LA_PROP_FLOAT){ UseA=n->Angle->Source->Data; }else{ UseA=&n->UseAngle; }
  87. if(LA_SRC_AND_PARENT(n->Sca) && n->Sca->Source->DataType&LA_PROP_FLOAT){ UseS=n->Sca->Source->Data; }else{ UseS=&n->UseSca; }
  88. tnsMakeTranslationMatrix44d(mt,LA_COLOR3(UseT));
  89. tnsNormalize3d(nr, UseR); tnsMakeRotationMatrix44d(mr,*UseA, LA_COLOR3(nr));
  90. tnsMakeScaleMatrix44d(ms,*UseS,*UseS,*UseS);
  91. tnsMultiply44d(mm,mt,mr); tnsMultiply44d(n->Mat,mm,ms);
  92. n->Out->ArrLen=16; n->Out->Data=n->Mat; n->Out->DataType=LA_PROP_FLOAT|LA_PROP_ARRAY;
  93. return 1;
  94. }
  95. void IDN_MakeTransformCopy(tnsMakeTransformNode* new, tnsMakeTransformNode* old, int DoRematch){
  96. if(DoRematch){ LA_IDN_NEW_LINK(Loc) LA_IDN_NEW_LINK(Rot) LA_IDN_NEW_LINK(Sca) LA_IDN_NEW_LINK(Angle) return; }
  97. old->Out->Duplicated=new->Out; new->UseSca=old->UseSca; new->UseAngle=old->UseAngle;
  98. tnsVectorSet3v(new->UseLoc,old->UseLoc); tnsVectorSet3v(new->UseRot,old->UseRot);
  99. }
  100. void tnsui_MakeTransformNode(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
  101. laColumn* c=laFirstColumn(uil); tnsMakeTransformNode*n=This->EndInstance;
  102. laUiItem* b2, *rui;
  103. LA_BASE_NODE_HEADER(uil,c,This);
  104. laUiItem* b=laBeginRow(uil,c,0,0);
  105. laShowNodeSocket(uil,c,This,"loc",0)->Flags|=LA_UI_SOCKET_LABEL_E; b2=laOnConditionThat(uil,c,laNot(laPropExpression(This,"loc.source")));{
  106. laShowItem(uil,c,This,"use_loc")->Expand=1;
  107. }laEndCondition(uil,b2);
  108. laEndRow(uil,b);
  109. b=laBeginRow(uil,c,0,0);
  110. laShowNodeSocket(uil,c,This,"rot",0)->Flags|=LA_UI_SOCKET_LABEL_E; b2=laOnConditionThat(uil,c,laNot(laPropExpression(This,"rot.source")));{
  111. laShowItem(uil,c,This,"use_rot")->Expand=1;
  112. }laEndCondition(uil,b2);
  113. laEndRow(uil,b);
  114. b=laBeginRow(uil,c,0,0);
  115. laShowNodeSocket(uil,c,This,"angle",0)->Flags|=LA_UI_SOCKET_LABEL_E; b2=laOnConditionThat(uil,c,laNot(laPropExpression(This,"angle.source")));{
  116. laShowItem(uil,c,This,"use_angle")->Expand=1;
  117. }laEndCondition(uil,b2);
  118. laEndRow(uil,b);
  119. b=laBeginRow(uil,c,0,0);
  120. laShowNodeSocket(uil,c,This,"sca",0)->Flags|=LA_UI_SOCKET_LABEL_E; b2=laOnConditionThat(uil,c,laNot(laPropExpression(This,"sca.source")));{
  121. laShowItem(uil,c,This,"use_sca")->Expand=1;
  122. }laEndCondition(uil,b2);
  123. laEndRow(uil,b);
  124. b=laBeginRow(uil,c,0,0);
  125. laShowSeparator(uil,c)->Expand=1;;
  126. laShowNodeSocket(uil,c,This,"out",0)->Flags|=LA_UI_SOCKET_LABEL_W;
  127. laEndRow(uil,b);
  128. }
  129. int OPCHK_AddDriverPage(laPropPack *This, laStringSplitor *ss){
  130. if (This && la_EnsureSubTarget(This->LastPs->p,0) == TNS_PC_OBJECT_GENERIC) return 1;
  131. return 0;
  132. }
  133. int OPINV_AddDriverPage(laOperator* a, laEvent *e){
  134. tnsObject* ob=a->This?a->This->EndInstance:0; if(!ob || !ob->Drivers) return LA_CANCELED;
  135. laRackPage* dp=memAcquire(sizeof(laRackPage));
  136. strSafeSet(&dp->Name,"New Page");
  137. lstAppendItem(&ob->Drivers->Pages, dp);
  138. memAssignRef(dp,&dp->ParentObject,ob); memAssignRef(ob->Drivers,&ob->Drivers->CurrentPage,dp);
  139. dp->RackType=LA_RACK_TYPE_DRIVER;
  140. laNotifyInstanceUsers(ob->Drivers); laRecordAndPush(a->This,"","Add driver page", 0);
  141. return LA_FINISHED;
  142. }
  143. int OPCHK_RemoveDriverPage(laPropPack *This, laStringSplitor *ss){
  144. if ((!This) || (la_EnsureSubTarget(This->LastPs->p,0) != LA_PC_RACK_PAGE)) return 0;
  145. laRackPage* dp=This->EndInstance; if(!dp->ParentObject) return 0;
  146. return 1;
  147. }
  148. int OPINV_RemoveDriverPage(laOperator* a, laEvent *e){
  149. laRackPage* dp=a->This?a->This->EndInstance:0; if(!dp || !dp->ParentObject) return LA_CANCELED;
  150. tnsObject* ob=dp->ParentObject;
  151. if(strSame(strGetArgumentString(a->ExtraInstructionsP,"confirm"),"true")){
  152. strSafeDestroy(&dp->Name);
  153. while(dp->Racks.pFirst){ laDestroyRack(dp->Racks.pFirst); }
  154. while(lstPopPointer(&dp->Eval)); while(lstPopPointer(&dp->AlwaysBranchers));
  155. if(dp->ParentObject->Drivers->CurrentPage==dp){
  156. laRackPage* adp=dp->Item.pNext?dp->Item.pNext:dp->Item.pPrev;
  157. memAssignRef(dp->ParentObject->Drivers,&dp->ParentObject->Drivers->CurrentPage,adp);
  158. }laNotifyInstanceUsers(dp);
  159. lstRemoveItem(&dp->ParentObject->Drivers->Pages, dp); memLeave(dp);
  160. laNotifyInstanceUsers(ob->Drivers); laRecordAndPush(a->This,"","Remove driver page", 0);
  161. return LA_FINISHED;
  162. }
  163. laEnableOperatorPanel(a,a->This,e->x,e->y,200,200,0,0,0,0,0,0,0,0,e);
  164. return LA_RUNNING;
  165. }
  166. void laui_RemoveDriverPage(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
  167. laColumn* c=laFirstColumn(uil);
  168. laShowItemFull(uil,c,This,"remove_driver_page",0,"confirm=true;text=Confirm",0,0);
  169. }
  170. int OPINV_RebuildDrivers(laOperator* a, laEvent *e){
  171. laGraphRequestRebuild();
  172. return LA_FINISHED;
  173. }
  174. tnsObject* tnsget_FirstObject(void* unused, void* unused2){
  175. return T->World->AllObjects.pFirst;
  176. }
  177. void tns_RegisterNodes(){
  178. laPropContainer *pc; laProp *p;
  179. laOperatorType *at;
  180. laEnumProp *ep;
  181. laCreateOperatorType("LA_add_driver_page", "New Page", "Add a driver page",OPCHK_AddDriverPage,0,0,OPINV_AddDriverPage,0,'+',0);
  182. laCreateOperatorType("LA_remove_driver_page", "Remove Page", "Remove a driver page",OPCHK_RemoveDriverPage,0,0,OPINV_RemoveDriverPage,OPMOD_FinishOnData,L'🗴',0)
  183. ->UiDefine=laui_RemoveDriverPage;
  184. laCreateOperatorType("LA_driver_rebuild", "Rebuild Drivers", "Rebuild drivers for evaluation",0,0,0,OPINV_RebuildDrivers,0,U'⭮',0);
  185. LA_NODE_CATEGORY_DRIVER=laAddNodeCategory("Driver",0,LA_RACK_TYPE_DRIVER);
  186. if(!MAIN.InitArgs.HasWorldObjects) return;
  187. pc=laAddPropertyContainer("tns_transform_node", "Transform", "Transform objects",0,tnsui_TransformNode,sizeof(tnsTransformNode),lapost_Node,0,1);
  188. TNS_PC_IDN_TRANSFORM=pc; laPropContainerExtraFunctions(pc,0,0,0,0,laui_DefaultNodeOperationsPropUiDefine);
  189. laAddSubGroup(pc,"base","Base","Base node","la_base_node",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
  190. laAddSubGroup(pc,"mat", "Mat","Input matrix","la_in_socket",0,0,0,offsetof(tnsTransformNode,Mat),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  191. laAddSubGroup(pc,"target", "Target","Target object","tns_object",0,LA_WIDGET_COLLECTION_SELECTOR,laui_IdentifierOnly,offsetof(tnsTransformNode,Target),tnsget_FirstObject,0,laget_ListNext,0,0,0,0,LA_UDF_REFER);
  192. pc=laAddPropertyContainer("tns_make_transform_node", "Make Transform", "Make Transform matrix",0,tnsui_MakeTransformNode,sizeof(tnsMakeTransformNode),lapost_Node,0,1);
  193. TNS_PC_IDN_MAKE_TRANSFORM=pc; laPropContainerExtraFunctions(pc,0,0,0,0,laui_DefaultNodeOperationsPropUiDefine);
  194. laAddSubGroup(pc,"base","Base","Base node","la_base_node",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
  195. laAddSubGroup(pc,"out", "Out","Output matrix","la_out_socket",0,0,0,offsetof(tnsMakeTransformNode,Out),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  196. laAddSubGroup(pc,"loc", "Location","Location","la_in_socket",0,0,0,offsetof(tnsMakeTransformNode,Loc),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  197. laAddSubGroup(pc,"rot", "Rotation","Rotation","la_in_socket",0,0,0,offsetof(tnsMakeTransformNode,Rot),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  198. laAddSubGroup(pc,"sca", "Scale","Scale","la_in_socket",0,0,0,offsetof(tnsMakeTransformNode,Sca),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  199. laAddSubGroup(pc,"angle", "Angle","Rotation Angle","la_in_socket",0,0,0,offsetof(tnsMakeTransformNode,Angle),0,0,0,0,0,0,0,LA_UDF_SINGLE);
  200. laAddFloatProperty(pc,"use_loc", "Location", "Use Location",0,"X,Y,Z",0,0,0,0.05,0,0,offsetof(tnsMakeTransformNode, UseLoc),0,0,3,0,0,0,0,0,0,0,0);
  201. laAddFloatProperty(pc,"use_rot", "Rotation", "Use Rotation",0,"X,Y,Z",0,0,0,0.05,0,0,offsetof(tnsMakeTransformNode, UseRot),0,0,3,0,0,0,0,0,0,0,0);
  202. laAddFloatProperty(pc,"use_sca", "Scale", "Use Scale",0,0,0,0,0,0.05,0,0,offsetof(tnsMakeTransformNode, UseSca),0,0,0,0,0,0,0,0,0,0,0);
  203. laAddFloatProperty(pc,"use_angle", "Angle", "Use Angle",0,0,0,0,0,0.05,0,0,offsetof(tnsMakeTransformNode, UseAngle),0,0,0,0,0,0,0,0,0,0,0);
  204. LA_IDN_REGISTER("Transform",0,TNS_IDN_TRANSFORM,TNS_PC_IDN_TRANSFORM, IDN_Transform, tnsTransformNode);
  205. LA_IDN_REGISTER("Make Transform",0,TNS_IDN_MAKE_TRANSFORM,TNS_PC_IDN_MAKE_TRANSFORM, IDN_MakeTransform, tnsMakeTransformNode);
  206. laNodeCategoryAddNodeTypes(LA_NODE_CATEGORY_MATH, &TNS_IDN_MAKE_TRANSFORM,0);
  207. laNodeCategoryAddNodeTypes(LA_NODE_CATEGORY_DRIVER, &TNS_IDN_TRANSFORM,0);
  208. }