Better functionality for ducks linked to BLine
[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 "canvasview.h"
44 #include "workarea.h"
45 #include "app.h"
46
47 #include <synfigapp/action.h>
48 #include "event_mouse.h"
49 #include "event_layerclick.h"
50 #include "toolbox.h"
51 #include "dialog_tooloptions.h"
52 #include <gtkmm/optionmenu.h>
53 #include "duck.h"
54 #include "onemoment.h"
55 #include <synfigapp/main.h>
56
57 #include "general.h"
58
59 #endif
60
61 /* === U S I N G =========================================================== */
62
63 using namespace std;
64 using namespace etl;
65 using namespace synfig;
66 using namespace studio;
67
68 /* === M A C R O S ========================================================= */
69
70 /* === G L O B A L S ======================================================= */
71
72 StateSmoothMove studio::state_smooth_move;
73
74 /* === C L A S S E S & S T R U C T S ======================================= */
75
76 class DuckDrag_SmoothMove : public DuckDrag_Base
77 {
78         float radius;
79
80         synfig::Vector last_translate_;
81         synfig::Vector drag_offset_;
82         synfig::Vector snap;
83
84         std::vector<synfig::Vector> last_;
85         std::vector<synfig::Vector> positions;
86
87 public:
88         DuckDrag_SmoothMove();
89         void begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& begin);
90         bool end_duck_drag(Duckmatic* duckmatic);
91         void duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector);
92
93         void set_radius(float x) { radius=x; }
94         float get_radius()const { return radius; }
95 };
96
97
98 class studio::StateSmoothMove_Context : public sigc::trackable
99 {
100         etl::handle<CanvasView> canvas_view_;
101
102         //Duckmatic::Push duckmatic_push;
103
104         synfigapp::Settings& settings;
105
106         etl::handle<DuckDrag_SmoothMove> duck_dragger_;
107
108         Gtk::Table options_table;
109
110         Gtk::Adjustment  adj_radius;
111         Gtk::SpinButton  spin_radius;
112
113         float pressure;
114
115 public:
116         float get_radius()const { return adj_radius.get_value(); }
117         void set_radius(float x) { return adj_radius.set_value(x); }
118
119         void refresh_radius() { duck_dragger_->set_radius(get_radius()*pressure); }
120
121         Smach::event_result event_stop_handler(const Smach::event& x);
122
123         Smach::event_result event_refresh_tool_options(const Smach::event& x);
124
125         void refresh_tool_options();
126
127         StateSmoothMove_Context(CanvasView* canvas_view);
128
129         ~StateSmoothMove_Context();
130
131         const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
132         etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
133         synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
134         WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
135
136         void load_settings();
137         void save_settings();
138 };      // END of class StateSmoothMove_Context
139
140 /* === M E T H O D S ======================================================= */
141
142 StateSmoothMove::StateSmoothMove():
143         Smach::state<StateSmoothMove_Context>("smooth_move")
144 {
145         insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateSmoothMove_Context::event_refresh_tool_options));
146 }
147
148 StateSmoothMove::~StateSmoothMove()
149 {
150 }
151
152 void
153 StateSmoothMove_Context::load_settings()
154 {
155         String value;
156
157         if(settings.get_value("smooth_move.radius",value))
158                 set_radius(atof(value.c_str()));
159         else
160                 set_radius(1.0f);
161 }
162
163 void
164 StateSmoothMove_Context::save_settings()
165 {
166         settings.set_value("smooth_move.radius",strprintf("%f",get_radius()));
167 }
168
169 StateSmoothMove_Context::StateSmoothMove_Context(CanvasView* canvas_view):
170         canvas_view_(canvas_view),
171 //      duckmatic_push(get_work_area()),
172         settings(synfigapp::Main::get_selected_input_device()->settings()),
173         duck_dragger_(new DuckDrag_SmoothMove()),
174         adj_radius(1,0,100000,0.01,0.1),
175         spin_radius(adj_radius,0.1,3)
176 {
177         pressure=1.0f;
178
179         // Set up the tool options dialog
180         options_table.attach(*manage(new Gtk::Label(_("SmoothMove Tool"))),     0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
181         options_table.attach(*manage(new Gtk::Label(_("Radius"))),                      0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
182         options_table.attach(spin_radius,                                                                       0, 2, 2, 3, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
183
184         spin_radius.signal_value_changed().connect(sigc::mem_fun(*this,&StateSmoothMove_Context::refresh_radius));
185
186         options_table.show_all();
187         refresh_tool_options();
188         //App::dialog_tool_options->set_widget(options_table);
189         App::dialog_tool_options->present();
190
191         get_work_area()->set_allow_layer_clicks(true);
192         get_work_area()->set_duck_dragger(duck_dragger_);
193
194         App::toolbox->refresh();
195
196 //      get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
197         get_canvas_view()->work_area->reset_cursor();
198
199         load_settings();
200 }
201
202 void
203 StateSmoothMove_Context::refresh_tool_options()
204 {
205         App::dialog_tool_options->clear();
206         App::dialog_tool_options->set_widget(options_table);
207         App::dialog_tool_options->set_local_name(_("Smooth Move"));
208         App::dialog_tool_options->set_name("smooth_move");
209 }
210
211 Smach::event_result
212 StateSmoothMove_Context::event_refresh_tool_options(const Smach::event& /*x*/)
213 {
214         refresh_tool_options();
215         return Smach::RESULT_ACCEPT;
216 }
217
218 StateSmoothMove_Context::~StateSmoothMove_Context()
219 {
220         save_settings();
221
222         get_work_area()->clear_duck_dragger();
223         get_canvas_view()->work_area->reset_cursor();
224
225         App::dialog_tool_options->clear();
226
227         App::toolbox->refresh();
228 }
229
230
231
232
233 DuckDrag_SmoothMove::DuckDrag_SmoothMove():radius(1.0f)
234 {
235 }
236
237 void
238 DuckDrag_SmoothMove::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& offset)
239 {
240         last_translate_=Vector(0,0);
241                 drag_offset_=duckmatic->find_duck(offset)->get_trans_point();
242
243                 //snap=drag_offset-duckmatic->snap_point_to_grid(drag_offset);
244                 //snap=offset-drag_offset_;
245                 snap=Vector(0,0);
246
247         last_.clear();
248         positions.clear();
249         const DuckList selected_ducks(duckmatic->get_selected_ducks());
250         DuckList::const_iterator iter;
251         int i;
252         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
253         {
254                 last_.push_back(Vector(0,0));
255                 positions.push_back((*iter)->get_trans_point());
256         }
257 }
258
259 void
260 DuckDrag_SmoothMove::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
261 {
262         const DuckList selected_ducks(duckmatic->get_selected_ducks());
263         DuckList::const_iterator iter;
264         synfig::Vector vect(duckmatic->snap_point_to_grid(vector)-drag_offset_+snap);
265
266         int i;
267
268         Time time(duckmatic->get_time());
269
270         // process vertex and position ducks first
271         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
272         {
273                 // skip this duck if it is NOT a vertex or a position
274                 if (((*iter)->get_type() != Duck::TYPE_VERTEX &&
275                          (*iter)->get_type() != Duck::TYPE_POSITION))
276                         continue;
277                 Point p(positions[i]);
278
279                 float dist(1.0f-(p-drag_offset_).mag()/get_radius());
280                 if(dist<0)
281                         dist=0;
282
283                 last_[i]=vect*dist;
284                 (*iter)->set_trans_point(p+last_[i], time);
285         }
286
287         // then process non vertex and non position ducks
288         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
289         {
290                 // skip this duck if it IS a vertex or a position
291                 if (!((*iter)->get_type() != Duck::TYPE_VERTEX &&
292                          (*iter)->get_type() != Duck::TYPE_POSITION))
293                         continue;
294                 Point p(positions[i]);
295
296                 float dist(1.0f-(p-drag_offset_).mag()/get_radius());
297                 if(dist<0)
298                         dist=0;
299
300                 last_[i]=vect*dist;
301                 (*iter)->set_trans_point(p+last_[i], time);
302         }
303
304         // then patch up the tangents for the vertices we've moved
305         DuckList duck_list(duckmatic->get_duck_list());
306         for (iter=selected_ducks.begin(); iter!=selected_ducks.end(); ++iter)
307         {
308                 etl::handle<Duck> duck(*iter);
309                 if (duck->get_type() == Duck::TYPE_VERTEX || duck->get_type() == Duck::TYPE_POSITION)
310                 {
311                         ValueNode_Composite::Handle composite;
312
313                         if ((ValueNode_BLineCalcVertex::Handle::cast_dynamic(duck->get_value_desc().get_value_node())) ||
314                                 ((composite = ValueNode_Composite::Handle::cast_dynamic(duck->get_value_desc().get_value_node())) &&
315                                  composite->get_type() == ValueBase::TYPE_BLINEPOINT &&
316                                  (ValueNode_BLineCalcVertex::Handle::cast_dynamic(composite->get_link("point")))))
317                         {
318                                 //! \todo update() will call dynamic cast again, see if we can avoid that
319                                 DuckList::iterator iter;
320                                 for (iter=duck_list.begin(); iter!=duck_list.end(); iter++)
321                                         if ((*iter)->get_origin_duck()==duck 
322                                                 && std::find(selected_ducks.begin(), 
323                                                                          selected_ducks.end(), *iter) == selected_ducks.end() )
324                                                 (*iter)->update(time);
325                         }
326                 }
327         }
328
329         last_translate_=vect;
330         //snap=Vector(0,0);
331 }
332
333 bool
334 DuckDrag_SmoothMove::end_duck_drag(Duckmatic* duckmatic)
335 {
336         //synfig::info("end_duck_drag(): Diff= %f",last_translate_.mag());
337         if(last_translate_.mag()>0.0001)
338         {
339                 const DuckList selected_ducks(duckmatic->get_selected_ducks());
340                 DuckList::const_iterator iter;
341
342                 int i;
343
344                 smart_ptr<OneMoment> wait;if(selected_ducks.size()>20)wait.spawn();
345
346                 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
347                 {
348                         if(last_[i].mag()>0.0001)
349                                 {
350                                 if ((*iter)->get_type() == Duck::TYPE_ANGLE)
351                                         {
352                                                 if(!(*iter)->signal_edited_angle()((*iter)->get_rotations()))
353                                                 {
354                                                         throw String("Bad edit");
355                                                 }
356                                         }
357                                         else if (App::restrict_radius_ducks &&
358                                                          (*iter)->is_radius())
359                                         {
360                                                 Point point((*iter)->get_point());
361                                                 bool changed = false;
362
363                                                 if (point[0] < 0)
364                                                 {
365                                                         point[0] = 0;
366                                                         changed = true;
367                                                 }
368                                                 if (point[1] < 0)
369                                                 {
370                                                         point[1] = 0;
371                                                         changed = true;
372                                                 }
373
374                                                 if (changed) (*iter)->set_point(point);
375
376                                                 if(!(*iter)->signal_edited()(point))
377                                                 {
378                                                         throw String("Bad edit");
379                                                 }
380                                         }
381                                         else
382                                         {
383                                                 if(!(*iter)->signal_edited()((*iter)->get_point()))
384                                                 {
385                                                         throw String("Bad edit");
386                                                 }
387                                         }
388                                 }
389                 }
390                 //duckmatic->get_selected_ducks()=new_set;
391                 //duckmatic->refresh_selected_ducks();
392                 return true;
393         }
394         else
395         {
396                 duckmatic->signal_user_click_selected_ducks(0);
397                 return false;
398         }
399 }