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>
69 /* === U S I N G =========================================================== */
73 using namespace synfig;
74 using namespace studio;
76 /* === M A C R O S ========================================================= */
78 /* === G L O B A L S ======================================================= */
80 StateDraw studio::state_draw;
82 /* === C L A S S E S & S T R U C T S ======================================= */
84 class studio::StateDraw_Context : public sigc::trackable
86 typedef etl::smart_ptr<std::list<synfig::Point> > StrokeData;
87 typedef etl::smart_ptr<std::list<synfig::Real> > WidthData;
89 typedef list< pair<StrokeData,WidthData> > StrokeQueue;
91 StrokeQueue stroke_queue;
94 etl::handle<CanvasView> canvas_view_;
95 CanvasView::IsWorking is_working;
97 bool prev_table_status;
99 bool prev_workarea_layer_status_;
102 sigc::connection process_queue_connection;
104 ValueNode_BLine::Handle last_stroke;
105 synfig::String last_stroke_id;
109 //Duckmatic::Push duckmatic_push;
111 std::list< etl::smart_ptr<std::list<synfig::Point> > > stroke_list;
113 void refresh_ducks();
115 Duckmatic::Type old_duckmask;
117 void fill_last_stroke();
118 void fill_last_stroke_and_unselect_other_layers();
120 Smach::event_result new_bline(std::list<synfig::BLinePoint> bline,bool loop_bline_flag,float radius);
122 Smach::event_result new_region(std::list<synfig::BLinePoint> bline,synfig::Real radius);
124 Smach::event_result extend_bline_from_begin(ValueNode_BLine::Handle value_node,std::list<synfig::BLinePoint> bline,bool complete_loop);
125 Smach::event_result extend_bline_from_end(ValueNode_BLine::Handle value_node,std::list<synfig::BLinePoint> bline,bool complete_loop);
126 void reverse_bline(std::list<synfig::BLinePoint> &bline);
128 synfigapp::Settings& settings;
130 Gtk::Table options_table;
132 Gtk::CheckButton checkbutton_pressure_width;
133 Gtk::CheckButton checkbutton_round_ends;
134 Gtk::CheckButton checkbutton_auto_loop; // whether to loop new strokes which start and end in the same place
135 Gtk::CheckButton checkbutton_auto_extend; // whether to extend existing lines
136 Gtk::CheckButton checkbutton_auto_link; // whether to link new ducks to existing ducks
137 Gtk::CheckButton checkbutton_region; // whether to create regions
138 Gtk::CheckButton checkbutton_outline; // whether to create outlines
139 Gtk::CheckButton checkbutton_auto_export;
140 Gtk::Button button_fill_last_stroke;
142 //pressure spinner and such
143 Gtk::Adjustment adj_min_pressure;
144 Gtk::SpinButton spin_min_pressure;
145 Gtk::CheckButton check_min_pressure;
147 Gtk::Adjustment adj_feather;
148 Gtk::SpinButton spin_feather;
150 Gtk::Adjustment adj_globalthres;
151 Gtk::SpinButton spin_globalthres;
153 Gtk::Adjustment adj_localthres;
154 Gtk::CheckButton check_localerror;
155 void UpdateErrorBox(); //switches the stuff if need be :)
157 //Added by Adrian - data drive HOOOOO
158 synfigapp::BLineConverter blineconv;
161 synfig::String get_id()const { return entry_id.get_text(); }
162 void set_id(const synfig::String& x) { return entry_id.set_text(x); }
164 bool get_pressure_width_flag()const { return checkbutton_pressure_width.get_active(); }
165 void set_pressure_width_flag(bool x) { return checkbutton_pressure_width.set_active(x); }
167 bool get_auto_loop_flag()const { return checkbutton_auto_loop.get_active(); }
168 void set_auto_loop_flag(bool x) { return checkbutton_auto_loop.set_active(x); }
170 bool get_auto_extend_flag()const { return checkbutton_auto_extend.get_active(); }
171 void set_auto_extend_flag(bool x) { return checkbutton_auto_extend.set_active(x); }
173 bool get_auto_link_flag()const { return checkbutton_auto_link.get_active(); }
174 void set_auto_link_flag(bool x) { return checkbutton_auto_link.set_active(x); }
176 bool get_region_flag()const { return checkbutton_region.get_active(); }
177 void set_region_flag(bool x) { return checkbutton_region.set_active(x); }
179 bool get_outline_flag()const { return checkbutton_outline.get_active(); }
180 void set_outline_flag(bool x) { return checkbutton_outline.set_active(x); }
182 bool get_auto_export_flag()const { return checkbutton_auto_export.get_active(); }
183 void set_auto_export_flag(bool x) { return checkbutton_auto_export.set_active(x); }
185 Real get_min_pressure() const { return adj_min_pressure.get_value(); }
186 void set_min_pressure(Real x) { return adj_min_pressure.set_value(x); }
188 Real get_feather() const { return adj_feather.get_value(); }
189 void set_feather(Real x) { return adj_feather.set_value(x); }
191 Real get_gthres() const { return adj_globalthres.get_value(); }
192 void set_gthres(Real x) { return adj_globalthres.set_value(x); }
194 Real get_lthres() const { return adj_localthres.get_value(); }
195 void set_lthres(Real x) { return adj_localthres.set_value(x); }
197 bool get_local_error_flag() const { return check_localerror.get_active(); }
198 void set_local_error_flag(bool x) { check_localerror.set_active(x); }
200 bool get_min_pressure_flag()const { return check_min_pressure.get_active(); }
201 void set_min_pressure_flag(bool x) { check_min_pressure.set_active(x); }
203 void load_settings();
204 void save_settings();
207 Smach::event_result event_stop_handler(const Smach::event& x);
209 Smach::event_result event_refresh_handler(const Smach::event& x);
211 Smach::event_result event_mouse_down_handler(const Smach::event& x);
213 Smach::event_result event_stroke(const Smach::event& x);
214 Smach::event_result event_refresh_tool_options(const Smach::event& x);
215 void refresh_tool_options();
217 Smach::event_result process_stroke(StrokeData stroke_data, WidthData width_data, bool region_flag=false);
219 bool process_queue();
222 StateDraw_Context(CanvasView* canvas_view);
224 ~StateDraw_Context();
226 const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
227 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
228 synfig::Time get_time()const { return get_canvas_interface()->get_time(); }
229 synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
230 WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
232 //void on_user_click(synfig::Point point);
235 }; // END of class StateDraw_Context
238 /* === M E T H O D S ======================================================= */
240 StateDraw::StateDraw():
241 Smach::state<StateDraw_Context>("draw")
243 insert(event_def(EVENT_STOP,&StateDraw_Context::event_stop_handler));
244 insert(event_def(EVENT_REFRESH,&StateDraw_Context::event_refresh_handler));
245 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateDraw_Context::event_mouse_down_handler));
246 insert(event_def(EVENT_WORKAREA_STROKE,&StateDraw_Context::event_stroke));
247 insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateDraw_Context::event_refresh_tool_options));
250 StateDraw::~StateDraw()
256 StateDraw_Context::load_settings()
260 if(settings.get_value("draw.id",value))
263 set_id("NewDrawing");
265 if(settings.get_value("draw.pressure_width",value) && value=="0")
266 set_pressure_width_flag(false);
268 set_pressure_width_flag(true);
270 if(settings.get_value("draw.auto_loop",value) && value=="0")
271 set_auto_loop_flag(false);
273 set_auto_loop_flag(true);
275 if(settings.get_value("draw.auto_extend",value) && value=="0")
276 set_auto_extend_flag(false);
278 set_auto_extend_flag(true);
280 if(settings.get_value("draw.auto_link",value) && value=="0")
281 set_auto_link_flag(false);
283 set_auto_link_flag(true);
285 if(settings.get_value("draw.region",value) && value=="0")
286 set_region_flag(false);
288 set_region_flag(true);
290 if(settings.get_value("draw.outline",value) && value=="0")
291 set_outline_flag(false);
293 set_outline_flag(true);
295 if(settings.get_value("draw.auto_export",value) && value=="1")
296 set_auto_export_flag(true);
298 set_auto_export_flag(false);
300 if(settings.get_value("draw.min_pressure_on",value) && value=="0")
301 set_min_pressure_flag(false);
303 set_min_pressure_flag(true);
305 if(settings.get_value("draw.min_pressure",value))
307 Real n = atof(value.c_str());
312 if(settings.get_value("draw.feather",value))
314 Real n = atof(value.c_str());
319 if(settings.get_value("draw.gthreshold",value))
321 Real n = atof(value.c_str());
325 if(settings.get_value("draw.lthreshold",value))
327 Real n = atof(value.c_str());
331 if(settings.get_value("draw.localize",value) && value == "1")
332 set_local_error_flag(true);
334 set_local_error_flag(false);
338 StateDraw_Context::save_settings()
340 settings.set_value("draw.id",get_id().c_str());
341 settings.set_value("draw.pressure_width",get_pressure_width_flag()?"1":"0");
342 settings.set_value("draw.auto_loop",get_auto_loop_flag()?"1":"0");
343 settings.set_value("draw.auto_extend",get_auto_extend_flag()?"1":"0");
344 settings.set_value("draw.auto_link",get_auto_link_flag()?"1":"0");
345 settings.set_value("draw.region",get_region_flag()?"1":"0");
346 settings.set_value("draw.outline",get_outline_flag()?"1":"0");
347 settings.set_value("draw.auto_export",get_auto_export_flag()?"1":"0");
348 settings.set_value("draw.min_pressure",strprintf("%f",get_min_pressure()));
349 settings.set_value("draw.feather",strprintf("%f",get_feather()));
350 settings.set_value("draw.min_pressure_on",get_min_pressure_flag()?"1":"0");
351 settings.set_value("draw.gthreshold",strprintf("%f",get_gthres()));
352 settings.set_value("draw.lthreshold",strprintf("%f",get_lthres()));
353 settings.set_value("draw.localize",get_local_error_flag()?"1":"0");
357 StateDraw_Context::increment_id()
366 // If there is a number
367 // already at the end of the
368 // id, then remove it.
369 if(id[id.size()-1]<='9' && id[id.size()-1]>='0')
371 // figure out how many digits it is
373 (int)id.size()-1 >= digits && id[id.size()-1-digits] <= '9' && id[id.size()-1-digits] >= '0';
378 str_number=String(id,id.size()-digits,id.size());
379 id=String(id,0,id.size()-digits);
380 synfig::info("---------------- \"%s\"",str_number.c_str());
382 number=atoi(str_number.c_str());
392 // Add the number back onto the id
394 const String format(strprintf("%%0%dd",digits));
395 id+=strprintf(format.c_str(),number);
402 StateDraw_Context::StateDraw_Context(CanvasView* canvas_view):
403 canvas_view_(canvas_view),
404 is_working(*canvas_view),
406 prev_workarea_layer_status_(get_work_area()->get_allow_layer_clicks()),
407 settings(synfigapp::Main::get_selected_input_device()->settings()),
409 checkbutton_pressure_width(_("Pressure Width")),
410 checkbutton_auto_loop(_("Auto Loop")),
411 checkbutton_auto_extend(_("Auto Extend")),
412 checkbutton_auto_link(_("Auto Link")),
413 checkbutton_region(_("Create Region")),
414 checkbutton_outline(_("Create Outline")),
415 checkbutton_auto_export(_("Auto Export")),
416 button_fill_last_stroke(_("Fill Last Stroke")),
417 adj_min_pressure(0,0,1,0.01,0.1),
418 spin_min_pressure(adj_min_pressure,0.1,3),
419 check_min_pressure(_("Min Pressure")),
420 adj_feather(0,0,10000,0.01,0.1),
421 spin_feather(adj_feather,0.01,4),
422 adj_globalthres(.70f,0.01,10000,0.01,0.1),
423 spin_globalthres(adj_globalthres,0.01,3),
424 adj_localthres(20,1,100000,0.1,1),
425 check_localerror(_("LocalError"))
428 synfig::info("STATE SKETCH: entering state");
435 //options_table.attach(*manage(new Gtk::Label(_("Draw Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
436 options_table.attach(entry_id, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
437 options_table.attach(checkbutton_region, 0, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
438 options_table.attach(checkbutton_outline, 0, 2, 3, 4, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
439 options_table.attach(checkbutton_auto_loop, 0, 2, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
440 options_table.attach(checkbutton_auto_extend, 0, 2, 5, 6, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
441 options_table.attach(checkbutton_auto_link, 0, 2, 6, 7, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
442 options_table.attach(checkbutton_auto_export, 0, 2, 7, 8, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
443 options_table.attach(checkbutton_pressure_width, 0, 2, 8, 9, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
444 options_table.attach(check_localerror, 0, 2, 9, 10, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
446 options_table.attach(check_min_pressure, 0, 1, 10, 11, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
447 options_table.attach(spin_min_pressure, 1, 2, 10, 11, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
449 options_table.attach(*manage(new Gtk::Label(_("Smooth"))), 0, 1, 11, 12, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
450 options_table.attach(spin_globalthres, 1, 2, 11, 12, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
452 options_table.attach(*manage(new Gtk::Label(_("Feather"))), 0, 1, 12, 13, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
453 options_table.attach(spin_feather, 1, 2, 12, 13, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
455 //options_table.attach(button_fill_last_stroke, 0, 2, 13, 14, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
457 button_fill_last_stroke.signal_pressed().connect(sigc::mem_fun(*this,&StateDraw_Context::fill_last_stroke));
458 check_localerror.signal_toggled().connect(sigc::mem_fun(*this,&StateDraw_Context::UpdateErrorBox));
460 options_table.show_all();
461 refresh_tool_options();
462 //App::dialog_tool_options->set_widget(options_table);
463 App::dialog_tool_options->present();
466 old_duckmask=get_work_area()->get_type_mask();
467 get_work_area()->set_type_mask(Duck::TYPE_ALL-Duck::TYPE_TANGENT-Duck::TYPE_WIDTH);
469 // Turn off layer clicking
470 get_work_area()->set_allow_layer_clicks(false);
472 // Turn off duck clicking
473 get_work_area()->set_allow_duck_clicks(false);
475 // clear out the ducks
476 //get_work_area()->clear_ducks();
478 // Refresh the work area
479 //get_work_area()->queue_draw();
481 // Hide the tables if they are showing
482 prev_table_status=get_canvas_view()->tables_are_visible();
483 //if(prev_table_status)get_canvas_view()->hide_tables();
485 // Disable the time bar
486 get_canvas_view()->set_sensitive_timebar(false);
489 //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StateDraw_Context::on_user_click));
491 get_canvas_view()->work_area->set_cursor(Gdk::PENCIL);
493 App::toolbox->refresh();
499 void StateDraw_Context::UpdateErrorBox()
501 if(get_local_error_flag())
503 spin_globalthres.set_adjustment(adj_localthres);
504 spin_globalthres.set_value(adj_localthres.get_value());
505 spin_globalthres.set_increments(0.1,1);
508 spin_globalthres.set_adjustment(adj_globalthres);
509 spin_globalthres.set_value(adj_globalthres.get_value());
510 spin_globalthres.set_increments(0.01,.1);
513 spin_globalthres.update();
517 StateDraw_Context::refresh_tool_options()
519 App::dialog_tool_options->clear();
520 App::dialog_tool_options->set_widget(options_table);
521 App::dialog_tool_options->set_local_name(_("Draw Tool"));
522 App::dialog_tool_options->set_name("draw");
524 App::dialog_tool_options->add_button(
525 Gtk::StockID("synfig-fill"),
526 _("Fill Last Stroke")
527 )->signal_clicked().connect(
530 &StateDraw_Context::fill_last_stroke));
534 StateDraw_Context::event_refresh_tool_options(const Smach::event& /*x*/)
536 refresh_tool_options();
537 return Smach::RESULT_ACCEPT;
540 StateDraw_Context::~StateDraw_Context()
544 App::dialog_tool_options->clear();
546 get_work_area()->set_type_mask(old_duckmask);
548 get_canvas_view()->work_area->reset_cursor();
550 // Restore layer clicking
551 get_work_area()->set_allow_layer_clicks(prev_workarea_layer_status_);
553 // Restore duck clicking
554 get_work_area()->set_allow_duck_clicks(true);
556 // Enable the time bar
557 get_canvas_view()->set_sensitive_timebar(true);
559 // Bring back the tables if they were out before
560 if(prev_table_status)get_canvas_view()->show_tables();
562 // Refresh the work area
563 get_work_area()->queue_draw();
565 App::toolbox->refresh();
569 StateDraw_Context::event_stop_handler(const Smach::event& /*x*/)
571 throw Smach::egress_exception();
575 StateDraw_Context::event_refresh_handler(const Smach::event& /*x*/)
578 return Smach::RESULT_ACCEPT;
582 StateDraw_Context::event_mouse_down_handler(const Smach::event& x)
584 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
589 // Enter the stroke state to get the stroke
590 get_canvas_view()->get_smach().push_state(&state_stroke);
591 return Smach::RESULT_ACCEPT;
594 case BUTTON_RIGHT: // Intercept the right-button click to short-circuit the pop-up menu
595 if (!getenv("SYNFIG_ENABLE_POPUP_MENU_IN_ALL_TOOLS"))
596 return Smach::RESULT_ACCEPT;
599 return Smach::RESULT_OK;
603 #define SIMILAR_TANGENT_THRESHOLD (0.2)
608 debugclass(const synfig::String &x):x(x)
610 // synfig::warning(">>>>>>>>>>>>>>>>>>> "+x);
614 // synfig::warning("<<<<<<<<<<<<<<<<<<< "+x);
621 DepthCounter(int &i):i(i) { i++; }
622 ~DepthCounter() { i--; }
626 StateDraw_Context::event_stroke(const Smach::event& x)
628 // debugclass debugger("StateDraw_Context::event_stroke(const Smach::event& x)");
630 const EventStroke& event(*reinterpret_cast<const EventStroke*>(&x));
632 assert(event.stroke_data);
634 get_work_area()->add_stroke(event.stroke_data,synfigapp::Main::get_foreground_color());
638 DirtyTrap dirty_trap(get_work_area());
639 Smach::event_result result;
640 result=process_stroke(event.stroke_data,event.width_data,event.modifier&Gdk::CONTROL_MASK || event.modifier&Gdk::BUTTON2_MASK);
645 stroke_queue.push_back(pair<StrokeData,WidthData>(event.stroke_data,event.width_data));
647 return Smach::RESULT_ACCEPT;
651 StateDraw_Context::process_queue()
653 // debugclass debugger("StateDraw_Context::process_queue()");
656 DepthCounter depth_counter(nested);
657 while(!stroke_queue.empty())
659 pair<StrokeData,WidthData> front(stroke_queue.front());
660 process_stroke(front.first,front.second);
661 stroke_queue.pop_front();
667 StateDraw_Context::process_stroke(StrokeData stroke_data, WidthData width_data, bool region_flag)
669 // debugclass debugger("StateDraw_Context::process_stroke");
670 DepthCounter depth_counter(nested);
672 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);
675 // If we aren't using pressure width,
676 // then set all the width to 1
677 if(!get_pressure_width_flag())
679 std::list<synfig::Real>::iterator iter;
680 for(iter=width_data->begin();iter!=width_data->end();++iter)
686 //get_work_area()->add_stroke(event.stroke_data,synfigapp::Main::get_foreground_color());
687 //stroke_list.push_back(event.stroke_data);
690 std::list<synfig::BLinePoint> bline;
691 bool loop_bline_flag(false);
693 //Changed by Adrian - use resident class :)
694 //synfigapp::convert_stroke_to_bline(bline, *event.stroke_data,*event.width_data, synfigapp::Main::get_bline_width());
695 blineconv.width = synfigapp::Main::get_bline_width().units(get_canvas()->rend_desc());
697 if(get_local_error_flag())
699 float pw = get_work_area()->get_pw();
700 float ph = get_work_area()->get_ph();
702 blineconv.pixelwidth = sqrt(pw*pw+ph*ph);
703 blineconv.smoothness = get_lthres();
706 blineconv.pixelwidth = 1;
707 blineconv.smoothness = get_gthres();
710 blineconv(bline,*stroke_data,*width_data);
712 //Postprocess to require minimum pressure
713 if(get_min_pressure_flag())
715 synfigapp::BLineConverter::EnforceMinWidth(bline,get_min_pressure());
718 // If the start and end points are similar, then make them the same point
719 if (get_auto_loop_flag() &&
721 (bline.front().get_vertex() - bline.back().get_vertex()).mag() <= radius)
723 loop_bline_flag=true;
727 while (bline.size() > 2 &&
728 (bline.front().get_vertex() - bline.back().get_vertex()).mag() <= radius)
730 tangent=bline.back().get_tangent1();
731 width=bline.back().get_width();
735 if(abs(bline.front().get_tangent1().norm()*tangent.norm().perp())>SIMILAR_TANGENT_THRESHOLD)
737 // If the tangents are not similar, then
738 // split the tangents
739 bline.front().set_split_tangent_flag(true);
740 bline.front().set_tangent1(tangent);
744 // If the tangents are similar, then set the tangent
745 // to the average of the two
746 bline.front().set_tangent((tangent+bline.front().get_tangent1())*0.5f);
749 // Add the widths of the two points
751 Real tmp_width(bline.front().get_width()+width);
752 tmp_width=tmp_width<=1?tmp_width:1;
753 bline.front().set_width(tmp_width);
757 // If the bline only has one blinepoint, then there is nothing to do.
760 // hide the 'stroke' line we were drawing, unless the user
761 // explicitly requests that they are kept
762 if (!getenv("SYNFIG_KEEP_ABORTED_DRAW_LINES"))
765 return Smach::RESULT_OK;
769 return new_region(bline,radius);
771 return new_bline(bline,loop_bline_flag,radius);
775 StateDraw_Context::new_bline(std::list<synfig::BLinePoint> bline,bool loop_bline_flag,float radius)
777 synfigapp::SelectionManager::LayerList layer_list = get_canvas_view()->get_selection_manager()->get_selected_layers();
779 // Create the action group
780 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Sketch BLine"));
782 bool shift_offset = false;
783 Vector shift_offset_vector;
784 bool join_start_no_extend=false,join_finish_no_extend=false;
785 synfigapp::ValueDesc start_duck_value_desc,finish_duck_value_desc;
786 bool extend_start=false,extend_finish=false,complete_loop=false;
787 bool extend_start_join_same=false,extend_start_join_different=false;
788 bool extend_finish_join_same=false,extend_finish_join_different=false;
789 int start_duck_index = 0,finish_duck_index = 0; // initialized to keep the compiler happy; shouldn't be needed though
790 ValueNode_BLine::Handle start_duck_value_node_bline=NULL,finish_duck_value_node_bline=NULL;
792 // Find any ducks at the start or end that we might attach to
793 // (this used to only run if we didn't just draw a loop - ie. !loop_bline_flag
794 // but having loops auto-connect can be useful as well)
795 if(get_auto_extend_flag() || get_auto_link_flag())
797 etl::handle<Duck> start_duck(get_work_area()->find_duck(bline.front().get_vertex(),radius,Duck::TYPE_VERTEX));
798 etl::handle<Duck> finish_duck(get_work_area()->find_duck(bline.back().get_vertex(),radius,Duck::TYPE_VERTEX));
800 // check whether the start of the new line extends an
801 // existing line. this is only the case if the new
802 // line isn't a self-contained loop, and if the new
803 // line starts at one of the ends of an existing line
806 if(!(start_duck_value_desc=start_duck->get_value_desc()))break;
807 if(loop_bline_flag)break; // loops don't extend anything
808 if(!start_duck_value_desc.parent_is_value_node())break;
809 start_duck_index=start_duck_value_desc.get_index(); // which point on the line did we start drawing at
810 start_duck_value_node_bline=ValueNode_BLine::Handle::cast_dynamic(start_duck_value_desc.get_parent_value_node());
811 if(!get_auto_extend_flag())break;
813 // don't extend looped blines
814 if(start_duck_value_node_bline&&!start_duck_value_node_bline->get_loop()&&
815 // did we start drawing at either end of the line?
816 (start_duck_index==0||start_duck_index==start_duck_value_node_bline->link_count()-1))
820 shift_offset_vector=start_duck->get_origin();
824 // check whether the end of the new line extends an
825 // existing line. this is only the case if the new
826 // line isn't a self-contained loop, and if the new
827 // line ends at one of the ends of an existing line
830 if(!(finish_duck_value_desc=finish_duck->get_value_desc()))break;
831 if(loop_bline_flag)break;
832 if(!finish_duck_value_desc.parent_is_value_node())break;
833 finish_duck_index=finish_duck_value_desc.get_index();
834 finish_duck_value_node_bline=ValueNode_BLine::Handle::cast_dynamic(finish_duck_value_desc.get_parent_value_node());
835 if(!get_auto_extend_flag())break;
837 // don't extend looped blines
838 if(finish_duck_value_node_bline&&!finish_duck_value_node_bline->get_loop()&&
839 (finish_duck_index==0||finish_duck_index==finish_duck_value_node_bline->link_count()-1))
843 // we've started and finished drawing at the end of a bline. we can't
844 // extend both blines, so unless we started and finished at the 2 ends
845 // of the same bline, only extend the one we started on
846 if(start_duck_value_node_bline==finish_duck_value_node_bline)
847 complete_loop=extend_finish=true;
851 shift_offset_vector=finish_duck->get_origin();
856 // if the new line's start didn't extend an existing line,
857 // check whether it needs to be linked to an existing duck
858 if(!extend_start&&get_auto_link_flag()&&start_duck&&start_duck_value_desc)
859 switch(start_duck_value_desc.get_value_type())
861 case synfig::ValueBase::TYPE_BLINEPOINT:
862 start_duck_value_desc=synfigapp::ValueDesc(LinkableValueNode::Handle::cast_dynamic(start_duck_value_desc.get_value_node()),0);
864 case synfig::ValueBase::TYPE_VECTOR:
865 if (shift_offset && shift_offset_vector != start_duck->get_origin())
868 shift_offset_vector = start_duck->get_origin();
869 get_canvas_interface()->auto_export(start_duck_value_desc);
871 if(start_duck_value_node_bline&&start_duck_value_node_bline==finish_duck_value_node_bline)
872 extend_finish_join_same=true;
874 extend_finish_join_different=true;
876 join_start_no_extend=true;
881 // if the new line's end didn't extend an existing line,
882 // check whether it needs to be linked to an existing duck
883 if(!extend_finish&&get_auto_link_flag()&&finish_duck&&finish_duck_value_desc)
884 switch(finish_duck_value_desc.get_value_type())
886 case synfig::ValueBase::TYPE_BLINEPOINT:
887 finish_duck_value_desc=synfigapp::ValueDesc(LinkableValueNode::Handle::cast_dynamic(finish_duck_value_desc.get_value_node()),0);
889 case synfig::ValueBase::TYPE_VECTOR:
890 if (shift_offset && shift_offset_vector != finish_duck->get_origin())
893 shift_offset_vector = finish_duck->get_origin();
894 get_canvas_interface()->auto_export(finish_duck_value_desc);
896 if(finish_duck_value_node_bline&&start_duck_value_node_bline==finish_duck_value_node_bline)
897 extend_start_join_same=true;
899 extend_start_join_different=true;
901 join_finish_no_extend=true;
907 ValueNode_BLine::Handle value_node;
908 std::list<synfig::BLinePoint> trans_bline;
911 std::list<synfig::BLinePoint>::iterator iter;
912 const synfig::TransformStack& transform(get_canvas_view()->get_curr_transform_stack());
914 for(iter=bline.begin();iter!=bline.end();++iter)
916 BLinePoint bline_point(*iter);
917 Point new_vertex(transform.unperform(bline_point.get_vertex()));
919 bline_point.set_tangent1(
921 bline_point.get_tangent1()+bline_point.get_vertex()
925 bline_point.set_tangent2(
927 bline_point.get_tangent2()+bline_point.get_vertex()
932 new_vertex=new_vertex-shift_offset_vector;
934 bline_point.set_vertex(new_vertex);
936 trans_bline.push_back(bline_point);
938 value_node=ValueNode_BLine::create(synfig::ValueBase(trans_bline,loop_bline_flag));
940 Canvas::Handle canvas(get_canvas_view()->get_canvas());
941 Layer::Handle layer(get_canvas_view()->get_selection_manager()->get_selected_layer());
942 if (layer) canvas=layer->get_canvas();
943 value_node->set_member_canvas(canvas);
946 Smach::event_result result;
947 synfig::ValueNode_DynamicList::ListEntry source;
949 // the new line's start extends an existing line
952 int target_offset = 0;
953 if(complete_loop)trans_bline.pop_back();
954 trans_bline.pop_front();
955 if(start_duck_index==0)
956 { // We need to reverse the BLine first.
957 reverse_bline(trans_bline);
958 result=extend_bline_from_begin(start_duck_value_node_bline,trans_bline,complete_loop);
959 source=start_duck_value_node_bline->list.front();
960 target_offset=trans_bline.size();
964 result=extend_bline_from_end(start_duck_value_node_bline,trans_bline,complete_loop);
965 source=start_duck_value_node_bline->list.back();
968 if(extend_start_join_different)
969 LinkableValueNode::Handle::cast_dynamic(source.value_node)->
970 set_link(0,finish_duck_value_desc.get_value_node());
971 else if(extend_start_join_same)
972 LinkableValueNode::Handle::cast_dynamic(source.value_node)->
973 set_link(0,synfigapp::ValueDesc(LinkableValueNode::Handle::cast_dynamic(start_duck_value_node_bline->
974 list[target_offset+finish_duck_index].value_node),0).get_value_node());
978 // the new line's end extends an existing line
981 int target_offset = 0;
982 trans_bline.pop_back();
983 if(finish_duck_index==0)
985 result=extend_bline_from_begin(finish_duck_value_node_bline,trans_bline,false);
986 source=finish_duck_value_node_bline->list.front();
987 target_offset=trans_bline.size();
990 { // We need to reverse the BLine first.
991 reverse_bline(trans_bline);
992 result=extend_bline_from_end(finish_duck_value_node_bline,trans_bline,false);
993 source=finish_duck_value_node_bline->list.back();
996 if(extend_finish_join_different)
997 LinkableValueNode::Handle::cast_dynamic(source.value_node)->
998 set_link(0,start_duck_value_desc.get_value_node());
999 else if(extend_finish_join_same)
1000 LinkableValueNode::Handle::cast_dynamic(source.value_node)->
1001 set_link(0,synfigapp::ValueDesc(LinkableValueNode::Handle::cast_dynamic(finish_duck_value_node_bline->
1002 list[target_offset+start_duck_index].value_node),0).get_value_node());
1006 if (join_start_no_extend)
1007 LinkableValueNode::Handle::cast_dynamic(value_node->list.front().value_node)->
1008 set_link(0,start_duck_value_desc.get_value_node());
1010 if (join_finish_no_extend)
1011 LinkableValueNode::Handle::cast_dynamic(value_node->list.back().value_node)->
1012 set_link(0,finish_duck_value_desc.get_value_node());
1014 if(get_auto_export_flag())
1015 if (!get_canvas_interface()->add_value_node(value_node,get_id()))
1017 /* it's no big deal, is it? let's keep the shape anyway */
1018 // get_canvas_view()->get_ui_interface()->error(_("Unable to add value node"));
1021 // return Smach::RESULT_ERROR;
1024 last_stroke=value_node;
1025 last_stroke_id=get_id();
1028 // Create the layer(s)
1029 Layer::Handle layer;
1030 Canvas::Handle canvas(get_canvas_view()->get_canvas());
1033 // we are temporarily using the layer to hold something
1034 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
1037 depth=layer->get_depth();
1038 canvas=layer->get_canvas();
1041 // fill_last_stroke() will take care of clearing the selection if we're calling it
1042 if(get_outline_flag() && get_region_flag())
1043 fill_last_stroke_and_unselect_other_layers();
1045 get_canvas_interface()->get_selection_manager()->clear_selected_layers();
1047 //int number(synfig::UniqueID().get_uid());
1049 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
1051 // if they're both defined, we'll add the region later
1052 if(get_outline_flag())
1054 layer=get_canvas_interface()->add_layer_to("outline",canvas,depth);
1055 layer->set_description(get_id()+_(" Outline"));
1059 layer=get_canvas_interface()->add_layer_to("region",canvas,depth);
1060 layer->set_description(get_id()+_(" Region"));
1065 layer->set_param("feather",get_feather());
1066 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
1069 //layer->set_description(strprintf("Stroke %d",number));
1070 //get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
1073 get_canvas_interface()->
1074 change_value(synfigapp::ValueDesc(layer,"offset"),shift_offset_vector);
1076 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
1080 action->set_param("canvas",get_canvas());
1081 action->set_param("canvas_interface",get_canvas_interface());
1082 action->set_param("layer",layer);
1083 if(!action->set_param("param",String("bline")))
1084 synfig::error("LayerParamConnect didn't like \"param\"");
1085 if(!action->set_param("value_node",ValueNode::Handle(value_node)))
1086 synfig::error("LayerParamConnect didn't like \"value_node\"");
1088 if(!get_canvas_interface()->get_instance()->perform_action(action))
1090 get_canvas_view()->get_ui_interface()->error(_("Unable to create layer"));
1094 return Smach::RESULT_ERROR;
1096 layer_list.push_back(layer);
1097 get_canvas_view()->get_selection_manager()->set_selected_layers(layer_list);
1102 return Smach::RESULT_ACCEPT;
1107 debug_show_vertex_list(int iteration, std::list<synfigapp::ValueDesc>& vertex_list,
1108 std::string title, int current)
1110 std::list<synfigapp::ValueDesc>::iterator i = vertex_list.begin();
1111 printf("\n%s\n ----- iter %d : ", title.c_str(), iteration);
1113 synfig::LinkableValueNode::Handle last = 0;
1118 bool started = false;
1119 for(;i!=vertex_list.end();i++,c++)
1121 synfigapp::ValueDesc value_desc(*i);
1123 if (value_desc.parent_is_value_node()) {
1124 if(value_desc.parent_is_linkable_value_node())
1126 index = value_desc.get_index();
1127 // printf("<%d>", index);
1128 if (last == synfig::LinkableValueNode::Handle::cast_reinterpret(value_desc.get_parent_value_node()))
1130 // printf("\n%s:%d\n", __FILE__, __LINE__);
1133 // printf("\n%s:%d\n", __FILE__, __LINE__);
1136 // printf("\n%s:%d\n", __FILE__, __LINE__);
1139 if (started) printf(", "); else started = true;
1140 printf("%d--%d", start, prev);
1144 if (started) printf(", "); else started = true;
1145 printf("%d", start);
1147 printf(", *%d*", index);
1152 // printf("\n%s:%d\n", __FILE__, __LINE__);
1153 if (index == start + 1)
1155 // printf("\n%s:%d\n", __FILE__, __LINE__);
1159 else if (index == start - 1)
1161 // printf("\n%s:%d\n", __FILE__, __LINE__);
1167 if (started) printf(", "); else started = true;
1168 printf("%d", start);
1172 else if (index == prev + dir)
1174 // printf("\n%s:%d\n", __FILE__, __LINE__);
1179 // printf("\n%s:%d\n", __FILE__, __LINE__);
1180 if (started) printf(", "); else started = true;
1182 printf("%d--%d", start, prev);
1184 printf("%d", start);
1185 // printf("\n%s:%d\n", __FILE__, __LINE__);
1192 // printf("\n%s:%d\n", __FILE__, __LINE__);
1195 if (started) printf(", "); else started = true;
1196 printf("*%d*", index);
1200 // printf("\n%s:%d\n", __FILE__, __LINE__);
1208 // printf("\n%s:%d\n", __FILE__, __LINE__);
1211 // printf("\n%s:%d\n", __FILE__, __LINE__);
1214 if (started) printf(", "); else started = true;
1216 printf("%d--%d", start, prev);
1218 printf("%d", start);
1220 // printf("\n%s:%d\n", __FILE__, __LINE__);
1223 // printf("\n%s:%d\n", __FILE__, __LINE__);
1224 last = synfig::LinkableValueNode::Handle::cast_reinterpret(value_desc.get_parent_value_node());
1225 printf("%d:(", synfig::LinkableValueNode::Handle::cast_reinterpret(value_desc.get_parent_value_node())->link_count());
1227 // printf("\n%s:%d\n", __FILE__, __LINE__);
1231 printf("*%d*", index);
1235 // printf("\n%s:%d\n", __FILE__, __LINE__);
1239 // printf("\n%s:%d\n", __FILE__, __LINE__);
1241 // printf("\n%s:%d\n", __FILE__, __LINE__);
1244 if (last) printf("?!) ");
1254 if (started) printf(", "); else started = true;
1258 printf("%d--%d", start, prev);
1260 printf("%d", start);
1267 #define debug_show_vertex_list(a,b,c,d)
1271 StateDraw_Context::new_region(std::list<synfig::BLinePoint> bline, synfig::Real radius)
1273 // Create the action group
1274 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Define Region"));
1276 std::list<synfigapp::ValueDesc> vertex_list;
1278 printf("new_region with %d bline points\n", bline.size());
1280 // First we need to come up with a rough list of
1281 // BLinePoints that we are going to be using to
1282 // define our region.
1284 std::list<synfig::BLinePoint>::iterator iter;
1285 for(iter=bline.begin();iter!=bline.end();++iter)
1287 etl::handle<Duck> duck(get_work_area()->find_duck(iter->get_vertex(),0,Duck::TYPE_VERTEX));
1291 synfig::info(__FILE__":%d: Nothing to enclose!",__LINE__);
1292 return Smach::RESULT_OK;
1295 assert(duck->get_type()==Duck::TYPE_VERTEX);
1297 synfigapp::ValueDesc value_desc(duck->get_value_desc());
1301 synfig::info(__FILE__":%d: Got a hit, but no ValueDesc on this duck",__LINE__);
1305 switch(value_desc.get_value_type())
1307 case synfig::ValueBase::TYPE_BLINEPOINT:
1308 //if(vertex_list.empty() || value_desc!=vertex_list.back())
1309 vertex_list.push_back(value_desc);
1310 assert(vertex_list.back().is_valid());
1319 assert(vertex_list.back().is_valid());
1321 printf("vertex list with %d bline points\n", vertex_list.size());
1323 // Remove any duplicates
1327 ValueNode_BLine::Handle value_node_bline;
1329 // Now we need to test for the trivial case,
1330 // which is where all of the vertices
1331 // come from one BLine.
1332 if(vertex_list.front().parent_is_linkable_value_node())
1334 bool trivial_case(true);
1335 ValueNode::Handle trivial_case_value_node;
1337 trivial_case_value_node=vertex_list.front().get_parent_value_node();
1339 std::list<synfigapp::ValueDesc>::iterator iter;
1340 for(iter=vertex_list.begin();iter!=vertex_list.end();++iter)
1342 if(trivial_case_value_node!=iter->get_parent_value_node())
1349 // \todo - re-enable this code
1350 if(trivial_case && false)
1352 synfig::info("all points are on the same bline, so just fill that line");
1353 value_node_bline=ValueNode_BLine::Handle::cast_dynamic(trivial_case_value_node);
1355 synfig::info("the line has %d vertices", value_node_bline->link_count());
1357 if(value_node_bline->link_count() <= 2)
1359 synfig::info(__FILE__":%d: Vertex list too small to make region.",__LINE__);
1360 return Smach::RESULT_OK;
1365 if(!value_node_bline)
1366 if(vertex_list.size()<=2)
1368 synfig::info(__FILE__":%d: Vertex list too small to make region.",__LINE__);
1369 return Smach::RESULT_OK;
1372 // Now we need to clean the list of vertices up
1373 // a bit. This includes inserting missing vertices
1374 // and removing extraneous ones.
1375 // We can do this in multiple passes.
1376 if(!value_node_bline)
1378 debug_show_vertex_list(0, vertex_list, "before shifting stuff", -1);
1379 // rearrange the list so that the first and last node are on different blines
1380 std::list<synfigapp::ValueDesc>::iterator iter, start;
1381 ValueNode::Handle last_value_node = vertex_list.back().get_parent_value_node();
1382 for(iter = vertex_list.begin(); iter!=vertex_list.end(); iter++)
1383 if (iter->get_parent_value_node() != last_value_node)
1385 vertex_list.insert(vertex_list.end(), vertex_list.begin(), iter);
1386 vertex_list.erase(vertex_list.begin(), iter);
1390 debug_show_vertex_list(0, vertex_list, "before detecting direction and limits", -1);
1391 // rearrange the list so that the first and last node are on different blines
1392 iter = vertex_list.begin();
1393 while (iter!=vertex_list.end())
1395 // make a note of which bline we're looking at
1396 ValueNode::Handle parent_value_node = iter->get_parent_value_node();
1398 int points_in_line = synfig::LinkableValueNode::Handle::cast_reinterpret(parent_value_node)->link_count();
1399 bool looped = (*parent_value_node)(get_time()).get_loop();
1400 int this_index, last_index = iter->get_index();
1401 int min_index = last_index, max_index = last_index;
1405 // printf("there are %d points in this line - first is index %d\n", points_in_line, last_index);
1407 // while we're looking at the same bline, keep going
1409 while (iter != vertex_list.end() && iter->get_parent_value_node() == parent_value_node)
1411 this_index = iter->get_index();
1412 // printf("index went from %d to %d\n", last_index, this_index);
1415 if (this_index - last_index > points_in_line/2)
1416 while (this_index - last_index > points_in_line/2)
1417 this_index -= points_in_line;
1418 else if (last_index - this_index > points_in_line/2)
1419 while (last_index - this_index > points_in_line/2)
1420 this_index += points_in_line;
1423 if (this_index < min_index) min_index = this_index;
1424 if (this_index > max_index) max_index = this_index;
1426 // printf("so let's imagine index went from %d to %d\n", last_index, this_index);
1427 if (this_index > last_index)
1429 else if (this_index < last_index)
1432 last_index = this_index;
1436 // printf("min %d and max %d\n", min_index, max_index);
1437 whole = max_index - min_index >= points_in_line;
1438 min_index = (min_index % points_in_line + points_in_line) % points_in_line;
1439 max_index = (max_index % points_in_line + points_in_line) % points_in_line;
1440 // they drew around a shape more than once - what's the start/end point? does it matter?
1441 if (whole) min_index = max_index = (min_index + max_index) / 2;
1442 // printf("processed min %d max %d whole %d\n", min_index, max_index, whole);
1448 // printf("whole (down) (%d) ", min_index);
1449 for (int i = min_index; i >= 0; i--)
1451 // printf("%d ", i);
1452 vertex_list.insert(start, synfigapp::ValueDesc(parent_value_node, i));
1454 for (int i = points_in_line - 1; i >= min_index; i--)
1456 // printf("%d ", i);
1457 vertex_list.insert(start, synfigapp::ValueDesc(parent_value_node, i));
1462 // printf("part (down) (%d -> %d) ", max_index, min_index);
1463 for (int i = max_index; i != min_index; i--)
1465 if (i == -1) i = points_in_line - 1;
1466 // printf("%d ", i);
1467 vertex_list.insert(start, synfigapp::ValueDesc(parent_value_node, i));
1469 vertex_list.insert(start, synfigapp::ValueDesc(parent_value_node, min_index));
1476 // printf("whole (%d) ", min_index);
1477 for (int i = min_index; i < points_in_line; i++)
1479 // printf("%d ", i);
1480 vertex_list.insert(start, synfigapp::ValueDesc(parent_value_node, i));
1482 for (int i = 0; i <= min_index; i++)
1484 // printf("%d ", i);
1485 vertex_list.insert(start, synfigapp::ValueDesc(parent_value_node, i));
1490 // printf("part (%d -> %d) ", min_index, max_index);
1491 for (int i = min_index; i != max_index; i++)
1493 if (i == points_in_line) i = 0;
1494 // printf("%d ", i);
1495 vertex_list.insert(start, synfigapp::ValueDesc(parent_value_node, i));
1497 vertex_list.insert(start, synfigapp::ValueDesc(parent_value_node, max_index));
1501 // debug_show_vertex_list(0, vertex_list, "after insert", -1);
1502 vertex_list.erase(start, iter);
1503 // debug_show_vertex_list(0, vertex_list, "after delete", -1);
1506 debug_show_vertex_list(0, vertex_list, "continuous vertices", -1);
1508 // \todo reenable or delete this section
1510 for(bool done=false;!done && i<30;i++)
1512 debug_show_vertex_list(i, vertex_list, "in big loop", -1);
1514 // Set done to "true" for now. If
1515 // any updates are performed, we will
1516 // change it back to false.
1519 std::list<synfigapp::ValueDesc>::iterator prev,next;
1520 prev=vertex_list.end();prev--; // Set prev to the last ValueDesc
1521 next=vertex_list.begin();
1522 iter=next++; // Set iter to the first value desc, and next to the second
1525 for(;iter!=vertex_list.end();prev=iter,iter++,next++,current++)
1527 // we need to be able to erase(next) and can't do that if next is end()
1528 if (next == vertex_list.end()) next = vertex_list.begin();
1529 debug_show_vertex_list(i, vertex_list, "in loop around vertices", current);
1530 synfigapp::ValueDesc value_prev(*prev);
1531 synfigapp::ValueDesc value_desc(*iter);
1532 synfigapp::ValueDesc value_next(*next);
1534 assert(value_desc.is_valid());
1535 assert(value_next.is_valid());
1536 assert(value_prev.is_valid());
1538 // synfig::info("-------");
1539 // synfig::info(__FILE__":%d: value_prev 0x%08X:%d",__LINE__,value_prev.get_parent_value_node().get(),value_prev.get_index());
1540 // synfig::info(__FILE__":%d: value_desc 0x%08X:%d",__LINE__,value_desc.get_parent_value_node().get(),value_desc.get_index());
1541 // synfig::info(__FILE__":%d: value_next 0x%08X:%d",__LINE__,value_next.get_parent_value_node().get(),value_next.get_index());
1544 if(value_prev.parent_is_value_node() && value_desc.parent_is_value_node() && value_next.parent_is_value_node())
1546 // Remove random extraneous vertices
1547 if(value_prev.get_parent_value_node()==value_next.get_parent_value_node() &&
1548 value_prev.get_parent_value_node()!=value_desc.get_parent_value_node())
1550 vertex_list.erase(iter);
1557 // // Remove duplicate vertices
1559 // // if previous is the same as current or
1560 // // current is the same as next, remove current
1561 // if(value_prev.get_value_node()==value_desc.get_value_node() ||
1562 // value_desc.get_value_node()==value_next.get_value_node())
1564 // vertex_list.erase(iter);
1566 // printf("erased node - i = %d\n", i);
1570 // // if previous is the same as next, remove previous? or next?
1571 // if(value_prev.get_value_node()==value_next.get_value_node())
1573 // vertex_list.erase(next);
1574 // // vertex_list.erase(prev);
1576 // printf("erased node - i = %d\n", i);
1580 // if 'this' and 'next' both have parents
1581 if (value_desc.parent_is_value_node() && value_next.parent_is_value_node())
1583 // if they are both on the same bline - this has been handled by new code above
1584 if (value_desc.get_parent_value_node() == value_next.get_parent_value_node())
1586 // // if (next != vertex_list.end())
1588 // printf("parent loop is %d and node loop is ??\n",
1589 // (*(value_desc.get_parent_value_node()))(get_time()).get_loop()
1590 // // value_desc.get_value_node().get_loop(),
1593 // // Fill in missing vertices
1594 // // \todo take loops into account: seeing (15, 2, 3, 4) probably means that (0, 1) is missing, not 14 through 3
1595 // if(value_desc.get_index()<value_next.get_index()-1)
1597 // debug_show_vertex_list(i, vertex_list,
1598 // strprintf("same parent, different points this %d < next-1 %d",
1599 // value_desc.get_index(), ((value_next.get_index()-1))),
1601 // for (int index = value_desc.get_index()+1; index < value_next.get_index(); index++)
1603 // printf("inserting up %d\n", index);
1604 // vertex_list.insert(next, synfigapp::ValueDesc(value_desc.get_parent_value_node(), index));
1606 // debug_show_vertex_list(i, vertex_list, "new list", current);
1610 // if(value_next.get_index()<value_desc.get_index()-1)
1612 // debug_show_vertex_list(i, vertex_list,
1613 // strprintf("same parent, different points next %d < this-1 %d",
1614 // value_next.get_index(), ((value_desc.get_index()-1))),
1616 // for (int index = value_desc.get_index()-1; index > value_next.get_index(); index--)
1618 // printf("inserting down %d\n", index);
1619 // vertex_list.insert(next, synfigapp::ValueDesc(value_desc.get_parent_value_node(), index));
1621 // debug_show_vertex_list(i, vertex_list, "new list", current);
1627 // 'this' and 'next' have different parents
1630 ValueNode::Handle v1 = value_desc.get_value_node();
1631 ValueNode::Handle v2 = value_desc.get_parent_value_node();
1635 printf("different\n");
1637 if (value_desc.get_value_node() != value_next.get_value_node())
1639 // Ensure that connections between blines are properly connected
1640 BLinePoint vertex(value_desc.get_value(get_time()).get(BLinePoint()));
1641 BLinePoint vertex_next(value_next.get_value(get_time()).get(BLinePoint()));
1643 //synfig::info("--------");
1644 //synfig::info(__FILE__":%d: vertex: [%f, %f]",__LINE__,vertex.get_vertex()[0],vertex.get_vertex()[1]);
1645 //synfig::info(__FILE__":%d: vertex_next: [%f, %f]",__LINE__,vertex_next.get_vertex()[0],vertex_next.get_vertex()[1]);
1647 // if this vertex is close to the next one, replace this vertex with a new one
1648 // and erase the next one
1649 printf("this point is %5.2f from the next point - compare with %5.2f\n",
1650 (vertex.get_vertex()-vertex_next.get_vertex()).mag_squared(),
1652 if((vertex.get_vertex()-vertex_next.get_vertex()).mag_squared()<radius*radius)
1654 printf("in one - it's close\n");
1655 ValueNode_Composite::Handle value_node;
1656 ValueNode_Composite::Handle value_node_next;
1657 value_node=ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node().clone());
1658 value_node_next=ValueNode_Composite::Handle::cast_dynamic(value_next.get_value_node().clone());
1659 if(!value_node || !value_node_next)
1661 synfig::info(__FILE__":%d: Unable to properly connect blines.",__LINE__);
1664 // \todo if next isn't split, don't we want to copy its 'Tangent 1' instead?
1665 value_node->set_link(5,value_node_next->get_link(5)); // Tangent 2
1666 value_node->set_link(3,ValueNode_Const::create(true)); // Split Tangents
1668 // get_canvas_interface()->auto_export(value_node);
1669 printf("exporting\n");
1670 get_canvas_interface()->add_value_node(value_node,value_node->get_id() + strprintf("foo %d", rand()));
1672 assert(value_node->is_exported());
1673 // replace 'this' with the new valuenode
1674 *iter=synfigapp::ValueDesc(get_canvas(),value_node->get_id());
1675 printf("erasing next\n");
1676 printf("erasing next point\n");
1677 vertex_list.erase(next);
1680 } // this vertex isn't close to the next one
1681 else if (value_prev.parent_is_value_node())
1683 printf("in two - it's far\n");
1684 // \todo this only makes sense if prev is on the same bline
1685 printf("this is index %d\n", value_desc.get_index());
1686 printf("prev is index %d\n", value_prev.get_index());
1687 bool positive_trend(value_desc.get_index()>value_prev.get_index());
1691 printf("positive trend\n");
1692 printf("comparing index %d < link_count()-1 = %d-1 = %d\n",
1693 value_desc.get_index(),
1694 LinkableValueNode::Handle::cast_static(value_desc.get_parent_value_node())->link_count(),
1695 LinkableValueNode::Handle::cast_static(value_desc.get_parent_value_node())->link_count()-1);
1696 if (value_desc.get_index()<LinkableValueNode::Handle::cast_static(value_desc.get_parent_value_node())->link_count()-1)
1698 printf("in two - b\n");
1699 printf("inserting node with index %d\n", value_desc.get_index()+1);
1700 vertex_list.insert(next,
1701 synfigapp::ValueDesc(value_desc.get_parent_value_node(),
1702 value_desc.get_index()+1));
1707 else // !positive_trend
1709 printf("negative trend\n");
1710 if(value_desc.get_index()>0)
1712 printf("in two - a\n");
1713 printf("inserting node on this line with index %d\n",
1714 value_desc.get_index()-1);
1715 vertex_list.insert(next,
1716 synfigapp::ValueDesc(value_desc.get_parent_value_node(),
1717 value_desc.get_index()-1));
1729 if(vertex_list.size()<=2)
1731 synfig::info(__FILE__":%d: Vertex list too small to make region.",__LINE__);
1732 return Smach::RESULT_OK;
1735 debug_show_vertex_list(i, vertex_list, "finished tidying list", -1);
1738 // If we aren't the trivial case,
1739 // then go ahead and create the new
1741 if(!value_node_bline)
1743 synfig::info("not all points are on the same bline");
1744 value_node_bline=ValueNode_BLine::create();
1746 std::list<synfigapp::ValueDesc>::iterator iter;
1747 for(iter=vertex_list.begin();iter!=vertex_list.end();++iter)
1749 // Ensure that the vertex is exported.
1750 get_canvas_interface()->auto_export(*iter);
1752 value_node_bline->add(iter->get_value_node());
1753 //value_node_bline->add(ValueNode_BLine::ListEntry(iter->get_value_node()));
1756 value_node_bline->set_loop(true);
1759 get_canvas_interface()->auto_export(value_node_bline);
1761 // Now we create the region layer
1764 Layer::Handle layer;
1765 Canvas::Handle canvas(get_canvas_view()->get_canvas());
1768 // we are temporarily using the layer to hold something
1769 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
1772 depth=layer->get_depth();
1773 canvas=layer->get_canvas();
1776 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
1778 layer=get_canvas_interface()->add_layer_to("region",canvas,depth);
1780 layer->set_param("color",synfigapp::Main::get_background_color());
1783 layer->set_param("feather",get_feather());
1784 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
1786 get_canvas_interface()->signal_layer_param_changed()(layer,"color");
1788 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
1792 action->set_param("canvas",get_canvas());
1793 action->set_param("canvas_interface",get_canvas_interface());
1794 action->set_param("layer",layer);
1795 if(!action->set_param("param",String("bline")))
1796 synfig::error("LayerParamConnect didn't like \"param\"");
1797 if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
1798 synfig::error("LayerParamConnect didn't like \"value_node\"");
1800 if(!get_canvas_interface()->get_instance()->perform_action(action))
1802 get_canvas_view()->get_ui_interface()->error(_("Unable to create Region layer"));
1804 return Smach::RESULT_ERROR;
1806 get_canvas_view()->get_selection_manager()->set_selected_layer(layer);
1809 return Smach::RESULT_ACCEPT;
1813 StateDraw_Context::refresh_ducks()
1815 get_canvas_view()->queue_rebuild_ducks();
1817 get_work_area()->clear_ducks();
1820 std::list< etl::smart_ptr<std::list<synfig::Point> > >::iterator iter;
1822 for(iter=stroke_list.begin();iter!=stroke_list.end();++iter)
1824 get_work_area()->add_stroke(*iter);
1827 get_work_area()->queue_draw();
1833 StateDraw_Context::extend_bline_from_begin(ValueNode_BLine::Handle value_node,std::list<synfig::BLinePoint> bline,bool complete_loop)
1835 // Create the action group
1836 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Extend BLine"));
1840 synfigapp::Action::Handle action(synfigapp::Action::create("value_node_dynamic_list_loop"));
1843 action->set_param("canvas",get_canvas());
1844 action->set_param("canvas_interface",get_canvas_interface());
1845 action->set_param("value_node",ValueNode::Handle(value_node));
1847 if(!get_canvas_interface()->get_instance()->perform_action(action))
1849 get_canvas_view()->get_ui_interface()->error(_("Unable to set loop for bline"));
1851 return Smach::RESULT_ERROR;
1855 std::list<synfig::BLinePoint>::reverse_iterator iter;
1856 for(iter=bline.rbegin();!(iter==bline.rend());++iter)
1858 ValueNode_Composite::Handle composite(ValueNode_Composite::create(*iter));
1860 synfigapp::Action::Handle action(synfigapp::Action::create("value_node_dynamic_list_insert"));
1863 synfigapp::ValueDesc value_desc(value_node,0);
1865 action->set_param("canvas",get_canvas());
1866 action->set_param("canvas_interface",get_canvas_interface());
1867 action->set_param("value_desc",value_desc);
1868 if(!action->set_param("item",ValueNode::Handle(composite)))
1869 synfig::error("ACTION didn't like \"item\"");
1871 if(!get_canvas_interface()->get_instance()->perform_action(action))
1873 get_canvas_view()->get_ui_interface()->error(_("Unable to insert item"));
1876 return Smach::RESULT_ERROR;
1879 last_stroke=value_node;
1880 return Smach::RESULT_ACCEPT;
1884 StateDraw_Context::extend_bline_from_end(ValueNode_BLine::Handle value_node,std::list<synfig::BLinePoint> bline,bool complete_loop)
1886 // Create the action group
1887 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Extend BLine"));
1891 synfigapp::Action::Handle action(synfigapp::Action::create("value_node_dynamic_list_loop"));
1894 action->set_param("canvas",get_canvas());
1895 action->set_param("canvas_interface",get_canvas_interface());
1896 action->set_param("value_node",ValueNode::Handle(value_node));
1898 if(!get_canvas_interface()->get_instance()->perform_action(action))
1900 get_canvas_view()->get_ui_interface()->error(_("Unable to set loop for bline"));
1902 return Smach::RESULT_ERROR;
1906 std::list<synfig::BLinePoint>::iterator iter;
1907 for(iter=bline.begin();iter!=bline.end();++iter)
1909 ValueNode_Composite::Handle composite(ValueNode_Composite::create(*iter));
1911 synfigapp::Action::Handle action(synfigapp::Action::create("value_node_dynamic_list_insert"));
1914 synfigapp::ValueDesc value_desc(value_node,value_node->link_count());
1916 action->set_param("canvas",get_canvas());
1917 action->set_param("canvas_interface",get_canvas_interface());
1918 action->set_param("value_desc",value_desc);
1919 if(!action->set_param("item",ValueNode::Handle(composite)))
1920 synfig::error("ACTION didn't like \"item\"");
1922 if(!get_canvas_interface()->get_instance()->perform_action(action))
1924 get_canvas_view()->get_ui_interface()->error(_("Unable to insert item"));
1927 return Smach::RESULT_ERROR;
1930 last_stroke=value_node;
1931 return Smach::RESULT_ACCEPT;
1935 StateDraw_Context::reverse_bline(std::list<synfig::BLinePoint> &bline)
1939 std::list<synfig::BLinePoint>::iterator iter,eiter;
1943 for(i=0;i<(int)bline.size()/2;++iter,--eiter,i++)
1945 iter_swap(iter,eiter);
1952 StateDraw_Context::fill_last_stroke_and_unselect_other_layers()
1957 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Fill Stroke"));
1959 Layer::Handle layer;
1961 get_canvas_interface()->auto_export(last_stroke);
1963 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
1965 Canvas::Handle canvas(get_canvas_view()->get_canvas());
1968 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
1971 depth=layer->get_depth();
1972 canvas=layer->get_canvas();
1975 get_canvas_interface()->get_selection_manager()->clear_selected_layers();
1976 layer=get_canvas_interface()->add_layer_to("region", canvas, depth);
1978 layer->set_param("color",synfigapp::Main::get_background_color());
1979 layer->set_description(last_stroke_id + _(" Region"));
1981 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
1985 action->set_param("canvas",get_canvas());
1986 action->set_param("canvas_interface",get_canvas_interface());
1987 action->set_param("layer",layer);
1988 if(!action->set_param("param",String("bline")))
1989 synfig::error("LayerParamConnect didn't like \"param\"");
1990 if(!action->set_param("value_node",ValueNode::Handle(last_stroke)))
1991 synfig::error("LayerParamConnect didn't like \"value_node\"");
1993 if(!get_canvas_interface()->get_instance()->perform_action(action))
1995 get_canvas_view()->get_ui_interface()->error(_("Unable to create Region layer"));
1999 get_canvas_view()->get_selection_manager()->set_selected_layer(layer);
2003 StateDraw_Context::fill_last_stroke()
2008 synfigapp::SelectionManager::LayerList layer_list = get_canvas_view()->get_selection_manager()->get_selected_layers();
2009 fill_last_stroke_and_unselect_other_layers();
2010 get_canvas_view()->get_selection_manager()->set_selected_layers(layer_list);