4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
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.
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
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "automation.inc"
25 #include "localsession.h"
27 #include "maskautos.h"
29 #include "transportque.inc"
31 MaskAutos::MaskAutos(EDL *edl,
35 type = AUTOMATION_TYPE_MASK;
38 MaskAutos::~MaskAutos()
45 void MaskAutos::update_parameter(MaskAuto *src)
47 double selection_start = edl->local_session->get_selectionstart(0);
48 double selection_end = edl->local_session->get_selectionend(0);
50 // Selection is always aligned to frame for masks
52 // Create new keyframe if auto keyframes or replace entire keyframe.
53 if(selection_start == selection_end)
55 // Search for keyframe to write to
56 MaskAuto *dst = (MaskAuto*)get_auto_for_editing();
61 // Replace changed parameter in all selected keyframes.
63 // Search all keyframes in selection but don't create a new one.
64 int64_t start = track->to_units(selection_start, 0);
65 int64_t end = track->to_units(selection_end, 0);
66 Auto *current_auto = 0;
67 MaskAuto *current = 0;
68 current = (MaskAuto*)get_prev_auto(start,
73 // The first one determines the changed parameters since it is the one displayed
75 MaskAuto *first = current;
77 // Update the first one last, so it is available for comparisons to the changed one.
78 for(current = (MaskAuto*)NEXT;
79 current && current->position < end;
80 current = (MaskAuto*)NEXT)
82 current->update_parameter(first,
85 first->copy_data(src);
91 void MaskAutos::get_points(ArrayList<MaskPoint*> *points,
96 MaskAuto *begin = 0, *end = 0;
97 position = (direction == PLAY_FORWARD) ? position : (position - 1);
99 // Get auto before and after position
100 for(MaskAuto* current = (MaskAuto*)last;
102 current = (MaskAuto*)PREVIOUS)
104 if(current->position <= position)
107 end = NEXT ? (MaskAuto*)NEXT : current;
112 // Nothing before position found
115 begin = end = (MaskAuto*)first;
118 // Nothing after position found
121 begin = end = (MaskAuto*)default_auto;
125 SubMask *mask1 = begin->get_submask(submask);
126 SubMask *mask2 = end->get_submask(submask);
128 points->remove_all_objects();
129 int total_points = MAX(mask1->points.size(), mask2->points.size());
130 for(int i = 0; i < total_points; i++)
132 MaskPoint *point = new MaskPoint;
138 if(i < mask1->points.size())
140 point1.copy_from(*mask1->points.get(i));
144 if(i < mask2->points.size())
146 point2.copy_from(*mask2->points.get(i));
150 if(need_point1) point1.copy_from(point2);
151 if(need_point2) point2.copy_from(point1);
159 points->append(point);
164 double MaskAutos::get_feather(int64_t position, int i, int direction)
166 Auto *begin = 0, *end = 0;
167 position = (direction == PLAY_FORWARD) ? position : (position - 1);
168 MaskAuto*prev = (MaskAuto*)get_prev_auto(position, PLAY_FORWARD, begin, 1);
169 MaskAuto*next = (MaskAuto*)get_next_auto(position, PLAY_FORWARD, end, 1);
170 SubMask *prev_mask = prev->get_submask(i), *next_mask = next->get_submask(i);
172 double weight = end->position == begin->position ? 0.0 :
173 (double)(position - begin->position) / (end->position - begin->position);
175 double result = prev_mask->feather * (1-weight) + next_mask->feather*weight;
179 double MaskAutos::get_fader(int64_t position, int i, int direction)
181 Auto *begin = 0, *end = 0;
182 position = (direction == PLAY_FORWARD) ? position : (position - 1);
183 MaskAuto*prev = (MaskAuto*)get_prev_auto(position, PLAY_FORWARD, begin, 1);
184 MaskAuto*next = (MaskAuto*)get_next_auto(position, PLAY_FORWARD, end, 1);
185 SubMask *prev_mask = prev->get_submask(i), *next_mask = next->get_submask(i);
187 double weight = end->position == begin->position ? 0.0 :
188 (double)(position - begin->position) / (end->position - begin->position);
190 double result = prev_mask->fader * (1-weight) + next_mask->fader*weight;
191 // printf("MaskAutos::get_fader %d %d %d %f %d %f %d\n", __LINE__, i,
192 // ((MaskAuto*)begin)->fader, 1.0 - weight, ((MaskAuto*)end)->fader, weight, result);
197 void MaskAutos::avg_points(MaskPoint *output, MaskPoint *input1, MaskPoint *input2,
198 int64_t output_position, int64_t position1, int64_t position2)
200 if(position2 == position1) {
204 float fraction2 = (float)(output_position - position1) / (position2 - position1);
205 float fraction1 = 1 - fraction2;
206 output->x = input1->x * fraction1 + input2->x * fraction2;
207 output->y = input1->y * fraction1 + input2->y * fraction2;
208 output->control_x1 = input1->control_x1 * fraction1 + input2->control_x1 * fraction2;
209 output->control_y1 = input1->control_y1 * fraction1 + input2->control_y1 * fraction2;
210 output->control_x2 = input1->control_x2 * fraction1 + input2->control_x2 * fraction2;
211 output->control_y2 = input1->control_y2 * fraction1 + input2->control_y2 * fraction2;
217 Auto* MaskAutos::new_auto()
219 return new MaskAuto(edl, this);
222 void MaskAutos::dump(FILE *fp)
224 fprintf(fp, " MaskAutos::dump %p\n", this);
225 fprintf(fp, " Default: position %jd submasks %d\n",
226 default_auto->position,
227 ((MaskAuto*)default_auto)->masks.total);
228 ((MaskAuto*)default_auto)->dump(fp);
229 for( Auto* current = first; current; current=NEXT ) {
230 fprintf(fp, " position %jd masks %d\n",
232 ((MaskAuto*)current)->masks.total);
233 ((MaskAuto*)current)->dump(fp);
237 int MaskAutos::mask_exists(int64_t position, int direction)
240 position = (direction == PLAY_FORWARD) ? position : (position - 1);
242 MaskAuto* keyframe = (MaskAuto*)get_prev_auto(position, direction, current);
244 for( int i = 0; i < keyframe->masks.total; i++ ) {
245 SubMask *mask = keyframe->get_submask(i);
246 if(mask->points.total > 1)
252 int MaskAutos::total_submasks(int64_t position, int direction)
254 position = (direction == PLAY_FORWARD) ? position : (position - 1);
255 for( MaskAuto* current=(MaskAuto*)last; current; current=(MaskAuto*)PREVIOUS ) {
256 if( current->position <= position ) {
257 return current->masks.total;
261 return ((MaskAuto*)default_auto)->masks.total;
265 void MaskAutos::translate_masks(float translate_x, float translate_y)
267 ((MaskAuto *)default_auto)->translate_submasks(translate_x, translate_y);
268 for( MaskAuto* current=(MaskAuto*)first; current; current=(MaskAuto*)NEXT ) {
269 current->translate_submasks(translate_x, translate_y);
273 void MaskAutos::set_proxy(int orig_scale, int new_scale)
275 ((MaskAuto *)default_auto)->scale_submasks(orig_scale, new_scale);
276 for( MaskAuto* current=(MaskAuto*)first; current; current=(MaskAuto*)NEXT ) {
277 current->scale_submasks(orig_scale, new_scale);