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