Documentation for target_scanline.h
[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         String value;
151
152         if(settings.get_value("scale.lock_aspect",value) && value=="0")
153                 set_aspect_lock_flag(false);
154         else
155                 set_aspect_lock_flag(true);
156 }
157
158 void
159 StateScale_Context::save_settings()
160 {
161         settings.set_value("scale.lock_aspect",get_aspect_lock_flag()?"1":"0");
162 }
163
164 StateScale_Context::StateScale_Context(CanvasView* canvas_view):
165         canvas_view_(canvas_view),
166         is_working(*canvas_view),
167         settings(synfigapp::Main::get_selected_input_device()->settings()),
168         duck_dragger_(new DuckDrag_Scale()),
169         checkbutton_aspect_lock(_("Lock Aspect Ratio"))
170 {
171         // Set up the tool options dialog
172         options_table.attach(*manage(new Gtk::Label(_("Scale Tool"))),  0, 2, 0, 1, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
173         options_table.attach(checkbutton_aspect_lock,                                   0, 2, 1, 2, Gtk::EXPAND|Gtk::FILL, Gtk::EXPAND|Gtk::FILL, 0, 0);
174
175         checkbutton_aspect_lock.signal_toggled().connect(sigc::mem_fun(*this,&StateScale_Context::refresh_aspect_lock_flag));
176
177         options_table.show_all();
178         refresh_tool_options();
179         App::dialog_tool_options->present();
180
181         get_work_area()->set_allow_layer_clicks(true);
182         get_work_area()->set_duck_dragger(duck_dragger_);
183
184         get_work_area()->set_cursor(Gdk::SIZING);
185 //      get_work_area()->reset_cursor();
186
187         App::toolbox->refresh();
188
189         set_aspect_lock_flag(true);
190         load_settings();
191 }
192
193 void
194 StateScale_Context::refresh_tool_options()
195 {
196         App::dialog_tool_options->clear();
197         App::dialog_tool_options->set_widget(options_table);
198         App::dialog_tool_options->set_local_name(_("Scale Tool"));
199         App::dialog_tool_options->set_name("scale");
200 }
201
202 Smach::event_result
203 StateScale_Context::event_refresh_tool_options(const Smach::event& /*x*/)
204 {
205         refresh_tool_options();
206         return Smach::RESULT_ACCEPT;
207 }
208
209 Smach::event_result
210 StateScale_Context::event_stop_handler(const Smach::event& /*x*/)
211 {
212         throw &state_normal;
213         return Smach::RESULT_OK;
214 }
215
216 StateScale_Context::~StateScale_Context()
217 {
218         save_settings();
219
220         get_work_area()->clear_duck_dragger();
221         get_work_area()->reset_cursor();
222
223         App::dialog_tool_options->clear();
224
225         App::toolbox->refresh();
226 }
227
228
229
230
231 DuckDrag_Scale::DuckDrag_Scale():
232         lock_aspect(true)
233 {
234 }
235
236 #ifndef EPSILON
237 #define EPSILON 0.0000001
238 #endif
239
240 void
241 DuckDrag_Scale::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& offset)
242 {
243         last_scale=Vector(1,1);
244         const DuckList selected_ducks(duckmatic->get_selected_ducks());
245         DuckList::const_iterator iter;
246
247         //if(duckmatic->get_selected_ducks().size()<2)
248         //{
249         //      bad_drag=true;
250 //              return;
251 //      }
252         bad_drag=false;
253
254                 drag_offset=duckmatic->find_duck(offset)->get_trans_point();
255
256                 //snap=drag_offset-duckmatic->snap_point_to_grid(drag_offset);
257                 //snap=offset-drag_offset;
258                 snap=Vector(0,0);
259
260         // Calculate center
261         Point vmin(100000000,100000000);
262         Point vmax(-100000000,-100000000);
263         //std::set<etl::handle<Duck> >::iterator iter;
264         positions.clear();
265         int i;
266         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
267         {
268                 Point p((*iter)->get_trans_point());
269                 vmin[0]=min(vmin[0],p[0]);
270                 vmin[1]=min(vmin[1],p[1]);
271                 vmax[0]=max(vmax[0],p[0]);
272                 vmax[1]=max(vmax[1],p[1]);
273                 positions.push_back(p);
274         }
275         if((vmin-vmax).mag()<=EPSILON)
276                 move_only=true;
277         else
278                 move_only=false;
279
280         center=(vmin+vmax)*0.5;
281 }
282
283
284 void
285 DuckDrag_Scale::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
286 {
287         const DuckList selected_ducks(duckmatic->get_selected_ducks());
288         DuckList::const_iterator iter;
289
290         if(bad_drag)
291                 return;
292
293         //std::set<etl::handle<Duck> >::iterator iter;
294         synfig::Vector vect(duckmatic->snap_point_to_grid(vector)-center);
295         last_scale=vect;
296
297         if(move_only)
298         {
299                 int i;
300                 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
301                 {
302                         if(((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
303
304                         Vector p(positions[i]);
305
306                         p[0]+=vect[0];
307                         p[1]+=vect[1];
308                         (*iter)->set_trans_point(p);
309                 }
310                 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
311                 {
312                         if(!((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
313
314                         Vector p(positions[i]);
315
316                         p[0]+=vect[0];
317                         p[1]+=vect[1];
318                         (*iter)->set_trans_point(p);
319                 }
320                 return;
321         }
322
323         if(!lock_aspect)
324         {
325                 if(abs(drag_offset[0]-center[0])>EPSILON)
326                         vect[0]/=drag_offset[0]-center[0];
327                 else
328                         vect[0]=1;
329                 if(abs(drag_offset[1]-center[1])>EPSILON)
330                         vect[1]/=drag_offset[1]-center[1];
331                 else
332                         vect[1]=1;
333                 }
334         else
335         {
336                 //vect[0]=vect[1]=vect.mag()*0.707106781;
337                 Real amount(vect.mag()/(drag_offset-center).mag());
338                 vect[0]=vect[1]=amount;
339         }
340
341         if(vect[0]<EPSILON && vect[0]>-EPSILON)
342                 vect[0]=1;
343         if(vect[1]<EPSILON && vect[1]>-EPSILON)
344                 vect[1]=1;
345
346         int i;
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 p(positions[i]-center);
352
353                 p[0]*=vect[0];
354                 p[1]*=vect[1];
355                 p+=center;
356                 (*iter)->set_trans_point(p);
357         }
358         for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
359         {
360                 if(!((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
361
362                 Vector p(positions[i]-center);
363
364                 p[0]*=vect[0];
365                 p[1]*=vect[1];
366                 p+=center;
367                 (*iter)->set_trans_point(p);
368         }
369
370         last_scale=vect;
371         //snap=Vector(0,0);
372 }
373
374 bool
375 DuckDrag_Scale::end_duck_drag(Duckmatic* duckmatic)
376 {
377         if(bad_drag)return false;
378
379         if((last_scale-Vector(1,1)).mag()>0.0001)
380         {
381                 duckmatic->signal_edited_selected_ducks();
382                 return true;
383         }
384         else
385         {
386                 duckmatic->signal_user_click_selected_ducks(0);
387                 return false;
388         }
389 }