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_dynamiclist.h>
38 #include <synfigapp/action_system.h>
40 #include "state_smoothmove.h"
41 #include "canvasview.h"
45 #include <synfigapp/action.h>
46 #include "event_mouse.h"
47 #include "event_layerclick.h"
49 #include "dialog_tooloptions.h"
50 #include <gtkmm/optionmenu.h>
52 #include "onemoment.h"
53 #include <synfigapp/main.h>
59 /* === U S I N G =========================================================== */
63 using namespace synfig;
64 using namespace studio;
66 /* === M A C R O S ========================================================= */
68 /* === G L O B A L S ======================================================= */
70 StateSmoothMove studio::state_smooth_move;
72 /* === C L A S S E S & S T R U C T S ======================================= */
74 class DuckDrag_SmoothMove : public DuckDrag_Base
78 synfig::Vector last_translate_;
79 synfig::Vector drag_offset_;
82 std::vector<synfig::Vector> last_;
83 std::vector<synfig::Vector> positions;
86 DuckDrag_SmoothMove();
87 void begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& begin);
88 bool end_duck_drag(Duckmatic* duckmatic);
89 void duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector);
91 void set_radius(float x) { radius=x; }
92 float get_radius()const { return radius; }
96 class studio::StateSmoothMove_Context : public sigc::trackable
98 etl::handle<CanvasView> canvas_view_;
100 //Duckmatic::Push duckmatic_push;
102 synfigapp::Settings& settings;
104 etl::handle<DuckDrag_SmoothMove> duck_dragger_;
106 Gtk::Table options_table;
108 Gtk::Adjustment adj_radius;
109 Gtk::SpinButton spin_radius;
114 float get_radius()const { return adj_radius.get_value(); }
115 void set_radius(float x) { return adj_radius.set_value(x); }
117 void refresh_radius() { duck_dragger_->set_radius(get_radius()*pressure); }
119 Smach::event_result event_stop_handler(const Smach::event& x);
121 Smach::event_result event_refresh_tool_options(const Smach::event& x);
123 void refresh_tool_options();
125 StateSmoothMove_Context(CanvasView* canvas_view);
127 ~StateSmoothMove_Context();
129 const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
130 etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
131 synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
132 WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
134 void load_settings();
135 void save_settings();
136 }; // END of class StateSmoothMove_Context
138 /* === M E T H O D S ======================================================= */
140 StateSmoothMove::StateSmoothMove():
141 Smach::state<StateSmoothMove_Context>("smooth_move")
143 insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateSmoothMove_Context::event_refresh_tool_options));
146 StateSmoothMove::~StateSmoothMove()
151 StateSmoothMove_Context::load_settings()
155 if(settings.get_value("smooth_move.radius",value))
156 set_radius(atof(value.c_str()));
162 StateSmoothMove_Context::save_settings()
164 settings.set_value("smooth_move.radius",strprintf("%f",get_radius()));
167 StateSmoothMove_Context::StateSmoothMove_Context(CanvasView* canvas_view):
168 canvas_view_(canvas_view),
169 // duckmatic_push(get_work_area()),
170 settings(synfigapp::Main::get_selected_input_device()->settings()),
171 duck_dragger_(new DuckDrag_SmoothMove()),
172 adj_radius(1,0,100000,0.01,0.1),
173 spin_radius(adj_radius,0.1,3)
177 // Set up the tool options dialog
178 options_table.attach(*manage(new Gtk::Label(_("SmoothMove Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
179 options_table.attach(*manage(new Gtk::Label(_("Radius"))), 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
180 options_table.attach(spin_radius, 0, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
182 spin_radius.signal_value_changed().connect(sigc::mem_fun(*this,&StateSmoothMove_Context::refresh_radius));
184 options_table.show_all();
185 refresh_tool_options();
186 //App::dialog_tool_options->set_widget(options_table);
187 App::dialog_tool_options->present();
189 get_work_area()->set_allow_layer_clicks(true);
190 get_work_area()->set_duck_dragger(duck_dragger_);
192 App::toolbox->refresh();
194 // get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
195 get_canvas_view()->work_area->reset_cursor();
201 StateSmoothMove_Context::refresh_tool_options()
203 App::dialog_tool_options->clear();
204 App::dialog_tool_options->set_widget(options_table);
205 App::dialog_tool_options->set_local_name(_("Smooth Move"));
206 App::dialog_tool_options->set_name("smooth_move");
210 StateSmoothMove_Context::event_refresh_tool_options(const Smach::event& /*x*/)
212 refresh_tool_options();
213 return Smach::RESULT_ACCEPT;
216 StateSmoothMove_Context::~StateSmoothMove_Context()
220 get_work_area()->clear_duck_dragger();
221 get_canvas_view()->work_area->reset_cursor();
223 App::dialog_tool_options->clear();
225 App::toolbox->refresh();
231 DuckDrag_SmoothMove::DuckDrag_SmoothMove():radius(1.0f)
236 DuckDrag_SmoothMove::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& offset)
238 last_translate_=Vector(0,0);
239 drag_offset_=duckmatic->find_duck(offset)->get_trans_point();
241 //snap=drag_offset-duckmatic->snap_point_to_grid(drag_offset);
242 //snap=offset-drag_offset_;
247 const DuckList selected_ducks(duckmatic->get_selected_ducks());
248 DuckList::const_iterator iter;
250 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
252 last_.push_back(Vector(0,0));
253 positions.push_back((*iter)->get_trans_point());
258 DuckDrag_SmoothMove::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
260 const DuckList selected_ducks(duckmatic->get_selected_ducks());
261 DuckList::const_iterator iter;
262 synfig::Vector vect(duckmatic->snap_point_to_grid(vector)-drag_offset_+snap);
266 Time time(duckmatic->get_time());
268 // process vertex and position ducks first
269 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
271 // skip this duck if it is NOT a vertex or a position
272 if (((*iter)->get_type() != Duck::TYPE_VERTEX &&
273 (*iter)->get_type() != Duck::TYPE_POSITION))
275 Point p(positions[i]);
277 float dist(1.0f-(p-drag_offset_).mag()/get_radius());
282 (*iter)->set_trans_point(p+last_[i], time);
285 // then process non vertex and non position ducks
286 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
288 // skip this duck if it IS a vertex or a position
289 if (!((*iter)->get_type() != Duck::TYPE_VERTEX &&
290 (*iter)->get_type() != Duck::TYPE_POSITION))
292 Point p(positions[i]);
294 float dist(1.0f-(p-drag_offset_).mag()/get_radius());
299 (*iter)->set_trans_point(p+last_[i], time);
302 last_translate_=vect;
307 DuckDrag_SmoothMove::end_duck_drag(Duckmatic* duckmatic)
309 //synfig::info("end_duck_drag(): Diff= %f",last_translate_.mag());
310 if(last_translate_.mag()>0.0001)
312 const DuckList selected_ducks(duckmatic->get_selected_ducks());
313 DuckList::const_iterator iter;
317 smart_ptr<OneMoment> wait;if(selected_ducks.size()>20)wait.spawn();
319 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
321 if(last_[i].mag()>0.0001)
322 if(!(*iter)->signal_edited()((*iter)->get_point()))
324 throw String("Bad Move");
327 //duckmatic->get_selected_ducks()=new_set;
328 //duckmatic->refresh_selected_ducks();
333 duckmatic->signal_user_click_selected_ducks(0);