78f617b22402a14109e13cf160045264686e7310
[synfig.git] / synfig-studio / src / gtkmm / state_smoothmove.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file state_smoothmove.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **  Copyright (c) 2008 Chris Moore
10 **      Copyright (c) 2009 Nikita Kitaev
11 **
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.
16 **
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.
21 **      \endlegal
22 */
23 /* ========================================================================= */
24
25 /* === H E A D E R S ======================================================= */
26
27 #ifdef USING_PCH
28 #       include "pch.h"
29 #else
30 #ifdef HAVE_CONFIG_H
31 #       include <config.h>
32 #endif
33
34 #include <gtkmm/dialog.h>
35 #include <gtkmm/entry.h>
36
37 #include <synfig/valuenode_dynamiclist.h>
38 #include <synfigapp/action_system.h>
39
40 #include "state_smoothmove.h"
41 #include "canvasview.h"
42 #include "workarea.h"
43 #include "app.h"
44
45 #include <synfigapp/action.h>
46 #include "event_mouse.h"
47 #include "event_layerclick.h"
48 #include "toolbox.h"
49 #include "dialog_tooloptions.h"
50 #include <gtkmm/optionmenu.h>
51 #include "duck.h"
52 #include "onemoment.h"
53 #include <synfigapp/main.h>
54
55 #include "general.h"
56
57 #endif
58
59 /* === U S I N G =========================================================== */
60
61 using namespace std;
62 using namespace etl;
63 using namespace synfig;
64 using namespace studio;
65
66 /* === M A C R O S ========================================================= */
67
68 /* === G L O B A L S ======================================================= */
69
70 StateSmoothMove studio::state_smooth_move;
71
72 /* === C L A S S E S & S T R U C T S ======================================= */
73
74 class DuckDrag_SmoothMove : public DuckDrag_Base
75 {
76         float radius;
77
78         synfig::Vector last_translate_;
79         synfig::Vector drag_offset_;
80         synfig::Vector snap;
81
82         std::vector<synfig::Vector> last_;
83         std::vector<synfig::Vector> positions;
84
85 public:
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);
90
91         void set_radius(float x) { radius=x; }
92         float get_radius()const { return radius; }
93 };
94
95
96 class studio::StateSmoothMove_Context : public sigc::trackable
97 {
98         etl::handle<CanvasView> canvas_view_;
99
100         //Duckmatic::Push duckmatic_push;
101
102         synfigapp::Settings& settings;
103
104         etl::handle<DuckDrag_SmoothMove> duck_dragger_;
105
106         Gtk::Table options_table;
107
108         Gtk::Adjustment  adj_radius;
109         Gtk::SpinButton  spin_radius;
110
111         float pressure;
112
113 public:
114         float get_radius()const { return adj_radius.get_value(); }
115         void set_radius(float x) { return adj_radius.set_value(x); }
116
117         void refresh_radius() { duck_dragger_->set_radius(get_radius()*pressure); }
118
119         Smach::event_result event_stop_handler(const Smach::event& x);
120
121         Smach::event_result event_refresh_tool_options(const Smach::event& x);
122
123         void refresh_tool_options();
124
125         StateSmoothMove_Context(CanvasView* canvas_view);
126
127         ~StateSmoothMove_Context();
128
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();}
133
134         void load_settings();
135         void save_settings();
136 };      // END of class StateSmoothMove_Context
137
138 /* === M E T H O D S ======================================================= */
139
140 StateSmoothMove::StateSmoothMove():
141         Smach::state<StateSmoothMove_Context>("smooth_move")
142 {
143         insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateSmoothMove_Context::event_refresh_tool_options));
144 }
145
146 StateSmoothMove::~StateSmoothMove()
147 {
148 }
149
150 void
151 StateSmoothMove_Context::load_settings()
152 {
153         String value;
154
155         if(settings.get_value("smooth_move.radius",value))
156                 set_radius(atof(value.c_str()));
157         else
158                 set_radius(1.0f);
159 }
160
161 void
162 StateSmoothMove_Context::save_settings()
163 {
164         settings.set_value("smooth_move.radius",strprintf("%f",get_radius()));
165 }
166
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)
174 {
175         pressure=1.0f;
176
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);
181
182         spin_radius.signal_value_changed().connect(sigc::mem_fun(*this,&StateSmoothMove_Context::refresh_radius));
183
184         options_table.show_all();
185         refresh_tool_options();
186         //App::dialog_tool_options->set_widget(options_table);
187         App::dialog_tool_options->present();
188
189         get_work_area()->set_allow_layer_clicks(true);
190         get_work_area()->set_duck_dragger(duck_dragger_);
191
192         App::toolbox->refresh();
193
194 //      get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
195         get_canvas_view()->work_area->reset_cursor();
196
197         load_settings();
198 }
199
200 void
201 StateSmoothMove_Context::refresh_tool_options()
202 {
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");
207 }
208
209 Smach::event_result
210 StateSmoothMove_Context::event_refresh_tool_options(const Smach::event& /*x*/)
211 {
212         refresh_tool_options();
213         return Smach::RESULT_ACCEPT;
214 }
215
216 StateSmoothMove_Context::~StateSmoothMove_Context()
217 {
218         save_settings();
219
220         get_work_area()->clear_duck_dragger();
221         get_canvas_view()->work_area->reset_cursor();
222
223         App::dialog_tool_options->clear();
224
225         App::toolbox->refresh();
226 }
227
228
229
230
231 DuckDrag_SmoothMove::DuckDrag_SmoothMove():radius(1.0f)
232 {
233 }
234
235 void
236 DuckDrag_SmoothMove::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& offset)
237 {
238         last_translate_=Vector(0,0);
239                 drag_offset_=duckmatic->find_duck(offset)->get_trans_point();
240
241                 //snap=drag_offset-duckmatic->snap_point_to_grid(drag_offset);
242                 //snap=offset-drag_offset_;
243                 snap=Vector(0,0);
244
245         last_.clear();
246         positions.clear();
247         const DuckList selected_ducks(duckmatic->get_selected_ducks());
248         DuckList::const_iterator iter;
249         int i;
250         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
251         {
252                 last_.push_back(Vector(0,0));
253                 positions.push_back((*iter)->get_trans_point());
254         }
255 }
256
257 void
258 DuckDrag_SmoothMove::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
259 {
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);
263
264         int i;
265
266         Time time(duckmatic->get_time());
267
268         // process vertex and position ducks first
269         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
270         {
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))
274                         continue;
275                 Point p(positions[i]);
276
277                 float dist(1.0f-(p-drag_offset_).mag()/get_radius());
278                 if(dist<0)
279                         dist=0;
280
281                 last_[i]=vect*dist;
282                 (*iter)->set_trans_point(p+last_[i], time);
283         }
284
285         // then process non vertex and non position ducks
286         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
287         {
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))
291                         continue;
292                 Point p(positions[i]);
293
294                 float dist(1.0f-(p-drag_offset_).mag()/get_radius());
295                 if(dist<0)
296                         dist=0;
297
298                 last_[i]=vect*dist;
299                 (*iter)->set_trans_point(p+last_[i], time);
300         }
301
302         last_translate_=vect;
303         //snap=Vector(0,0);
304 }
305
306 bool
307 DuckDrag_SmoothMove::end_duck_drag(Duckmatic* duckmatic)
308 {
309         //synfig::info("end_duck_drag(): Diff= %f",last_translate_.mag());
310         if(last_translate_.mag()>0.0001)
311         {
312                 const DuckList selected_ducks(duckmatic->get_selected_ducks());
313                 DuckList::const_iterator iter;
314
315                 int i;
316
317                 smart_ptr<OneMoment> wait;if(selected_ducks.size()>20)wait.spawn();
318
319                 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
320                 {
321                         if(last_[i].mag()>0.0001)
322                                 if(!(*iter)->signal_edited()((*iter)->get_point()))
323                                 {
324                                         throw String("Bad Move");
325                                 }
326                 }
327                 //duckmatic->get_selected_ducks()=new_set;
328                 //duckmatic->refresh_selected_ducks();
329                 return true;
330         }
331         else
332         {
333                 duckmatic->signal_user_click_selected_ducks(0);
334                 return false;
335         }
336 }