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