1 /* === S Y N F I G ========================================================= */
2 /*! \file state_scale.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2008 Chris Moore
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.
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.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
33 #include <gtkmm/dialog.h>
34 #include <gtkmm/entry.h>
36 #include <synfig/valuenode_dynamiclist.h>
37 #include <synfigapp/action_system.h>
39 #include "state_scale.h"
40 #include "state_normal.h"
41 #include "canvasview.h"
45 #include <synfigapp/action.h>
46 #include "event_mouse.h"
47 #include "event_layerclick.h"
49 #include "docks/dialog_tooloptions.h"
50 #include <gtkmm/optionmenu.h>
52 #include <synfigapp/main.h>
58 /* === U S I N G =========================================================== */
62 using namespace synfig;
63 using namespace studio;
65 /* === M A C R O S ========================================================= */
67 /* === G L O B A L S ======================================================= */
69 StateScale studio::state_scale;
71 /* === C L A S S E S & S T R U C T S ======================================= */
73 class DuckDrag_Scale : public DuckDrag_Base
76 synfig::Vector last_scale;
77 synfig::Vector drag_offset;
78 synfig::Vector center;
81 std::vector<synfig::Vector> positions;
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);
95 class studio::StateScale_Context : public sigc::trackable
97 etl::handle<CanvasView> canvas_view_;
98 CanvasView::IsWorking is_working;
100 synfigapp::Settings& settings;
102 etl::handle<DuckDrag_Scale> duck_dragger_;
104 Gtk::Table options_table;
107 Gtk::CheckButton checkbutton_aspect_lock;
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(); }
114 void refresh_aspect_lock_flag() { if(duck_dragger_)duck_dragger_->lock_aspect=get_aspect_lock_flag(); }
116 Smach::event_result event_stop_handler(const Smach::event& x);
117 Smach::event_result event_refresh_tool_options(const Smach::event& x);
119 void refresh_tool_options();
121 StateScale_Context(CanvasView* canvas_view);
123 ~StateScale_Context();
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();}
130 void load_settings();
131 void save_settings();
132 }; // END of class StateScale_Context
134 /* === M E T H O D S ======================================================= */
136 StateScale::StateScale():
137 Smach::state<StateScale_Context>("scale")
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));
143 StateScale::~StateScale()
148 StateScale_Context::load_settings()
152 if(settings.get_value("scale.lock_aspect",value) && value=="0")
153 set_aspect_lock_flag(false);
155 set_aspect_lock_flag(true);
159 StateScale_Context::save_settings()
161 settings.set_value("scale.lock_aspect",get_aspect_lock_flag()?"1":"0");
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"))
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);
175 checkbutton_aspect_lock.signal_toggled().connect(sigc::mem_fun(*this,&StateScale_Context::refresh_aspect_lock_flag));
177 options_table.show_all();
178 refresh_tool_options();
179 App::dialog_tool_options->present();
181 get_work_area()->set_allow_layer_clicks(true);
182 get_work_area()->set_duck_dragger(duck_dragger_);
184 get_work_area()->set_cursor(Gdk::SIZING);
185 // get_work_area()->reset_cursor();
187 App::toolbox->refresh();
189 set_aspect_lock_flag(true);
194 StateScale_Context::refresh_tool_options()
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");
203 StateScale_Context::event_refresh_tool_options(const Smach::event& /*x*/)
205 refresh_tool_options();
206 return Smach::RESULT_ACCEPT;
210 StateScale_Context::event_stop_handler(const Smach::event& /*x*/)
213 return Smach::RESULT_OK;
216 StateScale_Context::~StateScale_Context()
220 get_work_area()->clear_duck_dragger();
221 get_work_area()->reset_cursor();
223 App::dialog_tool_options->clear();
225 App::toolbox->refresh();
231 DuckDrag_Scale::DuckDrag_Scale():
237 #define EPSILON 0.0000001
241 DuckDrag_Scale::begin_duck_drag(Duckmatic* duckmatic, const synfig::Vector& offset)
243 last_scale=Vector(1,1);
244 const DuckList selected_ducks(duckmatic->get_selected_ducks());
245 DuckList::const_iterator iter;
247 //if(duckmatic->get_selected_ducks().size()<2)
254 drag_offset=duckmatic->find_duck(offset)->get_trans_point();
256 //snap=drag_offset-duckmatic->snap_point_to_grid(drag_offset);
257 //snap=offset-drag_offset;
261 Point vmin(100000000,100000000);
262 Point vmax(-100000000,-100000000);
263 //std::set<etl::handle<Duck> >::iterator iter;
266 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
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);
275 if((vmin-vmax).mag()<=EPSILON)
280 center=(vmin+vmax)*0.5;
285 DuckDrag_Scale::duck_drag(Duckmatic* duckmatic, const synfig::Vector& vector)
287 const DuckList selected_ducks(duckmatic->get_selected_ducks());
288 DuckList::const_iterator iter;
293 //std::set<etl::handle<Duck> >::iterator iter;
294 synfig::Vector vect(duckmatic->snap_point_to_grid(vector)-center);
300 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
302 if(((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
304 Vector p(positions[i]);
308 (*iter)->set_trans_point(p);
310 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
312 if(!((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
314 Vector p(positions[i]);
318 (*iter)->set_trans_point(p);
325 if(abs(drag_offset[0]-center[0])>EPSILON)
326 vect[0]/=drag_offset[0]-center[0];
329 if(abs(drag_offset[1]-center[1])>EPSILON)
330 vect[1]/=drag_offset[1]-center[1];
336 //vect[0]=vect[1]=vect.mag()*0.707106781;
337 Real amount(vect.mag()/(drag_offset-center).mag());
338 vect[0]=vect[1]=amount;
341 if(vect[0]<EPSILON && vect[0]>-EPSILON)
343 if(vect[1]<EPSILON && vect[1]>-EPSILON)
347 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
349 if(((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
351 Vector p(positions[i]-center);
356 (*iter)->set_trans_point(p);
358 for(i=0,iter=selected_ducks.begin();iter!=selected_ducks.end();++iter,i++)
360 if(!((*iter)->get_type()!=Duck::TYPE_VERTEX&&(*iter)->get_type()!=Duck::TYPE_POSITION))continue;
362 Vector p(positions[i]-center);
367 (*iter)->set_trans_point(p);
375 DuckDrag_Scale::end_duck_drag(Duckmatic* duckmatic)
377 if(bad_drag)return false;
379 if((last_scale-Vector(1,1)).mag()>0.0001)
381 duckmatic->signal_edited_selected_ducks();
386 duckmatic->signal_user_click_selected_ducks(0);