Bugfix #11738:
[blender.git] / source / blender / src / poselib.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2007, Blender Foundation
21  * This is a new part of Blender
22  *
23  * Contributor(s): Joshua Leung
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27  
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <stddef.h>
31 #include <string.h>
32 #include <math.h>
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_arithb.h"
41 #include "BLI_blenlib.h"
42 #include "BLI_dynstr.h"
43
44 #include "DNA_listBase.h"
45 #include "DNA_action_types.h"
46 #include "DNA_armature_types.h"
47 #include "DNA_curve_types.h"
48 #include "DNA_ipo_types.h"
49 #include "DNA_object_types.h"
50 #include "DNA_object_force.h"
51 #include "DNA_scene_types.h"
52 #include "DNA_userdef_types.h"
53
54 #include "BKE_action.h"
55 #include "BKE_armature.h"
56 #include "BKE_depsgraph.h"
57 #include "BKE_ipo.h"
58 #include "BKE_modifier.h"
59 #include "BKE_object.h"
60
61 #include "BKE_global.h"
62 #include "BKE_utildefines.h"
63
64 //#include "BIF_keyframing.h"
65 #include "BSE_editipo.h"
66
67 #include "BDR_drawaction.h"
68 #include "BSE_time.h"
69
70 #include "BIF_poselib.h"
71 #include "BIF_interface.h"
72 #include "BIF_editaction.h"
73 #include "BIF_space.h"
74 #include "BIF_screen.h"
75 #include "BIF_toets.h"
76 #include "BIF_toolbox.h"
77
78
79 #include "blendef.h"
80
81 #include "PIL_time.h"                   /* sleep                                */
82 #include "mydevice.h"
83
84 /* ************************************************************* */
85 /* == POSE-LIBRARY TOOL FOR BLENDER == 
86  *      
87  * Overview: 
88  *      This tool allows animators to store a set of frequently used poses to dump into
89  *      the active action to help in "budget" productions to quickly block out new actions.
90  *      It acts as a kind of "glorified clipboard for poses", allowing for naming of poses.
91  *
92  * Features:
93  *      - PoseLibs are simply normal Actions
94  *      - Each "pose" is simply a set of keyframes that occur on a particular frame
95  *              -> a set of TimeMarkers that belong to each Action, help 'label' where a 'pose' can be
96  *                 found in the Action
97  *      - The Scrollwheel or PageUp/Down buttons when used in a special mode or after pressing/holding
98  *        [a modifier] key, cycles through the poses available for the active pose's poselib, allowing the
99  *        animator to preview what action best suits that pose
100  */
101 /* ************************************************************* */
102
103 /* gets list of poses in poselib as a string usable for pupmenu() */
104 char *poselib_build_poses_menu (bAction *act, char title[])
105 {
106         DynStr *pupds= BLI_dynstr_new();
107         TimeMarker *marker;
108         char *str;
109         char buf[64];
110         int i;
111         
112         /* add title first */
113         sprintf(buf, "%s%%t|", title);
114         BLI_dynstr_append(pupds, buf);
115         
116         /* loop through markers, adding them */
117         for (marker=act->markers.first, i=1; marker; marker=marker->next, i++) {
118                 BLI_dynstr_append(pupds, marker->name);
119                 
120                 sprintf(buf, "%%x%d", i);
121                 BLI_dynstr_append(pupds, buf);
122                 
123                 if (marker->next)
124                         BLI_dynstr_append(pupds, "|");
125         }
126         
127         /* convert to normal MEM_malloc'd string */
128         str= BLI_dynstr_get_cstring(pupds);
129         BLI_dynstr_free(pupds);
130         
131         return str;
132 }
133
134 /* gets the first available frame in poselib to store a pose on 
135  *      - frames start from 1, and a pose should occur on every frame... 0 is error!
136  */
137 int poselib_get_free_index (bAction *act)
138 {
139         TimeMarker *marker;
140         int low=0, high=0;
141         
142         /* sanity checks */
143         if (ELEM(NULL, act, act->markers.first)) return 1;
144         
145         /* loop over poses finding various values (poses are not stored in chronological order) */
146         for (marker= act->markers.first; marker; marker= marker->next) {
147                 /* only increase low if value is 1 greater than low, to find "gaps" where
148                  * poses were removed from the poselib
149                  */
150                 if (marker->frame == (low + 1)) 
151                         low++;
152                 
153                 /* value replaces high if it is the highest value encountered yet */
154                 if (marker->frame > high) 
155                         high= marker->frame;
156         }
157         
158         /* - if low is not equal to high, then low+1 is a gap 
159          * - if low is equal to high, then high+1 is the next index (add at end) 
160          */
161         if (low < high) 
162                 return (low + 1);
163         else 
164                 return (high + 1);
165 }
166
167 /* returns the active pose for a poselib */
168 TimeMarker *poselib_get_active_pose (bAction *act)
169 {       
170         if ((act) && (act->active_marker))
171                 return BLI_findlink(&act->markers, act->active_marker-1);
172         else
173                 return NULL;
174 }
175
176 /* ************************************************************* */
177
178 /* Initialise a new poselib (whether it is needed or not) */
179 bAction *poselib_init_new (Object *ob)
180 {
181         /* sanity checks - only for armatures */
182         if (ELEM(NULL, ob, ob->pose))
183                 return NULL;
184         
185         /* init object's poselib action (unlink old one if there) */
186         if (ob->poselib)
187                 ob->poselib->id.us--;
188         ob->poselib= add_empty_action("PoseLib");
189         
190         return ob->poselib;
191 }
192
193 /* Initialise a new poselib (checks if that needs to happen) */
194 bAction *poselib_validate (Object *ob)
195 {
196         if (ELEM(NULL, ob, ob->pose))
197                 return NULL;
198         else if (ob->poselib == NULL)
199                 return poselib_init_new(ob);
200         else
201                 return ob->poselib;
202 }
203
204
205 /* This tool automagically generates/validates poselib data so that it corresponds to the data 
206  * in the action. This is for use in making existing actions usable as poselibs.
207  */
208 void poselib_validate_act (bAction *act)
209 {
210         ListBase keys = {NULL, NULL};
211         ActKeyColumn *ak;
212         TimeMarker *marker, *markern;
213         
214         /* validate action and poselib */
215         if (act == NULL)  {
216                 error("No Action to validate");
217                 return;
218         }
219         
220         /* determine which frames have keys */
221         action_to_keylist(act, &keys, NULL, NULL);
222         
223         /* for each key, make sure there is a correspnding pose */
224         for (ak= keys.first; ak; ak= ak->next) {
225                 /* check if any pose matches this */
226                 for (marker= act->markers.first; marker; marker= marker->next) {
227                         if (IS_EQ(marker->frame, ak->cfra)) {
228                                 marker->flag = -1;
229                                 break;
230                         }
231                 }
232                 
233                 /* add new if none found */
234                 if (marker == NULL) {
235                         char name[64];
236                         
237                         /* add pose to poselib */
238                         marker= MEM_callocN(sizeof(TimeMarker), "ActionMarker");
239                         
240                         strcpy(name, "Pose");
241                         BLI_strncpy(marker->name, name, sizeof(marker->name));
242                         
243                         marker->frame= (int)ak->cfra;
244                         marker->flag= -1;
245                         
246                         BLI_addtail(&act->markers, marker);
247                 }
248         }
249         
250         /* remove all untagged poses (unused), and remove all tags */
251         for (marker= act->markers.first; marker; marker= markern) {
252                 markern= marker->next;
253                 
254                 if (marker->flag != -1)
255                         BLI_freelinkN(&act->markers, marker);
256                 else
257                         marker->flag = 0;
258         }
259         
260         /* free temp memory */
261         BLI_freelistN(&keys);
262         
263         BIF_undo_push("PoseLib Validate Action");
264 }
265
266 /* ************************************************************* */
267
268 /* This function adds an ipo-curve of the right type where it's needed */
269 static IpoCurve *poselib_verify_icu (Ipo *ipo, int adrcode)
270 {
271         IpoCurve *icu;
272         
273         for (icu= ipo->curve.first; icu; icu= icu->next) {
274                 if (icu->adrcode==adrcode) break;
275         }
276         if (icu==NULL) {
277                 icu= MEM_callocN(sizeof(IpoCurve), "ipocurve");
278                 
279                 icu->flag |= IPO_VISIBLE|IPO_AUTO_HORIZ;
280                 if (ipo->curve.first==NULL) icu->flag |= IPO_ACTIVE;    /* first one added active */
281                 
282                 icu->blocktype= ID_PO;
283                 icu->adrcode= adrcode;
284                 
285                 set_icu_vars(icu);
286                 
287                 BLI_addtail(&ipo->curve, icu);
288         }
289         
290         return icu;
291 }
292
293 /* This tool adds the current pose to the poselib 
294  *      Note: Standard insertkey cannot be used for this due to its limitations
295  */
296 void poselib_add_current_pose (Object *ob, int val)
297 {
298         bArmature *arm= (ob) ? ob->data : NULL;
299         bPose *pose= (ob) ? ob->pose : NULL;
300         bPoseChannel *pchan;
301         TimeMarker *marker;
302         bAction *act;
303         bActionChannel *achan;
304         IpoCurve *icu;
305         int frame;
306         char name[64];
307         
308         /* sanity check */
309         if (ELEM3(NULL, ob, arm, pose)) 
310                 return;
311         
312         /* mode - add new or replace existing */
313         if (val == 0) {
314                 if ((ob->poselib) && (ob->poselib->markers.first)) {
315                         val= pupmenu("PoseLib Add Current Pose%t|Add New%x1|Replace Existing%x2");
316                         if (val <= 0) return;
317                 }
318                 else 
319                         val= 1;
320         }
321         
322         if ((ob->poselib) && (val == 2)) {
323                 char *menustr;
324                 
325                 /* get poselib */
326                 act= ob->poselib;
327                 
328                 /* get the pose to replace */
329                 menustr= poselib_build_poses_menu(act, "Replace PoseLib Pose");
330                 val= pupmenu_col(menustr, 20);
331                 if (menustr) MEM_freeN(menustr);
332                 
333                 if (val <= 0) return;
334                 marker= BLI_findlink(&act->markers, val-1);
335                 if (marker == NULL) return;
336                 
337                 /* get the frame from the poselib */
338                 frame= marker->frame;
339         }
340         else {
341                 /* get name of pose */
342                 sprintf(name, "Pose");
343                 if (sbutton(name, 0, sizeof(name)-1, "Name: ") == 0)
344                         return;
345                         
346                 /* get/initialise poselib */
347                 act= poselib_validate(ob);
348                 
349                 /* get frame */
350                 frame= poselib_get_free_index(act);
351                 
352                 /* add pose to poselib - replaces any existing pose there */
353                 for (marker= act->markers.first; marker; marker= marker->next) {
354                         if (marker->frame == frame) {
355                                 BLI_strncpy(marker->name, name, sizeof(marker->name));
356                                 break;
357                         }
358                 }
359                 if (marker == NULL) {
360                         marker= MEM_callocN(sizeof(TimeMarker), "ActionMarker");
361                         
362                         BLI_strncpy(marker->name, name, sizeof(marker->name));
363                         marker->frame= frame;
364                         
365                         BLI_addtail(&act->markers, marker);
366                 }
367                 
368                 /* validate name */
369                 BLI_uniquename(&act->markers, marker, "Pose", offsetof(TimeMarker, name), 64);
370         }       
371         
372         /* loop through selected posechannels, keying their pose to the action */
373         for (pchan= pose->chanbase.first; pchan; pchan= pchan->next) {
374                 /* check if available */
375                 if ((pchan->bone) && (arm->layer & pchan->bone->layer)) {
376                         if (pchan->bone->flag & (BONE_SELECTED|BONE_ACTIVE)) {
377                                 /* make action-channel if needed (action groups are also created) */
378                                 achan= verify_action_channel(act, pchan->name);
379                                 verify_pchan2achan_grouping(act, pose, pchan->name);
380                                 
381                                 /* make ipo if needed... */
382                                 if (achan->ipo == NULL)
383                                         achan->ipo= add_ipo(achan->name, ID_PO);
384                                         
385                                 /* add missing ipo-curves and insert keys */
386                                 #define INSERT_KEY_ICU(adrcode, data) {\
387                                                 icu= poselib_verify_icu(achan->ipo, adrcode); \
388                                                 insert_vert_icu(icu, frame, data, 1); \
389                                         }
390                                         
391                                 INSERT_KEY_ICU(AC_LOC_X, pchan->loc[0])
392                                 INSERT_KEY_ICU(AC_LOC_Y, pchan->loc[1])
393                                 INSERT_KEY_ICU(AC_LOC_Z, pchan->loc[2])
394                                 INSERT_KEY_ICU(AC_SIZE_X, pchan->size[0])
395                                 INSERT_KEY_ICU(AC_SIZE_Y, pchan->size[1])
396                                 INSERT_KEY_ICU(AC_SIZE_Z, pchan->size[2])
397                                 INSERT_KEY_ICU(AC_QUAT_W, pchan->quat[0])
398                                 INSERT_KEY_ICU(AC_QUAT_X, pchan->quat[1])
399                                 INSERT_KEY_ICU(AC_QUAT_Y, pchan->quat[2])
400                                 INSERT_KEY_ICU(AC_QUAT_Z, pchan->quat[3])
401                         }
402                 }
403         }
404         
405         /* store new 'active' pose number */
406         act->active_marker= BLI_countlist(&act->markers);
407         
408         BIF_undo_push("PoseLib Add Pose");
409         allqueue(REDRAWBUTSEDIT, 0);
410 }
411
412
413 /* This tool removes the pose that the user selected from the poselib (or the provided pose) */
414 void poselib_remove_pose (Object *ob, TimeMarker *marker)
415 {
416         bPose *pose= (ob) ? ob->pose : NULL;
417         bAction *act= (ob) ? ob->poselib : NULL;
418         bActionChannel *achan;
419         char *menustr;
420         int val;
421         
422         /* check if valid poselib */
423         if (ELEM(NULL, ob, pose)) {
424                 error("PoseLib is only for Armatures in PoseMode");
425                 return;
426         }
427         if (act == NULL) {
428                 error("Object doesn't have PoseLib data");
429                 return;
430         }
431         
432         /* get index (and pointer) of pose to remove */
433         if (marker == NULL) {
434                 menustr= poselib_build_poses_menu(act, "Remove PoseLib Pose");
435                 val= pupmenu_col(menustr, 20);
436                 if (menustr) MEM_freeN(menustr);
437                 
438                 if (val <= 0) return;
439                 marker= BLI_findlink(&act->markers, val-1);
440                 if (marker == NULL) return;
441         }
442         else {
443                 /* only continue if pose belongs to poselib */
444                 if (BLI_findindex(&act->markers, marker) == -1) 
445                         return;
446         }
447         
448         /* remove relevant keyframes */
449         for (achan= act->chanbase.first; achan; achan= achan->next) {
450                 Ipo *ipo= achan->ipo;
451                 IpoCurve *icu;
452                 BezTriple *bezt;
453                 int i;
454                 
455                 for (icu= ipo->curve.first; icu; icu= icu->next) {
456                         for (i=0, bezt=icu->bezt; i < icu->totvert; i++, bezt++) {
457                                 /* check if remove... */
458                                 if (IS_EQ(bezt->vec[1][0], marker->frame)) {
459                                         delete_icu_key(icu, i, 1);
460                                         break;
461                                 }
462                         }       
463                 }
464         }
465         
466         /* remove poselib from list */
467         BLI_freelinkN(&act->markers, marker);
468         
469         /* fix active pose number */
470         act->active_marker= 0;
471         
472         /* undo + redraw */
473         BIF_undo_push("PoseLib Remove Pose");
474         allqueue(REDRAWBUTSEDIT, 0);
475         allqueue(REDRAWACTION, 0);
476 }
477
478
479 /* This tool renames the pose that the user selected from the poselib */
480 void poselib_rename_pose (Object *ob)
481 {
482         bPose *pose= (ob) ? ob->pose : NULL;
483         bAction *act= (ob) ? ob->poselib : NULL;
484         TimeMarker *marker;
485         char *menustr, name[64];
486         int val;
487         
488         /* check if valid poselib */
489         if (ELEM(NULL, ob, pose)) {
490                 error("PoseLib is only for Armatures in PoseMode");
491                 return;
492         }
493         if (act == NULL) {
494                 error("Object doesn't have a valid PoseLib");
495                 return;
496         }
497         
498         /* get index of pose to remove */
499         menustr= poselib_build_poses_menu(act, "Rename PoseLib Pose");
500         val= pupmenu_col(menustr, 20);
501         if (menustr) MEM_freeN(menustr);
502         
503         if (val <= 0) return;
504         marker= BLI_findlink(&act->markers, val-1);
505         if (marker == NULL) return;
506         
507         /* get name of pose */
508         sprintf(name, marker->name);
509         if (sbutton(name, 0, sizeof(name)-1, "Name: ") == 0)
510                 return;
511         
512         /* copy name and validate it */
513         BLI_strncpy(marker->name, name, sizeof(marker->name));
514         BLI_uniquename(&act->markers, marker, "Pose", offsetof(TimeMarker, name), 64);
515         
516         /* undo and update */
517         BIF_undo_push("PoseLib Rename Pose");
518         allqueue(REDRAWBUTSEDIT, 0);
519         allqueue(REDRAWACTION, 0);
520 }
521
522
523 /* ************************************************************* */
524
525 /* Simple struct for storing settings/data for use during PoseLib preview */
526 typedef struct tPoseLib_PreviewData {
527         ListBase backups;               /* tPoseLib_Backup structs for restoring poses */
528         ListBase searchp;               /* LinkData structs storing list of poses which match the current search-string */
529         
530         Object *ob;                             /* object to work on */
531         bArmature *arm;                 /* object's armature data */
532         bPose *pose;                    /* object's pose */
533         bAction *act;                   /* poselib to use */
534         TimeMarker *marker;             /* 'active' pose */
535         
536         short state;                    /* state of main loop */
537         short redraw;                   /* redraw/update settings during main loop */
538         short flag;                             /* flags for various settings */
539         
540         int selcount;                   /* number of selected elements to work on */
541         int totcount;                   /* total number of elements to work on */
542         
543         char headerstr[200];    /* Info-text to print in header */
544         
545         char searchstr[64];             /* (Part of) Name to search for to filter poses that get shown */
546         char searchold[64];             /* Previously set searchstr (from last loop run), so that we can detected when to rebuild searchp */
547         short search_cursor;    /* position of cursor in searchstr (cursor occurs before the item at the nominated index) */
548 } tPoseLib_PreviewData;
549
550 /* defines for tPoseLib_PreviewData->state values */
551 enum {
552         PL_PREVIEW_ERROR = -1,
553         PL_PREVIEW_RUNNING,
554         PL_PREVIEW_CONFIRM,
555         PL_PREVIEW_CANCEL,
556         PL_PREVIEW_RUNONCE 
557 };
558
559 /* defines for tPoseLib_PreviewData->redraw values */
560 enum {
561         PL_PREVIEW_NOREDRAW = 0,
562         PL_PREVIEW_REDRAWALL,
563         PL_PREVIEW_REDRAWHEADER,
564 };
565
566 /* defines for tPoseLib_PreviewData->flag values */
567 enum {
568         PL_PREVIEW_FIRSTTIME    = (1<<0),
569         PL_PREVIEW_SHOWORIGINAL = (1<<1)
570 };
571
572 /* ---------------------------- */
573
574 /* simple struct for storing backup info */
575 typedef struct tPoseLib_Backup {
576         struct tPoseLib_Backup *next, *prev;
577         
578         bPoseChannel *pchan;
579         bPoseChannel olddata;
580 } tPoseLib_Backup;
581
582 /* Makes a copy of the current pose for restoration purposes - doesn't do constraints currently */
583 static void poselib_backup_posecopy (tPoseLib_PreviewData *pld)
584 {
585         bActionChannel *achan;
586         bPoseChannel *pchan;
587         
588         /* for each posechannel that has an actionchannel in */
589         for (achan= pld->act->chanbase.first; achan; achan= achan->next) {
590                 /* try to find posechannel */
591                 pchan= get_pose_channel(pld->pose, achan->name);
592                 
593                 /* backup data if available */
594                 if (pchan) {
595                         tPoseLib_Backup *plb;
596                         
597                         /* store backup */
598                         plb= MEM_callocN(sizeof(tPoseLib_Backup), "tPoseLib_Backup");
599                         
600                         plb->pchan= pchan;
601                         memcpy(&plb->olddata, plb->pchan, sizeof(bPoseChannel));
602                         
603                         BLI_addtail(&pld->backups, plb);
604                         
605                         /* mark as being affected */
606                         if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED))
607                                 pld->selcount++;
608                         pld->totcount++;
609                 }
610         }
611 }
612
613 /* Restores original pose - doesn't do constraints currently */
614 static void poselib_backup_restore (tPoseLib_PreviewData *pld)
615 {
616         tPoseLib_Backup *plb;
617         
618         for (plb= pld->backups.first; plb; plb= plb->next) {
619                 memcpy(plb->pchan, &plb->olddata, sizeof(bPoseChannel));
620         }
621 }
622
623 /* ---------------------------- */
624
625 /* Applies the appropriate stored pose from the pose-library to the current pose
626  *      - assumes that a valid object, with a poselib has been supplied
627  *      - gets the string to print in the header
628  *      - this code is based on the code for extract_pose_from_action in blenkernel/action.c
629  */
630 static void poselib_apply_pose (tPoseLib_PreviewData *pld)
631 {
632         bPose *pose= pld->pose;
633         bPoseChannel *pchan;
634         bAction *act= pld->act;
635         bActionChannel *achan;
636         IpoCurve *icu;
637         int frame;
638         
639         if (pld->marker)
640                 frame= pld->marker->frame;
641         else
642                 return; 
643         
644         /* start applying - only those channels which have a key at this point in time! */
645         for (achan= act->chanbase.first; achan; achan= achan->next) {
646                 short found= 0;
647                 
648                 /* apply this achan? */
649                 if (achan->ipo) {
650                         /* find a keyframe at this frame - users may not have defined the pose on every channel, so this is necessary */
651                         // TODO: this may be bad for user-defined poses...
652                         for (icu= achan->ipo->curve.first; icu; icu= icu->next) {
653                                 BezTriple *bezt;
654                                 int i;
655                                 
656                                 for (i=0, bezt=icu->bezt; i < icu->totvert; i++, bezt++) {
657                                         if (IN_RANGE(bezt->vec[1][0], (frame-0.5f), (frame+0.5f))) {
658                                                 found= 1;
659                                                 break;
660                                         }
661                                 }
662                                 
663                                 if (found) break;
664                         }
665                         
666                         /* apply pose - only if posechannel selected? */
667                         if (found) {
668                                 pchan= get_pose_channel(pose, achan->name);
669                                 
670                                 if (pchan) {    
671                                         short ok= 0;
672                                         
673                                         if (pchan->bone) {
674                                                 if ( (pchan->bone->flag & (BONE_SELECTED|BONE_ACTIVE)) &&
675                                                          (pchan->bone->flag & BONE_HIDDEN_P)==0 )
676                                                         ok = 1;
677                                                 else if (pld->selcount == 0)
678                                                         ok= 1;
679                                         }
680                                         else if (pld->selcount == 0)
681                                                 ok= 1;
682                                         
683                                         if (ok) {
684                                                 /* Evaluates and sets the internal ipo values   */
685                                                 calc_ipo(achan->ipo, frame);
686                                                 /* This call also sets the pchan flags */
687                                                 execute_action_ipo(achan, pchan);
688                                         }
689                                 }
690                         }
691                 }
692                 
693                 /* tag achan as having been used or not... */
694                 if (found)
695                         achan->flag |= ACHAN_SELECTED;
696                 else
697                         achan->flag &= ~ACHAN_SELECTED;
698         }
699 }
700
701 /* Auto-keys/tags bones affected by the pose used from the poselib */
702 static void poselib_keytag_pose (tPoseLib_PreviewData *pld)
703 {
704         bPose *pose= pld->pose;
705         bPoseChannel *pchan;
706         bAction *act= pld->act;
707         bActionChannel *achan;
708         
709         /* start tagging/keying */
710         for (achan= act->chanbase.first; achan; achan= achan->next) {
711                 /* only for selected action channels */
712                 if (achan->flag & ACHAN_SELECTED) {
713                         pchan= get_pose_channel(pose, achan->name);
714                         
715                         if (pchan) {
716                                 // TODO: use a standard autokeying function in future (to allow autokeying-editkeys to work)
717                                 if (IS_AUTOKEY_MODE(NORMAL)) {
718                                         ID *id= &pld->ob->id;
719                                         
720                                         /* Set keys on pose */
721                                         if (pchan->flag & POSE_ROT) {
722                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_X, 0);
723                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Y, 0);
724                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_Z, 0);
725                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_QUAT_W, 0);
726                                         }
727                                         if (pchan->flag & POSE_SIZE) {
728                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_X, 0);
729                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Y, 0);
730                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_SIZE_Z, 0);
731                                         }
732                                         if (pchan->flag & POSE_LOC) {
733                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_X, 0);
734                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Y, 0);
735                                                 insertkey(id, ID_PO, pchan->name, NULL, AC_LOC_Z, 0);
736                                         }
737                                         
738                                         /* clear any unkeyed tags */
739                                         if (pchan->bone)
740                                                 pchan->bone->flag &= ~BONE_UNKEYED;
741                                 }
742                                 else {
743                                         /* add unkeyed tags */
744                                         if (pchan->bone)
745                                                 pchan->bone->flag |= BONE_UNKEYED;
746                                 }
747                         }
748                 }
749         }
750 }
751
752 /* ---------------------------- */
753
754 /* This helper function is called during poselib_preview_poses to find the 
755  * pose to preview next (after a change event)
756  */
757 static void poselib_preview_get_next (tPoseLib_PreviewData *pld, int step)
758 {
759         if ((pld->marker) && (step)) {
760                 /* search-string dictates a special approach */
761                 if (pld->searchstr[0]) {
762                         TimeMarker *marker;
763                         LinkData *ld, *ldn, *ldc;
764                         
765                         /* free and rebuild if needed (i.e. if search-str changed) */
766                         if (strcmp(pld->searchstr, pld->searchold)) {
767                                 /* free list of temporary search matches */
768                                 BLI_freelistN(&pld->searchp);
769                                 
770                                 /* generate a new list of search matches */
771                                 for (marker= pld->act->markers.first; marker; marker= marker->next) {
772                                         /* does the name partially match? 
773                                          *      - don't worry about case, to make it easier for users to quickly input a name (or 
774                                          *        part of one), which is the whole point of this feature
775                                          */
776                                         if (BLI_strcasestr(marker->name, pld->searchstr)) {
777                                                 /* make link-data to store reference to it */
778                                                 ld= MEM_callocN(sizeof(LinkData), "PoseMatch");
779                                                 ld->data= marker;
780                                                 BLI_addtail(&pld->searchp, ld);
781                                         }
782                                 }
783                                 
784                                 /* set current marker to NULL (so that we start from first) */
785                                 pld->marker= NULL;
786                         }
787                         
788                         /* check if any matches */
789                         if (pld->searchp.first == NULL) { 
790                                 pld->marker= NULL;
791                                 return;
792                         }
793                         
794                         /* find first match */
795                         for (ldc= pld->searchp.first; ldc; ldc= ldc->next) {
796                                 if (ldc->data == pld->marker)
797                                         break;
798                         }
799                         if (ldc == NULL)
800                                 ldc= pld->searchp.first;
801                                 
802                         /* Loop through the matches in a cyclic fashion, incrementing/decrementing step as appropriate 
803                          * until step == 0. At this point, marker should be the correct marker.
804                          */
805                         if (step > 0) {
806                                 for (ld=ldc; ld && step; ld=ldn, step--)
807                                         ldn= (ld->next) ? ld->next : pld->searchp.first;
808                         }
809                         else {
810                                 for (ld=ldc; ld && step; ld=ldn, step++)
811                                         ldn= (ld->prev) ? ld->prev : pld->searchp.last;
812                         }
813                         
814                         /* set marker */
815                         if (ld)
816                                 pld->marker= ld->data;
817                 }
818                 else {
819                         TimeMarker *marker, *next;
820                         
821                         /* Loop through the markers in a cyclic fashion, incrementing/decrementing step as appropriate 
822                          * until step == 0. At this point, marker should be the correct marker.
823                          */
824                         if (step > 0) {
825                                 for (marker=pld->marker; marker && step; marker=next, step--)
826                                         next= (marker->next) ? marker->next : pld->act->markers.first;
827                         }
828                         else {
829                                 for (marker=pld->marker; marker && step; marker=next, step++)
830                                         next= (marker->prev) ? marker->prev : pld->act->markers.last;
831                         }
832                         
833                         /* it should be fairly impossible for marker to be NULL */
834                         if (marker)
835                                 pld->marker= marker;
836                 }
837         }
838 }
839
840 /* specially handle events for searching */
841 static void poselib_preview_handle_search (tPoseLib_PreviewData *pld, unsigned short event, char ascii)
842 {
843         if (ascii) {
844                 /* character to add to the string */
845                 short index= pld->search_cursor;
846                 short len= (pld->searchstr[0]) ? strlen(pld->searchstr) : 0;
847                 short i;
848                 
849                 if (len) {
850                         for (i = len; i > index; i--)  
851                                 pld->searchstr[i]= pld->searchstr[i-1];
852                 }
853                 else
854                         pld->searchstr[1]= 0;
855                         
856                 pld->searchstr[index]= ascii;
857                 pld->search_cursor++;
858                 
859                 poselib_preview_get_next(pld, 1);
860                 pld->redraw = PL_PREVIEW_REDRAWALL;
861         }
862         else {
863                 /* some form of string manipulation */
864                 switch (event) {
865                         case BACKSPACEKEY:
866                                 if (pld->searchstr[0] && pld->search_cursor) {
867                                         short len= strlen(pld->searchstr);
868                                         short index= pld->search_cursor;
869                                         short i;
870                                         
871                                         for (i = index; i <= len; i++) 
872                                                 pld->searchstr[i-1] = pld->searchstr[i];
873                                         
874                                         pld->search_cursor--;
875                                         
876                                         poselib_preview_get_next(pld, 1);
877                                         pld->redraw = PL_PREVIEW_REDRAWALL;
878                                 }       
879                                 break;
880                                 
881                         case DELKEY:
882                                 if (pld->searchstr[0] && pld->searchstr[1]) {
883                                         short len= strlen(pld->searchstr);
884                                         short index= pld->search_cursor;
885                                         int i;
886                                         
887                                         if (index < len) {
888                                                 for (i = index; i < len; i++) 
889                                                         pld->searchstr[i] = pld->searchstr[i+1];
890                                                         
891                                                 poselib_preview_get_next(pld, 1);
892                                                 pld->redraw = PL_PREVIEW_REDRAWALL;
893                                         }
894                                 }
895                                 break;
896                 }
897         }
898 }
899
900 /* handle events for poselib_preview_poses */
901 static void poselib_preview_handle_event (tPoseLib_PreviewData *pld, unsigned short event, char ascii)
902 {
903         /* backup stuff that needs to occur before every operation
904          *      - make a copy of searchstr, so that we know if cache needs to be rebuilt
905          */
906         strcpy(pld->searchold, pld->searchstr);
907         
908         /* if we're currently showing the original pose, only certain events are handled */
909         if (pld->flag & PL_PREVIEW_SHOWORIGINAL) {
910                 switch (event) {
911                         /* exit - cancel */
912                         case ESCKEY:
913                         case RIGHTMOUSE:
914                                 pld->state= PL_PREVIEW_CANCEL;
915                                 break;
916                                 
917                         /* exit - confirm */
918                         case LEFTMOUSE:
919                         case RETKEY:
920                         case PADENTER:
921                         case SPACEKEY:
922                                 pld->state= PL_PREVIEW_CONFIRM;
923                                 break;
924                         
925                         /* view manipulation */
926                         case MIDDLEMOUSE:
927                                 // there's a little bug here that causes the normal header to get drawn while view is manipulated 
928                                 handle_view_middlemouse();
929                                 pld->redraw= PL_PREVIEW_REDRAWHEADER;
930                                 break;
931                                 
932                         /* view manipulation, or searching */
933                         case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
934                         case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
935                         case PADPLUSKEY: case PADMINUS:
936                                 persptoetsen(event);
937                                 pld->redraw= PL_PREVIEW_REDRAWHEADER;
938                                 break;
939                                 
940                         case TABKEY:
941                                 pld->flag &= ~PL_PREVIEW_SHOWORIGINAL;
942                                 pld->redraw= PL_PREVIEW_REDRAWALL;
943                                 break;
944                 }
945                 
946                 /* EXITS HERE... */
947                 return;
948         }
949         
950         /* NORMAL EVENT HANDLING... */
951         /* searching takes priority over normal activity */
952         switch (event) {
953                 /* exit - cancel */
954                 case ESCKEY:
955                 case RIGHTMOUSE:
956                         pld->state= PL_PREVIEW_CANCEL;
957                         break;
958                         
959                 /* exit - confirm */
960                 case LEFTMOUSE:
961                 case RETKEY:
962                 case PADENTER:
963                 case SPACEKEY:
964                         pld->state= PL_PREVIEW_CONFIRM;
965                         break;
966                         
967                 /* toggle between original pose and poselib pose*/
968                 case TABKEY:
969                         pld->flag |= PL_PREVIEW_SHOWORIGINAL;
970                         pld->redraw= PL_PREVIEW_REDRAWALL;
971                         break;
972                 
973                 /* change to previous pose (cyclic) */
974                 case PAGEUPKEY:
975                 case WHEELUPMOUSE:
976                         poselib_preview_get_next(pld, -1);
977                         pld->redraw= PL_PREVIEW_REDRAWALL;
978                         break;
979                 
980                 /* change to next pose (cyclic) */
981                 case PAGEDOWNKEY:
982                 case WHEELDOWNMOUSE:
983                         poselib_preview_get_next(pld, 1);
984                         pld->redraw= PL_PREVIEW_REDRAWALL;
985                         break;
986                 
987                 /* jump 5 poses (cyclic, back) */
988                 case DOWNARROWKEY:
989                         poselib_preview_get_next(pld, -5);
990                         pld->redraw= PL_PREVIEW_REDRAWALL;
991                         break;
992                 
993                 /* jump 5 poses (cyclic, forward) */
994                 case UPARROWKEY:
995                         poselib_preview_get_next(pld, 5);
996                         pld->redraw= PL_PREVIEW_REDRAWALL;
997                         break;
998                 
999                 /* change to next pose or searching cursor control */
1000                 case RIGHTARROWKEY:
1001                         if (pld->searchstr[0]) {
1002                                 /* move text-cursor to the right */
1003                                 if (pld->search_cursor < strlen(pld->searchstr))
1004                                         pld->search_cursor++;
1005                                 pld->redraw= PL_PREVIEW_REDRAWHEADER;
1006                         }
1007                         else {
1008                                 /* change to next pose (cyclic) */
1009                                 poselib_preview_get_next(pld, 1);
1010                                 pld->redraw= PL_PREVIEW_REDRAWALL;
1011                         }
1012                         break;
1013                         
1014                 /* change to next pose or searching cursor control */
1015                 case LEFTARROWKEY:
1016                         if (pld->searchstr[0]) {
1017                                 /* move text-cursor to the left */
1018                                 if (pld->search_cursor)
1019                                         pld->search_cursor--;
1020                                 pld->redraw= PL_PREVIEW_REDRAWHEADER;
1021                         }
1022                         else {
1023                                 /* change to previous pose (cyclic) */
1024                                 poselib_preview_get_next(pld, -1);
1025                                 pld->redraw= PL_PREVIEW_REDRAWALL;
1026                         }
1027                         break;
1028                         
1029                 /* change to first pose or start of searching string */
1030                 case HOMEKEY:
1031                         if (pld->searchstr[0]) {
1032                                 pld->search_cursor= 0;
1033                                 pld->redraw= PL_PREVIEW_REDRAWHEADER;
1034                         }
1035                         else {
1036                                 /* change to first pose */
1037                                 pld->marker= pld->act->markers.first;
1038                                 pld->act->active_marker= 1;
1039                                 
1040                                 pld->redraw= PL_PREVIEW_REDRAWALL;
1041                         }
1042                         break;
1043                         
1044                 /* change to last pose or start of searching string */
1045                 case ENDKEY:
1046                         if (pld->searchstr[0]) {
1047                                 pld->search_cursor= strlen(pld->searchstr);
1048                                 pld->redraw= PL_PREVIEW_REDRAWHEADER;
1049                         }
1050                         else {
1051                                 /* change to last pose */
1052                                 pld->marker= pld->act->markers.last;
1053                                 pld->act->active_marker= BLI_countlist(&pld->act->markers);
1054                                 
1055                                 pld->redraw= PL_PREVIEW_REDRAWALL;
1056                         }
1057                         break;
1058                         
1059                 /* view manipulation */
1060                 case MIDDLEMOUSE:
1061                         // there's a little bug here that causes the normal header to get drawn while view is manipulated 
1062                         handle_view_middlemouse();
1063                         pld->redraw= PL_PREVIEW_REDRAWHEADER;
1064                         break;
1065                         
1066                 /* view manipulation, or searching */
1067                 case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
1068                 case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
1069                 case PADPLUSKEY: case PADMINUS:
1070                         if (pld->searchstr[0]) {
1071                                 poselib_preview_handle_search(pld, event, ascii);
1072                         }
1073                         else {
1074                                 persptoetsen(event);
1075                                 pld->redraw= PL_PREVIEW_REDRAWHEADER;
1076                         }
1077                         break;
1078                         
1079                 /* otherwise, assume that searching might be able to handle it */
1080                 default:
1081                         poselib_preview_handle_search(pld, event, ascii);
1082                         break;
1083         }
1084 }
1085
1086 /* ---------------------------- */
1087
1088 /* Init PoseLib Previewing data */
1089 static void poselib_preview_init_data (tPoseLib_PreviewData *pld, Object *ob, short apply_active)
1090 {
1091         /* clear pld first as it resides on the stack */
1092         memset(pld, 0, sizeof(tPoseLib_PreviewData));
1093         
1094         /* get basic data */
1095         pld->ob= ob;
1096         pld->arm= (ob) ? (ob->data) : NULL;
1097         pld->pose= (ob) ? (ob->pose) : NULL;
1098         pld->act= (ob) ? (ob->poselib) : NULL;
1099         pld->marker= poselib_get_active_pose(pld->act);
1100         
1101         /* check if valid poselib */
1102         if (ELEM3(NULL, pld->ob, pld->pose, pld->arm)) {
1103                 error("PoseLib is only for Armatures in PoseMode");
1104                 pld->state= PL_PREVIEW_ERROR;
1105                 return;
1106         }
1107         if (pld->act == NULL) {
1108                 error("Object doesn't have a valid PoseLib");
1109                 pld->state= PL_PREVIEW_ERROR;
1110                 return;
1111         }
1112         if (pld->marker == NULL) {
1113                 if ((apply_active==0) && (pld->act->markers.first)) {
1114                         /* just use first one then... */
1115                         pld->marker= pld->act->markers.first;
1116                         printf("PoseLib had no active pose\n");
1117                 }
1118                 else {
1119                         error("PoseLib has no poses to preview/apply");
1120                         pld->state= PL_PREVIEW_ERROR;
1121                         return;
1122                 }
1123         }
1124         
1125         /* make backups for restoring pose */
1126         poselib_backup_posecopy(pld);
1127         
1128         /* set flags for running */
1129         pld->state= (apply_active) ? PL_PREVIEW_RUNONCE : PL_PREVIEW_RUNNING;
1130         pld->redraw= PL_PREVIEW_REDRAWALL;
1131         pld->flag= PL_PREVIEW_FIRSTTIME;
1132         
1133         /* set depsgraph flags */
1134                 /* make sure the lock is set OK, unlock can be accidentally saved? */
1135         pld->pose->flag |= POSE_LOCKED;
1136         pld->pose->flag &= ~POSE_DO_UNLOCK;
1137         
1138         /* clear strings + search */
1139         strcpy(pld->headerstr, "");
1140         strcpy(pld->searchstr, "");
1141         strcpy(pld->searchold, "");
1142         pld->search_cursor= 0;
1143 }
1144
1145 /* After previewing poses */
1146 static void poselib_preview_cleanup (tPoseLib_PreviewData *pld)
1147 {
1148         Object *ob= pld->ob;
1149         bPose *pose= pld->pose;
1150         bArmature *arm= pld->arm;
1151         bAction *act= pld->act;
1152         TimeMarker *marker= pld->marker;
1153         
1154         /* this signal does one recalc on pose, then unlocks, so ESC or edit will work */
1155         pose->flag |= POSE_DO_UNLOCK;
1156         
1157         /* clear pose if cancelled */
1158         if (pld->state == PL_PREVIEW_CANCEL) {
1159                 poselib_backup_restore(pld);
1160                 
1161                 /* old optimize trick... this enforces to bypass the depgraph 
1162                  *      - note: code copied from transform_generics.c -> recalcData()
1163                  */
1164                 if ((arm->flag & ARM_DELAYDEFORM)==0)
1165                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);  /* sets recalc flags */
1166                 else
1167                         where_is_pose(ob);
1168                 
1169                 allqueue(REDRAWVIEW3D, 0);
1170                 allqueue(REDRAWBUTSEDIT, 0);
1171         }
1172         else if (pld->state == PL_PREVIEW_CONFIRM) {
1173                 /* tag poses as appropriate */
1174                 poselib_keytag_pose(pld);
1175                 
1176                 /* change active pose setting */
1177                 act->active_marker= BLI_findindex(&act->markers, marker) + 1;
1178                 action_set_activemarker(act, marker, 0);
1179                 
1180                 /* Update event for pose and deformation children */
1181                 DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
1182                 
1183                 /* updates */
1184                 if (IS_AUTOKEY_MODE(NORMAL)) {
1185                         remake_action_ipos(ob->action);
1186                         
1187                         allqueue(REDRAWIPO, 0);
1188                         allqueue(REDRAWVIEW3D, 0);
1189                         allqueue(REDRAWBUTSEDIT, 0);
1190                         allqueue(REDRAWACTION, 0);              
1191                         allqueue(REDRAWNLA, 0);
1192                 }
1193                 else {
1194                         /* need to trick depgraph, action is not allowed to execute on pose */
1195                         where_is_pose(ob);
1196                         ob->recalc= 0;
1197                         
1198                         allqueue(REDRAWVIEW3D, 0);
1199                         allqueue(REDRAWBUTSEDIT, 0);
1200                 }
1201         }
1202         
1203         /* free memory used for backups */
1204         BLI_freelistN(&pld->backups);
1205         BLI_freelistN(&pld->searchp);
1206 }
1207
1208
1209
1210 /* This tool allows users to preview the pose from the pose-lib using the mouse-scrollwheel/pageupdown
1211  * It is also used to apply the active poselib pose only
1212  */
1213 void poselib_preview_poses (Object *ob, short apply_active)
1214 {
1215         tPoseLib_PreviewData pld;
1216         
1217         unsigned short event;
1218         short val=0;
1219         char ascii;
1220         
1221         /* check if valid poselib */
1222         poselib_preview_init_data(&pld, ob, apply_active);
1223         if (pld.state == PL_PREVIEW_ERROR)
1224                 return;
1225                 
1226         /* start preview loop */
1227         while (ELEM(pld.state, PL_PREVIEW_RUNNING, PL_PREVIEW_RUNONCE)) {
1228                 /* preview a pose */
1229                 if (pld.redraw) {
1230                         /* only recalc pose (and its dependencies) if pose has changed */
1231                         if (pld.redraw == PL_PREVIEW_REDRAWALL) {
1232                                 /* don't clear pose if firsttime */
1233                                 if ((pld.flag & PL_PREVIEW_FIRSTTIME)==0)
1234                                         poselib_backup_restore(&pld);
1235                                 else
1236                                         pld.flag &= ~PL_PREVIEW_FIRSTTIME;
1237                                         
1238                                 /* pose should be the right one to draw (unless we're temporarily not showing it) */
1239                                 if ((pld.flag & PL_PREVIEW_SHOWORIGINAL)==0)
1240                                         poselib_apply_pose(&pld);
1241                                 
1242                                 /* old optimize trick... this enforces to bypass the depgraph 
1243                                  *      - note: code copied from transform_generics.c -> recalcData()
1244                                  */
1245                                 if ((pld.arm->flag & ARM_DELAYDEFORM)==0)
1246                                         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);  /* sets recalc flags */
1247                                 else
1248                                         where_is_pose(ob);
1249                         }
1250                         
1251                         /* do header print - if interactively previewing */
1252                         if (pld.state == PL_PREVIEW_RUNNING) {
1253                                 if (pld.flag & PL_PREVIEW_SHOWORIGINAL) {
1254                                         sprintf(pld.headerstr, "PoseLib Previewing Pose: [Showing Original Pose] | Use Tab to start previewing poses again");
1255                                         headerprint(pld.headerstr);
1256                                 }
1257                                 else if (pld.searchstr[0]) {
1258                                         char tempstr[65];
1259                                         char markern[64];
1260                                         short index;
1261                                         
1262                                         /* get search-string */
1263                                         index= pld.search_cursor;
1264                                         
1265                                         memcpy(&tempstr[0], &pld.searchstr[0], index);
1266                                         tempstr[index]= '|';
1267                                         memcpy(&tempstr[index+1], &pld.searchstr[index], 64-index);
1268                                         
1269                                         /* get marker name */
1270                                         if (pld.marker)
1271                                                 strcpy(markern, pld.marker->name);
1272                                         else
1273                                                 strcpy(markern, "No Matches");
1274                                         
1275                                         sprintf(pld.headerstr, "PoseLib Previewing Pose: Filter - [%s] | Current Pose - \"%s\"  | Use ScrollWheel or PageUp/Down to change", tempstr, markern);
1276                                         headerprint(pld.headerstr);
1277                                 }
1278                                 else {
1279                                         sprintf(pld.headerstr, "PoseLib Previewing Pose: \"%s\"  | Use ScrollWheel or PageUp/Down to change", pld.marker->name);
1280                                         headerprint(pld.headerstr);
1281                                 }
1282                         }
1283                         
1284                         /* force drawing of view + clear redraw flag */
1285                         force_draw(0);
1286                         pld.redraw= PL_PREVIEW_NOREDRAW;
1287                 }
1288                 
1289                 /* stop now if only running once */
1290                 if (pld.state == PL_PREVIEW_RUNONCE) {
1291                         pld.state = PL_PREVIEW_CONFIRM;
1292                         break;
1293                 }
1294                 
1295                 /* essential for idling subloop */
1296                 if (qtest() == 0) 
1297                         PIL_sleep_ms(2);
1298                 
1299                 /* emptying queue and reading events */
1300                 while ( qtest() ) {
1301                         event= extern_qread_ext(&val, &ascii);
1302                         
1303                         /* event processing */
1304                         if (val) {
1305                                 poselib_preview_handle_event(&pld, event, ascii);
1306                         }
1307                 }
1308         }
1309         
1310         /* finish up */
1311         poselib_preview_cleanup(&pld);
1312         
1313         BIF_undo_push("PoseLib Apply Pose");
1314 }