Depsgraph: remove EvaluationContext, pass Depsgraph instead.
[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 = BKE_action_add(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 totcount;           /* total number of elements to work on */
838
839         short state;            /* state of main loop */
840         short redraw;           /* redraw/update settings during main loop */
841         short flag;             /* flags for various settings */
842
843         short search_cursor;    /* position of cursor in searchstr (cursor occurs before the item at the nominated index) */
844         char searchstr[64];     /* (Part of) Name to search for to filter poses that get shown */
845         char searchold[64];     /* Previously set searchstr (from last loop run), so that we can detected when to rebuild searchp */
846         
847         char headerstr[UI_MAX_DRAW_STR];    /* Info-text to print in header */
848 } tPoseLib_PreviewData;
849
850 /* defines for tPoseLib_PreviewData->state values */
851 enum {
852         PL_PREVIEW_ERROR = -1,
853         PL_PREVIEW_RUNNING,
854         PL_PREVIEW_CONFIRM,
855         PL_PREVIEW_CANCEL,
856         PL_PREVIEW_RUNONCE 
857 };
858
859 /* defines for tPoseLib_PreviewData->redraw values */
860 enum {
861         PL_PREVIEW_NOREDRAW = 0,
862         PL_PREVIEW_REDRAWALL,
863         PL_PREVIEW_REDRAWHEADER,
864 };
865
866 /* defines for tPoseLib_PreviewData->flag values */
867 enum {
868         PL_PREVIEW_FIRSTTIME    = (1 << 0),
869         PL_PREVIEW_SHOWORIGINAL = (1 << 1),
870         PL_PREVIEW_ANY_BONE_SELECTED = (1 << 2),
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         bool selected = false;
891
892         /* determine whether any bone is selected. */
893         LISTBASE_FOREACH (bPoseChannel *, bchan, &pld->pose->chanbase) {
894                 selected = bchan->bone != NULL && bchan->bone->flag & BONE_SELECTED;
895                 if (selected) {
896                         pld->flag |= PL_PREVIEW_ANY_BONE_SELECTED;
897                         break;
898                 }
899         }
900         if (!selected) {
901                 pld->flag &= ~PL_PREVIEW_ANY_BONE_SELECTED;
902         }
903
904         /* for each posechannel that has an actionchannel in */
905         for (agrp = pld->act->groups.first; agrp; agrp = agrp->next) {
906                 /* try to find posechannel */
907                 pchan = BKE_pose_channel_find_name(pld->pose, agrp->name);
908                 
909                 /* backup data if available */
910                 if (pchan) {
911                         tPoseLib_Backup *plb;
912                         
913                         /* store backup */
914                         plb = MEM_callocN(sizeof(tPoseLib_Backup), "tPoseLib_Backup");
915                         
916                         plb->pchan = pchan;
917                         memcpy(&plb->olddata, plb->pchan, sizeof(bPoseChannel));
918                         
919                         if (pchan->prop)
920                                 plb->oldprops = IDP_CopyProperty(pchan->prop);
921                         
922                         BLI_addtail(&pld->backups, plb);
923                         
924                         /* mark as being affected */
925                         pld->totcount++;
926                 }
927         }
928 }
929
930 /* Restores original pose */
931 static void poselib_backup_restore(tPoseLib_PreviewData *pld)
932 {
933         tPoseLib_Backup *plb;
934         
935         for (plb = pld->backups.first; plb; plb = plb->next) {
936                 /* copy most of data straight back */
937                 memcpy(plb->pchan, &plb->olddata, sizeof(bPoseChannel));
938
939                 /* just overwrite values of properties from the stored copies (there should be some) */
940                 if (plb->oldprops)
941                         IDP_SyncGroupValues(plb->pchan->prop, plb->oldprops);
942
943                 /* TODO: constraints settings aren't restored yet, even though these could change (though not that likely) */
944         }
945 }
946
947 /* Free list of backups, including any side data it may use */
948 static void poselib_backup_free_data(tPoseLib_PreviewData *pld)
949 {
950         tPoseLib_Backup *plb, *plbn;
951
952         for (plb = pld->backups.first; plb; plb = plbn) {
953                 plbn = plb->next;
954
955                 /* free custom data */
956                 if (plb->oldprops) {
957                         IDP_FreeProperty(plb->oldprops);
958                         MEM_freeN(plb->oldprops);
959                 }
960                 
961                 /* free backup element now */
962                 BLI_freelinkN(&pld->backups, plb);
963         }
964 }
965
966 /* ---------------------------- */
967
968 /* Applies the appropriate stored pose from the pose-library to the current pose
969  *      - assumes that a valid object, with a poselib has been supplied
970  *      - gets the string to print in the header
971  *  - this code is based on the code for extract_pose_from_action in blenkernel/action.c
972  */
973 static void poselib_apply_pose(tPoseLib_PreviewData *pld)
974 {
975         PointerRNA *ptr = &pld->rna_ptr;
976         bArmature *arm = pld->arm;
977         bPose *pose = pld->pose;
978         bPoseChannel *pchan;
979         bAction *act = pld->act;
980         bActionGroup *agrp;
981         
982         KeyframeEditData ked = {{NULL}};
983         KeyframeEditFunc group_ok_cb;
984         int frame = 1;
985         const bool any_bone_selected = pld->flag & PL_PREVIEW_ANY_BONE_SELECTED;
986         
987         /* get the frame */
988         if (pld->marker)
989                 frame = pld->marker->frame;
990         else
991                 return;
992         
993         
994         /* init settings for testing groups for keyframes */
995         group_ok_cb = ANIM_editkeyframes_ok(BEZT_OK_FRAMERANGE);
996         ked.f1 = ((float)frame) - 0.5f;
997         ked.f2 = ((float)frame) + 0.5f;
998
999         /* start applying - only those channels which have a key at this point in time! */
1000         for (agrp = act->groups.first; agrp; agrp = agrp->next) {
1001                 /* check if group has any keyframes */
1002                 if (ANIM_animchanneldata_keyframes_loop(&ked, NULL, agrp, ALE_GROUP, NULL, group_ok_cb, NULL)) {
1003                         /* has keyframe on this frame, so try to get a PoseChannel with this name */
1004                         pchan = BKE_pose_channel_find_name(pose, agrp->name);
1005                         
1006                         if (pchan) {
1007                                 bool ok = 0;
1008                                 
1009                                 /* check if this bone should get any animation applied */
1010                                 if (!any_bone_selected) {
1011                                         /* if no bones are selected, then any bone is ok */
1012                                         ok = 1;
1013                                 }
1014                                 else if (pchan->bone) {
1015                                         /* only ok if bone is visible and selected */
1016                                         if ((pchan->bone->flag & BONE_SELECTED) &&
1017                                             (pchan->bone->flag & BONE_HIDDEN_P) == 0 &&
1018                                             (pchan->bone->layer & arm->layer))
1019                                         {
1020                                                 ok = 1;
1021                                         }
1022                                 }
1023
1024                                 if (ok) 
1025                                         animsys_evaluate_action_group(ptr, act, agrp, NULL, (float)frame);
1026                         }
1027                 }
1028         }
1029 }
1030
1031 /* Auto-keys/tags bones affected by the pose used from the poselib */
1032 static void poselib_keytag_pose(bContext *C, Scene *scene, tPoseLib_PreviewData *pld)
1033 {
1034         bPose *pose = pld->pose;
1035         bPoseChannel *pchan;
1036         bAction *act = pld->act;
1037         bActionGroup *agrp;
1038         
1039         KeyingSet *ks = ANIM_get_keyingset_for_autokeying(scene, ANIM_KS_WHOLE_CHARACTER_ID);
1040         ListBase dsources = {NULL, NULL};
1041         bool autokey = autokeyframe_cfra_can_key(scene, &pld->ob->id);
1042         const bool any_bone_selected = pld->flag & PL_PREVIEW_ANY_BONE_SELECTED;
1043
1044         /* start tagging/keying */
1045         for (agrp = act->groups.first; agrp; agrp = agrp->next) {
1046                 /* only for selected bones unless there aren't any selected, in which case all are included  */
1047                 pchan = BKE_pose_channel_find_name(pose, agrp->name);
1048                 
1049                 if (pchan) {
1050                         if (!any_bone_selected || ((pchan->bone) && (pchan->bone->flag & BONE_SELECTED))) {
1051                                 if (autokey) {
1052                                         /* add datasource override for the PoseChannel, to be used later */
1053                                         ANIM_relative_keyingset_add_source(&dsources, &pld->ob->id, &RNA_PoseBone, pchan); 
1054                                         
1055                                         /* clear any unkeyed tags */
1056                                         if (pchan->bone)
1057                                                 pchan->bone->flag &= ~BONE_UNKEYED;
1058                                 }
1059                                 else {
1060                                         /* add unkeyed tags */
1061                                         if (pchan->bone)
1062                                                 pchan->bone->flag |= BONE_UNKEYED;
1063                                 }
1064                         }
1065                 }
1066         }
1067         
1068         /* perform actual auto-keying now */
1069         if (autokey) {
1070                 /* insert keyframes for all relevant bones in one go */
1071                 ANIM_apply_keyingset(C, &dsources, NULL, ks, MODIFYKEY_MODE_INSERT, (float)CFRA);
1072                 BLI_freelistN(&dsources);
1073         }
1074         
1075         /* send notifiers for this */
1076         WM_event_add_notifier(C, NC_ANIMATION | ND_KEYFRAME | NA_EDITED, NULL);
1077 }
1078
1079 /* Apply the relevant changes to the pose */
1080 static void poselib_preview_apply(bContext *C, wmOperator *op)
1081 {
1082         tPoseLib_PreviewData *pld = (tPoseLib_PreviewData *)op->customdata;
1083         
1084         /* only recalc pose (and its dependencies) if pose has changed */
1085         if (pld->redraw == PL_PREVIEW_REDRAWALL) {
1086                 /* don't clear pose if firsttime */
1087                 if ((pld->flag & PL_PREVIEW_FIRSTTIME) == 0)
1088                         poselib_backup_restore(pld);
1089                 else
1090                         pld->flag &= ~PL_PREVIEW_FIRSTTIME;
1091                         
1092                 /* pose should be the right one to draw (unless we're temporarily not showing it) */
1093                 if ((pld->flag & PL_PREVIEW_SHOWORIGINAL) == 0) {
1094                         RNA_int_set(op->ptr, "pose_index", BLI_findindex(&pld->act->markers, pld->marker));
1095                         poselib_apply_pose(pld);
1096                 }
1097                 else
1098                         RNA_int_set(op->ptr, "pose_index", -2);  /* -2 means don't apply any pose */
1099                 
1100                 /* old optimize trick... this enforces to bypass the depgraph 
1101                  *      - note: code copied from transform_generics.c -> recalcData()
1102                  */
1103                 // FIXME: shouldn't this use the builtin stuff?
1104                 if ((pld->arm->flag & ARM_DELAYDEFORM) == 0)
1105                         DEG_id_tag_update(&pld->ob->id, OB_RECALC_DATA);  /* sets recalc flags */
1106                 else
1107                         BKE_pose_where_is(CTX_data_depsgraph(C), pld->scene, pld->ob);
1108         }
1109         
1110         /* do header print - if interactively previewing */
1111         if (pld->state == PL_PREVIEW_RUNNING) {
1112                 if (pld->flag & PL_PREVIEW_SHOWORIGINAL) {
1113                         BLI_strncpy(pld->headerstr,
1114                                     IFACE_("PoseLib Previewing Pose: [Showing Original Pose] | Use Tab to start previewing poses again"),
1115                                     sizeof(pld->headerstr));
1116                         ED_area_headerprint(pld->sa, pld->headerstr);
1117                 }
1118                 else if (pld->searchstr[0]) {
1119                         char tempstr[65];
1120                         char markern[64];
1121                         short index;
1122                         
1123                         /* get search-string */
1124                         index = pld->search_cursor;
1125                         
1126                         if (index >= 0 && index < sizeof(tempstr) - 1) {
1127                                 memcpy(&tempstr[0], &pld->searchstr[0], index);
1128                                 tempstr[index] = '|';
1129                                 memcpy(&tempstr[index + 1], &pld->searchstr[index], (sizeof(tempstr) - 1) - index);
1130                         }
1131                         else {
1132                                 BLI_strncpy(tempstr, pld->searchstr, sizeof(tempstr));
1133                         }
1134                         
1135                         /* get marker name */
1136                         BLI_strncpy(markern, pld->marker ? pld->marker->name : "No Matches", sizeof(markern));
1137
1138                         BLI_snprintf(pld->headerstr, sizeof(pld->headerstr),
1139                                      IFACE_("PoseLib Previewing Pose: Filter - [%s] | "
1140                                             "Current Pose - \"%s\"  | "
1141                                             "Use ScrollWheel or PageUp/Down to change"),
1142                                      tempstr, markern);
1143                         ED_area_headerprint(pld->sa, pld->headerstr);
1144                 }
1145                 else {
1146                         BLI_snprintf(pld->headerstr, sizeof(pld->headerstr),
1147                                      IFACE_("PoseLib Previewing Pose: \"%s\"  | "
1148                                             "Use ScrollWheel or PageUp/Down to change"),
1149                                      pld->marker->name);
1150                         ED_area_headerprint(pld->sa, pld->headerstr);
1151                 }
1152         }
1153         
1154         /* request drawing of view + clear redraw flag */
1155         WM_event_add_notifier(C, NC_OBJECT | ND_POSE, pld->ob);
1156         pld->redraw = PL_PREVIEW_NOREDRAW;
1157 }
1158
1159 /* ---------------------------- */
1160
1161 /* This helper function is called during poselib_preview_poses to find the 
1162  * pose to preview next (after a change event)
1163  */
1164 static void poselib_preview_get_next(tPoseLib_PreviewData *pld, int step)
1165 {
1166         /* stop if not going anywhere, as we assume that there is a direction to move in */
1167         if (step == 0)
1168                 return;
1169         
1170         /* search-string dictates a special approach */
1171         if (pld->searchstr[0]) {
1172                 TimeMarker *marker;
1173                 LinkData *ld, *ldn, *ldc;
1174                 
1175                 /* free and rebuild if needed (i.e. if search-str changed) */
1176                 if (!STREQ(pld->searchstr, pld->searchold)) {
1177                         /* free list of temporary search matches */
1178                         BLI_freelistN(&pld->searchp);
1179                         
1180                         /* generate a new list of search matches */
1181                         for (marker = pld->act->markers.first; marker; marker = marker->next) {
1182                                 /* does the name partially match? 
1183                                  *  - don't worry about case, to make it easier for users to quickly input a name (or
1184                                  *        part of one), which is the whole point of this feature
1185                                  */
1186                                 if (BLI_strcasestr(marker->name, pld->searchstr)) {
1187                                         /* make link-data to store reference to it */
1188                                         ld = MEM_callocN(sizeof(LinkData), "PoseMatch");
1189                                         ld->data = marker;
1190                                         BLI_addtail(&pld->searchp, ld);
1191                                 }
1192                         }
1193                         
1194                         /* set current marker to NULL (so that we start from first) */
1195                         pld->marker = NULL;
1196                 }
1197                 
1198                 /* check if any matches */
1199                 if (BLI_listbase_is_empty(&pld->searchp)) {
1200                         pld->marker = NULL;
1201                         return;
1202                 }
1203                 
1204                 /* find first match */
1205                 for (ldc = pld->searchp.first; ldc; ldc = ldc->next) {
1206                         if (ldc->data == pld->marker)
1207                                 break;
1208                 }
1209                 if (ldc == NULL)
1210                         ldc = pld->searchp.first;
1211                         
1212                 /* Loop through the matches in a cyclic fashion, incrementing/decrementing step as appropriate 
1213                  * until step == 0. At this point, marker should be the correct marker.
1214                  */
1215                 if (step > 0) {
1216                         for (ld = ldc; ld && step; ld = ldn, step--)
1217                                 ldn = (ld->next) ? ld->next : pld->searchp.first;
1218                 }
1219                 else {
1220                         for (ld = ldc; ld && step; ld = ldn, step++)
1221                                 ldn = (ld->prev) ? ld->prev : pld->searchp.last;
1222                 }
1223                 
1224                 /* set marker */
1225                 if (ld)
1226                         pld->marker = ld->data;
1227         }
1228         else {
1229                 TimeMarker *marker, *next;
1230                 
1231                 /* if no marker, because we just ended searching, then set that to the start of the list */
1232                 if (pld->marker == NULL)
1233                         pld->marker = pld->act->markers.first;
1234                 
1235                 /* Loop through the markers in a cyclic fashion, incrementing/decrementing step as appropriate 
1236                  * until step == 0. At this point, marker should be the correct marker.
1237                  */
1238                 if (step > 0) {
1239                         for (marker = pld->marker; marker && step; marker = next, step--)
1240                                 next = (marker->next) ? marker->next : pld->act->markers.first;
1241                 }
1242                 else {
1243                         for (marker = pld->marker; marker && step; marker = next, step++)
1244                                 next = (marker->prev) ? marker->prev : pld->act->markers.last;
1245                 }
1246                 
1247                 /* it should be fairly impossible for marker to be NULL */
1248                 if (marker)
1249                         pld->marker = marker;
1250         }
1251 }
1252
1253 /* specially handle events for searching */
1254 static void poselib_preview_handle_search(tPoseLib_PreviewData *pld, unsigned short event, char ascii)
1255 {
1256         /* try doing some form of string manipulation first */
1257         switch (event) {
1258                 case BACKSPACEKEY:
1259                         if (pld->searchstr[0] && pld->search_cursor) {
1260                                 short len = strlen(pld->searchstr);
1261                                 short index = pld->search_cursor;
1262                                 short i;
1263                                 
1264                                 for (i = index; i <= len; i++) 
1265                                         pld->searchstr[i - 1] = pld->searchstr[i];
1266                                 
1267                                 pld->search_cursor--;
1268                                 
1269                                 poselib_preview_get_next(pld, 1);
1270                                 pld->redraw = PL_PREVIEW_REDRAWALL;
1271                                 return;
1272                         }
1273                         break;
1274                         
1275                 case DELKEY:
1276                         if (pld->searchstr[0] && pld->searchstr[1]) {
1277                                 short len = strlen(pld->searchstr);
1278                                 short index = pld->search_cursor;
1279                                 int i;
1280                                 
1281                                 if (index < len) {
1282                                         for (i = index; i < len; i++) 
1283                                                 pld->searchstr[i] = pld->searchstr[i + 1];
1284                                                 
1285                                         poselib_preview_get_next(pld, 1);
1286                                         pld->redraw = PL_PREVIEW_REDRAWALL;
1287                                         return;
1288                                 }
1289                         }
1290                         break;
1291         }
1292         
1293         if (ascii) {
1294                 /* character to add to the string */
1295                 short index = pld->search_cursor;
1296                 short len = (pld->searchstr[0]) ? strlen(pld->searchstr) : 0;
1297                 short i;
1298                 
1299                 if (len) {
1300                         for (i = len; i > index; i--)  
1301                                 pld->searchstr[i] = pld->searchstr[i - 1];
1302                 }
1303                 else
1304                         pld->searchstr[1] = 0;
1305                         
1306                 pld->searchstr[index] = ascii;
1307                 pld->search_cursor++;
1308                 
1309                 poselib_preview_get_next(pld, 1);
1310                 pld->redraw = PL_PREVIEW_REDRAWALL;
1311         }
1312 }
1313
1314 /* handle events for poselib_preview_poses */
1315 static int poselib_preview_handle_event(bContext *UNUSED(C), wmOperator *op, const wmEvent *event)
1316 {
1317         tPoseLib_PreviewData *pld = op->customdata;
1318         int ret = OPERATOR_RUNNING_MODAL;
1319         
1320         /* only accept 'press' event, and ignore 'release', so that we don't get double actions */
1321         if (ELEM(event->val, KM_PRESS, KM_NOTHING) == 0) {
1322                 //printf("PoseLib: skipping event with type '%s' and val %d\n", WM_key_event_string(event->type, false), event->val);
1323                 return ret; 
1324         }
1325         
1326         /* backup stuff that needs to occur before every operation
1327          *      - make a copy of searchstr, so that we know if cache needs to be rebuilt
1328          */
1329         BLI_strncpy(pld->searchold, pld->searchstr, sizeof(pld->searchold));
1330         
1331         /* if we're currently showing the original pose, only certain events are handled */
1332         if (pld->flag & PL_PREVIEW_SHOWORIGINAL) {
1333                 switch (event->type) {
1334                         /* exit - cancel */
1335                         case ESCKEY:
1336                         case RIGHTMOUSE:
1337                                 pld->state = PL_PREVIEW_CANCEL;
1338                                 break;
1339                                 
1340                         /* exit - confirm */
1341                         case LEFTMOUSE:
1342                         case RETKEY:
1343                         case PADENTER:
1344                         case SPACEKEY:
1345                                 pld->state = PL_PREVIEW_CONFIRM;
1346                                 break;
1347                         
1348                         /* view manipulation */
1349                         /* we add pass through here, so that the operators responsible for these can still run, 
1350                          * even though we still maintain control (as RUNNING_MODAL flag is still set too)
1351                          */
1352                         case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
1353                         case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
1354                         case PADPLUSKEY: case PADMINUS: 
1355                         case MIDDLEMOUSE: case MOUSEMOVE:
1356                                 //pld->redraw = PL_PREVIEW_REDRAWHEADER;
1357                                 ret = OPERATOR_PASS_THROUGH;
1358                                 break;
1359                                 
1360                         /* quicky compare to original */
1361                         case TABKEY:
1362                                 pld->flag &= ~PL_PREVIEW_SHOWORIGINAL;
1363                                 pld->redraw = PL_PREVIEW_REDRAWALL;
1364                                 break;
1365                 }
1366                 
1367                 /* EXITS HERE... */
1368                 return ret;
1369         }
1370         
1371         /* NORMAL EVENT HANDLING... */
1372         /* searching takes priority over normal activity */
1373         switch (event->type) {
1374                 /* exit - cancel */
1375                 case ESCKEY:
1376                 case RIGHTMOUSE:
1377                         pld->state = PL_PREVIEW_CANCEL;
1378                         break;
1379                         
1380                 /* exit - confirm */
1381                 case LEFTMOUSE:
1382                 case RETKEY:
1383                 case PADENTER:
1384                 case SPACEKEY:
1385                         pld->state = PL_PREVIEW_CONFIRM;
1386                         break;
1387                         
1388                 /* toggle between original pose and poselib pose*/
1389                 case TABKEY:
1390                         pld->flag |= PL_PREVIEW_SHOWORIGINAL;
1391                         pld->redraw = PL_PREVIEW_REDRAWALL;
1392                         break;
1393                 
1394                 /* change to previous pose (cyclic) */
1395                 case PAGEUPKEY:
1396                 case WHEELUPMOUSE:
1397                         poselib_preview_get_next(pld, -1);
1398                         pld->redraw = PL_PREVIEW_REDRAWALL;
1399                         break;
1400                 
1401                 /* change to next pose (cyclic) */
1402                 case PAGEDOWNKEY:
1403                 case WHEELDOWNMOUSE:
1404                         poselib_preview_get_next(pld, 1);
1405                         pld->redraw = PL_PREVIEW_REDRAWALL;
1406                         break;
1407                 
1408                 /* jump 5 poses (cyclic, back) */
1409                 case DOWNARROWKEY:
1410                         poselib_preview_get_next(pld, -5);
1411                         pld->redraw = PL_PREVIEW_REDRAWALL;
1412                         break;
1413                 
1414                 /* jump 5 poses (cyclic, forward) */
1415                 case UPARROWKEY:
1416                         poselib_preview_get_next(pld, 5);
1417                         pld->redraw = PL_PREVIEW_REDRAWALL;
1418                         break;
1419                 
1420                 /* change to next pose or searching cursor control */
1421                 case RIGHTARROWKEY:
1422                         if (pld->searchstr[0]) {
1423                                 /* move text-cursor to the right */
1424                                 if (pld->search_cursor < strlen(pld->searchstr))
1425                                         pld->search_cursor++;
1426                                 pld->redraw = PL_PREVIEW_REDRAWHEADER;
1427                         }
1428                         else {
1429                                 /* change to next pose (cyclic) */
1430                                 poselib_preview_get_next(pld, 1);
1431                                 pld->redraw = PL_PREVIEW_REDRAWALL;
1432                         }
1433                         break;
1434                         
1435                 /* change to next pose or searching cursor control */
1436                 case LEFTARROWKEY:
1437                         if (pld->searchstr[0]) {
1438                                 /* move text-cursor to the left */
1439                                 if (pld->search_cursor)
1440                                         pld->search_cursor--;
1441                                 pld->redraw = PL_PREVIEW_REDRAWHEADER;
1442                         }
1443                         else {
1444                                 /* change to previous pose (cyclic) */
1445                                 poselib_preview_get_next(pld, -1);
1446                                 pld->redraw = PL_PREVIEW_REDRAWALL;
1447                         }
1448                         break;
1449                         
1450                 /* change to first pose or start of searching string */
1451                 case HOMEKEY:
1452                         if (pld->searchstr[0]) {
1453                                 pld->search_cursor = 0;
1454                                 pld->redraw = PL_PREVIEW_REDRAWHEADER;
1455                         }
1456                         else {
1457                                 /* change to first pose */
1458                                 pld->marker = pld->act->markers.first;
1459                                 pld->act->active_marker = 1;
1460                                 
1461                                 pld->redraw = PL_PREVIEW_REDRAWALL;
1462                         }
1463                         break;
1464                         
1465                 /* change to last pose or start of searching string */
1466                 case ENDKEY:
1467                         if (pld->searchstr[0]) {
1468                                 pld->search_cursor = strlen(pld->searchstr);
1469                                 pld->redraw = PL_PREVIEW_REDRAWHEADER;
1470                         }
1471                         else {
1472                                 /* change to last pose */
1473                                 pld->marker = pld->act->markers.last;
1474                                 pld->act->active_marker = BLI_listbase_count(&pld->act->markers);
1475                                 
1476                                 pld->redraw = PL_PREVIEW_REDRAWALL;
1477                         }
1478                         break;
1479                 
1480                 /* view manipulation */
1481                 /* we add pass through here, so that the operators responsible for these can still run, 
1482                  * even though we still maintain control (as RUNNING_MODAL flag is still set too)
1483                  */
1484                 case MIDDLEMOUSE: case MOUSEMOVE:
1485                         //pld->redraw = PL_PREVIEW_REDRAWHEADER;
1486                         ret = OPERATOR_PASS_THROUGH;
1487                         break;
1488                         
1489                 /* view manipulation, or searching */
1490                 case PAD0: case PAD1: case PAD2: case PAD3: case PAD4:
1491                 case PAD5: case PAD6: case PAD7: case PAD8: case PAD9:
1492                 case PADPLUSKEY: case PADMINUS:
1493                         if (pld->searchstr[0]) {
1494                                 /* searching... */
1495                                 poselib_preview_handle_search(pld, event->type, event->ascii);
1496                         }
1497                         else {
1498                                 /* view manipulation (see above) */
1499                                 //pld->redraw = PL_PREVIEW_REDRAWHEADER;
1500                                 ret = OPERATOR_PASS_THROUGH;
1501                         }
1502                         break;
1503                         
1504                 /* otherwise, assume that searching might be able to handle it */
1505                 default:
1506                         poselib_preview_handle_search(pld, event->type, event->ascii);
1507                         break;
1508         }
1509         
1510         return ret;
1511 }
1512
1513 /* ---------------------------- */
1514
1515 /* Init PoseLib Previewing data */
1516 static void poselib_preview_init_data(bContext *C, wmOperator *op)
1517 {
1518         tPoseLib_PreviewData *pld;
1519         Object *ob = get_poselib_object(C);
1520         int pose_index = RNA_int_get(op->ptr, "pose_index");
1521         
1522         /* set up preview state info */
1523         op->customdata = pld = MEM_callocN(sizeof(tPoseLib_PreviewData), "PoseLib Preview Data");
1524         
1525         /* get basic data */
1526         pld->ob = ob;
1527         pld->arm = (ob) ? (ob->data) : NULL;
1528         pld->pose = (ob) ? (ob->pose) : NULL;
1529         pld->act = (ob) ? (ob->poselib) : NULL;
1530         
1531         pld->scene = CTX_data_scene(C);
1532         pld->sa = CTX_wm_area(C);
1533         
1534         /* get starting pose based on RNA-props for this operator */
1535         if (pose_index == -1)
1536                 pld->marker = poselib_get_active_pose(pld->act);
1537         else if (pose_index == -2)
1538                 pld->flag |= PL_PREVIEW_SHOWORIGINAL;
1539         else
1540                 pld->marker = (pld->act) ? BLI_findlink(&pld->act->markers, pose_index) : NULL;
1541         
1542         /* check if valid poselib */
1543         if (ELEM(NULL, pld->ob, pld->pose, pld->arm)) {
1544                 BKE_report(op->reports, RPT_ERROR, "Pose lib is only for armatures in pose mode");
1545                 pld->state = PL_PREVIEW_ERROR;
1546                 return;
1547         }
1548         if (pld->act == NULL) {
1549                 BKE_report(op->reports, RPT_ERROR, "Object does not have a valid pose lib");
1550                 pld->state = PL_PREVIEW_ERROR;
1551                 return;
1552         }
1553         if (pld->marker == NULL) {
1554                 if (pld->act->markers.first) {
1555                         /* just use first one then... */
1556                         pld->marker = pld->act->markers.first;
1557                         if (pose_index > -2) 
1558                                 BKE_report(op->reports, RPT_WARNING, "Pose lib had no active pose");
1559                 }
1560                 else {
1561                         BKE_report(op->reports, RPT_ERROR, "Pose lib has no poses to preview/apply");
1562                         pld->state = PL_PREVIEW_ERROR;
1563                         return;
1564                 }
1565         }
1566         
1567         /* get ID pointer for applying poses */
1568         RNA_id_pointer_create(&ob->id, &pld->rna_ptr);
1569         
1570         /* make backups for restoring pose */
1571         poselib_backup_posecopy(pld);
1572         
1573         /* set flags for running */
1574         pld->state = PL_PREVIEW_RUNNING;
1575         pld->redraw = PL_PREVIEW_REDRAWALL;
1576         pld->flag |= PL_PREVIEW_FIRSTTIME;
1577         
1578         /* set depsgraph flags */
1579         /* make sure the lock is set OK, unlock can be accidentally saved? */
1580         pld->pose->flag |= POSE_LOCKED;
1581         pld->pose->flag &= ~POSE_DO_UNLOCK;
1582         
1583         /* clear strings + search */
1584         pld->headerstr[0] = pld->searchstr[0] = pld->searchold[0] = '\0';
1585         pld->search_cursor = 0;
1586 }
1587
1588 /* After previewing poses */
1589 static void poselib_preview_cleanup(bContext *C, wmOperator *op)
1590 {
1591         tPoseLib_PreviewData *pld = (tPoseLib_PreviewData *)op->customdata;
1592         Scene *scene = pld->scene;
1593         Object *ob = pld->ob;
1594         bPose *pose = pld->pose;
1595         bArmature *arm = pld->arm;
1596         bAction *act = pld->act;
1597         TimeMarker *marker = pld->marker;
1598         
1599         /* redraw the header so that it doesn't show any of our stuff anymore */
1600         ED_area_headerprint(pld->sa, NULL);
1601         
1602         /* this signal does one recalc on pose, then unlocks, so ESC or edit will work */
1603         pose->flag |= POSE_DO_UNLOCK;
1604         
1605         /* clear pose if canceled */
1606         if (pld->state == PL_PREVIEW_CANCEL) {
1607                 poselib_backup_restore(pld);
1608                 
1609                 /* old optimize trick... this enforces to bypass the depgraph 
1610                  *      - note: code copied from transform_generics.c -> recalcData()
1611                  */
1612                 if ((arm->flag & ARM_DELAYDEFORM) == 0)
1613                         DEG_id_tag_update(&ob->id, OB_RECALC_DATA);  /* sets recalc flags */
1614                 else
1615                         BKE_pose_where_is(CTX_data_depsgraph(C), scene, ob);
1616         }
1617         else if (pld->state == PL_PREVIEW_CONFIRM) {
1618                 /* tag poses as appropriate */
1619                 poselib_keytag_pose(C, scene, pld);
1620                 
1621                 /* change active pose setting */
1622                 act->active_marker = BLI_findindex(&act->markers, marker) + 1;
1623                 action_set_activemarker(act, marker, NULL);
1624                 
1625                 /* Update event for pose and deformation children */
1626                 DEG_id_tag_update(&ob->id, OB_RECALC_DATA);
1627                 
1628                 /* updates */
1629                 if (IS_AUTOKEY_MODE(scene, NORMAL)) {
1630                         //remake_action_ipos(ob->action);
1631                 }
1632                 else
1633                         BKE_pose_where_is(CTX_data_depsgraph(C), scene, ob);
1634         }
1635         
1636         /* Request final redraw of the view. */
1637         WM_event_add_notifier(C, NC_OBJECT | ND_POSE, pld->ob);
1638         
1639         /* free memory used for backups and searching */
1640         poselib_backup_free_data(pld);
1641         BLI_freelistN(&pld->searchp);
1642         
1643         /* free temp data for operator */
1644         MEM_freeN(pld);
1645         op->customdata = NULL;
1646 }
1647
1648 /* End previewing operation */
1649 static int poselib_preview_exit(bContext *C, wmOperator *op)
1650 {
1651         tPoseLib_PreviewData *pld = op->customdata;
1652         int exit_state = pld->state;
1653         
1654         /* finish up */
1655         poselib_preview_cleanup(C, op);
1656         
1657         if (ELEM(exit_state, PL_PREVIEW_CANCEL, PL_PREVIEW_ERROR))
1658                 return OPERATOR_CANCELLED;
1659         else
1660                 return OPERATOR_FINISHED;
1661 }
1662
1663 /* Cancel previewing operation (called when exiting Blender) */
1664 static void poselib_preview_cancel(bContext *C, wmOperator *op)
1665 {
1666         poselib_preview_exit(C, op);
1667 }
1668
1669 /* main modal status check */
1670 static int poselib_preview_modal(bContext *C, wmOperator *op, const wmEvent *event)
1671 {
1672         tPoseLib_PreviewData *pld = op->customdata;
1673         int ret;
1674         
1675         /* 1) check state to see if we're still running */
1676         if (pld->state != PL_PREVIEW_RUNNING)
1677                 return poselib_preview_exit(C, op);
1678         
1679         /* 2) handle events */
1680         ret = poselib_preview_handle_event(C, op, event);
1681         
1682         /* 3) apply changes and redraw, otherwise, confirming goes wrong */
1683         if (pld->redraw)
1684                 poselib_preview_apply(C, op);
1685         
1686         return ret;
1687 }
1688
1689 /* Modal Operator init */
1690 static int poselib_preview_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
1691 {
1692         tPoseLib_PreviewData *pld;
1693         
1694         /* check if everything is ok, and init settings for modal operator */
1695         poselib_preview_init_data(C, op);
1696         pld = (tPoseLib_PreviewData *)op->customdata;
1697         
1698         if (pld->state == PL_PREVIEW_ERROR) {
1699                 /* an error occurred, so free temp mem used */
1700                 poselib_preview_cleanup(C, op);
1701                 return OPERATOR_CANCELLED;
1702         }
1703         
1704         /* do initial apply to have something to look at */
1705         poselib_preview_apply(C, op);
1706         
1707         /* add temp handler if we're running as a modal operator */
1708         WM_event_add_modal_handler(C, op);
1709
1710         return OPERATOR_RUNNING_MODAL;
1711 }
1712
1713 /* Repeat operator */
1714 static int poselib_preview_exec(bContext *C, wmOperator *op)
1715 {
1716         tPoseLib_PreviewData *pld;
1717         
1718         /* check if everything is ok, and init settings for modal operator */
1719         poselib_preview_init_data(C, op);
1720         pld = (tPoseLib_PreviewData *)op->customdata;
1721         
1722         if (pld->state == PL_PREVIEW_ERROR) {
1723                 /* an error occurred, so free temp mem used */
1724                 poselib_preview_cleanup(C, op);
1725                 return OPERATOR_CANCELLED;
1726         }
1727         
1728         /* the exec() callback is effectively a 'run-once' scenario, so set the state to that
1729          * so that everything draws correctly
1730          */
1731         pld->state = PL_PREVIEW_RUNONCE;
1732         
1733         /* apply the active pose */
1734         poselib_preview_apply(C, op);
1735         
1736         /* now, set the status to exit */
1737         pld->state = PL_PREVIEW_CONFIRM;
1738         
1739         /* cleanup */
1740         return poselib_preview_exit(C, op);
1741 }
1742
1743 void POSELIB_OT_browse_interactive(wmOperatorType *ot)
1744 {
1745         /* identifiers */
1746         ot->name = "PoseLib Browse Poses";
1747         ot->idname = "POSELIB_OT_browse_interactive";
1748         ot->description = "Interactively browse poses in 3D-View";
1749         
1750         /* callbacks */
1751         ot->invoke = poselib_preview_invoke;
1752         ot->modal = poselib_preview_modal;
1753         ot->cancel = poselib_preview_cancel;
1754         ot->exec = poselib_preview_exec;
1755         ot->poll = has_poselib_pose_data_poll;
1756         
1757         /* flags */
1758         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_BLOCKING;
1759         
1760         /* properties */
1761         // TODO: make the pose_index into a proper enum instead of a cryptic int...
1762         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);
1763         
1764         // XXX: percentage vs factor?
1765         /* not used yet */
1766         /* 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); */
1767 }
1768
1769 void POSELIB_OT_apply_pose(wmOperatorType *ot)
1770 {
1771         /* identifiers */
1772         ot->name = "Apply Pose Library Pose";
1773         ot->idname = "POSELIB_OT_apply_pose";
1774         ot->description = "Apply specified Pose Library pose to the rig";
1775         
1776         /* callbacks */
1777         ot->exec = poselib_preview_exec;
1778         ot->poll = has_poselib_pose_data_poll;
1779
1780         /* flags */
1781         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
1782
1783         /* properties */
1784         /* TODO: make the pose_index into a proper enum instead of a cryptic int... */
1785         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);
1786 }