*/}}

la_animation.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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 tnsMain* T;
  21. int la_GetKeyablePropertyStorageSize(laProp* p);
  22. void la_AnimationEvaluateActions(int ClampOffsets);
  23. void laget_AnimationActionHolderCategory(void* a_unused, laActionHolder* ah, char* copy, char** ptr);
  24. void laui_AnimationActionHolder(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *ExtraColumns, int context);
  25. int laAnimationRegisterHolderPath(const char* Path){
  26. laPropPack PP={0}; if(!la_GetPropFromPath(&PP,0,Path,0)){ la_FreePropStepCache(PP.Go); return 0; }
  27. la_EnsureSubTarget(PP.LastPs->p,0);
  28. if((!PP.LastPs->p->SubProp) || (!la_PropLookup(&PP.LastPs->p->SubProp->Props,"__actions__"))){
  29. la_FreePropStepCache(PP.Go); return 0;
  30. }
  31. laActionHolderPath* ahp=memAcquire(sizeof(laActionHolderPath));
  32. ahp->OriginalPath=Path;
  33. la_CopyPropPack(&PP,&ahp->PP); lstAppendItem(&MAIN.Animation->ActionHolderPaths,ahp);
  34. la_FreePropStepCache(PP.Go);
  35. return 1;
  36. }
  37. void laAnimationUpdateHolderList(){
  38. laActionHolder* ah;
  39. while(ah=lstPopItem(&MAIN.Animation->ActionHolders)){
  40. strSafeDestroy(&ah->Name); strSafeDestroy(&ah->CategoryTitle); memFree(ah);
  41. }
  42. for(laActionHolderPath* ahp=MAIN.Animation->ActionHolderPaths.pFirst;ahp;ahp=ahp->Item.pNext){
  43. la_StepPropPack(&ahp->PP); laSubProp* pa=ahp->PP.LastPs->p; la_EnsureSubTarget(pa,0); laPropIterator pi={0};
  44. int ListOffset=0; laSubProp* paa=la_PropLookup(&pa->Base.SubProp->Props,"__actions__");
  45. if((!paa) || (!paa->ListHandleOffset)){ continue; } ListOffset=paa->ListHandleOffset;
  46. void* inst=laGetInstance(ahp->PP.LastPs->p,ahp->PP.LastPs->UseInstance,&pi); int FirstIn=1;
  47. while(inst){
  48. ah=memAcquire(sizeof(laActionHolder));
  49. memAssignRef(ah,&ah->Instance,inst);
  50. ah->ListHandleOffset=ListOffset;
  51. ah->Container=ahp->PP.LastPs->p->SubProp;
  52. char _id[64]="unamed", *id=_id;
  53. laTryGetInstanceIdentifier(inst,pa->Base.SubProp,_id,&id);
  54. strSafeSet(&ah->Name,id);
  55. if(FirstIn){ strSafeSet(&ah->CategoryTitle,pa->Base.SubProp->Name); FirstIn=0; }
  56. lstAppendItem(&MAIN.Animation->ActionHolders,ah);
  57. inst=laGetNextInstance(ahp->PP.LastPs->p, inst, &pi);
  58. }
  59. }
  60. laNotifyUsers("la.animation.action_holders");
  61. }
  62. laAction* laAnimiationNewAction(laActionHolder* ah, char* Name){
  63. laAction* aa=memAcquire(sizeof(laAction));
  64. if(!Name || !Name[0]){ Name="New Action"; }
  65. strSafeSet(&aa->Name,Name);
  66. aa->Length=2; aa->FrameCount=24; aa->HolderContainer=ah->Container;
  67. memAssignRef(aa,&aa->HolderInstance,ah->Instance);
  68. memAssignRef(MAIN.Animation,&MAIN.Animation->CurrentAction,aa);
  69. void* lh=((uint8_t*)ah->Instance)+ah->ListHandleOffset;
  70. lstAppendItem(lh,aa);
  71. laNotifyInstanceUsers(ah->Instance);
  72. laNotifyUsers("la.animation.current_action");
  73. return aa;
  74. }
  75. laActionProp* laAnimationEnsureProp(void* hyper1, laProp* p){
  76. int DataSize=la_GetKeyablePropertyStorageSize(p); if(!DataSize) return 0;
  77. for(laActionProp* ap=MAIN.Animation->Props.pFirst;ap;ap=ap->Item.pNext){
  78. if(ap->For==hyper1 && ap->Prop==p){ return ap; }
  79. }
  80. laActionProp* ap = memAcquire(sizeof(laActionProp)); lstAppendItem(&MAIN.Animation->Props,ap);
  81. memAssignRef(ap,&ap->For,hyper1); ap->Prop=p;
  82. ap->DataSize = DataSize;
  83. char _name[128]={0}, *name=_name; laTryGetInstanceIdentifier(hyper1, p->Container,_name,&name);
  84. strSafeSet(&ap->CachedName,name); strSafePrint(&ap->CachedName," ⯈ %s",p->Identifier);
  85. return ap;
  86. }
  87. laActionChannel* laAnimationEnsureChannel(laAction* aa, void* hyper1, laProp* p){
  88. laActionProp* ap=laAnimationEnsureProp(hyper1,p); if(!ap) return 0; laActionChannel* ac;
  89. for(ac=aa->Channels.pFirst;ac;ac=ac->Item.pNext){
  90. if(ac->AP==ap) return ac;
  91. }
  92. ac=memAcquire(sizeof(laActionChannel)); ac->AP=ap;
  93. lstAppendItem(&aa->Channels,ac); laNotifyUsers("la.animation.current_action.channels");
  94. return ac;
  95. }
  96. laActionChannel* laAnimationEnsureFrame(laActionChannel* ac, int frame){
  97. laActionKey* akf=0,*beforeakf=0;
  98. for(laActionKey* ak=ac->Keys.pFirst;ak;ak=ak->Item.pNext){
  99. if(ak->At==frame){ akf=ak; break; } if(ak->At>frame){ beforeakf=ak; break; }
  100. }
  101. if(!akf){
  102. akf=memAcquireSimple(sizeof(laActionKey)-sizeof(uint64_t)+ac->AP->DataSize);
  103. akf->At=frame;
  104. if(beforeakf){ lstInsertItemBefore(&ac->Keys, akf, beforeakf); }
  105. else{ lstAppendItem(&ac->Keys, akf); }
  106. }
  107. return akf;
  108. }
  109. void laAnimationStoreKeyValue(laActionChannel* ac, laActionKey* ak){
  110. laPropPack PP={0}; laPropStep PS={0}; laActionProp* ap=ac->AP; if(!ap) return;
  111. PS.p=ap->Prop; PS.Type='.'; PS.UseInstance=ap->For; PP.LastPs=&PS; PP.EndInstance=ap->For;
  112. switch(ap->Prop->PropertyType){
  113. case LA_PROP_INT: case LA_PROP_INT | LA_PROP_ARRAY: laGetIntArray(&PP,(int*)&ak->Data); break;
  114. case LA_PROP_FLOAT: case LA_PROP_FLOAT | LA_PROP_ARRAY: laGetFloatArray(&PP,(real*)&ak->Data); break;
  115. case LA_PROP_ENUM: case LA_PROP_ENUM | LA_PROP_ARRAY: laGetEnumArray(&PP,(laEnumItem**)&ak->Data); break;
  116. case LA_PROP_SUB: case LA_PROP_OPERATOR: case LA_PROP_STRING: case LA_PROP_RAW: default: return;
  117. }
  118. }
  119. laActionKey* laAnimationInsertKeyFrame(laAction* aa, void* hyper1, laProp* p, int* error){
  120. if(error) *error=0; if(!aa) return 0;
  121. if(!aa->HolderInstance||!aa->HolderContainer){ if(error) *error=1; return 0; }
  122. if(aa->HolderContainer->ActionHolderVerify){
  123. if(!aa->HolderContainer->ActionHolderVerify(aa->HolderInstance,aa->HolderContainer,hyper1,p->Container)){
  124. if(error) *error=2; return 0;
  125. }
  126. }
  127. laActionChannel* ac=laAnimationEnsureChannel(aa,hyper1,p); if(!ac || !ac->AP || !ac->AP->For) return;
  128. int frame=LA_ACTION_FRAME(aa);
  129. laActionKey* ak=laAnimationEnsureFrame(ac,frame);
  130. laAnimationStoreKeyValue(ac,ak);
  131. laNotifyUsers("la.animation.current_action");
  132. return ak;
  133. }
  134. void laAnimationSetPlayStatus(int PlayStatus){
  135. if(PlayStatus>3||PlayStatus<0) PlayStatus=0; MAIN.Animation->PlayStatus=PlayStatus;
  136. if(PlayStatus) laRecordTime(&MAIN.Animation->TimeOrigin);
  137. laNotifyUsers("la.animation.play_status");
  138. }
  139. void laAnimationSetPlayHead(real time){
  140. MAIN.Animation->PlayHead=time;
  141. }
  142. STRUCTURE(laNewActionData){
  143. laActionHolder* SelectedHolder;
  144. };
  145. laActionHolder* laget_AnimationFirstActionHolder(void* unused1, void* unused2){
  146. return MAIN.Animation->ActionHolders.pFirst;
  147. }
  148. void laset_AnimationNewActionSetHolder(laNewActionData *np, laActionHolder *ah, int State){
  149. np->SelectedHolder = ah;
  150. }
  151. int OPINV_AnimationNewAction(laOperator *a, laEvent *e){
  152. laAnimationUpdateHolderList();
  153. laNewActionData* np= CreateNew(laNewActionData);
  154. a->CustomData = np;
  155. laEnableOperatorPanel(a, 0, e->x-50,e->y-50,500,500,10000,0,0,0,0,0,0,0,e);
  156. return LA_RUNNING;
  157. }
  158. int OPMOD_AnimationNewAction(laOperator *a, laEvent *e){
  159. laNewActionData* np=a->CustomData;
  160. if(!a->ConfirmData) return LA_RUNNING;
  161. if(a->ConfirmData->Mode == LA_CONFIRM_CANCEL||a->ConfirmData->Mode == LA_CONFIRM_OK){ if(np) free(np); return LA_CANCELED; }
  162. if(a->ConfirmData->Mode == LA_CONFIRM_DATA){
  163. if (!np || !np->SelectedHolder){ if(np) free(np); return LA_CANCELED; }
  164. laAnimiationNewAction(np->SelectedHolder,0); free(np);
  165. return LA_FINISHED;
  166. }
  167. return LA_RUNNING;
  168. }
  169. void laui_AnimationNewAction(laUiList *uil, laPropPack *This, laPropPack *OperatorProps, laColumn *UNUSED, int context){
  170. laColumn *c = laFirstColumn(uil);
  171. laUiItem* ui=laShowItem(uil, c, OperatorProps, "holder");ui->SymbolID=3; ui->Flags|=LA_UI_FLAGS_NO_DECAL;
  172. }
  173. int OPINV_AnimationSelectAction(laOperator *a, laEvent *e){
  174. laEnableOperatorPanel(a, 0, e->x-50,e->y-50,500,500,10000,0,0,0,0,0,0,0,e);
  175. return LA_RUNNING;
  176. }
  177. void laui_AnimationSelectAction(laUiList *uil, laPropPack *This, laPropPack *OperatorProps, laColumn *UNUSED, int context){
  178. laColumn *c = laFirstColumn(uil);
  179. for(laActionHolderPath* ahp=MAIN.Animation->ActionHolderPaths.pFirst;ahp;ahp=ahp->Item.pNext){
  180. laShowLabel(uil,c,ahp->PP.LastPs->p->Name,0,0)->Flags|=LA_UI_FLAGS_DISABLED|LA_TEXT_MONO;
  181. laShowItemFull(uil,c,0,ahp->OriginalPath,LA_WIDGET_COLLECTION,"feedback=NONE",laui_AnimationActionHolder,0)->Flags|=LA_UI_FLAGS_NO_DECAL|LA_UI_FLAGS_NO_GAP;
  182. }
  183. }
  184. int OPINV_AnimationPlayAction(laOperator *a, laEvent *e){
  185. char* str=strGetArgumentString(a->ExtraInstructionsP, "mode");
  186. int PlayStatus=LA_ANIMATION_STATUS_PAUSED;
  187. if(strSame(str,"forward")){ PlayStatus=LA_ANIMATION_STATUS_PLAY_FWD; }
  188. elif(strSame(str,"reverse")){ if(MAIN.Animation->PlayHead>0) PlayStatus=LA_ANIMATION_STATUS_PLAY_REV; }
  189. laAnimationSetPlayStatus(PlayStatus);
  190. return LA_FINISHED;
  191. }
  192. void la_AnimationActionSetOwnFrame(laAction* aa, int frame){
  193. if(!aa) return;
  194. aa->Offset+=(aa->PlayHead-(real)frame/aa->FrameCount)*aa->Length-1e-4;
  195. la_AnimationEvaluateActions(0);
  196. laNotifyUsers("la.animation.current_action");
  197. }
  198. int OPINV_AnimationResetTime(laOperator *a, laEvent *e){
  199. char* arg=strGetArgumentString(a->ExtraInstructionsP,"current");
  200. if(strSame(arg,"true")){ la_AnimationActionSetOwnFrame(MAIN.Animation->CurrentAction,0); return LA_FINISHED; }
  201. laAnimationSetPlayHead(0); la_AnimationEvaluateActions(1);
  202. return LA_FINISHED;
  203. }
  204. laActionChannel* laAnimationGetFrame(laActionChannel* ac, int frame){
  205. if(ac->Keys.pFirst==ac->Keys.pLast){ return ac->Keys.pFirst; }
  206. for(laActionKey* ak=ac->Keys.pFirst;ak;ak=ak->Item.pNext){
  207. if(ak->At<=frame && ((!ak->Item.pNext) ||ak->Item.pNext && ((laActionKey*)ak->Item.pNext)->At>frame)){ return ak; }
  208. }
  209. return ac->Keys.pFirst;
  210. }
  211. void la_AnimationMarkPropReset(){
  212. for(laActionProp* ap=MAIN.Animation->Props.pFirst;ap;ap=ap->Item.pNext){ ap->Reset=1; }
  213. }
  214. void la_AnimationInterpolateKeys(laActionChannel* ac, laActionKey* ak1, laActionKey* ak2, real PlayHead_mul_Length, void** data){
  215. int* id1,*id2,*iret=(*data); real* fd1,*fd2,*fret=(*data);
  216. if(!ak2){ *data=&ak1->Data; return; } laActionProp* ap=ac->AP; if(!ap) return;
  217. int arrlen=ap->Prop->Len?ap->Prop->Len:1;
  218. real fac=tnsGetRatiod(ak1->At,ak2->At,PlayHead_mul_Length); TNS_CLAMP(fac,0,1);
  219. switch(ap->Prop->PropertyType){
  220. case LA_PROP_INT: case LA_PROP_INT|LA_PROP_ARRAY:
  221. id1=&ak1->Data;id2=&ak2->Data; for(int i=0;i<arrlen;i++){ iret[i]=tnsLinearItp(id1[i],id2[i],fac); } break;
  222. case LA_PROP_FLOAT: case LA_PROP_FLOAT|LA_PROP_ARRAY:
  223. fd1=&ak1->Data;fd2=&ak2->Data; for(int i=0;i<arrlen;i++){ fret[i]=tnsLinearItp(fd1[i],fd2[i],fac); } break;
  224. default:
  225. *data=ak1->Data; break;
  226. }
  227. }
  228. void la_AnimationSetPropValue(laActionProp* ap){
  229. void* data=ap->Data;
  230. laPropPack PP={0}; laPropStep PS={0}; if(!ap) return;
  231. PS.p=ap->Prop; PS.Type='.'; PS.UseInstance=ap->For; PP.LastPs=&PS; PP.EndInstance=ap->For;
  232. switch(ap->Prop->PropertyType){
  233. case LA_PROP_INT: laSetInt(&PP,*((int*)data)); break;
  234. case LA_PROP_INT|LA_PROP_ARRAY: laSetIntArrayAllArray(&PP,(int*)data); break;
  235. case LA_PROP_FLOAT: laSetFloat(&PP,*((real*)data)); break;
  236. case LA_PROP_FLOAT|LA_PROP_ARRAY: laSetFloatArrayAllArray(&PP,(real*)data); break;
  237. case LA_PROP_ENUM: laSetEnum(&PP,*((int*)data)); break;
  238. //case LA_PROP_ENUM|LA_PROP_ARRAY: laSetEnumArrayAllArray(&PP,(real*)data); break; //doesnt work
  239. default: break;
  240. }
  241. }
  242. void la_AnimationMixChannelValue(laActionChannel* ac, void* data, int MixMode, real Factor){
  243. laActionProp* ap=ac->AP; if(!ap) return; int* ip=&ap->Data,*ipd=data; real* fp=&ap->Data,*fpd=data;
  244. int arrlen=ap->Prop->Len?ap->Prop->Len:1;
  245. switch(ap->Prop->PropertyType){
  246. case LA_PROP_INT: case LA_PROP_INT|LA_PROP_ARRAY: for(int i=0;i<arrlen;i++){
  247. ip[i]=ap->Reset?ipd[i]:(MixMode==LA_ANIMATION_MIX_REPLACE?ipd[i]:(ipd[i]+ip[i]));
  248. } break;
  249. case LA_PROP_FLOAT: case LA_PROP_FLOAT|LA_PROP_ARRAY: for(int i=0;i<arrlen;i++){
  250. fp[i]=ap->Reset?fpd[i]:(MixMode==LA_ANIMATION_MIX_REPLACE?fpd[i]:(fpd[i]+fp[i]));
  251. } break;
  252. case LA_PROP_ENUM: ip[0]=ipd[0]; break;
  253. //case LA_PROP_ENUM|LA_PROP_ARRAY: laSetEnumArrayAllArray(&PP,(real*)data); break; //doesnt work
  254. default: break;
  255. }
  256. ap->Reset=0;
  257. }
  258. void la_AnimationEvaluateActionChannels(laAction* aa){
  259. int64_t _data[16]; void* data=_data;
  260. int frame=LA_ACTION_FRAME(aa); real totframe=aa->PlayHead*aa->FrameCount;
  261. for(laActionChannel* ac=aa->Channels.pFirst;ac;ac=ac->Item.pNext){
  262. data=_data;
  263. laActionKey* ak1=laAnimationGetFrame(ac,frame); if(!ak1) continue;
  264. laActionKey* ak2=ak1->Item.pNext;
  265. la_AnimationInterpolateKeys(ac,ak1,ak2,totframe,&data);
  266. la_AnimationMixChannelValue(ac, data, aa->MixMode, 0);
  267. }
  268. }
  269. void la_AnimationEvaluateActions(int ClampOffsets){
  270. int any=0;
  271. la_AnimationMarkPropReset();
  272. for(laActionHolder* ah=MAIN.Animation->ActionHolders.pFirst;ah;ah=ah->Item.pNext){
  273. if(!ah->Instance) continue;
  274. laListHandle* lh=((uint8_t*)ah->Instance)+ah->ListHandleOffset;
  275. for(laAction* aa=lh->pFirst;aa;aa=aa->Item.pNext){
  276. real preoffset=0,postoffset=aa->Offset/aa->Length;
  277. if(ClampOffsets || (MAIN.Animation->PlayStatus!=LA_ANIMATION_STATUS_PAUSED) || (aa!=MAIN.Animation->CurrentAction)){
  278. while(aa->Offset>aa->Length){ aa->Offset-=aa->Length; }
  279. while(aa->Offset<-1e-6){ aa->Offset+=aa->Length; }
  280. preoffset=aa->Offset; postoffset=0;
  281. }
  282. real UseTime=MAIN.Animation->PlayHead-preoffset;
  283. int repeats=UseTime/aa->Length;
  284. real remaining=UseTime-repeats*aa->Length;
  285. while(remaining<0){ remaining+=aa->Length; }
  286. if(aa->PlayMode==LA_ANIMATION_PLAY_MODE_REPEAT){ aa->PlayHead=remaining/aa->Length-postoffset; }
  287. elif(aa->PlayMode==LA_ANIMATION_PLAY_MODE_HOLD){ aa->PlayHead=((UseTime>aa->Length)?1.0:(UseTime<0?0:UseTime/aa->Length))-postoffset; }
  288. elif(aa->PlayMode==LA_ANIMATION_PLAY_MODE_BOUNCE){ real t=remaining/aa->Length; aa->PlayHead=((repeats%2)?(1-t):t)-postoffset; }
  289. any=1;
  290. la_AnimationEvaluateActionChannels(aa);
  291. }
  292. }
  293. for(laActionProp* ap=MAIN.Animation->Props.pFirst;ap;ap=ap->Item.pNext){ if(ap->Reset){ continue; }
  294. la_AnimationSetPropValue(ap);
  295. }
  296. if(any) laNotifyUsers("la.animation");
  297. }
  298. void la_AnimationPreFrame(){
  299. if(MAIN.Animation->PlayHead<0){
  300. MAIN.Animation->PlayHead=0; laAnimationSetPlayStatus(0);
  301. la_AnimationEvaluateActions(1);
  302. laNotifyUsers("la.animation.play_head");
  303. }
  304. if(MAIN.Animation->PlayStatus){
  305. la_AnimationEvaluateActions(0);
  306. }
  307. }
  308. void la_AnimationPostFrame(){
  309. laTimeRecorder cur={0};
  310. if(MAIN.Animation->PlayStatus){ laRecordTime(&cur);
  311. real td=laTimeElapsedSecondsf(&cur,&MAIN.Animation->TimeOrigin);
  312. laRecordTime(&MAIN.Animation->TimeOrigin);
  313. if(MAIN.Animation->PlayStatus==LA_ANIMATION_STATUS_PLAY_REV){ td*=-1; }
  314. MAIN.Animation->PlayHead+=td; laNotifyUsers("la.animation.play_head");
  315. }
  316. }
  317. void la_ActionDeselectFrames(laAction* aa){
  318. for(laActionChannel* ac=aa->Channels.pFirst;ac;ac=ac->Item.pNext){
  319. for(laActionKey* ak=ac->Keys.pFirst;ak;ak=ak->Item.pNext){
  320. ak->Selected=0;
  321. }
  322. }
  323. }
  324. void la_ActionSelectFrame(laAction* aa, int Channel, real At, real zoomx){
  325. int tc=0;laActionChannel* ac;
  326. for(ac=aa->Channels.pFirst;ac;ac=ac->Item.pNext){ if(tc==Channel) break; tc++; }
  327. if(!ac) return;
  328. real ClosestDist=FLT_MAX; laActionKey* ClosestKey=0;
  329. for(laActionKey* ak=ac->Keys.pFirst;ak;ak=ak->Item.pNext){
  330. real d=fabs(ak->At-At+0.5); if(d*zoomx<LA_RH*2 && d<ClosestDist){ ClosestDist=d; ClosestKey=ak; }
  331. }
  332. if(ClosestKey){ ClosestKey->Selected=1; }
  333. }
  334. int la_ActionSaveKeyOriginalAt(laAction* aa){
  335. int any=0;
  336. for(laActionChannel* ac=aa->Channels.pFirst;ac;ac=ac->Item.pNext){
  337. for(laActionKey* ak=ac->Keys.pFirst;ak;ak=ak->Item.pNext){ if(!ak->Selected) continue; any=1; ak->OriginaAt=ak->At; }
  338. }
  339. return any;
  340. }
  341. void la_ActionRestoreKeyOriginalAt(laAction* aa){
  342. for(laActionChannel* ac=aa->Channels.pFirst;ac;ac=ac->Item.pNext){
  343. for(laActionKey* ak=ac->Keys.pFirst;ak;ak=ak->Item.pNext){ if(!ak->Selected) continue; ak->At=ak->OriginaAt; }
  344. }
  345. la_ActionEnsureFrameOrder(aa);
  346. }
  347. void la_ActionEnsureFrameOrder(laAction* aa){
  348. for(laActionChannel* ac=aa->Channels.pFirst;ac;ac=ac->Item.pNext){
  349. laListHandle lst={0};
  350. laActionKey* ak; while(ak=lstPopItem(&ac->Keys)){ int done=0;
  351. if(!lst.pFirst){ lstAppendItem(&lst,ak); continue; }
  352. laActionKey* ak2=0;
  353. for(ak2=lst.pFirst;ak2;ak2=ak2->Item.pNext){
  354. if(ak2->At>=ak->At){ lstInsertItemBefore(&lst,ak,ak2); done=1; break; }
  355. }
  356. if(!done) lstAppendItem(&lst,ak);
  357. }
  358. lstCopyHandle(&ac->Keys,&lst);
  359. }
  360. }
  361. void la_ActionRemoveDuplicatedFrames(laAction* aa){
  362. for(laActionChannel* ac=aa->Channels.pFirst;ac;ac=ac->Item.pNext){
  363. laActionKey* ak2; for(laActionKey* ak=ac->Keys.pFirst;ak;ak=ak->Item.pNext){
  364. while((ak2=ak->Item.pNext)&&ak->At==ak2->At){ lstRemoveItem(&ac->Keys,ak2); memLeave(ak2); }
  365. }
  366. }
  367. }
  368. void la_ActionMoveSelectedFrames(laAction* aa, int Delta){
  369. for(laActionChannel* ac=aa->Channels.pFirst;ac;ac=ac->Item.pNext){
  370. for(laActionKey* ak=ac->Keys.pFirst;ak;ak=ak->Item.pNext){ if(!ak->Selected) continue;
  371. ak->At=ak->OriginaAt+Delta;
  372. }
  373. }
  374. la_ActionEnsureFrameOrder(aa);
  375. }
  376. int la_ActionDeleteSelectedFrames(laAction* aa){
  377. int any=0;
  378. for(laActionChannel* ac=aa->Channels.pFirst;ac;ac=ac->Item.pNext){
  379. laActionKey* NextAk; for(laActionKey* ak=ac->Keys.pFirst;ak;ak=NextAk){ NextAk=ak->Item.pNext;if(!ak->Selected) continue;
  380. lstRemoveItem(&ac->Keys, ak); memLeave(ak); any=1;
  381. }
  382. }
  383. return any;
  384. }
  385. int LAMOD_AnimationActionsCanvas(laOperator *a, laEvent *e){
  386. laUiItem *ui = a->Instance; laBoxedTheme *bt = (*ui->Type->Theme);
  387. laCanvasExtra *ex = a->CustomData; laAction* aa=ui->PP.EndInstance;
  388. if(!aa) LA_RUNNING_PASS;
  389. int W, H; W = ui->R - ui->L; H = ui->B - ui->U;
  390. int ShowSide=(ex->ShowLegend&&ex->LW<W-2*LA_RH);
  391. int ll=ui->L, lr=ll+(ShowSide?ex->LW:0), tl=lr, tr=ui->R;
  392. int FW=LA_RH/2;
  393. int Channels=aa?lstCountElements(&aa->Channels):0;
  394. int Modal=0; int mid=((W-lr)/2);
  395. if(e->Type==LA_M_MOUSE_DOWN){
  396. ex->UiMode=1; ex->ClickedX=e->x; ex->ClickedY=e->y; Modal=1; ex->Dragging=10;
  397. }elif(e->Type==LA_M_MOUSE_UP || e->Type==LA_L_MOUSE_UP){
  398. ex->UiMode=0; Modal=1; ex->Dragging=0; ex->TargetIndexVali=0;
  399. }
  400. if(ex->UiMode==1){
  401. if(e->Type&LA_MOUSE_EVENT){
  402. ex->PanY-=e->y-ex->ClickedY; ex->PanX-=e->x-ex->ClickedX;
  403. ex->ClickedX=e->x; ex->ClickedY=e->y; Modal=1;
  404. }
  405. }elif(ex->UiMode==2){ Modal=1;
  406. if(e->Type&LA_MOUSE_EVENT){
  407. ex->LW=e->x-ui->L+LA_SEAM_W; ex->ClickedX=e->x; ex->ClickedY=e->y;
  408. if(ex->LW<LA_RH*3 && ex->LW>=LA_RH/2){ ex->LW=LA_RH*3; }
  409. if(ex->LW<LA_RH/2 && ex->Dragging!=12){ ex->ShowLegend=0; ex->LW=0; }
  410. if(ex->LW<LA_RH*3){ ex->LW=LA_RH*3;}
  411. }
  412. }elif(ex->UiMode==3){ Modal=1;
  413. if(e->Type&LA_MOUSE_EVENT){ ex->ClickedX=e->x; ex->ClickedY=e->y;
  414. int SetFrame=(real)(e->x-tl+ex->PanX)/ex->ZoomX/FW;
  415. la_AnimationActionSetOwnFrame(aa,SetFrame);
  416. }
  417. }elif(ex->UiMode==4){ Modal=1;
  418. if(e->Type==LA_R_MOUSE_DOWN || (e->Type==LA_ESCAPE_DOWN)){ ex->UiMode=0; ex->Dragging=0;
  419. la_ActionRestoreKeyOriginalAt(aa); laNotifyUsers("la.animation.current_action");
  420. }elif(e->Type==LA_L_MOUSE_DOWN){ la_ActionRemoveDuplicatedFrames(aa);
  421. laNotifyUsers("la.animation.current_action"); ex->UiMode=0; ex->Dragging=0;
  422. }elif(e->Type&LA_MOUSE_EVENT){ Modal=1;
  423. int ToFrame=(real)(e->x-tl+ex->PanX)/ex->ZoomX/FW,FromFrame=(real)(ex->ClickedX-tl+ex->PanX)/ex->ZoomX/FW;
  424. la_ActionMoveSelectedFrames(aa,ToFrame-FromFrame); laNotifyUsers("la.animation.current_action");
  425. }
  426. }
  427. int MaxDN=LA_RH*(Channels+1)-H+bt->TP+bt->BP; if(MaxDN<0) MaxDN=0;
  428. if(!Modal){
  429. if(e->x<=lr){
  430. if(e->Type==LA_MOUSE_WHEEL_DOWN){ ex->PanY+=LA_RH*MAIN.ScrollingSpeed; Modal=1; }
  431. if(e->Type==LA_MOUSE_WHEEL_UP){ ex->PanY-=LA_RH*MAIN.ScrollingSpeed; Modal=1; }
  432. if(e->x>=lr-LA_SEAM_W*2){ if(!ex->TargetIndexVali){ ex->TargetIndexVali=1; Modal=1; }
  433. if(e->Type==LA_L_MOUSE_DOWN){
  434. ex->UiMode=2; ex->ClickedX=e->x; ex->ClickedY=e->y; Modal=1; ex->Dragging=10; Modal=1;
  435. }
  436. }else{ if(ex->TargetIndexVali){ ex->TargetIndexVali=0; Modal=1; } }
  437. }elif(aa){
  438. if((!ex->ShowLegend)&&e->x<=ll+LA_SEAM_W*2){
  439. if(!ex->TargetIndexVali){ ex->TargetIndexVali=1; Modal=1; }
  440. if(e->Type==LA_L_MOUSE_DOWN){ ex->LW=LA_RH*4; ex->Dragging=12; ex->ShowLegend=1; Modal=1; ex->UiMode=2; ex->TargetIndexVali=1; }
  441. }else{ if(ex->TargetIndexVali){ ex->TargetIndexVali=0; Modal=1; } }
  442. if(e->Type==LA_MOUSE_WHEEL_DOWN){ ex->ZoomX*=0.9; ex->PanX+=mid; ex->PanX*=0.9; ex->PanX-=mid; Modal=1; }
  443. elif(e->Type==LA_MOUSE_WHEEL_UP){ ex->ZoomX*=1.1; ex->PanX+=mid; ex->PanX*=1.1; ex->PanX-=mid; Modal=1; }
  444. elif(e->Type==LA_L_MOUSE_DOWN){
  445. ex->UiMode=3; int SetFrame=(real)(e->x-tl+ex->PanX)/ex->ZoomX/FW;
  446. la_AnimationActionSetOwnFrame(aa,SetFrame); Modal=1;
  447. }elif(e->Type==LA_R_MOUSE_DOWN){
  448. int row=(e->y-ui->U-bt->BM-LA_RH)/LA_RH; real at=(real)(e->x-tl+ex->PanX)/ex->ZoomX/FW;
  449. if(!(e->SpecialKeyBit&LA_KEY_SHIFT)){ la_ActionDeselectFrames(aa); }
  450. la_ActionSelectFrame(aa,row,at,ex->ZoomX);
  451. laNotifyUsers("la.animation.current_action");
  452. }elif(e->Type==LA_KEY_DOWN&&e->key=='g'){
  453. int any=la_ActionSaveKeyOriginalAt(aa);
  454. if(any){ ex->ClickedX=e->x; ex->ClickedY=e->y; ex->UiMode=4; Modal=1; ex->Dragging=13;
  455. laNotifyUsers("la.animation.current_action");
  456. }
  457. }elif(e->Type==LA_KEY_DOWN&&e->key=='x'){
  458. int any=la_ActionDeleteSelectedFrames(aa);
  459. if(any){
  460. laNotifyUsers("la.animation.current_action");
  461. }
  462. }
  463. }
  464. }
  465. if(ex->PanY>MaxDN){ ex->PanY=MaxDN; } if(ex->PanY<0){ ex->PanY=0; }
  466. //if(ex->PanX<0){ ex->PanX=0; }
  467. if(ex->TargetIndexVali){ laSetWindowCursor(LA_LEFT_AND_RIGHT); }else{ laSetWindowCursor(LA_ARROW); }
  468. if(Modal){ laRedrawCurrentPanel(); return LA_RUNNING; }
  469. return LA_RUNNING_PASS;
  470. }
  471. void la_AnimationActionCanvasInit(laUiItem *ui){
  472. la_CanvasInit(ui);
  473. laCanvasExtra* ex=ui->Extra; ex->LW=LA_RH*7; ex->ShowLegend=1;
  474. }
  475. void la_AnimationActionDrawCanvas(laBoxedTheme *bt, laAction *aa, laUiItem* ui){
  476. laCanvasExtra* ex=ui->Extra; //laAction* aa=ui->PP.EndInstance;
  477. int W, H; W = ui->R - ui->L; H = ui->B - ui->U; if (W<=0 || H<=0) return;
  478. int ShowSide=(ex->ShowLegend&&ex->LW<W-2*LA_RH);
  479. int ll=ui->L, lr=ll+(ShowSide?ex->LW-1:0), tl=lr, tr=ui->R;
  480. int FW=LA_RH/2;
  481. //if (!e->OffScr || e->OffScr->pColor[0]->Height != ui->B - ui->U || e->Be.OffScr->pColor[0]->Width != ui->R - ui->L){
  482. // if (e->OffScr) tnsDelete2DOffscreen(e->OffScr);
  483. // e->OffScr = tnsCreate2DOffscreen(GL_RGBA, W, H, MAIN.PanelMultisample, 1, 0);
  484. //}
  485. real *bkg=laThemeColor(bt,LA_BT_NORMAL);
  486. real *txt=laThemeColor(bt,LA_BT_TEXT);
  487. real *act=laThemeColor(bt,LA_BT_ACTIVE);
  488. tnsUseNoTexture();
  489. tnsColor4d(LA_COLOR3(bkg),bkg[3]*0.3);
  490. tnsVertex2d(tl, ui->U); tnsVertex2d(tr, ui->U);
  491. tnsVertex2d(tr, ui->B); tnsVertex2d(tl, ui->B);
  492. tnsPackAs(GL_TRIANGLE_FAN);
  493. int row=1,curframe,cx; real fl,fr;
  494. tnsColor4dv(laThemeColor(bt,LA_BT_NORMAL));
  495. tnsVertex2d(ui->L, ui->U+bt->TP+LA_RH); tnsVertex2d(ui->R, ui->U+bt->TP+LA_RH);
  496. tnsVertex2d(ui->R, ui->U); tnsVertex2d(ui->L, ui->U);
  497. tnsPackAs(GL_TRIANGLE_FAN);
  498. tnsColor4dv(laThemeColor(bt,LA_BT_TEXT));
  499. tnsVertex2d(tl, LA_RH+ui->U); tnsVertex2d(tr, LA_RH+ui->U);
  500. tnsPackAs(GL_LINES);
  501. tnsFlush();
  502. int sx,sy,sw,sh,vl,vr,vu,vb;
  503. la_DoUiScissor(ui,&sx,&sy,&sw,&sh,&vl,&vr,&vu,&vb);
  504. if(aa){
  505. curframe=LA_ACTION_FRAME(aa); fl=tl+curframe*FW*ex->ZoomX-ex->PanX, fr=tl+(curframe+1)*FW*ex->ZoomX-ex->PanX;
  506. tnsUseNoTexture();
  507. tnsColor4dv(laAccentColor(LA_BT_NORMAL));
  508. tnsVertex2d(fl, ui->U); tnsVertex2d(fr, ui->U);
  509. tnsVertex2d(fr, ui->B); tnsVertex2d(fl, ui->B);
  510. tnsPackAs(GL_TRIANGLE_FAN);
  511. tnsFlush(); glLineWidth(2);
  512. for(laActionChannel* ac=aa->Channels.pFirst;ac;ac=ac->Item.pNext){
  513. real ku=ui->U+bt->TP+row*LA_RH-ex->PanY,kb=ku+LA_RH;
  514. for(laActionKey* ak=ac->Keys.pFirst;ak;ak=ak->Item.pNext){
  515. real kl=tl+ak->At*FW*ex->ZoomX-ex->PanX, kr=tl+(ak->At+1)*FW*ex->ZoomX-ex->PanX;
  516. if(ak->Selected) tnsColor4dv(laAccentColor(LA_BT_TEXT_ACTIVE));
  517. elif(curframe==ak->At) tnsColor4d(LA_COLOR3(txt),txt[3]*0.6);
  518. else tnsColor4dv(laThemeColor(bt,LA_BT_ACTIVE));
  519. tnsVertex2d(kl, ku); tnsVertex2d(kr, ku);
  520. tnsVertex2d(kr, kb); tnsVertex2d(kl, kb);
  521. tnsPackAs(GL_TRIANGLE_FAN);
  522. tnsColor4dv(laThemeColor(bt,LA_BT_TEXT_ACTIVE));
  523. tnsVertex2d(kl, ku); tnsVertex2d(kr, ku);
  524. tnsVertex2d(kr, kb); tnsVertex2d(kl, kb);
  525. tnsPackAs(GL_LINE_LOOP);
  526. }
  527. row++;
  528. }
  529. tnsFlush(); glLineWidth(1);
  530. real end=tl+aa->FrameCount*FW*ex->ZoomX-ex->PanX;
  531. tnsColor4d(0,0,0,0.3);
  532. tnsVertex2d(end, ui->U+LA_RH); tnsVertex2d(end+1e6, ui->U+LA_RH);
  533. tnsVertex2d(end+1e6, ui->B); tnsVertex2d(end, ui->B);
  534. tnsPackAs(GL_TRIANGLE_FAN);
  535. tnsVertex2d(tl-ex->PanX, ui->U+LA_RH); tnsVertex2d(tl-ex->PanX-1e6, ui->U+LA_RH);
  536. tnsVertex2d(tl-ex->PanX-1e6, ui->B); tnsVertex2d(tl-ex->PanX, ui->B);
  537. tnsPackAs(GL_TRIANGLE_FAN);
  538. tnsColor4dv(laAccentColor(LA_BT_TEXT_ACTIVE));
  539. int FrameFix=aa->PlayHead<0?1:0;
  540. cx=tl+(aa->PlayHead*aa->FrameCount+FrameFix)*FW*ex->ZoomX-ex->PanX; tnsVertex2d(cx, ui->U); tnsVertex2d(cx, ui->B);
  541. tnsPackAs(GL_LINES); glLineWidth(3); tnsFlush(); glLineWidth(1);
  542. char buf[32]; sprintf(buf,"%d",curframe); real tlen=tnsStringGetDimension(buf,0,0,0,0,0)+bt->LP+bt->RP;
  543. tnsColor4dv(laAccentColor(LA_BT_TEXT_ACTIVE));
  544. tnsVertex2d(cx, ui->U+LA_RH); tnsVertex2d(cx+tlen, ui->U+LA_RH);
  545. tnsVertex2d(cx+tlen, ui->U); tnsVertex2d(cx, ui->U);
  546. tnsPackAs(GL_TRIANGLE_FAN);
  547. tnsDrawStringAuto(buf,laThemeColor(bt,LA_BT_TEXT_ACTIVE),cx+bt->LP,cx+LA_RH*2,ui->U+bt->TP,0);
  548. tnsPackAs(GL_TRIANGLE_FAN); tnsUseNoTexture();
  549. }
  550. if(ShowSide){
  551. tnsColor4dv(laThemeColor(bt,LA_BT_NORMAL));
  552. tnsVertex2d(ll, ui->U); tnsVertex2d(lr, ui->U);
  553. tnsVertex2d(lr, ui->B); tnsVertex2d(ll, ui->B);
  554. tnsPackAs(GL_TRIANGLE_FAN);
  555. if(!aa){
  556. tnsDrawStringAuto("No action selected",laThemeColor(bt,LA_BT_TEXT),ll+bt->LP,lr-bt->RP,ui->U+bt->BP,0);
  557. }else{ int row=1;
  558. tnsDrawStringAuto(aa->Name->Ptr,laThemeColor(bt,LA_BT_TEXT),ll+bt->LP,lr-bt->RP,ui->U+bt->BP,0);
  559. for(laActionChannel* ac=aa->Channels.pFirst;ac;ac=ac->Item.pNext){
  560. tnsDrawStringAuto(ac->AP->CachedName->Ptr,laThemeColor(bt,LA_BT_TEXT),ll+bt->LP,lr-bt->RP,ui->U+bt->TP+row*LA_RH-ex->PanY,0);
  561. row++;
  562. }
  563. }
  564. }
  565. if(ex->TargetIndexVali==1){
  566. int LR=TNS_MAX2(lr,ui->L+LA_SEAM_W*2);
  567. tnsColor4dv(act);tnsUseNoTexture();
  568. tnsVertex2d(LR, ui->B); tnsVertex2d(LR-LA_SEAM_W*2, ui->B);
  569. tnsVertex2d(LR-LA_SEAM_W*2, ui->U); tnsVertex2d(LR, ui->U);
  570. tnsPackAs(GL_TRIANGLE_FAN);
  571. }
  572. tnsFlush();
  573. tnsViewportWithScissor(sx,sy,sw,sh);
  574. tnsOrtho(vl,vr,vb,vu,-100,100);
  575. tnsUseNoTexture();
  576. tnsColor4dv(laThemeColor(bt,LA_BT_BORDER));
  577. tnsVertex2d(ui->L, ui->U); tnsVertex2d(ui->R, ui->U);
  578. tnsVertex2d(ui->R, ui->B); tnsVertex2d(ui->L, ui->B);
  579. tnsPackAs(GL_LINE_LOOP);
  580. }
  581. void la_AnimationActionDrawOverlay(laUiItem *ui, int h){
  582. laCanvasExtra *e = ui->Extra; laBoxedTheme *bt = (*ui->Type->Theme);
  583. tnsUseImmShader();
  584. if(MAIN.CurrentWindow->MaximizedUi!=ui){
  585. tnsUseNoTexture();
  586. tnsColor4dv(laThemeColor(bt,LA_BT_BORDER));
  587. tnsVertex2d(ui->L, ui->U); tnsVertex2d(ui->R, ui->U);
  588. tnsVertex2d(ui->R, ui->B); tnsVertex2d(ui->L, ui->B);
  589. tnsPackAs(GL_LINE_LOOP);
  590. }
  591. if (e->DrawCursor){
  592. tnsColor4dv(laThemeColor(bt,LA_BT_TEXT));
  593. int drawx=(e->DrawCursor==LA_CANVAS_CURSOR_CROSS)||(e->DrawCursor==LA_CANVAS_CURSOR_X);
  594. int drawy=(e->DrawCursor==LA_CANVAS_CURSOR_CROSS)||(e->DrawCursor==LA_CANVAS_CURSOR_Y);
  595. if(drawx) tnsVertex2d(e->OnX, ui->U); tnsVertex2d(e->OnX, ui->B);
  596. if(drawy) tnsVertex2d(ui->L, e->OnY); tnsVertex2d(ui->R, e->OnY);
  597. tnsPackAs(GL_LINES);
  598. tnsFlush();
  599. }
  600. return;
  601. }
  602. void la_RegisterAnimationResources(){
  603. laPropContainer *pc; laProp *p; laOperatorType *at; laEnumProp *ep;
  604. at=laCreateOperatorType("LA_animation_new_action", "New Action", "Add a new action",0,0,0,OPINV_AnimationNewAction,OPMOD_AnimationNewAction,U'🞦',0);
  605. pc = laDefineOperatorProps(at, 0);
  606. p = laAddSubGroup(pc, "holder", "Holder", "Action holder to add the new action into", "la_animation_action_holder",
  607. 0, 0, laui_IdentifierOnly, -1, laget_AnimationFirstActionHolder, 0, laget_ListNext, 0, 0, laset_AnimationNewActionSetHolder,0,0);
  608. laSubGroupExtraFunctions(p,0,0,0,laget_AnimationActionHolderCategory);
  609. at->UiDefine=laui_AnimationNewAction;
  610. at=laCreateOperatorType("LA_animation_select_action", "Select Action", "Select an action",0,0,0,OPINV_AnimationSelectAction,OPMOD_FinishOnData,U'⯆',0);
  611. at->UiDefine=laui_AnimationSelectAction;
  612. laCreateOperatorType("LA_animation_set_play_status", "Set Play", "Set global animation player status",0,0,0,OPINV_AnimationPlayAction,0,0,0);
  613. laCreateOperatorType("LA_animation_reset_time", "Reset Time", "Reset Time",0,0,0,OPINV_AnimationResetTime,0,U'🡄',0);
  614. laCanvasTemplate* ct=laRegisterCanvasTemplate("la_AnimationActionDrawCanvas", "la_animation_action", LAMOD_AnimationActionsCanvas, la_AnimationActionDrawCanvas, la_AnimationActionDrawOverlay, la_AnimationActionCanvasInit, la_CanvasDestroy);
  615. pc = laCanvasHasExtraProps(ct,sizeof(laCanvasExtra),2); laAddIntProperty(pc, "height_coeff", "Ui Height", "Ui Height Coefficiency Entry", 0, 0, "Rows", 100, -100, 1, 0, 0, offsetof(laCanvasExtra, HeightCoeff), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  616. }