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