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