Welcome to mirror list, hosted at ThFree Co, Russian Federation.

keyframes_edit.c « animation « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dfe6566df67db1166785ec2b99b0896ab2729f0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2008 Blender Foundation
 */

/** \file
 * \ingroup edanimation
 */

#include <float.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

#include "MEM_guardedalloc.h"

#include "BLI_blenlib.h"
#include "BLI_lasso_2d.h"
#include "BLI_math.h"
#include "BLI_utildefines.h"

#include "DNA_anim_types.h"
#include "DNA_object_types.h"
#include "DNA_scene_types.h"

#include "BKE_fcurve.h"
#include "BKE_nla.h"

#include "ED_anim_api.h"
#include "ED_keyframes_edit.h"
#include "ED_markers.h"

/* This file defines an API and set of callback-operators for
 * non-destructive editing of keyframe data.
 *
 * Two API functions are defined for actually performing the operations on the data:
 * ANIM_fcurve_keyframes_loop()
 * which take the data they operate on, a few callbacks defining what operations to perform.
 *
 * As operators which work on keyframes usually apply the same operation on all BezTriples in
 * every channel, the code has been optimized providing a set of functions which will get the
 * appropriate bezier-modify function to set. These functions (ANIM_editkeyframes_*) will need
 * to be called before getting any channels.
 *
 * A set of 'validation' callbacks are provided for checking if a BezTriple should be operated on.
 * These should only be used when using a 'general' BezTriple editor (i.e. selection setters which
 * don't check existing selection status).
 *
 * - Joshua Leung, Dec 2008
 */

/* ************************************************************************** */
/* Keyframe Editing Loops - Exposed API */

/* --------------------------- Base Functions ------------------------------------ */

short ANIM_fcurve_keyframes_loop(KeyframeEditData *ked,
                                 FCurve *fcu,
                                 KeyframeEditFunc key_ok,
                                 KeyframeEditFunc key_cb,
                                 FcuEditFunc fcu_cb)
{
  BezTriple *bezt;
  short ok = 0;
  uint i;

  /* sanity check */
  if (ELEM(NULL, fcu, fcu->bezt)) {
    return 0;
  }

  /* Set the F-Curve into the edit-data so that it can be accessed. */
  if (ked) {
    ked->fcu = fcu;
    ked->curIndex = 0;
    ked->curflags = ok;
  }

  /* if function to apply to bezier curves is set, then loop through executing it on beztriples */
  if (key_cb) {
    /* if there's a validation func, include that check in the loop
     * (this is should be more efficient than checking for it in every loop)
     */
    if (key_ok) {
      for (bezt = fcu->bezt, i = 0; i < fcu->totvert; bezt++, i++) {
        if (ked) {
          /* advance the index, and reset the ok flags (to not influence the result) */
          ked->curIndex = i;
          ked->curflags = 0;
        }

        /* Only operate on this BezTriple if it fulfills the criteria of the validation func */
        if ((ok = key_ok(ked, bezt))) {
          if (ked) {
            ked->curflags = ok;
          }

          /* Exit with return-code '1' if function returns positive
           * This is useful if finding if some BezTriple satisfies a condition.
           */
          if (key_cb(ked, bezt)) {
            return 1;
          }
        }
      }
    }
    else {
      for (bezt = fcu->bezt, i = 0; i < fcu->totvert; bezt++, i++) {
        if (ked) {
          ked->curIndex = i;
        }

        /* Exit with return-code '1' if function returns positive
         * This is useful if finding if some BezTriple satisfies a condition.
         */
        if (key_cb(ked, bezt)) {
          return 1;
        }
      }
    }
  }

  /* unset the F-Curve from the editdata now that it's done */
  if (ked) {
    ked->fcu = NULL;
    ked->curIndex = 0;
    ked->curflags = 0;
  }

  /* if fcu_cb (F-Curve post-editing callback) has been specified then execute it */
  if (fcu_cb) {
    fcu_cb(fcu);
  }

  /* done */
  return 0;
}

/* --------------------- Further Abstracted (Not Exposed Directly) ----------------------------- */

/* This function is used to loop over the keyframe data in an Action Group */
static short agrp_keyframes_loop(KeyframeEditData *ked,
                                 bActionGroup *agrp,
                                 KeyframeEditFunc key_ok,
                                 KeyframeEditFunc key_cb,
                                 FcuEditFunc fcu_cb)
{
  FCurve *fcu;

  /* sanity check */
  if (agrp == NULL) {
    return 0;
  }

  /* only iterate over the F-Curves that are in this group */
  for (fcu = agrp->channels.first; fcu && fcu->grp == agrp; fcu = fcu->next) {
    if (ANIM_fcurve_keyframes_loop(ked, fcu, key_ok, key_cb, fcu_cb)) {
      return 1;
    }
  }

  return 0;
}

/* This function is used to loop over the keyframe data in an Action */
static short act_keyframes_loop(KeyframeEditData *ked,
                                bAction *act,
                                KeyframeEditFunc key_ok,
                                KeyframeEditFunc key_cb,
                                FcuEditFunc fcu_cb)
{
  FCurve *fcu;

  /* sanity check */
  if (act == NULL) {
    return 0;
  }

  /* just loop through all F-Curves */
  for (fcu = act->curves.first; fcu; fcu = fcu->next) {
    if (ANIM_fcurve_keyframes_loop(ked, fcu, key_ok, key_cb, fcu_cb)) {
      return 1;
    }
  }

  return 0;
}

/* This function is used to loop over the keyframe data in an Object */
static short ob_keyframes_loop(KeyframeEditData *ked,
                               bDopeSheet *ads,
                               Object *ob,
                               KeyframeEditFunc key_ok,
                               KeyframeEditFunc key_cb,
                               FcuEditFunc fcu_cb)
{
  bAnimContext ac = {NULL};
  ListBase anim_data = {NULL, NULL};
  bAnimListElem *ale;
  int filter;
  int ret = 0;

  bAnimListElem dummychan = {NULL};
  Base dummybase = {NULL};

  if (ob == NULL) {
    return 0;
  }

  /* create a dummy wrapper data to work with */
  dummybase.object = ob;

  dummychan.type = ANIMTYPE_OBJECT;
  dummychan.data = &dummybase;
  dummychan.id = &ob->id;
  dummychan.adt = ob->adt;

  ac.ads = ads;
  ac.data = &dummychan;
  ac.datatype = ANIMCONT_CHANNEL;

  /* get F-Curves to take keyframes from */
  filter = ANIMFILTER_DATA_VISIBLE; /* curves only */
  ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);

  /* Loop through each F-Curve, applying the operation as required,
   * but stopping on the first one. */
  for (ale = anim_data.first; ale; ale = ale->next) {
    if (ANIM_fcurve_keyframes_loop(ked, (FCurve *)ale->data, key_ok, key_cb, fcu_cb)) {
      ret = 1;
      break;
    }
  }

  ANIM_animdata_freelist(&anim_data);

  /* Return the return code (defaults to zero if nothing happened). */
  return ret;
}

