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