Allow PasteCanvas layers with unset canvas parameters to be exported.
[synfig.git] / synfig-studio / trunk / src / gtkmm / state_circle.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file state_circle.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) 2008 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include <gtkmm/dialog.h>
34 #include <gtkmm/entry.h>
35
36 #include <synfig/valuenode_dynamiclist.h>
37 #include <synfigapp/action_system.h>
38 #include <synfig/valuenode_bline.h>
39
40 #include "state_circle.h"
41 #include "canvasview.h"
42 #include "workarea.h"
43 #include "app.h"
44
45 #include <synfigapp/action.h>
46 #include "event_mouse.h"
47 #include "event_layerclick.h"
48 #include "toolbox.h"
49 #include "dialog_tooloptions.h"
50 #include <gtkmm/optionmenu.h>
51 #include "duck.h"
52 #include "widget_enum.h"
53 #include <synfigapp/main.h>
54
55 #include "general.h"
56
57 #endif
58
59 /* === U S I N G =========================================================== */
60
61 using namespace std;
62 using namespace etl;
63 using namespace synfig;
64 using namespace studio;
65
66 /* === M A C R O S ========================================================= */
67 enum CircleFalloff
68 {
69         CIRCLE_SQUARED  =0,
70         CIRCLE_INTERPOLATION_LINEAR     =1,
71         CIRCLE_COSINE   =2,
72         CIRCLE_SIGMOND  =3,
73         CIRCLE_SQRT             =4,
74         CIRCLE_NUM_FALLOFF
75 };
76
77 /* === G L O B A L S ======================================================= */
78
79 StateCircle studio::state_circle;
80
81 /* === C L A S S E S & S T R U C T S ======================================= */
82
83 class studio::StateCircle_Context : public sigc::trackable
84 {
85         etl::handle<CanvasView> canvas_view_;
86         CanvasView::IsWorking is_working;
87
88         Duckmatic::Push duckmatic_push;
89
90         Point point_holder;
91
92         etl::handle<Duck> point2_duck;
93
94         void refresh_ducks();
95
96         bool prev_workarea_layer_status_;
97
98         //Toolbox settings
99         synfigapp::Settings& settings;
100
101         //Toolbox display
102         Gtk::Table options_table;
103
104         Gtk::Entry              entry_id; //what to name the layer
105
106         Widget_Enum             enum_falloff;
107 #ifdef BLEND_METHOD_IN_TOOL_OPTIONS
108         Widget_Enum             enum_blend;
109 #endif  // BLEND_METHOD_IN_TOOL_OPTIONS
110
111         Gtk::Adjustment adj_feather;
112         Gtk::Adjustment adj_number_of_bline_points;
113         Gtk::Adjustment adj_bline_point_angle_offset;
114         Gtk::SpinButton spin_feather;
115         Gtk::SpinButton spin_number_of_bline_points;
116         Gtk::SpinButton spin_bline_point_angle_offset;
117
118         Gtk::CheckButton checkbutton_invert;
119         Gtk::CheckButton checkbutton_layer_circle;
120         Gtk::CheckButton checkbutton_layer_region;
121         Gtk::CheckButton checkbutton_layer_outline;
122         Gtk::CheckButton checkbutton_layer_curve_gradient;
123         Gtk::CheckButton checkbutton_layer_plant;
124         Gtk::CheckButton checkbutton_layer_link_origins;
125
126 public:
127
128         // this only counts the layers which use blines - they're the only
129         // ones we link the origins for
130         int layers_to_create()const
131         {
132                 return
133                         get_layer_circle_flag() +
134                         get_layer_region_flag() +
135                         get_layer_outline_flag() +
136                         get_layer_curve_gradient_flag() +
137                         get_layer_plant_flag();
138         }
139
140         synfig::String get_id()const { return entry_id.get_text(); }
141         void set_id(const synfig::String& x) { return entry_id.set_text(x); }
142
143         int get_falloff()const { return enum_falloff.get_value(); }
144         void set_falloff(int x) { return enum_falloff.set_value(x); }
145
146 #ifdef BLEND_METHOD_IN_TOOL_OPTIONS
147         int get_blend()const { return enum_blend.get_value(); }
148         void set_blend(int x) { return enum_blend.set_value(x); }
149 #endif  // BLEND_METHOD_IN_TOOL_OPTIONS
150
151         Real get_feather()const { return adj_feather.get_value(); }
152         void set_feather(Real f) { adj_feather.set_value(f); }
153
154         Real get_number_of_bline_points()const { return adj_number_of_bline_points.get_value(); }
155         void set_number_of_bline_points(Real f) { adj_number_of_bline_points.set_value(f); }
156
157         Real get_bline_point_angle_offset()const { return adj_bline_point_angle_offset.get_value(); }
158         void set_bline_point_angle_offset(Real f) { adj_bline_point_angle_offset.set_value(f); }
159
160         bool get_invert()const { return checkbutton_invert.get_active(); }
161         void set_invert(bool i) { checkbutton_invert.set_active(i); }
162
163         bool get_layer_circle_flag()const { return checkbutton_layer_circle.get_active(); }
164         void set_layer_circle_flag(bool x) { return checkbutton_layer_circle.set_active(x); }
165
166         bool get_layer_region_flag()const { return checkbutton_layer_region.get_active(); }
167         void set_layer_region_flag(bool x) { return checkbutton_layer_region.set_active(x); }
168
169         bool get_layer_outline_flag()const { return checkbutton_layer_outline.get_active(); }
170         void set_layer_outline_flag(bool x) { return checkbutton_layer_outline.set_active(x); }
171
172         bool get_layer_curve_gradient_flag()const { return checkbutton_layer_curve_gradient.get_active(); }
173         void set_layer_curve_gradient_flag(bool x) { return checkbutton_layer_curve_gradient.set_active(x); }
174
175         bool get_layer_plant_flag()const { return checkbutton_layer_plant.get_active(); }
176         void set_layer_plant_flag(bool x) { return checkbutton_layer_plant.set_active(x); }
177
178         bool get_layer_link_origins_flag()const { return checkbutton_layer_link_origins.get_active(); }
179         void set_layer_link_origins_flag(bool x) { return checkbutton_layer_link_origins.set_active(x); }
180
181         void refresh_tool_options(); //to refresh the toolbox
182
183         //events
184         Smach::event_result event_stop_handler(const Smach::event& x);
185         Smach::event_result event_refresh_handler(const Smach::event& x);
186         Smach::event_result event_mouse_click_handler(const Smach::event& x);
187         Smach::event_result event_refresh_tool_options(const Smach::event& x);
188
189         //constructor destructor
190         StateCircle_Context(CanvasView* canvas_view);
191         ~StateCircle_Context();
192
193         //Canvas interaction
194         const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
195         etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
196         synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
197         WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
198
199         //Modifying settings etc.
200         void load_settings();
201         void save_settings();
202         void reset();
203         void increment_id();
204         bool egress_on_selection_change;
205         Smach::event_result event_layer_selection_changed_handler(const Smach::event& /*x*/)
206         {
207                 if(egress_on_selection_change)
208                         throw Smach::egress_exception();
209                 return Smach::RESULT_OK;
210         }
211
212         void make_circle(const Point& p1, const Point& p2);
213
214 };      // END of class StateCircle_Context
215
216 /* === M E T H O D S ======================================================= */
217
218 StateCircle::StateCircle():
219         Smach::state<StateCircle_Context>("circle")
220 {
221         insert(event_def(EVENT_LAYER_SELECTION_CHANGED,&StateCircle_Context::event_layer_selection_changed_handler));
222         insert(event_def(EVENT_STOP,&StateCircle_Context::event_stop_handler));
223         insert(event_def(EVENT_REFRESH,&StateCircle_Context::event_refresh_handler));
224         insert(event_def(EVENT_REFRESH_DUCKS,&StateCircle_Context::event_refresh_handler));
225         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateCircle_Context::event_mouse_click_handler));
226         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DRAG,&StateCircle_Context::event_mouse_click_handler));
227         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_UP,&StateCircle_Context::event_mouse_click_handler));
228         insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateCircle_Context::event_refresh_tool_options));
229 }
230
231 StateCircle::~StateCircle()
232 {
233 }
234
235 void
236 StateCircle_Context::load_settings()
237 {
238         String value;
239
240         //parse the arguments yargh!
241         if(settings.get_value("circle.id",value))
242                 set_id(value);
243         else
244                 set_id("Circle");
245
246         if(settings.get_value("circle.fallofftype",value) && value != "")
247                 set_falloff(atoi(value.c_str()));
248         else
249                 set_falloff(2);
250
251 #ifdef BLEND_METHOD_IN_TOOL_OPTIONS
252         if(settings.get_value("circle.blend",value) && value != "")
253                 set_blend(atoi(value.c_str()));
254         else
255                 set_blend(0);//(int)Color::BLEND_COMPOSITE); //0 should be blend composites value
256 #endif  // BLEND_METHOD_IN_TOOL_OPTIONS
257
258         if(settings.get_value("circle.feather",value))
259                 set_feather(atof(value.c_str()));
260         else
261                 set_feather(0);
262
263         if(settings.get_value("circle.number_of_bline_points",value))
264                 set_number_of_bline_points(atof(value.c_str()));
265         else
266                 set_number_of_bline_points(4);
267
268         if(settings.get_value("circle.bline_point_angle_offset",value))
269                 set_bline_point_angle_offset(atof(value.c_str()));
270         else
271                 set_bline_point_angle_offset(0);
272
273         if(settings.get_value("circle.invert",value) && value != "0")
274                 set_invert(true);
275         else
276                 set_invert(false);
277
278         if(settings.get_value("circle.layer_circle",value) && value=="0")
279                 set_layer_circle_flag(false);
280         else
281                 set_layer_circle_flag(true);
282
283         if(settings.get_value("circle.layer_region",value) && value=="1")
284                 set_layer_region_flag(true);
285         else
286                 set_layer_region_flag(false);
287
288         if(settings.get_value("circle.layer_outline",value) && value=="1")
289                 set_layer_outline_flag(true);
290         else
291                 set_layer_outline_flag(false);
292
293         if(settings.get_value("circle.layer_curve_gradient",value) && value=="1")
294                 set_layer_curve_gradient_flag(true);
295         else
296                 set_layer_curve_gradient_flag(false);
297
298         if(settings.get_value("circle.layer_plant",value) && value=="1")
299                 set_layer_plant_flag(true);
300         else
301                 set_layer_plant_flag(false);
302
303         if(settings.get_value("circle.layer_link_origins",value) && value=="0")
304                 set_layer_link_origins_flag(false);
305         else
306                 set_layer_link_origins_flag(true);
307 }
308
309 void
310 StateCircle_Context::save_settings()
311 {
312         settings.set_value("circle.id",get_id());
313         settings.set_value("circle.fallofftype",strprintf("%d",get_falloff()));
314 #ifdef BLEND_METHOD_IN_TOOL_OPTIONS
315         settings.set_value("circle.blend",strprintf("%d",get_blend()));
316 #endif  // BLEND_METHOD_IN_TOOL_OPTIONS
317         settings.set_value("circle.feather",strprintf("%f",(float)get_feather()));
318         settings.set_value("circle.number_of_bline_points",strprintf("%d",(int)(get_number_of_bline_points() + 0.5)));
319         settings.set_value("circle.bline_point_angle_offset",strprintf("%f",(float)get_bline_point_angle_offset()));
320         settings.set_value("circle.invert",get_invert()?"1":"0");
321         settings.set_value("circle.layer_circle",get_layer_circle_flag()?"1":"0");
322         settings.set_value("circle.layer_outline",get_layer_outline_flag()?"1":"0");
323         settings.set_value("circle.layer_region",get_layer_region_flag()?"1":"0");
324         settings.set_value("circle.layer_curve_gradient",get_layer_curve_gradient_flag()?"1":"0");
325         settings.set_value("circle.layer_plant",get_layer_plant_flag()?"1":"0");
326         settings.set_value("circle.layer_link_origins",get_layer_link_origins_flag()?"1":"0");
327 }
328
329 void
330 StateCircle_Context::reset()
331 {
332         refresh_ducks();
333 }
334
335 void
336 StateCircle_Context::increment_id()
337 {
338         String id(get_id());
339         int number=1;
340         int digits=0;
341
342         if(id.empty())
343                 id="Circle";
344
345         // If there is a number
346         // already at the end of the
347         // id, then remove it.
348         if(id[id.size()-1]<='9' && id[id.size()-1]>='0')
349         {
350                 // figure out how many digits it is
351                 for (digits = 0;
352                          (int)id.size()-1 >= digits && id[id.size()-1-digits] <= '9' && id[id.size()-1-digits] >= '0';
353                          digits++)
354                         ;
355
356                 String str_number;
357                 str_number=String(id,id.size()-digits,id.size());
358                 id=String(id,0,id.size()-digits);
359
360                 number=atoi(str_number.c_str());
361         }
362         else
363         {
364                 number=1;
365                 digits=3;
366         }
367
368         number++;
369
370         // Add the number back onto the id
371         {
372                 const String format(strprintf("%%0%dd",digits));
373                 id+=strprintf(format.c_str(),number);
374         }
375
376         // Set the ID
377         set_id(id);
378 }
379
380 StateCircle_Context::StateCircle_Context(CanvasView* canvas_view):
381         canvas_view_(canvas_view),
382         is_working(*canvas_view),
383         duckmatic_push(get_work_area()),
384         prev_workarea_layer_status_(get_work_area()->get_allow_layer_clicks()),
385         settings(synfigapp::Main::get_selected_input_device()->settings()),
386         entry_id(),                             //   value lower upper  step page
387         adj_feather(                                    0,    0,    1, 0.01, 0.1),
388         adj_number_of_bline_points(             0,    2,  120, 1   , 1  ),
389         adj_bline_point_angle_offset(   0, -360,  360, 0.1 , 1  ),
390         spin_feather(adj_feather,0.1,3),
391         spin_number_of_bline_points(adj_number_of_bline_points,1,0),
392         spin_bline_point_angle_offset(adj_bline_point_angle_offset,1,1),
393         checkbutton_invert(_("Invert")),
394         checkbutton_layer_circle(_("Create Circle Layer")),
395         checkbutton_layer_region(_("Create Region BLine")),
396         checkbutton_layer_outline(_("Create Outline BLine")),
397         checkbutton_layer_curve_gradient(_("Create Curve Gradient BLine")),
398         checkbutton_layer_plant(_("Create Plant BLine")),
399         checkbutton_layer_link_origins(_("Link Origins"))
400 {
401         egress_on_selection_change=true;
402
403         // Set up the tool options dialog
404         enum_falloff.set_param_desc(ParamDesc("falloff")
405                 .set_local_name(_("Falloff"))
406                 .set_description(_("Determines the falloff function for the feather"))
407                 .set_hint("enum")
408                 .add_enum_value(CIRCLE_INTERPOLATION_LINEAR,"linear",_("Linear"))
409                 .add_enum_value(CIRCLE_SQUARED,"squared",_("Squared"))
410                 .add_enum_value(CIRCLE_SQRT,"sqrt",_("Square Root"))
411                 .add_enum_value(CIRCLE_SIGMOND,"sigmond",_("Sigmond"))
412                 .add_enum_value(CIRCLE_COSINE,"cosine",_("Cosine")));
413
414 #ifdef BLEND_METHOD_IN_TOOL_OPTIONS
415         enum_blend.set_param_desc(ParamDesc(Color::BLEND_COMPOSITE,"blend_method")
416                 .set_local_name(_("Blend Method"))
417                 .set_description(_("Defines the blend method to be used for circles")));
418 #endif  // BLEND_METHOD_IN_TOOL_OPTIONS
419
420         load_settings();
421
422         options_table.attach(*manage(new Gtk::Label(_("Circle Tool"))),         0, 2,  0,  1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
423         options_table.attach(entry_id,                                                                          0, 2,  1,  2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
424
425         options_table.attach(checkbutton_layer_circle,                                          0, 2,  2,  3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
426         options_table.attach(checkbutton_layer_outline,                                         0, 2,  3,  4, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
427         options_table.attach(checkbutton_layer_region,                                          0, 2,  4,  5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
428         options_table.attach(checkbutton_layer_plant,                                           0, 2,  5,  6, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
429         options_table.attach(checkbutton_layer_curve_gradient,                          0, 2,  6,  7, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
430         options_table.attach(checkbutton_layer_link_origins,                            0, 2,  7,  8, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
431
432         //invert flag
433         options_table.attach(checkbutton_invert,                                                0, 2,  8,  9, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
434
435         options_table.attach(*manage(new Gtk::Label(_("Falloff:"))),            0, 1,  9, 10, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
436         options_table.attach(enum_falloff,                                      1, 2,  9, 10, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
437
438         //feather stuff
439         options_table.attach(*manage(new Gtk::Label(_("Feather:"))),            0, 1, 10, 11, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
440         options_table.attach(spin_feather,                                                              1, 2, 10, 11, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
441
442 #ifdef BLEND_METHOD_IN_TOOL_OPTIONS
443         options_table.attach(enum_blend,                                        0, 2, 11, 12, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
444 #endif  // BLEND_METHOD_IN_TOOL_OPTIONS
445
446         options_table.attach(*manage(new Gtk::Label(_("BLine Points:"))),       0, 1, 12, 13, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
447         options_table.attach(spin_number_of_bline_points,                                       1, 2, 12, 13, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
448
449 options_table.attach(*manage(new Gtk::Label(_("Point Angle Offset:"))), 0, 1, 13, 14, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
450         options_table.attach(spin_bline_point_angle_offset,                                     1, 2, 13, 14, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
451
452         options_table.show_all();
453
454         refresh_tool_options();
455         App::dialog_tool_options->present();
456
457         // Turn off layer clicking
458         get_work_area()->set_allow_layer_clicks(false);
459
460         // clear out the ducks
461         get_work_area()->clear_ducks();
462
463         // Refresh the work area
464         get_work_area()->queue_draw();
465
466         // Hide the tables if they are showing
467         //prev_table_status=get_canvas_view()->tables_are_visible();
468         //if(prev_table_status)get_canvas_view()->hide_tables();
469
470         // Disable the time bar
471         //get_canvas_view()->set_sensitive_timebar(false);
472
473         // Connect a signal
474         //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StateCircle_Context::on_user_click));
475         get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
476
477         App::toolbox->refresh();
478 }
479
480 void
481 StateCircle_Context::refresh_tool_options()
482 {
483         App::dialog_tool_options->clear();
484         App::dialog_tool_options->set_widget(options_table);
485         App::dialog_tool_options->set_local_name(_("Circle Tool"));
486         App::dialog_tool_options->set_name("circle");
487 }
488
489 Smach::event_result
490 StateCircle_Context::event_refresh_tool_options(const Smach::event& /*x*/)
491 {
492         refresh_tool_options();
493         return Smach::RESULT_ACCEPT;
494 }
495
496 StateCircle_Context::~StateCircle_Context()
497 {
498         save_settings();
499
500         // Restore layer clicking
501         get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
502         get_canvas_view()->work_area->reset_cursor();
503
504         App::dialog_tool_options->clear();
505
506         // Enable the time bar
507         //get_canvas_view()->set_sensitive_timebar(true);
508
509         // Bring back the tables if they were out before
510         //if(prev_table_status)get_canvas_view()->show_tables();
511
512         // Refresh the work area
513         get_work_area()->queue_draw();
514
515         get_canvas_view()->queue_rebuild_ducks();
516
517         App::toolbox->refresh();
518 }
519
520 Smach::event_result
521 StateCircle_Context::event_stop_handler(const Smach::event& /*x*/)
522 {
523         throw Smach::egress_exception();
524 }
525
526 Smach::event_result
527 StateCircle_Context::event_refresh_handler(const Smach::event& /*x*/)
528 {
529         refresh_ducks();
530         return Smach::RESULT_ACCEPT;
531 }
532
533 void
534 StateCircle_Context::make_circle(const Point& _p1, const Point& _p2)
535 {
536         synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New Circle"));
537         synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
538
539         Layer::Handle layer;
540
541         Canvas::Handle canvas;
542         int depth(0);
543
544         // we are temporarily using the layer to hold something
545         layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
546         if(layer)
547         {
548                 depth=layer->get_depth();
549                 canvas=layer->get_canvas();
550         }
551
552         synfigapp::SelectionManager::LayerList layer_selection;
553         if (!getenv("SYNFIG_TOOLS_CLEAR_SELECTION"))
554                 layer_selection = get_canvas_view()->get_selection_manager()->get_selected_layers();
555
556         const synfig::TransformStack& transform(get_canvas_view()->get_curr_transform_stack());
557         const Point p1(transform.unperform(_p1));
558         const Point p2(transform.unperform(_p2));
559
560         Real radius((p2-p1).mag());
561         int points = get_number_of_bline_points();
562         Angle::deg offset(get_bline_point_angle_offset());
563         Angle::deg angle(360.0/points);
564         Real tangent(4 * ((points == 2)
565                                           ? 1
566                                           : ((2 * Angle::cos(angle/2).get() - Angle::cos(angle).get() - 1) / Angle::sin(angle).get())));
567
568         std::vector<BLinePoint> new_list;
569         for (int i = 0; i < points; i++)
570         {
571                 new_list.push_back(*(new BLinePoint));
572                 new_list[i].set_width(1);
573                 new_list[i].set_vertex(Point(radius*Angle::cos(angle*i + offset).get(),
574                                                                          radius*Angle::sin(angle*i + offset).get()));
575                 new_list[i].set_tangent(Point(-radius*tangent*Angle::sin(angle*i + offset).get(),
576                                                                            radius*tangent*Angle::cos(angle*i + offset).get()));
577         }
578
579         ValueNode_BLine::Handle value_node_bline(ValueNode_BLine::create(new_list));
580         assert(value_node_bline);
581
582         ValueNode_Const::Handle value_node_origin(ValueNode_Const::create(p1));
583         assert(value_node_origin);
584
585         // Set the looping flag
586         value_node_bline->set_loop(true);
587
588         if(!canvas)
589                 canvas=get_canvas_view()->get_canvas();
590
591         value_node_bline->set_member_canvas(canvas);
592
593         // count how many layers we're going to be creating
594         int layers_to_create = this->layers_to_create();
595
596         ///////////////////////////////////////////////////////////////////////////
597         //   C I R C L E
598         ///////////////////////////////////////////////////////////////////////////
599
600         if (get_layer_circle_flag() &&
601                 get_falloff() >= 0 && get_falloff() < CIRCLE_NUM_FALLOFF)
602         {
603                 layer=get_canvas_interface()->add_layer_to("circle",canvas,depth);
604                 if (!layer)
605                 {
606                         get_canvas_view()->get_ui_interface()->error(_("Unable to create layer"));
607                         group.cancel();
608                         return;
609                 }
610                 layer_selection.push_back(layer);
611
612                 layer->set_param("origin",p1);
613                 get_canvas_interface()->signal_layer_param_changed()(layer,"origin");
614
615                 layer->set_param("radius",(p2-p1).mag());
616                 get_canvas_interface()->signal_layer_param_changed()(layer,"radius");
617
618                 layer->set_param("falloff",get_falloff());
619                 get_canvas_interface()->signal_layer_param_changed()(layer,"falloff");
620
621                 layer->set_param("feather",get_feather());
622                 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
623
624                 layer->set_param("invert",get_invert());
625                 get_canvas_interface()->signal_layer_param_changed()(layer,"invert");
626
627 #ifdef BLEND_METHOD_IN_TOOL_OPTIONS
628                 layer->set_param("blend_method",get_blend());
629                 get_canvas_interface()->signal_layer_param_changed()(layer,"blend_method");
630 #endif  // BLEND_METHOD_IN_TOOL_OPTIONS
631
632                 layer->set_description(get_id());
633                 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
634
635                 if(get_layer_outline_flag())
636                 {
637                         layer->set_param("color",synfigapp::Main::get_background_color());
638                         get_canvas_interface()->signal_layer_param_changed()(layer,"color");
639                 }
640
641                 // only link the circle's origin parameter if the option is selected and we're creating more than one layer
642                 if (get_layer_link_origins_flag() && layers_to_create > 1)
643                 {
644                         synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
645                         assert(action);
646
647                         action->set_param("canvas",get_canvas());
648                         action->set_param("canvas_interface",get_canvas_interface());
649                         action->set_param("layer",layer);
650                         if(!action->set_param("param",String("origin")))
651                                 synfig::error("LayerParamConnect didn't like \"param\"");
652                         if(!action->set_param("value_node",ValueNode::Handle(value_node_origin)))
653                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
654
655                         if(!get_canvas_interface()->get_instance()->perform_action(action))
656                         {
657                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create Circle layer"));
658                                 group.cancel();
659                                 throw String(_("Unable to create Circle layer"));
660                                 return;
661                         }
662                 }
663                 else
664                 {
665                         layer->set_param("origin",p1);
666                         get_canvas_interface()->signal_layer_param_changed()(layer,"origin");
667                 }
668         }
669
670         ///////////////////////////////////////////////////////////////////////////
671         //   C U R V E   G R A D I E N T
672         ///////////////////////////////////////////////////////////////////////////
673
674         if(get_layer_curve_gradient_flag())
675         {
676                 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
677
678                 Layer::Handle layer(get_canvas_interface()->add_layer_to("curve_gradient",canvas,depth));
679                 if (!layer)
680                 {
681                         get_canvas_view()->get_ui_interface()->error(_("Unable to create layer"));
682                         group.cancel();
683                         return;
684                 }
685                 layer_selection.push_back(layer);
686                 layer->set_description(get_id()+_(" Gradient"));
687                 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
688
689 #ifdef BLEND_METHOD_IN_TOOL_OPTIONS
690                 layer->set_param("blend_method",get_blend());
691                 get_canvas_interface()->signal_layer_param_changed()(layer,"blend_method");
692 #endif  // BLEND_METHOD_IN_TOOL_OPTIONS
693
694                 {
695                         synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
696                         assert(action);
697
698                         action->set_param("canvas",get_canvas());
699                         action->set_param("canvas_interface",get_canvas_interface());
700                         action->set_param("layer",layer);
701                         if(!action->set_param("param",String("bline")))
702                                 synfig::error("LayerParamConnect didn't like \"param\"");
703                         if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
704                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
705
706                         if(!get_canvas_interface()->get_instance()->perform_action(action))
707                         {
708                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
709                                 group.cancel();
710                                 throw String(_("Unable to create Gradient layer"));
711                                 return;
712                         }
713                 }
714
715                 // only link the curve gradient's origin parameter if the option is selected and we're creating more than one layer
716                 if (get_layer_link_origins_flag() && layers_to_create > 1)
717                 {
718                         synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
719                         assert(action);
720
721                         action->set_param("canvas",get_canvas());
722                         action->set_param("canvas_interface",get_canvas_interface());
723                         action->set_param("layer",layer);
724                         if(!action->set_param("param",String("origin")))
725                                 synfig::error("LayerParamConnect didn't like \"param\"");
726                         if(!action->set_param("value_node",ValueNode::Handle(value_node_origin)))
727                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
728
729                         if(!get_canvas_interface()->get_instance()->perform_action(action))
730                         {
731                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
732                                 group.cancel();
733                                 throw String(_("Unable to create Gradient layer"));
734                                 return;
735                         }
736                 }
737                 else
738                 {
739                         layer->set_param("origin",p1);
740                         get_canvas_interface()->signal_layer_param_changed()(layer,"origin");
741                 }
742         }
743
744         ///////////////////////////////////////////////////////////////////////////
745         //   P L A N T
746         ///////////////////////////////////////////////////////////////////////////
747
748         if(get_layer_plant_flag())
749         {
750                 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
751
752                 Layer::Handle layer(get_canvas_interface()->add_layer_to("plant",canvas,depth));
753                 if (!layer)
754                 {
755                         get_canvas_view()->get_ui_interface()->error(_("Unable to create layer"));
756                         group.cancel();
757                         return;
758                 }
759                 layer_selection.push_back(layer);
760                 layer->set_description(get_id()+_(" Plant"));
761                 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
762
763 #ifdef BLEND_METHOD_IN_TOOL_OPTIONS
764                 layer->set_param("blend_method",get_blend());
765                 get_canvas_interface()->signal_layer_param_changed()(layer,"blend_method");
766 #endif  // BLEND_METHOD_IN_TOOL_OPTIONS
767
768                 {
769                         synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
770                         assert(action);
771
772                         action->set_param("canvas",get_canvas());
773                         action->set_param("canvas_interface",get_canvas_interface());
774                         action->set_param("layer",layer);
775                         if(!action->set_param("param",String("bline")))
776                                 synfig::error("LayerParamConnect didn't like \"param\"");
777                         if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
778                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
779
780                         if(!get_canvas_interface()->get_instance()->perform_action(action))
781                         {
782                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
783                                 group.cancel();
784                                 throw String(_("Unable to create Plant layer"));
785                                 return;
786                         }
787                 }
788
789                 // only link the plant's origin parameter if the option is selected and we're creating more than one layer
790                 if (get_layer_link_origins_flag() && layers_to_create > 1)
791                 {
792                         synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
793                         assert(action);
794
795                         action->set_param("canvas",get_canvas());
796                         action->set_param("canvas_interface",get_canvas_interface());
797                         action->set_param("layer",layer);
798                         if(!action->set_param("param",String("origin")))
799                                 synfig::error("LayerParamConnect didn't like \"param\"");
800                         if(!action->set_param("value_node",ValueNode::Handle(value_node_origin)))
801                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
802
803                         if(!get_canvas_interface()->get_instance()->perform_action(action))
804                         {
805                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
806                                 group.cancel();
807                                 throw String(_("Unable to create Plant layer"));
808                                 return;
809                         }
810                 }
811                 else
812                 {
813                         layer->set_param("origin",p1);
814                         get_canvas_interface()->signal_layer_param_changed()(layer,"origin");
815                 }
816         }
817
818         ///////////////////////////////////////////////////////////////////////////
819         //   R E G I O N
820         ///////////////////////////////////////////////////////////////////////////
821
822         if(get_layer_region_flag())
823         {
824                 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
825
826                 Layer::Handle layer(get_canvas_interface()->add_layer_to("region",canvas,depth));
827                 if (!layer)
828                 {
829                         get_canvas_view()->get_ui_interface()->error(_("Unable to create layer"));
830                         group.cancel();
831                         return;
832                 }
833                 layer_selection.push_back(layer);
834                 layer->set_description(get_id()+_(" Region"));
835                 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
836
837 #ifdef BLEND_METHOD_IN_TOOL_OPTIONS
838                 layer->set_param("blend_method",get_blend());
839                 get_canvas_interface()->signal_layer_param_changed()(layer,"blend_method");
840 #endif  // BLEND_METHOD_IN_TOOL_OPTIONS
841
842                 layer->set_param("feather",get_feather());
843                 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
844
845                 layer->set_param("invert",get_invert());
846                 get_canvas_interface()->signal_layer_param_changed()(layer,"invert");
847
848                 if(get_layer_outline_flag())
849                         layer->set_param("color",synfigapp::Main::get_background_color());
850
851                 // I don't know if it's safe to reuse the same layer_param_connect action, so I'm
852                 // using 2 separate ones.
853                 {
854                         synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
855                         assert(action);
856
857                         action->set_param("canvas",get_canvas());
858                         action->set_param("canvas_interface",get_canvas_interface());
859                         action->set_param("layer",layer);
860                         if(!action->set_param("param",String("bline")))
861                                 synfig::error("LayerParamConnect didn't like \"param\"");
862                         if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
863                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
864
865                         if(!get_canvas_interface()->get_instance()->perform_action(action))
866                         {
867                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create Region layer"));
868                                 group.cancel();
869                                 throw String(_("Unable to create Region layer"));
870                                 return;
871                         }
872                 }
873
874                 // only link the region's origin parameter if the option is selected and we're creating more than one layer
875                 if (get_layer_link_origins_flag() && layers_to_create > 1)
876                 {
877                         synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
878                         assert(action);
879
880                         action->set_param("canvas",get_canvas());
881                         action->set_param("canvas_interface",get_canvas_interface());
882                         action->set_param("layer",layer);
883                         if(!action->set_param("param",String("origin")))
884                                 synfig::error("LayerParamConnect didn't like \"param\"");
885                         if(!action->set_param("value_node",ValueNode::Handle(value_node_origin)))
886                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
887
888                         if(!get_canvas_interface()->get_instance()->perform_action(action))
889                         {
890                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create Region layer"));
891                                 group.cancel();
892                                 throw String(_("Unable to create Region layer"));
893                                 return;
894                         }
895                 }
896                 else
897                 {
898                         layer->set_param("origin",p1);
899                         get_canvas_interface()->signal_layer_param_changed()(layer,"origin");
900                 }
901         }
902
903         ///////////////////////////////////////////////////////////////////////////
904         //   O U T L I N E
905         ///////////////////////////////////////////////////////////////////////////
906
907         if (get_layer_outline_flag())
908         {
909                 Layer::Handle layer(get_canvas_interface()->add_layer_to("outline",canvas,depth));
910                 if (!layer)
911                 {
912                         get_canvas_view()->get_ui_interface()->error(_("Unable to create layer"));
913                         group.cancel();
914                         return;
915                 }
916                 layer_selection.push_back(layer);
917                 layer->set_description(get_id()+_(" Outline"));
918                 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
919
920 #ifdef BLEND_METHOD_IN_TOOL_OPTIONS
921                 layer->set_param("blend_method",get_blend());
922                 get_canvas_interface()->signal_layer_param_changed()(layer,"blend_method");
923 #endif  // BLEND_METHOD_IN_TOOL_OPTIONS
924
925                 layer->set_param("feather",get_feather());
926                 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
927
928                 layer->set_param("invert",get_invert());
929                 get_canvas_interface()->signal_layer_param_changed()(layer,"invert");
930
931                 {
932                         synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
933                         assert(action);
934
935                         action->set_param("canvas",get_canvas());
936                         action->set_param("canvas_interface",get_canvas_interface());
937                         action->set_param("layer",layer);
938                         if(!action->set_param("param",String("bline")))
939                                 synfig::error("LayerParamConnect didn't like \"param\"");
940                         if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
941                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
942
943                         if(!get_canvas_interface()->get_instance()->perform_action(action))
944                         {
945                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
946                                 group.cancel();
947                                 throw String(_("Unable to create Outline layer"));
948                                 return;
949                         }
950                 }
951
952                 // only link the outline's origin parameter if the option is selected and we're creating more than one layer
953                 if (get_layer_link_origins_flag() && layers_to_create > 1)
954                 {
955                         synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
956                         assert(action);
957
958                         action->set_param("canvas",get_canvas());
959                         action->set_param("canvas_interface",get_canvas_interface());
960                         action->set_param("layer",layer);
961                         if(!action->set_param("param",String("origin")))
962                                 synfig::error("LayerParamConnect didn't like \"param\"");
963                         if(!action->set_param("value_node",ValueNode::Handle(value_node_origin)))
964                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
965
966                         if(!get_canvas_interface()->get_instance()->perform_action(action))
967                         {
968                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
969                                 group.cancel();
970                                 throw String(_("Unable to create Outline layer"));
971                                 return;
972                         }
973                 }
974                 else
975                 {
976                         layer->set_param("origin",p1);
977                         get_canvas_interface()->signal_layer_param_changed()(layer,"origin");
978                 }
979         }
980
981         egress_on_selection_change=false;
982         get_canvas_interface()->get_selection_manager()->clear_selected_layers();
983         get_canvas_interface()->get_selection_manager()->set_selected_layers(layer_selection);
984         egress_on_selection_change=true;
985
986         reset();
987         increment_id();
988 }
989
990 Smach::event_result
991 StateCircle_Context::event_mouse_click_handler(const Smach::event& x)
992 {
993         const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
994
995         if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_DOWN && event.button==BUTTON_LEFT)
996         {
997                 point_holder=get_work_area()->snap_point_to_grid(event.pos);
998                 etl::handle<Duck> duck=new Duck();
999                 duck->set_point(point_holder);
1000                 duck->set_name("p1");
1001                 duck->set_type(Duck::TYPE_POSITION);
1002                 duck->set_editable(false);
1003                 get_work_area()->add_duck(duck);
1004
1005                 point2_duck=new Duck();
1006                 point2_duck->set_point(Vector(0,0));
1007                 point2_duck->set_name("radius");
1008                 point2_duck->set_origin(duck);
1009                 point2_duck->set_radius(true);
1010                 point2_duck->set_scalar(-1);
1011                 point2_duck->set_type(Duck::TYPE_RADIUS);
1012                 point2_duck->set_hover(true);
1013                 get_work_area()->add_duck(point2_duck);
1014
1015                 return Smach::RESULT_ACCEPT;
1016         }
1017
1018         if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_DRAG && event.button==BUTTON_LEFT)
1019         {
1020                 if (!point2_duck) return Smach::RESULT_OK;
1021                 point2_duck->set_point(point_holder-get_work_area()->snap_point_to_grid(event.pos));
1022                 get_work_area()->queue_draw();
1023                 return Smach::RESULT_ACCEPT;
1024         }
1025
1026         if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_UP && event.button==BUTTON_LEFT)
1027         {
1028                 Point point(get_work_area()->snap_point_to_grid(event.pos));
1029
1030                 if (App::restrict_radius_ducks)
1031                 {
1032                         if ((point[0] - point_holder[0]) < 0) point[0] = point_holder[0];
1033                         if ((point[1] - point_holder[1]) < 0) point[1] = point_holder[1];
1034                 }
1035
1036                 make_circle(point_holder, point);
1037                 get_work_area()->clear_ducks();
1038                 return Smach::RESULT_ACCEPT;
1039         }
1040
1041         return Smach::RESULT_OK;
1042 }
1043
1044
1045 void
1046 StateCircle_Context::refresh_ducks()
1047 {
1048         get_work_area()->clear_ducks();
1049         get_work_area()->queue_draw();
1050 }