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