doxygen: prevent GPL license block from being parsed as doxygen comment.
[blender.git] / source / blender / editors / armature / 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 #include <float.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_math.h"
38 #include "BLI_blenlib.h"
39 #include "BLI_dynstr.h"
40 #include "BLI_dlrbTree.h"
41 #include "BLI_utildefines.h"
42
43 #include "DNA_anim_types.h"
44 #include "DNA_armature_types.h"
45 #include "DNA_object_types.h"
46 #include "DNA_scene_types.h"
47
48 #include "BKE_animsys.h"
49 #include "BKE_action.h"
50 #include "BKE_armature.h"
51 #include "BKE_depsgraph.h"
52 #include "BKE_idprop.h"
53 #include "BKE_library.h"
54
55 #include "BKE_context.h"
56 #include "BKE_report.h"
57
58 #include "RNA_access.h"
59 #include "RNA_define.h"
60 #include "RNA_enum_types.h"
61
62 #include "WM_api.h"
63 #include "WM_types.h"
64
65 #include "UI_interface.h"
66 #include "UI_resources.h"
67
68 #include "ED_anim_api.h"
69 #include "ED_armature.h"
70 #include "ED_keyframes_draw.h"
71 #include "ED_keyframing.h"
72 #include "ED_keyframes_edit.h"
73 #include "ED_screen.h"
74
75 #include "armature_intern.h"
76
77 /* ******* XXX ********** */
78
79 static void action_set_activemarker(void *UNUSED(a), void *UNUSED(b), void *UNUSED(c)) {}
80
81 /* ************************************************************* */
82 /* == POSE-LIBRARY TOOL FOR BLENDER == 
83  *      
84  * Overview: 
85  *      This tool allows animators to store a set of frequently used poses to dump into
86  *      the active action to help in "budget" productions to quickly block out new actions.
87  *      It acts as a kind of "glorified clipboard for poses", allowing for naming of poses.
88  *
89  * Features:
90  *      - PoseLibs are simply normal Actions
91  *      - Each "pose" is simply a set of keyframes that occur on a particular frame
92  *              -> a set of TimeMarkers that belong to each Action, help 'label' where a 'pose' can be
93  *                 found in the Action
94  *      - The Scrollwheel or PageUp/Down buttons when used in a special mode or after pressing/holding
95  *        [a modifier] key, cycles through the poses available for the active pose's poselib, allowing the
96  *        animator to preview what action best suits that pose
97  */
98 /* ************************************************************* */
99
100
101 /* gets the first available frame in poselib to store a pose on 
102  *      - frames start from 1, and a pose should occur on every frame... 0 is error!
103  */
104 static int poselib_get_free_index (bAction *act)
105 {
106         TimeMarker *marker;
107         int low=0, high=0;
108         
109         /* sanity checks */
110         if (ELEM(NULL, act, act->markers.first)) return 1;
111         
112         /* loop over poses finding various values (poses are not stored in chronological order) */
113         for (marker= act->markers.first; marker; marker= marker->next) {
114                 /* only increase low if value is 1 greater than low, to find "gaps" where
115                  * poses were removed from the poselib
116                  */
117                 if (marker->frame == (low + 1)) 
118                         low++;
119                 
120                 /* value replaces high if it is the highest value encountered yet */
121                 if (marker->frame > high) 
122                         high= marker->frame;
123         }
124         
125         /* - if low is not equal to high, then low+1 is a gap 
126          * - if low is equal to high, then high+1 is the next index (add at end) 
127          */
128         if (low < high) 
129                 return (low + 1);
130         else 
131                 return (high + 1);
132 }
133
134 /* returns the active pose for a poselib */
135 static TimeMarker *poselib_get_active_pose (bAction *act)
136 {       
137         if ((act) && (act->active_marker))
138                 return BLI_findlink(&act->markers, act->active_marker-1);
139         else
140                 return NULL;
141 }
142
143 /* Get object that Pose Lib should be found on */
144 static Object *get_poselib_object (bContext *C)
145 {
146         ScrArea *sa = CTX_wm_area(C);
147         
148         /* sanity check */
149         if (C == NULL)
150                 return NULL;
151         
152         if (sa && (sa->spacetype == SPACE_BUTS)) 
153                 return CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
154         else
155                 return ED_object_pose_armature(CTX_data_active_object(C));
156 }
157
158 /* Poll callback for operators that require existing PoseLib data (with poses) to work */
159 static int has_poselib_pose_data_poll (bContext *C)
160 {
161         Object *ob = get_poselib_object(C);
162         return (ob && ob->poselib);
163 }
164
165 /* ----------------------------------- */
166
167 /* Initialise a new poselib (whether it is needed or not) */
168 static bAction *poselib_init_new (Object *ob)
169 {
170         /* sanity checks - only for armatures */
171         if (ELEM(NULL, ob, ob->pose))
172                 return NULL;
173         
174         /* init object's poselib action (unlink old one if there) */
175         if (ob->poselib)
176                 id_us_min(&ob->poselib->id);
177         ob->poselib= add_empty_action("PoseLib");
178         
179         return ob->poselib;
180 }
181
182 /* Initialise a new poselib (checks if that needs to happen) */
183 static bAction *poselib_validate (Object *ob)
184 {
185         if (ELEM(NULL, ob, ob->pose))
186                 return NULL;
187         else if (ob->poselib == NULL)
188                 return poselib_init_new(ob);
189         else
190                 return ob->poselib;
191 }
192
193 /* ************************************************************* */
194 /* Pose Lib UI Operators */
195
196 static int poselib_new_exec (bContext *C, wmOperator *UNUSED(op))
197 {
198         Object *ob = get_poselib_object(C);
199         
200         /* sanity checks */
201         if (ob == NULL)
202                 return OPERATOR_CANCELLED;
203                 
204         /* new method here deals with the rest... */
205         poselib_init_new(ob);
206         
207         /* notifier here might evolve? */
208         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, NULL);
209         
210         return OPERATOR_FINISHED;
211 }
212
213 void POSELIB_OT_new (wmOperatorType *ot)
214 {
215         /* identifiers */
216         ot->name = "New Pose Library";
217         ot->idname = "POSELIB_OT_new";
218         ot->description = "Add New Pose Library to active Object";
219         
220         /* callbacks */
221         ot->exec = poselib_new_exec;
222         ot->poll= ED_operator_posemode;
223         
224         /* flags */
225         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
226 }
227
228 /* ------------------------------------------------ */
229
230 static int poselib_unlink_exec (bContext *C, wmOperator *UNUSED(op))
231 {
232         Object *ob = get_poselib_object(C);
233         
234         /* sanity checks */
235         if (ELEM(NULL, ob, ob->poselib))
236                 return OPERATOR_CANCELLED;
237                 
238         /* there should be a poselib (we just checked above!), so just lower its user count and remove */
239         id_us_min(&ob->poselib->id);
240         ob->poselib = NULL;
241         
242         /* notifier here might evolve? */
243         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, NULL);
244         
245         return OPERATOR_FINISHED;
246 }
247
248 void POSELIB_OT_unlink (wmOperatorType *ot)
249 {
250         /* identifiers */
251         ot->name = "Unlink Pose Library";
252         ot->idname = "POSELIB_OT_unlink";
253         ot->description = "Remove Pose Library from active Object";
254         
255         /* callbacks */
256         ot->exec = poselib_unlink_exec;
257         ot->poll= has_poselib_pose_data_poll;
258         
259         /* flags */
260         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
261 }
262
263 /* ************************************************************* */
264 /* Pose Editing Operators */
265
266 /* This tool automagically generates/validates poselib data so that it corresponds to the data 
267  * in the action. This is for use in making existing actions usable as poselibs.
268  */
269 static int poselib_sanitise_exec (bContext *C, wmOperator *op)
270 {
271         Object *ob = get_poselib_object(C);
272         bAction *act = (ob)? ob->poselib : NULL;
273         DLRBT_Tree keys;
274         ActKeyColumn *ak;
275         TimeMarker *marker, *markern;
276         
277         /* validate action */
278         if (act == NULL)  {
279                 BKE_report(op->reports, RPT_WARNING, "No Action to validate");
280                 return OPERATOR_CANCELLED;
281         }
282         
283         /* determine which frames have keys */
284         BLI_dlrbTree_init(&keys);
285                 action_to_keylist(NULL, act, &keys, NULL);
286         BLI_dlrbTree_linkedlist_sync(&keys);
287         
288         /* for each key, make sure there is a corresponding pose */
289         for (ak= keys.first; ak; ak= ak->next) {
290                 /* check if any pose matches this */
291                 // TODO: don't go looking through the list like this every time...
292                 for (marker= act->markers.first; marker; marker= marker->next) {
293                         if (IS_EQ(marker->frame, ak->cfra)) {
294                                 marker->flag = -1;
295                                 break;
296                         }
297                 }
298                 
299                 /* add new if none found */
300                 if (marker == NULL) {
301                         /* add pose to poselib */
302                         marker= MEM_callocN(sizeof(TimeMarker), "ActionMarker");
303                         
304                         BLI_strncpy(marker->name, "Pose", sizeof(marker->name));
305                         
306                         marker->frame= (int)ak->cfra;
307                         marker->flag= -1;
308                         
309                         BLI_addtail(&act->markers, marker);
310                 }
311         }
312         
313         /* remove all untagged poses (unused), and remove all tags */
314         for (marker= act->markers.first; marker; marker= markern) {
315                 markern= marker->next;
316                 
317                 if (marker->flag != -1)
318                         BLI_freelinkN(&act->markers, marker);
319                 else
320                         marker->flag = 0;
321         }
322         
323         /* free temp memory */
324         BLI_dlrbTree_free(&keys);
325         
326         return OPERATOR_FINISHED;
327 }
328
329 void POSELIB_OT_action_sanitise (wmOperatorType *ot)
330 {
331         /* identifiers */
332         ot->name = "Sanitise Pose Library Action";
333         ot->idname = "POSELIB_OT_action_sanitise";
334         ot->description = "Make action suitable for use as a Pose Library";
335         
336         /* callbacks */
337         ot->exec = poselib_sanitise_exec;
338         ot->poll = has_poselib_pose_data_poll;
339         
340         /* flags */
341         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
342 }
343
344 /* ------------------------------------------ */
345
346 static void poselib_add_menu_invoke__replacemenu (bContext *C, uiLayout *layout, void *UNUSED(arg))
347 {
348         Object *ob= get_poselib_object(C);
349         bAction *act= (ob) ? ob->poselib : NULL;
350         TimeMarker *marker;
351         
352         /* set the operator execution context correctly */
353         uiLayoutSetOperatorContext(layout, WM_OP_EXEC_DEFAULT);
354         
355         /* add each marker to this menu */
356         for (marker= act->markers.first; marker; marker= marker->next) {
357                 PointerRNA props_ptr;
358                 
359                 props_ptr = uiItemFullO(layout, "POSELIB_OT_pose_add", 
360                                                 marker->name, ICON_ARMATURE_DATA, NULL, 
361                                                 WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
362                 
363                 RNA_int_set(&props_ptr, "frame", marker->frame);
364                 RNA_string_set(&props_ptr, "name", marker->name);
365         }
366 }
367
368 static int poselib_add_menu_invoke (bContext *C, wmOperator *op, wmEvent *UNUSED(evt))
369 {
370         Scene *scene= CTX_data_scene(C);
371         Object *ob= get_poselib_object(C);
372         bPose *pose= (ob) ? ob->pose : NULL;
373         uiPopupMenu *pup;
374         uiLayout *layout;
375         
376         /* sanity check */
377         if (ELEM(NULL, ob, pose)) 
378                 return OPERATOR_CANCELLED;
379         
380         /* start building */
381         pup= uiPupMenuBegin(C, op->type->name, ICON_NULL);
382         layout= uiPupMenuLayout(pup);
383         uiLayoutSetOperatorContext(layout, WM_OP_EXEC_DEFAULT);
384         
385         /* add new (adds to the first unoccupied frame) */
386         uiItemIntO(layout, "Add New", ICON_NULL, "POSELIB_OT_pose_add", "frame", poselib_get_free_index(ob->poselib));
387         
388         /* check if we have any choices to add a new pose in any other way */
389         if ((ob->poselib) && (ob->poselib->markers.first)) {
390                 /* add new (on current frame) */
391                 uiItemIntO(layout, "Add New (Current Frame)", ICON_NULL, "POSELIB_OT_pose_add", "frame", CFRA);
392                 
393                 /* replace existing - submenu */
394                 uiItemMenuF(layout, "Replace Existing...", 0, poselib_add_menu_invoke__replacemenu, NULL);
395         }
396         
397         uiPupMenuEnd(C, pup);
398         
399         /* this operator is only for a menu, not used further */
400         return OPERATOR_CANCELLED;
401 }
402
403
404 static int poselib_add_exec (bContext *C, wmOperator *op)
405 {
406         Object *ob= get_poselib_object(C);
407         bAction *act = poselib_validate(ob);
408         bPose *pose= (ob) ? ob->pose : NULL;
409         TimeMarker *marker;
410         KeyingSet *ks= ANIM_builtin_keyingset_get_named(NULL, "Whole Character"); /* this includes custom props :)*/
411         int frame= RNA_int_get(op->ptr, "frame");
412         char name[64];
413         
414         /* sanity check (invoke should have checked this anyway) */
415         if (ELEM(NULL, ob, pose)) 
416                 return OPERATOR_CANCELLED;
417         
418         /* get name to give to pose */
419         RNA_string_get(op->ptr, "name", name);
420         
421         /* add pose to poselib - replaces any existing pose there
422          *      - for the 'replace' option, this should end up finding the appropriate marker,
423          *        so no new one will be added
424          */
425         for (marker= act->markers.first; marker; marker= marker->next) {
426                 if (marker->frame == frame) {
427                         BLI_strncpy(marker->name, name, sizeof(marker->name));
428                         break;
429                 }
430         }
431         if (marker == NULL) {
432                 marker= MEM_callocN(sizeof(TimeMarker), "ActionMarker");
433                 
434                 BLI_strncpy(marker->name, name, sizeof(marker->name));
435                 marker->frame= frame;
436                 
437                 BLI_addtail(&act->markers, marker);
438         }
439         
440         /* validate name */
441         BLI_uniquename(&act->markers, marker, "Pose", '.', offsetof(TimeMarker, name), sizeof(marker->name));
442         
443         /* use Keying Set to determine what to store for the pose */
444         // FIXME: in the past, the Keying Set respected selections (LocRotScale), but the current one doesn't (Whole Character)
445         // so perhaps we need either a new Keying Set, or just to add overrides here...
446         ANIM_apply_keyingset(C, NULL, act, ks, MODIFYKEY_MODE_INSERT, (float)frame);
447         
448         /* store new 'active' pose number */
449         act->active_marker= BLI_countlist(&act->markers);
450         
451         /* done */
452         return OPERATOR_FINISHED;
453 }
454
455 void POSELIB_OT_pose_add (wmOperatorType *ot)
456 {
457         /* identifiers */
458         ot->name= "PoseLib Add Pose";
459         ot->idname= "POSELIB_OT_pose_add";
460         ot->description= "Add the current Pose to the active Pose Library";
461         
462         /* api callbacks */
463         ot->invoke= poselib_add_menu_invoke;
464         ot->exec= poselib_add_exec;
465         ot->poll= ED_operator_posemode;
466         
467         /* flags */
468         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
469         
470         /* properties */
471         RNA_def_int(ot->srna, "frame", 1, 0, INT_MAX, "Frame", "Frame to store pose on", 0, INT_MAX);
472         RNA_def_string(ot->srna, "name", "Pose", 64, "Pose Name", "Name of newly added Pose");
473 }
474
475 /* ----- */
476
477 /* can be called with C == NULL */
478 static EnumPropertyItem *poselib_stored_pose_itemf(bContext *C, PointerRNA *UNUSED(ptr), int *free)
479 {
480         Object *ob = get_poselib_object(C);
481         bAction *act = (ob) ? ob->poselib : NULL;
482         TimeMarker *marker;
483         EnumPropertyItem *item= NULL, item_tmp= {0};
484         int totitem= 0;
485         int i= 0;
486
487         if (C == NULL) {
488                 return DummyRNA_DEFAULT_items;
489         }
490         
491         /* check that the action exists */
492         if (act) {
493                 /* add each marker to the list */
494                 for (marker=act->markers.first, i=0; marker; marker= marker->next, i++) {
495                         item_tmp.identifier= item_tmp.name= marker->name;
496                         item_tmp.icon= ICON_ARMATURE_DATA;
497                         item_tmp.value= i;
498                         RNA_enum_item_add(&item, &totitem, &item_tmp);
499                 }
500         }
501
502         RNA_enum_item_end(&item, &totitem);
503         *free= 1;
504
505         return item;
506 }
507
508 static int poselib_remove_exec (bContext *C, wmOperator *op)
509 {
510         Object *ob= get_poselib_object(C);
511         bAction *act= (ob) ? ob->poselib : NULL;
512         TimeMarker *marker;
513         FCurve *fcu;
514         
515         /* check if valid poselib */
516         if (act == NULL) {
517                 BKE_report(op->reports, RPT_ERROR, "Object doesn't have PoseLib data");
518                 return OPERATOR_CANCELLED;
519         }
520         
521         /* get index (and pointer) of pose to remove */
522         marker= BLI_findlink(&act->markers, RNA_int_get(op->ptr, "pose"));
523         if (marker == NULL) {
524                 BKE_reportf(op->reports, RPT_ERROR, "Invalid Pose specified %d", RNA_int_get(op->ptr, "pose"));
525                 return OPERATOR_CANCELLED;
526         }
527         
528         /* remove relevant keyframes */
529         for (fcu= act->curves.first; fcu; fcu= fcu->next) {
530                 BezTriple *bezt;
531                 int i;
532                 
533                 if (fcu->bezt) {
534                         for (i=0, bezt=fcu->bezt; i < fcu->totvert; i++, bezt++) {
535                                 /* check if remove */
536                                 if (IS_EQ(bezt->vec[1][0], marker->frame)) {
537                                         delete_fcurve_key(fcu, i, 1);
538                                         break;
539                                 }
540                         }
541                 }
542         }
543         
544         /* remove poselib from list */
545         BLI_freelinkN(&act->markers, marker);
546         
547         /* fix active pose number */
548         act->active_marker= 0;
549         
550         /* done */
551         return OPERATOR_FINISHED;
552 }
553
554 void POSELIB_OT_pose_remove (wmOperatorType *ot)
555 {
556         PropertyRNA *prop;
557         
558         /* identifiers */
559         ot->name= "PoseLib Remove Pose";
560         ot->idname= "POSELIB_OT_pose_remove";
561         ot->description= "Remove nth pose from the active Pose Library";
562         
563         /* api callbacks */
564         ot->invoke= WM_menu_invoke;
565         ot->exec= poselib_remove_exec;
566         ot->poll= has_poselib_pose_data_poll;
567         
568         /* flags */
569         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
570         
571         /* properties */
572         prop= RNA_def_enum(ot->srna, "pose", DummyRNA_DEFAULT_items, 0, "Pose", "The pose to remove");
573                 RNA_def_enum_funcs(prop, poselib_stored_pose_itemf);
574         ot->prop= prop;
575 }
576
577 static int poselib_rename_invoke (bContext *C, wmOperator *op, wmEvent *evt)
578 {
579         Object *ob= get_poselib_object(C);
580         bAction *act= (ob) ? ob->poselib : NULL;
581         TimeMarker *marker;
582         
583         /* check if valid poselib */
584         if (act == NULL) {
585                 BKE_report(op->reports, RPT_ERROR, "Object doesn't have PoseLib data");
586                 return OPERATOR_CANCELLED;
587         }
588         
589         /* get index (and pointer) of pose to remove */
590         marker= BLI_findlink(&act->markers, act->active_marker-1);
591         if (marker == NULL) {
592                 BKE_report(op->reports, RPT_ERROR, "Invalid index for Pose");
593                 return OPERATOR_CANCELLED;
594         }
595         else {
596                 /* use the existing name of the marker as the name, and use the active marker as the one to rename */
597                 RNA_enum_set(op->ptr, "pose", act->active_marker-1);
598                 RNA_string_set(op->ptr, "name", marker->name);
599         }
600         
601         /* part to sync with other similar operators... */
602         return WM_operator_props_popup(C, op, evt);
603 }
604
605 static int poselib_rename_exec (bContext *C, wmOperator *op)
606 {
607         Object *ob= ED_object_pose_armature(CTX_data_active_object(C));
608         bAction *act= (ob) ? ob->poselib : NULL;
609         TimeMarker *marker;
610         char newname[64];
611         
612         /* check if valid poselib */
613         if (act == NULL) {
614                 BKE_report(op->reports, RPT_ERROR, "Object doesn't have PoseLib data");
615                 return OPERATOR_CANCELLED;
616         }
617         
618         /* get index (and pointer) of pose to remove */
619         marker= BLI_findlink(&act->markers, RNA_int_get(op->ptr, "pose"));
620         if (marker == NULL) {
621                 BKE_report(op->reports, RPT_ERROR, "Invalid index for Pose");
622                 return OPERATOR_CANCELLED;
623         }
624         
625         /* get new name */
626         RNA_string_get(op->ptr, "name", newname);
627         
628         /* copy name and validate it */
629         BLI_strncpy(marker->name, newname, sizeof(marker->name));
630         BLI_uniquename(&act->markers, marker, "Pose", '.', offsetof(TimeMarker, name), sizeof(marker->name));
631         
632         /* done */
633         return OPERATOR_FINISHED;
634 }
635
636 void POSELIB_OT_pose_rename (wmOperatorType *ot)
637 {
638         PropertyRNA *prop;
639         static EnumPropertyItem prop_poses_dummy_types[] = {
640                 {0, NULL, 0, NULL, NULL}
641         };
642         
643         /* identifiers */
644         ot->name= "PoseLib Rename Pose";
645         ot->idname= "POSELIB_OT_pose_rename";
646         ot->description= "Rename specified pose from the active Pose Library";
647         
648         /* api callbacks */
649         ot->invoke= poselib_rename_invoke;
650         ot->exec= poselib_rename_exec;
651         ot->poll= has_poselib_pose_data_poll;
652         
653         /* flags */
654         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
655         
656         /* properties */
657                 /* NOTE: name not pose is the operator's "main" property, so that it will get activated in the popup for easy renaming */
658         ot->prop= RNA_def_string(ot->srna, "name", "RenamedPose", 64, "New Pose Name", "New name for pose");
659         prop= RNA_def_enum(ot->srna, "pose", prop_poses_dummy_types, 0, "Pose", "The pose to rename");
660                 RNA_def_enum_funcs(prop, poselib_stored_pose_itemf);
661 }
662
663 /* ************************************************************* */
664 /* Pose-Lib Browsing/Previewing Operator */
665
666 /* Simple struct for storing settings/data for use during PoseLib preview */
667 typedef struct tPoseLib_PreviewData {
668         ListBase backups;               /* tPoseLib_Backup structs for restoring poses */
669         ListBase searchp;               /* LinkData structs storing list of poses which match the current search-string */
670         
671         Scene *scene;                   /* active scene */
672         ScrArea *sa;                    /* active area */
673         
674         PointerRNA rna_ptr;             /* RNA-Pointer to Object 'ob' */
675         Object *ob;                             /* object to work on */
676         bArmature *arm;                 /* object's armature data */
677         bPose *pose;                    /* object's pose */
678         bAction *act;                   /* poselib to use */
679         TimeMarker *marker;             /* 'active' pose */
680         
681         int selcount;                   /* number of selected elements to work on */
682         int totcount;                   /* total number of elements to work on */
683         
684         short state;                    /* state of main loop */
685         short redraw;                   /* redraw/update settings during main loop */
686         short flag;                             /* flags for various settings */
687         
688         short search_cursor;    /* position of cursor in searchstr (cursor occurs before the item at the nominated index) */
689         char searchstr[64];             /* (Part of) Name to search for to filter poses that get shown */
690         char searchold[64];             /* Previously set searchstr (from last loop run), so that we can detected when to rebuild searchp */
691         
692         char headerstr[200];    /* Info-text to print in header */
693 } tPoseLib_PreviewData;
694
695 /* defines for tPoseLib_PreviewData->state values */
696 enum {
697         PL_PREVIEW_ERROR = -1,
698         PL_PREVIEW_RUNNING,
699         PL_PREVIEW_CONFIRM,
700         PL_PREVIEW_CANCEL,
701         PL_PREVIEW_RUNONCE 
702 };
703
704 /* defines for tPoseLib_PreviewData->redraw values */
705 enum {
706         PL_PREVIEW_NOREDRAW = 0,
707         PL_PREVIEW_REDRAWALL,
708         PL_PREVIEW_REDRAWHEADER,
709 };
710
711 /* defines for tPoseLib_PreviewData->flag values */
712 enum {
713         PL_PREVIEW_FIRSTTIME    = (1<<0),
714         PL_PREVIEW_SHOWORIGINAL = (1<<1)
715 };
716
717 /* ---------------------------- */
718
719 /* simple struct for storing backup info */
720 typedef struct tPoseLib_Backup {
721         struct tPoseLib_Backup *next, *prev;
722         
723         bPoseChannel *pchan;            /* pose channel backups are for */
724         
725         bPoseChannel olddata;           /* copy of pose channel's old data (at start) */
726         IDProperty *oldprops;           /* copy (needs freeing) of pose channel's properties (at start) */
727 } tPoseLib_Backup;
728
729 /* Makes a copy of the current pose for restoration purposes - doesn't do constraints currently */
730 static void poselib_backup_posecopy (tPoseLib_PreviewData *pld)
731 {
732         bActionGroup *agrp;
733         bPoseChannel *pchan;
734         
735         /* for each posechannel that has an actionchannel in */
736         for (agrp= pld->act->groups.first; agrp; agrp= agrp->next) {
737                 /* try to find posechannel */
738                 pchan= get_pose_channel(pld->pose, agrp->name);
739                 
740                 /* backup data if available */
741                 if (pchan) {
742                         tPoseLib_Backup *plb;
743                         
744                         /* store backup */
745                         plb= MEM_callocN(sizeof(tPoseLib_Backup), "tPoseLib_Backup");
746                         
747                         plb->pchan= pchan;
748                         memcpy(&plb->olddata, plb->pchan, sizeof(bPoseChannel));
749                         
750                         if (pchan->prop)
751                                 plb->oldprops= IDP_CopyProperty(pchan->prop);
752                         
753                         BLI_addtail(&pld->backups, plb);
754                         
755                         /* mark as being affected */
756                         if ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED))
757                                 pld->selcount++;
758                         pld->totcount++;
759                 }
760         }
761 }
762
763 /* Restores original pose */
764 static void poselib_backup_restore (tPoseLib_PreviewData *pld)
765 {
766         tPoseLib_Backup *plb;
767         
768         for (plb= pld->backups.first; plb; plb= plb->next) {
769                 /* copy most of data straight back */
770                 memcpy(plb->pchan, &plb->olddata, sizeof(bPoseChannel));
771                 
772                 /* just overwrite values of properties from the stored copies (there should be some) */
773                 if (plb->oldprops)
774                         IDP_SyncGroupValues(plb->pchan->prop, plb->oldprops);
775                         
776                 // TODO: constraints settings aren't restored yet, even though these could change (though not that likely)
777         }
778 }
779
780 /* Free list of backups, including any side data it may use */
781 static void poselib_backup_free_data (tPoseLib_PreviewData *pld)
782 {
783         tPoseLib_Backup *plb, *plbn;
784         
785         for (plb= pld->backups.first; plb; plb= plbn) {
786                 plbn= plb->next;
787                 
788                 /* free custom data */
789                 if (plb->oldprops) {
790                         IDP_FreeProperty(plb->oldprops);
791                         MEM_freeN(plb->oldprops);
792                 }
793                 
794                 /* free backup element now */
795                 BLI_freelinkN(&pld->backups, plb);
796         }
797 }
798
799 /* ---------------------------- */
800
801 /* Applies the appropriate stored pose from the pose-library to the current pose
802  *      - assumes that a valid object, with a poselib has been supplied
803  *      - gets the string to print in the header
804  *      - this code is based on the code for extract_pose_from_action in blenkernel/action.c
805  */
806 static void poselib_apply_pose (tPoseLib_PreviewData *pld)
807 {
808         PointerRNA *ptr= &pld->rna_ptr;
809         bArmature *arm= pld->arm;
810         bPose *pose= pld->pose;
811         bPoseChannel *pchan;
812         bAction *act= pld->act;
813         bActionGroup *agrp;
814         
815         KeyframeEditData ked= {{0}};
816         KeyframeEditFunc group_ok_cb;
817         int frame= 1;
818         
819         /* get the frame */
820         if (pld->marker)
821                 frame= pld->marker->frame;
822         else
823                 return; 
824         
825         
826         /* init settings for testing groups for keyframes */
827         group_ok_cb= ANIM_editkeyframes_ok(BEZT_OK_FRAMERANGE);
828         ked.f1= ((float)frame) - 0.5f;
829         ked.f2= ((float)frame) + 0.5f;
830         
831         
832         /* start applying - only those channels which have a key at this point in time! */
833         for (agrp= act->groups.first; agrp; agrp= agrp->next) {
834                 /* check if group has any keyframes */
835                 if (ANIM_animchanneldata_keyframes_loop(&ked, agrp, ALE_GROUP, NULL, group_ok_cb, NULL, 0)) {
836                         /* has keyframe on this frame, so try to get a PoseChannel with this name */
837                         pchan= get_pose_channel(pose, agrp->name);
838                         
839                         if (pchan) {    
840                                 short ok= 0;
841                                 
842                                 /* check if this bone should get any animation applied */
843                                 if (pld->selcount == 0) {
844                                         /* if no bones are selected, then any bone is ok */
845                                         ok= 1;
846                                 }
847                                 else if (pchan->bone) {
848                                         /* only ok if bone is visible and selected */
849                                         if ( (pchan->bone->flag & BONE_SELECTED) &&
850                                                  (pchan->bone->flag & BONE_HIDDEN_P)==0 &&
851                                                  (pchan->bone->layer & arm->layer) )
852                                                 ok = 1;
853                                 }
854                                 
855                                 if (ok) 
856                                         animsys_evaluate_action_group(ptr, act, agrp, NULL, (float)frame);
857                         }
858                 }
859         }
860 }
861
862 /* Auto-keys/tags bones affected by the pose used from the poselib */
863 static void poselib_keytag_pose (bContext *C, Scene *scene, tPoseLib_PreviewData *pld)
864 {
865         bPose *pose= pld->pose;
866         bPoseChannel *pchan;
867         bAction *act= pld->act;
868         bActionGroup *agrp;
869         
870         KeyingSet *ks = ANIM_get_keyingset_for_autokeying(scene, "Whole Character");
871         ListBase dsources = {NULL, NULL};
872         short autokey = autokeyframe_cfra_can_key(scene, &pld->ob->id);
873         
874         /* start tagging/keying */
875         for (agrp= act->groups.first; agrp; agrp= agrp->next) {
876                 /* only for selected action channels */
877                 if (agrp->flag & AGRP_SELECTED) {
878                         pchan= get_pose_channel(pose, agrp->name);
879                         
880                         if (pchan) {
881                                 if (autokey) {
882                                         /* add datasource override for the PoseChannel, to be used later */
883                                         ANIM_relative_keyingset_add_source(&dsources, &pld->ob->id, &RNA_PoseBone, pchan); 
884                                         
885                                         /* clear any unkeyed tags */
886                                         if (pchan->bone)
887                                                 pchan->bone->flag &= ~BONE_UNKEYED;
888                                 }
889                                 else {
890                                         /* add unkeyed tags */
891                                         if (pchan->bone)
892                                                 pchan->bone->flag |= BONE_UNKEYED;
893                                 }
894                         }
895                 }
896         }
897         
898         /* perform actual auto-keying now */
899         if (autokey) {
900                 /* insert keyframes for all relevant bones in one go */
901                 ANIM_apply_keyingset(C, &dsources, NULL, ks, MODIFYKEY_MODE_INSERT, (float)CFRA);
902                 BLI_freelistN(&dsources);
903         }
904         
905         /* send notifiers for this */
906         WM_event_add_notifier(C, NC_ANIMATION|ND_KEYFRAME|NA_EDITED, NULL);
907 }
908
909 /* Apply the relevant changes to the pose */
910 static void poselib_preview_apply (bContext *C, wmOperator *op)
911 {
912         tPoseLib_PreviewData *pld= (tPoseLib_PreviewData *)op->customdata;
913         
914         /* only recalc pose (and its dependencies) if pose has changed */
915         if (pld->redraw == PL_PREVIEW_REDRAWALL) {
916                 /* don't clear pose if firsttime */
917                 if ((pld->flag & PL_PREVIEW_FIRSTTIME)==0)
918                         poselib_backup_restore(pld);
919                 else
920                         pld->flag &= ~PL_PREVIEW_FIRSTTIME;
921                         
922                 /* pose should be the right one to draw (unless we're temporarily not showing it) */
923                 if ((pld->flag & PL_PREVIEW_SHOWORIGINAL)==0) {
924                         RNA_int_set(op->ptr, "pose_index", BLI_findindex(&pld->act->markers, pld->marker));
925                         poselib_apply_pose(pld);
926                 }
927                 else
928                         RNA_int_set(op->ptr, "pose_index", -2); /* -2 means don't apply any pose */
929                 
930                 /* old optimize trick... this enforces to bypass the depgraph 
931                  *      - note: code copied from transform_generics.c -> recalcData()
932                  */
933                 // FIXME: shouldn't this use the builtin stuff?
934                 if ((pld->arm->flag & ARM_DELAYDEFORM)==0)
935                         DAG_id_tag_update(&pld->ob->id, OB_RECALC_DATA);  /* sets recalc flags */
936                 else
937                         where_is_pose(pld->scene, pld->ob);
938         }
939         
940         /* do header print - if interactively previewing */
941         if (pld->state == PL_PREVIEW_RUNNING) {
942                 if (pld->flag & PL_PREVIEW_SHOWORIGINAL) {
943                         sprintf(pld->headerstr, "PoseLib Previewing Pose: [Showing Original Pose] | Use Tab to start previewing poses again");
944                         ED_area_headerprint(pld->sa, pld->headerstr);
945                 }
946                 else if (pld->searchstr[0]) {
947                         char tempstr[65];
948                         char markern[64];
949                         short index;
950                         
951                         /* get search-string */
952                         index= pld->search_cursor;
953                         
954                         if (index >= 0 && index <= 64) {
955                                 memcpy(&tempstr[0], &pld->searchstr[0], index);
956                                 tempstr[index]= '|';
957                                 memcpy(&tempstr[index+1], &pld->searchstr[index], 64-index);
958                         }
959                         else {
960                                 strncpy(tempstr, pld->searchstr, 64);
961                         }
962                         
963                         /* get marker name */
964                         if (pld->marker)
965                                 strcpy(markern, pld->marker->name);
966                         else
967                                 strcpy(markern, "No Matches");
968                         
969                         sprintf(pld->headerstr, "PoseLib Previewing Pose: Filter - [%s] | Current Pose - \"%s\"  | Use ScrollWheel or PageUp/Down to change", tempstr, markern);
970                         ED_area_headerprint(pld->sa, pld->headerstr);
971                 }
972                 else {
973                         sprintf(pld->headerstr, "PoseLib Previewing Pose: \"%s\"  | Use ScrollWheel or PageUp/Down to change", pld->marker->name);
974                         ED_area_headerprint(pld->sa, pld->headerstr);
975                 }
976         }
977         
978         /* request drawing of view + clear redraw flag */
979         WM_event_add_notifier(C, NC_OBJECT|ND_POSE, pld->ob);
980         pld->redraw= PL_PREVIEW_NOREDRAW;
981 }
982
983 /* ---------------------------- */
984
985 /* This helper function is called during poselib_preview_poses to find the 
986  * pose to preview next (after a change event)
987  */
988 static void poselib_preview_get_next (tPoseLib_PreviewData *pld, int step)
989 {
990         /* stop if not going anywhere, as we assume that there is a direction to move in */
991         if (step == 0)
992                 return;
993         
994         /* search-string dictates a special approach */
995         if (pld->searchstr[0]) {
996                 TimeMarker *marker;
997                 LinkData *ld, *ldn, *ldc;
998                 
999                 /* free and rebuild if needed (i.e. if search-str changed) */
1000                 if (strcmp(pld->searchstr, pld->searchold)) {
1001                         /* free list of temporary search matches */
1002                         BLI_freelistN(&pld->searchp);
1003                         
1004                         /* generate a new list of search matches */
1005                         for (marker= pld->act->markers.first; marker; marker= marker->next) {
1006                                 /* does the name partially match? 
1007                                  *      - don't worry about case, to make it easier for users to quickly input a name (or 
1008                                  *        part of one), which is the whole point of this feature
1009                                  */
1010                                 if (BLI_strcasestr(marker->name, pld->searchstr)) {
1011                                         /* make link-data to store reference to it */
1012                                         ld= MEM_callocN(sizeof(LinkData), "PoseMatch");
1013                                         ld->data= marker;
1014                                         BLI_addtail(&pld->searchp, ld);
1015                                 }
1016                         }
1017                         
1018                         /* set current marker to NULL (so that we start from first) */
1019                         pld->marker= NULL;
1020                 }
1021                 
1022                 /* check if any matches */
1023                 if (pld->searchp.first == NULL) { 
1024                         pld->marker= NULL;
1025                         return;
1026                 }
1027                 
1028                 /* find first match */
1029                 for (ldc= pld->searchp.first; ldc; ldc= ldc->next) {
1030                         if (ldc->data == pld->marker)
1031                                 break;
1032                 }
1033                 if (ldc == NULL)
1034                         ldc= pld->searchp.first;
1035                         
1036                 /* Loop through the matches in a cyclic fashion, incrementing/decrementing step as appropriate 
1037                  * until step == 0. At this point, marker should be the correct marker.
1038                  */
1039                 if (step > 0) {
1040                         for (ld=ldc; ld && step; ld=ldn, step--)
1041                                 ldn= (ld->next) ? ld->next : pld->searchp.first;
1042                 }
1043                 else {
1044                         for (ld=ldc; ld && step; ld=ldn, step++)
1045                                 ldn= (ld->prev) ? ld->prev : pld->searchp.last;
1046                 }
1047                 
1048                 /* set marker */
1049                 if (ld)
1050                         pld->marker= ld->data;
1051         }
1052         else {
1053                 TimeMarker *marker, *next;
1054                 
1055                 /* if no marker, because we just ended searching, then set that to the start of the list */
1056                 if (pld->marker == NULL)
1057                         pld->marker= pld->act->markers.first;
1058                 
1059                 /* Loop through the markers in a cyclic fashion, incrementing/decrementing step as appropriate 
1060                  * until step == 0. At this point, marker should be the correct marker.
1061                  */
1062                 if (step > 0) {
1063                         for (marker=pld->marker; marker && step; marker=next, step--)
1064                                 next= (marker->next) ? marker->next : pld->act->markers.first;
1065                 }
1066                 else {
1067                         for (marker=pld->marker; marker && step; marker=next, step++)
1068                                 next= (marker->prev) ? marker->prev : pld->act->markers.last;
1069                 }
1070                 
1071                 /* it should be fairly impossible for marker to be NULL */
1072                 if (marker)
1073                         pld->marker= marker;
1074         }
1075 }
1076
1077 /* specially handle events for searching */
1078 static void poselib_preview_handle_search (tPoseLib_PreviewData *pld, unsigned short event, char ascii)
1079 {
1080         /* try doing some form of string manipulation first */
1081         switch (event) {
1082                 case BACKSPACEKEY:
1083                         if (pld->searchstr[0] && pld->search_cursor) {
1084                                 short len= strlen(pld->searchstr);
1085                                 short index= pld->search_cursor;
1086                                 short i;
1087                                 
1088                                 for (i = index; i <= len; i++) 
1089                                         pld->searchstr[i-1] = pld->searchstr[i];
1090                                 
1091                                 pld->search_cursor--;
1092                                 
1093                                 poselib_preview_get_next(pld, 1);
1094                                 pld->redraw = PL_PREVIEW_REDRAWALL;
1095                                 return;
1096                         }       
1097                         break;
1098                         
1099                 case DELKEY:
1100                         if (pld->searchstr[0] && pld->searchstr[1]) {
1101                                 short len= strlen(pld->searchstr);
1102                                 short index= pld->search_cursor;
1103                                 int i;
1104                                 
1105                                 if (index < len) {
1106                                         for (i = index; i < len; i++) 
1107                                                 pld->searchstr[i] = pld->searchstr[i+1];
1108                                                 
1109                                         poselib_preview_get_next(pld, 1);
1110                                         pld->redraw = PL_PREVIEW_REDRAWALL;
1111                                         return;
1112                                 }
1113                         }
1114                         break;
1115         }
1116         
1117         if (ascii) {
1118                 /* character to add to the string */
1119                 short index= pld->search_cursor;
1120                 short len= (pld->searchstr[0]) ? strlen(pld->searchstr) : 0;
1121                 short i;
1122                 
1123                 if (len) {
1124                         for (i = len; i > index; i--)  
1125                                 pld->searchstr[i]= pld->searchstr[i-1];
1126                 }
1127                 else
1128                         pld->searchstr[1]= 0;
1129                         
1130                 pld->searchstr[index]= ascii;
1131                 pld->search_cursor++;
1132                 
1133                 poselib_preview_get_next(pld, 1);
1134                 pld->redraw = PL_PREVIEW_REDRAWALL;
1135         }
1136 }
1137
1138 /* handle events for poselib_preview_poses */
1139 static int poselib_preview_handle_event (bContext *UNUSED(C), wmOperator *op, wmEvent *event)
1140 {
1141         tPoseLib_PreviewData *pld= op->customdata; 
1142         int ret = OPERATOR_RUNNING_MODAL;
1143         
1144         /* only accept 'press' event, and ignore 'release', so that we don't get double actions */
1145         if (ELEM(event->val, KM_PRESS, KM_NOTHING) == 0) {
1146                 //printf("PoseLib: skipping event with type '%s' and val %d\n", WM_key_event_string(event->type), event->val);
1147                 return ret; 
1148         }
1149         
1150         /* backup stuff that needs to occur before every operation
1151          *      - make a copy of searchstr, so that we know if cache needs to be rebuilt
1152          */
1153         strcpy(pld->searchold, pld->searchstr);
1154         
1155         /* if we're currently showing the original pose, only certain events are handled */
1156         if (pld->flag & PL_PREVIEW_SHOWORIGINAL) {
1157                 switch (event->type) {
1158                         /* exit - cancel */
1159                         case ESCKEY:
1160                         case RIGHTMOUSE:
1161                                 pld->state= PL_PREVIEW_CANCEL;
1162                                 break;
1163                                 
1164                         /* exit - confirm */
1165                         case LEFTMOUSE:
1166                         case RETKEY:
1167                         case PADENTER:
1168                         case SPACEKEY:
1169                                 pld->state= PL_PREVIEW_CONFIRM;
1170                                 break;
1171                         
1172                         /* view manipulation */
1173                         /* we add pass through here, so that the operators responsible for these can still run, 
1174                          * even though we still maintain control (as RUNNING_MODAL flag is still set too)
1175                          */
1176                         case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
1177                         case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
1178                         case PADPLUSKEY: case PADMINUS: 
1179                         case MIDDLEMOUSE: case MOUSEMOVE:
1180                                 //pld->redraw= PL_PREVIEW_REDRAWHEADER;
1181                                 ret = OPERATOR_PASS_THROUGH;
1182                                 break;
1183                                 
1184                         /* quicky compare to original */
1185                         case TABKEY:
1186                                 pld->flag &= ~PL_PREVIEW_SHOWORIGINAL;
1187                                 pld->redraw= PL_PREVIEW_REDRAWALL;
1188                                 break;
1189                 }
1190                 
1191                 /* EXITS HERE... */
1192                 return ret;
1193         }
1194         
1195         /* NORMAL EVENT HANDLING... */
1196         /* searching takes priority over normal activity */
1197         switch (event->type) {
1198                 /* exit - cancel */
1199                 case ESCKEY:
1200                 case RIGHTMOUSE:
1201                         pld->state= PL_PREVIEW_CANCEL;
1202                         break;
1203                         
1204                 /* exit - confirm */
1205                 case LEFTMOUSE:
1206                 case RETKEY:
1207                 case PADENTER:
1208                 case SPACEKEY:
1209                         pld->state= PL_PREVIEW_CONFIRM;
1210                         break;
1211                         
1212                 /* toggle between original pose and poselib pose*/
1213                 case TABKEY:
1214                         pld->flag |= PL_PREVIEW_SHOWORIGINAL;
1215                         pld->redraw= PL_PREVIEW_REDRAWALL;
1216                         break;
1217                 
1218                 /* change to previous pose (cyclic) */
1219                 case PAGEUPKEY:
1220                 case WHEELUPMOUSE:
1221                         poselib_preview_get_next(pld, -1);
1222                         pld->redraw= PL_PREVIEW_REDRAWALL;
1223                         break;
1224                 
1225                 /* change to next pose (cyclic) */
1226                 case PAGEDOWNKEY:
1227                 case WHEELDOWNMOUSE:
1228                         poselib_preview_get_next(pld, 1);
1229                         pld->redraw= PL_PREVIEW_REDRAWALL;
1230                         break;
1231                 
1232                 /* jump 5 poses (cyclic, back) */
1233                 case DOWNARROWKEY:
1234                         poselib_preview_get_next(pld, -5);
1235                         pld->redraw= PL_PREVIEW_REDRAWALL;
1236                         break;
1237                 
1238                 /* jump 5 poses (cyclic, forward) */
1239                 case UPARROWKEY:
1240                         poselib_preview_get_next(pld, 5);
1241                         pld->redraw= PL_PREVIEW_REDRAWALL;
1242                         break;
1243                 
1244                 /* change to next pose or searching cursor control */
1245                 case RIGHTARROWKEY:
1246                         if (pld->searchstr[0]) {
1247                                 /* move text-cursor to the right */
1248                                 if (pld->search_cursor < strlen(pld->searchstr))
1249                                         pld->search_cursor++;
1250                                 pld->redraw= PL_PREVIEW_REDRAWHEADER;
1251                         }
1252                         else {
1253                                 /* change to next pose (cyclic) */
1254                                 poselib_preview_get_next(pld, 1);
1255                                 pld->redraw= PL_PREVIEW_REDRAWALL;
1256                         }
1257                         break;
1258                         
1259                 /* change to next pose or searching cursor control */
1260                 case LEFTARROWKEY:
1261                         if (pld->searchstr[0]) {
1262                                 /* move text-cursor to the left */
1263                                 if (pld->search_cursor)
1264                                         pld->search_cursor--;
1265                                 pld->redraw= PL_PREVIEW_REDRAWHEADER;
1266                         }
1267                         else {
1268                                 /* change to previous pose (cyclic) */
1269                                 poselib_preview_get_next(pld, -1);
1270                                 pld->redraw= PL_PREVIEW_REDRAWALL;
1271                         }
1272                         break;
1273                         
1274                 /* change to first pose or start of searching string */
1275                 case HOMEKEY:
1276                         if (pld->searchstr[0]) {
1277                                 pld->search_cursor= 0;
1278                                 pld->redraw= PL_PREVIEW_REDRAWHEADER;
1279                         }
1280                         else {
1281                                 /* change to first pose */
1282                                 pld->marker= pld->act->markers.first;
1283                                 pld->act->active_marker= 1;
1284                                 
1285                                 pld->redraw= PL_PREVIEW_REDRAWALL;
1286                         }
1287                         break;
1288                         
1289                 /* change to last pose or start of searching string */
1290                 case ENDKEY:
1291                         if (pld->searchstr[0]) {
1292                                 pld->search_cursor= strlen(pld->searchstr);
1293                                 pld->redraw= PL_PREVIEW_REDRAWHEADER;
1294                         }
1295                         else {
1296                                 /* change to last pose */
1297                                 pld->marker= pld->act->markers.last;
1298                                 pld->act->active_marker= BLI_countlist(&pld->act->markers);
1299                                 
1300                                 pld->redraw= PL_PREVIEW_REDRAWALL;
1301                         }
1302                         break;
1303                 
1304                 /* view manipulation */
1305                 /* we add pass through here, so that the operators responsible for these can still run, 
1306                  * even though we still maintain control (as RUNNING_MODAL flag is still set too)
1307                  */
1308                 case MIDDLEMOUSE: case MOUSEMOVE:
1309                         //pld->redraw= PL_PREVIEW_REDRAWHEADER;
1310                         ret = OPERATOR_PASS_THROUGH;
1311                         break;
1312                         
1313                 /* view manipulation, or searching */
1314                 case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
1315                 case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
1316                 case PADPLUSKEY: case PADMINUS:
1317                         if (pld->searchstr[0]) {
1318                                 /* searching... */
1319                                 poselib_preview_handle_search(pld, event->type, event->ascii);
1320                         }
1321                         else {
1322                                 /* view manipulation (see above) */
1323                                 //pld->redraw= PL_PREVIEW_REDRAWHEADER;
1324                                 ret = OPERATOR_PASS_THROUGH;
1325                         }
1326                         break;
1327                         
1328                 /* otherwise, assume that searching might be able to handle it */
1329                 default:
1330                         poselib_preview_handle_search(pld, event->type, event->ascii);
1331                         break;
1332         }
1333         
1334         return ret;
1335 }
1336
1337 /* ---------------------------- */
1338
1339 /* Init PoseLib Previewing data */
1340 static void poselib_preview_init_data (bContext *C, wmOperator *op)
1341 {
1342         tPoseLib_PreviewData *pld;
1343         Object *ob= get_poselib_object(C);
1344         int pose_index = RNA_int_get(op->ptr, "pose_index");
1345         
1346         /* set up preview state info */
1347         op->customdata= pld= MEM_callocN(sizeof(tPoseLib_PreviewData), "PoseLib Preview Data");
1348         
1349         /* get basic data */
1350         pld->ob= ob;
1351         pld->arm= (ob) ? (ob->data) : NULL;
1352         pld->pose= (ob) ? (ob->pose) : NULL;
1353         pld->act= (ob) ? (ob->poselib) : NULL;
1354         
1355         pld->scene= CTX_data_scene(C);
1356         pld->sa= CTX_wm_area(C);
1357         
1358         /* get starting pose based on RNA-props for this operator */
1359         if (pose_index == -1)
1360                 pld->marker= poselib_get_active_pose(pld->act);
1361         else if (pose_index == -2)
1362                 pld->flag |= PL_PREVIEW_SHOWORIGINAL;
1363         else
1364                 pld->marker= (pld->act) ? BLI_findlink(&pld->act->markers, pose_index) : NULL;
1365         
1366         /* check if valid poselib */
1367         if (ELEM3(NULL, pld->ob, pld->pose, pld->arm)) {
1368                 BKE_report(op->reports, RPT_ERROR, "PoseLib is only for Armatures in PoseMode");
1369                 pld->state= PL_PREVIEW_ERROR;
1370                 return;
1371         }
1372         if (pld->act == NULL) {
1373                 BKE_report(op->reports, RPT_ERROR, "Object doesn't have a valid PoseLib");
1374                 pld->state= PL_PREVIEW_ERROR;
1375                 return;
1376         }
1377         if (pld->marker == NULL) {
1378                 if (pld->act->markers.first) {
1379                         /* just use first one then... */
1380                         pld->marker= pld->act->markers.first;
1381                         if (pose_index > -2) 
1382                                 BKE_report(op->reports, RPT_WARNING, "PoseLib had no active pose");
1383                 }
1384                 else {
1385                         BKE_report(op->reports, RPT_ERROR, "PoseLib has no poses to preview/apply");
1386                         pld->state= PL_PREVIEW_ERROR;
1387                         return;
1388                 }
1389         }
1390         
1391         /* get ID pointer for applying poses */
1392         RNA_id_pointer_create(&ob->id, &pld->rna_ptr);
1393         
1394         /* make backups for restoring pose */
1395         poselib_backup_posecopy(pld);
1396         
1397         /* set flags for running */
1398         pld->state= PL_PREVIEW_RUNNING;
1399         pld->redraw= PL_PREVIEW_REDRAWALL;
1400         pld->flag |= PL_PREVIEW_FIRSTTIME;
1401         
1402         /* set depsgraph flags */
1403                 /* make sure the lock is set OK, unlock can be accidentally saved? */
1404         pld->pose->flag |= POSE_LOCKED;
1405         pld->pose->flag &= ~POSE_DO_UNLOCK;
1406         
1407         /* clear strings + search */
1408         strcpy(pld->headerstr, "");
1409         strcpy(pld->searchstr, "");
1410         strcpy(pld->searchold, "");
1411         pld->search_cursor= 0;
1412 }
1413
1414 /* After previewing poses */
1415 static void poselib_preview_cleanup (bContext *C, wmOperator *op)
1416 {
1417         tPoseLib_PreviewData *pld= (tPoseLib_PreviewData *)op->customdata;
1418         Scene *scene= pld->scene;
1419         Object *ob= pld->ob;
1420         bPose *pose= pld->pose;
1421         bArmature *arm= pld->arm;
1422         bAction *act= pld->act;
1423         TimeMarker *marker= pld->marker;
1424         
1425         /* redraw the header so that it doesn't show any of our stuff anymore */
1426         ED_area_headerprint(pld->sa, NULL);
1427         
1428         /* this signal does one recalc on pose, then unlocks, so ESC or edit will work */
1429         pose->flag |= POSE_DO_UNLOCK;
1430         
1431         /* clear pose if cancelled */
1432         if (pld->state == PL_PREVIEW_CANCEL) {
1433                 poselib_backup_restore(pld);
1434                 
1435                 /* old optimize trick... this enforces to bypass the depgraph 
1436                  *      - note: code copied from transform_generics.c -> recalcData()
1437                  */
1438                 if ((arm->flag & ARM_DELAYDEFORM)==0)
1439                         DAG_id_tag_update(&ob->id, OB_RECALC_DATA);  /* sets recalc flags */
1440                 else
1441                         where_is_pose(scene, ob);
1442                 
1443         }
1444         else if (pld->state == PL_PREVIEW_CONFIRM) {
1445                 /* tag poses as appropriate */
1446                 poselib_keytag_pose(C, scene, pld);
1447                 
1448                 /* change active pose setting */
1449                 act->active_marker= BLI_findindex(&act->markers, marker) + 1;
1450                 action_set_activemarker(act, marker, 0);
1451                 
1452                 /* Update event for pose and deformation children */
1453                 DAG_id_tag_update(&ob->id, OB_RECALC_DATA);
1454                 
1455                 /* updates */
1456                 if (IS_AUTOKEY_MODE(scene, NORMAL)) {
1457                         //remake_action_ipos(ob->action);
1458                 }
1459                 else
1460                         where_is_pose(scene, ob);
1461         }
1462         
1463         /* free memory used for backups and searching */
1464         poselib_backup_free_data(pld);
1465         BLI_freelistN(&pld->searchp);
1466         
1467         /* free temp data for operator */
1468         MEM_freeN(pld);
1469         op->customdata= NULL;
1470 }
1471
1472 /* End previewing operation */
1473 static int poselib_preview_exit (bContext *C, wmOperator *op)
1474 {
1475         tPoseLib_PreviewData *pld= op->customdata;
1476         int exit_state = pld->state;
1477         
1478         /* finish up */
1479         poselib_preview_cleanup(C, op);
1480         
1481         if (ELEM(exit_state, PL_PREVIEW_CANCEL, PL_PREVIEW_ERROR))
1482                 return OPERATOR_CANCELLED;
1483         else
1484                 return OPERATOR_FINISHED;
1485 }
1486
1487 /* Cancel previewing operation (called when exiting Blender) */
1488 static int poselib_preview_cancel (bContext *C, wmOperator *op)
1489 {
1490         poselib_preview_exit(C, op);
1491         return OPERATOR_CANCELLED;
1492 }
1493
1494 /* main modal status check */
1495 static int poselib_preview_modal (bContext *C, wmOperator *op, wmEvent *event)
1496 {
1497         tPoseLib_PreviewData *pld= op->customdata;
1498         int ret;
1499         
1500         /* 1) check state to see if we're still running */
1501         if (pld->state != PL_PREVIEW_RUNNING)
1502                 return poselib_preview_exit(C, op);
1503         
1504         /* 2) handle events */
1505         ret= poselib_preview_handle_event(C, op, event);
1506         
1507         /* 3) apply changes and redraw, otherwise, confirming goes wrong */
1508         if (pld->redraw)
1509                 poselib_preview_apply(C, op);
1510         
1511         return ret;
1512 }
1513
1514 /* Modal Operator init */
1515 static int poselib_preview_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
1516 {
1517         tPoseLib_PreviewData *pld;
1518         
1519         /* check if everything is ok, and init settings for modal operator */
1520         poselib_preview_init_data(C, op);
1521         pld= (tPoseLib_PreviewData *)op->customdata;
1522         
1523         if (pld->state == PL_PREVIEW_ERROR) {
1524                 /* an error occurred, so free temp mem used */
1525                 poselib_preview_cleanup(C, op);
1526                 return OPERATOR_CANCELLED;
1527         }
1528         
1529         /* do initial apply to have something to look at */
1530         poselib_preview_apply(C, op);
1531         
1532         /* add temp handler if we're running as a modal operator */
1533         WM_event_add_modal_handler(C, op);
1534
1535         return OPERATOR_RUNNING_MODAL;
1536 }
1537
1538 /* Repeat operator */
1539 static int poselib_preview_exec (bContext *C, wmOperator *op)
1540 {
1541         tPoseLib_PreviewData *pld;
1542         
1543         /* check if everything is ok, and init settings for modal operator */
1544         poselib_preview_init_data(C, op);
1545         pld= (tPoseLib_PreviewData *)op->customdata;
1546         
1547         if (pld->state == PL_PREVIEW_ERROR) {
1548                 /* an error occurred, so free temp mem used */
1549                 poselib_preview_cleanup(C, op);
1550                 return OPERATOR_CANCELLED;
1551         }
1552         
1553         /* the exec() callback is effectively a 'run-once' scenario, so set the state to that
1554          * so that everything draws correctly
1555          */
1556         pld->state = PL_PREVIEW_RUNONCE;
1557         
1558         /* apply the active pose */
1559         poselib_preview_apply(C, op);
1560         
1561         /* now, set the status to exit */
1562         pld->state = PL_PREVIEW_CONFIRM;
1563         
1564         /* cleanup */
1565         return poselib_preview_exit(C, op);
1566 }
1567
1568 void POSELIB_OT_browse_interactive (wmOperatorType *ot)
1569 {
1570         /* identifiers */
1571         ot->name= "PoseLib Browse Poses";
1572         ot->idname= "POSELIB_OT_browse_interactive";
1573         ot->description= "Interactively browse poses in 3D-View";
1574         
1575         /* callbacks */
1576         ot->invoke= poselib_preview_invoke;
1577         ot->modal= poselib_preview_modal;
1578         ot->cancel= poselib_preview_cancel;
1579         ot->exec= poselib_preview_exec;
1580         ot->poll= has_poselib_pose_data_poll;
1581         
1582         /* flags */
1583         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
1584         
1585         /* properties */        
1586                 // TODO: make the pose_index into a proper enum instead of a cryptic int...
1587         ot->prop= RNA_def_int(ot->srna, "pose_index", -1, -2, INT_MAX, "Pose", "Index of the pose to apply (-2 for no change to pose, -1 for poselib active pose)", 0, INT_MAX);
1588         
1589         // XXX: percentage vs factor?
1590         /* not used yet */
1591         /* RNA_def_float_factor(ot->srna, "blend_factor", 1.0f, 0.0f, 1.0f, "Blend Factor", "Amount that the pose is applied on top of the existing poses", 0.0f, 1.0f); */
1592 }
1593
1594 void POSELIB_OT_apply_pose (wmOperatorType *ot)
1595 {
1596         /* identifiers */
1597         ot->name = "Apply Pose Library Pose";
1598         ot->idname = "POSELIB_OT_apply_pose";
1599         ot->description = "Apply specified Pose Library pose to the rig";
1600         
1601         /* callbacks */
1602         ot->exec= poselib_preview_exec;
1603         ot->poll= has_poselib_pose_data_poll;
1604         
1605         /* flags */
1606         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
1607         
1608         /* properties */        
1609                 // TODO: make the pose_index into a proper enum instead of a cryptic int...
1610         ot->prop= RNA_def_int(ot->srna, "pose_index", -1, -2, INT_MAX, "Pose", "Index of the pose to apply (-2 for no change to pose, -1 for poselib active pose)", 0, INT_MAX);
1611 }