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