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 "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 if(settings.get_value("smooth_move.radius",value))
161 set_radius(atof(value.c_str()));
167 StateSmoothMove_Context::save_settings()
169 settings.set_value("smooth_move.radius",strprintf("%f",get_radius()));
172 StateSmoothMove_Context::StateSmoothMove_Context(CanvasView* canvas_view):
173 canvas_view_(canvas_view),
174 is_working(*canvas_view),
175 // duckmatic_push(get_work_area()),
176 settings(synfigapp::Main::get_selected_input_device()->settings()),
177 duck_dragger_(new DuckDrag_SmoothMove()),
178 adj_radius(1,0,100000,0.01,0.1),
179 spin_radius(adj_radius,0.1,3)
183 // Set up the tool options dialog
184 options_table.attach(*manage(new Gtk::Label(_("SmoothMove Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
185 options_table.attach(*manage(new Gtk::Label(_("Radius"))), 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
186 options_table.attach(spin_radius, 0, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
188 spin_radius.signal_value_changed().connect(sigc::mem_fun(*this,&StateSmoothMove_Context::refresh_radius));
190 options_table.show_all();
191 refresh_tool_options();
192 //App::dialog_tool_options->set_widget(options_table);
193 App::dialog_tool_options->present();
195 get_work_area()->set_allow_layer_clicks(true);
196 get_work_area()->set_duck_dragger(duck_dragger_);
198 App::toolbox->refresh();
200 get_work_area()->set_cursor(Gdk::FLEUR);
201 //get_work_area()->reset_cursor();
207 StateSmoothMove_Context::refresh_tool_options()
209 App::dialog_tool_options->clear();
210 App::dialog_tool_options->set_widget(options_table);
211 App::dialog_tool_options->set_local_name(_("Smooth Move"));
212 App::dialog_tool_options->set_name("smooth_move");
216 StateSmoothMove_Context::event_refresh_tool_options(const Smach::event& /*x*/)
218 refresh_tool_options();
219 return Smach::RESULT_ACCEPT;
223 StateSmoothMove_Context::event_stop_handler(const Smach::event& /*x*/)
226 return Smach::RESULT_OK;
229 StateSmoothMove_Context::~StateSmoothMove_Context()
233 get_work_area()->clear_duck_dragger();
234 get_work_area()->reset_cursor();
236 App::dialog_tool_options->clear();
238 App::toolbox->refresh();
244 DuckDrag_SmoothMove::DuckDrag_SmoothMove():radius(1.0f)
249 DuckDrag_SmoothMove::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& offset)
251 last_translate_=Vector(0,0);
252 drag_offset_=duckmatic->find_duck(offset)->get_trans_point();
254 //snap=drag_offset-duckmatic->snap_point_to_grid(drag_offset);
255 //snap=offset-drag_offset_;
260 const DuckList selected_ducks(duckmatic->get_selected_ducks());
261 DuckList::const_iterator iter;
263 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
265 last_.push_back(Vector(0,0));
266 positions.push_back((*iter)->get_trans_point());
271 DuckDrag_SmoothMove::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
273 const DuckList selected_ducks(duckmatic->get_selected_ducks());
274 DuckList::const_iterator iter;
275 synfig::Vector vect(duckmatic->snap_point_to_grid(vector)-drag_offset_+snap);
279 Time time(duckmatic->get_time());
281 // process vertex and position ducks first
282 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
284 // skip this duck if it is NOT a vertex or a position
285 if (((*iter)->get_type() != Duck::TYPE_VERTEX &&
286 (*iter)->get_type() != Duck::TYPE_POSITION))
288 Point p(positions[i]);
290 float dist(1.0f-(p-drag_offset_).mag()/get_radius());
295 (*iter)->set_trans_point(p+last_[i], time);
298 // then process non vertex and non position ducks
299 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
301 // skip this duck if it IS a vertex or a position
302 if (!((*iter)->get_type() != Duck::TYPE_VERTEX &&
303 (*iter)->get_type() != Duck::TYPE_POSITION))
305 Point p(positions[i]);
307 float dist(1.0f-(p-drag_offset_).mag()/get_radius());
312 (*iter)->set_trans_point(p+last_[i], time);
315 // then patch up the tangents for the vertices we've moved
316 duckmatic->update_ducks();
318 last_translate_=vect;
323 DuckDrag_SmoothMove::end_duck_drag(Duckmatic* duckmatic)
325 //synfig::info("end_duck_drag(): Diff= %f",last_translate_.mag());
326 if(last_translate_.mag()>0.0001)
328 const DuckList selected_ducks(duckmatic->get_selected_ducks());
329 DuckList::const_iterator iter;
333 smart_ptr<OneMoment> wait;if(selected_ducks.size()>20)wait.spawn();
335 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
337 if(last_[i].mag()>0.0001)
339 if ((*iter)->get_type() == Duck::TYPE_ANGLE)
341 if(!(*iter)->signal_edited_angle()((*iter)->get_rotations()))
343 throw String("Bad edit");
346 else if (App::restrict_radius_ducks &&
347 (*iter)->is_radius())
349 Point point((*iter)->get_point());
350 bool changed = false;
363 if (changed) (*iter)->set_point(point);
365 if(!(*iter)->signal_edited()(point))
367 throw String("Bad edit");
372 if(!(*iter)->signal_edited()((*iter)->get_point()))
374 throw String("Bad edit");
379 //duckmatic->get_selected_ducks()=new_set;
380 //duckmatic->refresh_selected_ducks();
385 duckmatic->signal_user_click_selected_ducks(0);