Add actions (add to group, ,remove from group, export, un-export, set layer descripti...
[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_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 "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         String value;
159
160         if(settings.get_value("smooth_move.radius",value))
161                 set_radius(atof(value.c_str()));
162         else
163                 set_radius(1.0f);
164 }
165
166 void
167 StateSmoothMove_Context::save_settings()
168 {
169         settings.set_value("smooth_move.radius",strprintf("%f",get_radius()));
170 }
171
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)
180 {
181         pressure=1.0f;
182
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);
187
188         spin_radius.signal_value_changed().connect(sigc::mem_fun(*this,&StateSmoothMove_Context::refresh_radius));
189
190         options_table.show_all();
191         refresh_tool_options();
192         //App::dialog_tool_options->set_widget(options_table);
193         App::dialog_tool_options->present();
194
195         get_work_area()->set_allow_layer_clicks(true);
196         get_work_area()->set_duck_dragger(duck_dragger_);
197
198         App::toolbox->refresh();
199
200         get_work_area()->set_cursor(Gdk::FLEUR);
201         //get_work_area()->reset_cursor();
202
203         load_settings();
204 }
205
206 void
207 StateSmoothMove_Context::refresh_tool_options()
208 {
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");
213 }
214
215 Smach::event_result
216 StateSmoothMove_Context::event_refresh_tool_options(const Smach::event& /*x*/)
217 {
218         refresh_tool_options();
219         return Smach::RESULT_ACCEPT;
220 }
221
222 Smach::event_result
223 StateSmoothMove_Context::event_stop_handler(const Smach::event& /*x*/)
224 {
225         throw &state_normal;
226         return Smach::RESULT_OK;
227 }
228
229 StateSmoothMove_Context::~StateSmoothMove_Context()
230 {
231         save_settings();
232
233         get_work_area()->clear_duck_dragger();
234         get_work_area()->reset_cursor();
235
236         App::dialog_tool_options->clear();
237
238         App::toolbox->refresh();
239 }
240
241
242
243
244 DuckDrag_SmoothMove::DuckDrag_SmoothMove():radius(1.0f)
245 {
246 }
247
248 void
249 DuckDrag_SmoothMove::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& offset)
250 {
251         last_translate_=Vector(0,0);
252                 drag_offset_=duckmatic->find_duck(offset)->get_trans_point();
253
254                 //snap=drag_offset-duckmatic->snap_point_to_grid(drag_offset);
255                 //snap=offset-drag_offset_;
256                 snap=Vector(0,0);
257
258         last_.clear();
259         positions.clear();
260         const DuckList selected_ducks(duckmatic->get_selected_ducks());
261         DuckList::const_iterator iter;
262         int i;
263         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
264         {
265                 last_.push_back(Vector(0,0));
266                 positions.push_back((*iter)->get_trans_point());
267         }
268 }
269
270 void
271 DuckDrag_SmoothMove::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
272 {
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);
276
277         int i;
278
279         Time time(duckmatic->get_time());
280
281         // process vertex and position ducks first
282         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
283         {
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))
287                         continue;
288                 Point p(positions[i]);
289
290                 float dist(1.0f-(p-drag_offset_).mag()/get_radius());
291                 if(dist<0)
292                         dist=0;
293
294                 last_[i]=vect*dist;
295                 (*iter)->set_trans_point(p+last_[i], time);
296         }
297
298         // then process non vertex and non position ducks
299         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
300         {
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))
304                         continue;
305                 Point p(positions[i]);
306
307                 float dist(1.0f-(p-drag_offset_).mag()/get_radius());
308                 if(dist<0)
309                         dist=0;
310
311                 last_[i]=vect*dist;
312                 (*iter)->set_trans_point(p+last_[i], time);
313         }
314
315         // then patch up the tangents for the vertices we've moved
316         duckmatic->update_ducks();
317
318         last_translate_=vect;
319         //snap=Vector(0,0);
320 }
321
322 bool
323 DuckDrag_SmoothMove::end_duck_drag(Duckmatic* duckmatic)
324 {
325         //synfig::info("end_duck_drag(): Diff= %f",last_translate_.mag());
326         if(last_translate_.mag()>0.0001)
327         {
328                 const DuckList selected_ducks(duckmatic->get_selected_ducks());
329                 DuckList::const_iterator iter;
330
331                 int i;
332
333                 smart_ptr<OneMoment> wait;if(selected_ducks.size()>20)wait.spawn();
334
335                 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
336                 {
337                         if(last_[i].mag()>0.0001)
338                                 {
339                                 if ((*iter)->get_type() == Duck::TYPE_ANGLE)
340                                         {
341                                                 if(!(*iter)->signal_edited_angle()((*iter)->get_rotations()))
342                                                 {
343                                                         throw String("Bad edit");
344                                                 }
345                                         }
346                                         else if (App::restrict_radius_ducks &&
347                                                          (*iter)->is_radius())
348                                         {
349                                                 Point point((*iter)->get_point());
350                                                 bool changed = false;
351
352                                                 if (point[0] < 0)
353                                                 {
354                                                         point[0] = 0;
355                                                         changed = true;
356                                                 }
357                                                 if (point[1] < 0)
358                                                 {
359                                                         point[1] = 0;
360                                                         changed = true;
361                                                 }
362
363                                                 if (changed) (*iter)->set_point(point);
364
365                                                 if(!(*iter)->signal_edited()(point))
366                                                 {
367                                                         throw String("Bad edit");
368                                                 }
369                                         }
370                                         else
371                                         {
372                                                 if(!(*iter)->signal_edited()((*iter)->get_point()))
373                                                 {
374                                                         throw String("Bad edit");
375                                                 }
376                                         }
377                                 }
378                 }
379                 //duckmatic->get_selected_ducks()=new_set;
380                 //duckmatic->refresh_selected_ducks();
381                 return true;
382         }
383         else
384         {
385                 duckmatic->signal_user_click_selected_ducks(0);
386                 return false;
387         }
388 }