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