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