*/}}

la_animation.c 26 KB

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