1 /* === S Y N F I G ========================================================= */
2 /*! \file state_smoothmove.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2008 Chris Moore
10 ** Copyright (c) 2009 Nikita Kitaev
12 ** This package is free software; you can redistribute it and/or
13 ** modify it under the terms of the GNU General Public License as
14 ** published by the Free Software Foundation; either version 2 of
15 ** the License, or (at your option) any later version.
17 ** This package is distributed in the hope that it will be useful,
18 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ** General Public License for more details.
23 /* ========================================================================= */
25 /* === H E A D E R S ======================================================= */
34 #include <gtkmm/dialog.h>
35 #include <gtkmm/entry.h>
37 #include <synfig/valuenode_blinecalcvertex.h>
38 #include <synfig/valuenode_composite.h>
39 #include <synfig/valuenode_dynamiclist.h>
40 #include <synfigapp/action_system.h>
42 #include "state_smoothmove.h"
43 #include "state_normal.h"
44 #include "canvasview.h"
48 #include <synfigapp/action.h>
49 #include "event_mouse.h"
50 #include "event_layerclick.h"
52 #include "docks/dialog_tooloptions.h"
53 #include <gtkmm/optionmenu.h>
55 #include "onemoment.h"
56 #include <synfigapp/main.h>
62 /* === U S I N G =========================================================== */
66 using namespace synfig;
67 using namespace studio;
69 /* === M A C R O S ========================================================= */
71 /* === G L O B A L S ======================================================= */
73 StateSmoothMove studio::state_smooth_move;
75 /* === C L A S S E S & S T R U C T S ======================================= */
77 class DuckDrag_SmoothMove : public DuckDrag_Base
81 synfig::Vector last_translate_;
82 synfig::Vector drag_offset_;
85 std::vector<synfig::Vector> last_;
86 std::vector<synfig::Vector> positions;
89 DuckDrag_SmoothMove();
90 void begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& begin);
91 bool end_duck_drag(Duckmatic* duckmatic);
92 void duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector);
94 void set_radius(float x) { radius=x; }
95 float get_radius()const { return radius; }
99 class studio::StateSmoothMove_Context : public sigc::trackable
101 etl::handle<CanvasView> canvas_view_;
102 CanvasView::IsWorking is_working;
104 //Duckmatic::Push duckmatic_push;
106 synfigapp::Settings& settings;
108 etl::handle<DuckDrag_SmoothMove> duck_dragger_;
110 Gtk::Table options_table;
112 Gtk::Adjustment adj_radius;
113 Gtk::SpinButton spin_radius;
118 float get_radius()const { return adj_radius.get_value(); }
119 void set_radius(float x) { return adj_radius.set_value(x); }
121 void refresh_radius() { duck_dragger_->set_radius(get_radius()*pressure); }
123 Smach::event_result event_stop_handler(const Smach::event& x);
125 Smach::event_result event_refresh_tool_options(const Smach::event& x);
127 void refresh_tool_options();
129 StateSmoothMove_Context(CanvasView* canvas_view);
131 ~StateSmoothMove_Context();
133 const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
134 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
135 synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
136 WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
138 void load_settings();
139 void save_settings();
140 }; // END of class StateSmoothMove_Context
142 /* === M E T H O D S ======================================================= */
144 StateSmoothMove::StateSmoothMove():
145 Smach::state<StateSmoothMove_Context>("smooth_move")
147 insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateSmoothMove_Context::event_refresh_tool_options));
148 insert(event_def(EVENT_STOP,&StateSmoothMove_Context::event_stop_handler));
151 StateSmoothMove::~StateSmoothMove()
156 StateSmoothMove_Context::load_settings()
160 SETTINGS_LOCALE_SAFE_AND_BACKUP
163 if(settings.get_value("smooth_move.radius",value))
164 set_radius(atof(value.c_str()));
167 SETTINGS_LOCALE_RESTORE
171 synfig::warning("State SmothMove: Caught exception when attempting to load settings.");
176 StateSmoothMove_Context::save_settings()
180 SETTINGS_LOCALE_SAFE_AND_BACKUP
181 settings.set_value("smooth_move.radius",strprintf("%f",get_radius()));
182 SETTINGS_LOCALE_RESTORE
186 synfig::warning("State SmoothMove: Caught exception when attempting to save settings.");
190 StateSmoothMove_Context::StateSmoothMove_Context(CanvasView* canvas_view):
191 canvas_view_(canvas_view),
192 is_working(*canvas_view),
193 // duckmatic_push(get_work_area()),
194 settings(synfigapp::Main::get_selected_input_device()->settings()),
195 duck_dragger_(new DuckDrag_SmoothMove()),
196 adj_radius(1,0,100000,0.01,0.1),
197 spin_radius(adj_radius,0.1,3)
201 // Set up the tool options dialog
202 options_table.attach(*manage(new Gtk::Label(_("SmoothMove Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
203 options_table.attach(*manage(new Gtk::Label(_("Radius"))), 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
204 options_table.attach(spin_radius, 0, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
206 spin_radius.signal_value_changed().connect(sigc::mem_fun(*this,&StateSmoothMove_Context::refresh_radius));
208 options_table.show_all();
209 refresh_tool_options();
210 //App::dialog_tool_options->set_widget(options_table);
211 App::dialog_tool_options->present();
213 get_work_area()->set_allow_layer_clicks(true);
214 get_work_area()->set_duck_dragger(duck_dragger_);
216 App::toolbox->refresh();
218 get_work_area()->set_cursor(Gdk::FLEUR);
219 //get_work_area()->reset_cursor();
225 StateSmoothMove_Context::refresh_tool_options()
227 App::dialog_tool_options->clear();
228 App::dialog_tool_options->set_widget(options_table);
229 App::dialog_tool_options->set_local_name(_("Smooth Move"));
230 App::dialog_tool_options->set_name("smooth_move");
234 StateSmoothMove_Context::event_refresh_tool_options(const Smach::event& /*x*/)
236 refresh_tool_options();
237 return Smach::RESULT_ACCEPT;
241 StateSmoothMove_Context::event_stop_handler(const Smach::event& /*x*/)
244 return Smach::RESULT_OK;
247 StateSmoothMove_Context::~StateSmoothMove_Context()
251 get_work_area()->clear_duck_dragger();
252 get_work_area()->reset_cursor();
254 App::dialog_tool_options->clear();
256 App::toolbox->refresh();
262 DuckDrag_SmoothMove::DuckDrag_SmoothMove():radius(1.0f)
267 DuckDrag_SmoothMove::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& offset)
269 last_translate_=Vector(0,0);
270 drag_offset_=duckmatic->find_duck(offset)->get_trans_point();
272 //snap=drag_offset-duckmatic->snap_point_to_grid(drag_offset);
273 //snap=offset-drag_offset_;
278 const DuckList selected_ducks(duckmatic->get_selected_ducks());
279 DuckList::const_iterator iter;
281 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
283 last_.push_back(Vector(0,0));
284 positions.push_back((*iter)->get_trans_point());
289 DuckDrag_SmoothMove::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
291 const DuckList selected_ducks(duckmatic->get_selected_ducks());
292 DuckList::const_iterator iter;
293 synfig::Vector vect(duckmatic->snap_point_to_grid(vector)-drag_offset_+snap);
297 Time time(duckmatic->get_time());
299 // process vertex and position ducks first
300 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
302 // skip this duck if it is NOT a vertex or a position
303 if (((*iter)->get_type() != Duck::TYPE_VERTEX &&
304 (*iter)->get_type() != Duck::TYPE_POSITION))
306 Point p(positions[i]);
308 float dist(1.0f-(p-drag_offset_).mag()/get_radius());
313 (*iter)->set_trans_point(p+last_[i], time);
316 // then process non vertex and non position ducks
317 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
319 // skip this duck if it IS a vertex or a position
320 if (!((*iter)->get_type() != Duck::TYPE_VERTEX &&
321 (*iter)->get_type() != Duck::TYPE_POSITION))
323 Point p(positions[i]);
325 float dist(1.0f-(p-drag_offset_).mag()/get_radius());
330 (*iter)->set_trans_point(p+last_[i], time);
333 // then patch up the tangents for the vertices we've moved
334 duckmatic->update_ducks();
336 last_translate_=vect;
341 DuckDrag_SmoothMove::end_duck_drag(Duckmatic* duckmatic)
343 //synfig::info("end_duck_drag(): Diff= %f",last_translate_.mag());
344 if(last_translate_.mag()>0.0001)
346 const DuckList selected_ducks(duckmatic->get_selected_ducks());
347 DuckList::const_iterator iter;
351 smart_ptr<OneMoment> wait;if(selected_ducks.size()>20)wait.spawn();
353 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
355 if(last_[i].mag()>0.0001)
357 if ((*iter)->get_type() == Duck::TYPE_ANGLE)
359 if(!(*iter)->signal_edited_angle()((*iter)->get_rotations()))
361 throw String("Bad edit");
364 else if (App::restrict_radius_ducks &&
365 (*iter)->is_radius())
367 Point point((*iter)->get_point());
368 bool changed = false;
381 if (changed) (*iter)->set_point(point);
383 if(!(*iter)->signal_edited()(point))
385 throw String("Bad edit");
390 if(!(*iter)->signal_edited()((*iter)->get_point()))
392 throw String("Bad edit");
397 //duckmatic->get_selected_ducks()=new_set;
398 //duckmatic->refresh_selected_ducks();
403 duckmatic->signal_user_click_selected_ducks(0);