2.5 - Action Editor Transforms now work again
[blender.git] / source / blender / editors / transform / transform_ops.c
1 /**
2  * $Id: transform_ops.c 17542 2008-11-23 15:27:53Z theeth $
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * Contributor(s): none yet.
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 #include "MEM_guardedalloc.h"
26
27 #include "DNA_space_types.h"
28
29 #include "RNA_access.h"
30 #include "RNA_define.h"
31
32 #include "BLI_arithb.h"
33
34 #include "BKE_utildefines.h"
35 #include "BKE_context.h"
36
37 #include "WM_api.h"
38 #include "WM_types.h"
39
40 #include "ED_screen.h"
41
42 #include "transform.h"
43
44 static void transformops_exit(bContext *C, wmOperator *op)
45 {
46         MEM_freeN(op->customdata);
47         op->customdata = NULL;
48 }
49
50 static void transformops_data(bContext *C, wmOperator *op, wmEvent *event)
51 {
52         int mode    = RNA_int_get(op->ptr, "mode");
53         int options = RNA_int_get(op->ptr, "options");
54         TransInfo *t = MEM_callocN(sizeof(TransInfo), "TransInfo data");
55         
56         initTransform(C, t, mode, options, event);
57
58         /* store data */
59         op->customdata = t;
60 }
61
62 static int transform_modal(bContext *C, wmOperator *op, wmEvent *event)
63 {
64         TransInfo *t = op->customdata;
65         
66         /* need to set context here, otherwise we get crashes with things that use context */
67         // XXX this seems quite hackish - Aligorith
68         t->context= C;
69         
70         transformEvent(t, event);
71         
72         transformApply(t);
73         
74         if (transformEnd(t))
75         {
76                 transformops_exit(C, op);
77                 return OPERATOR_FINISHED;
78         }
79         else
80         {
81                 return OPERATOR_RUNNING_MODAL;
82         }
83 }
84
85 static int transform_exec(bContext *C, wmOperator *op)
86 {
87         TransInfo *t = op->customdata;
88         
89         transformApply(t);
90         
91         transformEnd(t);
92
93         ED_region_tag_redraw(CTX_wm_region(C));
94
95         transformops_exit(C, op);
96         
97         return OPERATOR_FINISHED;
98 }
99
100 static int transform_invoke(bContext *C, wmOperator *op, wmEvent *event)
101 {
102         float value[4];
103         
104         RNA_float_get_array(op->ptr, "value", value);
105
106         /* makes op->customdata */
107         transformops_data(C, op, event);
108
109         if(!QuatIsNul(value)) {
110                 TransInfo *t = op->customdata;
111                 
112                 VECCOPY(t->values, value); /* SHOULD BE VEC-4 */
113                 
114                 return transform_exec(C, op);
115         }
116         else {
117                 /* add temp handler */
118                 WM_event_add_modal_handler(C, &CTX_wm_window(C)->handlers, op);
119
120                 return OPERATOR_RUNNING_MODAL;
121         }
122 }
123
124 void TFM_OT_transform(struct wmOperatorType *ot)
125 {
126         PropertyRNA *prop;
127         static float value[4] = {0, 0, 0};
128         
129         /* identifiers */
130         ot->name   = "Transform";
131         ot->idname = "TFM_OT_transform";
132
133         /* api callbacks */
134         ot->invoke = transform_invoke;
135         ot->exec   = transform_exec;
136         ot->modal  = transform_modal;
137         ot->poll   = ED_operator_areaactive;
138
139         RNA_def_property(ot->srna, "mode", PROP_INT, PROP_NONE);
140         RNA_def_property(ot->srna, "options", PROP_INT, PROP_NONE);
141         
142         prop = RNA_def_property(ot->srna, "value", PROP_FLOAT, PROP_VECTOR);
143         RNA_def_property_array(prop, 4);
144         RNA_def_property_float_array_default(prop, value);
145 }
146
147 void transform_operatortypes(void)
148 {
149         WM_operatortype_append(TFM_OT_transform);
150 }
151  
152 void transform_keymap_for_space(struct wmWindowManager *wm, struct ListBase *keymap, int spaceid)
153 {
154         wmKeymapItem *km;
155         switch(spaceid)
156         {
157                 case SPACE_VIEW3D:
158                         km = WM_keymap_add_item(keymap, "TFM_OT_transform", GKEY, KM_PRESS, 0, 0);
159                         RNA_int_set(km->ptr, "mode", TFM_TRANSLATION);
160
161                         km = WM_keymap_add_item(keymap, "TFM_OT_transform", RKEY, KM_PRESS, 0, 0);
162                         RNA_int_set(km->ptr, "mode", TFM_ROTATION);
163
164                         km = WM_keymap_add_item(keymap, "TFM_OT_transform", SKEY, KM_PRESS, 0, 0);
165                         RNA_int_set(km->ptr, "mode", TFM_RESIZE);
166
167                         km = WM_keymap_add_item(keymap, "TFM_OT_transform", WKEY, KM_PRESS, KM_SHIFT, 0);
168                         RNA_int_set(km->ptr, "mode", TFM_WARP);
169
170                         km = WM_keymap_add_item(keymap, "TFM_OT_transform", SKEY, KM_PRESS, KM_CTRL|KM_SHIFT, 0);
171                         RNA_int_set(km->ptr, "mode", TFM_TOSPHERE);
172                         
173                         km = WM_keymap_add_item(keymap, "TFM_OT_transform", SKEY, KM_PRESS, KM_ALT|KM_CTRL|KM_SHIFT, 0);
174                         RNA_int_set(km->ptr, "mode", TFM_SHEAR);
175                         
176                         break;
177                 case SPACE_ACTION:
178                         km= WM_keymap_add_item(keymap, "TFM_OT_transform", GKEY, KM_PRESS, 0, 0);
179                         RNA_int_set(km->ptr, "mode", TFM_TIME_TRANSLATE);
180                         
181                         km= WM_keymap_add_item(keymap, "TFM_OT_transform", EKEY, KM_PRESS, 0, 0);
182                         RNA_int_set(km->ptr, "mode", TFM_TIME_EXTEND);
183                         
184                         km= WM_keymap_add_item(keymap, "TFM_OT_transform", SKEY, KM_PRESS, 0, 0);
185                         RNA_int_set(km->ptr, "mode", TFM_TIME_SCALE);
186                         
187                         km= WM_keymap_add_item(keymap, "TFM_OT_transform", TKEY, KM_PRESS, 0, 0);
188                         RNA_int_set(km->ptr, "mode", TFM_TIME_SLIDE);
189                 default:
190                         break;
191         }
192 }