*/}}

la_animation.c 31 KB

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