/* This function is used to loop over the keyframe data in a Scene */
static short scene_keyframes_loop(KeyframeEditData *ked,
                                  bDopeSheet *ads,
                                  Scene *sce,
                                  KeyframeEditFunc key_ok,
                                  KeyframeEditFunc key_cb,
                                  FcuEditFunc fcu_cb)
{
  bAnimContext ac = {NULL};
  ListBase anim_data = {NULL, NULL};
  bAnimListElem *ale;
  int filter;
  int ret = 0;

  bAnimListElem dummychan = {NULL};

  if (sce == NULL) {
    return 0;
  }

  /* create a dummy wrapper data to work with */
  dummychan.type = ANIMTYPE_SCENE;
  dummychan.data = sce;
  dummychan.id = &sce->id;
  dummychan.adt = sce->adt;

  ac.ads = ads;
  ac.data = &dummychan;
  ac.datatype = ANIMCONT_CHANNEL;

  /* get F-Curves to take keyframes from */
  filter = ANIMFILTER_DATA_VISIBLE; /* curves only */
  ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);

  /* Loop through each F-Curve, applying the operation as required,
   * but stopping on the first one. */
  for (ale = anim_data.first; ale; ale = ale->next) {
    if (ANIM_fcurve_keyframes_loop(ked, (FCurve *)ale->data, key_ok, key_cb, fcu_cb)) {
      ret = 1;
      break;
    }
  }

  ANIM_animdata_freelist(&anim_data);

  /* Return the return code (defaults to zero if nothing happened). */
  return ret;
}

/* This function is used to loop over the keyframe data in a DopeSheet summary */
static short summary_keyframes_loop(KeyframeEditData *ked,
                                    bAnimContext *ac,
                                    KeyframeEditFunc key_ok,
                                    KeyframeEditFunc key_cb,
                                    FcuEditFunc fcu_cb)
{
  ListBase anim_data = {NULL, NULL};
  bAnimListElem *ale;
  int filter, ret_code = 0;

  /* sanity check */
  if (ac == NULL) {
    return 0;
  }

  /* get F-Curves to take keyframes from */
  filter = ANIMFILTER_DATA_VISIBLE;
  ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);

  /* loop through each F-Curve, working on the keyframes until the first curve aborts */
  for (ale = anim_data.first; ale; ale = ale->next) {
    switch (ale->datatype) {
      case ALE_MASKLAY:
      case ALE_GPFRAME:
        break;

      case ALE_FCURVE:
      default: {
        if (ked && ked->iterflags) {
          /* make backups of the current values, so that a localized fix
           * (e.g. NLA time remapping) can be applied to these values
           */
          float f1 = ked->f1;
          float f2 = ked->f2;

          if (ked->iterflags & (KED_F1_NLA_UNMAP | KED_F2_NLA_UNMAP)) {
            AnimData *adt = ANIM_nla_mapping_get(ac, ale);

            if (ked->iterflags & KED_F1_NLA_UNMAP) {
              ked->f1 = BKE_nla_tweakedit_remap(adt, f1, NLATIME_CONVERT_UNMAP);
            }
            if (ked->iterflags & KED_F2_NLA_UNMAP) {
              ked->f2 = BKE_nla_tweakedit_remap(adt, f2, NLATIME_CONVERT_UNMAP);
            }
          }

          /* now operate on the channel as per normal */
          ret_code = ANIM_fcurve_keyframes_loop(ked, ale->data, key_ok, key_cb, fcu_cb);

          /* reset */
          ked->f1 = f1;
          ked->f2 = f2;
        }
        else {
          /* no special handling required... */
          ret_code = ANIM_fcurve_keyframes_loop(ked, ale->data, key_ok, key_cb, fcu_cb);
        }
        break;
      }
    }

    if (ret_code) {
      break;
    }
  }

  ANIM_animdata_freelist(&anim_data);

  return ret_code;
}

/* --- */

short ANIM_animchannel_keyframes_loop(KeyframeEditData *ked,
                                      bDopeSheet *ads,
                                      bAnimListElem *ale,
                                      KeyframeEditFunc key_ok,
                                      KeyframeEditFunc key_cb,
                                      FcuEditFunc fcu_cb)
{
  /* sanity checks */
  if (ale == NULL) {
    return 0;
  }

  /* method to use depends on the type of keyframe data */
  switch (ale->datatype) {
    /* direct keyframe data (these loops are exposed) */
    case ALE_FCURVE: /* F-Curve */
      return ANIM_fcurve_keyframes_loop(ked, ale->key_data, key_ok, key_cb, fcu_cb);

    /* indirect 'summaries' (these are not exposed directly)
     * NOTE: must keep this code in sync with the drawing code and also the filtering code!
     */
    case ALE_GROUP: /* action group */
      return agrp_keyframes_loop(ked, (bActionGroup *)ale->data, key_ok, key_cb, fcu_cb);
    case ALE_ACT: /* action */
      return act_keyframes_loop(ked, (bAction *)ale->key_data, key_ok, key_cb, fcu_cb);

    case ALE_OB: /* object */
      return ob_keyframes_loop(ked, ads, (Object *)ale->key_data, key_ok, key_cb, fcu_cb);
    case ALE_SCE: /* scene */
      return scene_keyframes_loop(ked, ads, (Scene *)ale->data, key_ok, key_cb, fcu_cb);
    case ALE_ALL: /* 'all' (DopeSheet summary) */
      return summary_keyframes_loop(ked, (bAnimContext *)ale->data, key_ok, key_cb, fcu_cb);
  }

  return 0;
}

