1 /* === S Y N F I G ========================================================= */
2 /*! \file state_draw.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_draw.h"
39 #include "state_stroke.h"
40 #include "canvasview.h"
43 #include <synfig/valuenode_bline.h>
44 #include <synfig/valuenode_composite.h>
45 #include <ETL/hermite>
46 #include <ETL/calculus>
48 #include "event_mouse.h"
49 #include "event_layerclick.h"
52 #include <synfigapp/blineconvert.h>
53 #include <synfigapp/main.h>
55 #include <ETL/gaussian>
56 #include "dialog_tooloptions.h"
58 #include <gtkmm/table.h>
59 #include <gtkmm/label.h>
60 #include <gtkmm/button.h>
61 #include <gtkmm/checkbutton.h>
62 #include <gtkmm/scale.h>
63 #include <sigc++/connection.h>
67 /* === U S I N G =========================================================== */
71 using namespace synfig;
72 using namespace studio;
74 /* === M A C R O S ========================================================= */
76 /* === G L O B A L S ======================================================= */
78 StateDraw studio::state_draw;
80 /* === C L A S S E S & S T R U C T S ======================================= */
82 class studio::StateDraw_Context : public sigc::trackable
84 typedef etl::smart_ptr<std::list<synfig::Point> > StrokeData;
85 typedef etl::smart_ptr<std::list<synfig::Real> > WidthData;
87 typedef list< pair<StrokeData,WidthData> > StrokeQueue;
89 StrokeQueue stroke_queue;
92 etl::handle<CanvasView> canvas_view_;
93 CanvasView::IsWorking is_working;
95 bool prev_table_status;
97 bool prev_workarea_layer_status_;
100 SigC::Connection process_queue_connection;
102 ValueNode_BLine::Handle last_stroke;
106 //Duckmatic::Push duckmatic_push;
108 std::list< etl::smart_ptr<std::list<synfig::Point> > > stroke_list;
110 void refresh_ducks();
112 Duckmatic::Type old_duckmask;
114 void fill_last_stroke();
116 Smach::event_result new_bline(std::list<synfig::BLinePoint> bline,bool loop_bline_flag,float radius);
118 Smach::event_result new_region(std::list<synfig::BLinePoint> bline,synfig::Real radius);
120 Smach::event_result extend_bline_from_begin(ValueNode_BLine::Handle value_node,std::list<synfig::BLinePoint> bline,bool complete_loop);
121 Smach::event_result extend_bline_from_end(ValueNode_BLine::Handle value_node,std::list<synfig::BLinePoint> bline,bool complete_loop);
122 void reverse_bline(std::list<synfig::BLinePoint> &bline);
124 synfigapp::Settings& settings;
126 Gtk::Table options_table;
127 Gtk::CheckButton checkbutton_pressure_width;
128 Gtk::CheckButton checkbutton_round_ends;
129 Gtk::CheckButton checkbutton_auto_loop; // whether to loop new strokes which start and end in the same place
130 Gtk::CheckButton checkbutton_auto_extend; // whether to extend existing lines
131 Gtk::CheckButton checkbutton_auto_link; // whether to link new ducks to existing ducks
132 Gtk::CheckButton checkbutton_region_only;
133 Gtk::Button button_fill_last_stroke;
135 //pressure spinner and such
136 Gtk::Adjustment adj_min_pressure;
137 Gtk::SpinButton spin_min_pressure;
138 Gtk::CheckButton check_min_pressure;
140 Gtk::Adjustment adj_feather;
141 Gtk::SpinButton spin_feather;
143 Gtk::Adjustment adj_globalthres;
144 Gtk::SpinButton spin_globalthres;
146 Gtk::Adjustment adj_localthres;
147 Gtk::CheckButton check_localerror;
148 void UpdateErrorBox(); //switches the stuff if need be :)
150 //Added by Adrian - data drive HOOOOO
151 synfigapp::BLineConverter blineconv;
154 bool get_pressure_width_flag()const { return checkbutton_pressure_width.get_active(); }
155 void set_pressure_width_flag(bool x) { return checkbutton_pressure_width.set_active(x); }
157 bool get_auto_loop_flag()const { return checkbutton_auto_loop.get_active(); }
158 void set_auto_loop_flag(bool x) { return checkbutton_auto_loop.set_active(x); }
160 bool get_auto_extend_flag()const { return checkbutton_auto_extend.get_active(); }
161 void set_auto_extend_flag(bool x) { return checkbutton_auto_extend.set_active(x); }
163 bool get_auto_link_flag()const { return checkbutton_auto_link.get_active(); }
164 void set_auto_link_flag(bool x) { return checkbutton_auto_link.set_active(x); }
166 bool get_region_only_flag()const { return checkbutton_region_only.get_active(); }
167 void set_region_only_flag(bool x) { return checkbutton_region_only.set_active(x); }
169 Real get_min_pressure() const { return adj_min_pressure.get_value(); }
170 void set_min_pressure(Real x) { return adj_min_pressure.set_value(x); }
172 Real get_feather() const { return adj_feather.get_value(); }
173 void set_feather(Real x) { return adj_feather.set_value(x); }
175 Real get_gthres() const { return adj_globalthres.get_value(); }
176 void set_gthres(Real x) { return adj_globalthres.set_value(x); }
178 Real get_lthres() const { return adj_localthres.get_value(); }
179 void set_lthres(Real x) { return adj_localthres.set_value(x); }
181 bool get_local_error_flag() const { return check_localerror.get_active(); }
182 void set_local_error_flag(bool x) { check_localerror.set_active(x); }
184 bool get_min_pressure_flag()const { return check_min_pressure.get_active(); }
185 void set_min_pressure_flag(bool x) { check_min_pressure.set_active(x); }
187 void load_settings();
188 void save_settings();
190 Smach::event_result event_stop_handler(const Smach::event& x);
192 Smach::event_result event_refresh_handler(const Smach::event& x);
194 Smach::event_result event_mouse_down_handler(const Smach::event& x);
196 Smach::event_result event_stroke(const Smach::event& x);
197 Smach::event_result event_refresh_tool_options(const Smach::event& x);
198 void refresh_tool_options();
200 Smach::event_result process_stroke(StrokeData stroke_data, WidthData width_data, bool region_flag=false);
202 bool process_queue();
205 StateDraw_Context(CanvasView* canvas_view);
207 ~StateDraw_Context();
209 const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
210 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
211 synfig::Time get_time()const { return get_canvas_interface()->get_time(); }
212 synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
213 WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
215 //void on_user_click(synfig::Point point);
218 }; // END of class StateDraw_Context
221 /* === M E T H O D S ======================================================= */
223 StateDraw::StateDraw():
224 Smach::state<StateDraw_Context>("draw")
226 insert(event_def(EVENT_STOP,&StateDraw_Context::event_stop_handler));
227 insert(event_def(EVENT_REFRESH,&StateDraw_Context::event_refresh_handler));
228 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateDraw_Context::event_mouse_down_handler));
229 insert(event_def(EVENT_WORKAREA_STROKE,&StateDraw_Context::event_stroke));
230 insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateDraw_Context::event_refresh_tool_options));
233 StateDraw::~StateDraw()
239 StateDraw_Context::load_settings()
243 if(settings.get_value("draw.pressure_width",value) && value=="0")
244 set_pressure_width_flag(false);
246 set_pressure_width_flag(true);
248 if(settings.get_value("draw.auto_loop",value) && value=="0")
249 set_auto_loop_flag(false);
251 set_auto_loop_flag(true);
253 if(settings.get_value("draw.auto_extend",value) && value=="0")
254 set_auto_extend_flag(false);
256 set_auto_extend_flag(true);
258 if(settings.get_value("draw.auto_link",value) && value=="0")
259 set_auto_link_flag(false);
261 set_auto_link_flag(true);
263 if(settings.get_value("draw.region_only",value) && value=="1")
264 set_region_only_flag(true);
266 set_region_only_flag(false);
268 if(settings.get_value("draw.min_pressure_on",value) && value=="0")
269 set_min_pressure_flag(false);
271 set_min_pressure_flag(true);
273 if(settings.get_value("draw.min_pressure",value))
275 Real n = atof(value.c_str());
280 if(settings.get_value("draw.feather",value))
282 Real n = atof(value.c_str());
287 if(settings.get_value("draw.gthreshold",value))
289 Real n = atof(value.c_str());
293 if(settings.get_value("draw.lthreshold",value))
295 Real n = atof(value.c_str());
299 if(settings.get_value("draw.localize",value) && value == "1")
300 set_local_error_flag(true);
302 set_local_error_flag(false);
306 StateDraw_Context::save_settings()
308 settings.set_value("draw.pressure_width",get_pressure_width_flag()?"1":"0");
309 settings.set_value("draw.auto_loop",get_auto_loop_flag()?"1":"0");
310 settings.set_value("draw.auto_extend",get_auto_extend_flag()?"1":"0");
311 settings.set_value("draw.auto_link",get_auto_link_flag()?"1":"0");
312 settings.set_value("draw.region_only",get_region_only_flag()?"1":"0");
313 settings.set_value("draw.min_pressure",strprintf("%f",get_min_pressure()));
314 settings.set_value("draw.feather",strprintf("%f",get_feather()));
315 settings.set_value("draw.min_pressure_on",get_min_pressure_flag()?"1":"0");
316 settings.set_value("draw.gthreshold",strprintf("%f",get_gthres()));
317 settings.set_value("draw.lthreshold",strprintf("%f",get_lthres()));
318 settings.set_value("draw.localize",get_local_error_flag()?"1":"0");
321 StateDraw_Context::StateDraw_Context(CanvasView* canvas_view):
322 canvas_view_(canvas_view),
323 is_working(*canvas_view),
325 prev_workarea_layer_status_(get_work_area()->allow_layer_clicks),
326 settings(synfigapp::Main::get_selected_input_device()->settings()),
327 checkbutton_pressure_width(_("Pressure Width")),
328 checkbutton_auto_loop(_("Auto Loop")),
329 checkbutton_auto_extend(_("Auto Extend")),
330 checkbutton_auto_link(_("Auto Link")),
331 checkbutton_region_only(_("Create Region Only")),
332 button_fill_last_stroke(_("Fill Last Stroke")),
333 adj_min_pressure(0,0,1,0.01,0.1),
334 spin_min_pressure(adj_min_pressure,0.1,3),
335 check_min_pressure(_("Min Pressure")),
336 adj_feather(0,0,10000,0.01,0.1),
337 spin_feather(adj_feather,0.01,4),
338 adj_globalthres(.70f,0.01,10000,0.01,0.1),
339 spin_globalthres(adj_globalthres,0.01,3),
340 adj_localthres(20,1,100000,0.1,1),
341 check_localerror(_("LocalError"))
344 synfig::info("STATE SKETCH: entering state");
351 //options_table.attach(*manage(new Gtk::Label(_("Draw Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
352 options_table.attach(checkbutton_pressure_width, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
353 options_table.attach(checkbutton_auto_loop, 0, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
354 options_table.attach(checkbutton_auto_extend, 0, 2, 3, 4, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
355 options_table.attach(checkbutton_auto_link, 0, 2, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
356 options_table.attach(checkbutton_region_only, 0, 2, 5, 6, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
358 options_table.attach(check_min_pressure, 0, 2, 6, 7, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
359 options_table.attach(spin_min_pressure, 0, 2, 7, 8, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
361 options_table.attach(*manage(new Gtk::Label(_("Feather"))), 0, 1, 8, 9, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
362 options_table.attach(spin_feather, 1, 2, 8, 9, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
364 options_table.attach(check_localerror, 0, 2, 9, 10, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
365 options_table.attach(*manage(new Gtk::Label(_("Smooth"))), 0, 1, 10, 11, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
366 options_table.attach(spin_globalthres, 1, 2, 10, 11, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
368 //options_table.attach(button_fill_last_stroke, 0, 2, 11, 12, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
370 button_fill_last_stroke.signal_pressed().connect(sigc::mem_fun(*this,&StateDraw_Context::fill_last_stroke));
371 check_localerror.signal_toggled().connect(sigc::mem_fun(*this,&StateDraw_Context::UpdateErrorBox));
373 options_table.show_all();
374 refresh_tool_options();
375 //App::dialog_tool_options->set_widget(options_table);
376 App::dialog_tool_options->present();
379 old_duckmask=get_work_area()->get_type_mask();
380 get_work_area()->set_type_mask(Duck::TYPE_ALL-Duck::TYPE_TANGENT-Duck::TYPE_WIDTH);
382 // Turn off layer clicking
383 get_work_area()->allow_layer_clicks=false;
385 // Turn off duck clicking
386 get_work_area()->allow_duck_clicks=false;
388 // clear out the ducks
389 //get_work_area()->clear_ducks();
391 // Refresh the work area
392 //get_work_area()->queue_draw();
394 // Hide the tables if they are showing
395 prev_table_status=get_canvas_view()->tables_are_visible();
396 //if(prev_table_status)get_canvas_view()->hide_tables();
398 // Disable the time bar
399 get_canvas_view()->set_sensitive_timebar(false);
402 //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StateDraw_Context::on_user_click));
404 get_canvas_view()->work_area->set_cursor(Gdk::PENCIL);
406 App::toolbox->refresh();
412 void StateDraw_Context::UpdateErrorBox()
414 if(get_local_error_flag())
416 spin_globalthres.set_adjustment(adj_localthres);
417 spin_globalthres.set_value(adj_localthres.get_value());
418 spin_globalthres.set_increments(0.1,1);
421 spin_globalthres.set_adjustment(adj_globalthres);
422 spin_globalthres.set_value(adj_globalthres.get_value());
423 spin_globalthres.set_increments(0.01,.1);
426 spin_globalthres.update();
430 StateDraw_Context::refresh_tool_options()
432 App::dialog_tool_options->clear();
433 App::dialog_tool_options->set_widget(options_table);
434 App::dialog_tool_options->set_local_name(_("Draw Tool"));
435 App::dialog_tool_options->set_name("draw");
437 App::dialog_tool_options->add_button(
438 Gtk::StockID("synfig-fill"),
439 _("Fill Last Stroke")
440 )->signal_clicked().connect(
443 &StateDraw_Context::fill_last_stroke
450 StateDraw_Context::event_refresh_tool_options(const Smach::event& x)
452 refresh_tool_options();
453 return Smach::RESULT_ACCEPT;
456 StateDraw_Context::~StateDraw_Context()
460 App::dialog_tool_options->clear();
462 get_work_area()->set_type_mask(old_duckmask);
464 get_canvas_view()->work_area->reset_cursor();
466 // Restore layer clicking
467 get_work_area()->allow_layer_clicks=prev_workarea_layer_status_;
469 // Restore duck clicking
470 get_work_area()->allow_duck_clicks=true;
472 // Enable the time bar
473 get_canvas_view()->set_sensitive_timebar(true);
475 // Bring back the tables if they were out before
476 if(prev_table_status)get_canvas_view()->show_tables();
478 // Refresh the work area
479 get_work_area()->queue_draw();
481 App::toolbox->refresh();
485 StateDraw_Context::event_stop_handler(const Smach::event& x)
487 throw Smach::egress_exception();
491 StateDraw_Context::event_refresh_handler(const Smach::event& x)
494 return Smach::RESULT_ACCEPT;
498 StateDraw_Context::event_mouse_down_handler(const Smach::event& x)
500 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
505 // Enter the stroke state to get the stroke
506 get_canvas_view()->get_smach().push_state(&state_stroke);
507 return Smach::RESULT_ACCEPT;
510 case BUTTON_RIGHT: // Intercept the right-button click to short-circut the pop-up menu
511 return Smach::RESULT_ACCEPT;
514 return Smach::RESULT_OK;
518 #define SIMILAR_TANGENT_THRESHOLD (0.2)
523 debugclass(const synfig::String &x):x(x)
525 // synfig::warning(">>>>>>>>>>>>>>>>>>> "+x);
529 // synfig::warning("<<<<<<<<<<<<<<<<<<< "+x);
536 DepthCounter(int &i):i(i) { i++; }
537 ~DepthCounter() { i--; }
541 StateDraw_Context::event_stroke(const Smach::event& x)
543 // debugclass debugger("StateDraw_Context::event_stroke(const Smach::event& x)");
545 const EventStroke& event(*reinterpret_cast<const EventStroke*>(&x));
547 assert(event.stroke_data);
549 get_work_area()->add_stroke(event.stroke_data,synfigapp::Main::get_foreground_color());
553 DirtyTrap dirty_trap(get_work_area());
554 Smach::event_result result;
555 result=process_stroke(event.stroke_data,event.width_data,event.modifier&Gdk::CONTROL_MASK || event.modifier&Gdk::BUTTON2_MASK);
560 stroke_queue.push_back(pair<StrokeData,WidthData>(event.stroke_data,event.width_data));
562 return Smach::RESULT_ACCEPT;
566 StateDraw_Context::process_queue()
568 // debugclass debugger("StateDraw_Context::process_queue()");
571 DepthCounter depth_counter(nested);
572 while(!stroke_queue.empty())
574 pair<StrokeData,WidthData> front(stroke_queue.front());
575 process_stroke(front.first,front.second);
576 stroke_queue.pop_front();
582 StateDraw_Context::process_stroke(StrokeData stroke_data, WidthData width_data, bool region_flag)
584 // debugclass debugger("StateDraw_Context::process_stroke");
585 DepthCounter depth_counter(nested);
587 const float radius(synfigapp::Main::get_bline_width().units(get_canvas()->rend_desc())+(abs(get_work_area()->get_pw())+abs(get_work_area()->get_ph()))*5);
590 // If we aren't using pressure width,
591 // then set all the width to 1
592 if(!get_pressure_width_flag())
594 std::list<synfig::Real>::iterator iter;
595 for(iter=width_data->begin();iter!=width_data->end();++iter)
601 //get_work_area()->add_stroke(event.stroke_data,synfigapp::Main::get_foreground_color());
602 //stroke_list.push_back(event.stroke_data);
605 std::list<synfig::BLinePoint> bline;
606 bool loop_bline_flag(false);
608 //Changed by Adrian - use resident class :)
609 //synfigapp::convert_stroke_to_bline(bline, *event.stroke_data,*event.width_data, synfigapp::Main::get_bline_width());
610 blineconv.width = synfigapp::Main::get_bline_width().units(get_canvas()->rend_desc());
612 if(get_local_error_flag())
614 float pw = get_work_area()->get_pw();
615 float ph = get_work_area()->get_ph();
617 blineconv.pixelwidth = sqrt(pw*pw+ph*ph);
618 blineconv.smoothness = get_lthres();
621 blineconv.pixelwidth = 1;
622 blineconv.smoothness = get_gthres();
625 blineconv(bline,*stroke_data,*width_data);
627 //Postprocess to require minimum pressure
628 if(get_min_pressure_flag())
630 synfigapp::BLineConverter::EnforceMinWidth(bline,get_min_pressure());
633 // If the start and end points are similar, then make them the same point
634 if(get_auto_loop_flag() &&
635 bline.size()>2&&(bline.front().get_vertex()-bline.back().get_vertex()).mag()<=radius)
637 loop_bline_flag=true;
641 while(bline.size()>2&&(bline.front().get_vertex()-bline.back().get_vertex()).mag()<=radius)
643 tangent=bline.back().get_tangent1();
644 width=bline.back().get_width();
648 if(abs(bline.front().get_tangent1().norm()*tangent.norm().perp())>SIMILAR_TANGENT_THRESHOLD)
650 // If the tangents are not similar, then
651 // split the tangents
652 bline.front().set_split_tangent_flag(true);
653 bline.front().set_tangent1(tangent);
657 // If the tangents are similar, then set the tangent
658 // to the average of the two
659 bline.front().set_tangent((tangent+bline.front().get_tangent1())*0.5f);
662 // Add the widths of the two points
664 Real width(bline.front().get_width()+width);
665 width=width<=1?width:1;
666 bline.front().set_width(width);
670 // If the bline only has once blinepoint, then there is nothing to do.
672 return Smach::RESULT_OK;
675 return new_region(bline,radius);
677 return new_bline(bline,loop_bline_flag,radius);
681 StateDraw_Context::new_bline(std::list<synfig::BLinePoint> bline,bool loop_bline_flag,float radius)
683 // Create the action group
684 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Sketch BLine"));
686 bool shift_offset = false;
687 Vector shift_offset_vector;
688 bool join_start_no_extend=false,join_finish_no_extend=false;
689 synfigapp::ValueDesc start_duck_value_desc,finish_duck_value_desc;
690 bool extend_start=false,extend_finish=false,complete_loop=false;
691 bool extend_start_join_same=false,extend_start_join_different=false;
692 bool extend_finish_join_same=false,extend_finish_join_different=false;
693 int start_duck_index,finish_duck_index;
694 ValueNode_BLine::Handle start_duck_value_node_bline=NULL,finish_duck_value_node_bline=NULL;
696 // Find any ducks at the start or end that we might attach to
697 // (this used to only run if we aren't a loop - ie. !loop_bline_flag
698 // but having loops auto-connect can be useful as well)
699 if(get_auto_extend_flag() || get_auto_link_flag())
701 etl::handle<Duck> start_duck(get_work_area()->find_duck(bline.front().get_vertex(),radius,Duck::TYPE_VERTEX));
702 etl::handle<Duck> finish_duck(get_work_area()->find_duck(bline.back().get_vertex(),radius,Duck::TYPE_VERTEX));
704 // check whether the start of the new line extends an
705 // existing line. this is only the case if the new
706 // line isn't a self-contained loop, and if the new
707 // line starts at one of the ends of an existing line
710 if(!(start_duck_value_desc=start_duck->get_value_desc()))break;
711 if(loop_bline_flag)break;
712 if(!start_duck_value_desc.parent_is_value_node())break;
713 start_duck_index=start_duck_value_desc.get_index();
714 start_duck_value_node_bline=ValueNode_BLine::Handle::cast_dynamic(start_duck_value_desc.get_parent_value_node());
715 if(!get_auto_extend_flag())break;
717 // don't extend looped blines
718 if(start_duck_value_node_bline&&!start_duck_value_node_bline->get_loop()&&
719 (start_duck_index==0||start_duck_index==start_duck_value_node_bline->link_count()-1))
723 shift_offset_vector=start_duck->get_origin();
727 // check whether the end of the new line extends an
728 // existing line. this is only the case if the new
729 // line isn't a self-contained loop, and if the new
730 // line ends at one of the ends of an existing line
733 if(!(finish_duck_value_desc=finish_duck->get_value_desc()))break;
734 if(loop_bline_flag)break;
735 if(!finish_duck_value_desc.parent_is_value_node())break;
736 finish_duck_index=finish_duck_value_desc.get_index();
737 finish_duck_value_node_bline=ValueNode_BLine::Handle::cast_dynamic(finish_duck_value_desc.get_parent_value_node());
738 if(!get_auto_extend_flag())break;
740 // don't extend looped blines
741 if(finish_duck_value_node_bline&&!finish_duck_value_node_bline->get_loop()&&
742 (finish_duck_index==0||finish_duck_index==finish_duck_value_node_bline->link_count()-1))
745 // we've started and finished drawing at the end of a bline. we can't
746 // extend both blines, so unless we started and finished at the 2 ends
747 // of the same bline, only extend the one we started on
748 if(start_duck_value_node_bline==finish_duck_value_node_bline)
749 complete_loop=extend_finish=true;
753 shift_offset_vector=finish_duck->get_origin();
757 // if the new line's start didn't extend an existing line,
758 // check whether it needs to be linked to an existing duck
759 if(!extend_start&&get_auto_link_flag()&&start_duck&&start_duck_value_desc)
760 switch(start_duck_value_desc.get_value_type())
762 case synfig::ValueBase::TYPE_BLINEPOINT:
763 start_duck_value_desc=synfigapp::ValueDesc(LinkableValueNode::Handle::cast_dynamic(start_duck_value_desc.get_value_node()),0);
765 case synfig::ValueBase::TYPE_VECTOR:
766 if (shift_offset && shift_offset_vector != start_duck->get_origin())
769 shift_offset_vector = start_duck->get_origin();
770 get_canvas_interface()->auto_export(start_duck_value_desc);
772 if(start_duck_value_node_bline&&start_duck_value_node_bline==finish_duck_value_node_bline)
773 extend_finish_join_same=true;
775 extend_finish_join_different=true;
777 join_start_no_extend=true;
782 // if the new line's end didn't extend an existing line,
783 // check whether it needs to be linked to an existing duck
784 if(!extend_finish&&get_auto_link_flag()&&finish_duck&&finish_duck_value_desc)
785 switch(finish_duck_value_desc.get_value_type())
787 case synfig::ValueBase::TYPE_BLINEPOINT:
788 finish_duck_value_desc=synfigapp::ValueDesc(LinkableValueNode::Handle::cast_dynamic(finish_duck_value_desc.get_value_node()),0);
790 case synfig::ValueBase::TYPE_VECTOR:
791 if (shift_offset && shift_offset_vector != finish_duck->get_origin())
794 shift_offset_vector = finish_duck->get_origin();
795 get_canvas_interface()->auto_export(finish_duck_value_desc);
797 if(finish_duck_value_node_bline&&start_duck_value_node_bline==finish_duck_value_node_bline)
798 extend_start_join_same=true;
800 extend_start_join_different=true;
802 join_finish_no_extend=true;
808 ValueNode_BLine::Handle value_node;
809 std::list<synfig::BLinePoint> trans_bline;
812 std::list<synfig::BLinePoint>::iterator iter;
813 const synfig::TransformStack& transform(get_canvas_view()->get_curr_transform_stack());
815 for(iter=bline.begin();iter!=bline.end();++iter)
817 BLinePoint bline_point(*iter);
818 Point new_vertex(transform.unperform(bline_point.get_vertex()));
820 bline_point.set_tangent1(
822 bline_point.get_tangent1()+bline_point.get_vertex()
826 bline_point.set_tangent2(
828 bline_point.get_tangent2()+bline_point.get_vertex()
833 new_vertex=new_vertex-shift_offset_vector;
835 bline_point.set_vertex(new_vertex);
837 trans_bline.push_back(bline_point);
839 value_node=ValueNode_BLine::create(synfig::ValueBase(trans_bline,loop_bline_flag));
842 Smach::event_result result;
843 synfig::ValueNode_DynamicList::ListEntry source;
846 // the new line's start extends an existing line
849 if(complete_loop)trans_bline.pop_back();
850 trans_bline.pop_front();
851 if(start_duck_index==0)
852 { // We need to reverse the BLine first.
853 reverse_bline(trans_bline);
854 result=extend_bline_from_begin(start_duck_value_node_bline,trans_bline,complete_loop);
855 source=start_duck_value_node_bline->list.front();
856 target_index=trans_bline.size()+finish_duck_index;
860 result=extend_bline_from_end(start_duck_value_node_bline,trans_bline,complete_loop);
861 source=start_duck_value_node_bline->list.back();
862 target_index=finish_duck_index;
865 if(extend_start_join_different)
866 LinkableValueNode::Handle::cast_dynamic(source.value_node)->
867 set_link(0,finish_duck_value_desc.get_value_node());
868 else if(extend_start_join_same)
869 LinkableValueNode::Handle::cast_dynamic(source.value_node)->
870 set_link(0,synfigapp::ValueDesc(LinkableValueNode::Handle::cast_dynamic(start_duck_value_node_bline->
871 list[target_index].value_node),0).get_value_node());
875 // the new line's end extends an existing line
877 { // SPECIAL CASE -- EXTENSION
878 trans_bline.pop_back();
879 if(finish_duck_index==0)
881 result=extend_bline_from_begin(finish_duck_value_node_bline,trans_bline,false);
882 source=finish_duck_value_node_bline->list.front();
883 target_index=trans_bline.size()+start_duck_index;
886 { // We need to reverse the BLine first.
887 reverse_bline(trans_bline);
888 result=extend_bline_from_end(finish_duck_value_node_bline,trans_bline,false);
889 source=finish_duck_value_node_bline->list.back();
890 target_index=start_duck_index;
893 if(extend_finish_join_different)
894 LinkableValueNode::Handle::cast_dynamic(source.value_node)->
895 set_link(0,start_duck_value_desc.get_value_node());
896 else if(extend_finish_join_same)
897 LinkableValueNode::Handle::cast_dynamic(source.value_node)->
898 set_link(0,synfigapp::ValueDesc(LinkableValueNode::Handle::cast_dynamic(finish_duck_value_node_bline->
899 list[target_index].value_node),0).get_value_node());
903 if (join_start_no_extend)
904 LinkableValueNode::Handle::cast_dynamic(value_node->list.front().value_node)->
905 set_link(0,start_duck_value_desc.get_value_node());
907 if (join_finish_no_extend)
908 LinkableValueNode::Handle::cast_dynamic(value_node->list.back().value_node)->
909 set_link(0,finish_duck_value_desc.get_value_node());
914 Canvas::Handle canvas(get_canvas_view()->get_canvas());
917 // we are temporarily using the layer to hold something
918 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
921 depth=layer->get_depth();
922 canvas=layer->get_canvas();
925 //int number(synfig::UniqueID().get_uid());
927 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
929 if(get_region_only_flag())
930 layer=get_canvas_interface()->add_layer_to("region",canvas,depth);
932 layer=get_canvas_interface()->add_layer_to("outline",canvas,depth);
936 layer->set_param("feather",get_feather());
937 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
940 //layer->set_description(strprintf("Stroke %d",number));
941 //get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
944 get_canvas_interface()->
945 change_value(synfigapp::ValueDesc(layer,"offset"),shift_offset_vector);
947 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
951 action->set_param("canvas",get_canvas());
952 action->set_param("canvas_interface",get_canvas_interface());
953 action->set_param("layer",layer);
954 if(!action->set_param("param",String("bline")))
955 synfig::error("LayerParamConnect didn't like \"param\"");
956 if(!action->set_param("value_node",ValueNode::Handle(value_node)))
957 synfig::error("LayerParamConnect didn't like \"value_node\"");
959 if(!get_canvas_interface()->get_instance()->perform_action(action))
961 get_canvas_view()->get_ui_interface()->error(_("Unable to create layer"));
964 return Smach::RESULT_ERROR;
966 get_canvas_view()->get_selection_manager()->set_selected_layer(layer);
970 last_stroke=value_node;
971 return Smach::RESULT_ACCEPT;
975 StateDraw_Context::new_region(std::list<synfig::BLinePoint> bline, synfig::Real radius)
977 // Create the action group
978 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Define Region"));
980 std::list<synfigapp::ValueDesc> vertex_list;
982 // First we need to come up with a rough list of
983 // BLinePoints that we are going to be using to
984 // define our region.
986 std::list<synfig::BLinePoint>::iterator iter;
987 for(iter=bline.begin();iter!=bline.end();++iter)
989 etl::handle<Duck> duck(get_work_area()->find_duck(iter->get_vertex(),0,Duck::TYPE_VERTEX));
993 synfig::info(__FILE__":%d: Nothing to enclose!",__LINE__);
994 return Smach::RESULT_OK;
998 assert(duck->get_type()==Duck::TYPE_VERTEX);
1000 synfigapp::ValueDesc value_desc(duck->get_value_desc());
1004 synfig::info(__FILE__":%d: Got a hit, but no ValueDesc on this duck",__LINE__);
1008 switch(value_desc.get_value_type())
1010 case synfig::ValueBase::TYPE_BLINEPOINT:
1011 //if(vertex_list.empty() || value_desc!=vertex_list.back())
1012 vertex_list.push_back(value_desc);
1013 assert(vertex_list.back().is_valid());
1022 if(vertex_list.size()<=2)
1024 synfig::info(__FILE__":%d: Vertex list too small to make region.",__LINE__);
1025 return Smach::RESULT_OK;
1028 assert(vertex_list.back().is_valid());
1030 // Remove any duplicates
1034 // Now we need to clean the list of vertices up
1035 // a bit. This includes inserting missing vertices
1036 // and removing extraneous ones.
1037 // We can do this in multiple passes.
1039 for(bool done=false;!done && i<30;i++)
1041 // Set done to "true" for now. If
1042 // any updates are performed, we will
1043 // change it back to false.
1046 std::list<synfigapp::ValueDesc>::iterator prev,iter,next;
1047 prev=vertex_list.end();prev--; // Set prev to the last ValueDesc
1048 next=vertex_list.begin();
1049 iter=next++; // Set iter to the first value desc, and next to the second
1051 for(;iter!=vertex_list.end();prev=iter,iter=next++)
1053 synfigapp::ValueDesc value_prev(*prev);
1054 synfigapp::ValueDesc value_desc(*iter);
1055 synfigapp::ValueDesc value_next((next==vertex_list.end())?vertex_list.front():*next);
1057 assert(value_desc.is_valid());
1058 assert(value_next.is_valid());
1059 assert(value_prev.is_valid());
1061 //synfig::info("-------");
1062 //synfig::info(__FILE__":%d: value_prev 0x%08X:%d",__LINE__,value_prev.get_parent_value_node().get(),value_prev.get_index());
1063 //synfig::info(__FILE__":%d: value_desc 0x%08X:%d",__LINE__,value_desc.get_parent_value_node().get(),value_desc.get_index());
1064 //synfig::info(__FILE__":%d: value_next 0x%08X:%d",__LINE__,value_next.get_parent_value_node().get(),value_next.get_index());
1067 if(value_prev.parent_is_value_node() && value_desc.parent_is_value_node() && value_next.parent_is_value_node())
1069 // Remove random extraneous vertices
1070 if(value_prev.get_parent_value_node()==value_next.get_parent_value_node() &&
1071 value_prev.get_parent_value_node()!=value_desc.get_parent_value_node())
1074 vertex_list.erase(iter);
1081 // Remove duplicate vertices
1082 if(value_prev.get_value_node()==value_desc.get_value_node()
1083 || value_desc.get_value_node()==value_next.get_value_node())
1086 vertex_list.erase(iter);
1090 if(value_prev.get_value_node()==value_next.get_value_node())
1093 vertex_list.erase(prev);
1098 if(value_desc.parent_is_value_node() && value_next.parent_is_value_node())
1099 if(value_desc.get_parent_value_node()==value_next.get_parent_value_node() && (next!=vertex_list.end()))
1101 // Fill in missing vertices
1102 if(value_desc.get_index()<value_next.get_index()-1)
1105 vertex_list.insert(next,synfigapp::ValueDesc(value_desc.get_parent_value_node(),value_desc.get_index()+1));
1109 if(value_next.get_index()<value_desc.get_index()-1)
1112 vertex_list.insert(next,synfigapp::ValueDesc(value_desc.get_parent_value_node(),value_next.get_index()+1));
1118 // Ensure that connections
1119 // between blines are properly
1121 if(value_desc.parent_is_value_node() && value_next.parent_is_value_node())
1122 if(value_desc.get_parent_value_node()!=value_next.get_parent_value_node() &&
1123 value_desc.get_value_node()!=value_next.get_value_node())
1125 BLinePoint vertex(value_desc.get_value(get_time()).get(BLinePoint()));
1126 BLinePoint vertex_next(value_next.get_value(get_time()).get(BLinePoint()));
1128 //synfig::info("--------");
1129 //synfig::info(__FILE__":%d: vertex: [%f, %f]",__LINE__,vertex.get_vertex()[0],vertex.get_vertex()[1]);
1130 //synfig::info(__FILE__":%d: vertex_next: [%f, %f]",__LINE__,vertex_next.get_vertex()[0],vertex_next.get_vertex()[1]);
1132 if((vertex.get_vertex()-vertex_next.get_vertex()).mag_squared()<radius*radius)
1135 ValueNode_Composite::Handle value_node;
1136 ValueNode_Composite::Handle value_node_next;
1137 value_node=ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node().clone());
1138 value_node_next=ValueNode_Composite::Handle::cast_dynamic(value_next.get_value_node().clone());
1139 if(!value_node || !value_node_next)
1141 synfig::info(__FILE__":%d: Unable to properly connect blines.",__LINE__);
1145 value_node->set_link(5,value_node_next->get_link(5));
1146 value_node->set_link(3,ValueNode_Const::create(true));
1148 get_canvas_interface()->auto_export(value_node);
1149 assert(value_node->is_exported());
1150 *iter=synfigapp::ValueDesc(get_canvas(),value_node->get_id());
1151 vertex_list.erase(next);
1158 bool positive_trend(value_desc.get_index()>value_prev.get_index());
1160 if(!positive_trend && value_desc.get_index()>0)
1163 vertex_list.insert(next,synfigapp::ValueDesc(value_desc.get_parent_value_node(),value_desc.get_index()-1));
1167 if(positive_trend && value_desc.get_index()<LinkableValueNode::Handle::cast_static(value_desc.get_value_node())->link_count()-1)
1170 vertex_list.insert(next,synfigapp::ValueDesc(value_desc.get_parent_value_node(),value_desc.get_index()+1));
1180 if(vertex_list.size()<=2)
1182 synfig::info(__FILE__":%d: Vertex list too small to make region.",__LINE__);
1183 return Smach::RESULT_OK;
1186 ValueNode_BLine::Handle value_node_bline;
1188 // Now we need to test for the trivial case,
1189 // which is where all of the vertices
1190 // come from one BLine.
1191 if(vertex_list.front().parent_is_linkable_value_node())
1193 bool trivial_case(true);
1194 ValueNode::Handle trivial_case_value_node;
1196 trivial_case_value_node=vertex_list.front().get_parent_value_node();
1198 std::list<synfigapp::ValueDesc>::iterator iter;
1199 for(iter=vertex_list.begin();iter!=vertex_list.end();++iter)
1201 if(trivial_case_value_node!=iter->get_parent_value_node())
1208 value_node_bline=ValueNode_BLine::Handle::cast_dynamic(trivial_case_value_node);
1211 // If we aren't the trivial case,
1212 // then go ahead and create the new
1214 if(!value_node_bline)
1216 value_node_bline=ValueNode_BLine::create();
1218 std::list<synfigapp::ValueDesc>::iterator iter;
1219 for(iter=vertex_list.begin();iter!=vertex_list.end();++iter)
1221 // Ensure that the vertex is exported.
1222 get_canvas_interface()->auto_export(*iter);
1224 value_node_bline->add(iter->get_value_node());
1225 //value_node_bline->add(ValueNode_BLine::ListEntry(iter->get_value_node()));
1228 value_node_bline->set_loop(true);
1231 get_canvas_interface()->auto_export(value_node_bline);
1233 // Now we create the region layer
1236 Layer::Handle layer;
1237 Canvas::Handle canvas(get_canvas_view()->get_canvas());
1240 // we are temporarily using the layer to hold something
1241 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
1244 depth=layer->get_depth();
1245 canvas=layer->get_canvas();
1248 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
1250 layer=get_canvas_interface()->add_layer_to("region",canvas,depth);
1252 layer->set_param("color",synfigapp::Main::get_background_color());
1255 layer->set_param("feather",get_feather());
1256 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
1258 get_canvas_interface()->signal_layer_param_changed()(layer,"color");
1260 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
1264 action->set_param("canvas",get_canvas());
1265 action->set_param("canvas_interface",get_canvas_interface());
1266 action->set_param("layer",layer);
1267 if(!action->set_param("param",String("bline")))
1268 synfig::error("LayerParamConnect didn't like \"param\"");
1269 if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
1270 synfig::error("LayerParamConnect didn't like \"value_node\"");
1272 if(!get_canvas_interface()->get_instance()->perform_action(action))
1274 get_canvas_view()->get_ui_interface()->error(_("Unable to create Region layer"));
1276 return Smach::RESULT_ERROR;
1278 get_canvas_view()->get_selection_manager()->set_selected_layer(layer);
1281 return Smach::RESULT_ACCEPT;
1285 StateDraw_Context::refresh_ducks()
1287 get_canvas_view()->queue_rebuild_ducks();
1289 get_work_area()->clear_ducks();
1292 std::list< etl::smart_ptr<std::list<synfig::Point> > >::iterator iter;
1294 for(iter=stroke_list.begin();iter!=stroke_list.end();++iter)
1296 get_work_area()->add_stroke(*iter);
1299 get_work_area()->queue_draw();
1305 StateDraw_Context::extend_bline_from_begin(ValueNode_BLine::Handle value_node,std::list<synfig::BLinePoint> bline,bool complete_loop)
1307 // Create the action group
1308 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Extend BLine"));
1312 synfigapp::Action::Handle action(synfigapp::Action::create("value_node_dynamic_list_loop"));
1315 action->set_param("canvas",get_canvas());
1316 action->set_param("canvas_interface",get_canvas_interface());
1317 action->set_param("value_node",ValueNode::Handle(value_node));
1319 if(!get_canvas_interface()->get_instance()->perform_action(action))
1321 get_canvas_view()->get_ui_interface()->error(_("Unable to set loop for bline"));
1323 return Smach::RESULT_ERROR;
1327 std::list<synfig::BLinePoint>::reverse_iterator iter;
1328 for(iter=bline.rbegin();!(iter==bline.rend());++iter)
1330 ValueNode_Composite::Handle composite(ValueNode_Composite::create(*iter));
1332 synfigapp::Action::Handle action(synfigapp::Action::create("value_node_dynamic_list_insert"));
1335 synfigapp::ValueDesc value_desc(value_node,0);
1337 action->set_param("canvas",get_canvas());
1338 action->set_param("canvas_interface",get_canvas_interface());
1339 action->set_param("value_desc",value_desc);
1340 if(!action->set_param("item",ValueNode::Handle(composite)))
1341 synfig::error("ACTION didn't like \"item\"");
1343 if(!get_canvas_interface()->get_instance()->perform_action(action))
1345 get_canvas_view()->get_ui_interface()->error(_("Unable to insert item"));
1348 return Smach::RESULT_ERROR;
1351 last_stroke=value_node;
1352 return Smach::RESULT_ACCEPT;
1356 StateDraw_Context::extend_bline_from_end(ValueNode_BLine::Handle value_node,std::list<synfig::BLinePoint> bline,bool complete_loop)
1358 // Create the action group
1359 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Extend BLine"));
1363 synfigapp::Action::Handle action(synfigapp::Action::create("value_node_dynamic_list_loop"));
1366 action->set_param("canvas",get_canvas());
1367 action->set_param("canvas_interface",get_canvas_interface());
1368 action->set_param("value_node",ValueNode::Handle(value_node));
1370 if(!get_canvas_interface()->get_instance()->perform_action(action))
1372 get_canvas_view()->get_ui_interface()->error(_("Unable to set loop for bline"));
1374 return Smach::RESULT_ERROR;
1378 std::list<synfig::BLinePoint>::iterator iter;
1379 for(iter=bline.begin();iter!=bline.end();++iter)
1381 ValueNode_Composite::Handle composite(ValueNode_Composite::create(*iter));
1383 synfigapp::Action::Handle action(synfigapp::Action::create("value_node_dynamic_list_insert"));
1386 synfigapp::ValueDesc value_desc(value_node,value_node->link_count());
1388 action->set_param("canvas",get_canvas());
1389 action->set_param("canvas_interface",get_canvas_interface());
1390 action->set_param("value_desc",value_desc);
1391 if(!action->set_param("item",ValueNode::Handle(composite)))
1392 synfig::error("ACTION didn't like \"item\"");
1394 if(!get_canvas_interface()->get_instance()->perform_action(action))
1396 get_canvas_view()->get_ui_interface()->error(_("Unable to insert item"));
1399 return Smach::RESULT_ERROR;
1402 last_stroke=value_node;
1403 return Smach::RESULT_ACCEPT;
1407 StateDraw_Context::reverse_bline(std::list<synfig::BLinePoint> &bline)
1411 std::list<synfig::BLinePoint>::iterator iter,eiter;
1415 for(i=0;i<(int)bline.size()/2;++iter,--eiter,i++)
1417 iter_swap(iter,eiter);
1424 StateDraw_Context::fill_last_stroke()
1429 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Fill Stroke"));
1431 Layer::Handle layer;
1433 get_canvas_interface()->auto_export(last_stroke);
1435 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
1437 layer=get_canvas_interface()->add_layer("region");
1439 layer->set_param("color",synfigapp::Main::get_background_color());
1441 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
1445 action->set_param("canvas",get_canvas());
1446 action->set_param("canvas_interface",get_canvas_interface());
1447 action->set_param("layer",layer);
1448 if(!action->set_param("param",String("segment_list")))
1449 synfig::error("LayerParamConnect didn't like \"param\"");
1450 if(!action->set_param("value_node",ValueNode::Handle(last_stroke)))
1451 synfig::error("LayerParamConnect didn't like \"value_node\"");
1453 if(!get_canvas_interface()->get_instance()->perform_action(action))
1455 get_canvas_view()->get_ui_interface()->error(_("Unable to create Region layer"));
1459 get_canvas_view()->get_selection_manager()->set_selected_layer(layer);