Allow PasteCanvas layers with unset canvas parameters to be exported.
[synfig.git] / synfig-studio / trunk / src / gtkmm / state_polygon.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file state_polygon.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) 2007, 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_polygon.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 <synfigapp/main.h>
51
52 #include "general.h"
53
54 #endif
55
56 /* === U S I N G =========================================================== */
57
58 using namespace std;
59 using namespace etl;
60 using namespace synfig;
61 using namespace studio;
62
63 /* === M A C R O S ========================================================= */
64
65 /* === G L O B A L S ======================================================= */
66
67 StatePolygon studio::state_polygon;
68
69 /* === C L A S S E S & S T R U C T S ======================================= */
70
71 class studio::StatePolygon_Context : public sigc::trackable
72 {
73         etl::handle<CanvasView> canvas_view_;
74         CanvasView::IsWorking is_working;
75
76         bool prev_table_status;
77         bool prev_workarea_layer_status_;
78
79         Gtk::Menu menu;
80
81         Duckmatic::Push duckmatic_push;
82
83         std::list<synfig::Point> polygon_point_list;
84         synfigapp::Settings& settings;
85
86
87         bool on_polygon_duck_change(const synfig::Point &point, std::list<synfig::Point>::iterator iter);
88
89
90         void popup_handle_menu(synfigapp::ValueDesc value_desc);
91
92
93         void refresh_ducks();
94
95         Gtk::Table options_table;
96         Gtk::Entry entry_id;
97         Gtk::CheckButton checkbutton_invert;
98         Gtk::CheckButton checkbutton_layer_polygon;
99         Gtk::CheckButton checkbutton_layer_region;
100         Gtk::CheckButton checkbutton_layer_outline;
101         Gtk::CheckButton checkbutton_layer_curve_gradient;
102         Gtk::CheckButton checkbutton_layer_plant;
103         Gtk::CheckButton checkbutton_layer_link_origins;
104         Gtk::Button button_make;
105         Gtk::Adjustment  adj_feather;
106         Gtk::SpinButton  spin_feather;
107
108 public:
109
110         // this counts the layers we create - they all have origins we can link
111         int layers_to_create()const
112         {
113                 return
114                         get_layer_polygon_flag() +
115                         get_layer_region_flag() +
116                         get_layer_outline_flag() +
117                         get_layer_curve_gradient_flag() +
118                         get_layer_plant_flag();
119         }
120
121         synfig::String get_id()const { return entry_id.get_text(); }
122         void set_id(const synfig::String& x) { return entry_id.set_text(x); }
123
124         bool get_invert()const { return checkbutton_invert.get_active(); }
125         void set_invert(bool i) { checkbutton_invert.set_active(i); }
126
127         bool get_layer_polygon_flag()const { return checkbutton_layer_polygon.get_active(); }
128         void set_layer_polygon_flag(bool x) { return checkbutton_layer_polygon.set_active(x); }
129
130         bool get_layer_region_flag()const { return checkbutton_layer_region.get_active(); }
131         void set_layer_region_flag(bool x) { return checkbutton_layer_region.set_active(x); }
132
133         bool get_layer_outline_flag()const { return checkbutton_layer_outline.get_active(); }
134         void set_layer_outline_flag(bool x) { return checkbutton_layer_outline.set_active(x); }
135
136         bool get_layer_curve_gradient_flag()const { return checkbutton_layer_curve_gradient.get_active(); }
137         void set_layer_curve_gradient_flag(bool x) { return checkbutton_layer_curve_gradient.set_active(x); }
138
139         bool get_layer_plant_flag()const { return checkbutton_layer_plant.get_active(); }
140         void set_layer_plant_flag(bool x) { return checkbutton_layer_plant.set_active(x); }
141
142         bool get_layer_link_origins_flag()const { return checkbutton_layer_link_origins.get_active(); }
143         void set_layer_link_origins_flag(bool x) { return checkbutton_layer_link_origins.set_active(x); }
144
145         Real get_feather() const { return adj_feather.get_value(); }
146         void set_feather(Real x) { return adj_feather.set_value(x); }
147
148         Smach::event_result event_stop_handler(const Smach::event& x);
149
150         Smach::event_result event_refresh_handler(const Smach::event& x);
151
152         Smach::event_result event_mouse_click_handler(const Smach::event& x);
153         Smach::event_result event_refresh_tool_options(const Smach::event& x);
154         void refresh_tool_options();
155
156         StatePolygon_Context(CanvasView* canvas_view);
157
158         ~StatePolygon_Context();
159
160         const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
161         etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
162         synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
163         WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
164
165         //void on_user_click(synfig::Point point);
166         void load_settings();
167         void save_settings();
168         void reset();
169         void increment_id();
170         bool egress_on_selection_change;
171         Smach::event_result event_layer_selection_changed_handler(const Smach::event& /*x*/)
172         {
173                 if(egress_on_selection_change)
174                         throw Smach::egress_exception();
175                 return Smach::RESULT_OK;
176         }
177
178         void run();
179 };      // END of class StatePolygon_Context
180
181 /* === M E T H O D S ======================================================= */
182
183 StatePolygon::StatePolygon():
184         Smach::state<StatePolygon_Context>("polygon")
185 {
186         insert(event_def(EVENT_LAYER_SELECTION_CHANGED,&StatePolygon_Context::event_layer_selection_changed_handler));
187         insert(event_def(EVENT_STOP,&StatePolygon_Context::event_stop_handler));
188         insert(event_def(EVENT_REFRESH,&StatePolygon_Context::event_refresh_handler));
189         insert(event_def(EVENT_REFRESH_DUCKS,&StatePolygon_Context::event_refresh_handler));
190         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StatePolygon_Context::event_mouse_click_handler));
191         insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StatePolygon_Context::event_refresh_tool_options));
192 }
193
194 StatePolygon::~StatePolygon()
195 {
196 }
197
198 void
199 StatePolygon_Context::load_settings()
200 {
201         String value;
202
203         if(settings.get_value("polygon.id",value))
204                 set_id(value);
205         else
206                 set_id("Polygon");
207
208         if(settings.get_value("polygon.invert",value) && value != "0")
209                 set_invert(true);
210         else
211                 set_invert(false);
212
213         if(settings.get_value("polygon.layer_polygon",value) && value=="0")
214                 set_layer_polygon_flag(false);
215         else
216                 set_layer_polygon_flag(true);
217
218         if(settings.get_value("polygon.layer_region",value) && value=="1")
219                 set_layer_region_flag(true);
220         else
221                 set_layer_region_flag(false);
222
223         if(settings.get_value("polygon.layer_outline",value) && value=="1")
224                 set_layer_outline_flag(true);
225         else
226                 set_layer_outline_flag(false);
227
228         if(settings.get_value("polygon.layer_curve_gradient",value) && value=="1")
229                 set_layer_curve_gradient_flag(true);
230         else
231                 set_layer_curve_gradient_flag(false);
232
233         if(settings.get_value("polygon.layer_plant",value) && value=="1")
234                 set_layer_plant_flag(true);
235         else
236                 set_layer_plant_flag(false);
237
238         if(settings.get_value("polygon.layer_link_origins",value) && value=="0")
239                 set_layer_link_origins_flag(false);
240         else
241                 set_layer_link_origins_flag(true);
242
243         if(settings.get_value("polygon.feather",value))
244         {
245                 Real n = atof(value.c_str());
246                 set_feather(n);
247         }
248 }
249
250 void
251 StatePolygon_Context::save_settings()
252 {
253         settings.set_value("polygon.id",get_id().c_str());
254         settings.set_value("polygon.invert",get_invert()?"1":"0");
255         settings.set_value("polygon.layer_polygon",get_layer_polygon_flag()?"1":"0");
256         settings.set_value("polygon.layer_outline",get_layer_outline_flag()?"1":"0");
257         settings.set_value("polygon.layer_region",get_layer_region_flag()?"1":"0");
258         settings.set_value("polygon.layer_curve_gradient",get_layer_curve_gradient_flag()?"1":"0");
259         settings.set_value("polygon.layer_plant",get_layer_plant_flag()?"1":"0");
260         settings.set_value("polygon.layer_link_origins",get_layer_link_origins_flag()?"1":"0");
261         settings.set_value("polygon.feather",strprintf("%f",get_feather()));
262 }
263
264 void
265 StatePolygon_Context::reset()
266 {
267         polygon_point_list.clear();
268         refresh_ducks();
269 }
270
271 void
272 StatePolygon_Context::increment_id()
273 {
274         String id(get_id());
275         int number=1;
276         int digits=0;
277
278         if(id.empty())
279                 id="Polygon";
280
281         // If there is a number
282         // already at the end of the
283         // id, then remove it.
284         if(id[id.size()-1]<='9' && id[id.size()-1]>='0')
285         {
286                 // figure out how many digits it is
287                 for (digits = 0;
288                          (int)id.size()-1 >= digits && id[id.size()-1-digits] <= '9' && id[id.size()-1-digits] >= '0';
289                          digits++)
290                         ;
291
292                 String str_number;
293                 str_number=String(id,id.size()-digits,id.size());
294                 id=String(id,0,id.size()-digits);
295
296                 number=atoi(str_number.c_str());
297         }
298         else
299         {
300                 number=1;
301                 digits=3;
302         }
303
304         number++;
305
306         // Add the number back onto the id
307         {
308                 const String format(strprintf("%%0%dd",digits));
309                 id+=strprintf(format.c_str(),number);
310         }
311
312         // Set the ID
313         set_id(id);
314 }
315
316 StatePolygon_Context::StatePolygon_Context(CanvasView* canvas_view):
317         canvas_view_(canvas_view),
318         is_working(*canvas_view),
319         prev_workarea_layer_status_(get_work_area()->get_allow_layer_clicks()),
320         duckmatic_push(get_work_area()),
321         settings(synfigapp::Main::get_selected_input_device()->settings()),
322         entry_id(),
323         checkbutton_invert(_("Invert")),
324         checkbutton_layer_polygon(_("Create Polygon Layer")),
325         checkbutton_layer_region(_("Create Region BLine")),
326         checkbutton_layer_outline(_("Create Outline BLine")),
327         checkbutton_layer_curve_gradient(_("Create Curve Gradient BLine")),
328         checkbutton_layer_plant(_("Create Plant BLine")),
329         checkbutton_layer_link_origins(_("Link Origins")),
330         button_make(_("Make")),
331         adj_feather(0,0,10000,0.01,0.1),
332         spin_feather(adj_feather,0.01,4)
333 {
334         egress_on_selection_change=true;
335         load_settings();
336
337         // Set up the tool options dialog
338         options_table.attach(*manage(new Gtk::Label(_("Polygon Tool"))),        0, 2, 0,  1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
339         options_table.attach(entry_id,                                                                          0, 2, 1,  2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
340
341         options_table.attach(checkbutton_layer_polygon,                                         0, 2, 2,  3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
342         options_table.attach(checkbutton_layer_outline,                                         0, 2, 3,  4, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
343         options_table.attach(checkbutton_layer_region,                                          0, 2, 4,  5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
344         options_table.attach(checkbutton_layer_plant,                                           0, 2, 5,  6, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
345         options_table.attach(checkbutton_layer_curve_gradient,                          0, 2, 6,  7, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
346         options_table.attach(checkbutton_layer_link_origins,                            0, 2, 7,  8, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
347
348         //invert flag
349         options_table.attach(checkbutton_invert,                                                        0, 2, 8,  9, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
350
351         //feather stuff
352         options_table.attach(*manage(new Gtk::Label(_("Feather"))),             0, 1, 9, 10, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
353         options_table.attach(spin_feather,                                                                      1, 2, 9, 10, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
354
355         //options_table.attach(button_make, 0, 2, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
356         button_make.signal_pressed().connect(sigc::mem_fun(*this,&StatePolygon_Context::run));
357         options_table.show_all();
358         refresh_tool_options();
359         App::dialog_tool_options->present();
360
361
362         // Turn off layer clicking
363         get_work_area()->set_allow_layer_clicks(false);
364
365         // clear out the ducks
366         get_work_area()->clear_ducks();
367
368         // Refresh the work area
369         get_work_area()->queue_draw();
370
371         get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
372
373         // Hide the tables if they are showing
374         prev_table_status=get_canvas_view()->tables_are_visible();
375         if(prev_table_status)get_canvas_view()->hide_tables();
376
377         // Disable the time bar
378         get_canvas_view()->set_sensitive_timebar(false);
379
380         // Connect a signal
381         //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StatePolygon_Context::on_user_click));
382
383         App::toolbox->refresh();
384 }
385
386 void
387 StatePolygon_Context::refresh_tool_options()
388 {
389         App::dialog_tool_options->clear();
390         App::dialog_tool_options->set_widget(options_table);
391
392         App::dialog_tool_options->set_local_name(_("Polygon Tool"));
393         App::dialog_tool_options->set_name("polygon");
394
395         App::dialog_tool_options->add_button(
396                 Gtk::StockID("gtk-execute"),
397                 _("Make Polygon")
398         )->signal_clicked().connect(
399                 sigc::mem_fun(
400                         *this,
401                         &StatePolygon_Context::run
402                 )
403         );
404
405         App::dialog_tool_options->add_button(
406                 Gtk::StockID("gtk-clear"),
407                 _("Clear current Polygon")
408         )->signal_clicked().connect(
409                 sigc::mem_fun(
410                         *this,
411                         &StatePolygon_Context::reset
412                 )
413         );
414 }
415
416 Smach::event_result
417 StatePolygon_Context::event_refresh_tool_options(const Smach::event& /*x*/)
418 {
419         refresh_tool_options();
420         return Smach::RESULT_ACCEPT;
421 }
422
423 StatePolygon_Context::~StatePolygon_Context()
424 {
425         run();
426
427         save_settings();
428         // Restore layer clicking
429         get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
430
431         App::dialog_tool_options->clear();
432
433         get_canvas_view()->work_area->reset_cursor();
434
435         // Enable the time bar
436         get_canvas_view()->set_sensitive_timebar(true);
437
438         // Bring back the tables if they were out before
439         if(prev_table_status)get_canvas_view()->show_tables();
440
441         // Refresh the work area
442         get_work_area()->queue_draw();
443
444         App::toolbox->refresh();
445 }
446
447 Smach::event_result
448 StatePolygon_Context::event_stop_handler(const Smach::event& /*x*/)
449 {
450         synfig::info("STATE RotoPolygon: Received Stop Event");
451         //throw Smach::egress_exception();
452         reset();
453         return Smach::RESULT_ACCEPT;
454
455 }
456
457 Smach::event_result
458 StatePolygon_Context::event_refresh_handler(const Smach::event& /*x*/)
459 {
460         synfig::info("STATE RotoPolygon: Received Refresh Event");
461         refresh_ducks();
462         return Smach::RESULT_ACCEPT;
463 }
464
465 void
466 StatePolygon_Context::run()
467 {
468         if(polygon_point_list.empty())
469                 return;
470
471         if(polygon_point_list.size()<3)
472         {
473                 get_canvas_view()->get_ui_interface()->error("You need at least 3 points to create a polygon");
474                 return;
475         }
476
477         synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New Polygon"));
478
479         Layer::Handle layer;
480
481         Canvas::Handle canvas;
482         int depth(0);
483
484         // we are temporarily using the layer to hold something
485         layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
486         if(layer)
487         {
488                 depth=layer->get_depth();
489                 canvas=layer->get_canvas();
490         }
491
492         synfigapp::SelectionManager::LayerList layer_selection;
493         if (!getenv("SYNFIG_TOOLS_CLEAR_SELECTION"))
494                 layer_selection = get_canvas_view()->get_selection_manager()->get_selected_layers();
495
496         const synfig::TransformStack& transform(get_canvas_view()->get_curr_transform_stack());
497
498         std::vector<BLinePoint> new_list;
499         std::list<synfig::Point>::iterator iter;
500         int i;
501         for(i=0,iter=polygon_point_list.begin();iter!=polygon_point_list.end();++iter,++i)
502         {
503                 *iter = transform.unperform(*iter);
504                 new_list.push_back(*(new BLinePoint));
505                 new_list[i].set_width(1);
506                 new_list[i].set_vertex(*iter);
507                 new_list[i].set_tangent(Point(0,0));
508         }
509
510         ValueNode_BLine::Handle value_node_bline(ValueNode_BLine::create(new_list));
511         assert(value_node_bline);
512
513         ValueNode_Const::Handle value_node_origin(ValueNode_Const::create(Vector()));
514         assert(value_node_origin);
515
516         // Set the looping flag
517         value_node_bline->set_loop(true);
518
519         if(!canvas)
520                 canvas=get_canvas_view()->get_canvas();
521
522         value_node_bline->set_member_canvas(canvas);
523
524         // count how many layers we're going to be creating
525         int layers_to_create = this->layers_to_create();
526
527         ///////////////////////////////////////////////////////////////////////////
528         //   P O L Y G O N
529         ///////////////////////////////////////////////////////////////////////////
530
531         if (get_layer_polygon_flag())
532         {
533                 layer=get_canvas_interface()->add_layer_to("polygon",canvas,depth);
534                 if (!layer)
535                 {
536                         get_canvas_view()->get_ui_interface()->error(_("Unable to create layer"));
537                         group.cancel();
538                         return;
539                 }
540                 layer_selection.push_back(layer);
541
542                 layer->set_param("invert",get_invert());
543                 get_canvas_interface()->signal_layer_param_changed()(layer,"invert");
544
545                 if(get_feather())
546                 {
547                         layer->set_param("feather",get_feather());
548                         get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
549                 }
550
551                 layer->set_description(get_id());
552                 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
553
554
555                 if(get_layer_outline_flag())
556                 {
557                         layer->set_param("color",synfigapp::Main::get_background_color());
558                         get_canvas_interface()->signal_layer_param_changed()(layer,"color");
559                 }
560
561                 layer->disconnect_dynamic_param("vector_list");
562                 if(!layer->set_param("vector_list",polygon_point_list))
563                 {
564                         group.cancel();
565                         get_canvas_view()->get_ui_interface()->error("Unable to set layer parameter");
566                         return;
567                 }
568
569                 {
570                         synfigapp::Action::Handle action(synfigapp::Action::create("value_desc_convert"));
571                         synfigapp::ValueDesc value_desc(layer,"vector_list");
572                         action->set_param("canvas",get_canvas());
573                         action->set_param("canvas_interface",get_canvas_interface());
574                         action->set_param("value_desc",value_desc);
575                         action->set_param("type","dynamic_list");
576                         action->set_param("time",get_canvas_interface()->get_time());
577                         if(!get_canvas_interface()->get_instance()->perform_action(action))
578                         {
579                                 group.cancel();
580                                 get_canvas_view()->get_ui_interface()->error("Unable to execute action \"value_desc_convert\"");
581                                 return;
582                         }
583                 }
584
585                 // only link the polygon's origin parameter if the option is selected and we're creating more than one layer
586                 if (get_layer_link_origins_flag() && layers_to_create > 1)
587                 {
588                         synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
589                         assert(action);
590
591                         action->set_param("canvas",get_canvas());
592                         action->set_param("canvas_interface",get_canvas_interface());
593                         action->set_param("layer",layer);
594                         if(!action->set_param("param",String("origin")))
595                                 synfig::error("LayerParamConnect didn't like \"param\"");
596                         if(!action->set_param("value_node",ValueNode::Handle(value_node_origin)))
597                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
598
599                         if(!get_canvas_interface()->get_instance()->perform_action(action))
600                         {
601                                 get_canvas_view()->get_ui_interface()->error(_("Unable to create Polygon layer"));
602                                 group.cancel();
603                                 throw String(_("Unable to create Polygon layer"));
604                                 return;
605                         }
606                 }
607         }
608
609         ///////////////////////////////////////////////////////////////////////////
610         //   C U R V E   G R A D I E N T
611         ///////////////////////////////////////////////////////////////////////////
612
613         if(get_layer_curve_gradient_flag())
614         {
615                 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
616
617                 Layer::Handle layer(get_canvas_interface()->add_layer_to("curve_gradient",canvas,depth));
618                 if (!layer)
619                 {
620                         get_canvas_view()->get_ui_interface()->error(_("Unable to create layer"));
621                         group.cancel();
622                         return;
623                 }
624                 layer_selection.push_back(layer);
625                 layer->set_description(get_id()+_(" Gradient"));
626                 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
627
628                 {
629                         synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
630                         assert(action);
631
632                         action->set_param("canvas",get_canvas());
633                         action->set_param("canvas_interface",get_canvas_interface());
634                         action->set_param("layer",layer);
635                         if(!action->set_param("param",String("bline")))
636                                 synfig::error("LayerParamConnect didn't like \"param\"");
637                         if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
638                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
639
640                         if(!get_canvas_interface()->get_instance()->perform_action(action))
641                         {
642                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
643                                 group.cancel();
644                                 throw String(_("Unable to create Gradient layer"));
645                                 return;
646                         }
647                 }
648
649                 // only link the curve gradient's origin parameter if the option is selected and we're creating more than one layer
650                 if (get_layer_link_origins_flag() && layers_to_create > 1)
651                 {
652                         synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
653                         assert(action);
654
655                         action->set_param("canvas",get_canvas());
656                         action->set_param("canvas_interface",get_canvas_interface());
657                         action->set_param("layer",layer);
658                         if(!action->set_param("param",String("origin")))
659                                 synfig::error("LayerParamConnect didn't like \"param\"");
660                         if(!action->set_param("value_node",ValueNode::Handle(value_node_origin)))
661                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
662
663                         if(!get_canvas_interface()->get_instance()->perform_action(action))
664                         {
665                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
666                                 group.cancel();
667                                 throw String(_("Unable to create Gradient layer"));
668                                 return;
669                         }
670                 }
671         }
672
673         ///////////////////////////////////////////////////////////////////////////
674         //   P L A N T
675         ///////////////////////////////////////////////////////////////////////////
676
677         if(get_layer_plant_flag())
678         {
679                 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
680
681                 Layer::Handle layer(get_canvas_interface()->add_layer_to("plant",canvas,depth));
682                 if (!layer)
683                 {
684                         get_canvas_view()->get_ui_interface()->error(_("Unable to create layer"));
685                         group.cancel();
686                         return;
687                 }
688                 layer_selection.push_back(layer);
689                 layer->set_description(get_id()+_(" Plant"));
690                 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
691
692                 {
693                         synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
694                         assert(action);
695
696                         action->set_param("canvas",get_canvas());
697                         action->set_param("canvas_interface",get_canvas_interface());
698                         action->set_param("layer",layer);
699                         if(!action->set_param("param",String("bline")))
700                                 synfig::error("LayerParamConnect didn't like \"param\"");
701                         if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
702                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
703
704                         if(!get_canvas_interface()->get_instance()->perform_action(action))
705                         {
706                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
707                                 group.cancel();
708                                 throw String(_("Unable to create Plant layer"));
709                                 return;
710                         }
711                 }
712
713                 // only link the plant's origin parameter if the option is selected and we're creating more than one layer
714                 if (get_layer_link_origins_flag() && layers_to_create > 1)
715                 {
716                         synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
717                         assert(action);
718
719                         action->set_param("canvas",get_canvas());
720                         action->set_param("canvas_interface",get_canvas_interface());
721                         action->set_param("layer",layer);
722                         if(!action->set_param("param",String("origin")))
723                                 synfig::error("LayerParamConnect didn't like \"param\"");
724                         if(!action->set_param("value_node",ValueNode::Handle(value_node_origin)))
725                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
726
727                         if(!get_canvas_interface()->get_instance()->perform_action(action))
728                         {
729                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
730                                 group.cancel();
731                                 throw String(_("Unable to create Plant layer"));
732                                 return;
733                         }
734                 }
735         }
736
737         ///////////////////////////////////////////////////////////////////////////
738         //   R E G I O N
739         ///////////////////////////////////////////////////////////////////////////
740
741         if(get_layer_region_flag())
742         {
743                 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
744
745                 Layer::Handle layer(get_canvas_interface()->add_layer_to("region",canvas,depth));
746                 if (!layer)
747                 {
748                         get_canvas_view()->get_ui_interface()->error(_("Unable to create layer"));
749                         group.cancel();
750                         return;
751                 }
752                 layer_selection.push_back(layer);
753                 layer->set_description(get_id()+_(" Region"));
754                 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
755
756                 layer->set_param("feather",get_feather());
757                 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
758
759                 layer->set_param("invert",get_invert());
760                 get_canvas_interface()->signal_layer_param_changed()(layer,"invert");
761
762                 if(get_layer_outline_flag())
763                         layer->set_param("color",synfigapp::Main::get_background_color());
764
765                 // I don't know if it's safe to reuse the same layer_param_connect action, so I'm
766                 // using 2 separate ones.
767                 {
768                         synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
769                         assert(action);
770
771                         action->set_param("canvas",get_canvas());
772                         action->set_param("canvas_interface",get_canvas_interface());
773                         action->set_param("layer",layer);
774                         if(!action->set_param("param",String("bline")))
775                                 synfig::error("LayerParamConnect didn't like \"param\"");
776                         if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
777                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
778
779                         if(!get_canvas_interface()->get_instance()->perform_action(action))
780                         {
781                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create Region layer"));
782                                 group.cancel();
783                                 throw String(_("Unable to create Region layer"));
784                                 return;
785                         }
786                 }
787
788                 // only link the region's origin parameter if the option is selected and we're creating more than one layer
789                 if (get_layer_link_origins_flag() && layers_to_create > 1)
790                 {
791                         synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
792                         assert(action);
793
794                         action->set_param("canvas",get_canvas());
795                         action->set_param("canvas_interface",get_canvas_interface());
796                         action->set_param("layer",layer);
797                         if(!action->set_param("param",String("origin")))
798                                 synfig::error("LayerParamConnect didn't like \"param\"");
799                         if(!action->set_param("value_node",ValueNode::Handle(value_node_origin)))
800                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
801
802                         if(!get_canvas_interface()->get_instance()->perform_action(action))
803                         {
804                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create Region layer"));
805                                 group.cancel();
806                                 throw String(_("Unable to create Region layer"));
807                                 return;
808                         }
809                 }
810         }
811
812         ///////////////////////////////////////////////////////////////////////////
813         //   O U T L I N E
814         ///////////////////////////////////////////////////////////////////////////
815
816         if (get_layer_outline_flag())
817         {
818                 Layer::Handle layer(get_canvas_interface()->add_layer_to("outline",canvas,depth));
819                 if (!layer)
820                 {
821                         get_canvas_view()->get_ui_interface()->error(_("Unable to create layer"));
822                         group.cancel();
823                         return;
824                 }
825                 layer_selection.push_back(layer);
826                 layer->set_description(get_id()+_(" Outline"));
827                 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
828
829                 layer->set_param("feather",get_feather());
830                 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
831
832                 layer->set_param("invert",get_invert());
833                 get_canvas_interface()->signal_layer_param_changed()(layer,"invert");
834
835                 {
836                         synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
837                         assert(action);
838
839                         action->set_param("canvas",get_canvas());
840                         action->set_param("canvas_interface",get_canvas_interface());
841                         action->set_param("layer",layer);
842                         if(!action->set_param("param",String("bline")))
843                                 synfig::error("LayerParamConnect didn't like \"param\"");
844                         if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
845                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
846
847                         if(!get_canvas_interface()->get_instance()->perform_action(action))
848                         {
849                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
850                                 group.cancel();
851                                 throw String(_("Unable to create Outline layer"));
852                                 return;
853                         }
854                 }
855
856                 // only link the outline's origin parameter if the option is selected and we're creating more than one layer
857                 if (get_layer_link_origins_flag() && layers_to_create > 1)
858                 {
859                         synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
860                         assert(action);
861
862                         action->set_param("canvas",get_canvas());
863                         action->set_param("canvas_interface",get_canvas_interface());
864                         action->set_param("layer",layer);
865                         if(!action->set_param("param",String("origin")))
866                                 synfig::error("LayerParamConnect didn't like \"param\"");
867                         if(!action->set_param("value_node",ValueNode::Handle(value_node_origin)))
868                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
869
870                         if(!get_canvas_interface()->get_instance()->perform_action(action))
871                         {
872                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
873                                 group.cancel();
874                                 throw String(_("Unable to create Outline layer"));
875                                 return;
876                         }
877                 }
878         }
879
880         egress_on_selection_change=false;
881         get_canvas_interface()->get_selection_manager()->clear_selected_layers();
882         get_canvas_interface()->get_selection_manager()->set_selected_layers(layer_selection);
883         egress_on_selection_change=true;
884
885         //post clean up stuff...
886         reset();
887         increment_id();
888 }
889
890 Smach::event_result
891 StatePolygon_Context::event_mouse_click_handler(const Smach::event& x)
892 {
893         synfig::info("STATE ROTOPOLYGON: Received mouse button down Event");
894         const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
895         switch(event.button)
896         {
897         case BUTTON_LEFT:
898                 polygon_point_list.push_back(get_work_area()->snap_point_to_grid(event.pos));
899                 refresh_ducks();
900                 return Smach::RESULT_ACCEPT;
901
902         default:
903                 return Smach::RESULT_OK;
904         }
905 }
906
907
908 void
909 StatePolygon_Context::refresh_ducks()
910 {
911         get_work_area()->clear_ducks();
912
913         if(polygon_point_list.empty()) return;
914
915         std::list<synfig::Point>::iterator iter=polygon_point_list.begin();
916
917         etl::handle<WorkArea::Duck> duck;
918         duck=new WorkArea::Duck(*iter);
919         duck->set_editable(true);
920         duck->signal_edited().connect(
921                 sigc::bind(sigc::mem_fun(*this,&studio::StatePolygon_Context::on_polygon_duck_change),iter)
922         );
923         duck->signal_user_click(0).connect(sigc::mem_fun(*this,&StatePolygon_Context::run));
924
925         get_work_area()->add_duck(duck);
926
927         for(++iter;iter!=polygon_point_list.end();++iter)
928         {
929                 etl::handle<WorkArea::Bezier> bezier(new WorkArea::Bezier());
930                 bezier->p1=bezier->c1=duck;
931
932                 duck=new WorkArea::Duck(*iter);
933                 duck->set_editable(true);
934                 duck->set_name(strprintf("%x",&*iter));
935                 duck->signal_edited().connect(
936                         sigc::bind(sigc::mem_fun(*this,&studio::StatePolygon_Context::on_polygon_duck_change),iter)
937                 );
938
939                 get_work_area()->add_duck(duck);
940
941                 bezier->p2=bezier->c2=duck;
942                 get_work_area()->add_bezier(bezier);
943         }
944         get_work_area()->queue_draw();
945 }
946
947
948 bool
949 StatePolygon_Context::on_polygon_duck_change(const synfig::Point &point, std::list<synfig::Point>::iterator iter)
950 {
951         *iter=point;
952         return true;
953 }