38dd7b3ba353debc809df90af5c0c3abd520a33e
[synfig.git] /
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_region_flag() +
134                         get_layer_outline_flag() +
135                         get_layer_curve_gradient_flag() +
136                         get_layer_plant_flag();
137         }
138
139         synfig::String get_id()const { return entry_id.get_text(); }
140         void set_id(const synfig::String& x) { return entry_id.set_text(x); }
141
142         int get_falloff()const { return enum_falloff.get_value(); }
143         void set_falloff(int x) { return enum_falloff.set_value(x); }
144
145 #ifdef BLEND_METHOD_IN_TOOL_OPTIONS
146         int get_blend()const { return enum_blend.get_value(); }
147         void set_blend(int x) { return enum_blend.set_value(x); }
148 #endif  // BLEND_METHOD_IN_TOOL_OPTIONS
149
150         Real get_feather()const { return adj_feather.get_value(); }
151         void set_feather(Real f) { adj_feather.set_value(f); }
152
153         Real get_number_of_bline_points()const { return adj_number_of_bline_points.get_value(); }
154         void set_number_of_bline_points(Real f) { adj_number_of_bline_points.set_value(f); }
155
156         Real get_bline_point_angle_offset()const { return adj_bline_point_angle_offset.get_value(); }
157         void set_bline_point_angle_offset(Real f) { adj_bline_point_angle_offset.set_value(f); }
158
159         bool get_invert()const { return checkbutton_invert.get_active(); }
160         void set_invert(bool i) { checkbutton_invert.set_active(i); }
161
162         bool get_layer_circle_flag()const { return checkbutton_layer_circle.get_active(); }
163         void set_layer_circle_flag(bool x) { return checkbutton_layer_circle.set_active(x); }
164
165         bool get_layer_region_flag()const { return checkbutton_layer_region.get_active(); }
166         void set_layer_region_flag(bool x) { return checkbutton_layer_region.set_active(x); }
167
168         bool get_layer_outline_flag()const { return checkbutton_layer_outline.get_active(); }
169         void set_layer_outline_flag(bool x) { return checkbutton_layer_outline.set_active(x); }
170
171         bool get_layer_curve_gradient_flag()const { return checkbutton_layer_curve_gradient.get_active(); }
172         void set_layer_curve_gradient_flag(bool x) { return checkbutton_layer_curve_gradient.set_active(x); }
173
174         bool get_layer_plant_flag()const { return checkbutton_layer_plant.get_active(); }
175         void set_layer_plant_flag(bool x) { return checkbutton_layer_plant.set_active(x); }
176
177         bool get_layer_link_origins_flag()const { return checkbutton_layer_link_origins.get_active(); }
178         void set_layer_link_origins_flag(bool x) { return checkbutton_layer_link_origins.set_active(x); }
179
180         void refresh_tool_options(); //to refresh the toolbox
181
182         //events
183         Smach::event_result event_stop_handler(const Smach::event& x);
184         Smach::event_result event_refresh_handler(const Smach::event& x);
185         Smach::event_result event_mouse_click_handler(const Smach::event& x);
186         Smach::event_result event_refresh_tool_options(const Smach::event& x);
187
188         //constructor destructor
189         StateCircle_Context(CanvasView* canvas_view);
190         ~StateCircle_Context();
191
192         //Canvas interaction
193         const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
194         etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
195         synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
196         WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
197
198         //Modifying settings etc.
199         void load_settings();
200         void save_settings();
201         void reset();
202         void increment_id();
203         bool egress_on_selection_change;
204         Smach::event_result event_layer_selection_changed_handler(const Smach::event& /*x*/)
205         {
206                 if(egress_on_selection_change)
207                         throw Smach::egress_exception();
208                 return Smach::RESULT_OK;
209         }
210
211         void make_circle(const Point& p1, const Point& p2);
212
213 };      // END of class StateCircle_Context
214
215 /* === M E T H O D S ======================================================= */
216
217 StateCircle::StateCircle():
218         Smach::state<StateCircle_Context>("circle")
219 {
220         insert(event_def(EVENT_LAYER_SELECTION_CHANGED,&StateCircle_Context::event_layer_selection_changed_handler));
221         insert(event_def(EVENT_STOP,&StateCircle_Context::event_stop_handler));
222         insert(event_def(EVENT_REFRESH,&StateCircle_Context::event_refresh_handler));
223         insert(event_def(EVENT_REFRESH_DUCKS,&StateCircle_Context::event_refresh_handler));
224         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateCircle_Context::event_mouse_click_handler));
225         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DRAG,&StateCircle_Context::event_mouse_click_handler));
226         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_UP,&StateCircle_Context::event_mouse_click_handler));
227         insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateCircle_Context::event_refresh_tool_options));
228 }
229
230 StateCircle::~StateCircle()
231 {
232 }
233
234 void
235 StateCircle_Context::load_settings()
236 {
237         String value;
238
239         //parse the arguments yargh!
240         if(settings.get_value("circle.id",value))
241                 set_id(value);
242         else
243                 set_id("Circle");
244
245         if(settings.get_value("circle.fallofftype",value) && value != "")
246                 set_falloff(atoi(value.c_str()));
247         else
248                 set_falloff(2);
249
250 #ifdef BLEND_METHOD_IN_TOOL_OPTIONS
251         if(settings.get_value("circle.blend",value) && value != "")
252                 set_blend(atoi(value.c_str()));
253         else
254                 set_blend(0);//(int)Color::BLEND_COMPOSITE); //0 should be blend composites value
255 #endif  // BLEND_METHOD_IN_TOOL_OPTIONS
256
257         if(settings.get_value("circle.feather",value))
258                 set_feather(atof(value.c_str()));
259         else
260                 set_feather(0);
261
262         if(settings.get_value("circle.number_of_bline_points",value))
263                 set_number_of_bline_points(atof(value.c_str()));
264         else
265                 set_number_of_bline_points(4);
266
267         if(settings.get_value("circle.bline_point_angle_offset",value))
268                 set_bline_point_angle_offset(atof(value.c_str()));
269         else
270                 set_bline_point_angle_offset(0);
271
272         if(settings.get_value("circle.invert",value) && value != "0")
273                 set_invert(true);
274         else
275                 set_invert(false);
276
277         if(settings.get_value("circle.layer_circle",value) && value=="0")
278                 set_layer_circle_flag(false);
279         else
280                 set_layer_circle_flag(true);
281
282         if(settings.get_value("circle.layer_region",value) && value=="1")
283                 set_layer_region_flag(true);
284         else
285                 set_layer_region_flag(false);
286
287         if(settings.get_value("circle.layer_outline",value) && value=="1")
288                 set_layer_outline_flag(true);
289         else
290                 set_layer_outline_flag(false);
291
292         if(settings.get_value("circle.layer_curve_gradient",value) && value=="1")
293                 set_layer_curve_gradient_flag(true);
294         else
295                 set_layer_curve_gradient_flag(false);
296
297         if(settings.get_value("circle.layer_plant",value) && value=="1")
298                 set_layer_plant_flag(true);
299         else
300                 set_layer_plant_flag(false);
301
302         if(settings.get_value("circle.layer_link_origins",value) && value=="0")
303                 set_layer_link_origins_flag(false);
304         else
305                 set_layer_link_origins_flag(true);
306 }
307
308 void
309 StateCircle_Context::save_settings()
310 {
311         settings.set_value("circle.id",get_id());
312         settings.set_value("circle.fallofftype",strprintf("%d",get_falloff()));
313 #ifdef BLEND_METHOD_IN_TOOL_OPTIONS
314         settings.set_value("circle.blend",strprintf("%d",get_blend()));
315 #endif  // BLEND_METHOD_IN_TOOL_OPTIONS
316         settings.set_value("circle.feather",strprintf("%f",(float)get_feather()));
317         settings.set_value("circle.number_of_bline_points",strprintf("%d",(int)(get_number_of_bline_points() + 0.5)));
318         settings.set_value("circle.bline_point_angle_offset",strprintf("%f",(float)get_bline_point_angle_offset()));
319         settings.set_value("circle.invert",get_invert()?"1":"0");
320         settings.set_value("circle.layer_circle",get_layer_circle_flag()?"1":"0");
321         settings.set_value("circle.layer_outline",get_layer_outline_flag()?"1":"0");
322         settings.set_value("circle.layer_region",get_layer_region_flag()?"1":"0");
323         settings.set_value("circle.layer_curve_gradient",get_layer_curve_gradient_flag()?"1":"0");
324         settings.set_value("circle.layer_plant",get_layer_plant_flag()?"1":"0");
325         settings.set_value("circle.layer_link_origins",get_layer_link_origins_flag()?"1":"0");
326 }
327
328 void
329 StateCircle_Context::reset()
330 {
331         refresh_ducks();
332 }
333
334 void
335 StateCircle_Context::increment_id()
336 {
337         String id(get_id());
338         int number=1;
339         int digits=0;
340
341         if(id.empty())
342                 id="Circle";
343
344         // If there is a number
345         // already at the end of the
346         // id, then remove it.
347         if(id[id.size()-1]<='9' && id[id.size()-1]>='0')
348         {
349                 // figure out how many digits it is
350                 for (digits = 0;
351                          (int)id.size()-1 >= digits && id[id.size()-1-digits] <= '9' && id[id.size()-1-digits] >= '0';
352                          digits++)
353                         ;
354
355                 String str_number;
356                 str_number=String(id,id.size()-digits,id.size());
357                 id=String(id,0,id.size()-digits);
358
359                 number=atoi(str_number.c_str());
360         }
361         else
362         {
363                 number=1;
364                 digits=3;
365         }
366
367         number++;
368
369         // Add the number back onto the id
370         {
371                 const String format(strprintf("%%0%dd",digits));
372                 id+=strprintf(format.c_str(),number);
373         }
374
375         // Set the ID
376         set_id(id);
377 }
378
379 StateCircle_Context::StateCircle_Context(CanvasView* canvas_view):
380         canvas_view_(canvas_view),
381         is_working(*canvas_view),
382         duckmatic_push(get_work_area()),
383         prev_workarea_layer_status_(get_work_area()->get_allow_layer_clicks()),
384         settings(synfigapp::Main::get_selected_input_device()->settings()),
385         entry_id(),                             //   value lower upper  step page
386         adj_feather(                                    0,    0,    1, 0.01, 0.1),
387         adj_number_of_bline_points(             0,    2,  120, 1   , 1  ),
388         adj_bline_point_angle_offset(   0, -360,  360, 0.1 , 1  ),
389         spin_feather(adj_feather,0.1,3),
390         spin_number_of_bline_points(adj_number_of_bline_points,1,0),
391         spin_bline_point_angle_offset(adj_bline_point_angle_offset,1,1),
392         checkbutton_invert(_("Invert")),
393         checkbutton_layer_circle(_("Create Circle Layer")),
394         checkbutton_layer_region(_("Create Region BLine")),
395         checkbutton_layer_outline(_("Create Outline BLine")),
396         checkbutton_layer_curve_gradient(_("Create Curve Gradient BLine")),
397         checkbutton_layer_plant(_("Create Plant BLine")),
398         checkbutton_layer_link_origins(_("Link BLine Origins"))
399 {
400         egress_on_selection_change=true;
401         // Set up the tool options dialog
402         options_table.attach(*manage(new Gtk::Label(_("Circle Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
403         options_table.attach(entry_id,                                                                  0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
404
405         enum_falloff.set_param_desc(ParamDesc("falloff")
406                 .set_local_name(_("Falloff"))
407                 .set_description(_("Determines the falloff function for the feather"))
408                 .set_hint("enum")
409                 .add_enum_value(CIRCLE_INTERPOLATION_LINEAR,"linear",_("Linear"))
410                 .add_enum_value(CIRCLE_SQUARED,"squared",_("Squared"))
411                 .add_enum_value(CIRCLE_SQRT,"sqrt",_("Square Root"))
412                 .add_enum_value(CIRCLE_SIGMOND,"sigmond",_("Sigmond"))
413                 .add_enum_value(CIRCLE_COSINE,"cosine",_("Cosine")));
414
415 #ifdef BLEND_METHOD_IN_TOOL_OPTIONS
416         enum_blend.set_param_desc(ParamDesc(Color::BLEND_COMPOSITE,"blend_method")
417                 .set_local_name(_("Blend Method"))
418                 .set_description(_("Defines the blend method to be used for circles")));
419 #endif  // BLEND_METHOD_IN_TOOL_OPTIONS
420
421         load_settings();
422
423         //feather stuff
424         options_table.attach(*manage(new Gtk::Label(_("Feather:"))), 0, 1, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
425         options_table.attach(spin_feather, 1, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
426         options_table.attach(enum_falloff, 0, 2, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
427 #ifdef BLEND_METHOD_IN_TOOL_OPTIONS
428         options_table.attach(enum_blend, 0, 2, 5, 6, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
429 #endif  // BLEND_METHOD_IN_TOOL_OPTIONS
430         options_table.attach(checkbutton_invert,                                        0, 2,  6,  7, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
431         options_table.attach(checkbutton_layer_circle,                          0, 2,  7,  8, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
432         options_table.attach(checkbutton_layer_outline,                         0, 2,  8,  9, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
433         options_table.attach(checkbutton_layer_region,                          0, 2,  9, 10, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
434         options_table.attach(checkbutton_layer_plant,                           0, 2, 10, 11, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
435         options_table.attach(checkbutton_layer_curve_gradient,          0, 2, 11, 12, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
436         options_table.attach(checkbutton_layer_link_origins,            0, 2, 12, 13, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
437         options_table.attach(*manage(new Gtk::Label(_("BLine Points:"))), 0, 1, 13, 14, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
438         options_table.attach(spin_number_of_bline_points, 1, 2, 13, 14, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
439         options_table.attach(*manage(new Gtk::Label(_("Point Angle Offset:"))), 0, 1, 14, 15, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
440         options_table.attach(spin_bline_point_angle_offset, 1, 2, 14, 15, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
441
442         options_table.show_all();
443
444         refresh_tool_options();
445         App::dialog_tool_options->present();
446
447         // Turn off layer clicking
448         get_work_area()->set_allow_layer_clicks(false);
449
450         // clear out the ducks
451         get_work_area()->clear_ducks();
452
453         // Refresh the work area
454         get_work_area()->queue_draw();
455
456         // Hide the tables if they are showing
457         //prev_table_status=get_canvas_view()->tables_are_visible();
458         //if(prev_table_status)get_canvas_view()->hide_tables();
459
460         // Disable the time bar
461         //get_canvas_view()->set_sensitive_timebar(false);
462
463         // Connect a signal
464         //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StateCircle_Context::on_user_click));
465         get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
466
467         App::toolbox->refresh();
468 }
469
470 void
471 StateCircle_Context::refresh_tool_options()
472 {
473         App::dialog_tool_options->clear();
474         App::dialog_tool_options->set_widget(options_table);
475         App::dialog_tool_options->set_local_name(_("Circle Tool"));
476         App::dialog_tool_options->set_name("circle");
477 }
478
479 Smach::event_result
480 StateCircle_Context::event_refresh_tool_options(const Smach::event& /*x*/)
481 {
482         refresh_tool_options();
483         return Smach::RESULT_ACCEPT;
484 }
485
486 StateCircle_Context::~StateCircle_Context()
487 {
488         save_settings();
489
490         // Restore layer clicking
491         get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
492         get_canvas_view()->work_area->reset_cursor();
493
494         App::dialog_tool_options->clear();
495
496         // Enable the time bar
497         //get_canvas_view()->set_sensitive_timebar(true);
498
499         // Bring back the tables if they were out before
500         //if(prev_table_status)get_canvas_view()->show_tables();
501
502         // Refresh the work area
503         get_work_area()->queue_draw();
504
505         get_canvas_view()->queue_rebuild_ducks();
506
507         App::toolbox->refresh();
508 }
509
510 Smach::event_result
511 StateCircle_Context::event_stop_handler(const Smach::event& /*x*/)
512 {
513         throw Smach::egress_exception();
514 }
515
516 Smach::event_result
517 StateCircle_Context::event_refresh_handler(const Smach::event& /*x*/)
518 {
519         refresh_ducks();
520         return Smach::RESULT_ACCEPT;
521 }
522
523 void
524 StateCircle_Context::make_circle(const Point& _p1, const Point& _p2)
525 {
526         synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New Circle"));
527         synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
528
529         Layer::Handle layer;
530
531         Canvas::Handle canvas;
532         int depth(0);
533
534         // we are temporarily using the layer to hold something
535         layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
536         if(layer)
537         {
538                 depth=layer->get_depth();
539                 canvas=layer->get_canvas();
540         }
541
542         synfigapp::SelectionManager::LayerList layer_selection;
543         if (!getenv("SYNFIG_TOOLS_CLEAR_SELECTION"))
544                 layer_selection = get_canvas_view()->get_selection_manager()->get_selected_layers();
545
546         const synfig::TransformStack& transform(get_canvas_view()->get_curr_transform_stack());
547         const Point p1(transform.unperform(_p1));
548         const Point p2(transform.unperform(_p2));
549
550         Real radius((p2-p1).mag());
551         int points = get_number_of_bline_points();
552         Angle::deg offset(get_bline_point_angle_offset());
553         Angle::deg angle(360.0/points);
554         Real tangent(4 * ((points == 2)
555                                           ? 1
556                                           : ((2 * Angle::cos(angle/2).get() - Angle::cos(angle).get() - 1) / Angle::sin(angle).get())));
557         Real x(p1[0]), y(p1[1]);
558
559         std::vector<BLinePoint> new_list;
560         for (int i = 0; i < points; i++)
561         {
562                 new_list.push_back(*(new BLinePoint));
563                 new_list[i].set_width(1);
564                 new_list[i].set_vertex(Point(radius*Angle::cos(angle*i + offset).get() + x,
565                                                                          radius*Angle::sin(angle*i + offset).get() + y));
566                 new_list[i].set_tangent(Point(-radius*tangent*Angle::sin(angle*i + offset).get(),
567                                                                            radius*tangent*Angle::cos(angle*i + offset).get()));
568         }
569
570         ValueNode_BLine::Handle value_node_bline(ValueNode_BLine::create(new_list));
571         assert(value_node_bline);
572
573         ValueNode_Const::Handle value_node_origin(ValueNode_Const::create(Vector()));
574         assert(value_node_origin);
575
576         // Set the looping flag
577         value_node_bline->set_loop(true);
578
579         if(!canvas)
580                 canvas=get_canvas_view()->get_canvas();
581
582         value_node_bline->set_member_canvas(canvas);
583
584         // count how many layers we're going to be creating
585         int layers_to_create = this->layers_to_create();
586
587         ///////////////////////////////////////////////////////////////////////////
588         //   C I R C L E
589         ///////////////////////////////////////////////////////////////////////////
590
591         if (get_layer_circle_flag() &&
592                 get_falloff() >= 0 && get_falloff() < CIRCLE_NUM_FALLOFF)
593         {
594                 layer=get_canvas_interface()->add_layer_to("circle",canvas,depth);
595                 layer_selection.push_back(layer);
596
597                 layer->set_param("origin",p1);
598                 get_canvas_interface()->signal_layer_param_changed()(layer,"origin");
599
600                 layer->set_param("radius",(p2-p1).mag());
601                 get_canvas_interface()->signal_layer_param_changed()(layer,"radius");
602
603                 layer->set_param("falloff",get_falloff());
604                 get_canvas_interface()->signal_layer_param_changed()(layer,"falloff");
605
606                 layer->set_param("feather",get_feather());
607                 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
608
609                 layer->set_param("invert",get_invert());
610                 get_canvas_interface()->signal_layer_param_changed()(layer,"invert");
611
612 #ifdef BLEND_METHOD_IN_TOOL_OPTIONS
613                 layer->set_param("blend_method",get_blend());
614                 get_canvas_interface()->signal_layer_param_changed()(layer,"blend_method");
615 #endif  // BLEND_METHOD_IN_TOOL_OPTIONS
616
617                 layer->set_description(get_id());
618                 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
619
620                 if(get_layer_outline_flag())
621                 {
622                         layer->set_param("color",synfigapp::Main::get_background_color());
623                         get_canvas_interface()->signal_layer_param_changed()(layer,"color");
624                 }
625         }
626
627         ///////////////////////////////////////////////////////////////////////////
628         //   C U R V E   G R A D I E N T
629         ///////////////////////////////////////////////////////////////////////////
630
631         if(get_layer_curve_gradient_flag())
632         {
633                 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
634
635                 Layer::Handle layer(get_canvas_interface()->add_layer_to("curve_gradient",canvas,depth));
636                 assert(layer);
637                 layer_selection.push_back(layer);
638                 layer->set_description(get_id()+_(" Gradient"));
639                 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
640
641 #ifdef BLEND_METHOD_IN_TOOL_OPTIONS
642                 layer->set_param("blend_method",get_blend());
643                 get_canvas_interface()->signal_layer_param_changed()(layer,"blend_method");
644 #endif  // BLEND_METHOD_IN_TOOL_OPTIONS
645
646                 {
647                         synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
648                         assert(action);
649
650                         action->set_param("canvas",get_canvas());
651                         action->set_param("canvas_interface",get_canvas_interface());
652                         action->set_param("layer",layer);
653                         if(!action->set_param("param",String("bline")))
654                                 synfig::error("LayerParamConnect didn't like \"param\"");
655                         if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
656                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
657
658                         if(!get_canvas_interface()->get_instance()->perform_action(action))
659                         {
660                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
661                                 group.cancel();
662                                 throw String(_("Unable to create Gradient layer"));
663                                 return;
664                         }
665                 }
666
667                 // only link the curve gradient's origin parameter if the option is selected and we're creating more than one layer
668                 if (get_layer_link_origins_flag() && layers_to_create > 1)
669                 {
670                         synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
671                         assert(action);
672
673                         action->set_param("canvas",get_canvas());
674                         action->set_param("canvas_interface",get_canvas_interface());
675                         action->set_param("layer",layer);
676                         if(!action->set_param("param",String("origin")))
677                                 synfig::error("LayerParamConnect didn't like \"param\"");
678                         if(!action->set_param("value_node",ValueNode::Handle(value_node_origin)))
679                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
680
681                         if(!get_canvas_interface()->get_instance()->perform_action(action))
682                         {
683                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
684                                 group.cancel();
685                                 throw String(_("Unable to create Gradient layer"));
686                                 return;
687                         }
688                 }
689         }
690
691         ///////////////////////////////////////////////////////////////////////////
692         //   P L A N T
693         ///////////////////////////////////////////////////////////////////////////
694
695         if(get_layer_plant_flag())
696         {
697                 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
698
699                 Layer::Handle layer(get_canvas_interface()->add_layer_to("plant",canvas,depth));
700                 assert(layer);
701                 layer_selection.push_back(layer);
702                 layer->set_description(get_id()+_(" Plant"));
703                 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
704
705 #ifdef BLEND_METHOD_IN_TOOL_OPTIONS
706                 layer->set_param("blend_method",get_blend());
707                 get_canvas_interface()->signal_layer_param_changed()(layer,"blend_method");
708 #endif  // BLEND_METHOD_IN_TOOL_OPTIONS
709
710                 {
711                         synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
712                         assert(action);
713
714                         action->set_param("canvas",get_canvas());
715                         action->set_param("canvas_interface",get_canvas_interface());
716                         action->set_param("layer",layer);
717                         if(!action->set_param("param",String("bline")))
718                                 synfig::error("LayerParamConnect didn't like \"param\"");
719                         if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
720                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
721
722                         if(!get_canvas_interface()->get_instance()->perform_action(action))
723                         {
724                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
725                                 group.cancel();
726                                 throw String(_("Unable to create Plant layer"));
727                                 return;
728                         }
729                 }
730
731                 // only link the plant's origin parameter if the option is selected and we're creating more than one layer
732                 if (get_layer_link_origins_flag() && layers_to_create > 1)
733                 {
734                         synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
735                         assert(action);
736
737                         action->set_param("canvas",get_canvas());
738                         action->set_param("canvas_interface",get_canvas_interface());
739                         action->set_param("layer",layer);
740                         if(!action->set_param("param",String("origin")))
741                                 synfig::error("LayerParamConnect didn't like \"param\"");
742                         if(!action->set_param("value_node",ValueNode::Handle(value_node_origin)))
743                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
744
745                         if(!get_canvas_interface()->get_instance()->perform_action(action))
746                         {
747                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
748                                 group.cancel();
749                                 throw String(_("Unable to create Plant layer"));
750                                 return;
751                         }
752                 }
753         }
754
755         ///////////////////////////////////////////////////////////////////////////
756         //   R E G I O N
757         ///////////////////////////////////////////////////////////////////////////
758
759         if(get_layer_region_flag())
760         {
761                 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
762
763                 Layer::Handle layer(get_canvas_interface()->add_layer_to("region",canvas,depth));
764                 assert(layer);
765                 layer_selection.push_back(layer);
766                 layer->set_description(get_id()+_(" Region"));
767                 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
768
769 #ifdef BLEND_METHOD_IN_TOOL_OPTIONS
770                 layer->set_param("blend_method",get_blend());
771                 get_canvas_interface()->signal_layer_param_changed()(layer,"blend_method");
772 #endif  // BLEND_METHOD_IN_TOOL_OPTIONS
773
774                 layer->set_param("feather",get_feather());
775                 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
776
777                 layer->set_param("invert",get_invert());
778                 get_canvas_interface()->signal_layer_param_changed()(layer,"invert");
779
780                 if(get_layer_outline_flag())
781                         layer->set_param("color",synfigapp::Main::get_background_color());
782
783                 // I don't know if it's safe to reuse the same layer_param_connect action, so I'm
784                 // using 2 separate ones.
785                 {
786                         synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
787                         assert(action);
788
789                         action->set_param("canvas",get_canvas());
790                         action->set_param("canvas_interface",get_canvas_interface());
791                         action->set_param("layer",layer);
792                         if(!action->set_param("param",String("bline")))
793                                 synfig::error("LayerParamConnect didn't like \"param\"");
794                         if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
795                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
796
797                         if(!get_canvas_interface()->get_instance()->perform_action(action))
798                         {
799                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create Region layer"));
800                                 group.cancel();
801                                 throw String(_("Unable to create Region layer"));
802                                 return;
803                         }
804                 }
805
806                 // only link the region's origin parameter if the option is selected and we're creating more than one layer
807                 if (get_layer_link_origins_flag() && layers_to_create > 1)
808                 {
809                         synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
810                         assert(action);
811
812                         action->set_param("canvas",get_canvas());
813                         action->set_param("canvas_interface",get_canvas_interface());
814                         action->set_param("layer",layer);
815                         if(!action->set_param("param",String("origin")))
816                                 synfig::error("LayerParamConnect didn't like \"param\"");
817                         if(!action->set_param("value_node",ValueNode::Handle(value_node_origin)))
818                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
819
820                         if(!get_canvas_interface()->get_instance()->perform_action(action))
821                         {
822                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create Region layer"));
823                                 group.cancel();
824                                 throw String(_("Unable to create Region layer"));
825                                 return;
826                         }
827                 }
828         }
829
830         ///////////////////////////////////////////////////////////////////////////
831         //   O U T L I N E
832         ///////////////////////////////////////////////////////////////////////////
833
834         if (get_layer_outline_flag())
835         {
836                 Layer::Handle layer(get_canvas_interface()->add_layer_to("outline",canvas,depth));
837                 assert(layer);
838                 layer_selection.push_back(layer);
839                 layer->set_description(get_id()+_(" Outline"));
840                 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
841
842 #ifdef BLEND_METHOD_IN_TOOL_OPTIONS
843                 layer->set_param("blend_method",get_blend());
844                 get_canvas_interface()->signal_layer_param_changed()(layer,"blend_method");
845 #endif  // BLEND_METHOD_IN_TOOL_OPTIONS
846
847                 layer->set_param("feather",get_feather());
848                 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
849
850                 layer->set_param("invert",get_invert());
851                 get_canvas_interface()->signal_layer_param_changed()(layer,"invert");
852
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 BLine layer"));
868                                 group.cancel();
869                                 throw String(_("Unable to create Outline layer"));
870                                 return;
871                         }
872                 }
873
874                 // only link the outline'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 BLine layer"));
891                                 group.cancel();
892                                 throw String(_("Unable to create Outline layer"));
893                                 return;
894                         }
895                 }
896         }
897
898         egress_on_selection_change=false;
899         get_canvas_interface()->get_selection_manager()->clear_selected_layers();
900         get_canvas_interface()->get_selection_manager()->set_selected_layers(layer_selection);
901         egress_on_selection_change=true;
902
903         reset();
904         increment_id();
905 }
906
907 Smach::event_result
908 StateCircle_Context::event_mouse_click_handler(const Smach::event& x)
909 {
910         const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
911
912         if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_DOWN && event.button==BUTTON_LEFT)
913         {
914                 point_holder=get_work_area()->snap_point_to_grid(event.pos);
915                 etl::handle<Duck> duck=new Duck();
916                 duck->set_point(point_holder);
917                 duck->set_name("p1");
918                 duck->set_type(Duck::TYPE_POSITION);
919                 duck->set_editable(false);
920                 get_work_area()->add_duck(duck);
921
922                 point2_duck=new Duck();
923                 point2_duck->set_point(Vector(0,0));
924                 point2_duck->set_name("radius");
925                 point2_duck->set_origin(duck);
926                 point2_duck->set_radius(true);
927                 point2_duck->set_scalar(-1);
928                 point2_duck->set_type(Duck::TYPE_RADIUS);
929                 point2_duck->set_hover(true);
930                 get_work_area()->add_duck(point2_duck);
931
932                 return Smach::RESULT_ACCEPT;
933         }
934
935         if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_DRAG && event.button==BUTTON_LEFT)
936         {
937                 if (!point2_duck) return Smach::RESULT_OK;
938                 point2_duck->set_point(point_holder-get_work_area()->snap_point_to_grid(event.pos));
939                 get_work_area()->queue_draw();
940                 return Smach::RESULT_ACCEPT;
941         }
942
943         if(event.key==EVENT_WORKAREA_MOUSE_BUTTON_UP && event.button==BUTTON_LEFT)
944         {
945                 Point point(get_work_area()->snap_point_to_grid(event.pos));
946
947                 if (App::restrict_radius_ducks)
948                 {
949                         if ((point[0] - point_holder[0]) < 0) point[0] = point_holder[0];
950                         if ((point[1] - point_holder[1]) < 0) point[1] = point_holder[1];
951                 }
952
953                 make_circle(point_holder, point);
954                 get_work_area()->clear_ducks();
955                 return Smach::RESULT_ACCEPT;
956         }
957
958         return Smach::RESULT_OK;
959 }
960
961
962 void
963 StateCircle_Context::refresh_ducks()
964 {
965         get_work_area()->clear_ducks();
966         get_work_area()->queue_draw();
967 }