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