short ANIM_animchanneldata_keyframes_loop(KeyframeEditData *ked,
                                          bDopeSheet *ads,
                                          void *data,
                                          int keytype,
                                          KeyframeEditFunc key_ok,
                                          KeyframeEditFunc key_cb,
                                          FcuEditFunc fcu_cb)
{
  /* sanity checks */
  if (data == NULL) {
    return 0;
  }

  /* method to use depends on the type of keyframe data */
  switch (keytype) {
    /* direct keyframe data (these loops are exposed) */
    case ALE_FCURVE: /* F-Curve */
      return ANIM_fcurve_keyframes_loop(ked, data, key_ok, key_cb, fcu_cb);

    /* indirect 'summaries' (these are not exposed directly)
     * NOTE: must keep this code in sync with the drawing code and also the filtering code!
     */
    case ALE_GROUP: /* action group */
      return agrp_keyframes_loop(ked, (bActionGroup *)data, key_ok, key_cb, fcu_cb);
    case ALE_ACT: /* action */
      return act_keyframes_loop(ked, (bAction *)data, key_ok, key_cb, fcu_cb);

    case ALE_OB: /* object */
      return ob_keyframes_loop(ked, ads, (Object *)data, key_ok, key_cb, fcu_cb);
    case ALE_SCE: /* scene */
      return scene_keyframes_loop(ked, ads, (Scene *)data, key_ok, key_cb, fcu_cb);
    case ALE_ALL: /* 'all' (DopeSheet summary) */
      return summary_keyframes_loop(ked, (bAnimContext *)data, key_ok, key_cb, fcu_cb);
  }

  return 0;
}

void ANIM_animdata_keyframe_callback(bAnimContext *ac,
                                     eAnimFilter_Flags filter,
                                     KeyframeEditFunc callback_fn)
{
  ListBase anim_data = {NULL, NULL};
  bAnimListElem *ale;

  ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);

  for (ale = anim_data.first; ale; ale = ale->next) {
    ANIM_fcurve_keyframes_loop(NULL, ale->key_data, NULL, callback_fn, calchandles_fcurve);
    ale->update |= ANIM_UPDATE_DEFAULT;
  }

  ANIM_animdata_update(ac, &anim_data);
  ANIM_animdata_freelist(&anim_data);
}

/* ************************************************************************** */
/* Keyframe Integrity Tools */

void ANIM_editkeyframes_refresh(bAnimContext *ac)
{
  ListBase anim_data = {NULL, NULL};
  bAnimListElem *ale;
  int filter;

  /* filter animation data */
  filter = ANIMFILTER_DATA_VISIBLE;
  ANIM_animdata_filter(ac, &anim_data, filter, ac->data, ac->datatype);

  /* Loop over F-Curves that are likely to have been edited, and tag them to
   * ensure the keyframes are in order and handles are in a valid position. */
  for (ale = anim_data.first; ale; ale = ale->next) {
    ale->update |= ANIM_UPDATE_DEPS | ANIM_UPDATE_HANDLES | ANIM_UPDATE_ORDER;
  }

  /* free temp data */
  ANIM_animdata_update(ac, &anim_data);
  ANIM_animdata_freelist(&anim_data);
}

/* ************************************************************************** */
/* BezTriple Validation Callbacks */

/* ------------------------ */
/* Some macros to make this easier... */

/* run the given check on the 3 handles:
 * - Check should be a macro, which takes the handle index as its single arg,
 *   which it substitutes later.
 * - Requires that a var, of type short, is named 'ok',
 *   and has been initialized to 0.
 */
#define KEYFRAME_OK_CHECKS(check) \
  { \
    CHECK_TYPE(ok, short); \
    if (check(1)) { \
      ok |= KEYFRAME_OK_KEY; \
    } \
    if (ked && (ked->iterflags & KEYFRAME_ITER_INCL_HANDLES)) { \
      /* Only act on visible items, so check handle visibility state. */ \
      const bool handles_visible = ((ked->iterflags & KEYFRAME_ITER_HANDLES_DEFAULT_INVISIBLE) ? \
                                        (BEZT_ISSEL_ANY(bezt)) : \
                                        true); \
      if (handles_visible) { \
        if (check(0)) { \
          ok |= KEYFRAME_OK_H1; \
        } \
        if (check(2)) { \
          ok |= KEYFRAME_OK_H2; \
        } \
      } \
    } \
  } \
  (void)0

/* ------------------------ */

static short ok_bezier_frame(KeyframeEditData *ked, BezTriple *bezt)
{
  short ok = 0;

  /* frame is stored in f1 property (this float accuracy check may need to be dropped?) */
#define KEY_CHECK_OK(_index) IS_EQF(bezt->vec[_index][0], ked->f1)
  KEYFRAME_OK_CHECKS(KEY_CHECK_OK);
#undef KEY_CHECK_OK

  /* return ok flags */
  return ok;
}

static short ok_bezier_framerange(KeyframeEditData *ked, BezTriple *bezt)
{
  short ok = 0;

  /* frame range is stored in float properties */
#define KEY_CHECK_OK(_index) ((bezt->vec[_index][0] > ked->f1) && (bezt->vec[_index][0] < ked->f2))
  KEYFRAME_OK_CHECKS(KEY_CHECK_OK);
#undef KEY_CHECK_OK

  /* return ok flags */
  return ok;
}

static short ok_bezier_selected(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  /* this macro checks all beztriple handles for selection...
   * only one of the verts has to be selected for this to be ok...
   */
  if (BEZT_ISSEL_ANY(bezt)) {
    return KEYFRAME_OK_ALL;
  }
  return 0;
}

static short ok_bezier_value(KeyframeEditData *ked, BezTriple *bezt)
{
  short ok = 0;

  /* Value is stored in f1 property:
   * - This float accuracy check may need to be dropped?
   * - Should value be stored in f2 instead
   *   so that we won't have conflicts when using f1 for frames too?
   */
#define KEY_CHECK_OK(_index) IS_EQF(bezt->vec[_index][1], ked->f1)
  KEYFRAME_OK_CHECKS(KEY_CHECK_OK);
#undef KEY_CHECK_OK

  /* return ok flags */
  return ok;
}

static short ok_bezier_valuerange(KeyframeEditData *ked, BezTriple *bezt)
{
  short ok = 0;

  /* value range is stored in float properties */
#define KEY_CHECK_OK(_index) ((bezt->vec[_index][1] > ked->f1) && (bezt->vec[_index][1] < ked->f2))
  KEYFRAME_OK_CHECKS(KEY_CHECK_OK);
#undef KEY_CHECK_OK

  /* return ok flags */
  return ok;
}

static short ok_bezier_region(KeyframeEditData *ked, BezTriple *bezt)
{
  /* rect is stored in data property (it's of type rectf, but may not be set) */
  if (ked->data) {
    short ok = 0;

#define KEY_CHECK_OK(_index) BLI_rctf_isect_pt_v(ked->data, bezt->vec[_index])
    KEYFRAME_OK_CHECKS(KEY_CHECK_OK);
#undef KEY_CHECK_OK

    /* return ok flags */
    return ok;
  }
  return 0;
}

