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