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