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 "canvasview.h"
42 #include <synfig/valuenode_bline.h>
43 #include <ETL/hermite>
44 #include <ETL/calculus>
46 #include "event_mouse.h"
47 #include "event_layerclick.h"
49 #include "dialog_tooloptions.h"
50 #include <gtkmm/spinbutton.h>
51 #include <synfig/transform.h>
52 #include <synfigapp/main.h>
58 /* === U S I N G =========================================================== */
62 using namespace synfig;
63 using namespace studio;
65 /* === M A C R O S ========================================================= */
67 // if defined, show the first duck as green while drawing
68 #define DISTINGUISH_FIRST_DUCK
70 /* === G L O B A L S ======================================================= */
72 StateBLine studio::state_bline;
74 /* === C L A S S E S & S T R U C T S ======================================= */
76 class studio::StateBLine_Context : public sigc::trackable
78 etl::handle<CanvasView> canvas_view_;
79 CanvasView::IsWorking is_working;
81 bool prev_table_status;
83 bool prev_workarea_layer_status_;
86 Canvas::Handle canvas;
90 Duckmatic::Push duckmatic_push;
92 etl::handle<Duck> curr_duck;
94 etl::handle<Duck> next_duck;
96 std::list<synfig::ValueNode_Const::Handle> bline_point_list;
97 synfigapp::Settings& settings;
99 bool on_vertex_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node);
100 bool on_tangent1_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node);
101 bool on_tangent2_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node);
104 void popup_handle_menu(synfig::ValueNode_Const::Handle value_node);
105 void popup_vertex_menu(synfig::ValueNode_Const::Handle value_node);
106 void popup_bezier_menu(float location, synfig::ValueNode_Const::Handle value_node);
108 void bline_detach_handle(synfig::ValueNode_Const::Handle value_node);
109 void bline_attach_handle(synfig::ValueNode_Const::Handle value_node);
110 void bline_delete_vertex(synfig::ValueNode_Const::Handle value_node);
111 void bline_insert_vertex(synfig::ValueNode_Const::Handle value_node,float origin=0.5);
115 void refresh_ducks(bool x=true);
117 Gtk::Table options_table;
119 Gtk::CheckButton checkbutton_layer_region;
120 Gtk::CheckButton checkbutton_layer_outline;
121 Gtk::CheckButton checkbutton_layer_curve_gradient;
122 Gtk::CheckButton checkbutton_layer_plant;
123 Gtk::CheckButton checkbutton_layer_link_origins;
124 Gtk::CheckButton checkbutton_auto_export;
125 Gtk::Button button_make;
126 Gtk::Button button_clear;
127 Gtk::Adjustment adj_feather;
128 Gtk::SpinButton spin_feather;
134 int layers_to_create()const
137 get_layer_region_flag() +
138 get_layer_outline_flag() +
139 get_layer_curve_gradient_flag() +
140 get_layer_plant_flag();
145 if(layers_to_create()==0)
146 set_layer_region_flag(true);
149 bool get_auto_export_flag()const { return checkbutton_auto_export.get_active(); }
150 void set_auto_export_flag(bool x) { return checkbutton_auto_export.set_active(x); }
152 bool get_layer_region_flag()const { return checkbutton_layer_region.get_active(); }
153 void set_layer_region_flag(bool x) { return checkbutton_layer_region.set_active(x); }
155 bool get_layer_outline_flag()const { return checkbutton_layer_outline.get_active(); }
156 void set_layer_outline_flag(bool x) { return checkbutton_layer_outline.set_active(x); }
158 bool get_layer_curve_gradient_flag()const { return checkbutton_layer_curve_gradient.get_active(); }
159 void set_layer_curve_gradient_flag(bool x) { return checkbutton_layer_curve_gradient.set_active(x); }
161 bool get_layer_plant_flag()const { return checkbutton_layer_plant.get_active(); }
162 void set_layer_plant_flag(bool x) { return checkbutton_layer_plant.set_active(x); }
164 bool get_layer_link_origins_flag()const { return checkbutton_layer_link_origins.get_active(); }
165 void set_layer_link_origins_flag(bool x) { return checkbutton_layer_link_origins.set_active(x); }
167 Real get_feather() const { return adj_feather.get_value(); }
168 void set_feather(Real x) { return adj_feather.set_value(x); }
169 synfig::String get_id()const { return entry_id.get_text(); }
170 void set_id(const synfig::String& x) { return entry_id.set_text(x); }
172 Smach::event_result event_stop_handler(const Smach::event& x);
174 Smach::event_result event_refresh_handler(const Smach::event& x);
176 Smach::event_result event_mouse_click_handler(const Smach::event& x);
177 Smach::event_result event_mouse_release_handler(const Smach::event& x);
178 Smach::event_result event_mouse_motion_handler(const Smach::event& x);
179 Smach::event_result event_refresh_tool_options(const Smach::event& x);
181 Smach::event_result event_hijack(const Smach::event& /*x*/) { return Smach::RESULT_ACCEPT; }
183 void refresh_tool_options();
185 StateBLine_Context(CanvasView* canvas_view);
187 ~StateBLine_Context();
189 const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
190 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
191 synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
192 WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
193 const synfig::TransformStack& get_transform_stack()const { return canvas_view_->get_curr_transform_stack(); }
195 void load_settings();
196 void save_settings();
199 //void on_user_click(synfig::Point point);
204 bool egress_on_selection_change;
205 Smach::event_result event_layer_selection_changed_handler(const Smach::event& /*x*/)
207 if(egress_on_selection_change)
208 throw Smach::egress_exception();
209 return Smach::RESULT_OK;
212 }; // END of class StateBLine_Context
215 /* === M E T H O D S ======================================================= */
217 StateBLine::StateBLine():
218 Smach::state<StateBLine_Context>("bline")
220 insert(event_def(EVENT_LAYER_SELECTION_CHANGED, &StateBLine_Context::event_layer_selection_changed_handler));
221 insert(event_def(EVENT_STOP, &StateBLine_Context::event_stop_handler));
222 insert(event_def(EVENT_REFRESH, &StateBLine_Context::event_refresh_handler));
223 insert(event_def(EVENT_REFRESH_DUCKS, &StateBLine_Context::event_hijack));
224 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN, &StateBLine_Context::event_mouse_click_handler));
225 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_UP, &StateBLine_Context::event_mouse_release_handler));
226 insert(event_def(EVENT_WORKAREA_MOUSE_MOTION, &StateBLine_Context::event_mouse_motion_handler));
227 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DRAG, &StateBLine_Context::event_mouse_motion_handler));
228 insert(event_def(EVENT_REFRESH_TOOL_OPTIONS, &StateBLine_Context::event_refresh_tool_options));
231 StateBLine::~StateBLine()
236 StateBLine_Context::load_settings()
240 if(settings.get_value("bline.layer_region",value) && value=="0")
241 set_layer_region_flag(false);
243 set_layer_region_flag(true);
245 if(settings.get_value("bline.layer_outline",value) && value=="0")
246 set_layer_outline_flag(false);
248 set_layer_outline_flag(true);
250 if(settings.get_value("bline.layer_curve_gradient",value) && value=="1")
251 set_layer_curve_gradient_flag(true);
253 set_layer_curve_gradient_flag(false);
255 if(settings.get_value("bline.layer_plant",value) && value=="1")
256 set_layer_plant_flag(true);
258 set_layer_plant_flag(false);
260 if(settings.get_value("bline.layer_link_origins",value) && value=="0")
261 set_layer_link_origins_flag(false);
263 set_layer_link_origins_flag(true);
265 if(settings.get_value("bline.auto_export",value) && value=="1")
266 set_auto_export_flag(true);
268 set_auto_export_flag(false);
270 if(settings.get_value("bline.id",value))
275 if(settings.get_value("bline.feather",value))
277 Real n = atof(value.c_str());
285 StateBLine_Context::save_settings()
288 settings.set_value("bline.layer_outline",get_layer_outline_flag()?"1":"0");
289 settings.set_value("bline.layer_region",get_layer_region_flag()?"1":"0");
290 settings.set_value("bline.layer_curve_gradient",get_layer_curve_gradient_flag()?"1":"0");
291 settings.set_value("bline.layer_plant",get_layer_plant_flag()?"1":"0");
292 settings.set_value("bline.layer_link_origins",get_layer_link_origins_flag()?"1":"0");
293 settings.set_value("bline.auto_export",get_auto_export_flag()?"1":"0");
294 settings.set_value("bline.id",get_id().c_str());
295 settings.set_value("bline.feather",strprintf("%f",get_feather()));
299 StateBLine_Context::reset()
302 bline_point_list.clear();
307 StateBLine_Context::increment_id()
316 // If there is a number
317 // already at the end of the
318 // id, then remove it.
319 if(id[id.size()-1]<='9' && id[id.size()-1]>='0')
321 // figure out how many digits it is
323 (int)id.size()-1 >= digits && id[id.size()-1-digits] <= '9' && id[id.size()-1-digits] >= '0';
328 str_number=String(id,id.size()-digits,id.size());
329 id=String(id,0,id.size()-digits);
330 // synfig::info("---------------- \"%s\"",str_number.c_str());
332 number=atoi(str_number.c_str());
342 // Add the number back onto the id
344 const String format(strprintf("%%0%dd",digits));
345 id+=strprintf(format.c_str(),number);
353 StateBLine_Context::StateBLine_Context(CanvasView* canvas_view):
354 canvas_view_(canvas_view),
355 is_working(*canvas_view),
357 prev_workarea_layer_status_(get_work_area()->get_allow_layer_clicks()),
358 duckmatic_push(get_work_area()),
359 settings(synfigapp::Main::get_selected_input_device()->settings()),
361 checkbutton_layer_region(_("Create Region BLine")),
362 checkbutton_layer_outline(_("Create Outline BLine")),
363 checkbutton_layer_curve_gradient(_("Create Curve Gradient BLine")),
364 checkbutton_layer_plant(_("Create Plant BLine")),
365 checkbutton_layer_link_origins(_("Link Origins")),
366 checkbutton_auto_export(_("Auto Export")),
367 button_make(_("Make")),
368 button_clear(_("Clear")),
369 adj_feather(0,0,10000,0.01,0.1),
370 spin_feather(adj_feather,0.01,4)
373 egress_on_selection_change=true;
376 // Set up the tool options dialog
377 options_table.attach(*manage(new Gtk::Label(_("BLine Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
378 options_table.attach(entry_id, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
379 options_table.attach(checkbutton_layer_outline, 0, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
380 options_table.attach(checkbutton_layer_region, 0, 2, 3, 4, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
381 options_table.attach(checkbutton_layer_plant, 0, 2, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
382 options_table.attach(checkbutton_layer_curve_gradient, 0, 2, 5, 6, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
383 options_table.attach(checkbutton_layer_link_origins, 0, 2, 6, 7, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
384 options_table.attach(checkbutton_auto_export, 0, 2, 7, 8, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
385 options_table.attach(*manage(new Gtk::Label(_("Feather"))), 0, 1, 10, 11, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
386 options_table.attach(spin_feather, 1, 2, 10, 11, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
387 //options_table.attach(button_make, 0, 2, 5, 6, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
388 //button_make.signal_pressed().connect(sigc::mem_fun(*this,&StateBLine_Context::run));
389 options_table.show_all();
390 refresh_tool_options();
391 App::dialog_tool_options->present();
393 // Turn off layer clicking
394 get_work_area()->set_allow_layer_clicks(false);
396 // clear out the ducks
397 get_work_area()->clear_ducks();
399 // Refresh the work area
400 get_work_area()->queue_draw();
402 // Hide the tables if they are showing
403 prev_table_status=get_canvas_view()->tables_are_visible();
404 if(prev_table_status)get_canvas_view()->hide_tables();
406 // Disable the time bar
407 get_canvas_view()->set_sensitive_timebar(false);
410 //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StateBLine_Context::on_user_click));
411 get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
413 App::toolbox->refresh();
417 StateBLine_Context::refresh_tool_options()
419 App::dialog_tool_options->clear();
420 App::dialog_tool_options->set_widget(options_table);
421 App::dialog_tool_options->set_local_name(_("BLine Tool"));
422 App::dialog_tool_options->set_name("bline");
424 App::dialog_tool_options->add_button(
425 Gtk::StockID("gtk-execute"),
426 _("Make BLine and/or Region")
427 )->signal_clicked().connect(
428 sigc::hide_return(sigc::mem_fun(
430 &StateBLine_Context::run
434 App::dialog_tool_options->add_button(
435 Gtk::StockID("gtk-clear"),
436 _("Clear current BLine")
437 )->signal_clicked().connect(
440 &StateBLine_Context::reset
446 StateBLine_Context::event_refresh_tool_options(const Smach::event& /*x*/)
448 refresh_tool_options();
449 return Smach::RESULT_ACCEPT;
452 StateBLine_Context::~StateBLine_Context()
457 App::dialog_tool_options->clear();
459 get_canvas_view()->work_area->reset_cursor();
461 // Restore layer clicking
462 get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
464 // Enable the time bar
465 get_canvas_view()->set_sensitive_timebar(true);
467 // Bring back the tables if they were out before
468 if(prev_table_status)get_canvas_view()->show_tables();
470 // get_canvas_view()->get_smach().process_event(EVENT_REFRESH_DUCKS);
472 // Refresh the work area
473 get_work_area()->queue_draw();
475 App::toolbox->refresh();
479 StateBLine_Context::event_stop_handler(const Smach::event& /*x*/)
481 // synfig::info("STATE RotoBLine: Received Stop Event");
484 // throw Smach::egress_exception();
485 // get_canvas_view()->get_smach().pop_state();
486 return Smach::RESULT_ACCEPT;
490 StateBLine_Context::event_refresh_handler(const Smach::event& /*x*/)
492 // synfig::info("STATE RotoBLine: Received Refresh Event");
494 return Smach::RESULT_ACCEPT;
498 StateBLine_Context::run()
504 for(int i=5;i>0 && !success;i--)try
512 if(!success && !err.empty())
514 get_canvas_view()->get_ui_interface()->error(err);
520 StateBLine_Context::run_()
525 // Now we need to generate it
527 if(bline_point_list.empty())
531 if(bline_point_list.size()<2)
533 //get_canvas_view()->get_ui_interface()->error(_("You need at least two (2) points to create a BLine"));
540 // Create the action group
541 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New BLine"));
543 std::vector<BLinePoint> new_list;
544 std::list<synfig::ValueNode_Const::Handle>::iterator iter;
545 const synfig::TransformStack& transform(get_transform_stack());
547 for(iter=bline_point_list.begin();iter!=bline_point_list.end();++iter)
549 BLinePoint bline_point((*iter)->get_value().get(BLinePoint()));
550 Point new_vertex(transform.unperform(bline_point.get_vertex()));
552 bline_point.set_tangent1(
554 bline_point.get_tangent1()+bline_point.get_vertex()
558 bline_point.set_tangent2(
560 bline_point.get_tangent2()+bline_point.get_vertex()
564 bline_point.set_vertex(new_vertex);
566 new_list.push_back(bline_point);
569 ValueNode_BLine::Handle value_node_bline(ValueNode_BLine::create(new_list));
570 assert(value_node_bline);
572 ValueNode_Const::Handle value_node_origin(ValueNode_Const::create(Vector()));
573 assert(value_node_origin);
575 // Set the looping flag
576 value_node_bline->set_loop(loop_);
578 // Add the BLine to the canvas
579 if(get_auto_export_flag() && !get_canvas_interface()->add_value_node(value_node_bline,get_id()))
581 //get_canvas_view()->get_ui_interface()->error(_("Unable to add value node"));
584 throw String(_("Unable to add value node"));
590 // we are temporarily using the layer to hold something
591 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
596 depth=layer->get_depth();
598 canvas=layer->get_canvas();
604 canvas=get_canvas_view()->get_canvas();
606 value_node_bline->set_member_canvas(canvas);
608 synfigapp::SelectionManager::LayerList layer_selection;
609 if (!getenv("SYNFIG_TOOLS_CLEAR_SELECTION"))
610 layer_selection = get_canvas_view()->get_selection_manager()->get_selected_layers();
612 // count how many layers we're going to be creating
613 int layers_to_create = this->layers_to_create();
615 ///////////////////////////////////////////////////////////////////////////
616 // C U R V E G R A D I E N T
617 ///////////////////////////////////////////////////////////////////////////
619 if(get_layer_curve_gradient_flag())
621 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
623 Layer::Handle layer(get_canvas_interface()->add_layer_to("curve_gradient",canvas,depth));
625 layer_selection.push_back(layer);
626 layer->set_description(get_id()+_(" Gradient"));
627 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
630 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
633 action->set_param("canvas",get_canvas());
634 action->set_param("canvas_interface",get_canvas_interface());
635 action->set_param("layer",layer);
636 if(!action->set_param("param",String("bline")))
637 synfig::error("LayerParamConnect didn't like \"param\"");
638 if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
639 synfig::error("LayerParamConnect didn't like \"value_node\"");
641 if(!get_canvas_interface()->get_instance()->perform_action(action))
643 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
645 throw String(_("Unable to create Gradient layer"));
650 // only link the curve gradient's origin parameter if the option is selected and we're creating more than one layer
651 if (get_layer_link_origins_flag() && layers_to_create > 1)
653 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
656 action->set_param("canvas",get_canvas());
657 action->set_param("canvas_interface",get_canvas_interface());
658 action->set_param("layer",layer);
659 if(!action->set_param("param",String("origin")))
660 synfig::error("LayerParamConnect didn't like \"param\"");
661 if(!action->set_param("value_node",ValueNode::Handle(value_node_origin)))
662 synfig::error("LayerParamConnect didn't like \"value_node\"");
664 if(!get_canvas_interface()->get_instance()->perform_action(action))
666 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
668 throw String(_("Unable to create Gradient layer"));
674 ///////////////////////////////////////////////////////////////////////////
676 ///////////////////////////////////////////////////////////////////////////
678 if(get_layer_plant_flag())
680 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
682 Layer::Handle layer(get_canvas_interface()->add_layer_to("plant",canvas,depth));
684 layer_selection.push_back(layer);
685 layer->set_description(get_id()+_(" Plant"));
686 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
689 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
692 action->set_param("canvas",get_canvas());
693 action->set_param("canvas_interface",get_canvas_interface());
694 action->set_param("layer",layer);
695 if(!action->set_param("param",String("bline")))
696 synfig::error("LayerParamConnect didn't like \"param\"");
697 if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
698 synfig::error("LayerParamConnect didn't like \"value_node\"");
700 if(!get_canvas_interface()->get_instance()->perform_action(action))
702 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
704 throw String(_("Unable to create Plant layer"));
709 // only link the plant's origin parameter if the option is selected and we're creating more than one layer
710 if (get_layer_link_origins_flag() && layers_to_create > 1)
712 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
715 action->set_param("canvas",get_canvas());
716 action->set_param("canvas_interface",get_canvas_interface());
717 action->set_param("layer",layer);
718 if(!action->set_param("param",String("origin")))
719 synfig::error("LayerParamConnect didn't like \"param\"");
720 if(!action->set_param("value_node",ValueNode::Handle(value_node_origin)))
721 synfig::error("LayerParamConnect didn't like \"value_node\"");
723 if(!get_canvas_interface()->get_instance()->perform_action(action))
725 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
727 throw String(_("Unable to create Plant layer"));
733 ///////////////////////////////////////////////////////////////////////////
735 ///////////////////////////////////////////////////////////////////////////
737 if(get_layer_region_flag())
739 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
741 Layer::Handle layer(get_canvas_interface()->add_layer_to("region",canvas,depth));
743 layer_selection.push_back(layer);
744 layer->set_description(get_id()+_(" Region"));
745 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
749 layer->set_param("feather",get_feather());
750 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
753 if(get_layer_outline_flag())
754 layer->set_param("color",synfigapp::Main::get_background_color());
756 // I don't know if it's safe to reuse the same layer_param_connect action, so I'm
757 // using 2 separate ones.
759 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
762 action->set_param("canvas",get_canvas());
763 action->set_param("canvas_interface",get_canvas_interface());
764 action->set_param("layer",layer);
765 if(!action->set_param("param",String("bline")))
766 synfig::error("LayerParamConnect didn't like \"param\"");
767 if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
768 synfig::error("LayerParamConnect didn't like \"value_node\"");
770 if(!get_canvas_interface()->get_instance()->perform_action(action))
772 //get_canvas_view()->get_ui_interface()->error(_("Unable to create Region layer"));
774 throw String(_("Unable to create Region layer"));
779 // only link the region's origin parameter if the option is selected and we're creating more than one layer
780 if (get_layer_link_origins_flag() && layers_to_create > 1)
782 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
785 action->set_param("canvas",get_canvas());
786 action->set_param("canvas_interface",get_canvas_interface());
787 action->set_param("layer",layer);
788 if(!action->set_param("param",String("origin")))
789 synfig::error("LayerParamConnect didn't like \"param\"");
790 if(!action->set_param("value_node",ValueNode::Handle(value_node_origin)))
791 synfig::error("LayerParamConnect didn't like \"value_node\"");
793 if(!get_canvas_interface()->get_instance()->perform_action(action))
795 //get_canvas_view()->get_ui_interface()->error(_("Unable to create Region layer"));
797 throw String(_("Unable to create Region layer"));
803 ///////////////////////////////////////////////////////////////////////////
805 ///////////////////////////////////////////////////////////////////////////
807 if(get_layer_outline_flag())
809 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
811 Layer::Handle layer(get_canvas_interface()->add_layer_to("outline",canvas,depth));
813 layer_selection.push_back(layer);
814 layer->set_description(get_id()+_(" Outline"));
815 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
818 layer->set_param("feather",get_feather());
819 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
823 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
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\"");
834 if(!get_canvas_interface()->get_instance()->perform_action(action))
836 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
838 throw String(_("Unable to create Outline layer"));
843 // only link the outline'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)
846 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
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\"");
857 if(!get_canvas_interface()->get_instance()->perform_action(action))
859 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
861 throw String(_("Unable to create Outline layer"));
867 egress_on_selection_change=false;
868 get_canvas_interface()->get_selection_manager()->clear_selected_layers();
869 get_canvas_interface()->get_selection_manager()->set_selected_layers(layer_selection);
870 egress_on_selection_change=true;
872 //if(finish_bline_dialog.get_region_flag() || finish_bline_dialog.get_bline_flag())
873 // get_canvas_interface()->signal_dirty_preview()();
883 StateBLine_Context::event_mouse_motion_handler(const Smach::event& x)
885 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
889 //synfig::info("Moved Duck");
890 Point p(get_work_area()->snap_point_to_grid(event.pos));
891 curr_duck->set_trans_point(p);
893 next_duck->set_trans_point(p);
894 get_work_area()->queue_draw();
895 return Smach::RESULT_ACCEPT;
898 return Smach::RESULT_OK;
902 StateBLine_Context::event_mouse_release_handler(const Smach::event& /*x*/)
906 //synfig::info("Released current duck");
907 curr_duck->signal_edited()(curr_duck->get_point());
910 //synfig::info("grabbing next duck");
914 return Smach::RESULT_ACCEPT;
916 return Smach::RESULT_OK;
920 StateBLine_Context::event_mouse_click_handler(const Smach::event& x)
922 // synfig::info("STATE BLINE: Received mouse button down Event");
923 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
928 // If we are already looped up, then don't try to add anything else
930 return Smach::RESULT_OK;
932 BLinePoint bline_point;
934 bline_point.set_vertex(get_work_area()->snap_point_to_grid(event.pos));
935 //bline_point.set_width(synfigapp::Main::get_bline_width());
936 bline_point.set_width(1.0f);
937 bline_point.set_origin(0.5f);
938 bline_point.set_split_tangent_flag(false);
939 bline_point.set_tangent1(Vector(0,0));
943 if(bline_point_list.empty())
945 bline_point.set_tangent1(Vector(1,1));
949 const Vector t(event.pos-bline_point_list.back()->get_value().get(BLinePoint()).get_vertex());
950 bline_point.set_tangent1(t);
953 if(bline_point_list.size()>1)
955 std::list<synfig::ValueNode_Const::Handle>::iterator iter;
956 iter=bline_point_list.end();
958 BLinePoint prev(bline_point_list.back()->get_value().get(BLinePoint()));
959 prev.set_tangent1(event.pos-(*iter)->get_value().get(BLinePoint()).get_vertex());
960 bline_point_list.back()->set_value(prev);
964 bline_point_list.push_back(ValueNode_Const::create(bline_point));
967 return Smach::RESULT_ACCEPT;
970 case BUTTON_RIGHT: // Intercept the right-button click to short-circuit the pop-up menu
971 if (!getenv("SYNFIG_ENABLE_POPUP_MENU_IN_ALL_TOOLS"))
972 return Smach::RESULT_ACCEPT;
975 return Smach::RESULT_OK;
980 StateBLine_Context::refresh_ducks(bool button_down)
982 get_work_area()->clear_ducks();
983 get_work_area()->queue_draw();
985 if(bline_point_list.empty())
988 list<ValueNode_Const::Handle>::iterator iter;
990 handle<WorkArea::Bezier> bezier;
991 handle<WorkArea::Duck> duck,tduck;
992 BLinePoint bline_point;
994 for(iter=bline_point_list.begin();iter!=bline_point_list.end();++iter)
996 ValueNode_Const::Handle value_node(*iter);
997 bline_point=(value_node->get_value().get(BLinePoint()));
1001 // First add the duck associated with this vertex
1002 duck=new WorkArea::Duck(bline_point.get_vertex());
1003 duck->set_editable(true);
1004 #ifdef DISTINGUISH_FIRST_DUCK
1005 if (iter!=bline_point_list.begin())
1006 duck->set_type(Duck::TYPE_VERTEX);
1008 duck->set_type(Duck::TYPE_VERTEX);
1010 duck->set_name(strprintf("%x-vertex",value_node.get()));
1011 duck->signal_edited().connect(
1012 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_vertex_change),value_node)
1014 duck->signal_user_click(2).connect(
1015 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::popup_vertex_menu),value_node)
1017 duck->set_guid(value_node->get_guid()^synfig::GUID::hasher(0));
1019 get_work_area()->add_duck(duck);
1021 // Add the tangent1 duck
1022 tduck=new WorkArea::Duck(bline_point.get_tangent1());
1023 tduck->set_editable(true);
1024 tduck->set_name(strprintf("%x-tangent1",value_node.get()));
1025 tduck->set_origin(duck);
1026 tduck->set_scalar(-0.33333333333333333);
1027 tduck->set_tangent(true);
1028 tduck->set_guid(value_node->get_guid()^synfig::GUID::hasher(3));
1029 tduck->signal_edited().connect(
1030 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_tangent1_change),value_node)
1032 tduck->signal_user_click(2).connect(
1033 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::popup_handle_menu),value_node)
1036 // See if we need to add that duck to the previous bezier
1039 get_work_area()->add_duck(tduck);
1043 bezier->signal_user_click(2).connect(
1047 &studio::StateBLine_Context::popup_bezier_menu
1053 //get_work_area()->add_duck(bezier->c1);
1054 //get_work_area()->add_duck(bezier->c2);
1055 get_work_area()->add_bezier(bezier);
1060 // Now we see if we need to create a bezier
1061 list<ValueNode_Const::Handle>::iterator next(iter);
1064 // If our next iterator is the end, then we don't need
1066 //if(next==bline_point_list.end() && !loop_)
1069 bezier=new WorkArea::Bezier();
1071 // Add the tangent2 duck
1072 tduck=new WorkArea::Duck(bline_point.get_tangent2());
1073 tduck->set_editable(true);
1074 tduck->set_origin(duck);
1075 tduck->set_scalar(0.33333333333333333);
1076 tduck->set_tangent(true);
1077 if(bline_point.get_split_tangent_flag())
1079 tduck->set_name(strprintf("%x-tangent2",value_node.get()));
1080 tduck->signal_edited().connect(
1081 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_tangent2_change),value_node)
1086 tduck->set_name(strprintf("%x-tangent1",value_node.get()));
1087 tduck->signal_edited().connect(
1088 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_tangent1_change),value_node)
1091 tduck->set_guid(value_node->get_guid()^synfig::GUID::hasher(4));
1092 tduck->signal_user_click(2).connect(
1093 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::popup_handle_menu),value_node)
1096 // Setup the next bezier
1100 get_work_area()->add_duck(tduck);
1104 // Add the loop, if requested
1108 BLinePoint bline_point(bline_point_list.front()->get_value().get(BLinePoint()));
1110 duck=new WorkArea::Duck(bline_point.get_vertex());
1111 duck->set_editable(true);
1112 #ifndef DISTINGUISH_FIRST_DUCK
1113 duck->set_type(Duck::TYPE_VERTEX);
1115 duck->set_name(strprintf("%x-vertex",bline_point_list.front().get()));
1116 duck->signal_edited().connect(
1117 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_vertex_change),bline_point_list.front())
1119 duck->signal_user_click(2).connect(
1120 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::popup_vertex_menu),bline_point_list.front())
1122 get_work_area()->add_duck(duck);
1124 // Add the tangent1 duck
1125 tduck=new WorkArea::Duck(bline_point.get_tangent1());
1126 tduck->set_editable(true);
1127 tduck->set_name(strprintf("%x-tangent1",bline_point_list.front().get()));
1128 tduck->set_origin(duck);
1129 tduck->set_scalar(-0.33333333333333333);
1130 tduck->set_tangent(true);
1131 tduck->signal_edited().connect(
1132 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_tangent1_change),bline_point_list.front())
1134 tduck->signal_user_click(2).connect(
1135 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::popup_handle_menu),bline_point_list.front())
1137 get_work_area()->add_duck(tduck);
1142 bezier->signal_user_click(2).connect(
1146 &studio::StateBLine_Context::popup_bezier_menu
1148 bline_point_list.front()
1152 //get_work_area()->add_duck(bezier->c1);
1153 get_work_area()->add_bezier(bezier);
1155 if(bezier && !loop_)
1157 duck=new WorkArea::Duck(bline_point.get_vertex());
1158 duck->set_ignore(true);
1159 duck->set_name("temp");
1161 // Add the tangent1 duck
1162 tduck=new WorkArea::Duck(Vector(0,0));
1163 tduck->set_ignore(true);
1164 tduck->set_name("ttemp");
1165 tduck->set_origin(duck);
1166 tduck->set_scalar(-0.33333333333333333);
1168 tduck->set_tangent(true);
1172 get_work_area()->add_duck(bezier->p2);
1173 //get_work_area()->add_duck(bezier->c2);
1174 get_work_area()->add_bezier(bezier);
1176 duck->set_guid(synfig::GUID());
1177 tduck->set_guid(synfig::GUID());
1188 curr_duck=next_duck;
1193 get_work_area()->queue_draw();
1198 StateBLine_Context::on_vertex_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node)
1200 BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1201 bline_point.set_vertex(point);
1202 value_node->set_value(bline_point);
1208 StateBLine_Context::on_tangent1_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_tangent1(point);
1212 value_node->set_value(bline_point);
1218 StateBLine_Context::on_tangent2_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_tangent2(point);
1222 value_node->set_value(bline_point);
1228 StateBLine_Context::loop_bline()
1232 refresh_ducks(false);
1236 StateBLine_Context::unloop_bline()
1240 refresh_ducks(false);
1244 StateBLine_Context::popup_vertex_menu(synfig::ValueNode_Const::Handle value_node)
1246 menu.items().clear();
1250 menu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Unloop BLine"),
1251 sigc::mem_fun(*this,&studio::StateBLine_Context::unloop_bline)
1254 menu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Loop BLine"),
1255 sigc::mem_fun(*this,&studio::StateBLine_Context::loop_bline)
1259 menu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Delete Vertex"),
1261 sigc::mem_fun(*this,&studio::StateBLine_Context::bline_delete_vertex),
1270 StateBLine_Context::popup_bezier_menu(float location, synfig::ValueNode_Const::Handle value_node)
1272 menu.items().clear();
1274 menu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Insert Vertex"),
1277 sigc::mem_fun(*this,&studio::StateBLine_Context::bline_insert_vertex),
1288 StateBLine_Context::bline_insert_vertex(synfig::ValueNode_Const::Handle value_node, float origin)
1290 list<ValueNode_Const::Handle>::iterator iter;
1292 for(iter=bline_point_list.begin();iter!=bline_point_list.end();++iter)
1293 if(*iter==value_node)
1295 BLinePoint bline_point;
1296 BLinePoint next_bline_point((*iter)->get_value().get(BLinePoint()));
1297 BLinePoint prev_bline_point;
1299 list<ValueNode_Const::Handle>::iterator prev(iter);
1300 if(iter==bline_point_list.begin())
1303 prev = bline_point_list.end();
1307 prev_bline_point=(*prev)->get_value().get(BLinePoint());
1309 etl::hermite<Vector> curve(prev_bline_point.get_vertex(),
1310 next_bline_point.get_vertex(),
1311 prev_bline_point.get_tangent2(),
1312 next_bline_point.get_tangent1());
1313 etl::derivative< etl::hermite<Vector> > deriv(curve);
1315 bline_point.set_vertex(curve(origin));
1316 bline_point.set_width((next_bline_point.get_width()-prev_bline_point.get_width())*origin+prev_bline_point.get_width());
1317 bline_point.set_tangent1(deriv(origin)*std::min(1.0f-origin,origin));
1318 bline_point.set_tangent2(bline_point.get_tangent1());
1319 bline_point.set_split_tangent_flag(false);
1320 bline_point.set_origin(origin);
1323 bline_point.set_vertex((next_bline_point.get_vertex()+prev_bline_point.get_vertex())*0.5);
1324 bline_point.set_width((next_bline_point.get_width()+prev_bline_point.get_width())*0.5);
1325 bline_point.set_origin(origin);
1326 bline_point.set_split_tangent_flag(false);
1327 bline_point.set_tangent1((next_bline_point.get_vertex()-prev_bline_point.get_vertex())*0.5);
1330 bline_point_list.insert(iter,ValueNode_Const::create(bline_point));
1334 if(iter==bline_point_list.end())
1336 get_canvas_view()->get_ui_interface()->error(_("Unable to find where to insert vertex, internal error, please report this bug"));
1339 refresh_ducks(false);
1343 StateBLine_Context::bline_delete_vertex(synfig::ValueNode_Const::Handle value_node)
1345 list<ValueNode_Const::Handle>::iterator iter;
1347 for(iter=bline_point_list.begin();iter!=bline_point_list.end();++iter)
1348 if(*iter==value_node)
1350 bline_point_list.erase(iter);
1353 if(iter==bline_point_list.end())
1355 get_canvas_view()->get_ui_interface()->error(_("Unable to remove vertex, internal error, please report this bug"));
1358 refresh_ducks(false);
1362 StateBLine_Context::popup_handle_menu(synfig::ValueNode_Const::Handle value_node)
1364 menu.items().clear();
1366 BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1368 if(bline_point.get_split_tangent_flag())
1369 menu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Merge Tangents"),
1371 sigc::mem_fun(*this,&studio::StateBLine_Context::bline_attach_handle),
1376 menu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Split Tangents"),
1378 sigc::mem_fun(*this,&studio::StateBLine_Context::bline_detach_handle),
1387 StateBLine_Context::bline_detach_handle(synfig::ValueNode_Const::Handle value_node)
1389 BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1390 bline_point.set_split_tangent_flag(true);
1391 bline_point.set_tangent2(bline_point.get_tangent1());
1392 value_node->set_value(bline_point);
1393 refresh_ducks(false);
1397 StateBLine_Context::bline_attach_handle(synfig::ValueNode_Const::Handle value_node)
1399 BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1400 bline_point.set_tangent1((bline_point.get_tangent1()+bline_point.get_tangent2())*0.5);
1401 bline_point.set_split_tangent_flag(false);
1402 value_node->set_value(bline_point);
1403 refresh_ducks(false);