bool keyframe_region_lasso_test(const KeyframeEdit_LassoData *data_lasso, const float xy[2])
{
  if (BLI_rctf_isect_pt_v(data_lasso->rectf_scaled, xy)) {
    float xy_view[2];

    BLI_rctf_transform_pt_v(data_lasso->rectf_view, data_lasso->rectf_scaled, xy_view, xy);

    if (BLI_lasso_is_point_inside(
            data_lasso->mcoords, data_lasso->mcoords_len, xy_view[0], xy_view[1], INT_MAX)) {
      return true;
    }
  }

  return false;
}

static short ok_bezier_region_lasso(KeyframeEditData *ked, BezTriple *bezt)
{
  /* check for lasso customdata (KeyframeEdit_LassoData) */
  if (ked->data) {
    short ok = 0;

#define KEY_CHECK_OK(_index) keyframe_region_lasso_test(ked->data, bezt->vec[_index])
    KEYFRAME_OK_CHECKS(KEY_CHECK_OK);
#undef KEY_CHECK_OK

    /* return ok flags */
    return ok;
  }
  return 0;
}

static short ok_bezier_channel_lasso(KeyframeEditData *ked, BezTriple *bezt)
{
  /* check for lasso customdata (KeyframeEdit_LassoData) */
  if (ked->data) {
    KeyframeEdit_LassoData *data = ked->data;
    float pt[2];

    /* late-binding remap of the x values (for summary channels) */
    /* XXX: Ideally we reset, but it should be fine just leaving it as-is
     * as the next channel will reset it properly, while the next summary-channel
     * curve will also reset by itself...
     */
    if (ked->iterflags & (KED_F1_NLA_UNMAP | KED_F2_NLA_UNMAP)) {
      data->rectf_scaled->xmin = ked->f1;
      data->rectf_scaled->xmax = ked->f2;
    }

    /* only use the x-coordinate of the point; the y is the channel range... */
    pt[0] = bezt->vec[1][0];
    pt[1] = ked->channel_y;

    if (keyframe_region_lasso_test(data, pt)) {
      return KEYFRAME_OK_KEY;
    }
  }
  return 0;
}

bool keyframe_region_circle_test(const KeyframeEdit_CircleData *data_circle, const float xy[2])
{
  if (BLI_rctf_isect_pt_v(data_circle->rectf_scaled, xy)) {
    float xy_view[2];

    BLI_rctf_transform_pt_v(data_circle->rectf_view, data_circle->rectf_scaled, xy_view, xy);

    xy_view[0] = xy_view[0] - data_circle->mval[0];
    xy_view[1] = xy_view[1] - data_circle->mval[1];
    return len_squared_v2(xy_view) < data_circle->radius_squared;
  }

  return false;
}

static short ok_bezier_region_circle(KeyframeEditData *ked, BezTriple *bezt)
{
  /* check for circle select customdata (KeyframeEdit_CircleData) */
  if (ked->data) {
    short ok = 0;

#define KEY_CHECK_OK(_index) keyframe_region_circle_test(ked->data, bezt->vec[_index])
    KEYFRAME_OK_CHECKS(KEY_CHECK_OK);
#undef KEY_CHECK_OK

    /* return ok flags */
    return ok;
  }
  return 0;
}

static short ok_bezier_channel_circle(KeyframeEditData *ked, BezTriple *bezt)
{
  /* check for circle select customdata (KeyframeEdit_CircleData) */
  if (ked->data) {
    KeyframeEdit_CircleData *data = ked->data;
    float pt[2];

    /* late-binding remap of the x values (for summary channels) */
    /* XXX: Ideally we reset, but it should be fine just leaving it as-is
     * as the next channel will reset it properly, while the next summary-channel
     * curve will also reset by itself...
     */
    if (ked->iterflags & (KED_F1_NLA_UNMAP | KED_F2_NLA_UNMAP)) {
      data->rectf_scaled->xmin = ked->f1;
      data->rectf_scaled->xmax = ked->f2;
    }

    /* only use the x-coordinate of the point; the y is the channel range... */
    pt[0] = bezt->vec[1][0];
    pt[1] = ked->channel_y;

    if (keyframe_region_circle_test(data, pt)) {
      return KEYFRAME_OK_KEY;
    }
  }
  return 0;
}

KeyframeEditFunc ANIM_editkeyframes_ok(short mode)
{
  /* eEditKeyframes_Validate */
  switch (mode) {
    case BEZT_OK_FRAME:
      /* only if bezt falls on the right frame (float) */
      return ok_bezier_frame;
    case BEZT_OK_FRAMERANGE:
      /* only if bezt falls within the specified frame range (floats) */
      return ok_bezier_framerange;
    case BEZT_OK_SELECTED:
      /* only if bezt is selected (self) */
      return ok_bezier_selected;
    case BEZT_OK_VALUE:
      /* only if bezt value matches (float) */
      return ok_bezier_value;
    case BEZT_OK_VALUERANGE:
      /* only if bezier falls within the specified value range (floats) */
      return ok_bezier_valuerange;
    case BEZT_OK_REGION:
      /* only if bezier falls within the specified rect (data -> rectf) */
      return ok_bezier_region;
    case BEZT_OK_REGION_LASSO:
      /* only if the point falls within KeyframeEdit_LassoData defined data */
      return ok_bezier_region_lasso;
    case BEZT_OK_REGION_CIRCLE:
      /* only if the point falls within KeyframeEdit_CircleData defined data */
      return ok_bezier_region_circle;
    case BEZT_OK_CHANNEL_LASSO:
      /* same as BEZT_OK_REGION_LASSO, but we're only using the x-value of the points */
      return ok_bezier_channel_lasso;
    case BEZT_OK_CHANNEL_CIRCLE:
      /* same as BEZT_OK_REGION_CIRCLE, but we're only using the x-value of the points */
      return ok_bezier_channel_circle;
    default: /* nothing was ok */
      return NULL;
  }
}

/* ******************************************* */
/* Assorted Utility Functions */

short bezt_calc_average(KeyframeEditData *ked, BezTriple *bezt)
{
  /* only if selected */
  if (bezt->f2 & SELECT) {
    /* store average time in float 1 (only do rounding at last step) */
    ked->f1 += bezt->vec[1][0];

    /* store average value in float 2 (only do rounding at last step)
     * - this isn't always needed, but some operators may also require this
     */
    ked->f2 += bezt->vec[1][1];

    /* increment number of items */
    ked->i1++;
  }

  return 0;
}

