c016f1d5bfc520a60d30c2167023a58d8fedccb4
[synfig.git] / 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                 layer->disconnect_dynamic_param("vector_list");
555                 if(!layer->set_param("vector_list",polygon_point_list))
556                 {
557                         group.cancel();
558                         get_canvas_view()->get_ui_interface()->error("Unable to set layer parameter");
559                         return;
560                 }
561
562                 {
563                         synfigapp::Action::Handle action(synfigapp::Action::create("ValueDescConvert"));
564                         synfigapp::ValueDesc value_desc(layer,"vector_list");
565                         action->set_param("canvas",get_canvas());
566                         action->set_param("canvas_interface",get_canvas_interface());
567                         action->set_param("value_desc",value_desc);
568                         action->set_param("type","dynamic_list");
569                         action->set_param("time",get_canvas_interface()->get_time());
570                         if(!get_canvas_interface()->get_instance()->perform_action(action))
571                         {
572                                 group.cancel();
573                                 get_canvas_view()->get_ui_interface()->error("Unable to execute action \"ValueDescConvert\"");
574                                 return;
575                         }
576                 }
577
578                 // only link the polygon's origin parameter if the option is selected and we're creating more than one layer
579                 if (get_layer_link_origins_flag() && layers_to_create > 1)
580                 {
581                         synfigapp::Action::Handle action(synfigapp::Action::create("LayerParamConnect"));
582                         assert(action);
583
584                         action->set_param("canvas",get_canvas());
585                         action->set_param("canvas_interface",get_canvas_interface());
586                         action->set_param("layer",layer);
587                         if(!action->set_param("param",String("origin")))
588                                 synfig::error("LayerParamConnect didn't like \"param\"");
589                         if(!action->set_param("value_node",ValueNode::Handle(value_node_origin)))
590                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
591
592                         if(!get_canvas_interface()->get_instance()->perform_action(action))
593                         {
594                                 get_canvas_view()->get_ui_interface()->error(_("Unable to create Polygon layer"));
595                                 group.cancel();
596                                 throw String(_("Unable to create Polygon layer"));
597                                 return;
598                         }
599                 }
600         }
601
602         ///////////////////////////////////////////////////////////////////////////
603         //   C U R V E   G R A D I E N T
604         ///////////////////////////////////////////////////////////////////////////
605
606         if(get_layer_curve_gradient_flag())
607         {
608                 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
609
610                 Layer::Handle layer(get_canvas_interface()->add_layer_to("curve_gradient",canvas,depth));
611                 if (!layer)
612                 {
613                         get_canvas_view()->get_ui_interface()->error(_("Unable to create layer"));
614                         group.cancel();
615                         return;
616                 }
617                 layer_selection.push_back(layer);
618                 layer->set_description(get_id()+_(" Gradient"));
619                 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
620
621                 {
622                         synfigapp::Action::Handle action(synfigapp::Action::create("LayerParamConnect"));
623                         assert(action);
624
625                         action->set_param("canvas",get_canvas());
626                         action->set_param("canvas_interface",get_canvas_interface());
627                         action->set_param("layer",layer);
628                         if(!action->set_param("param",String("bline")))
629                                 synfig::error("LayerParamConnect didn't like \"param\"");
630                         if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
631                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
632
633                         if(!get_canvas_interface()->get_instance()->perform_action(action))
634                         {
635                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
636                                 group.cancel();
637                                 throw String(_("Unable to create Gradient layer"));
638                                 return;
639                         }
640                 }
641
642                 // only link the curve gradient's origin parameter if the option is selected and we're creating more than one layer
643                 if (get_layer_link_origins_flag() && layers_to_create > 1)
644                 {
645                         synfigapp::Action::Handle action(synfigapp::Action::create("LayerParamConnect"));
646                         assert(action);
647
648                         action->set_param("canvas",get_canvas());
649                         action->set_param("canvas_interface",get_canvas_interface());
650                         action->set_param("layer",layer);
651                         if(!action->set_param("param",String("origin")))
652                                 synfig::error("LayerParamConnect didn't like \"param\"");
653                         if(!action->set_param("value_node",ValueNode::Handle(value_node_origin)))
654                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
655
656                         if(!get_canvas_interface()->get_instance()->perform_action(action))
657                         {
658                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
659                                 group.cancel();
660                                 throw String(_("Unable to create Gradient layer"));
661                                 return;
662                         }
663                 }
664         }
665
666         ///////////////////////////////////////////////////////////////////////////
667         //   P L A N T
668         ///////////////////////////////////////////////////////////////////////////
669
670         if(get_layer_plant_flag())
671         {
672                 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
673
674                 Layer::Handle layer(get_canvas_interface()->add_layer_to("plant",canvas,depth));
675                 if (!layer)
676                 {
677                         get_canvas_view()->get_ui_interface()->error(_("Unable to create layer"));
678                         group.cancel();
679                         return;
680                 }
681                 layer_selection.push_back(layer);
682                 layer->set_description(get_id()+_(" Plant"));
683                 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
684
685                 {
686                         synfigapp::Action::Handle action(synfigapp::Action::create("LayerParamConnect"));
687                         assert(action);
688
689                         action->set_param("canvas",get_canvas());
690                         action->set_param("canvas_interface",get_canvas_interface());
691                         action->set_param("layer",layer);
692                         if(!action->set_param("param",String("bline")))
693                                 synfig::error("LayerParamConnect didn't like \"param\"");
694                         if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
695                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
696
697                         if(!get_canvas_interface()->get_instance()->perform_action(action))
698                         {
699                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
700                                 group.cancel();
701                                 throw String(_("Unable to create Plant layer"));
702                                 return;
703                         }
704                 }
705
706                 // only link the plant's origin parameter if the option is selected and we're creating more than one layer
707                 if (get_layer_link_origins_flag() && layers_to_create > 1)
708                 {
709                         synfigapp::Action::Handle action(synfigapp::Action::create("LayerParamConnect"));
710                         assert(action);
711
712                         action->set_param("canvas",get_canvas());
713                         action->set_param("canvas_interface",get_canvas_interface());
714                         action->set_param("layer",layer);
715                         if(!action->set_param("param",String("origin")))
716                                 synfig::error("LayerParamConnect didn't like \"param\"");
717                         if(!action->set_param("value_node",ValueNode::Handle(value_node_origin)))
718                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
719
720                         if(!get_canvas_interface()->get_instance()->perform_action(action))
721                         {
722                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
723                                 group.cancel();
724                                 throw String(_("Unable to create Plant layer"));
725                                 return;
726                         }
727                 }
728         }
729
730         ///////////////////////////////////////////////////////////////////////////
731         //   R E G I O N
732         ///////////////////////////////////////////////////////////////////////////
733
734         if(get_layer_region_flag())
735         {
736                 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
737
738                 Layer::Handle layer(get_canvas_interface()->add_layer_to("region",canvas,depth));
739                 if (!layer)
740                 {
741                         get_canvas_view()->get_ui_interface()->error(_("Unable to create layer"));
742                         group.cancel();
743                         return;
744                 }
745                 layer_selection.push_back(layer);
746                 layer->set_description(get_id()+_(" Region"));
747                 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
748
749                 layer->set_param("feather",get_feather());
750                 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
751
752                 layer->set_param("invert",get_invert());
753                 get_canvas_interface()->signal_layer_param_changed()(layer,"invert");
754
755                 // I don't know if it's safe to reuse the same LayerParamConnect action, so I'm
756                 // using 2 separate ones.
757                 {
758                         synfigapp::Action::Handle action(synfigapp::Action::create("LayerParamConnect"));
759                         assert(action);
760
761                         action->set_param("canvas",get_canvas());
762                         action->set_param("canvas_interface",get_canvas_interface());
763                         action->set_param("layer",layer);
764                         if(!action->set_param("param",String("bline")))
765                                 synfig::error("LayerParamConnect didn't like \"param\"");
766                         if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
767                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
768
769                         if(!get_canvas_interface()->get_instance()->perform_action(action))
770                         {
771                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create Region layer"));
772                                 group.cancel();
773                                 throw String(_("Unable to create Region layer"));
774                                 return;
775                         }
776                 }
777
778                 // only link the region's origin parameter if the option is selected and we're creating more than one layer
779                 if (get_layer_link_origins_flag() && layers_to_create > 1)
780                 {
781                         synfigapp::Action::Handle action(synfigapp::Action::create("LayerParamConnect"));
782                         assert(action);
783
784                         action->set_param("canvas",get_canvas());
785                         action->set_param("canvas_interface",get_canvas_interface());
786                         action->set_param("layer",layer);
787                         if(!action->set_param("param",String("origin")))
788                                 synfig::error("LayerParamConnect didn't like \"param\"");
789                         if(!action->set_param("value_node",ValueNode::Handle(value_node_origin)))
790                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
791
792                         if(!get_canvas_interface()->get_instance()->perform_action(action))
793                         {
794                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create Region layer"));
795                                 group.cancel();
796                                 throw String(_("Unable to create Region layer"));
797                                 return;
798                         }
799                 }
800         }
801
802         ///////////////////////////////////////////////////////////////////////////
803         //   O U T L I N E
804         ///////////////////////////////////////////////////////////////////////////
805
806         if (get_layer_outline_flag())
807         {
808                 Layer::Handle layer(get_canvas_interface()->add_layer_to("outline",canvas,depth));
809                 if (!layer)
810                 {
811                         get_canvas_view()->get_ui_interface()->error(_("Unable to create layer"));
812                         group.cancel();
813                         return;
814                 }
815                 layer_selection.push_back(layer);
816                 layer->set_description(get_id()+_(" Outline"));
817                 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
818
819                 layer->set_param("feather",get_feather());
820                 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
821
822                 layer->set_param("invert",get_invert());
823                 get_canvas_interface()->signal_layer_param_changed()(layer,"invert");
824
825                 {
826                         synfigapp::Action::Handle action(synfigapp::Action::create("LayerParamConnect"));
827                         assert(action);
828
829                         action->set_param("canvas",get_canvas());
830                         action->set_param("canvas_interface",get_canvas_interface());
831                         action->set_param("layer",layer);
832                         if(!action->set_param("param",String("bline")))
833                                 synfig::error("LayerParamConnect didn't like \"param\"");
834                         if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
835                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
836
837                         if(!get_canvas_interface()->get_instance()->perform_action(action))
838                         {
839                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
840                                 group.cancel();
841                                 throw String(_("Unable to create Outline layer"));
842                                 return;
843                         }
844                 }
845
846                 // only link the outline's origin parameter if the option is selected and we're creating more than one layer
847                 if (get_layer_link_origins_flag() && layers_to_create > 1)
848                 {
849                         synfigapp::Action::Handle action(synfigapp::Action::create("LayerParamConnect"));
850                         assert(action);
851
852                         action->set_param("canvas",get_canvas());
853                         action->set_param("canvas_interface",get_canvas_interface());
854                         action->set_param("layer",layer);
855                         if(!action->set_param("param",String("origin")))
856                                 synfig::error("LayerParamConnect didn't like \"param\"");
857                         if(!action->set_param("value_node",ValueNode::Handle(value_node_origin)))
858                                 synfig::error("LayerParamConnect didn't like \"value_node\"");
859
860                         if(!get_canvas_interface()->get_instance()->perform_action(action))
861                         {
862                                 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
863                                 group.cancel();
864                                 throw String(_("Unable to create Outline layer"));
865                                 return;
866                         }
867                 }
868         }
869
870         egress_on_selection_change=false;
871         get_canvas_interface()->get_selection_manager()->clear_selected_layers();
872         get_canvas_interface()->get_selection_manager()->set_selected_layers(layer_selection);
873         egress_on_selection_change=true;
874
875         //post clean up stuff...
876         reset();
877         increment_id();
878 }
879
880 Smach::event_result
881 StatePolygon_Context::event_mouse_click_handler(const Smach::event& x)
882 {
883         synfig::info("STATE ROTOPOLYGON: Received mouse button down Event");
884         const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
885         switch(event.button)
886         {
887         case BUTTON_LEFT:
888                 polygon_point_list.push_back(get_work_area()->snap_point_to_grid(event.pos));
889                 refresh_ducks();
890                 return Smach::RESULT_ACCEPT;
891
892         default:
893                 return Smach::RESULT_OK;
894         }
895 }
896
897
898 void
899 StatePolygon_Context::refresh_ducks()
900 {
901         get_work_area()->clear_ducks();
902
903         if(polygon_point_list.empty()) return;
904
905         std::list<synfig::Point>::iterator iter=polygon_point_list.begin();
906
907         etl::handle<WorkArea::Duck> duck;
908         duck=new WorkArea::Duck(*iter);
909         duck->set_editable(true);
910         duck->signal_edited().connect(
911                 sigc::bind(sigc::mem_fun(*this,&studio::StatePolygon_Context::on_polygon_duck_change),iter)
912         );
913         duck->signal_user_click(0).connect(sigc::mem_fun(*this,&StatePolygon_Context::run));
914
915         get_work_area()->add_duck(duck);
916
917         for(++iter;iter!=polygon_point_list.end();++iter)
918         {
919                 etl::handle<WorkArea::Bezier> bezier(new WorkArea::Bezier());
920                 bezier->p1=bezier->c1=duck;
921
922                 duck=new WorkArea::Duck(*iter);
923                 duck->set_editable(true);
924                 duck->set_name(strprintf("%x",&*iter));
925                 duck->signal_edited().connect(
926                         sigc::bind(sigc::mem_fun(*this,&studio::StatePolygon_Context::on_polygon_duck_change),iter)
927                 );
928
929                 get_work_area()->add_duck(duck);
930
931                 bezier->p2=bezier->c2=duck;
932                 get_work_area()->add_bezier(bezier);
933         }
934         get_work_area()->queue_draw();
935 }
936
937
938 bool
939 StatePolygon_Context::on_polygon_duck_change(const synfig::Point &point, std::list<synfig::Point>::iterator iter)
940 {
941         *iter=point;
942         return true;
943 }