5b723fcf7c874c46376f71402b2529c1f365c267
[synfig.git] / synfig-studio / src / gui / states / state_scale.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file state_scale.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_scale.h"
40 #include "state_normal.h"
41 #include "canvasview.h"
42 #include "workarea.h"
43 #include "app.h"
44
45 #include <synfigapp/action.h>
46 #include "event_mouse.h"
47 #include "event_layerclick.h"
48 #include "toolbox.h"
49 #include "docks/dialog_tooloptions.h"
50 #include <gtkmm/optionmenu.h>
51 #include "duck.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 StateScale studio::state_scale;
70
71 /* === C L A S S E S & S T R U C T S ======================================= */
72
73 class DuckDrag_Scale : public DuckDrag_Base
74 {
75
76         synfig::Vector last_scale;
77         synfig::Vector drag_offset;
78         synfig::Vector center;
79         synfig::Vector snap;
80
81         std::vector<synfig::Vector> positions;
82
83         bool move_only;
84
85         bool bad_drag;
86 public:
87         bool lock_aspect;
88         DuckDrag_Scale();
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
94
95 class studio::StateScale_Context : public sigc::trackable
96 {
97         etl::handle<CanvasView> canvas_view_;
98         CanvasView::IsWorking is_working;
99
100         synfigapp::Settings& settings;
101
102         etl::handle<DuckDrag_Scale> duck_dragger_;
103
104         Gtk::Table options_table;
105
106
107         Gtk::CheckButton checkbutton_aspect_lock;
108
109 public:
110
111         bool get_aspect_lock_flag()const { return checkbutton_aspect_lock.get_active(); }
112         void set_aspect_lock_flag(bool x) { return checkbutton_aspect_lock.set_active(x); refresh_aspect_lock_flag(); }
113
114         void refresh_aspect_lock_flag() { if(duck_dragger_)duck_dragger_->lock_aspect=get_aspect_lock_flag(); }
115
116         Smach::event_result event_stop_handler(const Smach::event& x);
117         Smach::event_result event_refresh_tool_options(const Smach::event& x);
118
119         void refresh_tool_options();
120
121         StateScale_Context(CanvasView* canvas_view);
122
123         ~StateScale_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 StateScale_Context
133
134 /* === M E T H O D S ======================================================= */
135
136 StateScale::StateScale():
137         Smach::state<StateScale_Context>("scale")
138 {
139         insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateScale_Context::event_refresh_tool_options));
140         insert(event_def(EVENT_STOP,&StateScale_Context::event_stop_handler));
141 }
142
143 StateScale::~StateScale()
144 {
145 }
146
147 void
148 StateScale_Context::load_settings()
149 {
150         try
151         {
152                 SETTINGS_LOCALE_SAFE_AND_BACKUP
153                 String value;
154
155                 if(settings.get_value("scale.lock_aspect",value) && value=="0")
156                         set_aspect_lock_flag(false);
157                 else
158                         set_aspect_lock_flag(true);
159                 SETTINGS_LOCALE_RESTORE
160         }
161         catch(...)
162         {
163                 synfig::warning("State Scale: Caught exception when attempting to load settings.");
164         }
165 }
166
167 void
168 StateScale_Context::save_settings()
169 {
170         try
171         {
172                 SETTINGS_LOCALE_SAFE_AND_BACKUP
173                 settings.set_value("scale.lock_aspect",get_aspect_lock_flag()?"1":"0");
174                 SETTINGS_LOCALE_RESTORE
175         }
176         catch(...)
177         {
178                 synfig::warning("State Scale: Caught exception when attempting to save settings.");
179         }
180 }
181
182 StateScale_Context::StateScale_Context(CanvasView* canvas_view):
183         canvas_view_(canvas_view),
184         is_working(*canvas_view),
185         settings(synfigapp::Main::get_selected_input_device()->settings()),
186         duck_dragger_(new DuckDrag_Scale()),
187         checkbutton_aspect_lock(_("Lock Aspect Ratio"))
188 {
189         // Set up the tool options dialog
190         options_table.attach(*manage(new Gtk::Label(_("Scale Tool"))),  0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
191         options_table.attach(checkbutton_aspect_lock,                                   0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
192
193         checkbutton_aspect_lock.signal_toggled().connect(sigc::mem_fun(*this,&StateScale_Context::refresh_aspect_lock_flag));
194
195         options_table.show_all();
196         refresh_tool_options();
197         App::dialog_tool_options->present();
198
199         get_work_area()->set_allow_layer_clicks(true);
200         get_work_area()->set_duck_dragger(duck_dragger_);
201
202         get_work_area()->set_cursor(Gdk::SIZING);
203 //      get_work_area()->reset_cursor();
204
205         App::toolbox->refresh();
206
207         set_aspect_lock_flag(true);
208         load_settings();
209 }
210
211 void
212 StateScale_Context::refresh_tool_options()
213 {
214         App::dialog_tool_options->clear();
215         App::dialog_tool_options->set_widget(options_table);
216         App::dialog_tool_options->set_local_name(_("Scale Tool"));
217         App::dialog_tool_options->set_name("scale");
218 }
219
220 Smach::event_result
221 StateScale_Context::event_refresh_tool_options(const Smach::event& /*x*/)
222 {
223         refresh_tool_options();
224         return Smach::RESULT_ACCEPT;
225 }
226
227 Smach::event_result
228 StateScale_Context::event_stop_handler(const Smach::event& /*x*/)
229 {
230         throw &state_normal;
231         return Smach::RESULT_OK;
232 }
233
234 StateScale_Context::~StateScale_Context()
235 {
236         save_settings();
237
238         get_work_area()->clear_duck_dragger();
239         get_work_area()->reset_cursor();
240
241         App::dialog_tool_options->clear();
242
243         App::toolbox->refresh();
244 }
245
246
247
248
249 DuckDrag_Scale::DuckDrag_Scale():
250         lock_aspect(true)
251 {
252 }
253
254 #ifndef EPSILON
255 #define EPSILON 0.0000001
256 #endif
257
258 void
259 DuckDrag_Scale::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& offset)
260 {
261         last_scale=Vector(1,1);
262         const DuckList selected_ducks(duckmatic->get_selected_ducks());
263         DuckList::const_iterator iter;
264
265         //if(duckmatic->get_selected_ducks().size()<2)
266         //{
267         //      bad_drag=true;
268 //              return;
269 //      }
270         bad_drag=false;
271
272                 drag_offset=duckmatic->find_duck(offset)->get_trans_point();
273
274                 //snap=drag_offset-duckmatic->snap_point_to_grid(drag_offset);
275                 //snap=offset-drag_offset;
276                 snap=Vector(0,0);
277
278         // Calculate center
279         Point vmin(100000000,100000000);
280         Point vmax(-100000000,-100000000);
281         //std::set<etl::handle<Duck> >::iterator iter;
282         positions.clear();
283         int i;
284         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
285         {
286                 Point p((*iter)->get_trans_point());
287                 vmin[0]=min(vmin[0],p[0]);
288                 vmin[1]=min(vmin[1],p[1]);
289                 vmax[0]=max(vmax[0],p[0]);
290                 vmax[1]=max(vmax[1],p[1]);
291                 positions.push_back(p);
292         }
293         if((vmin-vmax).mag()<=EPSILON)
294                 move_only=true;
295         else
296                 move_only=false;
297
298         center=(vmin+vmax)*0.5;
299 }
300
301
302 void
303 DuckDrag_Scale::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
304 {
305         const DuckList selected_ducks(duckmatic->get_selected_ducks());
306         DuckList::const_iterator iter;
307
308         if(bad_drag)
309                 return;
310
311         //std::set<etl::handle<Duck> >::iterator iter;
312         synfig::Vector vect(duckmatic->snap_point_to_grid(vector)-center);
313         last_scale=vect;
314
315         if(move_only)
316         {
317                 int i;
318                 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
319                 {
320                         if(((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
321
322                         Vector p(positions[i]);
323
324                         p[0]+=vect[0];
325                         p[1]+=vect[1];
326                         (*iter)->set_trans_point(p);
327                 }
328                 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
329                 {
330                         if(!((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
331
332                         Vector p(positions[i]);
333
334                         p[0]+=vect[0];
335                         p[1]+=vect[1];
336                         (*iter)->set_trans_point(p);
337                 }
338                 return;
339         }
340
341         if(!lock_aspect)
342         {
343                 if(abs(drag_offset[0]-center[0])>EPSILON)
344                         vect[0]/=drag_offset[0]-center[0];
345                 else
346                         vect[0]=1;
347                 if(abs(drag_offset[1]-center[1])>EPSILON)
348                         vect[1]/=drag_offset[1]-center[1];
349                 else
350                         vect[1]=1;
351                 }
352         else
353         {
354                 //vect[0]=vect[1]=vect.mag()*0.707106781;
355                 Real amount(vect.mag()/(drag_offset-center).mag());
356                 vect[0]=vect[1]=amount;
357         }
358
359         if(vect[0]<EPSILON && vect[0]>-EPSILON)
360                 vect[0]=1;
361         if(vect[1]<EPSILON && vect[1]>-EPSILON)
362                 vect[1]=1;
363
364         int i;
365         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
366         {
367                 if(((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
368
369                 Vector p(positions[i]-center);
370
371                 p[0]*=vect[0];
372                 p[1]*=vect[1];
373                 p+=center;
374                 (*iter)->set_trans_point(p);
375         }
376         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
377         {
378                 if(!((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
379
380                 Vector p(positions[i]-center);
381
382                 p[0]*=vect[0];
383                 p[1]*=vect[1];
384                 p+=center;
385                 (*iter)->set_trans_point(p);
386         }
387
388         last_scale=vect;
389         //snap=Vector(0,0);
390 }
391
392 bool
393 DuckDrag_Scale::end_duck_drag(Duckmatic* duckmatic)
394 {
395         if(bad_drag)return false;
396
397         if((last_scale-Vector(1,1)).mag()>0.0001)
398         {
399                 duckmatic->signal_edited_selected_ducks();
400                 return true;
401         }
402         else
403         {
404                 duckmatic->signal_user_click_selected_ducks(0);
405                 return false;
406         }
407 }