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