16355000ed84cdb137909b03eae098e17b5ac076
[synfig.git] /
1 /* === S Y N F I G ========================================================= */
2 /*!     \file state_rotate.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_rotate.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 <synfig/angle.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 #ifndef EPSILON
68 #define EPSILON 0.0000001
69 #endif
70
71 /* === G L O B A L S ======================================================= */
72
73 StateRotate studio::state_rotate;
74
75 /* === C L A S S E S & S T R U C T S ======================================= */
76
77 class DuckDrag_Rotate : public DuckDrag_Base
78 {
79
80         synfig::Vector last_rotate;
81         synfig::Vector drag_offset;
82         synfig::Vector center;
83         synfig::Vector snap;
84
85         Angle original_angle;
86         Real original_mag;
87
88         std::vector<synfig::Vector> positions;
89
90
91         bool bad_drag;
92         bool move_only;
93
94 public:
95         etl::handle<CanvasView> canvas_view_;
96         bool use_magnitude;
97         DuckDrag_Rotate();
98         void begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& begin);
99         bool end_duck_drag(Duckmatic* duckmatic);
100         void duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector);
101
102         etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
103 };
104
105
106 class studio::StateRotate_Context : public sigc::trackable
107 {
108         etl::handle<CanvasView> canvas_view_;
109
110         synfigapp::Settings& settings;
111
112         etl::handle<DuckDrag_Rotate> duck_dragger_;
113
114         Gtk::Table options_table;
115
116         Gtk::CheckButton checkbutton_scale;
117
118 public:
119
120         bool get_scale_flag()const { return checkbutton_scale.get_active(); }
121         void set_scale_flag(bool x) { return checkbutton_scale.set_active(x); refresh_scale_flag(); }
122
123
124         Smach::event_result event_refresh_tool_options(const Smach::event& x);
125
126         void refresh_tool_options();
127
128         void refresh_scale_flag() { if(duck_dragger_)duck_dragger_->use_magnitude=get_scale_flag(); }
129
130         StateRotate_Context(CanvasView* canvas_view);
131
132         ~StateRotate_Context();
133
134         const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
135         etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
136         synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
137         WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
138
139         void load_settings();
140         void save_settings();
141 };      // END of class StateRotate_Context
142
143 /* === M E T H O D S ======================================================= */
144
145 StateRotate::StateRotate():
146         Smach::state<StateRotate_Context>("rotate")
147 {
148         insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateRotate_Context::event_refresh_tool_options));
149 }
150
151 StateRotate::~StateRotate()
152 {
153 }
154
155 void
156 StateRotate_Context::load_settings()
157 {
158         String value;
159
160         if(settings.get_value("rotate.scale",value) && value=="0")
161                 set_scale_flag(false);
162         else
163                 set_scale_flag(true);
164 }
165
166 void
167 StateRotate_Context::save_settings()
168 {
169         settings.set_value("rotate.scale",get_scale_flag()?"1":"0");
170 }
171
172 StateRotate_Context::StateRotate_Context(CanvasView* canvas_view):
173         canvas_view_(canvas_view),
174         settings(synfigapp::Main::get_selected_input_device()->settings()),
175         duck_dragger_(new DuckDrag_Rotate()),
176         checkbutton_scale(_("Allow Scale"))
177 {
178         duck_dragger_->canvas_view_=get_canvas_view();
179
180         // Set up the tool options dialog
181         options_table.attach(*manage(new Gtk::Label(_("Rotate Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
182         options_table.attach(checkbutton_scale,                                                 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
183
184         checkbutton_scale.signal_toggled().connect(sigc::mem_fun(*this,&StateRotate_Context::refresh_scale_flag));
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 //      get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
195         get_canvas_view()->work_area->reset_cursor();
196
197         App::toolbox->refresh();
198
199         load_settings();
200         refresh_scale_flag();
201 }
202
203 void
204 StateRotate_Context::refresh_tool_options()
205 {
206         App::dialog_tool_options->clear();
207         App::dialog_tool_options->set_widget(options_table);
208         App::dialog_tool_options->set_local_name(_("Rotate Tool"));
209         App::dialog_tool_options->set_name("rotate");
210 }
211
212 Smach::event_result
213 StateRotate_Context::event_refresh_tool_options(const Smach::event& /*x*/)
214 {
215         refresh_tool_options();
216         return Smach::RESULT_ACCEPT;
217 }
218
219 StateRotate_Context::~StateRotate_Context()
220 {
221         save_settings();
222
223         get_work_area()->clear_duck_dragger();
224         get_canvas_view()->work_area->reset_cursor();
225
226         App::dialog_tool_options->clear();
227
228         App::toolbox->refresh();
229 }
230
231
232
233
234 DuckDrag_Rotate::DuckDrag_Rotate()
235 {
236         use_magnitude=true;
237 }
238
239 void
240 DuckDrag_Rotate::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& offset)
241 {
242         last_rotate=Vector(1,1);
243
244         const DuckList selected_ducks(duckmatic->get_selected_ducks());
245         DuckList::const_iterator iter;
246
247 /*
248         if(duckmatic->get_selected_ducks().size()<2)
249         {
250                 bad_drag=true;
251                 return;
252         }
253 */
254         bad_drag=false;
255
256                 drag_offset=duckmatic->find_duck(offset)->get_trans_point();
257
258                 //snap=drag_offset-duckmatic->snap_point_to_grid(drag_offset);
259                 //snap=offset-drag_offset;
260                 snap=Vector(0,0);
261
262         // Calculate center
263         Point vmin(100000000,100000000);
264         Point vmax(-100000000,-100000000);
265         //std::set<etl::handle<Duck> >::iterator iter;
266         positions.clear();
267         int i;
268         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
269         {
270                 Point p((*iter)->get_trans_point());
271                 vmin[0]=min(vmin[0],p[0]);
272                 vmin[1]=min(vmin[1],p[1]);
273                 vmax[0]=max(vmax[0],p[0]);
274                 vmax[1]=max(vmax[1],p[1]);
275                 positions.push_back(p);
276         }
277         center=(vmin+vmax)*0.5;
278         if((vmin-vmax).mag()<=EPSILON)
279                 move_only=true;
280         else
281                 move_only=false;
282
283
284         synfig::Vector vect(offset-center);
285         original_angle=Angle::tan(vect[1],vect[0]);
286         original_mag=vect.mag();
287 }
288
289
290 void
291 DuckDrag_Rotate::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
292 {
293         if(bad_drag)
294                 return;
295
296         //std::set<etl::handle<Duck> >::iterator iter;
297         synfig::Vector vect(duckmatic->snap_point_to_grid(vector)-center+snap);
298
299         const DuckList selected_ducks(duckmatic->get_selected_ducks());
300         DuckList::const_iterator iter;
301
302         if(move_only)
303         {
304                 int i;
305                 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
306                 {
307                         if((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION)continue;
308
309                         Vector p(positions[i]);
310
311                         p[0]+=vect[0];
312                         p[1]+=vect[1];
313                         (*iter)->set_trans_point(p);
314                 }
315                 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
316                 {
317                         if(!((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
318
319                         Vector p(positions[i]);
320
321                         p[0]+=vect[0];
322                         p[1]+=vect[1];
323                         (*iter)->set_trans_point(p);
324                 }
325                 return;
326         }
327
328         Angle::tan angle(vect[1],vect[0]);
329         angle=original_angle-angle;
330         Real mag(vect.mag()/original_mag);
331         Real sine(Angle::sin(angle).get());
332         Real cosine(Angle::cos(angle).get());
333
334         int i;
335         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
336         {
337                 if((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION)continue;
338
339                 Vector x(positions[i]-center),p;
340
341                 p[0]=cosine*x[0]+sine*x[1];
342                 p[1]=-sine*x[0]+cosine*x[1];
343                 if(use_magnitude)p*=mag;
344                 p+=center;
345                 (*iter)->set_trans_point(p);
346         }
347         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
348         {
349                 if(!((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
350
351                 Vector x(positions[i]-center),p;
352
353                 p[0]=cosine*x[0]+sine*x[1];
354                 p[1]=-sine*x[0]+cosine*x[1];
355                 if(use_magnitude)p*=mag;
356                 p+=center;
357                 (*iter)->set_trans_point(p);
358         }
359
360         last_rotate=vect;
361         //snap=Vector(0,0);
362 }
363
364 bool
365 DuckDrag_Rotate::end_duck_drag(Duckmatic* duckmatic)
366 {
367         if(bad_drag)return false;
368         if(move_only)
369         {
370                 synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Move Duck"));
371                 duckmatic->signal_edited_selected_ducks();
372                 return true;
373         }
374
375         synfigapp::Action::PassiveGrouper group(get_canvas_interface()->get_instance().get(),_("Rotate Ducks"));
376
377         if((last_rotate-Vector(1,1)).mag()>0.0001)
378         {
379                 duckmatic->signal_edited_selected_ducks();
380                 return true;
381         }
382         else
383         {
384                 duckmatic->signal_user_click_selected_ducks(0);
385                 return false;
386         }
387 }