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