Add checkboxes to create outlines, regions, etc. for the polygon tool. They're not...
[synfig.git] / synfig-studio / trunk / src / gtkmm / state_polygon.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file state_polygon.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007, 2008 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include <gtkmm/dialog.h>
34 #include <gtkmm/entry.h>
35
36 #include <synfig/valuenode_dynamiclist.h>
37 #include <synfigapp/action_system.h>
38
39 #include "state_polygon.h"
40 #include "canvasview.h"
41 #include "workarea.h"
42 #include "app.h"
43
44 #include <synfigapp/action.h>
45 #include "event_mouse.h"
46 #include "event_layerclick.h"
47 #include "toolbox.h"
48 #include "dialog_tooloptions.h"
49 #include <synfigapp/main.h>
50
51 #include "general.h"
52
53 #endif
54
55 /* === U S I N G =========================================================== */
56
57 using namespace std;
58 using namespace etl;
59 using namespace synfig;
60 using namespace studio;
61
62 /* === M A C R O S ========================================================= */
63
64 /* === G L O B A L S ======================================================= */
65
66 StatePolygon studio::state_polygon;
67
68 /* === C L A S S E S & S T R U C T S ======================================= */
69
70 class studio::StatePolygon_Context : public sigc::trackable
71 {
72         etl::handle<CanvasView> canvas_view_;
73         CanvasView::IsWorking is_working;
74
75         bool prev_table_status;
76         bool prev_workarea_layer_status_;
77
78         Gtk::Menu menu;
79
80         Duckmatic::Push duckmatic_push;
81
82         std::list<synfig::Point> polygon_point_list;
83         synfigapp::Settings& settings;
84
85
86         bool on_polygon_duck_change(const synfig::Point &point, std::list<synfig::Point>::iterator iter);
87
88
89         void popup_handle_menu(synfigapp::ValueDesc value_desc);
90
91
92         void refresh_ducks();
93
94         Gtk::Table options_table;
95         Gtk::Entry entry_id;
96         Gtk::CheckButton checkbutton_invert;
97         Gtk::CheckButton checkbutton_layer_polygon;
98         Gtk::CheckButton checkbutton_layer_region;
99         Gtk::CheckButton checkbutton_layer_outline;
100         Gtk::CheckButton checkbutton_layer_curve_gradient;
101         Gtk::CheckButton checkbutton_layer_plant;
102         Gtk::CheckButton checkbutton_layer_link_origins;
103         Gtk::Button button_make;
104         Gtk::Adjustment  adj_feather;
105         Gtk::SpinButton  spin_feather;
106
107 public:
108
109         // this counts the layers we create - they all have origins we can link
110         int layers_to_create()const
111         {
112                 return
113                         get_layer_polygon_flag() +
114                         get_layer_region_flag() +
115                         get_layer_outline_flag() +
116                         get_layer_curve_gradient_flag() +
117                         get_layer_plant_flag();
118         }
119
120         synfig::String get_id()const { return entry_id.get_text(); }
121         void set_id(const synfig::String& x) { return entry_id.set_text(x); }
122
123         bool get_invert()const { return checkbutton_invert.get_active(); }
124         void set_invert(bool i) { checkbutton_invert.set_active(i); }
125
126         bool get_layer_polygon_flag()const { return checkbutton_layer_polygon.get_active(); }
127         void set_layer_polygon_flag(bool x) { return checkbutton_layer_polygon.set_active(x); }
128
129         bool get_layer_region_flag()const { return checkbutton_layer_region.get_active(); }
130         void set_layer_region_flag(bool x) { return checkbutton_layer_region.set_active(x); }
131
132         bool get_layer_outline_flag()const { return checkbutton_layer_outline.get_active(); }
133         void set_layer_outline_flag(bool x) { return checkbutton_layer_outline.set_active(x); }
134
135         bool get_layer_curve_gradient_flag()const { return checkbutton_layer_curve_gradient.get_active(); }
136         void set_layer_curve_gradient_flag(bool x) { return checkbutton_layer_curve_gradient.set_active(x); }
137
138         bool get_layer_plant_flag()const { return checkbutton_layer_plant.get_active(); }
139         void set_layer_plant_flag(bool x) { return checkbutton_layer_plant.set_active(x); }
140
141         bool get_layer_link_origins_flag()const { return checkbutton_layer_link_origins.get_active(); }
142         void set_layer_link_origins_flag(bool x) { return checkbutton_layer_link_origins.set_active(x); }
143
144         Real get_feather() const { return adj_feather.get_value(); }
145         void set_feather(Real x) { return adj_feather.set_value(x); }
146
147         Smach::event_result event_stop_handler(const Smach::event& x);
148
149         Smach::event_result event_refresh_handler(const Smach::event& x);
150
151         Smach::event_result event_mouse_click_handler(const Smach::event& x);
152         Smach::event_result event_refresh_tool_options(const Smach::event& x);
153         void refresh_tool_options();
154
155         StatePolygon_Context(CanvasView* canvas_view);
156
157         ~StatePolygon_Context();
158
159         const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
160         etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
161         synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
162         WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
163
164         //void on_user_click(synfig::Point point);
165         void load_settings();
166         void save_settings();
167         void reset();
168         void increment_id();
169         bool egress_on_selection_change;
170         Smach::event_result event_layer_selection_changed_handler(const Smach::event& /*x*/)
171         {
172                 if(egress_on_selection_change)
173                         throw Smach::egress_exception();
174                 return Smach::RESULT_OK;
175         }
176
177         void run();
178 };      // END of class StatePolygon_Context
179
180 /* === M E T H O D S ======================================================= */
181
182 StatePolygon::StatePolygon():
183         Smach::state<StatePolygon_Context>("polygon")
184 {
185         insert(event_def(EVENT_LAYER_SELECTION_CHANGED,&StatePolygon_Context::event_layer_selection_changed_handler));
186         insert(event_def(EVENT_STOP,&StatePolygon_Context::event_stop_handler));
187         insert(event_def(EVENT_REFRESH,&StatePolygon_Context::event_refresh_handler));
188         insert(event_def(EVENT_REFRESH_DUCKS,&StatePolygon_Context::event_refresh_handler));
189         insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StatePolygon_Context::event_mouse_click_handler));
190         insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StatePolygon_Context::event_refresh_tool_options));
191 }
192
193 StatePolygon::~StatePolygon()
194 {
195 }
196
197 void
198 StatePolygon_Context::load_settings()
199 {
200         String value;
201
202         if(settings.get_value("polygon.id",value))
203                 set_id(value);
204         else
205                 set_id("Polygon");
206
207         if(settings.get_value("polygon.invert",value) && value != "0")
208                 set_invert(true);
209         else
210                 set_invert(false);
211
212         if(settings.get_value("polygon.layer_polygon",value) && value=="0")
213                 set_layer_polygon_flag(false);
214         else
215                 set_layer_polygon_flag(true);
216
217         if(settings.get_value("polygon.layer_region",value) && value=="1")
218                 set_layer_region_flag(true);
219         else
220                 set_layer_region_flag(false);
221
222         if(settings.get_value("polygon.layer_outline",value) && value=="1")
223                 set_layer_outline_flag(true);
224         else
225                 set_layer_outline_flag(false);
226
227         if(settings.get_value("polygon.layer_curve_gradient",value) && value=="1")
228                 set_layer_curve_gradient_flag(true);
229         else
230                 set_layer_curve_gradient_flag(false);
231
232         if(settings.get_value("polygon.layer_plant",value) && value=="1")
233                 set_layer_plant_flag(true);
234         else
235                 set_layer_plant_flag(false);
236
237         if(settings.get_value("polygon.layer_link_origins",value) && value=="0")
238                 set_layer_link_origins_flag(false);
239         else
240                 set_layer_link_origins_flag(true);
241
242         if(settings.get_value("polygon.feather",value))
243         {
244                 Real n = atof(value.c_str());
245                 set_feather(n);
246         }
247 }
248
249 void
250 StatePolygon_Context::save_settings()
251 {
252         settings.set_value("polygon.id",get_id().c_str());
253         settings.set_value("polygon.invert",get_invert()?"1":"0");
254         settings.set_value("polygon.layer_polygon",get_layer_polygon_flag()?"1":"0");
255         settings.set_value("polygon.layer_outline",get_layer_outline_flag()?"1":"0");
256         settings.set_value("polygon.layer_region",get_layer_region_flag()?"1":"0");
257         settings.set_value("polygon.layer_curve_gradient",get_layer_curve_gradient_flag()?"1":"0");
258         settings.set_value("polygon.layer_plant",get_layer_plant_flag()?"1":"0");
259         settings.set_value("polygon.layer_link_origins",get_layer_link_origins_flag()?"1":"0");
260         settings.set_value("polygon.feather",strprintf("%f",get_feather()));
261 }
262
263 void
264 StatePolygon_Context::reset()
265 {
266         polygon_point_list.clear();
267         refresh_ducks();
268 }
269
270 void
271 StatePolygon_Context::increment_id()
272 {
273         String id(get_id());
274         int number=1;
275         int digits=0;
276
277         if(id.empty())
278                 id="Polygon";
279
280         // If there is a number
281         // already at the end of the
282         // id, then remove it.
283         if(id[id.size()-1]<='9' && id[id.size()-1]>='0')
284         {
285                 // figure out how many digits it is
286                 for (digits = 0;
287                          (int)id.size()-1 >= digits && id[id.size()-1-digits] <= '9' && id[id.size()-1-digits] >= '0';
288                          digits++)
289                         ;
290
291                 String str_number;
292                 str_number=String(id,id.size()-digits,id.size());
293                 id=String(id,0,id.size()-digits);
294
295                 number=atoi(str_number.c_str());
296         }
297         else
298         {
299                 number=1;
300                 digits=3;
301         }
302
303         number++;
304
305         // Add the number back onto the id
306         {
307                 const String format(strprintf("%%0%dd",digits));
308                 id+=strprintf(format.c_str(),number);
309         }
310
311         // Set the ID
312         set_id(id);
313 }
314
315 StatePolygon_Context::StatePolygon_Context(CanvasView* canvas_view):
316         canvas_view_(canvas_view),
317         is_working(*canvas_view),
318         prev_workarea_layer_status_(get_work_area()->get_allow_layer_clicks()),
319         duckmatic_push(get_work_area()),
320         settings(synfigapp::Main::get_selected_input_device()->settings()),
321         entry_id(),
322         checkbutton_invert(_("Invert")),
323         checkbutton_layer_polygon(_("Create Polygon Layer")),
324         checkbutton_layer_region(_("Create Region BLine")),
325         checkbutton_layer_outline(_("Create Outline BLine")),
326         checkbutton_layer_curve_gradient(_("Create Curve Gradient BLine")),
327         checkbutton_layer_plant(_("Create Plant BLine")),
328         checkbutton_layer_link_origins(_("Link Origins")),
329         button_make(_("Make")),
330         adj_feather(0,0,10000,0.01,0.1),
331         spin_feather(adj_feather,0.01,4)
332 {
333         egress_on_selection_change=true;
334         load_settings();
335
336         // Set up the tool options dialog
337         options_table.attach(*manage(new Gtk::Label(_("Polygon Tool"))),        0, 2, 0,  1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
338         options_table.attach(entry_id,                                                                          0, 2, 1,  2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
339
340         options_table.attach(checkbutton_layer_polygon,                                         0, 2, 2,  3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
341         options_table.attach(checkbutton_layer_outline,                                         0, 2, 3,  4, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
342         options_table.attach(checkbutton_layer_region,                                          0, 2, 4,  5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
343         options_table.attach(checkbutton_layer_plant,                                           0, 2, 5,  6, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
344         options_table.attach(checkbutton_layer_curve_gradient,                          0, 2, 6,  7, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
345         options_table.attach(checkbutton_layer_link_origins,                            0, 2, 7,  8, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
346
347         //invert flag
348         options_table.attach(checkbutton_invert,                                                        0, 2, 8,  9, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
349
350         //feather stuff
351         options_table.attach(*manage(new Gtk::Label(_("Feather"))),             0, 1, 9, 10, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
352         options_table.attach(spin_feather,                                                                      1, 2, 9, 10, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
353
354         //options_table.attach(button_make, 0, 2, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
355         button_make.signal_pressed().connect(sigc::mem_fun(*this,&StatePolygon_Context::run));
356         options_table.show_all();
357         refresh_tool_options();
358         App::dialog_tool_options->present();
359
360
361         // Turn off layer clicking
362         get_work_area()->set_allow_layer_clicks(false);
363
364         // clear out the ducks
365         get_work_area()->clear_ducks();
366
367         // Refresh the work area
368         get_work_area()->queue_draw();
369
370         get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
371
372         // Hide the tables if they are showing
373         prev_table_status=get_canvas_view()->tables_are_visible();
374         if(prev_table_status)get_canvas_view()->hide_tables();
375
376         // Disable the time bar
377         get_canvas_view()->set_sensitive_timebar(false);
378
379         // Connect a signal
380         //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StatePolygon_Context::on_user_click));
381
382         App::toolbox->refresh();
383 }
384
385 void
386 StatePolygon_Context::refresh_tool_options()
387 {
388         App::dialog_tool_options->clear();
389         App::dialog_tool_options->set_widget(options_table);
390
391         App::dialog_tool_options->set_local_name(_("Polygon Tool"));
392         App::dialog_tool_options->set_name("polygon");
393
394         App::dialog_tool_options->add_button(
395                 Gtk::StockID("gtk-execute"),
396                 _("Make Polygon")
397         )->signal_clicked().connect(
398                 sigc::mem_fun(
399                         *this,
400                         &StatePolygon_Context::run
401                 )
402         );
403
404         App::dialog_tool_options->add_button(
405                 Gtk::StockID("gtk-clear"),
406                 _("Clear current Polygon")
407         )->signal_clicked().connect(
408                 sigc::mem_fun(
409                         *this,
410                         &StatePolygon_Context::reset
411                 )
412         );
413 }
414
415 Smach::event_result
416 StatePolygon_Context::event_refresh_tool_options(const Smach::event& /*x*/)
417 {
418         refresh_tool_options();
419         return Smach::RESULT_ACCEPT;
420 }
421
422 StatePolygon_Context::~StatePolygon_Context()
423 {
424         run();
425
426         save_settings();
427         // Restore layer clicking
428         get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
429
430         App::dialog_tool_options->clear();
431
432         get_canvas_view()->work_area->reset_cursor();
433
434         // Enable the time bar
435         get_canvas_view()->set_sensitive_timebar(true);
436
437         // Bring back the tables if they were out before
438         if(prev_table_status)get_canvas_view()->show_tables();
439
440         // Refresh the work area
441         get_work_area()->queue_draw();
442
443         App::toolbox->refresh();
444 }
445
446 Smach::event_result
447 StatePolygon_Context::event_stop_handler(const Smach::event& /*x*/)
448 {
449         synfig::info("STATE RotoPolygon: Received Stop Event");
450         //throw Smach::egress_exception();
451         reset();
452         return Smach::RESULT_ACCEPT;
453
454 }
455
456 Smach::event_result
457 StatePolygon_Context::event_refresh_handler(const Smach::event& /*x*/)
458 {
459         synfig::info("STATE RotoPolygon: Received Refresh Event");
460         refresh_ducks();
461         return Smach::RESULT_ACCEPT;
462 }
463
464 void
465 StatePolygon_Context::run()
466 {
467         if(polygon_point_list.empty())
468                 return;
469
470         if(polygon_point_list.size()<3)
471         {
472                 get_canvas_view()->get_ui_interface()->error("You need at least 3 points to create a polygon");
473                 return;
474         }
475                 Layer::Handle layer;
476                 Canvas::Handle canvas(get_canvas_view()->get_canvas());
477                 int depth(0);
478
479                 // we are temporarily using the layer to hold something
480                 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
481                 if(layer)
482                 {
483                         depth=layer->get_depth();
484                         canvas=layer->get_canvas();
485                 }
486
487                 {
488                         synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New Polygon"));
489                         synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
490
491                         Layer::Handle layer(get_canvas_interface()->add_layer_to("polygon",canvas,depth));
492
493                         layer->set_param("invert",get_invert());
494                         get_canvas_interface()->signal_layer_param_changed()(layer,"invert");
495
496                         if(get_feather())
497                         {
498                                 layer->set_param("feather",get_feather());
499                                 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
500                         }
501
502                         layer->set_description(get_id());
503                         get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
504
505                         layer->disconnect_dynamic_param("vector_list");
506                         if(!layer->set_param("vector_list",polygon_point_list))
507                         {
508                                 group.cancel();
509                                 get_canvas_view()->get_ui_interface()->error("Unable to set layer parameter");
510                                 return;
511                         }
512
513                         {
514                                 synfigapp::Action::Handle action(synfigapp::Action::create("value_desc_convert"));
515                                 synfigapp::ValueDesc value_desc(layer,"vector_list");
516                                 action->set_param("canvas",get_canvas());
517                                 action->set_param("canvas_interface",get_canvas_interface());
518                                 action->set_param("value_desc",value_desc);
519                                 action->set_param("type","dynamic_list");
520                                 action->set_param("time",get_canvas_interface()->get_time());
521                                 if(!get_canvas_interface()->get_instance()->perform_action(action))
522                                 {
523                                         group.cancel();
524                                         get_canvas_view()->get_ui_interface()->error("Unable to execute action \"value_desc_convert\"");
525                                         return;
526                                 }
527                         }
528                         egress_on_selection_change=false;
529                         synfigapp::SelectionManager::LayerList layer_selection;
530                         if (!getenv("SYNFIG_TOOLS_CLEAR_SELECTION"))
531                                 layer_selection = get_canvas_view()->get_selection_manager()->get_selected_layers();
532                         get_canvas_interface()->get_selection_manager()->clear_selected_layers();
533                         layer_selection.push_back(layer);
534                         get_canvas_interface()->get_selection_manager()->set_selected_layers(layer_selection);
535                         egress_on_selection_change=true;
536                         //get_canvas_interface()->signal_dirty_preview()();
537                 }
538 /*
539                 else
540                 {
541                         ValueNode::Handle value_node=(ValueNode_Const::create(polygon_point_list));
542                         std::string valuenode_name="Poly";
543                         while(studio::App::dialog_entry("New Polygon", "Please enter the new ID for this value_node",valuenode_name))
544                                 if(get_canvas_interface()->add_value_node(value_node,valuenode_name))
545                                         return true;
546                 }
547 */
548         reset();
549         increment_id();
550 }
551
552 Smach::event_result
553 StatePolygon_Context::event_mouse_click_handler(const Smach::event& x)
554 {
555         synfig::info("STATE ROTOPOLYGON: Received mouse button down Event");
556         const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
557         switch(event.button)
558         {
559         case BUTTON_LEFT:
560                 polygon_point_list.push_back(get_work_area()->snap_point_to_grid(event.pos));
561                 refresh_ducks();
562                 return Smach::RESULT_ACCEPT;
563
564         case BUTTON_RIGHT: // Intercept the right-button click to short-circuit the pop-up menu
565                 if (!getenv("SYNFIG_ENABLE_POPUP_MENU_IN_ALL_TOOLS"))
566                         return Smach::RESULT_ACCEPT;
567
568         default:
569                 return Smach::RESULT_OK;
570         }
571 }
572
573
574 void
575 StatePolygon_Context::refresh_ducks()
576 {
577         get_work_area()->clear_ducks();
578
579         if(polygon_point_list.empty()) return;
580
581         std::list<synfig::Point>::iterator iter=polygon_point_list.begin();
582
583         etl::handle<WorkArea::Duck> duck;
584         duck=new WorkArea::Duck(*iter);
585         duck->set_editable(true);
586         duck->signal_edited().connect(
587                 sigc::bind(sigc::mem_fun(*this,&studio::StatePolygon_Context::on_polygon_duck_change),iter)
588         );
589         duck->signal_user_click(0).connect(sigc::mem_fun(*this,&StatePolygon_Context::run));
590
591         get_work_area()->add_duck(duck);
592
593         for(++iter;iter!=polygon_point_list.end();++iter)
594         {
595                 etl::handle<WorkArea::Bezier> bezier(new WorkArea::Bezier());
596                 bezier->p1=bezier->c1=duck;
597
598                 duck=new WorkArea::Duck(*iter);
599                 duck->set_editable(true);
600                 duck->set_name(strprintf("%x",&*iter));
601                 duck->signal_edited().connect(
602                         sigc::bind(sigc::mem_fun(*this,&studio::StatePolygon_Context::on_polygon_duck_change),iter)
603                 );
604
605                 get_work_area()->add_duck(duck);
606
607                 bezier->p2=bezier->c2=duck;
608                 get_work_area()->add_bezier(bezier);
609         }
610         get_work_area()->queue_draw();
611 }
612
613
614 bool
615 StatePolygon_Context::on_polygon_duck_change(const synfig::Point &point, std::list<synfig::Point>::iterator iter)
616 {
617         *iter=point;
618         return true;
619 }