r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / cinelerra / bezierauto.C
blobd98dfdec1f2990afb7d0ca972ff7225fc43f7615
1 #include "bezierauto.h"
2 #include "bezierautos.h"
3 #include "clip.h"
4 #include "filexml.h"
6 BezierAuto::BezierAuto(EDL *edl, BezierAutos *autos)
7  : Auto(edl, autos)
9         WIDTH = 10;
10         HEIGHT = 10;
11         center_x = 0;
12         center_y = 0;
13         center_z = 1;
14         control_in_x = 0;
15         control_in_y = 0;
16         control_in_z = 0;
17         control_out_x = 0;
18         control_out_y = 0;
19         control_out_z = 0;
22 BezierAuto::~BezierAuto()
26 int BezierAuto::operator==(Auto &that)
28         return identical((BezierAuto*)&that);
31 int BezierAuto::operator==(BezierAuto &that)
33         return identical((BezierAuto*)&that);
36 int BezierAuto::identical(BezierAuto *src)
38         return EQUIV(center_x, src->center_x) &&
39                 EQUIV(center_y, src->center_y) &&
40                 EQUIV(center_z, src->center_z) &&
41                 EQUIV(control_in_x, src->control_in_x) &&
42                 EQUIV(control_in_y, src->control_in_y) &&
43                 EQUIV(control_out_x, src->control_out_x) &&
44                 EQUIV(control_out_y, src->control_out_y) &&
45                 EQUIV(control_in_z, src->control_in_z) &&
46                 EQUIV(control_out_z, src->control_out_z) ;
50 void BezierAuto::copy_from(Auto *that)
52         copy_from((BezierAuto*)that);
55 void BezierAuto::copy_from(BezierAuto *that)
57         Auto::copy_from(that);
59         center_x = that->center_x;
60         center_y = that->center_y;
61         center_z = that->center_z;
62         control_in_x = that->control_in_x;
63         control_in_y = that->control_in_y;
64         control_in_z = that->control_in_z;
65         control_out_x = that->control_out_x;
66         control_out_y = that->control_out_y;
67         control_out_z = that->control_out_z;
70 void BezierAuto::copy(int64_t start, int64_t end, FileXML *file, int default_auto)
72         file->tag.set_title("AUTO");
73         if(default_auto)
74                 file->tag.set_property("POSITION", 0);
75         else
76                 file->tag.set_property("POSITION", position - start);
77         file->tag.set_property("CENTER_X", center_x);
78         file->tag.set_property("CENTER_Y", center_y);
79         file->tag.set_property("CENTER_Z", center_z);
80         file->tag.set_property("CONTROL_IN_X", control_in_x);
81         file->tag.set_property("CONTROL_IN_Y", control_in_y);
82         file->tag.set_property("CONTROL_IN_Z", control_in_z);
83         file->tag.set_property("CONTROL_OUT_X", control_out_x);
84         file->tag.set_property("CONTROL_OUT_Y", control_out_y);
85         file->tag.set_property("CONTROL_OUT_Z", control_out_z);
86         file->append_tag();
87         file->append_newline();
90 void BezierAuto::load(FileXML *file)
92         center_x = file->tag.get_property("CENTER_X", (float)0);
93         center_y = file->tag.get_property("CENTER_Y", (float)0);
94         center_z = file->tag.get_property("CENTER_Z", (float)1);
95         control_in_x = file->tag.get_property("CONTROL_IN_X", (float)control_in_x);
96         control_in_y = file->tag.get_property("CONTROL_IN_Y", (float)control_in_y);
97         control_in_z = file->tag.get_property("CONTROL_IN_Z", (float)control_in_z);
98         control_out_x = file->tag.get_property("CONTROL_OUT_X", (float)control_out_x);
99         control_out_y = file->tag.get_property("CONTROL_OUT_Y", (float)control_out_y);
100         control_out_z = file->tag.get_property("CONTROL_OUT_Z", (float)control_out_z);
103 int BezierAuto::draw(BC_SubWindow *canvas, 
104                                 int x, 
105                                 int center_pixel, 
106                                 float scale,
107                                 int vertical,
108                                 int show_value)
110         show_value = 0;    // skip drawing value for now
111         static int control_x[5], control_y[5];
112         static int i;
114         get_control_points(x, center_pixel, scale, control_x, control_y, vertical);
116 //      canvas->draw_disc(control_x[0] - WIDTH/2, control_y[0] - HEIGHT/2, WIDTH, HEIGHT);
117         for(i = 1; i < 5; i++)
118         {
119 // Draw zooms hollow and pan opaque.
120                 if(i < 3) 
121                         canvas->draw_box(control_x[i] - WIDTH/2, control_y[i] - HEIGHT/2, WIDTH, HEIGHT);
122                 else
123                         canvas->draw_rectangle(control_x[i] - WIDTH/2, control_y[i] - HEIGHT/2, WIDTH, HEIGHT);
125                 canvas->draw_line(control_x[i], control_y[i], control_x[0], control_y[0]);
126         }
128         if(show_value && ((BezierAutos*)autos)->selection_type > 1)
129         {
130                 static char string[16];
131                 static int text_x, text_y; // text position relative to canvas
132                 
133                 value_to_str(string);
135                 text_x = control_x[((BezierAutos*)autos)->selection_type - 2] + 20;
136                 text_y = control_y[((BezierAutos*)autos)->selection_type - 2] + 20;
137                 //canvas->set_font(SMALLFONT);
139                 canvas->draw_text(text_x, text_y, string);
140                 //canvas->set_font(MEDIUMFONT);
141         }
142         return 0;
145 int BezierAuto::select(BC_SubWindow *canvas, 
146                                 int x, 
147                                 int center_pixel, 
148                                 float scale,
149                                 int cursor_x, 
150                                 int cursor_y, 
151                                 int shift_down,
152                                 int ctrl_down,
153                                 int mouse_button,
154                                 int vertical)
156         int control_x[5], control_y[5];
157         get_control_points(x, center_pixel, scale, control_x, control_y, vertical);
159         if(vertical)
160         {
161                 cursor_x ^= cursor_y;
162                 cursor_y ^= cursor_x;
163                 cursor_x ^= cursor_y;
164         }
166         if(shift_down)
167         {
168 // test control_in_xy
169                 if(mouse_button == 1 
170                         && test_control_point(control_x[0], control_y[0], control_x[1], control_y[1], cursor_x, cursor_y))
171                 { return 4; }
173 // test control_out_xy
174                 if(mouse_button == 1 
175                         && test_control_point(control_x[0], control_y[0], control_x[2], control_y[2], cursor_x, cursor_y))
176                 { return 5; }
178 // test control_in_zoom
179                 if(mouse_button == 3
180                         && test_control_point(control_x[0], control_y[0], control_x[3], control_y[3], cursor_x, cursor_y))
181                 { return 6; }
183 // test control_out_zoom
184                 if(mouse_button == 3 
185                         && test_control_point(control_x[0], control_y[0], control_x[4], control_y[4], cursor_x, cursor_y))
186                 { return 7; }
187         }
188         else
189         if(cursor_x > control_x[0] - WIDTH/2 &&
190         cursor_x < control_x[0] + WIDTH/2 &&
191         cursor_y > control_y[0] - HEIGHT/2 &&
192         cursor_y < control_y[0] + HEIGHT/2)
193         {
194                 if(mouse_button == 1)
195                 {
196                         if(ctrl_down)
197                                 return 2;      // move xy
198                         else
199                                 return 1;      // move frame
200                 }
201                 else
202                 if(mouse_button == 3)
203                 {
204                         if(ctrl_down)
205                                 return 3;      // move zoom
206                 }
207         }
209         return 0;
212 int BezierAuto::test_control_point(int center_x, int center_y, int x, int y, int cursor_x, int cursor_y)
214         if(cursor_x > x - WIDTH/2 &&
215                 cursor_x < x + WIDTH/2 &&
216                 cursor_y > y - HEIGHT/2 &&
217                 cursor_y < y + HEIGHT/2) return 1;
219 //      if(((x > center_x) ? cursor_x > center_x - WIDTH/2 : cursor_x > x - WIDTH/2) &&
220 //              ((x > center_x) ? cursor_x < x + WIDTH/2 : x < center_x + WIDTH/2) &&
221 //              ((y > center_y) ? cursor_y > center_y - HEIGHT/2 : cursor_y > y - HEIGHT/2) &&
222 //              ((y > center_y) ? cursor_y < y - HEIGHT/2 : cursor_y < center_y + HEIGHT/2))
223 //      {
224 //              float A = cursor_x - center_x;
225 //              float B = cursor_y - center_y;
226 //              float C = x - center_x;
227 //              float D = y - center_y;
228 //              float dist = (A * D - B * C) / sqrt(C * C + D * D);
229 // 
230 // printf("%f\n", dist);
231 //              if(dist < WIDTH / 2) return 1;
232 //      }
233         
234         return 0;
237 float BezierAuto::get_distance(int x1, int y1, int x2, int y2)
239         return sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
242 int BezierAuto::get_control_points(int x, int center_pixel, float scale, int *control_x, int *control_y, int vertical)
244 // get control point locations relative to canvas
245         int y = center_pixel;
246         float zoom_scale;
247         if(vertical)
248         {
249                 y = x;
250                 x = center_pixel;
251         }
253         zoom_scale = ((BezierAutos*)autos)->frame_h * scale;
255 // frame point
256         control_x[0] = x;
257         control_y[0] = y;
258 // control_in_xy
259         control_x[1] = (int)(control_in_x * scale + x);
260         control_y[1] = (int)(control_in_y * scale + y);
261 // control_out_xy
262         control_x[2] = (int)(control_out_x * scale + x);
263         control_y[2] = (int)(control_out_y * scale + y);
264 // zoom_in_control
265         control_x[3] = (int)x;
266         control_y[3] = (int)(control_in_z * zoom_scale + y);
267 //printf("BezierAuto::get_control_points %d %f %f %d\n", control_y[3], control_in_z, scale, y);
268 // zoom_out_control
269         control_x[4] = (int)x;
270         control_y[4] = (int)(control_out_z * zoom_scale + y);
273 int BezierAuto::value_to_str(char *string)
275 // not used
276         if(((BezierAutos*)autos)->frame_w)
277         switch(((BezierAutos*)autos)->selection_type)
278         {
279                 case 2:
280                         sprintf(string, "%.0f, %.0f", center_x, center_y);
281                         break;
283                 case 3:
284                         sprintf(string, "%.0f, %.0f", control_in_x, control_in_y);
285                         break;
287                 case 4:
288                         sprintf(string, "%.0f, %.0f", control_out_x, control_out_y);
289                         break;
290         }
291         else
292         switch(((BezierAutos*)autos)->selection_type)
293         {
294                 case 2:
295                         sprintf(string, "%.0f", center_y);
296                         break;
298                 case 3:
299                         sprintf(string, "%.0f", control_in_y);
300                         break;
302                 case 4:
303                         sprintf(string, "%.0f", control_out_y);
304                         break;
305         }
306         return 0;