Fix a crash when running single-threaded and dragging the time slider.
[synfig.git] / synfig-studio / trunk / src / gtkmm / renderer_ducks.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file renderer_ducks.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "renderer_ducks.h"
33 #include "workarea.h"
34 #include "duckmatic.h"
35 #include <ETL/bezier>
36 #include <ETL/misc>
37 #include "widget_color.h"
38 #include <synfig/distance.h>
39 #include "app.h"
40
41 #endif
42
43 /* === U S I N G =========================================================== */
44
45 using namespace std;
46 using namespace etl;
47 using namespace synfig;
48 using namespace studio;
49
50 /* === M A C R O S ========================================================= */
51
52 /* === G L O B A L S ======================================================= */
53
54 /* === P R O C E D U R E S ================================================= */
55
56 /* === M E T H O D S ======================================================= */
57
58 Renderer_Ducks::~Renderer_Ducks()
59 {
60 }
61
62 /*
63 bool
64 Renderer_Ducks::get_enabled_vfunc()const
65 {
66         return get_work_area()->grid_status();
67 }
68 */
69
70 struct ScreenDuck
71 {
72         synfig::Point pos;
73         Gdk::Color color;
74         bool selected;
75         bool hover;
76         Real width;
77
78         ScreenDuck():width(0) { }
79 };
80
81 void
82 Renderer_Ducks::render_vfunc(
83         const Glib::RefPtr<Gdk::Drawable>& drawable,
84         const Gdk::Rectangle& /*expose_area*/
85 )
86 {
87         assert(get_work_area());
88         if(!get_work_area())
89                 return;
90
91         const synfig::Vector focus_point(get_work_area()->get_focus_point());
92
93
94         int drawable_w,drawable_h;
95         drawable->get_size(drawable_w,drawable_h);
96
97         Glib::RefPtr<Gdk::GC> gc(Gdk::GC::create(drawable));
98
99
100         const synfig::Vector::value_type window_startx(get_work_area()->get_window_tl()[0]);
101         const synfig::Vector::value_type window_starty(get_work_area()->get_window_tl()[1]);
102
103         const float pw(get_pw()),ph(get_ph());
104
105         const std::list<etl::handle<Duckmatic::Bezier> >& bezier_list(get_work_area()->bezier_list());
106         const bool solid_lines(get_work_area()->solid_lines);
107
108         const std::list<handle<Duckmatic::Stroke> >& stroke_list(get_work_area()->stroke_list());
109
110         Glib::RefPtr<Pango::Layout> layout(Pango::Layout::create(get_work_area()->get_pango_context()));
111
112         // Render the strokes
113         for(std::list<handle<Duckmatic::Stroke> >::const_iterator iter=stroke_list.begin();iter!=stroke_list.end();++iter)
114         {
115                 Point window_start(window_startx,window_starty);
116                 vector<Gdk::Point> points;
117                 std::list<synfig::Point>::iterator iter2;
118                 Point holder;
119
120                 for(iter2=(*iter)->stroke_data->begin();iter2!=(*iter)->stroke_data->end();++iter2)
121                 {
122                         holder=*iter2-window_start;
123                         holder[0]/=pw;holder[1]/=ph;
124                         points.push_back(Gdk::Point(round_to_int(holder[0]),round_to_int(holder[1])));
125                 }
126
127                 gc->set_rgb_fg_color(colorconv_synfig2gdk((*iter)->color));
128                 gc->set_function(Gdk::COPY);
129                 gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
130
131                 // Draw the stroke
132                 drawable->draw_lines(gc, Glib::ArrayHandle<Gdk::Point>(points));
133         }
134
135
136
137         // Render the beziers
138         for(std::list<handle<Duckmatic::Bezier> >::const_iterator iter=bezier_list.begin();iter!=bezier_list.end();++iter)
139         {
140                 Point window_start(window_startx,window_starty);
141                 Point p1((*iter)->p1->get_trans_point()-window_start);
142                 Point p2((*iter)->p2->get_trans_point()-window_start);
143                 Point c1((*iter)->c1->get_trans_point()-window_start);
144                 Point c2((*iter)->c2->get_trans_point()-window_start);
145                 p1[0]/=pw;p1[1]/=ph;
146                 p2[0]/=pw;p2[1]/=ph;
147                 c1[0]/=pw;c1[1]/=ph;
148                 c2[0]/=pw;c2[1]/=ph;
149                 bezier<Point> curve(p1,c1,c2,p2);
150                 vector<Gdk::Point> points;
151
152                 float f;
153                 Point pt;
154                 for(f=0;f<1.0;f+=1.0/17.0)
155                 {
156                         pt=curve(f);
157                         points.push_back(Gdk::Point(round_to_int(pt[0]),round_to_int(pt[1])));
158                 }
159                 points.push_back(Gdk::Point(round_to_int(p2[0]),round_to_int(p2[1])));
160
161                 // Draw the curve
162 /*              if(solid_lines)
163                 {
164                         gc->set_rgb_fg_color(Gdk::Color("#000000"));
165                         gc->set_function(Gdk::COPY);
166                         gc->set_line_attributes(3,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
167                         drawable->draw_lines(gc, Glib::ArrayHandle<Gdk::Point>(points));
168                         gc->set_rgb_fg_color(Gdk::Color("#afafaf"));
169                         gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
170                         drawable->draw_lines(gc, Glib::ArrayHandle<Gdk::Point>(points));
171                 }
172                 else
173 */
174                 {
175 //                      gc->set_rgb_fg_color(Gdk::Color("#ffffff"));
176 //                      gc->set_function(Gdk::INVERT);
177 //                      gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
178 //                      drawable->draw_lines(gc, Glib::ArrayHandle<Gdk::Point>(points));
179                         gc->set_rgb_fg_color(Gdk::Color("#000000"));
180                         gc->set_function(Gdk::COPY);
181                         gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
182                         drawable->draw_lines(gc, Glib::ArrayHandle<Gdk::Point>(points));
183                         gc->set_rgb_fg_color(Gdk::Color("#afafaf"));
184                         gc->set_line_attributes(1,Gdk::LINE_ON_OFF_DASH,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
185                         drawable->draw_lines(gc, Glib::ArrayHandle<Gdk::Point>(points));
186
187                 }
188         }
189
190
191         const DuckList duck_list(get_work_area()->get_duck_list());
192         //Gtk::StateType state = Gtk::STATE_ACTIVE;
193         Gtk::ShadowType shadow=Gtk::SHADOW_OUT;
194         std::list<ScreenDuck> screen_duck_list;
195         const float radius((abs(pw)+abs(ph))*4);
196
197         etl::handle<Duck> hover_duck(get_work_area()->find_duck(get_work_area()->get_cursor_pos(),radius, get_work_area()->get_type_mask()));
198
199         // Render the ducks
200         for(std::list<handle<Duck> >::const_iterator iter=duck_list.begin();iter!=duck_list.end();++iter)
201         {
202
203                 // If this type of duck has been masked, then skip it
204                 if((*iter)->get_type() && (!(get_work_area()->get_type_mask() & (*iter)->get_type())))
205                         continue;
206
207 //              Real x,y;
208         //      Gdk::Rectangle area;
209                 Point point((*iter)->get_trans_point());
210                 Point origin((*iter)->get_trans_origin());
211
212                 point[0]=(point[0]-window_startx)/pw;
213                 point[1]=(point[1]-window_starty)/ph;
214
215                 bool has_connect(false);
216                 if((*iter)->get_tangent() || (*iter)->get_type()&Duck::TYPE_ANGLE)
217                 {
218                         has_connect=true;
219                 }
220                 if((*iter)->get_connect_duck())
221                 {
222                         has_connect=true;
223                         origin=(*iter)->get_connect_duck()->get_trans_point();
224                 }
225
226                 origin[0]=(origin[0]-window_startx)/pw;
227                 origin[1]=(origin[1]-window_starty)/ph;
228
229
230                 bool selected(get_work_area()->duck_is_selected(*iter));
231                 bool hover(*iter==hover_duck);
232
233                 shadow = selected?Gtk::SHADOW_IN:Gtk::SHADOW_OUT;
234
235                 if(get_work_area()->get_selected_value_node())
236                 {
237                         synfigapp::ValueDesc value_desc((*iter)->get_value_desc());
238                         if(value_desc.is_valid() && value_desc.is_value_node() && get_work_area()->get_selected_value_node()==value_desc.get_value_node())
239                         {
240                                 gc->set_function(Gdk::COPY);
241                                 gc->set_rgb_fg_color(Gdk::Color("#FF0000"));
242                                 //gc->set_line_attributes(1,Gdk::LINE_ON_OFF_DASH,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
243                                 gc->set_line_attributes(2,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
244
245                                 drawable->draw_rectangle(gc,false,
246                                         round_to_int(point[0]-5),
247                                         round_to_int(point[1]-5),
248                                         10,
249                                         10
250                                 );
251                         }
252
253                 }
254
255                 if((*iter)->get_box_duck())
256                 {
257                         Point boxpoint((*iter)->get_box_duck()->get_trans_point());
258                         boxpoint[0]=(boxpoint[0]-window_startx)/pw;
259                         boxpoint[1]=(boxpoint[1]-window_starty)/ph;
260                         Point tl(min(point[0],boxpoint[0]),min(point[1],boxpoint[1]));
261
262                         gc->set_function(Gdk::COPY);
263                         gc->set_rgb_fg_color(Gdk::Color("#FFFFFF"));
264                         gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
265                         drawable->draw_rectangle(gc,false,
266                                 round_to_int(tl[0]),
267                                 round_to_int(tl[1]),
268                                 round_to_int(abs(boxpoint[0]-point[0])),
269                                 round_to_int(abs(boxpoint[1]-point[1]))
270                         );
271                         gc->set_function(Gdk::COPY);
272                         gc->set_rgb_fg_color(Gdk::Color("#000000"));
273                         gc->set_line_attributes(1,Gdk::LINE_ON_OFF_DASH,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
274                         drawable->draw_rectangle(gc,false,
275                                 round_to_int(tl[0]),
276                                 round_to_int(tl[1]),
277                                 round_to_int(abs(boxpoint[0]-point[0])),
278                                 round_to_int(abs(boxpoint[1]-point[1]))
279                         );
280                 }
281
282                 ScreenDuck screen_duck;
283                 screen_duck.pos=point;
284                 screen_duck.selected=selected;
285                 screen_duck.hover=hover;
286
287                 if(!(*iter)->get_editable())
288                         screen_duck.color=(Gdk::Color("#cfcfcf"));
289                 else if((*iter)->get_tangent())
290                         screen_duck.color=((*iter)->get_scalar()<0?Gdk::Color("#ffff00"):Gdk::Color("#ff0000"));
291                 else if((*iter)->get_type()&Duck::TYPE_VERTEX)
292                         screen_duck.color=Gdk::Color("#ff7f00");
293                 else if((*iter)->get_type()&Duck::TYPE_RADIUS)
294                         screen_duck.color=Gdk::Color("#00ffff");
295                 else if((*iter)->get_type()&Duck::TYPE_WIDTH)
296                         screen_duck.color=Gdk::Color("#ff00ff");
297                 else if((*iter)->get_type()&Duck::TYPE_ANGLE)
298                         screen_duck.color=(Gdk::Color("#0000ff"));
299                 else
300                         screen_duck.color=Gdk::Color("#00ff00");
301
302                 screen_duck_list.push_front(screen_duck);
303
304                 if(has_connect)
305                 {
306                         if(solid_lines)
307                         {
308                                 gc->set_line_attributes(3,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
309                                 gc->set_rgb_fg_color(Gdk::Color("#000000"));
310                                 gc->set_function(Gdk::COPY);
311                                 drawable->draw_line(gc, (int)origin[0],(int)origin[1],(int)(point[0]),(int)(point[1]));
312                                 gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
313                                 gc->set_rgb_fg_color(Gdk::Color("#9fefef"));
314                                 drawable->draw_line(gc, (int)origin[0],(int)origin[1],(int)(point[0]),(int)(point[1]));
315                         }
316                         else
317                         {
318 //                              gc->set_rgb_fg_color(Gdk::Color("#ffffff"));
319 //                              gc->set_function(Gdk::INVERT);
320 //                              drawable->draw_line(gc, (int)origin[0],(int)origin[1],(int)(point[0]),(int)(point[1]));
321                                 gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
322                                 gc->set_rgb_fg_color(Gdk::Color("#000000"));
323                                 gc->set_function(Gdk::COPY);
324                                 drawable->draw_line(gc, (int)origin[0],(int)origin[1],(int)(point[0]),(int)(point[1]));
325                                 gc->set_line_attributes(1,Gdk::LINE_ON_OFF_DASH,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
326                                 gc->set_rgb_fg_color(Gdk::Color("#9fefef"));
327                                 drawable->draw_line(gc, (int)origin[0],(int)origin[1],(int)(point[0]),(int)(point[1]));
328                         }
329                 }
330
331                 if((*iter)->is_radius())
332                 {
333                         const Real mag((point-origin).mag());
334                         const int d(round_to_int(mag*2));
335                         const int x(round_to_int(origin[0]-mag));
336                         const int y(round_to_int(origin[1]-mag));
337
338                         if(solid_lines)
339                         {
340                                 gc->set_rgb_fg_color(Gdk::Color("#000000"));
341                                 gc->set_function(Gdk::COPY);
342                                 gc->set_line_attributes(3,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
343                                 drawable->draw_arc(
344                                         gc,
345                                         false,
346                                         x,
347                                         y,
348                                         d,
349                                         d,
350                                         0,
351                                         360*64
352                                 );
353                                 gc->set_rgb_fg_color(Gdk::Color("#afafaf"));
354                         }
355                         else
356                         {
357                                 gc->set_rgb_fg_color(Gdk::Color("#ffffff"));
358                                 gc->set_function(Gdk::INVERT);
359                         }
360                         gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
361
362                         drawable->draw_arc(
363                                 gc,
364                                 false,
365                                 x,
366                                 y,
367                                 d,
368                                 d,
369                                 0,
370                                 360*64
371                         );
372
373                         if(hover)
374                         {
375                                 Distance real_mag(((*iter)->get_trans_point()-(*iter)->get_trans_origin()).mag(),Distance::SYSTEM_UNITS);
376                                 real_mag.convert(App::distance_system,get_work_area()->get_rend_desc());
377                                 layout->set_text(real_mag.get_string());
378
379                                 gc->set_rgb_fg_color(Gdk::Color("#000000"));
380                                 drawable->draw_layout(
381                                         gc,
382                                         round_to_int(point[0])+1+6,
383                                         round_to_int(point[1])+1-8,
384                                         layout
385                                 );
386                                 gc->set_rgb_fg_color(Gdk::Color("#FF00FF"));
387                                 drawable->draw_layout(
388                                         gc,
389                                         round_to_int(point[0])+6,
390                                         round_to_int(point[1])-8,
391                                         layout
392                                 );
393                         }
394
395                 }
396
397         }
398
399
400         for(;screen_duck_list.size();screen_duck_list.pop_front())
401         {
402                 int radius=4;
403                 int outline=1;
404                 Gdk::Color color(screen_duck_list.front().color);
405
406                 if(!screen_duck_list.front().selected)
407                 {
408                         color.set_red(color.get_red()*2/3);
409                         color.set_green(color.get_green()*2/3);
410                         color.set_blue(color.get_blue()*2/3);
411                 }
412
413                 if(screen_duck_list.front().hover && !screen_duck_list.back().hover && screen_duck_list.size()>1)
414                 {
415                         screen_duck_list.push_back(screen_duck_list.front());
416                         continue;
417                 }
418
419                 if(screen_duck_list.front().hover)
420                 {
421                         radius+=2;
422                         outline++;
423                 }
424
425                 gc->set_function(Gdk::COPY);
426                 gc->set_line_attributes(1,Gdk::LINE_SOLID,Gdk::CAP_BUTT,Gdk::JOIN_MITER);
427                 gc->set_rgb_fg_color(Gdk::Color("#000000"));
428                 drawable->draw_arc(
429                         gc,
430                         true,
431                         round_to_int(screen_duck_list.front().pos[0]-radius),
432                         round_to_int(screen_duck_list.front().pos[1]-radius),
433                         radius*2,
434                         radius*2,
435                         0,
436                         360*64
437                 );
438
439
440                 gc->set_rgb_fg_color(color);
441
442                 drawable->draw_arc(
443                         gc,
444                         true,
445                         round_to_int(screen_duck_list.front().pos[0]-radius+outline),
446                         round_to_int(screen_duck_list.front().pos[1]-radius+outline),
447                         radius*2-outline*2,
448                         radius*2-outline*2,
449                         0,
450                         360*64
451                 );
452         }
453 }