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>
56 /* === U S I N G =========================================================== */
60 using namespace synfig;
61 using namespace studio;
63 /* === M A C R O S ========================================================= */
65 /* === G L O B A L S ======================================================= */
67 StateBLine studio::state_bline;
69 /* === C L A S S E S & S T R U C T S ======================================= */
71 class studio::StateBLine_Context : public sigc::trackable
73 etl::handle<CanvasView> canvas_view_;
74 CanvasView::IsWorking is_working;
76 bool prev_table_status;
78 bool prev_workarea_layer_status_;
81 Canvas::Handle canvas;
85 Duckmatic::Push duckmatic_push;
87 etl::handle<Duck> curr_duck;
89 etl::handle<Duck> next_duck;
91 std::list<synfig::ValueNode_Const::Handle> bline_point_list;
92 synfigapp::Settings& settings;
94 bool on_vertex_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node);
95 bool on_tangent1_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node);
96 bool on_tangent2_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node);
99 void popup_handle_menu(synfig::ValueNode_Const::Handle value_node);
100 void popup_vertex_menu(synfig::ValueNode_Const::Handle value_node);
101 void popup_bezier_menu(float location, synfig::ValueNode_Const::Handle value_node);
103 void bline_detach_handle(synfig::ValueNode_Const::Handle value_node);
104 void bline_attach_handle(synfig::ValueNode_Const::Handle value_node);
105 void bline_delete_vertex(synfig::ValueNode_Const::Handle value_node);
106 void bline_insert_vertex(synfig::ValueNode_Const::Handle value_node,float origin=0.5);
110 void refresh_ducks(bool x=true);
112 Gtk::Table options_table;
114 Gtk::CheckButton checkbutton_layer_region;
115 Gtk::CheckButton checkbutton_layer_bline;
116 Gtk::CheckButton checkbutton_layer_curve_gradient;
117 Gtk::CheckButton checkbutton_layer_link_offsets;
118 Gtk::CheckButton checkbutton_auto_export;
119 Gtk::Button button_make;
120 Gtk::Button button_clear;
121 Gtk::Adjustment adj_feather;
122 Gtk::SpinButton spin_feather;
128 int layers_to_create()const
131 get_layer_region_flag()+
132 get_layer_bline_flag()+
133 get_layer_curve_gradient_flag();
138 if(layers_to_create()==0)
139 set_layer_region_flag(true);
142 bool get_auto_export_flag()const { return checkbutton_auto_export.get_active(); }
143 void set_auto_export_flag(bool x) { return checkbutton_auto_export.set_active(x); }
145 bool get_layer_region_flag()const { return checkbutton_layer_region.get_active(); }
146 void set_layer_region_flag(bool x) { return checkbutton_layer_region.set_active(x); }
148 bool get_layer_bline_flag()const { return checkbutton_layer_bline.get_active(); }
149 void set_layer_bline_flag(bool x) { return checkbutton_layer_bline.set_active(x); }
151 bool get_layer_curve_gradient_flag()const { return checkbutton_layer_curve_gradient.get_active(); }
152 void set_layer_curve_gradient_flag(bool x) { return checkbutton_layer_curve_gradient.set_active(x); }
154 bool get_layer_link_offsets_flag()const { return checkbutton_layer_link_offsets.get_active(); }
155 void set_layer_link_offsets_flag(bool x) { return checkbutton_layer_link_offsets.set_active(x); }
157 Real get_feather() const { return adj_feather.get_value(); }
158 void set_feather(Real x) { return adj_feather.set_value(x); }
159 synfig::String get_id()const { return entry_id.get_text(); }
160 void set_id(const synfig::String& x) { return entry_id.set_text(x); }
162 Smach::event_result event_stop_handler(const Smach::event& x);
164 Smach::event_result event_refresh_handler(const Smach::event& x);
166 Smach::event_result event_mouse_click_handler(const Smach::event& x);
167 Smach::event_result event_mouse_release_handler(const Smach::event& x);
168 Smach::event_result event_mouse_motion_handler(const Smach::event& x);
169 Smach::event_result event_refresh_tool_options(const Smach::event& x);
171 Smach::event_result event_hijack(const Smach::event& /*x*/) { return Smach::RESULT_ACCEPT; }
173 void refresh_tool_options();
175 StateBLine_Context(CanvasView* canvas_view);
177 ~StateBLine_Context();
179 const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
180 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
181 synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
182 WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
183 const synfig::TransformStack& get_transform_stack()const { return canvas_view_->get_curr_transform_stack(); }
185 void load_settings();
186 void save_settings();
189 //void on_user_click(synfig::Point point);
194 bool egress_on_selection_change;
195 Smach::event_result event_layer_selection_changed_handler(const Smach::event& /*x*/)
197 if(egress_on_selection_change)
198 throw Smach::egress_exception();
199 return Smach::RESULT_OK;
202 }; // END of class StateBLine_Context
205 /* === M E T H O D S ======================================================= */
207 StateBLine::StateBLine():
208 Smach::state<StateBLine_Context>("bline")
210 insert(event_def(EVENT_LAYER_SELECTION_CHANGED,&StateBLine_Context::event_layer_selection_changed_handler));
211 insert(event_def(EVENT_STOP,&StateBLine_Context::event_stop_handler));
212 insert(event_def(EVENT_REFRESH,&StateBLine_Context::event_refresh_handler));
213 insert(event_def(EVENT_REFRESH_DUCKS,&StateBLine_Context::event_hijack));
214 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateBLine_Context::event_mouse_click_handler));
215 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_UP,&StateBLine_Context::event_mouse_release_handler));
216 insert(event_def(EVENT_WORKAREA_MOUSE_MOTION,&StateBLine_Context::event_mouse_motion_handler));
217 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DRAG,&StateBLine_Context::event_mouse_motion_handler));
218 insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateBLine_Context::event_refresh_tool_options));
221 StateBLine::~StateBLine()
226 StateBLine_Context::load_settings()
230 if(settings.get_value("bline.layer_region",value) && value=="0")
231 set_layer_region_flag(false);
233 set_layer_region_flag(true);
235 if(settings.get_value("bline.layer_bline",value) && value=="0")
236 set_layer_bline_flag(false);
238 set_layer_bline_flag(true);
240 if(settings.get_value("bline.layer_curve_gradient",value) && value=="1")
241 set_layer_curve_gradient_flag(true);
243 set_layer_curve_gradient_flag(false);
245 if(settings.get_value("bline.layer_link_offsets",value) && value=="0")
246 set_layer_link_offsets_flag(false);
248 set_layer_link_offsets_flag(true);
250 if(settings.get_value("bline.auto_export",value) && value=="1")
251 set_auto_export_flag(true);
253 set_auto_export_flag(false);
255 if(settings.get_value("bline.id",value))
260 if(settings.get_value("bline.feather",value))
262 Real n = atof(value.c_str());
270 StateBLine_Context::save_settings()
273 settings.set_value("bline.layer_bline",get_layer_bline_flag()?"1":"0");
274 settings.set_value("bline.layer_region",get_layer_region_flag()?"1":"0");
275 settings.set_value("bline.layer_curve_gradient",get_layer_curve_gradient_flag()?"1":"0");
276 settings.set_value("bline.layer_link_offsets",get_layer_link_offsets_flag()?"1":"0");
277 settings.set_value("bline.auto_export",get_auto_export_flag()?"1":"0");
278 settings.set_value("bline.id",get_id().c_str());
279 settings.set_value("bline.feather",strprintf("%f",get_feather()));
283 StateBLine_Context::reset()
286 bline_point_list.clear();
291 StateBLine_Context::increment_id()
300 // If there is a number
301 // already at the end of the
302 // id, then remove it.
303 if(id[id.size()-1]<='9' && id[id.size()-1]>='0')
305 // figure out how many digits it is
306 for(digits=0;(int)id.size()-1>=digits && id[id.size()-1-digits]<='9' && id[id.size()-1-digits]>='0';digits++)while(false);
309 str_number=String(id,id.size()-digits,id.size());
310 id=String(id,0,id.size()-digits);
311 synfig::info("---------------- \"%s\"",str_number.c_str());
313 number=atoi(str_number.c_str());
323 // Add the number back onto the id
325 const String format(strprintf("%%0%dd",digits));
326 id+=strprintf(format.c_str(),number);
334 StateBLine_Context::StateBLine_Context(CanvasView* canvas_view):
335 canvas_view_(canvas_view),
336 is_working(*canvas_view),
338 prev_workarea_layer_status_(get_work_area()->get_allow_layer_clicks()),
339 duckmatic_push(get_work_area()),
340 settings(synfigapp::Main::get_selected_input_device()->settings()),
342 checkbutton_layer_region(_("Fill")),
343 checkbutton_layer_bline(_("Outline")),
344 checkbutton_layer_curve_gradient(_("Gradient")),
345 checkbutton_layer_link_offsets(_("Link Offsets")),
346 checkbutton_auto_export(_("Auto Export")),
347 button_make(_("Make")),
348 button_clear(_("Clear")),
349 adj_feather(0,0,10000,0.01,0.1),
350 spin_feather(adj_feather,0.01,4)
353 egress_on_selection_change=true;
356 // Set up the tool options dialog
357 //options_table.attach(*manage(new Gtk::Label(_("BLine Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
358 options_table.attach(entry_id, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
359 options_table.attach(checkbutton_layer_region, 0, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
360 options_table.attach(checkbutton_layer_bline, 0, 2, 3, 4, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
361 options_table.attach(checkbutton_layer_curve_gradient, 0, 2, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
362 options_table.attach(checkbutton_layer_link_offsets, 0, 2, 5, 6, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
363 options_table.attach(checkbutton_auto_export, 0, 2, 6, 7, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
364 options_table.attach(*manage(new Gtk::Label(_("Feather"))), 0, 1, 10, 11, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
365 options_table.attach(spin_feather, 1, 2, 10, 11, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
366 //options_table.attach(button_make, 0, 2, 5, 6, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
367 //button_make.signal_pressed().connect(sigc::mem_fun(*this,&StateBLine_Context::run));
368 options_table.show_all();
369 refresh_tool_options();
370 App::dialog_tool_options->present();
372 // Turn off layer clicking
373 get_work_area()->set_allow_layer_clicks(false);
375 // clear out the ducks
376 get_work_area()->clear_ducks();
378 // Refresh the work area
379 get_work_area()->queue_draw();
381 // Hide the tables if they are showing
382 prev_table_status=get_canvas_view()->tables_are_visible();
383 if(prev_table_status)get_canvas_view()->hide_tables();
385 // Disable the time bar
386 get_canvas_view()->set_sensitive_timebar(false);
389 //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StateBLine_Context::on_user_click));
390 get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
392 App::toolbox->refresh();
396 StateBLine_Context::refresh_tool_options()
398 App::dialog_tool_options->clear();
399 App::dialog_tool_options->set_widget(options_table);
400 App::dialog_tool_options->set_local_name(_("BLine Tool"));
401 App::dialog_tool_options->set_name("bline");
403 App::dialog_tool_options->add_button(
404 Gtk::StockID("gtk-execute"),
405 _("Make BLine and/or Region")
406 )->signal_clicked().connect(
407 sigc::hide_return(sigc::mem_fun(
409 &StateBLine_Context::run
413 App::dialog_tool_options->add_button(
414 Gtk::StockID("gtk-clear"),
415 _("Clear current BLine")
416 )->signal_clicked().connect(
419 &StateBLine_Context::reset
425 StateBLine_Context::event_refresh_tool_options(const Smach::event& /*x*/)
427 refresh_tool_options();
428 return Smach::RESULT_ACCEPT;
431 StateBLine_Context::~StateBLine_Context()
436 App::dialog_tool_options->clear();
438 get_canvas_view()->work_area->reset_cursor();
440 // Restore layer clicking
441 get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
443 // Enable the time bar
444 get_canvas_view()->set_sensitive_timebar(true);
446 // Bring back the tables if they were out before
447 if(prev_table_status)get_canvas_view()->show_tables();
449 // get_canvas_view()->get_smach().process_event(EVENT_REFRESH_DUCKS);
451 // Refresh the work area
452 get_work_area()->queue_draw();
454 App::toolbox->refresh();
458 StateBLine_Context::event_stop_handler(const Smach::event& /*x*/)
460 synfig::info("STATE RotoBLine: Received Stop Event");
463 // throw Smach::egress_exception();
464 // get_canvas_view()->get_smach().pop_state();
465 return Smach::RESULT_ACCEPT;
469 StateBLine_Context::event_refresh_handler(const Smach::event& /*x*/)
471 synfig::info("STATE RotoBLine: Received Refresh Event");
473 return Smach::RESULT_ACCEPT;
477 StateBLine_Context::run()
483 for(int i=5;i>0 && !success;i--)try
491 if(!success && !err.empty())
493 get_canvas_view()->get_ui_interface()->error(err);
499 StateBLine_Context::run_()
504 // Now we need to generate it
506 if(bline_point_list.empty())
510 if(bline_point_list.size()<2)
512 //get_canvas_view()->get_ui_interface()->error(_("You need at least two (2) points to create a BLine"));
519 // Create the action group
520 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New BLine"));
522 std::vector<BLinePoint> new_list;
523 std::list<synfig::ValueNode_Const::Handle>::iterator iter;
524 const synfig::TransformStack& transform(get_transform_stack());
526 for(iter=bline_point_list.begin();iter!=bline_point_list.end();++iter)
528 BLinePoint bline_point((*iter)->get_value().get(BLinePoint()));
529 Point new_vertex(transform.unperform(bline_point.get_vertex()));
531 bline_point.set_tangent1(
533 bline_point.get_tangent1()+bline_point.get_vertex()
537 bline_point.set_tangent2(
539 bline_point.get_tangent2()+bline_point.get_vertex()
543 bline_point.set_vertex(new_vertex);
545 new_list.push_back(bline_point);
548 ValueNode_BLine::Handle value_node_bline(ValueNode_BLine::create(new_list));
549 assert(value_node_bline);
551 ValueNode_Const::Handle value_node_offset(ValueNode_Const::create(Vector()));
552 assert(value_node_offset);
554 // Set the looping flag
555 value_node_bline->set_loop(loop_);
557 // Add the BLine to the canvas
558 if(get_auto_export_flag() && !get_canvas_interface()->add_value_node(value_node_bline,get_id()))
560 //get_canvas_view()->get_ui_interface()->error(_("Unable to add value node"));
563 throw String(_("Unable to add value node"));
569 // we are temporarily using the layer to hold something
570 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
575 depth=layer->get_depth();
577 canvas=layer->get_canvas();
583 canvas=get_canvas_view()->get_canvas();
585 synfigapp::SelectionManager::LayerList layer_selection;
587 // If we were asked to create a region layer, go ahead and do so
588 if(get_layer_region_flag())
590 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
592 Layer::Handle layer(get_canvas_interface()->add_layer_to("region",canvas,depth));
594 layer_selection.push_back(layer);
595 layer->set_description(get_id()+_(" Region"));
596 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
600 layer->set_param("feather",get_feather());
601 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
604 if(get_layer_bline_flag())
605 layer->set_param("color",synfigapp::Main::get_background_color());
607 // I don't know if it's safe to reuse the same layer_param_connect action, so I'm
608 // using 2 separate ones.
610 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
613 action->set_param("canvas",get_canvas());
614 action->set_param("canvas_interface",get_canvas_interface());
615 action->set_param("layer",layer);
616 if(!action->set_param("param",String("bline")))
617 synfig::error("LayerParamConnect didn't like \"param\"");
618 if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
619 synfig::error("LayerParamConnect didn't like \"value_node\"");
621 if(!get_canvas_interface()->get_instance()->perform_action(action))
623 //get_canvas_view()->get_ui_interface()->error(_("Unable to create Region layer"));
625 throw String(_("Unable to create Region layer"));
630 if (get_layer_link_offsets_flag())
632 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
635 action->set_param("canvas",get_canvas());
636 action->set_param("canvas_interface",get_canvas_interface());
637 action->set_param("layer",layer);
638 if(!action->set_param("param",String("offset")))
639 synfig::error("LayerParamConnect didn't like \"param\"");
640 if(!action->set_param("value_node",ValueNode::Handle(value_node_offset)))
641 synfig::error("LayerParamConnect didn't like \"value_node\"");
643 if(!get_canvas_interface()->get_instance()->perform_action(action))
645 //get_canvas_view()->get_ui_interface()->error(_("Unable to create Region layer"));
647 throw String(_("Unable to create Region layer"));
653 // If we were asked to create a BLine layer, go ahead and do so
654 if(get_layer_bline_flag())
656 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
658 Layer::Handle layer(get_canvas_interface()->add_layer_to("outline",canvas,depth));
660 layer_selection.push_back(layer);
661 layer->set_description(get_id()+_(" Outline"));
662 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
665 layer->set_param("feather",get_feather());
666 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
670 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
673 action->set_param("canvas",get_canvas());
674 action->set_param("canvas_interface",get_canvas_interface());
675 action->set_param("layer",layer);
676 if(!action->set_param("param",String("bline")))
677 synfig::error("LayerParamConnect didn't like \"param\"");
678 if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
679 synfig::error("LayerParamConnect didn't like \"value_node\"");
681 if(!get_canvas_interface()->get_instance()->perform_action(action))
683 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
685 throw String(_("Unable to create Outline layer"));
690 if (get_layer_link_offsets_flag())
692 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
695 action->set_param("canvas",get_canvas());
696 action->set_param("canvas_interface",get_canvas_interface());
697 action->set_param("layer",layer);
698 if(!action->set_param("param",String("offset")))
699 synfig::error("LayerParamConnect didn't like \"param\"");
700 if(!action->set_param("value_node",ValueNode::Handle(value_node_offset)))
701 synfig::error("LayerParamConnect didn't like \"value_node\"");
703 if(!get_canvas_interface()->get_instance()->perform_action(action))
705 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
707 throw String(_("Unable to create Outline layer"));
712 /*if(get_layer_region_flag() && !get_auto_export_flag())
714 get_canvas_interface()->auto_export(synfigapp::ValueDesc(layer,"bline"));
720 // If we were asked to create a CurveGradient layer, go ahead and do so
721 if(get_layer_curve_gradient_flag())
723 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
725 Layer::Handle layer(get_canvas_interface()->add_layer_to("curve_gradient",canvas,depth));
727 layer_selection.push_back(layer);
728 layer->set_description(get_id()+_(" Gradient"));
729 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
732 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
735 action->set_param("canvas",get_canvas());
736 action->set_param("canvas_interface",get_canvas_interface());
737 action->set_param("layer",layer);
738 if(!action->set_param("param",String("bline")))
739 synfig::error("LayerParamConnect didn't like \"param\"");
740 if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
741 synfig::error("LayerParamConnect didn't like \"value_node\"");
743 if(!get_canvas_interface()->get_instance()->perform_action(action))
745 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
747 throw String(_("Unable to create Gradient layer"));
752 if (get_layer_link_offsets_flag())
754 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
757 action->set_param("canvas",get_canvas());
758 action->set_param("canvas_interface",get_canvas_interface());
759 action->set_param("layer",layer);
760 if(!action->set_param("param",String("offset")))
761 synfig::error("LayerParamConnect didn't like \"param\"");
762 if(!action->set_param("value_node",ValueNode::Handle(value_node_offset)))
763 synfig::error("LayerParamConnect didn't like \"value_node\"");
765 if(!get_canvas_interface()->get_instance()->perform_action(action))
767 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
769 throw String(_("Unable to create Gradient layer"));
774 /*if(get_layer_region_flag() && !get_auto_export_flag())
776 get_canvas_interface()->auto_export(synfigapp::ValueDesc(layer,"bline"));
780 egress_on_selection_change=false;
781 get_canvas_interface()->get_selection_manager()->clear_selected_layers();
782 get_canvas_interface()->get_selection_manager()->set_selected_layers(layer_selection);
783 egress_on_selection_change=true;
785 //if(finish_bline_dialog.get_region_flag() || finish_bline_dialog.get_bline_flag())
786 // get_canvas_interface()->signal_dirty_preview()();
796 StateBLine_Context::event_mouse_motion_handler(const Smach::event& x)
798 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
802 //synfig::info("Moved Duck");
803 Point p(get_work_area()->snap_point_to_grid(event.pos));
804 curr_duck->set_trans_point(p);
806 next_duck->set_trans_point(p);
807 get_work_area()->queue_draw();
808 return Smach::RESULT_ACCEPT;
811 return Smach::RESULT_OK;
815 StateBLine_Context::event_mouse_release_handler(const Smach::event& /*x*/)
819 //synfig::info("Released current duck");
820 curr_duck->signal_edited()(curr_duck->get_point());
823 //synfig::info("grabbing next duck");
827 return Smach::RESULT_ACCEPT;
829 return Smach::RESULT_OK;
833 StateBLine_Context::event_mouse_click_handler(const Smach::event& x)
835 synfig::info("STATE BLINE: Received mouse button down Event");
836 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
841 // If we are already looped up, then don't try to add anything else
843 return Smach::RESULT_OK;
845 BLinePoint bline_point;
847 bline_point.set_vertex(get_work_area()->snap_point_to_grid(event.pos));
848 //bline_point.set_width(synfigapp::Main::get_bline_width());
849 bline_point.set_width(1.0f);
850 bline_point.set_origin(0.5f);
851 bline_point.set_split_tangent_flag(false);
852 bline_point.set_tangent1(Vector(0,0));
856 if(bline_point_list.empty())
858 bline_point.set_tangent1(Vector(1,1));
862 const Vector t(event.pos-bline_point_list.back()->get_value().get(BLinePoint()).get_vertex());
863 bline_point.set_tangent1(t);
866 if(bline_point_list.size()>1)
868 std::list<synfig::ValueNode_Const::Handle>::iterator iter;
869 iter=bline_point_list.end();
871 BLinePoint prev(bline_point_list.back()->get_value().get(BLinePoint()));
872 prev.set_tangent1(event.pos-(*iter)->get_value().get(BLinePoint()).get_vertex());
873 bline_point_list.back()->set_value(prev);
877 bline_point_list.push_back(ValueNode_Const::create(bline_point));
880 return Smach::RESULT_ACCEPT;
883 case BUTTON_RIGHT: // Intercept the right-button click to short-circut the pop-up menu
884 return Smach::RESULT_ACCEPT;
887 return Smach::RESULT_OK;
892 StateBLine_Context::refresh_ducks(bool button_down)
894 get_work_area()->clear_ducks();
895 get_work_area()->queue_draw();
897 if(bline_point_list.empty())
900 list<ValueNode_Const::Handle>::iterator iter;
902 handle<WorkArea::Bezier> bezier;
903 handle<WorkArea::Duck> duck,tduck;
904 BLinePoint bline_point;
906 for(iter=bline_point_list.begin();iter!=bline_point_list.end();++iter)
908 ValueNode_Const::Handle value_node(*iter);
909 bline_point=(value_node->get_value().get(BLinePoint()));
913 // First add the duck associated with this vertex
914 duck=new WorkArea::Duck(bline_point.get_vertex());
915 duck->set_editable(true);
916 duck->set_type(Duck::TYPE_VERTEX);
917 duck->set_name(strprintf("%x-vertex",value_node.get()));
918 duck->signal_edited().connect(
919 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_vertex_change),value_node)
921 duck->signal_user_click(2).connect(
922 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::popup_vertex_menu),value_node)
924 duck->set_guid(value_node->get_guid()^GUID::hasher(0));
926 get_work_area()->add_duck(duck);
928 // Add the tangent1 duck
929 tduck=new WorkArea::Duck(bline_point.get_tangent1());
930 tduck->set_editable(true);
931 tduck->set_name(strprintf("%x-tangent1",value_node.get()));
932 tduck->set_origin(duck);
933 tduck->set_scalar(-0.33333333333333333);
934 tduck->set_tangent(true);
935 tduck->set_guid(value_node->get_guid()^GUID::hasher(3));
936 tduck->signal_edited().connect(
937 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_tangent1_change),value_node)
939 tduck->signal_user_click(2).connect(
940 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::popup_handle_menu),value_node)
943 // See if we need to add that duck to the previous bezier
946 get_work_area()->add_duck(tduck);
950 bezier->signal_user_click(2).connect(
954 &studio::StateBLine_Context::popup_bezier_menu
960 //get_work_area()->add_duck(bezier->c1);
961 //get_work_area()->add_duck(bezier->c2);
962 get_work_area()->add_bezier(bezier);
967 // Now we see if we need to create a bezier
968 list<ValueNode_Const::Handle>::iterator next(iter);
971 // If our next iterator is the end, then we don't need
973 //if(next==bline_point_list.end() && !loop_)
976 bezier=new WorkArea::Bezier();
978 // Add the tangent2 duck
979 tduck=new WorkArea::Duck(bline_point.get_tangent2());
980 tduck->set_editable(true);
981 tduck->set_origin(duck);
982 tduck->set_scalar(0.33333333333333333);
983 tduck->set_tangent(true);
984 if(bline_point.get_split_tangent_flag())
986 tduck->set_name(strprintf("%x-tangent2",value_node.get()));
987 tduck->signal_edited().connect(
988 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_tangent2_change),value_node)
993 tduck->set_name(strprintf("%x-tangent1",value_node.get()));
994 tduck->signal_edited().connect(
995 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_tangent1_change),value_node)
998 tduck->set_guid(value_node->get_guid()^GUID::hasher(4));
999 tduck->signal_user_click(2).connect(
1000 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::popup_handle_menu),value_node)
1003 // Setup the next bezier
1007 get_work_area()->add_duck(tduck);
1011 // Add the loop, if requested
1015 BLinePoint bline_point(bline_point_list.front()->get_value().get(BLinePoint()));
1017 duck=new WorkArea::Duck(bline_point.get_vertex());
1018 duck->set_editable(true);
1019 duck->set_name(strprintf("%x-vertex",bline_point_list.front().get()));
1020 duck->signal_edited().connect(
1021 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_vertex_change),bline_point_list.front())
1023 duck->signal_user_click(2).connect(
1024 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::popup_vertex_menu),bline_point_list.front())
1026 get_work_area()->add_duck(duck);
1028 // Add the tangent1 duck
1029 tduck=new WorkArea::Duck(bline_point.get_tangent1());
1030 tduck->set_editable(true);
1031 tduck->set_name(strprintf("%x-tangent1",bline_point_list.front().get()));
1032 tduck->set_origin(duck);
1033 tduck->set_scalar(-0.33333333333333333);
1034 tduck->set_tangent(true);
1035 tduck->signal_edited().connect(
1036 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_tangent1_change),bline_point_list.front())
1038 tduck->signal_user_click(2).connect(
1039 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::popup_handle_menu),bline_point_list.front())
1041 get_work_area()->add_duck(tduck);
1046 bezier->signal_user_click(2).connect(
1050 &studio::StateBLine_Context::popup_bezier_menu
1052 bline_point_list.front()
1056 //get_work_area()->add_duck(bezier->c1);
1057 get_work_area()->add_bezier(bezier);
1059 if(bezier && !loop_)
1061 duck=new WorkArea::Duck(bline_point.get_vertex());
1062 duck->set_editable(false);
1063 duck->set_name("temp");
1065 // Add the tangent1 duck
1066 tduck=new WorkArea::Duck(Vector(0,0));
1067 tduck->set_editable(false);
1068 tduck->set_name("ttemp");
1069 tduck->set_origin(duck);
1070 tduck->set_scalar(-0.33333333333333333);
1072 tduck->set_tangent(true);
1076 get_work_area()->add_duck(bezier->p2);
1077 //get_work_area()->add_duck(bezier->c2);
1078 get_work_area()->add_bezier(bezier);
1080 duck->set_guid(GUID());
1081 tduck->set_guid(GUID());
1092 curr_duck=next_duck;
1097 get_work_area()->queue_draw();
1102 StateBLine_Context::on_vertex_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node)
1104 BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1105 bline_point.set_vertex(point);
1106 value_node->set_value(bline_point);
1112 StateBLine_Context::on_tangent1_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node)
1114 BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1115 bline_point.set_tangent1(point);
1116 value_node->set_value(bline_point);
1122 StateBLine_Context::on_tangent2_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node)
1124 BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1125 bline_point.set_tangent2(point);
1126 value_node->set_value(bline_point);
1132 StateBLine_Context::loop_bline()
1136 refresh_ducks(false);
1140 StateBLine_Context::unloop_bline()
1144 refresh_ducks(false);
1148 StateBLine_Context::popup_vertex_menu(synfig::ValueNode_Const::Handle value_node)
1150 menu.items().clear();
1154 menu.items().push_back(Gtk::Menu_Helpers::MenuElem("Unloop BLine",
1155 sigc::mem_fun(*this,&studio::StateBLine_Context::unloop_bline)
1158 menu.items().push_back(Gtk::Menu_Helpers::MenuElem("Loop BLine",
1159 sigc::mem_fun(*this,&studio::StateBLine_Context::loop_bline)
1163 menu.items().push_back(Gtk::Menu_Helpers::MenuElem("Delete Vertex",
1165 sigc::mem_fun(*this,&studio::StateBLine_Context::bline_delete_vertex),
1174 StateBLine_Context::popup_bezier_menu(float location, synfig::ValueNode_Const::Handle value_node)
1176 menu.items().clear();
1178 menu.items().push_back(Gtk::Menu_Helpers::MenuElem("Insert Vertex",
1181 sigc::mem_fun(*this,&studio::StateBLine_Context::bline_insert_vertex),
1192 StateBLine_Context::bline_insert_vertex(synfig::ValueNode_Const::Handle value_node, float origin)
1194 list<ValueNode_Const::Handle>::iterator iter;
1196 for(iter=bline_point_list.begin();iter!=bline_point_list.end();++iter)
1197 if(*iter==value_node)
1199 list<ValueNode_Const::Handle>::iterator prev(iter);
1202 BLinePoint bline_point;
1204 BLinePoint next_bline_point((*iter)->get_value().get(BLinePoint()));
1205 BLinePoint prev_bline_point;
1207 if(iter!=bline_point_list.begin())
1209 prev_bline_point=(*prev)->get_value().get(BLinePoint());
1213 prev_bline_point.set_vertex(Point(0,0));
1214 prev_bline_point.set_width(next_bline_point.get_width());
1215 prev_bline_point.set_origin(0.5);
1216 prev_bline_point.set_split_tangent_flag(false);
1219 etl::hermite<Vector> curve(prev_bline_point.get_vertex(),next_bline_point.get_vertex(),prev_bline_point.get_tangent2(),next_bline_point.get_tangent1());
1220 etl::derivative< etl::hermite<Vector> > deriv(curve);
1222 bline_point.set_vertex(curve(origin));
1223 bline_point.set_width((next_bline_point.get_width()-prev_bline_point.get_width())*origin+prev_bline_point.get_width());
1224 #ifdef ETL_FIXED_DERIVATIVE
1225 bline_point.set_tangent1(deriv(origin)*std::min(1.0f-origin,origin));
1227 bline_point.set_tangent1(-deriv(origin)*std::min(1.0f-origin,origin));
1229 bline_point.set_tangent2(bline_point.get_tangent1());
1230 bline_point.set_split_tangent_flag(false);
1231 bline_point.set_origin(origin);
1234 bline_point.set_vertex((next_bline_point.get_vertex()+prev_bline_point.get_vertex())*0.5);
1235 bline_point.set_width((next_bline_point.get_width()+prev_bline_point.get_width())*0.5);
1236 bline_point.set_origin(origin);
1237 bline_point.set_split_tangent_flag(false);
1238 bline_point.set_tangent1((next_bline_point.get_vertex()-prev_bline_point.get_vertex())*0.5);
1241 bline_point_list.insert(iter,ValueNode_Const::create(bline_point));
1245 if(iter==bline_point_list.end())
1247 get_canvas_view()->get_ui_interface()->error("Unable to find where to insert vertex, internal error, please report this bug");
1250 refresh_ducks(false);
1254 StateBLine_Context::bline_delete_vertex(synfig::ValueNode_Const::Handle value_node)
1256 list<ValueNode_Const::Handle>::iterator iter;
1258 for(iter=bline_point_list.begin();iter!=bline_point_list.end();++iter)
1259 if(*iter==value_node)
1261 bline_point_list.erase(iter);
1264 if(iter==bline_point_list.end())
1266 get_canvas_view()->get_ui_interface()->error("Unable to remove vertex, internal error, please report this bug");
1269 refresh_ducks(false);
1273 StateBLine_Context::popup_handle_menu(synfig::ValueNode_Const::Handle value_node)
1275 menu.items().clear();
1277 BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1279 if(bline_point.get_split_tangent_flag())
1280 menu.items().push_back(Gtk::Menu_Helpers::MenuElem("Merge Tangents",
1282 sigc::mem_fun(*this,&studio::StateBLine_Context::bline_attach_handle),
1287 menu.items().push_back(Gtk::Menu_Helpers::MenuElem("Split Tangents",
1289 sigc::mem_fun(*this,&studio::StateBLine_Context::bline_detach_handle),
1298 StateBLine_Context::bline_detach_handle(synfig::ValueNode_Const::Handle value_node)
1300 BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1301 bline_point.set_split_tangent_flag(true);
1302 bline_point.set_tangent2(bline_point.get_tangent1());
1303 value_node->set_value(bline_point);
1304 refresh_ducks(false);
1308 StateBLine_Context::bline_attach_handle(synfig::ValueNode_Const::Handle value_node)
1310 BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1311 bline_point.set_tangent1((bline_point.get_tangent1()+bline_point.get_tangent2())*0.5);
1312 bline_point.set_split_tangent_flag(false);
1313 value_node->set_value(bline_point);
1314 refresh_ducks(false);