short bezt_to_cfraelem(KeyframeEditData *ked, BezTriple *bezt)
{
  /* only if selected */
  if (bezt->f2 & SELECT) {
    CfraElem *ce = MEM_callocN(sizeof(CfraElem), "cfraElem");
    BLI_addtail(&ked->list, ce);

    ce->cfra = bezt->vec[1][0];
  }

  return 0;
}

void bezt_remap_times(KeyframeEditData *ked, BezTriple *bezt)
{
  KeyframeEditCD_Remap *rmap = (KeyframeEditCD_Remap *)ked->data;
  const float scale = (rmap->newMax - rmap->newMin) / (rmap->oldMax - rmap->oldMin);

  /* perform transform on all three handles unless indicated otherwise */
  /* TODO: need to include some checks for that */

  bezt->vec[0][0] = scale * (bezt->vec[0][0] - rmap->oldMin) + rmap->newMin;
  bezt->vec[1][0] = scale * (bezt->vec[1][0] - rmap->oldMin) + rmap->newMin;
  bezt->vec[2][0] = scale * (bezt->vec[2][0] - rmap->oldMin) + rmap->newMin;
}

/* ******************************************* */
/* Transform */

/* snaps the keyframe to the nearest frame */
static short snap_bezier_nearest(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  if (bezt->f2 & SELECT) {
    bezt->vec[1][0] = (float)(floorf(bezt->vec[1][0] + 0.5f));
  }
  return 0;
}

/* snaps the keyframe to the nearest second */
static short snap_bezier_nearestsec(KeyframeEditData *ked, BezTriple *bezt)
{
  const Scene *scene = ked->scene;
  const float secf = (float)FPS;

  if (bezt->f2 & SELECT) {
    bezt->vec[1][0] = (floorf(bezt->vec[1][0] / secf + 0.5f) * secf);
  }
  return 0;
}

/* snaps the keyframe to the current frame */
static short snap_bezier_cframe(KeyframeEditData *ked, BezTriple *bezt)
{
  const Scene *scene = ked->scene;
  if (bezt->f2 & SELECT) {
    bezt->vec[1][0] = (float)CFRA;
  }
  return 0;
}

/* snaps the keyframe time to the nearest marker's frame */
static short snap_bezier_nearmarker(KeyframeEditData *ked, BezTriple *bezt)
{
  if (bezt->f2 & SELECT) {
    bezt->vec[1][0] = (float)ED_markers_find_nearest_marker_time(&ked->list, bezt->vec[1][0]);
  }
  return 0;
}

/* make the handles have the same value as the key */
static short snap_bezier_horizontal(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  if (bezt->f2 & SELECT) {
    bezt->vec[0][1] = bezt->vec[2][1] = bezt->vec[1][1];

    if (ELEM(bezt->h1, HD_AUTO, HD_AUTO_ANIM, HD_VECT)) {
      bezt->h1 = HD_ALIGN;
    }
    if (ELEM(bezt->h2, HD_AUTO, HD_AUTO_ANIM, HD_VECT)) {
      bezt->h2 = HD_ALIGN;
    }
  }
  return 0;
}

/* frame to snap to is stored in the custom data -> first float value slot */
static short snap_bezier_time(KeyframeEditData *ked, BezTriple *bezt)
{
  if (bezt->f2 & SELECT) {
    bezt->vec[1][0] = ked->f1;
  }
  return 0;
}

/* value to snap to is stored in the custom data -> first float value slot */
static short snap_bezier_value(KeyframeEditData *ked, BezTriple *bezt)
{
  if (bezt->f2 & SELECT) {
    bezt->vec[1][1] = ked->f1;
  }
  return 0;
}

KeyframeEditFunc ANIM_editkeyframes_snap(short mode)
{
  /* eEditKeyframes_Snap */
  switch (mode) {
    case SNAP_KEYS_NEARFRAME: /* snap to nearest frame */
      return snap_bezier_nearest;
    case SNAP_KEYS_CURFRAME: /* snap to current frame */
      return snap_bezier_cframe;
    case SNAP_KEYS_NEARMARKER: /* snap to nearest marker */
      return snap_bezier_nearmarker;
    case SNAP_KEYS_NEARSEC: /* snap to nearest second */
      return snap_bezier_nearestsec;
    case SNAP_KEYS_HORIZONTAL: /* snap handles to same value */
      return snap_bezier_horizontal;
    case SNAP_KEYS_TIME: /* snap to given frame/time */
      return snap_bezier_time;
    case SNAP_KEYS_VALUE: /* snap to given value */
      return snap_bezier_value;
    default: /* just in case */
      return snap_bezier_nearest;
  }
}

/* --------- */

static void mirror_bezier_xaxis_ex(BezTriple *bezt, const float center)
{
  for (int i = 0; i < 3; i++) {
    float diff = (center - bezt->vec[i][0]);
    bezt->vec[i][0] = (center + diff);
  }
  swap_v3_v3(bezt->vec[0], bezt->vec[2]);

  SWAP(uint8_t, bezt->h1, bezt->h2);
  SWAP(uint8_t, bezt->f1, bezt->f3);
}

static void mirror_bezier_yaxis_ex(BezTriple *bezt, const float center)
{
  for (int i = 0; i < 3; i++) {
    float diff = (center - bezt->vec[i][1]);
    bezt->vec[i][1] = (center + diff);
  }
}

static short mirror_bezier_cframe(KeyframeEditData *ked, BezTriple *bezt)
{
  const Scene *scene = ked->scene;

  if (bezt->f2 & SELECT) {
    mirror_bezier_xaxis_ex(bezt, CFRA);
  }

  return 0;
}

static short mirror_bezier_yaxis(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  if (bezt->f2 & SELECT) {
    /* Yes, names are inverted, we are mirroring across y axis, hence along x axis... */
    mirror_bezier_xaxis_ex(bezt, 0.0f);
  }

  return 0;
}

static short mirror_bezier_xaxis(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  if (bezt->f2 & SELECT) {
    /* Yes, names are inverted, we are mirroring across x axis, hence along y axis... */
    mirror_bezier_yaxis_ex(bezt, 0.0f);
  }

  return 0;
}

static short mirror_bezier_marker(KeyframeEditData *ked, BezTriple *bezt)
{
  /* mirroring time stored in f1 */
  if (bezt->f2 & SELECT) {
    mirror_bezier_xaxis_ex(bezt, ked->f1);
  }

  return 0;
}

