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
10 ** Copyright (c) 2009 Nikita Kitaev
12 ** This package is free software; you can redistribute it and/or
13 ** modify it under the terms of the GNU General Public License as
14 ** published by the Free Software Foundation; either version 2 of
15 ** the License, or (at your option) any later version.
17 ** This package is distributed in the hope that it will be useful,
18 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ** General Public License for more details.
23 /* ========================================================================= */
25 /* === H E A D E R S ======================================================= */
34 #include <gtkmm/dialog.h>
35 #include <gtkmm/entry.h>
37 #include <synfig/valuenode_dynamiclist.h>
39 #include "state_draw.h"
40 #include "state_stroke.h"
41 #include "canvasview.h"
44 #include <synfig/valuenode_bline.h>
45 #include <synfig/valuenode_composite.h>
46 #include <ETL/hermite>
47 #include <ETL/calculus>
49 #include "event_mouse.h"
50 #include "event_layerclick.h"
53 #include <synfigapp/blineconvert.h>
54 #include <synfigapp/main.h>
56 #include <ETL/gaussian>
57 #include "dialog_tooloptions.h"
59 #include <gtkmm/table.h>
60 #include <gtkmm/label.h>
61 #include <gtkmm/button.h>
62 #include <gtkmm/checkbutton.h>
63 #include <gtkmm/scale.h>
64 #include <sigc++/connection.h>
70 /* === U S I N G =========================================================== */
74 using namespace synfig;
75 using namespace studio;
77 /* === M A C R O S ========================================================= */
79 /* === G L O B A L S ======================================================= */
81 StateDraw studio::state_draw;
83 /* === C L A S S E S & S T R U C T S ======================================= */
85 class studio::StateDraw_Context : public sigc::trackable
87 typedef etl::smart_ptr<std::list<synfig::Point> > StrokeData;
88 typedef etl::smart_ptr<std::list<synfig::Real> > WidthData;
90 typedef list< pair<StrokeData,WidthData> > StrokeQueue;
92 StrokeQueue stroke_queue;
95 etl::handle<CanvasView> canvas_view_;
96 CanvasView::IsWorking is_working;
98 bool prev_table_status;
100 bool prev_workarea_layer_status_;
103 sigc::connection process_queue_connection;
105 ValueNode_BLine::Handle last_stroke;
106 synfig::String last_stroke_id;
110 //Duckmatic::Push duckmatic_push;
112 std::list< etl::smart_ptr<std::list<synfig::Point> > > stroke_list;
114 void refresh_ducks();
116 Duckmatic::Type old_duckmask;
118 void fill_last_stroke();
119 Smach::event_result fill_last_stroke_and_unselect_other_layers();
121 Smach::event_result new_bline(std::list<synfig::BLinePoint> bline,bool loop_bline_flag,float radius);
123 Smach::event_result new_region(std::list<synfig::BLinePoint> bline,synfig::Real radius);
125 Smach::event_result extend_bline_from_begin(ValueNode_BLine::Handle value_node,std::list<synfig::BLinePoint> bline,bool complete_loop);
126 Smach::event_result extend_bline_from_end(ValueNode_BLine::Handle value_node,std::list<synfig::BLinePoint> bline,bool complete_loop);
127 void reverse_bline(std::list<synfig::BLinePoint> &bline);
129 synfigapp::Settings& settings;
131 Gtk::Table options_table;
133 Gtk::CheckButton checkbutton_pressure_width;
134 Gtk::CheckButton checkbutton_round_ends;
135 Gtk::CheckButton checkbutton_auto_loop; // whether to loop new strokes which start and end in the same place
136 Gtk::CheckButton checkbutton_auto_extend; // whether to extend existing lines
137 Gtk::CheckButton checkbutton_auto_link; // whether to link new ducks to existing ducks
138 Gtk::CheckButton checkbutton_region; // whether to create regions
139 Gtk::CheckButton checkbutton_outline; // whether to create outlines
140 Gtk::CheckButton checkbutton_auto_export;
141 Gtk::Button button_fill_last_stroke;
143 //pressure spinner and such
144 Gtk::Adjustment adj_min_pressure;
145 Gtk::SpinButton spin_min_pressure;
146 Gtk::CheckButton check_min_pressure;
148 Gtk::Adjustment adj_feather;
149 Gtk::SpinButton spin_feather;
151 Gtk::Adjustment adj_globalthres;
152 Gtk::SpinButton spin_globalthres;
154 Gtk::Adjustment adj_localthres;
155 Gtk::CheckButton check_localerror;
156 void UpdateErrorBox(); //switches the stuff if need be :)
158 //Added by Adrian - data drive HOOOOO
159 synfigapp::BLineConverter blineconv;
162 synfig::String get_id()const { return entry_id.get_text(); }
163 void set_id(const synfig::String& x) { return entry_id.set_text(x); }
165 bool get_pressure_width_flag()const { return checkbutton_pressure_width.get_active(); }
166 void set_pressure_width_flag(bool x) { return checkbutton_pressure_width.set_active(x); }
168 bool get_auto_loop_flag()const { return checkbutton_auto_loop.get_active(); }
169 void set_auto_loop_flag(bool x) { return checkbutton_auto_loop.set_active(x); }
171 bool get_auto_extend_flag()const { return checkbutton_auto_extend.get_active(); }
172 void set_auto_extend_flag(bool x) { return checkbutton_auto_extend.set_active(x); }
174 bool get_auto_link_flag()const { return checkbutton_auto_link.get_active(); }
175 void set_auto_link_flag(bool x) { return checkbutton_auto_link.set_active(x); }
177 bool get_region_flag()const { return checkbutton_region.get_active(); }
178 void set_region_flag(bool x) { return checkbutton_region.set_active(x); }
180 bool get_outline_flag()const { return checkbutton_outline.get_active(); }
181 void set_outline_flag(bool x) { return checkbutton_outline.set_active(x); }
183 bool get_auto_export_flag()const { return checkbutton_auto_export.get_active(); }
184 void set_auto_export_flag(bool x) { return checkbutton_auto_export.set_active(x); }
186 Real get_min_pressure() const { return adj_min_pressure.get_value(); }
187 void set_min_pressure(Real x) { return adj_min_pressure.set_value(x); }
189 Real get_feather() const { return adj_feather.get_value(); }
190 void set_feather(Real x) { return adj_feather.set_value(x); }
192 Real get_gthres() const { return adj_globalthres.get_value(); }
193 void set_gthres(Real x) { return adj_globalthres.set_value(x); }
195 Real get_lthres() const { return adj_localthres.get_value(); }
196 void set_lthres(Real x) { return adj_localthres.set_value(x); }
198 bool get_local_error_flag() const { return check_localerror.get_active(); }
199 void set_local_error_flag(bool x) { check_localerror.set_active(x); }
201 bool get_min_pressure_flag()const { return check_min_pressure.get_active(); }
202 void set_min_pressure_flag(bool x) { check_min_pressure.set_active(x); }
204 void load_settings();
205 void save_settings();
208 Smach::event_result event_stop_handler(const Smach::event& x);
210 Smach::event_result event_refresh_handler(const Smach::event& x);
212 Smach::event_result event_mouse_down_handler(const Smach::event& x);
214 Smach::event_result event_stroke(const Smach::event& x);
215 Smach::event_result event_refresh_tool_options(const Smach::event& x);
216 void refresh_tool_options();
218 Smach::event_result process_stroke(StrokeData stroke_data, WidthData width_data, bool region_flag=false);
220 bool process_queue();
223 StateDraw_Context(CanvasView* canvas_view);
225 ~StateDraw_Context();
227 const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
228 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
229 synfig::Time get_time()const { return get_canvas_interface()->get_time(); }
230 synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
231 WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
233 //void on_user_click(synfig::Point point);
236 }; // END of class StateDraw_Context
239 /* === M E T H O D S ======================================================= */
241 StateDraw::StateDraw():
242 Smach::state<StateDraw_Context>("draw")
244 insert(event_def(EVENT_STOP,&StateDraw_Context::event_stop_handler));
245 insert(event_def(EVENT_REFRESH,&StateDraw_Context::event_refresh_handler));
246 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateDraw_Context::event_mouse_down_handler));
247 insert(event_def(EVENT_WORKAREA_STROKE,&StateDraw_Context::event_stroke));
248 insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateDraw_Context::event_refresh_tool_options));
251 StateDraw::~StateDraw()
257 StateDraw_Context::load_settings()
261 if(settings.get_value("draw.id",value))
264 set_id("NewDrawing");
266 if(settings.get_value("draw.pressure_width",value) && value=="0")
267 set_pressure_width_flag(false);
269 set_pressure_width_flag(true);
271 if(settings.get_value("draw.auto_loop",value) && value=="0")
272 set_auto_loop_flag(false);
274 set_auto_loop_flag(true);
276 if(settings.get_value("draw.auto_extend",value) && value=="0")
277 set_auto_extend_flag(false);
279 set_auto_extend_flag(true);
281 if(settings.get_value("draw.auto_link",value) && value=="0")
282 set_auto_link_flag(false);
284 set_auto_link_flag(true);
286 if(settings.get_value("draw.region",value) && value=="0")
287 set_region_flag(false);
289 set_region_flag(true);
291 if(settings.get_value("draw.outline",value) && value=="0")
292 set_outline_flag(false);
294 set_outline_flag(true);
296 if(settings.get_value("draw.auto_export",value) && value=="1")
297 set_auto_export_flag(true);
299 set_auto_export_flag(false);
301 if(settings.get_value("draw.min_pressure_on",value) && value=="0")
302 set_min_pressure_flag(false);
304 set_min_pressure_flag(true);
306 if(settings.get_value("draw.min_pressure",value))
308 Real n = atof(value.c_str());
313 if(settings.get_value("draw.feather",value))
315 Real n = atof(value.c_str());
320 if(settings.get_value("draw.gthreshold",value))
322 Real n = atof(value.c_str());
326 if(settings.get_value("draw.lthreshold",value))
328 Real n = atof(value.c_str());
332 if(settings.get_value("draw.localize",value) && value == "1")
333 set_local_error_flag(true);
335 set_local_error_flag(false);
339 StateDraw_Context::save_settings()
341 settings.set_value("draw.id",get_id().c_str());
342 settings.set_value("draw.pressure_width",get_pressure_width_flag()?"1":"0");
343 settings.set_value("draw.auto_loop",get_auto_loop_flag()?"1":"0");
344 settings.set_value("draw.auto_extend",get_auto_extend_flag()?"1":"0");
345 settings.set_value("draw.auto_link",get_auto_link_flag()?"1":"0");
346 settings.set_value("draw.region",get_region_flag()?"1":"0");
347 settings.set_value("draw.outline",get_outline_flag()?"1":"0");
348 settings.set_value("draw.auto_export",get_auto_export_flag()?"1":"0");
349 settings.set_value("draw.min_pressure",strprintf("%f",get_min_pressure()));
350 settings.set_value("draw.feather",strprintf("%f",get_feather()));
351 settings.set_value("draw.min_pressure_on",get_min_pressure_flag()?"1":"0");
352 settings.set_value("draw.gthreshold",strprintf("%f",get_gthres()));
353 settings.set_value("draw.lthreshold",strprintf("%f",get_lthres()));
354 settings.set_value("draw.localize",get_local_error_flag()?"1":"0");
358 StateDraw_Context::increment_id()
367 // If there is a number
368 // already at the end of the
369 // id, then remove it.
370 if(id[id.size()-1]<='9' && id[id.size()-1]>='0')
372 // figure out how many digits it is
374 (int)id.size()-1 >= digits && id[id.size()-1-digits] <= '9' && id[id.size()-1-digits] >= '0';
379 str_number=String(id,id.size()-digits,id.size());
380 id=String(id,0,id.size()-digits);
381 // synfig::info("---------------- \"%s\"",str_number.c_str());
383 number=atoi(str_number.c_str());
393 // Add the number back onto the id
395 const String format(strprintf("%%0%dd",digits));
396 id+=strprintf(format.c_str(),number);
403 StateDraw_Context::StateDraw_Context(CanvasView* canvas_view):
404 canvas_view_(canvas_view),
405 is_working(*canvas_view),
407 prev_workarea_layer_status_(get_work_area()->get_allow_layer_clicks()),
408 settings(synfigapp::Main::get_selected_input_device()->settings()),
410 checkbutton_pressure_width(_("Pressure Width")),
411 checkbutton_auto_loop(_("Auto Loop")),
412 checkbutton_auto_extend(_("Auto Extend")),
413 checkbutton_auto_link(_("Auto Link")),
414 checkbutton_region(_("Create Region BLine")),
415 checkbutton_outline(_("Create Outline BLine")),
416 checkbutton_auto_export(_("Auto Export")),
417 button_fill_last_stroke(_("Fill Last Stroke")),
418 adj_min_pressure(0,0,1,0.01,0.1),
419 spin_min_pressure(adj_min_pressure,0.1,3),
420 check_min_pressure(_("Min Pressure")),
421 adj_feather(0,0,10000,0.01,0.1),
422 spin_feather(adj_feather,0.01,4),
423 adj_globalthres(.70f,0.01,10000,0.01,0.1),
424 spin_globalthres(adj_globalthres,0.01,3),
425 adj_localthres(20,1,100000,0.1,1),
426 check_localerror(_("LocalError"))
434 options_table.attach(*manage(new Gtk::Label(_("Draw Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
435 options_table.attach(entry_id, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
436 options_table.attach(checkbutton_outline, 0, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
437 options_table.attach(checkbutton_region, 0, 2, 3, 4, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
438 options_table.attach(checkbutton_auto_loop, 0, 2, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
439 options_table.attach(checkbutton_auto_extend, 0, 2, 5, 6, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
440 options_table.attach(checkbutton_auto_link, 0, 2, 6, 7, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
441 options_table.attach(checkbutton_auto_export, 0, 2, 7, 8, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
442 options_table.attach(checkbutton_pressure_width, 0, 2, 8, 9, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
443 options_table.attach(check_localerror, 0, 2, 9, 10, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
445 options_table.attach(check_min_pressure, 0, 1, 10, 11, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
446 options_table.attach(spin_min_pressure, 1, 2, 10, 11, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
448 options_table.attach(*manage(new Gtk::Label(_("Smooth"))), 0, 1, 11, 12, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
449 options_table.attach(spin_globalthres, 1, 2, 11, 12, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
451 options_table.attach(*manage(new Gtk::Label(_("Feather"))), 0, 1, 12, 13, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
452 options_table.attach(spin_feather, 1, 2, 12, 13, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
454 //options_table.attach(button_fill_last_stroke, 0, 2, 13, 14, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
456 button_fill_last_stroke.signal_pressed().connect(sigc::mem_fun(*this,&StateDraw_Context::fill_last_stroke));
457 check_localerror.signal_toggled().connect(sigc::mem_fun(*this,&StateDraw_Context::UpdateErrorBox));
459 options_table.show_all();
460 refresh_tool_options();
461 //App::dialog_tool_options->set_widget(options_table);
462 App::dialog_tool_options->present();
465 old_duckmask=get_work_area()->get_type_mask();
466 get_work_area()->set_type_mask(Duck::TYPE_ALL-Duck::TYPE_TANGENT-Duck::TYPE_WIDTH);
468 // Turn off layer clicking
469 get_work_area()->set_allow_layer_clicks(false);
471 // Turn off duck clicking
472 get_work_area()->set_allow_duck_clicks(false);
474 // clear out the ducks
475 //get_work_area()->clear_ducks();
477 // Refresh the work area
478 //get_work_area()->queue_draw();
480 // Hide the tables if they are showing
481 prev_table_status=get_canvas_view()->tables_are_visible();
482 //if(prev_table_status)get_canvas_view()->hide_tables();
484 // Disable the time bar
485 get_canvas_view()->set_sensitive_timebar(false);
488 //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StateDraw_Context::on_user_click));
490 get_canvas_view()->work_area->set_cursor(Gdk::PENCIL);
492 App::toolbox->refresh();
498 void StateDraw_Context::UpdateErrorBox()
500 if(get_local_error_flag())
502 spin_globalthres.set_adjustment(adj_localthres);
503 spin_globalthres.set_value(adj_localthres.get_value());
504 spin_globalthres.set_increments(0.1,1);
507 spin_globalthres.set_adjustment(adj_globalthres);
508 spin_globalthres.set_value(adj_globalthres.get_value());
509 spin_globalthres.set_increments(0.01,.1);
512 spin_globalthres.update();
516 StateDraw_Context::refresh_tool_options()
518 App::dialog_tool_options->clear();
519 App::dialog_tool_options->set_widget(options_table);
520 App::dialog_tool_options->set_local_name(_("Draw Tool"));
521 App::dialog_tool_options->set_name("draw");
523 App::dialog_tool_options->add_button(
524 Gtk::StockID("synfig-fill"),
525 _("Fill Last Stroke")
526 )->signal_clicked().connect(
529 &StateDraw_Context::fill_last_stroke));
533 StateDraw_Context::event_refresh_tool_options(const Smach::event& /*x*/)
535 refresh_tool_options();
536 return Smach::RESULT_ACCEPT;
539 StateDraw_Context::~StateDraw_Context()
543 App::dialog_tool_options->clear();
545 get_work_area()->set_type_mask(old_duckmask);
547 get_canvas_view()->work_area->reset_cursor();
549 // Restore layer clicking
550 get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
552 // Restore duck clicking
553 get_work_area()->set_allow_duck_clicks(true);
555 // Enable the time bar
556 get_canvas_view()->set_sensitive_timebar(true);
558 // Bring back the tables if they were out before
559 if(prev_table_status)get_canvas_view()->show_tables();
561 // Refresh the work area
562 get_work_area()->queue_draw();
564 App::toolbox->refresh();
568 StateDraw_Context::event_stop_handler(const Smach::event& /*x*/)
570 throw Smach::egress_exception();
574 StateDraw_Context::event_refresh_handler(const Smach::event& /*x*/)
577 return Smach::RESULT_ACCEPT;
581 StateDraw_Context::event_mouse_down_handler(const Smach::event& x)
583 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
588 // Enter the stroke state to get the stroke
589 get_canvas_view()->get_smach().push_state(&state_stroke);
590 return Smach::RESULT_ACCEPT;
594 return Smach::RESULT_OK;
598 #define SIMILAR_TANGENT_THRESHOLD (0.2)
603 debugclass(const synfig::String &x):x(x)
605 // synfig::warning(">>>>>>>>>>>>>>>>>>> "+x);
609 // synfig::warning("<<<<<<<<<<<<<<<<<<< "+x);
616 DepthCounter(int &i):i(i) { i++; }
617 ~DepthCounter() { i--; }
621 StateDraw_Context::event_stroke(const Smach::event& x)
623 // debugclass debugger("StateDraw_Context::event_stroke(const Smach::event& x)");
625 const EventStroke& event(*reinterpret_cast<const EventStroke*>(&x));
627 assert(event.stroke_data);
629 get_work_area()->add_stroke(event.stroke_data,synfigapp::Main::get_outline_color());
633 DirtyTrap dirty_trap(get_work_area());
634 Smach::event_result result;
635 result=process_stroke(event.stroke_data,event.width_data,event.modifier&Gdk::CONTROL_MASK || event.modifier&Gdk::BUTTON2_MASK);
640 stroke_queue.push_back(pair<StrokeData,WidthData>(event.stroke_data,event.width_data));
642 return Smach::RESULT_ACCEPT;
646 StateDraw_Context::process_queue()
648 // debugclass debugger("StateDraw_Context::process_queue()");
651 DepthCounter depth_counter(nested);
652 while(!stroke_queue.empty())
654 pair<StrokeData,WidthData> front(stroke_queue.front());
655 process_stroke(front.first,front.second);
656 stroke_queue.pop_front();
662 StateDraw_Context::process_stroke(StrokeData stroke_data, WidthData width_data, bool region_flag)
664 // debugclass debugger("StateDraw_Context::process_stroke");
665 DepthCounter depth_counter(nested);
667 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);
670 // If we aren't using pressure width,
671 // then set all the width to 1
672 if(!get_pressure_width_flag())
674 std::list<synfig::Real>::iterator iter;
675 for(iter=width_data->begin();iter!=width_data->end();++iter)
681 //get_work_area()->add_stroke(event.stroke_data,synfigapp::Main::get_outline_color());
682 //stroke_list.push_back(event.stroke_data);
685 std::list<synfig::BLinePoint> bline;
686 bool loop_bline_flag(false);
688 //Changed by Adrian - use resident class :)
689 //synfigapp::convert_stroke_to_bline(bline, *event.stroke_data,*event.width_data, synfigapp::Main::get_bline_width());
690 blineconv.width = synfigapp::Main::get_bline_width().units(get_canvas()->rend_desc());
692 if(get_local_error_flag())
694 float pw = get_work_area()->get_pw();
695 float ph = get_work_area()->get_ph();
697 blineconv.pixelwidth = sqrt(pw*pw+ph*ph);
698 blineconv.smoothness = get_lthres();
701 blineconv.pixelwidth = 1;
702 blineconv.smoothness = get_gthres();
705 blineconv(bline,*stroke_data,*width_data);
707 //Postprocess to require minimum pressure
708 if(get_min_pressure_flag())
710 synfigapp::BLineConverter::EnforceMinWidth(bline,get_min_pressure());
713 // If the start and end points are similar, then make them the same point
714 if (get_auto_loop_flag() &&
716 (bline.front().get_vertex() - bline.back().get_vertex()).mag() <= radius)
718 loop_bline_flag=true;
722 while (bline.size() > 2 &&
723 (bline.front().get_vertex() - bline.back().get_vertex()).mag() <= radius)
725 tangent=bline.back().get_tangent1();
726 width=bline.back().get_width();
730 if(abs(bline.front().get_tangent1().norm()*tangent.norm().perp())>SIMILAR_TANGENT_THRESHOLD)
732 // If the tangents are not similar, then
733 // split the tangents
734 bline.front().set_split_tangent_flag(true);
735 bline.front().set_tangent1(tangent);
739 // If the tangents are similar, then set the tangent
740 // to the average of the two
741 bline.front().set_tangent((tangent+bline.front().get_tangent1())*0.5f);
744 // Add the widths of the two points
746 Real tmp_width(bline.front().get_width()+width);
747 tmp_width=tmp_width<=1?tmp_width:1;
748 bline.front().set_width(tmp_width);
752 // If the bline only has one blinepoint, then there is nothing to do.
755 // hide the 'stroke' line we were drawing, unless the user
756 // explicitly requests that they are kept
757 if (!getenv("SYNFIG_KEEP_ABORTED_DRAW_LINES"))
760 return Smach::RESULT_OK;
764 return new_region(bline,radius);
766 return new_bline(bline,loop_bline_flag,radius);
770 StateDraw_Context::new_bline(std::list<synfig::BLinePoint> bline,bool loop_bline_flag,float radius)
772 synfigapp::SelectionManager::LayerList layer_list = get_canvas_view()->get_selection_manager()->get_selected_layers();
774 // Create the action group
775 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Sketch BLine"));
777 bool shift_origin = false;
778 Vector shift_origin_vector;
779 bool join_start_no_extend=false,join_finish_no_extend=false;
780 synfigapp::ValueDesc start_duck_value_desc,finish_duck_value_desc;
781 bool extend_start=false,extend_finish=false,complete_loop=false;
782 bool extend_start_join_same=false,extend_start_join_different=false;
783 bool extend_finish_join_same=false,extend_finish_join_different=false;
784 int start_duck_index = 0,finish_duck_index = 0; // initialized to keep the compiler happy; shouldn't be needed though
785 ValueNode_BLine::Handle start_duck_value_node_bline=NULL,finish_duck_value_node_bline=NULL;
787 // Find any ducks at the start or end that we might attach to
788 // (this used to only run if we didn't just draw a loop - ie. !loop_bline_flag
789 // but having loops auto-connect can be useful as well)
790 if(get_auto_extend_flag() || get_auto_link_flag())
792 etl::handle<Duck> start_duck(get_work_area()->find_duck(bline.front().get_vertex(),radius,Duck::TYPE_VERTEX));
793 etl::handle<Duck> finish_duck(get_work_area()->find_duck(bline.back().get_vertex(),radius,Duck::TYPE_VERTEX));
795 // check whether the start of the new line extends an
796 // existing line. this is only the case if the new
797 // line isn't a self-contained loop, and if the new
798 // line starts at one of the ends of an existing line
801 if(!(start_duck_value_desc=start_duck->get_value_desc()))break;
802 if(loop_bline_flag)break; // loops don't extend anything
803 if(!start_duck_value_desc.parent_is_value_node())break;
804 start_duck_index=start_duck_value_desc.get_index(); // which point on the line did we start drawing at
805 start_duck_value_node_bline=ValueNode_BLine::Handle::cast_dynamic(start_duck_value_desc.get_parent_value_node());
806 if(!get_auto_extend_flag())break;
808 // don't extend looped blines
809 if(start_duck_value_node_bline&&!start_duck_value_node_bline->get_loop()&&
810 // did we start drawing at either end of the line?
811 (start_duck_index==0||start_duck_index==start_duck_value_node_bline->link_count()-1))
815 shift_origin_vector=start_duck->get_origin();
819 // check whether the end of the new line extends an
820 // existing line. this is only the case if the new
821 // line isn't a self-contained loop, and if the new
822 // line ends at one of the ends of an existing line
825 if(!(finish_duck_value_desc=finish_duck->get_value_desc()))break;
826 if(loop_bline_flag)break;
827 if(!finish_duck_value_desc.parent_is_value_node())break;
828 finish_duck_index=finish_duck_value_desc.get_index();
829 finish_duck_value_node_bline=ValueNode_BLine::Handle::cast_dynamic(finish_duck_value_desc.get_parent_value_node());
830 if(!get_auto_extend_flag())break;
832 // don't extend looped blines
833 if(finish_duck_value_node_bline&&!finish_duck_value_node_bline->get_loop()&&
834 (finish_duck_index==0||finish_duck_index==finish_duck_value_node_bline->link_count()-1))
838 // we've started and finished drawing at the end of a bline. we can't
839 // extend both blines, so unless we started and finished at the 2 ends
840 // of the same bline, only extend the one we started on
841 if(start_duck_value_node_bline==finish_duck_value_node_bline)
842 complete_loop=extend_finish=true;
846 shift_origin_vector=finish_duck->get_origin();
851 // if the new line's start didn't extend an existing line,
852 // check whether it needs to be linked to an existing duck
853 if(!extend_start&&get_auto_link_flag()&&start_duck&&start_duck_value_desc)
854 switch(start_duck_value_desc.get_value_type())
856 case synfig::ValueBase::TYPE_BLINEPOINT:
857 start_duck_value_desc=synfigapp::ValueDesc(LinkableValueNode::Handle::cast_dynamic(start_duck_value_desc.get_value_node()),0);
859 case synfig::ValueBase::TYPE_VECTOR:
860 if (shift_origin && shift_origin_vector != start_duck->get_origin())
863 shift_origin_vector = start_duck->get_origin();
864 get_canvas_interface()->auto_export(start_duck_value_desc);
866 if(start_duck_value_node_bline&&start_duck_value_node_bline==finish_duck_value_node_bline)
867 extend_finish_join_same=true;
869 extend_finish_join_different=true;
871 join_start_no_extend=true;
876 // if the new line's end didn't extend an existing line,
877 // check whether it needs to be linked to an existing duck
878 if(!extend_finish&&get_auto_link_flag()&&finish_duck&&finish_duck_value_desc)
879 switch(finish_duck_value_desc.get_value_type())
881 case synfig::ValueBase::TYPE_BLINEPOINT:
882 finish_duck_value_desc=synfigapp::ValueDesc(LinkableValueNode::Handle::cast_dynamic(finish_duck_value_desc.get_value_node()),0);
884 case synfig::ValueBase::TYPE_VECTOR:
885 if (shift_origin && shift_origin_vector != finish_duck->get_origin())
888 shift_origin_vector = finish_duck->get_origin();
889 get_canvas_interface()->auto_export(finish_duck_value_desc);
891 if(finish_duck_value_node_bline&&start_duck_value_node_bline==finish_duck_value_node_bline)
892 extend_start_join_same=true;
894 extend_start_join_different=true;
896 join_finish_no_extend=true;
902 ValueNode_BLine::Handle value_node;
903 std::list<synfig::BLinePoint> trans_bline;
906 std::list<synfig::BLinePoint>::iterator iter;
907 const synfig::TransformStack& transform(get_canvas_view()->get_curr_transform_stack());
909 for(iter=bline.begin();iter!=bline.end();++iter)
911 BLinePoint bline_point(*iter);
912 Point new_vertex(transform.unperform(bline_point.get_vertex()));
914 bline_point.set_tangent1(
916 bline_point.get_tangent1()+bline_point.get_vertex()
920 bline_point.set_tangent2(
922 bline_point.get_tangent2()+bline_point.get_vertex()
927 new_vertex=new_vertex-shift_origin_vector;
929 bline_point.set_vertex(new_vertex);
931 trans_bline.push_back(bline_point);
933 value_node=ValueNode_BLine::create(synfig::ValueBase(trans_bline,loop_bline_flag));
935 Canvas::Handle canvas(get_canvas_view()->get_canvas());
936 Layer::Handle layer(get_canvas_view()->get_selection_manager()->get_selected_layer());
937 if (layer) canvas=layer->get_canvas();
938 value_node->set_member_canvas(canvas);
941 Smach::event_result result;
942 synfig::ValueNode_DynamicList::ListEntry source;
944 // the new line's start extends an existing line
947 int target_offset = 0;
948 if(complete_loop)trans_bline.pop_back();
949 trans_bline.pop_front();
950 if(start_duck_index==0)
951 { // We need to reverse the BLine first.
952 reverse_bline(trans_bline);
953 result=extend_bline_from_begin(start_duck_value_node_bline,trans_bline,complete_loop);
954 source=start_duck_value_node_bline->list.front();
955 target_offset=trans_bline.size();
959 result=extend_bline_from_end(start_duck_value_node_bline,trans_bline,complete_loop);
960 source=start_duck_value_node_bline->list.back();
963 if(extend_start_join_different)
964 LinkableValueNode::Handle::cast_dynamic(source.value_node)->
965 set_link("point",finish_duck_value_desc.get_value_node());
966 else if(extend_start_join_same)
967 LinkableValueNode::Handle::cast_dynamic(source.value_node)->
968 set_link("point",synfigapp::ValueDesc(LinkableValueNode::Handle::cast_dynamic(start_duck_value_node_bline->
969 list[target_offset+finish_duck_index].value_node),0).get_value_node());
973 // the new line's end extends an existing line
976 int target_offset = 0;
977 trans_bline.pop_back();
978 if(finish_duck_index==0)
980 result=extend_bline_from_begin(finish_duck_value_node_bline,trans_bline,false);
981 source=finish_duck_value_node_bline->list.front();
982 target_offset=trans_bline.size();
985 { // We need to reverse the BLine first.
986 reverse_bline(trans_bline);
987 result=extend_bline_from_end(finish_duck_value_node_bline,trans_bline,false);
988 source=finish_duck_value_node_bline->list.back();
991 if(extend_finish_join_different)
992 LinkableValueNode::Handle::cast_dynamic(source.value_node)->
993 set_link("point",start_duck_value_desc.get_value_node());
994 else if(extend_finish_join_same)
995 LinkableValueNode::Handle::cast_dynamic(source.value_node)->
996 set_link("point",synfigapp::ValueDesc(LinkableValueNode::Handle::cast_dynamic(finish_duck_value_node_bline->
997 list[target_offset+start_duck_index].value_node),0).get_value_node());
1001 if (join_start_no_extend)
1002 LinkableValueNode::Handle::cast_dynamic(value_node->list.front().value_node)->
1003 set_link("point",start_duck_value_desc.get_value_node());
1005 if (join_finish_no_extend)
1006 LinkableValueNode::Handle::cast_dynamic(value_node->list.back().value_node)->
1007 set_link("point",finish_duck_value_desc.get_value_node());
1009 if(get_auto_export_flag())
1010 if (!get_canvas_interface()->add_value_node(value_node,get_id()))
1012 /* it's no big deal, is it? let's keep the shape anyway */
1013 // get_canvas_view()->get_ui_interface()->error(_("Unable to add value node"));
1016 // return Smach::RESULT_ERROR;
1019 last_stroke=value_node;
1020 last_stroke_id=get_id();
1023 // Create the layer(s)
1024 Layer::Handle layer;
1025 Canvas::Handle canvas(get_canvas_view()->get_canvas());
1028 // we are temporarily using the layer to hold something
1029 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
1032 depth=layer->get_depth();
1033 canvas=layer->get_canvas();
1036 // fill_last_stroke() will take care of clearing the selection if we're calling it
1037 if(get_outline_flag() && get_region_flag())
1039 if (fill_last_stroke_and_unselect_other_layers() == Smach::RESULT_ERROR)
1041 get_canvas_view()->get_selection_manager()->set_selected_layers(layer_list);
1042 get_canvas_view()->get_ui_interface()->error(_("Unable to create layer"));
1044 return Smach::RESULT_ERROR;
1048 get_canvas_interface()->get_selection_manager()->clear_selected_layers();
1050 //int number(synfig::UniqueID().get_uid());
1052 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
1054 // if they're both defined, we'll add the region later
1055 if(get_outline_flag())
1057 layer=get_canvas_interface()->add_layer_to("outline",canvas,depth);
1060 get_canvas_view()->get_selection_manager()->set_selected_layers(layer_list);
1061 get_canvas_view()->get_ui_interface()->error(_("Unable to create layer"));
1063 return Smach::RESULT_ERROR;
1065 layer->set_description(get_id()+_(" Outline"));
1069 layer=get_canvas_interface()->add_layer_to("region",canvas,depth);
1072 get_canvas_view()->get_selection_manager()->set_selected_layers(layer_list);
1073 get_canvas_view()->get_ui_interface()->error(_("Unable to create layer"));
1075 return Smach::RESULT_ERROR;
1077 layer->set_description(get_id()+_(" Region"));
1082 layer->set_param("feather",get_feather());
1083 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
1086 //layer->set_description(strprintf("Stroke %d",number));
1087 //get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
1090 get_canvas_interface()->
1091 change_value(synfigapp::ValueDesc(layer,"origin"),shift_origin_vector);
1093 synfigapp::Action::Handle action(synfigapp::Action::create("LayerParamConnect"));
1097 action->set_param("canvas",get_canvas());
1098 action->set_param("canvas_interface",get_canvas_interface());
1099 action->set_param("layer",layer);
1100 if(!action->set_param("param",String("bline")))
1101 synfig::error("LayerParamConnect didn't like \"param\"");
1102 if(!action->set_param("value_node",ValueNode::Handle(value_node)))
1103 synfig::error("LayerParamConnect didn't like \"value_node\"");
1105 if(!get_canvas_interface()->get_instance()->perform_action(action))
1107 get_canvas_view()->get_ui_interface()->error(_("Unable to create layer"));
1111 return Smach::RESULT_ERROR;
1113 layer_list.push_back(layer);
1114 get_canvas_view()->get_selection_manager()->set_selected_layers(layer_list);
1119 return Smach::RESULT_ACCEPT;
1124 debug_show_vertex_list(int iteration, std::list<synfigapp::ValueDesc>& vertex_list,
1125 std::string title, int current)
1127 std::list<synfigapp::ValueDesc>::iterator i = vertex_list.begin();
1128 printf("\n%s\n ----- iter %d : ", title.c_str(), iteration);
1130 synfig::LinkableValueNode::Handle last = 0;
1135 bool started = false;
1136 for(;i!=vertex_list.end();i++,c++)
1138 synfigapp::ValueDesc value_desc(*i);
1140 if (value_desc.parent_is_value_node()) {
1141 if(value_desc.parent_is_linkable_value_node())
1143 index = value_desc.get_index();
1144 // printf("<%d>", index);
1145 if (last == synfig::LinkableValueNode::Handle::cast_reinterpret(value_desc.get_parent_value_node()))
1147 // printf("\n%s:%d\n", __FILE__, __LINE__);
1150 // printf("\n%s:%d\n", __FILE__, __LINE__);
1153 // printf("\n%s:%d\n", __FILE__, __LINE__);
1156 if (started) printf(", "); else started = true;
1157 printf("%d--%d", start, prev);
1161 if (started) printf(", "); else started = true;
1162 printf("%d", start);
1164 printf(", *%d*", index);
1169 // printf("\n%s:%d\n", __FILE__, __LINE__);
1170 if (index == start + 1)
1172 // printf("\n%s:%d\n", __FILE__, __LINE__);
1176 else if (index == start - 1)
1178 // printf("\n%s:%d\n", __FILE__, __LINE__);
1184 if (started) printf(", "); else started = true;
1185 printf("%d", start);
1189 else if (index == prev + dir)
1191 // printf("\n%s:%d\n", __FILE__, __LINE__);
1196 // printf("\n%s:%d\n", __FILE__, __LINE__);
1197 if (started) printf(", "); else started = true;
1199 printf("%d--%d", start, prev);
1201 printf("%d", start);
1202 // printf("\n%s:%d\n", __FILE__, __LINE__);
1209 // printf("\n%s:%d\n", __FILE__, __LINE__);
1212 if (started) printf(", "); else started = true;
1213 printf("*%d*", index);
1217 // printf("\n%s:%d\n", __FILE__, __LINE__);
1225 // printf("\n%s:%d\n", __FILE__, __LINE__);
1228 // printf("\n%s:%d\n", __FILE__, __LINE__);
1231 if (started) printf(", "); else started = true;
1233 printf("%d--%d", start, prev);
1235 printf("%d", start);
1237 // printf("\n%s:%d\n", __FILE__, __LINE__);
1240 // printf("\n%s:%d\n", __FILE__, __LINE__);
1241 last = synfig::LinkableValueNode::Handle::cast_reinterpret(value_desc.get_parent_value_node());
1242 printf("%d:(", synfig::LinkableValueNode::Handle::cast_reinterpret(value_desc.get_parent_value_node())->link_count());
1244 // printf("\n%s:%d\n", __FILE__, __LINE__);
1248 printf("*%d*", index);
1252 // printf("\n%s:%d\n", __FILE__, __LINE__);
1256 // printf("\n%s:%d\n", __FILE__, __LINE__);
1258 // printf("\n%s:%d\n", __FILE__, __LINE__);
1261 if (last) printf("?!) ");
1271 if (started) printf(", "); else started = true;
1275 printf("%d--%d", start, prev);
1277 printf("%d", start);
1284 #define debug_show_vertex_list(a,b,c,d)
1288 StateDraw_Context::new_region(std::list<synfig::BLinePoint> bline, synfig::Real radius)
1290 // Create the action group
1291 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Define Region"));
1293 std::list<synfigapp::ValueDesc> vertex_list;
1295 printf("new_region with %zd bline points\n", bline.size());
1297 // First we need to come up with a rough list of
1298 // BLinePoints that we are going to be using to
1299 // define our region.
1301 std::list<synfig::BLinePoint>::iterator iter;
1302 for(iter=bline.begin();iter!=bline.end();++iter)
1304 etl::handle<Duck> duck(get_work_area()->find_duck(iter->get_vertex(),0,Duck::TYPE_VERTEX));
1308 synfig::info(__FILE__":%d: Nothing to enclose!",__LINE__);
1309 return Smach::RESULT_OK;
1312 assert(duck->get_type()==Duck::TYPE_VERTEX);
1314 synfigapp::ValueDesc value_desc(duck->get_value_desc());
1318 synfig::info(__FILE__":%d: Got a hit, but no ValueDesc on this duck",__LINE__);
1322 switch(value_desc.get_value_type())
1324 case synfig::ValueBase::TYPE_BLINEPOINT:
1325 //if(vertex_list.empty() || value_desc!=vertex_list.back())
1326 vertex_list.push_back(value_desc);
1327 assert(vertex_list.back().is_valid());
1336 assert(vertex_list.back().is_valid());
1338 printf("vertex list with %zd bline points\n", vertex_list.size());
1340 // Remove any duplicates
1344 ValueNode_BLine::Handle value_node_bline;
1346 // Now we need to test for the trivial case,
1347 // which is where all of the vertices
1348 // come from one BLine.
1349 if(vertex_list.front().parent_is_linkable_value_node())
1351 bool trivial_case(true);
1352 ValueNode::Handle trivial_case_value_node;
1354 trivial_case_value_node=vertex_list.front().get_parent_value_node();
1356 std::list<synfigapp::ValueDesc>::iterator iter;
1357 for(iter=vertex_list.begin();iter!=vertex_list.end();++iter)
1359 if(trivial_case_value_node!=iter->get_parent_value_node())
1366 // \todo - re-enable this code
1367 if(trivial_case && false)
1369 synfig::info("all points are on the same bline, so just fill that line");
1370 value_node_bline=ValueNode_BLine::Handle::cast_dynamic(trivial_case_value_node);
1372 synfig::info("the line has %d vertices", value_node_bline->link_count());
1374 if(value_node_bline->link_count() <= 2)
1376 synfig::info(__FILE__":%d: Vertex list too small to make region.",__LINE__);
1377 return Smach::RESULT_OK;
1382 if(!value_node_bline)
1383 if(vertex_list.size()<=2)
1385 synfig::info(__FILE__":%d: Vertex list too small to make region.",__LINE__);
1386 return Smach::RESULT_OK;
1389 // Now we need to clean the list of vertices up
1390 // a bit. This includes inserting missing vertices
1391 // and removing extraneous ones.
1392 // We can do this in multiple passes.
1393 if(!value_node_bline)
1395 debug_show_vertex_list(0, vertex_list, "before shifting stuff", -1);
1396 // rearrange the list so that the first and last node are on different blines
1397 std::list<synfigapp::ValueDesc>::iterator iter, start;
1398 ValueNode::Handle last_value_node = vertex_list.back().get_parent_value_node();
1399 for(iter = vertex_list.begin(); iter!=vertex_list.end(); iter++)
1400 if (iter->get_parent_value_node() != last_value_node)
1402 vertex_list.insert(vertex_list.end(), vertex_list.begin(), iter);
1403 vertex_list.erase(vertex_list.begin(), iter);
1407 debug_show_vertex_list(0, vertex_list, "before detecting direction and limits", -1);
1408 // rearrange the list so that the first and last node are on different blines
1409 iter = vertex_list.begin();
1410 while (iter!=vertex_list.end())
1412 // make a note of which bline we're looking at
1413 ValueNode::Handle parent_value_node = iter->get_parent_value_node();
1415 int points_in_line = synfig::LinkableValueNode::Handle::cast_reinterpret(parent_value_node)->link_count();
1416 bool looped = (*parent_value_node)(get_time()).get_loop();
1417 int this_index, last_index = iter->get_index();
1418 int min_index = last_index, max_index = last_index;
1422 // printf("there are %d points in this line - first is index %d\n", points_in_line, last_index);
1424 // while we're looking at the same bline, keep going
1426 while (iter != vertex_list.end() && iter->get_parent_value_node() == parent_value_node)
1428 this_index = iter->get_index();
1429 // printf("index went from %d to %d\n", last_index, this_index);
1432 if (this_index - last_index > points_in_line/2)
1433 while (this_index - last_index > points_in_line/2)
1434 this_index -= points_in_line;
1435 else if (last_index - this_index > points_in_line/2)
1436 while (last_index - this_index > points_in_line/2)
1437 this_index += points_in_line;
1440 if (this_index < min_index) min_index = this_index;
1441 if (this_index > max_index) max_index = this_index;
1443 // printf("so let's imagine index went from %d to %d\n", last_index, this_index);
1444 if (this_index > last_index)
1446 else if (this_index < last_index)
1449 last_index = this_index;
1453 // printf("min %d and max %d\n", min_index, max_index);
1454 whole = max_index - min_index >= points_in_line;
1455 min_index = (min_index % points_in_line + points_in_line) % points_in_line;
1456 max_index = (max_index % points_in_line + points_in_line) % points_in_line;
1457 // they drew around a shape more than once - what's the start/end point? does it matter?
1458 if (whole) min_index = max_index = (min_index + max_index) / 2;
1459 // printf("processed min %d max %d whole %d\n", min_index, max_index, whole);
1465 // printf("whole (down) (%d) ", min_index);
1466 for (int i = min_index; i >= 0; i--)
1468 // printf("%d ", i);
1469 vertex_list.insert(start, synfigapp::ValueDesc(parent_value_node, i));
1471 for (int i = points_in_line - 1; i >= min_index; i--)
1473 // printf("%d ", i);
1474 vertex_list.insert(start, synfigapp::ValueDesc(parent_value_node, i));
1479 // printf("part (down) (%d -> %d) ", max_index, min_index);
1480 for (int i = max_index; i != min_index; i--)
1482 if (i == -1) i = points_in_line - 1;
1483 // printf("%d ", i);
1484 vertex_list.insert(start, synfigapp::ValueDesc(parent_value_node, i));
1486 vertex_list.insert(start, synfigapp::ValueDesc(parent_value_node, min_index));
1493 // printf("whole (%d) ", min_index);
1494 for (int i = min_index; i < points_in_line; i++)
1496 // printf("%d ", i);
1497 vertex_list.insert(start, synfigapp::ValueDesc(parent_value_node, i));
1499 for (int i = 0; i <= min_index; i++)
1501 // printf("%d ", i);
1502 vertex_list.insert(start, synfigapp::ValueDesc(parent_value_node, i));
1507 // printf("part (%d -> %d) ", min_index, max_index);
1508 for (int i = min_index; i != max_index; i++)
1510 if (i == points_in_line) i = 0;
1511 // printf("%d ", i);
1512 vertex_list.insert(start, synfigapp::ValueDesc(parent_value_node, i));
1514 vertex_list.insert(start, synfigapp::ValueDesc(parent_value_node, max_index));
1518 // debug_show_vertex_list(0, vertex_list, "after insert", -1);
1519 vertex_list.erase(start, iter);
1520 // debug_show_vertex_list(0, vertex_list, "after delete", -1);
1523 debug_show_vertex_list(0, vertex_list, "continuous vertices", -1);
1525 // \todo reenable or delete this section
1527 for(bool done=false;!done && i<30;i++)
1529 debug_show_vertex_list(i, vertex_list, "in big loop", -1);
1531 // Set done to "true" for now. If
1532 // any updates are performed, we will
1533 // change it back to false.
1536 std::list<synfigapp::ValueDesc>::iterator prev,next;
1537 prev=vertex_list.end();prev--; // Set prev to the last ValueDesc
1538 next=vertex_list.begin();
1539 iter=next++; // Set iter to the first value desc, and next to the second
1542 for(;iter!=vertex_list.end();prev=iter,iter++,next++,current++)
1544 // we need to be able to erase(next) and can't do that if next is end()
1545 if (next == vertex_list.end()) next = vertex_list.begin();
1546 debug_show_vertex_list(i, vertex_list, "in loop around vertices", current);
1547 synfigapp::ValueDesc value_prev(*prev);
1548 synfigapp::ValueDesc value_desc(*iter);
1549 synfigapp::ValueDesc value_next(*next);
1551 assert(value_desc.is_valid());
1552 assert(value_next.is_valid());
1553 assert(value_prev.is_valid());
1555 // synfig::info("-------");
1556 // synfig::info(__FILE__":%d: value_prev 0x%08X:%d",__LINE__,value_prev.get_parent_value_node().get(),value_prev.get_index());
1557 // synfig::info(__FILE__":%d: value_desc 0x%08X:%d",__LINE__,value_desc.get_parent_value_node().get(),value_desc.get_index());
1558 // synfig::info(__FILE__":%d: value_next 0x%08X:%d",__LINE__,value_next.get_parent_value_node().get(),value_next.get_index());
1561 if(value_prev.parent_is_value_node() && value_desc.parent_is_value_node() && value_next.parent_is_value_node())
1563 // Remove random extraneous vertices
1564 if(value_prev.get_parent_value_node()==value_next.get_parent_value_node() &&
1565 value_prev.get_parent_value_node()!=value_desc.get_parent_value_node())
1567 vertex_list.erase(iter);
1574 // // Remove duplicate vertices
1576 // // if previous is the same as current or
1577 // // current is the same as next, remove current
1578 // if(value_prev.get_value_node()==value_desc.get_value_node() ||
1579 // value_desc.get_value_node()==value_next.get_value_node())
1581 // vertex_list.erase(iter);
1583 // printf("erased node - i = %d\n", i);
1587 // // if previous is the same as next, remove previous? or next?
1588 // if(value_prev.get_value_node()==value_next.get_value_node())
1590 // vertex_list.erase(next);
1591 // // vertex_list.erase(prev);
1593 // printf("erased node - i = %d\n", i);
1597 // if 'this' and 'next' both have parents
1598 if (value_desc.parent_is_value_node() && value_next.parent_is_value_node())
1600 // if they are both on the same bline - this has been handled by new code above
1601 if (value_desc.get_parent_value_node() == value_next.get_parent_value_node())
1603 // // if (next != vertex_list.end())
1605 // printf("parent loop is %d and node loop is ??\n",
1606 // (*(value_desc.get_parent_value_node()))(get_time()).get_loop()
1607 // // value_desc.get_value_node().get_loop(),
1610 // // Fill in missing vertices
1611 // // \todo take loops into account: seeing (15, 2, 3, 4) probably means that (0, 1) is missing, not 14 through 3
1612 // if(value_desc.get_index()<value_next.get_index()-1)
1614 // debug_show_vertex_list(i, vertex_list,
1615 // strprintf("same parent, different points this %d < next-1 %d",
1616 // value_desc.get_index(), ((value_next.get_index()-1))),
1618 // for (int index = value_desc.get_index()+1; index < value_next.get_index(); index++)
1620 // printf("inserting up %d\n", index);
1621 // vertex_list.insert(next, synfigapp::ValueDesc(value_desc.get_parent_value_node(), index));
1623 // debug_show_vertex_list(i, vertex_list, "new list", current);
1627 // if(value_next.get_index()<value_desc.get_index()-1)
1629 // debug_show_vertex_list(i, vertex_list,
1630 // strprintf("same parent, different points next %d < this-1 %d",
1631 // value_next.get_index(), ((value_desc.get_index()-1))),
1633 // for (int index = value_desc.get_index()-1; index > value_next.get_index(); index--)
1635 // printf("inserting down %d\n", index);
1636 // vertex_list.insert(next, synfigapp::ValueDesc(value_desc.get_parent_value_node(), index));
1638 // debug_show_vertex_list(i, vertex_list, "new list", current);
1644 // 'this' and 'next' have different parents
1647 ValueNode::Handle v1 = value_desc.get_value_node();
1648 ValueNode::Handle v2 = value_desc.get_parent_value_node();
1652 printf("different\n");
1654 if (value_desc.get_value_node() != value_next.get_value_node())
1656 // Ensure that connections between blines are properly connected
1657 BLinePoint vertex(value_desc.get_value(get_time()).get(BLinePoint()));
1658 BLinePoint vertex_next(value_next.get_value(get_time()).get(BLinePoint()));
1660 //synfig::info("--------");
1661 //synfig::info(__FILE__":%d: vertex: [%f, %f]",__LINE__,vertex.get_vertex()[0],vertex.get_vertex()[1]);
1662 //synfig::info(__FILE__":%d: vertex_next: [%f, %f]",__LINE__,vertex_next.get_vertex()[0],vertex_next.get_vertex()[1]);
1664 // if this vertex is close to the next one, replace this vertex with a new one
1665 // and erase the next one
1666 printf("this point is %5.2f from the next point - compare with %5.2f\n",
1667 (vertex.get_vertex()-vertex_next.get_vertex()).mag_squared(),
1669 if((vertex.get_vertex()-vertex_next.get_vertex()).mag_squared()<radius*radius)
1671 printf("in one - it's close\n");
1672 ValueNode_Composite::Handle value_node;
1673 ValueNode_Composite::Handle value_node_next;
1674 value_node=ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node().clone());
1675 value_node_next=ValueNode_Composite::Handle::cast_dynamic(value_next.get_value_node().clone());
1676 if(!value_node || !value_node_next)
1678 synfig::info(__FILE__":%d: Unable to properly connect blines.",__LINE__);
1681 // \todo if next isn't split, don't we want to copy its 'Tangent 1' instead?
1682 value_node->set_link("t2",value_node_next->get_link("t2"));
1683 value_node->set_link("split",ValueNode_Const::create(true));
1685 // get_canvas_interface()->auto_export(value_node);
1686 printf("exporting\n");
1687 get_canvas_interface()->add_value_node(value_node,value_node->get_id() + strprintf("foo %d", rand()));
1689 assert(value_node->is_exported());
1690 // replace 'this' with the new valuenode
1691 *iter=synfigapp::ValueDesc(get_canvas(),value_node->get_id());
1692 printf("erasing next\n");
1693 printf("erasing next point\n");
1694 vertex_list.erase(next);
1697 } // this vertex isn't close to the next one
1698 else if (value_prev.parent_is_value_node())
1700 printf("in two - it's far\n");
1701 // \todo this only makes sense if prev is on the same bline
1702 printf("this is index %d\n", value_desc.get_index());
1703 printf("prev is index %d\n", value_prev.get_index());
1704 bool positive_trend(value_desc.get_index()>value_prev.get_index());
1708 printf("positive trend\n");
1709 printf("comparing index %d < link_count()-1 = %d-1 = %d\n",
1710 value_desc.get_index(),
1711 LinkableValueNode::Handle::cast_static(value_desc.get_parent_value_node())->link_count(),
1712 LinkableValueNode::Handle::cast_static(value_desc.get_parent_value_node())->link_count()-1);
1713 if (value_desc.get_index()<LinkableValueNode::Handle::cast_static(value_desc.get_parent_value_node())->link_count()-1)
1715 printf("in two - b\n");
1716 printf("inserting node with index %d\n", value_desc.get_index()+1);
1717 vertex_list.insert(next,
1718 synfigapp::ValueDesc(value_desc.get_parent_value_node(),
1719 value_desc.get_index()+1));
1724 else // !positive_trend
1726 printf("negative trend\n");
1727 if(value_desc.get_index()>0)
1729 printf("in two - a\n");
1730 printf("inserting node on this line with index %d\n",
1731 value_desc.get_index()-1);
1732 vertex_list.insert(next,
1733 synfigapp::ValueDesc(value_desc.get_parent_value_node(),
1734 value_desc.get_index()-1));
1746 if(vertex_list.size()<=2)
1748 synfig::info(__FILE__":%d: Vertex list too small to make region.",__LINE__);
1749 return Smach::RESULT_OK;
1752 debug_show_vertex_list(i, vertex_list, "finished tidying list", -1);
1755 // If we aren't the trivial case,
1756 // then go ahead and create the new
1758 if(!value_node_bline)
1760 synfig::info("not all points are on the same bline");
1761 value_node_bline=ValueNode_BLine::create();
1763 std::list<synfigapp::ValueDesc>::iterator iter;
1764 for(iter=vertex_list.begin();iter!=vertex_list.end();++iter)
1766 // Ensure that the vertex is exported.
1767 get_canvas_interface()->auto_export(*iter);
1769 value_node_bline->add(iter->get_value_node());
1770 //value_node_bline->add(ValueNode_BLine::ListEntry(iter->get_value_node()));
1773 value_node_bline->set_loop(true);
1776 get_canvas_interface()->auto_export(value_node_bline);
1778 // Now we create the region layer
1781 Layer::Handle layer;
1782 Canvas::Handle canvas(get_canvas_view()->get_canvas());
1785 // we are temporarily using the layer to hold something
1786 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
1789 depth=layer->get_depth();
1790 canvas=layer->get_canvas();
1793 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
1795 layer=get_canvas_interface()->add_layer_to("region",canvas,depth);
1798 get_canvas_view()->get_ui_interface()->error(_("Unable to create layer"));
1800 return Smach::RESULT_ERROR;
1805 layer->set_param("feather",get_feather());
1806 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
1808 get_canvas_interface()->signal_layer_param_changed()(layer,"color");
1810 synfigapp::Action::Handle action(synfigapp::Action::create("LayerParamConnect"));
1814 action->set_param("canvas",get_canvas());
1815 action->set_param("canvas_interface",get_canvas_interface());
1816 action->set_param("layer",layer);
1817 if(!action->set_param("param",String("bline")))
1818 synfig::error("LayerParamConnect didn't like \"param\"");
1819 if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
1820 synfig::error("LayerParamConnect didn't like \"value_node\"");
1822 if(!get_canvas_interface()->get_instance()->perform_action(action))
1824 get_canvas_view()->get_ui_interface()->error(_("Unable to create Region layer"));
1826 return Smach::RESULT_ERROR;
1828 get_canvas_view()->get_selection_manager()->set_selected_layer(layer);
1831 return Smach::RESULT_ACCEPT;
1835 StateDraw_Context::refresh_ducks()
1837 get_canvas_view()->queue_rebuild_ducks();
1839 get_work_area()->clear_ducks();
1842 std::list< etl::smart_ptr<std::list<synfig::Point> > >::iterator iter;
1844 for(iter=stroke_list.begin();iter!=stroke_list.end();++iter)
1846 get_work_area()->add_stroke(*iter);
1849 get_work_area()->queue_draw();
1855 StateDraw_Context::extend_bline_from_begin(ValueNode_BLine::Handle value_node,std::list<synfig::BLinePoint> bline,bool complete_loop)
1857 // Create the action group
1858 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Extend BLine"));
1862 synfigapp::Action::Handle action(synfigapp::Action::create("ValueNodeDynamicListLoop"));
1865 action->set_param("canvas",get_canvas());
1866 action->set_param("canvas_interface",get_canvas_interface());
1867 action->set_param("value_node",ValueNode::Handle(value_node));
1869 if(!get_canvas_interface()->get_instance()->perform_action(action))
1871 get_canvas_view()->get_ui_interface()->error(_("Unable to set loop for bline"));
1873 return Smach::RESULT_ERROR;
1877 std::list<synfig::BLinePoint>::reverse_iterator iter;
1878 for(iter=bline.rbegin();!(iter==bline.rend());++iter)
1880 ValueNode_Composite::Handle composite(ValueNode_Composite::create(*iter));
1882 synfigapp::Action::Handle action(synfigapp::Action::create("ValueNodeDynamicListInsert"));
1885 synfigapp::ValueDesc value_desc(value_node,0);
1887 action->set_param("canvas",get_canvas());
1888 action->set_param("canvas_interface",get_canvas_interface());
1889 action->set_param("value_desc",value_desc);
1890 if(!action->set_param("item",ValueNode::Handle(composite)))
1891 synfig::error("ACTION didn't like \"item\"");
1893 if(!get_canvas_interface()->get_instance()->perform_action(action))
1895 get_canvas_view()->get_ui_interface()->error(_("Unable to insert item"));
1898 return Smach::RESULT_ERROR;
1901 last_stroke=value_node;
1902 return Smach::RESULT_ACCEPT;
1906 StateDraw_Context::extend_bline_from_end(ValueNode_BLine::Handle value_node,std::list<synfig::BLinePoint> bline,bool complete_loop)
1908 // Create the action group
1909 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Extend BLine"));
1913 synfigapp::Action::Handle action(synfigapp::Action::create("ValueNodeDynamicListLoop"));
1916 action->set_param("canvas",get_canvas());
1917 action->set_param("canvas_interface",get_canvas_interface());
1918 action->set_param("value_node",ValueNode::Handle(value_node));
1920 if(!get_canvas_interface()->get_instance()->perform_action(action))
1922 get_canvas_view()->get_ui_interface()->error(_("Unable to set loop for bline"));
1924 return Smach::RESULT_ERROR;
1928 std::list<synfig::BLinePoint>::iterator iter;
1929 for(iter=bline.begin();iter!=bline.end();++iter)
1931 ValueNode_Composite::Handle composite(ValueNode_Composite::create(*iter));
1933 synfigapp::Action::Handle action(synfigapp::Action::create("ValueNodeDynamicListInsert"));
1936 synfigapp::ValueDesc value_desc(value_node,value_node->link_count());
1938 action->set_param("canvas",get_canvas());
1939 action->set_param("canvas_interface",get_canvas_interface());
1940 action->set_param("value_desc",value_desc);
1941 if(!action->set_param("item",ValueNode::Handle(composite)))
1942 synfig::error("ACTION didn't like \"item\"");
1944 if(!get_canvas_interface()->get_instance()->perform_action(action))
1946 get_canvas_view()->get_ui_interface()->error(_("Unable to insert item"));
1949 return Smach::RESULT_ERROR;
1952 last_stroke=value_node;
1953 return Smach::RESULT_ACCEPT;
1957 StateDraw_Context::reverse_bline(std::list<synfig::BLinePoint> &bline)
1961 std::list<synfig::BLinePoint>::iterator iter,eiter;
1965 for(i=0;i<(int)bline.size()/2;++iter,--eiter,i++)
1967 iter_swap(iter,eiter);
1974 StateDraw_Context::fill_last_stroke_and_unselect_other_layers()
1977 return Smach::RESULT_OK;
1979 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Fill Stroke"));
1981 Layer::Handle layer;
1983 get_canvas_interface()->auto_export(last_stroke);
1985 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
1987 Canvas::Handle canvas(get_canvas_view()->get_canvas());
1990 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
1993 depth=layer->get_depth();
1994 canvas=layer->get_canvas();
1997 get_canvas_interface()->get_selection_manager()->clear_selected_layers();
1998 layer=get_canvas_interface()->add_layer_to("region", canvas, depth);
1999 if (!layer) return Smach::RESULT_ERROR;
2000 layer->set_description(last_stroke_id + _(" Region"));
2002 synfigapp::Action::Handle action(synfigapp::Action::create("LayerParamConnect"));
2006 action->set_param("canvas",get_canvas());
2007 action->set_param("canvas_interface",get_canvas_interface());
2008 action->set_param("layer",layer);
2009 if(!action->set_param("param",String("bline")))
2010 synfig::error("LayerParamConnect didn't like \"param\"");
2011 if(!action->set_param("value_node",ValueNode::Handle(last_stroke)))
2012 synfig::error("LayerParamConnect didn't like \"value_node\"");
2014 if(!get_canvas_interface()->get_instance()->perform_action(action))
2016 get_canvas_view()->get_ui_interface()->error(_("Unable to create Region layer"));
2018 return Smach::RESULT_OK;
2020 get_canvas_view()->get_selection_manager()->set_selected_layer(layer);
2021 return Smach::RESULT_OK;
2025 StateDraw_Context::fill_last_stroke()
2030 synfigapp::SelectionManager::LayerList layer_list = get_canvas_view()->get_selection_manager()->get_selected_layers();
2031 fill_last_stroke_and_unselect_other_layers();
2032 get_canvas_view()->get_selection_manager()->set_selected_layers(layer_list);