Added my copyright line.
[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 #endif
54
55 /* === U S I N G =========================================================== */
56
57 using namespace std;
58 using namespace etl;
59 using namespace synfig;
60 using namespace studio;
61
62 /* === M A C R O S ========================================================= */
63
64 /* === G L O B A L S ======================================================= */
65
66 StateSmoothMove studio::state_smooth_move;
67
68 /* === C L A S S E S & S T R U C T S ======================================= */
69
70 class DuckDrag_SmoothMove : public DuckDrag_Base
71 {
72         float radius;
73
74         synfig::Vector last_translate_;
75         synfig::Vector drag_offset_;
76         synfig::Vector snap;
77
78         std::vector<synfig::Vector> last_;
79         std::vector<synfig::Vector> positions;
80
81 public:
82         DuckDrag_SmoothMove();
83         void begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& begin);
84         bool end_duck_drag(Duckmatic* duckmatic);
85         void duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector);
86
87         void set_radius(float x) { radius=x; }
88         float get_radius()const { return radius; }
89 };
90
91
92 class studio::StateSmoothMove_Context : public sigc::trackable
93 {
94         etl::handle<CanvasView> canvas_view_;
95
96         //Duckmatic::Push duckmatic_push;
97
98         synfigapp::Settings& settings;
99
100         etl::handle<DuckDrag_SmoothMove> duck_dragger_;
101
102         Gtk::Table options_table;
103
104         Gtk::Adjustment  adj_radius;
105         Gtk::SpinButton  spin_radius;
106
107         float pressure;
108
109 public:
110         float get_radius()const { return adj_radius.get_value(); }
111         void set_radius(float x) { return adj_radius.set_value(x); }
112
113         void refresh_radius() { duck_dragger_->set_radius(get_radius()*pressure); }
114
115         Smach::event_result event_stop_handler(const Smach::event& x);
116
117         Smach::event_result event_refresh_tool_options(const Smach::event& x);
118
119         void refresh_tool_options();
120
121         StateSmoothMove_Context(CanvasView* canvas_view);
122
123         ~StateSmoothMove_Context();
124
125         const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
126         etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
127         synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
128         WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
129
130         void load_settings();
131         void save_settings();
132 };      // END of class StateSmoothMove_Context
133
134 /* === M E T H O D S ======================================================= */
135
136 StateSmoothMove::StateSmoothMove():
137         Smach::state<StateSmoothMove_Context>("smooth_move")
138 {
139         insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateSmoothMove_Context::event_refresh_tool_options));
140 }
141
142 StateSmoothMove::~StateSmoothMove()
143 {
144 }
145
146 void
147 StateSmoothMove_Context::load_settings()
148 {
149         String value;
150
151         if(settings.get_value("smooth_move.radius",value))
152                 set_radius(atof(value.c_str()));
153         else
154                 set_radius(1.0f);
155 }
156
157 void
158 StateSmoothMove_Context::save_settings()
159 {
160         settings.set_value("smooth_move.radius",strprintf("%f",get_radius()));
161 }
162
163 StateSmoothMove_Context::StateSmoothMove_Context(CanvasView* canvas_view):
164         canvas_view_(canvas_view),
165 //      duckmatic_push(get_work_area()),
166         settings(synfigapp::Main::get_selected_input_device()->settings()),
167         duck_dragger_(new DuckDrag_SmoothMove()),
168         adj_radius(1,0,100000,0.01,0.1),
169         spin_radius(adj_radius,0.1,3)
170 {
171         pressure=1.0f;
172
173         // Set up the tool options dialog
174         //options_table.attach(*manage(new Gtk::Label(_("SmoothMove Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
175
176         options_table.attach(*manage(new Gtk::Label(_("Radius"))), 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
177         options_table.attach(spin_radius, 0, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
178
179         spin_radius.signal_value_changed().connect(sigc::mem_fun(*this,&StateSmoothMove_Context::refresh_radius));
180
181         options_table.show_all();
182         refresh_tool_options();
183         //App::dialog_tool_options->set_widget(options_table);
184         App::dialog_tool_options->present();
185
186         get_work_area()->allow_layer_clicks=true;
187         get_work_area()->set_duck_dragger(duck_dragger_);
188
189         App::toolbox->refresh();
190
191 //      get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
192         get_canvas_view()->work_area->reset_cursor();
193
194         load_settings();
195 }
196
197 void
198 StateSmoothMove_Context::refresh_tool_options()
199 {
200         App::dialog_tool_options->clear();
201         App::dialog_tool_options->set_widget(options_table);
202         App::dialog_tool_options->set_local_name(_("Smooth Move"));
203         App::dialog_tool_options->set_name("smooth_move");
204 }
205
206 Smach::event_result
207 StateSmoothMove_Context::event_refresh_tool_options(const Smach::event& x)
208 {
209         refresh_tool_options();
210         return Smach::RESULT_ACCEPT;
211 }
212
213 StateSmoothMove_Context::~StateSmoothMove_Context()
214 {
215         save_settings();
216
217         get_work_area()->clear_duck_dragger();
218         get_canvas_view()->work_area->reset_cursor();
219
220         App::dialog_tool_options->clear();
221
222         App::toolbox->refresh();
223 }
224
225
226
227
228 DuckDrag_SmoothMove::DuckDrag_SmoothMove():radius(1.0f)
229 {
230 }
231
232 void
233 DuckDrag_SmoothMove::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& offset)
234 {
235         last_translate_=Vector(0,0);
236                 drag_offset_=duckmatic->find_duck(offset)->get_trans_point();
237
238                 //snap=drag_offset-duckmatic->snap_point_to_grid(drag_offset);
239                 //snap=offset-drag_offset_;
240                 snap=Vector(0,0);
241
242         last_.clear();
243         positions.clear();
244         const DuckList selected_ducks(duckmatic->get_selected_ducks());
245         DuckList::const_iterator iter;
246         int i;
247         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
248         {
249                 last_.push_back(Vector(0,0));
250                 positions.push_back((*iter)->get_trans_point());
251         }
252 }
253
254 void
255 DuckDrag_SmoothMove::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
256 {
257         const DuckList selected_ducks(duckmatic->get_selected_ducks());
258         DuckList::const_iterator iter;
259         synfig::Vector vect(duckmatic->snap_point_to_grid(vector)-drag_offset_+snap);
260
261         int i;
262
263         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
264         {
265                 if(((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
266                 Point p(positions[i]);
267
268                 float dist(1.0f-(p-drag_offset_).mag()/get_radius());
269                 if(dist<0)
270                         dist=0;
271
272                 last_[i]=vect*dist;
273                 (*iter)->set_trans_point(p+last_[i]);
274         }
275
276         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
277         {
278                 if(!((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
279                 Point p(positions[i]);
280
281                 float dist(1.0f-(p-drag_offset_).mag()/get_radius());
282                 if(dist<0)
283                         dist=0;
284
285                 last_[i]=vect*dist;
286                 (*iter)->set_trans_point(p+last_[i]);
287         }
288
289         last_translate_=vect;
290         //snap=Vector(0,0);
291 }
292
293 bool
294 DuckDrag_SmoothMove::end_duck_drag(Duckmatic* duckmatic)
295 {
296         //synfig::info("end_duck_drag(): Diff= %f",last_translate_.mag());
297         if(last_translate_.mag()>0.0001)
298         {
299                 const DuckList selected_ducks(duckmatic->get_selected_ducks());
300                 DuckList::const_iterator iter;
301
302                 int i;
303
304                 smart_ptr<OneMoment> wait;if(selected_ducks.size()>20)wait.spawn();
305
306                 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
307                 {
308                         if(last_[i].mag()>0.0001)
309                                 if(!(*iter)->signal_edited()((*iter)->get_point()))
310                                 {
311                                         throw String("Bad Move");
312                                 }
313                 }
314                 //duckmatic->get_selected_ducks()=new_set;
315                 //duckmatic->refresh_selected_ducks();
316                 return true;
317         }
318         else
319         {
320                 duckmatic->signal_user_click_selected_ducks(0);
321                 return false;
322         }
323 }