6576d2aeb01441372111ddab08f918058d7b4c63
[blender.git] / source / blender / editors / space_clip / clip_dopesheet_ops.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) 2012 Blender Foundation.
19  * All rights reserved.
20  *
21  *
22  * Contributor(s): Blender Foundation,
23  *                 Sergey Sharybin
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/space_clip/clip_dopesheet_ops.c
29  *  \ingroup spclip
30  */
31
32 #include "DNA_object_types.h"   /* SELECT */
33 #include "DNA_scene_types.h"
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_utildefines.h"
38 #include "BLI_math.h"
39 #include "BLI_listbase.h"
40 #include "BLI_rect.h"
41
42 #include "BKE_context.h"
43 #include "BKE_movieclip.h"
44 #include "BKE_tracking.h"
45 #include "BKE_depsgraph.h"
46
47 #include "WM_api.h"
48 #include "WM_types.h"
49
50 #include "ED_screen.h"
51 #include "ED_clip.h"
52
53 #include "UI_interface.h"
54
55 #include "RNA_access.h"
56 #include "RNA_define.h"
57
58 #include "UI_view2d.h"
59
60 #include "clip_intern.h"        // own include
61
62 #if 0
63 static int ED_space_clip_dopesheet_poll(bContext *C)
64 {
65         SpaceClip *sc = CTX_wm_space_clip(C);
66
67         if (sc && sc->clip) {
68                 if (sc->view == SC_VIEW_DOPESHEET) {
69                         ARegion *ar = CTX_wm_region(C);
70
71                         return ar->regiontype == RGN_TYPE_PREVIEW;
72                 }
73         }
74
75         return FALSE;
76 }
77 #endif
78
79 /********************** select channel operator *********************/
80
81 static int dopesheet_select_channel_poll(bContext *C)
82 {
83         SpaceClip *sc = CTX_wm_space_clip(C);
84
85         if (sc && sc->clip)
86                 return sc->view == SC_VIEW_DOPESHEET;
87
88         return FALSE;
89 }
90
91 static int dopesheet_select_channel_exec(bContext *C, wmOperator *op)
92 {
93         SpaceClip *sc = CTX_wm_space_clip(C);
94         MovieClip *clip = ED_space_clip(sc);
95         MovieTracking *tracking = &clip->tracking;
96         MovieTrackingObject *object = BKE_tracking_active_object(tracking);
97         MovieTrackingDopesheet *dopesheet = &tracking->dopesheet;
98         MovieTrackingDopesheetChannel *channel;
99         ListBase *tracksbase = BKE_tracking_object_tracks(tracking, object);
100         float location[2];
101         int extend = RNA_boolean_get(op->ptr, "extend");
102         int current_channel_index = 0, channel_index;
103
104         RNA_float_get_array(op->ptr, "location", location);
105         channel_index = -(location[1] - (CHANNEL_FIRST + CHANNEL_HEIGHT_HALF)) / CHANNEL_STEP;
106
107         for (channel = dopesheet->channels.first; channel; channel = channel->next) {
108                 MovieTrackingTrack *track = channel->track;
109
110                 if (current_channel_index == channel_index) {
111                         if (extend)
112                                 track->flag ^= TRACK_DOPE_SEL;
113                         else
114                                 track->flag |= TRACK_DOPE_SEL;
115
116                         if (track->flag & TRACK_DOPE_SEL) {
117                                 tracking->act_track = track;
118                                 BKE_tracking_select_track(tracksbase, track, TRACK_AREA_ALL, TRUE);
119                         }
120                 }
121                 else if (!extend)
122                         track->flag &= ~TRACK_DOPE_SEL;
123
124                 current_channel_index++;
125         }
126
127         WM_event_add_notifier(C, NC_GEOM | ND_SELECT, NULL);
128
129         return OPERATOR_FINISHED;
130 }
131
132 static int dopesheet_select_channel_invoke(bContext *C, wmOperator *op, wmEvent *event)
133 {
134         ARegion *ar = CTX_wm_region(C);
135         float location[2];
136
137         UI_view2d_region_to_view(&ar->v2d, event->mval[0], event->mval[1], &location[0], &location[1]);
138         RNA_float_set_array(op->ptr, "location", location);
139
140         return dopesheet_select_channel_exec(C, op);
141 }
142
143 void CLIP_OT_dopesheet_select_channel(wmOperatorType *ot)
144 {
145         /* identifiers */
146         ot->name = "Select Channel";
147         ot->description = "Select movie tracking channel";
148         ot->idname = "CLIP_OT_dopesheet_select_channel";
149
150         /* api callbacks */
151         ot->invoke = dopesheet_select_channel_invoke;
152         ot->exec = dopesheet_select_channel_exec;
153         ot->poll = dopesheet_select_channel_poll;
154
155         /* flags */
156         ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
157
158         /* properties */
159         RNA_def_float_vector(ot->srna, "location", 2, NULL, -FLT_MAX, FLT_MAX,
160                 "Location", "Mouse location to select channel", -100.0f, 100.0f);
161         RNA_def_boolean(ot->srna, "extend", 0,
162                 "Extend", "Extend selection rather than clearing the existing selection");
163 }