static short mirror_bezier_time(KeyframeEditData *ked, BezTriple *bezt)
{
  /* value to mirror over is stored in f1 */
  if (bezt->f2 & SELECT) {
    mirror_bezier_xaxis_ex(bezt, ked->f1);
  }

  return 0;
}

static short mirror_bezier_value(KeyframeEditData *ked, BezTriple *bezt)
{
  /* value to mirror over is stored in the custom data -> first float value slot */
  if (bezt->f2 & SELECT) {
    mirror_bezier_yaxis_ex(bezt, ked->f1);
  }

  return 0;
}

KeyframeEditFunc ANIM_editkeyframes_mirror(short mode)
{
  switch (mode) {
    case MIRROR_KEYS_CURFRAME: /* mirror over current frame */
      return mirror_bezier_cframe;
    case MIRROR_KEYS_YAXIS: /* mirror over frame 0 */
      return mirror_bezier_yaxis;
    case MIRROR_KEYS_XAXIS: /* mirror over value 0 */
      return mirror_bezier_xaxis;
    case MIRROR_KEYS_MARKER: /* mirror over marker */
      return mirror_bezier_marker;
    case MIRROR_KEYS_TIME: /* mirror over frame/time */
      return mirror_bezier_time;
    case MIRROR_KEYS_VALUE: /* mirror over given value */
      return mirror_bezier_value;
    default: /* just in case */
      return mirror_bezier_yaxis;
  }
}

/* ******************************************* */
/* Settings */

/**
 * Standard validation step for a few of these
 * (implemented as macro for inlining without fn-call overhead):
 * "if the handles are not of the same type, set them to type free".
 */
#define ENSURE_HANDLES_MATCH(bezt) \
  if (bezt->h1 != bezt->h2) { \
    if (ELEM(bezt->h1, HD_ALIGN, HD_AUTO, HD_AUTO_ANIM)) { \
      bezt->h1 = HD_FREE; \
    } \
    if (ELEM(bezt->h2, HD_ALIGN, HD_AUTO, HD_AUTO_ANIM)) { \
      bezt->h2 = HD_FREE; \
    } \
  } \
  (void)0

/* Sets the selected bezier handles to type 'auto' */
static short set_bezier_auto(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  /* If the key is selected, always apply to both handles. */
  if (bezt->f2 & SELECT) {
    bezt->h1 = bezt->h2 = HD_AUTO;
  }
  else {
    if (bezt->f1 & SELECT) {
      bezt->h1 = HD_AUTO;
    }
    if (bezt->f3 & SELECT) {
      bezt->h2 = HD_AUTO;
    }

    ENSURE_HANDLES_MATCH(bezt);
  }

  return 0;
}

/* Sets the selected bezier handles to type 'auto-clamped'
 * NOTE: this is like auto above, but they're handled a bit different
 */
static short set_bezier_auto_clamped(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  /* If the key is selected, always apply to both handles. */
  if (bezt->f2 & SELECT) {
    bezt->h1 = bezt->h2 = HD_AUTO_ANIM;
  }
  else {
    if (bezt->f1 & SELECT) {
      bezt->h1 = HD_AUTO_ANIM;
    }
    if (bezt->f3 & SELECT) {
      bezt->h2 = HD_AUTO_ANIM;
    }

    ENSURE_HANDLES_MATCH(bezt);
  }

  return 0;
}

/* Sets the selected bezier handles to type 'vector'. */
static short set_bezier_vector(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  /* If the key is selected, always apply to both handles. */
  if (bezt->f2 & SELECT) {
    bezt->h1 = bezt->h2 = HD_VECT;
  }
  else {
    if (bezt->f1 & SELECT) {
      bezt->h1 = HD_VECT;
    }
    if (bezt->f3 & SELECT) {
      bezt->h2 = HD_VECT;
    }
  }

  return 0;
}

/**
 * Queries if the handle should be set to 'free' or 'align'.
 *
 * \note This was used for the 'toggle free/align' option
 * currently this isn't used, but may be restored later.
 */
static short bezier_isfree(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  if ((bezt->f1 & SELECT) && (bezt->h1)) {
    return 1;
  }
  if ((bezt->f3 & SELECT) && (bezt->h2)) {
    return 1;
  }
  return 0;
}

/* Sets selected bezier handles to type 'align' */
static short set_bezier_align(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  /* If the key is selected, always apply to both handles. */
  if (bezt->f2 & SELECT) {
    bezt->h1 = bezt->h2 = HD_ALIGN;
  }
  else {
    if (bezt->f1 & SELECT) {
      bezt->h1 = HD_ALIGN;
    }
    if (bezt->f3 & SELECT) {
      bezt->h2 = HD_ALIGN;
    }
  }

  return 0;
}

/* Sets selected bezier handles to type 'free'. */
static short set_bezier_free(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  /* If the key is selected, always apply to both handles. */
  if (bezt->f2 & SELECT) {
    bezt->h1 = bezt->h2 = HD_FREE;
  }
  else {
    if (bezt->f1 & SELECT) {
      bezt->h1 = HD_FREE;
    }
    if (bezt->f3 & SELECT) {
      bezt->h2 = HD_FREE;
    }
  }

  return 0;
}

KeyframeEditFunc ANIM_editkeyframes_handles(short mode)
{
  switch (mode) {
    case HD_AUTO: /* auto */
      return set_bezier_auto;
    case HD_AUTO_ANIM: /* auto clamped */
      return set_bezier_auto_clamped;

    case HD_VECT: /* vector */
      return set_bezier_vector;
    case HD_FREE: /* free */
      return set_bezier_free;
    case HD_ALIGN: /* align */
      return set_bezier_align;

    default: /* check for toggle free or align? */
      return bezier_isfree;
  }
}

/* ------- */

static short set_bezt_constant(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  if (bezt->f2 & SELECT) {
    bezt->ipo = BEZT_IPO_CONST;
  }
  return 0;
}

static short set_bezt_linear(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  if (bezt->f2 & SELECT) {
    bezt->ipo = BEZT_IPO_LIN;
  }
  return 0;
}

static short set_bezt_bezier(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  if (bezt->f2 & SELECT) {
    bezt->ipo = BEZT_IPO_BEZ;
  }
  return 0;
}

static short set_bezt_back(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  if (bezt->f2 & SELECT) {
    bezt->ipo = BEZT_IPO_BACK;
  }
  return 0;
}

static short set_bezt_bounce(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  if (bezt->f2 & SELECT) {
    bezt->ipo = BEZT_IPO_BOUNCE;
  }
  return 0;
}

static short set_bezt_circle(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  if (bezt->f2 & SELECT) {
    bezt->ipo = BEZT_IPO_CIRC;
  }
  return 0;
}

