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