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 "canvasview.h"
47 #include <synfigapp/action.h>
48 #include "event_mouse.h"
49 #include "event_layerclick.h"
51 #include "dialog_tooloptions.h"
52 #include <gtkmm/optionmenu.h>
54 #include "onemoment.h"
55 #include <synfigapp/main.h>
61 /* === U S I N G =========================================================== */
65 using namespace synfig;
66 using namespace studio;
68 /* === M A C R O S ========================================================= */
70 /* === G L O B A L S ======================================================= */
72 StateSmoothMove studio::state_smooth_move;
74 /* === C L A S S E S & S T R U C T S ======================================= */
76 class DuckDrag_SmoothMove : public DuckDrag_Base
80 synfig::Vector last_translate_;
81 synfig::Vector drag_offset_;
84 std::vector<synfig::Vector> last_;
85 std::vector<synfig::Vector> positions;
88 DuckDrag_SmoothMove();
89 void begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& begin);
90 bool end_duck_drag(Duckmatic* duckmatic);
91 void duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector);
93 void set_radius(float x) { radius=x; }
94 float get_radius()const { return radius; }
98 class studio::StateSmoothMove_Context : public sigc::trackable
100 etl::handle<CanvasView> canvas_view_;
102 //Duckmatic::Push duckmatic_push;
104 synfigapp::Settings& settings;
106 etl::handle<DuckDrag_SmoothMove> duck_dragger_;
108 Gtk::Table options_table;
110 Gtk::Adjustment adj_radius;
111 Gtk::SpinButton spin_radius;
116 float get_radius()const { return adj_radius.get_value(); }
117 void set_radius(float x) { return adj_radius.set_value(x); }
119 void refresh_radius() { duck_dragger_->set_radius(get_radius()*pressure); }
121 Smach::event_result event_stop_handler(const Smach::event& x);
123 Smach::event_result event_refresh_tool_options(const Smach::event& x);
125 void refresh_tool_options();
127 StateSmoothMove_Context(CanvasView* canvas_view);
129 ~StateSmoothMove_Context();
131 const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
132 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
133 synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
134 WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
136 void load_settings();
137 void save_settings();
138 }; // END of class StateSmoothMove_Context
140 /* === M E T H O D S ======================================================= */
142 StateSmoothMove::StateSmoothMove():
143 Smach::state<StateSmoothMove_Context>("smooth_move")
145 insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateSmoothMove_Context::event_refresh_tool_options));
148 StateSmoothMove::~StateSmoothMove()
153 StateSmoothMove_Context::load_settings()
157 if(settings.get_value("smooth_move.radius",value))
158 set_radius(atof(value.c_str()));
164 StateSmoothMove_Context::save_settings()
166 settings.set_value("smooth_move.radius",strprintf("%f",get_radius()));
169 StateSmoothMove_Context::StateSmoothMove_Context(CanvasView* canvas_view):
170 canvas_view_(canvas_view),
171 // duckmatic_push(get_work_area()),
172 settings(synfigapp::Main::get_selected_input_device()->settings()),
173 duck_dragger_(new DuckDrag_SmoothMove()),
174 adj_radius(1,0,100000,0.01,0.1),
175 spin_radius(adj_radius,0.1,3)
179 // Set up the tool options dialog
180 options_table.attach(*manage(new Gtk::Label(_("SmoothMove Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
181 options_table.attach(*manage(new Gtk::Label(_("Radius"))), 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
182 options_table.attach(spin_radius, 0, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
184 spin_radius.signal_value_changed().connect(sigc::mem_fun(*this,&StateSmoothMove_Context::refresh_radius));
186 options_table.show_all();
187 refresh_tool_options();
188 //App::dialog_tool_options->set_widget(options_table);
189 App::dialog_tool_options->present();
191 get_work_area()->set_allow_layer_clicks(true);
192 get_work_area()->set_duck_dragger(duck_dragger_);
194 App::toolbox->refresh();
196 // get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
197 get_canvas_view()->work_area->reset_cursor();
203 StateSmoothMove_Context::refresh_tool_options()
205 App::dialog_tool_options->clear();
206 App::dialog_tool_options->set_widget(options_table);
207 App::dialog_tool_options->set_local_name(_("Smooth Move"));
208 App::dialog_tool_options->set_name("smooth_move");
212 StateSmoothMove_Context::event_refresh_tool_options(const Smach::event& /*x*/)
214 refresh_tool_options();
215 return Smach::RESULT_ACCEPT;
218 StateSmoothMove_Context::~StateSmoothMove_Context()
222 get_work_area()->clear_duck_dragger();
223 get_canvas_view()->work_area->reset_cursor();
225 App::dialog_tool_options->clear();
227 App::toolbox->refresh();
233 DuckDrag_SmoothMove::DuckDrag_SmoothMove():radius(1.0f)
238 DuckDrag_SmoothMove::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& offset)
240 last_translate_=Vector(0,0);
241 drag_offset_=duckmatic->find_duck(offset)->get_trans_point();
243 //snap=drag_offset-duckmatic->snap_point_to_grid(drag_offset);
244 //snap=offset-drag_offset_;
249 const DuckList selected_ducks(duckmatic->get_selected_ducks());
250 DuckList::const_iterator iter;
252 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
254 last_.push_back(Vector(0,0));
255 positions.push_back((*iter)->get_trans_point());
260 DuckDrag_SmoothMove::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
262 const DuckList selected_ducks(duckmatic->get_selected_ducks());
263 DuckList::const_iterator iter;
264 synfig::Vector vect(duckmatic->snap_point_to_grid(vector)-drag_offset_+snap);
268 Time time(duckmatic->get_time());
270 // process vertex and position ducks first
271 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
273 // skip this duck if it is NOT a vertex or a position
274 if (((*iter)->get_type() != Duck::TYPE_VERTEX &&
275 (*iter)->get_type() != Duck::TYPE_POSITION))
277 Point p(positions[i]);
279 float dist(1.0f-(p-drag_offset_).mag()/get_radius());
284 (*iter)->set_trans_point(p+last_[i], time);
287 // then process non vertex and non position ducks
288 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
290 // skip this duck if it IS a vertex or a position
291 if (!((*iter)->get_type() != Duck::TYPE_VERTEX &&
292 (*iter)->get_type() != Duck::TYPE_POSITION))
294 Point p(positions[i]);
296 float dist(1.0f-(p-drag_offset_).mag()/get_radius());
301 (*iter)->set_trans_point(p+last_[i], time);
304 // then patch up the tangents for the vertices we've moved
305 DuckList duck_list(duckmatic->get_duck_list());
306 for (iter=selected_ducks.begin(); iter!=selected_ducks.end(); ++iter)
308 etl::handle<Duck> duck(*iter);
309 if (duck->get_type() == Duck::TYPE_VERTEX || duck->get_type() == Duck::TYPE_POSITION)
311 ValueNode_Composite::Handle composite;
313 if ((ValueNode_BLineCalcVertex::Handle::cast_dynamic(duck->get_value_desc().get_value_node())) ||
314 ((composite = ValueNode_Composite::Handle::cast_dynamic(duck->get_value_desc().get_value_node())) &&
315 composite->get_type() == ValueBase::TYPE_BLINEPOINT &&
316 (ValueNode_BLineCalcVertex::Handle::cast_dynamic(composite->get_link("point")))))
318 //! \todo update() will call dynamic cast again, see if we can avoid that
319 DuckList::iterator iter;
320 for (iter=duck_list.begin(); iter!=duck_list.end(); iter++)
321 if ((*iter)->get_origin_duck()==duck
322 && std::find(selected_ducks.begin(),
323 selected_ducks.end(), *iter) == selected_ducks.end() )
324 (*iter)->update(time);
329 last_translate_=vect;
334 DuckDrag_SmoothMove::end_duck_drag(Duckmatic* duckmatic)
336 //synfig::info("end_duck_drag(): Diff= %f",last_translate_.mag());
337 if(last_translate_.mag()>0.0001)
339 const DuckList selected_ducks(duckmatic->get_selected_ducks());
340 DuckList::const_iterator iter;
344 smart_ptr<OneMoment> wait;if(selected_ducks.size()>20)wait.spawn();
346 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
348 if(last_[i].mag()>0.0001)
350 if ((*iter)->get_type() == Duck::TYPE_ANGLE)
352 if(!(*iter)->signal_edited_angle()((*iter)->get_rotations()))
354 throw String("Bad edit");
357 else if (App::restrict_radius_ducks &&
358 (*iter)->is_radius())
360 Point point((*iter)->get_point());
361 bool changed = false;
374 if (changed) (*iter)->set_point(point);
376 if(!(*iter)->signal_edited()(point))
378 throw String("Bad edit");
383 if(!(*iter)->signal_edited()((*iter)->get_point()))
385 throw String("Bad edit");
390 //duckmatic->get_selected_ducks()=new_set;
391 //duckmatic->refresh_selected_ducks();
396 duckmatic->signal_user_click_selected_ducks(0);