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