1 /* === S Y N F I G ========================================================= */
2 /*! \file state_bline.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007, 2008 Chris Moore
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.
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.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
33 #include <gtkmm/dialog.h>
34 #include <gtkmm/entry.h>
36 #include <synfig/valuenode_dynamiclist.h>
38 #include "state_bline.h"
39 #include "state_normal.h"
40 #include "canvasview.h"
43 #include <synfig/valuenode_bline.h>
44 #include <ETL/hermite>
45 #include <ETL/calculus>
47 #include "event_mouse.h"
48 #include "event_layerclick.h"
50 #include "docks/dialog_tooloptions.h"
51 #include <gtkmm/spinbutton.h>
52 #include <synfig/transform.h>
53 #include <synfigapp/main.h>
59 /* === U S I N G =========================================================== */
63 using namespace synfig;
64 using namespace studio;
66 /* === M A C R O S ========================================================= */
68 // if defined, show the first duck as green while drawing
69 #define DISTINGUISH_FIRST_DUCK
71 /* === G L O B A L S ======================================================= */
73 StateBLine studio::state_bline;
75 /* === C L A S S E S & S T R U C T S ======================================= */
77 class studio::StateBLine_Context : public sigc::trackable
79 etl::handle<CanvasView> canvas_view_;
80 CanvasView::IsWorking is_working;
82 bool prev_table_status;
84 bool prev_workarea_layer_status_;
87 Canvas::Handle canvas;
91 Duckmatic::Push duckmatic_push;
93 etl::handle<Duck> curr_duck;
95 etl::handle<Duck> next_duck;
97 std::list<synfig::ValueNode_Const::Handle> bline_point_list;
98 synfigapp::Settings& settings;
100 bool on_vertex_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node);
101 bool on_tangent1_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node);
102 bool on_tangent2_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node);
105 void popup_handle_menu(synfig::ValueNode_Const::Handle value_node);
106 void popup_vertex_menu(synfig::ValueNode_Const::Handle value_node);
107 void popup_bezier_menu(float location, synfig::ValueNode_Const::Handle value_node);
109 void bline_detach_handle(synfig::ValueNode_Const::Handle value_node);
110 void bline_attach_handle(synfig::ValueNode_Const::Handle value_node);
111 void bline_delete_vertex(synfig::ValueNode_Const::Handle value_node);
112 void bline_insert_vertex(synfig::ValueNode_Const::Handle value_node,float origin=0.5);
116 void refresh_ducks(bool x=true);
118 Gtk::Table options_table;
120 Gtk::CheckButton checkbutton_layer_region;
121 Gtk::CheckButton checkbutton_layer_outline;
122 Gtk::CheckButton checkbutton_layer_curve_gradient;
123 Gtk::CheckButton checkbutton_layer_plant;
124 Gtk::CheckButton checkbutton_layer_link_origins;
125 Gtk::CheckButton checkbutton_auto_export;
126 Gtk::Button button_make;
127 Gtk::Button button_clear;
128 Gtk::Adjustment adj_feather;
129 Gtk::SpinButton spin_feather;
135 int layers_to_create()const
138 get_layer_region_flag() +
139 get_layer_outline_flag() +
140 get_layer_curve_gradient_flag() +
141 get_layer_plant_flag();
146 if(layers_to_create()==0)
147 set_layer_region_flag(true);
150 bool get_auto_export_flag()const { return checkbutton_auto_export.get_active(); }
151 void set_auto_export_flag(bool x) { return checkbutton_auto_export.set_active(x); }
153 bool get_layer_region_flag()const { return checkbutton_layer_region.get_active(); }
154 void set_layer_region_flag(bool x) { return checkbutton_layer_region.set_active(x); }
156 bool get_layer_outline_flag()const { return checkbutton_layer_outline.get_active(); }
157 void set_layer_outline_flag(bool x) { return checkbutton_layer_outline.set_active(x); }
159 bool get_layer_curve_gradient_flag()const { return checkbutton_layer_curve_gradient.get_active(); }
160 void set_layer_curve_gradient_flag(bool x) { return checkbutton_layer_curve_gradient.set_active(x); }
162 bool get_layer_plant_flag()const { return checkbutton_layer_plant.get_active(); }
163 void set_layer_plant_flag(bool x) { return checkbutton_layer_plant.set_active(x); }
165 bool get_layer_link_origins_flag()const { return checkbutton_layer_link_origins.get_active(); }
166 void set_layer_link_origins_flag(bool x) { return checkbutton_layer_link_origins.set_active(x); }
168 Real get_feather() const { return adj_feather.get_value(); }
169 void set_feather(Real x) { return adj_feather.set_value(x); }
170 synfig::String get_id()const { return entry_id.get_text(); }
171 void set_id(const synfig::String& x) { return entry_id.set_text(x); }
173 Smach::event_result event_stop_handler(const Smach::event& x);
175 Smach::event_result event_refresh_handler(const Smach::event& x);
177 Smach::event_result event_mouse_click_handler(const Smach::event& x);
178 Smach::event_result event_mouse_release_handler(const Smach::event& x);
179 Smach::event_result event_mouse_motion_handler(const Smach::event& x);
180 Smach::event_result event_refresh_tool_options(const Smach::event& x);
182 Smach::event_result event_hijack(const Smach::event& /*x*/) { return Smach::RESULT_ACCEPT; }
184 void refresh_tool_options();
186 StateBLine_Context(CanvasView* canvas_view);
188 ~StateBLine_Context();
190 const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
191 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
192 synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
193 WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
194 const synfig::TransformStack& get_transform_stack()const { return canvas_view_->get_curr_transform_stack(); }
196 void load_settings();
197 void save_settings();
200 //void on_user_click(synfig::Point point);
205 bool egress_on_selection_change;
206 Smach::event_result event_layer_selection_changed_handler(const Smach::event& /*x*/)
208 if(egress_on_selection_change)
209 throw &state_normal; //throw Smach::egress_exception();
210 return Smach::RESULT_OK;
213 }; // END of class StateBLine_Context
216 /* === M E T H O D S ======================================================= */
218 StateBLine::StateBLine():
219 Smach::state<StateBLine_Context>("bline")
221 insert(event_def(EVENT_LAYER_SELECTION_CHANGED, &StateBLine_Context::event_layer_selection_changed_handler));
222 insert(event_def(EVENT_STOP, &StateBLine_Context::event_stop_handler));
223 insert(event_def(EVENT_REFRESH, &StateBLine_Context::event_refresh_handler));
224 insert(event_def(EVENT_REFRESH_DUCKS, &StateBLine_Context::event_hijack));
225 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN, &StateBLine_Context::event_mouse_click_handler));
226 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_UP, &StateBLine_Context::event_mouse_release_handler));
227 insert(event_def(EVENT_WORKAREA_MOUSE_MOTION, &StateBLine_Context::event_mouse_motion_handler));
228 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DRAG, &StateBLine_Context::event_mouse_motion_handler));
229 insert(event_def(EVENT_REFRESH_TOOL_OPTIONS, &StateBLine_Context::event_refresh_tool_options));
232 StateBLine::~StateBLine()
237 StateBLine_Context::load_settings()
241 if(settings.get_value("bline.layer_region",value) && value=="0")
242 set_layer_region_flag(false);
244 set_layer_region_flag(true);
246 if(settings.get_value("bline.layer_outline",value) && value=="0")
247 set_layer_outline_flag(false);
249 set_layer_outline_flag(true);
251 if(settings.get_value("bline.layer_curve_gradient",value) && value=="1")
252 set_layer_curve_gradient_flag(true);
254 set_layer_curve_gradient_flag(false);
256 if(settings.get_value("bline.layer_plant",value) && value=="1")
257 set_layer_plant_flag(true);
259 set_layer_plant_flag(false);
261 if(settings.get_value("bline.layer_link_origins",value) && value=="0")
262 set_layer_link_origins_flag(false);
264 set_layer_link_origins_flag(true);
266 if(settings.get_value("bline.auto_export",value) && value=="1")
267 set_auto_export_flag(true);
269 set_auto_export_flag(false);
271 if(settings.get_value("bline.id",value))
276 if(settings.get_value("bline.feather",value))
278 Real n = atof(value.c_str());
286 StateBLine_Context::save_settings()
289 settings.set_value("bline.layer_outline",get_layer_outline_flag()?"1":"0");
290 settings.set_value("bline.layer_region",get_layer_region_flag()?"1":"0");
291 settings.set_value("bline.layer_curve_gradient",get_layer_curve_gradient_flag()?"1":"0");
292 settings.set_value("bline.layer_plant",get_layer_plant_flag()?"1":"0");
293 settings.set_value("bline.layer_link_origins",get_layer_link_origins_flag()?"1":"0");
294 settings.set_value("bline.auto_export",get_auto_export_flag()?"1":"0");
295 settings.set_value("bline.id",get_id().c_str());
296 settings.set_value("bline.feather",strprintf("%f",get_feather()));
300 StateBLine_Context::reset()
303 bline_point_list.clear();
308 StateBLine_Context::increment_id()
317 // If there is a number
318 // already at the end of the
319 // id, then remove it.
320 if(id[id.size()-1]<='9' && id[id.size()-1]>='0')
322 // figure out how many digits it is
324 (int)id.size()-1 >= digits && id[id.size()-1-digits] <= '9' && id[id.size()-1-digits] >= '0';
329 str_number=String(id,id.size()-digits,id.size());
330 id=String(id,0,id.size()-digits);
331 // synfig::info("---------------- \"%s\"",str_number.c_str());
333 number=atoi(str_number.c_str());
343 // Add the number back onto the id
345 const String format(strprintf("%%0%dd",digits));
346 id+=strprintf(format.c_str(),number);
354 StateBLine_Context::StateBLine_Context(CanvasView* canvas_view):
355 canvas_view_(canvas_view),
356 is_working(*canvas_view),
358 prev_workarea_layer_status_(get_work_area()->get_allow_layer_clicks()),
359 duckmatic_push(get_work_area()),
360 settings(synfigapp::Main::get_selected_input_device()->settings()),
362 checkbutton_layer_region(_("Create Region BLine")),
363 checkbutton_layer_outline(_("Create Outline BLine")),
364 checkbutton_layer_curve_gradient(_("Create Curve Gradient BLine")),
365 checkbutton_layer_plant(_("Create Plant BLine")),
366 checkbutton_layer_link_origins(_("Link Origins")),
367 checkbutton_auto_export(_("Auto Export")),
368 button_make(_("Make")),
369 button_clear(_("Clear")),
370 adj_feather(0,0,10000,0.01,0.1),
371 spin_feather(adj_feather,0.01,4)
374 egress_on_selection_change=true;
377 // Set up the tool options dialog
378 options_table.attach(*manage(new Gtk::Label(_("BLine Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
379 options_table.attach(entry_id, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
380 options_table.attach(checkbutton_layer_outline, 0, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
381 options_table.attach(checkbutton_layer_region, 0, 2, 3, 4, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
382 options_table.attach(checkbutton_layer_plant, 0, 2, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
383 options_table.attach(checkbutton_layer_curve_gradient, 0, 2, 5, 6, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
384 options_table.attach(checkbutton_layer_link_origins, 0, 2, 6, 7, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
385 options_table.attach(checkbutton_auto_export, 0, 2, 7, 8, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
386 options_table.attach(*manage(new Gtk::Label(_("Feather"))), 0, 1, 10, 11, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
387 options_table.attach(spin_feather, 1, 2, 10, 11, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
388 //options_table.attach(button_make, 0, 2, 5, 6, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
389 //button_make.signal_pressed().connect(sigc::mem_fun(*this,&StateBLine_Context::run));
390 options_table.show_all();
391 refresh_tool_options();
392 App::dialog_tool_options->present();
394 // Turn off layer clicking
395 get_work_area()->set_allow_layer_clicks(false);
397 // clear out the ducks
398 get_work_area()->clear_ducks();
400 // Refresh the work area
401 get_work_area()->queue_draw();
403 // Hide the tables if they are showing
404 prev_table_status=get_canvas_view()->tables_are_visible();
405 if(prev_table_status)get_canvas_view()->hide_tables();
407 // Disable the time bar
408 get_canvas_view()->set_sensitive_timebar(false);
411 //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StateBLine_Context::on_user_click));
412 get_work_area()->set_cursor(Gdk::CROSSHAIR);
414 App::toolbox->refresh();
418 StateBLine_Context::refresh_tool_options()
420 App::dialog_tool_options->clear();
421 App::dialog_tool_options->set_widget(options_table);
422 App::dialog_tool_options->set_local_name(_("BLine Tool"));
423 App::dialog_tool_options->set_name("bline");
425 App::dialog_tool_options->add_button(
426 Gtk::StockID("gtk-execute"),
427 _("Make BLine and/or Region")
428 )->signal_clicked().connect(
429 sigc::hide_return(sigc::mem_fun(
431 &StateBLine_Context::run
435 App::dialog_tool_options->add_button(
436 Gtk::StockID("gtk-clear"),
437 _("Clear current BLine")
438 )->signal_clicked().connect(
441 &StateBLine_Context::reset
447 StateBLine_Context::event_refresh_tool_options(const Smach::event& /*x*/)
449 refresh_tool_options();
450 return Smach::RESULT_ACCEPT;
453 StateBLine_Context::~StateBLine_Context()
458 App::dialog_tool_options->clear();
460 get_work_area()->reset_cursor();
462 // Restore layer clicking
463 get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
465 // Enable the time bar
466 get_canvas_view()->set_sensitive_timebar(true);
468 // Bring back the tables if they were out before
469 if(prev_table_status)get_canvas_view()->show_tables();
471 // get_canvas_view()->get_smach().process_event(EVENT_REFRESH_DUCKS);
473 // Refresh the work area
474 get_work_area()->queue_draw();
476 App::toolbox->refresh();
480 StateBLine_Context::event_stop_handler(const Smach::event& /*x*/)
482 // synfig::info("STATE RotoBLine: Received Stop Event");
485 // throw Smach::egress_exception();
486 // get_canvas_view()->get_smach().pop_state();
487 return Smach::RESULT_ACCEPT;
491 StateBLine_Context::event_refresh_handler(const Smach::event& /*x*/)
493 // synfig::info("STATE RotoBLine: Received Refresh Event");
495 return Smach::RESULT_ACCEPT;
499 StateBLine_Context::run()
505 for(int i=5;i>0 && !success;i--)try
513 if(!success && !err.empty())
515 get_canvas_view()->get_ui_interface()->error(err);
521 StateBLine_Context::run_()
526 // Now we need to generate it
528 if(bline_point_list.empty())
532 if(bline_point_list.size()<2)
534 //get_canvas_view()->get_ui_interface()->error(_("You need at least two (2) points to create a BLine"));
541 // Create the action group
542 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New BLine"));
544 std::vector<BLinePoint> new_list;
545 std::list<synfig::ValueNode_Const::Handle>::iterator iter;
546 const synfig::TransformStack& transform(get_transform_stack());
548 for(iter=bline_point_list.begin();iter!=bline_point_list.end();++iter)
550 BLinePoint bline_point((*iter)->get_value().get(BLinePoint()));
551 Point new_vertex(transform.unperform(bline_point.get_vertex()));
553 bline_point.set_tangent1(
555 bline_point.get_tangent1()+bline_point.get_vertex()
559 bline_point.set_tangent2(
561 bline_point.get_tangent2()+bline_point.get_vertex()
565 bline_point.set_vertex(new_vertex);
567 new_list.push_back(bline_point);
570 ValueNode_BLine::Handle value_node_bline(ValueNode_BLine::create(new_list));
571 assert(value_node_bline);
573 ValueNode_Const::Handle value_node_origin(ValueNode_Const::create(Vector()));
574 assert(value_node_origin);
576 // Set the looping flag
577 value_node_bline->set_loop(loop_);
579 // Add the BLine to the canvas
580 if(get_auto_export_flag() && !get_canvas_interface()->add_value_node(value_node_bline,get_id()))
582 //get_canvas_view()->get_ui_interface()->error(_("Unable to add value node"));
585 throw String(_("Unable to add value node"));
591 // we are temporarily using the layer to hold something
592 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
597 depth=layer->get_depth();
599 canvas=layer->get_canvas();
605 canvas=get_canvas_view()->get_canvas();
607 value_node_bline->set_member_canvas(canvas);
609 synfigapp::SelectionManager::LayerList layer_selection;
610 if (!getenv("SYNFIG_TOOLS_CLEAR_SELECTION"))
611 layer_selection = get_canvas_view()->get_selection_manager()->get_selected_layers();
613 // count how many layers we're going to be creating
614 int layers_to_create = this->layers_to_create();
616 ///////////////////////////////////////////////////////////////////////////
617 // C U R V E G R A D I E N T
618 ///////////////////////////////////////////////////////////////////////////
620 if(get_layer_curve_gradient_flag())
622 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
624 Layer::Handle layer(get_canvas_interface()->add_layer_to("curve_gradient",canvas,depth));
628 throw String(_("Unable to create layer"));
630 layer_selection.push_back(layer);
631 layer->set_description(get_id()+_(" Gradient"));
632 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
635 synfigapp::Action::Handle action(synfigapp::Action::create("LayerParamConnect"));
638 action->set_param("canvas",get_canvas());
639 action->set_param("canvas_interface",get_canvas_interface());
640 action->set_param("layer",layer);
641 if(!action->set_param("param",String("bline")))
642 synfig::error("LayerParamConnect didn't like \"param\"");
643 if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
644 synfig::error("LayerParamConnect didn't like \"value_node\"");
646 if(!get_canvas_interface()->get_instance()->perform_action(action))
648 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
650 throw String(_("Unable to create Gradient layer"));
655 // only link the curve gradient's origin parameter if the option is selected and we're creating more than one layer
656 if (get_layer_link_origins_flag() && layers_to_create > 1)
658 synfigapp::Action::Handle action(synfigapp::Action::create("LayerParamConnect"));
661 action->set_param("canvas",get_canvas());
662 action->set_param("canvas_interface",get_canvas_interface());
663 action->set_param("layer",layer);
664 if(!action->set_param("param",String("origin")))
665 synfig::error("LayerParamConnect didn't like \"param\"");
666 if(!action->set_param("value_node",ValueNode::Handle(value_node_origin)))
667 synfig::error("LayerParamConnect didn't like \"value_node\"");
669 if(!get_canvas_interface()->get_instance()->perform_action(action))
671 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
673 throw String(_("Unable to create Gradient layer"));
679 ///////////////////////////////////////////////////////////////////////////
681 ///////////////////////////////////////////////////////////////////////////
683 if(get_layer_plant_flag())
685 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
687 Layer::Handle layer(get_canvas_interface()->add_layer_to("plant",canvas,depth));
691 throw String(_("Unable to create layer"));
693 layer_selection.push_back(layer);
694 layer->set_description(get_id()+_(" Plant"));
695 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
698 synfigapp::Action::Handle action(synfigapp::Action::create("LayerParamConnect"));
701 action->set_param("canvas",get_canvas());
702 action->set_param("canvas_interface",get_canvas_interface());
703 action->set_param("layer",layer);
704 if(!action->set_param("param",String("bline")))
705 synfig::error("LayerParamConnect didn't like \"param\"");
706 if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
707 synfig::error("LayerParamConnect didn't like \"value_node\"");
709 if(!get_canvas_interface()->get_instance()->perform_action(action))
711 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
713 throw String(_("Unable to create Plant layer"));
718 // only link the plant's origin parameter if the option is selected and we're creating more than one layer
719 if (get_layer_link_origins_flag() && layers_to_create > 1)
721 synfigapp::Action::Handle action(synfigapp::Action::create("LayerParamConnect"));
724 action->set_param("canvas",get_canvas());
725 action->set_param("canvas_interface",get_canvas_interface());
726 action->set_param("layer",layer);
727 if(!action->set_param("param",String("origin")))
728 synfig::error("LayerParamConnect didn't like \"param\"");
729 if(!action->set_param("value_node",ValueNode::Handle(value_node_origin)))
730 synfig::error("LayerParamConnect didn't like \"value_node\"");
732 if(!get_canvas_interface()->get_instance()->perform_action(action))
734 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
736 throw String(_("Unable to create Plant layer"));
742 ///////////////////////////////////////////////////////////////////////////
744 ///////////////////////////////////////////////////////////////////////////
746 if(get_layer_region_flag())
748 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
750 Layer::Handle layer(get_canvas_interface()->add_layer_to("region",canvas,depth));
754 throw String(_("Unable to create layer"));
756 layer_selection.push_back(layer);
757 layer->set_description(get_id()+_(" Region"));
758 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
762 layer->set_param("feather",get_feather());
763 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
766 // I don't know if it's safe to reuse the same LayerParamConnect action, so I'm
767 // using 2 separate ones.
769 synfigapp::Action::Handle action(synfigapp::Action::create("LayerParamConnect"));
772 action->set_param("canvas",get_canvas());
773 action->set_param("canvas_interface",get_canvas_interface());
774 action->set_param("layer",layer);
775 if(!action->set_param("param",String("bline")))
776 synfig::error("LayerParamConnect didn't like \"param\"");
777 if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
778 synfig::error("LayerParamConnect didn't like \"value_node\"");
780 if(!get_canvas_interface()->get_instance()->perform_action(action))
782 //get_canvas_view()->get_ui_interface()->error(_("Unable to create Region layer"));
784 throw String(_("Unable to create Region layer"));
789 // only link the region's origin parameter if the option is selected and we're creating more than one layer
790 if (get_layer_link_origins_flag() && layers_to_create > 1)
792 synfigapp::Action::Handle action(synfigapp::Action::create("LayerParamConnect"));
795 action->set_param("canvas",get_canvas());
796 action->set_param("canvas_interface",get_canvas_interface());
797 action->set_param("layer",layer);
798 if(!action->set_param("param",String("origin")))
799 synfig::error("LayerParamConnect didn't like \"param\"");
800 if(!action->set_param("value_node",ValueNode::Handle(value_node_origin)))
801 synfig::error("LayerParamConnect didn't like \"value_node\"");
803 if(!get_canvas_interface()->get_instance()->perform_action(action))
805 //get_canvas_view()->get_ui_interface()->error(_("Unable to create Region layer"));
807 throw String(_("Unable to create Region layer"));
813 ///////////////////////////////////////////////////////////////////////////
815 ///////////////////////////////////////////////////////////////////////////
817 if(get_layer_outline_flag())
819 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
821 Layer::Handle layer(get_canvas_interface()->add_layer_to("outline",canvas,depth));
825 throw String(_("Unable to create layer"));
827 layer_selection.push_back(layer);
828 layer->set_description(get_id()+_(" Outline"));
829 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
832 layer->set_param("feather",get_feather());
833 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
837 synfigapp::Action::Handle action(synfigapp::Action::create("LayerParamConnect"));
840 action->set_param("canvas",get_canvas());
841 action->set_param("canvas_interface",get_canvas_interface());
842 action->set_param("layer",layer);
843 if(!action->set_param("param",String("bline")))
844 synfig::error("LayerParamConnect didn't like \"param\"");
845 if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
846 synfig::error("LayerParamConnect didn't like \"value_node\"");
848 if(!get_canvas_interface()->get_instance()->perform_action(action))
850 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
852 throw String(_("Unable to create Outline layer"));
857 // only link the outline's origin parameter if the option is selected and we're creating more than one layer
858 if (get_layer_link_origins_flag() && layers_to_create > 1)
860 synfigapp::Action::Handle action(synfigapp::Action::create("LayerParamConnect"));
863 action->set_param("canvas",get_canvas());
864 action->set_param("canvas_interface",get_canvas_interface());
865 action->set_param("layer",layer);
866 if(!action->set_param("param",String("origin")))
867 synfig::error("LayerParamConnect didn't like \"param\"");
868 if(!action->set_param("value_node",ValueNode::Handle(value_node_origin)))
869 synfig::error("LayerParamConnect didn't like \"value_node\"");
871 if(!get_canvas_interface()->get_instance()->perform_action(action))
873 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
875 throw String(_("Unable to create Outline layer"));
881 egress_on_selection_change=false;
882 get_canvas_interface()->get_selection_manager()->clear_selected_layers();
883 get_canvas_interface()->get_selection_manager()->set_selected_layers(layer_selection);
884 egress_on_selection_change=true;
886 //if(finish_bline_dialog.get_region_flag() || finish_bline_dialog.get_bline_flag())
887 // get_canvas_interface()->signal_dirty_preview()();
897 StateBLine_Context::event_mouse_motion_handler(const Smach::event& x)
899 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
903 //synfig::info("Moved Duck");
904 Point p(get_work_area()->snap_point_to_grid(event.pos));
905 curr_duck->set_trans_point(p);
907 next_duck->set_trans_point(p);
908 get_work_area()->queue_draw();
909 return Smach::RESULT_ACCEPT;
912 return Smach::RESULT_OK;
916 StateBLine_Context::event_mouse_release_handler(const Smach::event& /*x*/)
920 //synfig::info("Released current duck");
921 curr_duck->signal_edited()(curr_duck->get_point());
924 //synfig::info("grabbing next duck");
928 return Smach::RESULT_ACCEPT;
930 return Smach::RESULT_OK;
934 StateBLine_Context::event_mouse_click_handler(const Smach::event& x)
936 // synfig::info("STATE BLINE: Received mouse button down Event");
937 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
942 // If we are already looped up, then don't try to add anything else
944 return Smach::RESULT_OK;
946 BLinePoint bline_point;
948 bline_point.set_vertex(get_work_area()->snap_point_to_grid(event.pos));
949 //bline_point.set_width(synfigapp::Main::get_bline_width());
950 bline_point.set_width(1.0f);
951 bline_point.set_origin(0.5f);
952 bline_point.set_split_tangent_flag(false);
953 bline_point.set_tangent1(Vector(0,0));
957 if(bline_point_list.empty())
959 bline_point.set_tangent1(Vector(1,1));
963 const Vector t(event.pos-bline_point_list.back()->get_value().get(BLinePoint()).get_vertex());
964 bline_point.set_tangent1(t);
967 if(bline_point_list.size()>1)
969 std::list<synfig::ValueNode_Const::Handle>::iterator iter;
970 iter=bline_point_list.end();
972 BLinePoint prev(bline_point_list.back()->get_value().get(BLinePoint()));
973 prev.set_tangent1(event.pos-(*iter)->get_value().get(BLinePoint()).get_vertex());
974 bline_point_list.back()->set_value(prev);
978 bline_point_list.push_back(ValueNode_Const::create(bline_point));
981 return Smach::RESULT_ACCEPT;
985 return Smach::RESULT_OK;
990 StateBLine_Context::refresh_ducks(bool button_down)
992 get_work_area()->clear_ducks();
993 get_work_area()->queue_draw();
995 if(bline_point_list.empty())
998 list<ValueNode_Const::Handle>::iterator iter;
1000 handle<WorkArea::Bezier> bezier;
1001 handle<WorkArea::Duck> duck,tduck;
1002 BLinePoint bline_point;
1004 for(iter=bline_point_list.begin();iter!=bline_point_list.end();++iter)
1006 ValueNode_Const::Handle value_node(*iter);
1007 bline_point=(value_node->get_value().get(BLinePoint()));
1011 // First add the duck associated with this vertex
1012 duck=new WorkArea::Duck(bline_point.get_vertex());
1013 duck->set_editable(true);
1014 #ifdef DISTINGUISH_FIRST_DUCK
1015 if (iter!=bline_point_list.begin())
1016 duck->set_type(Duck::TYPE_VERTEX);
1018 duck->set_type(Duck::TYPE_VERTEX);
1020 duck->set_name(strprintf("%x-vertex",value_node.get()));
1021 duck->signal_edited().connect(
1022 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_vertex_change),value_node)
1024 duck->signal_user_click(2).connect(
1025 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::popup_vertex_menu),value_node)
1027 duck->set_guid(value_node->get_guid()^synfig::GUID::hasher(0));
1029 get_work_area()->add_duck(duck);
1031 // Add the tangent1 duck
1032 tduck=new WorkArea::Duck(bline_point.get_tangent1());
1033 tduck->set_editable(true);
1034 tduck->set_name(strprintf("%x-tangent1",value_node.get()));
1035 tduck->set_origin(duck);
1036 tduck->set_scalar(-0.33333333333333333);
1037 tduck->set_tangent(true);
1038 tduck->set_guid(value_node->get_guid()^synfig::GUID::hasher(3));
1039 tduck->signal_edited().connect(
1040 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_tangent1_change),value_node)
1042 tduck->signal_user_click(2).connect(
1043 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::popup_handle_menu),value_node)
1046 // See if we need to add that duck to the previous bezier
1049 get_work_area()->add_duck(tduck);
1053 bezier->signal_user_click(2).connect(
1057 &studio::StateBLine_Context::popup_bezier_menu
1063 //get_work_area()->add_duck(bezier->c1);
1064 //get_work_area()->add_duck(bezier->c2);
1065 get_work_area()->add_bezier(bezier);
1070 // Now we see if we need to create a bezier
1071 list<ValueNode_Const::Handle>::iterator next(iter);
1074 // If our next iterator is the end, then we don't need
1076 //if(next==bline_point_list.end() && !loop_)
1079 bezier=new WorkArea::Bezier();
1081 // Add the tangent2 duck
1082 tduck=new WorkArea::Duck(bline_point.get_tangent2());
1083 tduck->set_editable(true);
1084 tduck->set_origin(duck);
1085 tduck->set_scalar(0.33333333333333333);
1086 tduck->set_tangent(true);
1087 if(bline_point.get_split_tangent_flag())
1089 tduck->set_name(strprintf("%x-tangent2",value_node.get()));
1090 tduck->signal_edited().connect(
1091 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_tangent2_change),value_node)
1096 tduck->set_name(strprintf("%x-tangent1",value_node.get()));
1097 tduck->signal_edited().connect(
1098 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_tangent1_change),value_node)
1101 tduck->set_guid(value_node->get_guid()^synfig::GUID::hasher(4));
1102 tduck->signal_user_click(2).connect(
1103 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::popup_handle_menu),value_node)
1106 // Setup the next bezier
1110 get_work_area()->add_duck(tduck);
1114 // Add the loop, if requested
1118 BLinePoint bline_point(bline_point_list.front()->get_value().get(BLinePoint()));
1120 duck=new WorkArea::Duck(bline_point.get_vertex());
1121 duck->set_editable(true);
1122 #ifndef DISTINGUISH_FIRST_DUCK
1123 duck->set_type(Duck::TYPE_VERTEX);
1125 duck->set_name(strprintf("%x-vertex",bline_point_list.front().get()));
1126 duck->signal_edited().connect(
1127 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_vertex_change),bline_point_list.front())
1129 duck->signal_user_click(2).connect(
1130 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::popup_vertex_menu),bline_point_list.front())
1132 get_work_area()->add_duck(duck);
1134 // Add the tangent1 duck
1135 tduck=new WorkArea::Duck(bline_point.get_tangent1());
1136 tduck->set_editable(true);
1137 tduck->set_name(strprintf("%x-tangent1",bline_point_list.front().get()));
1138 tduck->set_origin(duck);
1139 tduck->set_scalar(-0.33333333333333333);
1140 tduck->set_tangent(true);
1141 tduck->signal_edited().connect(
1142 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_tangent1_change),bline_point_list.front())
1144 tduck->signal_user_click(2).connect(
1145 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::popup_handle_menu),bline_point_list.front())
1147 get_work_area()->add_duck(tduck);
1152 bezier->signal_user_click(2).connect(
1156 &studio::StateBLine_Context::popup_bezier_menu
1158 bline_point_list.front()
1162 //get_work_area()->add_duck(bezier->c1);
1163 get_work_area()->add_bezier(bezier);
1165 if(bezier && !loop_)
1167 duck=new WorkArea::Duck(bline_point.get_vertex());
1168 duck->set_ignore(true);
1169 duck->set_name("temp");
1171 // Add the tangent1 duck
1172 tduck=new WorkArea::Duck(Vector(0,0));
1173 tduck->set_ignore(true);
1174 tduck->set_name("ttemp");
1175 tduck->set_origin(duck);
1176 tduck->set_scalar(-0.33333333333333333);
1178 tduck->set_tangent(true);
1182 get_work_area()->add_duck(bezier->p2);
1183 //get_work_area()->add_duck(bezier->c2);
1184 get_work_area()->add_bezier(bezier);
1186 duck->set_guid(synfig::GUID());
1187 tduck->set_guid(synfig::GUID());
1198 curr_duck=next_duck;
1203 get_work_area()->queue_draw();
1208 StateBLine_Context::on_vertex_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node)
1210 BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1211 bline_point.set_vertex(point);
1212 value_node->set_value(bline_point);
1218 StateBLine_Context::on_tangent1_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node)
1220 BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1221 bline_point.set_tangent1(point);
1222 value_node->set_value(bline_point);
1228 StateBLine_Context::on_tangent2_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node)
1230 BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1231 bline_point.set_tangent2(point);
1232 value_node->set_value(bline_point);
1238 StateBLine_Context::loop_bline()
1242 refresh_ducks(false);
1246 StateBLine_Context::unloop_bline()
1250 refresh_ducks(false);
1254 StateBLine_Context::popup_vertex_menu(synfig::ValueNode_Const::Handle value_node)
1256 menu.items().clear();
1260 menu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Unloop BLine"),
1261 sigc::mem_fun(*this,&studio::StateBLine_Context::unloop_bline)
1264 menu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Loop BLine"),
1265 sigc::mem_fun(*this,&studio::StateBLine_Context::loop_bline)
1269 menu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Delete Vertex"),
1271 sigc::mem_fun(*this,&studio::StateBLine_Context::bline_delete_vertex),
1280 StateBLine_Context::popup_bezier_menu(float location, synfig::ValueNode_Const::Handle value_node)
1282 menu.items().clear();
1284 menu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Insert Vertex"),
1287 sigc::mem_fun(*this,&studio::StateBLine_Context::bline_insert_vertex),
1298 StateBLine_Context::bline_insert_vertex(synfig::ValueNode_Const::Handle value_node, float origin)
1300 list<ValueNode_Const::Handle>::iterator iter;
1302 for(iter=bline_point_list.begin();iter!=bline_point_list.end();++iter)
1303 if(*iter==value_node)
1305 BLinePoint bline_point;
1306 BLinePoint next_bline_point((*iter)->get_value().get(BLinePoint()));
1307 BLinePoint prev_bline_point;
1309 list<ValueNode_Const::Handle>::iterator prev(iter);
1310 if(iter==bline_point_list.begin())
1313 prev = bline_point_list.end();
1317 prev_bline_point=(*prev)->get_value().get(BLinePoint());
1319 etl::hermite<Vector> curve(prev_bline_point.get_vertex(),
1320 next_bline_point.get_vertex(),
1321 prev_bline_point.get_tangent2(),
1322 next_bline_point.get_tangent1());
1323 etl::derivative< etl::hermite<Vector> > deriv(curve);
1325 bline_point.set_vertex(curve(origin));
1326 bline_point.set_width((next_bline_point.get_width()-prev_bline_point.get_width())*origin+prev_bline_point.get_width());
1327 bline_point.set_tangent1(deriv(origin)*std::min(1.0f-origin,origin));
1328 bline_point.set_tangent2(bline_point.get_tangent1());
1329 bline_point.set_split_tangent_flag(false);
1330 bline_point.set_origin(origin);
1333 bline_point.set_vertex((next_bline_point.get_vertex()+prev_bline_point.get_vertex())*0.5);
1334 bline_point.set_width((next_bline_point.get_width()+prev_bline_point.get_width())*0.5);
1335 bline_point.set_origin(origin);
1336 bline_point.set_split_tangent_flag(false);
1337 bline_point.set_tangent1((next_bline_point.get_vertex()-prev_bline_point.get_vertex())*0.5);
1340 bline_point_list.insert(iter,ValueNode_Const::create(bline_point));
1344 if(iter==bline_point_list.end())
1346 get_canvas_view()->get_ui_interface()->error(_("Unable to find where to insert vertex, internal error, please report this bug"));
1349 refresh_ducks(false);
1353 StateBLine_Context::bline_delete_vertex(synfig::ValueNode_Const::Handle value_node)
1355 list<ValueNode_Const::Handle>::iterator iter;
1357 for(iter=bline_point_list.begin();iter!=bline_point_list.end();++iter)
1358 if(*iter==value_node)
1360 bline_point_list.erase(iter);
1363 if(iter==bline_point_list.end())
1365 get_canvas_view()->get_ui_interface()->error(_("Unable to remove vertex, internal error, please report this bug"));
1368 refresh_ducks(false);
1372 StateBLine_Context::popup_handle_menu(synfig::ValueNode_Const::Handle value_node)
1374 menu.items().clear();
1376 BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1378 if(bline_point.get_split_tangent_flag())
1379 menu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Merge Tangents"),
1381 sigc::mem_fun(*this,&studio::StateBLine_Context::bline_attach_handle),
1386 menu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Split Tangents"),
1388 sigc::mem_fun(*this,&studio::StateBLine_Context::bline_detach_handle),
1397 StateBLine_Context::bline_detach_handle(synfig::ValueNode_Const::Handle value_node)
1399 BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1400 bline_point.set_split_tangent_flag(true);
1401 bline_point.set_tangent2(bline_point.get_tangent1());
1402 value_node->set_value(bline_point);
1403 refresh_ducks(false);
1407 StateBLine_Context::bline_attach_handle(synfig::ValueNode_Const::Handle value_node)
1409 BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1410 bline_point.set_tangent1((bline_point.get_tangent1()+bline_point.get_tangent2())*0.5);
1411 bline_point.set_split_tangent_flag(false);
1412 value_node->set_value(bline_point);
1413 refresh_ducks(false);