static short set_bezt_cubic(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  if (bezt->f2 & SELECT) {
    bezt->ipo = BEZT_IPO_CUBIC;
  }
  return 0;
}

static short set_bezt_elastic(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  if (bezt->f2 & SELECT) {
    bezt->ipo = BEZT_IPO_ELASTIC;
  }
  return 0;
}

static short set_bezt_expo(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  if (bezt->f2 & SELECT) {
    bezt->ipo = BEZT_IPO_EXPO;
  }
  return 0;
}

static short set_bezt_quad(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  if (bezt->f2 & SELECT) {
    bezt->ipo = BEZT_IPO_QUAD;
  }
  return 0;
}

static short set_bezt_quart(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  if (bezt->f2 & SELECT) {
    bezt->ipo = BEZT_IPO_QUART;
  }
  return 0;
}

static short set_bezt_quint(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  if (bezt->f2 & SELECT) {
    bezt->ipo = BEZT_IPO_QUINT;
  }
  return 0;
}

static short set_bezt_sine(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  if (bezt->f2 & SELECT) {
    bezt->ipo = BEZT_IPO_SINE;
  }
  return 0;
}

static void handle_flatten(float vec[3][3], const int idx, const float direction[2])
{
  BLI_assert_msg(idx == 0 || idx == 2, "handle_flatten() expects a handle index");

  add_v2_v2v2(vec[idx], vec[1], direction);
}

static void handle_set_length(float vec[3][3], const int idx, const float handle_length)
{
  BLI_assert_msg(idx == 0 || idx == 2, "handle_set_length() expects a handle index");

  float handle_direction[2];
  sub_v2_v2v2(handle_direction, vec[idx], vec[1]);
  normalize_v2_length(handle_direction, handle_length);
  add_v2_v2v2(vec[idx], vec[1], handle_direction);
}

void ANIM_fcurve_equalize_keyframes_loop(FCurve *fcu,
                                         const eEditKeyframes_Equalize mode,
                                         const float handle_length,
                                         const bool flatten)
{
  uint i;
  BezTriple *bezt;
  const float flat_direction_left[2] = {-handle_length, 0.f};
  const float flat_direction_right[2] = {handle_length, 0.f};

  /* Loop through an F-Curves keyframes. */
  for (bezt = fcu->bezt, i = 0; i < fcu->totvert; bezt++, i++) {
    if ((bezt->f2 & SELECT) == 0) {
      continue;
    }

    /* Perform handle equalization if mode is 'Both' or 'Left'. */
    if (mode & EQUALIZE_HANDLES_LEFT) {
      if (flatten) {
        handle_flatten(bezt->vec, 0, flat_direction_left);
      }
      else {
        handle_set_length(bezt->vec, 0, handle_length);
      }
    }

    /* Perform handle equalization if mode is 'Both' or 'Right'. */
    if (mode & EQUALIZE_HANDLES_RIGHT) {
      if (flatten) {
        handle_flatten(bezt->vec, 2, flat_direction_right);
      }
      else {
        handle_set_length(bezt->vec, 2, handle_length);
      }
    }
  }
}

KeyframeEditFunc ANIM_editkeyframes_ipo(short mode)
{
  switch (mode) {
    /* interpolation */
    case BEZT_IPO_CONST: /* constant */
      return set_bezt_constant;
    case BEZT_IPO_LIN: /* linear */
      return set_bezt_linear;

    /* easing */
    case BEZT_IPO_BACK:
      return set_bezt_back;
    case BEZT_IPO_BOUNCE:
      return set_bezt_bounce;
    case BEZT_IPO_CIRC:
      return set_bezt_circle;
    case BEZT_IPO_CUBIC:
      return set_bezt_cubic;
    case BEZT_IPO_ELASTIC:
      return set_bezt_elastic;
    case BEZT_IPO_EXPO:
      return set_bezt_expo;
    case BEZT_IPO_QUAD:
      return set_bezt_quad;
    case BEZT_IPO_QUART:
      return set_bezt_quart;
    case BEZT_IPO_QUINT:
      return set_bezt_quint;
    case BEZT_IPO_SINE:
      return set_bezt_sine;

    default: /* bezier */
      return set_bezt_bezier;
  }
}

/* ------- */

static short set_keytype_keyframe(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  if (bezt->f2 & SELECT) {
    BEZKEYTYPE(bezt) = BEZT_KEYTYPE_KEYFRAME;
  }
  return 0;
}

static short set_keytype_breakdown(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  if (bezt->f2 & SELECT) {
    BEZKEYTYPE(bezt) = BEZT_KEYTYPE_BREAKDOWN;
  }
  return 0;
}

static short set_keytype_extreme(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  if (bezt->f2 & SELECT) {
    BEZKEYTYPE(bezt) = BEZT_KEYTYPE_EXTREME;
  }
  return 0;
}

static short set_keytype_jitter(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  if (bezt->f2 & SELECT) {
    BEZKEYTYPE(bezt) = BEZT_KEYTYPE_JITTER;
  }
  return 0;
}

static short set_keytype_moving_hold(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  if (bezt->f2 & SELECT) {
    BEZKEYTYPE(bezt) = BEZT_KEYTYPE_MOVEHOLD;
  }
  return 0;
}

KeyframeEditFunc ANIM_editkeyframes_keytype(short mode)
{
  switch (mode) {
    case BEZT_KEYTYPE_BREAKDOWN: /* breakdown */
      return set_keytype_breakdown;

    case BEZT_KEYTYPE_EXTREME: /* extreme keyframe */
      return set_keytype_extreme;

    case BEZT_KEYTYPE_JITTER: /* jitter keyframe */
      return set_keytype_jitter;

    case BEZT_KEYTYPE_MOVEHOLD: /* moving hold */
      return set_keytype_moving_hold;

    case BEZT_KEYTYPE_KEYFRAME: /* proper keyframe */
    default:
      return set_keytype_keyframe;
  }
}

/* ------- */

static short set_easingtype_easein(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  if (bezt->f2 & SELECT) {
    bezt->easing = BEZT_IPO_EASE_IN;
  }
  return 0;
}

static short set_easingtype_easeout(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  if (bezt->f2 & SELECT) {
    bezt->easing = BEZT_IPO_EASE_OUT;
  }
  return 0;
}

static short set_easingtype_easeinout(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  if (bezt->f2 & SELECT) {
    bezt->easing = BEZT_IPO_EASE_IN_OUT;
  }
  return 0;
}

