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