0931f3fad5e00409a2a9cda3cc8a19c74cbfb306
[synfig.git] / synfig-studio / trunk / 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 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include <gtkmm/dialog.h>
33 #include <gtkmm/entry.h>
34
35 #include <synfig/valuenode_dynamiclist.h>
36 #include <synfigapp/action_system.h>
37
38 #include "state_smoothmove.h"
39 #include "canvasview.h"
40 #include "workarea.h"
41 #include "app.h"
42
43 #include <synfigapp/action.h>
44 #include "event_mouse.h"
45 #include "event_layerclick.h"
46 #include "toolbox.h"
47 #include "dialog_tooloptions.h"
48 #include <gtkmm/optionmenu.h>
49 #include "duck.h"
50 #include "onemoment.h"
51 #include <synfigapp/main.h>
52
53 #include "general.h"
54
55 #endif
56
57 /* === U S I N G =========================================================== */
58
59 using namespace std;
60 using namespace etl;
61 using namespace synfig;
62 using namespace studio;
63
64 /* === M A C R O S ========================================================= */
65
66 /* === G L O B A L S ======================================================= */
67
68 StateSmoothMove studio::state_smooth_move;
69
70 /* === C L A S S E S & S T R U C T S ======================================= */
71
72 class DuckDrag_SmoothMove : public DuckDrag_Base
73 {
74         float radius;
75
76         synfig::Vector last_translate_;
77         synfig::Vector drag_offset_;
78         synfig::Vector snap;
79
80         std::vector<synfig::Vector> last_;
81         std::vector<synfig::Vector> positions;
82
83 public:
84         DuckDrag_SmoothMove();
85         void begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& begin);
86         bool end_duck_drag(Duckmatic* duckmatic);
87         void duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector);
88
89         void set_radius(float x) { radius=x; }
90         float get_radius()const { return radius; }
91 };
92
93
94 class studio::StateSmoothMove_Context : public sigc::trackable
95 {
96         etl::handle<CanvasView> canvas_view_;
97
98         //Duckmatic::Push duckmatic_push;
99
100         synfigapp::Settings& settings;
101
102         etl::handle<DuckDrag_SmoothMove> duck_dragger_;
103
104         Gtk::Table options_table;
105
106         Gtk::Adjustment  adj_radius;
107         Gtk::SpinButton  spin_radius;
108
109         float pressure;
110
111 public:
112         float get_radius()const { return adj_radius.get_value(); }
113         void set_radius(float x) { return adj_radius.set_value(x); }
114
115         void refresh_radius() { duck_dragger_->set_radius(get_radius()*pressure); }
116
117         Smach::event_result event_stop_handler(const Smach::event& x);
118
119         Smach::event_result event_refresh_tool_options(const Smach::event& x);
120
121         void refresh_tool_options();
122
123         StateSmoothMove_Context(CanvasView* canvas_view);
124
125         ~StateSmoothMove_Context();
126
127         const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
128         etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
129         synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
130         WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
131
132         void load_settings();
133         void save_settings();
134 };      // END of class StateSmoothMove_Context
135
136 /* === M E T H O D S ======================================================= */
137
138 StateSmoothMove::StateSmoothMove():
139         Smach::state<StateSmoothMove_Context>("smooth_move")
140 {
141         insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateSmoothMove_Context::event_refresh_tool_options));
142 }
143
144 StateSmoothMove::~StateSmoothMove()
145 {
146 }
147
148 void
149 StateSmoothMove_Context::load_settings()
150 {
151         String value;
152
153         if(settings.get_value("smooth_move.radius",value))
154                 set_radius(atof(value.c_str()));
155         else
156                 set_radius(1.0f);
157 }
158
159 void
160 StateSmoothMove_Context::save_settings()
161 {
162         settings.set_value("smooth_move.radius",strprintf("%f",get_radius()));
163 }
164
165 StateSmoothMove_Context::StateSmoothMove_Context(CanvasView* canvas_view):
166         canvas_view_(canvas_view),
167 //      duckmatic_push(get_work_area()),
168         settings(synfigapp::Main::get_selected_input_device()->settings()),
169         duck_dragger_(new DuckDrag_SmoothMove()),
170         adj_radius(1,0,100000,0.01,0.1),
171         spin_radius(adj_radius,0.1,3)
172 {
173         pressure=1.0f;
174
175         // Set up the tool options dialog
176         options_table.attach(*manage(new Gtk::Label(_("SmoothMove Tool"))),     0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
177         options_table.attach(*manage(new Gtk::Label(_("Radius"))),                      0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
178         options_table.attach(spin_radius,                                                                       0, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
179
180         spin_radius.signal_value_changed().connect(sigc::mem_fun(*this,&StateSmoothMove_Context::refresh_radius));
181
182         options_table.show_all();
183         refresh_tool_options();
184         //App::dialog_tool_options->set_widget(options_table);
185         App::dialog_tool_options->present();
186
187         get_work_area()->set_allow_layer_clicks(true);
188         get_work_area()->set_duck_dragger(duck_dragger_);
189
190         App::toolbox->refresh();
191
192 //      get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
193         get_canvas_view()->work_area->reset_cursor();
194
195         load_settings();
196 }
197
198 void
199 StateSmoothMove_Context::refresh_tool_options()
200 {
201         App::dialog_tool_options->clear();
202         App::dialog_tool_options->set_widget(options_table);
203         App::dialog_tool_options->set_local_name(_("Smooth Move"));
204         App::dialog_tool_options->set_name("smooth_move");
205 }
206
207 Smach::event_result
208 StateSmoothMove_Context::event_refresh_tool_options(const Smach::event& /*x*/)
209 {
210         refresh_tool_options();
211         return Smach::RESULT_ACCEPT;
212 }
213
214 StateSmoothMove_Context::~StateSmoothMove_Context()
215 {
216         save_settings();
217
218         get_work_area()->clear_duck_dragger();
219         get_canvas_view()->work_area->reset_cursor();
220
221         App::dialog_tool_options->clear();
222
223         App::toolbox->refresh();
224 }
225
226
227
228
229 DuckDrag_SmoothMove::DuckDrag_SmoothMove():radius(1.0f)
230 {
231 }
232
233 void
234 DuckDrag_SmoothMove::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& offset)
235 {
236         last_translate_=Vector(0,0);
237                 drag_offset_=duckmatic->find_duck(offset)->get_trans_point();
238
239                 //snap=drag_offset-duckmatic->snap_point_to_grid(drag_offset);
240                 //snap=offset-drag_offset_;
241                 snap=Vector(0,0);
242
243         last_.clear();
244         positions.clear();
245         const DuckList selected_ducks(duckmatic->get_selected_ducks());
246         DuckList::const_iterator iter;
247         int i;
248         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
249         {
250                 last_.push_back(Vector(0,0));
251                 positions.push_back((*iter)->get_trans_point());
252         }
253 }
254
255 void
256 DuckDrag_SmoothMove::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
257 {
258         const DuckList selected_ducks(duckmatic->get_selected_ducks());
259         DuckList::const_iterator iter;
260         synfig::Vector vect(duckmatic->snap_point_to_grid(vector)-drag_offset_+snap);
261
262         int i;
263
264         // process vertex and position ducks first
265         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
266         {
267                 // skip this duck if it is NOT a vertex or a position
268                 if (((*iter)->get_type() != Duck::TYPE_VERTEX &&
269                          (*iter)->get_type() != Duck::TYPE_POSITION))
270                         continue;
271                 Point p(positions[i]);
272
273                 float dist(1.0f-(p-drag_offset_).mag()/get_radius());
274                 if(dist<0)
275                         dist=0;
276
277                 last_[i]=vect*dist;
278                 (*iter)->set_trans_point(p+last_[i]);
279         }
280
281         // then process non vertex and non position ducks
282         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
283         {
284                 // skip this duck if it IS 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]);
296         }
297
298         last_translate_=vect;
299         //snap=Vector(0,0);
300 }
301
302 bool
303 DuckDrag_SmoothMove::end_duck_drag(Duckmatic* duckmatic)
304 {
305         //synfig::info("end_duck_drag(): Diff= %f",last_translate_.mag());
306         if(last_translate_.mag()>0.0001)
307         {
308                 const DuckList selected_ducks(duckmatic->get_selected_ducks());
309                 DuckList::const_iterator iter;
310
311                 int i;
312
313                 smart_ptr<OneMoment> wait;if(selected_ducks.size()>20)wait.spawn();
314
315                 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
316                 {
317                         if(last_[i].mag()>0.0001)
318                                 if(!(*iter)->signal_edited()((*iter)->get_point()))
319                                 {
320                                         throw String("Bad Move");
321                                 }
322                 }
323                 //duckmatic->get_selected_ducks()=new_set;
324                 //duckmatic->refresh_selected_ducks();
325                 return true;
326         }
327         else
328         {
329                 duckmatic->signal_user_click_selected_ducks(0);
330                 return false;
331         }
332 }