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 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 /* === G L O B A L S ======================================================= */
69 StateBLine studio::state_bline;
71 /* === C L A S S E S & S T R U C T S ======================================= */
73 class studio::StateBLine_Context : public sigc::trackable
75 etl::handle<CanvasView> canvas_view_;
76 CanvasView::IsWorking is_working;
78 bool prev_table_status;
80 bool prev_workarea_layer_status_;
83 Canvas::Handle canvas;
87 Duckmatic::Push duckmatic_push;
89 etl::handle<Duck> curr_duck;
91 etl::handle<Duck> next_duck;
93 std::list<synfig::ValueNode_Const::Handle> bline_point_list;
94 synfigapp::Settings& settings;
96 bool on_vertex_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node);
97 bool on_tangent1_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node);
98 bool on_tangent2_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node);
101 void popup_handle_menu(synfig::ValueNode_Const::Handle value_node);
102 void popup_vertex_menu(synfig::ValueNode_Const::Handle value_node);
103 void popup_bezier_menu(float location, synfig::ValueNode_Const::Handle value_node);
105 void bline_detach_handle(synfig::ValueNode_Const::Handle value_node);
106 void bline_attach_handle(synfig::ValueNode_Const::Handle value_node);
107 void bline_delete_vertex(synfig::ValueNode_Const::Handle value_node);
108 void bline_insert_vertex(synfig::ValueNode_Const::Handle value_node,float origin=0.5);
112 void refresh_ducks(bool x=true);
114 Gtk::Table options_table;
116 Gtk::CheckButton checkbutton_layer_region;
117 Gtk::CheckButton checkbutton_layer_bline;
118 Gtk::CheckButton checkbutton_layer_curve_gradient;
119 Gtk::CheckButton checkbutton_layer_link_offsets;
120 Gtk::CheckButton checkbutton_auto_export;
121 Gtk::Button button_make;
122 Gtk::Button button_clear;
123 Gtk::Adjustment adj_feather;
124 Gtk::SpinButton spin_feather;
130 int layers_to_create()const
133 get_layer_region_flag()+
134 get_layer_bline_flag()+
135 get_layer_curve_gradient_flag();
140 if(layers_to_create()==0)
141 set_layer_region_flag(true);
144 bool get_auto_export_flag()const { return checkbutton_auto_export.get_active(); }
145 void set_auto_export_flag(bool x) { return checkbutton_auto_export.set_active(x); }
147 bool get_layer_region_flag()const { return checkbutton_layer_region.get_active(); }
148 void set_layer_region_flag(bool x) { return checkbutton_layer_region.set_active(x); }
150 bool get_layer_bline_flag()const { return checkbutton_layer_bline.get_active(); }
151 void set_layer_bline_flag(bool x) { return checkbutton_layer_bline.set_active(x); }
153 bool get_layer_curve_gradient_flag()const { return checkbutton_layer_curve_gradient.get_active(); }
154 void set_layer_curve_gradient_flag(bool x) { return checkbutton_layer_curve_gradient.set_active(x); }
156 bool get_layer_link_offsets_flag()const { return checkbutton_layer_link_offsets.get_active(); }
157 void set_layer_link_offsets_flag(bool x) { return checkbutton_layer_link_offsets.set_active(x); }
159 Real get_feather() const { return adj_feather.get_value(); }
160 void set_feather(Real x) { return adj_feather.set_value(x); }
161 synfig::String get_id()const { return entry_id.get_text(); }
162 void set_id(const synfig::String& x) { return entry_id.set_text(x); }
164 Smach::event_result event_stop_handler(const Smach::event& x);
166 Smach::event_result event_refresh_handler(const Smach::event& x);
168 Smach::event_result event_mouse_click_handler(const Smach::event& x);
169 Smach::event_result event_mouse_release_handler(const Smach::event& x);
170 Smach::event_result event_mouse_motion_handler(const Smach::event& x);
171 Smach::event_result event_refresh_tool_options(const Smach::event& x);
173 Smach::event_result event_hijack(const Smach::event& /*x*/) { return Smach::RESULT_ACCEPT; }
175 void refresh_tool_options();
177 StateBLine_Context(CanvasView* canvas_view);
179 ~StateBLine_Context();
181 const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
182 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
183 synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
184 WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
185 const synfig::TransformStack& get_transform_stack()const { return canvas_view_->get_curr_transform_stack(); }
187 void load_settings();
188 void save_settings();
191 //void on_user_click(synfig::Point point);
196 bool egress_on_selection_change;
197 Smach::event_result event_layer_selection_changed_handler(const Smach::event& /*x*/)
199 if(egress_on_selection_change)
200 throw Smach::egress_exception();
201 return Smach::RESULT_OK;
204 }; // END of class StateBLine_Context
207 /* === M E T H O D S ======================================================= */
209 StateBLine::StateBLine():
210 Smach::state<StateBLine_Context>("bline")
212 insert(event_def(EVENT_LAYER_SELECTION_CHANGED,&StateBLine_Context::event_layer_selection_changed_handler));
213 insert(event_def(EVENT_STOP,&StateBLine_Context::event_stop_handler));
214 insert(event_def(EVENT_REFRESH,&StateBLine_Context::event_refresh_handler));
215 insert(event_def(EVENT_REFRESH_DUCKS,&StateBLine_Context::event_hijack));
216 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateBLine_Context::event_mouse_click_handler));
217 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_UP,&StateBLine_Context::event_mouse_release_handler));
218 insert(event_def(EVENT_WORKAREA_MOUSE_MOTION,&StateBLine_Context::event_mouse_motion_handler));
219 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DRAG,&StateBLine_Context::event_mouse_motion_handler));
220 insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateBLine_Context::event_refresh_tool_options));
223 StateBLine::~StateBLine()
228 StateBLine_Context::load_settings()
232 if(settings.get_value("bline.layer_region",value) && value=="0")
233 set_layer_region_flag(false);
235 set_layer_region_flag(true);
237 if(settings.get_value("bline.layer_bline",value) && value=="0")
238 set_layer_bline_flag(false);
240 set_layer_bline_flag(true);
242 if(settings.get_value("bline.layer_curve_gradient",value) && value=="1")
243 set_layer_curve_gradient_flag(true);
245 set_layer_curve_gradient_flag(false);
247 if(settings.get_value("bline.layer_link_offsets",value) && value=="0")
248 set_layer_link_offsets_flag(false);
250 set_layer_link_offsets_flag(true);
252 if(settings.get_value("bline.auto_export",value) && value=="1")
253 set_auto_export_flag(true);
255 set_auto_export_flag(false);
257 if(settings.get_value("bline.id",value))
262 if(settings.get_value("bline.feather",value))
264 Real n = atof(value.c_str());
272 StateBLine_Context::save_settings()
275 settings.set_value("bline.layer_bline",get_layer_bline_flag()?"1":"0");
276 settings.set_value("bline.layer_region",get_layer_region_flag()?"1":"0");
277 settings.set_value("bline.layer_curve_gradient",get_layer_curve_gradient_flag()?"1":"0");
278 settings.set_value("bline.layer_link_offsets",get_layer_link_offsets_flag()?"1":"0");
279 settings.set_value("bline.auto_export",get_auto_export_flag()?"1":"0");
280 settings.set_value("bline.id",get_id().c_str());
281 settings.set_value("bline.feather",strprintf("%f",get_feather()));
285 StateBLine_Context::reset()
288 bline_point_list.clear();
293 StateBLine_Context::increment_id()
302 // If there is a number
303 // already at the end of the
304 // id, then remove it.
305 if(id[id.size()-1]<='9' && id[id.size()-1]>='0')
307 // figure out how many digits it is
309 (int)id.size()-1 >= digits && id[id.size()-1-digits] <= '9' && id[id.size()-1-digits] >= '0';
314 str_number=String(id,id.size()-digits,id.size());
315 id=String(id,0,id.size()-digits);
316 synfig::info("---------------- \"%s\"",str_number.c_str());
318 number=atoi(str_number.c_str());
328 // Add the number back onto the id
330 const String format(strprintf("%%0%dd",digits));
331 id+=strprintf(format.c_str(),number);
339 StateBLine_Context::StateBLine_Context(CanvasView* canvas_view):
340 canvas_view_(canvas_view),
341 is_working(*canvas_view),
343 prev_workarea_layer_status_(get_work_area()->get_allow_layer_clicks()),
344 duckmatic_push(get_work_area()),
345 settings(synfigapp::Main::get_selected_input_device()->settings()),
347 checkbutton_layer_region(_("Fill")),
348 checkbutton_layer_bline(_("Outline")),
349 checkbutton_layer_curve_gradient(_("Gradient")),
350 checkbutton_layer_link_offsets(_("Link Offsets")),
351 checkbutton_auto_export(_("Auto Export")),
352 button_make(_("Make")),
353 button_clear(_("Clear")),
354 adj_feather(0,0,10000,0.01,0.1),
355 spin_feather(adj_feather,0.01,4)
358 egress_on_selection_change=true;
361 // Set up the tool options dialog
362 //options_table.attach(*manage(new Gtk::Label(_("BLine Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
363 options_table.attach(entry_id, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
364 options_table.attach(checkbutton_layer_region, 0, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
365 options_table.attach(checkbutton_layer_bline, 0, 2, 3, 4, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
366 options_table.attach(checkbutton_layer_curve_gradient, 0, 2, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
367 options_table.attach(checkbutton_layer_link_offsets, 0, 2, 5, 6, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
368 options_table.attach(checkbutton_auto_export, 0, 2, 6, 7, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
369 options_table.attach(*manage(new Gtk::Label(_("Feather"))), 0, 1, 10, 11, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
370 options_table.attach(spin_feather, 1, 2, 10, 11, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
371 //options_table.attach(button_make, 0, 2, 5, 6, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
372 //button_make.signal_pressed().connect(sigc::mem_fun(*this,&StateBLine_Context::run));
373 options_table.show_all();
374 refresh_tool_options();
375 App::dialog_tool_options->present();
377 // Turn off layer clicking
378 get_work_area()->set_allow_layer_clicks(false);
380 // clear out the ducks
381 get_work_area()->clear_ducks();
383 // Refresh the work area
384 get_work_area()->queue_draw();
386 // Hide the tables if they are showing
387 prev_table_status=get_canvas_view()->tables_are_visible();
388 if(prev_table_status)get_canvas_view()->hide_tables();
390 // Disable the time bar
391 get_canvas_view()->set_sensitive_timebar(false);
394 //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StateBLine_Context::on_user_click));
395 get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
397 App::toolbox->refresh();
401 StateBLine_Context::refresh_tool_options()
403 App::dialog_tool_options->clear();
404 App::dialog_tool_options->set_widget(options_table);
405 App::dialog_tool_options->set_local_name(_("BLine Tool"));
406 App::dialog_tool_options->set_name("bline");
408 App::dialog_tool_options->add_button(
409 Gtk::StockID("gtk-execute"),
410 _("Make BLine and/or Region")
411 )->signal_clicked().connect(
412 sigc::hide_return(sigc::mem_fun(
414 &StateBLine_Context::run
418 App::dialog_tool_options->add_button(
419 Gtk::StockID("gtk-clear"),
420 _("Clear current BLine")
421 )->signal_clicked().connect(
424 &StateBLine_Context::reset
430 StateBLine_Context::event_refresh_tool_options(const Smach::event& /*x*/)
432 refresh_tool_options();
433 return Smach::RESULT_ACCEPT;
436 StateBLine_Context::~StateBLine_Context()
441 App::dialog_tool_options->clear();
443 get_canvas_view()->work_area->reset_cursor();
445 // Restore layer clicking
446 get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
448 // Enable the time bar
449 get_canvas_view()->set_sensitive_timebar(true);
451 // Bring back the tables if they were out before
452 if(prev_table_status)get_canvas_view()->show_tables();
454 // get_canvas_view()->get_smach().process_event(EVENT_REFRESH_DUCKS);
456 // Refresh the work area
457 get_work_area()->queue_draw();
459 App::toolbox->refresh();
463 StateBLine_Context::event_stop_handler(const Smach::event& /*x*/)
465 synfig::info("STATE RotoBLine: Received Stop Event");
468 // throw Smach::egress_exception();
469 // get_canvas_view()->get_smach().pop_state();
470 return Smach::RESULT_ACCEPT;
474 StateBLine_Context::event_refresh_handler(const Smach::event& /*x*/)
476 synfig::info("STATE RotoBLine: Received Refresh Event");
478 return Smach::RESULT_ACCEPT;
482 StateBLine_Context::run()
488 for(int i=5;i>0 && !success;i--)try
496 if(!success && !err.empty())
498 get_canvas_view()->get_ui_interface()->error(err);
504 StateBLine_Context::run_()
509 // Now we need to generate it
511 if(bline_point_list.empty())
515 if(bline_point_list.size()<2)
517 //get_canvas_view()->get_ui_interface()->error(_("You need at least two (2) points to create a BLine"));
524 // Create the action group
525 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New BLine"));
527 std::vector<BLinePoint> new_list;
528 std::list<synfig::ValueNode_Const::Handle>::iterator iter;
529 const synfig::TransformStack& transform(get_transform_stack());
531 for(iter=bline_point_list.begin();iter!=bline_point_list.end();++iter)
533 BLinePoint bline_point((*iter)->get_value().get(BLinePoint()));
534 Point new_vertex(transform.unperform(bline_point.get_vertex()));
536 bline_point.set_tangent1(
538 bline_point.get_tangent1()+bline_point.get_vertex()
542 bline_point.set_tangent2(
544 bline_point.get_tangent2()+bline_point.get_vertex()
548 bline_point.set_vertex(new_vertex);
550 new_list.push_back(bline_point);
553 ValueNode_BLine::Handle value_node_bline(ValueNode_BLine::create(new_list));
554 assert(value_node_bline);
556 ValueNode_Const::Handle value_node_offset(ValueNode_Const::create(Vector()));
557 assert(value_node_offset);
559 // Set the looping flag
560 value_node_bline->set_loop(loop_);
562 // Add the BLine to the canvas
563 if(get_auto_export_flag() && !get_canvas_interface()->add_value_node(value_node_bline,get_id()))
565 //get_canvas_view()->get_ui_interface()->error(_("Unable to add value node"));
568 throw String(_("Unable to add value node"));
574 // we are temporarily using the layer to hold something
575 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
580 depth=layer->get_depth();
582 canvas=layer->get_canvas();
588 canvas=get_canvas_view()->get_canvas();
590 synfigapp::SelectionManager::LayerList layer_selection;
592 // If we were asked to create a region layer, go ahead and do so
593 if(get_layer_region_flag())
595 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
597 Layer::Handle layer(get_canvas_interface()->add_layer_to("region",canvas,depth));
599 layer_selection.push_back(layer);
600 layer->set_description(get_id()+_(" Region"));
601 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
605 layer->set_param("feather",get_feather());
606 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
609 if(get_layer_bline_flag())
610 layer->set_param("color",synfigapp::Main::get_background_color());
612 // I don't know if it's safe to reuse the same layer_param_connect action, so I'm
613 // using 2 separate ones.
615 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
618 action->set_param("canvas",get_canvas());
619 action->set_param("canvas_interface",get_canvas_interface());
620 action->set_param("layer",layer);
621 if(!action->set_param("param",String("bline")))
622 synfig::error("LayerParamConnect didn't like \"param\"");
623 if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
624 synfig::error("LayerParamConnect didn't like \"value_node\"");
626 if(!get_canvas_interface()->get_instance()->perform_action(action))
628 //get_canvas_view()->get_ui_interface()->error(_("Unable to create Region layer"));
630 throw String(_("Unable to create Region layer"));
635 if (get_layer_link_offsets_flag())
637 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
640 action->set_param("canvas",get_canvas());
641 action->set_param("canvas_interface",get_canvas_interface());
642 action->set_param("layer",layer);
643 if(!action->set_param("param",String("offset")))
644 synfig::error("LayerParamConnect didn't like \"param\"");
645 if(!action->set_param("value_node",ValueNode::Handle(value_node_offset)))
646 synfig::error("LayerParamConnect didn't like \"value_node\"");
648 if(!get_canvas_interface()->get_instance()->perform_action(action))
650 //get_canvas_view()->get_ui_interface()->error(_("Unable to create Region layer"));
652 throw String(_("Unable to create Region layer"));
658 // If we were asked to create a BLine layer, go ahead and do so
659 if(get_layer_bline_flag())
661 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
663 Layer::Handle layer(get_canvas_interface()->add_layer_to("outline",canvas,depth));
665 layer_selection.push_back(layer);
666 layer->set_description(get_id()+_(" Outline"));
667 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
670 layer->set_param("feather",get_feather());
671 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
675 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
678 action->set_param("canvas",get_canvas());
679 action->set_param("canvas_interface",get_canvas_interface());
680 action->set_param("layer",layer);
681 if(!action->set_param("param",String("bline")))
682 synfig::error("LayerParamConnect didn't like \"param\"");
683 if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
684 synfig::error("LayerParamConnect didn't like \"value_node\"");
686 if(!get_canvas_interface()->get_instance()->perform_action(action))
688 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
690 throw String(_("Unable to create Outline layer"));
695 if (get_layer_link_offsets_flag())
697 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
700 action->set_param("canvas",get_canvas());
701 action->set_param("canvas_interface",get_canvas_interface());
702 action->set_param("layer",layer);
703 if(!action->set_param("param",String("offset")))
704 synfig::error("LayerParamConnect didn't like \"param\"");
705 if(!action->set_param("value_node",ValueNode::Handle(value_node_offset)))
706 synfig::error("LayerParamConnect didn't like \"value_node\"");
708 if(!get_canvas_interface()->get_instance()->perform_action(action))
710 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
712 throw String(_("Unable to create Outline layer"));
717 /*if(get_layer_region_flag() && !get_auto_export_flag())
719 get_canvas_interface()->auto_export(synfigapp::ValueDesc(layer,"bline"));
725 // If we were asked to create a CurveGradient layer, go ahead and do so
726 if(get_layer_curve_gradient_flag())
728 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
730 Layer::Handle layer(get_canvas_interface()->add_layer_to("curve_gradient",canvas,depth));
732 layer_selection.push_back(layer);
733 layer->set_description(get_id()+_(" Gradient"));
734 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
737 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
740 action->set_param("canvas",get_canvas());
741 action->set_param("canvas_interface",get_canvas_interface());
742 action->set_param("layer",layer);
743 if(!action->set_param("param",String("bline")))
744 synfig::error("LayerParamConnect didn't like \"param\"");
745 if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
746 synfig::error("LayerParamConnect didn't like \"value_node\"");
748 if(!get_canvas_interface()->get_instance()->perform_action(action))
750 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
752 throw String(_("Unable to create Gradient layer"));
757 if (get_layer_link_offsets_flag())
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("offset")))
766 synfig::error("LayerParamConnect didn't like \"param\"");
767 if(!action->set_param("value_node",ValueNode::Handle(value_node_offset)))
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 BLine layer"));
774 throw String(_("Unable to create Gradient layer"));
779 /*if(get_layer_region_flag() && !get_auto_export_flag())
781 get_canvas_interface()->auto_export(synfigapp::ValueDesc(layer,"bline"));
785 egress_on_selection_change=false;
786 get_canvas_interface()->get_selection_manager()->clear_selected_layers();
787 get_canvas_interface()->get_selection_manager()->set_selected_layers(layer_selection);
788 egress_on_selection_change=true;
790 //if(finish_bline_dialog.get_region_flag() || finish_bline_dialog.get_bline_flag())
791 // get_canvas_interface()->signal_dirty_preview()();
801 StateBLine_Context::event_mouse_motion_handler(const Smach::event& x)
803 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
807 //synfig::info("Moved Duck");
808 Point p(get_work_area()->snap_point_to_grid(event.pos));
809 curr_duck->set_trans_point(p);
811 next_duck->set_trans_point(p);
812 get_work_area()->queue_draw();
813 return Smach::RESULT_ACCEPT;
816 return Smach::RESULT_OK;
820 StateBLine_Context::event_mouse_release_handler(const Smach::event& /*x*/)
824 //synfig::info("Released current duck");
825 curr_duck->signal_edited()(curr_duck->get_point());
828 //synfig::info("grabbing next duck");
832 return Smach::RESULT_ACCEPT;
834 return Smach::RESULT_OK;
838 StateBLine_Context::event_mouse_click_handler(const Smach::event& x)
840 synfig::info("STATE BLINE: Received mouse button down Event");
841 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
846 // If we are already looped up, then don't try to add anything else
848 return Smach::RESULT_OK;
850 BLinePoint bline_point;
852 bline_point.set_vertex(get_work_area()->snap_point_to_grid(event.pos));
853 //bline_point.set_width(synfigapp::Main::get_bline_width());
854 bline_point.set_width(1.0f);
855 bline_point.set_origin(0.5f);
856 bline_point.set_split_tangent_flag(false);
857 bline_point.set_tangent1(Vector(0,0));
861 if(bline_point_list.empty())
863 bline_point.set_tangent1(Vector(1,1));
867 const Vector t(event.pos-bline_point_list.back()->get_value().get(BLinePoint()).get_vertex());
868 bline_point.set_tangent1(t);
871 if(bline_point_list.size()>1)
873 std::list<synfig::ValueNode_Const::Handle>::iterator iter;
874 iter=bline_point_list.end();
876 BLinePoint prev(bline_point_list.back()->get_value().get(BLinePoint()));
877 prev.set_tangent1(event.pos-(*iter)->get_value().get(BLinePoint()).get_vertex());
878 bline_point_list.back()->set_value(prev);
882 bline_point_list.push_back(ValueNode_Const::create(bline_point));
885 return Smach::RESULT_ACCEPT;
888 case BUTTON_RIGHT: // Intercept the right-button click to short-circuit the pop-up menu
889 if (!getenv("SYNFIG_ENABLE_POPUP_MENU_IN_ALL_TOOLS"))
890 return Smach::RESULT_ACCEPT;
893 return Smach::RESULT_OK;
898 StateBLine_Context::refresh_ducks(bool button_down)
900 get_work_area()->clear_ducks();
901 get_work_area()->queue_draw();
903 if(bline_point_list.empty())
906 list<ValueNode_Const::Handle>::iterator iter;
908 handle<WorkArea::Bezier> bezier;
909 handle<WorkArea::Duck> duck,tduck;
910 BLinePoint bline_point;
912 for(iter=bline_point_list.begin();iter!=bline_point_list.end();++iter)
914 ValueNode_Const::Handle value_node(*iter);
915 bline_point=(value_node->get_value().get(BLinePoint()));
919 // First add the duck associated with this vertex
920 duck=new WorkArea::Duck(bline_point.get_vertex());
921 duck->set_editable(true);
922 duck->set_type(Duck::TYPE_VERTEX);
923 duck->set_name(strprintf("%x-vertex",value_node.get()));
924 duck->signal_edited().connect(
925 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_vertex_change),value_node)
927 duck->signal_user_click(2).connect(
928 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::popup_vertex_menu),value_node)
930 duck->set_guid(value_node->get_guid()^GUID::hasher(0));
932 get_work_area()->add_duck(duck);
934 // Add the tangent1 duck
935 tduck=new WorkArea::Duck(bline_point.get_tangent1());
936 tduck->set_editable(true);
937 tduck->set_name(strprintf("%x-tangent1",value_node.get()));
938 tduck->set_origin(duck);
939 tduck->set_scalar(-0.33333333333333333);
940 tduck->set_tangent(true);
941 tduck->set_guid(value_node->get_guid()^GUID::hasher(3));
942 tduck->signal_edited().connect(
943 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_tangent1_change),value_node)
945 tduck->signal_user_click(2).connect(
946 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::popup_handle_menu),value_node)
949 // See if we need to add that duck to the previous bezier
952 get_work_area()->add_duck(tduck);
956 bezier->signal_user_click(2).connect(
960 &studio::StateBLine_Context::popup_bezier_menu
966 //get_work_area()->add_duck(bezier->c1);
967 //get_work_area()->add_duck(bezier->c2);
968 get_work_area()->add_bezier(bezier);
973 // Now we see if we need to create a bezier
974 list<ValueNode_Const::Handle>::iterator next(iter);
977 // If our next iterator is the end, then we don't need
979 //if(next==bline_point_list.end() && !loop_)
982 bezier=new WorkArea::Bezier();
984 // Add the tangent2 duck
985 tduck=new WorkArea::Duck(bline_point.get_tangent2());
986 tduck->set_editable(true);
987 tduck->set_origin(duck);
988 tduck->set_scalar(0.33333333333333333);
989 tduck->set_tangent(true);
990 if(bline_point.get_split_tangent_flag())
992 tduck->set_name(strprintf("%x-tangent2",value_node.get()));
993 tduck->signal_edited().connect(
994 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_tangent2_change),value_node)
999 tduck->set_name(strprintf("%x-tangent1",value_node.get()));
1000 tduck->signal_edited().connect(
1001 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_tangent1_change),value_node)
1004 tduck->set_guid(value_node->get_guid()^GUID::hasher(4));
1005 tduck->signal_user_click(2).connect(
1006 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::popup_handle_menu),value_node)
1009 // Setup the next bezier
1013 get_work_area()->add_duck(tduck);
1017 // Add the loop, if requested
1021 BLinePoint bline_point(bline_point_list.front()->get_value().get(BLinePoint()));
1023 duck=new WorkArea::Duck(bline_point.get_vertex());
1024 duck->set_editable(true);
1025 duck->set_name(strprintf("%x-vertex",bline_point_list.front().get()));
1026 duck->signal_edited().connect(
1027 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_vertex_change),bline_point_list.front())
1029 duck->signal_user_click(2).connect(
1030 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::popup_vertex_menu),bline_point_list.front())
1032 get_work_area()->add_duck(duck);
1034 // Add the tangent1 duck
1035 tduck=new WorkArea::Duck(bline_point.get_tangent1());
1036 tduck->set_editable(true);
1037 tduck->set_name(strprintf("%x-tangent1",bline_point_list.front().get()));
1038 tduck->set_origin(duck);
1039 tduck->set_scalar(-0.33333333333333333);
1040 tduck->set_tangent(true);
1041 tduck->signal_edited().connect(
1042 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_tangent1_change),bline_point_list.front())
1044 tduck->signal_user_click(2).connect(
1045 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::popup_handle_menu),bline_point_list.front())
1047 get_work_area()->add_duck(tduck);
1052 bezier->signal_user_click(2).connect(
1056 &studio::StateBLine_Context::popup_bezier_menu
1058 bline_point_list.front()
1062 //get_work_area()->add_duck(bezier->c1);
1063 get_work_area()->add_bezier(bezier);
1065 if(bezier && !loop_)
1067 duck=new WorkArea::Duck(bline_point.get_vertex());
1068 duck->set_editable(false);
1069 duck->set_name("temp");
1071 // Add the tangent1 duck
1072 tduck=new WorkArea::Duck(Vector(0,0));
1073 tduck->set_editable(false);
1074 tduck->set_name("ttemp");
1075 tduck->set_origin(duck);
1076 tduck->set_scalar(-0.33333333333333333);
1078 tduck->set_tangent(true);
1082 get_work_area()->add_duck(bezier->p2);
1083 //get_work_area()->add_duck(bezier->c2);
1084 get_work_area()->add_bezier(bezier);
1086 duck->set_guid(GUID());
1087 tduck->set_guid(GUID());
1098 curr_duck=next_duck;
1103 get_work_area()->queue_draw();
1108 StateBLine_Context::on_vertex_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node)
1110 BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1111 bline_point.set_vertex(point);
1112 value_node->set_value(bline_point);
1118 StateBLine_Context::on_tangent1_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node)
1120 BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1121 bline_point.set_tangent1(point);
1122 value_node->set_value(bline_point);
1128 StateBLine_Context::on_tangent2_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node)
1130 BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1131 bline_point.set_tangent2(point);
1132 value_node->set_value(bline_point);
1138 StateBLine_Context::loop_bline()
1142 refresh_ducks(false);
1146 StateBLine_Context::unloop_bline()
1150 refresh_ducks(false);
1154 StateBLine_Context::popup_vertex_menu(synfig::ValueNode_Const::Handle value_node)
1156 menu.items().clear();
1160 menu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Unloop BLine"),
1161 sigc::mem_fun(*this,&studio::StateBLine_Context::unloop_bline)
1164 menu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Loop BLine"),
1165 sigc::mem_fun(*this,&studio::StateBLine_Context::loop_bline)
1169 menu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Delete Vertex"),
1171 sigc::mem_fun(*this,&studio::StateBLine_Context::bline_delete_vertex),
1180 StateBLine_Context::popup_bezier_menu(float location, synfig::ValueNode_Const::Handle value_node)
1182 menu.items().clear();
1184 menu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Insert Vertex"),
1187 sigc::mem_fun(*this,&studio::StateBLine_Context::bline_insert_vertex),
1198 StateBLine_Context::bline_insert_vertex(synfig::ValueNode_Const::Handle value_node, float origin)
1200 list<ValueNode_Const::Handle>::iterator iter;
1202 for(iter=bline_point_list.begin();iter!=bline_point_list.end();++iter)
1203 if(*iter==value_node)
1205 list<ValueNode_Const::Handle>::iterator prev(iter);
1208 BLinePoint bline_point;
1210 BLinePoint next_bline_point((*iter)->get_value().get(BLinePoint()));
1211 BLinePoint prev_bline_point;
1213 if(iter!=bline_point_list.begin())
1215 prev_bline_point=(*prev)->get_value().get(BLinePoint());
1219 prev_bline_point.set_vertex(Point(0,0));
1220 prev_bline_point.set_width(next_bline_point.get_width());
1221 prev_bline_point.set_origin(0.5);
1222 prev_bline_point.set_split_tangent_flag(false);
1225 etl::hermite<Vector> curve(prev_bline_point.get_vertex(),next_bline_point.get_vertex(),prev_bline_point.get_tangent2(),next_bline_point.get_tangent1());
1226 etl::derivative< etl::hermite<Vector> > deriv(curve);
1228 bline_point.set_vertex(curve(origin));
1229 bline_point.set_width((next_bline_point.get_width()-prev_bline_point.get_width())*origin+prev_bline_point.get_width());
1230 #ifdef ETL_FIXED_DERIVATIVE
1231 bline_point.set_tangent1(deriv(origin)*std::min(1.0f-origin,origin));
1233 bline_point.set_tangent1(-deriv(origin)*std::min(1.0f-origin,origin));
1235 bline_point.set_tangent2(bline_point.get_tangent1());
1236 bline_point.set_split_tangent_flag(false);
1237 bline_point.set_origin(origin);
1240 bline_point.set_vertex((next_bline_point.get_vertex()+prev_bline_point.get_vertex())*0.5);
1241 bline_point.set_width((next_bline_point.get_width()+prev_bline_point.get_width())*0.5);
1242 bline_point.set_origin(origin);
1243 bline_point.set_split_tangent_flag(false);
1244 bline_point.set_tangent1((next_bline_point.get_vertex()-prev_bline_point.get_vertex())*0.5);
1247 bline_point_list.insert(iter,ValueNode_Const::create(bline_point));
1251 if(iter==bline_point_list.end())
1253 get_canvas_view()->get_ui_interface()->error(_("Unable to find where to insert vertex, internal error, please report this bug"));
1256 refresh_ducks(false);
1260 StateBLine_Context::bline_delete_vertex(synfig::ValueNode_Const::Handle value_node)
1262 list<ValueNode_Const::Handle>::iterator iter;
1264 for(iter=bline_point_list.begin();iter!=bline_point_list.end();++iter)
1265 if(*iter==value_node)
1267 bline_point_list.erase(iter);
1270 if(iter==bline_point_list.end())
1272 get_canvas_view()->get_ui_interface()->error(_("Unable to remove vertex, internal error, please report this bug"));
1275 refresh_ducks(false);
1279 StateBLine_Context::popup_handle_menu(synfig::ValueNode_Const::Handle value_node)
1281 menu.items().clear();
1283 BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1285 if(bline_point.get_split_tangent_flag())
1286 menu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Merge Tangents"),
1288 sigc::mem_fun(*this,&studio::StateBLine_Context::bline_attach_handle),
1293 menu.items().push_back(Gtk::Menu_Helpers::MenuElem(_("Split Tangents"),
1295 sigc::mem_fun(*this,&studio::StateBLine_Context::bline_detach_handle),
1304 StateBLine_Context::bline_detach_handle(synfig::ValueNode_Const::Handle value_node)
1306 BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1307 bline_point.set_split_tangent_flag(true);
1308 bline_point.set_tangent2(bline_point.get_tangent1());
1309 value_node->set_value(bline_point);
1310 refresh_ducks(false);
1314 StateBLine_Context::bline_attach_handle(synfig::ValueNode_Const::Handle value_node)
1316 BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1317 bline_point.set_tangent1((bline_point.get_tangent1()+bline_point.get_tangent2())*0.5);
1318 bline_point.set_split_tangent_flag(false);
1319 value_node->set_value(bline_point);
1320 refresh_ducks(false);