1 /* === S Y N F I G ========================================================= */
2 /*! \file rotoscope_bline.cpp
3 ** \brief Template File
5 ** $Id: state_draw.cpp,v 1.1.1.1 2005/01/07 03:34:36 darco Exp $
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
10 ** This package is free software; you can redistribute it and/or
11 ** modify it under the terms of the GNU General Public License as
12 ** published by the Free Software Foundation; either version 2 of
13 ** the License, or (at your option) any later version.
15 ** This package is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 ** General Public License for more details.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
32 #include <gtkmm/dialog.h>
33 #include <gtkmm/entry.h>
35 #include <synfig/valuenode_dynamiclist.h>
37 #include "state_draw.h"
38 #include "state_stroke.h"
39 #include "canvasview.h"
42 #include <synfig/valuenode_bline.h>
43 #include <synfig/valuenode_composite.h>
44 #include <ETL/hermite>
45 #include <ETL/calculus>
47 #include "event_mouse.h"
48 #include "event_layerclick.h"
51 #include <synfigapp/blineconvert.h>
52 #include <synfigapp/main.h>
54 #include <ETL/gaussian>
55 #include "dialog_tooloptions.h"
57 #include <gtkmm/table.h>
58 #include <gtkmm/label.h>
59 #include <gtkmm/button.h>
60 #include <gtkmm/checkbutton.h>
61 #include <gtkmm/scale.h>
62 #include <sigc++/connection.h>
66 /* === U S I N G =========================================================== */
70 using namespace synfig;
71 using namespace studio;
73 /* === M A C R O S ========================================================= */
75 /* === G L O B A L S ======================================================= */
77 StateDraw studio::state_draw;
79 /* === C L A S S E S & S T R U C T S ======================================= */
81 class studio::StateDraw_Context : public sigc::trackable
83 typedef etl::smart_ptr<std::list<synfig::Point> > StrokeData;
84 typedef etl::smart_ptr<std::list<synfig::Real> > WidthData;
86 typedef list< pair<StrokeData,WidthData> > StrokeQueue;
88 StrokeQueue stroke_queue;
91 etl::handle<CanvasView> canvas_view_;
92 CanvasView::IsWorking is_working;
94 bool prev_table_status;
96 bool prev_workarea_layer_status_;
99 SigC::Connection process_queue_connection;
101 ValueNode_BLine::Handle last_stroke;
105 //Duckmatic::Push duckmatic_push;
107 std::list< etl::smart_ptr<std::list<synfig::Point> > > stroke_list;
109 void refresh_ducks();
111 Duckmatic::Type old_duckmask;
113 void fill_last_stroke();
115 Smach::event_result new_bline(std::list<synfig::BLinePoint> bline,bool loop_bline_flag,float radius);
117 Smach::event_result new_region(std::list<synfig::BLinePoint> bline,synfig::Real radius);
119 Smach::event_result extend_bline_from_begin(ValueNode_BLine::Handle value_node,std::list<synfig::BLinePoint> bline);
120 Smach::event_result extend_bline_from_end(ValueNode_BLine::Handle value_node,std::list<synfig::BLinePoint> bline);
121 void reverse_bline(std::list<synfig::BLinePoint> &bline);
123 synfigapp::Settings& settings;
125 Gtk::Table options_table;
126 Gtk::CheckButton checkbutton_pressure_width;
127 Gtk::CheckButton checkbutton_round_ends;
128 Gtk::CheckButton checkbutton_auto_loop;
129 Gtk::CheckButton checkbutton_auto_connect;
130 Gtk::CheckButton checkbutton_region_only;
131 Gtk::Button button_fill_last_stroke;
133 //pressure spinner and such
134 Gtk::Adjustment adj_min_pressure;
135 Gtk::SpinButton spin_min_pressure;
136 Gtk::CheckButton check_min_pressure;
138 Gtk::Adjustment adj_feather;
139 Gtk::SpinButton spin_feather;
141 Gtk::Adjustment adj_globalthres;
142 Gtk::SpinButton spin_globalthres;
144 Gtk::Adjustment adj_localthres;
145 Gtk::CheckButton check_localerror;
146 void UpdateErrorBox(); //switches the stuff if need be :)
148 //Added by Adrian - data drive HOOOOO
149 synfigapp::BLineConverter blineconv;
152 bool get_pressure_width_flag()const { return checkbutton_pressure_width.get_active(); }
153 void set_pressure_width_flag(bool x) { return checkbutton_pressure_width.set_active(x); }
155 bool get_auto_loop_flag()const { return checkbutton_auto_loop.get_active(); }
156 void set_auto_loop_flag(bool x) { return checkbutton_auto_loop.set_active(x); }
158 bool get_auto_connect_flag()const { return checkbutton_auto_connect.get_active(); }
159 void set_auto_connect_flag(bool x) { return checkbutton_auto_connect.set_active(x); }
161 bool get_region_only_flag()const { return checkbutton_region_only.get_active(); }
162 void set_region_only_flag(bool x) { return checkbutton_region_only.set_active(x); }
164 Real get_min_pressure() const { return adj_min_pressure.get_value(); }
165 void set_min_pressure(Real x) { return adj_min_pressure.set_value(x); }
167 Real get_feather() const { return adj_feather.get_value(); }
168 void set_feather(Real x) { return adj_feather.set_value(x); }
170 Real get_gthres() const { return adj_globalthres.get_value(); }
171 void set_gthres(Real x) { return adj_globalthres.set_value(x); }
173 Real get_lthres() const { return adj_localthres.get_value(); }
174 void set_lthres(Real x) { return adj_localthres.set_value(x); }
176 bool get_local_error_flag() const { return check_localerror.get_active(); }
177 void set_local_error_flag(bool x) { check_localerror.set_active(x); }
179 bool get_min_pressure_flag()const { return check_min_pressure.get_active(); }
180 void set_min_pressure_flag(bool x) { check_min_pressure.set_active(x); }
182 void load_settings();
183 void save_settings();
185 Smach::event_result event_stop_handler(const Smach::event& x);
187 Smach::event_result event_refresh_handler(const Smach::event& x);
189 Smach::event_result event_mouse_down_handler(const Smach::event& x);
191 Smach::event_result event_stroke(const Smach::event& x);
192 Smach::event_result event_refresh_tool_options(const Smach::event& x);
193 void refresh_tool_options();
195 Smach::event_result process_stroke(StrokeData stroke_data, WidthData width_data, bool region_flag=false);
197 bool process_queue();
200 StateDraw_Context(CanvasView* canvas_view);
202 ~StateDraw_Context();
204 const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
205 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
206 synfig::Time get_time()const { return get_canvas_interface()->get_time(); }
207 synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
208 WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
210 //void on_user_click(synfig::Point point);
213 }; // END of class StateDraw_Context
216 /* === M E T H O D S ======================================================= */
218 StateDraw::StateDraw():
219 Smach::state<StateDraw_Context>("draw")
221 insert(event_def(EVENT_STOP,&StateDraw_Context::event_stop_handler));
222 insert(event_def(EVENT_REFRESH,&StateDraw_Context::event_refresh_handler));
223 insert(event_def(EVENT_WORKAREA_MOUSE_BUTTON_DOWN,&StateDraw_Context::event_mouse_down_handler));
224 insert(event_def(EVENT_WORKAREA_STROKE,&StateDraw_Context::event_stroke));
225 insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateDraw_Context::event_refresh_tool_options));
228 StateDraw::~StateDraw()
234 StateDraw_Context::load_settings()
238 if(settings.get_value("draw.pressure_width",value) && value=="0")
239 set_pressure_width_flag(false);
241 set_pressure_width_flag(true);
243 if(settings.get_value("draw.auto_loop",value) && value=="0")
244 set_auto_loop_flag(false);
246 set_auto_loop_flag(true);
248 if(settings.get_value("draw.auto_connect",value) && value=="0")
249 set_auto_connect_flag(false);
251 set_auto_connect_flag(true);
253 if(settings.get_value("draw.region_only",value) && value=="1")
254 set_region_only_flag(true);
256 set_region_only_flag(false);
258 if(settings.get_value("draw.min_pressure_on",value) && value=="0")
259 set_min_pressure_flag(false);
261 set_min_pressure_flag(true);
263 if(settings.get_value("draw.min_pressure",value))
265 Real n = atof(value.c_str());
270 if(settings.get_value("draw.feather",value))
272 Real n = atof(value.c_str());
277 if(settings.get_value("draw.gthreshold",value))
279 Real n = atof(value.c_str());
283 if(settings.get_value("draw.lthreshold",value))
285 Real n = atof(value.c_str());
289 if(settings.get_value("draw.localize",value) && value == "1")
290 set_local_error_flag(true);
292 set_local_error_flag(false);
296 StateDraw_Context::save_settings()
298 settings.set_value("draw.pressure_width",get_pressure_width_flag()?"1":"0");
299 settings.set_value("draw.auto_loop",get_auto_loop_flag()?"1":"0");
300 settings.set_value("draw.auto_connect",get_auto_connect_flag()?"1":"0");
301 settings.set_value("draw.region_only",get_region_only_flag()?"1":"0");
302 settings.set_value("draw.min_pressure",strprintf("%f",get_min_pressure()));
303 settings.set_value("draw.feather",strprintf("%f",get_feather()));
304 settings.set_value("draw.min_pressure_on",get_min_pressure_flag()?"1":"0");
305 settings.set_value("draw.gthreshold",strprintf("%f",get_gthres()));
306 settings.set_value("draw.lthreshold",strprintf("%f",get_lthres()));
307 settings.set_value("draw.localize",get_local_error_flag()?"1":"0");
310 StateDraw_Context::StateDraw_Context(CanvasView* canvas_view):
311 canvas_view_(canvas_view),
312 is_working(*canvas_view),
314 prev_workarea_layer_status_(get_work_area()->allow_layer_clicks),
315 settings(synfigapp::Main::get_selected_input_device()->settings()),
316 checkbutton_pressure_width(_("Pressure Width")),
317 checkbutton_auto_loop(_("Auto Loop")),
318 checkbutton_auto_connect(_("Auto Connect")),
319 checkbutton_region_only(_("Create Region Only")),
320 button_fill_last_stroke(_("Fill Last Stroke")),
321 adj_min_pressure(0,0,1,0.01,0.1),
322 spin_min_pressure(adj_min_pressure,0.1,3),
323 check_min_pressure(_("Min Pressure")),
324 adj_feather(0,0,10000,0.01,0.1),
325 spin_feather(adj_feather,0.01,4),
326 adj_globalthres(.70f,0.01,10000,0.01,0.1),
327 spin_globalthres(adj_globalthres,0.01,3),
328 adj_localthres(20,1,100000,0.1,1),
329 check_localerror(_("LocalError"))
332 synfig::info("STATE SKETCH: entering state");
339 //options_table.attach(*manage(new Gtk::Label(_("Draw Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
340 options_table.attach(checkbutton_pressure_width, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
341 options_table.attach(checkbutton_auto_loop, 0, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
342 options_table.attach(checkbutton_auto_connect, 0, 2, 3, 4, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
343 options_table.attach(checkbutton_region_only, 0, 2, 4, 5, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
345 options_table.attach(check_min_pressure, 0, 2, 5, 6, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
346 options_table.attach(spin_min_pressure, 0, 2, 6, 7, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
348 options_table.attach(*manage(new Gtk::Label(_("Feather"))), 0, 1, 7, 8, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
349 options_table.attach(spin_feather, 1, 2, 7, 8, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
351 options_table.attach(check_localerror, 0, 2, 8, 9, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
352 options_table.attach(*manage(new Gtk::Label(_("Smooth"))), 0, 1, 9, 10, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
353 options_table.attach(spin_globalthres, 1, 2, 9, 10, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
355 //options_table.attach(button_fill_last_stroke, 0, 2, 10, 11, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
357 button_fill_last_stroke.signal_pressed().connect(sigc::mem_fun(*this,&StateDraw_Context::fill_last_stroke));
358 check_localerror.signal_toggled().connect(sigc::mem_fun(*this,&StateDraw_Context::UpdateErrorBox));
360 options_table.show_all();
361 refresh_tool_options();
362 //App::dialog_tool_options->set_widget(options_table);
363 App::dialog_tool_options->present();
366 old_duckmask=get_work_area()->get_type_mask();
367 get_work_area()->set_type_mask(Duck::TYPE_ALL-Duck::TYPE_TANGENT-Duck::TYPE_WIDTH);
369 // Turn off layer clicking
370 get_work_area()->allow_layer_clicks=false;
372 // Turn off duck clicking
373 get_work_area()->allow_duck_clicks=false;
375 // clear out the ducks
376 //get_work_area()->clear_ducks();
378 // Refresh the work area
379 //get_work_area()->queue_draw();
381 // Hide the tables if they are showing
382 prev_table_status=get_canvas_view()->tables_are_visible();
383 //if(prev_table_status)get_canvas_view()->hide_tables();
386 get_canvas_view()->hide_timebar();
389 //get_work_area()->signal_user_click().connect(sigc::mem_fun(*this,&studio::StateDraw_Context::on_user_click));
391 get_canvas_view()->work_area->set_cursor(Gdk::PENCIL);
393 App::toolbox->refresh();
399 void StateDraw_Context::UpdateErrorBox()
401 if(get_local_error_flag())
403 spin_globalthres.set_adjustment(adj_localthres);
404 spin_globalthres.set_value(adj_localthres.get_value());
405 spin_globalthres.set_increments(0.1,1);
408 spin_globalthres.set_adjustment(adj_globalthres);
409 spin_globalthres.set_value(adj_globalthres.get_value());
410 spin_globalthres.set_increments(0.01,.1);
413 spin_globalthres.update();
417 StateDraw_Context::refresh_tool_options()
419 App::dialog_tool_options->clear();
420 App::dialog_tool_options->set_widget(options_table);
421 App::dialog_tool_options->set_local_name(_("Draw Tool"));
422 App::dialog_tool_options->set_name("draw");
424 App::dialog_tool_options->add_button(
425 Gtk::StockID("synfig-fill"),
426 _("Fill Last Stroke")
427 )->signal_clicked().connect(
430 &StateDraw_Context::fill_last_stroke
437 StateDraw_Context::event_refresh_tool_options(const Smach::event& x)
439 refresh_tool_options();
440 return Smach::RESULT_ACCEPT;
443 StateDraw_Context::~StateDraw_Context()
447 App::dialog_tool_options->clear();
449 get_work_area()->set_type_mask(old_duckmask);
451 get_canvas_view()->work_area->reset_cursor();
453 // Restore layer clicking
454 get_work_area()->allow_layer_clicks=prev_workarea_layer_status_;
456 // Restore duck clicking
457 get_work_area()->allow_duck_clicks=true;
460 if(get_canvas_view()->get_canvas()->rend_desc().get_time_start()!=get_canvas_view()->get_canvas()->rend_desc().get_time_end())
461 get_canvas_view()->show_timebar();
463 // Bring back the tables if they were out before
464 if(prev_table_status)get_canvas_view()->show_tables();
466 // Refresh the work area
467 get_work_area()->queue_draw();
469 App::toolbox->refresh();
473 StateDraw_Context::event_stop_handler(const Smach::event& x)
475 throw Smach::egress_exception();
479 StateDraw_Context::event_refresh_handler(const Smach::event& x)
482 return Smach::RESULT_ACCEPT;
486 StateDraw_Context::event_mouse_down_handler(const Smach::event& x)
488 const EventMouse& event(*reinterpret_cast<const EventMouse*>(&x));
493 // Enter the stroke state to get the stroke
494 get_canvas_view()->get_smach().push_state(&state_stroke);
495 return Smach::RESULT_ACCEPT;
498 case BUTTON_RIGHT: // Intercept the right-button click to short-circut the pop-up menu
499 return Smach::RESULT_ACCEPT;
502 return Smach::RESULT_OK;
506 #define SIMILAR_TANGENT_THRESHOLD (0.2)
511 debugclass(const synfig::String &x):x(x)
513 // synfig::warning(">>>>>>>>>>>>>>>>>>> "+x);
517 // synfig::warning("<<<<<<<<<<<<<<<<<<< "+x);
524 DepthCounter(int &i):i(i) { i++; }
525 ~DepthCounter() { i--; }
529 StateDraw_Context::event_stroke(const Smach::event& x)
531 // debugclass debugger("StateDraw_Context::event_stroke(const Smach::event& x)");
533 const EventStroke& event(*reinterpret_cast<const EventStroke*>(&x));
535 assert(event.stroke_data);
537 get_work_area()->add_stroke(event.stroke_data,synfigapp::Main::get_foreground_color());
541 DirtyTrap dirty_trap(get_work_area());
542 Smach::event_result result;
543 result=process_stroke(event.stroke_data,event.width_data,event.modifier&Gdk::CONTROL_MASK || event.modifier&Gdk::BUTTON2_MASK);
548 stroke_queue.push_back(pair<StrokeData,WidthData>(event.stroke_data,event.width_data));
550 return Smach::RESULT_ACCEPT;
554 StateDraw_Context::process_queue()
556 // debugclass debugger("StateDraw_Context::process_queue()");
559 DepthCounter depth_counter(nested);
560 while(!stroke_queue.empty())
562 pair<StrokeData,WidthData> front(stroke_queue.front());
563 process_stroke(front.first,front.second);
564 stroke_queue.pop_front();
570 StateDraw_Context::process_stroke(StrokeData stroke_data, WidthData width_data, bool region_flag)
572 // debugclass debugger("StateDraw_Context::process_stroke");
573 DepthCounter depth_counter(nested);
575 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);
578 // If we aren't using pressure width,
579 // then set all the width to 1
580 if(!get_pressure_width_flag())
582 std::list<synfig::Real>::iterator iter;
583 for(iter=width_data->begin();iter!=width_data->end();++iter)
589 //get_work_area()->add_stroke(event.stroke_data,synfigapp::Main::get_foreground_color());
590 //stroke_list.push_back(event.stroke_data);
593 std::list<synfig::BLinePoint> bline;
594 bool loop_bline_flag(false);
596 //Changed by Adrian - use resident class :)
597 //synfigapp::convert_stroke_to_bline(bline, *event.stroke_data,*event.width_data, synfigapp::Main::get_bline_width());
598 blineconv.width = synfigapp::Main::get_bline_width().units(get_canvas()->rend_desc());
600 if(get_local_error_flag())
602 float pw = get_work_area()->get_pw();
603 float ph = get_work_area()->get_ph();
605 blineconv.pixelwidth = sqrt(pw*pw+ph*ph);
606 blineconv.smoothness = get_lthres();
609 blineconv.pixelwidth = 1;
610 blineconv.smoothness = get_gthres();
613 blineconv(bline,*stroke_data,*width_data);
615 //Postprocess to require minimum pressure
616 if(get_min_pressure_flag())
618 synfigapp::BLineConverter::EnforceMinWidth(bline,get_min_pressure());
621 // If the start and end points are similar, then make then the same point
622 if(get_auto_loop_flag())
623 if(bline.size()>2&&(bline.front().get_vertex()-bline.back().get_vertex()).mag()<=radius)
625 loop_bline_flag=true;
629 while(bline.size()>2&&(bline.front().get_vertex()-bline.back().get_vertex()).mag()<=radius)
631 tangent=bline.back().get_tangent1();
632 width=bline.back().get_width();
636 if(abs(bline.front().get_tangent1().norm()*tangent.norm().perp())>SIMILAR_TANGENT_THRESHOLD)
638 // If the tangents are not similar, then
639 // split the tangents
640 bline.front().set_split_tangent_flag(true);
641 bline.front().set_tangent1(tangent);
645 // If the tangents are similar, then set the tangent
646 // to the average of the two
647 bline.front().set_tangent((tangent+bline.front().get_tangent1())*0.5f);
650 // Add the widths of the two points
652 Real width(bline.front().get_width()+width);
653 width=width<=1?width:1;
654 bline.front().set_width(width);
658 // If the bline only has once blinepoint, then there is nothing to do.
660 return Smach::RESULT_OK;
663 return new_region(bline,radius);
665 return new_bline(bline,loop_bline_flag,radius);
669 StateDraw_Context::new_bline(std::list<synfig::BLinePoint> bline,bool loop_bline_flag,float radius)
671 // Create the action group
672 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Sketch BLine"));
674 //ValueNode_BLine::Handle value_node(ValueNode_BLine::create(synfig::ValueBase(bline,loop_bline_flag)));
675 ValueNode_BLine::Handle value_node;
678 std::list<synfig::BLinePoint> trans_bline;
679 std::list<synfig::BLinePoint>::iterator iter;
680 const synfig::TransformStack& transform(get_canvas_view()->get_curr_transform_stack());
682 for(iter=bline.begin();iter!=bline.end();++iter)
684 BLinePoint bline_point(*iter);
685 Point new_vertex(transform.unperform(bline_point.get_vertex()));
687 bline_point.set_tangent1(
689 bline_point.get_tangent1()+bline_point.get_vertex()
693 bline_point.set_tangent2(
695 bline_point.get_tangent2()+bline_point.get_vertex()
699 bline_point.set_vertex(new_vertex);
701 trans_bline.push_back(bline_point);
703 value_node=ValueNode_BLine::create(synfig::ValueBase(trans_bline,loop_bline_flag));
706 // Find any ducks at the start or end that we might attach to
707 // (If we aren't a loop)
708 if(!loop_bline_flag && get_auto_connect_flag())
711 etl::handle<Duck> start_duck(get_work_area()->find_duck(bline.front().get_vertex(),radius,Duck::TYPE_VERTEX));
712 etl::handle<Duck> finish_duck(get_work_area()->find_duck(bline.back().get_vertex(),radius,Duck::TYPE_VERTEX));
716 synfigapp::ValueDesc value_desc(start_duck->get_value_desc());
722 ValueNode_BLine::Handle value_node_bline;
724 if(value_desc.parent_is_value_node())
726 value_node_bline=ValueNode_BLine::Handle::cast_dynamic(value_desc.get_parent_value_node());
730 if(value_desc.get_index()==0)
732 // SPECIAL CASE -- EXTENSION
733 // We need to reverse the BLine first.
735 reverse_bline(bline);
736 return extend_bline_from_begin(value_node_bline,bline);
738 if(value_desc.get_index()==value_node_bline->link_count()-1)
740 // SPECIAL CASE -- EXTENSION
742 return extend_bline_from_end(value_node_bline,bline);
746 switch(value_desc.get_value_type())
748 case synfig::ValueBase::TYPE_BLINEPOINT:
749 //get_canvas_interface()->auto_export(value_desc);
750 //value_node->list.front().value_node=value_desc.get_value_node();
752 value_desc=synfigapp::ValueDesc(LinkableValueNode::Handle::cast_dynamic(value_desc.get_value_node()),0);
754 case synfig::ValueBase::TYPE_VECTOR:
755 get_canvas_interface()->auto_export(value_desc);
756 LinkableValueNode::Handle::cast_dynamic(value_node->list.front().value_node)->set_link(0,value_desc.get_value_node());
765 synfigapp::ValueDesc value_desc(finish_duck->get_value_desc());
771 ValueNode_BLine::Handle value_node_bline;
773 if(value_desc.parent_is_value_node())
774 value_node_bline=ValueNode_BLine::Handle::cast_dynamic(value_desc.get_parent_value_node());
777 if(value_desc.get_index()==0)
779 // SPECIAL CASE -- EXTENSION
781 return extend_bline_from_begin(value_node_bline,bline);
783 if(value_desc.get_index()==value_node_bline->link_count()-1)
785 // SPECIAL CASE -- EXTENSION
786 // We need to reverse the BLine first.
788 reverse_bline(bline);
789 return extend_bline_from_end(value_node_bline,bline);
793 switch(value_desc.get_value_type())
795 case synfig::ValueBase::TYPE_BLINEPOINT:
796 //get_canvas_interface()->auto_export(value_desc);
797 //value_node->list.back().value_node=value_desc.get_value_node();
799 value_desc=synfigapp::ValueDesc(LinkableValueNode::Handle::cast_dynamic(value_desc.get_value_node()),0);
801 case synfig::ValueBase::TYPE_VECTOR:
802 get_canvas_interface()->auto_export(value_desc);
803 LinkableValueNode::Handle::cast_dynamic(value_node->list.back().value_node)->set_link(0,value_desc.get_value_node());
815 Canvas::Handle canvas(get_canvas_view()->get_canvas());
818 // we are temporarily using the layer to hold something
819 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
822 depth=layer->get_depth();
823 canvas=layer->get_canvas();
826 //int number(synfig::UniqueID().get_uid());
828 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
830 if(get_region_only_flag())
831 layer=get_canvas_interface()->add_layer_to("region",canvas,depth);
833 layer=get_canvas_interface()->add_layer_to("outline",canvas,depth);
837 layer->set_param("feather",get_feather());
838 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
841 //layer->set_description(strprintf("Stroke %d",number));
842 //get_canvas_interface()->signal_layer_new_description()(layer,layer->get_description());
846 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
850 action->set_param("canvas",get_canvas());
851 action->set_param("canvas_interface",get_canvas_interface());
852 action->set_param("layer",layer);
853 if(!action->set_param("param",String("bline")))
854 synfig::error("LayerParamConnect didn't like \"param\"");
855 if(!action->set_param("value_node",ValueNode::Handle(value_node)))
856 synfig::error("LayerParamConnect didn't like \"value_node\"");
858 if(!get_canvas_interface()->get_instance()->perform_action(action))
860 get_canvas_view()->get_ui_interface()->error(_("Unable to create layer"));
863 return Smach::RESULT_ERROR;
865 get_canvas_view()->get_selection_manager()->set_selected_layer(layer);
869 last_stroke=value_node;
870 return Smach::RESULT_ACCEPT;
874 StateDraw_Context::new_region(std::list<synfig::BLinePoint> bline, synfig::Real radius)
876 // Create the action group
877 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Define Region"));
879 std::list<synfigapp::ValueDesc> vertex_list;
881 // First we need to come up with a rough list of
882 // BLinePoints that we are going to be using to
883 // define our region.
885 std::list<synfig::BLinePoint>::iterator iter;
886 for(iter=bline.begin();iter!=bline.end();++iter)
888 etl::handle<Duck> duck(get_work_area()->find_duck(iter->get_vertex(),0,Duck::TYPE_VERTEX));
892 synfig::info(__FILE__":%d: Nothing to enclose!",__LINE__);
893 return Smach::RESULT_OK;
897 assert(duck->get_type()==Duck::TYPE_VERTEX);
899 synfigapp::ValueDesc value_desc(duck->get_value_desc());
903 synfig::info(__FILE__":%d: Got a hit, but no ValueDesc on this duck",__LINE__);
907 switch(value_desc.get_value_type())
909 case synfig::ValueBase::TYPE_BLINEPOINT:
910 //if(vertex_list.empty() || value_desc!=vertex_list.back())
911 vertex_list.push_back(value_desc);
912 assert(vertex_list.back().is_valid());
921 if(vertex_list.size()<=2)
923 synfig::info(__FILE__":%d: Vertex list too small to make region.",__LINE__);
924 return Smach::RESULT_OK;
927 assert(vertex_list.back().is_valid());
929 // Remove any duplicates
933 // Now we need to clean the list of vertices up
934 // a bit. This includes inserting missing vertices
935 // and removing extraneous ones.
936 // We can do this in multiple passes.
938 for(bool done=false;!done && i<30;i++)
940 // Set done to "true" for now. If
941 // any updates are performed, we will
942 // change it back to false.
945 std::list<synfigapp::ValueDesc>::iterator prev,iter,next;
946 prev=vertex_list.end();prev--; // Set prev to the last ValueDesc
947 next=vertex_list.begin();
948 iter=next++; // Set iter to the first value desc, and next to the second
950 for(;iter!=vertex_list.end();prev=iter,iter=next++)
952 synfigapp::ValueDesc value_prev(*prev);
953 synfigapp::ValueDesc value_desc(*iter);
954 synfigapp::ValueDesc value_next((next==vertex_list.end())?vertex_list.front():*next);
956 assert(value_desc.is_valid());
957 assert(value_next.is_valid());
958 assert(value_prev.is_valid());
960 //synfig::info("-------");
961 //synfig::info(__FILE__":%d: value_prev 0x%08X:%d",__LINE__,value_prev.get_parent_value_node().get(),value_prev.get_index());
962 //synfig::info(__FILE__":%d: value_desc 0x%08X:%d",__LINE__,value_desc.get_parent_value_node().get(),value_desc.get_index());
963 //synfig::info(__FILE__":%d: value_next 0x%08X:%d",__LINE__,value_next.get_parent_value_node().get(),value_next.get_index());
966 if(value_prev.parent_is_value_node() && value_desc.parent_is_value_node() && value_next.parent_is_value_node())
968 // Remove random extraneous vertices
969 if(value_prev.get_parent_value_node()==value_next.get_parent_value_node() &&
970 value_prev.get_parent_value_node()!=value_desc.get_parent_value_node())
973 vertex_list.erase(iter);
980 // Remove duplicate vertices
981 if(value_prev.get_value_node()==value_desc.get_value_node()
982 || value_desc.get_value_node()==value_next.get_value_node())
985 vertex_list.erase(iter);
989 if(value_prev.get_value_node()==value_next.get_value_node())
992 vertex_list.erase(prev);
997 if(value_desc.parent_is_value_node() && value_next.parent_is_value_node())
998 if(value_desc.get_parent_value_node()==value_next.get_parent_value_node() && (next!=vertex_list.end()))
1000 // Fill in missing vertices
1001 if(value_desc.get_index()<value_next.get_index()-1)
1004 vertex_list.insert(next,synfigapp::ValueDesc(value_desc.get_parent_value_node(),value_desc.get_index()+1));
1008 if(value_next.get_index()<value_desc.get_index()-1)
1011 vertex_list.insert(next,synfigapp::ValueDesc(value_desc.get_parent_value_node(),value_next.get_index()+1));
1017 // Ensure that connections
1018 // between blines are properly
1020 if(value_desc.parent_is_value_node() && value_next.parent_is_value_node())
1021 if(value_desc.get_parent_value_node()!=value_next.get_parent_value_node() &&
1022 value_desc.get_value_node()!=value_next.get_value_node())
1024 BLinePoint vertex(value_desc.get_value(get_time()).get(BLinePoint()));
1025 BLinePoint vertex_next(value_next.get_value(get_time()).get(BLinePoint()));
1027 //synfig::info("--------");
1028 //synfig::info(__FILE__":%d: vertex: [%f, %f]",__LINE__,vertex.get_vertex()[0],vertex.get_vertex()[1]);
1029 //synfig::info(__FILE__":%d: vertex_next: [%f, %f]",__LINE__,vertex_next.get_vertex()[0],vertex_next.get_vertex()[1]);
1031 if((vertex.get_vertex()-vertex_next.get_vertex()).mag_squared()<radius*radius)
1034 ValueNode_Composite::Handle value_node;
1035 ValueNode_Composite::Handle value_node_next;
1036 value_node=ValueNode_Composite::Handle::cast_dynamic(value_desc.get_value_node().clone());
1037 value_node_next=ValueNode_Composite::Handle::cast_dynamic(value_next.get_value_node().clone());
1038 if(!value_node || !value_node_next)
1040 synfig::info(__FILE__":%d: Unable to properly connect blines.",__LINE__);
1044 value_node->set_link(5,value_node_next->get_link(5));
1045 value_node->set_link(3,ValueNode_Const::create(true));
1047 get_canvas_interface()->auto_export(value_node);
1048 assert(value_node->is_exported());
1049 *iter=synfigapp::ValueDesc(get_canvas(),value_node->get_id());
1050 vertex_list.erase(next);
1057 bool positive_trend(value_desc.get_index()>value_prev.get_index());
1059 if(!positive_trend && value_desc.get_index()>0)
1062 vertex_list.insert(next,synfigapp::ValueDesc(value_desc.get_parent_value_node(),value_desc.get_index()-1));
1066 if(positive_trend && value_desc.get_index()<LinkableValueNode::Handle::cast_static(value_desc.get_value_node())->link_count()-1)
1069 vertex_list.insert(next,synfigapp::ValueDesc(value_desc.get_parent_value_node(),value_desc.get_index()+1));
1079 if(vertex_list.size()<=2)
1081 synfig::info(__FILE__":%d: Vertex list too small to make region.",__LINE__);
1082 return Smach::RESULT_OK;
1085 ValueNode_BLine::Handle value_node_bline;
1087 // Now we need to test for the trivial case,
1088 // which is where all of the vertices
1089 // come from one BLine.
1090 if(vertex_list.front().parent_is_linkable_value_node())
1092 bool trivial_case(true);
1093 ValueNode::Handle trivial_case_value_node;
1095 trivial_case_value_node=vertex_list.front().get_parent_value_node();
1097 std::list<synfigapp::ValueDesc>::iterator iter;
1098 for(iter=vertex_list.begin();iter!=vertex_list.end();++iter)
1100 if(trivial_case_value_node!=iter->get_parent_value_node())
1107 value_node_bline=ValueNode_BLine::Handle::cast_dynamic(trivial_case_value_node);
1110 // If we aren't the trivial case,
1111 // then go ahead and create the new
1113 if(!value_node_bline)
1115 value_node_bline=ValueNode_BLine::create();
1117 std::list<synfigapp::ValueDesc>::iterator iter;
1118 for(iter=vertex_list.begin();iter!=vertex_list.end();++iter)
1120 // Ensure that the vertex is exported.
1121 get_canvas_interface()->auto_export(*iter);
1123 value_node_bline->add(iter->get_value_node());
1124 //value_node_bline->add(ValueNode_BLine::ListEntry(iter->get_value_node()));
1127 value_node_bline->set_loop(true);
1130 get_canvas_interface()->auto_export(value_node_bline);
1132 // Now we create the region layer
1135 Layer::Handle layer;
1136 Canvas::Handle canvas(get_canvas_view()->get_canvas());
1139 // we are temporarily using the layer to hold something
1140 layer=get_canvas_view()->get_selection_manager()->get_selected_layer();
1143 depth=layer->get_depth();
1144 canvas=layer->get_canvas();
1147 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
1149 layer=get_canvas_interface()->add_layer_to("region",canvas,depth);
1151 layer->set_param("color",synfigapp::Main::get_background_color());
1154 layer->set_param("feather",get_feather());
1155 get_canvas_interface()->signal_layer_param_changed()(layer,"feather");
1157 get_canvas_interface()->signal_layer_param_changed()(layer,"color");
1159 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
1163 action->set_param("canvas",get_canvas());
1164 action->set_param("canvas_interface",get_canvas_interface());
1165 action->set_param("layer",layer);
1166 if(!action->set_param("param",String("bline")))
1167 synfig::error("LayerParamConnect didn't like \"param\"");
1168 if(!action->set_param("value_node",ValueNode::Handle(value_node_bline)))
1169 synfig::error("LayerParamConnect didn't like \"value_node\"");
1171 if(!get_canvas_interface()->get_instance()->perform_action(action))
1173 get_canvas_view()->get_ui_interface()->error(_("Unable to create Region layer"));
1175 return Smach::RESULT_ERROR;
1177 get_canvas_view()->get_selection_manager()->set_selected_layer(layer);
1180 return Smach::RESULT_ACCEPT;
1184 StateDraw_Context::refresh_ducks()
1186 get_canvas_view()->queue_rebuild_ducks();
1188 get_work_area()->clear_ducks();
1191 std::list< etl::smart_ptr<std::list<synfig::Point> > >::iterator iter;
1193 for(iter=stroke_list.begin();iter!=stroke_list.end();++iter)
1195 get_work_area()->add_stroke(*iter);
1198 get_work_area()->queue_draw();
1204 StateDraw_Context::extend_bline_from_begin(ValueNode_BLine::Handle value_node,std::list<synfig::BLinePoint> bline)
1206 // Create the action group
1207 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Extend BLine"));
1209 std::list<synfig::BLinePoint>::reverse_iterator iter;
1210 iter=bline.rbegin();
1211 for(;!(iter==bline.rend());++iter)
1214 ValueNode_Composite::Handle composite(ValueNode_Composite::create(*iter));
1216 synfigapp::Action::Handle action(synfigapp::Action::create("value_node_dynamic_list_insert"));
1219 synfigapp::ValueDesc value_desc(value_node,0);
1221 action->set_param("canvas",get_canvas());
1222 action->set_param("canvas_interface",get_canvas_interface());
1223 action->set_param("value_desc",value_desc);
1224 if(!action->set_param("item",ValueNode::Handle(composite)))
1225 synfig::error("ACTION didn't like \"item\"");
1227 if(!get_canvas_interface()->get_instance()->perform_action(action))
1229 get_canvas_view()->get_ui_interface()->error(_("Unable to insert item"));
1232 return Smach::RESULT_ERROR;
1235 last_stroke=value_node;
1236 return Smach::RESULT_ACCEPT;
1240 StateDraw_Context::extend_bline_from_end(ValueNode_BLine::Handle value_node,std::list<synfig::BLinePoint> bline)
1242 // Create the action group
1243 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Extend BLine"));
1245 std::list<synfig::BLinePoint>::iterator iter;
1247 for(;iter!=bline.end();++iter)
1249 ValueNode_Composite::Handle composite(ValueNode_Composite::create(*iter));
1251 synfigapp::Action::Handle action(synfigapp::Action::create("value_node_dynamic_list_insert"));
1254 synfigapp::ValueDesc value_desc(value_node,value_node->link_count());
1256 action->set_param("canvas",get_canvas());
1257 action->set_param("canvas_interface",get_canvas_interface());
1258 action->set_param("value_desc",value_desc);
1259 if(!action->set_param("item",ValueNode::Handle(composite)))
1260 synfig::error("ACTION didn't like \"item\"");
1262 if(!get_canvas_interface()->get_instance()->perform_action(action))
1264 get_canvas_view()->get_ui_interface()->error(_("Unable to insert item"));
1267 return Smach::RESULT_ERROR;
1270 last_stroke=value_node;
1271 return Smach::RESULT_ACCEPT;
1275 StateDraw_Context::reverse_bline(std::list<synfig::BLinePoint> &bline)
1279 std::list<synfig::BLinePoint>::iterator iter,eiter;
1283 for(i=0;i<(int)bline.size()/2;++iter,--eiter,i++)
1285 iter_swap(iter,eiter);
1292 StateDraw_Context::fill_last_stroke()
1297 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Fill Stroke"));
1299 Layer::Handle layer;
1301 get_canvas_interface()->auto_export(last_stroke);
1303 synfigapp::PushMode push_mode(get_canvas_interface(),synfigapp::MODE_NORMAL);
1305 layer=get_canvas_interface()->add_layer("region");
1307 layer->set_param("color",synfigapp::Main::get_background_color());
1309 synfigapp::Action::Handle action(synfigapp::Action::create("layer_param_connect"));
1313 action->set_param("canvas",get_canvas());
1314 action->set_param("canvas_interface",get_canvas_interface());
1315 action->set_param("layer",layer);
1316 if(!action->set_param("param",String("segment_list")))
1317 synfig::error("LayerParamConnect didn't like \"param\"");
1318 if(!action->set_param("value_node",ValueNode::Handle(last_stroke)))
1319 synfig::error("LayerParamConnect didn't like \"value_node\"");
1321 if(!get_canvas_interface()->get_instance()->perform_action(action))
1323 get_canvas_view()->get_ui_interface()->error(_("Unable to create Region layer"));
1327 get_canvas_view()->get_selection_manager()->set_selected_layer(layer);