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
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
32 #include <gtkmm/dialog.h>
33 #include <gtkmm/entry.h>
35 #include <synfig/valuenode_dynamiclist.h>
37 #include "state_bline.h"
38 #include "canvasview.h"
41 #include <synfig/valuenode_bline.h>
42 #include <ETL/hermite>
43 #include <ETL/calculus>
45 #include "event_mouse.h"
46 #include "event_layerclick.h"
48 #include "dialog_tooloptions.h"
49 #include <gtkmm/spinbutton.h>
50 #include <synfig/transform.h>
51 #include <synfigapp/main.h>
55 /* === U S I N G =========================================================== */
59 using namespace synfig;
60 using namespace studio;
62 /* === M A C R O S ========================================================= */
64 /* === G L O B A L S ======================================================= */
66 StateBLine studio::state_bline;
68 /* === C L A S S E S & S T R U C T S ======================================= */
70 class studio::StateBLine_Context : public sigc::trackable
72 etl::handle<CanvasView> canvas_view_;
73 CanvasView::IsWorking is_working;
75 bool prev_table_status;
77 bool prev_workarea_layer_status_;
80 Canvas::Handle canvas;
84 Duckmatic::Push duckmatic_push;
86 etl::handle<Duck> curr_duck;
88 etl::handle<Duck> next_duck;
90 std::list<synfig::ValueNode_Const::Handle> bline_point_list;
91 synfigapp::Settings& settings;
93 bool on_vertex_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node);
94 bool on_tangent1_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node);
95 bool on_tangent2_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node);
98 void popup_handle_menu(synfig::ValueNode_Const::Handle value_node);
99 void popup_vertex_menu(synfig::ValueNode_Const::Handle value_node);
100 void popup_bezier_menu(float location, synfig::ValueNode_Const::Handle value_node);
102 void bline_detach_handle(synfig::ValueNode_Const::Handle value_node);
103 void bline_attach_handle(synfig::ValueNode_Const::Handle value_node);
104 void bline_delete_vertex(synfig::ValueNode_Const::Handle value_node);
105 void bline_insert_vertex(synfig::ValueNode_Const::Handle value_node,float origin=0.5);
109 void refresh_ducks(bool x=true);
111 Gtk::Table options_table;
113 Gtk::CheckButton checkbutton_layer_region;
114 Gtk::CheckButton checkbutton_layer_bline;
115 Gtk::CheckButton checkbutton_layer_curve_gradient;
116 Gtk::CheckButton checkbutton_auto_export;
117 Gtk::Button button_make;
118 Gtk::Button button_clear;
119 Gtk::Adjustment adj_feather;
120 Gtk::SpinButton spin_feather;
126 int layers_to_create()const
129 get_layer_region_flag()+
130 get_layer_bline_flag()+
131 get_layer_curve_gradient_flag();
136 if(layers_to_create()==0)
137 set_layer_region_flag(true);
140 bool get_auto_export_flag()const { return checkbutton_auto_export.get_active(); }
141 void set_auto_export_flag(bool x) { return checkbutton_auto_export.set_active(x); }
143 bool get_layer_region_flag()const { return checkbutton_layer_region.get_active(); }
144 void set_layer_region_flag(bool x) { return checkbutton_layer_region.set_active(x); }
146 bool get_layer_bline_flag()const { return checkbutton_layer_bline.get_active(); }
147 void set_layer_bline_flag(bool x) { return checkbutton_layer_bline.set_active(x); }
149 bool get_layer_curve_gradient_flag()const { return checkbutton_layer_curve_gradient.get_active(); }
150 void set_layer_curve_gradient_flag(bool x) { return checkbutton_layer_curve_gradient.set_active(x); }
152 Real get_feather() const { return adj_feather.get_value(); }
153 void set_feather(Real x) { return adj_feather.set_value(x); }
154 synfig::String get_id()const { return entry_id.get_text(); }
155 void set_id(const synfig::String& x) { return entry_id.set_text(x); }
157 Smach::event_result event_stop_handler(const Smach::event& x);
159 Smach::event_result event_refresh_handler(const Smach::event& x);
161 Smach::event_result event_mouse_click_handler(const Smach::event& x);
162 Smach::event_result event_mouse_release_handler(const Smach::event& x);
163 Smach::event_result event_mouse_motion_handler(const Smach::event& x);
164 Smach::event_result event_refresh_tool_options(const Smach::event& x);
166 Smach::event_result event_hijack(const Smach::event& x) { return Smach::RESULT_ACCEPT; }
168 void refresh_tool_options();
170 StateBLine_Context(CanvasView* canvas_view);
172 ~StateBLine_Context();
174 const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
175 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
176 synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
177 WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
178 const synfig::TransformStack& get_transform_stack()const { return canvas_view_->get_curr_transform_stack(); }
180 void load_settings();
181 void save_settings();
184 //void on_user_click(synfig::Point point);
189 bool no_egress_on_selection_change;
190 Smach::event_result event_layer_selection_changed_handler(const Smach::event& x)
192 if(!no_egress_on_selection_change)
193 throw Smach::egress_exception();
194 return Smach::RESULT_OK;
197 }; // END of class StateBLine_Context
200 /* === M E T H O D S ======================================================= */
202 StateBLine::StateBLine():
203 Smach::state<StateBLine_Context>("bline")
205 insert(event_def(EVENT_LAYER_SELECTION_CHANGED,&StateBLine_Context::event_layer_selection_changed_handler));
206 insert(event_def(EVENT_STOP,&StateBLine_Context::event_stop_handler));
207 insert(event_def(EVENT_REFRESH,&StateBLine_Context::event_refresh_handler));
208 insert(event_def(EVENT_REFRESH_DUCKS,&StateBLine_Context::event_hijack));
209 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateBLine_Context::event_mouse_click_handler));
210 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_UP,&StateBLine_Context::event_mouse_release_handler));
211 insert(event_def(EVENT_WORKAREA_MOUSE_MOTION,&StateBLine_Context::event_mouse_motion_handler));
212 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DRAG,&StateBLine_Context::event_mouse_motion_handler));
213 insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateBLine_Context::event_refresh_tool_options));
216 StateBLine::~StateBLine()
221 StateBLine_Context::load_settings()
225 if(settings.get_value("bline.layer_region",value) && value=="0")
226 set_layer_region_flag(false);
228 set_layer_region_flag(true);
230 if(settings.get_value("bline.layer_bline",value) && value=="0")
231 set_layer_bline_flag(false);
233 set_layer_bline_flag(true);
235 if(settings.get_value("bline.layer_curve_gradient",value) && value=="1")
236 set_layer_curve_gradient_flag(true);
238 set_layer_curve_gradient_flag(false);
240 if(settings.get_value("bline.auto_export",value) && value=="1")
241 set_auto_export_flag(true);
243 set_auto_export_flag(false);
245 if(settings.get_value("bline.id",value))
250 if(settings.get_value("bline.feather",value))
252 Real n = atof(value.c_str());
260 StateBLine_Context::save_settings()
263 settings.set_value("bline.layer_bline",get_layer_bline_flag()?"1":"0");
264 settings.set_value("bline.layer_region",get_layer_region_flag()?"1":"0");
265 settings.set_value("bline.layer_curve_gradient",get_layer_curve_gradient_flag()?"1":"0");
266 settings.set_value("bline.auto_export",get_auto_export_flag()?"1":"0");
267 settings.set_value("bline.id",get_id().c_str());
268 settings.set_value("bline.feather",strprintf("%f",get_feather()));
272 StateBLine_Context::reset()
275 bline_point_list.clear();
280 StateBLine_Context::increment_id()
289 // If there is a number
290 // already at the end of the
291 // id, then remove it.
292 if(id[id.size()-1]<='9' && id[id.size()-1]>='0')
294 // figure out how many digits it is
295 for(digits=0;(int)id.size()-1>=digits && id[id.size()-1-digits]<='9' && id[id.size()-1-digits]>='0';digits++)while(false);
298 str_number=String(id,id.size()-digits,id.size());
299 id=String(id,0,id.size()-digits);
300 synfig::info("---------------- \"%s\"",str_number.c_str());
302 number=atoi(str_number.c_str());
312 // Add the number back onto the id
314 const String format(strprintf("%%0%dd",digits));
315 id+=strprintf(format.c_str(),number);
323 StateBLine_Context::StateBLine_Context(CanvasView* canvas_view):
324 canvas_view_(canvas_view),
325 is_working(*canvas_view),
327 prev_workarea_layer_status_(get_work_area()->allow_layer_clicks),
328 duckmatic_push(get_work_area()),
329 settings(synfigapp::Main::get_selected_input_device()->settings()),
331 checkbutton_layer_region(_("Fill")),
332 checkbutton_layer_bline(_("Outline")),
333 checkbutton_layer_curve_gradient(_("Gradient")),
334 checkbutton_auto_export(_("Auto Export")),
335 button_make(_("Make")),
336 button_clear(_("Clear")),
337 adj_feather(0,0,10000,0.01,0.1),
338 spin_feather(adj_feather,0.01,4)
341 no_egress_on_selection_change=false;
344 // Set up the tool options dialog
345 //options_table.attach(*manage(new Gtk::Label(_("BLine Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
346 options_table.attach(entry_id, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
347 options_table.attach(checkbutton_layer_region, 0, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
348 options_table.attach(checkbutton_layer_bline, 0, 2, 3, 4, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
349 options_table.attach(checkbutton_layer_curve_gradient, 0, 2, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
350 options_table.attach(*manage(new Gtk::Label(_("Feather"))), 0, 1, 10, 11, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
351 options_table.attach(spin_feather, 1, 2, 10, 11, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
352 options_table.attach(checkbutton_auto_export, 0, 2, 11, 12, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
353 //options_table.attach(button_make, 0, 2, 5, 6, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
354 //button_make.signal_pressed().connect(sigc::mem_fun(*this,&StateBLine_Context::run));
355 options_table.show_all();
356 refresh_tool_options();
357 App::dialog_tool_options->present();
359 // Turn off layer clicking
360 get_work_area()->allow_layer_clicks=false;
362 // clear out the ducks
363 get_work_area()->clear_ducks();
365 // Refresh the work area
366 get_work_area()->queue_draw();
368 // Hide the tables if they are showing
369 prev_table_status=get_canvas_view()->tables_are_visible();
370 if(prev_table_status)get_canvas_view()->hide_tables();
372 // Disable the time bar
373 get_canvas_view()->set_sensitive_timebar(false);
376 //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StateBLine_Context::on_user_click));
377 get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
379 App::toolbox->refresh();
383 StateBLine_Context::refresh_tool_options()
385 App::dialog_tool_options->clear();
386 App::dialog_tool_options->set_widget(options_table);
387 App::dialog_tool_options->set_local_name(_("BLine Tool"));
388 App::dialog_tool_options->set_name("bline");
390 App::dialog_tool_options->add_button(
391 Gtk::StockID("gtk-execute"),
392 _("Make BLine and/or Region")
393 )->signal_clicked().connect(
394 sigc::hide_return(sigc::mem_fun(
396 &StateBLine_Context::run
400 App::dialog_tool_options->add_button(
401 Gtk::StockID("gtk-clear"),
402 _("Clear current BLine")
403 )->signal_clicked().connect(
406 &StateBLine_Context::reset
412 StateBLine_Context::event_refresh_tool_options(const Smach::event& x)
414 refresh_tool_options();
415 return Smach::RESULT_ACCEPT;
418 StateBLine_Context::~StateBLine_Context()
423 App::dialog_tool_options->clear();
425 get_canvas_view()->work_area->reset_cursor();
427 // Restore layer clicking
428 get_work_area()->allow_layer_clicks=prev_workarea_layer_status_;
430 // Enable the time bar
431 get_canvas_view()->set_sensitive_timebar(true);
433 // Bring back the tables if they were out before
434 if(prev_table_status)get_canvas_view()->show_tables();
436 // get_canvas_view()->get_smach().process_event(EVENT_REFRESH_DUCKS);
438 // Refresh the work area
439 get_work_area()->queue_draw();
441 App::toolbox->refresh();
445 StateBLine_Context::event_stop_handler(const Smach::event& x)
447 synfig::info("STATE RotoBLine: Received Stop Event");
450 // throw Smach::egress_exception();
451 // get_canvas_view()->get_smach().pop_state();
452 return Smach::RESULT_ACCEPT;
456 StateBLine_Context::event_refresh_handler(const Smach::event& x)
458 synfig::info("STATE RotoBLine: Received Refresh Event");
460 return Smach::RESULT_ACCEPT;
464 StateBLine_Context::run()
470 for(int i=5;i>0 && !success;i--)try
478 if(!success && !err.empty())
480 get_canvas_view()->get_ui_interface()->error(err);
486 StateBLine_Context::run_()
491 // Now we need to generate it
493 if(bline_point_list.empty())
497 if(bline_point_list.size()<2)
499 //get_canvas_view()->get_ui_interface()->error(_("You need at least two (2) points to create a BLine"));
506 // Create the action group
507 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("New BLine"));
509 std::vector<BLinePoint> new_list;
510 std::list<synfig::ValueNode_Const::Handle>::iterator iter;
511 const synfig::TransformStack& transform(get_transform_stack());
513 for(iter=bline_point_list.begin();iter!=bline_point_list.end();++iter)
515 BLinePoint bline_point((*iter)->get_value().get(BLinePoint()));
516 Point new_vertex(transform.unperform(bline_point.get_vertex()));
518 bline_point.set_tangent1(
520 bline_point.get_tangent1()+bline_point.get_vertex()
524 bline_point.set_tangent2(
526 bline_point.get_tangent2()+bline_point.get_vertex()
530 bline_point.set_vertex(new_vertex);
532 new_list.push_back(bline_point);
535 ValueNode_BLine::Handle value_node_bline(ValueNode_BLine::create(new_list));
537 assert(value_node_bline);
539 // Set the looping flag
540 value_node_bline->set_loop(loop_);
542 // Add the BLine to the canvas
543 if(get_auto_export_flag() && !get_canvas_interface()->add_value_node(value_node_bline,get_id()))
545 //get_canvas_view()->get_ui_interface()->error(_("Unable to add value node"));
548 throw String(_("Unable to add value node"));
554 // we are temporarily using the layer to hold something
555 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
560 depth=layer->get_depth();
562 canvas=layer->get_canvas();
568 canvas=get_canvas_view()->get_canvas();
570 synfigapp::SelectionManager::LayerList layer_selection;
572 // If we were asked to create a region layer, go ahead and do so
573 if(get_layer_region_flag())
575 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
577 Layer::Handle layer(get_canvas_interface()->add_layer_to("region",canvas,depth));
578 layer_selection.push_back(layer);
580 layer->set_description(get_id()+_(" Region"));
581 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
585 layer->set_param("feather",get_feather());
586 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
589 if(get_layer_bline_flag())
590 layer->set_param("color",synfigapp::Main::get_background_color());
592 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
596 action->set_param("canvas",get_canvas());
597 action->set_param("canvas_interface",get_canvas_interface());
598 action->set_param("layer",layer);
599 if(!action->set_param("param",String("bline")))
600 synfig::error("LayerParamConnect didn't like \"param\"");
601 if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
602 synfig::error("LayerParamConnect didn't like \"value_node\"");
604 if(!get_canvas_interface()->get_instance()->perform_action(action))
606 //get_canvas_view()->get_ui_interface()->error(_("Unable to create Region layer"));
608 throw String(_("Unable to create Region layer"));
613 // If we were asked to create a BLine layer, go ahead and do so
614 if(get_layer_bline_flag())
616 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
618 Layer::Handle layer(get_canvas_interface()->add_layer_to("outline",canvas,depth));
619 layer_selection.push_back(layer);
620 layer->set_description(get_id()+_(" Outline"));
621 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
624 layer->set_param("feather",get_feather());
625 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
631 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("bline")))
639 synfig::error("LayerParamConnect didn't like \"param\"");
640 if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
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 BLine layer"));
647 throw String(_("Unable to create Outline layer"));
651 /*if(get_layer_region_flag() && !get_auto_export_flag())
653 get_canvas_interface()->auto_export(synfigapp::ValueDesc(layer,"bline"));
659 // If we were asked to create a CurveGradient layer, go ahead and do so
660 if(get_layer_curve_gradient_flag())
662 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
664 Layer::Handle layer(get_canvas_interface()->add_layer_to("curve_gradient",canvas,depth));
665 layer_selection.push_back(layer);
666 layer->set_description(get_id()+_(" Gradient"));
667 get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
672 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
676 action->set_param("canvas",get_canvas());
677 action->set_param("canvas_interface",get_canvas_interface());
678 action->set_param("layer",layer);
679 if(!action->set_param("param",String("bline")))
680 synfig::error("LayerParamConnect didn't like \"param\"");
681 if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
682 synfig::error("LayerParamConnect didn't like \"value_node\"");
684 if(!get_canvas_interface()->get_instance()->perform_action(action))
686 //get_canvas_view()->get_ui_interface()->error(_("Unable to create BLine layer"));
688 throw String(_("Unable to create Gradient layer"));
692 /*if(get_layer_region_flag() && !get_auto_export_flag())
694 get_canvas_interface()->auto_export(synfigapp::ValueDesc(layer,"bline"));
698 no_egress_on_selection_change=true;
699 get_canvas_interface()->get_selection_manager()->clear_selected_layers();
700 get_canvas_interface()->get_selection_manager()->set_selected_layers(layer_selection);
701 no_egress_on_selection_change=false;
703 //if(finish_bline_dialog.get_region_flag() || finish_bline_dialog.get_bline_flag())
704 // get_canvas_interface()->signal_dirty_preview()();
714 StateBLine_Context::event_mouse_motion_handler(const Smach::event& x)
716 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
720 //synfig::info("Moved Duck");
721 Point p(get_work_area()->snap_point_to_grid(event.pos));
722 curr_duck->set_trans_point(p);
724 next_duck->set_trans_point(p);
725 get_work_area()->queue_draw();
726 return Smach::RESULT_ACCEPT;
729 return Smach::RESULT_OK;
733 StateBLine_Context::event_mouse_release_handler(const Smach::event& x)
737 //synfig::info("Released current duck");
738 curr_duck->signal_edited()(curr_duck->get_point());
741 //synfig::info("grabbing next duck");
745 return Smach::RESULT_ACCEPT;
747 return Smach::RESULT_OK;
751 StateBLine_Context::event_mouse_click_handler(const Smach::event& x)
753 synfig::info("STATE BLINE: Received mouse button down Event");
754 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
759 // If we are already looped up, then don't try to add anything else
761 return Smach::RESULT_OK;
763 BLinePoint bline_point;
765 bline_point.set_vertex(get_work_area()->snap_point_to_grid(event.pos));
766 //bline_point.set_width(synfigapp::Main::get_bline_width());
767 bline_point.set_width(1.0f);
768 bline_point.set_origin(0.5f);
769 bline_point.set_split_tangent_flag(false);
770 bline_point.set_tangent1(Vector(0,0));
774 if(bline_point_list.empty())
776 bline_point.set_tangent1(Vector(1,1));
780 const Vector t(event.pos-bline_point_list.back()->get_value().get(BLinePoint()).get_vertex());
781 bline_point.set_tangent1(t);
784 if(bline_point_list.size()>1)
786 std::list<synfig::ValueNode_Const::Handle>::iterator iter;
787 iter=bline_point_list.end();
789 BLinePoint prev(bline_point_list.back()->get_value().get(BLinePoint()));
790 prev.set_tangent1(event.pos-(*iter)->get_value().get(BLinePoint()).get_vertex());
791 bline_point_list.back()->set_value(prev);
795 bline_point_list.push_back(ValueNode_Const::create(bline_point));
798 return Smach::RESULT_ACCEPT;
801 case BUTTON_RIGHT: // Intercept the right-button click to short-circut the pop-up menu
802 return Smach::RESULT_ACCEPT;
805 return Smach::RESULT_OK;
810 StateBLine_Context::refresh_ducks(bool button_down)
812 get_work_area()->clear_ducks();
813 get_work_area()->queue_draw();
815 if(bline_point_list.empty())
818 list<ValueNode_Const::Handle>::iterator iter;
820 handle<WorkArea::Bezier> bezier;
821 handle<WorkArea::Duck> duck,tduck;
822 BLinePoint bline_point;
824 for(iter=bline_point_list.begin();iter!=bline_point_list.end();++iter)
826 ValueNode_Const::Handle value_node(*iter);
827 bline_point=(value_node->get_value().get(BLinePoint()));
831 // First add the duck associated with this vertex
832 duck=new WorkArea::Duck(bline_point.get_vertex());
833 duck->set_editable(true);
834 duck->set_type(Duck::TYPE_VERTEX);
835 duck->set_name(strprintf("%x-vertex",value_node.get()));
836 duck->signal_edited().connect(
837 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_vertex_change),value_node)
839 duck->signal_user_click(2).connect(
840 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::popup_vertex_menu),value_node)
842 duck->set_guid(value_node->get_guid()^GUID::hasher(0));
844 get_work_area()->add_duck(duck);
846 // Add the tangent1 duck
847 tduck=new WorkArea::Duck(bline_point.get_tangent1());
848 tduck->set_editable(true);
849 tduck->set_name(strprintf("%x-tangent1",value_node.get()));
850 tduck->set_origin(duck);
851 tduck->set_scalar(-0.33333333333333333);
852 tduck->set_tangent(true);
853 tduck->set_guid(value_node->get_guid()^GUID::hasher(3));
854 tduck->signal_edited().connect(
855 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_tangent1_change),value_node)
857 tduck->signal_user_click(2).connect(
858 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::popup_handle_menu),value_node)
861 // See if we need to add that duck to the previous bezier
864 get_work_area()->add_duck(tduck);
868 bezier->signal_user_click(2).connect(
872 &studio::StateBLine_Context::popup_bezier_menu
878 //get_work_area()->add_duck(bezier->c1);
879 //get_work_area()->add_duck(bezier->c2);
880 get_work_area()->add_bezier(bezier);
885 // Now we see if we need to create a bezier
886 list<ValueNode_Const::Handle>::iterator next(iter);
889 // If our next iterator is the end, then we don't need
891 //if(next==bline_point_list.end() && !loop_)
894 bezier=new WorkArea::Bezier();
896 // Add the tangent2 duck
897 tduck=new WorkArea::Duck(bline_point.get_tangent2());
898 tduck->set_editable(true);
899 tduck->set_origin(duck);
900 tduck->set_scalar(0.33333333333333333);
901 tduck->set_tangent(true);
902 if(bline_point.get_split_tangent_flag())
904 tduck->set_name(strprintf("%x-tangent2",value_node.get()));
905 tduck->signal_edited().connect(
906 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_tangent2_change),value_node)
911 tduck->set_name(strprintf("%x-tangent1",value_node.get()));
912 tduck->signal_edited().connect(
913 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_tangent1_change),value_node)
916 tduck->set_guid(value_node->get_guid()^GUID::hasher(4));
917 tduck->signal_user_click(2).connect(
918 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::popup_handle_menu),value_node)
921 // Setup the next bezier
925 get_work_area()->add_duck(tduck);
929 // Add the loop, if requested
933 BLinePoint bline_point(bline_point_list.front()->get_value().get(BLinePoint()));
935 duck=new WorkArea::Duck(bline_point.get_vertex());
936 duck->set_editable(true);
937 duck->set_name(strprintf("%x-vertex",bline_point_list.front().get()));
938 duck->signal_edited().connect(
939 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_vertex_change),bline_point_list.front())
941 duck->signal_user_click(2).connect(
942 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::popup_vertex_menu),bline_point_list.front())
944 get_work_area()->add_duck(duck);
946 // Add the tangent1 duck
947 tduck=new WorkArea::Duck(bline_point.get_tangent1());
948 tduck->set_editable(true);
949 tduck->set_name(strprintf("%x-tangent1",bline_point_list.front().get()));
950 tduck->set_origin(duck);
951 tduck->set_scalar(-0.33333333333333333);
952 tduck->set_tangent(true);
953 tduck->signal_edited().connect(
954 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::on_tangent1_change),bline_point_list.front())
956 tduck->signal_user_click(2).connect(
957 sigc::bind(sigc::mem_fun(*this,&studio::StateBLine_Context::popup_handle_menu),bline_point_list.front())
959 get_work_area()->add_duck(tduck);
964 bezier->signal_user_click(2).connect(
968 &studio::StateBLine_Context::popup_bezier_menu
970 bline_point_list.front()
974 //get_work_area()->add_duck(bezier->c1);
975 get_work_area()->add_bezier(bezier);
979 duck=new WorkArea::Duck(bline_point.get_vertex());
980 duck->set_editable(false);
981 duck->set_name("temp");
983 // Add the tangent1 duck
984 tduck=new WorkArea::Duck(Vector(0,0));
985 tduck->set_editable(false);
986 tduck->set_name("ttemp");
987 tduck->set_origin(duck);
988 tduck->set_scalar(-0.33333333333333333);
990 tduck->set_tangent(true);
994 get_work_area()->add_duck(bezier->p2);
995 //get_work_area()->add_duck(bezier->c2);
996 get_work_area()->add_bezier(bezier);
998 duck->set_guid(GUID());
999 tduck->set_guid(GUID());
1010 curr_duck=next_duck;
1015 get_work_area()->queue_draw();
1020 StateBLine_Context::on_vertex_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node)
1022 BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1023 bline_point.set_vertex(point);
1024 value_node->set_value(bline_point);
1030 StateBLine_Context::on_tangent1_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node)
1032 BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1033 bline_point.set_tangent1(point);
1034 value_node->set_value(bline_point);
1040 StateBLine_Context::on_tangent2_change(const synfig::Point &point, synfig::ValueNode_Const::Handle value_node)
1042 BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1043 bline_point.set_tangent2(point);
1044 value_node->set_value(bline_point);
1050 StateBLine_Context::loop_bline()
1054 refresh_ducks(false);
1058 StateBLine_Context::unloop_bline()
1062 refresh_ducks(false);
1066 StateBLine_Context::popup_vertex_menu(synfig::ValueNode_Const::Handle value_node)
1068 menu.items().clear();
1072 menu.items().push_back(Gtk::Menu_Helpers::MenuElem("Unloop BLine",
1073 sigc::mem_fun(*this,&studio::StateBLine_Context::unloop_bline)
1076 menu.items().push_back(Gtk::Menu_Helpers::MenuElem("Loop BLine",
1077 sigc::mem_fun(*this,&studio::StateBLine_Context::loop_bline)
1081 menu.items().push_back(Gtk::Menu_Helpers::MenuElem("Delete Vertex",
1083 sigc::mem_fun(*this,&studio::StateBLine_Context::bline_delete_vertex),
1092 StateBLine_Context::popup_bezier_menu(float location, synfig::ValueNode_Const::Handle value_node)
1094 menu.items().clear();
1096 menu.items().push_back(Gtk::Menu_Helpers::MenuElem("Insert Vertex",
1099 sigc::mem_fun(*this,&studio::StateBLine_Context::bline_insert_vertex),
1110 StateBLine_Context::bline_insert_vertex(synfig::ValueNode_Const::Handle value_node, float origin)
1112 list<ValueNode_Const::Handle>::iterator iter;
1114 for(iter=bline_point_list.begin();iter!=bline_point_list.end();++iter)
1115 if(*iter==value_node)
1117 list<ValueNode_Const::Handle>::iterator prev(iter);
1120 BLinePoint bline_point;
1122 BLinePoint next_bline_point((*iter)->get_value().get(BLinePoint()));
1123 BLinePoint prev_bline_point;
1125 if(iter!=bline_point_list.begin())
1127 prev_bline_point=(*prev)->get_value().get(BLinePoint());
1131 prev_bline_point.set_vertex(Point(0,0));
1132 prev_bline_point.set_width(next_bline_point.get_width());
1133 prev_bline_point.set_origin(0.5);
1134 prev_bline_point.set_split_tangent_flag(false);
1137 etl::hermite<Vector> curve(prev_bline_point.get_vertex(),next_bline_point.get_vertex(),prev_bline_point.get_tangent2(),next_bline_point.get_tangent1());
1138 etl::derivative< etl::hermite<Vector> > deriv(curve);
1140 bline_point.set_vertex(curve(origin));
1141 bline_point.set_width((next_bline_point.get_width()-prev_bline_point.get_width())*origin+prev_bline_point.get_width());
1142 #ifdef ETL_FIXED_DERIVATIVE
1143 bline_point.set_tangent1(deriv(origin)*std::min(1.0f-origin,origin));
1145 bline_point.set_tangent1(-deriv(origin)*std::min(1.0f-origin,origin));
1147 bline_point.set_tangent2(bline_point.get_tangent1());
1148 bline_point.set_split_tangent_flag(false);
1149 bline_point.set_origin(origin);
1152 bline_point.set_vertex((next_bline_point.get_vertex()+prev_bline_point.get_vertex())*0.5);
1153 bline_point.set_width((next_bline_point.get_width()+prev_bline_point.get_width())*0.5);
1154 bline_point.set_origin(origin);
1155 bline_point.set_split_tangent_flag(false);
1156 bline_point.set_tangent1((next_bline_point.get_vertex()-prev_bline_point.get_vertex())*0.5);
1159 bline_point_list.insert(iter,ValueNode_Const::create(bline_point));
1163 if(iter==bline_point_list.end())
1165 get_canvas_view()->get_ui_interface()->error("Unable to find where to insert vertex, internal error, please report this bug");
1168 refresh_ducks(false);
1172 StateBLine_Context::bline_delete_vertex(synfig::ValueNode_Const::Handle value_node)
1174 list<ValueNode_Const::Handle>::iterator iter;
1176 for(iter=bline_point_list.begin();iter!=bline_point_list.end();++iter)
1177 if(*iter==value_node)
1179 bline_point_list.erase(iter);
1182 if(iter==bline_point_list.end())
1184 get_canvas_view()->get_ui_interface()->error("Unable to remove vertex, internal error, please report this bug");
1187 refresh_ducks(false);
1191 StateBLine_Context::popup_handle_menu(synfig::ValueNode_Const::Handle value_node)
1193 menu.items().clear();
1195 BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1197 if(bline_point.get_split_tangent_flag())
1198 menu.items().push_back(Gtk::Menu_Helpers::MenuElem("Merge Tangents",
1200 sigc::mem_fun(*this,&studio::StateBLine_Context::bline_attach_handle),
1205 menu.items().push_back(Gtk::Menu_Helpers::MenuElem("Split Tangents",
1207 sigc::mem_fun(*this,&studio::StateBLine_Context::bline_detach_handle),
1216 StateBLine_Context::bline_detach_handle(synfig::ValueNode_Const::Handle value_node)
1218 BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1219 bline_point.set_split_tangent_flag(true);
1220 bline_point.set_tangent2(bline_point.get_tangent1());
1221 value_node->set_value(bline_point);
1222 refresh_ducks(false);
1226 StateBLine_Context::bline_attach_handle(synfig::ValueNode_Const::Handle value_node)
1228 BLinePoint bline_point(value_node->get_value().get(BLinePoint()));
1229 bline_point.set_tangent1((bline_point.get_tangent1()+bline_point.get_tangent2())*0.5);
1230 bline_point.set_split_tangent_flag(false);
1231 value_node->set_value(bline_point);
1232 refresh_ducks(false);