3b4f36ca08c4f16ac5c6d70797cedd06a17599dc
[synfig.git] / synfig-studio / src / gui / states / 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_blinecalcvertex.h>
38 #include <synfig/valuenode_composite.h>
39 #include <synfig/valuenode_dynamiclist.h>
40 #include <synfigapp/action_system.h>
41
42 #include "state_smoothmove.h"
43 #include "state_normal.h"
44 #include "canvasview.h"
45 #include "workarea.h"
46 #include "app.h"
47
48 #include <synfigapp/action.h>
49 #include "event_mouse.h"
50 #include "event_layerclick.h"
51 #include "toolbox.h"
52 #include "docks/dialog_tooloptions.h"
53 #include <gtkmm/optionmenu.h>
54 #include "duck.h"
55 #include "onemoment.h"
56 #include <synfigapp/main.h>
57
58 #include "general.h"
59
60 #endif
61
62 /* === U S I N G =========================================================== */
63
64 using namespace std;
65 using namespace etl;
66 using namespace synfig;
67 using namespace studio;
68
69 /* === M A C R O S ========================================================= */
70
71 /* === G L O B A L S ======================================================= */
72
73 StateSmoothMove studio::state_smooth_move;
74
75 /* === C L A S S E S & S T R U C T S ======================================= */
76
77 class DuckDrag_SmoothMove : public DuckDrag_Base
78 {
79         float radius;
80
81         synfig::Vector last_translate_;
82         synfig::Vector drag_offset_;
83         synfig::Vector snap;
84
85         std::vector<synfig::Vector> last_;
86         std::vector<synfig::Vector> positions;
87
88 public:
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);
93
94         void set_radius(float x) { radius=x; }
95         float get_radius()const { return radius; }
96 };
97
98
99 class studio::StateSmoothMove_Context : public sigc::trackable
100 {
101         etl::handle<CanvasView> canvas_view_;
102         CanvasView::IsWorking is_working;
103
104         //Duckmatic::Push duckmatic_push;
105
106         synfigapp::Settings& settings;
107
108         etl::handle<DuckDrag_SmoothMove> duck_dragger_;
109
110         Gtk::Table options_table;
111
112         Gtk::Adjustment  adj_radius;
113         Gtk::SpinButton  spin_radius;
114
115         float pressure;
116
117 public:
118         float get_radius()const { return adj_radius.get_value(); }
119         void set_radius(float x) { return adj_radius.set_value(x); }
120
121         void refresh_radius() { duck_dragger_->set_radius(get_radius()*pressure); }
122
123         Smach::event_result event_stop_handler(const Smach::event& x);
124
125         Smach::event_result event_refresh_tool_options(const Smach::event& x);
126
127         void refresh_tool_options();
128
129         StateSmoothMove_Context(CanvasView* canvas_view);
130
131         ~StateSmoothMove_Context();
132
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();}
137
138         void load_settings();
139         void save_settings();
140 };      // END of class StateSmoothMove_Context
141
142 /* === M E T H O D S ======================================================= */
143
144 StateSmoothMove::StateSmoothMove():
145         Smach::state<StateSmoothMove_Context>("smooth_move")
146 {
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));
149 }
150
151 StateSmoothMove::~StateSmoothMove()
152 {
153 }
154
155 void
156 StateSmoothMove_Context::load_settings()
157 {
158         try
159         {
160                 SETTINGS_LOCALE_SAFE_AND_BACKUP
161                 String value;
162
163                 if(settings.get_value("smooth_move.radius",value))
164                         set_radius(atof(value.c_str()));
165                 else
166                         set_radius(1.0f);
167                 SETTINGS_LOCALE_RESTORE
168         }
169         catch(...)
170         {
171                 synfig::warning("State SmothMove: Caught exception when attempting to load settings.");
172         }
173 }
174
175 void
176 StateSmoothMove_Context::save_settings()
177 {
178         try
179         {
180         SETTINGS_LOCALE_SAFE_AND_BACKUP
181                 settings.set_value("smooth_move.radius",strprintf("%f",get_radius()));
182         SETTINGS_LOCALE_RESTORE
183         }
184         catch(...)
185         {
186                 synfig::warning("State SmoothMove: Caught exception when attempting to save settings.");
187         }
188 }
189
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)
198 {
199         pressure=1.0f;
200
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);
205
206         spin_radius.signal_value_changed().connect(sigc::mem_fun(*this,&StateSmoothMove_Context::refresh_radius));
207
208         options_table.show_all();
209         refresh_tool_options();
210         //App::dialog_tool_options->set_widget(options_table);
211         App::dialog_tool_options->present();
212
213         get_work_area()->set_allow_layer_clicks(true);
214         get_work_area()->set_duck_dragger(duck_dragger_);
215
216         App::toolbox->refresh();
217
218         get_work_area()->set_cursor(Gdk::FLEUR);
219         //get_work_area()->reset_cursor();
220
221         load_settings();
222 }
223
224 void
225 StateSmoothMove_Context::refresh_tool_options()
226 {
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");
231 }
232
233 Smach::event_result
234 StateSmoothMove_Context::event_refresh_tool_options(const Smach::event& /*x*/)
235 {
236         refresh_tool_options();
237         return Smach::RESULT_ACCEPT;
238 }
239
240 Smach::event_result
241 StateSmoothMove_Context::event_stop_handler(const Smach::event& /*x*/)
242 {
243         throw &state_normal;
244         return Smach::RESULT_OK;
245 }
246
247 StateSmoothMove_Context::~StateSmoothMove_Context()
248 {
249         save_settings();
250
251         get_work_area()->clear_duck_dragger();
252         get_work_area()->reset_cursor();
253
254         App::dialog_tool_options->clear();
255
256         App::toolbox->refresh();
257 }
258
259
260
261
262 DuckDrag_SmoothMove::DuckDrag_SmoothMove():radius(1.0f)
263 {
264 }
265
266 void
267 DuckDrag_SmoothMove::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& offset)
268 {
269         last_translate_=Vector(0,0);
270                 drag_offset_=duckmatic->find_duck(offset)->get_trans_point();
271
272                 //snap=drag_offset-duckmatic->snap_point_to_grid(drag_offset);
273                 //snap=offset-drag_offset_;
274                 snap=Vector(0,0);
275
276         last_.clear();
277         positions.clear();
278         const DuckList selected_ducks(duckmatic->get_selected_ducks());
279         DuckList::const_iterator iter;
280         int i;
281         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
282         {
283                 last_.push_back(Vector(0,0));
284                 positions.push_back((*iter)->get_trans_point());
285         }
286 }
287
288 void
289 DuckDrag_SmoothMove::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
290 {
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);
294
295         int i;
296
297         Time time(duckmatic->get_time());
298
299         // process vertex and position ducks first
300         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
301         {
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))
305                         continue;
306                 Point p(positions[i]);
307
308                 float dist(1.0f-(p-drag_offset_).mag()/get_radius());
309                 if(dist<0)
310                         dist=0;
311
312                 last_[i]=vect*dist;
313                 (*iter)->set_trans_point(p+last_[i], time);
314         }
315
316         // then process non vertex and non position ducks
317         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
318         {
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))
322                         continue;
323                 Point p(positions[i]);
324
325                 float dist(1.0f-(p-drag_offset_).mag()/get_radius());
326                 if(dist<0)
327                         dist=0;
328
329                 last_[i]=vect*dist;
330                 (*iter)->set_trans_point(p+last_[i], time);
331         }
332
333         // then patch up the tangents for the vertices we've moved
334         duckmatic->update_ducks();
335
336         last_translate_=vect;
337         //snap=Vector(0,0);
338 }
339
340 bool
341 DuckDrag_SmoothMove::end_duck_drag(Duckmatic* duckmatic)
342 {
343         //synfig::info("end_duck_drag(): Diff= %f",last_translate_.mag());
344         if(last_translate_.mag()>0.0001)
345         {
346                 const DuckList selected_ducks(duckmatic->get_selected_ducks());
347                 DuckList::const_iterator iter;
348
349                 int i;
350
351                 smart_ptr<OneMoment> wait;if(selected_ducks.size()>20)wait.spawn();
352
353                 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
354                 {
355                         if(last_[i].mag()>0.0001)
356                                 {
357                                 if ((*iter)->get_type() == Duck::TYPE_ANGLE)
358                                         {
359                                                 if(!(*iter)->signal_edited_angle()((*iter)->get_rotations()))
360                                                 {
361                                                         throw String("Bad edit");
362                                                 }
363                                         }
364                                         else if (App::restrict_radius_ducks &&
365                                                          (*iter)->is_radius())
366                                         {
367                                                 Point point((*iter)->get_point());
368                                                 bool changed = false;
369
370                                                 if (point[0] < 0)
371                                                 {
372                                                         point[0] = 0;
373                                                         changed = true;
374                                                 }
375                                                 if (point[1] < 0)
376                                                 {
377                                                         point[1] = 0;
378                                                         changed = true;
379                                                 }
380
381                                                 if (changed) (*iter)->set_point(point);
382
383                                                 if(!(*iter)->signal_edited()(point))
384                                                 {
385                                                         throw String("Bad edit");
386                                                 }
387                                         }
388                                         else
389                                         {
390                                                 if(!(*iter)->signal_edited()((*iter)->get_point()))
391                                                 {
392                                                         throw String("Bad edit");
393                                                 }
394                                         }
395                                 }
396                 }
397                 //duckmatic->get_selected_ducks()=new_set;
398                 //duckmatic->refresh_selected_ducks();
399                 return true;
400         }
401         else
402         {
403                 duckmatic->signal_user_click_selected_ducks(0);
404                 return false;
405         }
406 }