static short set_easingtype_easeauto(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  if (bezt->f2 & SELECT) {
    bezt->easing = BEZT_IPO_EASE_AUTO;
  }
  return 0;
}

KeyframeEditFunc ANIM_editkeyframes_easing(short mode)
{
  switch (mode) {
    case BEZT_IPO_EASE_IN: /* ease in */
      return set_easingtype_easein;

    case BEZT_IPO_EASE_OUT: /* ease out */
      return set_easingtype_easeout;

    case BEZT_IPO_EASE_IN_OUT: /* both */
      return set_easingtype_easeinout;

    default: /* auto */
      return set_easingtype_easeauto;
  }
}

/* ******************************************* */
/* Selection */

static short select_bezier_add(KeyframeEditData *ked, BezTriple *bezt)
{
  /* Only act on visible items, so check handle visibility state. */
  const bool handles_visible = ked && ((ked->iterflags & KEYFRAME_ITER_HANDLES_DEFAULT_INVISIBLE) ?
                                           (BEZT_ISSEL_ANY(bezt)) :
                                           true);

  /* if we've got info on what to select, use it, otherwise select all */
  if ((ked) && (ked->iterflags & KEYFRAME_ITER_INCL_HANDLES) && handles_visible) {
    if (ked->curflags & KEYFRAME_OK_KEY) {
      bezt->f2 |= SELECT;
    }
    if (ked->curflags & KEYFRAME_OK_H1) {
      bezt->f1 |= SELECT;
    }
    if (ked->curflags & KEYFRAME_OK_H2) {
      bezt->f3 |= SELECT;
    }
  }
  else {
    BEZT_SEL_ALL(bezt);
  }

  return 0;
}

static short select_bezier_subtract(KeyframeEditData *ked, BezTriple *bezt)
{
  /* Only act on visible items, so check handle visibility state. */
  const bool handles_visible = ked && ((ked->iterflags & KEYFRAME_ITER_HANDLES_DEFAULT_INVISIBLE) ?
                                           (BEZT_ISSEL_ANY(bezt)) :
                                           true);

  /* if we've got info on what to deselect, use it, otherwise deselect all */
  if ((ked) && (ked->iterflags & KEYFRAME_ITER_INCL_HANDLES) && handles_visible) {
    if (ked->curflags & KEYFRAME_OK_KEY) {
      bezt->f2 &= ~SELECT;
    }
    if (ked->curflags & KEYFRAME_OK_H1) {
      bezt->f1 &= ~SELECT;
    }
    if (ked->curflags & KEYFRAME_OK_H2) {
      bezt->f3 &= ~SELECT;
    }
  }
  else {
    BEZT_DESEL_ALL(bezt);
  }

  return 0;
}

static short select_bezier_invert(KeyframeEditData *UNUSED(ked), BezTriple *bezt)
{
  /* Invert the selection for the whole bezier triple */
  bezt->f2 ^= SELECT;
  if (bezt->f2 & SELECT) {
    bezt->f1 |= SELECT;
    bezt->f3 |= SELECT;
  }
  else {
    bezt->f1 &= ~SELECT;
    bezt->f3 &= ~SELECT;
  }
  return 0;
}

KeyframeEditFunc ANIM_editkeyframes_select(short selectmode)
{
  switch (selectmode) {
    case SELECT_ADD: /* add */
      return select_bezier_add;
    case SELECT_SUBTRACT: /* subtract */
      return select_bezier_subtract;
    case SELECT_INVERT: /* invert */
      return select_bezier_invert;
    default: /* replace (need to clear all, then add) */
      return select_bezier_add;
  }
}

/* ******************************************* */
/* Selection Maps */

/* Selection maps are simply fancy names for char arrays that store on/off
 * info for whether the selection status. The main purpose for these is to
 * allow extra info to be tagged to the keyframes without influencing their
 * values or having to be removed later.
 */

/* ----------- */

static short selmap_build_bezier_more(KeyframeEditData *ked, BezTriple *bezt)
{
  FCurve *fcu = ked->fcu;
  char *map = ked->data;
  int i = ked->curIndex;

  /* if current is selected, just make sure it stays this way */
  if (BEZT_ISSEL_ANY(bezt)) {
    map[i] = 1;
    return 0;
  }

  /* if previous is selected, that means that selection should extend across */
  if (i > 0) {
    BezTriple *prev = bezt - 1;

    if (BEZT_ISSEL_ANY(prev)) {
      map[i] = 1;
      return 0;
    }
  }

  /* if next is selected, that means that selection should extend across */
  if (i < (fcu->totvert - 1)) {
    BezTriple *next = bezt + 1;

    if (BEZT_ISSEL_ANY(next)) {
      map[i] = 1;
      return 0;
    }
  }

  return 0;
}

static short selmap_build_bezier_less(KeyframeEditData *ked, BezTriple *bezt)
{
  FCurve *fcu = ked->fcu;
  char *map = ked->data;
  int i = ked->curIndex;

  /* if current is selected, check the left/right keyframes
   * since it might need to be deselected (but otherwise no)
   */
  if (BEZT_ISSEL_ANY(bezt)) {
    /* if previous is not selected, we're on the tip of an iceberg */
    if (i > 0) {
      BezTriple *prev = bezt - 1;

      if (BEZT_ISSEL_ANY(prev) == 0) {
        return 0;
      }
    }
    else if (i == 0) {
      /* current keyframe is selected at an endpoint, so should get deselected */
      return 0;
    }

    /* if next is not selected, we're on the tip of an iceberg */
    if (i < (fcu->totvert - 1)) {
      BezTriple *next = bezt + 1;

      if (BEZT_ISSEL_ANY(next) == 0) {
        return 0;
      }
    }
    else if (i == (fcu->totvert - 1)) {
      /* current keyframe is selected at an endpoint, so should get deselected */
      return 0;
    }

    /* if we're still here, that means that keyframe should remain untouched */
    map[i] = 1;
  }

  return 0;
}

KeyframeEditFunc ANIM_editkeyframes_buildselmap(short mode)
{
  switch (mode) {
    case SELMAP_LESS: /* less */
      return selmap_build_bezier_less;

    case SELMAP_MORE: /* more */
    default:
      return selmap_build_bezier_more;
  }
}

/* ----------- */

short bezt_selmap_flush(KeyframeEditData *ked, BezTriple *bezt)
{
  const char *map = ked->data;
  short on = map[ked->curIndex];

  /* select or deselect based on whether the map allows it or not */
  if (on) {
    BEZT_SEL_ALL(bezt);
  }
  else {
    BEZT_DESEL_ALL(bezt);
  }

  return 0;
}