Fix the error about being unable to parse the grid_size metadata. It was previously...
[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 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include <gtkmm/dialog.h>
33 #include <gtkmm/entry.h>
34
35 #include <synfig/valuenode_dynamiclist.h>
36 #include <synfigapp/action_system.h>
37
38 #include "state_scale.h"
39 #include "canvasview.h"
40 #include "workarea.h"
41 #include "app.h"
42
43 #include <synfigapp/action.h>
44 #include "event_mouse.h"
45 #include "event_layerclick.h"
46 #include "toolbox.h"
47 #include "dialog_tooloptions.h"
48 #include <gtkmm/optionmenu.h>
49 #include "duck.h"
50 #include <synfigapp/main.h>
51
52 #include "general.h"
53
54 #endif
55
56 /* === U S I N G =========================================================== */
57
58 using namespace std;
59 using namespace etl;
60 using namespace synfig;
61 using namespace studio;
62
63 /* === M A C R O S ========================================================= */
64
65 /* === G L O B A L S ======================================================= */
66
67 StateScale studio::state_scale;
68
69 /* === C L A S S E S & S T R U C T S ======================================= */
70
71 class DuckDrag_Scale : public DuckDrag_Base
72 {
73
74         synfig::Vector last_scale;
75         synfig::Vector drag_offset;
76         synfig::Vector center;
77         synfig::Vector snap;
78
79         std::vector<synfig::Vector> positions;
80
81         bool move_only;
82
83         bool bad_drag;
84 public:
85         bool lock_aspect;
86         DuckDrag_Scale();
87         void begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& begin);
88         bool end_duck_drag(Duckmatic* duckmatic);
89         void duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector);
90 };
91
92
93 class studio::StateScale_Context : public sigc::trackable
94 {
95         etl::handle<CanvasView> canvas_view_;
96
97         synfigapp::Settings& settings;
98
99         etl::handle<DuckDrag_Scale> duck_dragger_;
100
101         Gtk::Table options_table;
102
103
104         Gtk::CheckButton checkbutton_aspect_lock;
105
106 public:
107
108         bool get_aspect_lock_flag()const { return checkbutton_aspect_lock.get_active(); }
109         void set_aspect_lock_flag(bool x) { return checkbutton_aspect_lock.set_active(x); refresh_aspect_lock_flag(); }
110
111         void refresh_aspect_lock_flag() { if(duck_dragger_)duck_dragger_->lock_aspect=get_aspect_lock_flag(); }
112
113         Smach::event_result event_refresh_tool_options(const Smach::event& x);
114
115         void refresh_tool_options();
116
117         StateScale_Context(CanvasView* canvas_view);
118
119         ~StateScale_Context();
120
121         const etl::handle<CanvasView>& get_canvas_view()const{return canvas_view_;}
122         etl::handle<synfigapp::CanvasInterface> get_canvas_interface()const{return canvas_view_->canvas_interface();}
123         synfig::Canvas::Handle get_canvas()const{return canvas_view_->get_canvas();}
124         WorkArea * get_work_area()const{return canvas_view_->get_work_area();}
125
126         void load_settings();
127         void save_settings();
128 };      // END of class StateScale_Context
129
130 /* === M E T H O D S ======================================================= */
131
132 StateScale::StateScale():
133         Smach::state<StateScale_Context>("scale")
134 {
135         insert(event_def(EVENT_REFRESH_TOOL_OPTIONS,&StateScale_Context::event_refresh_tool_options));
136 }
137
138 StateScale::~StateScale()
139 {
140 }
141
142 void
143 StateScale_Context::load_settings()
144 {
145         String value;
146
147         if(settings.get_value("scale.lock_aspect",value) && value=="0")
148                 set_aspect_lock_flag(false);
149         else
150                 set_aspect_lock_flag(true);
151 }
152
153 void
154 StateScale_Context::save_settings()
155 {
156         settings.set_value("scale.lock_aspect",get_aspect_lock_flag()?"1":"0");
157 }
158
159 StateScale_Context::StateScale_Context(CanvasView* canvas_view):
160         canvas_view_(canvas_view),
161         settings(synfigapp::Main::get_selected_input_device()->settings()),
162         duck_dragger_(new DuckDrag_Scale()),
163         checkbutton_aspect_lock(_("Lock Aspect Ratio"))
164 {
165         // Set up the tool options dialog
166         //options_table.attach(*manage(new Gtk::Label(_("Scale Tool"))), 0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
167         options_table.attach(checkbutton_aspect_lock, 0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
168
169         checkbutton_aspect_lock.signal_toggled().connect(sigc::mem_fun(*this,&StateScale_Context::refresh_aspect_lock_flag));
170
171         options_table.show_all();
172         refresh_tool_options();
173         App::dialog_tool_options->present();
174
175         get_work_area()->set_allow_layer_clicks(true);
176         get_work_area()->set_duck_dragger(duck_dragger_);
177
178 //      get_canvas_view()->work_area->set_cursor(Gdk::CROSSHAIR);
179         get_canvas_view()->work_area->reset_cursor();
180
181         App::toolbox->refresh();
182
183         set_aspect_lock_flag(true);
184         load_settings();
185 }
186
187 void
188 StateScale_Context::refresh_tool_options()
189 {
190         App::dialog_tool_options->clear();
191         App::dialog_tool_options->set_widget(options_table);
192         App::dialog_tool_options->set_local_name(_("Scale Tool"));
193         App::dialog_tool_options->set_name("scale");
194 }
195
196 Smach::event_result
197 StateScale_Context::event_refresh_tool_options(const Smach::event& /*x*/)
198 {
199         refresh_tool_options();
200         return Smach::RESULT_ACCEPT;
201 }
202
203 StateScale_Context::~StateScale_Context()
204 {
205         save_settings();
206
207         get_work_area()->clear_duck_dragger();
208         get_canvas_view()->work_area->reset_cursor();
209
210         App::dialog_tool_options->clear();
211
212         App::toolbox->refresh();
213 }
214
215
216
217
218 DuckDrag_Scale::DuckDrag_Scale():
219         lock_aspect(true)
220 {
221 }
222
223 #ifndef EPSILON
224 #define EPSILON 0.0000001
225 #endif
226
227 void
228 DuckDrag_Scale::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& offset)
229 {
230         last_scale=Vector(1,1);
231         const DuckList selected_ducks(duckmatic->get_selected_ducks());
232         DuckList::const_iterator iter;
233
234         //if(duckmatic->get_selected_ducks().size()<2)
235         //{
236         //      bad_drag=true;
237 //              return;
238 //      }
239         bad_drag=false;
240
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         // Calculate center
248         Point vmin(100000000,100000000);
249         Point vmax(-100000000,-100000000);
250         //std::set<etl::handle<Duck> >::iterator iter;
251         positions.clear();
252         int i;
253         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
254         {
255                 Point p((*iter)->get_trans_point());
256                 vmin[0]=min(vmin[0],p[0]);
257                 vmin[1]=min(vmin[1],p[1]);
258                 vmax[0]=max(vmax[0],p[0]);
259                 vmax[1]=max(vmax[1],p[1]);
260                 positions.push_back(p);
261         }
262         if((vmin-vmax).mag()<=EPSILON)
263                 move_only=true;
264         else
265                 move_only=false;
266
267         center=(vmin+vmax)*0.5;
268 }
269
270
271 void
272 DuckDrag_Scale::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
273 {
274         const DuckList selected_ducks(duckmatic->get_selected_ducks());
275         DuckList::const_iterator iter;
276
277         if(bad_drag)
278                 return;
279
280         //std::set<etl::handle<Duck> >::iterator iter;
281         synfig::Vector vect(duckmatic->snap_point_to_grid(vector)-center);
282         last_scale=vect;
283
284         if(move_only)
285         {
286                 int i;
287                 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
288                 {
289                         if(((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
290
291                         Vector p(positions[i]);
292
293                         p[0]+=vect[0];
294                         p[1]+=vect[1];
295                         (*iter)->set_trans_point(p);
296                 }
297                 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
298                 {
299                         if(!((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
300
301                         Vector p(positions[i]);
302
303                         p[0]+=vect[0];
304                         p[1]+=vect[1];
305                         (*iter)->set_trans_point(p);
306                 }
307                 return;
308         }
309
310         if(!lock_aspect)
311         {
312                 if(abs(drag_offset[0]-center[0])>EPSILON)
313                         vect[0]/=drag_offset[0]-center[0];
314                 else
315                         vect[0]=1;
316                 if(abs(drag_offset[1]-center[1])>EPSILON)
317                         vect[1]/=drag_offset[1]-center[1];
318                 else
319                         vect[1]=1;
320                 }
321         else
322         {
323                 //vect[0]=vect[1]=vect.mag()*0.707106781;
324                 Real amount(vect.mag()/(drag_offset-center).mag());
325                 vect[0]=vect[1]=amount;
326         }
327
328         if(vect[0]<EPSILON && vect[0]>-EPSILON)
329                 vect[0]=1;
330         if(vect[1]<EPSILON && vect[1]>-EPSILON)
331                 vect[1]=1;
332
333         int i;
334         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
335         {
336                 if(((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
337
338                 Vector p(positions[i]-center);
339
340                 p[0]*=vect[0];
341                 p[1]*=vect[1];
342                 p+=center;
343                 (*iter)->set_trans_point(p);
344         }
345         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
346         {
347                 if(!((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
348
349                 Vector p(positions[i]-center);
350
351                 p[0]*=vect[0];
352                 p[1]*=vect[1];
353                 p+=center;
354                 (*iter)->set_trans_point(p);
355         }
356
357         last_scale=vect;
358         //snap=Vector(0,0);
359 }
360
361 bool
362 DuckDrag_Scale::end_duck_drag(Duckmatic* duckmatic)
363 {
364         if(bad_drag)return false;
365
366         if((last_scale-Vector(1,1)).mag()>0.0001)
367         {
368                 duckmatic->signal_edited_selected_ducks();
369                 return true;
370         }
371         else
372         {
373                 duckmatic->signal_user_click_selected_ducks(0);
374                 return false;
375         }
376 }